3
0

checkstack.pl 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #!/usr/bin/env perl
  2. # SPDX-License-Identifier: GPL-2.0
  3. # Stolen from Linux kernel :)
  4. # Check the stack usage of functions
  5. #
  6. # Copyright Joern Engel <joern@lazybastard.org>
  7. # Inspired by Linus Torvalds
  8. # Original idea maybe from Keith Owens
  9. # s390 port and big speedup by Arnd Bergmann <arnd@bergmann-dalldorf.de>
  10. # Mips port by Juan Quintela <quintela@mandrakesoft.com>
  11. # IA64 port via Andreas Dilger
  12. # Arm port by Holger Schurig
  13. # sh64 port by Paul Mundt
  14. # Random bits by Matt Mackall <mpm@selenic.com>
  15. # M68k port by Geert Uytterhoeven and Andreas Schwab
  16. # blackfin port by Alex Landau
  17. # AArch64, PARISC ports by Kyle McMartin
  18. # sparc port by Martin Habets <errandir_news@mph.eclipse.co.uk>
  19. # ppc64le port by Breno Leitao <leitao@debian.org>
  20. #
  21. # Usage:
  22. # objdump -d vmlinux | scripts/checkstack.pl [arch]
  23. #
  24. # TODO : Port to all architectures (one regex per arch)
  25. use strict;
  26. # check for arch
  27. #
  28. # $re is used for two matches:
  29. # $& (whole re) matches the complete objdump line with the stack growth
  30. # $1 (first bracket) matches the size of the stack growth
  31. #
  32. # $dre is similar, but for dynamic stack reductions:
  33. # $& (whole re) matches the complete objdump line with the stack growth
  34. # $1 (first bracket) matches the dynamic amount of the stack growth
  35. #
  36. # use anything else and feel the pain ;)
  37. my (@stack, $re, $dre, $x, $xs, $funcre);
  38. {
  39. my $arch = shift;
  40. if ($arch eq "") {
  41. $arch = `uname -m`;
  42. chomp($arch);
  43. }
  44. $x = "[0-9a-f]"; # hex character
  45. $xs = "[0-9a-f ]"; # hex character or space
  46. $funcre = qr/^$x* <(.*)>:$/;
  47. if ($arch eq 'aarch64') {
  48. #ffffffc0006325cc: a9bb7bfd stp x29, x30, [sp, #-80]!
  49. #a110: d11643ff sub sp, sp, #0x590
  50. $re = qr/^.*stp.*sp, \#-([0-9]{1,8})\]\!/o;
  51. $dre = qr/^.*sub.*sp, sp, #(0x$x{1,8})/o;
  52. } elsif ($arch eq 'arm') {
  53. #c0008ffc: e24dd064 sub sp, sp, #100 ; 0x64
  54. $re = qr/.*sub.*sp, sp, #(([0-9]{2}|[3-9])[0-9]{2})/o;
  55. } elsif ($arch =~ /^x86(_64)?$/ || $arch =~ /^i[3456]86$/) {
  56. #c0105234: 81 ec ac 05 00 00 sub $0x5ac,%esp
  57. # or
  58. # 2f60: 48 81 ec e8 05 00 00 sub $0x5e8,%rsp
  59. $re = qr/^.*[as][du][db] \$(0x$x{1,8}),\%(e|r)sp$/o;
  60. $dre = qr/^.*[as][du][db] (%.*),\%(e|r)sp$/o;
  61. } elsif ($arch eq 'blackfin') {
  62. # 52: 00 e8 03 00 LINK 0xc;
  63. $re = qr/.*[[:space:]]LINK[[:space:]]*(0x$x{1,8})/o;
  64. } elsif ($arch eq 'ia64') {
  65. #e0000000044011fc: 01 0f fc 8c adds r12=-384,r12
  66. $re = qr/.*adds.*r12=-(([0-9]{2}|[3-9])[0-9]{2}),r12/o;
  67. } elsif ($arch eq 'm68k') {
  68. # 2b6c: 4e56 fb70 linkw %fp,#-1168
  69. # 1df770: defc ffe4 addaw #-28,%sp
  70. $re = qr/.*(?:linkw %fp,|addaw )#-([0-9]{1,4})(?:,%sp)?$/o;
  71. } elsif ($arch eq 'mips64') {
  72. #8800402c: 67bdfff0 daddiu sp,sp,-16
  73. $re = qr/.*daddiu.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
  74. } elsif ($arch eq 'mips') {
  75. #88003254: 27bdffe0 addiu sp,sp,-32
  76. $re = qr/.*addiu.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
  77. } elsif ($arch eq 'nios2') {
  78. #25a8: defffb04 addi sp,sp,-20
  79. $re = qr/.*addi.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
  80. } elsif ($arch eq 'openrisc') {
  81. # c000043c: 9c 21 fe f0 l.addi r1,r1,-272
  82. $re = qr/.*l\.addi.*r1,r1,-(([0-9]{2}|[3-9])[0-9]{2})/o;
  83. } elsif ($arch eq 'parisc' || $arch eq 'parisc64') {
  84. $re = qr/.*ldo ($x{1,8})\(sp\),sp/o;
  85. } elsif ($arch eq 'powerpc' || $arch =~ /^ppc(64)?(le)?$/ ) {
  86. # powerpc : 94 21 ff 30 stwu r1,-208(r1)
  87. # ppc64(le) : 81 ff 21 f8 stdu r1,-128(r1)
  88. $re = qr/.*st[dw]u.*r1,-($x{1,8})\(r1\)/o;
  89. } elsif ($arch =~ /^s390x?$/) {
  90. # 11160: a7 fb ff 60 aghi %r15,-160
  91. # or
  92. # 100092: e3 f0 ff c8 ff 71 lay %r15,-56(%r15)
  93. $re = qr/.*(?:lay|ag?hi).*\%r15,-(([0-9]{2}|[3-9])[0-9]{2})
  94. (?:\(\%r15\))?$/ox;
  95. } elsif ($arch =~ /^sh64$/) {
  96. #XXX: we only check for the immediate case presently,
  97. # though we will want to check for the movi/sub
  98. # pair for larger users. -- PFM.
  99. #a00048e0: d4fc40f0 addi.l r15,-240,r15
  100. $re = qr/.*addi\.l.*r15,-(([0-9]{2}|[3-9])[0-9]{2}),r15/o;
  101. } elsif ($arch eq 'sparc' || $arch eq 'sparc64') {
  102. # f0019d10: 9d e3 bf 90 save %sp, -112, %sp
  103. $re = qr/.*save.*%sp, -(([0-9]{2}|[3-9])[0-9]{2}), %sp/o;
  104. } else {
  105. print("wrong or unknown architecture \"$arch\"\n");
  106. exit
  107. }
  108. }
  109. #
  110. # main()
  111. #
  112. my ($func, $file, $lastslash);
  113. while (my $line = <STDIN>) {
  114. if ($line =~ m/$funcre/) {
  115. $func = $1;
  116. }
  117. elsif ($line =~ m/(.*):\s*file format/) {
  118. $file = $1;
  119. $file =~ s/\.ko//;
  120. $lastslash = rindex($file, "/");
  121. if ($lastslash != -1) {
  122. $file = substr($file, $lastslash + 1);
  123. }
  124. }
  125. elsif ($line =~ m/$re/) {
  126. my $size = $1;
  127. $size = hex($size) if ($size =~ /^0x/);
  128. if ($size > 0xf0000000) {
  129. $size = - $size;
  130. $size += 0x80000000;
  131. $size += 0x80000000;
  132. }
  133. next if ($size > 0x10000000);
  134. next if $line !~ m/^($xs*)/;
  135. my $addr = $1;
  136. $addr =~ s/ /0/g;
  137. $addr = "0x$addr";
  138. # bbox: was: my $intro = "$addr $func [$file]:";
  139. my $intro = "$func [$file]:";
  140. my $padlen = 56 - length($intro);
  141. while ($padlen > 0) {
  142. $intro .= ' ';
  143. $padlen -= 8;
  144. }
  145. next if ($size < 100);
  146. push @stack, "$intro$size\n";
  147. }
  148. elsif (defined $dre && $line =~ m/$dre/) {
  149. my $size = "Dynamic ($1)";
  150. next if $line !~ m/^($xs*)/;
  151. my $addr = $1;
  152. $addr =~ s/ /0/g;
  153. $addr = "0x$addr";
  154. #bbox: was: my $intro = "$addr $func [$file]:";
  155. my $intro = "$func [$file]:";
  156. my $padlen = 56 - length($intro);
  157. while ($padlen > 0) {
  158. $intro .= ' ';
  159. $padlen -= 8;
  160. }
  161. push @stack, "$intro$size\n";
  162. }
  163. }
  164. sub bysize($) {
  165. my ($asize, $bsize);
  166. ($asize = $a) =~ s/.*: *(.*)$/$1/;
  167. ($bsize = $b) =~ s/.*: *(.*)$/$1/;
  168. $bsize <=> $asize
  169. }
  170. # Sort output by size (last field)
  171. print sort { ($b =~ /:\t*(\d+)$/)[0] <=> ($a =~ /:\t*(\d+)$/)[0] } @stack;