×

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

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.

Linux cd

Linux cd: As in GUI operating systems like Windows 10, we use the mouse to travel to various folders back and forth; similarly, we have the “cd” command in Linux...

4 minutes read.

Difference Between Unix and Linux

Unix vs Linux Unix and Linux both have been of great use in changing the world of operating systems. Unix was among the very few operating systems which were programmed for...

4 minutes read.

How to change the ApacheHTTP port in Linux

What is an Apache HTTP server in Linux? When we talk about the most commonly used web servers, one of the web servers that cannot be missed is Apache. With the...

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.

Linux Tutorial for Beginners

What is Linux? Linux is an open source Operating System like Windows, Android, Mac OS, and iOS. An operating system is a software that enables communication between the user and the...

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

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.

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.

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 mkdir | Linux Create Directory

The Linux mkdir command is very much similar to Windows’s "new folder" feature. This command is used in Linux os to create new directories. The command mkdir stands for "make...

1 minute read.

ps Command in Linux/Unix with Examples

The 'ps' command in Linux is used to display information about the running processes on a system. The command stands for "process status" and it allows you to view details...

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.

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.

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.

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

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.

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.

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.