Tuesday, October 05, 2010

GDB Essential commands


Command Abbr Description
set args
set command args. Also can do: gdb --args command arg1 ...
break b set breakpoint (at function, line number, ...)
run r (re)start execution
continue c Continue execution
step s Next line
next n next line without recursing into functions
finish fin next line after this function returns
list l show source (for line, function, offset, ...)
backtrace bt Show the stack of functions. Add "full" to include local variables
up, down, frame up, down, f Move between current stack frames
watch wa break when variable changes value
display

disp display expression each time program stops

info locals i loc display local variables
info threads i thr Display all threads
thread thr Switch to thread #
info breakpoints i b Display all breakpoints
Delete, enable, disable d, en, dis Delete, enable, disable breakpoint
help h display online help
focus next fs n switch window (allows cursor keys in CMD window for e.g.)
Ctrl-x a
Display code in another window
Ctrl-L
redraw the display (if program outputs for example)
print p Print value of expression
set variable set v Evaluate expression EXP and assign result to variable VAR
x/FMT x/FMT Examine memory


Sample .gdbinit file: 

# Set verbose printing of informational messages.
set verbose on
# Set printing of addresses
set print address on
# Set printing of object's derived type based on vtable info
set print object on
set print sym on
# Set prettyprinting of structures
#set print pretty off
# Set printing of C++ static members
set print static-members on
# Set demangling of encoded C++/ObjC names when displaying symbols
set print demangle on
# Unset printing of 8-bit characters in strings as \nnn
set print sevenbit-strings off
# Set prettyprinting of arrays
set print array on
# Set printing of array indexes
set print array-indexes
# Set printing of char arrays to stop at first null char
set print null-stop on
# Set printing of unions interior to structures
set print union on
# Set printing of C++ virtual function tables
set print vtbl on
# Set saving of the history record on exit
set history save on
# Set history expansion on command input
set history expansion on
# Set gdb's prompt
set prompt (onkar)

handle SIGCONT nostop

#### OTHER OPTIONAL SETTINGS ####
# Set a limit on how many elements of an array GDB will print. If GDB is printing a large array, it stops printing after it has printed the number of elements
# set by the set print elements command. This limit also applies to the display of strings. When GDB starts, this limit is set to 200. Setting number-of-elements
# to zero means that the printing is unlimited.
#set print elements number-of-elements


#source ~/stl-views-1.0.3.gdb

#set history filename     # TODO: enable this if reqd. Set the filename in which to record the command history

#catch throw

Useful commands:

Conditional breakpoint:

break main.cc:100 if i == 10
 

Repetitive commands:

b main()
(gdb) command 1
Type commands for when breakpoint 1 is hit, one per line.
End with a line saying just "end".
>print i
>print j
>print k
>end

The directory command and setting source directory:

(gdb) directory ~/src/somepackage/src
Source directories searched: /home/nelhage/src/coreutils-7.4:$cdir:$cwd 
 
This requests gdb to search for source files in the given dir in addition to 
the existing directories.

No comments: