1181335Sjhb#!/bin/sh
2181335Sjhb#
3181335Sjhb# Copyright (c) 2008 Yahoo!, Inc.
4181335Sjhb# All rights reserved.
5181335Sjhb#
6181335Sjhb# Redistribution and use in source and binary forms, with or without
7181335Sjhb# modification, are permitted provided that the following conditions
8181335Sjhb# are met:
9181335Sjhb# 1. Redistributions of source code must retain the above copyright
10181335Sjhb#    notice, this list of conditions and the following disclaimer.
11181335Sjhb# 2. Redistributions in binary form must reproduce the above copyright
12181335Sjhb#    notice, this list of conditions and the following disclaimer in the
13181335Sjhb#    documentation and/or other materials provided with the distribution.
14181335Sjhb# 3. Neither the name of the author nor the names of any co-contributors
15181335Sjhb#    may be used to endorse or promote products derived from this software
16181335Sjhb#    without specific prior written permission.
17181335Sjhb#
18181335Sjhb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19181335Sjhb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20181335Sjhb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21181335Sjhb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22181335Sjhb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23181335Sjhb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24181335Sjhb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25181335Sjhb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26181335Sjhb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27181335Sjhb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28181335Sjhb# SUCH DAMAGE.
29181335Sjhb#
30181335Sjhb# $FreeBSD$
31181335Sjhb
32181335Sjhbusage()
33181335Sjhb{
34181335Sjhb	echo "usage: crashinfo [-d crashdir] [-n dumpnr] [-k kernel] [core]"
35181335Sjhb	exit 1
36181335Sjhb}
37181335Sjhb
38181335Sjhbfind_kernel()
39181335Sjhb{
40181335Sjhb	local ivers k kvers
41181335Sjhb
42181335Sjhb	ivers=$(awk '
43181335Sjhb	/Version String/ {
44181335Sjhb		print
45181335Sjhb		nextline=1
46181335Sjhb		next
47181335Sjhb	}
48232666Semaste	nextline==1 {
49232666Semaste		if ($0 ~ "^  [A-Za-z ]+: ") {
50232666Semaste			nextline=0
51232666Semaste		} else {
52181335Sjhb			print
53181335Sjhb		}
54181335Sjhb	}' $INFO)
55181335Sjhb
56181335Sjhb	# Look for a matching kernel version.
57216077Scperciva	for k in `sysctl -n kern.bootfile` $(ls -t /boot/*/kernel); do
58181335Sjhb		kvers=$(echo 'printf "  Version String: %s", version' | \
59181335Sjhb		    gdb -x /dev/stdin -batch $k 2>/dev/null)
60181335Sjhb		if [ "$ivers" = "$kvers" ]; then
61181335Sjhb			KERNEL=$k
62181335Sjhb			break
63181335Sjhb		fi
64181335Sjhb	done
65181335Sjhb}
66181335Sjhb
67181335SjhbCRASHDIR=/var/crash
68181335SjhbDUMPNR=
69181335SjhbKERNEL=
70181335Sjhb
71181335Sjhbwhile getopts "d:n:k:" opt; do
72181335Sjhb	case "$opt" in
73181335Sjhb	d)
74181335Sjhb		CRASHDIR=$OPTARG
75181335Sjhb		;;
76181335Sjhb	n)
77181335Sjhb		DUMPNR=$OPTARG
78181335Sjhb		;;
79181335Sjhb	k)
80181335Sjhb		KERNEL=$OPTARG
81181335Sjhb		;;
82181335Sjhb	\?)
83181335Sjhb		usage
84181335Sjhb		;;
85181335Sjhb	esac
86181335Sjhbdone
87181335Sjhb
88181335Sjhbshift $((OPTIND - 1))
89181335Sjhb
90181335Sjhbif [ $# -eq 1 ]; then
91181335Sjhb	if [ -n "$DUMPNR" ]; then
92181335Sjhb		echo "-n and an explicit vmcore are mutually exclusive"
93181335Sjhb		usage
94181335Sjhb	fi
95181335Sjhb
96181335Sjhb	# Figure out the crash directory and number from the vmcore name.
97181335Sjhb	CRASHDIR=`dirname $1`
98181335Sjhb	DUMPNR=$(expr $(basename $1) : 'vmcore\.\([0-9]*\)$')
99181335Sjhb	if [ -z "$DUMPNR" ]; then
100181335Sjhb		echo "Unable to determine dump number from vmcore file $1."
101181335Sjhb		exit 1
102181335Sjhb	fi
103181335Sjhbelif [ $# -gt 1 ]; then
104181335Sjhb	usage
105181335Sjhbelse
106181335Sjhb	# If we don't have an explicit dump number, operate on the most
107181335Sjhb	# recent dump.
108181335Sjhb	if [ -z "$DUMPNR" ]; then
109181335Sjhb		if ! [ -r $CRASHDIR/bounds ]; then
110181335Sjhb			echo "No crash dumps in $CRASHDIR."
111181335Sjhb			exit 1
112181335Sjhb		fi			
113181335Sjhb		next=`cat $CRASHDIR/bounds`
114181335Sjhb		if [ -z "$next" ] || [ "$next" -eq 0 ]; then
115181335Sjhb			echo "No crash dumps in $CRASHDIR."
116181335Sjhb			exit 1
117181335Sjhb		fi
118181335Sjhb		DUMPNR=$(($next - 1))
119181335Sjhb	fi
120181335Sjhbfi
121181335Sjhb
122181335SjhbVMCORE=$CRASHDIR/vmcore.$DUMPNR
123181335SjhbINFO=$CRASHDIR/info.$DUMPNR
124181335SjhbFILE=$CRASHDIR/core.txt.$DUMPNR
125181335SjhbHOSTNAME=`hostname`
126181335Sjhb
127181335Sjhbif [ ! -e $VMCORE ]; then
128181335Sjhb	echo "$VMCORE not found"
129181335Sjhb	exit 1
130181335Sjhbfi
131181335Sjhb
132181335Sjhbif [ ! -e $INFO ]; then
133181335Sjhb	echo "$INFO not found"
134181335Sjhb	exit 1
135181335Sjhbfi
136181335Sjhb
137181335Sjhb# If the user didn't specify a kernel, then try to find one.
138181335Sjhbif [ -z "$KERNEL" ]; then
139181335Sjhb	find_kernel
140181335Sjhb	if [ -z "$KERNEL" ]; then
141181335Sjhb		echo "Unable to find matching kernel for $VMCORE"
142181335Sjhb		exit 1
143181335Sjhb	fi
144181335Sjhbelif [ ! -e $KERNEL ]; then
145181335Sjhb	echo "$KERNEL not found"
146181335Sjhb	exit 1
147181335Sjhbfi
148181335Sjhb
149181335Sjhbecho "Writing crash summary to $FILE."
150181335Sjhb
151198846Sdelphijumask 077
152198846Sdelphij
153181335Sjhb# Simulate uname
154181335Sjhbostype=$(echo -e printf '"%s", ostype' | gdb -x /dev/stdin -batch $KERNEL)
155181335Sjhbosrelease=$(echo -e printf '"%s", osrelease' | gdb -x /dev/stdin -batch $KERNEL)
156181335Sjhbversion=$(echo -e printf '"%s", version' | gdb -x /dev/stdin -batch $KERNEL | \
157181335Sjhb    tr '\t\n' '  ')
158181335Sjhbmachine=$(echo -e printf '"%s", machine' | gdb -x /dev/stdin -batch $KERNEL)
159181335Sjhb
160181335Sjhbexec > $FILE 2>&1
161181335Sjhb
162181335Sjhbecho "$HOSTNAME dumped core - see $VMCORE"
163181335Sjhbecho
164181335Sjhbdate
165181335Sjhbecho
166181335Sjhbecho "$ostype $HOSTNAME $osrelease $version $machine"
167181335Sjhbecho
168181335Sjhbsed -ne '/^  Panic String: /{s//panic: /;p;}' $INFO
169181335Sjhbecho
170181335Sjhb
171181335Sjhb# XXX: /bin/sh on 7.0+ is broken so we can't simply pipe the commands to
172181335Sjhb# kgdb via stdin and have to use a temporary file instead.
173181335Sjhbfile=`mktemp /tmp/crashinfo.XXXXXX`
174181335Sjhbif [ $? -eq 0 ]; then
175181335Sjhb	echo "bt" >> $file
176181335Sjhb	echo "quit" >> $file
177181335Sjhb	kgdb $KERNEL $VMCORE < $file
178181335Sjhb	rm -f $file
179181335Sjhb	echo
180181335Sjhbfi
181181335Sjhbecho
182181335Sjhb
183181335Sjhbecho "------------------------------------------------------------------------"
184181335Sjhbecho "ps -axl"
185181335Sjhbecho
186181335Sjhbps -M $VMCORE -N $KERNEL -axl
187181335Sjhbecho
188181335Sjhb
189181335Sjhbecho "------------------------------------------------------------------------"
190181335Sjhbecho "vmstat -s"
191181335Sjhbecho
192181335Sjhbvmstat -M $VMCORE -N $KERNEL -s
193181335Sjhbecho
194181335Sjhb
195181335Sjhbecho "------------------------------------------------------------------------"
196181335Sjhbecho "vmstat -m"
197181335Sjhbecho
198181335Sjhbvmstat -M $VMCORE -N $KERNEL -m
199181335Sjhbecho
200181335Sjhb
201181335Sjhbecho "------------------------------------------------------------------------"
202181335Sjhbecho "vmstat -z"
203181335Sjhbecho
204181335Sjhbvmstat -M $VMCORE -N $KERNEL -z
205181335Sjhbecho
206181335Sjhb
207181335Sjhbecho "------------------------------------------------------------------------"
208181335Sjhbecho "vmstat -i"
209181335Sjhbecho
210181335Sjhbvmstat -M $VMCORE -N $KERNEL -i
211181335Sjhbecho
212181335Sjhb
213181335Sjhbecho "------------------------------------------------------------------------"
214181335Sjhbecho "pstat -T"
215181335Sjhbecho
216181335Sjhbpstat -M $VMCORE -N $KERNEL -T
217181335Sjhbecho
218181335Sjhb
219181335Sjhbecho "------------------------------------------------------------------------"
220181335Sjhbecho "pstat -s"
221181335Sjhbecho
222181335Sjhbpstat -M $VMCORE -N $KERNEL -s
223181335Sjhbecho
224181335Sjhb
225181335Sjhbecho "------------------------------------------------------------------------"
226181335Sjhbecho "iostat"
227181335Sjhbecho
228181335Sjhbiostat -M $VMCORE -N $KERNEL
229181335Sjhbecho
230181335Sjhb
231181335Sjhbecho "------------------------------------------------------------------------"
232181335Sjhbecho "ipcs -a"
233181335Sjhbecho
234181335Sjhbipcs -C $VMCORE -N $KERNEL -a
235181335Sjhbecho
236181335Sjhb
237181335Sjhbecho "------------------------------------------------------------------------"
238181335Sjhbecho "ipcs -T"
239181335Sjhbecho
240181335Sjhbipcs -C $VMCORE -N $KERNEL -T
241181335Sjhbecho
242181335Sjhb
243181335Sjhb# XXX: This doesn't actually work in 5.x+
244181335Sjhbif false; then
245181335Sjhbecho "------------------------------------------------------------------------"
246181335Sjhbecho "w -dn"
247181335Sjhbecho
248181335Sjhbw -M $VMCORE -N $KERNEL -dn
249181335Sjhbecho
250181335Sjhbfi
251181335Sjhb
252181335Sjhbecho "------------------------------------------------------------------------"
253181335Sjhbecho "nfsstat"
254181335Sjhbecho
255181335Sjhbnfsstat -M $VMCORE -N $KERNEL
256181335Sjhbecho
257181335Sjhb
258181335Sjhbecho "------------------------------------------------------------------------"
259181335Sjhbecho "netstat -s"
260181335Sjhbecho
261181335Sjhbnetstat -M $VMCORE -N $KERNEL -s
262181335Sjhbecho
263181335Sjhb
264181335Sjhbecho "------------------------------------------------------------------------"
265181335Sjhbecho "netstat -m"
266181335Sjhbecho
267181335Sjhbnetstat -M $VMCORE -N $KERNEL -m
268181335Sjhbecho
269181335Sjhb
270181335Sjhbecho "------------------------------------------------------------------------"
271254959Sgavinecho "netstat -idW"
272181335Sjhbecho
273254959Sgavinnetstat -M $VMCORE -N $KERNEL -idW
274181335Sjhbecho
275181335Sjhb
276181335Sjhbecho "------------------------------------------------------------------------"
277181335Sjhbecho "netstat -anr"
278181335Sjhbecho
279181335Sjhbnetstat -M $VMCORE -N $KERNEL -anr
280181335Sjhbecho
281181335Sjhb
282181335Sjhbecho "------------------------------------------------------------------------"
283181335Sjhbecho "netstat -anA"
284181335Sjhbecho
285181335Sjhbnetstat -M $VMCORE -N $KERNEL -anA
286181335Sjhbecho
287181335Sjhb
288181335Sjhbecho "------------------------------------------------------------------------"
289181335Sjhbecho "netstat -aL"
290181335Sjhbecho
291181335Sjhbnetstat -M $VMCORE -N $KERNEL -aL
292181335Sjhbecho
293181335Sjhb
294181335Sjhbecho "------------------------------------------------------------------------"
295181335Sjhbecho "fstat"
296181335Sjhbecho
297181335Sjhbfstat -M $VMCORE -N $KERNEL
298181335Sjhbecho
299181335Sjhb
300181335Sjhbecho "------------------------------------------------------------------------"
301181335Sjhbecho "dmesg"
302181335Sjhbecho
303181335Sjhbdmesg -a -M $VMCORE -N $KERNEL
304181335Sjhbecho
305181335Sjhb
306181335Sjhbecho "------------------------------------------------------------------------"
307181335Sjhbecho "kernel config"
308181335Sjhbecho
309181335Sjhbconfig -x $KERNEL
310198586Sjhb
311198586Sjhbecho
312198586Sjhbecho "------------------------------------------------------------------------"
313198586Sjhbecho "ddb capture buffer"
314198586Sjhbecho
315198586Sjhb
316198586Sjhbddb capture -M $VMCORE -N $KERNEL print
317