Easy Stack Traces

about | archive

[ 2008-April-03 20:42 ]

I like writing unit tests and using lots of assert() statements while writing C++ code. This means I spend a lot of time looking at stack traces to figure out where my tests are failing. I got annoyed with having to run my programs from GDB, when all I really want is the stack trace, with the functions, files and line numbers listed.

A little bit of searching revealed a mailing list posting with a preload library that will use glib's backtrace API to get a stack trace, and then use libbfd from GNU binutils to resolve file and line numbers. My system doesn't have this library, so I simplified it to only print the addresses. I then use a script to call addr2line to translate the addresses into line numbers. You can grab my code if you think it might be useful:

backtrace.c symbolize.py

I would like to integrate code to resolve files and line numbers, so I did some digging for what libraries can read DWARF debugging info. It turns out that there are a few options out there. Basically none of them will permit you to use them with commercial code, except FreeBSD's libdwarf which may not be complete yet. This probably isn't a practical issue for me, but I still find it interesting that there are so few packages out there for this important part of the tool chain. Here is a list of related software projects that I found:

binutils: C, GPL
Contains libbfd, which is what GDB uses.

libunwind: C, X11
Get stack traces from current and remote processes. No debugging info.

Breakpad: C, BSD
Get stack traces, send them to a server. Seems to be able to get debugging symbols?

libdwarf: C, LGPL
A reference implementation of sorts.

elfutils: C, GPL + linking exception for OSI-approved licences that require source code distribution with binaries
No real web presence, but it is used in Frysk.

libdwarf: C, BSD
A FreeBSD implementation of dwarf symbol manipulation.

libcwd: C++, QPL
A C++ implementation of dwarf symbols.