如何获得更详细的回溯 [英] How to get more detailed backtrace

查看:214
本文介绍了如何获得更详细的回溯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打印一个回溯我的C ++程序终止时。打印功能回溯是像下面;

I am trying to print a backtrace when my C++ program terminated. Function printing backtrace is like below;

   void print_backtrace(void){

       void *tracePtrs[10];
       size_t count;

       count = backtrace(tracePtrs, 10);

       char** funcNames = backtrace_symbols(tracePtrs, count);

       for (int i = 0; i < count; i++)
           syslog(LOG_INFO,"%s\n", funcNames[i]);

       free(funcNames);

}

它给像一个输出;

It gives an output like ;

   desktop program: Received SIGSEGV signal, last error is : Success
   desktop program: ./program() [0x422225]
   desktop program: ./program() [0x422371]
   desktop program: /lib/libc.so.6(+0x33af0) [0x7f0710f75af0]
   desktop program: /lib/libc.so.6(+0x12a08e) [0x7f071106c08e]
   desktop program: ./program() [0x428895]
   desktop program: /lib/libc.so.6(__libc_start_main+0xfd) [0x7f0710f60c4d]
   desktop program: ./program() [0x4082c9]

有没有办法让与函数名和线条更详细的回溯,如GDB输出?

Is there a way to get more detailed backtrace with function names and lines, like gdb outputs?

推荐答案

是 - 通过-rdynamic标志的链接。这将导致链接器出在连接表的所有没有静态函数的名称与code,而不仅仅是出口的。

Yes - pass the -rdynamic flag to the linker. It will cause the linker to out in the link tables the name of all the none static functions in your code, not just the exported ones.

您付出的代价是你的程序非常稍长的启动时间。对于中小型项目,你不会注意到它。你得到的是该回溯()是能够给你的所有无静态函数的名称在你的背部走线。

The price you pay is a very slightly longer startup time of your program. For small to medium programs you wont notice it. What you get is that backtrace() is able to give you the name of all the none static functions in your back trace.

不过 - 请注意:还有,你需要知道的几个陷阱:

However - BEWARE: there are several gotchas you need to be aware of:


  1. backtrace_symbols通过malloc分配内存。如果你陷入了SIGSEGV由于malloc的舞台腐败(很常见),你将在这里双误,从来没有看到你的背部走线。

  1. backtrace_symbols allocates memory from malloc. If you got into a SIGSEGV due to malloc arena corruption (quite common) you will double fault here and never see your back trace.

根据本上运行(例如x86)的平台,在这里你会坠毁的地方被替换堆栈与信号处理程序的返回地址的确切函数的地址/函数名上。你需要从这些平台的信号处理程序的参数功能坠毁的右侧EIP。

Depending on the platform this runs on (e.g. x86), the address/function name of the exact function where you crashed will be replaced in place on the stack with the return address of the signal handler. You need to get the right EIP of the crashed function from the signal handler parameters for those platforms.

日志不是异步信号安全功能。发生碰撞时,它可能采取适度的锁,如果锁被采取了(因为你在另一个调用系统日志中部坠毁),你有一个死锁

syslog is not an async signal safe function. It might take a lock internally and if that lock is taken when the crash occurred (because you crashed in the middle of another call to syslog) you have a dead lock

如果您想了解所有的血淋淋的细节,看看我的这段视频给人的OLS一说起吧:<一href=\"http://free-electrons.com/pub/video/2008/ols/ols2008-gilad-ben-yossef-fault-handlers.ogg\">http://free-electrons.com/pub/video/2008/ols/ols2008-gilad-ben-yossef-fault-handlers.ogg

If you want to learn all the gory details, check out this video of me giving a talk about it at OLS: http://free-electrons.com/pub/video/2008/ols/ols2008-gilad-ben-yossef-fault-handlers.ogg

这篇关于如何获得更详细的回溯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