Thank You, GDB!

by chromatic

I program in C very reluctantly. I don't hate the language, but it occupies a curious niche between assembly language (where you can do absolutely anything, if you're willing to write it yourself, and eval is trivial) and a true high level language (where you can do absolutely anything, you don't have to write it yourself, and eval is available for everything else). Yet it's ubiquitous, it has a lot of libraries, and it's probably the best way to write reasonably efficient code that has to run on plenty of platforms.

Because I'm writing a lot more C code lately (and of that, finding and fixing a lot of bugs), I've spent a lot of time using the GNU debugger GDB.

As a reluctant programmer in general, I spent many years happily debugging with print statements, and then plenty of years debugging with comprehensive test cases. When you're writing a virtual machine and your test cases are all in a high level language, you don't always have that luxury, especially when you have segfaults.

I already knew the value of backtraces, breakpoints, and printing the value of local variables. Then I forced myself to learn a few more tricks to make the most of the debugger. For example, breakpoints can take conditions. That is, you can write break src/exceptions.c:59 if exception->type == exception_class_NULLACCESS. Learning that alone paid off several times over.

The other feature I forget after a couple of months away from marathon debugging sessions is that p can dereference a pointer to a struct. That is, if you have a Coord pointer in the variable coords, use p *coords to see a serialized version of the struct and its contents. Handy!

I could talk more about using up and down to walk up and down the call stack after a breakpoint, but even learning only three or four useful commands has already cut out hours of debugging time in the past month. (I even found myself wishing for a better debugger in one of the HLLs I was working on.)

Thanks to all of the contributors to GNU GDB and its ecosystem; you've made it easier for me to write further free software.


2007-07-16 17:04:19
A couple of years ago my hippy father in law gave our young son a kids' book called "Thank You Sun, Thank You Moon" which was full of thank yous to all sorts of things (thank you water, thank you birds - you get the picture), and every time I see an entry in this series of yours it makes me think of that.

Don't get me wrong, I understand what you're trying to do - there's precious little appreciation of good work in our world - but it does always give me a little chuckle.

2007-07-17 21:09:36
Ever tried debugging Visual studio!
Dusan Peterc
2007-07-18 13:30:43
Then do yourself a favor and try it with DDD
Being lazy, reluctant to read documentation, I still figured out how to make a breakpoint (double click on the line), watch and examine a variable / structure, or backtrack the function calls after a core dump.
To me, using a debugger without a graphical font end, is like writing machine code instead of C.
Ralph Corderoy
2007-07-23 15:25:21
Following on from DDD, use Delta Debugging. They've the same author IIRC.
2007-09-03 23:20:34
GDB is probably the life saving took for all C programmers. The more you talk the less it is. All for free. I am a huge fan of free-software and intent to write more about them in my blogs. Interesting I have an article on GDB now at my blog.