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