forked from cinsk/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backtrace
executable file
·245 lines (197 loc) · 5.57 KB
/
backtrace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#!/bin/bash
# Stolen from:
# http://www.samba.org/ftp/unpacked/junkcode/segv_handler/
# we want everything on stderr, so the program is not disturbed
exec 1>&2
TIMEOUT=5
DEBUG=1
WATCHDOG=0
VERSION=0.2
function help_and_exit () {
cat <<EOF
Generate backtrace from the process
Usage: `basename $0` [OPTION...] PID [BACKTRACE-FILE]
-o F set the output file to F (default: stdout)
-w enable watchdog (to kill the either GDB or the target process)
-t N set watchdog expiration to N second(s). (default: $TIMEOUT)
-h show this help and exit
-v display version information and exit
If BACKTRACE-FILE is not specified or '-', the backtrace information goes to
stdout.
If watchdog is on (-w), this script wait for a certain amount of
time, then ensure the GDB to terminate. i.e. the script will
send SIGTERM or SIGKILL to the target process.
Normaly, you don't need to use the watchdog (-w). However, in some
cases, GDB does not exit but keep existing with the target process.
Using the watchdog helps by sending signal to the target process so
that GDB can terminate.
EOF
exit 0
}
function version_and_exit() {
cat <<EOF
`basename $0` version $VERSION
EOF
exit 0
}
function debug() {
[ -n "$DEBUG" ] && echo "$@"
return 0
}
BTFILE=-
while getopts whvt:Do: opt; do
case $opt in
w)
WATCHDOG=1
;;
o)
BTFILE=$OPTARG
;;
t)
WATCHDOG=1
TIMEOUT=$OPTARG
;;
k)
KILLPROC=$((KILLPROC + 1))
;;
D)
DEBUG=1
;;
h)
help_and_exit
;;
v)
version_and_exit
;;
?)
echo "usage: $0 PID [BACKTRACE-FILE]" 1>&2
exit 1
;;
esac
done
shift $(($OPTIND - 1))
if test $# -ne 2 -a $# -ne 1; then
echo "usage: $0 PID [BACKTRACE-FILE]" 1>&2
exit 1
fi
PID=$1
TMPFILE=/tmp/gdb.$$
if ! kill -0 "$PID" >&/dev/null; then
echo "backtrace: process (PID=$PID) does not exist"
exit 1
fi
debug "watchdog: $WATCHDOG"
debug "timeout: $TIMEOUT"
if which dirname >&/dev/null; then
mkdir -p `dirname $TMPFILE` 1>&2
fi
trap "rm -f $TMPFILE; exit 1" SIGINT SIGHUP
case "$KILLPROC" in
0)
KILLCMD=""
;;
1)
KILLCMD="signal SIGTERM"
;;
*)
KILLCMD="signal SIGKILL"
;;
esac
cat << EOF > "$TMPFILE"
handle all nostop print pass
set height 0
bt full
thread apply all bt full
#$KILLCMD
quit
EOF
if test -n "$2"; then
BTFILE=$2
fi
if which dirname >&/dev/null; then
[ "$BTFILE" != "-" ] && mkdir -p `dirname $BTFILE` 1>&2
fi
if [ "$BTFILE" != "-" ]; then
BTTMP=${BTFILE}.tmp
WATMP=${BTFILE}.watch
CATMP=${BTFILE}.cat
else
BTTMP=/tmp/.gdb.$$.tmp
WATMP=/tmp/.gdb.$$.watch
CATMP=/tmp/.gdb.$$.cat
fi
trap "rm -f \"$TMPFILE\" \"$BTTMP\" \"$WATMP\" \"$CATMP\"; exit 1" \
SIGINT SIGHUP
#
# I don't know why, but there are some cases that GDB and the target
# process looks hanged.
#
# - Killing the GDB with SIGTERM does not help.
# - Killing the target process DOES help.
#
# So, I made GDB process asynchronous, and after TIMEOUT second(s),
# this script will trying to kill the target process with SIGTERM,
# then SIGKILL. If it does not help, this script will send SIGTERM and SIGKILL
# to the GDB process respectively.
#
if [ "$WATCHDOG" -ne 0 ]; then
major=$(echo $BASH_VERSION | sed -e 's/\..*//g')
if [ -z "$major" -o "$major" -lt 4 ]; then
echo "warning: watchdog disabled (need bash version 4.0 or higher)" 1>&2
WATCHDOG=0
fi
fi
if [ "$WATCHDOG" -ne 0 ]; then
debug "run COPROC gdb ..."
coproc gdb --batch -x $TMPFILE --pid $PID </dev/null &> $BTTMP
#coproc ./gdb.sh </dev/null &> $BTTMP
#set | grep COPROC
else
gdb --batch -x $TMPFILE --pid $PID </dev/null &> $BTTMP
fi
#gdb --batch -x $TMPFILE --pid $PID 2>&1 #| tee $BTFILE
tm_start=`date +%s`
while [ "$WATCHDOG" -ne 0 ]; do
sleep 0.1
debug "COPROC_PID: $COPROC_PID"
[ -z "$COPROC_PID" ] && debug "GDB seems to be gone" && break
tm_ellapsed=$((`date +%s` - tm_start))
debug "ellapsed: $tm_ellapsed"
if [ "$tm_ellapsed" -ge "$TIMEOUT" ]; then
echo "----" > $WATMP
echo "backtrace: timeout reached; $tm_ellapsed second(s)" >> $WATMP
debug "timeout reached: force quit the program"
kill -0 "$PID" >&/dev/null && kill -TERM "$PID" && \
echo "backtrace: sending SIGTERM to the process $PID" >> $WATMP
sleep 0.1
if kill -0 "$PID" >&/dev/null; then
debug "GDB pid: $COPROC_PID"
[ -n "$COPROC_PID" ] && kill -KILL "$PID" && \
echo "backtrace: sending SIGKILL to the process $PID" >> $WATMP
[ -n "$COPROC_PID" ] && sleep 0.2
fi
if [ -n "$COPROC_PID" ]; then
[ -n "$COPROC_PID" ] && kill -TERM "$COPROC_PID" && \
echo "backtrace: sending SIGTERM to the GDB" >> $WATMP
[ -n "$COPROC_PID" ] && sleep 0.2
[ -n "$COPROC_PID" ] && kill -9 "$COPROC_PID" && \
echo "backtrace: sending SIGKILL to the GDB" >> $WATMP
fi
break;
fi
done
# $BTTMP contains the output of GDB.
# $WATMP may contains the output of the watchdoc scripts above
# $CATMP contains the concatenated output of $BTTMP and $WATMP
if [ -f "$WATMP" ]; then
cat "$BTTMP" "$WATMP" > "$CATMP"
else
mv "$BTTMP" "$CATMP"
fi
if [ "$BTFILE" != "-" ]; then
mv "$CATMP" "$BTFILE"
else
cat $CATMP
fi
/bin/rm -f "$TMPFILE" "$BTTMP" "$WATMP" "$CATMP"
exit 0