checkstack.pl 3.8 KB

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