6 # Copyright 2008, Intel Corporation
8 # This file is part of the Linux kernel
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.
15 # Arjan van de Ven <arjan@linux.intel.com>
18 my $vmlinux_name = $ARGV[0];
19 if (!defined($vmlinux_name)) {
20 my $kerver = `uname -r`;
22 $vmlinux_name = "/lib/modules/$kerver/build/vmlinux";
23 print "No vmlinux specified, assuming $vmlinux_name\n";
25 my $filename = $vmlinux_name;
27 # Step 1: Parse the oops to find the EIP value
42 if ($line =~ /EAX: ([0-9a-f]+) EBX: ([0-9a-f]+) ECX: ([0-9a-f]+) EDX: ([0-9a-f]+)/) {
48 if ($line =~ /ESI: ([0-9a-f]+) EDI: ([0-9a-f]+) EBP: ([0-9a-f]+) ESP: ([0-9a-f]+)/) {
53 if ($line =~ /RAX: ([0-9a-f]+) RBX: ([0-9a-f]+) RCX: ([0-9a-f]+)/) {
58 if ($line =~ /RDX: ([0-9a-f]+) RSI: ([0-9a-f]+) RDI: ([0-9a-f]+)/) {
63 if ($line =~ /RBP: ([0-9a-f]+) R08: ([0-9a-f]+) R09: ([0-9a-f]+)/) {
67 if ($line =~ /R10: ([0-9a-f]+) R11: ([0-9a-f]+) R12: ([0-9a-f]+)/) {
72 if ($line =~ /R13: ([0-9a-f]+) R14: ([0-9a-f]+) R15: ([0-9a-f]+)/) {
82 $reg =~ s/r(.)x/e\1x/;
83 $reg =~ s/r(.)i/e\1i/;
84 $reg =~ s/r(.)p/e\1p/;
90 my ($line, $cntr) = @_;
92 if (length($line) < 40) {
93 return ""; # not an asm istruction
96 # find the arguments to the instruction
97 if ($line =~ /([0-9a-zA-Z\,\%\(\)\-\+]+)$/) {
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.
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;
113 # if this is the instruction that faulted, we haven't actually done
114 # the write yet... nothing is clobbered.
119 foreach $reg (keys(%regs)) {
120 my $clobberprime = reg_name
($clobber);
121 my $lastwordprime = reg_name
($lastword);
122 my $val = $regs{$reg};
123 if ($val =~ /^[0]+$/) {
129 # first check if we're clobbering this register; if we do
130 # we print it with a =>, and then delete its value
131 if ($clobber =~ /$reg/ || $clobberprime =~ /$reg/) {
132 if (length($val) > 0) {
133 $str = $str . " $reg => $val ";
138 # now check if we're reading this register
139 if ($lastword =~ /$reg/ || $lastwordprime =~ /$reg/) {
140 if (length($val) > 0) {
141 $str = $str . " $reg = $val ";
151 if ($line =~ /EIP: 0060:\[\<([a-z0-9]+)\>\]/) {
154 if ($line =~ /RIP: 0010:\[\<([a-z0-9]+)\>\]/) {
157 if ($line =~ /EIP is at ([a-zA-Z0-9\_]+)\+(0x[0-9a-f]+)\/0x
[a
-f0
-9]/) {
161 if ($line =~ /RIP: 0010:\[\<[0-9a-f]+\>\] \[\<[0-9a-f]+\>\] ([a-zA-Z0-9\_]+)\+(0x[0-9a-f]+)\/0x
[a
-f0
-9]/) {
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\_\
-]+)\
]/) {
170 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\_\
-]+)\
]/) {
173 parse_x86_regs
($line);
176 my $decodestart = Math
::BigInt
->from_hex("0x$target") - Math
::BigInt
->from_hex("0x$func_offset");
177 my $decodestop = Math
::BigInt
->from_hex("0x$target") + 8192;
178 if ($target eq "0") {
179 print "No oops found!\n";
181 print " dmesg | perl scripts/markup_oops.pl vmlinux\n";
185 # if it's a module, we need to find the .ko file and calculate a load offset
187 my $dir = dirname
($filename);
189 my $mod = $module . ".ko";
190 my $modulefile = `find $dir -name $mod | head -1`;
192 $filename = $modulefile;
193 if ($filename eq "") {
194 print "Module .ko file for $module not found. Aborting\n";
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";
200 if ($_ =~ /^([0-9a-f]+) \<$function\>\:/) {
202 $vmaoffset = hex($target) - hex($fu) - hex($func_offset);
215 my ($address, $target) = @_;
216 my $ad = "0x".$address;
217 my $ta = "0x".$target;
218 my $delta = hex($ad) - hex($ta);
220 if (($delta > -4096) && ($delta < 4096)) {
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
231 open(FILE
, "objdump -dS --adjust-vma=$vmaoffset --start-address=$decodestart --stop-address=$decodestop $filename |") || die "Cannot start objdump";
237 if ($line =~ /^([a-f0-9]+)\:/) {
238 if (InRange
($1, $target)) {
243 if ($line =~ /^([a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]+)\:/) {
245 if (!InRange
($val, $target)) {
248 if ($val eq $target) {
252 $lines[$counter] = $line;
254 $counter = $counter + 1;
261 print "No matching code found \n";
266 print "No matching code found \n";
274 # now we go up and down in the array to find how much we want to print
280 my $line = $lines[$start];
281 if ($line =~ /^([a-f0-9]+)\:/) {
282 $binarylines = $binarylines + 1;
284 $codelines = $codelines + 1;
286 if ($codelines > 10) {
289 if ($binarylines > 20) {
298 while ($finish < $counter) {
299 $finish = $finish + 1;
300 my $line = $lines[$finish];
301 if ($line =~ /^([a-f0-9]+)\:/) {
302 $binarylines = $binarylines + 1;
304 $codelines = $codelines + 1;
306 if ($codelines > 10) {
309 if ($binarylines > 20) {
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.
324 while ($i >= $start) {
325 $reglines[$i] = process_x86_regs
($lines[$i], $center - $i);
330 while ($i < $finish) {
333 $line = "*$lines[$i] ";
335 $line = " $lines[$i] ";
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]";
344 print "<--- faulting instruction";