A cool little bit of code...

Owen Stampflee ostampflee at terrasoftsolutions.com
Tue Jan 18 19:55:54 MST 2005


I call it minidebugger... I dont have a proper debugger available during
a real install (and the problem doesnt exist in the test environment),
so I'm bundling this little code in. When the code segfaults, I get a
nice little backtrace :).

#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
 
/* Obtain a backtrace and print it to stdout. */
void print_trace (int sig)
{
        void *array[10];
        size_t size;
        char **strings;
        size_t i;
 
        size = backtrace (array, 10);
        strings = backtrace_symbols (array, size);
 
        printf ("Obtained %zd stack frames.\n", size);
 
        for (i = 0; i < size; i++)
                printf ("%s\n", strings[i]);
 
        free (strings);
        exit(-1);
}
 
void kaboom()
{
        printf("Going kaboom...");
        int *foo = NULL;
        *foo = 42;
}
 
int main (void)
{
        signal(SIGSEGV, print_trace);
        kaboom();
        return 0;
}




More information about the yellowdog-general mailing list