【原创】pstack 执行解析

简介:

首先,确认 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)
恩恩,又学到了新知识~~  

目录
相关文章
|
3天前
|
存储 安全 编译器
C/C++面试题:堆栈的作用
C/C++面试题:堆栈的作用
23 0
wait 和 waitpid 的区别(附有案例代码)
wait 和 waitpid 的区别(附有案例代码)
|
11月前
|
Unix Linux
Linux 的父进程和子进程的执行情况(附有案例代码)
Linux 的父进程和子进程的执行情况(附有案例代码)
|
Linux
内核笔记](四)——内核常见调试手段(printf、dump_stack、devmem)
内核笔记](四)——内核常见调试手段(printf、dump_stack、devmem)
185 0
内核笔记](四)——内核常见调试手段(printf、dump_stack、devmem)
|
缓存 Linux 索引
内核源码kfifo分析(原创)
从2.6.10开始,Linux内核提供了一个通用的环形缓存(我喜欢称为环形队列);它的头文件是<linux/kfifo.h>,kfifo.c是实现代码。 在设备驱动中环形缓存出现相当多. 网络适配器, 特别地, 常常使用环形缓存来与处理器交换数据(报文)[LDD3]。 见下面的图“LDD3中描述的队列”。 我们来看下kfifo的数据结构: struct kfifo { unsigned char *buffer; /* the buffer holding the data */ unsigned int size; /* the size of the al
570 0
|
监控 Windows
艾伟:.Net 下跟踪线程挂起和程序死循环
.Net 下调试跟踪线程挂起和程序死循环   作者:Eaglet      .Net 下的程序调试相对C/C++要简单很多,少了那些令人头疼的指针越界的问题。不过当你的程序遇到如下问题时,依然非常棘手:      1. 进程异常终止。
1014 0
排查程序死循环,死锁的方法 ——pstack
pstack命令可显示每个进程的栈跟踪,pstack $pid即可,pstack命令须由$pid进程的属主或者root运行。 这次出现cpu占比100%的情况,但看memory占比,并无异常,怀疑是某个地方死循环了。
2260 0