GDB Cheat-Sheet GDB command cheat sheet

GDB Cheat-Sheet

############################################## ###############################
# GDB CHEATSHEET (Chinese cheat sheet) - by skywind (created on 2018/02/20)
# Version: 9, Last Modified: 2023/06/26 14:31
# https://github.com/skywind3000/awesome-cheatsheets
################################################ ############################


################################################ ############################
# Start GDB
################################################ ############################

gdb object # Normal startup, loading executable
gdb object core #Debug executable + core files
gdb object pid #Debug the executing process
gdb # Start normally. After startup, you need to manually load the file command.
gdb -tui # Enable gdb's text interface (or ctrl-x ctrl-a to replace CLI/TUI)


################################################ ############################
# help information
################################################ ############################

help # List command categories
help running # View help information for a certain category
help run # View help for the command run
help info # List commands related to viewing program running status
help info line # List help for a specific running status command
help show # List commands related to GDB status
help show commands # List help for show commands


################################################ ############################
# Breakpoint
################################################ ############################

break main #Set a breakpoint on function main, which can be abbreviated as b main
break 101 # Set a breakpoint on the line number of the source code, which can be abbreviated as b 101
break basic.c:101 # Set breakpoints on source code and line numbers
break basic.c:foo # Set breakpoints on source code and function names
break *0x00400448 # Set a breakpoint at memory address 0x00400448
info breakpoints # List all current breakpoint information, which can be abbreviated as info break
delete 1 # Delete a breakpoint by number
delete # Delete all breakpoints
clear # Delete the breakpoint on the current line
clear function # Delete function breakpoint
clear line # Delete line number breakpoint
clear basic.c:101 # Delete breakpoints for file names and line numbers
clear basic.c:main # Delete breakpoints on file names and function names
clear *0x00400448 # Delete the breakpoint of the memory address
disable 2 # Disable a breakpoint, but do not delete it
enable 2 # Allow a previously disabled breakpoint to take effect
rbreak {<!-- -->regexpr} # Breakpoint before the function that matches the regular expression, such as ex_* will break the function starting with ex_
tbreak function|line # Temporary breakpoint
hbreak function|line # Hardware breakpoint
ignore {<!-- -->id} {<!-- -->count} # Ignore a breakpoint N-1 times
condition {<!-- -->id} {<!-- -->expr} # Conditional breakpoint, which only occurs when the condition takes effect
condition 2 i == 20 # Breakpoint No. 2 only takes effect when the condition i == 20 is true.
watch {<!-- -->expr} # Set watch points on variables
info watchpoints # Display all watchpoints
catch exec # The breakpoint is at the exec event, which is the entry address of the child process


################################################ ############################
# Run the program
################################################ ############################

run # run the program
run {<!-- -->args} # Run the program with certain parameters
run < file # Run the program using a file as standard input
run < <(cmd) # Run the program using the output of a command as standard input
run <<< $(cmd) # Run the program using the output of a command as standard input
set args {<!-- -->args} ... # Set running parameters
show args # Display current running parameters
continue # Continue running, can be abbreviated as c or cont
step # Enter in a single step, and you will enter when you encounter a function (Step in)
step {<!-- -->count} # How many times to step
next # Skip in one step and will not enter when encountering a function (Step Over)
next {<!-- -->count} # How many times to step
finish #Run to the end of the current function (Step Out)
until #Continue execution until the code line number is greater than the current line number (break out of the loop)
until {<!-- -->line} #Continue execution until a certain line is reached
CTRL + C # Send SIGINT signal to terminate the currently running program
attach {<!-- -->process-id} # Link to the currently running process and start debugging
detach # Disconnect process link
kill # Kill the currently running function


################################################ ############################
# stack frame
################################################ ############################

bt # print backtrace (the command where is an alias of bt)
frame # Display the currently running stack frame
up # Move the stack frame up (toward the main function)
down # Move the stack frame downward (away from the main function)
info locals #Print related variables within the frame
info args #Print function parameters


################################################ ############################
# Code browsing
################################################ ############################

list 101 # Display 10 lines of code around line 101
list 1,10 # Display 1 to 10 lines of code
list main # Display the code around the function
list basic.c:main # Display the code surrounding the function of another source code file
list - # Repeat the previous 10 lines of code
list *0x22e4 # Display the code of a specific address
cd dir # switch current directory
pwd # Display the current directory
search {<!-- -->regexpr} # forward regular search
reverse-search {<!-- -->regexp} # Perform regular search backwards
dir {<!-- -->dirname} # Add source code search path
dir #Reset source code search path (clear)
show directories #show source code paths


################################################ ############################
# Browse data
################################################ ############################

