]> rtime.felk.cvut.cz Git - l4.git/blob - kernel/fiasco/tool/backtrace
update
[l4.git] / kernel / fiasco / tool / backtrace
1 #! /usr/bin/perl -W
2
3 use Math::BigInt;
4 use strict;
5
6 my %sym_tab = (new Math::BigInt(0) => "ERRRRRRROR");
7 my $sections = "BdDdTtVvWwuU";
8 my $img = shift;
9 my %sec_tab;
10
11 my %test;
12
13 sub as_hex($)
14 {
15   my $i = shift;
16   my $h = substr $i->as_hex(), 2;
17   $h = ('0' x (16-length($h))) . $h;
18   return $h;
19 }
20
21 if (!defined $img)
22 {
23   print "$0 image\n";
24   print " input is read from stdin\n";
25   exit 1;
26 }
27
28 my $nm = 'nm';
29 $nm = 'arm-softfloat-elf-nm' if !(system("file -L $img | grep -qw ARM") >> 8);
30
31 foreach my $l (split('\n', qx{$nm $img | c++filt}))
32 {
33   if ($l =~ /^([0-9a-fA-F]*)\s+([$sections])\s+(.*)$/)
34   {
35     my ($addr, $sec, $sym) = (new Math::BigInt("0x$1"), $2, $3);
36     if (defined $addr && ref $addr && !$addr->is_nan())
37     {
38       $sym_tab{as_hex($addr)} = $sym;
39       $sec_tab{as_hex($addr)} = $sec;
40     }
41   }
42 }
43 my @sorted_sym_tab_keys = sort keys %sym_tab;
44 my $min_addr = $sorted_sym_tab_keys[0];
45 my $max_addr = $sorted_sym_tab_keys[@sorted_sym_tab_keys - 1];
46
47 print "Scanning image done, proceed.\n";
48
49 sub find_sym($)
50 {
51   my $addr = as_hex(shift);
52   my $hit = '0';
53
54   return new Math::BigInt(0)
55     if $addr lt $min_addr or $addr gt $max_addr;
56
57   foreach my $s (@sorted_sym_tab_keys)
58   {
59     if ($s gt $addr)
60     {
61       return new Math::BigInt("0x$hit");
62     }
63
64     $hit = $s;
65   }
66
67   return new Math::BigInt(0);
68 }
69
70 sub print_func($)
71 {
72   my $addr = new Math::BigInt("0x".shift);
73   my $hit  = find_sym($addr);
74   my $offset = $addr-$hit;
75   my $o = $hit->as_hex();
76
77   return unless $hit;
78
79   printf " %s %30s(%s) + %6s = %s\n",
80          $addr->as_hex(), $sym_tab{as_hex($hit)}, $sec_tab{as_hex($hit)},
81          $offset->as_hex(), $hit->as_hex();
82 }
83
84
85 my $last_f = 0;
86 while (<>)
87 {
88   if (/^\s+#(\d+)\s+([0-9a-f]+)\s+([0-9a-f]+)/i) # fiasco bt without debuginfo
89   {
90     my $fn = $1;
91     my $stack = new Math::BigInt("0x$2");
92     my $addr = $3;
93     my $fsize = $stack - $last_f;
94
95     $last_f = $stack;
96     printf "%2d %s ", $fn, $stack->as_hex();
97     if ($fsize >= 0 && $fsize <= 2000)
98     {
99       printf "%4d", $fsize;
100     } else {
101       printf "....";
102     }
103     print_func($addr);
104   }
105   elsif (/^(?:.*?\|)?\s*(0x)?([0-9a-f]+)\s*$/i) # simple figure
106   {
107     print_func($2);
108   }
109   elsif (/^[\da-f]+:([\d\sa-f]+)$/i) # fiasco memory dump (mostly user stack)
110   {
111     my $l = $1;
112     for my $addr (split(/\s+/, $l))
113     {
114       print_func($addr);
115     }
116   }
117   elsif (/^\s*[\da-f]+\s+([\d\sa-f]+)\s*$/i) # fiasco tcb view stack
118   {
119     my $l = $1;
120     for my $val (split(/\s+/, $l))
121     {
122       next if $val eq '35353535'; # stack poison
123       if ($val =~ /^f.......(?:........)?$/i) {
124         print_func($val);
125       } else {
126         print " 0x$val  ... value ...\n";
127       }
128     }
129   }
130 }
131
132