Symbol table tool addr2line

The symbol table of an executable file (symbol table) records symbol information such as function names, global variables, and macro definitions in an executable file. This information is very important for our debugging.

The executable program with the symbol table contains debugging information, and one of the most important pieces of data is the corresponding relationship between the line number of the program source program and the compiled machine code, the Line Number Table. The Line Number Table in DWARF format is a highly compressed data that stores the difference between the two rows before and after the table. When parsing debugging information, the Line Number Table needs to be rebuilt in memory according to the rules before it can be used.

Line number information must be compiled with -g.

addr2line gets the function line according to the symbol table-address

#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;
}

The Line Number Table is stored in the .debug_line domain of the executable program, use the command

$readelf -w a.out
## Output DWARF debugging information
Line Number Statements:
  [0x000000bf]  Extended opcode 2: set Address to 0x64a
  [0x000000ca]  Special opcode 9: advance Address by 0 to 0x64a and Line by 4 to 5
  [0x000000cb]  Special opcode 146: advance Address by 10 to 0x654 and Line by 1 to 6
  [0x000000cc]  Special opcode 104: advance Address by 7 to 0x65b and Line by 1 to 7
  [0x000000cd]  Special opcode 36: advance Address by 2 to 0x65d and Line by 3 to 10
  [0x000000ce]  Special opcode 118: advance Address by 8 to 0x665 and Line by 1 to 11
  [0x000000cf]  Special opcode 104: advance Address by 7 to 0x66c and Line by 1 to 12
  [0x000000d0]  Special opcode 104: advance Address by 7 to 0x673 and Line by 1 to 13
  [0x000000d1]  Advance PC by 42 to 0x69d
  [0x000000d3]  Special opcode 6: advance Address by 0 to 0x69d and Line by 1 to 14
  [0x000000d4]  Special opcode 76: advance Address by 5 to 0x6a2 and Line by 1 to 15
  [0x000000d5]  Advance PC by 2 to 0x6a4
  [0x000000d7]  Extended opcode 1: End of Sequence
## run a.out
$./a.out

## Display kernel buffer (with coredump information and boot information)
# empty
$dmesg -C
# Record
$dmesg

The number after the ip field is the location where the program is executed when the program fails

[ 1246.281072] traps: a.out[3368] trap divide error ip:556663950658 sp:7fff4e09a9c0 error:0 in a.out[556663950000+1000]

addr2line - Reverse line number using signed executable address/library offset

Given an address in an executable or an offset in a section of a relocatable object, it uses the debugging information to figure out which file name and line number are associated with it.
$addr2line -e a.out 658
/home/ubuntu/daily/test.cpp:6

parameter

-C, remove the automatically added prefix and suffix

-i, display inline functions

-p, the output conforms to human aesthetics

-e, specify the file, default=a.out

common problem

addr2line

??:0 # The address is wrong, it has a symbol table (or it has been stripped, and the symbol table is stripped)

  • Try replacing with absolute/relative address

??:? # itself has no symbol table (!-g)

  • Compiled without -g

related functions

$nm

-C, remove the automatically added prefix and suffix

-n, can sort by offset

-l, display line number

  • You can view the function of the problem point, there is no row information

  • If down is in the library, it is used to determine which library function down is in

# Remove the debug information of the executable file to make it smaller (smaller than -O3)
$strip

associate

nm==? function name map, that is, the map file:

-Wl,-Map=test.map

application

A signed bin/so file, a crash stack or asan stack for an unsigned bin/so file.

Can be put together to parse out a backtrace with function lines.

principle

The same code, the same compiler, compiled with g and compiled without g, the memory distribution of the code segment is the same.

The program runs and debugs multiple times, the loading address (start address of main) may be different, but the offset of the function is always the same.

refer to

addr2line

https://blog.csdn.net/u013827488/article/details/107048407/

nm

https://blog.csdn.net/qq_34026204/article/details/125415233

Tags: C++

Posted by d0rr on Tue, 28 Feb 2023 05:48:38 +0530