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.]+)\)/;
142 my $odmg = "$oarchive/$vers/macos-10.${major}/OpenAFS-$vers-\*.dmg";
143 if ($option_verbose) {
144 print "$hdutil $odmg extractall OpenAFS.pkg $tempdir/OpenAFS.pkg\n";
146 `$hdutil $odmg extractall OpenAFS.pkg $tempdir/OpenAFS.pkg`;
147 if ($option_verbose) {
148 print "cd $tempdir && gzcat $tempdir/OpenAFS.pkg/Contents/Archive.pax.gz | pax -r ./Library/OpenAFS/Tools/root.client/usr/vice/etc/afs.kext\n";
150 `cd $tempdir && gzcat $tempdir/OpenAFS.pkg/Contents/Archive.pax.gz | pax -r ./Library/OpenAFS/Tools/root.client/usr/vice/etc/afs.kext`;
151 if ($option_verbose) {
152 print "$hdutil $odmg extractall OpenAFS-debug-extension.pkg $tempdir/OpenAFS-debug-extension.pkg\n";
154 `$hdutil $odmg extractall OpenAFS-debug-extension.pkg $tempdir/OpenAFS-debug-extension.pkg`;
155 if (-f "$tempdir/OpenAFS-debug-extension.pkg/Contents/Archive.pax.gz" ) {
156 if ($option_verbose) {
157 print "cd $tempdir && gzcat $tempdir/OpenAFS-debug-extension.pkg/Contents/Archive.pax.gz | pax -r ./Library/OpenAFS/Debug/afs.kext.dSYM\n";
159 `cd $tempdir && gzcat $tempdir/OpenAFS-debug-extension.pkg/Contents/Archive.pax.gz | pax -r ./Library/OpenAFS/Debug/afs.kext.dSYM`;
160 if ($option_verbose) {
161 print "cd $tempdir && gzcat $tempdir/OpenAFS-debug-extension.pkg/Contents/Archive.pax.gz | pax -r ./Library/OpenAFS/Debug/afs.kext\n";
163 `cd $tempdir && gzcat $tempdir/OpenAFS-debug-extension.pkg/Contents/Archive.pax.gz | pax -r ./Library/OpenAFS/Debug/afs.kext`;
168 my $kversion = shift;
170 my $debugarchive = shift;
173 $kversion =~ /Darwin Kernel Version ([0-9]+).([0-9]+).[0-9]+:/;
177 my $kdk = "$debugarchive/kernel_debug_kit_10.${major}.${minor}_\*.dmg";
178 if ($option_verbose) {
179 print "$hdutil $kdk extractall System.kext $tempdir/System.kext\n";
181 `$hdutil $kdk extractall System.kext $tempdir/System.kext`;
182 if ($option_verbose) {
183 print "$hdutil $kdk extractall mach_kernel.dSYM $tempdir/mach_kernel.dSYM\n";
185 `$hdutil $kdk extractall mach_kernel.dSYM $tempdir/mach_kernel.dSYM`;
186 if ($option_verbose) {
187 print "$hdutil $kdk extract mach_kernel $tempdir/mach_kernel\n";
189 `$hdutil $kdk extract mach_kernel $tempdir/mach_kernel`;
193 # read the panic file and parse out the addresses
196 my $filename = shift;
197 my $hash_ref = shift;
201 my @panic_section_positions = ( 0 );
204 my $panic_fh = IO::File->new( $filename, '<' )
205 or croak "Can't open backtrace file $filename: $OS_ERROR\n";
207 # find the last panic section as denoted by "*********"
208 while ( $line = <$panic_fh> ) {
210 if ( $line eq "*********" ) {
213 push @panic_section_positions, $panic_fh->tell;
217 # ignore the empty last section
218 if ( @panic_section_positions > 2 ) {
219 pop @panic_section_positions
222 # Seek to last full panic section
223 # or the beginning of the file if appropriate
224 $panic_fh->seek( $panic_section_positions[-1], 0 );
226 $hash_ref->{ "date" } = <$panic_fh>;
227 chomp $hash_ref->{ "date" };
229 while ( $line = <$panic_fh> ) {
232 #skip lines until "Backtrace" is seen
233 $line =~ /^\s*(Backtrace,|Backtrace:|Backtrace \()/;
239 if ( $option_quiet ) {
242 croak "Couldn't find a backtrace in $filename\n";
246 # gather the backtrace addresses
247 if ( $backtrace eq "Backtrace:" ) {
249 while ( $line = <$panic_fh> ) {
251 last if $line !~ /^\s*(0x[0-9a-fA-F]+)/;
252 my @memory_addresses = split /\s+/, $line;
254 # add non-empty array elements to the list
255 push @{ $hash_ref->{ "backtrace" } },
256 grep { /0x/ } @memory_addresses;
260 while ( $line = <$panic_fh> ) {
262 last if $line !~ /^\s*0x[0-9a-fA-F]+ : (0x[0-9a-fA-F]*)/;
263 push @{ $hash_ref->{ "backtrace" } }, $1;
267 # now we need the address for the afs kernel module
268 while ( $line = <$panic_fh> ) {
270 next if ( $line !~ /org\.openafs\.filesystems\.afs/ );
272 $kernel_line = $line;
273 $line =~ /\@(0x[0-9a-fA-F]+)/;
274 $hash_ref->{ "afs_kernel_address" } = $1;
275 $kernel_line =~ /^\s+([^@]+)@.+/;
276 $hash_ref->{ "afs_info" } = $1;
281 # grab the kernel version
282 while ( $line = <$panic_fh> ) {
284 next if ( $line !~ /^Darwin Kernel Version/ );
285 $hash_ref->{ "kernel_version" } = $line;
289 or croak "Can't close file $filename: $OS_ERROR\n";
291 if ( !$kernel_line ) {
292 if ( $option_quiet ) {
295 croak "No OpenAFS reference found in latest panic!";
300 # generate the symbol files that will be read by gdb
301 sub generate_symbol_files {
302 my $kernel_address = shift;
303 my $symbol_write_dir = shift;
304 my $kextarch = shift;
308 if ($kextprog eq $kextload) {
309 if ($kernel eq "/mach_kernel") {
310 if ($option_verbose) {
311 print "$kextprog -k $kernel -s $temp_dir -a org.openafs.filesystems.afs\@${kernel_address} -n $kext\n";
316 "-a", 'org.openafs.filesystems.afs@' . $kernel_address,
319 if ($option_verbose) {
320 print "$kextprog -c -e -r $temp_dir -k $kernel -s $temp_dir -a org.openafs.filesystems.afs\@${kernel_address} -n $kext\n";
327 "-a", 'org.openafs.filesystems.afs@' . $kernel_address,
331 if ($kernel eq "/mach_kernel") {
332 if ($option_verbose) {
333 print "$kextprog -k $kernel -s $temp_dir -arch $kextarch -a org.openafs.filesystems.afs\@${kernel_address} -n $kext\n";
339 "-a", 'org.openafs.filesystems.afs@' . $kernel_address,
342 if ($option_verbose) {
343 print "$kextprog -c -e -r $temp_dir -k $kernel -s $temp_dir -arch $kextarch -a org.openafs.filesystems.afs\@${kernel_address} -n $kext\n";
351 "-a", 'org.openafs.filesystems.afs@' . $kernel_address,
355 if ( $CHILD_ERROR ) {
357 croak "kextload failed to run: $OS_ERROR\n";
362 sub write_gdb_input_file {
364 my $write_dir = shift;
365 my $filename = shift;
366 my $backtrace_ref = shift;
368 my @symbol_files = ( $write_dir . "/org.openafs.filesystems.afs.sym" );
370 my $fh = IO::File->new( $write_dir . "/" . $filename, '>' )
371 or croak "Can't open gdb file $filename for writing: $OS_ERROR\n";
373 for my $symbol ( @symbol_files ) {
374 print $fh "add-symbol-file $symbol\n";
377 print $fh "set print asm-demangle on\n";
379 for my $address ( @{ $backtrace_ref } ) {
380 print $fh "x/i $address\n";
384 or croak "Can't close file $filename: $OS_ERROR\n";
387 # write out the pertinent details to a file.
388 sub write_dump_file {
389 my $filename = shift;
390 my $hash_ref = shift;
393 my $log_dir = dirname $filename;
395 if ( ! -d $log_dir ) {
396 mkdir $log_dir, 0755;
397 croak "Can't create directory $log_dir: $OS_ERROR\n" if $CHILD_ERROR;
400 croak "Can't write to folder $log_dir." if ( ! -w $log_dir );
402 my $fh = IO::File->new( $filename, '>', 0664 )
403 or croak "Can't open dump file $filename for writing: $OS_ERROR\n";
405 print $fh "Panic Date: ", $hash_ref->{ "date" }, "\n";
406 print $fh "Kernel Version: ", $hash_ref->{ "kernel_version" }, "\n";
407 print $fh "OpenAFS Version: ", $hash_ref->{ "afs_info" }, "\n";
408 print $fh "=============\n";
412 or croak "Can't close file $filename: $OS_ERROR\n";
419 decode-panic - decode a Mac OS panic log to show source line numbers
423 This documentation refers to decode-panic version $Revision$
427 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]
431 -i The path to the panic log that should be read
432 -o The path to where the decoded panic log should be written
433 -k The path to the kernel image corresponding to the panic
434 -d The path to a directory containing kernel debug kit dmgs
435 -a The path to an archive of OpenAFS installer dmgs
436 -u The path to the hdutil dmg utility program
437 -q Quiet mode - don't complain if there is a problem.
438 -v Verbose mode - print all commands.
443 This tool parses the panic log for Mac OS X kernel panics that are caused by
444 openafs in order to produce a human-readable backtrace.
446 This program uses crash isolation procedure as outlined in
447 http://developer.apple.com/technotes/tn2002/tn2063.html#IsolatingCrash
449 Here is an example file that is fed to gdb:
451 add-symbol-file /tmp/afsdebugt8dGOb/org.openafs.filesystems.afs.sym
452 set print asm-demangle on
456 Panic logs can be found in /Library/Logs/panic.log in 10.4 (Tiger),
457 /Library/Logs/PanicReporter/YYYY-MM-DD-HHMMSS.panic in 10.5 (Leopard),
458 and /Library/Logs/DiagnosticReports/Kernel_YYYY-MM-DD-HHMMSS.panic in 10.6
463 This program needs gdb and kextload; Starting in SnowLeopard, it needs kextutil.
465 Batch decoding requires a directory of Kernel Debug Kit DMGs, a directory of
466 OpenAFS installer DMGs, and the DMG extraction utility currently available
467 in source form at http://www.dementia.org/~shadow/dmgutil-0.1.tar.gz
469 =head1 BUGS AND LIMITATIONS
471 decode-panic clobbers the output file.
475 Copyright 2008-2010. Jason Edgecombe <jason@rampaginggeek.com> and others.
477 This documentation is covered by the BSD License as written in the
478 doc/LICENSE file in the OpenAFS source tree. This program was originally
479 written by Jason Edgecombe for OpenAFS.