A debugger lets you pause a program, examine and change variables, and step through code. Spend a few hours to learn one so you can avoid dozens of hours of frustration in the future. This is a quick guide, more information here:
thank you for this short tut, it’s very helpful.
It would be good if you make another one to us how to integrate the debugger in the source code (#define …)
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;”), it’s 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 I’m wrong.
You can set conditions on breakpoints. E.g. “cond 3 (x==2)” will stop at breakpoint 3 only when x==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…
When using “watch”, make sure gdb says “hardware watchpoint set”. Unlike software watchpoints, these do not slow down program execution.
It is possible to define macros (using define xxx … end) in .gdbrc
Other useful features I haven’t used:
gdb7 supports reverse debugging
gdb7 is scriptable using Python. Together with the new libstdc++ you get pretty printing of C++ STL collection classes.