Debugging With GDB - BetterExplained
Debugging With GDB - BetterExplained
Search...
g d ba . o u t
Opens GDB with file a.out, but does not run the program. Youll see a prompt ( g d b ) all examples are from this prompt.
r ra r g 1a r g 2 r<f i l e 1
Three ways to run a.out, loaded previously. You can run it directly (r), pass arguments (r arg1 arg2), or feed in a file. You will usually set breakpoints before running.
h e l p hb r e a k p o i n t s
List help topics (help) or get help on a specific topic (h breakpoints). GDB is welldocumented.
q Quit GDB
l l5 0 lm y f u n c t i o n
List 10 lines of source code for current line (l), a specific line (l 50), or for a function (l myfunction).
n e x t
Run program until next line, then pause. If the current line is a function, execute the entire
http://betterexplained.com/articles/debugging-with-gdb/ 1/9
4/7/2014
function, then pause. Next is good for walking through your code quickly.
s t e p
Run the next instruction, not line. If the current instructions is setting a variable, it is the same as n e x t . If its a function, it will jump into the function, execute the first statement, then pause. Step is good for diving into the details of your code.
f i n i s h
Finish executing the current function, then pause (also called step out). Useful if you accidentally stepped into a function.
b r e a k4 5 b r e a km y f u n c t i o n
Set a breakpoint at line 45, or at myfunction. The program will pause when it reaches the breakpoint.
w a t c hx= =3
Set a watchpoint, which pauses the program when a condition changes (when x == 3 changes). Watchpoints are great for certain inputs (myPtr != NULL) without having to break on every function call.
c o n t i n u e
Resume execution after being paused by a breakpoint/watchpoint. The program will continue until it hits the next breakpoint/watchpoint.
d e l e t eN
Delete breakpoint N (breakpoints are numbered when created).
p r i n tx
Print current value of variable x. Being able to use the original variable names is why the (g) flag is needed; programs compiled regularly have this information removed.
s e tx=3 s e tx=y
Set x to a set value (3) or to another variable (y)
c a l lm y f u n c t i o n ( ) c a l lm y o t h e r f u n c t i o n ( x ) c a l ls t r l e n ( m y s t r i n g )
Call user-defined or system functions. This is extremely useful, but beware calling buggy functions.
d i s p l a yx u n d i s p l a yx
http://betterexplained.com/articles/debugging-with-gdb/ 2/9
4/7/2014
Constantly display value of variable x, which is shown after every step or pause. Useful if you are constantly checking for a certain value. Use undisplay to remove the constant display.
b t
Backtrace, aka print the current function stack to show where you are in the current program. If m a i n calls function a ( ) , which calls b ( ) , which calls c ( ) , the backtrace is
c< =c u r r e n tl o c a t i o n b a m a i n
u p d o w n
Move to the next frame up or down in the function stack. If you are in c , you can move to b or a to examine local variables.
r e t u r n
Return from current function.
g d bm y p r o g r a mc o r e
Debug myprogram with core as the core dump file.
b t
Print the backtrace (function stack) at the point of the crash. Examine variables using the techniques above.
Handling Signals
Signals are messages thrown after certain events, such as a timer or error. GDB may pause when it encounters a signal; you may wish to ignore them instead.
h a n d l e[ s i g n a l n a m e ][ a c t i o n ]
http://betterexplained.com/articles/debugging-with-gdb/ 3/9
4/7/2014
h a n d l eS I G U S R 1n o s t o p h a n d l eS I G U S R 1n o p r i n t h a n d l eS I G U S R 1i g n o r e
Tell GDB to ignore a certain signal (S I G U S R 1 ) when it occurs. There are varying levels of ignoring.
Tips
I often prefer watchpoints to breakpoints. Rather than breaking on every loop and checking a variable, set a watchpoint for when the variable gets to the value you need (i == 25, ptr != null, etc.).
# i n c l u d e< s t d i o . h > # d e f i n eL O G _ N O N E0 # d e f i n eL O G _ E R R O R1 # d e f i n eL O G _ W A R N2 # d e f i n eL O G _ I N F O3 # d e f i n eL O G _ L E V E LL O G _ W A R N
4/7/2014
Spend the time to learn GDB (or another debugging tool)! I know, its like telling people to eat their vegetables, but it really is good for you youll thank me later. October 31, 2006 Guides, Programming Printable version
22 Comments
1. Nejd May 17, 2007 at 2:44 am thank you for this short tut, its very helpful. It would be good if you make another one to us how to integrate the debugger in the source code (#define )
2. Frodo
The article was clear and easy to follow. Thank you. But actually, the step command executes all the statements in a line (e.g. a=5;a+=1;), its a line step (Run the next instruction, not line.). For stepping on (machine) instructions the stepi command should be used. As far as I know, stepping on source level instructions/statements is not possible. Correct me, if Im wrong.
3. Kalid
Thanks Frodo, didnt know they had GNU tools in the shire Great catch, Ill update the article. Nejd, Ill put up some examples of the debugging too, thanks.
4. technochakra
Nice article. I just wrote a debugging article dedicated to hit breakpoints. Check it out and do give it a read if you get time. http://www.technochakra.com/debugging-using-breakpoint-hit-count-for-fun-and-profit/
5. Juan Carlos
http://betterexplained.com/articles/debugging-with-gdb/
4/7/2014
6. Mike
Beautiful GDB tut, easy, precise, and to the point, def the best for quick no brainer questions!
7. Kalid
@Juan: I believe you can just do set x = 3 @Mike: Thanks, glad you enjoyed it!
8. Nimrod
More tips that make gdb more bearable: 1. You can set conditions on breakpoints. E.g. cond 3 (x==2) will stop at breakpoint 3 only when x==2. 2. You can set ignore count on breakpoints. ignore 3 1000 ignores the next 1000 crossing of breakpoint 3. Then at some interesting point in time (when your program crashes), info break 3 shows exactly how many times breakpoint 3 had been hit. Next time set the ignore count to one less than that number and gdb will stop one iteration before the crash 3. When using watch, make sure gdb says hardware watchpoint set. Unlike software watchpoints, these do not slow down program execution. 4. It is possible to define macros (using define xxx end) in .gdbrc Other useful features I havent used: 1. gdb7 supports reverse debugging 2. gdb7 is scriptable using Python. Together with the new libstdc++ you get pretty printing of C++ STL collection classes.
9. Anonymous
can we load more den one exe?..like d one we did above gdb a.out. can it be like gdb a.out b.out?
http://betterexplained.com/articles/debugging-with-gdb/
6/9
4/7/2014
I have been using gdb minimally. but some of the extra info here is really hepful. Thanks for the turorial.
11. Kalid
@Anonymous: The gdb command line help indicates you can only load a single file.
13. Paul
16. rajesh
17. Dhanunjay
18. kalid
http://betterexplained.com/articles/debugging-with-gdb/
7/9
4/7/2014
using namespace std ; int main() { string stra , strb , strc ; cin>>stra>>strb>>strc ; int lena , lenb , lenc ; lena = stra.length(); lenb = strb.length() ; lenc = strc.length() ; int found =0 , j=0 , k=0 ; if((lena + lenb) == lenc) { int a,b,c ; for(int i =0 ; i<lenc ; i++) { found = 0 ; if(j<lena && found ==0) if(strc[i] == stra[j]) { j++ ; found = 1; } if(k<lenb && found ==0) if( strc[i] == strb[k] ) { k++; found = 1; } if(found == 0) { cout<<"not interlieved"<<endl; break ; } } if(found == 1) cout<<"interlieved"<<endl; } else { cout<<"not interlieved"<<endl; } }
http://betterexplained.com/articles/debugging-with-gdb/
8/9
4/7/2014
20. Ian S
Thanks for the quick tutorial, the basics look very simple and easy to use.
21. Ramaswamy
Very good explanation in simpler way. Its very much usefull. Thanks so much.
Really very useful, important contents are explained with the help of examples. Thanks
http://betterexplained.com/articles/debugging-with-gdb/
9/9