esp_heap_trace.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #pragma once
  14. #include "sdkconfig.h"
  15. #include <stdint.h>
  16. #include <esp_err.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. #if !defined(CONFIG_HEAP_TRACING) && !defined(HEAP_TRACE_SRCFILE)
  21. #warning "esp_heap_trace.h is included but heap tracing is disabled in menuconfig, functions are no-ops"
  22. #endif
  23. #ifndef CONFIG_HEAP_TRACING_STACK_DEPTH
  24. #define CONFIG_HEAP_TRACING_STACK_DEPTH 0
  25. #endif
  26. typedef enum {
  27. HEAP_TRACE_ALL,
  28. HEAP_TRACE_LEAKS,
  29. } heap_trace_mode_t;
  30. /**
  31. * @brief Trace record data type. Stores information about an allocated region of memory.
  32. */
  33. typedef struct {
  34. uint32_t ccount; ///< CCOUNT of the CPU when the allocation was made. LSB (bit value 1) is the CPU number (0 or 1).
  35. void *address; ///< Address which was allocated
  36. size_t size; ///< Size of the allocation
  37. void *alloced_by[CONFIG_HEAP_TRACING_STACK_DEPTH]; ///< Call stack of the caller which allocated the memory.
  38. void *freed_by[CONFIG_HEAP_TRACING_STACK_DEPTH]; ///< Call stack of the caller which freed the memory (all zero if not freed.)
  39. } heap_trace_record_t;
  40. /**
  41. * @brief Initialise heap tracing in standalone mode.
  42. *
  43. * This function must be called before any other heap tracing functions.
  44. *
  45. * To disable heap tracing and allow the buffer to be freed, stop tracing and then call heap_trace_init_standalone(NULL, 0);
  46. *
  47. * @param record_buffer Provide a buffer to use for heap trace data. Must remain valid any time heap tracing is enabled, meaning
  48. * it must be allocated from internal memory not in PSRAM.
  49. * @param num_records Size of the heap trace buffer, as number of record structures.
  50. * @return
  51. * - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
  52. * - ESP_ERR_INVALID_STATE Heap tracing is currently in progress.
  53. * - ESP_OK Heap tracing initialised successfully.
  54. */
  55. esp_err_t heap_trace_init_standalone(heap_trace_record_t *record_buffer, size_t num_records);
  56. /**
  57. * @brief Initialise heap tracing in host-based mode.
  58. *
  59. * This function must be called before any other heap tracing functions.
  60. *
  61. * @return
  62. * - ESP_ERR_INVALID_STATE Heap tracing is currently in progress.
  63. * - ESP_OK Heap tracing initialised successfully.
  64. */
  65. esp_err_t heap_trace_init_tohost(void);
  66. /**
  67. * @brief Start heap tracing. All heap allocations & frees will be traced, until heap_trace_stop() is called.
  68. *
  69. * @note heap_trace_init_standalone() must be called to provide a valid buffer, before this function is called.
  70. *
  71. * @note Calling this function while heap tracing is running will reset the heap trace state and continue tracing.
  72. *
  73. * @param mode Mode for tracing.
  74. * - HEAP_TRACE_ALL means all heap allocations and frees are traced.
  75. * - HEAP_TRACE_LEAKS means only suspected memory leaks are traced. (When memory is freed, the record is removed from the trace buffer.)
  76. * @return
  77. * - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
  78. * - ESP_ERR_INVALID_STATE A non-zero-length buffer has not been set via heap_trace_init_standalone().
  79. * - ESP_OK Tracing is started.
  80. */
  81. esp_err_t heap_trace_start(heap_trace_mode_t mode);
  82. /**
  83. * @brief Stop heap tracing.
  84. *
  85. * @return
  86. * - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
  87. * - ESP_ERR_INVALID_STATE Heap tracing was not in progress.
  88. * - ESP_OK Heap tracing stopped..
  89. */
  90. esp_err_t heap_trace_stop(void);
  91. /**
  92. * @brief Resume heap tracing which was previously stopped.
  93. *
  94. * Unlike heap_trace_start(), this function does not clear the
  95. * buffer of any pre-existing trace records.
  96. *
  97. * The heap trace mode is the same as when heap_trace_start() was
  98. * last called (or HEAP_TRACE_ALL if heap_trace_start() was never called).
  99. *
  100. * @return
  101. * - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
  102. * - ESP_ERR_INVALID_STATE Heap tracing was already started.
  103. * - ESP_OK Heap tracing resumed.
  104. */
  105. esp_err_t heap_trace_resume(void);
  106. /**
  107. * @brief Return number of records in the heap trace buffer
  108. *
  109. * It is safe to call this function while heap tracing is running.
  110. */
  111. size_t heap_trace_get_count(void);
  112. /**
  113. * @brief Return a raw record from the heap trace buffer
  114. *
  115. * @note It is safe to call this function while heap tracing is running, however in HEAP_TRACE_LEAK mode record indexing may
  116. * skip entries unless heap tracing is stopped first.
  117. *
  118. * @param index Index (zero-based) of the record to return.
  119. * @param[out] record Record where the heap trace record will be copied.
  120. * @return
  121. * - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
  122. * - ESP_ERR_INVALID_STATE Heap tracing was not initialised.
  123. * - ESP_ERR_INVALID_ARG Index is out of bounds for current heap trace record count.
  124. * - ESP_OK Record returned successfully.
  125. */
  126. esp_err_t heap_trace_get(size_t index, heap_trace_record_t *record);
  127. /**
  128. * @brief Dump heap trace record data to stdout
  129. *
  130. * @note It is safe to call this function while heap tracing is running, however in HEAP_TRACE_LEAK mode the dump may skip
  131. * entries unless heap tracing is stopped first.
  132. *
  133. *
  134. */
  135. void heap_trace_dump(void);
  136. #ifdef __cplusplus
  137. }
  138. #endif