markup_oops.pl 7.5 KB

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