markup_oops.pl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #!/usr/bin/perl -w
  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;
  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. }
  45. sub process_x86_regs
  46. {
  47. my ($line, $cntr) = @_;
  48. my $str = "";
  49. if (length($line) < 40) {
  50. return ""; # not an asm istruction
  51. }
  52. # find the arguments to the instruction
  53. if ($line =~ /([0-9a-zA-Z\,\%\(\)\-\+]+)$/) {
  54. $lastword = $1;
  55. } else {
  56. return "";
  57. }
  58. # we need to find the registers that get clobbered,
  59. # since their value is no longer relevant for previous
  60. # instructions in the stream.
  61. $clobber = $lastword;
  62. # first, remove all memory operands, they're read only
  63. $clobber =~ s/\([a-z0-9\%\,]+\)//g;
  64. # then, remove everything before the comma, thats the read part
  65. $clobber =~ s/.*\,//g;
  66. # if this is the instruction that faulted, we haven't actually done
  67. # the write yet... nothing is clobbered.
  68. if ($cntr == 0) {
  69. $clobber = "";
  70. }
  71. foreach $reg (keys(%regs)) {
  72. my $val = $regs{$reg};
  73. # first check if we're clobbering this register; if we do
  74. # we print it with a =>, and then delete its value
  75. if ($clobber =~ /$reg/) {
  76. if (length($val) > 0) {
  77. $str = $str . " $reg => $val ";
  78. }
  79. $regs{$reg} = "";
  80. $val = "";
  81. }
  82. # now check if we're reading this register
  83. if ($lastword =~ /$reg/) {
  84. if (length($val) > 0) {
  85. $str = $str . " $reg = $val ";
  86. }
  87. }
  88. }
  89. return $str;
  90. }
  91. # parse the oops
  92. while (<STDIN>) {
  93. my $line = $_;
  94. if ($line =~ /EIP: 0060:\[\<([a-z0-9]+)\>\]/) {
  95. $target = $1;
  96. }
  97. if ($line =~ /EIP is at ([a-zA-Z0-9\_]+)\+(0x[0-9a-f]+)\/0x[a-f0-9]/) {
  98. $function = $1;
  99. $func_offset = $2;
  100. }
  101. # check if it's a module
  102. if ($line =~ /EIP is at ([a-zA-Z0-9\_]+)\+(0x[0-9a-f]+)\/0x[a-f0-9]+\W\[([a-zA-Z0-9\_\-]+)\]/) {
  103. $module = $3;
  104. }
  105. parse_x86_regs($line);
  106. }
  107. my $decodestart = hex($target) - hex($func_offset);
  108. my $decodestop = hex($target) + 8192;
  109. if ($target eq "0") {
  110. print "No oops found!\n";
  111. print "Usage: \n";
  112. print " dmesg | perl scripts/markup_oops.pl vmlinux\n";
  113. exit;
  114. }
  115. # if it's a module, we need to find the .ko file and calculate a load offset
  116. if ($module ne "") {
  117. my $dir = dirname($filename);
  118. $dir = $dir . "/";
  119. my $mod = $module . ".ko";
  120. my $modulefile = `find $dir -name $mod | head -1`;
  121. chomp($modulefile);
  122. $filename = $modulefile;
  123. if ($filename eq "") {
  124. print "Module .ko file for $module not found. Aborting\n";
  125. exit;
  126. }
  127. # ok so we found the module, now we need to calculate the vma offset
  128. open(FILE, "objdump -dS $filename |") || die "Cannot start objdump";
  129. while (<FILE>) {
  130. if ($_ =~ /^([0-9a-f]+) \<$function\>\:/) {
  131. my $fu = $1;
  132. $vmaoffset = hex($target) - hex($fu) - hex($func_offset);
  133. }
  134. }
  135. close(FILE);
  136. }
  137. my $counter = 0;
  138. my $state = 0;
  139. my $center = 0;
  140. my @lines;
  141. my @reglines;
  142. sub InRange {
  143. my ($address, $target) = @_;
  144. my $ad = "0x".$address;
  145. my $ta = "0x".$target;
  146. my $delta = hex($ad) - hex($ta);
  147. if (($delta > -4096) && ($delta < 4096)) {
  148. return 1;
  149. }
  150. return 0;
  151. }
  152. # first, parse the input into the lines array, but to keep size down,
  153. # we only do this for 4Kb around the sweet spot
  154. open(FILE, "objdump -dS --adjust-vma=$vmaoffset --start-address=$decodestart --stop-address=$decodestop $filename |") || die "Cannot start objdump";
  155. while (<FILE>) {
  156. my $line = $_;
  157. chomp($line);
  158. if ($state == 0) {
  159. if ($line =~ /^([a-f0-9]+)\:/) {
  160. if (InRange($1, $target)) {
  161. $state = 1;
  162. }
  163. }
  164. } else {
  165. if ($line =~ /^([a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]+)\:/) {
  166. my $val = $1;
  167. if (!InRange($val, $target)) {
  168. last;
  169. }
  170. if ($val eq $target) {
  171. $center = $counter;
  172. }
  173. }
  174. $lines[$counter] = $line;
  175. $counter = $counter + 1;
  176. }
  177. }
  178. close(FILE);
  179. if ($counter == 0) {
  180. print "No matching code found \n";
  181. exit;
  182. }
  183. if ($center == 0) {
  184. print "No matching code found \n";
  185. exit;
  186. }
  187. my $start;
  188. my $finish;
  189. my $codelines = 0;
  190. my $binarylines = 0;
  191. # now we go up and down in the array to find how much we want to print
  192. $start = $center;
  193. while ($start > 1) {
  194. $start = $start - 1;
  195. my $line = $lines[$start];
  196. if ($line =~ /^([a-f0-9]+)\:/) {
  197. $binarylines = $binarylines + 1;
  198. } else {
  199. $codelines = $codelines + 1;
  200. }
  201. if ($codelines > 10) {
  202. last;
  203. }
  204. if ($binarylines > 20) {
  205. last;
  206. }
  207. }
  208. $finish = $center;
  209. $codelines = 0;
  210. $binarylines = 0;
  211. while ($finish < $counter) {
  212. $finish = $finish + 1;
  213. my $line = $lines[$finish];
  214. if ($line =~ /^([a-f0-9]+)\:/) {
  215. $binarylines = $binarylines + 1;
  216. } else {
  217. $codelines = $codelines + 1;
  218. }
  219. if ($codelines > 10) {
  220. last;
  221. }
  222. if ($binarylines > 20) {
  223. last;
  224. }
  225. }
  226. my $i;
  227. # start annotating the registers in the asm.
  228. # this goes from the oopsing point back, so that the annotator
  229. # can track (opportunistically) which registers got written and
  230. # whos value no longer is relevant.
  231. $i = $center;
  232. while ($i >= $start) {
  233. $reglines[$i] = process_x86_regs($lines[$i], $center - $i);
  234. $i = $i - 1;
  235. }
  236. $i = $start;
  237. while ($i < $finish) {
  238. my $line;
  239. if ($i == $center) {
  240. $line = "*$lines[$i] ";
  241. } else {
  242. $line = " $lines[$i] ";
  243. }
  244. print $line;
  245. if (defined($reglines[$i]) && length($reglines[$i]) > 0) {
  246. my $c = 60 - length($line);
  247. while ($c > 0) { print " "; $c = $c - 1; };
  248. print "| $reglines[$i]";
  249. }
  250. if ($i == $center) {
  251. print "<--- faulting instruction";
  252. }
  253. print "\n";
  254. $i = $i +1;
  255. }