通过该信息可以看出,ip 指向的地址为400524,得到该地址后,我们就可以使用 addr2line 来定位出错的位置。
addr2line -e div 400524 得到结果如下:
1
/home/bing/work/study/div.c:5
可以很直观的显示,该 core dump 信息是在div.c的 line 5。
addr2line 的原理
addr2line之所以能够利用可执行文件将地址信息转换为行号,是因为在可执行 ELF 文件中存有这些对应的信息。可以使用readelf来查看这些信息。例如上面的div文件,通过readelf div 可以看到如下信息:
123456789101112
Line Number Statements:
[0x00000026] Extended opcode 2: set Address to 0x400516
[0x00000031] Special opcode 8: advance Address by 0 to 0x400516 and Line by 3 to 4
[0x00000032] Special opcode 146: advance Address by 10 to 0x400520 and Line by 1 to 5
[0x00000033] Special opcode 104: advance Address by 7 to 0x400527 and Line by 1 to 6
[0x00000034] Special opcode 36: advance Address by 2 to 0x400529 and Line by 3 to 9
[0x00000035] Special opcode 216: advance Address by 15 to 0x400538 and Line by 1 to 10
[0x00000036] Special opcode 104: advance Address by 7 to 0x40053f and Line by 1 to 11
[0x00000037] Special opcode 105: advance Address by 7 to 0x400546 and Line by 2 to 13
[0x00000038] Special opcode 216: advance Address by 15 to 0x400555 and Line by 1 to 14
[0x00000039] Advance PC by 2 to 0x400557
[0x0000003b] Extended opcode 1: End of Sequence