2022-12-08 03:47:02 -05:00
|
|
|
/*
|
2024-05-20 08:18:26 -04:00
|
|
|
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
|
2022-12-08 03:47:02 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2017-05-11 03:56:17 -04:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "sdkconfig.h"
|
2023-01-10 18:36:33 -05:00
|
|
|
#include "sys/queue.h"
|
2017-05-11 03:56:17 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <esp_err.h>
|
|
|
|
|
2017-11-16 18:16:46 -05:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2017-05-11 03:56:17 -04:00
|
|
|
#if !defined(CONFIG_HEAP_TRACING) && !defined(HEAP_TRACE_SRCFILE)
|
|
|
|
#warning "esp_heap_trace.h is included but heap tracing is disabled in menuconfig, functions are no-ops"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_HEAP_TRACING_STACK_DEPTH
|
|
|
|
#define CONFIG_HEAP_TRACING_STACK_DEPTH 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
HEAP_TRACE_ALL,
|
|
|
|
HEAP_TRACE_LEAKS,
|
|
|
|
} heap_trace_mode_t;
|
|
|
|
|
2017-07-18 19:07:17 -04:00
|
|
|
/**
|
|
|
|
* @brief Trace record data type. Stores information about an allocated region of memory.
|
|
|
|
*/
|
2023-02-14 03:48:14 -05:00
|
|
|
typedef struct heap_trace_record_t {
|
2018-08-06 21:37:57 -04:00
|
|
|
uint32_t ccount; ///< CCOUNT of the CPU when the allocation was made. LSB (bit value 1) is the CPU number (0 or 1).
|
2023-01-10 18:36:33 -05:00
|
|
|
void *address; ///< Address which was allocated. If NULL, then this record is empty.
|
2017-05-11 03:56:17 -04:00
|
|
|
size_t size; ///< Size of the allocation
|
|
|
|
void *alloced_by[CONFIG_HEAP_TRACING_STACK_DEPTH]; ///< Call stack of the caller which allocated the memory.
|
|
|
|
void *freed_by[CONFIG_HEAP_TRACING_STACK_DEPTH]; ///< Call stack of the caller which freed the memory (all zero if not freed.)
|
2023-03-30 06:37:51 -04:00
|
|
|
#if CONFIG_HEAP_TRACING_STANDALONE
|
|
|
|
TAILQ_ENTRY(heap_trace_record_t) tailq_list; ///< Linked list: prev & next records
|
|
|
|
#if CONFIG_HEAP_TRACE_HASH_MAP
|
2023-10-12 06:24:15 -04:00
|
|
|
SLIST_ENTRY(heap_trace_record_t) slist_hashmap; ///< Linked list: next in hashmap entry list
|
2023-03-30 06:37:51 -04:00
|
|
|
#endif // CONFIG_HEAP_TRACE_HASH_MAP
|
2023-02-14 03:48:14 -05:00
|
|
|
#endif // CONFIG_HEAP_TRACING_STANDALONE
|
|
|
|
} heap_trace_record_t;
|
2017-05-11 03:56:17 -04:00
|
|
|
|
2022-12-08 03:47:02 -05:00
|
|
|
/**
|
|
|
|
* @brief Stores information about the result of a heap trace.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
heap_trace_mode_t mode; ///< The heap trace mode we just completed / are running
|
|
|
|
size_t total_allocations; ///< The total number of allocations made during tracing
|
|
|
|
size_t total_frees; ///< The total number of frees made during tracing
|
|
|
|
size_t count; ///< The number of records in the internal buffer
|
|
|
|
size_t capacity; ///< The capacity of the internal buffer
|
|
|
|
size_t high_water_mark; ///< The maximum value that 'count' got to
|
|
|
|
size_t has_overflowed; ///< True if the internal buffer overflowed at some point
|
2023-03-01 06:42:14 -05:00
|
|
|
#if CONFIG_HEAP_TRACE_HASH_MAP
|
2023-02-17 17:22:17 -05:00
|
|
|
size_t total_hashmap_hits; ///< If hashmap is used, the total number of hits
|
|
|
|
size_t total_hashmap_miss; ///< If hashmap is used, the total number of misses (possibly due to overflow)
|
2023-03-01 06:42:14 -05:00
|
|
|
#endif
|
2022-12-08 03:47:02 -05:00
|
|
|
} heap_trace_summary_t;
|
|
|
|
|
2017-05-11 03:56:17 -04:00
|
|
|
/**
|
|
|
|
* @brief Initialise heap tracing in standalone mode.
|
|
|
|
*
|
|
|
|
* This function must be called before any other heap tracing functions.
|
|
|
|
*
|
|
|
|
* To disable heap tracing and allow the buffer to be freed, stop tracing and then call heap_trace_init_standalone(NULL, 0);
|
|
|
|
*
|
2022-12-08 03:47:02 -05:00
|
|
|
* @param record_buffer Provide a buffer to use for heap trace data.
|
|
|
|
* Note: External RAM is allowed, but it prevents recording allocations made from ISR's.
|
2017-05-11 03:56:17 -04:00
|
|
|
* @param num_records Size of the heap trace buffer, as number of record structures.
|
|
|
|
* @return
|
|
|
|
* - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
|
|
|
|
* - ESP_ERR_INVALID_STATE Heap tracing is currently in progress.
|
|
|
|
* - ESP_OK Heap tracing initialised successfully.
|
|
|
|
*/
|
|
|
|
esp_err_t heap_trace_init_standalone(heap_trace_record_t *record_buffer, size_t num_records);
|
|
|
|
|
2018-12-12 12:29:47 -05:00
|
|
|
/**
|
|
|
|
* @brief Initialise heap tracing in host-based mode.
|
|
|
|
*
|
|
|
|
* This function must be called before any other heap tracing functions.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - ESP_ERR_INVALID_STATE Heap tracing is currently in progress.
|
|
|
|
* - ESP_OK Heap tracing initialised successfully.
|
|
|
|
*/
|
|
|
|
esp_err_t heap_trace_init_tohost(void);
|
|
|
|
|
2017-05-11 03:56:17 -04:00
|
|
|
/**
|
|
|
|
* @brief Start heap tracing. All heap allocations & frees will be traced, until heap_trace_stop() is called.
|
|
|
|
*
|
|
|
|
* @note heap_trace_init_standalone() must be called to provide a valid buffer, before this function is called.
|
|
|
|
*
|
|
|
|
* @note Calling this function while heap tracing is running will reset the heap trace state and continue tracing.
|
|
|
|
*
|
2017-07-18 19:07:17 -04:00
|
|
|
* @param mode Mode for tracing.
|
2017-05-11 03:56:17 -04:00
|
|
|
* - HEAP_TRACE_ALL means all heap allocations and frees are traced.
|
2017-07-18 19:07:17 -04:00
|
|
|
* - HEAP_TRACE_LEAKS means only suspected memory leaks are traced. (When memory is freed, the record is removed from the trace buffer.)
|
2017-05-11 03:56:17 -04:00
|
|
|
* @return
|
|
|
|
* - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
|
|
|
|
* - ESP_ERR_INVALID_STATE A non-zero-length buffer has not been set via heap_trace_init_standalone().
|
|
|
|
* - ESP_OK Tracing is started.
|
|
|
|
*/
|
|
|
|
esp_err_t heap_trace_start(heap_trace_mode_t mode);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Stop heap tracing.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
|
|
|
|
* - ESP_ERR_INVALID_STATE Heap tracing was not in progress.
|
2024-05-20 08:18:26 -04:00
|
|
|
* - ESP_OK Heap tracing stopped.
|
2017-05-11 03:56:17 -04:00
|
|
|
*/
|
|
|
|
esp_err_t heap_trace_stop(void);
|
|
|
|
|
2024-05-20 08:18:26 -04:00
|
|
|
/**
|
|
|
|
* @brief Pause heap tracing of allocations.
|
|
|
|
*
|
|
|
|
* @note This function puts the heap tracing in the state where the new allocations
|
|
|
|
* will no longer be traced but the free will still be. This can be used to e.g.,
|
|
|
|
* strategically monitor a set of allocations to make sure each of them will get freed
|
|
|
|
* without polluting the list of records with unwanted allocations.
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
|
|
|
|
* - ESP_ERR_INVALID_STATE Heap tracing was not in progress.
|
|
|
|
* - ESP_OK Heap tracing paused.
|
|
|
|
*/
|
|
|
|
esp_err_t heap_trace_alloc_pause(void);
|
|
|
|
|
2017-05-11 03:56:17 -04:00
|
|
|
/**
|
|
|
|
* @brief Resume heap tracing which was previously stopped.
|
|
|
|
*
|
|
|
|
* Unlike heap_trace_start(), this function does not clear the
|
2017-07-18 19:07:17 -04:00
|
|
|
* buffer of any pre-existing trace records.
|
2017-05-11 03:56:17 -04:00
|
|
|
*
|
|
|
|
* The heap trace mode is the same as when heap_trace_start() was
|
|
|
|
* last called (or HEAP_TRACE_ALL if heap_trace_start() was never called).
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
* - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
|
|
|
|
* - ESP_ERR_INVALID_STATE Heap tracing was already started.
|
|
|
|
* - ESP_OK Heap tracing resumed.
|
|
|
|
*/
|
|
|
|
esp_err_t heap_trace_resume(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return number of records in the heap trace buffer
|
|
|
|
*
|
|
|
|
* It is safe to call this function while heap tracing is running.
|
|
|
|
*/
|
|
|
|
size_t heap_trace_get_count(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return a raw record from the heap trace buffer
|
|
|
|
*
|
2022-12-08 03:47:02 -05:00
|
|
|
* @note It is safe to call this function while heap tracing is
|
|
|
|
* running, however in HEAP_TRACE_LEAK mode record indexing may
|
2017-05-11 03:56:17 -04:00
|
|
|
* skip entries unless heap tracing is stopped first.
|
|
|
|
*
|
|
|
|
* @param index Index (zero-based) of the record to return.
|
2017-07-18 19:07:17 -04:00
|
|
|
* @param[out] record Record where the heap trace record will be copied.
|
2017-05-11 03:56:17 -04:00
|
|
|
* @return
|
|
|
|
* - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
|
|
|
|
* - ESP_ERR_INVALID_STATE Heap tracing was not initialised.
|
|
|
|
* - ESP_ERR_INVALID_ARG Index is out of bounds for current heap trace record count.
|
|
|
|
* - ESP_OK Record returned successfully.
|
|
|
|
*/
|
|
|
|
esp_err_t heap_trace_get(size_t index, heap_trace_record_t *record);
|
|
|
|
|
|
|
|
/**
|
2017-07-18 19:07:17 -04:00
|
|
|
* @brief Dump heap trace record data to stdout
|
2017-05-11 03:56:17 -04:00
|
|
|
*
|
2022-12-08 03:47:02 -05:00
|
|
|
* @note It is safe to call this function while heap tracing is
|
|
|
|
* running, however in HEAP_TRACE_LEAK mode the dump may skip
|
2017-05-11 03:56:17 -04:00
|
|
|
* entries unless heap tracing is stopped first.
|
2022-12-08 03:47:02 -05:00
|
|
|
*/
|
|
|
|
void heap_trace_dump(void);
|
|
|
|
|
|
|
|
/**
|
2022-12-09 07:21:37 -05:00
|
|
|
* @brief Dump heap trace from the memory of the capabilities passed as parameter.
|
2017-05-11 03:56:17 -04:00
|
|
|
*
|
2022-12-09 07:21:37 -05:00
|
|
|
* @param caps Capability(ies) of the memory from which to dump the trace.
|
|
|
|
* Set MALLOC_CAP_INTERNAL to dump heap trace data from internal memory.
|
|
|
|
* Set MALLOC_CAP_SPIRAM to dump heap trace data from PSRAM.
|
|
|
|
* Set both to dump both heap trace data.
|
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);
|
2022-12-08 03:47:02 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get summary information about the result of a heap trace
|
|
|
|
*
|
|
|
|
* @note It is safe to call this function while heap tracing is running.
|
|
|
|
*/
|
|
|
|
esp_err_t heap_trace_summary(heap_trace_summary_t *summary);
|
2017-11-16 18:16:46 -05:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|