| 1 | |
| 2 |
|
| 3 | use File::Basename;
|
| 4 | use Math::BigInt;
|
| 5 |
|
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 |
|
| 17 |
|
| 18 | my $vmlinux_name = $ARGV[0];
|
| 19 | if (!defined($vmlinux_name)) {
|
| 20 | my $kerver = `uname -r`;
|
| 21 | chomp($kerver);
|
| 22 | $vmlinux_name = "/lib/modules/$kerver/build/vmlinux";
|
| 23 | print "No vmlinux specified, assuming $vmlinux_name\n";
|
| 24 | }
|
| 25 | my $filename = $vmlinux_name;
|
| 26 | |
| 27 | |
| 28 | |
| 29 |
|
| 30 | my $target = "0";
|
| 31 | my $function;
|
| 32 | my $module = "";
|
| 33 | my $func_offset = 0;
|
| 34 | my $vmaoffset = 0;
|
| 35 |
|
| 36 | my %regs;
|
| 37 |
|
| 38 |
|
| 39 | sub parse_x86_regs
|
| 40 | {
|
| 41 | my ($line) = @_;
|
| 42 | if ($line =~ /EAX: ([0-9a-f]+) EBX: ([0-9a-f]+) ECX: ([0-9a-f]+) EDX: ([0-9a-f]+)/) {
|
| 43 | $regs{"%eax"} = $1;
|
| 44 | $regs{"%ebx"} = $2;
|
| 45 | $regs{"%ecx"} = $3;
|
| 46 | $regs{"%edx"} = $4;
|
| 47 | }
|
| 48 | if ($line =~ /ESI: ([0-9a-f]+) EDI: ([0-9a-f]+) EBP: ([0-9a-f]+) ESP: ([0-9a-f]+)/) {
|
| 49 | $regs{"%esi"} = $1;
|
| 50 | $regs{"%edi"} = $2;
|
| 51 | $regs{"%esp"} = $4;
|
| 52 | }
|
| 53 | if ($line =~ /RAX: ([0-9a-f]+) RBX: ([0-9a-f]+) RCX: ([0-9a-f]+)/) {
|
| 54 | $regs{"%eax"} = $1;
|
| 55 | $regs{"%ebx"} = $2;
|
| 56 | $regs{"%ecx"} = $3;
|
| 57 | }
|
| 58 | if ($line =~ /RDX: ([0-9a-f]+) RSI: ([0-9a-f]+) RDI: ([0-9a-f]+)/) {
|
| 59 | $regs{"%edx"} = $1;
|
| 60 | $regs{"%esi"} = $2;
|
| 61 | $regs{"%edi"} = $3;
|
| 62 | }
|
| 63 | if ($line =~ /RBP: ([0-9a-f]+) R08: ([0-9a-f]+) R09: ([0-9a-f]+)/) {
|
| 64 | $regs{"%r08"} = $2;
|
| 65 | $regs{"%r09"} = $3;
|
| 66 | }
|
| 67 | if ($line =~ /R10: ([0-9a-f]+) R11: ([0-9a-f]+) R12: ([0-9a-f]+)/) {
|
| 68 | $regs{"%r10"} = $1;
|
| 69 | $regs{"%r11"} = $2;
|
| 70 | $regs{"%r12"} = $3;
|
| 71 | }
|
| 72 | if ($line =~ /R13: ([0-9a-f]+) R14: ([0-9a-f]+) R15: ([0-9a-f]+)/) {
|
| 73 | $regs{"%r13"} = $1;
|
| 74 | $regs{"%r14"} = $2;
|
| 75 | $regs{"%r15"} = $3;
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | sub reg_name
|
| 80 | {
|
| 81 | my ($reg) = @_;
|
| 82 | $reg =~ s/r(.)x/e\1x/;
|
| 83 | $reg =~ s/r(.)i/e\1i/;
|
| 84 | $reg =~ s/r(.)p/e\1p/;
|
| 85 | return $reg;
|
| 86 | }
|
| 87 |
|
| 88 | sub process_x86_regs
|
| 89 | {
|
| 90 | my ($line, $cntr) = @_;
|
| 91 | my $str = "";
|
| 92 | if (length($line) < 40) {
|
| 93 | return ""; |
| 94 | }
|
| 95 |
|
| 96 | |
| 97 | if ($line =~ /([0-9a-zA-Z\,\%\(\)\-\+]+)$/) {
|
| 98 | $lastword = $1;
|
| 99 | } else {
|
| 100 | return "";
|
| 101 | }
|
| 102 |
|
| 103 | |
| 104 | |
| 105 | |
| 106 |
|
| 107 | $clobber = $lastword;
|
| 108 | |
| 109 | $clobber =~ s/\([a-z0-9\%\,]+\)//g;
|
| 110 | |
| 111 | $clobber =~ s/.*\,//g;
|
| 112 |
|
| 113 | |
| 114 | |
| 115 | if ($cntr == 0) {
|
| 116 | $clobber = "";
|
| 117 | }
|
| 118 |
|
| 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]+$/) {
|
| 124 | $val = "0";
|
| 125 | } else {
|
| 126 | $val =~ s/^0*//;
|
| 127 | }
|
| 128 |
|
| 129 | |
| 130 | |
| 131 | if ($clobber =~ /$reg/ || $clobberprime =~ /$reg/) {
|
| 132 | if (length($val) > 0) {
|
| 133 | $str = $str . " $reg => $val ";
|
| 134 | }
|
| 135 | $regs{$reg} = "";
|
| 136 | $val = "";
|
| 137 | }
|
| 138 | |
| 139 | if ($lastword =~ /$reg/ || $lastwordprime =~ /$reg/) {
|
| 140 | if (length($val) > 0) {
|
| 141 | $str = $str . " $reg = $val ";
|
| 142 | }
|
| 143 | }
|
| 144 | }
|
| 145 | return $str;
|
| 146 | }
|
| 147 |
|
| 148 | |
| 149 | while (<STDIN>) {
|
| 150 | my $line = $_;
|
| 151 | if ($line =~ /EIP: 0060:\[\<([a-z0-9]+)\>\]/) {
|
| 152 | $target = $1;
|
| 153 | }
|
| 154 | if ($line =~ /RIP: 0010:\[\<([a-z0-9]+)\>\]/) {
|
| 155 | $target = $1;
|
| 156 | }
|
| 157 | if ($line =~ /EIP is at ([a-zA-Z0-9\_]+)\+(0x[0-9a-f]+)\/0x[a-f0-9]/) {
|
| 158 | $function = $1;
|
| 159 | $func_offset = $2;
|
| 160 | }
|
| 161 | if ($line =~ /RIP: 0010:\[\<[0-9a-f]+\>\] \[\<[0-9a-f]+\>\] ([a-zA-Z0-9\_]+)\+(0x[0-9a-f]+)\/0x[a-f0-9]/) {
|
| 162 | $function = $1;
|
| 163 | $func_offset = $2;
|
| 164 | }
|
| 165 |
|
| 166 | |
| 167 | if ($line =~ /EIP is at ([a-zA-Z0-9\_]+)\+(0x[0-9a-f]+)\/0x[a-f0-9]+\W\[([a-zA-Z0-9\_\-]+)\]/) {
|
| 168 | $module = $3;
|
| 169 | }
|
| 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\_\-]+)\]/) {
|
| 171 | $module = $3;
|
| 172 | }
|
| 173 | parse_x86_regs($line);
|
| 174 | }
|
| 175 |
|
| 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";
|
| 180 | print "Usage: \n";
|
| 181 | print " dmesg | perl scripts/markup_oops.pl vmlinux\n";
|
| 182 | exit;
|
| 183 | }
|
| 184 |
|
| 185 | |
| 186 | if ($module ne "") {
|
| 187 | my $modulefile = `modinfo $module | grep '^filename:' | awk '{ print \$2 }'`;
|
| 188 | chomp($modulefile);
|
| 189 | $filename = $modulefile;
|
| 190 | if ($filename eq "") {
|
| 191 | print "Module .ko file for $module not found. Aborting\n";
|
| 192 | exit;
|
| 193 | }
|
| 194 | |
| 195 | open(FILE, "objdump -dS $filename |") || die "Cannot start objdump";
|
| 196 | while (<FILE>) {
|
| 197 | if ($_ =~ /^([0-9a-f]+) \<$function\>\:/) {
|
| 198 | my $fu = $1;
|
| 199 | $vmaoffset = hex($target) - hex($fu) - hex($func_offset);
|
| 200 | }
|
| 201 | }
|
| 202 | close(FILE);
|
| 203 | }
|
| 204 |
|
| 205 | my $counter = 0;
|
| 206 | my $state = 0;
|
| 207 | my $center = 0;
|
| 208 | my @lines;
|
| 209 | my @reglines;
|
| 210 |
|
| 211 | sub InRange {
|
| 212 | my ($address, $target) = @_;
|
| 213 | my $ad = "0x".$address;
|
| 214 | my $ta = "0x".$target;
|
| 215 | my $delta = hex($ad) - hex($ta);
|
| 216 |
|
| 217 | if (($delta > -4096) && ($delta < 4096)) {
|
| 218 | return 1;
|
| 219 | }
|
| 220 | return 0;
|
| 221 | }
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 | |
| 226 | |
| 227 |
|
| 228 | open(FILE, "objdump -dS --adjust-vma=$vmaoffset --start-address=$decodestart --stop-address=$decodestop $filename |") || die "Cannot start objdump";
|
| 229 |
|
| 230 | while (<FILE>) {
|
| 231 | my $line = $_;
|
| 232 | chomp($line);
|
| 233 | if ($state == 0) {
|
| 234 | if ($line =~ /^([a-f0-9]+)\:/) {
|
| 235 | if (InRange($1, $target)) {
|
| 236 | $state = 1;
|
| 237 | }
|
| 238 | }
|
| 239 | } else {
|
| 240 | if ($line =~ /^([a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]+)\:/) {
|
| 241 | my $val = $1;
|
| 242 | if (!InRange($val, $target)) {
|
| 243 | last;
|
| 244 | }
|
| 245 | if ($val eq $target) {
|
| 246 | $center = $counter;
|
| 247 | }
|
| 248 | }
|
| 249 | $lines[$counter] = $line;
|
| 250 |
|
| 251 | $counter = $counter + 1;
|
| 252 | }
|
| 253 | }
|
| 254 |
|
| 255 | close(FILE);
|
| 256 |
|
| 257 | if ($counter == 0) {
|
| 258 | print "No matching code found \n";
|
| 259 | exit;
|
| 260 | }
|
| 261 |
|
| 262 | if ($center == 0) {
|
| 263 | print "No matching code found \n";
|
| 264 | exit;
|
| 265 | }
|
| 266 |
|
| 267 | my $start;
|
| 268 | my $finish;
|
| 269 | my $codelines = 0;
|
| 270 | my $binarylines = 0;
|
| 271 | |
| 272 |
|
| 273 | $start = $center;
|
| 274 |
|
| 275 | while ($start > 1) {
|
| 276 | $start = $start - 1;
|
| 277 | my $line = $lines[$start];
|
| 278 | if ($line =~ /^([a-f0-9]+)\:/) {
|
| 279 | $binarylines = $binarylines + 1;
|
| 280 | } else {
|
| 281 | $codelines = $codelines + 1;
|
| 282 | }
|
| 283 | if ($codelines > 10) {
|
| 284 | last;
|
| 285 | }
|
| 286 | if ($binarylines > 20) {
|
| 287 | last;
|
| 288 | }
|
| 289 | }
|
| 290 |
|
| 291 |
|
| 292 | $finish = $center;
|
| 293 | $codelines = 0;
|
| 294 | $binarylines = 0;
|
| 295 | while ($finish < $counter) {
|
| 296 | $finish = $finish + 1;
|
| 297 | my $line = $lines[$finish];
|
| 298 | if ($line =~ /^([a-f0-9]+)\:/) {
|
| 299 | $binarylines = $binarylines + 1;
|
| 300 | } else {
|
| 301 | $codelines = $codelines + 1;
|
| 302 | }
|
| 303 | if ($codelines > 10) {
|
| 304 | last;
|
| 305 | }
|
| 306 | if ($binarylines > 20) {
|
| 307 | last;
|
| 308 | }
|
| 309 | }
|
| 310 |
|
| 311 |
|
| 312 | my $i;
|
| 313 |
|
| 314 |
|
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 |
|
| 320 | $i = $center;
|
| 321 | while ($i >= $start) {
|
| 322 | $reglines[$i] = process_x86_regs($lines[$i], $center - $i);
|
| 323 | $i = $i - 1;
|
| 324 | }
|
| 325 |
|
| 326 | $i = $start;
|
| 327 | while ($i < $finish) {
|
| 328 | my $line;
|
| 329 | if ($i == $center) {
|
| 330 | $line = "*$lines[$i] ";
|
| 331 | } else {
|
| 332 | $line = " $lines[$i] ";
|
| 333 | }
|
| 334 | print $line;
|
| 335 | if (defined($reglines[$i]) && length($reglines[$i]) > 0) {
|
| 336 | my $c = 60 - length($line);
|
| 337 | while ($c > 0) { print " "; $c = $c - 1; };
|
| 338 | print "| $reglines[$i]";
|
| 339 | }
|
| 340 | if ($i == $center) {
|
| 341 | print "<--- faulting instruction";
|
| 342 | }
|
| 343 | print "\n";
|
| 344 | $i = $i +1;
|
| 345 | }
|
| 346 |
|