Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/usr.sbin/crashinfo/crashinfo.sh
107769 views
1
#!/bin/sh
2
#
3
# SPDX-License-Identifier: BSD-3-Clause
4
#
5
# Copyright (c) 2008 Yahoo!, Inc.
6
# All rights reserved.
7
#
8
# Redistribution and use in source and binary forms, with or without
9
# modification, are permitted provided that the following conditions
10
# are met:
11
# 1. Redistributions of source code must retain the above copyright
12
# notice, this list of conditions and the following disclaimer.
13
# 2. Redistributions in binary form must reproduce the above copyright
14
# notice, this list of conditions and the following disclaimer in the
15
# documentation and/or other materials provided with the distribution.
16
# 3. Neither the name of the author nor the names of any co-contributors
17
# may be used to endorse or promote products derived from this software
18
# without specific prior written permission.
19
#
20
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
# SUCH DAMAGE.
31
#
32
33
usage()
34
{
35
echo "usage: crashinfo [-b] [-d crashdir] [-n dumpnr]" \
36
"[-k kernel] [core]"
37
exit 1
38
}
39
40
# Remove an uncompressed copy of a dump
41
cleanup()
42
{
43
44
[ -e $VMCORE ] && rm -f $VMCORE
45
}
46
47
# Run a single gdb command against a kernel file in batch mode.
48
# The kernel file is specified as the first argument and the command
49
# is given in the remaining arguments.
50
gdb_command()
51
{
52
local k
53
54
k=$1 ; shift
55
56
${GDB} -batch -ex "$@" $k
57
}
58
59
find_kernel()
60
{
61
local ivers k kvers
62
63
ivers=$(awk '
64
/Version String/ {
65
print
66
nextline=1
67
next
68
}
69
nextline==1 {
70
if ($0 ~ "^ [A-Za-z ]+: ") {
71
nextline=0
72
} else {
73
print
74
}
75
}' $INFO)
76
77
# Look for a matching kernel version, handling possible truncation
78
# of the version string recovered from the dump.
79
for k in `sysctl -n kern.bootfile` $(ls -t /boot/*/kernel); do
80
kvers=$(gdb_command $k 'printf " Version String: %s", version' | \
81
awk "{line=line\$0\"\n\"} END{print substr(line,1,${#ivers})}" \
82
2>/dev/null)
83
if [ "$ivers" = "$kvers" ]; then
84
KERNEL=$k
85
break
86
fi
87
done
88
}
89
90
BATCH=false
91
CRASHDIR=/var/crash
92
DUMPNR=
93
KERNEL=
94
95
while getopts "bd:n:k:" opt; do
96
case "$opt" in
97
b)
98
BATCH=true
99
;;
100
d)
101
CRASHDIR=$OPTARG
102
;;
103
n)
104
DUMPNR=$OPTARG
105
;;
106
k)
107
KERNEL=$OPTARG
108
;;
109
\?)
110
usage
111
;;
112
esac
113
done
114
115
shift $((OPTIND - 1))
116
117
if [ $# -eq 1 ]; then
118
if [ -n "$DUMPNR" ]; then
119
echo "-n and an explicit vmcore are mutually exclusive"
120
usage
121
fi
122
123
# Figure out the crash directory and number from the vmcore name.
124
CRASHDIR=`dirname $1`
125
DUMPNR=$(expr $(basename $1) : 'vmcore\.\([0-9]*\)')
126
if [ -z "$DUMPNR" ]; then
127
echo "Unable to determine dump number from vmcore file $1."
128
exit 1
129
fi
130
elif [ $# -gt 1 ]; then
131
usage
132
else
133
# If we don't have an explicit dump number, operate on the most
134
# recent dump.
135
if [ -z "$DUMPNR" ]; then
136
if ! [ -r $CRASHDIR/bounds ]; then
137
echo "No crash dumps in $CRASHDIR."
138
exit 1
139
fi
140
next=`cat $CRASHDIR/bounds`
141
if [ -z "$next" ] || [ "$next" -eq 0 ]; then
142
echo "No crash dumps in $CRASHDIR."
143
exit 1
144
fi
145
DUMPNR=$(($next - 1))
146
fi
147
fi
148
149
VMCORE=$CRASHDIR/vmcore.$DUMPNR
150
INFO=$CRASHDIR/info.$DUMPNR
151
FILE=$CRASHDIR/core.txt.$DUMPNR
152
HOSTNAME=`hostname`
153
154
if $BATCH; then
155
echo "Writing crash summary to $FILE."
156
exec > $FILE 2>&1
157
fi
158
159
GDB=/usr/local/bin/gdb
160
if [ ! -x "$GDB" ]; then
161
echo "Unable to find a kernel debugger."
162
echo "Please install the devel/gdb port or gdb package."
163
exit 1
164
fi
165
166
if [ ! -e $VMCORE ]; then
167
if [ -e $VMCORE.gz ]; then
168
trap cleanup EXIT HUP INT QUIT TERM
169
gzcat $VMCORE.gz > $VMCORE
170
elif [ -e $VMCORE.zst ]; then
171
trap cleanup EXIT HUP INT QUIT TERM
172
zstdcat $VMCORE.zst > $VMCORE
173
else
174
echo "$VMCORE not found"
175
exit 1
176
fi
177
fi
178
179
if [ ! -e $INFO ]; then
180
echo "$INFO not found"
181
exit 1
182
fi
183
184
# If the user didn't specify a kernel, then try to find one.
185
if [ -z "$KERNEL" ]; then
186
find_kernel
187
if [ -z "$KERNEL" ]; then
188
echo "Unable to find matching kernel for $VMCORE"
189
exit 1
190
fi
191
elif [ ! -e $KERNEL ]; then
192
echo "$KERNEL not found"
193
exit 1
194
fi
195
196
umask 077
197
198
# Simulate uname
199
ostype=$(gdb_command $KERNEL 'printf "%s", ostype')
200
osrelease=$(gdb_command $KERNEL 'printf "%s", osrelease')
201
version=$(gdb_command $KERNEL 'printf "%s", version' | tr '\t\n' ' ')
202
machine=$(gdb_command $KERNEL 'printf "%s", machine')
203
204
if ! $BATCH; then
205
echo "Writing crash summary to $FILE."
206
exec > $FILE 2>&1
207
fi
208
209
echo "$HOSTNAME dumped core - see $VMCORE"
210
echo
211
date
212
echo
213
echo "$ostype $HOSTNAME $osrelease $version $machine"
214
echo
215
sed -ne '/^ Panic String: /{s//panic: /;p;}' $INFO
216
echo
217
218
file=`mktemp /tmp/crashinfo.XXXXXX`
219
if [ $? -eq 0 ]; then
220
echo "bt -full" >> $file
221
echo "acttrace" >> $file
222
echo "quit" >> $file
223
${GDB%gdb}kgdb -q $KERNEL $VMCORE < $file
224
rm -f $file
225
echo
226
fi
227
echo
228
229
echo "------------------------------------------------------------------------"
230
echo "ps -axlww"
231
echo
232
ps -M $VMCORE -N $KERNEL -axlww
233
echo
234
235
echo "------------------------------------------------------------------------"
236
echo "vmstat -s"
237
echo
238
vmstat -M $VMCORE -N $KERNEL -s
239
echo
240
241
echo "------------------------------------------------------------------------"
242
echo "vmstat -m"
243
echo
244
vmstat -M $VMCORE -N $KERNEL -m
245
echo
246
247
echo "------------------------------------------------------------------------"
248
echo "vmstat -z"
249
echo
250
vmstat -M $VMCORE -N $KERNEL -z
251
echo
252
253
echo "------------------------------------------------------------------------"
254
echo "vmstat -i"
255
echo
256
vmstat -M $VMCORE -N $KERNEL -i
257
echo
258
259
echo "------------------------------------------------------------------------"
260
echo "pstat -T"
261
echo
262
pstat -M $VMCORE -N $KERNEL -T
263
echo
264
265
echo "------------------------------------------------------------------------"
266
echo "pstat -s"
267
echo
268
pstat -M $VMCORE -N $KERNEL -s
269
echo
270
271
echo "------------------------------------------------------------------------"
272
echo "iostat"
273
echo
274
iostat -M $VMCORE -N $KERNEL
275
echo
276
277
echo "------------------------------------------------------------------------"
278
echo "ipcs -a"
279
echo
280
ipcs -C $VMCORE -N $KERNEL -a
281
echo
282
283
echo "------------------------------------------------------------------------"
284
echo "ipcs -T"
285
echo
286
ipcs -C $VMCORE -N $KERNEL -T
287
echo
288
289
# XXX: This doesn't actually work in 5.x+
290
if false; then
291
echo "------------------------------------------------------------------------"
292
echo "w -dn"
293
echo
294
w -M $VMCORE -N $KERNEL -dn
295
echo
296
fi
297
298
echo "------------------------------------------------------------------------"
299
echo "netstat -s"
300
echo
301
netstat -M $VMCORE -N $KERNEL -s
302
echo
303
304
echo "------------------------------------------------------------------------"
305
echo "netstat -m"
306
echo
307
netstat -M $VMCORE -N $KERNEL -m
308
echo
309
310
echo "------------------------------------------------------------------------"
311
echo "netstat -anA"
312
echo
313
netstat -M $VMCORE -N $KERNEL -anA
314
echo
315
316
echo "------------------------------------------------------------------------"
317
echo "netstat -aL"
318
echo
319
netstat -M $VMCORE -N $KERNEL -aL
320
echo
321
322
echo "------------------------------------------------------------------------"
323
echo "fstat"
324
echo
325
fstat -M $VMCORE -N $KERNEL
326
echo
327
328
echo "------------------------------------------------------------------------"
329
echo "dmesg"
330
echo
331
dmesg -a -M $VMCORE -N $KERNEL
332
echo
333
334
echo "------------------------------------------------------------------------"
335
echo "kernel config"
336
echo
337
config -x $KERNEL
338
339
echo
340
echo "------------------------------------------------------------------------"
341
echo "ddb capture buffer"
342
echo
343
344
ddb capture -M $VMCORE -N $KERNEL print
345
346