How to use the command addr2line (with examples)

How to use the command addr2line (with examples)

The addr2line command is a useful tool for converting addresses of a binary into file names and line numbers. It can provide information about the source code from an instruction address of an executable, including the filename, line number, and function name.

Use case 1: Display the filename and line number of the source code from an instruction address of an executable

Code:

addr2line --exe=path/to/executable address

Motivation: When debugging a program, it is often necessary to determine the source code location associated with a given address in an executable file. By using addr2line, we can easily retrieve the filename and line number associated with an instruction address.

Explanation:

  • --exe=path/to/executable: Specifies the path to the executable file from which to retrieve the source code information.
  • address: The instruction address for which to obtain the filename and line number.

Example output:

file.c:123

Use case 2: Display the function name, filename, and line number

Code:

addr2line --exe=path/to/executable --functions address

Motivation: In addition to the filename and line number, it can be helpful to know the function name associated with a given instruction address when debugging a program. This provides more context and allows for better understanding of the program flow.

Explanation:

  • --exe=path/to/executable: Specifies the path to the executable file.
  • --functions: Display the function name in addition to the filename and line number.
  • address: The instruction address for which to obtain the function name, filename, and line number.

Example output:

function_name
file.c:123

Use case 3: Demangle the function name for C++ code

Code:

addr2line --exe=path/to/executable --functions --demangle address

Motivation: When dealing with C++ code, function names can be mangled to accommodate features of the language such as function overloading and namespaces. Demangling the function name makes it more readable and easier to understand the program flow when debugging.

Explanation:

  • --exe=path/to/executable: Specifies the path to the executable file.
  • --functions: Display the function name.
  • --demangle: Demangle the function name for C++ code.
  • address: The instruction address for which to obtain the demangled function name, filename, and line number.

Example output:

MyClass::myFunction()
file.cpp:123

Conclusion:

The addr2line command is a powerful tool for retrieving source code information from instruction addresses in an executable file. It can provide the filename, line number, and function name associated with a given address, allowing for easier debugging and understanding of the program flow.

Related Posts

How to use the command 'py-spy' (with examples)

How to use the command 'py-spy' (with examples)

py-spy is a sampling profiler for Python programs. It allows you to analyze the execution time of your Python code by collecting samples of the program’s state at regular intervals.

Read More
How to use the command 'tshark' (with examples)

How to use the command 'tshark' (with examples)

Tshark is a packet analysis tool and the command line version of Wireshark.

Read More
How to use the command llvm-as (with examples)

How to use the command llvm-as (with examples)

The llvm-as command is used to assemble LLVM Intermediate Representation (.

Read More