From patchwork Sat Jan 3 01:18:54 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1212 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=1767403231; bh=00c+Oocz3FsopmY0dCsTBWfBFZGGQ4JyShK3j/18Xc4=; 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=J4LCBa1hxJLjEogXB9gPvlgd/Wim7qMqGaEetw39i8YdX52mVcWVzA+g5TRGDiWJ8 txB3/+vLaUa3VBjbbOO6jUS1M65wkiTIZXKd7WQ5x8zKV42AGIM5RhQokbDKHdXnle UTP14ItoLa9wXkf2K058JDX0AK+SyjDQe1B+m30k1dEHQyWM8kXbRWDj1tHYgB2WHc jRJZ11OtNYihUKU/FCABI5JMXfRpPMFH1slRC5+1a9bF9/KOxhopMzHBDamGPMrmYy HExdMetKXMc5876svyahUESSJNv5oSK/24V5AeT8eajgey/BasvgTXXA+/hs7+eTlO kCLYwZ/BiFhsg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 08D096904D for ; Fri, 2 Jan 2026 18:20:31 -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 Eaij997xroKX for ; Fri, 2 Jan 2026 18:20:30 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767403230; bh=00c+Oocz3FsopmY0dCsTBWfBFZGGQ4JyShK3j/18Xc4=; 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=XiWA9K7CmLpQQy/q0tnNyC5ogkHDHik7i+KGurEekbpvEJRjEn1dlCPo/iIGJFJak 9A2bBOtTRyycZgeVzIWrPigC7raxM5aQpGlla0+zaK86nEAkjRhzreDwcbE1r3lGeT vs5il2WkZqVN8p9niS9+qW1BuZiDKQ/zA5S7lT3d51DDfo3XMnWgpGiBxF7yNk/z89 ZX+Iup2iq0xipHnoYs3/S/uLMpM0rjjdSdMsQuL0WygOGv7A6EPAY00gsb7rgTKnc5 c7GfsaLkJsXZWHX6h4vbctTYar2c4cBGgpBN2hjbP7v96+Xk9zAsDTcqk4+sxpGdQP ZS4DYUE5BDBwg== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id E97BE68BF2 for ; Fri, 2 Jan 2026 18:20:30 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767403228; bh=ylApJgAbi3NyBysty9FVRnTCf+ECaEDVYF7COfJkaeE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sCVG6s2rr/MUb2HXjmCxQapPZVvDt5m7YgJ+mA4ON92Eo2VNMTbe2r7VVWrDuT/QJ 5zH3ziqubNMKQcequPoLgELZZxUIc5UGZEYI7/zJ5o0gdXSx6Ndwb88iQDWPtYkWlU S+xSC4f2oxox5voTqbeHXxK6288hCmHrl4iXw2b3nvgS1DlQdkkSuqZR6ZN5XZ3zQk vqn+HdE8W5rE2hsUaEMJGqUCHAePkkUwOzHh/FdFRVehQ9a32ttlzliHSDmBJVIag5 cc37dNaTGWhI4Z6l68vibyiTgLCpnbH8Mvu89D/uCtoS4YFeCff9oeLCiVKZTF+gK4 uXxW8gsxVGgZw== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 4EF3468FAF; Fri, 2 Jan 2026 18:20:28 -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 486LOFTQcEne; Fri, 2 Jan 2026 18:20:28 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767403222; bh=/qqwyzwveVuNUFr33BqNEfepr703XLb2ibLpBwLSY4A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vQ7IBFCPVgizcYsgj6Gqv+EqcwUO3mrGbJOmzP3gJcyqGJ+tfxrtIR14GOcDnn4wL HyWQft8yZQH0GfF8tckMscFdGcggEZmBVTEBWrT+WWCEjdCorePMmc8FXMWuNWpMBF CvJbfemAjBk7q076tXDeWXO4LUQ4ddEqIca2/8YK/JPWG+cABXxh/dLytNGxbNRWAR EEK4WDBTDdJ3kYu5e9OsVr7AW80Kdd+NqWEaB3cgXtkNzlxrJxrPcpYhbWWDaDUtxW GtxqL2Dt0/3+nF5JPeITTpzP5UaGvZq5309iRnffw8ns+i6rkQZAR3Y2EgK0tTcgHo v+yeIaGIWdI6w== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id DE94468BF2; Fri, 2 Jan 2026 18:20:21 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Fri, 2 Jan 2026 18:18:54 -0700 Message-ID: <20260103011908.149445-19-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260103011908.149445-1-sjg@u-boot.org> References: <20260103011908.149445-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: GIXM63UNP25CAY5ZNK3YYKRB7QWEJZYW X-Message-ID-Hash: GIXM63UNP25CAY5ZNK3YYKRB7QWEJZYW 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 18/24] malloc: Add file output for heap dump and malloc log 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 Add malloc_dump_to_file() and malloc_log_to_file() functions to write heap dumps and malloc traffic logs to host files. This is useful for debugging memory leaks in sandbox by allowing comparison of before/after heap states with external tools like diff. These functions are only available in sandbox builds since they use host-filesystem access. Co-developed-by: Claude Signed-off-by: Simon Glass --- common/dlmalloc.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++ include/malloc.h | 22 +++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index 7f00dea6eb1..e47f3c153a2 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -6144,6 +6144,38 @@ void malloc_log_dump(void) malloc_log_impl(log_to_console, NULL); } +#if IS_ENABLED(CONFIG_SANDBOX) +/* File output function for log */ +static void log_to_file(void *ctx, const char *fmt, ...) +{ + int fd = *(int *)ctx; + char buf[256]; + va_list args; + int len; + + va_start(args, fmt); + len = vsnprintf(buf, sizeof(buf), fmt, args); + va_end(args); + + if (len > 0) + os_write(fd, buf, len); +} + +int malloc_log_to_file(const char *fname) +{ + int fd; + + fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT | OS_O_TRUNC); + if (fd < 0) + return fd; + + malloc_log_impl(log_to_file, &fd); + os_close(fd); + + return 0; +} +#endif + int malloc_log_info(struct mlog_info *info) { if (!mlog.buf) @@ -7481,6 +7513,26 @@ static void dump_to_console(void *ctx, const char *fmt, ...) va_end(args); } +#if IS_ENABLED(CONFIG_SANDBOX) +#include + +/* File output function for heap dump */ +static void dump_to_file(void *ctx, const char *fmt, ...) +{ + int fd = *(int *)ctx; + char buf[256]; + va_list args; + int len; + + va_start(args, fmt); + len = vsnprintf(buf, sizeof(buf), fmt, args); + va_end(args); + + if (len > 0) + os_write(fd, buf, len); +} +#endif + static void malloc_dump_impl(dump_out_fn out, void *ctx) { mchunkptr q; @@ -7568,6 +7620,22 @@ void malloc_dump(void) malloc_dump_impl(dump_to_console, NULL); } +#if IS_ENABLED(CONFIG_SANDBOX) +int malloc_dump_to_file(const char *fname) +{ + int fd; + + fd = os_open(fname, OS_O_WRONLY | OS_O_CREAT | OS_O_TRUNC); + if (fd < 0) + return fd; + + malloc_dump_impl(dump_to_file, &fd); + os_close(fd); + + return 0; +} +#endif + int initf_malloc(void) { #if CONFIG_IS_ENABLED(SYS_MALLOC_F) diff --git a/include/malloc.h b/include/malloc.h index 4bd6458b70d..3deb90b2a0b 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -712,6 +712,17 @@ void malloc_disable_testing(void); */ void malloc_dump(void); +/** + * malloc_dump_to_file() - Write heap dump to a host file + * + * @fname: Path to the output file on the host filesystem + * Return: 0 on success, negative error code on failure + * + * This is only available in sandbox builds. It writes the same information + * as malloc_dump() but to a file instead of the console. + */ +int malloc_dump_to_file(const char *fname); + /** * malloc_log_start() - Start logging malloc traffic * @@ -733,6 +744,17 @@ void malloc_log_stop(void); */ void malloc_log_dump(void); +/** + * malloc_log_to_file() - Write malloc()-traffic log to a host file + * + * @fname: Path to the output file on the host filesystem + * Return: 0 on success, negative error code on failure + * + * This is only available in sandbox builds. It writes the same information + * as malloc_log_dump() but to a file instead of the console. + */ +int malloc_log_to_file(const char *fname); + /** * enum mlog_type - Type of malloc log entry */