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";
20 my $kextload = "/sbin/kextload";
21 my $gdb = "/usr/bin/gdb";
22 my $gdb_file = "gdb.input";
23 my $temp_dir = tempdir( "afsdebugXXXXXX", DIR => File::Spec->tmpdir,
24 TMPDIR => 1, CLEANUP => 1 );
25 my $dump_file = "/var/db/openafs/logs/crash.dump";
29 my $result = GetOptions ("input=s" => \$panic_file,
30 "output=s" => \$dump_file,
31 "quiet" => \$option_quiet,
32 "help" => \$option_help
36 pod2usage(-message => "Syntax error.",
45 pod2usage(-message => "",
52 # check for necessary programs & panic file
53 for my $program ( $kextload, $gdb ) {
54 if ( ! -x $program ) {
55 if ( $option_quiet ) {
58 croak "Can't find $program!\n"
63 croak "Can't find panic file: $panic_file!\n" if ( ! -r $panic_file );
65 read_panic( $panic_file, \%crash_info );
67 generate_symbol_files( $crash_info{"afs_kernel_address"}, $temp_dir );
69 write_gdb_input_file( $temp_dir, $gdb_file, $crash_info{ "backtrace" } );
71 my $gdb_output = `$gdb /mach_kernel -batch -x $temp_dir/$gdb_file`;
72 croak "gdb failed!\n" if $CHILD_ERROR;
74 write_dump_file( $dump_file, \%crash_info, $gdb_output );
76 # read the panic file and parse out the addresses
84 my @panic_section_positions = ( 0 );
87 my $panic_fh = IO::File->new( $filename, '<' )
88 or croak "Can't open backtrace file $filename: $OS_ERROR\n";
90 # find the last panic section as denoted by "*********"
91 while ( $line = <$panic_fh> ) {
93 if ( $line eq "*********" ) {
96 push @panic_section_positions, $panic_fh->tell;
100 # ignore the empty last section
101 if ( @panic_section_positions > 2 ) {
102 pop @panic_section_positions
105 # Seek to last full panic section
106 # or the beginning of the file if appropriate
107 $panic_fh->seek( $panic_section_positions[-1], 0 );
109 $hash_ref->{ "date" } = <$panic_fh>;
110 chomp $hash_ref->{ "date" };
112 while ( $line = <$panic_fh> ) {
115 #skip lines until "Backtrace" is seen
116 $line =~ /^\s*(Backtrace,|Backtrace:|Backtrace \()/;
122 if ( $option_quiet ) {
125 croak "Couldn't find a backtrace in $filename\n";
129 # gather the backtrace addresses
130 if ( $backtrace eq "Backtrace:" ) {
132 while ( $line = <$panic_fh> ) {
134 last if $line !~ /^\s*(0x[0-9a-fA-F]{8})/;
135 my @memory_addresses = split /\s+/, $line;
137 # add non-empty array elements to the list
138 push @{ $hash_ref->{ "backtrace" } },
139 grep { /0x/ } @memory_addresses;
143 while ( $line = <$panic_fh> ) {
145 last if $line !~ /^\s*0x[0-9a-fA-F]{8} : (0x[0-9a-fA-F]*)/;
146 push @{ $hash_ref->{ "backtrace" } }, $1;
150 # now we need the address for the afs kernel module
151 while ( $line = <$panic_fh> ) {
153 next if ( $line !~ /org\.openafs\.filesystems\.afs/ );
155 $kernel_line = $line;
156 $line =~ /\@(0x[0-9a-fA-F]+)/;
157 $hash_ref->{ "afs_kernel_address" } = $1;
158 $kernel_line =~ /^\s+([^@]+)@.+/;
159 $hash_ref->{ "afs_info" } = $1;
164 # grab the kernel version
165 while ( $line = <$panic_fh> ) {
167 next if ( $line !~ /^Darwin Kernel Version/ );
168 $hash_ref->{ "kernel_version" } = $line;
172 or croak "Can't close file $filename: $OS_ERROR\n";
174 if ( !$kernel_line ) {
175 if ( $option_quiet ) {
178 croak "No OpenAFS reference found in latest panic!";
183 # generate the symbol files that will be read by gdb
184 sub generate_symbol_files {
185 my $kernel_address = shift;
186 my $symbol_write_dir = shift;
188 system( "/sbin/kextload",
190 "-a", 'org.openafs.filesystems.afs@' . $kernel_address,
191 "-n", "/Library/OpenAFS/Tools/root.client/usr/vice/etc/afs.kext/" );
192 if ( $CHILD_ERROR ) {
194 croak "kextload failed to run: $OS_ERROR\n";
199 sub write_gdb_input_file {
201 my $write_dir = shift;
202 my $filename = shift;
203 my $backtrace_ref = shift;
205 my @symbol_files = ( $write_dir . "/org.openafs.filesystems.afs.sym" );
207 my $fh = IO::File->new( $write_dir . "/" . $filename, '>' )
208 or croak "Can't open gdb file $filename for writing: $OS_ERROR\n";
210 for my $symbol ( @symbol_files ) {
211 print $fh "add-symbol-file $symbol\n";
214 print $fh "set print asm-demangle on\n";
216 for my $address ( @{ $backtrace_ref } ) {
217 print $fh "x/i $address\n";
221 or croak "Can't close file $filename: $OS_ERROR\n";
224 # write out the pertinent details to a file.
225 sub write_dump_file {
226 my $filename = shift;
227 my $hash_ref = shift;
230 my $log_dir = dirname $filename;
232 if ( ! -d $log_dir ) {
233 mkdir $log_dir, 0755;
234 croak "Can't create directory $log_dir: $OS_ERROR\n" if $CHILD_ERROR;
237 croak "Can't write to folder $log_dir." if ( ! -w $log_dir );
239 my $fh = IO::File->new( $filename, '>', 0664 )
240 or croak "Can't open dump file $filename for writing: $OS_ERROR\n";
242 print $fh "Panic Date: ", $hash_ref->{ "date" }, "\n";
243 print $fh "Kernel Version: ", $hash_ref->{ "kernel_version" }, "\n";
244 print $fh "OpenAFS Version: ", $hash_ref->{ "afs_info" }, "\n";
245 print $fh "=============\n";
249 or croak "Can't close file $filename: $OS_ERROR\n";
256 decode-panic - decode a Mac OS panic log to show source line numbers
260 This documentation refers to decode-panic version $Revision$
264 decode-panic [-i <input panic log>] [-o <output dump file>] [-q]
268 -i The path to the panic log that should be read
269 -o The path to where the decoded panic log should be written
270 -q Quiet mode - don't complain if there is a problem.
275 It parses the panic log for Mac OS X kernel panics that are caused by
276 openafs in order to produce a human-readable backtrace.
278 This program uses crash isolation procedure as outlined in
279 http://developer.apple.com/technotes/tn2002/tn2063.html#IsolatingCrash
281 Here is an example file that is fed to gdb:
283 add-symbol-file /tmp/afsdebugt8dGOb/org.openafs.filesystems.afs.sym
284 set print asm-demangle on
290 This program needs gdb and kextload.
292 =head1 BUGS AND LIMITATIONS
294 It clobbers the output file.
298 Copyright 2008. Jason Edgecombe <jason@rampaginggeek.com>
300 This documentation is covered by the BSD License as written in the
301 doc/LICENSE file in the OpenAFS source tree. This program was written by
302 Jason Edgecombe for OpenAFS.