-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack_decode.pl
More file actions
505 lines (431 loc) · 13.2 KB
/
Copy pathstack_decode.pl
File metadata and controls
505 lines (431 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#!/usr/bin/perl
# Stack dump decoder for ESP8266/Arduino.
# Requires the git version as we use objdump
# Assume latest build dir is current...
my $build_dir=`/bin/ls -1 -t -d /tmp/arduino_build_*|head -1l`;
my $sdk=$ENV{'HOME'}."/Arduino/hardware/esp8266com/esp8266";
chomp($build_dir);
my $elf = $build_dir."/DebugTools.ino.elf";
my $objdump = $sdk."/tools/xtensa-lx106-elf/bin/xtensa-lx106-elf-objdump";
my $linkscript = $sdk."/tools/esptool/flasher_stub/rom_8266.ld";
# first load the objdump of the elf....
my $obj = load_elf($elf);
# and the linker file for the boot rom addresses
my $rom = load_ld($linkscript);
# now the disassembly of the boot rom so we can extract the stack frame sizes
#read_dis("boot.txt");
my %ExceptionName=(
0=>"Illegal Instruction",
3=>"LoadStoreError",
4=>"Level1 Interrupt",
6=>"Division by Zero",
9=>"Load/Store Alignment",
28=>"Load Prohibited",
29=>"Store Prohibited",
);
my %stack;
my $instack=0;
my $stack_ptr=0;
my $exception;
print("\n");
while (<STDIN>) {
my $line = $_;
if ($line =~ />>>stack>>>/) {
$instack=1;
print("found >>>stack>>>\n");
}
elsif ($line =~ /<<<stack<<</) {
$instack=0;
print("found <<<stack<<<\n");
}
elsif ($line =~ /Dumping the umm_heap/) {
$inheap=1;
print("found heap\n");
}
elsif ($inheap && $line =~ /Total Entries/) {
$inheap=0;
print("got all of heap\n");
}
elsif ($instack>0) {
if ($line =~ /([\d|a-f]{8}): ([\d|a-f]{8}) ([\d|a-f]{8}) ([\d|a-f]{8}) ([\d|a-f]{8})/) {
#printf("Data Line, addr=%08x\n", hex($1));
my $sp = hex($1);
$stack{$sp+0} =hex($2);
$stack{$sp+4} =hex($3);
$stack{$sp+8} =hex($4);
$stack{$sp+12}=hex($5);
$stack_ptr = $sp unless $stack_ptr;
$stack_end = $sp+16;
}
elsif ($line =~ /ctx: (.+)/ ) {
$stack_context=$1;
chomp($stack_context);
}
}
elsif ($inheap>0) {
if ($line =~ /\|0x([\d|a-f]{8})\|(.+?)\|/) {
my $addr=$1;
my $block=$2;
if ($addr >= $stack_ptr && $addr <= $stack_end) {
printf("**** Heap block in stack area! **** %s|%s\n", $addr, $block);
}
}
}
elsif ($line =~ /Exception \((\d+?)\):/ ) {
$exception->{'reason'}=$1;
printf("Exception %d: %s\n", $excep, $ExceptionName{$exception->{'reason'}});
}
elsif ($line =~ /Reset info: reason=(\d+?) / ) {
$exception->{'reason'}=$1;
printf("Exception %d: %s\n", $excep, $ExceptionName{$exception->{'reason'}});
}
elsif ($line =~ /epc1=(.{8,10}),{0,1} epc2=/) {
my @line = split(/ /,$line);
foreach my $item (@line) {
my ($var,$val)=split(/=/,$item,2);
$exception->{$var} = hex($val);
}
}
}
printf("%s Stack Ptr: %08x Stack End: %08x %d (0x%x) bytes\n",
$stack_context,
$stack_ptr, $stack_end,
$stack_end - $stack_ptr,
$stack_end - $stack_ptr);
if ($stack_ptr < ($stack_end-4096)) {
printf("WARNING: stack is > 4K\n");
if ($stack_ptr < ($stack_end - 16384)) {
printf("WARNING: stack is > 16K which may run into dram!\n");
}
}
validate_stack_refs();
print("\n");
print_exception_info();
my $sp = $stack_ptr;
if ($ARGV[0]) {
$sp = hex($ARGV[0]);
printf("Starting with sp=%08x\n", $sp);
print("\n");
traverse_back($sp);
print("\n");
traverse_forward($sp);
} else {
print("\n");
traverse_back($stack_ptr);
print("\n");
traverse_forward($stack_end-4);
}
print("\n");
print_exception_info();
sub print_exception_info
{
if (defined($exception) && defined($exception->{'reason'})) {
printf("%s Exception (%d) at epc1=%08x vector=%x",
$ExceptionName{$exception->{'reason'}},
$exception->{'reason'},
$exception->{'epc1'},
$exception->{'excvaddr'});
my $ip=$exception->{'epc1'};
my ($fname, $faddr, $fsize) = getAddrInfo($ip);
if ($fname) {
printf("<%s+%02x>\n", $fname, $ip - $faddr);
} else {
printf(" - invalid instruction address\n", $ip);
my $i=0;
if (isOurCode($ip)) {
while (($i<8) && !defined($obj->{$ip})) {
$ip++;
}
if (defined($obj->{$ip})) {
$fname = $obj->{$ip};
printf(" - address is at <%s+%02x>\n",
$fname, $ip - $obj->{$fname}{'addr'});
}
}
}
}
}
sub validate_stack_refs
{
# scan the stack for any addresses within the stack that refer to a location
# lower than where they are stored.
my $sp = $stack_ptr;
while ($sp < $stack_end) {
if (isStack($stack{$sp}) && ($stack{$sp} < $sp)) {
printf("WARNING: address on stack refers to part of stack that did not exist at the time\n");
printf(" \@ %08x -> %08x (%d bytes below self)\n",
$sp, $stack{$sp}, $sp - $stack{$sp});
}
$sp++;
}
}
sub traverse_back
{ my ($sp) = @_;
my ($func, $faddr, $fsize, $ip);
# Try traversing the stack from the current SP towards the end....
print("Looking for a code ref:");
while (($sp < $stack_end) &&
(!isOurCode($stack{$sp}) || !defined($obj->{$stack{$sp}}))
) {
# find a code ref because we don't know how big the top frame is
$sp+=4;
}
printf(" sp=%08x\n", $sp);
if ($sp < $stack_end) {
my $ip = $stack{$sp};
($func, $faddr, $fsize) = getAddrInfo($ip);
printf("Top Frame:\n");
printf("\@%08x returns to %08x <%s+%02x> whose frame is %s\n",
$sp, $ip, $func, $ip - $faddr,
defined($fsize) ? $fsize." bytes" : "unknown");
$sp += 4;
}
# now we know the frame size of the next frame, we can traverse the stack
my $count=0;
while (($sp < $stack_end) && ($count<5) && ($fsize>0)) {
my $bp = $sp + $fsize - 4;
my $ip = $stack{$bp};
$sp += $fsize;
($func, $faddr, $fsize) = getAddrInfo($ip);
printf("\@%08x returns to %08x <%s+%02x> %s\n",
$bp, $ip, $func, $ip - $faddr,
defined($fsize) ? "(".$fsize." byte frame)" : "(unknown frame size)");
if (!isOurCode($ip) && !isRomCode($sp)) {
printf("ERROR: caller address is not a code ref!\n");
} else {
$count=0;
}
$count++;
if ($fsize<=0) {
printf("The return address is invalid!\n");
$count=9999; # quit
}
}
}
sub traverse_forward
{ my ($sp) = @_;
my ($func, $faddr, $fsize, $ip);
# Try traversing the stack from the end towards the current SP
print("Looking for a code ref:");
while (($sp >= $stack_ptr) &&
(!isOurCode($stack{$sp}) || !defined($obj->{$stack{$sp}}))
) {
# find a code ref because we don't know how big the top frame is
$sp-=4;
}
printf(" sp=%08x\n", $sp);
if ($sp > $stack_ptr) {
my $ip = $stack{$sp};
($func, $faddr, $fsize) = getAddrInfo($ip);
printf("Bottom Frame:\n");
printf(" returns to %08x <%s+%02x> whose frame is %s\n",
$ip, $func, $ip - $faddr,
defined($fsize) ? $fsize." bytes" : "unknown");
$sp += 4;
}
my $count=0;
my $newsp=$sp-8;
while (($newsp > $stack_ptr) && ($count<50)) {
($func, $faddr, $fsize) = getAddrInfo($stack{$newsp});
if (isRomCode($stack{$newsp})) {
printf("%08x: ROM call detected (%08x: <%s+%02x>), moving sp to %08x\n",
$newsp, $stack{$newsp}, $func, $stack{$newsp}-$faddr, $newsp+4);
$sp=$newsp+4;
}
elsif (isOurCode($stack{$newsp}) && defined($func)) {
my $bp=$sp-4;
# a valid code address, see if it's in the right place...
$retip = $stack{$newsp};
# is the next return addr where we would expect it to be?
# - look at the end of this stack frame
my $from = $stack{$bp};
my ($from_func, $from_faddr, $from_fsize) = getAddrInfo($from);
#printf("retip=%08x, sp=%08x newsp=%08x diff=%d, fsize=%d func=%s\n",
# $retip, $sp, $newsp, $sp-$newsp-4, $fsize, $func);
if (($sp - $newsp - 4) == $fsize) {
# yes - valid frame so print it.
printf("%08x: call from %08x <%s+%02x> %s to %08x <%s>\n",
$newsp, $from, $from_func, $from - $from_faddr,
defined($fsize) ? "(".$fsize." byte frame)" : "(unknown frame size)",
$retip, $func);
$count=0;
$sp=$newsp+4;
} else {
#printf("STACK CORRUPTED: return address found but not in the right place\n");
#printf(" sp-newsp=%d but fsize=%d\n", $sp-$newsp, $fsize);
# attempt to re-sync
printf("%08x: Trying to re-sync... %08x <%s+%02x>\n",
$newsp, $retip, $func, $retip-$faddr);
$sp = $newsp+4;
$count++;
}
}
$newsp-=4;
}
}
sub isIram
{ my ($a)=@_;
return 1 if ($a>=0x4010E000) && ($a<(0x4010E000+0x2000));
}
sub isDram
{ my ($a)=@_;
return 1 if ($a>=0x3FFE8000) && ($a<(0x3FFE8000+0x14000));
}
sub isRomCode
{ my ($a)=@_;
$a &= 0xFFF00000;
return 1 if $a == 0x40000000;
return 0;
}
sub isOurCode
{ my ($a)=@_;
$a &= 0xFFF00000;
return 1 if $a == 0x40100000;
return 1 if $a == 0x40200000;
return 0;
}
sub isStack
{ my ($a) = @_;
return 1 if ($a>=$stack_ptr) && ($a<$stack_end);
return 0;
}
sub isStackInDram
{
return isDram($stack_ptr) && isDram($stack_end);
}
sub getAddrInfo
{ my ($addr)=@_;
my ($func, $faddr, $fsize);
if (isRomCode($addr)) {
my $rec = getRomName($rom, $addr);
if ($rec) {
$func = $rec->{'name'};
$faddr= $rec->{'addr'};
$fsize=4;
# actually we have no idea of frame size, and can't find out
# without downloading the ROM and objdumping it, then
# looking for the stack frame setup code on each function.
}
} elsif (isOurCode($addr)) {
$func = $obj->{$addr};
$faddr= $obj->{$func}{'addr'};
$fsize= $obj->{$func}{'frame'};
}
return ($func, $faddr, $fsize);
}
sub load_elf
{ my ($fn) = @_;
my $addr;
my $name;
my $frame;
my %memmap;
my $func_line=0;
my $fh;
$fn = $objdump.' -C -w -d '.$fn;
printf("Reading %s\n", $fn);
open($fh, '-|', $fn) || die($!);
while (<$fh>) {
my $line = $_;
#printf("OBJ:%s\n", $line);
if ($line =~ /^([\d|a-f]{8}) <(.+?)>:/) {
$addr=hex($1);
$name=$2;
$memmap{$addr} = $name;
$memmap{$name}{'frame'} = undef;
$memmap{$name}{'addr'} = $addr;
$func_line=0;
#printf("Found %08x: %s\n", $addr, $name);
}
elsif ($line =~ /^([\d|a-f]{8}):\s+?.+?\s+?addi\s+?a1, a1, -(\d{1,4})/) {
$addr=hex($1);
$frame=$2;
if ($func_line < 5) {
$memmap{$name}{'frame'} = $frame;
$func_line++;
$memmap{$addr} = $name;
#printf("Frame %s: %d (func_line=%d)\n", $name, $memmap{$name}, $func_line);
}
}
elsif ($line =~ /^([\d|a-f]{8}):/) {
$addr = hex($1);
$memmap{$addr} = $name;
#printf("ADDR: %08x %s\n", $addr, $name);
}
}
close($fh);
return \%memmap;
}
sub getRomName
{ my ($sorted_memmap, $addr)=@_;
return undef unless isRomCode($addr);
foreach my $r (@{$sorted_memmap}) {
#printf("Check %s @ %08x <= %08x\n", $r->{'name'}, $r->{'addr'}, $addr);
return $r if $r->{'addr'} <= $addr;
}
return undef;
}
sub load_ld
{ my ($fn) = @_;
my $fh;
my @memmap;
printf("Reading %s\n", $fn);
open($fh, '<', $fn) || die($!);
while (<$fh>) {
my $line = $_;
if ($line =~ /^PROVIDE \( (.+?) = 0x([\d|a-f]{8}) \)/ ) {
my $rec;
$rec->{'name'}=$1;
$rec->{'addr'}=hex($2);
#printf("Found %s @ %08x\n", $rec->{'name'}, $rec->{'addr'});
push(@memmap, $rec) if isRomCode($rec->{'addr'});
}
}
close($fh);
# we will need them sorted for searching...
my @sorted = sort { $b->{'addr'} <=> $a->{'addr'} } @memmap;
return \@sorted;
}
sub read_dis
{ my ($fn)=@_;
my $fh;
my $func,$addr,$fsize,$a6;
my $funcaddr, $funcline=0;
printf("Reading boot disassembly %s\n", $fn);
open($fh, "<", $fn) || (print("Can't load disassembly\n") && return);
while (<$fh>) {
my $line = $_;
if ($line =~ /^;/) {
# comment - ignore
}
elsif ($line =~ /^\s*?(.+?):$/ ) {
$func = $1;
$funcline=0;
}
elsif ($line =~ /^([\d|a-f]):/ ) {
$addr=hex($1);
$funcline++;
if (defined($rom->{$addr})) { # is this in our list of ROM functions?
$func = $rom->{'addr'}{'name'};
$funcaddr = $addr;
$fsize = 0; # it exists, must have at least no frame.
$funcline = 1;
}
if ($funcline < 5) {
if ($line =~ /^([\d|a-f]{8}):\s+?.+?\s+?movi\s+?a6, (\d{1,4})/) {
# prepping a6 to subtract from a1 ?
$a6=$2;
}
elsif ($line =~ /^([\d|a-f]{8}):\s+?.+?\s+?sub\s+?a1, a1, a6/) {
# actual adjustment of a1 by a6
$fsize=$a6;
}
elsif ($line =~ /^([\d|a-f]{8}):\s+?.+?\s+?addi\s+?a1, a1, -(\d{1,4})/) {
# direct adjustment of a1
$fsize=$2;
printf("FOUND: %08x %s, %d\n", $addr, $func, $fsize);
}
}
}
}
close($fh);
}