| 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; | 
| Johannes Berg | 271c511 | 2006-08-22 16:57:05 +0200 | [diff] [blame] | 65 | } elsif ($arch eq 'powerpc') { | 
|  | 66 | $re = qr/.*st[dw]u.*r1,-($x{1,8})\(r1\)/o; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } elsif ($arch =~ /^s390x?$/) { | 
|  | 68 | #   11160:       a7 fb ff 60             aghi   %r15,-160 | 
|  | 69 | $re = qr/.*ag?hi.*\%r15,-(([0-9]{2}|[3-9])[0-9]{2})/o; | 
|  | 70 | } elsif ($arch =~ /^sh64$/) { | 
|  | 71 | #XXX: we only check for the immediate case presently, | 
|  | 72 | #     though we will want to check for the movi/sub | 
|  | 73 | #     pair for larger users. -- PFM. | 
|  | 74 | #a00048e0:       d4fc40f0        addi.l  r15,-240,r15 | 
|  | 75 | $re = qr/.*addi\.l.*r15,-(([0-9]{2}|[3-9])[0-9]{2}),r15/o; | 
|  | 76 | } else { | 
|  | 77 | print("wrong or unknown architecture\n"); | 
|  | 78 | exit | 
|  | 79 | } | 
|  | 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 | # | 
|  | 90 | # main() | 
|  | 91 | # | 
|  | 92 | my $funcre = qr/^$x* <(.*)>:$/; | 
|  | 93 | my $func; | 
| Randy Dunlap | 8ad2914 | 2006-06-25 05:48:29 -0700 | [diff] [blame] | 94 | my $file, $lastslash; | 
|  | 95 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | while (my $line = <STDIN>) { | 
|  | 97 | if ($line =~ m/$funcre/) { | 
|  | 98 | $func = $1; | 
|  | 99 | } | 
| Randy Dunlap | 8ad2914 | 2006-06-25 05:48:29 -0700 | [diff] [blame] | 100 | elsif ($line =~ m/(.*):\s*file format/) { | 
|  | 101 | $file = $1; | 
|  | 102 | $file =~ s/\.ko//; | 
|  | 103 | $lastslash = rindex($file, "/"); | 
|  | 104 | if ($lastslash != -1) { | 
|  | 105 | $file = substr($file, $lastslash + 1); | 
|  | 106 | } | 
|  | 107 | } | 
|  | 108 | elsif ($line =~ m/$re/) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | my $size = $1; | 
|  | 110 | $size = hex($size) if ($size =~ /^0x/); | 
|  | 111 |  | 
|  | 112 | if ($size > 0xf0000000) { | 
|  | 113 | $size = - $size; | 
|  | 114 | $size += 0x80000000; | 
|  | 115 | $size += 0x80000000; | 
|  | 116 | } | 
|  | 117 | next if ($size > 0x10000000); | 
|  | 118 |  | 
|  | 119 | next if $line !~ m/^($xs*)/; | 
|  | 120 | my $addr = $1; | 
|  | 121 | $addr =~ s/ /0/g; | 
|  | 122 | $addr = "0x$addr"; | 
|  | 123 |  | 
| Randy Dunlap | 8ad2914 | 2006-06-25 05:48:29 -0700 | [diff] [blame] | 124 | my $intro = "$addr $func [$file]:"; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | my $padlen = 56 - length($intro); | 
|  | 126 | while ($padlen > 0) { | 
|  | 127 | $intro .= '	'; | 
|  | 128 | $padlen -= 8; | 
|  | 129 | } | 
|  | 130 | next if ($size < 100); | 
|  | 131 | push @stack, "$intro$size\n"; | 
|  | 132 | } | 
|  | 133 | } | 
|  | 134 |  | 
|  | 135 | print sort bysize @stack; |