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