GDB Cheatsheet

GDB is the GNU debugger which can be used to debug C and C++ programs at the commandline.

Compilation

  • Compile your code with debugging information to be able to use it with GDB. For GCC, this is the -g option.

Invocation

  • To open a program with GDB: gdb a.out

  • To open a program which has commandline arguments with GDB: gdb --args a.out --infile /home/joe/zoo.json

  • To load a core dump file, see here.

  • To debug a running process, use its PID. For example, to connect and debug a process with PID 945: gdb -p 945

Debugging

  • To start the program: r which is an alias for run.

  • To set a breakpoint: b which is an alias for breakpoint. Breakpoint can be set by specifying a line number (in the current file where the debugger has stopped) or function name or file path (partial or full) combined with line number or function name. For more info on locations that can be used with breakpoint command see here.

  • To continue from the current stopped location: c which is an alias for continue.

  • To execute next instruction n, step into the next instruction s and step out from a function finish.

  • To execute until a specified line number: until 420.

  • To set a breakpoint at every function in a given file: rbreak src/core/foo.cpp:. I find this incredibly useful when I start debugging a strange problem and I just to want to stop at every function in a file I suspect.

Information

  • To see list of breakpoints: info breakpoint or i b

  • To see list of shared library files that are loaded right now: info shared. Note that more shared libraries might be loaded during execution later.

  • To print value of a variable in the code right now: p foo_var

  • To print the data type of a variable or type in the code: ptype foo_type

Tried with: GDB 7.11.1 and Ubuntu 14.04

Advertisements

One thought on “GDB Cheatsheet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s