[lug] C++ question: Stack info at runtime?
D. Stimits
stimits at idcomm.com
Thu Feb 28 19:25:27 MST 2002
"Scott A. Herod" wrote:
>
> Hello,
>
> Java's got a nice method for getting the stack trace at runtime.
> I use it often for debugging as an "assert" mechanism. Is there
> something similar that I can do with C++?
>
> Thanks,
>
> Scott
> _______________________________________________
> Web Page: http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
There is if you use -rdynamic and -g. Incidentally, this may cause
normal core dumps to provide less or different info. Here is a basic
sample of a stack frame print function, which is probably specific to
linux:
#include <iostream>
#include <execinfo.h>
void show_stackframe()
{
void* trace[ 64 ];
char** messages = NULL;
int trace_size = 0;
cerr << "Begin Stack Frame Dump" << endl;
cerr << "(if useful symbols are not found, try recompiling "
<< "with -rdynamic during link, and -g without -O#)"
<< endl;
trace_size = backtrace( trace, 64 );
messages = backtrace_symbols( trace, trace_size );
for( int i = 0; i < trace_size; ++i ) {
cerr << "Traced: " << messages[i] << endl;
}
cerr << "End Stack Frame Dump" << endl;
}
I've used this at times to debug asyncronous code and figure out what
had been called from where, I actually keep a commented out version in
my game code.
D. Stimits, stimits at idcomm.com
More information about the LUG
mailing list