7 # Copyright 2008, Intel Corporation
9 # This file is part of the Linux kernel
11 # This program file is free software; you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by the
13 # Free Software Foundation; version 2 of the License.
16 # Arjan van de Ven <arjan@linux.intel.com>
19 my $cross_compile = "";
20 my $vmlinux_name = "";
24 Getopt
::Long
::GetOptions
(
25 'cross-compile|c=s' => \
$cross_compile,
26 'module|m=s' => \
$modulefile,
29 my $vmlinux_name = $ARGV[0];
30 if (!defined($vmlinux_name)) {
31 my $kerver = `uname -r`;
33 $vmlinux_name = "/lib/modules/$kerver/build/vmlinux";
34 print "No vmlinux specified, assuming $vmlinux_name\n";
36 my $filename = $vmlinux_name;
38 # Parse the oops to find the EIP value
52 if ($line =~ /EAX: ([0-9a-f]+) EBX: ([0-9a-f]+) ECX: ([0-9a-f]+) EDX: ([0-9a-f]+)/) {
58 if ($line =~ /ESI: ([0-9a-f]+) EDI: ([0-9a-f]+) EBP: ([0-9a-f]+) ESP: ([0-9a-f]+)/) {
63 if ($line =~ /RAX: ([0-9a-f]+) RBX: ([0-9a-f]+) RCX: ([0-9a-f]+)/) {
68 if ($line =~ /RDX: ([0-9a-f]+) RSI: ([0-9a-f]+) RDI: ([0-9a-f]+)/) {
73 if ($line =~ /RBP: ([0-9a-f]+) R08: ([0-9a-f]+) R09: ([0-9a-f]+)/) {
77 if ($line =~ /R10: ([0-9a-f]+) R11: ([0-9a-f]+) R12: ([0-9a-f]+)/) {
82 if ($line =~ /R13: ([0-9a-f]+) R14: ([0-9a-f]+) R15: ([0-9a-f]+)/) {
92 $reg =~ s/r(.)x/e\1x/;
93 $reg =~ s/r(.)i/e\1i/;
94 $reg =~ s/r(.)p/e\1p/;
100 my ($line, $cntr) = @_;
102 if (length($line) < 40) {
103 return ""; # not an asm istruction
106 # find the arguments to the instruction
107 if ($line =~ /([0-9a-zA-Z\,\%\(\)\-\+]+)$/) {
113 # we need to find the registers that get clobbered,
114 # since their value is no longer relevant for previous
115 # instructions in the stream.
117 $clobber = $lastword;
118 # first, remove all memory operands, they're read only
119 $clobber =~ s/\([a-z0-9\%\,]+\)//g;
120 # then, remove everything before the comma, thats the read part
121 $clobber =~ s/.*\,//g;
123 # if this is the instruction that faulted, we haven't actually done
124 # the write yet... nothing is clobbered.
129 foreach $reg (keys(%regs)) {
130 my $clobberprime = reg_name
($clobber);
131 my $lastwordprime = reg_name
($lastword);
132 my $val = $regs{$reg};
133 if ($val =~ /^[0]+$/) {
139 # first check if we're clobbering this register; if we do
140 # we print it with a =>, and then delete its value
141 if ($clobber =~ /$reg/ || $clobberprime =~ /$reg/) {
142 if (length($val) > 0) {
143 $str = $str . " $reg => $val ";
148 # now check if we're reading this register
149 if ($lastword =~ /$reg/ || $lastwordprime =~ /$reg/) {
150 if (length($val) > 0) {
151 $str = $str . " $reg = $val ";
161 if ($line =~ /EIP: 0060:\[\<([a-z0-9]+)\>\]/) {
164 if ($line =~ /RIP: 0010:\[\<([a-z0-9]+)\>\]/) {
167 if ($line =~ /EIP is at ([a-zA-Z0-9\_]+)\+0x([0-9a-f]+)\/0x
[a
-f0
-9]/) {
171 if ($line =~ /RIP: 0010:\[\<[0-9a-f]+\>\] \[\<[0-9a-f]+\>\] ([a-zA-Z0-9\_]+)\+0x([0-9a-f]+)\/0x
[a
-f0
-9]/) {
176 # check if it's a module
177 if ($line =~ /EIP is at ([a-zA-Z0-9\_]+)\+(0x[0-9a-f]+)\/0x
[a
-f0
-9]+\W\
[([a
-zA
-Z0
-9\_\
-]+)\
]/) {
180 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\_\
-]+)\
]/) {
183 parse_x86_regs
($line);
186 my $decodestart = Math
::BigInt
->from_hex("0x$target") - Math
::BigInt
->from_hex("0x$func_offset");
187 my $decodestop = Math
::BigInt
->from_hex("0x$target") + 8192;
188 if ($target eq "0") {
189 print "No oops found!\n";
193 # if it's a module, we need to find the .ko file and calculate a load offset
195 if ($modulefile eq "") {
196 $modulefile = `modinfo -F filename $module`;
199 $filename = $modulefile;
200 if ($filename eq "") {
201 print "Module .ko file for $module not found. Aborting\n";
204 # ok so we found the module, now we need to calculate the vma offset
205 open(FILE
, $cross_compile."objdump -dS $filename |") || die "Cannot start objdump";
207 if ($_ =~ /^([0-9a-f]+) \<$function\>\:/) {
209 $vmaoffset = Math
::BigInt
->from_hex("0x$target") - Math
::BigInt
->from_hex("0x$fu") - Math
::BigInt
->from_hex("0x$func_offset");
222 my ($address, $target) = @_;
223 my $ad = "0x".$address;
224 my $ta = "0x".$target;
225 my $delta = Math
::BigInt
->from_hex($ad) - Math
::BigInt
->from_hex($ta);
227 if (($delta > -4096) && ($delta < 4096)) {
235 # first, parse the input into the lines array, but to keep size down,
236 # we only do this for 4Kb around the sweet spot
238 open(FILE
, $cross_compile."objdump -dS --adjust-vma=$vmaoffset --start-address=$decodestart --stop-address=$decodestop $filename |") || die "Cannot start objdump";
244 if ($line =~ /^([a-f0-9]+)\:/) {
245 if (InRange
($1, $target)) {
251 if ($line =~ /^([a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]+)\:/) {
253 if (!InRange
($val, $target)) {
256 if ($val eq $target) {
260 $lines[$counter] = $line;
262 $counter = $counter + 1;
269 print "No matching code found \n";
274 print "No matching code found \n";
282 # now we go up and down in the array to find how much we want to print
288 my $line = $lines[$start];
289 if ($line =~ /^([a-f0-9]+)\:/) {
290 $binarylines = $binarylines + 1;
292 $codelines = $codelines + 1;
294 if ($codelines > 10) {
297 if ($binarylines > 20) {
306 while ($finish < $counter) {
307 $finish = $finish + 1;
308 my $line = $lines[$finish];
309 if ($line =~ /^([a-f0-9]+)\:/) {
310 $binarylines = $binarylines + 1;
312 $codelines = $codelines + 1;
314 if ($codelines > 10) {
317 if ($binarylines > 20) {
326 # start annotating the registers in the asm.
327 # this goes from the oopsing point back, so that the annotator
328 # can track (opportunistically) which registers got written and
329 # whos value no longer is relevant.
332 while ($i >= $start) {
333 $reglines[$i] = process_x86_regs
($lines[$i], $center - $i);
338 while ($i < $finish) {
341 $line = "*$lines[$i] ";
343 $line = " $lines[$i] ";
346 if (defined($reglines[$i]) && length($reglines[$i]) > 0) {
347 my $c = 60 - length($line);
348 while ($c > 0) { print " "; $c = $c - 1; };
349 print "| $reglines[$i]";
352 print "<--- faulting instruction";
361 dmesg | perl $0 [OPTION] [VMLINUX]
364 -c, --cross-compile CROSS_COMPILE Specify the prefix used for toolchain.
365 -m, --module MODULE_DIRNAME Specify the module filename.