addr2line use command

Reprinted from: http://blog.csdn.net/lhf_tiger/article/details/9088609

Prerequisite: Compile option plus -g

How to use:   addr2line -e test1 400506 Add -f to display the function name.

Programmers who write C/C++ programs under Linux often meet Core Dump. When the memory is accessed out of bounds, the signal that cannot be handled is received, and errors such as division by zero occur, the program that we carefully or not carefully wrote will die directly. Core Dump is the corpse of the program left by Linux kindly to help programmers. Solved one problem after another.

Sometimes the configuration is not strong enough, and Linux directly destroys the corpse and disappears, and there is no Core file; sometimes, the disk space just happens to be insufficient, and the Core file cannot be written. When there is no Core file, how do you know where the program has gone wrong? This is where addr2line comes in handy.

Here is a sample program, the func function returns the result of dividing the parameter a by the parameter b. Here, 0 is used as the divisor, and the result is that the program is directly interrupted because of division by 0.


[cpp]  view plain  copy
  1. #include <stdio.h>  
  2.   
  3. int func(int a, int b)  
  4. {  
  5.   return a / b;  
  6. }  
  7.   
  8. intmain  ()  
  9. {  
  10.   int x = 10;  
  11.   int y = 0;  
  12.   printf("%d / %d = %d\n", x, y, func(x, y));  
  13.   return 0;  
  14. }  

 

use

$ gcc -o test1 -g test1.c

Compile the program, test1.c is the program file name. The program is executed, and the program is abnormally interrupted. Check the system dmesg information and find the error information in the system log:

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

The number after the ip field in this message is the location of the program execution when the test1 program fails. Use addr2line to convert 400506 to the location of the faulty program:

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

Here test1.c:5 refers to line 5 of test1.c

return a / b;  

It's also the error that occurs here. addr2line helped us solve the problem.

 

How does addr2line find this line? Debugging information is included in the executable program, and a very important piece of data is the Line Number Table of the correspondence between the line number of the program source program and the compiled machine code. The Line Number Table in DWARF format is a highly compressed data, which stores the difference between the two lines before and after the table. When parsing the debugging information, the Line Number Table needs to be reconstructed in memory according to the rules before it can be used.

The Line Number Table is stored in the .debug_line field of the executable program, using the command

$ readelf -w test1

Can output DWARF debug information, which has two lines

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  


This shows that the position of 0x4004fe in the binary code of the machine starts, which corresponds to the 5th line in the source code, and the beginning of 0x400509 corresponds to the 6th line of the source code, so the address 400506 corresponds to the 5th line of the source code.

 

addr2line can automatically find out the error location in the source code by analyzing the Line Number Table in the debugging information, and no longer afraid of the destruction of Linux.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325813268&siteId=291194637