From patchwork Sat Jan 3 20:04: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: 1241 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=1767470821; bh=ilZcAqdY6VU0hIk/gArqlIlpaIuNdgS/tp/SvbTc42g=; 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=iVystiZ2Q/BQFuBdbp35VSaAgedfN3z547AMYKLKC/ZZMEMUbyni2j082AF48Q2AS 16xMpI8/J1C7SytjvV639b+Rr+i58GG1aFuYB0Zj2eAYCaxyBKLsbTuWdbdzSLDcow H4ijkHSySe86DW3Y0k/XyhNa5I51njWO5S39p8eqMCWxQ+MLFCmkn+ETwGqOzZdJYs /w4I4XHjhLzfmOd1SQDftSMNXCz77r4lOva+GrHwC5B0QBKC47jsK5twl+H+cyYins yh2Or1EFtUokvHjCkSjvEd2RbLGE8EKvM9hqQACJXeHiA/R+ZshObqTfZ9LQ0+CeBJ LEEqrNl+0iOCQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 7D20268FCC for ; Sat, 3 Jan 2026 13:07:01 -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 6nTncqbZVKhW for ; Sat, 3 Jan 2026 13:07:01 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767470819; bh=ilZcAqdY6VU0hIk/gArqlIlpaIuNdgS/tp/SvbTc42g=; 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=jWzkfYfb332IIuaVqBHgDqsblEjf820Ax/eKYoYzdBim/Xs4Ph60qp4HML9Nf/xij nwDCkOWQ76Zd36LTBtYqRSZ2n4YbLtBfC7lR3IE2E+uAw4fWAguW+sPRsfOw0ZhQMW jygdVN5omdWJSgr5pzIsh5vluT8G96x34PYnZtiHZec37tngSjuVst6MPx8xmob9u9 HGpIgcI5TOnssR6WN4aCKII4qP5rmlXGbYPBxOWiTHTQvvUS60jqYsQH7Hy+AGVqqK FDoxrAc4Uf9vOHFi+Fgp+KCjmwW/4gZWcgYWFmPqhPInr3QXP9FYOPYKXW/ZqypcD3 zQLbNM9nsH5HQ== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id DBC48690A1 for ; Sat, 3 Jan 2026 13:06:59 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767470818; bh=eDu7ibpR58PfLWI7MJNSCPh0Plfb50DKXIS8nRS9kDg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KdN5usowN1Jyvn8HfhIXV5wJm+BRgSQFw2KZ/qb7DpOSIJNTJESpA3oXG5xsL/d+t 2Gm1oYQWre6yq/YpnCEovOIaUKpyOpPSMxd4qWi+EzdD9ccdh6iHDOtG8rTGYlLdtc rNQLg+bKqEntt+XjJ8AmwVgCRKMR1kpgpQDgi8GwvCDwVmqvJFTaCij1GwyycQwd2t pG1Lz3cT940+bjE/QADlqDoq6WzUpMKLKJylaIelR+O5bDl5ejazuNak+ZS1H3dj05 RXgt01nvP9ng3eFuGqH5h4gg4DbHzhxkcSg7ThV9rrY1u95FV69wTw8Xxl3DtP4yYG c5iJQoJQV4izQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 9D8086907B; Sat, 3 Jan 2026 13:06:58 -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 fsUD9-vD17fv; Sat, 3 Jan 2026 13:06:58 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767470818; bh=k7gJh3hnWoJIkSFY/N+FdsTff5wfqRYgUvrAvLqw1NA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OLj/M8ORCLppeFpUHCCKG0XbTXosWtzR72C+vq9HVwyUVzcaRxwcfZ2ciq5iQb3Wd yteWxIfP++XpNLrNaVAneEO+qdZHA0DaEiBVORet2pEZHc5dwC9SDPrd6uVTCwq4jw wsIgoJ9a7/uOoRgm8kaXJPN0PnGICheBBt7/lamiQkOTOPe5dvfDKU6W77P6NVh1Lw 05CWwidVPG1HSW5ZtNhSmBduFDDq68sTnKyiJB7YW8ajNLM0BvLpBXNuM96WwlusLY SO8qTJn0b5DagakyWYPkKrbbHIY2P8sbv1h5Ss5Ooezsx5CpmH9S68n2OqQNHTZAgn 7Imq6R6bHEdFA== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id EE74B68FD2; Sat, 3 Jan 2026 13:06:57 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Sat, 3 Jan 2026 13:04:54 -0700 Message-ID: <20260103200510.3605009-24-sjg@u-boot.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260103200510.3605009-1-sjg@u-boot.org> References: <20260103200510.3605009-1-sjg@u-boot.org> MIME-Version: 1.0 Message-ID-Hash: NA3WN3OVXDYDAT7KG7KAWEQORUE6KQE7 X-Message-ID-Hash: NA3WN3OVXDYDAT7KG7KAWEQORUE6KQE7 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 v2 23/29] 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 --- (no changes since v1) common/dlmalloc.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++ include/malloc.h | 22 +++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index ebcf8439ff7..2bc5f63c0a8 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 */