xref: /linux-6.15/scripts/checkstack.pl (revision 572220aa)
1cb77f0d6SKamil Rytarowski#!/usr/bin/env perl
2b2441318SGreg Kroah-Hartman# SPDX-License-Identifier: GPL-2.0
31da177e4SLinus Torvalds
41da177e4SLinus Torvalds#	Check the stack usage of functions
51da177e4SLinus Torvalds#
62b54aaefSJoern Engel#	Copyright Joern Engel <[email protected]>
71da177e4SLinus Torvalds#	Inspired by Linus Torvalds
81da177e4SLinus Torvalds#	Original idea maybe from Keith Owens
91da177e4SLinus Torvalds#	s390 port and big speedup by Arnd Bergmann <[email protected]>
101da177e4SLinus Torvalds#	Mips port by Juan Quintela <[email protected]>
111da177e4SLinus Torvalds#	IA64 port via Andreas Dilger
121da177e4SLinus Torvalds#	Arm port by Holger Schurig
131da177e4SLinus Torvalds#	sh64 port by Paul Mundt
141da177e4SLinus Torvalds#	Random bits by Matt Mackall <[email protected]>
151da177e4SLinus Torvalds#	M68k port by Geert Uytterhoeven and Andreas Schwab
16208ad001SKyle McMartin#	AArch64, PARISC ports by Kyle McMartin
17d41e2d73SMartin Habets#	sparc port by Martin Habets <[email protected]>
188449a4cbSBreno Leitao#	ppc64le port by Breno Leitao <[email protected]>
191da177e4SLinus Torvalds#
201da177e4SLinus Torvalds#	Usage:
21477116e6SJoern Engel#	objdump -d vmlinux | scripts/checkstack.pl [arch]
221da177e4SLinus Torvalds#
231da177e4SLinus Torvalds#	TODO :	Port to all architectures (one regex per arch)
241da177e4SLinus Torvalds
251f2a144fSStephen Hemmingeruse strict;
261f2a144fSStephen Hemminger
271da177e4SLinus Torvalds# check for arch
281da177e4SLinus Torvalds#
291da177e4SLinus Torvalds# $re is used for two matches:
301da177e4SLinus Torvalds# $& (whole re) matches the complete objdump line with the stack growth
311da177e4SLinus Torvalds# $1 (first bracket) matches the size of the stack growth
321da177e4SLinus Torvalds#
33585e93aeSEric Sandeen# $dre is similar, but for dynamic stack redutions:
34585e93aeSEric Sandeen# $& (whole re) matches the complete objdump line with the stack growth
35585e93aeSEric Sandeen# $1 (first bracket) matches the dynamic amount of the stack growth
36585e93aeSEric Sandeen#
371da177e4SLinus Torvalds# use anything else and feel the pain ;)
38*572220aaSManinder Singhmy (@stack, $re, $dre, $x, $xs, $funcre, $min_stack);
391da177e4SLinus Torvalds{
401da177e4SLinus Torvalds	my $arch = shift;
411da177e4SLinus Torvalds	if ($arch eq "") {
421da177e4SLinus Torvalds		$arch = `uname -m`;
43abddaec5SEric Sandeen		chomp($arch);
441da177e4SLinus Torvalds	}
451da177e4SLinus Torvalds
46*572220aaSManinder Singh	$min_stack = shift;
47*572220aaSManinder Singh	if ($min_stack eq "" || $min_stack !~ /^\d+$/) {
48*572220aaSManinder Singh		$min_stack = 100;
49*572220aaSManinder Singh	}
50*572220aaSManinder Singh
511da177e4SLinus Torvalds	$x	= "[0-9a-f]";	# hex character
521da177e4SLinus Torvalds	$xs	= "[0-9a-f ]";	# hex character or space
53690998b6SJames Hogan	$funcre = qr/^$x* <(.*)>:$/;
544f45d62aSGeorge G. Davis	if ($arch =~ '^(aarch|arm)64$') {
55208ad001SKyle McMartin		#ffffffc0006325cc:       a9bb7bfd        stp     x29, x30, [sp, #-80]!
56919e9d39SQian Cai		#a110:       d11643ff        sub     sp, sp, #0x590
57208ad001SKyle McMartin		$re = qr/^.*stp.*sp, \#-([0-9]{1,8})\]\!/o;
58919e9d39SQian Cai		$dre = qr/^.*sub.*sp, sp, #(0x$x{1,8})/o;
59208ad001SKyle McMartin	} elsif ($arch eq 'arm') {
601da177e4SLinus Torvalds		#c0008ffc:	e24dd064	sub	sp, sp, #100	; 0x64
611da177e4SLinus Torvalds		$re = qr/.*sub.*sp, sp, #(([0-9]{2}|[3-9])[0-9]{2})/o;
62fda9f990SKonstantin Khlebnikov	} elsif ($arch =~ /^x86(_64)?$/ || $arch =~ /^i[3456]86$/) {
631da177e4SLinus Torvalds		#c0105234:       81 ec ac 05 00 00       sub    $0x5ac,%esp
64fda9f990SKonstantin Khlebnikov		# or
651da177e4SLinus Torvalds		#    2f60:    48 81 ec e8 05 00 00       sub    $0x5e8,%rsp
66fda9f990SKonstantin Khlebnikov		$re = qr/^.*[as][du][db]    \$(0x$x{1,8}),\%(e|r)sp$/o;
67fda9f990SKonstantin Khlebnikov		$dre = qr/^.*[as][du][db]    (%.*),\%(e|r)sp$/o;
681da177e4SLinus Torvalds	} elsif ($arch eq 'ia64') {
691da177e4SLinus Torvalds		#e0000000044011fc:       01 0f fc 8c     adds r12=-384,r12
701da177e4SLinus Torvalds		$re = qr/.*adds.*r12=-(([0-9]{2}|[3-9])[0-9]{2}),r12/o;
711da177e4SLinus Torvalds	} elsif ($arch eq 'm68k') {
721da177e4SLinus Torvalds		#    2b6c:       4e56 fb70       linkw %fp,#-1168
731da177e4SLinus Torvalds		#  1df770:       defc ffe4       addaw #-28,%sp
741da177e4SLinus Torvalds		$re = qr/.*(?:linkw %fp,|addaw )#-([0-9]{1,4})(?:,%sp)?$/o;
751da177e4SLinus Torvalds	} elsif ($arch eq 'mips64') {
761da177e4SLinus Torvalds		#8800402c:       67bdfff0        daddiu  sp,sp,-16
771da177e4SLinus Torvalds		$re = qr/.*daddiu.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
781da177e4SLinus Torvalds	} elsif ($arch eq 'mips') {
791da177e4SLinus Torvalds		#88003254:       27bdffe0        addiu   sp,sp,-32
801da177e4SLinus Torvalds		$re = qr/.*addiu.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
810e5a47a8STobias Klauser	} elsif ($arch eq 'nios2') {
820e5a47a8STobias Klauser		#25a8:	defffb04 	addi	sp,sp,-20
830e5a47a8STobias Klauser		$re = qr/.*addi.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o;
84f72deab3SStafford Horne	} elsif ($arch eq 'openrisc') {
85f72deab3SStafford Horne		# c000043c:       9c 21 fe f0     l.addi r1,r1,-272
86f72deab3SStafford Horne		$re = qr/.*l\.addi.*r1,r1,-(([0-9]{2}|[3-9])[0-9]{2})/o;
87562d139cSKyle McMartin	} elsif ($arch eq 'parisc' || $arch eq 'parisc64') {
88562d139cSKyle McMartin		$re = qr/.*ldo ($x{1,8})\(sp\),sp/o;
898449a4cbSBreno Leitao	} elsif ($arch eq 'powerpc' || $arch =~ /^ppc(64)?(le)?$/ ) {
908449a4cbSBreno Leitao		# powerpc    : 94 21 ff 30     stwu    r1,-208(r1)
918449a4cbSBreno Leitao		# ppc64(le)  : 81 ff 21 f8     stdu    r1,-128(r1)
92271c511dSJohannes Berg		$re = qr/.*st[dw]u.*r1,-($x{1,8})\(r1\)/o;
931da177e4SLinus Torvalds	} elsif ($arch =~ /^s390x?$/) {
941da177e4SLinus Torvalds		#   11160:       a7 fb ff 60             aghi   %r15,-160
9589d49841SChristian Borntraeger		# or
9689d49841SChristian Borntraeger		#  100092:	 e3 f0 ff c8 ff 71	 lay	 %r15,-56(%r15)
9789d49841SChristian Borntraeger		$re = qr/.*(?:lay|ag?hi).*\%r15,-(([0-9]{2}|[3-9])[0-9]{2})
9889d49841SChristian Borntraeger		      (?:\(\%r15\))?$/ox;
991da177e4SLinus Torvalds	} elsif ($arch =~ /^sh64$/) {
1001da177e4SLinus Torvalds		#XXX: we only check for the immediate case presently,
1011da177e4SLinus Torvalds		#     though we will want to check for the movi/sub
1021da177e4SLinus Torvalds		#     pair for larger users. -- PFM.
1031da177e4SLinus Torvalds		#a00048e0:       d4fc40f0        addi.l  r15,-240,r15
1041da177e4SLinus Torvalds		$re = qr/.*addi\.l.*r15,-(([0-9]{2}|[3-9])[0-9]{2}),r15/o;
105d41e2d73SMartin Habets	} elsif ($arch eq 'sparc' || $arch eq 'sparc64') {
106d41e2d73SMartin Habets		# f0019d10:       9d e3 bf 90     save  %sp, -112, %sp
107d41e2d73SMartin Habets		$re = qr/.*save.*%sp, -(([0-9]{2}|[3-9])[0-9]{2}), %sp/o;
1081da177e4SLinus Torvalds	} else {
109abddaec5SEric Sandeen		print("wrong or unknown architecture \"$arch\"\n");
1101da177e4SLinus Torvalds		exit
1111da177e4SLinus Torvalds	}
1121da177e4SLinus Torvalds}
1131da177e4SLinus Torvalds
1141da177e4SLinus Torvalds#
1151da177e4SLinus Torvalds# main()
1161da177e4SLinus Torvalds#
117677f1410SManinder Singhmy ($func, $file, $lastslash, $total_size, $addr, $intro);
118677f1410SManinder Singh
119677f1410SManinder Singh$total_size = 0;
1208ad2914dSRandy Dunlap
1211da177e4SLinus Torvaldswhile (my $line = <STDIN>) {
1221da177e4SLinus Torvalds	if ($line =~ m/$funcre/) {
1231da177e4SLinus Torvalds		$func = $1;
124677f1410SManinder Singh		next if $line !~ m/^($xs*)/;
125*572220aaSManinder Singh		if ($total_size > $min_stack) {
126677f1410SManinder Singh			push @stack, "$intro$total_size\n";
127677f1410SManinder Singh		}
128677f1410SManinder Singh
129677f1410SManinder Singh		$addr = $1;
130677f1410SManinder Singh		$addr =~ s/ /0/g;
131677f1410SManinder Singh		$addr = "0x$addr";
132677f1410SManinder Singh
133677f1410SManinder Singh		$intro = "$addr $func [$file]:";
134677f1410SManinder Singh		my $padlen = 56 - length($intro);
135677f1410SManinder Singh		while ($padlen > 0) {
136677f1410SManinder Singh			$intro .= '	';
137677f1410SManinder Singh			$padlen -= 8;
138677f1410SManinder Singh		}
139677f1410SManinder Singh
140677f1410SManinder Singh		$total_size = 0;
1411da177e4SLinus Torvalds	}
1428ad2914dSRandy Dunlap	elsif ($line =~ m/(.*):\s*file format/) {
1438ad2914dSRandy Dunlap		$file = $1;
1448ad2914dSRandy Dunlap		$file =~ s/\.ko//;
1458ad2914dSRandy Dunlap		$lastslash = rindex($file, "/");
1468ad2914dSRandy Dunlap		if ($lastslash != -1) {
1478ad2914dSRandy Dunlap			$file = substr($file, $lastslash + 1);
1488ad2914dSRandy Dunlap		}
1498ad2914dSRandy Dunlap	}
1508ad2914dSRandy Dunlap	elsif ($line =~ m/$re/) {
1511da177e4SLinus Torvalds		my $size = $1;
1521da177e4SLinus Torvalds		$size = hex($size) if ($size =~ /^0x/);
1531da177e4SLinus Torvalds
1541da177e4SLinus Torvalds		if ($size > 0xf0000000) {
1551da177e4SLinus Torvalds			$size = - $size;
1561da177e4SLinus Torvalds			$size += 0x80000000;
1571da177e4SLinus Torvalds			$size += 0x80000000;
1581da177e4SLinus Torvalds		}
1591da177e4SLinus Torvalds		next if ($size > 0x10000000);
1601da177e4SLinus Torvalds
161677f1410SManinder Singh		$total_size += $size;
1621da177e4SLinus Torvalds	}
163585e93aeSEric Sandeen	elsif (defined $dre && $line =~ m/$dre/) {
164677f1410SManinder Singh		my $size = $1;
165585e93aeSEric Sandeen
166677f1410SManinder Singh		$size = hex($size) if ($size =~ /^0x/);
167677f1410SManinder Singh		$total_size += $size;
168585e93aeSEric Sandeen	}
169585e93aeSEric Sandeen}
170*572220aaSManinder Singhif ($total_size > $min_stack) {
171677f1410SManinder Singh	push @stack, "$intro$total_size\n";
1721da177e4SLinus Torvalds}
1731da177e4SLinus Torvalds
1741f2a144fSStephen Hemminger# Sort output by size (last field)
1751f2a144fSStephen Hemmingerprint sort { ($b =~ /:\t*(\d+)$/)[0] <=> ($a =~ /:\t*(\d+)$/)[0] } @stack;
176