forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgdb-backtrace.sh
executable file
·254 lines (231 loc) · 7.68 KB
/
gdb-backtrace.sh
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
246
247
248
249
250
251
252
253
254
#!/bin/sh
# This script is almost identical to /usr/bin/gstack.
# It is used by TUnixSystem::StackTrace() on Linux and MacOS X.
tempname=`basename $0 .sh`
messfile=""
OUTFILE=`mktemp -q /tmp/${tempname}.XXXXXX`
if test $? -ne 0; then
OUTFILE=/dev/stdout
fi
if [ `uname -s` = "Darwin" ]; then
if test $# -lt 2; then
echo "Usage: ${tempname} <executable> <process-id> [gdb-mess-file]" 1>&2
exit 1
fi
if test $# -eq 3; then
messfile=$3
fi
if test ! -x $1; then
echo "${tempname}: process $1 not found." 1>&2
exit 1
fi
TMPFILE=`mktemp -q /tmp/${tempname}.XXXXXX`
if test $? -ne 0; then
echo "${tempname}: can't create temp file, exiting..." 1>&2
exit 1
fi
backtrace="thread apply all bt"
echo $backtrace > $TMPFILE
GDB=${GDB:-gdb}
# Run GDB, strip out unwanted noise.
$GDB -q -batch -n -x $TMPFILE $1 $2 2>&1 < /dev/null |
sed -n \
-e 's/^(gdb) //' \
-e '/^#/p' \
-e 's/\(^Thread.*\)/@\1/p' | tr "@" "\n" > $OUTFILE
rm -f $TMPFILE
else
if test $# -lt 1; then
echo "Usage: ${tempname} <process-id> [gdb-mess-file]" 1>&2
exit 1
fi
if test $# -eq 2; then
messfile=$2
fi
if test ! -r /proc/$1; then
echo "${tempname}: 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.
# The leading spaces are needed for have_eval_command's replacement.
backtrace="bt"
if test -d /proc/$1/task ; then
# Newer kernel; has a task/ directory.
if test `ls /proc/$1/task | 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 grep -e libpthread /proc/$1/maps > /dev/null 2>&1 ; then
backtrace="thread apply all bt"
fi
fi
GDB=${GDB:-gdb}
# Run GDB, strip out unwanted noise.
have_eval_command=`gdb --help 2>&1 |grep eval-command`
if ! test "x$have_eval_command" = "x"; then
$GDB --batch --eval-command="$backtrace" /proc/$1/exe $1 2>&1 < /dev/null |
sed -n \
-e 's,\x1B\[[0-9;]*[a-zA-Z],,g' \
-e 's/^(gdb) //' \
-e '/^#/p' \
-e '/^ /p' \
-e 's/\(^Thread.*\)/@\1/p' | tr '@' '\n' > $OUTFILE
else
$GDB -q -n /proc/$1/exe $1 <<EOF 2>&1 |
$backtrace
EOF
sed -n \
-e 's/^(gdb) //' \
-e '/^#/p' \
-e '/^ /p' \
-e 's/\(^Thread.*\)/@\1/p' | tr '@' '\n' > $OUTFILE
fi
fi
# Analyze the stack.
# The recommendations are based on the following assumptions:
# * More often than not, the crash is caused by user code.
# * The crash can be caused by the user's interpreted code,
# in which case sighandler() is called from CINT (G__...)
# * The crash can be called by the user's library code,
# in which case sighandler() is called from non-CINT and
# it's worth dumping the stack frames.
# * The user doesn't call CINT directly, so whenever we reach
# a stack frame with "G__" we can stop.
# * The crash is caused by only one thread, the one which
# invokes sighandler()
if ! test "x$OUTFILE" = "x/dev/stdout"; then
frames=""
wantthread=""
skip=""
ininterp=""
signal=""
while IFS= read line; do
case $line in
*'<signal handler called>'* )
# this frame doesn't exist
skip="yes"
frames=""
continue
;;
Thread* )
if test "x$wantthread" = "xyes"; then
break
fi
skip=""
frames=""
;;
'#'* )
skip=""
;;
esac
if test "x${skip}" = "x"; then
tag=`echo $line|sed 's,^.*[0-9a-fA-F] in ,,'`
if test "x$ininterp" = "xcheck next"; then
ininterp="check this"
fi
case $tag in
SigHandler* )
signal=`echo $line | sed 's/^.*\(kSig[^)]*\).*$/\1/'`
wantthread="yes"
ininterp="check next"
frames=""
skip="yes"
;;
sighandler* | TUnixSystem::DispatchSignals* )
wantthread="yes"
ininterp="check next"
frames=""
skip="yes"
;;
G__* | Cint::G__* )
if test "x$ininterp" = "xcheck this"; then
ininterp="yes"
break
elif test "x$ininterp" = "xno"; then
break
fi
skip="yes"
;;
*::ProcessLine* | TRint::HandleTermInput* )
# frames to ignore (upper end)
if test "x$ininterp" = "xcheck this"; then
ininterp="no"
break
fi
if test "x$wantthread" = "xyes"; then
break;
fi
skip="yes"
;;
* )
if test "x${skip}" = "x"; then
if test "x$frames" = "x"; then
frames="$line"
else
frames="$frames
$line"
fi
fi
if test "x$ininterp" = "xcheck this"; then
ininterp="no"
fi
;;
esac
fi
done < $OUTFILE
# Only print the informative text if we actually have a crash
# but not when TSystem::Stacktrace() was called.
if ! test "x$ininterp" = "x"; then
if ! test "x$signal" = "x"; then
signal=' ('${signal}')'
fi
echo ""
echo ""
echo ""
echo "==========================================================="
echo "There was a crash${signal}."
echo "This is the entire stack trace of all threads:"
echo "==========================================================="
fi
cat $OUTFILE
if ! test "x$ininterp" = "x"; then
echo "==========================================================="
if test "x$ininterp" = "xyes"; then
echo ""
echo ""
if test -f "$messfile"; then
cat $messfile | tr '%' '\n'
else
echo 'The crash is most likely caused by a problem in your script.'
echo 'Try to compile it (.L myscript.C+g) and fix any errors.'
echo 'You may get help by asking at the ROOT forum http://root.cern.ch/forum.'
echo 'If you are really convinced it is a bug in ROOT then please submit a report'
echo 'at http://root.cern.ch/bugs. Please post the ENTIRE stack trace'
echo 'from above as an attachment in addition to anything else'
echo 'that might help us fixing this issue.'
fi
elif ! test "x$frames" = "x"; then
echo ""
echo ""
if test -f "$messfile"; then
cat $messfile | tr '%' '\n'
else
echo 'The lines below might hint at the cause of the crash.'
echo 'You may get help by asking at the ROOT forum http://root.cern.ch/forum.'
echo 'Only if you are really convinced it is a bug in ROOT then please submit a'
echo 'report at http://root.cern.ch/bugs. Please post the ENTIRE stack trace'
echo 'from above as an attachment in addition to anything else'
echo 'that might help us fixing this issue.'
fi
echo "==========================================================="
echo "$frames"
echo "==========================================================="
fi
echo ""
echo ""
fi
rm -f $OUTFILE $messfile
fi