pstack

ittangzeng發表於2018-04-26
pstack有什麼作用?
   當程式僵住時,它能列印程式當時的堆疊,特別是很多執行緒的執行現場,分析死鎖非常有幫助。

pstack是什麼?
 pstack 其實就是封裝gdb的一個shell指令碼。列印堆疊其實就是gdb 的bt命令。

[root@host-172-24-115-217 ~]# ll /usr/bin/pstack 
lrwxrwxrwx. 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/sh
if test $# -ne 1; then
    echo "Usage: `basename $0 .sh` " 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 <&1 |
set width 0
set height 0
set pagination no
$backtrace
EOF
/bin/sed -n \
    -e 's/^\((gdb) \)*//' \
    -e '/^#/p' \
    -e '/^Thread/p'

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29497382/viewspace-2153387/,如需轉載,請註明出處,否則將追究法律責任。