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