markup_oops.pl 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 $dir = dirname($filename);
  168. $dir = $dir . "/";
  169. my $mod = $module . ".ko";
  170. my $modulefile = `find $dir -name $mod | head -1`;
  171. chomp($modulefile);
  172. $filename = $modulefile;
  173. if ($filename eq "") {
  174. print "Module .ko file for $module not found. Aborting\n";
  175. exit;
  176. }
  177. # ok so we found the module, now we need to calculate the vma offset
  178. open(FILE, "objdump -dS $filename |") || die "Cannot start objdump";
  179. while (<FILE>) {
  180. if ($_ =~ /^([0-9a-f]+) \<$function\>\:/) {
  181. my $fu = $1;
  182. $vmaoffset = hex($target) - hex($fu) - hex($func_offset);
  183. }
  184. }
  185. close(FILE);
  186. }
  187. my $counter = 0;
  188. my $state = 0;
  189. my $center = 0;
  190. my @lines;
  191. my @reglines;
  192. sub InRange {
  193. my ($address, $target) = @_;
  194. my $ad = "0x".$address;
  195. my $ta = "0x".$target;
  196. my $delta = hex($ad) - hex($ta);
  197. if (($delta > -4096) && ($delta < 4096)) {
  198. return 1;
  199. }
  200. return 0;
  201. }
  202. # first, parse the input into the lines array, but to keep size down,
  203. # we only do this for 4Kb around the sweet spot
  204. open(FILE, "objdump -dS --adjust-vma=$vmaoffset --start-address=$decodestart --stop-address=$decodestop $filename |") || die "Cannot start objdump";
  205. while (<FILE>) {
  206. my $line = $_;
  207. chomp($line);
  208. if ($state == 0) {
  209. if ($line =~ /^([a-f0-9]+)\:/) {
  210. if (InRange($1, $target)) {
  211. $state = 1;
  212. }
  213. }
  214. } else {
  215. if ($line =~ /^([a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]+)\:/) {
  216. my $val = $1;
  217. if (!InRange($val, $target)) {
  218. last;
  219. }
  220. if ($val eq $target) {
  221. $center = $counter;
  222. }
  223. }
  224. $lines[$counter] = $line;
  225. $counter = $counter + 1;
  226. }
  227. }
  228. close(FILE);
  229. if ($counter == 0) {
  230. print "No matching code found \n";
  231. exit;
  232. }
  233. if ($center == 0) {
  234. print "No matching code found \n";
  235. exit;
  236. }
  237. my $start;
  238. my $finish;
  239. my $codelines = 0;
  240. my $binarylines = 0;
  241. # now we go up and down in the array to find how much we want to print
  242. $start = $center;
  243. while ($start > 1) {
  244. $start = $start - 1;
  245. my $line = $lines[$start];
  246. if ($line =~ /^([a-f0-9]+)\:/) {
  247. $binarylines = $binarylines + 1;
  248. } else {
  249. $codelines = $codelines + 1;
  250. }
  251. if ($codelines > 10) {
  252. last;
  253. }
  254. if ($binarylines > 20) {
  255. last;
  256. }
  257. }
  258. $finish = $center;
  259. $codelines = 0;
  260. $binarylines = 0;
  261. while ($finish < $counter) {
  262. $finish = $finish + 1;
  263. my $line = $lines[$finish];
  264. if ($line =~ /^([a-f0-9]+)\:/) {
  265. $binarylines = $binarylines + 1;
  266. } else {
  267. $codelines = $codelines + 1;
  268. }
  269. if ($codelines > 10) {
  270. last;
  271. }
  272. if ($binarylines > 20) {
  273. last;
  274. }
  275. }
  276. my $i;
  277. # start annotating the registers in the asm.
  278. # this goes from the oopsing point back, so that the annotator
  279. # can track (opportunistically) which registers got written and
  280. # whos value no longer is relevant.
  281. $i = $center;
  282. while ($i >= $start) {
  283. $reglines[$i] = process_x86_regs($lines[$i], $center - $i);
  284. $i = $i - 1;
  285. }
  286. $i = $start;
  287. while ($i < $finish) {
  288. my $line;
  289. if ($i == $center) {
  290. $line = "*$lines[$i] ";
  291. } else {
  292. $line = " $lines[$i] ";
  293. }
  294. print $line;
  295. if (defined($reglines[$i]) && length($reglines[$i]) > 0) {
  296. my $c = 60 - length($line);
  297. while ($c > 0) { print " "; $c = $c - 1; };
  298. print "| $reglines[$i]";
  299. }
  300. if ($i == $center) {
  301. print "<--- faulting instruction";
  302. }
  303. print "\n";
  304. $i = $i +1;
  305. }