[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

6.5 How can I generate a stack dump from within a running program?

Some systems provide library functions for unwinding the stack, so that you can (for example) generate a stack dump in an error-handling function. However, these are highly system-specific, and only a minority of systems have them.

A possible workaround is to get your program to invoke a debugger on itself – the details still vary slightly between systems, but the general idea is to do this:

 
void dump_stack(void)
{
    char s[160];

    sprintf(s, "/bin/echo 'where\ndetach' | dbx -a %d", getpid());
    system(s);
        
    return;
}

You will need to tweak the commands and parameters to dbx according to your system, or even substitute another debugger such as gdb, but this is still the most general solution to this particular problem that I've ever seen. Kudos to Ralph Corderoy for this one :-)

Here's a list of the command lines required for some systems:

Most systems using dbx

"/bin/echo 'where\ndetach' | dbx /path/to/program %d"

AIX

"/bin/echo 'where\ndetach' | dbx -a %d"

IRIX

"/bin/echo 'where\ndetach' | dbx -p %d"


[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated on September, 10 2007 using texi2html 1.77.