1. 程式人生 > 其它 >gstack檢視執行緒資訊

gstack檢視執行緒資訊

 

1.檢視執行緒資訊

[upchina@iZbp14z6qodocy209yj5c8Z PriceAlarmServerGnn]$ ps  -ef  |  grep "PriceAlarm"
upchina  21261 19389  5 Apr15 ?        03:56:48 /usr/local/app/taf/tafnode/data/HQExtend.PriceAlarmServerGnn/bin/PriceAlarmServerGnn --config=/usr/local/app/taf/tafnode/data/HQExtend.PriceAlarmServerGnn/conf/HQExtend.PriceAlarmServerGnn.config.conf
upchina  
31896 28186 0 11:47 pts/2 00:00:00 grep --color=auto PriceAlarm

 

2.編寫 指令碼  gstack.sh ,指令碼內容如下:

#!/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} # Run GDB, strip out unwanted noise. # --readnever is no longer used since .gdb_index is now in use. $GDB --quiet -nx $GDBARGS /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'

 

3.給指令碼賦予可執行許可權

chmod  755  gstack.sh

 

 

4.執行 指令碼

sh   gstack.sh  21261  >> a1.log

21261  是上面我們用 ps 命名檢視到的程序id 

 >>  a1.log 是將輸出的字串 重定向到 a1.log文本里面;

sh  是執行我們指令碼的意思。