checkstack.pl 6.1 KB

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