Skip to content
Simon Gene Gottlieb edited this page Feb 15, 2022 · 1 revision

Setting up cmake

To use gdb on your tools, you have to compile them with Debug Symbols. This can be done as:

cmake ../test/unit -DCMAKE_BUILD_TYPE=Debug

Now recompile your program and you are ready to go.

Content for your .gdbinit file

gdb has as many other tools, a special file, that it loads configs from. .gdbinit must be placed in your home directory. Example configuration

catch throw # Always stop the program, if an exception is being thrown
set print pretty on
set print thread-events off # Don't show when threads are being spawned/closed
set history save on         # Better save the history data
set history size 2048
set history remove-duplicates unlimited
set history filename ~/.gdbhistory # Save history in home folder
set width 0   # remove annoying messages, when terminal window is small
set height 0

gdb usage

Lets say you have a program with some flags ./my_program --file test.txt --param 2. To call this you have to call gdb --args ./my_program --file test.txt --param 2. This will start gdb, but not start the execution of your program. You will get a command line from gdb. Here you can execute different things:

  • (gdb) run (re-)start your program
  • (gdb) break file.cpp:123 will set a breakpoint in file.cpp on line 123
  • (gdb) bt print complete call stack
  • (gdb) l (list) prints code from current selected call stack
  • (gdb) up moves the call stack up
  • (gdb) down moves the call stack down
  • (gdb) p i prints the value of variable i
  • (gdb) p size() prints the value of the function size()
  • (gdb) c continues execution until next break point
  • (gdb) n execute the next line
  • (gdb) s steps into the next line (jumps into function calls)
  • (gdb) fin steps out of the current function (runs until next 'return')
  • (gdb) quit stop gdb (you can also just press Ctrl-D)
  • pressing Ctrl-C while your program is running, will pause the program and you can inspect it
Clone this wiki locally