[Concept,18/35] malloc: Add 'malloc' command with 'info' subcommand

Message ID 20251210000737.180797-19-sjg@u-boot.org
State New
Headers
Series malloc: Add heap debugging commands and mcheck caller tracking |

Commit Message

Simon Glass Dec. 10, 2025, 12:07 a.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Add a command to display malloc heap statistics, showing total heap
size and memory currently in use.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 cmd/Kconfig              |  9 ++++++++
 cmd/Makefile             |  1 +
 cmd/malloc.c             | 34 ++++++++++++++++++++++++++++++
 doc/usage/cmd/malloc.rst | 45 ++++++++++++++++++++++++++++++++++++++++
 doc/usage/index.rst      |  1 +
 test/cmd/Makefile        |  1 +
 test/cmd/malloc.c        | 30 +++++++++++++++++++++++++++
 7 files changed, 121 insertions(+)
 create mode 100644 cmd/malloc.c
 create mode 100644 doc/usage/cmd/malloc.rst
 create mode 100644 test/cmd/malloc.c
  

Patch

diff --git a/cmd/Kconfig b/cmd/Kconfig
index ff5f6f85144..43e12e413f8 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -3018,6 +3018,15 @@  config CMD_LOG
 	  maximum log level for emitting of records). It also provides access
 	  to a command used for testing the log system.
 
+config CMD_MALLOC
+	bool "malloc - Show malloc statistics"
+	depends on MALLOC_DEBUG
+	default y
+	help
+	  This provides access to malloc information. It shows statistics
+	  about memory allocation, such as total memory allocated and
+	  currently in use.
+
 config CMD_MOUSE
 	bool "mouse - Show mouse input"
 	default y if MOUSE
diff --git a/cmd/Makefile b/cmd/Makefile
index ebf66ea0d3c..c010da8dc3b 100644
--- a/cmd/Makefile
+++ b/cmd/Makefile
@@ -113,6 +113,7 @@  obj-$(CONFIG_CMD_LED) += led.o
 obj-$(CONFIG_CMD_LICENSE) += license.o
 obj-y += load.o
 obj-$(CONFIG_CMD_LOG) += log.o
+obj-$(CONFIG_CMD_MALLOC) += malloc.o
 obj-$(CONFIG_CMD_LSBLK) += lsblk.o
 obj-$(CONFIG_CMD_MD5SUM) += md5sum.o
 obj-$(CONFIG_CMD_MEMORY) += mem.o
diff --git a/cmd/malloc.c b/cmd/malloc.c
new file mode 100644
index 00000000000..cb2fa34155b
--- /dev/null
+++ b/cmd/malloc.c
@@ -0,0 +1,34 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * malloc command - show malloc information
+ *
+ * Copyright 2025 Canonical Ltd
+ * Written by Simon Glass <simon.glass@canonical.com>
+ */
+
+#include <command.h>
+#include <display_options.h>
+#include <malloc.h>
+
+static int do_malloc_info(struct cmd_tbl *cmdtp, int flag, int argc,
+			  char *const argv[])
+{
+	struct malloc_info info;
+	char buf[12];
+	int ret;
+
+	ret = malloc_get_info(&info);
+	if (ret)
+		return CMD_RET_FAILURE;
+
+	printf("total bytes  = %s\n", format_size(buf, info.total_bytes));
+	printf("in use bytes = %s\n", format_size(buf, info.in_use_bytes));
+
+	return 0;
+}
+
+U_BOOT_LONGHELP(malloc,
+	"info - display malloc statistics\n");
+
+U_BOOT_CMD_WITH_SUBCMDS(malloc, "malloc information", malloc_help_text,
+	U_BOOT_SUBCMD_MKENT(info, 1, 1, do_malloc_info));
diff --git a/doc/usage/cmd/malloc.rst b/doc/usage/cmd/malloc.rst
new file mode 100644
index 00000000000..d21aa6c1421
--- /dev/null
+++ b/doc/usage/cmd/malloc.rst
@@ -0,0 +1,45 @@ 
+.. SPDX-License-Identifier: GPL-2.0+:
+
+.. index::
+   single: malloc (command)
+
+malloc command
+==============
+
+Synopsis
+--------
+
+::
+
+    malloc info
+
+Description
+-----------
+
+The malloc command shows information about the malloc heap.
+
+info
+    Shows memory-allocation statistics, including the total heap size and the
+    amount currently in use.
+
+The total heap size is set by ``CONFIG_SYS_MALLOC_LEN``.
+
+Example
+-------
+
+::
+
+    => malloc info
+    total bytes  = 96 MiB
+    in use bytes = 700.9 KiB
+
+Configuration
+-------------
+
+The malloc command is enabled by CONFIG_CMD_MALLOC which depends on
+CONFIG_MALLOC_DEBUG.
+
+Return value
+------------
+
+The return value $? is 0 (true) on success, 1 (false) on failure.
diff --git a/doc/usage/index.rst b/doc/usage/index.rst
index bee7884a066..7ed79f89af6 100644
--- a/doc/usage/index.rst
+++ b/doc/usage/index.rst
@@ -97,6 +97,7 @@  Shell commands
    cmd/loadx
    cmd/loady
    cmd/luks
+   cmd/malloc
    cmd/meminfo
    cmd/mbr
    cmd/md
diff --git a/test/cmd/Makefile b/test/cmd/Makefile
index c43aefb4eb3..14cbdc3a6e6 100644
--- a/test/cmd/Makefile
+++ b/test/cmd/Makefile
@@ -26,6 +26,7 @@  endif
 obj-$(CONFIG_CMD_HASH) += hash.o
 obj-$(CONFIG_CMD_HISTORY) += history.o
 obj-$(CONFIG_CMD_LOADM) += loadm.o
+obj-$(CONFIG_CMD_MALLOC) += malloc.o
 ifdef CONFIG_SANDBOX
 obj-$(CONFIG_CMD_MEMINFO) += meminfo.o
 endif
diff --git a/test/cmd/malloc.c b/test/cmd/malloc.c
new file mode 100644
index 00000000000..6cd52b68900
--- /dev/null
+++ b/test/cmd/malloc.c
@@ -0,0 +1,30 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Test for 'malloc' command
+ *
+ * Copyright 2025 Canonical Ltd
+ * Written by Simon Glass <simon.glass@canonical.com>
+ */
+
+#include <malloc.h>
+#include <dm/test.h>
+#include <test/cmd.h>
+#include <test/ut.h>
+
+/* Test 'malloc info' command */
+static int cmd_test_malloc_info(struct unit_test_state *uts)
+{
+	struct malloc_info info;
+
+	ut_assertok(malloc_get_info(&info));
+	ut_assert(info.total_bytes >= CONFIG_SYS_MALLOC_LEN);
+	ut_assert(info.in_use_bytes < info.total_bytes);
+
+	ut_assertok(run_command("malloc info", 0));
+	ut_assert_nextlinen("total bytes   = ");
+	ut_assert_nextlinen("in use bytes  = ");
+	ut_assert_console_end();
+
+	return 0;
+}
+CMD_TEST(cmd_test_malloc_info, UTF_CONSOLE);