[Concept,03/19] sandbox: Add a command-line option to dump malloc state
Commit Message
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(+)
@@ -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) {
@@ -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);
@@ -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]