📅 2017-Aug-22 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ cheatsheet, gdb ⬩ 📚 Archive
GDB is the GNU debugger which can be used to debug C and C++ programs at the commandline.
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 my post.
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:
r which is an alias for run.
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
To execute until a specified line number:
To list source code around the current statement:
To print the current method, line number and filename:
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 foobar. To print value in hex:
p/x foobar. To print value in binary:
p/t foobar. To print value in float:
To print the data type of a variable or type in the code:
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 see list of breakpoints:
info breakpoint or
To disable a breakpoint, use its listed number:
To set a conditional breakpoint:
b foobar.cpp:35 if x > 5
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 stop when an exception is thrown or caught, set a catchpoint:
catch throw and
To stop when a library is loaded, set a catchpoint:
Tried with: GDB 8.1 and Ubuntu 18.04