免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4134 | 回复: 3
打印 上一主题 下一主题

[Linux] 如何打印出call trace中的函数名 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-08-27 23:24 |只看该作者 |倒序浏览
写了个程序,当程序异常时,捕捉内核发送的信号,然后想把栈上的call trace打印出来;

问题是,栈上都是函数地址,如何找到地址所对应的函数名呢?

我目前都是手动反汇编ELF文件,然后根据地址去找函数,有没有方法可以让程序自己找到地址所对应的函数名呢?

论坛徽章:
95
程序设计版块每日发帖之星
日期:2015-09-05 06:20:00程序设计版块每日发帖之星
日期:2015-09-17 06:20:00程序设计版块每日发帖之星
日期:2015-09-18 06:20:002015亚冠之阿尔艾因
日期:2015-09-18 10:35:08月度论坛发贴之星
日期:2015-09-30 22:25:002015亚冠之阿尔沙巴布
日期:2015-10-03 08:57:39程序设计版块每日发帖之星
日期:2015-10-05 06:20:00每日论坛发贴之星
日期:2015-10-05 06:20:002015年亚冠纪念徽章
日期:2015-10-06 10:06:482015亚冠之塔什干棉农
日期:2015-10-19 19:43:35程序设计版块每日发帖之星
日期:2015-10-21 06:20:00每日论坛发贴之星
日期:2015-09-14 06:20:00
2 [报告]
发表于 2012-08-28 09:43 |只看该作者

论坛徽章:
0
3 [报告]
发表于 2012-08-28 11:46 |只看该作者
连接十分不错~~  刚刚的~~   
  1. With the GNU C Library, you can use the backtrace module. Here is an example for that:

  2. #include <stdio.h>
  3. #include <execinfo.h>
  4. #include <stdlib.h>


  5. void handler(char *caller) {
  6.   void *array[10];
  7.   size_t size;
  8.   printf("Stack Trace Start for %s\n",caller);
  9.   size = backtrace(array, 10);
  10.   backtrace_symbols_fd(array, size, 2);
  11.   printf("Stack Trace End\n");
  12. }

  13. void car() {
  14.     handler("car()");
  15.     printf("Continue Execution");
  16. }
  17. void baz() {car(); }

  18. void bar() { baz(); }
  19. void foo() { bar(); }


  20. int main(int argc, char **argv) {
  21.   foo();
  22. }
  23. compile with -g -rdynamic compiler option to load the symbols

  24. gcc -g -rdynamic Test1.c -o Test
  25. You will see an output similar to

  26. Stack Trace Start for car()
  27. ./Test(handler+0x2d)[0x80486f1]
  28. ./Test(car+0x12)[0x804872e]
  29. ./Test(baz+0xb)[0x8048747]
  30. ./Test(bar+0xb)[0x8048754]
  31. ./Test(foo+0xb)[0x8048761]
  32. ./Test(main+0xb)[0x804876e]
  33. /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0x126e37]
  34. ./Test[0x8048631]
  35. Stack Trace End
  36. Continue Execution in car
  37. You can write this handler function and call from anywhere in your program at any number of time. Remember to increase the array size as required.
复制代码

论坛徽章:
0
4 [报告]
发表于 2014-10-31 15:38 |只看该作者
Very good. Thank you very much!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP