Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2970807
  • 博文数量: 685
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5303
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-19 14:17
个人简介

文章分类

全部博文(685)

文章存档

2015年(116)

2014年(569)

分类: LINUX

2015-01-15 11:51:08

原文地址:
http://blog.chinaunix.net/uid-26772137-id-3361248.html
http://blog.csdn.net/wind19/article/details/6105617

源程序如下

#include
#include
#include

typedef uint32_t UINT32;

void fun3(void)
{
  void* array[10] = {0};
  UINT32 size = 0;
  char **strframe = NULL;
  UINT32 i = 0, j = 0;
  
  size = backtrace(array, 10);
  strframe = (char **)backtrace_symbols(array, size);
  
  printf("print call frame now:/n");
  for(i = 0; i < size; i++){
    printf("frame %d -- %s/n", i, strframe[i]);
  }
  
  if(strframe)
  {
    free(strframe);
    strframe = NULL;
  }
}

void fun2(void)
{
  fun3();
}

void fun1(void)
{
  fun2();
}

int main(void)
{
  fun1();
  return 0;
}

 

linux-xms:/data/test # gcc  test.c

linux-xms:/data/test # ./a.out

print call frame now:
frame 0 -- ./a.out [0x80484fe]
frame 1 -- ./a.out [0x8048582]
frame 2 -- ./a.out [0x804858f]
frame 3 -- ./a.out [0x80485a7]
frame 4 -- /lib/libc.so.6(__libc_start_main+0xdc) [0xb7e188ac]
frame 5 -- ./a.out [0x8048431]

只能看到地址

 

修改编译参数

linux-xms:/data/test # gcc -rdynamic  test.c 
linux-xms:/data/test # ./a.out 
print call frame now:
frame 0 -- ./a.out(fun3+0x4a) [0x80486de]
frame 1 -- ./a.out(fun2+0xb) [0x8048762]
frame 2 -- ./a.out(fun1+0xb) [0x804876f]
frame 3 -- ./a.out(main+0x16) [0x8048787]
frame 4 -- /lib/libc.so.6(__libc_start_main+0xdc) [0xb7e588ac]
frame 5 -- ./a.out [0x8048611]

现在可以看到函数名了,但没有行号,不过没关系addr2line提供了这个功能

 

然后我们试图用addr2line来看地址对应的函数和行号

linux-xms:/data/test # addr2line 0x80486de -e ./a.out -f
fun3
??:0

失败了,别急,我们再次修改编译参数

linux-xms:/data/test # gcc -g -rdynamic test.c 
linux-xms:/data/test # ./a.out 
print call frame now:
frame 0 -- ./a.out(fun3+0x4a) [0x80486de]
frame 1 -- ./a.out(fun2+0xb) [0x8048762]
frame 2 -- ./a.out(fun1+0xb) [0x804876f]
frame 3 -- ./a.out(main+0x16) [0x8048787]
frame 4 -- /lib/libc.so.6(__libc_start_main+0xdc) [0xb7dcb8ac]
frame 5 -- ./a.out [0x8048611]
linux-xms:/data/test # addr2line 0x80486de -e ./a.out -f
fun3
/data/test/test.c:14

这次我们成功了,多加了-g参数。

 

 

 

backtrace和backtrace_symbols的具体说明可以参考




阅读(1460) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~