您现在的位置是:主页 > Linux专区 > 其他Linux发行版专区 > 疑难问题 >
进程在做什么 Linux下pstack的实现
Linux下有时候我们需要知道一个进程在做什么,比如说程序不正常的时候,他到底在干吗?最直接的方法就是打印出他所有线程的调用栈,这样我们从栈再配合程序代码就知道程序在干吗了。
Linux下这个工具叫做pstack. 使用方法是
?
# pstack
Usage: pstack
当然这个被调查的程序需要有符号信息。 比较雷人的是 这个程序竟然是个shell脚本,核心实现是gdb的 thread apply all bt, 我们可以观摩下他的实现,这个我们做类似的程序提供了一个很好的思路:
?
[root@=i ~]# cat `which pstack`
#!/bin/sh
if test $# -ne 1; then
echo "Usage: `basename $0 .sh`
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 <
$backtrace
EOF
/bin/sed -n \
-e 's/^(gdb) //' \
-e '/^#/p' \
-e '/^Thread/p'
(责任编辑:中国电脑科技网)更多相关资讯
- linux几种快速清空文件内容的方法(2011-01-10)
- linux中发出“滴”的怪声的解决方案(2011-01-10)
- 虚拟机下Linux访问Windows最简办法(2010-12-06)
- Linux卸载后如何删除Grub引导方法(2010-12-06)
- linux下怎样在字符界面下显示中文(2010-12-06)
- 解决openSUSE从NTFS安装不能复制文件(2010-12-01)
- linux操作系统下的进程通信设计(2010-11-30)
- Linux操作系统下makefile和make规则(2010-11-29)
- 最新评论 进入详细评论页>>
- 发表评论
-
热门文章
更多>>-
Rc的Linux主机对Perl和Python说明
1、Perl的路径是/usr/bin/perl 2、Python的路径是/usr/bin/python 3、以符合Linux的主机的文件格式编制perl文件和python文件,用二进制的 ... [详细] - linux中发出“滴”的怪声的解决方案
- 虚拟机下Linux访问Windows最简办法
- Linux卸载后如何删除Grub引导方法
- linux下怎样在字符界面下显示中文
- 解决openSUSE从NTFS安装不能复制文件


腾讯QQ
微软MSN
阿里旺旺
移动飞信
360安全卫士
360免费杀毒
搜狗输入法
千千静听
酷我音乐盒
PPS
QQ影音
暴风影音
迅雷
115U蛋
WinRAR
115浏览器
火狐浏览器
一键还原
Photoshop
Flashplayer
PDF 阅读器