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
Command
Description
gdb <executable>
Start GDB with the specified executable
run
Start the program
break <line>
Set a breakpoint at the specified line
break <function>
Set a breakpoint at the beginning of the specified function
info breakpoints
List all breakpoints
delete <breakpoint>
Delete the specified breakpoint
next
Execute the next line of code
step
Execute the next line of code, stepping into function calls
continue
Continue execution until the next breakpoint
print <expression>
Print the value of the specified expression
backtrace
Print a backtrace of the call stack
quit
Quit GDB
Examining Data
Command
Description
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 registers
Print the values of all registers
info locals
Print the values of all local variables
info args
Print the values of all function arguments
Modifying Data
Command
Description
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
Command
Description
info threads
List all threads
thread <thread>
Switch to the specified thread
break <function> thread <thread>
Set a breakpoint in the specified thread
next
Execute the next line of code in the current thread
step
Execute the next line of code in the current thread, stepping into function calls
continue
Continue execution in the current thread
thread apply <thread-list> <command>
Apply the specified command to the specified threads