esp_heap_trace.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * @note Standalone mode is the only mode currently supported.
  43. *
  44. * This function must be called before any other heap tracing functions.
  45. *
  46. * To disable heap tracing and allow the buffer to be freed, stop tracing and then call heap_trace_init_standalone(NULL, 0);
  47. *
  48. * @param record_buffer Provide a buffer to use for heap trace data. Must remain valid any time heap tracing is enabled, meaning
  49. * it must be allocated from internal memory not in PSRAM.
  50. * @param num_records Size of the heap trace buffer, as number of record structures.
  51. * @return
  52. * - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
  53. * - ESP_ERR_INVALID_STATE Heap tracing is currently in progress.
  54. * - ESP_OK Heap tracing initialised successfully.
  55. */
  56. esp_err_t heap_trace_init_standalone(heap_trace_record_t *record_buffer, size_t num_records);
  57. /**
  58. * @brief Start heap tracing. All heap allocations & frees will be traced, until heap_trace_stop() is called.
  59. *
  60. * @note heap_trace_init_standalone() must be called to provide a valid buffer, before this function is called.
  61. *
  62. * @note Calling this function while heap tracing is running will reset the heap trace state and continue tracing.
  63. *
  64. * @param mode Mode for tracing.
  65. * - HEAP_TRACE_ALL means all heap allocations and frees are traced.
  66. * - HEAP_TRACE_LEAKS means only suspected memory leaks are traced. (When memory is freed, the record is removed from the trace buffer.)
  67. * @return
  68. * - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
  69. * - ESP_ERR_INVALID_STATE A non-zero-length buffer has not been set via heap_trace_init_standalone().
  70. * - ESP_OK Tracing is started.
  71. */
  72. esp_err_t heap_trace_start(heap_trace_mode_t mode);
  73. /**
  74. * @brief Stop heap tracing.
  75. *
  76. * @return
  77. * - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
  78. * - ESP_ERR_INVALID_STATE Heap tracing was not in progress.
  79. * - ESP_OK Heap tracing stopped..
  80. */
  81. esp_err_t heap_trace_stop(void);
  82. /**
  83. * @brief Resume heap tracing which was previously stopped.
  84. *
  85. * Unlike heap_trace_start(), this function does not clear the
  86. * buffer of any pre-existing trace records.
  87. *
  88. * The heap trace mode is the same as when heap_trace_start() was
  89. * last called (or HEAP_TRACE_ALL if heap_trace_start() was never called).
  90. *
  91. * @return
  92. * - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
  93. * - ESP_ERR_INVALID_STATE Heap tracing was already started.
  94. * - ESP_OK Heap tracing resumed.
  95. */
  96. esp_err_t heap_trace_resume(void);
  97. /**
  98. * @brief Return number of records in the heap trace buffer
  99. *
  100. * It is safe to call this function while heap tracing is running.
  101. */
  102. size_t heap_trace_get_count(void);
  103. /**
  104. * @brief Return a raw record from the heap trace buffer
  105. *
  106. * @note It is safe to call this function while heap tracing is running, however in HEAP_TRACE_LEAK mode record indexing may
  107. * skip entries unless heap tracing is stopped first.
  108. *
  109. * @param index Index (zero-based) of the record to return.
  110. * @param[out] record Record where the heap trace record will be copied.
  111. * @return
  112. * - ESP_ERR_NOT_SUPPORTED Project was compiled without heap tracing enabled in menuconfig.
  113. * - ESP_ERR_INVALID_STATE Heap tracing was not initialised.
  114. * - ESP_ERR_INVALID_ARG Index is out of bounds for current heap trace record count.
  115. * - ESP_OK Record returned successfully.
  116. */
  117. esp_err_t heap_trace_get(size_t index, heap_trace_record_t *record);
  118. /**
  119. * @brief Dump heap trace record data to stdout
  120. *
  121. * @note It is safe to call this function while heap tracing is running, however in HEAP_TRACE_LEAK mode the dump may skip
  122. * entries unless heap tracing is stopped first.
  123. *
  124. *
  125. */
  126. void heap_trace_dump(void);
  127. #ifdef __cplusplus
  128. }
  129. #endif