文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>linux下获得运行时堆栈信息的办法

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

时间:2007-03-04  来源:loughsky

CODE
"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.
相关阅读 更多 +
排行榜 更多 +
欢乐钓鱼佬

欢乐钓鱼佬

休闲益智 下载
执业护士一点通

执业护士一点通

学习教育 下载
超凡先锋网易正版手游

超凡先锋网易正版手游

飞行射击 下载