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.