3 # SPDX-License-Identifier: BSD-3-Clause
5 # Copyright (c) 2008 Yahoo!, Inc.
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
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.
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
35 echo "usage: crashinfo [-b] [-d crashdir] [-n dumpnr]" \
40 # Remove an uncompressed copy of a dump
44 [ -e $VMCORE ] && rm -f $VMCORE
47 # Find a gdb binary to use and save the value in GDB.
52 for binary in /usr/local/bin/gdb /usr/libexec/gdb; do
53 if [ -x ${binary} ]; then
60 # Run a single gdb command against a kernel file in batch mode.
61 # The kernel file is specified as the first argument and the command
62 # is given in the remaining arguments.
69 if [ ${GDB} = /usr/local/bin/gdb ]; then
70 ${GDB} -batch -ex "$@" $k
72 echo -e "$@" | ${GDB} -x /dev/stdin -batch $k
87 if ($0 ~ "^ [A-Za-z ]+: ") {
94 # Look for a matching kernel version, handling possible truncation
95 # of the version string recovered from the dump.
96 for k in `sysctl -n kern.bootfile` $(ls -t /boot/*/kernel); do
97 kvers=$(gdb_command $k 'printf " Version String: %s", version' | \
98 awk "{line=line\$0\"\n\"} END{print substr(line,1,${#ivers})}" \
100 if [ "$ivers" = "$kvers" ]; then
112 while getopts "bd:n:k:" opt; do
132 shift $((OPTIND - 1))
134 if [ $# -eq 1 ]; then
135 if [ -n "$DUMPNR" ]; then
136 echo "-n and an explicit vmcore are mutually exclusive"
140 # Figure out the crash directory and number from the vmcore name.
141 CRASHDIR=`dirname $1`
142 DUMPNR=$(expr $(basename $1) : 'vmcore\.\([0-9]*\)')
143 if [ -z "$DUMPNR" ]; then
144 echo "Unable to determine dump number from vmcore file $1."
147 elif [ $# -gt 1 ]; then
150 # If we don't have an explicit dump number, operate on the most
152 if [ -z "$DUMPNR" ]; then
153 if ! [ -r $CRASHDIR/bounds ]; then
154 echo "No crash dumps in $CRASHDIR."
157 next=`cat $CRASHDIR/bounds`
158 if [ -z "$next" ] || [ "$next" -eq 0 ]; then
159 echo "No crash dumps in $CRASHDIR."
162 DUMPNR=$(($next - 1))
166 VMCORE=$CRASHDIR/vmcore.$DUMPNR
167 INFO=$CRASHDIR/info.$DUMPNR
168 FILE=$CRASHDIR/core.txt.$DUMPNR
172 echo "Writing crash summary to $FILE."
177 if [ -z "$GDB" ]; then
178 echo "Unable to find a kernel debugger."
179 echo "Please install the devel/gdb port or gdb package."
183 if [ ! -e $VMCORE ]; then
184 if [ -e $VMCORE.gz ]; then
185 trap cleanup EXIT HUP INT QUIT TERM
186 gzcat $VMCORE.gz > $VMCORE
187 elif [ -e $VMCORE.zst ]; then
188 trap cleanup EXIT HUP INT QUIT TERM
189 zstdcat $VMCORE.zst > $VMCORE
191 echo "$VMCORE not found"
196 if [ ! -e $INFO ]; then
197 echo "$INFO not found"
201 # If the user didn't specify a kernel, then try to find one.
202 if [ -z "$KERNEL" ]; then
204 if [ -z "$KERNEL" ]; then
205 echo "Unable to find matching kernel for $VMCORE"
208 elif [ ! -e $KERNEL ]; then
209 echo "$KERNEL not found"
216 ostype=$(gdb_command $KERNEL 'printf "%s", ostype')
217 osrelease=$(gdb_command $KERNEL 'printf "%s", osrelease')
218 version=$(gdb_command $KERNEL 'printf "%s", version' | tr '\t\n' ' ')
219 machine=$(gdb_command $KERNEL 'printf "%s", machine')
222 echo "Writing crash summary to $FILE."
226 echo "$HOSTNAME dumped core - see $VMCORE"
230 echo "$ostype $HOSTNAME $osrelease $version $machine"
232 sed -ne '/^ Panic String: /{s//panic: /;p;}' $INFO
235 # XXX: /bin/sh on 7.0+ is broken so we can't simply pipe the commands to
236 # kgdb via stdin and have to use a temporary file instead.
237 file=`mktemp /tmp/crashinfo.XXXXXX`
238 if [ $? -eq 0 ]; then
241 ${GDB%gdb}kgdb $KERNEL $VMCORE < $file
247 echo "------------------------------------------------------------------------"
250 ps -M $VMCORE -N $KERNEL -axlww
253 echo "------------------------------------------------------------------------"
256 vmstat -M $VMCORE -N $KERNEL -s
259 echo "------------------------------------------------------------------------"
262 vmstat -M $VMCORE -N $KERNEL -m
265 echo "------------------------------------------------------------------------"
268 vmstat -M $VMCORE -N $KERNEL -z
271 echo "------------------------------------------------------------------------"
274 vmstat -M $VMCORE -N $KERNEL -i
277 echo "------------------------------------------------------------------------"
280 pstat -M $VMCORE -N $KERNEL -T
283 echo "------------------------------------------------------------------------"
286 pstat -M $VMCORE -N $KERNEL -s
289 echo "------------------------------------------------------------------------"
292 iostat -M $VMCORE -N $KERNEL
295 echo "------------------------------------------------------------------------"
298 ipcs -C $VMCORE -N $KERNEL -a
301 echo "------------------------------------------------------------------------"
304 ipcs -C $VMCORE -N $KERNEL -T
307 # XXX: This doesn't actually work in 5.x+
309 echo "------------------------------------------------------------------------"
312 w -M $VMCORE -N $KERNEL -dn
316 echo "------------------------------------------------------------------------"
319 netstat -M $VMCORE -N $KERNEL -s
322 echo "------------------------------------------------------------------------"
325 netstat -M $VMCORE -N $KERNEL -m
328 echo "------------------------------------------------------------------------"
331 netstat -M $VMCORE -N $KERNEL -anA
334 echo "------------------------------------------------------------------------"
337 netstat -M $VMCORE -N $KERNEL -aL
340 echo "------------------------------------------------------------------------"
343 fstat -M $VMCORE -N $KERNEL
346 echo "------------------------------------------------------------------------"
349 dmesg -a -M $VMCORE -N $KERNEL
352 echo "------------------------------------------------------------------------"
358 echo "------------------------------------------------------------------------"
359 echo "ddb capture buffer"
362 ddb capture -M $VMCORE -N $KERNEL print