checkstack.pl 6.0 KB

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