开发者社区> 摩云飞> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

【原创】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)
恩恩,又学到了新知识~~ 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
扩展知识---中断程序的实现
扩展知识---中断程序的实现
0 0
dump_stack调用过程【原创】
dump_stack调用: void dump_stack(void){    dump_backtrace(NULL, NULL);} 继续看: static void dump_backtrace(struct pt_regs *regs, struct task_struct *tsk)...
424 0
pstack使用和原理【转】
转自:http://www.cnblogs.com/mumuxinfei/p/4366708.html 前言:  最近小组在组织的读书会, 里面作者提到了pstack这个工具. 之前写JAVA程序, 对jstack这个工具, 非常的喜欢, 觉得很有用.
865 0
在Linux中打印函数调用堆栈【原创】
本人学习笔记,代码参考如下网址 参考http://www.cnblogs.com/dma1982/archive/2012/02/08/2342215.html zhangbh@prolin-srv: gcc -rdynamic -o my a.
677 0
+关注
摩云飞
十年磨一剑,我还差几年~~
文章
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载