Page 1 of 1

Logging from allocation trace hooks fails (LoadProhibited @_vfprintf_r)

Posted: Wed May 31, 2023 5:42 am
by RichPiano
We're currently fixing a notorious memory leak and wanted to try out the memory allocation hooks

Code: Select all

#include "esp_heap_caps.h"

void esp_heap_trace_alloc_hook(void* ptr, size_t size, uint32_t caps)
{
    printf("heap alloc of %zu bytes @%p", size, ptr);
}
void esp_heap_trace_free_hook(void* ptr)
{
    printf("heap free @%p", ptr);
}
This is not working however as the app crashes because of vfprintf accessing some prohibited memory:

Code: Select all

Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.

Core  0 register dump:
PC      : 0x401803e0  PS      : 0x00060930  A0      : 0x80177978  A1      : 0x3ffe34f0
0x401803e0: _vfprintf_r at /builds/idf/crosstool-NG/.build/HOST-x86_64-w64-mingw32/xtensa-esp32-elf/src/newlib/newlib/libc/stdio/vfprintf.c:849 (discriminator 14)

A2      : 0x3ff96458  A3      : 0x3ffe3500  A4      : 0x3f405528  A5      : 0x3ffe3840  
A6      : 0x3ffe3820  A7      : 0x00000004  A8      : 0x00000000  A9      : 0x3ffe34d0
A10     : 0x00000000  A11     : 0x3ffe3744  A12     : 0x000000ff  A13     : 0x0000ff00
A14     : 0x00ff0000  A15     : 0xff000000  SAR     : 0x0000001e  EXCCAUSE: 0x0000001c
EXCVADDR: 0x00000064  LBEG    : 0x4000c2e0  LEND    : 0x4000c2f6  LCOUNT  : 0x00000000

I had this before when I tried to inject logs into FreeRTOS functions and it's very annoying. Why does this happen and how can I fix it quickly?

Re: Logging from allocation trace hooks fails (LoadProhibited @_vfprintf_r)

Posted: Wed May 31, 2023 8:19 am
by MicroController
Solution: Use the heap tracing functionality of IDF.

Adding logging output to low-level functions like malloc or FreeRTOS is bound to cause problems. These functions may be used in ISR or OS contexts, or inside critical sections, with interrupts and/or context switching disabled, and may be called in bursts too frequently for logging to keep up; logging there can at least cause excessive delays in functions which are required to complete quickly.

Re: Logging from allocation trace hooks fails (LoadProhibited @_vfprintf_r)

Posted: Wed May 31, 2023 1:25 pm
by RichPiano
I don't care that's not production code and performance is secondary in debugging ;) The problem is that I already tried the heap tracing functionality, but unfortunately only allocations and not frees are being logged, so that is not of much help. My solution now would be to reimplement the

Code: Select all

heap_trace_start
and

Code: Select all

heap_trace_stop
functionality if no more convenient workaround is available.

Re: Logging from allocation trace hooks fails (LoadProhibited @_vfprintf_r)

Posted: Wed May 31, 2023 1:50 pm
by MicroController
RichPiano wrote:
Wed May 31, 2023 1:25 pm
I already tried the heap tracing functionality, but unfortunately only allocations and not frees are being logged, so that is not of much help.
So you're seeing more allocations than deallocations...

For that, there are three possible explanations:
a) there is a major bug in IDF's heap tracing which no-one noticed before,
b) you are using the heap tracing wrong, or
c) you have a memory leak in your application.

Re: Logging from allocation trace hooks fails (LoadProhibited @_vfprintf_r)

Posted: Mon Jun 05, 2023 4:13 pm
by RichPiano
Thanks I managed to trace back the memory leak to a bug in my application. I created some wrappers around the trace library in the process which was very helpful.