Linux addr2line Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the addr2line command in Linux, which is a tool used to translate addresses into file names and line numbers. This is particularly useful when working with debugging information, such as stack traces or core dumps, to determine the source code location corresponding to a particular address. You will explore the basic syntax and options of addr2line, and learn how to use it to resolve addresses to function names and source file locations. The lab covers the purpose of the addr2line command, its basic usage, and provides practical examples to help you understand how to effectively use this tool when debugging issues in your programs.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux/BasicSystemCommandsGroup -.-> linux/printf("`Text Formatting`") subgraph Lab Skills linux/printf -.-> lab-422539{{"`Linux addr2line Command with Practical Examples`"}} end

Understand the Purpose of addr2line Command

In this step, you will learn about the purpose of the addr2line command in Linux. The addr2line command is a tool used to translate addresses into file names and line numbers. It is particularly useful when working with debugging information, such as stack traces or core dumps, to determine the source code location corresponding to a particular address.

The addr2line command takes an address as input and uses the debugging information in an object file (such as an executable or shared library) to determine the file name and line number associated with that address. This can be helpful in understanding the context of a crash or other issue in a program.

Let's start by exploring the basic usage of the addr2line command:

addr2line -e <executable> <address>

Here, <executable> is the name of the executable file containing the debugging information, and <address> is the hexadecimal address you want to translate.

Example:

addr2line -e /bin/ls 0x4004e0

Example output:

/usr/src/debug/coreutils/8.32-r0/coreutils-8.32/src/ls.c:1372

The output shows that the address 0x4004e0 corresponds to line 1372 in the file /usr/src/debug/coreutils/8.32-r0/coreutils-8.32/src/ls.c.

This information can be very useful when debugging issues in a program, as it allows you to quickly identify the source code location associated with a particular address.

Explore the Basic Syntax and Options of addr2line

In this step, you will explore the basic syntax and options of the addr2line command. Understanding the available options will help you use addr2line more effectively when working with debugging information.

Let's start by reviewing the basic syntax of the addr2line command:

addr2line [options] <address> [<address> ...]

Here are some common options for the addr2line command:

  • -e <executable>: Specify the executable file containing the debugging information.
  • -f: Display the function name corresponding to each address.
  • -C: Demangle C++ symbol names.
  • -s: Display the section name for each address.
  • -p: Display the source file and line number in the format "file:line".

Example:

addr2line -e /bin/ls -f 0x4004e0

Example output:

ls_file
/usr/src/debug/coreutils/8.32-r0/coreutils-8.32/src/ls.c:1372

In this example, the -f option is used to display the function name (ls_file) corresponding to the address 0x4004e0.

You can also provide multiple addresses to the addr2line command:

addr2line -e /bin/ls 0x4004e0 0x4004f0

Example output:

ls_file
/usr/src/debug/coreutils/8.32-r0/coreutils-8.32/src/ls.c:1372
ls_file
/usr/src/debug/coreutils/8.32-r0/coreutils-8.32/src/ls.c:1373

This will display the source file and line number for each of the provided addresses.

By exploring the different options available with the addr2line command, you can customize the output to suit your debugging needs.

Resolve Addresses to Function Names and Source File Locations

In this final step, you will learn how to use the addr2line command to resolve addresses to function names and source file locations. This is particularly useful when working with debugging information, such as stack traces or core dumps.

Let's start by creating a simple C program that we can use for this example:

cd ~/project
nano hello.c

Add the following code to the hello.c file:

#include <stdio.h>

int main() {
    printf("Hello, World!\n");
    return 0;
}

Now, compile the program with debugging symbols:

gcc -g -o hello hello.c

Next, let's get the address of the main function:

addr2line -e hello -f 0x4004e0

Example output:

main
/home/labex/project/hello.c:3

The output shows that the address 0x4004e0 corresponds to the main function in the hello.c file, at line 3.

You can also use the addr2line command to resolve multiple addresses at once:

addr2line -e hello 0x4004e0 0x4004f0

Example output:

main
/home/labex/project/hello.c:3
printf
/usr/include/x86_64-linux-gnu/bits/stdio2.h:92

In this case, the address 0x4004f0 corresponds to the printf function, which is called from the main function.

By using the addr2line command, you can quickly identify the source code locations associated with specific addresses, which can be invaluable when debugging issues in your programs.

Summary

In this lab, you learned about the purpose of the addr2line command in Linux, which is used to translate addresses into file names and line numbers. This is particularly useful when working with debugging information, such as stack traces or core dumps, to determine the source code location corresponding to a particular address. You also explored the basic syntax and options of the addr2line command, including how to specify the executable file containing the debugging information and how to use various options to customize the command's behavior.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like