markup_oops.pl 7.5 KB

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