2022-07-20 07:59:14 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2017-05-11 03:56:17 -04:00
|
|
|
#include <string.h>
|
|
|
|
#include <sdkconfig.h>
|
|
|
|
|
|
|
|
#define HEAP_TRACE_SRCFILE /* don't warn on inclusion here */
|
|
|
|
#include "esp_heap_trace.h"
|
|
|
|
#undef HEAP_TRACE_SRCFILE
|
|
|
|
|
|
|
|
#include "esp_attr.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2022-12-09 07:21:37 -05:00
|
|
|
#include "esp_memory_utils.h"
|
2023-01-10 18:36:33 -05:00
|
|
|
#include "sys/queue.h"
|
2017-05-11 03:56:17 -04:00
|
|
|
|
2017-09-05 05:29:57 -04:00
|
|
|
|
2017-05-11 03:56:17 -04:00
|
|
|
#define STACK_DEPTH CONFIG_HEAP_TRACING_STACK_DEPTH
|
|
|
|
|
2018-12-12 12:29:47 -05:00
|
|
|
#if CONFIG_HEAP_TRACING_STANDALONE
|
|
|
|
|
2017-05-11 03:56:17 -04:00
|
|
|
static portMUX_TYPE trace_mux = portMUX_INITIALIZER_UNLOCKED;
|
|
|
|
static bool tracing;
|
|
|
|
static heap_trace_mode_t mode;
|
|
|
|
|
2023-01-10 18:36:33 -05:00
|
|
|
/* Define struct: linked list of records */
|
2023-02-14 03:48:14 -05:00
|
|
|
TAILQ_HEAD(heap_trace_record_list_struct_t, heap_trace_record_t);
|
2023-01-10 18:36:33 -05:00
|
|
|
typedef struct heap_trace_record_list_struct_t heap_trace_record_list_t;
|
|
|
|
|
|
|
|
/* Linked List of Records */
|
2022-12-08 03:47:02 -05:00
|
|
|
typedef struct {
|
2017-05-11 03:56:17 -04:00
|
|
|
|
2023-01-10 18:36:33 -05:00
|
|
|
/* Buffer used for records. */
|
2022-12-08 03:47:02 -05:00
|
|
|
heap_trace_record_t *buffer;
|
2017-05-11 03:56:17 -04:00
|
|
|
|
2023-01-10 18:36:33 -05:00
|
|
|
/* Linked list of recorded allocations */
|
|
|
|
heap_trace_record_list_t list;
|
|
|
|
|
|
|
|
/* Linked list of available record objects */
|
|
|
|
heap_trace_record_list_t unused;
|
|
|
|
|
2023-01-13 15:33:43 -05:00
|
|
|
/* capacity of 'buffer' */
|
2022-12-08 03:47:02 -05:00
|
|
|
size_t capacity;
|
|
|
|
|
2023-01-13 15:33:43 -05:00
|
|
|
/* Count of entries in 'list' */
|
2022-12-08 03:47:02 -05:00
|
|
|
size_t count;
|
|
|
|
|
|
|
|
/* During execution, we remember the maximum
|
|
|
|
value of 'count'. This can help you
|
|
|
|
choose the right size for your buffer capacity.*/
|
|
|
|
size_t high_water_mark;
|
|
|
|
|
|
|
|
/* Has the buffer overflowed and lost trace entries? */
|
|
|
|
bool has_overflowed;
|
|
|
|
} records_t;
|
|
|
|
|
|
|
|
// Forward Defines
|
|
|
|
static void heap_trace_dump_base(bool internal_ram, bool psram);
|
2023-02-14 03:48:14 -05:00
|
|
|
static void record_deep_copy(heap_trace_record_t *rDest, const heap_trace_record_t* r_src);
|
|
|
|
static void list_setup(void);
|
|
|
|
static void list_remove(heap_trace_record_t* r_remove);
|
|
|
|
static bool list_add(const heap_trace_record_t* r_append);
|
|
|
|
static heap_trace_record_t* list_pop_unused(void);
|
2023-01-10 18:36:33 -05:00
|
|
|
static heap_trace_record_t* list_find_address_reverse(void* p);
|
2022-12-08 03:47:02 -05:00
|
|
|
|
|
|
|
/* The actual records. */
|
|
|
|
static records_t records;
|
2017-05-11 03:56:17 -04:00
|
|
|
|
|
|
|
/* Actual number of allocations logged */
|
|
|
|
static size_t total_allocations;
|
|
|
|
|
|
|
|
/* Actual number of frees logged */
|
|
|
|
static size_t total_frees;
|
|
|
|
|
2023-01-13 15:33:43 -05:00
|
|
|
/* Used to speed up heap_trace_get */
|
2023-02-14 03:48:14 -05:00
|
|
|
static heap_trace_record_t* r_get;
|
|
|
|
static size_t r_get_idx;
|
2023-01-13 15:33:43 -05:00
|
|
|
|
2017-05-11 03:56:17 -04:00
|
|
|
esp_err_t heap_trace_init_standalone(heap_trace_record_t *record_buffer, size_t num_records)
|
|
|
|
{
|
|
|
|
if (tracing) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
2022-12-08 03:47:02 -05:00
|
|
|
|
2023-02-14 03:48:14 -05:00
|
|
|
if (record_buffer == NULL || num_records == 0) {
|
2023-01-10 18:36:33 -05:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2022-12-08 03:47:02 -05:00
|
|
|
records.buffer = record_buffer;
|
|
|
|
records.capacity = num_records;
|
|
|
|
|
2017-05-11 03:56:17 -04:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t heap_trace_start(heap_trace_mode_t mode_param)
|
|
|
|
{
|
2022-12-08 03:47:02 -05:00
|
|
|
if (records.buffer == NULL || records.capacity == 0) {
|
2017-05-11 03:56:17 -04:00
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
2018-12-12 12:29:47 -05:00
|
|
|
|
2018-01-29 08:41:58 -05:00
|
|
|
portENTER_CRITICAL(&trace_mux);
|
2017-05-11 03:56:17 -04:00
|
|
|
|
|
|
|
tracing = false;
|
|
|
|
mode = mode_param;
|
2022-12-08 03:47:02 -05:00
|
|
|
|
2023-01-10 18:36:33 -05:00
|
|
|
// clear buffer
|
|
|
|
memset(records.buffer, 0, sizeof(heap_trace_record_t) * records.capacity);
|
|
|
|
|
2022-12-08 03:47:02 -05:00
|
|
|
records.count = 0;
|
|
|
|
records.has_overflowed = false;
|
2023-02-14 03:48:14 -05:00
|
|
|
list_setup();
|
2022-12-08 03:47:02 -05:00
|
|
|
|
2017-05-11 03:56:17 -04:00
|
|
|
total_allocations = 0;
|
|
|
|
total_frees = 0;
|
|
|
|
heap_trace_resume();
|
|
|
|
|
2018-01-29 08:41:58 -05:00
|
|
|
portEXIT_CRITICAL(&trace_mux);
|
2017-05-11 03:56:17 -04:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t set_tracing(bool enable)
|
|
|
|
{
|
|
|
|
if (tracing == enable) {
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
tracing = enable;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t heap_trace_stop(void)
|
|
|
|
{
|
|
|
|
return set_tracing(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t heap_trace_resume(void)
|
|
|
|
{
|
|
|
|
return set_tracing(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t heap_trace_get_count(void)
|
|
|
|
{
|
2022-12-08 03:47:02 -05:00
|
|
|
return records.count;
|
2017-05-11 03:56:17 -04:00
|
|
|
}
|
|
|
|
|
2023-02-14 03:48:14 -05:00
|
|
|
esp_err_t heap_trace_get(size_t index, heap_trace_record_t *r_out)
|
2017-05-11 03:56:17 -04:00
|
|
|
{
|
2023-02-14 03:48:14 -05:00
|
|
|
if (r_out == NULL) {
|
2017-05-11 03:56:17 -04:00
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
2022-12-08 03:47:02 -05:00
|
|
|
|
2017-05-11 03:56:17 -04:00
|
|
|
esp_err_t result = ESP_OK;
|
|
|
|
|
2018-01-29 08:41:58 -05:00
|
|
|
portENTER_CRITICAL(&trace_mux);
|
2022-12-08 03:47:02 -05:00
|
|
|
|
|
|
|
if (index >= records.count) {
|
2023-01-10 18:36:33 -05:00
|
|
|
|
2017-05-11 03:56:17 -04:00
|
|
|
result = ESP_ERR_INVALID_ARG; /* out of range for 'count' */
|
2023-01-10 18:36:33 -05:00
|
|
|
|
2017-05-11 03:56:17 -04:00
|
|
|
} else {
|
2023-01-10 18:36:33 -05:00
|
|
|
|
2023-01-13 15:33:43 -05:00
|
|
|
// Perf: speed up sequential access
|
2023-02-14 03:48:14 -05:00
|
|
|
if (r_get && r_get_idx == index - 1) {
|
2023-01-10 18:36:33 -05:00
|
|
|
|
2023-02-14 03:48:14 -05:00
|
|
|
r_get = TAILQ_NEXT(r_get, tailq);
|
|
|
|
r_get_idx = index;
|
2023-01-10 18:36:33 -05:00
|
|
|
|
2023-01-13 15:33:43 -05:00
|
|
|
} else {
|
2023-01-10 18:36:33 -05:00
|
|
|
|
2023-01-13 15:33:43 -05:00
|
|
|
// Iterate through through the linked list
|
|
|
|
|
2023-02-14 03:48:14 -05:00
|
|
|
r_get = TAILQ_FIRST(&records.list);
|
2023-01-13 15:33:43 -05:00
|
|
|
|
|
|
|
for (int i = 0; i < index; i++) {
|
2023-01-10 18:36:33 -05:00
|
|
|
|
2023-02-14 03:48:14 -05:00
|
|
|
if (r_get == NULL) {
|
2023-01-13 15:33:43 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-02-14 03:48:14 -05:00
|
|
|
r_get = TAILQ_NEXT(r_get, tailq);
|
|
|
|
r_get_idx = i + 1;
|
2023-01-13 15:33:43 -05:00
|
|
|
}
|
2023-01-10 18:36:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// copy to destination
|
2023-02-14 03:48:14 -05:00
|
|
|
if (r_get) {
|
|
|
|
memcpy(r_out, r_get, sizeof(heap_trace_record_t));
|
2023-01-13 15:33:43 -05:00
|
|
|
} else {
|
|
|
|
// this should not happen since we already
|
|
|
|
// checked that index < records.count,
|
|
|
|
// but could be indicative of memory corruption
|
|
|
|
result = ESP_ERR_INVALID_STATE;
|
2023-02-14 03:48:14 -05:00
|
|
|
memset(r_out, 0, sizeof(heap_trace_record_t));
|
2023-01-13 15:33:43 -05:00
|
|
|
}
|
2017-05-11 03:56:17 -04:00
|
|
|
}
|
2022-12-08 03:47:02 -05:00
|
|
|
|
2018-01-29 08:41:58 -05:00
|
|
|
portEXIT_CRITICAL(&trace_mux);
|
2017-05-11 03:56:17 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-12-08 03:47:02 -05:00
|
|
|
esp_err_t heap_trace_summary(heap_trace_summary_t *summary)
|
|
|
|
{
|
|
|
|
if (summary == NULL) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
portENTER_CRITICAL(&trace_mux);
|
|
|
|
summary->mode = mode;
|
|
|
|
summary->total_allocations = total_allocations;
|
|
|
|
summary->total_frees = total_frees;
|
|
|
|
summary->count = records.count;
|
|
|
|
summary->capacity = records.capacity;
|
|
|
|
summary->high_water_mark = records.high_water_mark;
|
|
|
|
summary->has_overflowed = records.has_overflowed;
|
|
|
|
portEXIT_CRITICAL(&trace_mux);
|
2017-05-11 03:56:17 -04:00
|
|
|
|
2022-12-08 03:47:02 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2022-12-09 07:21:37 -05:00
|
|
|
void heap_trace_dump(void) {
|
|
|
|
heap_trace_dump_caps(MALLOC_CAP_INTERNAL | MALLOC_CAP_SPIRAM);
|
2022-12-08 03:47:02 -05:00
|
|
|
}
|
|
|
|
|
2022-12-09 07:21:37 -05:00
|
|
|
void heap_trace_dump_caps(const uint32_t caps) {
|
|
|
|
heap_trace_dump_base(caps & MALLOC_CAP_INTERNAL, caps & MALLOC_CAP_SPIRAM);
|
2022-12-08 03:47:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void heap_trace_dump_base(bool internal_ram, bool psram)
|
2017-05-11 03:56:17 -04:00
|
|
|
{
|
2023-01-10 18:36:33 -05:00
|
|
|
portENTER_CRITICAL(&trace_mux);
|
|
|
|
|
2017-05-11 03:56:17 -04:00
|
|
|
size_t delta_size = 0;
|
|
|
|
size_t delta_allocs = 0;
|
2022-12-08 03:47:02 -05:00
|
|
|
size_t start_count = records.count;
|
|
|
|
|
2023-02-14 03:48:14 -05:00
|
|
|
esp_rom_printf("====== Heap Trace: %u records (%u capacity) ======\n",
|
2022-12-08 03:47:02 -05:00
|
|
|
records.count, records.capacity);
|
|
|
|
|
2023-01-10 18:36:33 -05:00
|
|
|
// Iterate through through the linked list
|
|
|
|
|
|
|
|
heap_trace_record_t *rCur = TAILQ_FIRST(&records.list);
|
|
|
|
|
2022-12-08 03:47:02 -05:00
|
|
|
for (int i = 0; i < records.count; i++) {
|
|
|
|
|
2023-01-10 18:36:33 -05:00
|
|
|
// check corruption
|
|
|
|
if (rCur == NULL) {
|
2023-02-14 03:48:14 -05:00
|
|
|
esp_rom_printf("\nError: heap trace linked list is corrupt. expected more records.\n");
|
2023-01-10 18:36:33 -05:00
|
|
|
break;
|
|
|
|
}
|
2022-12-08 03:47:02 -05:00
|
|
|
|
2023-01-10 18:36:33 -05:00
|
|
|
bool should_print = rCur->address != NULL &&
|
2022-12-08 03:47:02 -05:00
|
|
|
((psram && internal_ram) ||
|
2023-01-10 18:36:33 -05:00
|
|
|
(internal_ram && esp_ptr_internal(rCur->address)) ||
|
|
|
|
(psram && esp_ptr_external_ram(rCur->address)));
|
2022-12-08 03:47:02 -05:00
|
|
|
|
|
|
|
if (should_print) {
|
|
|
|
|
|
|
|
const char* label = "";
|
2023-01-10 18:36:33 -05:00
|
|
|
if (esp_ptr_internal(rCur->address)) {
|
2022-12-08 03:47:02 -05:00
|
|
|
label = ", Internal";
|
|
|
|
}
|
2023-01-10 18:36:33 -05:00
|
|
|
if (esp_ptr_external_ram(rCur->address)) {
|
2022-12-08 03:47:02 -05:00
|
|
|
label = ", PSRAM";
|
|
|
|
}
|
|
|
|
|
2023-02-14 03:48:14 -05:00
|
|
|
esp_rom_printf("%6d bytes (@ %p%s) allocated CPU %d ccount 0x%08x caller ",
|
2023-01-10 18:36:33 -05:00
|
|
|
rCur->size, rCur->address, label, rCur->ccount & 1, rCur->ccount & ~3);
|
2022-12-08 03:47:02 -05:00
|
|
|
|
2023-01-10 18:36:33 -05:00
|
|
|
for (int j = 0; j < STACK_DEPTH && rCur->alloced_by[j] != 0; j++) {
|
2023-02-14 03:48:14 -05:00
|
|
|
esp_rom_printf("%p%s", rCur->alloced_by[j],
|
2017-05-11 03:56:17 -04:00
|
|
|
(j < STACK_DEPTH - 1) ? ":" : "");
|
|
|
|
}
|
|
|
|
|
2023-01-10 18:36:33 -05:00
|
|
|
if (mode != HEAP_TRACE_ALL || STACK_DEPTH == 0 || rCur->freed_by[0] == NULL) {
|
|
|
|
delta_size += rCur->size;
|
2017-05-11 03:56:17 -04:00
|
|
|
delta_allocs++;
|
2023-02-14 03:48:14 -05:00
|
|
|
esp_rom_printf("\n");
|
2017-05-11 03:56:17 -04:00
|
|
|
} else {
|
2023-02-14 03:48:14 -05:00
|
|
|
esp_rom_printf("\nfreed by ");
|
2017-05-11 03:56:17 -04:00
|
|
|
for (int j = 0; j < STACK_DEPTH; j++) {
|
2023-02-14 03:48:14 -05:00
|
|
|
esp_rom_printf("%p%s", rCur->freed_by[j],
|
2017-05-11 03:56:17 -04:00
|
|
|
(j < STACK_DEPTH - 1) ? ":" : "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-10 18:36:33 -05:00
|
|
|
|
|
|
|
rCur = TAILQ_NEXT(rCur, tailq);
|
2017-05-11 03:56:17 -04:00
|
|
|
}
|
2022-12-08 03:47:02 -05:00
|
|
|
|
2023-02-14 03:48:14 -05:00
|
|
|
esp_rom_printf("====== Heap Trace Summary ======\n");
|
2022-12-08 03:47:02 -05:00
|
|
|
|
2017-05-11 03:56:17 -04:00
|
|
|
if (mode == HEAP_TRACE_ALL) {
|
2023-02-14 03:48:14 -05:00
|
|
|
esp_rom_printf("Mode: Heap Trace All\n");
|
|
|
|
esp_rom_printf("%u bytes alive in trace (%u/%u allocations)\n",
|
2017-05-11 03:56:17 -04:00
|
|
|
delta_size, delta_allocs, heap_trace_get_count());
|
|
|
|
} else {
|
2023-02-14 03:48:14 -05:00
|
|
|
esp_rom_printf("Mode: Heap Trace Leaks\n");
|
|
|
|
esp_rom_printf("%u bytes 'leaked' in trace (%u allocations)\n", delta_size, delta_allocs);
|
2017-05-11 03:56:17 -04:00
|
|
|
}
|
2022-12-08 03:47:02 -05:00
|
|
|
|
2023-02-14 03:48:14 -05:00
|
|
|
esp_rom_printf("records: %u (%u capacity, %u high water mark)\n",
|
2022-12-08 03:47:02 -05:00
|
|
|
records.count, records.capacity, records.high_water_mark);
|
|
|
|
|
2023-02-14 03:48:14 -05:00
|
|
|
esp_rom_printf("total allocations: %u\n", total_allocations);
|
|
|
|
esp_rom_printf("total frees: %u\n", total_frees);
|
2022-12-08 03:47:02 -05:00
|
|
|
|
|
|
|
if (start_count != records.count) { // only a problem if trace isn't stopped before dumping
|
2023-02-14 03:48:14 -05:00
|
|
|
esp_rom_printf("(NB: New entries were traced while dumping, so trace dump may have duplicate entries.)\n");
|
2017-05-11 03:56:17 -04:00
|
|
|
}
|
2022-12-08 03:47:02 -05:00
|
|
|
if (records.has_overflowed) {
|
2023-02-14 03:48:14 -05:00
|
|
|
esp_rom_printf("(NB: Internal Buffer has overflowed, so trace data is incomplete.)\n");
|
2017-05-11 03:56:17 -04:00
|
|
|
}
|
2023-02-14 03:48:14 -05:00
|
|
|
esp_rom_printf("================================\n");
|
2023-01-10 18:36:33 -05:00
|
|
|
|
|
|
|
portEXIT_CRITICAL(&trace_mux);
|
2017-05-11 03:56:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a new allocation to the heap trace records */
|
2023-01-10 18:36:33 -05:00
|
|
|
static IRAM_ATTR void record_allocation(const heap_trace_record_t *rAllocation)
|
2017-05-11 03:56:17 -04:00
|
|
|
{
|
2023-01-10 18:36:33 -05:00
|
|
|
if (!tracing || rAllocation->address == NULL) {
|
2018-12-12 12:29:47 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-29 08:41:58 -05:00
|
|
|
portENTER_CRITICAL(&trace_mux);
|
2022-12-08 03:47:02 -05:00
|
|
|
|
2017-05-11 03:56:17 -04:00
|
|
|
if (tracing) {
|
2022-12-08 03:47:02 -05:00
|
|
|
|
2023-01-10 18:36:33 -05:00
|
|
|
// If buffer is full, pop off the oldest
|
|
|
|
// record to make more space
|
2022-12-08 03:47:02 -05:00
|
|
|
if (records.count == records.capacity) {
|
|
|
|
|
|
|
|
records.has_overflowed = true;
|
|
|
|
|
2023-01-10 18:36:33 -05:00
|
|
|
heap_trace_record_t *rFirst = TAILQ_FIRST(&records.list);
|
2022-12-08 03:47:02 -05:00
|
|
|
|
2023-01-10 18:36:33 -05:00
|
|
|
list_remove(rFirst);
|
2017-05-11 03:56:17 -04:00
|
|
|
}
|
2022-12-08 03:47:02 -05:00
|
|
|
|
2023-01-10 18:36:33 -05:00
|
|
|
// push onto end of list
|
2023-01-10 18:36:33 -05:00
|
|
|
list_add(rAllocation);
|
2022-12-08 03:47:02 -05:00
|
|
|
|
2017-05-11 03:56:17 -04:00
|
|
|
total_allocations++;
|
|
|
|
}
|
2022-12-08 03:47:02 -05:00
|
|
|
|
2018-01-29 08:41:58 -05:00
|
|
|
portEXIT_CRITICAL(&trace_mux);
|
2017-05-11 03:56:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* record a free event in the heap trace log
|
|
|
|
|
|
|
|
For HEAP_TRACE_ALL, this means filling in the freed_by pointer.
|
|
|
|
For HEAP_TRACE_LEAKS, this means removing the record from the log.
|
2023-02-14 03:48:14 -05:00
|
|
|
|
|
|
|
callers is an array of STACK_DEPTH function pointer from the call stack
|
|
|
|
leading to the call of record_free.
|
2017-05-11 03:56:17 -04:00
|
|
|
*/
|
|
|
|
static IRAM_ATTR void record_free(void *p, void **callers)
|
|
|
|
{
|
2018-12-12 12:29:47 -05:00
|
|
|
if (!tracing || p == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-29 08:41:58 -05:00
|
|
|
portENTER_CRITICAL(&trace_mux);
|
2022-12-08 03:47:02 -05:00
|
|
|
|
|
|
|
if (tracing) {
|
|
|
|
|
2017-05-11 03:56:17 -04:00
|
|
|
total_frees++;
|
2022-12-08 03:47:02 -05:00
|
|
|
|
2023-01-10 18:36:33 -05:00
|
|
|
// search backwards for the allocation record matching this free
|
2023-01-10 18:36:33 -05:00
|
|
|
heap_trace_record_t* rFound = list_find_address_reverse(p);
|
2017-05-11 03:56:17 -04:00
|
|
|
|
2023-01-10 18:36:33 -05:00
|
|
|
if (rFound) {
|
2017-05-11 03:56:17 -04:00
|
|
|
if (mode == HEAP_TRACE_ALL) {
|
2022-12-08 03:47:02 -05:00
|
|
|
|
|
|
|
// add 'freed_by' info to the record
|
2023-01-10 18:36:33 -05:00
|
|
|
memcpy(rFound->freed_by, callers, sizeof(void *) * STACK_DEPTH);
|
2022-12-08 03:47:02 -05:00
|
|
|
|
2017-05-11 03:56:17 -04:00
|
|
|
} else { // HEAP_TRACE_LEAKS
|
2022-12-08 03:47:02 -05:00
|
|
|
|
|
|
|
// Leak trace mode, once an allocation is freed
|
|
|
|
// we remove it from the list
|
2023-01-10 18:36:33 -05:00
|
|
|
list_remove(rFound);
|
2017-05-11 03:56:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-08 03:47:02 -05:00
|
|
|
|
2018-01-29 08:41:58 -05:00
|
|
|
portEXIT_CRITICAL(&trace_mux);
|
2017-05-11 03:56:17 -04:00
|
|
|
}
|
|
|
|
|
2023-01-10 18:36:33 -05:00
|
|
|
// connect all records into a linked list of 'unused' records
|
2023-02-14 03:48:14 -05:00
|
|
|
static void list_setup(void)
|
2017-05-11 03:56:17 -04:00
|
|
|
{
|
2023-01-10 18:36:33 -05:00
|
|
|
TAILQ_INIT(&records.list);
|
|
|
|
TAILQ_INIT(&records.unused);
|
|
|
|
|
|
|
|
for (int i = 0; i < records.capacity; i++) {
|
|
|
|
|
|
|
|
heap_trace_record_t* rCur = &records.buffer[i];
|
|
|
|
|
2023-02-14 03:48:14 -05:00
|
|
|
TAILQ_INSERT_TAIL(&records.unused, rCur, tailq);
|
2023-01-10 18:36:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-14 03:48:14 -05:00
|
|
|
/* 1. removes record r_remove from records.list,
|
2023-01-10 18:36:33 -05:00
|
|
|
2. places it into records.unused */
|
2023-02-14 03:48:14 -05:00
|
|
|
static IRAM_ATTR void list_remove(heap_trace_record_t* r_remove)
|
2023-01-10 18:36:33 -05:00
|
|
|
{
|
|
|
|
assert(records.count > 0);
|
|
|
|
|
|
|
|
// remove from records.list
|
2023-02-14 03:48:14 -05:00
|
|
|
TAILQ_REMOVE(&records.list, r_remove, tailq);
|
2023-01-10 18:36:33 -05:00
|
|
|
|
|
|
|
// set as unused
|
2023-02-14 03:48:14 -05:00
|
|
|
r_remove->address = 0;
|
|
|
|
r_remove->size = 0;
|
2023-01-10 18:36:33 -05:00
|
|
|
|
|
|
|
// add to records.unused
|
2023-02-14 03:48:14 -05:00
|
|
|
TAILQ_INSERT_HEAD(&records.unused, r_remove, tailq);
|
2023-01-10 18:36:33 -05:00
|
|
|
|
|
|
|
// decrement
|
|
|
|
records.count--;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// pop record from unused list
|
2023-02-14 03:48:14 -05:00
|
|
|
static IRAM_ATTR heap_trace_record_t* list_pop_unused(void)
|
2023-01-10 18:36:33 -05:00
|
|
|
{
|
|
|
|
// no records left?
|
|
|
|
if (records.count >= records.capacity) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get from records.unused
|
|
|
|
heap_trace_record_t* rUnused = TAILQ_FIRST(&records.unused);
|
|
|
|
assert(rUnused->address == NULL);
|
|
|
|
assert(rUnused->size == 0);
|
|
|
|
|
|
|
|
// remove from records.unused
|
|
|
|
TAILQ_REMOVE(&records.unused, rUnused, tailq);
|
|
|
|
|
|
|
|
return rUnused;
|
|
|
|
}
|
|
|
|
|
|
|
|
// deep copy a record.
|
|
|
|
// Note: only copies the *allocation data*, not the next & prev ptrs
|
2023-02-14 03:48:14 -05:00
|
|
|
static IRAM_ATTR void record_deep_copy(heap_trace_record_t *rDest, const heap_trace_record_t *r_src)
|
2023-01-10 18:36:33 -05:00
|
|
|
{
|
2023-02-14 03:48:14 -05:00
|
|
|
rDest->ccount = r_src->ccount;
|
|
|
|
rDest->address = r_src->address;
|
|
|
|
rDest->size = r_src->size;
|
|
|
|
memcpy(rDest->freed_by, r_src->freed_by, sizeof(void *) * STACK_DEPTH);
|
|
|
|
memcpy(rDest->alloced_by, r_src->alloced_by, sizeof(void *) * STACK_DEPTH);
|
2023-01-10 18:36:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Append a record to records.list
|
2023-02-14 03:48:14 -05:00
|
|
|
// Note: This deep copies r_append
|
|
|
|
static IRAM_ATTR bool list_add(const heap_trace_record_t *r_append)
|
2023-01-10 18:36:33 -05:00
|
|
|
{
|
|
|
|
if (records.count < records.capacity) {
|
|
|
|
|
|
|
|
// get unused record
|
2023-01-10 18:36:33 -05:00
|
|
|
heap_trace_record_t* rDest = list_pop_unused();
|
2023-01-10 18:36:33 -05:00
|
|
|
|
|
|
|
// we checked that there is capacity, so this
|
|
|
|
// should never be null.
|
|
|
|
assert(rDest != NULL);
|
|
|
|
|
|
|
|
// copy allocation data
|
2023-02-14 03:48:14 -05:00
|
|
|
record_deep_copy(rDest, r_append);
|
2023-01-10 18:36:33 -05:00
|
|
|
|
|
|
|
// append to records.list
|
|
|
|
TAILQ_INSERT_TAIL(&records.list, rDest, tailq);
|
|
|
|
|
|
|
|
// increment
|
|
|
|
records.count++;
|
|
|
|
|
|
|
|
// high water mark
|
|
|
|
if (records.count > records.high_water_mark) {
|
|
|
|
records.high_water_mark = records.count;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
2017-05-11 03:56:17 -04:00
|
|
|
} else {
|
2023-01-10 18:36:33 -05:00
|
|
|
records.has_overflowed = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// search records.list backwards for the allocation record matching this address
|
2023-01-10 18:36:33 -05:00
|
|
|
static IRAM_ATTR heap_trace_record_t* list_find_address_reverse(void* p)
|
2023-01-10 18:36:33 -05:00
|
|
|
{
|
|
|
|
if (records.count == 0) {
|
|
|
|
return NULL;
|
2017-05-11 03:56:17 -04:00
|
|
|
}
|
2023-01-10 18:36:33 -05:00
|
|
|
|
|
|
|
heap_trace_record_t* rFound = NULL;
|
|
|
|
|
|
|
|
// Perf: We search backwards because new allocations are appended
|
|
|
|
// to the end of the list and most allocations are short lived.
|
|
|
|
heap_trace_record_t *rCur = NULL;
|
|
|
|
TAILQ_FOREACH_REVERSE(rCur, &records.list, heap_trace_record_list_struct_t, tailq) {
|
|
|
|
if (rCur->address == p) {
|
|
|
|
rFound = rCur;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rFound;
|
2017-05-11 03:56:17 -04:00
|
|
|
}
|
|
|
|
|
2018-12-12 12:29:47 -05:00
|
|
|
#include "heap_trace.inc"
|
2017-05-11 03:56:17 -04:00
|
|
|
|
2022-12-08 03:47:02 -05:00
|
|
|
#endif // CONFIG_HEAP_TRACING_STANDALONE
|