# GDB

## gdb

* make sure debug flag is enabled when compiling the program.&#x20;
  * e.g., `gcc -g`&#x20;
  * e.g., `set(CMAKE_BUILD_TYPE RelWithDebInfo)` or `cmake -DCMAKE_BUILD_TYPE=Debug`&#x20;
* `gdb <program_name>`
* `gdb --args <program_name> arg1 arg2 arg3`
* `(gdb) break main` add a breakpoint at `main` function
* `(gdb) r` run the program
* `(gdb) list` list current function (in its original programming language)
* `(gdb) tbreak 48` add a temporary breakpoint at line 48 of current function
* `(gdb) c` continue running the program
* `(gdb) bt` print backtrace (of current stack)
* `(gdb) print <var>` print a variable
* `(gdb) info locals` print all local variables (of current stack)
* `(gdb) x /x 0x7fffffffd9b0` exam the memory at this address using hex format
* `(gdb) q` quit

More useful commands

* `(gdb) s` step into next function
* `(gdb) n` go to next instruction
* `(gdb) bt full` print full stack info including variables
* `(gdb) info args` print local `argc` and `argv` information&#x20;
* `(gdb) whatis v` check variable type
* `(gdb) disas` disassemble current function into assembly language
* `(gdb) list <filename:function_name>` list a function
* `(gdb) list <filename:line_number>` list a file around this line number
* `(gdb) tbreak 0x000055555576bf00` add a temporary breakpoint at the address (of an instruction)
* `(gdb) x /2xw 0xffff` print two-word length of the memory at this address using hex format
  * `b` byte, `h` half-word (2 bytes), `w` word (4 bytes), `g` giant word (8 bytes)
  * `x` hex, `a` pointer, `c` char, `d` integer, `u` uint, `s` string, `t` binary, `f` float, `o` octal

Fancy usage of extended gdb commands

* need to download and `source stl-views-1.0.3.gdb` ; can be added to `~/.gdbinit`&#x20;
* `(gdb) pvector <name>` print information about the STL vector class `std::vector<T>`&#x20;
* `(gdb) pmap <name>` print information about the STL map class `std::map<T,T>`&#x20;

references: [gdb cheatsheet](https://darkdust.net/files/GDB%20Cheat%20Sheet.pdf); [gdb commands](http://www.yolinux.com/TUTORIALS/GDB-Commands.html); [gdb STL support](https://sourceware.org/gdb/wiki/STLSupport); [download stl-views-1.0.3.gdb](https://sourceware.org/gdb/wiki/STLSupport?action=AttachFile\&do=view\&target=stl-views-1.0.3.gdb);&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wiki.hanzheteng.com/development/linux/gdb.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
