����Linux�Ż�
Linux����

linux addr2line��ʵ��

����ʱ��:2017-02-25 09:52:45��Դ:linux��վ����:swartz_lubel
��Linux��дC/C++����ij���Ա��ʱ����Core Dump��������ڴ�Խ����ʣ��յ����ܴ������źţ�����ȴ������ʱ�����Ǿ��Ļ򲻾���д�͵ij����ֱ���˷��ˣ�Core Dump��Linux�ʴȵ����µij����Ӧ�ã���������Ա�ǽ����һ����һ�����⡣
��ʱ���ò�������Linuxֱ�Ӹ�û�ˣ�û����Core�ļ�������ʱ���պô��̿ռ䲻�㣬Core�ļ�д�����ˡ�û��Core�ļ���ʱ�����֪��������ʲô�ط��������أ�addr2line������ʱ�����ó���
 
����һ��ʾ������func�������ز���a���Բ���b�Ľ��������ʹ��0��Ϊ������������dz�����Ϊ����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�dz����ļ�����ִ�г��򣬽�������쳣�жϡ��鿴ϵͳ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����ҵ�����һ���ء��ڿ�ִ�г����ж������е�����Ϣ�����к���Ҫ��һ�����ݾ��dz���Դ������кźͱ����Ļ�������֮��Ķ�Ӧ��ϵLine Number Table��DWARF��ʽ��Line  Number Table��һ�ָ߶�ѹ�������ݣ��洢���DZ���ǰ�����еIJ�ֵ���ڽ���������Ϣʱ����Ҫ���չ������ڴ����ؽ�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�Զ����ܰ�Դ���еij���λ���ҳ�������Ҳ����Linux��㻵�ˡ�
 
�������ø��µ�ַ��//m.ajphoenix.com/linux/28679.html