首先,确认 pstack 只是一个 shell 脚本。
[root@Betty ~]# cat `which pstack`
#!/bin/sh
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 |
set width 0
set height 0
set pagination no
$backtrace
EOF
/bin/sed -n \
-e 's/^\((gdb) \)*//' \
-e '/^#/p' \
-e '/^Thread/p'
[root@Betty ~]#
其次,找个目标进程用于分析执行过程。 [root@Betty ~]# ps aux|grep redis
root 1879 0.0 0.4 145624 15692 ? Tsl May03 0:58 /usr/local/bin/redis-server *:6379
root 24960 0.0 0.0 103256 856 pts/1 S+ 16:44 0:00 grep redis
[root@Betty ~]#
接着查看 pstack 脚本执行过程中个变量的值。 [root@Betty ~]# sh -x ./pstack 1879
+ test 1 -ne 1
+ test '!' -r /proc/1879
+ backtrace=bt
+ test -d /proc/1879/task
++ /bin/ls /proc/1879/task
++ /usr/bin/wc -l
+ test 3 -gt 1
+ backtrace='thread apply all bt'
+ GDB=/usr/bin/gdb
+ /usr/bin/gdb -nx --quiet --batch --readnever
+ readnever=--readnever
+ /usr/bin/gdb --quiet --readnever -nx /proc/1879/exe 1879
+ /bin/sed -n -e 's/^\((gdb) \)*//' -e '/^#/p' -e '/^Thread/p'
Thread 3 (Thread 0x7f53e67ff700 (LWP 1884)):
#0 0x000000322500b68c in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x000000000045d735 in bioProcessBackgroundJobs ()
#2 0x0000003225007aa1 in start_thread () from /lib64/libpthread.so.0
#3 0x0000003224ce893d in clone () from /lib64/libc.so.6
Thread 2 (Thread 0x7f53e5dfe700 (LWP 1885)):
#0 0x000000322500b68c in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x000000000045d735 in bioProcessBackgroundJobs ()
#2 0x0000003225007aa1 in start_thread () from /lib64/libpthread.so.0
#3 0x0000003224ce893d in clone () from /lib64/libc.so.6
Thread 1 (Thread 0x7f53ed0c9720 (LWP 1879)):
#0 0x0000003224ce8f33 in epoll_wait () from /lib64/libc.so.6
#1 0x0000000000419ece in aeProcessEvents ()
#2 0x000000000041a26b in aeMain ()
#3 0x00000000004236ad in main ()
[root@Betty ~]#
最后,手动执行 gdb 查看对应的输出。 [root@Betty ~]# /usr/bin/gdb --quiet --readnever -nx /proc/1879/exe 1879
Reading symbols from /proc/1879/exe...(no debugging symbols found)...done.
Attaching to program: /proc/1879/exe, process 1879
Reading symbols from /lib64/libm.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libm.so.6
Reading symbols from /lib64/libdl.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/libdl.so.2
Reading symbols from /lib64/libpthread.so.0...(no debugging symbols found)...done.
[New LWP 1885]
[New LWP 1884]
[Thread debugging using libthread_db enabled]
Loaded symbols for /lib64/libpthread.so.0
Reading symbols from /lib64/libc.so.6...(no debugging symbols found)...done.
Loaded symbols for /lib64/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...(no debugging symbols found)...done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x0000003224ce8f33 in epoll_wait () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.166.el6_7.7.x86_64
(gdb) set width 0
(gdb) set height 0
(gdb) set pagination no
(gdb) thread apply all bt
Thread 3 (Thread 0x7f53e67ff700 (LWP 1884)):
#0 0x000000322500b68c in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x000000000045d735 in bioProcessBackgroundJobs ()
#2 0x0000003225007aa1 in start_thread () from /lib64/libpthread.so.0
#3 0x0000003224ce893d in clone () from /lib64/libc.so.6
Thread 2 (Thread 0x7f53e5dfe700 (LWP 1885)):
#0 0x000000322500b68c in pthread_cond_wait@@GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x000000000045d735 in bioProcessBackgroundJobs ()
#2 0x0000003225007aa1 in start_thread () from /lib64/libpthread.so.0
#3 0x0000003224ce893d in clone () from /lib64/libc.so.6
Thread 1 (Thread 0x7f53ed0c9720 (LWP 1879)):
#0 0x0000003224ce8f33 in epoll_wait () from /lib64/libc.so.6
#1 0x0000000000419ece in aeProcessEvents ()
#2 0x000000000041a26b in aeMain ()
#3 0x00000000004236ad in main ()
(gdb)
恩恩,又学到了新知识~~