From patchwork Sat Jan 3 01:18: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: 1209 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=1767403223; bh=zLZjOjRN/yzZEVDba2aCj3ZP/tEhnPoIb3ggZAFpcNg=; 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=fyoZaPoLRTq4PH1Pe2goXar7ypMWSqiAm6qUkUXqczIaMpYbOKtcaqfGQuDtXyv0P MnCYFE64rFvZZdgXpruS5ZLsv8K2TxQXVDH/2Mo7uuB2GXBIPrHJcbspEKD1iNHtbT pK4n3pt6hkNvUjf7V+M63hMl9CuWdSlp8FD+hEfFA8gbcWl5LfjvLJugSsvKJwtiKU O9e/5NReHKntqg9UZicc6dS9a850J8u3rPqrMIVggCsNgkpji/7yyGUTRR2p24z3Eq Hzt7L3qI+vQbgeeRa62nas+0gdWO5ax0SlxPV0BdassDfcSidd2plnffO27HkrACRY 0VU6HGHXcVMdA== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 4BDA56904E for ; Fri, 2 Jan 2026 18:20:23 -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 nWjZz_pu_7-R for ; Fri, 2 Jan 2026 18:20:23 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767403223; bh=zLZjOjRN/yzZEVDba2aCj3ZP/tEhnPoIb3ggZAFpcNg=; 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=fyoZaPoLRTq4PH1Pe2goXar7ypMWSqiAm6qUkUXqczIaMpYbOKtcaqfGQuDtXyv0P MnCYFE64rFvZZdgXpruS5ZLsv8K2TxQXVDH/2Mo7uuB2GXBIPrHJcbspEKD1iNHtbT pK4n3pt6hkNvUjf7V+M63hMl9CuWdSlp8FD+hEfFA8gbcWl5LfjvLJugSsvKJwtiKU O9e/5NReHKntqg9UZicc6dS9a850J8u3rPqrMIVggCsNgkpji/7yyGUTRR2p24z3Eq Hzt7L3qI+vQbgeeRa62nas+0gdWO5ax0SlxPV0BdassDfcSidd2plnffO27HkrACRY 0VU6HGHXcVMdA== Received: from mail.u-boot.org (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id 3749E68FAF for ; Fri, 2 Jan 2026 18:20:23 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767403220; bh=6VtzEXBH8ifkaymgVQ+DmSz3ChsPT8iSMkuHH5ZdMmA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oOZkTqnZAvp69OLdE+tYgIDkPHt7eN3wf67/neptra7+Rncgaql+aVEpgQJx0+8Ky ev9evAuYYokf/dweFZi/JfRDILw87rDsLLHakJYT2XPaMlWZ1stXfz3IFRsGuwzNUo yjPfz5PadYYcQQHzbcotEutgacj8PQU1xEPRpC2/WFGj4KVUlLvAdNy24hzkyl9nfO XycOW4+4naOiCFQR6BlP9kbjJrkLhpDth/uoX7snLY10unajEzfGBQUbPU0Em7JHBf cSz9gQe0GH+BdpmudjvOx6Gfl2WEYDu16q/PEEiI4lC6gpI4bM5K0lP5NgL6KbnJ4V 4UqThDlLke02g== Received: from localhost (localhost [127.0.0.1]) by mail.u-boot.org (Postfix) with ESMTP id EA52268FAF; Fri, 2 Jan 2026 18:20:20 -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 00wYNfqBvEEW; Fri, 2 Jan 2026 18:20:20 -0700 (MST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=u-boot.org; s=default; t=1767403215; bh=yE/I1APmoZb3E+/Frl9Ot4Olog9PMwTMhbiEB8iiMaM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BAAqq4v2tNfQWw9OP5u3CkCUBFI1NH6QbqzscaS8oHJcgH/FPMP930PG7DOZO1SSP kUFyPdINvbdeIOZYpRt4VzaR3iIP9pmwcqSS2wJonSlvb/a11cdHb6O+tiEzoJbYWo eMQ02hK/MH7jsZs+2WPyGTjd8PkGiJTqXIKuXBvBEyayh9DjWsYh/N2qTCFpIMhZWI MDC3sTDOlqVXs31OtSlVuju8RJ2deXJpkv1TJvF+VQ0tT6H15Of6ga5V1LNQ3I0pHq YfpylWPOb0JdUGyKj5vf1Yp612LPYfuI0pPXtSMyB+Lb+YCpI5cp+X/Kw5SZq0zOsT +0AP1nUqzaKOw== Received: from u-boot.org (unknown [73.34.74.121]) by mail.u-boot.org (Postfix) with ESMTPSA id 6003B68BF2; Fri, 2 Jan 2026 18:20:15 -0700 (MST) From: Simon Glass To: U-Boot Concept Date: Fri, 2 Jan 2026 18:18:51 -0700 Message-ID: <20260103011908.149445-16-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: GFILVHXKXBKSDDGXBZMMQSEWDEILI6MM X-Message-ID-Hash: GFILVHXKXBKSDDGXBZMMQSEWDEILI6MM 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 15/24] 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 --- 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 bf60e3004d4..6f10a980d6e 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, ...) + __attribute__((format(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 4e5bd68c013..d29e9364b01 100644 --- a/test/common/malloc.c +++ b/test/common/malloc.c @@ -692,4 +692,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 */