addr2line
Convert addresses to file and line numbers
TLDR
Display the filename and line number of the source code from an instruction address of an executable
Display the function name, filename and line number
Demangle the function name for C++ code
SYNOPSIS
addr2line [options] [addresses]
addr2line -e executable_file [options] [addresses]
PARAMETERS
-a, --addresses
Show addresses before function names, file names, and line numbers. This is the default.
-b, --basename
Only show the basename of the file name. For example, 'main.c' instead of '/home/user/project/main.c'.
-C, --demangle[=STYLE]
Demangle C++ function names. This makes names like '_ZN12MyClassCtorEv' readable as 'MyClass::MyClass()'.
-e executable_file, --exe=executable_file
Specify the executable or object file containing the addresses. This option is crucial for addr2line to find debug information.
-f, --functions
Show function names as well as file and line information.
-i, --inlines
Display all inlined functions that are part of the address's location. This can produce multiple lines of output for a single address.
-j section_name, --section=section_name
Read addresses from the specified section instead of the default '.text' section.
-p, --pretty-print
Make the output more human-readable. Often combines addresses, functions, files, and lines on a single line.
-s, --absolute
Make file names absolute rather than relative to the current directory.
--help
Display a help message and exit.
--version
Display version information and exit.
DESCRIPTION
addr2line is a utility that translates program addresses into file names and line numbers, and optionally function names.
It is an essential tool for debugging, especially when dealing with crash logs or core dumps that provide raw memory addresses but lack symbolic information. By providing addr2line with an executable (or shared library) and a list of addresses, it can use the debug information (typically DWARF debug symbols) embedded within the binary to pinpoint the exact location in the source code where a particular address falls.
This allows developers to quickly identify the source file, line number, and even the function name corresponding to an instruction address, which is invaluable for understanding program behavior, tracing execution flow, and diagnosing issues in post-mortem analysis.
CAVEATS
addr2line's effectiveness is entirely dependent on the presence of debug information within the executable or shared library. If the binary was compiled without debug symbols (e.g., using -s to strip symbols, or without the -g compiler flag), addr2line will likely report '??' for function names, file names, and line numbers.
Highly optimized code can sometimes make the output less precise, as compiler optimizations might reorder instructions or inline functions, making a direct address-to-source mapping ambiguous.
COMPILING WITH DEBUG SYMBOLS
To ensure addr2line works correctly, always compile your C/C++ programs with the -g flag (e.g., gcc -g my_program.c -o my_program
). This flag tells the compiler to include DWARF debug information in the executable, which addr2line relies upon.
TYPICAL USAGE
You might use addr2line when a program crashes and prints a stack trace with memory addresses. For example:addr2line -e my_program 0x400500 0x40052a
Or to process addresses from a file:cat crash_addresses.txt | addr2line -f -C -e my_program
HISTORY
addr2line is part of the GNU Binutils project, a collection of programming tools for creating and managing binary programs, object files, libraries, and executables. Binutils has been actively developed since the 1980s, with addr2line emerging as a key component for post-mortem debugging alongside other utilities like objdump and readelf. Its functionality is often integrated into higher-level debuggers like GDB, but it remains an indispensable standalone tool for automated analysis and scripting.