Commit | Line | Data |
---|---|---|
5aea50b5 AV |
1 | #!/usr/bin/perl -w |
2 | ||
d32ad102 AV |
3 | use File::Basename; |
4 | ||
5aea50b5 AV |
5 | # Copyright 2008, Intel Corporation |
6 | # | |
7 | # This file is part of the Linux kernel | |
8 | # | |
9 | # This program file is free software; you can redistribute it and/or modify it | |
10 | # under the terms of the GNU General Public License as published by the | |
11 | # Free Software Foundation; version 2 of the License. | |
12 | # | |
13 | # Authors: | |
14 | # Arjan van de Ven <arjan@linux.intel.com> | |
15 | ||
16 | ||
17 | my $vmlinux_name = $ARGV[0]; | |
d32ad102 AV |
18 | if (!defined($vmlinux_name)) { |
19 | my $kerver = `uname -r`; | |
20 | chomp($kerver); | |
21 | $vmlinux_name = "/lib/modules/$kerver/build/vmlinux"; | |
22 | print "No vmlinux specified, assuming $vmlinux_name\n"; | |
23 | } | |
24 | my $filename = $vmlinux_name; | |
5aea50b5 AV |
25 | # |
26 | # Step 1: Parse the oops to find the EIP value | |
27 | # | |
28 | ||
29 | my $target = "0"; | |
d32ad102 AV |
30 | my $function; |
31 | my $module = ""; | |
32 | my $func_offset; | |
33 | my $vmaoffset = 0; | |
34 | ||
c19ef7fd AV |
35 | my %regs; |
36 | ||
37 | ||
38 | sub parse_x86_regs | |
39 | { | |
40 | my ($line) = @_; | |
41 | if ($line =~ /EAX: ([0-9a-f]+) EBX: ([0-9a-f]+) ECX: ([0-9a-f]+) EDX: ([0-9a-f]+)/) { | |
42 | $regs{"%eax"} = $1; | |
43 | $regs{"%ebx"} = $2; | |
44 | $regs{"%ecx"} = $3; | |
45 | $regs{"%edx"} = $4; | |
46 | } | |
47 | if ($line =~ /ESI: ([0-9a-f]+) EDI: ([0-9a-f]+) EBP: ([0-9a-f]+) ESP: ([0-9a-f]+)/) { | |
48 | $regs{"%esi"} = $1; | |
49 | $regs{"%edi"} = $2; | |
50 | $regs{"%esp"} = $4; | |
51 | } | |
52 | } | |
53 | ||
54 | sub process_x86_regs | |
55 | { | |
56 | my ($line, $cntr) = @_; | |
57 | my $str = ""; | |
58 | if (length($line) < 40) { | |
59 | return ""; # not an asm istruction | |
60 | } | |
61 | ||
62 | # find the arguments to the instruction | |
63 | if ($line =~ /([0-9a-zA-Z\,\%\(\)\-\+]+)$/) { | |
64 | $lastword = $1; | |
65 | } else { | |
66 | return ""; | |
67 | } | |
68 | ||
69 | # we need to find the registers that get clobbered, | |
70 | # since their value is no longer relevant for previous | |
71 | # instructions in the stream. | |
72 | ||
73 | $clobber = $lastword; | |
74 | # first, remove all memory operands, they're read only | |
75 | $clobber =~ s/\([a-z0-9\%\,]+\)//g; | |
76 | # then, remove everything before the comma, thats the read part | |
77 | $clobber =~ s/.*\,//g; | |
78 | ||
79 | # if this is the instruction that faulted, we haven't actually done | |
80 | # the write yet... nothing is clobbered. | |
81 | if ($cntr == 0) { | |
82 | $clobber = ""; | |
83 | } | |
84 | ||
85 | foreach $reg (keys(%regs)) { | |
86 | my $val = $regs{$reg}; | |
87 | # first check if we're clobbering this register; if we do | |
88 | # we print it with a =>, and then delete its value | |
89 | if ($clobber =~ /$reg/) { | |
90 | if (length($val) > 0) { | |
91 | $str = $str . " $reg => $val "; | |
92 | } | |
93 | $regs{$reg} = ""; | |
94 | $val = ""; | |
95 | } | |
96 | # now check if we're reading this register | |
97 | if ($lastword =~ /$reg/) { | |
98 | if (length($val) > 0) { | |
99 | $str = $str . " $reg = $val "; | |
100 | } | |
101 | } | |
102 | } | |
103 | return $str; | |
104 | } | |
105 | ||
106 | # parse the oops | |
5aea50b5 | 107 | while (<STDIN>) { |
d32ad102 AV |
108 | my $line = $_; |
109 | if ($line =~ /EIP: 0060:\[\<([a-z0-9]+)\>\]/) { | |
5aea50b5 AV |
110 | $target = $1; |
111 | } | |
d32ad102 AV |
112 | if ($line =~ /EIP is at ([a-zA-Z0-9\_]+)\+(0x[0-9a-f]+)\/0x[a-f0-9]/) { |
113 | $function = $1; | |
114 | $func_offset = $2; | |
115 | } | |
5aea50b5 | 116 | |
d32ad102 AV |
117 | # check if it's a module |
118 | if ($line =~ /EIP is at ([a-zA-Z0-9\_]+)\+(0x[0-9a-f]+)\/0x[a-f0-9]+\W\[([a-zA-Z0-9\_\-]+)\]/) { | |
119 | $module = $3; | |
120 | } | |
c19ef7fd | 121 | parse_x86_regs($line); |
5aea50b5 AV |
122 | } |
123 | ||
d32ad102 | 124 | my $decodestart = hex($target) - hex($func_offset); |
c19ef7fd | 125 | my $decodestop = hex($target) + 8192; |
5aea50b5 AV |
126 | if ($target eq "0") { |
127 | print "No oops found!\n"; | |
128 | print "Usage: \n"; | |
129 | print " dmesg | perl scripts/markup_oops.pl vmlinux\n"; | |
130 | exit; | |
131 | } | |
132 | ||
d32ad102 AV |
133 | # if it's a module, we need to find the .ko file and calculate a load offset |
134 | if ($module ne "") { | |
135 | my $dir = dirname($filename); | |
136 | $dir = $dir . "/"; | |
137 | my $mod = $module . ".ko"; | |
138 | my $modulefile = `find $dir -name $mod | head -1`; | |
139 | chomp($modulefile); | |
140 | $filename = $modulefile; | |
141 | if ($filename eq "") { | |
142 | print "Module .ko file for $module not found. Aborting\n"; | |
143 | exit; | |
144 | } | |
145 | # ok so we found the module, now we need to calculate the vma offset | |
146 | open(FILE, "objdump -dS $filename |") || die "Cannot start objdump"; | |
147 | while (<FILE>) { | |
148 | if ($_ =~ /^([0-9a-f]+) \<$function\>\:/) { | |
149 | my $fu = $1; | |
150 | $vmaoffset = hex($target) - hex($fu) - hex($func_offset); | |
151 | } | |
152 | } | |
153 | close(FILE); | |
154 | } | |
155 | ||
5aea50b5 AV |
156 | my $counter = 0; |
157 | my $state = 0; | |
158 | my $center = 0; | |
159 | my @lines; | |
c19ef7fd | 160 | my @reglines; |
5aea50b5 AV |
161 | |
162 | sub InRange { | |
163 | my ($address, $target) = @_; | |
164 | my $ad = "0x".$address; | |
165 | my $ta = "0x".$target; | |
166 | my $delta = hex($ad) - hex($ta); | |
167 | ||
168 | if (($delta > -4096) && ($delta < 4096)) { | |
169 | return 1; | |
170 | } | |
171 | return 0; | |
172 | } | |
173 | ||
174 | ||
175 | ||
176 | # first, parse the input into the lines array, but to keep size down, | |
177 | # we only do this for 4Kb around the sweet spot | |
178 | ||
d32ad102 | 179 | open(FILE, "objdump -dS --adjust-vma=$vmaoffset --start-address=$decodestart --stop-address=$decodestop $filename |") || die "Cannot start objdump"; |
5aea50b5 AV |
180 | |
181 | while (<FILE>) { | |
182 | my $line = $_; | |
183 | chomp($line); | |
184 | if ($state == 0) { | |
185 | if ($line =~ /^([a-f0-9]+)\:/) { | |
186 | if (InRange($1, $target)) { | |
187 | $state = 1; | |
188 | } | |
189 | } | |
190 | } else { | |
191 | if ($line =~ /^([a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]+)\:/) { | |
192 | my $val = $1; | |
193 | if (!InRange($val, $target)) { | |
194 | last; | |
195 | } | |
196 | if ($val eq $target) { | |
197 | $center = $counter; | |
198 | } | |
199 | } | |
200 | $lines[$counter] = $line; | |
201 | ||
202 | $counter = $counter + 1; | |
203 | } | |
204 | } | |
205 | ||
206 | close(FILE); | |
207 | ||
208 | if ($counter == 0) { | |
209 | print "No matching code found \n"; | |
210 | exit; | |
211 | } | |
212 | ||
213 | if ($center == 0) { | |
214 | print "No matching code found \n"; | |
215 | exit; | |
216 | } | |
217 | ||
218 | my $start; | |
219 | my $finish; | |
220 | my $codelines = 0; | |
221 | my $binarylines = 0; | |
222 | # now we go up and down in the array to find how much we want to print | |
223 | ||
224 | $start = $center; | |
225 | ||
226 | while ($start > 1) { | |
227 | $start = $start - 1; | |
228 | my $line = $lines[$start]; | |
229 | if ($line =~ /^([a-f0-9]+)\:/) { | |
230 | $binarylines = $binarylines + 1; | |
231 | } else { | |
232 | $codelines = $codelines + 1; | |
233 | } | |
234 | if ($codelines > 10) { | |
235 | last; | |
236 | } | |
237 | if ($binarylines > 20) { | |
238 | last; | |
239 | } | |
240 | } | |
241 | ||
242 | ||
243 | $finish = $center; | |
244 | $codelines = 0; | |
245 | $binarylines = 0; | |
246 | while ($finish < $counter) { | |
247 | $finish = $finish + 1; | |
248 | my $line = $lines[$finish]; | |
249 | if ($line =~ /^([a-f0-9]+)\:/) { | |
250 | $binarylines = $binarylines + 1; | |
251 | } else { | |
252 | $codelines = $codelines + 1; | |
253 | } | |
254 | if ($codelines > 10) { | |
255 | last; | |
256 | } | |
257 | if ($binarylines > 20) { | |
258 | last; | |
259 | } | |
260 | } | |
261 | ||
262 | ||
263 | my $i; | |
264 | ||
c19ef7fd AV |
265 | |
266 | # start annotating the registers in the asm. | |
267 | # this goes from the oopsing point back, so that the annotator | |
268 | # can track (opportunistically) which registers got written and | |
269 | # whos value no longer is relevant. | |
270 | ||
271 | $i = $center; | |
272 | while ($i >= $start) { | |
273 | $reglines[$i] = process_x86_regs($lines[$i], $center - $i); | |
274 | $i = $i - 1; | |
275 | } | |
276 | ||
5aea50b5 AV |
277 | $i = $start; |
278 | while ($i < $finish) { | |
c19ef7fd | 279 | my $line; |
5aea50b5 | 280 | if ($i == $center) { |
c19ef7fd | 281 | $line = "*$lines[$i] "; |
5aea50b5 | 282 | } else { |
c19ef7fd AV |
283 | $line = " $lines[$i] "; |
284 | } | |
285 | print $line; | |
286 | if (defined($reglines[$i]) && length($reglines[$i]) > 0) { | |
287 | my $c = 60 - length($line); | |
288 | while ($c > 0) { print " "; $c = $c - 1; }; | |
289 | print "| $reglines[$i]"; | |
5aea50b5 | 290 | } |
c19ef7fd AV |
291 | if ($i == $center) { |
292 | print "<--- faulting instruction"; | |
293 | } | |
294 | print "\n"; | |
5aea50b5 AV |
295 | $i = $i +1; |
296 | } | |
297 |