xref: /freebsd/usr.sbin/crashinfo/crashinfo.sh (revision 59bf36c3a797699801b9fe746d44650134554074)
1a37f9716SJohn Baldwin#!/bin/sh
2a37f9716SJohn Baldwin#
3a37f9716SJohn Baldwin# Copyright (c) 2008 Yahoo!, Inc.
4a37f9716SJohn Baldwin# All rights reserved.
5a37f9716SJohn Baldwin#
6a37f9716SJohn Baldwin# Redistribution and use in source and binary forms, with or without
7a37f9716SJohn Baldwin# modification, are permitted provided that the following conditions
8a37f9716SJohn Baldwin# are met:
9a37f9716SJohn Baldwin# 1. Redistributions of source code must retain the above copyright
10a37f9716SJohn Baldwin#    notice, this list of conditions and the following disclaimer.
11a37f9716SJohn Baldwin# 2. Redistributions in binary form must reproduce the above copyright
12a37f9716SJohn Baldwin#    notice, this list of conditions and the following disclaimer in the
13a37f9716SJohn Baldwin#    documentation and/or other materials provided with the distribution.
14a37f9716SJohn Baldwin# 3. Neither the name of the author nor the names of any co-contributors
15a37f9716SJohn Baldwin#    may be used to endorse or promote products derived from this software
16a37f9716SJohn Baldwin#    without specific prior written permission.
17a37f9716SJohn Baldwin#
18a37f9716SJohn Baldwin# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19a37f9716SJohn Baldwin# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20a37f9716SJohn Baldwin# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21a37f9716SJohn Baldwin# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22a37f9716SJohn Baldwin# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23a37f9716SJohn Baldwin# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24a37f9716SJohn Baldwin# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25a37f9716SJohn Baldwin# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26a37f9716SJohn Baldwin# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27a37f9716SJohn Baldwin# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28a37f9716SJohn Baldwin# SUCH DAMAGE.
29a37f9716SJohn Baldwin#
30a37f9716SJohn Baldwin# $FreeBSD$
31a37f9716SJohn Baldwin
32a37f9716SJohn Baldwinusage()
33a37f9716SJohn Baldwin{
34a37f9716SJohn Baldwin	echo "usage: crashinfo [-d crashdir] [-n dumpnr] [-k kernel] [core]"
35a37f9716SJohn Baldwin	exit 1
36a37f9716SJohn Baldwin}
37a37f9716SJohn Baldwin
38a37f9716SJohn Baldwinfind_kernel()
39a37f9716SJohn Baldwin{
40a37f9716SJohn Baldwin	local ivers k kvers
41a37f9716SJohn Baldwin
42a37f9716SJohn Baldwin	ivers=$(awk '
43a37f9716SJohn Baldwin	/Version String/ {
44a37f9716SJohn Baldwin		print
45a37f9716SJohn Baldwin		nextline=1
46a37f9716SJohn Baldwin		next
47a37f9716SJohn Baldwin	}
48a37f9716SJohn Baldwin	// {
49a37f9716SJohn Baldwin		if (nextline) {
50a37f9716SJohn Baldwin			print
51a37f9716SJohn Baldwin			nextline=0
52a37f9716SJohn Baldwin		}
53a37f9716SJohn Baldwin	}' $INFO)
54a37f9716SJohn Baldwin
55a37f9716SJohn Baldwin	# Look for a matching kernel version.
56a37f9716SJohn Baldwin	for k in /boot/kernel/kernel $(ls -t /boot/*/kernel); do
57a37f9716SJohn Baldwin		kvers=$(echo 'printf "  Version String: %s", version' | \
58a37f9716SJohn Baldwin		    gdb -x /dev/stdin -batch $k 2>/dev/null)
59a37f9716SJohn Baldwin		if [ "$ivers" = "$kvers" ]; then
60a37f9716SJohn Baldwin			KERNEL=$k
61a37f9716SJohn Baldwin			break
62a37f9716SJohn Baldwin		fi
63a37f9716SJohn Baldwin	done
64a37f9716SJohn Baldwin}
65a37f9716SJohn Baldwin
66a37f9716SJohn BaldwinCRASHDIR=/var/crash
67a37f9716SJohn BaldwinDUMPNR=
68a37f9716SJohn BaldwinKERNEL=
69a37f9716SJohn Baldwin
70a37f9716SJohn Baldwinwhile getopts "d:n:k:" opt; do
71a37f9716SJohn Baldwin	case "$opt" in
72a37f9716SJohn Baldwin	d)
73a37f9716SJohn Baldwin		CRASHDIR=$OPTARG
74a37f9716SJohn Baldwin		;;
75a37f9716SJohn Baldwin	n)
76a37f9716SJohn Baldwin		DUMPNR=$OPTARG
77a37f9716SJohn Baldwin		;;
78a37f9716SJohn Baldwin	k)
79a37f9716SJohn Baldwin		KERNEL=$OPTARG
80a37f9716SJohn Baldwin		;;
81a37f9716SJohn Baldwin	\?)
82a37f9716SJohn Baldwin		usage
83a37f9716SJohn Baldwin		;;
84a37f9716SJohn Baldwin	esac
85a37f9716SJohn Baldwindone
86a37f9716SJohn Baldwin
87a37f9716SJohn Baldwinshift $((OPTIND - 1))
88a37f9716SJohn Baldwin
89a37f9716SJohn Baldwinif [ $# -eq 1 ]; then
90a37f9716SJohn Baldwin	if [ -n "$DUMPNR" ]; then
91a37f9716SJohn Baldwin		echo "-n and an explicit vmcore are mutually exclusive"
92a37f9716SJohn Baldwin		usage
93a37f9716SJohn Baldwin	fi
94a37f9716SJohn Baldwin
95a37f9716SJohn Baldwin	# Figure out the crash directory and number from the vmcore name.
96a37f9716SJohn Baldwin	CRASHDIR=`dirname $1`
97a37f9716SJohn Baldwin	DUMPNR=$(expr $(basename $1) : 'vmcore\.\([0-9]*\)$')
98a37f9716SJohn Baldwin	if [ -z "$DUMPNR" ]; then
99a37f9716SJohn Baldwin		echo "Unable to determine dump number from vmcore file $1."
100a37f9716SJohn Baldwin		exit 1
101a37f9716SJohn Baldwin	fi
102a37f9716SJohn Baldwinelif [ $# -gt 1 ]; then
103a37f9716SJohn Baldwin	usage
104a37f9716SJohn Baldwinelse
105a37f9716SJohn Baldwin	# If we don't have an explicit dump number, operate on the most
106a37f9716SJohn Baldwin	# recent dump.
107a37f9716SJohn Baldwin	if [ -z "$DUMPNR" ]; then
108a37f9716SJohn Baldwin		if ! [ -r $CRASHDIR/bounds ]; then
109a37f9716SJohn Baldwin			echo "No crash dumps in $CRASHDIR."
110a37f9716SJohn Baldwin			exit 1
111a37f9716SJohn Baldwin		fi
112a37f9716SJohn Baldwin		next=`cat $CRASHDIR/bounds`
113a37f9716SJohn Baldwin		if [ -z "$next" ] || [ "$next" -eq 0 ]; then
114a37f9716SJohn Baldwin			echo "No crash dumps in $CRASHDIR."
115a37f9716SJohn Baldwin			exit 1
116a37f9716SJohn Baldwin		fi
117a37f9716SJohn Baldwin		DUMPNR=$(($next - 1))
118a37f9716SJohn Baldwin	fi
119a37f9716SJohn Baldwinfi
120a37f9716SJohn Baldwin
121a37f9716SJohn BaldwinVMCORE=$CRASHDIR/vmcore.$DUMPNR
122a37f9716SJohn BaldwinINFO=$CRASHDIR/info.$DUMPNR
123a37f9716SJohn BaldwinFILE=$CRASHDIR/core.txt.$DUMPNR
124a37f9716SJohn BaldwinHOSTNAME=`hostname`
125a37f9716SJohn Baldwin
126a37f9716SJohn Baldwinif [ ! -e $VMCORE ]; then
127a37f9716SJohn Baldwin	echo "$VMCORE not found"
128a37f9716SJohn Baldwin	exit 1
129a37f9716SJohn Baldwinfi
130a37f9716SJohn Baldwin
131a37f9716SJohn Baldwinif [ ! -e $INFO ]; then
132a37f9716SJohn Baldwin	echo "$INFO not found"
133a37f9716SJohn Baldwin	exit 1
134a37f9716SJohn Baldwinfi
135a37f9716SJohn Baldwin
136a37f9716SJohn Baldwin# If the user didn't specify a kernel, then try to find one.
137a37f9716SJohn Baldwinif [ -z "$KERNEL" ]; then
138a37f9716SJohn Baldwin	find_kernel
139a37f9716SJohn Baldwin	if [ -z "$KERNEL" ]; then
140a37f9716SJohn Baldwin		echo "Unable to find matching kernel for $VMCORE"
141a37f9716SJohn Baldwin		exit 1
142a37f9716SJohn Baldwin	fi
143a37f9716SJohn Baldwinelif [ ! -e $KERNEL ]; then
144a37f9716SJohn Baldwin	echo "$KERNEL not found"
145a37f9716SJohn Baldwin	exit 1
146a37f9716SJohn Baldwinfi
147a37f9716SJohn Baldwin
148a37f9716SJohn Baldwinecho "Writing crash summary to $FILE."
149a37f9716SJohn Baldwin
15059bf36c3SXin LIumask 077
15159bf36c3SXin LI
152a37f9716SJohn Baldwin# Simulate uname
153a37f9716SJohn Baldwinostype=$(echo -e printf '"%s", ostype' | gdb -x /dev/stdin -batch $KERNEL)
154a37f9716SJohn Baldwinosrelease=$(echo -e printf '"%s", osrelease' | gdb -x /dev/stdin -batch $KERNEL)
155a37f9716SJohn Baldwinversion=$(echo -e printf '"%s", version' | gdb -x /dev/stdin -batch $KERNEL | \
156a37f9716SJohn Baldwin    tr '\t\n' '  ')
157a37f9716SJohn Baldwinmachine=$(echo -e printf '"%s", machine' | gdb -x /dev/stdin -batch $KERNEL)
158a37f9716SJohn Baldwin
159a37f9716SJohn Baldwinexec > $FILE 2>&1
160a37f9716SJohn Baldwin
161a37f9716SJohn Baldwinecho "$HOSTNAME dumped core - see $VMCORE"
162a37f9716SJohn Baldwinecho
163a37f9716SJohn Baldwindate
164a37f9716SJohn Baldwinecho
165a37f9716SJohn Baldwinecho "$ostype $HOSTNAME $osrelease $version $machine"
166a37f9716SJohn Baldwinecho
167a37f9716SJohn Baldwinsed -ne '/^  Panic String: /{s//panic: /;p;}' $INFO
168a37f9716SJohn Baldwinecho
169a37f9716SJohn Baldwin
170a37f9716SJohn Baldwin# XXX: /bin/sh on 7.0+ is broken so we can't simply pipe the commands to
171a37f9716SJohn Baldwin# kgdb via stdin and have to use a temporary file instead.
172a37f9716SJohn Baldwinfile=`mktemp /tmp/crashinfo.XXXXXX`
173a37f9716SJohn Baldwinif [ $? -eq 0 ]; then
174a37f9716SJohn Baldwin	echo "bt" >> $file
175a37f9716SJohn Baldwin	echo "quit" >> $file
176a37f9716SJohn Baldwin	kgdb $KERNEL $VMCORE < $file
177a37f9716SJohn Baldwin	rm -f $file
178a37f9716SJohn Baldwin	echo
179a37f9716SJohn Baldwinfi
180a37f9716SJohn Baldwinecho
181a37f9716SJohn Baldwin
182a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
183a37f9716SJohn Baldwinecho "ps -axl"
184a37f9716SJohn Baldwinecho
185a37f9716SJohn Baldwinps -M $VMCORE -N $KERNEL -axl
186a37f9716SJohn Baldwinecho
187a37f9716SJohn Baldwin
188a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
189a37f9716SJohn Baldwinecho "vmstat -s"
190a37f9716SJohn Baldwinecho
191a37f9716SJohn Baldwinvmstat -M $VMCORE -N $KERNEL -s
192a37f9716SJohn Baldwinecho
193a37f9716SJohn Baldwin
194a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
195a37f9716SJohn Baldwinecho "vmstat -m"
196a37f9716SJohn Baldwinecho
197a37f9716SJohn Baldwinvmstat -M $VMCORE -N $KERNEL -m
198a37f9716SJohn Baldwinecho
199a37f9716SJohn Baldwin
200a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
201a37f9716SJohn Baldwinecho "vmstat -z"
202a37f9716SJohn Baldwinecho
203a37f9716SJohn Baldwinvmstat -M $VMCORE -N $KERNEL -z
204a37f9716SJohn Baldwinecho
205a37f9716SJohn Baldwin
206a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
207a37f9716SJohn Baldwinecho "vmstat -i"
208a37f9716SJohn Baldwinecho
209a37f9716SJohn Baldwinvmstat -M $VMCORE -N $KERNEL -i
210a37f9716SJohn Baldwinecho
211a37f9716SJohn Baldwin
212a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
213a37f9716SJohn Baldwinecho "pstat -T"
214a37f9716SJohn Baldwinecho
215a37f9716SJohn Baldwinpstat -M $VMCORE -N $KERNEL -T
216a37f9716SJohn Baldwinecho
217a37f9716SJohn Baldwin
218a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
219a37f9716SJohn Baldwinecho "pstat -s"
220a37f9716SJohn Baldwinecho
221a37f9716SJohn Baldwinpstat -M $VMCORE -N $KERNEL -s
222a37f9716SJohn Baldwinecho
223a37f9716SJohn Baldwin
224a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
225a37f9716SJohn Baldwinecho "iostat"
226a37f9716SJohn Baldwinecho
227a37f9716SJohn Baldwiniostat -M $VMCORE -N $KERNEL
228a37f9716SJohn Baldwinecho
229a37f9716SJohn Baldwin
230a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
231a37f9716SJohn Baldwinecho "ipcs -a"
232a37f9716SJohn Baldwinecho
233a37f9716SJohn Baldwinipcs -C $VMCORE -N $KERNEL -a
234a37f9716SJohn Baldwinecho
235a37f9716SJohn Baldwin
236a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
237a37f9716SJohn Baldwinecho "ipcs -T"
238a37f9716SJohn Baldwinecho
239a37f9716SJohn Baldwinipcs -C $VMCORE -N $KERNEL -T
240a37f9716SJohn Baldwinecho
241a37f9716SJohn Baldwin
242a37f9716SJohn Baldwin# XXX: This doesn't actually work in 5.x+
243a37f9716SJohn Baldwinif false; then
244a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
245a37f9716SJohn Baldwinecho "w -dn"
246a37f9716SJohn Baldwinecho
247a37f9716SJohn Baldwinw -M $VMCORE -N $KERNEL -dn
248a37f9716SJohn Baldwinecho
249a37f9716SJohn Baldwinfi
250a37f9716SJohn Baldwin
251a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
252a37f9716SJohn Baldwinecho "nfsstat"
253a37f9716SJohn Baldwinecho
254a37f9716SJohn Baldwinnfsstat -M $VMCORE -N $KERNEL
255a37f9716SJohn Baldwinecho
256a37f9716SJohn Baldwin
257a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
258a37f9716SJohn Baldwinecho "netstat -s"
259a37f9716SJohn Baldwinecho
260a37f9716SJohn Baldwinnetstat -M $VMCORE -N $KERNEL -s
261a37f9716SJohn Baldwinecho
262a37f9716SJohn Baldwin
263a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
264a37f9716SJohn Baldwinecho "netstat -m"
265a37f9716SJohn Baldwinecho
266a37f9716SJohn Baldwinnetstat -M $VMCORE -N $KERNEL -m
267a37f9716SJohn Baldwinecho
268a37f9716SJohn Baldwin
269a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
270a37f9716SJohn Baldwinecho "netstat -id"
271a37f9716SJohn Baldwinecho
272a37f9716SJohn Baldwinnetstat -M $VMCORE -N $KERNEL -id
273a37f9716SJohn Baldwinecho
274a37f9716SJohn Baldwin
275a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
276a37f9716SJohn Baldwinecho "netstat -anr"
277a37f9716SJohn Baldwinecho
278a37f9716SJohn Baldwinnetstat -M $VMCORE -N $KERNEL -anr
279a37f9716SJohn Baldwinecho
280a37f9716SJohn Baldwin
281a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
282a37f9716SJohn Baldwinecho "netstat -anA"
283a37f9716SJohn Baldwinecho
284a37f9716SJohn Baldwinnetstat -M $VMCORE -N $KERNEL -anA
285a37f9716SJohn Baldwinecho
286a37f9716SJohn Baldwin
287a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
288a37f9716SJohn Baldwinecho "netstat -aL"
289a37f9716SJohn Baldwinecho
290a37f9716SJohn Baldwinnetstat -M $VMCORE -N $KERNEL -aL
291a37f9716SJohn Baldwinecho
292a37f9716SJohn Baldwin
293a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
294a37f9716SJohn Baldwinecho "fstat"
295a37f9716SJohn Baldwinecho
296a37f9716SJohn Baldwinfstat -M $VMCORE -N $KERNEL
297a37f9716SJohn Baldwinecho
298a37f9716SJohn Baldwin
299a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
300a37f9716SJohn Baldwinecho "dmesg"
301a37f9716SJohn Baldwinecho
302a37f9716SJohn Baldwindmesg -a -M $VMCORE -N $KERNEL
303a37f9716SJohn Baldwinecho
304a37f9716SJohn Baldwin
305a37f9716SJohn Baldwinecho "------------------------------------------------------------------------"
306a37f9716SJohn Baldwinecho "kernel config"
307a37f9716SJohn Baldwinecho
308a37f9716SJohn Baldwinconfig -x $KERNEL
309c5229b9dSJohn Baldwin
310c5229b9dSJohn Baldwinecho
311c5229b9dSJohn Baldwinecho "------------------------------------------------------------------------"
312c5229b9dSJohn Baldwinecho "ddb capture buffer"
313c5229b9dSJohn Baldwinecho
314c5229b9dSJohn Baldwin
315c5229b9dSJohn Baldwinddb capture -M $VMCORE -N $KERNEL print
316