From patchwork Sat Nov 29 07:59:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 769 Return-Path: X-Original-To: u-boot-concept@u-boot.org Delivered-To: u-boot-concept@u-boot.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1764403247; bh=QsYecGH2r4PnNGt1EYx5mRvC7fIb06CtMpSSa7gSdUE=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=vw9TN87CTEdVp7PMNmfjlUUEuKlF/4xcaxpOnuMq0B83G1eMRlR/HA29l9gO4rSpf NUWXbw/8ijE1TDhq6TT5SPZf/Eyijk0w5UiJ2IxwHbcIo2SBba7kxNadsLpQb5OLVB RrLH80eAoFxSRx3nd/A2hKnL4Ep5O34wm203XaowsrCoZpgm0gJ+U1eRCF9CTksSP9 aBo9RP4XoSUZY8F72U/oyRg597YjvB3zs7A/SUyfyZ6oAFjADFosFTvVht8aPl+eh9 1fpCmyI2gXx8Q4vQKPGdo2xCEHunqAB3Ut6jCtscZLY0FLBLYSk7l36nFqvqdg47Pj wpfqKVn9MR2tw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id E6DD3687C8 for ; Sat, 29 Nov 2025 01:00:47 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10024) with ESMTP id cbnTlBtrGvHv for ; Sat, 29 Nov 2025 01:00:47 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1764403247; bh=QsYecGH2r4PnNGt1EYx5mRvC7fIb06CtMpSSa7gSdUE=; h=From:To:Date:In-Reply-To:References:CC:Subject:List-Id: List-Archive:List-Help:List-Owner:List-Post:List-Subscribe: List-Unsubscribe:From; b=vw9TN87CTEdVp7PMNmfjlUUEuKlF/4xcaxpOnuMq0B83G1eMRlR/HA29l9gO4rSpf NUWXbw/8ijE1TDhq6TT5SPZf/Eyijk0w5UiJ2IxwHbcIo2SBba7kxNadsLpQb5OLVB RrLH80eAoFxSRx3nd/A2hKnL4Ep5O34wm203XaowsrCoZpgm0gJ+U1eRCF9CTksSP9 aBo9RP4XoSUZY8F72U/oyRg597YjvB3zs7A/SUyfyZ6oAFjADFosFTvVht8aPl+eh9 1fpCmyI2gXx8Q4vQKPGdo2xCEHunqAB3Ut6jCtscZLY0FLBLYSk7l36nFqvqdg47Pj wpfqKVn9MR2tw== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id D5D46687BA for ; Sat, 29 Nov 2025 01:00:47 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1764403246; bh=OUdbJompNuGHWKUzbY8Xw1Vb+UGBA/fZjMOV9MxNusA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uP5SmPqjxxW7rfOS6H26dghz5NFTxSQI6FzlviXPTOXzMqFI9+aSN4MqFtw/0ttei oS6tfNV5Dk42RdLLIy3QKreB9wVozmYGoCyC/giE9LSS5B+cNuxET4y0ox1JfA4r6u lxmSKnHfIT94kAl+FoyYHTylhbAx2kKu2ERfuLNEf1t7bOp7vqFHNm2GnwmpfwgO1+ xnv4I7Le7E+yBU5k0v5ECQsxOgCdlcfsSMN7oXG0szaqGZPxKxjQUj0q1ZNwPMEc5m q8gZeiVgFwWMmBHF82bVG8idrJ8BRP7IBiSyW1C+QYtPVTO78AbFzZmqyg0q6ISeYL Y6U+5REJhxsAg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id CF6A26875D; Sat, 29 Nov 2025 01:00:46 -0700 (MST) X-Virus-Scanned: Debian amavis at Received: from mail.u-boot.org ([127.0.0.1]) by localhost (mail.u-boot.org [127.0.0.1]) (amavis, port 10026) with ESMTP id IO6ITbRRDv1q; Sat, 29 Nov 2025 01:00:46 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1764403242; bh=y7YQfNvGLdYHrS5YIfzMeDjglzBY5Nyr+pdjs/7Zh/M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kDanbK9Dh9aDLeKXK4SaSt6o0vv/nYrJGEBzGKbKEzjiBgcO7A7971llEMvR+AYmW ul/eh7Z0OZQJUA18IgX9kuCreMpNJxtfPHP6rz2qYXCmdzc2xPphp5AP67fVPmBjNH gxEK/MDAN3kxXz1zAioFrp/UxG0gfN59uaOh7KCnMohAd3B9XOX/Antx3UeBQI2CTW JvvOrsFJmhvoaRs4BFbe/6dT6Y0KzEanSd8wRBB3+rBLKN2XVgJCEuwLnN9OiksmFi MAlwWa453d89Gw7/JvQ6BZXPN3Wwbsq7hpO+isbgPGJ9oJRQtAKq+ja1gXHSrZJH+X 7YVaKzxYXB5+Q== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 3733E6876F; Sat, 29 Nov 2025 01:00:42 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Sat, 29 Nov 2025 00:59:47 -0700 Message-ID: <20251129080014.758001-3-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251129080014.758001-1-sjg@u-boot.org> References: <20251129080014.758001-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: 57B4M6TI4UJILHLHVKA3X5OYSCJJ7YH5 X-Message-ID-Hash: 57B4M6TI4UJILHLHVKA3X5OYSCJJ7YH5 X-MailFrom: sjg@u-boot.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Heinrich Schuchardt , Simon Glass , Claude X-Mailman-Version: 3.3.10 Precedence: list Subject: [Concept] [PATCH 02/10] backtrace: Add a library to access the backtrace List-Id: Discussion and patches related to U-Boot Concept Archived-At: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: From: Simon Glass Provide an API to access the backtrace, in an arch-neutral way. The backtrace can be retrieved, examined and printed. Co-developed-by: Claude Signed-off-by: Simon Glass --- include/backtrace.h | 72 +++++++++++++++++++++++++++++++++++++++++++++ lib/Kconfig | 8 +++++ lib/Makefile | 1 + lib/backtrace.c | 40 +++++++++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 include/backtrace.h create mode 100644 lib/backtrace.c diff --git a/include/backtrace.h b/include/backtrace.h new file mode 100644 index 00000000000..eece61e4d9a --- /dev/null +++ b/include/backtrace.h @@ -0,0 +1,72 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Backtrace support + * + * Copyright 2025 Canonical Ltd + * Written by Simon Glass + */ + +#ifndef __BACKTRACE_H +#define __BACKTRACE_H + +#define BACKTRACE_MAX 100 +#define BACKTRACE_SYM_SIZE 128 +#define BACKTRACE_BUFSZ (BACKTRACE_MAX * BACKTRACE_SYM_SIZE) + +/** + * struct backtrace_ctx - context for backtrace operations + * + * @addrs: array of return addresses + * @syms: array of symbol strings (NULL until backtrace_get_syms() called) + * @count: number of entries in addrs/syms arrays + */ +struct backtrace_ctx { + void *addrs[BACKTRACE_MAX]; + char *syms[BACKTRACE_MAX]; + unsigned int count; +}; + +/** + * backtrace_init() - collect a backtrace + * + * Collect backtrace addresses into the context. Call backtrace_uninit() when + * done with the context. + * + * @ctx: context to fill + * @skip: number of stack frames to skip (0 to include backtrace_init itself) + * Return: number of addresses collected, or -ve on error (e.g. -ENOSYS) + */ +int backtrace_init(struct backtrace_ctx *ctx, unsigned int skip); + +/** + * backtrace_get_syms() - get symbol strings for a backtrace + * + * Convert the addresses in the context to symbol strings. The strings are + * stored in ctx->syms[]. The caller must provide a buffer of sufficient size. + * + * @ctx: context with addresses from backtrace_init() + * @buf: buffer to use for string storage + * @size: size of buffer in bytes + * Return: 0 if OK, -ENOSPC if buffer too small + */ +int backtrace_get_syms(struct backtrace_ctx *ctx, char *buf, int size); + +/** + * backtrace_uninit() - free backtrace resources + * + * Free any memory allocated in the context. + * + * @ctx: context to free + */ +void backtrace_uninit(struct backtrace_ctx *ctx); + +/** + * backtrace_show() - print a backtrace + * + * Print a backtrace of the current call stack. + * + * Return: 0 if OK, -ve on error + */ +int backtrace_show(void); + +#endif /* __BACKTRACE_H */ diff --git a/lib/Kconfig b/lib/Kconfig index 662b1a44d45..d7f791f77f3 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -28,6 +28,14 @@ config PHYSMEM Enable this to access this basic support, which only supports clearing the memory. +config BACKTRACE + bool "Enable backtrace support" + depends on SANDBOX + help + Enables support for printing a backtrace showing the current call + stack. This is currently only available on sandbox. The backtrace + command can be used to print the backtrace. + config BCH bool "Enable Software based BCH ECC" help diff --git a/lib/Makefile b/lib/Makefile index 5cbf3071f96..b696e81c496 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -147,6 +147,7 @@ obj-$(CONFIG_TRACE) += trace.o obj-$(CONFIG_LIB_UUID) += uuid.o obj-$(CONFIG_LIB_RAND) += rand.o obj-y += panic.o +obj-$(CONFIG_BACKTRACE) += backtrace.o ifeq ($(CONFIG_XPL_BUILD),y) # SPL U-Boot may use full-printf, tiny-printf or none at all diff --git a/lib/backtrace.c b/lib/backtrace.c new file mode 100644 index 00000000000..e3d93b80b8a --- /dev/null +++ b/lib/backtrace.c @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Stack-backtrace support + * + * Copyright 2025 Canonical Ltd + * Written by Simon Glass + */ + +#include +#include + +int backtrace_show(void) +{ + char buf[BACKTRACE_BUFSZ]; + struct backtrace_ctx ctx; + uint i; + int ret; + + ret = backtrace_init(&ctx, 1); + if (ret < 0) + return ret; + + ret = backtrace_get_syms(&ctx, buf, sizeof(buf)); + if (ret) { + backtrace_uninit(&ctx); + return ret; + } + + printf("backtrace: %d addresses\n", ctx.count); + for (i = 0; i < ctx.count; i++) { + if (ctx.syms[i]) + printf(" %s\n", ctx.syms[i]); + else + printf(" %p\n", ctx.addrs[i]); + } + + backtrace_uninit(&ctx); + + return 0; +}