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 ;) 38690998b6SJames Hoganmy (@stack, $re, $dre, $x, $xs, $funcre); 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 461da177e4SLinus Torvalds $x = "[0-9a-f]"; # hex character 471da177e4SLinus Torvalds $xs = "[0-9a-f ]"; # hex character or space 48690998b6SJames Hogan $funcre = qr/^$x* <(.*)>:$/; 494f45d62aSGeorge G. Davis if ($arch =~ '^(aarch|arm)64$') { 50208ad001SKyle McMartin #ffffffc0006325cc: a9bb7bfd stp x29, x30, [sp, #-80]! 51919e9d39SQian Cai #a110: d11643ff sub sp, sp, #0x590 52208ad001SKyle McMartin $re = qr/^.*stp.*sp, \#-([0-9]{1,8})\]\!/o; 53919e9d39SQian Cai $dre = qr/^.*sub.*sp, sp, #(0x$x{1,8})/o; 54208ad001SKyle McMartin } elsif ($arch eq 'arm') { 551da177e4SLinus Torvalds #c0008ffc: e24dd064 sub sp, sp, #100 ; 0x64 561da177e4SLinus Torvalds $re = qr/.*sub.*sp, sp, #(([0-9]{2}|[3-9])[0-9]{2})/o; 57fda9f990SKonstantin Khlebnikov } elsif ($arch =~ /^x86(_64)?$/ || $arch =~ /^i[3456]86$/) { 581da177e4SLinus Torvalds #c0105234: 81 ec ac 05 00 00 sub $0x5ac,%esp 59fda9f990SKonstantin Khlebnikov # or 601da177e4SLinus Torvalds # 2f60: 48 81 ec e8 05 00 00 sub $0x5e8,%rsp 61fda9f990SKonstantin Khlebnikov $re = qr/^.*[as][du][db] \$(0x$x{1,8}),\%(e|r)sp$/o; 62fda9f990SKonstantin Khlebnikov $dre = qr/^.*[as][du][db] (%.*),\%(e|r)sp$/o; 631da177e4SLinus Torvalds } elsif ($arch eq 'ia64') { 641da177e4SLinus Torvalds #e0000000044011fc: 01 0f fc 8c adds r12=-384,r12 651da177e4SLinus Torvalds $re = qr/.*adds.*r12=-(([0-9]{2}|[3-9])[0-9]{2}),r12/o; 661da177e4SLinus Torvalds } elsif ($arch eq 'm68k') { 671da177e4SLinus Torvalds # 2b6c: 4e56 fb70 linkw %fp,#-1168 681da177e4SLinus Torvalds # 1df770: defc ffe4 addaw #-28,%sp 691da177e4SLinus Torvalds $re = qr/.*(?:linkw %fp,|addaw )#-([0-9]{1,4})(?:,%sp)?$/o; 701da177e4SLinus Torvalds } elsif ($arch eq 'mips64') { 711da177e4SLinus Torvalds #8800402c: 67bdfff0 daddiu sp,sp,-16 721da177e4SLinus Torvalds $re = qr/.*daddiu.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o; 731da177e4SLinus Torvalds } elsif ($arch eq 'mips') { 741da177e4SLinus Torvalds #88003254: 27bdffe0 addiu sp,sp,-32 751da177e4SLinus Torvalds $re = qr/.*addiu.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o; 760e5a47a8STobias Klauser } elsif ($arch eq 'nios2') { 770e5a47a8STobias Klauser #25a8: defffb04 addi sp,sp,-20 780e5a47a8STobias Klauser $re = qr/.*addi.*sp,sp,-(([0-9]{2}|[3-9])[0-9]{2})/o; 79f72deab3SStafford Horne } elsif ($arch eq 'openrisc') { 80f72deab3SStafford Horne # c000043c: 9c 21 fe f0 l.addi r1,r1,-272 81f72deab3SStafford Horne $re = qr/.*l\.addi.*r1,r1,-(([0-9]{2}|[3-9])[0-9]{2})/o; 82562d139cSKyle McMartin } elsif ($arch eq 'parisc' || $arch eq 'parisc64') { 83562d139cSKyle McMartin $re = qr/.*ldo ($x{1,8})\(sp\),sp/o; 848449a4cbSBreno Leitao } elsif ($arch eq 'powerpc' || $arch =~ /^ppc(64)?(le)?$/ ) { 858449a4cbSBreno Leitao # powerpc : 94 21 ff 30 stwu r1,-208(r1) 868449a4cbSBreno Leitao # ppc64(le) : 81 ff 21 f8 stdu r1,-128(r1) 87271c511dSJohannes Berg $re = qr/.*st[dw]u.*r1,-($x{1,8})\(r1\)/o; 881da177e4SLinus Torvalds } elsif ($arch =~ /^s390x?$/) { 891da177e4SLinus Torvalds # 11160: a7 fb ff 60 aghi %r15,-160 9089d49841SChristian Borntraeger # or 9189d49841SChristian Borntraeger # 100092: e3 f0 ff c8 ff 71 lay %r15,-56(%r15) 9289d49841SChristian Borntraeger $re = qr/.*(?:lay|ag?hi).*\%r15,-(([0-9]{2}|[3-9])[0-9]{2}) 9389d49841SChristian Borntraeger (?:\(\%r15\))?$/ox; 941da177e4SLinus Torvalds } elsif ($arch =~ /^sh64$/) { 951da177e4SLinus Torvalds #XXX: we only check for the immediate case presently, 961da177e4SLinus Torvalds # though we will want to check for the movi/sub 971da177e4SLinus Torvalds # pair for larger users. -- PFM. 981da177e4SLinus Torvalds #a00048e0: d4fc40f0 addi.l r15,-240,r15 991da177e4SLinus Torvalds $re = qr/.*addi\.l.*r15,-(([0-9]{2}|[3-9])[0-9]{2}),r15/o; 100d41e2d73SMartin Habets } elsif ($arch eq 'sparc' || $arch eq 'sparc64') { 101d41e2d73SMartin Habets # f0019d10: 9d e3 bf 90 save %sp, -112, %sp 102d41e2d73SMartin Habets $re = qr/.*save.*%sp, -(([0-9]{2}|[3-9])[0-9]{2}), %sp/o; 1031da177e4SLinus Torvalds } else { 104abddaec5SEric Sandeen print("wrong or unknown architecture \"$arch\"\n"); 1051da177e4SLinus Torvalds exit 1061da177e4SLinus Torvalds } 1071da177e4SLinus Torvalds} 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds# 1101da177e4SLinus Torvalds# main() 1111da177e4SLinus Torvalds# 112*677f1410SManinder Singhmy ($func, $file, $lastslash, $total_size, $addr, $intro); 113*677f1410SManinder Singh 114*677f1410SManinder Singh$total_size = 0; 1158ad2914dSRandy Dunlap 1161da177e4SLinus Torvaldswhile (my $line = <STDIN>) { 1171da177e4SLinus Torvalds if ($line =~ m/$funcre/) { 1181da177e4SLinus Torvalds $func = $1; 119*677f1410SManinder Singh next if $line !~ m/^($xs*)/; 120*677f1410SManinder Singh if ($total_size > 100) { 121*677f1410SManinder Singh push @stack, "$intro$total_size\n"; 122*677f1410SManinder Singh } 123*677f1410SManinder Singh 124*677f1410SManinder Singh $addr = $1; 125*677f1410SManinder Singh $addr =~ s/ /0/g; 126*677f1410SManinder Singh $addr = "0x$addr"; 127*677f1410SManinder Singh 128*677f1410SManinder Singh $intro = "$addr $func [$file]:"; 129*677f1410SManinder Singh my $padlen = 56 - length($intro); 130*677f1410SManinder Singh while ($padlen > 0) { 131*677f1410SManinder Singh $intro .= ' '; 132*677f1410SManinder Singh $padlen -= 8; 133*677f1410SManinder Singh } 134*677f1410SManinder Singh 135*677f1410SManinder Singh $total_size = 0; 1361da177e4SLinus Torvalds } 1378ad2914dSRandy Dunlap elsif ($line =~ m/(.*):\s*file format/) { 1388ad2914dSRandy Dunlap $file = $1; 1398ad2914dSRandy Dunlap $file =~ s/\.ko//; 1408ad2914dSRandy Dunlap $lastslash = rindex($file, "/"); 1418ad2914dSRandy Dunlap if ($lastslash != -1) { 1428ad2914dSRandy Dunlap $file = substr($file, $lastslash + 1); 1438ad2914dSRandy Dunlap } 1448ad2914dSRandy Dunlap } 1458ad2914dSRandy Dunlap elsif ($line =~ m/$re/) { 1461da177e4SLinus Torvalds my $size = $1; 1471da177e4SLinus Torvalds $size = hex($size) if ($size =~ /^0x/); 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds if ($size > 0xf0000000) { 1501da177e4SLinus Torvalds $size = - $size; 1511da177e4SLinus Torvalds $size += 0x80000000; 1521da177e4SLinus Torvalds $size += 0x80000000; 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds next if ($size > 0x10000000); 1551da177e4SLinus Torvalds 156*677f1410SManinder Singh $total_size += $size; 1571da177e4SLinus Torvalds } 158585e93aeSEric Sandeen elsif (defined $dre && $line =~ m/$dre/) { 159*677f1410SManinder Singh my $size = $1; 160585e93aeSEric Sandeen 161*677f1410SManinder Singh $size = hex($size) if ($size =~ /^0x/); 162*677f1410SManinder Singh $total_size += $size; 163585e93aeSEric Sandeen } 164585e93aeSEric Sandeen} 165*677f1410SManinder Singhif ($total_size > 100) { 166*677f1410SManinder Singh push @stack, "$intro$total_size\n"; 1671da177e4SLinus Torvalds} 1681da177e4SLinus Torvalds 1691f2a144fSStephen Hemminger# Sort output by size (last field) 1701f2a144fSStephen Hemmingerprint sort { ($b =~ /:\t*(\d+)$/)[0] <=> ($a =~ /:\t*(\d+)$/)[0] } @stack; 171