addr2line command in Linux with Examples

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 ...]

Note:

  • For the examples, we will be using the a.out file which is obtained after the following C program was compiled using gcc.

  • 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.:
    

Options:

  • -e : 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, press ctrl+c)
    addr2line -e a.out [addr1] [addr2]...

    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.
  • -f : If the -f option is specified, the name of the function to which the line address/line belongs is also printed.
    addr2line -e a.out -f [addr1] [addr2] ....
    

    Example 1:

    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.

    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.

  • -a, –addresses: This option is used to show the address before the function name, file and line number information.
  • -s, –basenames: This option is used to show only the base of each file name.
  • -j, –section: This option will read the offsets relatives to the specified section instead of the absolute addresses.
  • -p, –pretty-print: This option is used to make the output more human friendly.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.