×

addr2line Command in Linux with Examples

Use of addr2line Command

The addr2line command is used to convert addresses to line numbers and file names. This command uses debugging info to check which line number and file name are connected with that address.

We use the -e option to specify the relocatable or executable object that is to be used. The file a.out is the default. The -j option clears the section to be used from the relocatable object.

There are two modes of operation for the addr2line command:

In the first mode of operation, on the command line, hexadecimal addresses are identified, and the line address and file name are shown for each address by the addr2line command.

In the second mode, hexadecimal addresses are read by addr2line through standard input, and on standard output, the line number and file name for each address on standard output are printed.

FILENAME: LINENO is the format in which output is displayed.

To generate additional lines before each FILENAME: LINENO line, two options can be used:

  • A line is displayed with the input address if the -e option is used.
  • And a line is displayed with the FUNCTIONNAME if the -f option is used. This signifies the function storing the address.

And to generate additional lines after the FILENAME: LINENO line, one option can be used:

  • If both, the code at the given address is available there due to inclining by the compiler, and the -i option is used, then after the addr2line line, additional lines are displayed. For each inline function, one or two extra lines are printed.

If addr2line fails to determine the function name or the file name, then two marks (??) are displayed in place of them. And addr2line displays 0, if it fails to determine the line number.

The syntax for the addr2line command:

addr2line [options] [addr addr ...]

Some of the options used in the syntax are:

-a --addressesThis option is used to print the address before the file, line number information, and the function name. To identify the address easily, a 0x prefix is printed along with it.
-b bfdname --target= bfdnameThis option shows that bfdname is the object-code format for the object file.
-C --dmangle[=style]This option helps in decoding low-level symbol names to user understandable names. Every compiler has itsmangling style.
-e filename --exe= filenameThis option helps in identifying the name of the executable for which addresses will have to be translated.
-f --functionsThis option prints the name of the function along with the line number information and file name.
-s --basenamesThis option prints the base of every file.
-i --inlinesThis option prints all the source information for all enclosing scopes, even the first inlined function; if the address belongs to a function that was inlined.
-j --sectionThis option reads the offsets respective to the specified part in place of the complete address.
-p --pretty-printThis option helps in making the output more user-friendly i.e., on a single line every location is printed.
@fileThis option reads the command-line option from the file. The options read are placed in the place of the @file option. In the file, options are separated by whitespace.

Note: For the examples, we are going to use the a.out file, which can be obtained after the compilation of the following C program:

#include<stdio.h>
#include<conio.h>
int main()
{
 printf("hello \n");
 getch();
return 0;
}

We are also going to use the de-assembled code of the a.out executable file. To get the de-assembled code, the following command is used:

objdump -D a.out | grep -A20 main.
addr2line Command in Linux with Examples

Using Options

  1. -e: This option helps in identifying the name of the executable for which addresses will have to be translated. The addresses from this file will be converted to their line number and file name.

Note: Press ctrl + c, to exit from the addr2line command.

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

addr2line Command in Linux with Examples

From this, we can deduce that:

  • To produce a proper output, the code segment has to be produced with the debug information.
  • All offsets of a program are not able to give the correct line number and file name. As we can see in the above image even though 651 is the offset available in the main function, its file name, and line number cannot be tracked.
  • -f: This option prints the name of the function along with the line number information and file.
addr2line -e a.out -f [addr1] [addr2] ....
addr2line Command in Linux with Examples

From this example, we can deduce that:

  • The line number and file name are printed along with the function name if the line number and file name can be mapped.
  • The function name will be printed even if the line number and file name cannot be mapped.

Example:

#include<stdio.h>
#include<conio.h>
int Yolo()
{
 return 10;
}
int main()
{
 int a,b;
 printf("hello \n");
  a= Yolo;
  b= Yolo;
  getch();
   return 0;
}

Output:

addr2line Command in Linux with Examples

