Debugging with GDB

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:

This is a companion discussion topic for the original entry at

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. :slight_smile:

Thanks Frodo, didn’t know they had GNU tools in the shire :slight_smile:

Great catch, I’ll update the article. Nejd, I’ll put up some examples of the debugging too, thanks.

[…] ψ Debugging With Gdb Betterexplained - List of all available sources from internet on : Debugging With Gdb Betterexplained […]

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.

How I can change the value of a local or global variable in gdb?

Beautiful GDB tut, easy, precise, and to the point, def the best for quick no brainer questions!

@Juan: I believe you can just do “set x = 3”

@Mike: Thanks, glad you enjoyed it!

[…] Перевод, оригинал найден здесь. […]

[…] Debugging with GDB | BetterExplained (tags: debugging iphone osx gdb) […]

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 haven’t 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.

can we load more den one exe? d one we did above “gdb a.out”.

can it be like “gdb a.out b.out”?

I have been using gdb minimally. but some of the extra info here is really hepful. Thanks for the turorial.

@Anonymous: The gdb command line help indicates you can only load a single file.

[…] bits set from an integer number Will Microsoft certification help me find a job or advance? Debugging with GDB | BetterExplained Web workers: errors and debugging […]

Great article.

Good Intro… Very helpful.

@Paul: Thanks!

thanks, gdb tutorial really helped.