checkstack.pl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/perl
  2. # Stolen from Linux kernel :)
  3. # Check the stack usage of functions
  4. #
  5. # Copyright Joern Engel <joern@wh.fh-wedel.de>
  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. #
  16. # Usage:
  17. # objdump -d vmlinux | checkstack.pl [arch]
  18. #
  19. # TODO : Port to all architectures (one regex per arch)
  20. # check for arch
  21. #
  22. # $re is used for two matches:
  23. # $& (whole re) matches the complete objdump line with the stack growth
  24. # $1 (first bracket) matches the size of the stack growth
  25. #
  26. # use anything else and feel the pain ;)
  27. my (@stack, $re, $x, $xs);
  28. {
  29. my $arch = shift;
  30. if ($arch eq "") {
  31. $arch = `uname -m`;
  32. }
  33. $x = "[0-9a-f]"; # hex character
  34. $xs = "[0-9a-f ]"; # hex character or space
  35. if ($arch eq 'arm') {
  36. #c0008ffc: e24dd064 sub sp, sp, #100 ; 0x64
  37. $re = qr/.*sub.*sp, sp, #(([0-9]{2}|[3-9])[0-9]{2})/o;
  38. } elsif ($arch eq 'blackfin') {
  39. # 52: 00 e8 03 00 LINK 0xc;
  40. $re = qr/.*LINK (0x$x{1,5});$/o;
  41. } elsif ($arch =~ /^i[3456]86$/) {
  42. #c0105234: 81 ec ac 05 00 00 sub $0x5ac,%esp
  43. $re = qr/^.*[as][du][db] \$(0x$x{1,8}),\%esp$/o;
  44. } elsif ($arch eq 'x86_64') {
  45. # 2f60: 48 81 ec e8 05 00 00 sub $0x5e8,%rsp
  46. $re = qr/^.*[as][du][db] \$(0x$x{1,8}),\%rsp$/o;
  47. } elsif ($arch eq 'ia64') {
  48. #e0000000044011fc: 01 0f fc 8c adds r12=-384,r12
  49. $re = qr/.*adds.*r12=-(([0-9]{2}|[3-9])[0-9]{2}),r12/o;
  50. } elsif ($arch eq 'm68k') {
  51. # 2b6c: 4e56 fb70 linkw %fp,#-1168
  52. # 1df770: defc ffe4 addaw #-28,%sp
  53. $re = qr/.*(?:linkw %fp,|addaw )#-([0-9]{1,4})(?:,%sp)?$/o;
  54. } elsif ($arch eq 'mips64') {
  55. #8800402c: 67bdfff0 daddiu sp,sp,-16
  56. $re = qr/.*daddiu.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
  57. } elsif ($arch eq 'mips') {
  58. #88003254: 27bdffe0 addiu sp,sp,-32
  59. $re = qr/.*addiu.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
  60. } elsif ($arch eq 'ppc') {
  61. #c00029f4: 94 21 ff 30 stwu r1,-208(r1)
  62. $re = qr/.*stwu.*r1,-($x{1,8})\(r1\)/o;
  63. } elsif ($arch eq 'ppc64') {
  64. #XXX
  65. $re = qr/.*stdu.*r1,-($x{1,8})\(r1\)/o;
  66. } elsif ($arch eq 'powerpc') {
  67. $re = qr/.*st[dw]u.*r1,-($x{1,8})\(r1\)/o;
  68. } elsif ($arch =~ /^s390x?$/) {
  69. # 11160: a7 fb ff 60 aghi %r15,-160
  70. $re = qr/.*ag?hi.*\%r15,-(([0-9]{2}|[3-9])[0-9]{2})/o;
  71. } elsif ($arch =~ /^sh64$/) {
  72. #XXX: we only check for the immediate case presently,
  73. # though we will want to check for the movi/sub
  74. # pair for larger users. -- PFM.
  75. #a00048e0: d4fc40f0 addi.l r15,-240,r15
  76. $re = qr/.*addi\.l.*r15,-(([0-9]{2}|[3-9])[0-9]{2}),r15/o;
  77. } else {
  78. print("wrong or unknown architecture\n");
  79. exit
  80. }
  81. }
  82. sub bysize($) {
  83. my ($asize, $bsize);
  84. ($asize = $a) =~ s/.*: *(.*)$/$1/;
  85. ($bsize = $b) =~ s/.*: *(.*)$/$1/;
  86. $bsize <=> $asize
  87. }
  88. #
  89. # main()
  90. #
  91. my $funcre = qr/^$x* <(.*)>:$/;
  92. my $func;
  93. my $file, $lastslash;
  94. while (my $line = <STDIN>) {
  95. if ($line =~ m/$funcre/) {
  96. $func = $1;
  97. }
  98. elsif ($line =~ m/(.*):\s*file format/) {
  99. $file = $1;
  100. $file =~ s/\.ko//;
  101. $lastslash = rindex($file, "/");
  102. if ($lastslash != -1) {
  103. $file = substr($file, $lastslash + 1);
  104. }
  105. }
  106. elsif ($line =~ m/$re/) {
  107. my $size = $1;
  108. $size = hex($size) if ($size =~ /^0x/);
  109. if ($size > 0xf0000000) {
  110. $size = - $size;
  111. $size += 0x80000000;
  112. $size += 0x80000000;
  113. }
  114. next if ($size > 0x10000000);
  115. next if $line !~ m/^($xs*)/;
  116. my $addr = $1;
  117. $addr =~ s/ /0/g;
  118. $addr = "0x$addr";
  119. my $intro = "$addr $func [$file]:";
  120. my $padlen = 56 - length($intro);
  121. while ($padlen > 0) {
  122. $intro .= ' ';
  123. $padlen -= 8;
  124. }
  125. next if ($size < 100);
  126. push @stack, "$intro$size\n";
  127. }
  128. }
  129. print sort bysize @stack;