Linux下pstack的实现
浏览:1679次 出处信息
Linux下有时候我们需要知道一个进程在做什么,比如说程序不正常的时候,他到底在干吗?最直接的方法就是打印出他所有线程的调用栈,这样我们从栈再配合程序代码就知道程序在干吗了。
Linux下这个工具叫做pstack. 使用方法是
1 2 | # pstack Usage: pstack <process- id > |
当然这个被调查的程序需要有符号信息。 比较雷人的是 这个程序竟然是个shell脚本,核心实现是gdb的 thread apply all bt, 我们可以观摩下他的实现,这个我们做类似的程序提供了一个很好的思路:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | [root@=i ~] # 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 | $backtrace EOF /bin/sed -n \ -e 's/^(gdb) //' \ -e '/^#/p' \ -e '/^Thread/p' |
祝大家玩的开心。
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
扫一扫订阅我的微信号:IT技术博客大学习
<< 前一篇:完美使用 WINE 来运行 RTX
后一篇:bash shell杂记 >>
文章信息
- 作者:Yu Feng 来源: Erlang非业余研究
- 标签: pstack
- 发布时间:2011-01-06 22:38:48
近3天十大热文
-
[85] memory prefetch浅析
-
[53] 基本排序算法的PHP实现
-
[51] 深入浅出cassandra 4 数据一致性问
-
[50] 转载:cassandra读写性能原理分析
-
[43] MySQL半同步存在的问题
-
[41] javascript插入样式
-
[41] 字符引用和空白字符
-
[40] Inline Form Labels
-
[39] JS中如何判断字符串类型的数字
-
[38] 获取Dom元素的X/Y坐标