如何通过内存地址解析函数名?

时间:2015-10-22 03:49:38

标签: linux debugging gdb pstack

虽然Linux没有像pstack那样提供Solaris,但RedHat提供的脚本可以做同样的事情:

#!/bin/bash

if test $# -ne 1; then
    echo "Usage: `basename $0 .sh` <process-id>" 1>&2
    exit 1
fi

if test ! -r /proc/$1; then
    echo "Process $1 not found." 1>&2
    exit 1
fi

# GDB doesn't allow "thread apply all bt" when the process isn't
# threaded; need to peek at the process to determine if that or the
# simpler "bt" should be used.

backtrace="bt"
if test -d /proc/$1/task ; then
    # Newer kernel; has a task/ directory.
    if test `/bin/ls /proc/$1/task | /usr/bin/wc -l` -gt 1 2>/dev/null ; then
        backtrace="thread apply all bt"
    fi
elif test -f /proc/$1/maps ; then
    # Older kernel; go by it loading libpthread.
    if /bin/grep -e libpthread /proc/$1/maps > /dev/null 2>&1 ; then
        backtrace="thread apply all bt"
    fi
fi

GDB=${GDB:-/usr/bin/gdb}

if $GDB -nx --quiet --batch --readnever > /dev/null 2>&1; then
    readnever=--readnever
else
    readnever=
fi

# Run GDB, strip out unwanted noise.
$GDB --quiet $readnever -nx /proc/$1/exe $1 <<EOF 2>&1 |
$backtrace
EOF
/bin/sed -n \
    -e 's/^(gdb) //' \
    -e '/^#/p' \
    -e '/^Thread/p'

Suse执行脚本:

linux:~ # pstack 7286
Thread 3 (Thread 0x7f7c074b5700 (LWP 7287)):
#0  0x00007f7c055f7a9d in read () from /lib64/libpthread.so.0
#1  0x00007f7c050a3b76 in ?? () from /usr/lib64/libxenstore.so.3.0
#2  0x00007f7c050a3c2f in ?? () from /usr/lib64/libxenstore.so.3.0
#3  0x00007f7c050a3f72 in ?? () from /usr/lib64/libxenstore.so.3.0
#4  0x00007f7c055f10a4 in start_thread () from /lib64/libpthread.so.0
#5  0x00007f7c03c9104d in clone () from /lib64/libc.so.6
Thread 2 (Thread 0x7f7bfe4b0700 (LWP 7288)):
#0  0x00007f7c055f505f in pthread_cond_wait@@GLIBC_2.3.2 ()
#1  0x00007f7c07199d99 in ?? ()
#2  0x00007f7c0709a213 in ?? ()
#3  0x00007f7c0709a610 in ?? ()
#4  0x00007f7c055f10a4 in start_thread () from /lib64/libpthread.so.0
#5  0x00007f7c03c9104d in clone () from /lib64/libc.so.6
Thread 1 (Thread 0x7f7c07483980 (LWP 7286)):
#0  0x00007f7c03c88cdf in ppoll () from /lib64/libc.so.6
#1  0x00007f7c07060ec9 in ?? ()
#2  0x00007f7c07026654 in ?? ()
#3  0x00007f7c06edcb36 in ?? ()
#4  0x00007f7c03bcdb05 in __libc_start_main () from /lib64/libc.so.6
#5  0x00007f7c06ee0eec in ?? ()

我的问题是如何从地址解析函数名称?例如0x00007fe4ab73eb36。我知道可能通过安装debug-info包,但如何知道安装哪些包?

更新:
根据Mark Plotnick的评论,我使用以下命令来获取缺少debuginfo个软件包:

linux:~ # gdb --quiet -nx --readnever /proc/7286/exe 7286
......
Missing separate debuginfos, use: zypper install glibc-debuginfo-2.19-31.5.x86_64 ......

安装所有需要debuginfo包后,可以解析符号:

linux:~ # pstack 7286
Thread 3 (Thread 0x7f7c074b5700 (LWP 7287)):
#0  0x00007f7c055f7a9d in read () from /lib64/libpthread.so.0
#1  0x00007f7c050a3b76 in read_all.part.1.constprop ()
#2  0x00007f7c050a3c2f in read_message.constprop ()
#3  0x00007f7c050a3f72 in read_thread () from /usr/lib64/libxenstore.so.3.0
#4  0x00007f7c055f10a4 in start_thread () from /lib64/libpthread.so.0
#5  0x00007f7c03c9104d in clone () from /lib64/libc.so.6
Thread 2 (Thread 0x7f7bfe4b0700 (LWP 7288)):
#0  0x00007f7c055f505f in pthread_cond_wait@@GLIBC_2.3.2 ()
#1  0x00007f7c07199d99 in qemu_cond_wait ()
#2  0x00007f7c0709a213 in vnc_worker_thread_loop ()
#3  0x00007f7c0709a610 in vnc_worker_thread ()
#4  0x00007f7c055f10a4 in start_thread () from /lib64/libpthread.so.0
#5  0x00007f7c03c9104d in clone () from /lib64/libc.so.6
Thread 1 (Thread 0x7f7c07483980 (LWP 7286)):
#0  0x00007f7c03c88cdf in ppoll () from /lib64/libc.so.6
#1  0x00007f7c07060ec9 in qemu_poll_ns ()
#2  0x00007f7c07026654 in main_loop_wait ()
#3  0x00007f7c06edcb36 in main ()

但“objdump -t /proc/7286/exe | grep main”不输出任何内容:

linux:~ # objdump -t /proc/7286/exe | grep main
linux:~ #

0 个答案:

没有答案