markup_oops.pl 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #!/usr/bin/perl
  2. use File::Basename;
  3. use Math::BigInt;
  4. use Getopt::Long;
  5. # Copyright 2008, Intel Corporation
  6. #
  7. # This file is part of the Linux kernel
  8. #
  9. # This program file is free software; you can redistribute it and/or modify it
  10. # under the terms of the GNU General Public License as published by the
  11. # Free Software Foundation; version 2 of the License.
  12. #
  13. # Authors:
  14. # Arjan van de Ven <arjan@linux.intel.com>
  15. my $cross_compile = "";
  16. my $vmlinux_name = "";
  17. my $modulefile = "";
  18. # Get options
  19. Getopt::Long::GetOptions(
  20. 'cross-compile|c=s' => \$cross_compile,
  21. 'module|m=s' => \$modulefile,
  22. 'help|h' => \&usage,
  23. ) || usage ();
  24. my $vmlinux_name = $ARGV[0];
  25. if (!defined($vmlinux_name)) {
  26. my $kerver = `uname -r`;
  27. chomp($kerver);
  28. $vmlinux_name = "/lib/modules/$kerver/build/vmlinux";
  29. print "No vmlinux specified, assuming $vmlinux_name\n";
  30. }
  31. my $filename = $vmlinux_name;
  32. # Parse the oops to find the EIP value
  33. my $target = "0";
  34. my $function;
  35. my $module = "";
  36. my $func_offset = 0;
  37. my $vmaoffset = 0;
  38. my %regs;
  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. sub reg_name
  79. {
  80. my ($reg) = @_;
  81. $reg =~ s/r(.)x/e\1x/;
  82. $reg =~ s/r(.)i/e\1i/;
  83. $reg =~ s/r(.)p/e\1p/;
  84. return $reg;
  85. }
  86. sub process_x86_regs
  87. {
  88. my ($line, $cntr) = @_;
  89. my $str = "";
  90. if (length($line) < 40) {
  91. return ""; # not an asm istruction
  92. }
  93. # find the arguments to the instruction
  94. if ($line =~ /([0-9a-zA-Z\,\%\(\)\-\+]+)$/) {
  95. $lastword = $1;
  96. } else {
  97. return "";
  98. }
  99. # we need to find the registers that get clobbered,
  100. # since their value is no longer relevant for previous
  101. # instructions in the stream.
  102. $clobber = $lastword;
  103. # first, remove all memory operands, they're read only
  104. $clobber =~ s/\([a-z0-9\%\,]+\)//g;
  105. # then, remove everything before the comma, thats the read part
  106. $clobber =~ s/.*\,//g;
  107. # if this is the instruction that faulted, we haven't actually done
  108. # the write yet... nothing is clobbered.
  109. if ($cntr == 0) {
  110. $clobber = "";
  111. }
  112. foreach $reg (keys(%regs)) {
  113. my $clobberprime = reg_name($clobber);
  114. my $lastwordprime = reg_name($lastword);
  115. my $val = $regs{$reg};
  116. if ($val =~ /^[0]+$/) {
  117. $val = "0";
  118. } else {
  119. $val =~ s/^0*//;
  120. }
  121. # first check if we're clobbering this register; if we do
  122. # we print it with a =>, and then delete its value
  123. if ($clobber =~ /$reg/ || $clobberprime =~ /$reg/) {
  124. if (length($val) > 0) {
  125. $str = $str . " $reg => $val ";
  126. }
  127. $regs{$reg} = "";
  128. $val = "";
  129. }
  130. # now check if we're reading this register
  131. if ($lastword =~ /$reg/ || $lastwordprime =~ /$reg/) {
  132. if (length($val) > 0) {
  133. $str = $str . " $reg = $val ";
  134. }
  135. }
  136. }
  137. return $str;
  138. }
  139. # parse the oops
  140. while (<STDIN>) {
  141. my $line = $_;
  142. if ($line =~ /EIP: 0060:\[\<([a-z0-9]+)\>\]/) {
  143. $target = $1;
  144. }
  145. if ($line =~ /RIP: 0010:\[\<([a-z0-9]+)\>\]/) {
  146. $target = $1;
  147. }
  148. if ($line =~ /EIP is at ([a-zA-Z0-9\_]+)\+0x([0-9a-f]+)\/0x[a-f0-9]/) {
  149. $function = $1;
  150. $func_offset = $2;
  151. }
  152. if ($line =~ /RIP: 0010:\[\<[0-9a-f]+\>\] \[\<[0-9a-f]+\>\] ([a-zA-Z0-9\_]+)\+0x([0-9a-f]+)\/0x[a-f0-9]/) {
  153. $function = $1;
  154. $func_offset = $2;
  155. }
  156. # check if it's a module
  157. if ($line =~ /EIP is at ([a-zA-Z0-9\_]+)\+(0x[0-9a-f]+)\/0x[a-f0-9]+\W\[([a-zA-Z0-9\_\-]+)\]/) {
  158. $module = $3;
  159. }
  160. 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\_\-]+)\]/) {
  161. $module = $3;
  162. }
  163. parse_x86_regs($line);
  164. }
  165. my $decodestart = Math::BigInt->from_hex("0x$target") - Math::BigInt->from_hex("0x$func_offset");
  166. my $decodestop = Math::BigInt->from_hex("0x$target") + 8192;
  167. if ($target eq "0") {
  168. print "No oops found!\n";
  169. usage();
  170. }
  171. # if it's a module, we need to find the .ko file and calculate a load offset
  172. if ($module ne "") {
  173. if ($modulefile eq "") {
  174. $modulefile = `modinfo -F filename $module`;
  175. chomp($modulefile);
  176. }
  177. $filename = $modulefile;
  178. if ($filename eq "") {
  179. print "Module .ko file for $module not found. Aborting\n";
  180. exit;
  181. }
  182. # ok so we found the module, now we need to calculate the vma offset
  183. open(FILE, $cross_compile."objdump -dS $filename |") || die "Cannot start objdump";
  184. while (<FILE>) {
  185. if ($_ =~ /^([0-9a-f]+) \<$function\>\:/) {
  186. my $fu = $1;
  187. $vmaoffset = Math::BigInt->from_hex("0x$target") - Math::BigInt->from_hex("0x$fu") - Math::BigInt->from_hex("0x$func_offset");
  188. }
  189. }
  190. close(FILE);
  191. }
  192. my $counter = 0;
  193. my $state = 0;
  194. my $center = -1;
  195. my @lines;
  196. my @reglines;
  197. sub InRange {
  198. my ($address, $target) = @_;
  199. my $ad = "0x".$address;
  200. my $ta = "0x".$target;
  201. my $delta = Math::BigInt->from_hex($ad) - Math::BigInt->from_hex($ta);
  202. if (($delta > -4096) && ($delta < 4096)) {
  203. return 1;
  204. }
  205. return 0;
  206. }
  207. # first, parse the input into the lines array, but to keep size down,
  208. # we only do this for 4Kb around the sweet spot
  209. open(FILE, $cross_compile."objdump -dS --adjust-vma=$vmaoffset --start-address=$decodestart --stop-address=$decodestop $filename |") || die "Cannot start objdump";
  210. while (<FILE>) {
  211. my $line = $_;
  212. chomp($line);
  213. if ($state == 0) {
  214. if ($line =~ /^([a-f0-9]+)\:/) {
  215. if (InRange($1, $target)) {
  216. $state = 1;
  217. }
  218. }
  219. }
  220. if ($state == 1) {
  221. if ($line =~ /^([a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]+)\:/) {
  222. my $val = $1;
  223. if (!InRange($val, $target)) {
  224. last;
  225. }
  226. if ($val eq $target) {
  227. $center = $counter;
  228. }
  229. }
  230. $lines[$counter] = $line;
  231. $counter = $counter + 1;
  232. }
  233. }
  234. close(FILE);
  235. if ($counter == 0) {
  236. print "No matching code found \n";
  237. exit;
  238. }
  239. if ($center == -1) {
  240. print "No matching code found \n";
  241. exit;
  242. }
  243. my $start;
  244. my $finish;
  245. my $codelines = 0;
  246. my $binarylines = 0;
  247. # now we go up and down in the array to find how much we want to print
  248. $start = $center;
  249. while ($start > 1) {
  250. $start = $start - 1;
  251. my $line = $lines[$start];
  252. if ($line =~ /^([a-f0-9]+)\:/) {
  253. $binarylines = $binarylines + 1;
  254. } else {
  255. $codelines = $codelines + 1;
  256. }
  257. if ($codelines > 10) {
  258. last;
  259. }
  260. if ($binarylines > 20) {
  261. last;
  262. }
  263. }
  264. $finish = $center;
  265. $codelines = 0;
  266. $binarylines = 0;
  267. while ($finish < $counter) {
  268. $finish = $finish + 1;
  269. my $line = $lines[$finish];
  270. if ($line =~ /^([a-f0-9]+)\:/) {
  271. $binarylines = $binarylines + 1;
  272. } else {
  273. $codelines = $codelines + 1;
  274. }
  275. if ($codelines > 10) {
  276. last;
  277. }
  278. if ($binarylines > 20) {
  279. last;
  280. }
  281. }
  282. my $i;
  283. # start annotating the registers in the asm.
  284. # this goes from the oopsing point back, so that the annotator
  285. # can track (opportunistically) which registers got written and
  286. # whos value no longer is relevant.
  287. $i = $center;
  288. while ($i >= $start) {
  289. $reglines[$i] = process_x86_regs($lines[$i], $center - $i);
  290. $i = $i - 1;
  291. }
  292. $i = $start;
  293. while ($i < $finish) {
  294. my $line;
  295. if ($i == $center) {
  296. $line = "*$lines[$i] ";
  297. } else {
  298. $line = " $lines[$i] ";
  299. }
  300. print $line;
  301. if (defined($reglines[$i]) && length($reglines[$i]) > 0) {
  302. my $c = 60 - length($line);
  303. while ($c > 0) { print " "; $c = $c - 1; };
  304. print "| $reglines[$i]";
  305. }
  306. if ($i == $center) {
  307. print "<--- faulting instruction";
  308. }
  309. print "\n";
  310. $i = $i +1;
  311. }
  312. sub usage {
  313. print <<EOT;
  314. Usage:
  315. dmesg | perl $0 [OPTION] [VMLINUX]
  316. OPTION:
  317. -c, --cross-compile CROSS_COMPILE Specify the prefix used for toolchain.
  318. -m, --module MODULE_DIRNAME Specify the module filename.
  319. -h, --help Help.
  320. EOT
  321. exit;
  322. }