Command of the Day: addr2line

May 1, 2007

To kick off the “Command of the Day” series I decided to start with addr2line.

Addr2line, part of GNU Binutils, will translate a hex address into the appropriate filename and line number for the code at that address. So an example usage would be if a program crashes at a specific location you can easily find out which line of code in which file the crash occurred at. This is quite useful if you get the crash report via an email or forum post and don’t have access to a core dump or the ability to hook up a debugger (assuming the other person is running a well known pre-built binary and have proper debug symbols).

A concrete example helped for me. I wrote this simple program:


#include <stdio.h>
void test_method() {
  printf("%pn", &test_method);
}

int main(int argc, char **argv) {
  test_method();
  return 0;
}

When compiled (gcc a2ltest.c -ggdb) the produced executable (a.out) prints out the pointer to the function test_method in hex format. An example run is:

..@desktop:~/cotd/addr2line$ ./a.out
0x8048374

Now I can pipe that output to addr2line and see the expected results:

..@desktop:~/cotd/addr2line$ ./a.out  | addr2line
/home/../cotd/addr2line/addr2line.c:3

On Windows I could get similar info from various methods such as using map files (though you have to work through the relative addresses on your own and hope that the assembly loaded where you think it did)

Very useful tool.

One Response to “Command of the Day: addr2line”

  1. Egil Says:

    I’ve been running Linux on my desktop since 2002, and I didn’t know that command. Keep up the good work, it’s a lesson for all of us!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: