GDB Cheat Sheet

In C, C++

GDB, or the GNU Debugger, is a powerful tool for debugging programs written in C, C++, and other programming languages. It allows developers to step through their code, set breakpoints, and examine the state of their program at any given point in time.

One of the key features of GDB is its ability to provide detailed information about the state of a program at any given time. This includes information about the values of variables, the contents of memory, and the call stack. This information can be invaluable when trying to track down bugs or other issues in a program.

Basic Commands

CommandDescription
gdb <executable>Start GDB with the specified executable
runStart the program
break <line>Set a breakpoint at the specified line
break <function>Set a breakpoint at the beginning of the specified function
info breakpointsList all breakpoints
delete <breakpoint>Delete the specified breakpoint
nextExecute the next line of code
stepExecute the next line of code, stepping into function calls
continueContinue execution until the next breakpoint
print <expression>Print the value of the specified expression
backtracePrint a backtrace of the call stack
quitQuit GDB

Examining Data

CommandDescription
print <variable>Print the value of the specified variable
print *<pointer>Print the value pointed to by the specified pointer
print <expression>@<length>Print the specified number of elements of an array
x/<format> <address>Examine memory at the specified address
info registersPrint the values of all registers
info localsPrint the values of all local variables
info argsPrint the values of all function arguments

Modifying Data

CommandDescription
set <variable>=<value>Set the value of the specified variable
set *<pointer>=<value>Set the value pointed to by the specified pointer
set variable=<value>Set the value of the specified variable
set $<register>=<value>Set the value of the specified register

Debugging Multithreaded Programs

CommandDescription
info threadsList all threads
thread <thread>Switch to the specified thread
break <function> thread <thread>Set a breakpoint in the specified thread
nextExecute the next line of code in the current thread
stepExecute the next line of code in the current thread, stepping into function calls
continueContinue execution in the current thread
thread apply <thread-list> <command>Apply the specified command to the specified threads

Debugging Remote Programs

CommandDescription
target remote <hostname>:<port>Connect to a remote GDB server
file <executable>Load the specified executable
set remotebaud <baud>Set the baud rate for the remote connection
set remotebaudrate <baud>Set the baud rate for the remote connection
set remotebaudrate <baud>Set the baud rate for the remote connection
set remotebaudrate <baud>Set the baud rate for the remote connection

References

GDB Documentation