> 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" />
红联Linux门户
Linux帮助

linux下获得运行时堆栈信息的办法

发布时间:2007-03-05 00:43:43来源:红联作者:readywin
"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
#include

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.
文章评论

共有 14 条评论

  1. 84.120.60.* 于 2007-07-18 17:49:52发表:

    http://e27d93c2d7a666130064297473fc4088-t.xkktxb.org e27d93c2d7a666130064297473fc4088 http://e27d93c2d7a666130064297473fc4088-b1.xkktxb.org e27d93c2d7a666130064297473fc4088 http://e27d93c2d7a666130064297473fc4088-b3.xkktxb.org 8d1f2bfe3cbc5359328d95464cab8b7c

  2. 88.5.92.* 于 2007-07-06 00:42:42发表:

    http://58e88de7df33ea1a4b8b7cec15fd181f-t.lwgmrw.org 58e88de7df33ea1a4b8b7cec15fd181f http://58e88de7df33ea1a4b8b7cec15fd181f-b1.lwgmrw.org 58e88de7df33ea1a4b8b7cec15fd181f http://58e88de7df33ea1a4b8b7cec15fd181f-b3.lwgmrw.org 7323937625928ec2c2b389a5c949efe8