GDB

gdb

  • make sure debug flag is enabled when compiling the program.

    • e.g., gcc -g

    • e.g., set(CMAKE_BUILD_TYPE RelWithDebInfo) or cmake -DCMAKE_BUILD_TYPE=Debug

  • 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

  • (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

  • (gdb) pvector <name> print information about the STL vector class std::vector<T>

  • (gdb) pmap <name> print information about the STL map class std::map<T,T>

references: gdb cheatsheet; gdb commands; gdb STL support; download stl-views-1.0.3.gdb;

Last updated