checkstack.pl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 =~ /^s390x?$/) {
  63. # 11160: a7 fb ff 60 aghi %r15,-160
  64. $re = qr/.*ag?hi.*\%r15,-(([0-9]{2}|[3-9])[0-9]{2})/o;
  65. } elsif ($arch =~ /^sh64$/) {
  66. #XXX: we only check for the immediate case presently,
  67. # though we will want to check for the movi/sub
  68. # pair for larger users. -- PFM.
  69. #a00048e0: d4fc40f0 addi.l r15,-240,r15
  70. $re = qr/.*addi\.l.*r15,-(([0-9]{2}|[3-9])[0-9]{2}),r15/o;
  71. } else {
  72. print("wrong or unknown architecture\n");
  73. exit
  74. }
  75. }
  76. sub bysize($) {
  77. my ($asize, $bsize);
  78. ($asize = $a) =~ s/.*: *(.*)$/$1/;
  79. ($bsize = $b) =~ s/.*: *(.*)$/$1/;
  80. $bsize <=> $asize
  81. }
  82. #
  83. # main()
  84. #
  85. my $funcre = qr/^$x* <(.*)>:$/;
  86. my $func;
  87. while (my $line = <STDIN>) {
  88. if ($line =~ m/$funcre/) {
  89. $func = $1;
  90. }
  91. if ($line =~ m/$re/) {
  92. my $size = $1;
  93. $size = hex($size) if ($size =~ /^0x/);
  94. if ($size > 0xf0000000) {
  95. $size = - $size;
  96. $size += 0x80000000;
  97. $size += 0x80000000;
  98. }
  99. next if ($size > 0x10000000);
  100. next if $line !~ m/^($xs*)/;
  101. my $addr = $1;
  102. $addr =~ s/ /0/g;
  103. $addr = "0x$addr";
  104. my $intro = "$addr $func:";
  105. my $padlen = 56 - length($intro);
  106. while ($padlen > 0) {
  107. $intro .= ' ';
  108. $padlen -= 8;
  109. }
  110. next if ($size < 100);
  111. push @stack, "$intro$size\n";
  112. }
  113. }
  114. print sort bysize @stack;