📅 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.
-g
option.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 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 finish
.
To execute until a specified line number: until 420
.
To list source code around the current statement: l
To print the current method, line number and filename: frame
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: p/f foobar
.
To print the data type of a variable or type in the code: ptype foo_type
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 i b
To disable a breakpoint, use its listed number: disable 99
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 catch catch
.
To stop when a library is loaded, set a catchpoint: catch load
Tried with: GDB 8.1 and Ubuntu 18.04