First public contribution.
2 # Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
4 # This component and the accompanying materials are made available
5 # under the terms of the License "Eclipse Public License v1.0"
6 # which accompanies this distribution, and is available
7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 # Initial Contributors:
10 # Nokia Corporation - initial contribution.
19 #........1.........2.........3.........4.........5.........6.........7.....
23 printstk.pl d_exc_nnn [romimage.symbol]
25 Given the output of D_EXC, a file d_exc_nnn.txt and d_exc_nnn.stk, it
26 uses the other information to try to put symbolic information against
35 my ($base, $max, $name) = @_;
36 $address{$base} = [ $base, $max, $name ];
39 while ($key <= $maxkey) # allowing for objects that span the boundary
41 push @{$addresslist{$key}}, $base;
46 my $RomBase = 0xF8000000;
47 my $RomLimit = 0xFFF00000;
48 add_object($RomBase,$RomLimit, "ROM");
50 # Handle a MAKSYM.LOG file for a ROM
55 open ROMSYMBOLS, $romimage or print "Can't open $romimage\n" and return;
59 while (my $line = <ROMSYMBOLS>)
61 if(!($line =~ /^[0-9A-Fa-f]{8}/))
65 # 8 bytes for the address
67 $a = substr $line,0,8;
68 if(!($a =~ /[0-9A-Fa-f]{8}/))
72 # 4 bytes for the length
73 $b = substr $line,12,4;
74 if(!($b =~ /[0-9A-Fa-f]{4}/))
78 # rest of line is symbol
79 my $symbol = substr $line,20;
84 if ($base < 0x50000000)
86 next; # skip this line
88 if ($length==0xffffffff)
90 $length=100; # MAKSYM bug? choose a rational length
92 add_object($base, $base+$length-1, $symbol);
94 print "ROM Symbols from $romimage\n";
97 # Handle MAP file for a non execute-in-place binary
101 my ($binary, $binbase)=@_;
102 $binary =~ /([^\\]+)$/;
104 if (not open MAPFILE, "$basename.map")
106 print "Can't open map file for \n$binary.map)\n";
117 # See if we're dealing with the RVCT output
118 if ($maplines[0] =~ /^ARM Linker/)
120 # scroll down to the global symbols
121 while ($_ = shift @maplines)
123 if (/Global Symbols/)
128 # .text gets linked at 0x00008000
129 $imgtext=hex(8000);#start of the text section during linking
133 # name address ignore size section
134 if (/^\s*(.+)\s*(0x\S+)\s+[^\d]*(\d+)\s+(.*)$/)
139 if ($size > 0)#symbols of the 0 size contain some auxillary information, ignore them
141 add_object($addr-$imgtext+$binbase,#relocated address of the current symbol
142 $addr-$imgtext+$binbase+$size,#relocated address of the current symbol + size of the current symbol
149 #we are dealing with GCC output
154 while (($_ = shift @maplines) && !(/^\.text\s+/))
158 /^\.text\s+(\w+)\s+(\w+)/
159 or die "ERROR: Can't get .text section info for \"$file\"\n";
160 $imgtext=hex($1);#start of the text section during linking
165 if (/___CTOR_LIST__/)
167 last; # end of text section
170 if (/^\s(\.text)?\s+(0x\w+)\s+(0x\w+)\s+(.*)$/io)
172 $textlimit = hex($2)+$binbase+hex($3)-1;
176 if (/^\s+(\w+)\s\s+([a-zA-Z_].+)/o)
180 add_object($addr+$binbase,#relocated address of the current symbol
181 $textlimit,#limit of the current object section
187 #end of GCC output parsing
190 # Handle a matched pair of D_EXC output files (.txt and .stk)
197 open D_EXC, "$name.txt" or die "Can't open $name.txt\n";
200 read D_EXC, $data, 16;
203 if ($data =~ /^(..)*.\0.\0/)
208 # Charconv won't convert STDIN or write to STDOUT
209 # so we generate an intermediate UTF8 file
210 system "charconv -little -input=unicode $name.txt -output=utf8 $name.utf8.txt";
212 open D_EXC, "$name.utf8.txt" or die "Can't open $name.utf8.txt\n";
217 open D_EXC, "$name.txt" or die "Can't open $name.txt\n";
222 while (my $line = <D_EXC>)
225 if ($line =~ /^EKA2 USER CRASH LOG$/)
231 # code=1 PC=500f7ff8 FAR=00000042 FSR=e8820013
233 if ($line =~ /^code=\d PC=(.{8})/)
240 # R13svc=81719fc0 R14svc=50031da0 SPSRsvc=60000010
242 if ($line =~ /^R13svc=(.{8}) R14svc=(.{8}) SPSRsvc=(.{8})/)
248 # r00=fffffff8 00000000 80000718 80000003
250 if ($line =~ /^r(\d\d)=(.{8}) (.{8}) (.{8}) (.{8})/)
252 $registers{$1} = $line;
262 # User Stack 03900000-03905ffb
263 # EKA1 format deliberately broken (was /^Stack.*/) to catch version problems
265 if ($line =~ /^User Stack (.{8})-(.{8})/)
267 $stackbase = hex($1);
268 add_object($stackbase,hex($2), "Stack");
272 # fff00000-fff00fff C:\foo\bar.dll
274 if ($line =~ /^(.{8})-(.{8}) (.+)/)
276 next if ($RomBase <= hex($1) && hex($1) < $RomLimit); # skip ROM XIP binaries
277 add_object(hex($1), hex($2), $3);
278 read_map_symbols($3, hex($1));
283 die "$name.txt is not a valid EKA2 crash log" unless $is_eka2_log;
287 die "couldn't find stack information in $name.txt\n";
290 die "couldn't find stack pointer in $name.txt\n" unless $activesp != 0;
291 $activesp -= $stackbase;
293 # Read in the binary dump of the stack
295 open STACK, "$name.stk" or die "Can't open $name.stk\n";
296 print "Stack Data from $name.stk\n";
299 while (read STACK, $data, 4)
301 unshift @stack, (unpack "V", $data);
306 # Handle the captured text output from the Kernel debugger
312 open DEBUGFILE, "$name" or die "Can't open $name\n";
313 print "Kernel Debugger session from $name\n";
315 # stuff which should be inferred from "$name"
317 $stackbase = 0x81C00000;
318 $stackmax = 0x81C01DC0;
319 $activesp = 0x81c01bc4-$stackbase;
320 add_object($stackbase,0x81C01FFF, "Stack");
322 while (my $line = <DEBUGFILE>)
324 if ($line =~ /^(\w{8}): ((\w\w ){16})/)
327 if ($addr < $stackbase || $addr > $stackmax)
333 if ($addr != $stackbase)
335 printf "Missing stack data for %x-%x - fill with 0x29\n", $stackbase, $addr-1;
336 @stack = (0x29292929) x (($addr-$stackbase)/4);
339 unshift @stack, reverse (unpack "V4", (pack "H2"x16, (split / /,$2)));
345 read_d_exc(@ARGV[0]);
348 read_rom_symbols(@ARGV[1]);
351 # We've accumulated the ranges of objects indexed by start address,
352 # with a companion list of addresses subdivided by the leading byte
353 # Now sort them numerically...
355 sub numerically { $a <=> $b }
356 foreach my $key (keys %addresslist)
358 @{$addresslist{$key}} = sort numerically @{$addresslist{$key}};
361 # Off we go, reading the stack!
368 my $word=(pop @stack);
369 if ($word!=0x29292929)
376 $stackptr += $skipped;
384 # Optimization - try looking up the address directly
389 if(defined $address{$word}) {
390 ($base, $max, $name) = @{$address{$word}};
392 if (!(defined $base))
396 foreach $base (@{$addresslist{$key}})
408 if(defined $regionbase)
410 ($base, $max, $name) = @{$address{$regionbase}};
413 if (defined $base && defined $max && $base <= $word && $max >= $word)
415 my $data = pack "V", $word;
416 $data =~ tr [\040-\177]/./c;
417 return sprintf "%08x %4s %s + 0x%x", $word, $data, $name, $word - $base;
424 # Try matching one of the named areas in the addresslist
427 my $word = (pop @stack);
429 if ($word < 1024*1024)
435 my $result = lookup_addr($word);
448 # Try matching a TBuf8
449 # 0x3000LLLL 0x0000MMMM data
452 if (scalar @stack <3)
454 return 0; # too short
456 my $word = (pop @stack);
457 my $maxlen = (pop @stack);
459 my $len = $word & 0x0ffff;
460 my $type = ($word >> 16) & 0x0ffff;
461 if ( $type != 0x3000 || $maxlen <= $len || $maxlen > 4* scalar @stack
462 || ($stackptr < $activesp && $stackptr + $maxlen + 8 > $activesp))
464 push @stack, $maxlen;
466 return 0; # wrong type, or invalid looking sizes, or out of date
469 printf "TBuf8<%d>, length %d\n", $maxlen, $len;
475 $string .= pack "V", pop @stack;
484 my $line = substr($string,0,$len);
485 my @buf = unpack "C*", $line;
486 $line =~ tr [\040-\177]/./c;
487 printf "\n %s", $line;
499 my $char = shift @buf;
500 printf "%02x ", $char;
508 # Skip the unused part of the stack
511 printf "High watermark = %04x\n", $stackptr;
513 # process the interesting bit!
515 my $printed_current_sp = 0;
518 if (!$printed_current_sp && $stackptr >= $activesp)
520 printf "\n >>>> current user stack pointer >>>>\n\n";
522 print $registers{"00"};
523 print $registers{"04"};
524 print $registers{"08"};
525 print $registers{"12"};
527 if ($is_exc && $user_pc != $fault_pc)
529 print "\nWARNING: A kernel-side exception occured but this script\n";
530 print "is currently limited to user stack analysis. Sorry.\n";
531 my $result = lookup_addr($fault_pc);
534 print "Kernel PC = $result\n";
536 $result = lookup_addr($fault_lr);
539 print "Kernel LR = $result\n";
544 my $result = lookup_addr($user_pc);
547 print "User PC = $result\n";
549 $result = lookup_addr($user_lr);
552 print "User LR = $result\n";
554 printf "\n >>>> current user stack pointer >>>>\n\n";
555 $printed_current_sp = 1;
558 printf "%04x ", $stackptr;
560 match_tbuf8() and next;
561 match_addr() and next;
564 $data = pack "V", $word;
565 $data =~ tr [\040-\177]/./c;
566 printf "%08x %4s ", $word, $data;
569 if ($word == 0x29292929)
571 $skipped = skip_unused;
580 # Try matching $word against the known addresses of things