Files
u-boot/doc/usage/cmd/malloc.rst
Simon Glass 754a755e4a malloc: Record caller backtrace for each allocation
When CONFIG_MCHECK_HEAP_PROTECTION is enabled, use backtrace_str() to
capture the caller information for each malloc/calloc/realloc/memalign
call. This information is stored in the mcheck header and can be viewed
with 'malloc dump'.

Add a flag to disable the backtrace when the stack is corrupted, since
the backtrace code tries to walks the invalid stack frames and will
crash.

Note: A few allocations made during libbacktrace initialisation may
not have caller info since they occur during the first backtrace call.

Example output showing caller info:
    18a1d010   90  used  log_init:453 <-board_init_r:774
    18a1d0a0 6060  used  membuf_new:420 <-console_record
    18a3b840   90  used  of_alias_scan:911 <-board_init_

Fix up the backtrace test to avoid recursion.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
2025-12-10 05:53:03 -07:00

84 lines
2.1 KiB
ReStructuredText

.. SPDX-License-Identifier: GPL-2.0+:
.. index::
single: malloc (command)
malloc command
==============
Synopsis
--------
::
malloc info
malloc dump
Description
-----------
The malloc command shows information about the malloc heap.
info
Shows memory-allocation statistics, including the total heap size, the
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
-------
::
=> malloc info
total bytes = 96 MiB
in use bytes = 700.9 KiB
malloc count = 1234
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
With CONFIG_MCHECK_HEAP_PROTECTION enabled, the caller backtrace is shown::
=> malloc dump
Heap dump: 18a1d000 - 1ea1f000
Address Size Status
----------------------------------
18a1d000 10 (chunk header)
18a1d010 90 used log_init:453 <-board_init_r:774
18a1d0a0 6060 used membuf_new:420 <-console_record
18a3b840 90 used of_alias_scan:911 <-board_init_
...
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.