2 # SPDX-License-Identifier: GPL-2.0-only
8 # Copyright 2008, Intel Corporation
10 # This file is part of the Linux kernel
13 # Arjan van de Ven <arjan@linux.intel.com>
16 my $cross_compile = "";
17 my $vmlinux_name = "";
21 Getopt
::Long
::GetOptions
(
22 'cross-compile|c=s' => \
$cross_compile,
23 'module|m=s' => \
$modulefile,
26 my $vmlinux_name = $ARGV[0];
27 if (!defined($vmlinux_name)) {
28 my $kerver = `uname -r`;
30 $vmlinux_name = "/lib/modules/$kerver/build/vmlinux";
31 print "No vmlinux specified, assuming $vmlinux_name\n";
33 my $filename = $vmlinux_name;
35 # Parse the oops to find the EIP value
49 if ($line =~ /EAX: ([0-9a-f]+) EBX: ([0-9a-f]+) ECX: ([0-9a-f]+) EDX: ([0-9a-f]+)/) {
55 if ($line =~ /ESI: ([0-9a-f]+) EDI: ([0-9a-f]+) EBP: ([0-9a-f]+) ESP: ([0-9a-f]+)/) {
60 if ($line =~ /RAX: ([0-9a-f]+) RBX: ([0-9a-f]+) RCX: ([0-9a-f]+)/) {
65 if ($line =~ /RDX: ([0-9a-f]+) RSI: ([0-9a-f]+) RDI: ([0-9a-f]+)/) {
70 if ($line =~ /RBP: ([0-9a-f]+) R08: ([0-9a-f]+) R09: ([0-9a-f]+)/) {
74 if ($line =~ /R10: ([0-9a-f]+) R11: ([0-9a-f]+) R12: ([0-9a-f]+)/) {
79 if ($line =~ /R13: ([0-9a-f]+) R14: ([0-9a-f]+) R15: ([0-9a-f]+)/) {
89 $reg =~ s/r(.)x/e\1x/;
90 $reg =~ s/r(.)i/e\1i/;
91 $reg =~ s/r(.)p/e\1p/;
97 my ($line, $cntr) = @_;
99 if (length($line) < 40) {
100 return ""; # not an asm istruction
103 # find the arguments to the instruction
104 if ($line =~ /([0-9a-zA-Z\,\%\(\)\-\+]+)$/) {
110 # we need to find the registers that get clobbered,
111 # since their value is no longer relevant for previous
112 # instructions in the stream.
114 $clobber = $lastword;
115 # first, remove all memory operands, they're read only
116 $clobber =~ s/\([a-z0-9\%\,]+\)//g;
117 # then, remove everything before the comma, thats the read part
118 $clobber =~ s/.*\,//g;
120 # if this is the instruction that faulted, we haven't actually done
121 # the write yet... nothing is clobbered.
126 foreach $reg (keys(%regs)) {
127 my $clobberprime = reg_name
($clobber);
128 my $lastwordprime = reg_name
($lastword);
129 my $val = $regs{$reg};
130 if ($val =~ /^[0]+$/) {
136 # first check if we're clobbering this register; if we do
137 # we print it with a =>, and then delete its value
138 if ($clobber =~ /$reg/ || $clobberprime =~ /$reg/) {
139 if (length($val) > 0) {
140 $str = $str . " $reg => $val ";
145 # now check if we're reading this register
146 if ($lastword =~ /$reg/ || $lastwordprime =~ /$reg/) {
147 if (length($val) > 0) {
148 $str = $str . " $reg = $val ";
158 if ($line =~ /EIP: 0060:\[\<([a-z0-9]+)\>\]/) {
161 if ($line =~ /RIP: 0010:\[\<([a-z0-9]+)\>\]/) {
164 if ($line =~ /EIP is at ([a-zA-Z0-9\_]+)\+0x([0-9a-f]+)\/0x
[a
-f0
-9]/) {
168 if ($line =~ /RIP: 0010:\[\<[0-9a-f]+\>\] \[\<[0-9a-f]+\>\] ([a-zA-Z0-9\_]+)\+0x([0-9a-f]+)\/0x
[a
-f0
-9]/) {
173 # check if it's a module
174 if ($line =~ /EIP is at ([a-zA-Z0-9\_]+)\+(0x[0-9a-f]+)\/0x
[a
-f0
-9]+\W\
[([a
-zA
-Z0
-9\_\
-]+)\
]/) {
177 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\_\
-]+)\
]/) {
180 parse_x86_regs
($line);
183 my $decodestart = Math
::BigInt
->from_hex("0x$target") - Math
::BigInt
->from_hex("0x$func_offset");
184 my $decodestop = Math
::BigInt
->from_hex("0x$target") + 8192;
185 if ($target eq "0") {
186 print "No oops found!\n";
190 # if it's a module, we need to find the .ko file and calculate a load offset
192 if ($modulefile eq "") {
193 $modulefile = `modinfo -F filename $module`;
196 $filename = $modulefile;
197 if ($filename eq "") {
198 print "Module .ko file for $module not found. Aborting\n";
201 # ok so we found the module, now we need to calculate the vma offset
202 open(FILE
, $cross_compile."objdump -dS $filename |") || die "Cannot start objdump";
204 if ($_ =~ /^([0-9a-f]+) \<$function\>\:/) {
206 $vmaoffset = Math
::BigInt
->from_hex("0x$target") - Math
::BigInt
->from_hex("0x$fu") - Math
::BigInt
->from_hex("0x$func_offset");
219 my ($address, $target) = @_;
220 my $ad = "0x".$address;
221 my $ta = "0x".$target;
222 my $delta = Math
::BigInt
->from_hex($ad) - Math
::BigInt
->from_hex($ta);
224 if (($delta > -4096) && ($delta < 4096)) {
232 # first, parse the input into the lines array, but to keep size down,
233 # we only do this for 4Kb around the sweet spot
235 open(FILE
, $cross_compile."objdump -dS --adjust-vma=$vmaoffset --start-address=$decodestart --stop-address=$decodestop $filename |") || die "Cannot start objdump";
241 if ($line =~ /^([a-f0-9]+)\:/) {
242 if (InRange
($1, $target)) {
248 if ($line =~ /^([a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]+)\:/) {
250 if (!InRange
($val, $target)) {
253 if ($val eq $target) {
257 $lines[$counter] = $line;
259 $counter = $counter + 1;
266 print "No matching code found \n";
271 print "No matching code found \n";
279 # now we go up and down in the array to find how much we want to print
285 my $line = $lines[$start];
286 if ($line =~ /^([a-f0-9]+)\:/) {
287 $binarylines = $binarylines + 1;
289 $codelines = $codelines + 1;
291 if ($codelines > 10) {
294 if ($binarylines > 20) {
303 while ($finish < $counter) {
304 $finish = $finish + 1;
305 my $line = $lines[$finish];
306 if ($line =~ /^([a-f0-9]+)\:/) {
307 $binarylines = $binarylines + 1;
309 $codelines = $codelines + 1;
311 if ($codelines > 10) {
314 if ($binarylines > 20) {
323 # start annotating the registers in the asm.
324 # this goes from the oopsing point back, so that the annotator
325 # can track (opportunistically) which registers got written and
326 # whos value no longer is relevant.
329 while ($i >= $start) {
330 $reglines[$i] = process_x86_regs
($lines[$i], $center - $i);
335 while ($i < $finish) {
338 $line = "*$lines[$i] ";
340 $line = " $lines[$i] ";
343 if (defined($reglines[$i]) && length($reglines[$i]) > 0) {
344 my $c = 60 - length($line);
345 while ($c > 0) { print " "; $c = $c - 1; };
346 print "| $reglines[$i]";
349 print "<--- faulting instruction";
358 dmesg | perl $0 [OPTION] [VMLINUX]
361 -c, --cross-compile CROSS_COMPILE Specify the prefix used for toolchain.
362 -m, --module MODULE_DIRNAME Specify the module filename.