From: Breno Rodrigues Alves <breno3011alves@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Andy Whitcroft <apw@canonical.com>, Joe Perches <joe@perches.com>,
Dwaipayan Ray <dwaipayanray1@gmail.com>,
Lukas Bulwahn <lukas.bulwahn@gmail.com>,
Breno Rodrigues Alves <breno3011alves@gmail.com>
Subject: [RFC PATCH 1/7] scripts: convert checkstack.pl to Python
Date: Sun, 18 Oct 2026 08:28:32 -0300 [thread overview]
Message-ID: <20261018112838.16355-2-breno3011alves@gmail.com> (raw)
In-Reply-To: <20261018112838.16355-1-breno3011alves@gmail.com>
Faithful re-implementation of checkstack.pl in Python 3
(stdlib only). Same per-architecture regexes for x86/x86_64,
arm, arm64, m68k, mips, nios2, openrisc, parisc, powerpc,
s390, sparc, riscv and loongarch.
Verified byte-for-byte identical to the Perl version on a
synthetic objdump stream for x86_64.
Signed-off-by: Breno Rodrigues Alves <breno3011alves@gmail.com>
---
scripts/checkstack.py | 143 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 143 insertions(+)
create mode 100755 scripts/checkstack.py
diff --git a/scripts/checkstack.py b/scripts/checkstack.py
new file mode 100755
index 000000000..8be3861b3
--- /dev/null
+++ b/scripts/checkstack.py
@@ -0,0 +1,143 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0
+#
+# Check the stack usage of functions.
+# Usage: objdump -d vmlinux | checkstack.py [arch] [min_stack]
+
+import platform
+import re
+import sys
+
+X = '[0-9a-f]'
+RE_FUNC = re.compile(rf'^({X}*) <(.*)>:$')
+RE_FILE = re.compile(r'(.*):\s*file format')
+
+
+def arm_push_handler(line):
+ if re.search(r'.*push.*fp, ip, lr, pc}', line):
+ return (line.count(',') + 1) * 4
+ return 0
+
+
+def config_for(arch):
+ """Return (main_re, dynamic_re, sub_fn)."""
+ if re.match(r'^(?:aarch|arm)64$', arch):
+ return (
+ re.compile(rf'^.*stp.*sp, ?#-([0-9]{{1,8}})\]!'),
+ re.compile(rf'^.*sub.*sp, sp, #(0x{X}{{1,8}})'),
+ None)
+ if arch == 'arm':
+ return (re.compile(r'.*sub.*sp, sp, #([0-9]{1,4})'), None,
+ arm_push_handler)
+ if re.match(r'^x86(?:_64)?$', arch) or re.match(r'^i[3456]86$', arch):
+ return (
+ re.compile(rf'^.*[as][du][db] \$(0x{X}{{1,8}}),%(?:e|r)sp$'),
+ re.compile(r'^.*[as][du][db] (%.*),%(?:e|r)sp$'),
+ None)
+ if arch == 'm68k':
+ return (re.compile(r'.*(?:linkw %fp,|addaw )#-([0-9]{1,4})'
+ r'(?:,%sp)?$'), None, None)
+ if arch == 'mips64':
+ return (re.compile(r'.*daddiu.*sp,sp,-([0-9]{1,8})'), None, None)
+ if arch == 'mips':
+ return (re.compile(r'.*addiu.*sp,sp,-([0-9]{1,8})'), None, None)
+ if arch == 'nios2':
+ return (re.compile(r'.*addi.*sp,sp,-([0-9]{1,8})'), None, None)
+ if arch == 'openrisc':
+ return (re.compile(r'.*l\.addi.*r1,r1,-([0-9]{1,8})'), None, None)
+ if arch in ('parisc', 'parisc64'):
+ return (re.compile(rf'.*ldo ({X}{{1,8}})\(sp\),sp'), None, None)
+ if arch == 'powerpc' or re.match(r'^ppc(?:64)?(?:le)?$', arch):
+ return (re.compile(rf'.*st[dw]u.*r1,-({X}{{1,8}})\(r1\)'),
+ None, None)
+ if re.match(r'^s390x?$', arch):
+ return (re.compile(r'.*(?:lay|ag?hi).*\%r15,-([0-9]+)'
+ r'(?:\(\%r15\))?$'), None, None)
+ if arch in ('sparc', 'sparc64'):
+ return (re.compile(r'.*save.*%sp, -([0-9]{1,8}), %sp'), None, None)
+ if re.match(r'^riscv(?:64)?$', arch):
+ return (re.compile(r'.*addi.*sp,sp,-([0-9]{1,8})'), None, None)
+ if re.match(r'^loongarch(?:32|64)?$', arch):
+ return (re.compile(r'.*addi\..*sp, .*sp, -([0-9]{1,8}).*'),
+ None, None)
+ print(f'wrong or unknown architecture "{arch}"')
+ sys.exit(1)
+
+
+def parse_size(s):
+ return int(s, 16) if s.startswith('0x') else int(s)
+
+
+def main(argv):
+ arch = argv[0] if argv else platform.machine()
+ min_stack = int(argv[1]) if len(argv) > 1 and argv[1].isdigit() else 512
+
+ main_re, dyn_re, sub_fn = config_for(arch)
+
+ stack = []
+ func = None
+ file = ''
+ intro = ''
+ total = 0
+
+ for raw in sys.stdin:
+ line = raw.rstrip('\n')
+
+ m = RE_FUNC.match(line)
+ if m:
+ func = m.group(2)
+ if total > min_stack:
+ stack.append(f'{intro}{total}\n')
+ addr = m.group(1)
+ intro = f'0x{addr} {func} [{file}]:'
+ pad = 56 - len(intro)
+ while pad > 0:
+ intro += '\t'
+ pad -= 8
+ total = 0
+ continue
+
+ m = RE_FILE.match(line)
+ if m:
+ file = m.group(1).replace('.ko', '')
+ idx = file.rfind('/')
+ if idx != -1:
+ file = file[idx + 1:]
+ continue
+
+ m = main_re.search(line)
+ if m:
+ size = parse_size(m.group(1))
+ if size > 0xf0000000:
+ size = -size + 0x80000000 + 0x80000000
+ if size > 0x10000000:
+ continue
+ total += size
+ continue
+
+ if dyn_re is not None:
+ m = dyn_re.search(line)
+ if m:
+ total += parse_size(m.group(1))
+ continue
+
+ if sub_fn is not None:
+ total += sub_fn(line)
+
+ if total > min_stack:
+ stack.append(f'{intro}{total}\n')
+
+ def key(entry):
+ m = re.search(r':\t*(\d+)$', entry)
+ size = int(m.group(1)) if m else 0
+ m = re.search(r' (.*):', entry)
+ fname = m.group(1) if m else ''
+ return (-size, fname)
+
+ for entry in sorted(stack, key=key):
+ sys.stdout.write(entry)
+ return 0
+
+
+if __name__ == '__main__':
+ sys.exit(main(sys.argv[1:]))
\ No newline at end of file
--
2.43.0
next prev parent reply other threads:[~2026-10-18 11:28 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-18 11:28 [RFC PATCH 0/7] scripts: convert remaining Perl utilities in scripts/ Breno Rodrigues Alves
2026-10-18 11:28 ` Breno Rodrigues Alves [this message]
2026-10-18 11:28 ` [RFC PATCH 2/7] scripts: convert extract-module-sig.pl to Python Breno Rodrigues Alves
2026-10-18 11:28 ` [RFC PATCH 3/7] scripts: convert extract-sys-certs.pl " Breno Rodrigues Alves
2026-10-18 11:28 ` [RFC PATCH 4/7] scripts: convert headerdep.pl " Breno Rodrigues Alves
2026-10-18 11:28 ` [RFC PATCH 5/7] scripts: convert kconfig-sym-check.pl " Breno Rodrigues Alves
2026-10-18 11:28 ` [RFC PATCH 6/7] scripts: convert parse-maintainers.pl " Breno Rodrigues Alves
2026-10-18 11:28 ` [RFC PATCH 7/7] scripts: convert streamline_config.pl " Breno Rodrigues Alves
2026-10-18 13:57 ` [RFC PATCH v2 0/7] scripts: convert remaining Perl utilities in scripts/ Breno Rodrigues Alves
2026-10-18 13:57 ` [RFC PATCH v2 1/7] scripts: convert checkstack.pl to Python Breno Rodrigues Alves
2026-10-18 13:57 ` [RFC PATCH v2 2/7] scripts: convert extract-module-sig.pl " Breno Rodrigues Alves
2026-10-18 13:57 ` [RFC PATCH v2 3/7] scripts: convert extract-sys-certs.pl " Breno Rodrigues Alves
2026-10-18 13:57 ` [RFC PATCH v2 4/7] scripts: convert headerdep.pl " Breno Rodrigues Alves
2026-10-18 13:57 ` [RFC PATCH v2 5/7] scripts: convert kconfig-sym-check.pl " Breno Rodrigues Alves
2026-10-18 13:57 ` [RFC PATCH v2 6/7] scripts: convert parse-maintainers.pl " Breno Rodrigues Alves
2026-10-18 13:57 ` [RFC PATCH v2 7/7] scripts: convert streamline_config.pl " Breno Rodrigues Alves
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261018112838.16355-2-breno3011alves@gmail.com \
--to=breno3011alves@gmail.com \
--cc=apw@canonical.com \
--cc=dwaipayanray1@gmail.com \
--cc=joe@perches.com \
--cc=linux-kernel@vger.kernel.org \
--cc=lukas.bulwahn@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®