Convert a libc backtrace to a source line number

2019-03-17 23:15发布

问题:

I have an MPI application with which combines both C and Fortran sources. Occasionally it crashes due to a memory related bug, but I am having trouble finding the bug (it is somewhere in someone else's code, which at the moment I'm not very familiar with). I haven't yet been able to catch it with gdb, but sometimes a glibc backtrace is output as shown below.

The bug is probably close to "(main_main_+0x3bca)[0x804d5ce]", (but with a memory error, I know this may not be the case). My question is, does anyone know how to convert +0x3bca or 0x804d5ce into a particular line of the code?

Any other suggestions on tracking down the bug would also be appreciated. I'm quite familiar with the basics of gdb.

*** glibc detected *** /home/.../src/finite_element: munmap_chunk(): invalid pointer: 0x09d83018 ***
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(+0x73e42)[0xb7409e42]
/lib/i386-linux-gnu/libc.so.6(+0x74525)[0xb740a525]
/home/.../src/finite_element(main_main_+0x3bca)[0x804d5ce]
/home/.../src/finite_element[0x804e195]
/home/.../src/finite_element(main+0x34)[0x804e1e8]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb73af4d3]
/home/davepc/finite-element/src/finite_element[0x8049971]
======= Memory map: ========
08048000-08056000 r-xp 00000000 08:05 1346306    /home/.../src/finite_element
08056000-08057000 r--p 0000d000 08:05 1346306    /home/.../src/finite_element
08057000-08058000 rw-p 0000e000 08:05 1346306    /home/.../src/finite_element
09d1b000-09d8f000 rw-p 00000000 00:00 0          [heap]
b2999000-b699b000 rw-s 00000000 08:03 15855      /tmp/openmpi-sessions-_0/37612/1/shared_mem_pool.babel
b699b000-b6b1d000 rw-p 00000000 00:00 0 
b6b31000-b6b3d000 r-xp 00000000 08:03 407798     /usr/lib/openmpi/lib/openmpi/mca_osc_rdma.so
b6b3d000-b6b3e000 r--p 0000b000 08:03 407798     /usr/lib/openmpi/lib/openmpi/mca_osc_rdma.so
b6b3e000-b6b3f000 rw-p 0000c000 08:03 407798     /usr/lib/openmpi/lib/openmpi/mca_osc_rdma.so
<snip>

Thank you...

回答1:

If you are in gdb and you have debugging symbols, it is quite easy. Use list.

(gdb) list *0x804d5ce

This should give you the line of code, and show you the source if it is able to find the source file.

Without gdb you could try to use addr2line:

$ addr2line -e finite_element 0x804d5ce