esp_heap_trace.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include "sdkconfig.h"
  8. #include "sys/queue.h"
  9. #include <stdint.h>
  10. #include <esp_err.h>
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. #if !defined(CONFIG_HEAP_TRACING) && !defined(HEAP_TRACE_SRCFILE)
  15. #warning "esp_heap_trace.h is included but heap tracing is disabled in menuconfig, functions are no-ops"
  16. #endif
  17. #ifndef CONFIG_HEAP_TRACING_STACK_DEPTH
  18. #define CONFIG_HEAP_TRACING_STACK_DEPTH 0
  19. #endif
  20. typedef enum {
  21. HEAP_TRACE_ALL,
  22. HEAP_TRACE_LEAKS,
  23. } heap_trace_mode_t;
  24. /**
  25. * @brief Trace record data type. Stores information about an allocated region of memory.
  26. */
  27. typedef struct heap_trace_record_t {
  28. uint32_t ccount; ///< CCOUNT of the CPU when the allocation was made. LSB (bit value 1) is the CPU number (0 or 1).
  29. void *address; ///< Address which was allocated. If NULL, then this record is empty.
  30. size_t size; ///< Size of the allocation
  31. void *alloced_by[CONFIG_HEAP_TRACING_STACK_DEPTH]; ///< Call stack of the caller which allocated the memory.
  32. void *freed_by[CONFIG_HEAP_TRACING_STACK_DEPTH]; ///< Call stack of the caller which freed the memory (all zero if not freed.)
  33. #if CONFIG_HEAP_TRACING_STANDALONE
  34. TAILQ_ENTRY(heap_trace_record_t) tailq_list; ///< Linked list: prev & next records
  35. #if CONFIG_HEAP_TRACE_HASH_MAP
  36. SLIST_ENTRY(heap_trace_record_t) slist_hashmap; ///< Linked list: next in hashmap entry list
  37. #endif // CONFIG_HEAP_TRACE_HASH_MAP
  38. #endif // CONFIG_HEAP_TRACING_STANDALONE
  39. } heap_trace_record_t;
  40. /**
  41. * @brief Stores information about the result of a heap trace.
  42. */
  43. typedef struct {
  44. heap_trace_mode_t mode; ///< The heap trace mode we just completed / are running
  45. size_t total_allocations; ///< The total number of allocations made during tracing
  46. size_t total_frees; ///< The total number of frees made during tracing
  47. size_t count; ///< The number of records in the internal buffer
  48. size_t capacity; ///< The capacity of the internal buffer
  49. size_t high_water_mark; ///< The maximum value that 'count' got to
  50. size_t has_overflowed; ///< True if the internal buffer overflowed at some point
  51. #if CONFIG_HEAP_TRACE_HASH_MAP
  52. size_t total_hashmap_hits; ///< If hashmap is used, the total number of hits
  53. size_t total_hashmap_miss; ///< If hashmap is used, the total number of misses (possibly due to overflow)
  54. #endif
  55. } heap_trace_summary_t;
  56. /**
  57. * @brief Initialise heap tracing in standalone mode.
  58. *
  59. * This function must be called before any other heap tracing functions.
  60. *
  61. * To disable heap tracing and allow the buffer to be freed, stop tracing and then call heap_trace_init_standalone(NULL, 0);
  62. *
  63. * @param record_buffer Provide a buffer to use for heap trace data.
  64. * Note: External RAM is allowed, but it prevents recording allocations made from ISR's.
  65. * @param num_records Size of the heap trace buffer, as number of record structures.
  66. * @return
  67. * - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
  68. * - ESP_ERR_INVALID_STATE Heap tracing is currently in progress.
  69. * - ESP_OK Heap tracing initialised successfully.
  70. */
  71. esp_err_t heap_trace_init_standalone(heap_trace_record_t *record_buffer, size_t num_records);
  72. /**
  73. * @brief Initialise heap tracing in host-based mode.
  74. *
  75. * This function must be called before any other heap tracing functions.
  76. *
  77. * @return
  78. * - ESP_ERR_INVALID_STATE Heap tracing is currently in progress.
  79. * - ESP_OK Heap tracing initialised successfully.
  80. */
  81. esp_err_t heap_trace_init_tohost(void);
  82. /**
  83. * @brief Start heap tracing. All heap allocations & frees will be traced, until heap_trace_stop() is called.
  84. *
  85. * @note heap_trace_init_standalone() must be called to provide a valid buffer, before this function is called.
  86. *
  87. * @note Calling this function while heap tracing is running will reset the heap trace state and continue tracing.
  88. *
  89. * @param mode Mode for tracing.
  90. * - HEAP_TRACE_ALL means all heap allocations and frees are traced.
  91. * - HEAP_TRACE_LEAKS means only suspected memory leaks are traced. (When memory is freed, the record is removed from the trace buffer.)
  92. * @return
  93. * - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
  94. * - ESP_ERR_INVALID_STATE A non-zero-length buffer has not been set via heap_trace_init_standalone().
  95. * - ESP_OK Tracing is started.
  96. */
  97. esp_err_t heap_trace_start(heap_trace_mode_t mode);
  98. /**
  99. * @brief Stop heap tracing.
  100. *
  101. * @return
  102. * - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
  103. * - ESP_ERR_INVALID_STATE Heap tracing was not in progress.
  104. * - ESP_OK Heap tracing stopped..
  105. */
  106. esp_err_t heap_trace_stop(void);
  107. /**
  108. * @brief Resume heap tracing which was previously stopped.
  109. *
  110. * Unlike heap_trace_start(), this function does not clear the
  111. * buffer of any pre-existing trace records.
  112. *
  113. * The heap trace mode is the same as when heap_trace_start() was
  114. * last called (or HEAP_TRACE_ALL if heap_trace_start() was never called).
  115. *
  116. * @return
  117. * - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
  118. * - ESP_ERR_INVALID_STATE Heap tracing was already started.
  119. * - ESP_OK Heap tracing resumed.
  120. */
  121. esp_err_t heap_trace_resume(void);
  122. /**
  123. * @brief Return number of records in the heap trace buffer
  124. *
  125. * It is safe to call this function while heap tracing is running.
  126. */
  127. size_t heap_trace_get_count(void);
  128. /**
  129. * @brief Return a raw record from the heap trace buffer
  130. *
  131. * @note It is safe to call this function while heap tracing is
  132. * running, however in HEAP_TRACE_LEAK mode record indexing may
  133. * skip entries unless heap tracing is stopped first.
  134. *
  135. * @param index Index (zero-based) of the record to return.
  136. * @param[out] record Record where the heap trace record will be copied.
  137. * @return
  138. * - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
  139. * - ESP_ERR_INVALID_STATE Heap tracing was not initialised.
  140. * - ESP_ERR_INVALID_ARG Index is out of bounds for current heap trace record count.
  141. * - ESP_OK Record returned successfully.
  142. */
  143. esp_err_t heap_trace_get(size_t index, heap_trace_record_t *record);
  144. /**
  145. * @brief Dump heap trace record data to stdout
  146. *
  147. * @note It is safe to call this function while heap tracing is
  148. * running, however in HEAP_TRACE_LEAK mode the dump may skip
  149. * entries unless heap tracing is stopped first.
  150. */
  151. void heap_trace_dump(void);
  152. /**
  153. * @brief Dump heap trace from the memory of the capabilities passed as parameter.
  154. *
  155. * @param caps Capability(ies) of the memory from which to dump the trace.
  156. * Set MALLOC_CAP_INTERNAL to dump heap trace data from internal memory.
  157. * Set MALLOC_CAP_SPIRAM to dump heap trace data from PSRAM.
  158. * Set both to dump both heap trace data.
  159. */
  160. void heap_trace_dump_caps(const uint32_t caps);
  161. /**
  162. * @brief Get summary information about the result of a heap trace
  163. *
  164. * @note It is safe to call this function while heap tracing is running.
  165. */
  166. esp_err_t heap_trace_summary(heap_trace_summary_t *summary);
  167. #ifdef __cplusplus
  168. }
  169. #endif