Home | Projects | Notes > Unix/Linux > GDB Cheat Sheet
xxxxxxxxxx
1841# GDB Cheat Sheet
2
3
4## GDB
5
6* GNU Project Debugger
7
8
9## Startup
10
11* % gdb -help print startup help, show switches
12* % gdb object normal debug
13* % gdb objct core core debug (must specify core file)
14* % gdb ogject pid attatch to running process
15* % gdb use file command to load object
16
17
18## Help
19
20* (gdb) help list command classes
21* (gdb) help running list commands in one command class
22* (gdb) help run bottom-level help for a command "run"
23* (gdb) help info list info commands (running program state)
24* (gdb) help info line help for a particular info command
25* (gdb) help show list show commands (gdb state)
26* (gdb) help show commands specific help for a show command
27
28
29## Breakpoints
30
31* (gdb) break main set a breakpoint on a function
32* (gdb) break 101 set a breakpoint on a line number
33* (gdb) break basic.c:101 set a breakpoint at file and line (or function)
34* (gdb) info breakpoints show breakpoints
35* (gdb) delete 1 delete a breakpoint by number
36* (gdb) delete delete all breakpoints (prompted)
37* (gdb) clear delete breakpoints at currentl line
38* (gdb) clear function delete breakpoints a function
39* (gdb) clear line delete breakpoints at line
40* (gdb) disable 2 turn a breakpoint off, but don't remove it
41* (gdb) enable 2 turn a disabled breakpoint back on
42* (gdb) tbreak function|line set a temporary breakpoint
43* (gdb) commands break-no ... end set gdb commands with breakpoint
44* (gdb) ignore break-no count ignore btp N-1 times before activation
45* (gdb) condition break-no expression break only if condition is true
46* (gdb) condition 2 i == 20 example: break on breakpoint 2 if i equals to 20
47* (gdb) watch expression set software watchpoint on variable
48* (gdb) info watchpoints show current watchpoints
49
50
51## Running the Program
52
53* (gdb) run run the program with current arguments
54* (gdb) run args redirection run with args and redirection
55* (gdb) set args args... set arguments for run
56* (gdb) show args show current arguments to run
57* (gdb) cont continue the program
58* (gdb) step single step the program; step into functions
59* (gdb) step count single step \fIcount\fR times
60* (gdb) next step but step over functions
61* (gdb) next count next \fIcount\fR times
62* (gdb) CTRL-C actually SIGINT, stop execution of current program
63* (gdb) attach process-id attach to running program
64* (gdb) detach detach from running program
65* (gdb) finish finish current function's execution
66* (gdb) kill kill current executing program
67
68
69## Stack Backtrace
70
71* (gdb) bt printf stack backtrace
72* (gdb) frame show current execution position
73* (gdb) up move up stack trace (towards main)
74* (gdb) down move down stack trace (away from main)
75* (gdb) info locals print automatic variable in frame
76* (gdb) info args print function parameters
77
78
79## Browsing Source
80
81* (gdb) list 101 list 10 lines around line 101
82* (gdb) list 1,10 list lines 1 to 10
83* (gdb) list main list lines around function
84* (gdb) list basic.c:main list from another file basic.c
85* (gdb) list - list previous 10 lines
86* (gdb) list *0x22e4 *list source at address
87* (gdb) cd dir change current directory to \fIdir\fR
88* (gdb) pwd print working directory
89* (gdb) search regexpr forward current for regular expression
90* (gdb) reverse-search regexpr backward search for regular expression
91* (gdb) dir dirname add directory to source path
92* (gdb) dir reset source path to nothing
93* (gdb) show directories show source path
94
95
96## Browsing Data
97
98* (gdb) print expression print expression, added to value history
99* (gdb) print/x expressionR print in hex
100* (gdb) print array[i]@count artificial array - print array range
101* (gdb) print $ print last value
102* (gdb) print *$->next *print thru list
103* (gdb) print $1 print value 1 from value history
104* (gdb) print ::gx force scope to be global
105* (gdb) print 'basic.c'::gx flobal scope in named file (>=4,6)
106* (gdb) print/x &main print address of function
107* (gdb) x/countFormatSize address low-level examine command
108* (gdb) x/x &gx print gx in hex
109* (gdb) x/4wx &main print 4 longs at start of \fImain\fR in hex
110* (gdb) x/gf &gd1 print double
111* (gdb) help x show formats for x
112* (gdb) info locals print local automatics only
113* (gdb) info functions regexp print function names
114* (gdb) info variables regexp print global variable names
115* (gdb) ptype name print type of expression
116* (gdb) whatis expression print type of expression
117* (gdb) set variable = expression assign value
118* (gdb) display expression display expression result at stop
119* (gdb) undisplay delete displays
120* (gdb) info display show displays
121* (gdb) show values print value history (>= gdb 4.0)
122* (gdb) info history print value history (gdb 3.5)
123
124
125## Object File Manipulation
126
127* (gdb) file object load new file for debug (sym+exec)
128* (gdb) file discard sym+exec file info
129* (gdb) symbol-file object load only symbol table
130* (gdb) exec-file object specify object to run (not sym-file)
131* (gdb) core-file core post-mortem debugging
132
133
134## Signal Control
135
136* (gdb) info signals print signal setup
137* (gdb) handle signo actions set debugger actions for signal
138* (gdb) handle INT print print message when signal occurs
139* (gdb) handle INT noprint don't print message
140* (gdb) handle INT stop stop program when signal occurs
141* (gdb) handle INT nostop don't stop program
142* (gdb) handle INT pass allow program to receive signal
143* (gdb) handle INT nopass debugger catches signal; program doesn't
144* (gdb) signal signo continue and send signal to program
145* (gdb) signal 0 continue and send no signal to program
146
147
148## Machine-Level Debug
149
150* (gdb) info registers print registers sans floats
151* (gdb) info all-registers print all registers
152* (gdb) print/x $pc print one register
153* (gdb) step1 single step at machine level
154* (gdb) s1 single step at machine level
155* (gdb) next1 single step (over functions) at machine level
156* (gdb) n1 single step (over functions) at machine level
157* (gdb) display/i $pc print current instruction in display
158* (gdb) x/x &gx print variable gx in hex
159* (gdb) info line 22 print address for object code for line 22
160* (gdb) info line *0x2c4e *print line number of object code at address
161* (gdb) x/10i main disassemble first 10 instructions in \fImain\fR
162* (gdb) disassemble addr disassemble code for function around addr
163
164
165## History Display
166
167* (gdb) show commands print command history (>= gdb 4.0)
168* (gdb) info editing print command history (gdb 3.5)
169* (gdb) EXC-CTRL-J switch to vi edit mode from emacs edit mode
170* (gdb) set history expansion on turn on c-shell like history
171* (gdb) break class::member set breakpoint on class member. may get menu
172* (gdb) list class::member list member in class
173* (gdb) ptype class print class members
174* (gdb) print *this *print contents of this pointer
175* (gdb) rbreak regexpr useful for breakpoint on overloaded member name
176
177
178## Miscellaneous
179
180* (gdb) define command ... end define user command
181* (gdb) RETURN repeat last command
182* (gdb) shell command args execute shell command
183* (gdb) source file load gdb commands from file
184* (gdb) quit quit job