malloc: Add malloc dump command to walk the heap

Add a new 'malloc dump' command that walks the dlmalloc heap from start
to end, printing each chunk's address, size (in hex), and status
(used/free/top). This is useful for debugging memory allocation issues.

When CONFIG_MCHECK_HEAP_PROTECTION is enabled, the caller string is
also shown if available.

Example output:
    Heap dump: 18a1d000 - 1ea1f000
         Address        Size  Status
    ----------------------------------
        18a1d000          10  (chunk header)
        18a1d010          90  used
        18adfc30          60  <free>
        18adff90     5f3f030  top
        1ea1f000              end
    ----------------------------------
    Used: c2ef0 bytes in 931 chunks
    Free: 5f3f0c0 bytes in 2 chunks + top

Expand the console-record size to handle this command.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
This commit is contained in:
Simon Glass
2025-12-02 10:56:34 +00:00
parent e2ccfa9c23
commit ee8e9bf104
6 changed files with 133 additions and 3 deletions

View File

@@ -12,6 +12,7 @@ Synopsis
::
malloc info
malloc dump
Description
-----------
@@ -23,6 +24,13 @@ info
amount currently in use, and call counts for malloc(), free(), and
realloc().
dump
Walks the heap and prints each chunk's address, size (in hex), and status.
In-use chunks show no status label, while free chunks show ``<free>``.
Special entries show ``(chunk header)``, ``top``, or ``end``. This is useful
for debugging memory allocation issues. When CONFIG_MCHECK_HEAP_PROTECTION
is enabled, the caller string is also shown if available.
The total heap size is set by ``CONFIG_SYS_MALLOC_LEN``.
Example
@@ -37,6 +45,20 @@ Example
free count = 567
realloc count = 89
=> malloc dump
Heap dump: 19a0e000 - 1fa10000
Address Size Status
----------------------------------
19a0e000 10 (chunk header)
19a0e010 a0
19a0e0b0 6070
19adfc30 60 <free>
19adff90 5f3f030 top
1fa10000 end
----------------------------------
Used: c2ef0 bytes in 931 chunks
Free: 5f3f0c0 bytes in 2 chunks + top
Configuration
-------------