无core文件时应用addr2line定位死机位置





在Linux下写C/C++程序的程序员,时常与Core Dump相见。在内存越界访问,收到不能处理的信号,除零等错误出现时,我们精心或不精心写就的程序就直接一命呜呼了,Core Dump是Linux仁慈地留下的程序的尸体,帮助程序员们解决了一个又一个问题。

有时配置不给力,Linux直接毁尸灭迹,没有了Core文件;又有时,刚好磁盘空间不足,Core文件写不下了。没有Core文件的时候,如何知道程序在什么地方出错了呢?addr2line就在这时派上用场。


arm-eabi-addr2line 

Usage: arm-eabi-addr2line [option(s)] [addr(s)]

for example:

D:\gat\iBox\TRUNK\lib>arm-eabi-addr2line -e "D:\*.out.symbols\alps\out\target\product\*\symbols/../../../../../kernel/vmlinux" c0037aa0

 The options are:
  @<file>                Read options from <file>
  -b --target=<bfdname>  Set the binary file format
  -e --exe=<executable>  Set the input file name (default is a.out)
  -i --inlines           Unwind inlined functions
  -j --section=<name>    Read section-relative offsets instead of addresses
  -s --basenames         Strip directory names
  -f --functions         Show function names
  -C --demangle[=style]  Demangle function names
  -h --help              Display this information
  -v --version           Display the program's version.

常用-f, -C, -e.



这是一个示例程序,func函数返回参数a除以参数b的结果。这里使用0作为除数,结果就是程序因为除以0导致错误,直接中断了。


#include <stdio.h>

int func(int a, int b)
{
  return a / b;
}

int main()
{
  int x = 10;
  int y = 0;
  printf("%d / %d = %d\n", x, y, func(x, y));
  return 0;
}

 

使用

$ gcc -o test1 -g test1.c

编译程序,test1.c是程序文件名。执行程序,结果程序异常中断。查看系统dmesg信息,发现系统日志的错误信息:

[54106.016179] test1[8352] trap divide error ip:400506 sp:7fff2add87e0 error:0 in test1[400000+1000]

这条信息里的ip字段后面的数字就是test1程序出错时所程序执行的位置。使用addr2line就可以将400506转换成出错程序的位置:

$ addr2line -e test1 400506
/home/hanfoo/code/test/addr2line/test1.c:5

这里的test1.c:5指的就是test1.c的第5行

return a / b;  

也正是这里出现的错误。addr2line帮助我们解决了问题。

 

addr2line如何找到的这一行呢。在可执行程序中都包含有调试信息,其中很重要的一份数据就是程序源程序的行号和编译后的机器代码之间的对应关系Line Number Table。DWARF格式的Line  Number Table是一种高度压缩的数据,存储的是表格前后两行的差值,在解析调试信息时,需要按照规则在内存里重建Line Number  Table才能使用。

Line Number Table存储在可执行程序的.debug_line域,使用命令

$ readelf -w test1

可以输出DWARF的调试信息,其中有两行

Special opcode 146: advance Address by 10 to 0x4004fe and Line by 1 to 5  

Special opcode 160: advance Address by 11 to 0x400509 and Line by 1 to 6  


这里说明机器二进制编码的0x4004fe位置开始,对应于源码中的第5行,0x400509开始就对应与源码的第6行了,所以400506这个地址对应的是源码第5行位置。

 

addr2line通过分析调试信息中的Line Number Table自动就能把源码中的出错位置找出来,再也不怕Linux毁尸灭迹了。

 

for example:

prebuilts/tools/gcc-sdk/addr2line -e out/target/product/z4dtg/obj/EXECUTABLES/xxxxx_intermediates/LINKED/xxxxxxxx  0x00007165



利用dmesg和addr2line来重现bug

有些时候,我们的程序crash了,但是我们没有保存core dump信息,这时如果我们想要知道程序在哪个位置出错,就不是那么容易了。

下面有一种方法,可以大致判断出程序出错的大致位置。


1.用dmesg查找出错的代码段地址。
命令格式:

dmesg | grep program_name

其中program_name是可执行文件,比如:

$ dmesg | grep test_prog
[103936.227079] test_prog[29319]: segfault at 40078c ip 0000000000400634 sp 00007fffe54d4680 error 7 in test_prog[400000+1000]

其中的ip后面的地址是程序出错处的地址。

2.用addr2line将地址解析成函数名。
紧接上面的例子:

$ addr2line -e ./test_prog 0000000000400634 -f
_Z9errorFuncv
??:0

其中errorFunc即是出错的函数名,然后就可以找到相应的出错代码了。


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值