cmd: malloc: Add a command to show the malloc log
Add a command interface for the malloc-traffic log:
- malloc log start: Start recording allocations
- malloc log stop: Stop recording
- malloc log: Dump the recorded entries
Example output:
=> malloc log
Malloc log: 29 entries (max 524288, total 29)
Seq Type Ptr Size Caller
---- -------- ---------------- -------- ------
0 free 16a016e0 0 free_pipe_list:2001
<-parse_stream_outer:3208 <-parse_file_outer:3300
1 alloc 16a01b90 20 hush_file_init:3277
<-parse_file_outer:3295 <-run_pipe_real:1986
Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
*/
|
||||
|
||||
#include <malloc.h>
|
||||
#include <mapmem.h>
|
||||
#include <dm/test.h>
|
||||
#include <test/cmd.h>
|
||||
#include <test/ut.h>
|
||||
@@ -52,3 +53,50 @@ static int cmd_test_malloc_dump(struct unit_test_state *uts)
|
||||
return 0;
|
||||
}
|
||||
CMD_TEST(cmd_test_malloc_dump, UTF_CONSOLE);
|
||||
|
||||
#if CONFIG_IS_ENABLED(MCHECK_LOG)
|
||||
/* Test 'malloc log' command */
|
||||
static int cmd_test_malloc_log(struct unit_test_state *uts)
|
||||
{
|
||||
struct mlog_info info;
|
||||
void *ptr, *ptr2;
|
||||
int seq;
|
||||
|
||||
ut_assertok(run_command("malloc log start", 0));
|
||||
ut_assert_nextline("Malloc logging started");
|
||||
ut_assert_console_end();
|
||||
|
||||
/* Get current log position so we know our sequence numbers */
|
||||
ut_assertok(malloc_log_info(&info));
|
||||
seq = info.total_count;
|
||||
|
||||
/* Do allocations with distinctive sizes we can search for */
|
||||
ptr = malloc(12345);
|
||||
ut_assertnonnull(ptr);
|
||||
ptr2 = realloc(ptr, 23456);
|
||||
ut_assertnonnull(ptr2);
|
||||
free(ptr2);
|
||||
|
||||
ut_assertok(run_command("malloc log stop", 0));
|
||||
ut_assert_nextline("Malloc logging stopped");
|
||||
ut_assert_console_end();
|
||||
|
||||
/* Dump the log and find our allocations by sequence number and size */
|
||||
ut_assertok(run_command("malloc log", 0));
|
||||
ut_assert_nextlinen("Malloc log: ");
|
||||
ut_assert_nextline("%4s %-8s %10s %8s %s",
|
||||
"Seq", "Type", "Address", "Size", "Caller");
|
||||
ut_assert_nextline("---- -------- ---------- -------- ------");
|
||||
/* 12345 = 0x3039, 23456 = 0x5ba0 */
|
||||
ut_assert_skip_to_linen("%4d alloc %10lx 3039", seq,
|
||||
(ulong)map_to_sysmem(ptr));
|
||||
ut_assert_skip_to_linen("%4d realloc %10lx 5ba0", seq + 1,
|
||||
(ulong)map_to_sysmem(ptr2));
|
||||
ut_assert_skip_to_linen("%4d free %10lx 0", seq + 2,
|
||||
(ulong)map_to_sysmem(ptr2));
|
||||
console_record_reset_enable(); /* discard remaining output */
|
||||
|
||||
return 0;
|
||||
}
|
||||
CMD_TEST(cmd_test_malloc_log, UTF_CONSOLE);
|
||||
#endif /* MCHECK_LOG */
|
||||
|
||||
Reference in New Issue
Block a user