#!/bin/shiftest$#-ne1;thenecho"Usage: `basename $0 .sh` <process-id>"1>&2exit1fiiftest!-r /proc/$1;thenecho"Process $1 not found."1>&2exit1fi# 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"iftest-d /proc/$1/task ;then# Newer kernel; has a task/ directory.iftest`/bin/ls /proc/$1/task | /usr/bin/wc -l`-gt12>/dev/null ;thenbacktrace="thread apply all bt"fieliftest-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=--readnever
elsereadnever=fi# Run GDB, strip out unwanted noise.$GDB--quiet$readnever-nx /proc/$1/exe $1<<EOF2>&1|
set width 0
set height 0
set pagination no
$backtrace
EOF
/bin/sed -n\-e's/^\((gdb) \)*//'\-e'/^#/p'\-e'/^Thread/p'