Kamil Rytarowski | cb77f0d | 2017-05-07 23:25:26 +0200 | [diff] [blame] | 1 | #!/usr/bin/env perl |
Thomas Gleixner | 76e692f | 2019-05-31 01:09:33 -0700 | [diff] [blame] | 2 | # SPDX-License-Identifier: GPL-2.0-only |
Arjan van de Ven | 5aea50b | 2009-01-06 14:40:57 -0800 | [diff] [blame] | 3 | |
Arjan van de Ven | d32ad10 | 2009-01-11 15:03:23 +0000 | [diff] [blame] | 4 | use File::Basename; |
Matthew Wilcox | 51fbb4b | 2009-07-29 15:02:03 -0700 | [diff] [blame] | 5 | use Math::BigInt; |
Hui Zhu | 52e13e2 | 2010-01-26 17:13:07 +0800 | [diff] [blame] | 6 | use Getopt::Long; |
Arjan van de Ven | d32ad10 | 2009-01-11 15:03:23 +0000 | [diff] [blame] | 7 | |
Arjan van de Ven | 5aea50b | 2009-01-06 14:40:57 -0800 | [diff] [blame] | 8 | # Copyright 2008, Intel Corporation |
| 9 | # |
| 10 | # This file is part of the Linux kernel |
| 11 | # |
Arjan van de Ven | 5aea50b | 2009-01-06 14:40:57 -0800 | [diff] [blame] | 12 | # Authors: |
| 13 | # Arjan van de Ven <arjan@linux.intel.com> |
| 14 | |
| 15 | |
Hui Zhu | 52e13e2 | 2010-01-26 17:13:07 +0800 | [diff] [blame] | 16 | my $cross_compile = ""; |
| 17 | my $vmlinux_name = ""; |
| 18 | my $modulefile = ""; |
| 19 | |
| 20 | # Get options |
| 21 | Getopt::Long::GetOptions( |
| 22 | 'cross-compile|c=s' => \$cross_compile, |
Hui Zhu | 59dde38 | 2010-02-01 13:41:22 +0800 | [diff] [blame] | 23 | 'module|m=s' => \$modulefile, |
Hui Zhu | 52e13e2 | 2010-01-26 17:13:07 +0800 | [diff] [blame] | 24 | 'help|h' => \&usage, |
Hui Zhu | 59dde38 | 2010-02-01 13:41:22 +0800 | [diff] [blame] | 25 | ) || usage (); |
Arjan van de Ven | 5aea50b | 2009-01-06 14:40:57 -0800 | [diff] [blame] | 26 | my $vmlinux_name = $ARGV[0]; |
Arjan van de Ven | d32ad10 | 2009-01-11 15:03:23 +0000 | [diff] [blame] | 27 | if (!defined($vmlinux_name)) { |
| 28 | my $kerver = `uname -r`; |
| 29 | chomp($kerver); |
| 30 | $vmlinux_name = "/lib/modules/$kerver/build/vmlinux"; |
| 31 | print "No vmlinux specified, assuming $vmlinux_name\n"; |
| 32 | } |
| 33 | my $filename = $vmlinux_name; |
Hui Zhu | 52e13e2 | 2010-01-26 17:13:07 +0800 | [diff] [blame] | 34 | |
| 35 | # Parse the oops to find the EIP value |
Arjan van de Ven | 5aea50b | 2009-01-06 14:40:57 -0800 | [diff] [blame] | 36 | |
| 37 | my $target = "0"; |
Arjan van de Ven | d32ad10 | 2009-01-11 15:03:23 +0000 | [diff] [blame] | 38 | my $function; |
| 39 | my $module = ""; |
Arjan van de Ven | 11df65c | 2009-02-15 11:30:55 +0100 | [diff] [blame] | 40 | my $func_offset = 0; |
Arjan van de Ven | d32ad10 | 2009-01-11 15:03:23 +0000 | [diff] [blame] | 41 | my $vmaoffset = 0; |
| 42 | |
Arjan van de Ven | c19ef7f | 2009-02-15 11:30:52 +0100 | [diff] [blame] | 43 | my %regs; |
| 44 | |
| 45 | |
| 46 | sub parse_x86_regs |
| 47 | { |
| 48 | my ($line) = @_; |
| 49 | if ($line =~ /EAX: ([0-9a-f]+) EBX: ([0-9a-f]+) ECX: ([0-9a-f]+) EDX: ([0-9a-f]+)/) { |
| 50 | $regs{"%eax"} = $1; |
| 51 | $regs{"%ebx"} = $2; |
| 52 | $regs{"%ecx"} = $3; |
| 53 | $regs{"%edx"} = $4; |
| 54 | } |
| 55 | if ($line =~ /ESI: ([0-9a-f]+) EDI: ([0-9a-f]+) EBP: ([0-9a-f]+) ESP: ([0-9a-f]+)/) { |
| 56 | $regs{"%esi"} = $1; |
| 57 | $regs{"%edi"} = $2; |
| 58 | $regs{"%esp"} = $4; |
| 59 | } |
Arjan van de Ven | 11df65c | 2009-02-15 11:30:55 +0100 | [diff] [blame] | 60 | if ($line =~ /RAX: ([0-9a-f]+) RBX: ([0-9a-f]+) RCX: ([0-9a-f]+)/) { |
| 61 | $regs{"%eax"} = $1; |
| 62 | $regs{"%ebx"} = $2; |
| 63 | $regs{"%ecx"} = $3; |
| 64 | } |
| 65 | if ($line =~ /RDX: ([0-9a-f]+) RSI: ([0-9a-f]+) RDI: ([0-9a-f]+)/) { |
| 66 | $regs{"%edx"} = $1; |
| 67 | $regs{"%esi"} = $2; |
| 68 | $regs{"%edi"} = $3; |
| 69 | } |
| 70 | if ($line =~ /RBP: ([0-9a-f]+) R08: ([0-9a-f]+) R09: ([0-9a-f]+)/) { |
| 71 | $regs{"%r08"} = $2; |
| 72 | $regs{"%r09"} = $3; |
| 73 | } |
| 74 | if ($line =~ /R10: ([0-9a-f]+) R11: ([0-9a-f]+) R12: ([0-9a-f]+)/) { |
| 75 | $regs{"%r10"} = $1; |
| 76 | $regs{"%r11"} = $2; |
| 77 | $regs{"%r12"} = $3; |
| 78 | } |
| 79 | if ($line =~ /R13: ([0-9a-f]+) R14: ([0-9a-f]+) R15: ([0-9a-f]+)/) { |
| 80 | $regs{"%r13"} = $1; |
| 81 | $regs{"%r14"} = $2; |
| 82 | $regs{"%r15"} = $3; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | sub reg_name |
| 87 | { |
| 88 | my ($reg) = @_; |
| 89 | $reg =~ s/r(.)x/e\1x/; |
| 90 | $reg =~ s/r(.)i/e\1i/; |
| 91 | $reg =~ s/r(.)p/e\1p/; |
| 92 | return $reg; |
Arjan van de Ven | c19ef7f | 2009-02-15 11:30:52 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | sub process_x86_regs |
| 96 | { |
| 97 | my ($line, $cntr) = @_; |
| 98 | my $str = ""; |
| 99 | if (length($line) < 40) { |
| 100 | return ""; # not an asm istruction |
| 101 | } |
| 102 | |
| 103 | # find the arguments to the instruction |
| 104 | if ($line =~ /([0-9a-zA-Z\,\%\(\)\-\+]+)$/) { |
| 105 | $lastword = $1; |
| 106 | } else { |
| 107 | return ""; |
| 108 | } |
| 109 | |
| 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. |
| 113 | |
| 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; |
| 119 | |
| 120 | # if this is the instruction that faulted, we haven't actually done |
| 121 | # the write yet... nothing is clobbered. |
| 122 | if ($cntr == 0) { |
| 123 | $clobber = ""; |
| 124 | } |
| 125 | |
| 126 | foreach $reg (keys(%regs)) { |
Arjan van de Ven | 11df65c | 2009-02-15 11:30:55 +0100 | [diff] [blame] | 127 | my $clobberprime = reg_name($clobber); |
| 128 | my $lastwordprime = reg_name($lastword); |
Arjan van de Ven | c19ef7f | 2009-02-15 11:30:52 +0100 | [diff] [blame] | 129 | my $val = $regs{$reg}; |
Arjan van de Ven | 11df65c | 2009-02-15 11:30:55 +0100 | [diff] [blame] | 130 | if ($val =~ /^[0]+$/) { |
| 131 | $val = "0"; |
| 132 | } else { |
| 133 | $val =~ s/^0*//; |
| 134 | } |
| 135 | |
Arjan van de Ven | c19ef7f | 2009-02-15 11:30:52 +0100 | [diff] [blame] | 136 | # first check if we're clobbering this register; if we do |
| 137 | # we print it with a =>, and then delete its value |
Arjan van de Ven | 11df65c | 2009-02-15 11:30:55 +0100 | [diff] [blame] | 138 | if ($clobber =~ /$reg/ || $clobberprime =~ /$reg/) { |
Arjan van de Ven | c19ef7f | 2009-02-15 11:30:52 +0100 | [diff] [blame] | 139 | if (length($val) > 0) { |
| 140 | $str = $str . " $reg => $val "; |
| 141 | } |
| 142 | $regs{$reg} = ""; |
| 143 | $val = ""; |
| 144 | } |
| 145 | # now check if we're reading this register |
Arjan van de Ven | 11df65c | 2009-02-15 11:30:55 +0100 | [diff] [blame] | 146 | if ($lastword =~ /$reg/ || $lastwordprime =~ /$reg/) { |
Arjan van de Ven | c19ef7f | 2009-02-15 11:30:52 +0100 | [diff] [blame] | 147 | if (length($val) > 0) { |
| 148 | $str = $str . " $reg = $val "; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | return $str; |
| 153 | } |
| 154 | |
| 155 | # parse the oops |
Arjan van de Ven | 5aea50b | 2009-01-06 14:40:57 -0800 | [diff] [blame] | 156 | while (<STDIN>) { |
Arjan van de Ven | d32ad10 | 2009-01-11 15:03:23 +0000 | [diff] [blame] | 157 | my $line = $_; |
| 158 | if ($line =~ /EIP: 0060:\[\<([a-z0-9]+)\>\]/) { |
Arjan van de Ven | 5aea50b | 2009-01-06 14:40:57 -0800 | [diff] [blame] | 159 | $target = $1; |
| 160 | } |
Arjan van de Ven | 11df65c | 2009-02-15 11:30:55 +0100 | [diff] [blame] | 161 | if ($line =~ /RIP: 0010:\[\<([a-z0-9]+)\>\]/) { |
| 162 | $target = $1; |
| 163 | } |
Hui Zhu | 1f8cdae | 2010-01-15 17:01:07 -0800 | [diff] [blame] | 164 | if ($line =~ /EIP is at ([a-zA-Z0-9\_]+)\+0x([0-9a-f]+)\/0x[a-f0-9]/) { |
Arjan van de Ven | d32ad10 | 2009-01-11 15:03:23 +0000 | [diff] [blame] | 165 | $function = $1; |
| 166 | $func_offset = $2; |
| 167 | } |
Hui Zhu | ef2b9b0 | 2010-02-02 13:44:09 -0800 | [diff] [blame] | 168 | if ($line =~ /RIP: 0010:\[\<[0-9a-f]+\>\] \[\<[0-9a-f]+\>\] ([a-zA-Z0-9\_]+)\+0x([0-9a-f]+)\/0x[a-f0-9]/) { |
Arjan van de Ven | 11df65c | 2009-02-15 11:30:55 +0100 | [diff] [blame] | 169 | $function = $1; |
| 170 | $func_offset = $2; |
| 171 | } |
Arjan van de Ven | d32ad10 | 2009-01-11 15:03:23 +0000 | [diff] [blame] | 172 | |
| 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\_\-]+)\]/) { |
| 175 | $module = $3; |
| 176 | } |
Arjan van de Ven | 11df65c | 2009-02-15 11:30:55 +0100 | [diff] [blame] | 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\_\-]+)\]/) { |
| 178 | $module = $3; |
| 179 | } |
Arjan van de Ven | c19ef7f | 2009-02-15 11:30:52 +0100 | [diff] [blame] | 180 | parse_x86_regs($line); |
Arjan van de Ven | 5aea50b | 2009-01-06 14:40:57 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Matthew Wilcox | 51fbb4b | 2009-07-29 15:02:03 -0700 | [diff] [blame] | 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; |
Arjan van de Ven | 5aea50b | 2009-01-06 14:40:57 -0800 | [diff] [blame] | 185 | if ($target eq "0") { |
| 186 | print "No oops found!\n"; |
Hui Zhu | 52e13e2 | 2010-01-26 17:13:07 +0800 | [diff] [blame] | 187 | usage(); |
Arjan van de Ven | 5aea50b | 2009-01-06 14:40:57 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Arjan van de Ven | d32ad10 | 2009-01-11 15:03:23 +0000 | [diff] [blame] | 190 | # if it's a module, we need to find the .ko file and calculate a load offset |
| 191 | if ($module ne "") { |
Hui Zhu | 52e13e2 | 2010-01-26 17:13:07 +0800 | [diff] [blame] | 192 | if ($modulefile eq "") { |
Hui Zhu | 59dde38 | 2010-02-01 13:41:22 +0800 | [diff] [blame] | 193 | $modulefile = `modinfo -F filename $module`; |
Hui Zhu | 52e13e2 | 2010-01-26 17:13:07 +0800 | [diff] [blame] | 194 | chomp($modulefile); |
| 195 | } |
Arjan van de Ven | d32ad10 | 2009-01-11 15:03:23 +0000 | [diff] [blame] | 196 | $filename = $modulefile; |
| 197 | if ($filename eq "") { |
| 198 | print "Module .ko file for $module not found. Aborting\n"; |
| 199 | exit; |
| 200 | } |
| 201 | # ok so we found the module, now we need to calculate the vma offset |
Hui Zhu | 52e13e2 | 2010-01-26 17:13:07 +0800 | [diff] [blame] | 202 | open(FILE, $cross_compile."objdump -dS $filename |") || die "Cannot start objdump"; |
Arjan van de Ven | d32ad10 | 2009-01-11 15:03:23 +0000 | [diff] [blame] | 203 | while (<FILE>) { |
| 204 | if ($_ =~ /^([0-9a-f]+) \<$function\>\:/) { |
| 205 | my $fu = $1; |
Hui Zhu | 52e13e2 | 2010-01-26 17:13:07 +0800 | [diff] [blame] | 206 | $vmaoffset = Math::BigInt->from_hex("0x$target") - Math::BigInt->from_hex("0x$fu") - Math::BigInt->from_hex("0x$func_offset"); |
Arjan van de Ven | d32ad10 | 2009-01-11 15:03:23 +0000 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | close(FILE); |
| 210 | } |
| 211 | |
Arjan van de Ven | 5aea50b | 2009-01-06 14:40:57 -0800 | [diff] [blame] | 212 | my $counter = 0; |
| 213 | my $state = 0; |
Hui Zhu | 0139f1d | 2010-01-28 06:58:02 +0000 | [diff] [blame] | 214 | my $center = -1; |
Arjan van de Ven | 5aea50b | 2009-01-06 14:40:57 -0800 | [diff] [blame] | 215 | my @lines; |
Arjan van de Ven | c19ef7f | 2009-02-15 11:30:52 +0100 | [diff] [blame] | 216 | my @reglines; |
Arjan van de Ven | 5aea50b | 2009-01-06 14:40:57 -0800 | [diff] [blame] | 217 | |
| 218 | sub InRange { |
| 219 | my ($address, $target) = @_; |
| 220 | my $ad = "0x".$address; |
| 221 | my $ta = "0x".$target; |
Hui Zhu | 52e13e2 | 2010-01-26 17:13:07 +0800 | [diff] [blame] | 222 | my $delta = Math::BigInt->from_hex($ad) - Math::BigInt->from_hex($ta); |
Arjan van de Ven | 5aea50b | 2009-01-06 14:40:57 -0800 | [diff] [blame] | 223 | |
| 224 | if (($delta > -4096) && ($delta < 4096)) { |
| 225 | return 1; |
| 226 | } |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | |
| 231 | |
| 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 |
| 234 | |
Hui Zhu | 52e13e2 | 2010-01-26 17:13:07 +0800 | [diff] [blame] | 235 | open(FILE, $cross_compile."objdump -dS --adjust-vma=$vmaoffset --start-address=$decodestart --stop-address=$decodestop $filename |") || die "Cannot start objdump"; |
Arjan van de Ven | 5aea50b | 2009-01-06 14:40:57 -0800 | [diff] [blame] | 236 | |
| 237 | while (<FILE>) { |
| 238 | my $line = $_; |
| 239 | chomp($line); |
| 240 | if ($state == 0) { |
| 241 | if ($line =~ /^([a-f0-9]+)\:/) { |
| 242 | if (InRange($1, $target)) { |
| 243 | $state = 1; |
| 244 | } |
| 245 | } |
Hui Zhu | 0139f1d | 2010-01-28 06:58:02 +0000 | [diff] [blame] | 246 | } |
| 247 | if ($state == 1) { |
Arjan van de Ven | 5aea50b | 2009-01-06 14:40:57 -0800 | [diff] [blame] | 248 | if ($line =~ /^([a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]+)\:/) { |
| 249 | my $val = $1; |
| 250 | if (!InRange($val, $target)) { |
| 251 | last; |
| 252 | } |
| 253 | if ($val eq $target) { |
| 254 | $center = $counter; |
| 255 | } |
| 256 | } |
| 257 | $lines[$counter] = $line; |
| 258 | |
| 259 | $counter = $counter + 1; |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | close(FILE); |
| 264 | |
| 265 | if ($counter == 0) { |
| 266 | print "No matching code found \n"; |
| 267 | exit; |
| 268 | } |
| 269 | |
Hui Zhu | 0139f1d | 2010-01-28 06:58:02 +0000 | [diff] [blame] | 270 | if ($center == -1) { |
Arjan van de Ven | 5aea50b | 2009-01-06 14:40:57 -0800 | [diff] [blame] | 271 | print "No matching code found \n"; |
| 272 | exit; |
| 273 | } |
| 274 | |
| 275 | my $start; |
| 276 | my $finish; |
| 277 | my $codelines = 0; |
| 278 | my $binarylines = 0; |
| 279 | # now we go up and down in the array to find how much we want to print |
| 280 | |
| 281 | $start = $center; |
| 282 | |
| 283 | while ($start > 1) { |
| 284 | $start = $start - 1; |
| 285 | my $line = $lines[$start]; |
| 286 | if ($line =~ /^([a-f0-9]+)\:/) { |
| 287 | $binarylines = $binarylines + 1; |
| 288 | } else { |
| 289 | $codelines = $codelines + 1; |
| 290 | } |
| 291 | if ($codelines > 10) { |
| 292 | last; |
| 293 | } |
| 294 | if ($binarylines > 20) { |
| 295 | last; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | |
| 300 | $finish = $center; |
| 301 | $codelines = 0; |
| 302 | $binarylines = 0; |
| 303 | while ($finish < $counter) { |
| 304 | $finish = $finish + 1; |
| 305 | my $line = $lines[$finish]; |
| 306 | if ($line =~ /^([a-f0-9]+)\:/) { |
| 307 | $binarylines = $binarylines + 1; |
| 308 | } else { |
| 309 | $codelines = $codelines + 1; |
| 310 | } |
| 311 | if ($codelines > 10) { |
| 312 | last; |
| 313 | } |
| 314 | if ($binarylines > 20) { |
| 315 | last; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | |
| 320 | my $i; |
| 321 | |
Arjan van de Ven | c19ef7f | 2009-02-15 11:30:52 +0100 | [diff] [blame] | 322 | |
| 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. |
| 327 | |
| 328 | $i = $center; |
| 329 | while ($i >= $start) { |
| 330 | $reglines[$i] = process_x86_regs($lines[$i], $center - $i); |
| 331 | $i = $i - 1; |
Arjan van de Ven | 5aea50b | 2009-01-06 14:40:57 -0800 | [diff] [blame] | 332 | } |
| 333 | |
Arjan van de Ven | c19ef7f | 2009-02-15 11:30:52 +0100 | [diff] [blame] | 334 | $i = $start; |
| 335 | while ($i < $finish) { |
| 336 | my $line; |
| 337 | if ($i == $center) { |
| 338 | $line = "*$lines[$i] "; |
| 339 | } else { |
| 340 | $line = " $lines[$i] "; |
| 341 | } |
| 342 | print $line; |
| 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]"; |
| 347 | } |
| 348 | if ($i == $center) { |
| 349 | print "<--- faulting instruction"; |
| 350 | } |
| 351 | print "\n"; |
| 352 | $i = $i +1; |
| 353 | } |
Arjan van de Ven | 5aea50b | 2009-01-06 14:40:57 -0800 | [diff] [blame] | 354 | |
Hui Zhu | 52e13e2 | 2010-01-26 17:13:07 +0800 | [diff] [blame] | 355 | sub usage { |
| 356 | print <<EOT; |
| 357 | Usage: |
| 358 | dmesg | perl $0 [OPTION] [VMLINUX] |
| 359 | |
| 360 | OPTION: |
| 361 | -c, --cross-compile CROSS_COMPILE Specify the prefix used for toolchain. |
Hui Zhu | 59dde38 | 2010-02-01 13:41:22 +0800 | [diff] [blame] | 362 | -m, --module MODULE_DIRNAME Specify the module filename. |
Hui Zhu | 52e13e2 | 2010-01-26 17:13:07 +0800 | [diff] [blame] | 363 | -h, --help Help. |
| 364 | EOT |
| 365 | exit; |
| 366 | } |