
addr2line Command in Linux
addr2line is a command used in Linux for debugging programs. It converts memory addresses from compiled executable or object files into corresponding file names and line numbers in the source code.
With the help of addr2line command, the developers can trace where specific instructions or errors originate in the source code. This is particularly useful for debugging since it helps developers tpinpoint the exact location in the source code where an error or specific instruction occurs.
Table of Contents
Here is a comprehensive guide to the options available with the addr2line command −
- Syntax of addr2line Command
- Options addr2line Command
- Use of addr2line Command in Linux
- Examples of addr2line Command in Linux
Syntax of addr2line Command
The basic syntax to use the addr2line command in Linux is given below −
addr2line [options] [addr addr ...]
Where,
- [options] are command-line arguments that modify the operational parameters of the command.
- [addr addr ...] are the memory addresses you want to convert to file names and line numbers.
Options addr2line Command
The following are some options that can be used with the addr2line command in Linux −
Option | Description |
---|---|
-a --address | Displays the address before the function name, file, and line number information. |
-b --target=<bfdname> | Specifies the object-code format for the object files. |
-e exe=<executable> | Specifies the executable file to use for address translation. |
-i --inlines | Displays inlined functions, showing additional lines for each inlined function. |
-j --section=<name> | Reads offsets relative to the specified section. |
-p --pretty-print | Pretty-prints the output, combining address, function name, file name, and line number into a single line. |
-s --basenames | Shows only the base name of the file instead of the full path. |
-f --functions | Prints the function name associated with each address. |
-C --demangle | Demangles low-level symbol names into user-level names, making C++ function names readable. |
-R --recursive-limit | Sets the recursion limit for demangling. |
-r --no-recurse-limit | Disables the recursion limit for demangling. |
-h, --help | Displays help information about the command. |
-v, --version | Shows the version information of the command. |
Use of addr2line Command in Linux
Before using the addr2line command, you must ensure you have the following prerequisites −
Executable File with Debug Information
Make sure the executable file you want to analyze was compiled with debugging information. This is typically done using the -g flag during compilation. For example −
gcc -g hello.c
Note − We have used the following C code as an example −
#include <stdio.h> void my_function() { printf("Inside my_function\n"); } int main() { printf("Hello, World!\n"); my_function(); return 0; }
Correct Address
Verify that you have the correct memory address you want to translate. You can use tools like objdump to find function addresses within the executable. For example −
objdump -d a.out | grep my_function

Unstripped Executable
Ensure that the executable has not been stripped of its debugging information. You can check this using the file command −
file a.out

Once you have confirmed these prerequisites, you can use the execute the following examples of addr2line command in Linux.
Note − The address in our case is 0000000000001169, which is 0x1169 in hexadecimal format.
Examples of addr2line Command in Linux
In this section, we will have a set of examples to demonstrate how you can use the addr2line command in Linux −
- Translate Address to Filename and Line Number
- Include Function Name in Output
- Demangle C++ Function Name
- Show Inlined Functions
- Pretty-Print the Output
Translate Address to Filename and Line Number
The addr2line command can be used to convert an address in an executable to its corresponding filename and line number that helps you in debugging purposes. For example, to find the source location of an address in an executable file, you can use −
addr2line -e a.out 0x1169
This command will output the filename and line number where the address 0x1169 is located.

Include Function Name in Output
You can also use addr2line to get the function name along with the filename and line number. This provides more context for debugging. For instance, to include the function name, use −
addr2line -e a.out -f 0x1169
This command will output the function name, filename, and line number for the address 0x1169.

Demangle C++ Function Names
When dealing with C++ code, function names can be mangled. The addr2line command can demangle these names to make them readable. For example, to demangle a C++ function name, use −
addr2line -e a.out -f -C 0x1169
This command will output the demangled function name, filename, and line number for the address 0x1169.

Show Inlined Functions
If you want to see inlined functions, addr2line can show additional lines for each inlined function. For example, to display inlined functions, use −
addr2line -e a.out -i 0x1169
This command will output the inlined function names, filenames, and line numbers for the address 0x1169.

Pretty-Print the Output
To make the output more human-readable, you can use the pretty-print option. For example, to format the output nicely, use −
addr2line -e a.out -p 0x1169

Conclusion
The addr2line command is a pretty useful tool in Linux systems. It helps translate memory addresses from compiled executable or object files names and line numbers in the source code. This helps developers in tracing the origin of specific instructions or errors.
In this article, we explained the syntax, options and practical examples of this command in Linux. Following these examples will help you effectively use addr2line command to enhance your debugging process and improve code quality.