xref: /freebsd/usr.sbin/crashinfo/crashinfo.sh (revision 2524b7dfb0df7e37ea9651559ce6c027b006e2a9)
1a37f9716SJohn Baldwin#!/bin/sh
2a37f9716SJohn Baldwin#
38a16b7a1SPedro F. Giffuni# SPDX-License-Identifier: BSD-3-Clause
48a16b7a1SPedro F. Giffuni#
5a37f9716SJohn Baldwin# Copyright (c) 2008 Yahoo!, Inc.
6a37f9716SJohn Baldwin# All rights reserved.
7a37f9716SJohn Baldwin#
8a37f9716SJohn Baldwin# Redistribution and use in source and binary forms, with or without
9a37f9716SJohn Baldwin# modification, are permitted provided that the following conditions
10a37f9716SJohn Baldwin# are met:
11a37f9716SJohn Baldwin# 1. Redistributions of source code must retain the above copyright
12a37f9716SJohn Baldwin#    notice, this list of conditions and the following disclaimer.
13a37f9716SJohn Baldwin# 2. Redistributions in binary form must reproduce the above copyright
14a37f9716SJohn Baldwin#    notice, this list of conditions and the following disclaimer in the
15a37f9716SJohn Baldwin#    documentation and/or other materials provided with the distribution.
16a37f9716SJohn Baldwin# 3. Neither the name of the author nor the names of any co-contributors
17a37f9716SJohn Baldwin#    may be used to endorse or promote products derived from this software
18a37f9716SJohn Baldwin#    without specific prior written permission.
19a37f9716SJohn Baldwin#
20a37f9716SJohn Baldwin# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21a37f9716SJohn Baldwin# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22a37f9716SJohn Baldwin# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23a37f9716SJohn Baldwin# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24a37f9716SJohn Baldwin# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25a37f9716SJohn Baldwin# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26a37f9716SJohn Baldwin# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27a37f9716SJohn Baldwin# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28a37f9716SJohn Baldwin# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29a37f9716SJohn Baldwin# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30a37f9716SJohn Baldwin# SUCH DAMAGE.
31a37f9716SJohn Baldwin#
32a37f9716SJohn Baldwin
33a37f9716SJohn Baldwinusage()
34a37f9716SJohn Baldwin{
351f1ed24cSEric van Gyzen	echo "usage: crashinfo [-b] [-d crashdir] [-n dumpnr]" \
361f1ed24cSEric van Gyzen		"[-k kernel] [core]"
37a37f9716SJohn Baldwin	exit 1
38a37f9716SJohn Baldwin}
39a37f9716SJohn Baldwin
406f77212fSJohn Baldwin# Remove an uncompressed copy of a dump
416f77212fSJohn Baldwincleanup()
426f77212fSJohn Baldwin{
436f77212fSJohn Baldwin
446f77212fSJohn Baldwin	[ -e $VMCORE ] && rm -f $VMCORE
456f77212fSJohn Baldwin}
466f77212fSJohn Baldwin
47241cf416SJohn Baldwin# Run a single gdb command against a kernel file in batch mode.
48241cf416SJohn Baldwin# The kernel file is specified as the first argument and the command
49241cf416SJohn Baldwin# is given in the remaining arguments.
50241cf416SJohn Baldwingdb_command()
51241cf416SJohn Baldwin{
52241cf416SJohn Baldwin	local k
53241cf416SJohn Baldwin
54241cf416SJohn Baldwin	k=$1 ; shift
55241cf416SJohn Baldwin
5673ad3fb5SJohn Baldwin	${GDB} -batch -ex "$@" $k
57241cf416SJohn Baldwin}
58241cf416SJohn Baldwin
59a37f9716SJohn Baldwinfind_kernel()
60a37f9716SJohn Baldwin{
61a37f9716SJohn Baldwin	local ivers k kvers
62a37f9716SJohn Baldwin
63a37f9716SJohn Baldwin	ivers=$(awk '
64a37f9716SJohn Baldwin	/Version String/ {
65a37f9716SJohn Baldwin		print
66a37f9716SJohn Baldwin		nextline=1
67a37f9716SJohn Baldwin		next
68a37f9716SJohn Baldwin	}
697ad50a36SEd Maste	nextline==1 {
707ad50a36SEd Maste		if ($0 ~ "^  [A-Za-z ]+: ") {
71a37f9716SJohn Baldwin			nextline=0
727ad50a36SEd Maste		} else {
737ad50a36SEd Maste			print
74a37f9716SJohn Baldwin		}
75a37f9716SJohn Baldwin	}' $INFO)
76a37f9716SJohn Baldwin
775a3924b9SMark Johnston	# Look for a matching kernel version, handling possible truncation
785a3924b9SMark Johnston	# of the version string recovered from the dump.
799decd446SColin Percival	for k in `sysctl -n kern.bootfile` $(ls -t /boot/*/kernel); do
805a3924b9SMark Johnston		kvers=$(gdb_command $k 'printf "  Version String: %s", version' | \
815a3924b9SMark Johnston		    awk "{line=line\$0\"\n\"} END{print substr(line,1,${#ivers})}" \
82241cf416SJohn Baldwin		    2>/dev/null)
83a37f9716SJohn Baldwin		if [ "$ivers" = "$kvers" ]; then
84a37f9716SJohn Baldwin			KERNEL=$k
85a37f9716SJohn Baldwin			break
86a37f9716SJohn Baldwin		fi
87a37f9716SJohn Baldwin	done
88a37f9716SJohn Baldwin}
89a37f9716SJohn Baldwin
901f1ed24cSEric van GyzenBATCH=false
91a37f9716SJohn BaldwinCRASHDIR=/var/crash
92a37f9716SJohn BaldwinDUMPNR=
93a37f9716SJohn BaldwinKERNEL=
94a37f9716SJohn Baldwin
951f1ed24cSEric van Gyzenwhile getopts "bd:n:k:" opt; do
96a37f9716SJohn Baldwin	case "$opt" in
971f1ed24cSEric van Gyzen	b)
981f1ed24cSEric van Gyzen		BATCH=true
991f1ed24cSEric van Gyzen		;;
100a37f9716SJohn Baldwin	d)
101a37f9716SJohn Baldwin		CRASHDIR=$OPTARG
102a37f9716SJohn Baldwin		;;
103a37f9716SJohn Baldwin	n)
104a37f9716SJohn Baldwin		DUMPNR=$OPTARG
105a37f9716SJohn Baldwin		;;
106a37f9716SJohn Baldwin	k)
107a37f9716SJohn Baldwin		KERNEL=$OPTARG
108a37f9716SJohn Baldwin		;;
109a37f9716SJohn Baldwin	\?)
110a37f9716SJohn Baldwin		usage
111a37f9716SJohn Baldwin		;;
112a37f9716SJohn Baldwin	esac
113a37f9716SJohn Baldwindone
114a37f9716SJohn Baldwin
115a37f9716SJohn Baldwinshift $((OPTIND - 1))
116a37f9716SJohn Baldwin
117a37f9716SJohn Baldwinif [ $# -eq 1 ]; then
118a37f9716SJohn Baldwin	if [ -n "$DUMPNR" ]; then
119a37f9716SJohn Baldwin		echo "-n and an explicit vmcore are mutually exclusive"
120a37f9716SJohn Baldwin		usage
121a37f9716SJohn Baldwin	fi
122a37f9716SJohn Baldwin
123a37f9716SJohn Baldwin	# Figure out the crash directory and number from the vmcore name.
124a37f9716SJohn Baldwin	CRASHDIR=`dirname $1`
1256f77212fSJohn Baldwin	DUMPNR=$(expr $(basename $1) : 'vmcore\.\([0-9]*\)')
126a37f9716SJohn Baldwin	if [ -z "$DUMPNR" ]; then
127a37f9716SJohn Baldwin		echo "Unable to determine dump number from vmcore file $1."
128a37f9716SJohn Baldwin		exit 1
129a37f9716SJohn Baldwin	fi
130a37f9716SJohn Baldwinelif [ $# -gt 1 ]; then
131a37f9716SJohn Baldwin	usage
132a37f9716SJohn Baldwinelse
133a37f9716SJohn Baldwin	# If we don't have an explicit dump number, operate on the most
134a37f9716SJohn Baldwin	# recent dump.
135a37f9716SJohn Baldwin	if [ -z "$DUMPNR" ]; then
136a37f9716SJohn Baldwin		if ! [ -r $CRASHDIR/bounds ]; then
137a37f9716SJohn Baldwin			echo "No crash dumps in $CRASHDIR."
138a37f9716SJohn Baldwin			exit 1
139a37f9716SJohn Baldwin		fi
140a37f9716SJohn Baldwin		next=`cat $CRASHDIR/bounds`
141a37f9716SJohn Baldwin		if [ -z "$next" ] || [ "$next" -eq 0 ]; then
142a37f9716SJohn Baldwin			echo "No crash dumps in $CRASHDIR."
143a37f9716SJohn Baldwin			exit 1
144a37f9716SJohn Baldwin		fi
145a37f9716SJohn Baldwin		DUMPNR=$(($next - 1))
146a37f9716SJohn Baldwin	fi
147a37f9716SJohn Baldwinfi
148a37f9716SJohn Baldwin
149a37f9716SJohn BaldwinVMCORE=$CRASHDIR/vmcore.$DUMPNR
150a37f9716SJohn BaldwinINFO=$CRASHDIR/info.$DUMPNR
151a37f9716SJohn BaldwinFILE=$CRASHDIR/core.txt.$DUMPNR
152a37f9716SJohn BaldwinHOSTNAME=`hostname`
153a37f9716SJohn Baldwin
1541f1ed24cSEric van Gyzenif $BATCH; then
1551f1ed24cSEric van Gyzen	echo "Writing crash summary to $FILE."
1561f1ed24cSEric van Gyzen	exec > $FILE 2>&1
1571f1ed24cSEric van Gyzenfi
1581f1ed24cSEric van Gyzen
15913a97457SEd MasteGDB=/usr/local/bin/gdb
16013a97457SEd Masteif [ ! -x "$GDB" ]; then
16173ad3fb5SJohn Baldwin	echo "Unable to find a kernel debugger."
16255e6cbc4SEd Maste	echo "Please install the devel/gdb port or gdb package."
16373ad3fb5SJohn Baldwin	exit 1
16473ad3fb5SJohn Baldwinfi
16573ad3fb5SJohn Baldwin
166a37f9716SJohn Baldwinif [ ! -e $VMCORE ]; then
1676f77212fSJohn Baldwin    	if [ -e $VMCORE.gz ]; then
1686f77212fSJohn Baldwin		trap cleanup EXIT HUP INT QUIT TERM
1696f77212fSJohn Baldwin		gzcat $VMCORE.gz > $VMCORE
1706f77212fSJohn Baldwin	elif [ -e $VMCORE.zst ]; then
1716f77212fSJohn Baldwin		trap cleanup EXIT HUP INT QUIT TERM
1726f77212fSJohn Baldwin		zstdcat $VMCORE.zst > $VMCORE
1736f77212fSJohn Baldwin	else
174a37f9716SJohn Baldwin		echo "$VMCORE not found"
175a37f9716SJohn Baldwin		exit 1
176a37f9716SJohn Baldwin	fi
1776f77212fSJohn Baldwinfi
178a37f9716SJohn Baldwin
179a37f9716SJohn Baldwinif [ ! -e $INFO ]; then
180a37f9716SJohn Baldwin	echo "$INFO not found"
181a37f9716SJohn Baldwin	exit 1
182a37f9716SJohn Baldwinfi
183a37f9716SJohn Baldwin
184a37f9716SJohn Baldwin# If the user didn't specify a kernel, then try to find one.
185a37f9716SJohn Baldwinif [ -z "$KERNEL" ]; then
186a37f9716SJohn Baldwin	find_kernel
187a37f9716SJohn Baldwin	if [ -z "$KERNEL" ]; then
188a37f9716SJohn Baldwin		echo "Unable to find matching kernel for $VMCORE"
189a37f9716SJohn Baldwin		exit 1
190a37f9716SJohn Baldwin	fi
191a37f9716SJohn Baldwinelif [ ! -e $KERNEL ]; then
192a37f9716SJohn Baldwin	echo "$KERNEL not found"
193a37f9716SJohn Baldwin	exit 1
194a37f9716SJohn Baldwinfi
195a37f9716SJohn Baldwin
19659bf36c3SXin LIumask 077
19759bf36c3SXin LI
198a37f9716SJohn Baldwin# Simulate uname
199241cf416SJohn Baldwinostype=$(gdb_command $KERNEL 'printf "%s", ostype')
200241cf416SJohn Baldwinosrelease=$(gdb_command $KERNEL 'printf "%s", osrelease')
201241cf416SJohn Baldwinversion=$(gdb_command $KERNEL 'printf "%s", version' | tr '\t\n' '  ')
202241cf416SJohn Baldwinmachine=$(gdb_command $KERNEL 'printf "%s", machine')
203a37f9716SJohn Baldwin
2041f1ed24cSEric van Gyzenif ! $BATCH; then
2051f1ed24cSEric van Gyzen	echo "Writing crash summary to $FILE."
206a37f9716SJohn Baldwin	exec > $FILE 2>&1
2071f1ed24cSEric van Gyzenfi
208a37f9716SJohn Baldwin
209a37f9716SJohn Baldwinecho "$HOSTNAME dumped core - see $VMCORE"
210a37f9716SJohn Baldwinecho
211a37f9716SJohn Baldwindate
212a37f9716SJohn Baldwinecho
213a37f9716SJohn Baldwinecho "$ostype $HOSTNAME $osrelease $version $machine"
214a37f9716SJohn Baldwinecho
215a37f9716SJohn Baldwinsed -ne '/^  Panic String: /{s//panic: /;p;}' $INFO
216a37f9716SJohn Baldwinecho
217a37f9716SJohn Baldwin
218a37f9716SJohn Baldwinfile=`mktemp /tmp/crashinfo.XXXXXX`
219a37f9716SJohn Baldwinif [ $? -eq 0 ]; then
220*2524b7dfSMark Johnston	scriptdir=/usr/libexec/kgdb
221*2524b7dfSMark Johnston
2223e356fb8SMark Johnston	echo "bt -full" >> $file
223*2524b7dfSMark Johnston	echo "source ${scriptdir}/acttrace.py" >> $file
224*2524b7dfSMark Johnston	echo "acttrace" >> $file
225a37f9716SJohn Baldwin	echo "quit" >> $file
226*2524b7dfSMark Johnston	${GDB%gdb}kgdb -q $KERNEL $VMCORE < $file
227a37f9716SJohn Baldwin	rm -f $file
228a37f9716SJohn Baldwin	echo
229a37f9716SJohn Baldwinfi
230a37f9716SJohn Baldwinecho
231a37f9716SJohn Baldwin
232a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
23316aaaa69SSergey Kandaurovecho "ps -axlww"
234a37f9716SJohn Baldwinecho
23516aaaa69SSergey Kandaurovps -M $VMCORE -N $KERNEL -axlww
236a37f9716SJohn Baldwinecho
237a37f9716SJohn Baldwin
238a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
239a37f9716SJohn Baldwinecho "vmstat -s"
240a37f9716SJohn Baldwinecho
241a37f9716SJohn Baldwinvmstat -M $VMCORE -N $KERNEL -s
242a37f9716SJohn Baldwinecho
243a37f9716SJohn Baldwin
244a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
245a37f9716SJohn Baldwinecho "vmstat -m"
246a37f9716SJohn Baldwinecho
247a37f9716SJohn Baldwinvmstat -M $VMCORE -N $KERNEL -m
248a37f9716SJohn Baldwinecho
249a37f9716SJohn Baldwin
250a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
251a37f9716SJohn Baldwinecho "vmstat -z"
252a37f9716SJohn Baldwinecho
253a37f9716SJohn Baldwinvmstat -M $VMCORE -N $KERNEL -z
254a37f9716SJohn Baldwinecho
255a37f9716SJohn Baldwin
256a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
257a37f9716SJohn Baldwinecho "vmstat -i"
258a37f9716SJohn Baldwinecho
259a37f9716SJohn Baldwinvmstat -M $VMCORE -N $KERNEL -i
260a37f9716SJohn Baldwinecho
261a37f9716SJohn Baldwin
262a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
263a37f9716SJohn Baldwinecho "pstat -T"
264a37f9716SJohn Baldwinecho
265a37f9716SJohn Baldwinpstat -M $VMCORE -N $KERNEL -T
266a37f9716SJohn Baldwinecho
267a37f9716SJohn Baldwin
268a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
269a37f9716SJohn Baldwinecho "pstat -s"
270a37f9716SJohn Baldwinecho
271a37f9716SJohn Baldwinpstat -M $VMCORE -N $KERNEL -s
272a37f9716SJohn Baldwinecho
273a37f9716SJohn Baldwin
274a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
275a37f9716SJohn Baldwinecho "iostat"
276a37f9716SJohn Baldwinecho
277a37f9716SJohn Baldwiniostat -M $VMCORE -N $KERNEL
278a37f9716SJohn Baldwinecho
279a37f9716SJohn Baldwin
280a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
281a37f9716SJohn Baldwinecho "ipcs -a"
282a37f9716SJohn Baldwinecho
283a37f9716SJohn Baldwinipcs -C $VMCORE -N $KERNEL -a
284a37f9716SJohn Baldwinecho
285a37f9716SJohn Baldwin
286a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
287a37f9716SJohn Baldwinecho "ipcs -T"
288a37f9716SJohn Baldwinecho
289a37f9716SJohn Baldwinipcs -C $VMCORE -N $KERNEL -T
290a37f9716SJohn Baldwinecho
291a37f9716SJohn Baldwin
292a37f9716SJohn Baldwin# XXX: This doesn't actually work in 5.x+
293a37f9716SJohn Baldwinif false; then
294a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
295a37f9716SJohn Baldwinecho "w -dn"
296a37f9716SJohn Baldwinecho
297a37f9716SJohn Baldwinw -M $VMCORE -N $KERNEL -dn
298a37f9716SJohn Baldwinecho
299a37f9716SJohn Baldwinfi
300a37f9716SJohn Baldwin
301a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
302a37f9716SJohn Baldwinecho "netstat -s"
303a37f9716SJohn Baldwinecho
304a37f9716SJohn Baldwinnetstat -M $VMCORE -N $KERNEL -s
305a37f9716SJohn Baldwinecho
306a37f9716SJohn Baldwin
307a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
308a37f9716SJohn Baldwinecho "netstat -m"
309a37f9716SJohn Baldwinecho
310a37f9716SJohn Baldwinnetstat -M $VMCORE -N $KERNEL -m
311a37f9716SJohn Baldwinecho
312a37f9716SJohn Baldwin
313a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
314a37f9716SJohn Baldwinecho "netstat -anA"
315a37f9716SJohn Baldwinecho
316a37f9716SJohn Baldwinnetstat -M $VMCORE -N $KERNEL -anA
317a37f9716SJohn Baldwinecho
318a37f9716SJohn Baldwin
319a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
320a37f9716SJohn Baldwinecho "netstat -aL"
321a37f9716SJohn Baldwinecho
322a37f9716SJohn Baldwinnetstat -M $VMCORE -N $KERNEL -aL
323a37f9716SJohn Baldwinecho
324a37f9716SJohn Baldwin
325a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
326a37f9716SJohn Baldwinecho "fstat"
327a37f9716SJohn Baldwinecho
328a37f9716SJohn Baldwinfstat -M $VMCORE -N $KERNEL
329a37f9716SJohn Baldwinecho
330a37f9716SJohn Baldwin
331a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
332a37f9716SJohn Baldwinecho "dmesg"
333a37f9716SJohn Baldwinecho
334a37f9716SJohn Baldwindmesg -a -M $VMCORE -N $KERNEL
335a37f9716SJohn Baldwinecho
336a37f9716SJohn Baldwin
337a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
338a37f9716SJohn Baldwinecho "kernel config"
339a37f9716SJohn Baldwinecho
340a37f9716SJohn Baldwinconfig -x $KERNEL
341c5229b9dSJohn Baldwin
342c5229b9dSJohn Baldwinecho
343c5229b9dSJohn Baldwinecho "------------------------------------------------------------------------"
344c5229b9dSJohn Baldwinecho "ddb capture buffer"
345c5229b9dSJohn Baldwinecho
346c5229b9dSJohn Baldwin
347c5229b9dSJohn Baldwinddb capture -M $VMCORE -N $KERNEL print
348