×

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

Linux pwd

The pwd command is used to print or display the name of the working or current directory. Some shells have their own version of the pwd with several features, in...

1 minute read.

touch Command in Linux/Unix with Examples

The "touch" command in Linux (and other Unix-like operating systems) is used to create new empty files or update the timestamps of existing files. The touch command is a simple...

4 minutes read.

ls Command in Linux/Unix with Examples

The ls command in Linux is a command-line utility that is used to list the files and directories in a directory. It is one of the most basic and commonly...

4 minutes read.

tail Command in Linux/Unix with Examples

The "tail" command in Linux (and other Unix-like operating systems) is used to display the last few lines of a file. By default, the tail command will display the last...

4 minutes read.

Linux SCP Command

SCP stands for secure copy protocol. It is a command-line application for copying files and directories safely between two places. Command-line utilities are programs that operate on a computer's command...

3 minutes read.

sudo Command in Linux/Unix with Examples

The sudo command in Linux is used to run a command or set of commands with the privileges of another user, typically the root user. This allows users to perform...

3 minutes read.

pwd Command in Linux/Unix with Examples

The "pwd" command in Linux (and other Unix-like operating systems) stands for "print working directory." When executed, it simply displays the current directory (or folder) that the user is currently...

3 minutes read.

Linux Bash

Linux Bash In this tutorial, we are going to learn what the shell is? And how many various shells we have, we have also covered most of the bash shell features....

4 minutes read.

netstat Command in Linux/Unix with Examples

The "netstat" command in Linux is a command-line utility that displays network connections, routing tables, and various network statistics. It is used to display the status of network connections, both...

3 minutes read.

Linux Path Completion

Every operating system has some features which make them special. The Linux operating system primarily works on the command line interface, and we do have many features for this CLI....

1 minute 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.

Linux ls

The primary use case of the ls command is to list the content available in the directory. The default directory is the current directory of the user. The below screenshot...

5 minutes read.

Top 20 Linux Commands

1) clear command clear is a standard operating system command which is used to clear the terminal screen. This command brings the command line on the top of the terminal. By pressing the...

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

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.

Linux Absolute and Relative Path

The concept of the absolute and relative path is very simple. The names “relative path” and “absolute path” give us little understanding of the concept we are learning here. The...

1 minute read.

chown Command in Linux/Unix with Examples

The 'chown' command in Linux is used to change the ownership of a file or directory. The command stands for "change owner" and it allows you to change the user...

3 minutes read.

Linux Commands with Examples

Linux Commands with Examples All the Linux commands are executed on the terminal provided by the Linux system. This terminal is a command-line interface to implement and display command outputs. Linux...

10 minutes read.

awk Command in Linux/Unix with Examples

The 'awk' command in Linux is a powerful text processing tool that allows you to perform various operations on text files, such as searching, replacing, and manipulating text. The command...

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.