pstack是一个shell脚本,用于打印正在运行的进程的栈跟踪信息,它实际上是gstack的一个链接,而gstack本身是基于gdb封装的shell脚本.。此命令可显示每个进程的栈跟踪。pstack 命令必须由相应进程的属主或 root 运行。可以使用 pstack 来确定进程挂起的位置。此命令允许使用的唯一选项是要检查的进程的 PID。
与jstack功相比, 它能对潜在的死锁予以提示, 而pstack只提供了线索, 需要gdb进一步的确定。
pstack是gdb的一部分,如果系统没有pstack命令,使用yum搜索安装gdb即可。
这个命令在排查进程问题时非常有用,比如我们发现一个服务一直处于work状态(如假死状态,好似死循环),使用这个命令就能轻松定位问题所在;可以在一段时间内,多执行几次pstack,若发现代码栈总是停在同一个位置,那个位置就需要重点关注,很可能就是出问题的地方;
运行格式为:pstack
pstack是什么?
pstack 其实就是封装gdb的一个shell脚本。打印堆栈其实就是 gdb 的 bt 命令。**
[root@host-172-24-115-217 ~]# ll /usr/bin/pstacklrwxrwxrwx. 1 root root 6 Apr 26 12:54 /usr/bin/pstack -> gstack[root@host-172-24-115-217 ~]# file /usr/bin/gstack/usr/bin/gstack: POSIX shell script text executable[root@host-172-24-115-217 ~]# cat /usr/bin/gstack#!/bin/shif test $# -ne 1; thenecho "Usage: `basename $0 .sh` " 1>&2exit 1fiif test ! -r /proc/$1; thenecho "Process $1 not found." 1>&2exit 1fi# 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 ; thenbacktrace="thread apply all bt"fielif 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 ; thenbacktrace="thread apply all bt"fifiGDB=${GDB:-/usr/bin/gdb}if $GDB -nx --quiet --batch --readnever > /dev/null 2>&1; thenreadnever=--readneverelsereadnever=fi# Run GDB, strip out unwanted noise.$GDB --quiet $readnever -nx /proc/$1/exe $1 <&1 |set width 0set height 0set pagination no$backtraceEOF/bin/sed -n \-e 's/^\((gdb) \)*//' \-e '/^#/p' \-e '/^Thread/p'