Note: To display the unassembled part of the Yolo function and the segment following it, the obj command is used along with grep -A20 Yolo.


Related Topics

chmod Command in Linux/Unix with Examples

The chmod command in Linux is a command-line utility that is used to change the permissions of files and directories. It stands for "change mode" and it is used to...

4 minutes read.

du Command in Linux/Unix with Examples

The "du" command in Linux (and other Unix-like operating systems) is used to estimate the space used by a file or directory. The du command stands for "disk usage," and...

4 minutes read.

rm Command in Linux/Unix with Examples

The "rm" command in Linux (and other Unix-like operating systems) is used to remove files and directories. The "rm" command stands for "remove" and it is used to delete files...

4 minutes read.

mv Command in Linux/Unix with Examples

The 'mv' command in Linux is used to move or rename files and directories. The command stands for "move" and it allows you to move files and directories from one...

3 minutes read.

Rename Folder in Linux

Rename Folder in Linux In the Linux system, there is not any single dedicated command for renaming the files and directories. Mostly, the mv command is used extensively for the purpose...

3 minutes read.

df Command in Linux/Unix with Examples

The 'df' command in Linux is used to display the amount of disk space available on the file system. The command reports the amount of disk space used and available...

3 minutes read.

tar Command in Linux/Unix with Examples

The 'tar' command in Linux is used to create, maintain, and extract files from archive files called tarballs. The command stands for "tape archive" and it allows you to combine...

4 minutes read.

Difference Between Linux and Windows

Linux vs. Windows Here in this tutorial, we are going to discuss several features of Linux and Windows. These features or characteristics help us to come to a conclusion about which...

5 minutes read.

adduser Command in Linux with Examples

What is the adduser command? The adduser command creates a new user, and this command may be used to add apre-existing user to a group that already exists. How to use the...

4 minutes read.

ping Command in Linux/Unix with Examples

The "ping" command in Linux (and other Unix-like operating systems) is used to test the connectivity between two networked devices. The "ping" command sends a small packet of data, called...

4 minutes read.

grep Command in Linux/Unix with Examples

The grep command in Linux is a command-line utility that searches for patterns in text files. It stands for "global regular expression print" and it is used to search for...

5 minutes read.

find Command in Linux/Unix with Examples

The find command in Linux is a command-line utility that is used to search for files and directories in a file system. It is a versatile command that can be...

5 minutes read.

Linux Distributions

Linux Distributions Linux OS is not like other operating systems. Linux can be modified according to the user’s requirements, and it is very flexible to any change. Linux distributions are often...

4 minutes read.

scp Command in Linux/Unix with Examples

The scp (secure copy) command in Linux is a command-line utility that is used to securely transfer files between two hosts over a network. It is similar to the cp...

4 minutes read.

Linux Commands List

Linux Commands List The Linux systems are filled with commands for several use cases. We have divided these commands into some of the most common implementations for a better understanding of...

2 minutes read.

cat Command in Linux/Unix with Examples

The cat command in Linux is a command-line utility that is used to display the contents of a text file. It stands for "concatenate" and it is used to view...

4 minutes read.

addgroup Command in Linux with Examples

What is the addgroup command? It is a command in Linux to create a new group using specified command lined and the system’s default values. How to add groups Adding a user group:...

4 minutes read.

Linux Admin Tutorial

In publishing and graphic design, lorem ipsum is a placeholder text commonly used to demonstrate the visual form of a document without relying on meaningful content....

1 minute read.

gzip Command in Linux/Unix with Examples

The gzip command in Linux is used for compressing and decompressing files. It uses the Lempel-Ziv coding (LZ77) algorithm to compress files, which is known for its high compression ratio...

6 minutes read.

addr2line Command in Linux with Examples

Use of addr2line Command The addr2line command is used to convert addresses to line numbers and file names. This command uses debugging info to check which line number and file name...

4 minutes read.