gdb [options] [PROGRAM [COREFILE or PID]]
gdb [options] --args PROGRAM [INFARGS...] to pass any arguments after the executable file to the inferior
--silent[or-q/--quiet] to start without printing the front material--core COREFILE[or-c] to analyze a core dump--pid PID[or-p] to debug a running process (as with theattachcommand)--command EXECFILE[or-x] to execute commands from file (as with thesourcecommand)--symbols SYMFILE[or-s] to read symbol table from file
gdb -q --args gcc -O2 -c foo.c
Examples of command-lists from a command file
b main
commands 1
print argc
continue
end
b *0xdeadbeef if x > 0
commands 2
p i
p b
continue
end
run
- 2 Getting In and Out of gdb
- 3 gdb Commands
- 4 Running Programs Under gdb
- 4.1 Compiling for Debugging
- 4.2 Starting your Program
- 4.3 Your Program's Arguments
- 4.4 Your Program's Environment
- 4.7 Debugging an Already-running Process
- 4.9 Debugging Multiple Inferiors and Programs
- 4.10 Debugging Programs with Multiple Threads
- 4.11 Debugging Forks
- 4.12 Setting a Bookmark to Return to Later
- 5 Stopping and Continuing
- 6 Running Programs Backward
- 8 Examining the Stack
- 10 Examining Data
- 10.3 Program Variables
- 10.4 Artificial Arrays
- 10.6 Examining Memory
- 10.7 Automatic Display
- 10.10 Value History
- 10.11 Convenience Variables
- 10.12 Convenience Functions
- 10.13 Registers
- 10.18 Copy Between Memory and a File
- 10.19 How to Produce a Core File from Your Program
- 10.20 Character Sets
- 10.22 Search Memory
- 17 Altering Execution
quit[orq] to exit gdb. An interrupt (oftenCtrl-c) does not exit from gdb, but rather terminates the action of any gdb command that is in progress and returns to gdb command level. It is safe to type the interrupt character at any time because gdb does not allow it to take effect until a time when it is safe
shell COMMAND[or!COMMAND] to invoke a standard shell to execute COMMAND
set logging on|offto enable/disable loggingset logging file FILEto change the name of the current logfile. The default logfile isgdb.txt
help[orh] to display a short list of named classes of commandshelp COMMANDto display a short paragraph on how to use that commandapropos ARGSto searche through all of the gdb commands and their documentation for the regular expression specified in ARGScomplete ARGSto list all the possible completions for the beginning of a command specified by ARGSinfo[ori] to describe the state of your program. You can get a complete list of the info sub-commands withhelp infoinfo files[orinfo target] to display info on the debugged program (useful to find the entry point)info functions [REGEXP]to list all defined functions or whose matching REGEXPinfo address SYMBOLto find address of SYMBOLinfo proc mappingsto display the list of mapped memory regionsinfo registers [REGISTER]to display the contents of all the general-purpose processor registers or the content of register REGISTERinfo sharedlibraryto display information about loaded librariesinfo symbol ADDRto display the name of the symbol residing at a given address ADDRinfo types [REGEXP]to display the list of types defined in the currently loaded modules or the list of types matching REGEXPinfo variables [REGEXP]to display the list of global/static variables or whose matching REGEXP
help statusapropos aliascomplete iinfo addr system
To request debugging information, specify the -g option when you run the compiler.
run[orr] to start your program under gdbstartto set a temporary breakpoint at the beginning of the main procedure and then invoke theruncommandset exec-wrapper WRAPPERto set the wrapper used to launch programs for debugging, with a shell command of the formexec WRAPPER program. You can use any program that eventually callsexecvewith its arguments as a wrappershow exec-wrapperset disable-randomization on|offto enable/disable address randomization
set exec-wrapper env 'LD_PRELOAD=custom_libc.so'(to pass an environment variable to the debugged program without setting the variable in your shell's environment)
set argsto specify the arguments to be used the next time your program is run. Ifset argshas no arguments, run executes your program with no arguments. Once you have run your program with arguments, usingset argsbefore the next run is the only way to run it again without argumentsshow args
set environment VARNAME [VALUE]to set environment variable VARNAME to VALUEshow environment [VARNAME]to print the value of environment variable VARNAME. If VARNAME is not specified, print the names and values of all environment variablesunset environment [VARNAME]to remove variable VARNAME from the environment. If VARNAME is not specified, remove all environment variables
set environment LD_PRELOAD=./yourso.so
attachto attach to a running process started outside gdb
info inferiorsto print a list of all inferiors currently being managed by gdbinferior INFNOto make inferior number INFNO the current inferiorkill inferiors INFNO...to kill the inferior or inferiors identified by gdb inferior number(s)
thread THREADIDto switch among threadsinfo threadsto inquire about existing threads
set follow-fork-mode MODEto set the debugger response to a program call offorkorvfork. The MODE argument can beparent(the original process is debugged after a fork) orchild(the new process is debugged after a fork)show follow-fork-modeset detach-on-fork MODEto detach one of the processes after a fork or retain debugger control over them both. The MODE argument can beon(the child process (or parent process, depending on the value offollow-fork-mode) will be detached and allowed to run independently) oroff(both processes will be held under the control of gdb, one debugged and the other held suspended)show detach-on-forkset follow-exec-mode MODEto set debugger response to a program call ofexec. The MODE argument can benew(gdb creates a new inferior and rebinds the process to this new inferior. The program the process was running before theexeccall can be restarted afterwards by restarting the original inferior) orsame(gdb keeps the process bound to the same inferior. The new executable image replaces the previous executable loaded in the inferior. Restarting the inferior after theexeccall, with e.g., theruncommand, restarts the executable the process was running after theexeccall)show follow-exec-mode
checkpointsave a snapshot of the debugged program's current execution stateinfo checkpointsto list the checkpoints that have been saved in the current debugging sessionrestart CHKIDto restore the program state that was saved as checkpoint number CHKIDdelete checkpoint CHKIDto delete the previously-saved checkpoint identified by CHKID
break [LOCATION]to a breakpoint at the given LOCATION. If LOCATION is not specified, set a breakpoint at the next instruction to be executed in the selected stack framebreak ... if CONDto set a breakpoint with condition CONDtbreak ARGSto set a breakpoint enabled only for one stop (ARGS are the same as for thebreakcommand)hbreak ARGSto set a hardware-assisted breakpoint (ARGS are the same as for thebreakcommand)thbreak ARGSto set a hardware-assisted breakpoint enabled only for one stop (ARGS are the same as for thehbreakcommand)rbreak REGEXto set breakpoints on all functions matching the regular expression REGEXbreak ARGS thread THREADNOto set breakpoints on a particular threadinfo breakpointsto print a table of all breakpoints, watchpoints, and catchpoints set and not deleted
Use a watchpoint to stop execution whenever the value of an expression changes.
watch EXPRto set a watchpoint that will break when the expression EXPR is written into by the program and its value changesrwatch EXPRto set a watchpoint that will break when the value of EXPR is read by the programawatch EXPRto set a watchpoint that will break when EXPR is either read from or written into by the programinfo watchpointsto print a list of watchpoints
gdb sets a hardware watchpoint if possible. Hardware watchpoints execute very quickly, and the debugger reports a change in value at the exact instruction where the change occurs. If gdb cannot set a hardware watchpoint, it sets a software watchpoint, which executes more slowly and reports the change in value at the next statement, not the instruction, after the change occurs.
set can-use-hw-watchpoints 0|1to set whether or not to use hardware watchpointsshow can-use-hw-watchpointsto show the current mode of using hardware watchpoints
In multi-threaded programs, watchpoints will detect changes to the watched expression from every thread.
-
delete [RANGE...]to delete the breakpoints, watchpoints, or catchpoints of the breakpoint ranges. If RANGE... is not specified, delete all breakpoints, watchpoints or catchpoints -
disable [RANGE...]to disable the specified breakpoints. If RANGE... is not specified, disable all breakpoints -
enable [RANGE...]to enable the specified breakpoints. If RANGE... is not specified, enable all breakpoints -
enable once RANGE...to enable the specified breakpoints temporarily and then disable them after stopping your program -
enable delete RANGE...to enable the specified breakpoints temporarily and then delete them after stopping your program -
save breakpoints [FILE]to save breakpoint definitions to a file
watch xwatch *0x600850watch *(int *)0x12345678(to watch a 4-byte region at the specified address)watch a*b + c/ddelete 1 2 3delete 1-3 5-6disable 1 2 3enable delete 1 2
continue[orc] to resume program execution after a stopfinishto continue running until just after function in the selected stack frame returnsuntil[oru] to continue execution until the program counter is greater than the address of the jump (very useful to continue execution until loop exit)advance LOCATIONto continue running the program up to the given locationstepi[orsi] to execute one machine instructionnexti[orni] to execute one machine instruction stepping over function calls
info signals[orinfo handle] to print a table of all the kinds of signals and how gdb has been told to handle each onehandle SIGNAL [KEYWORDS...]to change the way gdb handles signal SIGNAL. The keywords can be:nostopto not stop your program when this signal happensstopto stop your program when this signal happens. This implies theprintkeyword as wellprintto print a message when this signal happensnoprintto not mention the occurrence of the signal at all. This implies thenostopkeyword as wellpass[ornoignore] to allow your program to see this signalnopass[orignore] to not allow your program to see this signal
handle SIGUSR1
reverse-continue[orrc] to start executing in reverse beginning at the point where your program last stoppedreverse-stepito reverse-execute one machine instructionreverse-nextito reverse-execute a single instruction in reverse (called functions are "un-executed" atomically)reverse-finishto take you to the point where the current function was called
backtrace [N][orbt] to print a backtrace of the entire stackbacktrace full [N]to print the values of the local variables also
frame N[orf] to select frame number N (frame zero is the innermost (currently executing) frame)frame STACKADDRto select the frame at address STACKADDRup [N]to move N frames up the stack. N defaults to1down [N]to move N frames down the stack. N defaults to1select-frame [N]to silently select a stack frame
frame[orf] to print a brief description of the currently selected stack frameinfo frameto print a verbose description of the selected stack frameinfo argsto print the arguments of the selected frameinfo localsto print the local variables of the selected frame
print [/F] [EXPR][orinspect] to evaluate and print the value of an expression of the language your program is written in. You can choose a different format by specifying/F, where F is a letter specifying the format. If you omit EXPR, gdb displays the last value again (useful to inspect the same value in an alternative format)explore ARGto explore either an expression (in the source language), or a type visible in the current context of the program being debugged
p filename[0] = 'a'p strlen(filename)explore arrexplore struct ComplexStruct
p 'f2.c'::x(to refer to static variables)p i@entry(to get value of variableiat the time the function got called)
p *array@lenp/x (short[])0x12345678(to create artificial arrays)
x[/NFU] ADDRto examine memory. N, F, and U are all optional parameters that specify how much memory to display and how to format it
x/3uh 0x54320(to display three halfwords (h) of memory, formatted as unsigned decimal integers (u), starting at address0x54320)x/4xw $sp(to print the four words (w) of memory above the stack pointer ($sp) in hexadecimal (x))x/5i $pc-6x/s *environ(to get the address of the first environment variable (or, alternatively, EBP of main + 16/32 bytes))
display[/FMT] EXPRto add the expression EXPR to the list of expressions to display each time your program stops. FMT is used to specify a display format
display/i $pc
To refer to any previous value, use $ followed by the value's history number.
show valuesto print the last ten values in the value history
p *$
gdb provides convenience variables that you can use within gdb to hold on to a value and refer to it later. Convenience variables are prefixed with $.
set $foo = *object_ptr
print $_isvoid ($v)p $_strlen($s)
info registersto print the names and values of all registers except floating-point and vector registersinfo all-registers
gdb has four "standard" register names that are available (in expressions) on most machines—whenever they do not conflict with an architecture's canonical mnemonics for registers. The register names $pc and $sp are used for the program counter register and the stack pointer. $fp is used for a register that contains a pointer to the current stack frame, and $ps is used for a register that contains the processor status.
set $sp += 4
dump [FORMAT] memory FILE START_ADDR END_ADDRto dump the contents of memory from START_ADDR to END_ADDR, or the value of expr, to FILE in the given formatrestore FILE [binary] BIAS START ENDto restore the contents of file FILE into memory
generate-core-file [FILE][orgcore] to produce a core dump of the inferior process
set charset CHARSETto set the current host and target character sets to CHARSET. If you typeset charset <TAB><TAB>, gdb will list the names of the character sets that can be used for both host and target
find [/SN] START_ADDR, +LEN|END_ADDR, VAL1 [, VAL2, ...]to search memory for the sequence of bytes specified by VAL1, VAL2, etc. The search begins at address START_ADDR and continues for either LEN bytes or through to END_ADDR inclusive
set is really the same as print except that the expression's value is not printed and is not put in the value history.
print x=4whatis widthset var width=47set {int}0x83040 = 4
jump LOCATION[orj] to resume execution at location. Thejumpcommand does not change the current stack frame, or the stack pointer, or the contents of any memory location or any register other than the program counter
jump *0x4028ba
signal SIGNALto resume execution where your program is stopped, but immediately give it the signal SIGNAL. The signal can be the name or the number of a signal
signal SIGINTsignal 2
return [EXPR]to discard the selected stack frame (and all frames within it). If you wish to specify a value to be returned, give that value as EXPR
return -1