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