markup_oops.pl 7.9 KB

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