checkstack.pl 5.9 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 <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. # IA64 port via Andreas Dilger
  11. # Arm port by Holger Schurig
  12. # sh64 port by Paul Mundt
  13. # Random bits by Matt Mackall <mpm@selenic.com>
  14. # M68k port by Geert Uytterhoeven and Andreas Schwab
  15. # AArch64, PARISC ports by Kyle McMartin
  16. # sparc port by Martin Habets <errandir_news@mph.eclipse.co.uk>
  17. # ppc64le port by Breno Leitao <leitao@debian.org>
  18. #
  19. # Usage:
  20. # objdump -d vmlinux | scripts/checkstack.pl [arch]
  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 = 100;
  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 'ia64') {
  67. #e0000000044011fc: 01 0f fc 8c adds r12=-384,r12
  68. $re = qr/.*adds.*r12=-(([0-9]{2}|[3-9])[0-9]{2}),r12/o;
  69. } elsif ($arch eq 'm68k') {
  70. # 2b6c: 4e56 fb70 linkw %fp,#-1168
  71. # 1df770: defc ffe4 addaw #-28,%sp
  72. $re = qr/.*(?:linkw %fp,|addaw )#-([0-9]{1,4})(?:,%sp)?$/o;
  73. } elsif ($arch eq 'mips64') {
  74. #8800402c: 67bdfff0 daddiu sp,sp,-16
  75. $re = qr/.*daddiu.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
  76. } elsif ($arch eq 'mips') {
  77. #88003254: 27bdffe0 addiu sp,sp,-32
  78. $re = qr/.*addiu.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
  79. } elsif ($arch eq 'nios2') {
  80. #25a8: defffb04 addi sp,sp,-20
  81. $re = qr/.*addi.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
  82. } elsif ($arch eq 'openrisc') {
  83. # c000043c: 9c 21 fe f0 l.addi r1,r1,-272
  84. $re = qr/.*l\.addi.*r1,r1,-(([0-9]{2}|[3-9])[0-9]{2})/o;
  85. } elsif ($arch eq 'parisc' || $arch eq 'parisc64') {
  86. $re = qr/.*ldo ($x{1,8})\(sp\),sp/o;
  87. } elsif ($arch eq 'powerpc' || $arch =~ /^ppc(64)?(le)?$/ ) {
  88. # powerpc : 94 21 ff 30 stwu r1,-208(r1)
  89. # ppc64(le) : 81 ff 21 f8 stdu r1,-128(r1)
  90. $re = qr/.*st[dw]u.*r1,-($x{1,8})\(r1\)/o;
  91. } elsif ($arch =~ /^s390x?$/) {
  92. # 11160: a7 fb ff 60 aghi %r15,-160
  93. # or
  94. # 100092: e3 f0 ff c8 ff 71 lay %r15,-56(%r15)
  95. $re = qr/.*(?:lay|ag?hi).*\%r15,-(([0-9]{2}|[3-9])[0-9]{2})
  96. (?:\(\%r15\))?$/ox;
  97. } elsif ($arch =~ /^sh64$/) {
  98. #XXX: we only check for the immediate case presently,
  99. # though we will want to check for the movi/sub
  100. # pair for larger users. -- PFM.
  101. #a00048e0: d4fc40f0 addi.l r15,-240,r15
  102. $re = qr/.*addi\.l.*r15,-(([0-9]{2}|[3-9])[0-9]{2}),r15/o;
  103. } elsif ($arch eq 'sparc' || $arch eq 'sparc64') {
  104. # f0019d10: 9d e3 bf 90 save %sp, -112, %sp
  105. $re = qr/.*save.*%sp, -(([0-9]{2}|[3-9])[0-9]{2}), %sp/o;
  106. } else {
  107. print("wrong or unknown architecture \"$arch\"\n");
  108. exit
  109. }
  110. }
  111. #
  112. # To count stack usage of push {*, fp, ip, lr, pc} instruction in ARM,
  113. # if FRAME POINTER is enabled.
  114. # e.g. c01f0d48: e92ddff0 push {r4, r5, r6, r7, r8, r9, sl, fp, ip, lr, pc}
  115. #
  116. sub arm_push_handling {
  117. my $regex = qr/.*push.*fp, ip, lr, pc}/o;
  118. my $size = 0;
  119. my $line_arg = shift;
  120. if ($line_arg =~ m/$regex/) {
  121. $size = $line_arg =~ tr/,//;
  122. $size = ($size + 1) * 4;
  123. }
  124. return $size;
  125. }
  126. #
  127. # main()
  128. #
  129. my ($func, $file, $lastslash, $total_size, $addr, $intro);
  130. $total_size = 0;
  131. while (my $line = <STDIN>) {
  132. if ($line =~ m/$funcre/) {
  133. $func = $1;
  134. next if $line !~ m/^($xs*)/;
  135. if ($total_size > $min_stack) {
  136. push @stack, "$intro$total_size\n";
  137. }
  138. $addr = $1;
  139. $addr =~ s/ /0/g;
  140. $addr = "0x$addr";
  141. $intro = "$addr $func [$file]:";
  142. my $padlen = 56 - length($intro);
  143. while ($padlen > 0) {
  144. $intro .= ' ';
  145. $padlen -= 8;
  146. }
  147. $total_size = 0;
  148. }
  149. elsif ($line =~ m/(.*):\s*file format/) {
  150. $file = $1;
  151. $file =~ s/\.ko//;
  152. $lastslash = rindex($file, "/");
  153. if ($lastslash != -1) {
  154. $file = substr($file, $lastslash + 1);
  155. }
  156. }
  157. elsif ($line =~ m/$re/) {
  158. my $size = $1;
  159. $size = hex($size) if ($size =~ /^0x/);
  160. if ($size > 0xf0000000) {
  161. $size = - $size;
  162. $size += 0x80000000;
  163. $size += 0x80000000;
  164. }
  165. next if ($size > 0x10000000);
  166. $total_size += $size;
  167. }
  168. elsif (defined $dre && $line =~ m/$dre/) {
  169. my $size = $1;
  170. $size = hex($size) if ($size =~ /^0x/);
  171. $total_size += $size;
  172. }
  173. elsif (defined $sub) {
  174. my $size = &$sub($line);
  175. $total_size += $size;
  176. }
  177. }
  178. if ($total_size > $min_stack) {
  179. push @stack, "$intro$total_size\n";
  180. }
  181. # Sort output by size (last field)
  182. print sort { ($b =~ /:\t*(\d+)$/)[0] <=> ($a =~ /:\t*(\d+)$/)[0] } @stack;