From patchwork Sat Jan 3 20:04:51 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Glass X-Patchwork-Id: 1238 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=1767470811; bh=56hJjPZrg83wpSmHQt/AmxkC+glh+AHxLYv5UHBwKRM=; 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=lUTXp6qdp9YLkzi6EHtqvVAtySoIG/KxgIj1QN1JcQkQyMXLUB/xb3EhO++4mmRZu zIjxdjPedSNOvxvO4K7qD/Zy7nDqH0x9Bm2Y7LKwfVKYKPozxIx+tzpOovQhQ8eKIu 9XVkgFsQ0uSHrUF6oD7IGpoiStrb5XfbT+VZ+BlBbtDvqTBvNfE3tia4VWzrnbN4Ip Gu5lV7j5bzLEfFfxWpgyXKS58fc7L19U0uxqAUAXDRvNbTGgc3Q05BHTso7FK501+Z oF8XX8G66ju5PQ0ib9Ud26lb+QeAjtCWaoKfjFz7WXx1u4wjgh/LVXr9wl47GA5yMC YcxJK3SlTnVNg== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 1AE3269097 for ; Sat, 3 Jan 2026 13:06:51 -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 ze8kBrlq42Hl for ; Sat, 3 Jan 2026 13:06:51 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767470811; bh=56hJjPZrg83wpSmHQt/AmxkC+glh+AHxLYv5UHBwKRM=; 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=lUTXp6qdp9YLkzi6EHtqvVAtySoIG/KxgIj1QN1JcQkQyMXLUB/xb3EhO++4mmRZu zIjxdjPedSNOvxvO4K7qD/Zy7nDqH0x9Bm2Y7LKwfVKYKPozxIx+tzpOovQhQ8eKIu 9XVkgFsQ0uSHrUF6oD7IGpoiStrb5XfbT+VZ+BlBbtDvqTBvNfE3tia4VWzrnbN4Ip Gu5lV7j5bzLEfFfxWpgyXKS58fc7L19U0uxqAUAXDRvNbTGgc3Q05BHTso7FK501+Z oF8XX8G66ju5PQ0ib9Ud26lb+QeAjtCWaoKfjFz7WXx1u4wjgh/LVXr9wl47GA5yMC YcxJK3SlTnVNg== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 099D56907B for ; Sat, 3 Jan 2026 13:06:51 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767470808; bh=8ZDc8gsTb2/wv3YVoIxOtQjeHKG1YImjewsedgUeSXw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=aZPeB9Wl0WNBjN2MDsEL1E9yFBcAp66uzsAw20M6ImJRtlTEuFjzy/NRt64jgVe/s EHs23SERv/pYzdLpBg+vpmOX6feDgo8YWydzTnsB1ov5AZokPG4S6bIWQmEsE5VuVk QHHHBuSJUsRoPnOdG6cfecIZ568nh87/yeWxJPqbg6zGoTmHll0MPX5RNrlqWnfXho zkL6V9WJ/4vRcQCgO/pAy9e9sb16LbBGhTF8p0k2pYfVt4vAAC+s3YsD9f8T5D0T5y 1fPICdZwk+PJ8BUhYL2XOKZ4og7xoYvzsNUgStadNHz6lxN+9AuVSq10rE5XeoLwYg 7zDJZu+D8XnLQ== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id B14B168FD2; Sat, 3 Jan 2026 13:06:48 -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 hnr-KHTift4M; Sat, 3 Jan 2026 13:06:48 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767470804; bh=GIYf+2pfI+LDb1w8P5/JqRva9GF03WJlL8JCqzvWulQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=prAUA/J3V/OyN0vwAjFW7EWAerxhZnkGyiecLr3soaD0qI5RCcupDa/LRPsoME0Xg bwym1DACqu0n69makerX5M1nXa+3S16c7hnB9pY5ulmkYe6Xdjm24SY/bugoO/ow1g YDoGJb47zf4SngjSkdQEOeEEZ1tKbB2lFzKq5n6DBVkFMI2Rdj+75UpT622B8CwqZG jxkVbbEMT0ZIHbV7Ppf92hlA3OviC23yA7BdC5qxtoAIRaosX4GQgMXDcKvZPvIbL1 k17/ym541f4kXBVwmmVD0wjfXlzZIm111JTZ5Tp3UydGKkpULkTFhSCWCc1Uy1O2sF Hll2EK/YuTtIA== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 070AB69095; Sat, 3 Jan 2026 13:06:43 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Sat, 3 Jan 2026 13:04:51 -0700 Message-ID: <20260103200510.3605009-21-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: RA7ZDJYJ4CEMRFQPSRHGTQYZCUBG7L3M X-Message-ID-Hash: RA7ZDJYJ4CEMRFQPSRHGTQYZCUBG7L3M 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 20/29] malloc: Add function to dump the malloc()-traffic 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_log_dump() to print all recorded malloc/free/realloc calls with their addresses, sizes, and caller information. This provides a way to inspect the log after recording. The dump shows a summary line with entry counts, followed by a table with sequence number, operation type, pointer, size, and caller for each recorded operation. Co-developed-by: Claude Signed-off-by: Simon Glass --- (no changes since v1) common/dlmalloc.c | 70 ++++++++++++++++++++++++++++++++++++++++++++ include/malloc.h | 8 +++++ test/common/malloc.c | 41 ++++++++++++++++++++++++++ 3 files changed, 119 insertions(+) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index ef482cd3309..0e363e78e26 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -6070,6 +6070,72 @@ void malloc_log_stop(void) mlog.enabled = false; } +/* Output function type for malloc_log_impl */ +typedef void (*log_out_fn)(void *ctx, const char *fmt, ...) + __printf(2, 3); + +/* Console output function for log */ +static void log_to_console(void *ctx, const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); +} + +static void malloc_log_impl(log_out_fn out, void *ctx) +{ + static const char * const mlog_type_names[] = { + "alloc", "free", "realloc", "memalign" + }; + uint i, start, count; + + if (!mlog.buf) { + out(ctx, "Malloc log not started\n"); + return; + } + + if (mlog.enabled) { + out(ctx, "Warning: log still active, results may be incomplete\n"); + malloc_log_stop(); + } + + if (mlog.count > mlog.size) { + out(ctx, "Warning: log wrapped, %u entries lost\n", + mlog.count - mlog.size); + count = mlog.size; + start = mlog.head; /* oldest entry is at current head */ + } else { + count = mlog.count; + start = 0; + } + + out(ctx, "Malloc log: %u entries (max %u, total %u)\n", count, mlog.size, + mlog.count); + out(ctx, "%4s %-8s %10s %8s %s\n", "Seq", "Type", "Address", "Size", + "Caller"); + out(ctx, "---- -------- ---------- -------- ------\n"); + + for (i = 0; i < count; i++) { + uint idx = (start + i) % mlog.size; + struct mlog_entry *ent = &mlog.buf[idx]; + + out(ctx, "%4u %-8s %10lx %8lx", i, mlog_type_names[ent->type], + (ulong)map_to_sysmem(ent->ptr), (ulong)ent->size); + if (ent->type == MLOG_REALLOC) + out(ctx, " (was %lx)", (ulong)ent->old_size); + if (ent->caller[0]) + out(ctx, " %s", ent->caller); + out(ctx, "\n"); + } +} + +void malloc_log_dump(void) +{ + malloc_log_impl(log_to_console, NULL); +} + int malloc_log_info(struct mlog_info *info) { if (!mlog.buf) @@ -6123,6 +6189,10 @@ void malloc_log_stop(void) { } +void malloc_log_dump(void) +{ +} + int malloc_log_info(struct mlog_info *info) { return -ENOENT; diff --git a/include/malloc.h b/include/malloc.h index f8bfe843738..4bd6458b70d 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -725,6 +725,14 @@ void malloc_log_start(void); */ void malloc_log_stop(void); +/** + * malloc_log_dump() - Dump the malloc traffic log + * + * Prints all recorded malloc/free/realloc calls with their addresses, + * sizes, and caller information. + */ +void malloc_log_dump(void); + /** * enum mlog_type - Type of malloc log entry */ diff --git a/test/common/malloc.c b/test/common/malloc.c index 292efd777b2..2a292fd969c 100644 --- a/test/common/malloc.c +++ b/test/common/malloc.c @@ -688,4 +688,45 @@ static int common_test_malloc_log_info(struct unit_test_state *uts) return 0; } COMMON_TEST(common_test_malloc_log_info, 0); + +/* Test malloc_log_dump() */ +static int common_test_malloc_log_dump(struct unit_test_state *uts) +{ + struct mlog_info info; + void *ptr, *ptr2; + + malloc_log_start(); + + /* Do an allocation, realloc, and free */ + ptr = malloc(0x100); + ut_assertnonnull(ptr); + + ptr2 = realloc(ptr, 0x200); + ut_assertnonnull(ptr2); + + free(ptr2); + + malloc_log_stop(); + + ut_assertok(malloc_log_info(&info)); + + console_record_reset_enable(); + malloc_log_dump(); + + ut_assert_nextline("Malloc log: 3 entries (max %u, total 3)", + info.max_entries); + ut_assert_nextline("%4s %-8s %10s %8s %s", "Seq", "Type", "Address", + "Size", "Caller"); + ut_assert_nextline("---- -------- ---------- -------- ------"); + ut_assert_nextlinen(" 0 alloc %10lx 100", + (ulong)map_to_sysmem(ptr)); + ut_assert_nextlinen(" 1 realloc %10lx 200 (was 100)", + (ulong)map_to_sysmem(ptr2)); + ut_assert_nextlinen(" 2 free %10lx 0", + (ulong)map_to_sysmem(ptr2)); + ut_assert_console_end(); + + return 0; +} +COMMON_TEST(common_test_malloc_log_dump, 0); #endif /* MCHECK_LOG */