GDB is the GNU debugger which can be used to debug C and C++ programs at the commandline.
- Compile your code with debugging information to be able to use it with GDB. For GCC, this is the
- To open a program with GDB:
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
To start the program:
rwhich is an alias for run.
To set a breakpoint:
bwhich 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:
cwhich is an alias for continue.
To execute next instruction
n, step into the next instruction
sand step out from a function
To execute until a specified line number:
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.
To list source code:
To print the current method, line number and filename:
To see list of breakpoints:
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:
To print the data type of a variable or type in the code:
Tried with: GDB 7.11.1 and Ubuntu 14.04