blob: 89774011965d7f17289b09af086c1651e9cf7ef1 [file] [log] [blame]
Arjan van de Ven11df65c2009-02-15 11:30:55 +01001#!/usr/bin/perl
Arjan van de Ven5aea50b2009-01-06 14:40:57 -08002
Arjan van de Vend32ad102009-01-11 15:03:23 +00003use File::Basename;
Matthew Wilcox51fbb4b2009-07-29 15:02:03 -07004use Math::BigInt;
Arjan van de Vend32ad102009-01-11 15:03:23 +00005
Arjan van de Ven5aea50b2009-01-06 14:40:57 -08006# Copyright 2008, Intel Corporation
7#
8# This file is part of the Linux kernel
9#
10# This program file is free software; you can redistribute it and/or modify it
11# under the terms of the GNU General Public License as published by the
12# Free Software Foundation; version 2 of the License.
13#
14# Authors:
15# Arjan van de Ven <arjan@linux.intel.com>
16
17
18my $vmlinux_name = $ARGV[0];
Arjan van de Vend32ad102009-01-11 15:03:23 +000019if (!defined($vmlinux_name)) {
20 my $kerver = `uname -r`;
21 chomp($kerver);
22 $vmlinux_name = "/lib/modules/$kerver/build/vmlinux";
23 print "No vmlinux specified, assuming $vmlinux_name\n";
24}
25my $filename = $vmlinux_name;
Arjan van de Ven5aea50b2009-01-06 14:40:57 -080026#
27# Step 1: Parse the oops to find the EIP value
28#
29
30my $target = "0";
Arjan van de Vend32ad102009-01-11 15:03:23 +000031my $function;
32my $module = "";
Arjan van de Ven11df65c2009-02-15 11:30:55 +010033my $func_offset = 0;
Arjan van de Vend32ad102009-01-11 15:03:23 +000034my $vmaoffset = 0;
35
Arjan van de Venc19ef7f2009-02-15 11:30:52 +010036my %regs;
37
38
39sub parse_x86_regs
40{
41 my ($line) = @_;
42 if ($line =~ /EAX: ([0-9a-f]+) EBX: ([0-9a-f]+) ECX: ([0-9a-f]+) EDX: ([0-9a-f]+)/) {
43 $regs{"%eax"} = $1;
44 $regs{"%ebx"} = $2;
45 $regs{"%ecx"} = $3;
46 $regs{"%edx"} = $4;
47 }
48 if ($line =~ /ESI: ([0-9a-f]+) EDI: ([0-9a-f]+) EBP: ([0-9a-f]+) ESP: ([0-9a-f]+)/) {
49 $regs{"%esi"} = $1;
50 $regs{"%edi"} = $2;
51 $regs{"%esp"} = $4;
52 }
Arjan van de Ven11df65c2009-02-15 11:30:55 +010053 if ($line =~ /RAX: ([0-9a-f]+) RBX: ([0-9a-f]+) RCX: ([0-9a-f]+)/) {
54 $regs{"%eax"} = $1;
55 $regs{"%ebx"} = $2;
56 $regs{"%ecx"} = $3;
57 }
58 if ($line =~ /RDX: ([0-9a-f]+) RSI: ([0-9a-f]+) RDI: ([0-9a-f]+)/) {
59 $regs{"%edx"} = $1;
60 $regs{"%esi"} = $2;
61 $regs{"%edi"} = $3;
62 }
63 if ($line =~ /RBP: ([0-9a-f]+) R08: ([0-9a-f]+) R09: ([0-9a-f]+)/) {
64 $regs{"%r08"} = $2;
65 $regs{"%r09"} = $3;
66 }
67 if ($line =~ /R10: ([0-9a-f]+) R11: ([0-9a-f]+) R12: ([0-9a-f]+)/) {
68 $regs{"%r10"} = $1;
69 $regs{"%r11"} = $2;
70 $regs{"%r12"} = $3;
71 }
72 if ($line =~ /R13: ([0-9a-f]+) R14: ([0-9a-f]+) R15: ([0-9a-f]+)/) {
73 $regs{"%r13"} = $1;
74 $regs{"%r14"} = $2;
75 $regs{"%r15"} = $3;
76 }
77}
78
79sub reg_name
80{
81 my ($reg) = @_;
82 $reg =~ s/r(.)x/e\1x/;
83 $reg =~ s/r(.)i/e\1i/;
84 $reg =~ s/r(.)p/e\1p/;
85 return $reg;
Arjan van de Venc19ef7f2009-02-15 11:30:52 +010086}
87
88sub process_x86_regs
89{
90 my ($line, $cntr) = @_;
91 my $str = "";
92 if (length($line) < 40) {
93 return ""; # not an asm istruction
94 }
95
96 # find the arguments to the instruction
97 if ($line =~ /([0-9a-zA-Z\,\%\(\)\-\+]+)$/) {
98 $lastword = $1;
99 } else {
100 return "";
101 }
102
103 # we need to find the registers that get clobbered,
104 # since their value is no longer relevant for previous
105 # instructions in the stream.
106
107 $clobber = $lastword;
108 # first, remove all memory operands, they're read only
109 $clobber =~ s/\([a-z0-9\%\,]+\)//g;
110 # then, remove everything before the comma, thats the read part
111 $clobber =~ s/.*\,//g;
112
113 # if this is the instruction that faulted, we haven't actually done
114 # the write yet... nothing is clobbered.
115 if ($cntr == 0) {
116 $clobber = "";
117 }
118
119 foreach $reg (keys(%regs)) {
Arjan van de Ven11df65c2009-02-15 11:30:55 +0100120 my $clobberprime = reg_name($clobber);
121 my $lastwordprime = reg_name($lastword);
Arjan van de Venc19ef7f2009-02-15 11:30:52 +0100122 my $val = $regs{$reg};
Arjan van de Ven11df65c2009-02-15 11:30:55 +0100123 if ($val =~ /^[0]+$/) {
124 $val = "0";
125 } else {
126 $val =~ s/^0*//;
127 }
128
Arjan van de Venc19ef7f2009-02-15 11:30:52 +0100129 # first check if we're clobbering this register; if we do
130 # we print it with a =>, and then delete its value
Arjan van de Ven11df65c2009-02-15 11:30:55 +0100131 if ($clobber =~ /$reg/ || $clobberprime =~ /$reg/) {
Arjan van de Venc19ef7f2009-02-15 11:30:52 +0100132 if (length($val) > 0) {
133 $str = $str . " $reg => $val ";
134 }
135 $regs{$reg} = "";
136 $val = "";
137 }
138 # now check if we're reading this register
Arjan van de Ven11df65c2009-02-15 11:30:55 +0100139 if ($lastword =~ /$reg/ || $lastwordprime =~ /$reg/) {
Arjan van de Venc19ef7f2009-02-15 11:30:52 +0100140 if (length($val) > 0) {
141 $str = $str . " $reg = $val ";
142 }
143 }
144 }
145 return $str;
146}
147
148# parse the oops
Arjan van de Ven5aea50b2009-01-06 14:40:57 -0800149while (<STDIN>) {
Arjan van de Vend32ad102009-01-11 15:03:23 +0000150 my $line = $_;
151 if ($line =~ /EIP: 0060:\[\<([a-z0-9]+)\>\]/) {
Arjan van de Ven5aea50b2009-01-06 14:40:57 -0800152 $target = $1;
153 }
Arjan van de Ven11df65c2009-02-15 11:30:55 +0100154 if ($line =~ /RIP: 0010:\[\<([a-z0-9]+)\>\]/) {
155 $target = $1;
156 }
Arjan van de Vend32ad102009-01-11 15:03:23 +0000157 if ($line =~ /EIP is at ([a-zA-Z0-9\_]+)\+(0x[0-9a-f]+)\/0x[a-f0-9]/) {
158 $function = $1;
159 $func_offset = $2;
160 }
Arjan van de Ven11df65c2009-02-15 11:30:55 +0100161 if ($line =~ /RIP: 0010:\[\<[0-9a-f]+\>\] \[\<[0-9a-f]+\>\] ([a-zA-Z0-9\_]+)\+(0x[0-9a-f]+)\/0x[a-f0-9]/) {
162 $function = $1;
163 $func_offset = $2;
164 }
Arjan van de Vend32ad102009-01-11 15:03:23 +0000165
166 # check if it's a module
167 if ($line =~ /EIP is at ([a-zA-Z0-9\_]+)\+(0x[0-9a-f]+)\/0x[a-f0-9]+\W\[([a-zA-Z0-9\_\-]+)\]/) {
168 $module = $3;
169 }
Arjan van de Ven11df65c2009-02-15 11:30:55 +0100170 if ($line =~ /RIP: 0010:\[\<[0-9a-f]+\>\] \[\<[0-9a-f]+\>\] ([a-zA-Z0-9\_]+)\+(0x[0-9a-f]+)\/0x[a-f0-9]+\W\[([a-zA-Z0-9\_\-]+)\]/) {
171 $module = $3;
172 }
Arjan van de Venc19ef7f2009-02-15 11:30:52 +0100173 parse_x86_regs($line);
Arjan van de Ven5aea50b2009-01-06 14:40:57 -0800174}
175
Matthew Wilcox51fbb4b2009-07-29 15:02:03 -0700176my $decodestart = Math::BigInt->from_hex("0x$target") - Math::BigInt->from_hex("0x$func_offset");
177my $decodestop = Math::BigInt->from_hex("0x$target") + 8192;
Arjan van de Ven5aea50b2009-01-06 14:40:57 -0800178if ($target eq "0") {
179 print "No oops found!\n";
180 print "Usage: \n";
181 print " dmesg | perl scripts/markup_oops.pl vmlinux\n";
182 exit;
183}
184
Arjan van de Vend32ad102009-01-11 15:03:23 +0000185# if it's a module, we need to find the .ko file and calculate a load offset
186if ($module ne "") {
187 my $dir = dirname($filename);
188 $dir = $dir . "/";
189 my $mod = $module . ".ko";
190 my $modulefile = `find $dir -name $mod | head -1`;
191 chomp($modulefile);
192 $filename = $modulefile;
193 if ($filename eq "") {
194 print "Module .ko file for $module not found. Aborting\n";
195 exit;
196 }
197 # ok so we found the module, now we need to calculate the vma offset
198 open(FILE, "objdump -dS $filename |") || die "Cannot start objdump";
199 while (<FILE>) {
200 if ($_ =~ /^([0-9a-f]+) \<$function\>\:/) {
201 my $fu = $1;
202 $vmaoffset = hex($target) - hex($fu) - hex($func_offset);
203 }
204 }
205 close(FILE);
206}
207
Arjan van de Ven5aea50b2009-01-06 14:40:57 -0800208my $counter = 0;
209my $state = 0;
210my $center = 0;
211my @lines;
Arjan van de Venc19ef7f2009-02-15 11:30:52 +0100212my @reglines;
Arjan van de Ven5aea50b2009-01-06 14:40:57 -0800213
214sub InRange {
215 my ($address, $target) = @_;
216 my $ad = "0x".$address;
217 my $ta = "0x".$target;
218 my $delta = hex($ad) - hex($ta);
219
220 if (($delta > -4096) && ($delta < 4096)) {
221 return 1;
222 }
223 return 0;
224}
225
226
227
228# first, parse the input into the lines array, but to keep size down,
229# we only do this for 4Kb around the sweet spot
230
Arjan van de Vend32ad102009-01-11 15:03:23 +0000231open(FILE, "objdump -dS --adjust-vma=$vmaoffset --start-address=$decodestart --stop-address=$decodestop $filename |") || die "Cannot start objdump";
Arjan van de Ven5aea50b2009-01-06 14:40:57 -0800232
233while (<FILE>) {
234 my $line = $_;
235 chomp($line);
236 if ($state == 0) {
237 if ($line =~ /^([a-f0-9]+)\:/) {
238 if (InRange($1, $target)) {
239 $state = 1;
240 }
241 }
242 } else {
243 if ($line =~ /^([a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]+)\:/) {
244 my $val = $1;
245 if (!InRange($val, $target)) {
246 last;
247 }
248 if ($val eq $target) {
249 $center = $counter;
250 }
251 }
252 $lines[$counter] = $line;
253
254 $counter = $counter + 1;
255 }
256}
257
258close(FILE);
259
260if ($counter == 0) {
261 print "No matching code found \n";
262 exit;
263}
264
265if ($center == 0) {
266 print "No matching code found \n";
267 exit;
268}
269
270my $start;
271my $finish;
272my $codelines = 0;
273my $binarylines = 0;
274# now we go up and down in the array to find how much we want to print
275
276$start = $center;
277
278while ($start > 1) {
279 $start = $start - 1;
280 my $line = $lines[$start];
281 if ($line =~ /^([a-f0-9]+)\:/) {
282 $binarylines = $binarylines + 1;
283 } else {
284 $codelines = $codelines + 1;
285 }
286 if ($codelines > 10) {
287 last;
288 }
289 if ($binarylines > 20) {
290 last;
291 }
292}
293
294
295$finish = $center;
296$codelines = 0;
297$binarylines = 0;
298while ($finish < $counter) {
299 $finish = $finish + 1;
300 my $line = $lines[$finish];
301 if ($line =~ /^([a-f0-9]+)\:/) {
302 $binarylines = $binarylines + 1;
303 } else {
304 $codelines = $codelines + 1;
305 }
306 if ($codelines > 10) {
307 last;
308 }
309 if ($binarylines > 20) {
310 last;
311 }
312}
313
314
315my $i;
316
Arjan van de Venc19ef7f2009-02-15 11:30:52 +0100317
318# start annotating the registers in the asm.
319# this goes from the oopsing point back, so that the annotator
320# can track (opportunistically) which registers got written and
321# whos value no longer is relevant.
322
323$i = $center;
324while ($i >= $start) {
325 $reglines[$i] = process_x86_regs($lines[$i], $center - $i);
326 $i = $i - 1;
Arjan van de Ven5aea50b2009-01-06 14:40:57 -0800327}
328
Arjan van de Venc19ef7f2009-02-15 11:30:52 +0100329$i = $start;
330while ($i < $finish) {
331 my $line;
332 if ($i == $center) {
333 $line = "*$lines[$i] ";
334 } else {
335 $line = " $lines[$i] ";
336 }
337 print $line;
338 if (defined($reglines[$i]) && length($reglines[$i]) > 0) {
339 my $c = 60 - length($line);
340 while ($c > 0) { print " "; $c = $c - 1; };
341 print "| $reglines[$i]";
342 }
343 if ($i == $center) {
344 print "<--- faulting instruction";
345 }
346 print "\n";
347 $i = $i +1;
348}
Arjan van de Ven5aea50b2009-01-06 14:40:57 -0800349