Generate DWARF debugging information in an executable?
To debug with
gdb
, compile with-g
. This generates DWARF information, which is used by the debugger to understand what the program is doing.gcc -g -o program source.cUse
-O
to compile with optimizations, but this can adversely affect the debug experience i.e.-O
and-g
are orthogonal and independent. To get a reasonable level of performance and a good debug experience, use-Og
. And to get an even better debug experience, use-g3
rather than just-g
.gcc -g3 -Og -o program source.cYou can use the readelf utility to look at the debug info.
Resources
- GDB Tips by Greg Law - Tip #1
Start the text user interface in gdb?
TUI (Text User Interface) mode allows you to see the context of what you’re debugging!
gdb -tui ./executableYou can also type
layout src
to switch to the TUI mode.layout srcThe shortcut
ctrl–x a
will also start the TUI.
ctrl+l
to redraw the screen reset in TUI mode.
Resources
- GDB Tips by Greg Law - Tip #2
Enter SingleKey mode?
Press
ctrl-x s
in TUI mode to enable SingleKey mode. In this mode you can user
torun
,c
tocontinue
,s
forstep
, andq
to leave SingleKey mode, without pressing enter.
Resources
- GDB Tips by Greg Law - Tip #65
Cycle through assembly and source code windows in GDB TUI?
ctrl-x 2
Resources
- GDB Tips by Greg Law - Tip #3
Switch focus between GDB TUI windows?
Description
(gdb) focus src (gdb) focus cmd
Resources
- GDB Tips by Greg Law - Tip #3
Navigate through command history?
Description
ctrl-p
for previous commandsctrl-n
for next commands
Resources
- GDB Tips by Greg Law - Tip #3
Begin debugging session of an executable?
start
: starts debugging session by running program line-by-line.run
: starts debugging session running program as usual.
Resources
- Boost.Asio C++ Network Programming - Chapter 7
Start debugging an already running process?
Attaching to a process requires root privileges.
After debugger gets attached to the process, the program will freeze as if it is on a breakpoint.
sudo gdb (gdb) attach 12345
Resources
Detach debugger from a debugging process?
Description
(gdb) detachAfter detaching, the program will continue its normal flow as there was no debugger attached to it at first place.
Resources
Show source code in debugging session?
list
displays 10 lines of source code. To see how many lines of source code will be displayed entershow listsize
. To adjust the lines of source code displayed enterset listsize 20
.
Resources
- Boost.Asio C++ Network Programming - Chapter 7
How many breakpoints does GDB offer?
- Hardware watchpoints:
watch foo
- Software watchpoints:
watch $rax
- Location watchpoints:
watch -l foo
- Read-only watchpoint:
rwatch foo
- Access watchpoints:
awatch foo
Resources
- GDB Tips by Greg Law - Tip #7
Get information about breakpoints, watchpoints and catchpoints?
Description
(gdb) info breakpoints
Resources
- GDB Tips by Greg Law - Tip #13
Set breakpoints on a program?
Description
(gdb) break 50 (gdb) break *main (gdb) break *main+50 (gdb) break source.cpp:main+50
Resources
- Boost.Asio C++ Network Programming - Chapter 7
Run program with an immediate stop on main function?
Description
Use
start
to put temporary breakpoint on main and then run the program orstarti
to stop at the very first instruction of the program.Resources
- GDB Tips by Greg Law - Tip #4
Use regex to select multiple breakpoints for an operation?
(gdb) rbreak std::sto[ifd]
Resources
- GDB Tips by Greg Law - Tip #14
Delete a breakpoint from a program?
Description
(gdb) delete 1
Resources
- Boost.Asio C++ Network Programming - Chapter 7
Set condition for a breakpoint?
(gdb) condition 3 e == 42 (gdb) break func(long) if e == 42
Resources
- GDB Tips by Greg Law - Tip #15
Conditionally break execution only when iteration index has increased to 3?
Description
(gdb) break source.cpp:10 if index == 3
Resources
Skip over a number of breakpoint hits?
To ignore the next 10000000 times that breakpoint 1 is hit.
(gdb) ignore 1 10000000 (gdb) info break 1breakpoint already hit 1000 times
(gdb) ignore 1 999 (gdb) run
Resources
- GDB Tips by Greg Law - Tip #5
- GDB Tips by Greg Law - Tip #6
Disable an already existing breakpoint?
(gdb) disable <breakpoint index>
Resources
Enable an already disabled breakpoint?
(gdb) enable <breakpoint index>
Resources
Save breakpoints of a debugging session?
(gdb) save breakpoints program.gdb
Resources
- GDB Tips by Greg Law - Tip #13
- GDB Tips by Greg Law - Tip #61
- https://www.youtube.com - https://www.youtube.com/watch?v=Rudz-uSdWHM
Restore saved breakpoints on a debugging session?
Description
(gdb) source program.gdb
Resources
- GDB Tips by Greg Law - Tip #61
- https://www.youtube.com - https://www.youtube.com/watch?v=Rudz-uSdWHM
Manually write the content of a file in which breakpoints were saved?
(gdb) break source.cpp:8 (gdb) break source.cpp:22Note that lines might have been changed during sessions.
Resources
Watch changes of a variable only when a criteria is met?
Description
(gdb) watch foo[90].number if bar[90].number == 255
Resources
- GDB Tips by Greg Law - Tip #7
Track a particular location in memory rather than the value of an expression?
(gdb) watch -l expressionConvinience variables also work nicely with location watchpoints:
(gdb) print * __errno_location() $2 = 0 (gdb) watch -l $2
Resources
- GDB Tips by Greg Law - Tip #9
- GDB Tips by Greg Law - Tip #10
Step through program execution in debugging session?
Description
continue
: Will resume the execution of the program until it completes.step
: Executes program one more step. Step might be one line of source code or one machine instruction.next
: Executes program similar tostep
, but it only continues to the next line in the current stack frame and will not step into functions.
Resources
- Boost.Asio C++ Network Programming - Chapter 7
Examine a memory address or register?
Display the memory contents at a given address using
x [address]
short forexamine
. You can customise the output with an optional format and length argument.(gdb) x/[length][format] [address] (gdb) x/2x 0x0ffffffff7c544f0 (gdb) x/2x $rsp
Resources
- GDB Tips by Greg Law - Tip #63
Get information of threads?
Description
To get information about the spawned threads:
(gdb) info threads
This will show thread ID, how the OS sees them, and the name the thread was given by the program.
Stepping through a thread that releases other threads, gets all threads out of sync. This can be changed with
set scheduler-locking on
, meaning only the current thread will run. Beware of deadlock through, the thread you're running might block on a lock held by another not running thread.A middle ground can be found with
set scheduler-locking step
, which has scheduler-locking on when usingstep
, but disables it when you usecontinue
, allowing you to travel between breakpoints without having to constantly switch between the threads.
Resources
- GDB Tips by Greg Law - Tip #64
List the innermost or outermost frames of backtrace?
Description
If you want to examine the backtrace of multiple frames but you're very deep in the stack, you can use
backtrace n
to show the n innermost frames. Similarly, you can usestacktrace -n
to show the n outermost frames.(gdb) bt full (gdb) bt 1 (gdb) bt -1
Resources
- GDB Tips by Greg Law - Tip #59
Where the coredumps are stored?
coredumpctl list
Resources
Store the core dump of your program into a file?
Description
coredumpctl dump $PWD/program --output program.core
Resources
Load the stored coredump into debugging session?
Description
(gdb) target core program.core
Resources
Select a core section to inspect the cause of termination?
When frame is selected from a core dump, program will be set to the corresponding state.
(gdb) frame 0 (gdb) p variable_causing_trouble
Resources
Create a core dump from the state of an executable in debugging session?
(gdb) gcore program.core
Resources
Print the value of an object?
(gdb) print bag_size
Resources
- Boost.Asio C++ Network Programming - Chapter 7
Inspect the type of a variable?
(gdb) whatis random_number
Resources
- Boost.Asio C++ Network Programming - Chapter 7
Modify the value of a variable?
(gdb) set var random_number = 5
Resources
- Boost.Asio C++ Network Programming - Chapter 7
Run shell commands in debugging session?
Description
(gdb) shell pwd
Resources
- Boost.Asio C++ Network Programming - Chapter 7
Automatically run commands on GDB startup?
If you have commands you want to run every time you debug, or if you want to definne a function to pretty print a specific data structure, you can put them in
.gdbinit
file on the top project directory, or in your home directory..gdbinit
set logging enabled on
You may need to enable auto-loading for the directory by adding the following line in
~/.config/gdb/gdbinit
file.set auto-load-safe-path /path/to/dir
You can also enable it for all directories with
set auto-load-safe-path /
.
Resources
- GDB Tips by Greg Law - Tip #62