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 theattach
command)--command EXECFILE
[or-x
] to execute commands from file (as with thesource
command)--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|off
to enable/disable loggingset logging file FILE
to change the name of the current logfile. The default logfile isgdb.txt
help
[orh
] to display a short list of named classes of commandshelp COMMAND
to display a short paragraph on how to use that commandapropos ARGS
to searche through all of the gdb commands and their documentation for the regular expression specified in ARGScomplete ARGS
to 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 info
info 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 SYMBOL
to find address of SYMBOLinfo proc mappings
to 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 sharedlibrary
to display information about loaded librariesinfo symbol ADDR
to 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 status
apropos alias
complete i
info addr system
To request debugging information, specify the -g
option when you run the compiler.
run
[orr
] to start your program under gdbstart
to set a temporary breakpoint at the beginning of the main procedure and then invoke therun
commandset exec-wrapper WRAPPER
to 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 callsexecve
with its arguments as a wrappershow exec-wrapper
set disable-randomization on|off
to 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 args
to specify the arguments to be used the next time your program is run. Ifset args
has no arguments, run executes your program with no arguments. Once you have run your program with arguments, usingset args
before 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
attach
to attach to a running process started outside gdb
info inferiors
to print a list of all inferiors currently being managed by gdbinferior INFNO
to make inferior number INFNO the current inferiorkill inferiors INFNO...
to kill the inferior or inferiors identified by gdb inferior number(s)
thread THREADID
to switch among threadsinfo threads
to inquire about existing threads
set follow-fork-mode MODE
to set the debugger response to a program call offork
orvfork
. 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-mode
set detach-on-fork MODE
to 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-fork
set follow-exec-mode MODE
to 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 theexec
call 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 theexec
call, with e.g., therun
command, restarts the executable the process was running after theexec
call)show follow-exec-mode
checkpoint
save a snapshot of the debugged program's current execution stateinfo checkpoints
to list the checkpoints that have been saved in the current debugging sessionrestart CHKID
to restore the program state that was saved as checkpoint number CHKIDdelete checkpoint CHKID
to 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 COND
to set a breakpoint with condition CONDtbreak ARGS
to set a breakpoint enabled only for one stop (ARGS are the same as for thebreak
command)hbreak ARGS
to set a hardware-assisted breakpoint (ARGS are the same as for thebreak
command)thbreak ARGS
to set a hardware-assisted breakpoint enabled only for one stop (ARGS are the same as for thehbreak
command)rbreak REGEX
to set breakpoints on all functions matching the regular expression REGEXbreak ARGS thread THREADNO
to set breakpoints on a particular threadinfo breakpoints
to 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 EXPR
to set a watchpoint that will break when the expression EXPR is written into by the program and its value changesrwatch EXPR
to set a watchpoint that will break when the value of EXPR is read by the programawatch EXPR
to set a watchpoint that will break when EXPR is either read from or written into by the programinfo watchpoints
to 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|1
to set whether or not to use hardware watchpointsshow can-use-hw-watchpoints
to 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 x
watch *0x600850
watch *(int *)0x12345678
(to watch a 4-byte region at the specified address)watch a*b + c/d
delete 1 2 3
delete 1-3 5-6
disable 1 2 3
enable delete 1 2
continue
[orc
] to resume program execution after a stopfinish
to 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 LOCATION
to 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:nostop
to not stop your program when this signal happensstop
to stop your program when this signal happens. This implies theprint
keyword as wellprint
to print a message when this signal happensnoprint
to not mention the occurrence of the signal at all. This implies thenostop
keyword 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-stepi
to reverse-execute one machine instructionreverse-nexti
to reverse-execute a single instruction in reverse (called functions are "un-executed" atomically)reverse-finish
to 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 STACKADDR
to select the frame at address STACKADDRup [N]
to move N frames up the stack. N defaults to1
down [N]
to move N frames down the stack. N defaults to1
select-frame [N]
to silently select a stack frame
frame
[orf
] to print a brief description of the currently selected stack frameinfo frame
to print a verbose description of the selected stack frameinfo args
to print the arguments of the selected frameinfo locals
to 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 ARG
to 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 arr
explore struct ComplexStruct
p 'f2.c'::x
(to refer to static variables)p i@entry
(to get value of variablei
at the time the function got called)
p *array@len
p/x (short[])0x12345678
(to create artificial arrays)
x[/NFU] ADDR
to 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-6
x/s *environ
(to get the address of the first environment variable (or, alternatively, EBP of main + 16/32 bytes))
display[/FMT] EXPR
to 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 values
to 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 registers
to 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_ADDR
to 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 END
to restore the contents of file FILE into memory
generate-core-file [FILE]
[orgcore
] to produce a core dump of the inferior process
set charset CHARSET
to 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=4
whatis width
set var width=47
set {int}0x83040 = 4
jump LOCATION
[orj
] to resume execution at location. Thejump
command 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 SIGNAL
to 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 SIGINT
signal 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