malloc: Add heap debugging commands and mcheck caller tracking

This series adds improved heap-debugging capabilities.

As part of this, the recently added backtrace feature is reworked to avoid itself using malloc(), which makes it difficult for malloc() to use.

A new 'malloc' command with 'info' and 'dump' subcommands allows inspecting the heap state at runtime.

The mcheck heap-protection feature is integrated into Kconfig and can be used to to track caller information for each allocation, showing the function name and line number.

The caller info can be seen with 'malloc dump', including for freed chunks where possible.

The 'malloc info' command shows a few more heap statistics:

=> malloc info
total bytes   = 96 MiB
in use bytes  = 700.9 KiB
malloc count  = 1234
free count    = 567
realloc count = 89

The 'malloc dump' command walks the heap showing each chunk:

=> malloc dump
Heap dump: 19a0e000 - 1fa10000
     Address        Size  Status
----------------------------------
    19a0e000          10  (chunk header)
    19a0e010          a0        log_init:453 <-board_init_r:774 <-sandbox_flow:
    19a0e0b0       20070        membuf_new:420 <-console_record_init:880 <-boar
    19a2e120         170        membuf_new:420 <-console_record_init:886 <-boar
    19a2e290         150        unflatten_device_tree:299 <-of_live_build:328 <
    19a2e3e0          a0        uclass_get:157 <-device_bind_common:59 <-device
    ...
    19a4b080          70  free  done_word:2489 <-parse_stream_outer:3190 <-pars
    ...

This is useful for debugging memory leaks, understanding allocation patterns, and tracking down heap corruption issues.

Some additional patches are included to make all this work:

  • format_size() for human-readable size formatting
  • ut_asserteq_regex() for flexible test assertions
  • backtrace_str() for condensed backtrace output

Finally, this series includes a few patches for some of the more obvious memory leaks (scmi and some test drivers), plus a reduction in truetype memory allocations and a fix for a watchdog crash with 'ut dm'.

Merge request reports

Loading