This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / scripts / checkstack.pl
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 #       Random bits by Matt Mackall <mpm@selenic.com>
13 #
14 #       Usage:
15 #       objdump -d vmlinux | stackcheck_ppc.pl [arch]
16 #
17 #       TODO :  Port to all architectures (one regex per arch)
18
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 {
27         my $arch = shift;
28         if ($arch eq "") {
29                 $arch = `uname -m`;
30         }
31
32         $x      = "[0-9a-f]";   # hex character
33         $xs     = "[0-9a-f ]";  # hex character or space
34         if ($arch =~ /^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 =~ /^ia64$/) {
41                 #e0000000044011fc:       01 0f fc 8c     adds r12=-384,r12
42                 $re = qr/.*adds.*r12=-(([0-9]{2}|[3-9])[0-9]{2}),r12/o;
43         } elsif ($arch =~ /^mips64$/) {
44                 #8800402c:       67bdfff0        daddiu  sp,sp,-16
45                 $re = qr/.*daddiu.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
46         } elsif ($arch =~ /^mips$/) {
47                 #88003254:       27bdffe0        addiu   sp,sp,-32
48                 $re = qr/.*addiu.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
49         } elsif ($arch =~ /^ppc$/) {
50                 #c00029f4:       94 21 ff 30     stwu    r1,-208(r1)
51                 $re = qr/.*stwu.*r1,-($x{1,8})\(r1\)/o;
52         } elsif ($arch =~ /^ppc64$/) {
53                 #XXX
54                 $re = qr/.*stdu.*r1,-($x{1,8})\(r1\)/o;
55         } elsif ($arch =~ /^s390x?$/) {
56                 #   11160:       a7 fb ff 60             aghi   %r15,-160
57                 $re = qr/.*ag?hi.*\%r15,-(([0-9]{2}|[3-9])[0-9]{2})/o;
58         } else {
59                 print("wrong or unknown architecture\n");
60                 exit
61         }
62 }
63
64 sub bysize($) {
65         ($asize = $a) =~ s/.*   +(.*)$/$1/;
66         ($bsize = $b) =~ s/.*   +(.*)$/$1/;
67         $bsize <=> $asize
68 }
69
70 #
71 # main()
72 #
73 $funcre = qr/^$x* \<(.*)\>:$/;
74 while ($line = <STDIN>) {
75         if ($line =~ m/$funcre/) {
76                 $func = $1;
77         }
78         if ($line =~ m/$re/) {
79                 my $size = $1;
80                 $size = hex($size) if ($size =~ /^0x/);
81
82                 if ($size > 0x80000000) {
83                         $size = - $size;
84                         $size += 0x80000000;
85                         $size += 0x80000000;
86                 }
87
88                 $line =~ m/^($xs*).*/;
89                 my $addr = $1;
90                 $addr =~ s/ /0/g;
91                 $addr = "0x$addr";
92
93                 my $intro = "$addr $func:";
94                 my $padlen = 56 - length($intro);
95                 while ($padlen > 0) {
96                         $intro .= '     ';
97                         $padlen -= 8;
98                 }
99                 next if ($size < 100);
100                 $stack[@stack] = "$intro$size\n";
101         }
102 }
103
104 @sortedstack = sort bysize @stack;
105
106 foreach $i (@sortedstack) {
107         print("$i");
108 }