3 # decode-panic - decode a Mac OS panic log to show source line numbers
4 # see the end of the file for full documentation and license.
7 use English qw( -no_match_vars ) ;
9 use File::Temp qw( tempdir );
17 my $panic_file = "/Library/Logs/panic.log";
23 my $kextload = "/sbin/kextload";
24 my $kextutil = "/usr/bin/kextutil";
26 my $gdb = "/usr/bin/gdb";
29 my $gdb_file = "gdb.input";
30 my $temp_dir = tempdir( "afsdebugXXXXXX", DIR => File::Spec->tmpdir,
31 TMPDIR => 1, CLEANUP => 1 );
32 my $dump_file = "/var/db/openafs/logs/crash.dump";
33 my $kernel = "/mach_kernel";
34 my $kextpath = "/Library/OpenAFS/Tools/root.client/usr/vice/etc/afs.kext/";
39 my $result = GetOptions ("input=s" => \$panic_file,
40 "output=s" => \$dump_file,
41 "kernel=s" => \$kernel,
42 "debugkit=s" => \$debugkit,
43 "archive=s"=> \$archive,
44 "util=s" => \$dmgutil,
45 "verbose" => \$option_verbose,
46 "quiet" => \$option_quiet,
47 "help" => \$option_help
51 pod2usage(-message => "Syntax error.",
60 pod2usage(-message => "",
67 # check for necessary programs & panic file
68 for my $program ( $gdb, $kextload ) {
69 if ( ! -x $program ) {
70 if ( $option_quiet ) {
73 croak "Can't find $program!\n"
79 $kextprog = $kextutil;
81 $kextprog = $kextload;
84 croak "Can't find panic file: $panic_file!\n" if ( ! -r $panic_file );
86 read_panic( $panic_file, \%crash_info );
88 if ($crash_info{"kernel_version"} =~ /X86_64/ ) {
92 if ($crash_info{"kernel_version"} =~ /I386/ ) {
96 if ($crash_info{"kernel_version"} =~ /PPC/ ) {
103 if (-d $debugkit && -f $dmgutil ) {
104 extract_kernel( $crash_info{"kernel_version"}, $temp_dir, $debugkit, $dmgutil );
105 $kernel = "$temp_dir/mach_kernel";
108 if (-d $archive && -f $dmgutil ) {
109 extract_openafs( $crash_info{"afs_info"}, $temp_dir, $archive, $crash_info{"kernel_version"}, $dmgutil );
110 if (-d "$temp_dir/Library/OpenAFS/Debug/afs.kext" ) {
111 $kextpath = "$temp_dir/Library/OpenAFS/Debug/afs.kext";
113 $kextpath = "$temp_dir/Library/OpenAFS/Tools/root.client/usr/vice/etc/afs.kext";
117 generate_symbol_files( $crash_info{"afs_kernel_address"}, $temp_dir, $kextarch , $kernel, $kextpath);
119 write_gdb_input_file( $temp_dir, $gdb_file, $crash_info{ "backtrace" } );
121 if ($option_verbose) {
122 print "$gdb $gdbarch $kernel -batch -x $temp_dir/$gdb_file\n";
124 my $gdb_output = `$gdb $gdbarch $kernel -batch -x $temp_dir/$gdb_file`;
125 croak "gdb failed!\n" if $CHILD_ERROR;
127 write_dump_file( $dump_file, \%crash_info, $gdb_output );
129 sub extract_openafs {
130 my $oversion = shift;
132 my $oarchive = shift;
133 my $kversion = shift;
136 $kversion =~ /Darwin Kernel Version ([0-9]+).[0-9]+.[0-9]+:/;
140 $oversion =~ /org.openafs.filesystems.afs\(([0-9.]+)\)/;
144 if ($vers =~ /([0-9]+)f([0-9]+)/) {
145 $dmgvers = "$1" . "." . "$2";
149 my $odmg = "$oarchive/$vers/macos-10.${major}/OpenAFS-$vers-\*.dmg";
150 if ($option_verbose) {
151 print "$hdutil $odmg extractall OpenAFS.pkg $tempdir/OpenAFS.pkg\n";
153 `$hdutil $odmg extractall OpenAFS.pkg $tempdir/OpenAFS.pkg`;
154 if ($option_verbose) {
155 print "cd $tempdir && gzcat $tempdir/OpenAFS.pkg/Contents/Archive.pax.gz | pax -r ./Library/OpenAFS/Tools/root.client/usr/vice/etc/afs.kext\n";
157 `cd $tempdir && gzcat $tempdir/OpenAFS.pkg/Contents/Archive.pax.gz | pax -r ./Library/OpenAFS/Tools/root.client/usr/vice/etc/afs.kext`;
158 if ($option_verbose) {
159 print "$hdutil $odmg extractall OpenAFS-debug-extension.pkg $tempdir/OpenAFS-debug-extension.pkg\n";
161 `$hdutil $odmg extractall OpenAFS-debug-extension.pkg $tempdir/OpenAFS-debug-extension.pkg`;
162 if (-f "$tempdir/OpenAFS-debug-extension.pkg/Contents/Archive.pax.gz" ) {
163 if ($option_verbose) {
164 print "cd $tempdir && gzcat $tempdir/OpenAFS-debug-extension.pkg/Contents/Archive.pax.gz | pax -r ./Library/OpenAFS/Debug/afs.kext.dSYM\n";
166 `cd $tempdir && gzcat $tempdir/OpenAFS-debug-extension.pkg/Contents/Archive.pax.gz | pax -r ./Library/OpenAFS/Debug/afs.kext.dSYM`;
167 if ($option_verbose) {
168 print "cd $tempdir && gzcat $tempdir/OpenAFS-debug-extension.pkg/Contents/Archive.pax.gz | pax -r ./Library/OpenAFS/Debug/afs.kext\n";
170 `cd $tempdir && gzcat $tempdir/OpenAFS-debug-extension.pkg/Contents/Archive.pax.gz | pax -r ./Library/OpenAFS/Debug/afs.kext`;
175 my $kversion = shift;
177 my $debugarchive = shift;
180 $kversion =~ /Darwin Kernel Version ([0-9]+).([0-9]+).[0-9]+:/;
184 my $kdk = "$debugarchive/kernel_debug_kit_10.${major}.${minor}_\*.dmg";
185 if ($option_verbose) {
186 print "$hdutil $kdk extractall System.kext $tempdir/System.kext\n";
188 `$hdutil $kdk extractall System.kext $tempdir/System.kext`;
189 if ($option_verbose) {
190 print "$hdutil $kdk extractall mach_kernel.dSYM $tempdir/mach_kernel.dSYM\n";
192 `$hdutil $kdk extractall mach_kernel.dSYM $tempdir/mach_kernel.dSYM`;
193 if ($option_verbose) {
194 print "$hdutil $kdk extract mach_kernel $tempdir/mach_kernel\n";
196 `$hdutil $kdk extract mach_kernel $tempdir/mach_kernel`;
200 # read the panic file and parse out the addresses
203 my $filename = shift;
204 my $hash_ref = shift;
208 my @panic_section_positions = ( 0 );
211 my $panic_fh = IO::File->new( $filename, '<' )
212 or croak "Can't open backtrace file $filename: $OS_ERROR\n";
214 # find the last panic section as denoted by "*********"
215 while ( $line = <$panic_fh> ) {
217 if ( $line eq "*********" ) {
220 push @panic_section_positions, $panic_fh->tell;
224 # ignore the empty last section
225 if ( @panic_section_positions > 2 ) {
226 pop @panic_section_positions
229 # Seek to last full panic section
230 # or the beginning of the file if appropriate
231 $panic_fh->seek( $panic_section_positions[-1], 0 );
233 $hash_ref->{ "date" } = <$panic_fh>;
234 chomp $hash_ref->{ "date" };
236 while ( $line = <$panic_fh> ) {
239 #skip lines until "Backtrace" is seen
240 $line =~ /^\s*(Backtrace,|Backtrace:|Backtrace \()/;
246 if ( $option_quiet ) {
249 croak "Couldn't find a backtrace in $filename\n";
253 # gather the backtrace addresses
254 if ( $backtrace eq "Backtrace:" ) {
256 while ( $line = <$panic_fh> ) {
258 last if $line !~ /^\s*(0x[0-9a-fA-F]+)/;
259 my @memory_addresses = split /\s+/, $line;
261 # add non-empty array elements to the list
262 push @{ $hash_ref->{ "backtrace" } },
263 grep { /0x/ } @memory_addresses;
267 while ( $line = <$panic_fh> ) {
269 last if $line !~ /^\s*0x[0-9a-fA-F]+ : (0x[0-9a-fA-F]*)/;
270 push @{ $hash_ref->{ "backtrace" } }, $1;
274 # now we need the address for the afs kernel module
275 while ( $line = <$panic_fh> ) {
277 next if ( $line !~ /org\.openafs\.filesystems\.afs/ );
279 $kernel_line = $line;
280 $line =~ /\@(0x[0-9a-fA-F]+)/;
281 $hash_ref->{ "afs_kernel_address" } = $1;
282 $kernel_line =~ /^\s+([^@]+)@.+/;
283 $hash_ref->{ "afs_info" } = $1;
288 # grab the kernel version
289 while ( $line = <$panic_fh> ) {
291 next if ( $line !~ /^Darwin Kernel Version/ );
292 $hash_ref->{ "kernel_version" } = $line;
296 or croak "Can't close file $filename: $OS_ERROR\n";
298 if ( !$kernel_line ) {
299 if ( $option_quiet ) {
302 croak "No OpenAFS reference found in latest panic!";
307 # generate the symbol files that will be read by gdb
308 sub generate_symbol_files {
309 my $kernel_address = shift;
310 my $symbol_write_dir = shift;
311 my $kextarch = shift;
315 if ($kextprog eq $kextload) {
316 if ($kernel eq "/mach_kernel") {
317 if ($option_verbose) {
318 print "$kextprog -k $kernel -s $temp_dir -a org.openafs.filesystems.afs\@${kernel_address} -n $kext\n";
323 "-a", 'org.openafs.filesystems.afs@' . $kernel_address,
326 if ($option_verbose) {
327 print "$kextprog -c -e -r $temp_dir -k $kernel -s $temp_dir -a org.openafs.filesystems.afs\@${kernel_address} -n $kext\n";
334 "-a", 'org.openafs.filesystems.afs@' . $kernel_address,
338 if ($kernel eq "/mach_kernel") {
339 if ($option_verbose) {
340 print "$kextprog -k $kernel -s $temp_dir -arch $kextarch -a org.openafs.filesystems.afs\@${kernel_address} -n $kext\n";
346 "-a", 'org.openafs.filesystems.afs@' . $kernel_address,
349 if ($option_verbose) {
350 print "$kextprog -c -e -r $temp_dir -k $kernel -s $temp_dir -arch $kextarch -a org.openafs.filesystems.afs\@${kernel_address} -n $kext\n";
358 "-a", 'org.openafs.filesystems.afs@' . $kernel_address,
362 if ( $CHILD_ERROR ) {
364 croak "kextload failed to run: $OS_ERROR\n";
369 sub write_gdb_input_file {
371 my $write_dir = shift;
372 my $filename = shift;
373 my $backtrace_ref = shift;
375 my @symbol_files = ( $write_dir . "/org.openafs.filesystems.afs.sym" );
377 my $fh = IO::File->new( $write_dir . "/" . $filename, '>' )
378 or croak "Can't open gdb file $filename for writing: $OS_ERROR\n";
380 for my $symbol ( @symbol_files ) {
381 print $fh "add-symbol-file $symbol\n";
384 print $fh "set print asm-demangle on\n";
386 for my $address ( @{ $backtrace_ref } ) {
387 print $fh "x/i $address\n";
391 or croak "Can't close file $filename: $OS_ERROR\n";
394 # write out the pertinent details to a file.
395 sub write_dump_file {
396 my $filename = shift;
397 my $hash_ref = shift;
400 my $log_dir = dirname $filename;
402 if ( ! -d $log_dir ) {
403 mkdir $log_dir, 0755;
404 croak "Can't create directory $log_dir: $OS_ERROR\n" if $CHILD_ERROR;
407 croak "Can't write to folder $log_dir." if ( ! -w $log_dir );
409 my $fh = IO::File->new( $filename, '>', 0664 )
410 or croak "Can't open dump file $filename for writing: $OS_ERROR\n";
412 print $fh "Panic Date: ", $hash_ref->{ "date" }, "\n";
413 print $fh "Kernel Version: ", $hash_ref->{ "kernel_version" }, "\n";
414 print $fh "OpenAFS Version: ", $hash_ref->{ "afs_info" }, "\n";
415 print $fh "=============\n";
419 or croak "Can't close file $filename: $OS_ERROR\n";
426 decode-panic - decode a Mac OS panic log to show source line numbers
430 This documentation refers to decode-panic version $Revision$
434 decode-panic [-i <input panic log>] [-o <output dump file>] [-k <kernel file>] [-d <kernel debug kit archive>] [-a <openafs package archive>] [-u <path to hdutil>] [-q] [-v]
438 -i The path to the panic log that should be read
439 -o The path to where the decoded panic log should be written
440 -k The path to the kernel image corresponding to the panic
441 -d The path to a directory containing kernel debug kit dmgs
442 -a The path to an archive of OpenAFS installer dmgs
443 -u The path to the hdutil dmg utility program
444 -q Quiet mode - don't complain if there is a problem.
445 -v Verbose mode - print all commands.
450 This tool parses the panic log for Mac OS X kernel panics that are caused by
451 openafs in order to produce a human-readable backtrace.
453 This program uses crash isolation procedure as outlined in
454 http://developer.apple.com/technotes/tn2002/tn2063.html#IsolatingCrash
456 Here is an example file that is fed to gdb:
458 add-symbol-file /tmp/afsdebugt8dGOb/org.openafs.filesystems.afs.sym
459 set print asm-demangle on
463 Panic logs can be found in /Library/Logs/panic.log in 10.4 (Tiger),
464 /Library/Logs/PanicReporter/YYYY-MM-DD-HHMMSS.panic in 10.5 (Leopard),
465 and /Library/Logs/DiagnosticReports/Kernel_YYYY-MM-DD-HHMMSS.panic in 10.6
470 This program needs gdb and kextload; Starting in SnowLeopard, it needs kextutil.
472 Batch decoding requires a directory of Kernel Debug Kit DMGs, a directory of
473 OpenAFS installer DMGs, and the DMG extraction utility currently available
474 in source form at http://www.dementia.org/~shadow/dmgutil-0.1.tar.gz
476 =head1 BUGS AND LIMITATIONS
478 decode-panic clobbers the output file.
482 Copyright 2008-2010. Jason Edgecombe <jason@rampaginggeek.com> and others.
484 This documentation is covered by the BSD License as written in the
485 doc/LICENSE file in the OpenAFS source tree. This program was originally
486 written by Jason Edgecombe for OpenAFS.