Skip to main content

Printing a backtrace from inside a C program

While digging in the latest code of htop in hunt of a resizing bug I came across calls to functions backtrace(3) and backtrace_symbols_fd(3), which I didn't know about before. htop installs a custom segfault handler which tries to shut down curses and print a stacktrace using the backtrace(3) function. Example output looks like this:

htop 0.9 aborting. Please report bug at
/lib64/ **sprintf** _chk+0x87)[0x7fbbca558237]

While that could be more helpful, it does give ahint that sprintf is involved in the crash - it actually is. Things get interesting when you deal with memory corruption so that the call to backtrace inside your segfault handler segfaults itself. In that case you get:

htop 0.9 aborting. Please report bug at
Segmentation fault

That's not helpful anymore. Still, not all segfaults are memory corruptions!

If you're interested in adding this feature to your own program or at least would like to play with it, here is some dedicated demo code:

/* Licensed as Public Domain */
#include <stdio.h>
#include <execinfo.h>
#include <signal.h>
#include <stdlib.h>

static void dump_trace() {
    void * buffer[255];
    const int calls = backtrace(buffer,
        sizeof(buffer) / sizeof(void *));
    backtrace_symbols_fd(buffer, calls, 1);

void boom() {
    int a[10];
    printf("%d", a[-100000]);

void two() { boom(); }
void one() { two(); }

int main() {
    signal(SIGSEGV, dump_trace);
    return 0;

In my Makefile I have

NUMBERS: backtrace.c
    $(CC) -o $@ $<

NAMES: backtrace.c
    $(CC) **-rdynamic** -o $@ $ <

Without -rdynamic you get numbers, only -- like this:


In contrast, with -rdynamic you get function names where available:

# ./NAMES 

The same job (and a bit more) is also done by library (and tool) stacktrace. I played with it (and put an ebuild into the betagarden overlay) but it's not working for me as expected, yet. I have contacted upstream about it and hope for some enlightening reply in the next few days.