[Concept,03/19] sandbox: Add a command-line option to dump malloc state

Message ID 20260314231618.338113-4-sjg@u-boot.org
State New
Headers
Series test: Fix pytest inter-test side effects |

Commit Message

Simon Glass March 14, 2026, 11:15 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

Add a --malloc_dump option that writes the heap dump to a file on exit.
This is useful for debugging memory leaks in test sessions.

The dump is written from state_uninit() which runs on clean shutdown
(poweroff, reset). The prototype for malloc_dump_to_file() comes from
os.h, avoiding the asm/malloc.h conflict.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 arch/sandbox/cpu/start.c         | 9 +++++++++
 arch/sandbox/cpu/state.c         | 3 +++
 arch/sandbox/include/asm/state.h | 1 +
 3 files changed, 13 insertions(+)
  

Patch

diff --git a/arch/sandbox/cpu/start.c b/arch/sandbox/cpu/start.c
index 7c9f0a99654..caa9b61dcd9 100644
--- a/arch/sandbox/cpu/start.c
+++ b/arch/sandbox/cpu/start.c
@@ -553,6 +553,15 @@  static int sandbox_cmdline_cb_pager_bypass(struct sandbox_state *state,
 SANDBOX_CMDLINE_OPT_SHORT(pager_bypass, 'P', 0,
 			  "Enable pager bypass mode");
 
+static int sandbox_cmdline_cb_malloc_dump(struct sandbox_state *state,
+					  const char *arg)
+{
+	state->malloc_dump_fname = arg;
+
+	return 0;
+}
+SANDBOX_CMDLINE_OPT(malloc_dump, 1, "Write malloc dump to file on exit");
+
 static int sandbox_cmdline_cb_bind(struct sandbox_state *state, const char *arg)
 {
 	if (state->num_binds >= SB_MAX_BINDS) {
diff --git a/arch/sandbox/cpu/state.c b/arch/sandbox/cpu/state.c
index e492855755b..fd31d93c500 100644
--- a/arch/sandbox/cpu/state.c
+++ b/arch/sandbox/cpu/state.c
@@ -544,6 +544,9 @@  int state_uninit(void)
 	if (state->jumped_fname)
 		os_unlink(state->jumped_fname);
 
+	if (state->malloc_dump_fname)
+		malloc_dump_to_file(state->malloc_dump_fname);
+
 	/* Disable tracing before unmapping RAM */
 	if (IS_ENABLED(CONFIG_TRACE))
 		trace_set_enabled(0);
diff --git a/arch/sandbox/include/asm/state.h b/arch/sandbox/include/asm/state.h
index 001d780aec8..8707d798d8d 100644
--- a/arch/sandbox/include/asm/state.h
+++ b/arch/sandbox/include/asm/state.h
@@ -184,6 +184,7 @@  struct sandbox_state {
 	int video_test;			/* ms to wait before next assert */
 	const char *video_frames_dir;	/* Directory to write video frames */
 	int video_frame_count;		/* Number of frames written */
+	const char *malloc_dump_fname;	/* File to write malloc dump on exit */
 
 	/* Pointer to information for each SPI bus/cs */
 	struct sandbox_spi_info spi[CONFIG_SANDBOX_SPI_MAX_BUS]