# `gdb` Introduction and `openocd`
###### tags: `2022/12` `gdb`
:::info
++(2022/12/29)++ Following QEMU simulation for hardware boards, created another article to collect everything related to `gdb`. Can refer to another article, [QEMU simulation with hardware boards](https://hackmd.io/@MarconiJiang/qemu_hardware), which provides examples related to `qemu`, `gdb`, and `openocd`.
++latest update on 2022/12/29++
:::
---
**Table of Contents**
[TOC]
---
## ++gdb Introduction++
++**`gdb` related documents**++
+ `gdb` official documents
+ [Debugging with GDB - ftp.gnu.org](https://ftp.gnu.org/old-gnu/Manuals/gdb/html_chapter/gdb_toc.html) : GDB manual Ninth Edition, for GDB version 5.1.1, January 2002
+ [Debugging with GDB - sourceware.org](https://sourceware.org/gdb/onlinedocs/gdb/) : This is the Tenth Edition, of Debugging with GDB: the GNU Source-Level Debugger for GDB (GDB) Version 12.1.90.20221022-git. This document includes [Section 20.3.3 Monitor Commands for gdbserver](https://sourceware.org/gdb/onlinedocs/gdb/Server.html#Monitor-Commands-for-gdbserver-1)
+ `gdb` popluar commands and usages
+ ++**`(gdb) target extended-remote localhost:3333`**++ : connect to gdbserver. From previous example, when we launch `openocd`, the system output `Info : starting gdb server for stm32f1x.cpu on 3333` `Info : Listening on port 3333 for gdb connections`, showing it starts a gdb server on port 3333. (since we use the same PC, so `localhost` is used, instead of specific ip address. And `localhost` can be omitted.)
+ ++**`(gdb) disconnect`**++ : to disconntect from gdb server.
+ ++**`(gdb) monitor reset init`**++ : Reset and intialize the target hardware.
+ ++**`(gdb) run or r`**++ : `Run` the program continueously from the beginning. To stop, press `CTRL-c`.
+ ++**`(gdb) Continue or c`**++ : `Continue` the program continueously from where it stopped. To stop, press `CTRL-c`.
+ ++**`(gdb) Next or n`**++: `Next` step by sept to execute the program per line instruction. Complete the subroutine if next instruction is a subroutine.
+ ++**`(gdb) Step or s`**++: `Step` by step to execute the program per line instruction. Step into the subroutine if if next instruction is a subroutine.
+ ++**[monitor](https://sourceware.org/gdb/onlinedocs/gdb/Server.html#Monitor-Commands-for-gdbserver-1) command in `gdb` is a magic command.**++ You can use the monitor command to send special requests to gdbserver.
+ `(gdb) monitor help` : This is a `gdb` command to list the `openocd` commands supported. More details in next section of `openocd`.
+ `gdb` examples
+ [用Open Source工具開發軟體: 新軟體開發關念 - Chapter 6. 除錯工具 - GDB Basics](http://www.study-area.org/cyril/opentools/opentools/x1253.html)
++**`openocd` Related Documents**++
+ `openocd` official documents
+ [OpenOCD Concept Index](https://openocd.org/doc/html/OpenOCD-Concept-Index.html#OpenOCD-Concept-Index)
+ [OpenOCD User's Guide](https://openocd.org/doc/html/index.html#SEC_Contents)
+ [GDB and OpenOCD](https://openocd.org/doc/html/GDB-and-OpenOCD.html) : Specific instructions of `gdb` for `openocd`.
+ `openocd` popular commands and usage
+ [monitor](https://sourceware.org/gdb/onlinedocs/gdb/Server.html#Monitor-Commands-for-gdbserver-1) command in `gdb` is a magic command, which was introduced at previous `gdb` section. Can use `monitor + openocd` commands to control `openocd` and target hardware in `gdb`.
+ `(gdb) monitor help` : This is a `gdb` command to list the `openocd` commands supported.
+ `(gdb) monitor reset {run, halt, init}` : Reset the hardware target, then take next action of `run` (run the program), `halt` (halt the CPU), `init` (Initialize the target hardware system).
+ `(gdb) monitor soft_reset_halt`: Requesting target halt and executing a soft reset. This is often used when a target cannot be reset and halted. The target, after reset is released begins to execute code. OpenOCD attempts to stop the CPU and then sets the program counter back to the reset vector. Unfortunately the code that was executed may have left the hardware in an unknown state.
+ `(gdb) monitor flash help` to know more `flash` command provided by target hardware [about OpenOCD flash commands](https://openocd.org/doc/html/Flash-Commands.html#Flash-Commands).
+ OpenOCD has different commands for NOR and NAND flash; the “flash” command works with NOR flash, while the “nand” command works with NAND flash. This partially reflects different hardware technologies: NOR flash usually supports direct CPU instruction and data bus access, while data from a NAND flash must be copied to memory before it can be used. (SPI flash must also be copied to memory before use.) However, the documentation also uses “flash” as a generic term; for example, “Put flash configuration in board-specific files”.
+ `openocd` + `gdb` examples
+ [Stackoverflow - STM32 GDB/OpenOCD Commands and Initialization for Flash and Ram Debugging](https://stackoverflow.com/questions/5535110/stm32-gdb-openocd-commands-and-initialization-for-flash-and-ram-debugging) : One of the best answer to provide clear introduction of GDB/OpenOCD programming the ROM.
+ [Using GDB Server Monitor Commands from Eclipse GDB Console](https://mcuoneclipse.com/2018/12/01/using-gdb-server-monitor-commands-from-eclipse-gdb-console/)
---
## ++gdb++ - One Page gdb commands
++**A :**++ [Debugging with GDB - the GNU debugger GDB Tenth Edition: the GNU Source-Level Debugger for GDB Version 14.0.50.20230124-git.](https://sourceware.org/gdb/current/onlinedocs/gdb)
---
## ++gdb++ - Debugging multithreaded programs in GDB
++**A :**++ [Debugging multithreaded programs in GDB](https://undo.io/resources/gdb-watchpoint/debugging-multithreaded-programs-gdb/)
---
## ++References++
+ Check the References of [QEMU simulation with hardware boards](https://hackmd.io/@MarconiJiang/qemu_hardware)
+ OpenOCD official documents
+ [OpenOCD Concept Index](https://openocd.org/doc/html/OpenOCD-Concept-Index.html#OpenOCD-Concept-Index)
+ [OpenOCD User's Guide](https://openocd.org/doc/html/index.html#SEC_Contents)
+ [GDB and OpenOCD](https://openocd.org/doc/html/GDB-and-OpenOCD.html) : Specific instructions of `gdb` for `openocd`.
+ [OpenOCD General Commands](https://openocd.org/doc/html/General-Commands.html)
+ [GDB and OpenOCD](https://openocd.org/doc/html/GDB-and-OpenOCD.html) : Specific instructions of `gdb` for `openocd`
+ `gdb` and `openocd` examples
+ [Stackoverflow - STM32 GDB/OpenOCD Commands and Initialization for Flash and Ram Debugging](https://stackoverflow.com/questions/5535110/stm32-gdb-openocd-commands-and-initialization-for-flash-and-ram-debugging) : One of the best answer to provide clear introduction of GDB/OpenOCD programming the ROM.
+ [Upload Code to STM32L4, Using Linux, GNU Make, and OpenOCD](https://www.hackster.io/yusefkarim/upload-code-to-stm32l4-using-linux-gnu-make-and-openocd-a3d4de)
+ [GDB example on x86 - 透過 GDB 進行遠端除錯 by jserv](https://hackmd.io/@sysprog/gdb-example)
+ [使用 gdb+qemu 來執行/除錯 raspberry pi linux kernel](https://descent-incoming.blogspot.com/2017/03/raspberry-pix86-linux-kernel-debug-by.html)
+ GDB official documents and examples
+ [Debugging with GDB - ftp.gnu.org](https://ftp.gnu.org/old-gnu/Manuals/gdb/html_chapter/gdb_toc.html) : GDB manual Ninth Edition, for GDB version 5.1.1, January 2002
+ [Debugging with GDB - sourceware.org](https://sourceware.org/gdb/onlinedocs/gdb/) : This is the Tenth Edition, of Debugging with GDB: the GNU Source-Level Debugger for GDB (GDB) Version 12.1.90.20221022-git. This document includes [Section 20.3.3 Monitor Commands for gdbserver](https://sourceware.org/gdb/onlinedocs/gdb/Server.html#Monitor-Commands-for-gdbserver-1)
+ `gdb` examples
+ [用Open Source工具開發軟體: 新軟體開發關念 - Chapter 6. 除錯工具 - GDB Basics (and Advanced)](http://www.study-area.org/cyril/opentools/opentools/x1253.html) : Check link above for OpenOCD specific commands for GDB.
+ [GDB實用教學:自動化你的debug](https://jasonblog.github.io/note/gdb/gdbshi_yong_jiao_xue_ff1a_zi_dong_hua_ni_de_debug.html) : `gdb -tui` or `cgdb` for Text UI or GUI operation.
---
[:arrow_left:Previous article - QEMU simulation with hardware boards](https://hackmd.io/@MarconiJiang/qemu_hardware)
[:arrow_right:Next article - back to marconi's blog](https://marconi1964.github.io/)
[:arrow_up:back to marconi's blog](https://marconi1964.github.io/)