Open In App

addr2line command in Linux with Examples

Last Updated : 23 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

‘addr2line’ command in Linux is used to convert addresses into file names and line numbers. When the executable files/object files are run with the ‘objdump’ command, the file is de-assembled and the machine code is displayed. These machine instructions for the executable are displayed along with their memory location (address). Using the addr2line command, one can map the address of the machine instruction to the line of the file from which it originated.

Syntax:

addr2line [options] [addr addr ...]

Basic addr2line command Example

For the examples, we will be using the ‘a.out’ file which is obtained after the following C program was compiled using ‘gcc’. addr2line command Example

We will also be using the de-assembled code of the ‘a.out’ executable file. The following command is used to get the ‘main’ part of the de-assembled code.

objdump -D a.out | grep -A20 main.:

Key Options of addr2line command

1. -e[file]:

The name of the executable for which addresses should be translated should be specified. The addresses from this file will be translated to their file name and line. (To exit the addr2line command, pressctrl+c’)

addr2line -e a.out [addr1] [addr2]...

-e[file]

From this, we can infer the following:

  • Only the code segment which is generated with the debug information could successfully generate a proper output.
  • Not all offsets of a program will successfully give the correct filename and line number. Here, even though 651 is an offset present in the main function, it’s line and filename can’t be traced back.

2. -f:

The -f option prints the name of the function associated with each address. This is especially useful when debugging larger programs.

addr2line -e a.out -f [addr1] [addr2] ....

Example 1:

Example 2: C file used:

Note: The obj command is used along with ‘grep -A20 yolo.’ to display the unassembled segment of the yolo function, and the part after it.

From this, we can infer the following:

  • If the file name and line can be mapped, then it is printed along with the function name. Here, the address 64a can be mapped both to the function and the file name + line.
  • Even if the file name + line can’t be mapped, the name of the function, which the address is a part of, is still printed. Here, even though the address can’t be mapped to the filename and line, the function name is still printed.

3. -a, –addresses:

This option is used to show the address before the function name, file and line number information.

addr2line -e a.out -a [addr1] [addr2]...

4. -s, –basenames:

Displays only the base of the file names instead of the full file path. This can simplify the output if full paths aren’t needed.

addr2line -e a.out -s [addr1] [addr2]...

5. -j, –section:

Specifies a section to read offsets relative to that section, instead of absolute addresses. This can be used when working with relocatable object files.

addr2line -e a.out -j .text [addr1] [addr2]...

6. -p, –pretty-print:

This option formats the output to be more human-readable, often adding indentation or line breaks for clarity.

addr2line -e a.out -p [addr1] [addr2]...

Conclusion

The addr2line command is an essential tool for developers working with compiled code and debugging. It allows the mapping of memory addresses back to the original source code, providing insight into where specific machine instructions came from. With options to display function names, section offsets, and simplified file names, addr2line provides flexibility for debugging and analyzing executables.


Next Article

Similar Reads