xref: /linux-6.15/scripts/checkstack.pl (revision 0df8e970)
1#!/usr/bin/env perl
2# SPDX-License-Identifier: GPL-2.0
3
4#	Check the stack usage of functions
5#
6#	Copyright Joern Engel <[email protected]>
7#	Inspired by Linus Torvalds
8#	Original idea maybe from Keith Owens
9#	s390 port and big speedup by Arnd Bergmann <[email protected]>
10#	Mips port by Juan Quintela <[email protected]>
11#	IA64 port via Andreas Dilger
12#	Arm port by Holger Schurig
13#	Random bits by Matt Mackall <[email protected]>
14#	M68k port by Geert Uytterhoeven and Andreas Schwab
15#	AArch64, PARISC ports by Kyle McMartin
16#	sparc port by Martin Habets <[email protected]>
17#	ppc64le port by Breno Leitao <[email protected]>
18#	riscv port by Wadim Mueller <[email protected]>
19#
20#	Usage:
21#	objdump -d vmlinux | scripts/checkstack.pl [arch]
22#
23#	TODO :	Port to all architectures (one regex per arch)
24
25use strict;
26
27# check for arch
28#
29# $re is used for two matches:
30# $& (whole re) matches the complete objdump line with the stack growth
31# $1 (first bracket) matches the size of the stack growth
32#
33# $dre is similar, but for dynamic stack redutions:
34# $& (whole re) matches the complete objdump line with the stack growth
35# $1 (first bracket) matches the dynamic amount of the stack growth
36#
37# $sub: subroutine for special handling to check stack usage.
38#
39# use anything else and feel the pain ;)
40my (@stack, $re, $dre, $sub, $x, $xs, $funcre, $min_stack);
41{
42	my $arch = shift;
43	if ($arch eq "") {
44		$arch = `uname -m`;
45		chomp($arch);
46	}
47
48	$min_stack = shift;
49	if ($min_stack eq "" || $min_stack !~ /^\d+$/) {
50		$min_stack = 100;
51	}
52
53	$x	= "[0-9a-f]";	# hex character
54	$xs	= "[0-9a-f ]";	# hex character or space
55	$funcre = qr/^$x* <(.*)>:$/;
56	if ($arch =~ '^(aarch|arm)64$') {
57		#ffffffc0006325cc:       a9bb7bfd        stp     x29, x30, [sp, #-80]!
58		#a110:       d11643ff        sub     sp, sp, #0x590
59		$re = qr/^.*stp.*sp, \#-([0-9]{1,8})\]\!/o;
60		$dre = qr/^.*sub.*sp, sp, #(0x$x{1,8})/o;
61	} elsif ($arch eq 'arm') {
62		#c0008ffc:	e24dd064	sub	sp, sp, #100	; 0x64
63		$re = qr/.*sub.*sp, sp, #([0-9]{1,4})/o;
64		$sub = \&arm_push_handling;
65	} elsif ($arch =~ /^x86(_64)?$/ || $arch =~ /^i[3456]86$/) {
66		#c0105234:       81 ec ac 05 00 00       sub    $0x5ac,%esp
67		# or
68		#    2f60:    48 81 ec e8 05 00 00       sub    $0x5e8,%rsp
69		$re = qr/^.*[as][du][db]    \$(0x$x{1,8}),\%(e|r)sp$/o;
70		$dre = qr/^.*[as][du][db]    (%.*),\%(e|r)sp$/o;
71	} elsif ($arch eq 'm68k') {
72		#    2b6c:       4e56 fb70       linkw %fp,#-1168
73		#  1df770:       defc ffe4       addaw #-28,%sp
74		$re = qr/.*(?:linkw %fp,|addaw )#-([0-9]{1,4})(?:,%sp)?$/o;
75	} elsif ($arch eq 'mips64') {
76		#8800402c:       67bdfff0        daddiu  sp,sp,-16
77		$re = qr/.*daddiu.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
78	} elsif ($arch eq 'mips') {
79		#88003254:       27bdffe0        addiu   sp,sp,-32
80		$re = qr/.*addiu.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
81	} elsif ($arch eq 'nios2') {
82		#25a8:	defffb04 	addi	sp,sp,-20
83		$re = qr/.*addi.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
84	} elsif ($arch eq 'openrisc') {
85		# c000043c:       9c 21 fe f0     l.addi r1,r1,-272
86		$re = qr/.*l\.addi.*r1,r1,-(([0-9]{2}|[3-9])[0-9]{2})/o;
87	} elsif ($arch eq 'parisc' || $arch eq 'parisc64') {
88		$re = qr/.*ldo ($x{1,8})\(sp\),sp/o;
89	} elsif ($arch eq 'powerpc' || $arch =~ /^ppc(64)?(le)?$/ ) {
90		# powerpc    : 94 21 ff 30     stwu    r1,-208(r1)
91		# ppc64(le)  : 81 ff 21 f8     stdu    r1,-128(r1)
92		$re = qr/.*st[dw]u.*r1,-($x{1,8})\(r1\)/o;
93	} elsif ($arch =~ /^s390x?$/) {
94		#   11160:       a7 fb ff 60             aghi   %r15,-160
95		# or
96		#  100092:	 e3 f0 ff c8 ff 71	 lay	 %r15,-56(%r15)
97		$re = qr/.*(?:lay|ag?hi).*\%r15,-([0-9]+)(?:\(\%r15\))?$/o;
98	} elsif ($arch eq 'sparc' || $arch eq 'sparc64') {
99		# f0019d10:       9d e3 bf 90     save  %sp, -112, %sp
100		$re = qr/.*save.*%sp, -(([0-9]{2}|[3-9])[0-9]{2}), %sp/o;
101	} elsif ($arch =~ /^riscv(64)?$/) {
102		#ffffffff8036e868:	c2010113          	addi	sp,sp,-992
103		$re = qr/.*addi.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
104	} else {
105		print("wrong or unknown architecture \"$arch\"\n");
106		exit
107	}
108}
109
110#
111# To count stack usage of push {*, fp, ip, lr, pc} instruction in ARM,
112# if FRAME POINTER is enabled.
113# e.g. c01f0d48: e92ddff0 push {r4, r5, r6, r7, r8, r9, sl, fp, ip, lr, pc}
114#
115sub arm_push_handling {
116	my $regex = qr/.*push.*fp, ip, lr, pc}/o;
117	my $size = 0;
118	my $line_arg = shift;
119
120	if ($line_arg =~ m/$regex/) {
121		$size = $line_arg =~ tr/,//;
122		$size = ($size + 1) * 4;
123	}
124
125	return $size;
126}
127
128#
129# main()
130#
131my ($func, $file, $lastslash, $total_size, $addr, $intro);
132
133$total_size = 0;
134
135while (my $line = <STDIN>) {
136	if ($line =~ m/$funcre/) {
137		$func = $1;
138		next if $line !~ m/^($xs*)/;
139		if ($total_size > $min_stack) {
140			push @stack, "$intro$total_size\n";
141		}
142
143		$addr = $1;
144		$addr =~ s/ /0/g;
145		$addr = "0x$addr";
146
147		$intro = "$addr $func [$file]:";
148		my $padlen = 56 - length($intro);
149		while ($padlen > 0) {
150			$intro .= '	';
151			$padlen -= 8;
152		}
153
154		$total_size = 0;
155	}
156	elsif ($line =~ m/(.*):\s*file format/) {
157		$file = $1;
158		$file =~ s/\.ko//;
159		$lastslash = rindex($file, "/");
160		if ($lastslash != -1) {
161			$file = substr($file, $lastslash + 1);
162		}
163	}
164	elsif ($line =~ m/$re/) {
165		my $size = $1;
166		$size = hex($size) if ($size =~ /^0x/);
167
168		if ($size > 0xf0000000) {
169			$size = - $size;
170			$size += 0x80000000;
171			$size += 0x80000000;
172		}
173		next if ($size > 0x10000000);
174
175		$total_size += $size;
176	}
177	elsif (defined $dre && $line =~ m/$dre/) {
178		my $size = $1;
179
180		$size = hex($size) if ($size =~ /^0x/);
181		$total_size += $size;
182	}
183	elsif (defined $sub) {
184		my $size = &$sub($line);
185
186		$total_size += $size;
187	}
188}
189if ($total_size > $min_stack) {
190	push @stack, "$intro$total_size\n";
191}
192
193# Sort output by size (last field)
194print sort { ($b =~ /:\t*(\d+)$/)[0] <=> ($a =~ /:\t*(\d+)$/)[0] } @stack;
195