print {<!-- -->expression} # Print the expression and add it to the printing history
print /x {<!-- -->expression} # Hexadecimal output, print can be abbreviated as p
print array[i]@count #Print array range
print $ #Print the previous variables
print *$->next # print list
print $1 # Output the first item in the print history
print ::gx #Set the variable scope to global
print 'basic.c'::gx # Print global variables in a certain source code, (gdb 4.6)
print /x & amp;main #Print function address
x *0x11223344 # Display the memory data at the given address
x /nfu {<!-- -->address} # Print memory data, n is the number, f is the format, u is the unit size
x /10xb *0x11223344 # Print the ten bytes at memory address 0x11223344 in hexadecimal
x/x & amp;gx # Print the variable gx in hexadecimal. Parameters can be written consecutively after x and the slash.
x/4wx & amp;main # Print the four longs at the beginning of the main function in hexadecimal
x/gf & amp;gd1 # print double type
help x #View help about x command
info locals #Print local variables
info functions {<!-- -->regexp} # Print function name
info variables {<!-- -->regexp} # Print global variable names
ptype name # View the type definition, such as ptype FILE, view the FILE structure definition
whatis {<!-- -->expression} # Check the type of expression
set var = {<!-- -->expression} # Variable assignment
display {<!-- -->expression} # View the value of an expression after a single step instruction
undisplay # Delete the monitoring of certain values after a single step
info display # Display monitored expressions
show values # View the values of variables recorded in the print history (gdb 4.0)
info history # View help for printing history (gdb 3.5)


################################################ ############################
# Target file operation
################################################ ############################

file {<!-- -->object} # Load a new executable file for debugging
file # Discard executable and symbol table information
symbol-file {<!-- -->object} # Only load the symbol table
exec-file {<!-- -->object} # Specify the executable file (not symbol table) used for debugging
core-file {<!-- -->core} # Load core for analysis


################################################ ############################
# Signal control
################################################ ############################

info signals #Print signal settings
handle {<!-- -->signo} {<!-- -->actions} # Set the debugging behavior of the signal
handle INT print # Print information when the signal occurs
handle INT noprint #Do not print information when the signal occurs
handle INT stop # Stop the debugged program when the signal occurs
handle INT nostop #Do not terminate the debugged program when the signal occurs
handle INT pass # The debugger receives the signal and does not let the program know
handle INT nopass # The debugger does not receive the signal
signal signo # continue and transfer the signal to the program
signal 0 # continue without giving the signal to the program


################################################ ############################
# Thread debugging
################################################ ############################

info threads # View current thread and id
thread {<!-- -->id} # Switch the current debugging thread to the thread with the specified id
break {<!-- -->line} thread all # Set breakpoints for all threads at the specified line number
thread apply {<!-- -->id..} cmd # Specify multiple threads to execute gdb commands together
thread apply all cmd # All threads execute the gdb command together
set schedule-locking ? # When debugging a thread, whether other threads are executed, off|on|step
set non-stop on/off # When debugging a thread, whether other threads are running
set pagination on/off # When debugging a thread, whether paging stops
set target-async on/off # Synchronous or asynchronous debugging, whether to wait for thread termination information


################################################ ############################
# Process debugging
################################################ ############################

info inferiors # View the current process and id
inferior {<!-- -->id} # Switch a process
kill inferior {<!-- -->id...} # Kill a process
set detach-on-fork on/off # Set whether gdb debugs the parent and child processes at the same time when the process calls fork
set follow-fork-mode parent/child # Set whether to enter the child process when the process calls fork


################################################ ############################
# Assembly debugging
################################################ ############################

info registers # print ordinary registers
info all-registers #Print all registers
print/x $pc # Print a single register
stepi # Command level single step entry, can be abbreviated as si
nexti #Instruction level single step skip, can be abbreviated as ni
display/i $pc # Monitoring register (the value will be automatically printed after each step)
x/x & amp;gx # Hexadecimal printing variable
info line 22 # Print the memory address information of line number 22
info line *0x2c4e # Print the source code and line number information corresponding to the given memory address
disassemble {<!-- -->addr} # Disassemble the address, such as disassemble 0x2c4e


################################################ ############################
# history information
################################################ ############################

show commands #Show historical commands (gdb 4.0)
info editing # Display command history (gdb 3.5)
ESC-CTRL-J # Switch to Vi command line editing mode
set history expansion on # Allow c-shell-like history
break class::member # Set a breakpoint at a class member
list class:member # Display class member codes
ptype class # View the members contained in the class
print *this # View this pointer


################################################ ############################
# Other commands
################################################ ############################

define command ... end #Define user command
<return> # Just press Enter to execute the previous command
shell {<!-- -->command} [args] # Execute shell command
source {<!-- -->file} # Load gdb command from file
quit # Exit gdb


################################################ ############################
# GDB frontend
################################################ ############################

gdb-tui Use gdb -tui to start (or ctrl-x ctrl-a to replace CLI/TUI)
cgdb http://cgdb.github.io/
emacs http://gnu.org/software/emacs
gdbgui https://github.com/cs01/gdbgui
vimspector https://github.com/puremourning/vimspector
termdebug https://github.com/vim/vim

GDB graphical front-end evaluation http://www.skywind.me/blog/archives/2036


################################################ ############################
# References
################################################ ############################

https://sourceware.org/gdb/current/onlinedocs/gdb/
https://kapeli.com/cheat_sheets/GDB.docset/Contents/Resources/Documents/index
http://www.yolinux.com/TUTORIALS/GDB-Commands.html
https://gist.github.com/rkubik/b96c23bd8ed58333de37f2b8cd052c30
http://security.cs.pub.ro/hexcellents/wiki/kb/toolset/gdb


# vim: set ts=4 sw=4 tw=0 noet ft=gdb: