heap_trace_standalone.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. #include <string.h>
  14. #include <sdkconfig.h>
  15. #define HEAP_TRACE_SRCFILE /* don't warn on inclusion here */
  16. #include "esp_heap_trace.h"
  17. #undef HEAP_TRACE_SRCFILE
  18. #include "esp_attr.h"
  19. #include "freertos/FreeRTOS.h"
  20. #include "freertos/task.h"
  21. #define STACK_DEPTH CONFIG_HEAP_TRACING_STACK_DEPTH
  22. #if CONFIG_HEAP_TRACING_STANDALONE
  23. static portMUX_TYPE trace_mux = portMUX_INITIALIZER_UNLOCKED;
  24. static bool tracing;
  25. static heap_trace_mode_t mode;
  26. /* Buffer used for records, starting at offset 0
  27. */
  28. static heap_trace_record_t *buffer;
  29. static size_t total_records;
  30. /* Count of entries logged in the buffer.
  31. Maximum total_records
  32. */
  33. static size_t count;
  34. /* Actual number of allocations logged */
  35. static size_t total_allocations;
  36. /* Actual number of frees logged */
  37. static size_t total_frees;
  38. /* Has the buffer overflowed and lost trace entries? */
  39. static bool has_overflowed = false;
  40. esp_err_t heap_trace_init_standalone(heap_trace_record_t *record_buffer, size_t num_records)
  41. {
  42. if (tracing) {
  43. return ESP_ERR_INVALID_STATE;
  44. }
  45. buffer = record_buffer;
  46. total_records = num_records;
  47. memset(buffer, 0, num_records * sizeof(heap_trace_record_t));
  48. return ESP_OK;
  49. }
  50. esp_err_t heap_trace_start(heap_trace_mode_t mode_param)
  51. {
  52. if (buffer == NULL || total_records == 0) {
  53. return ESP_ERR_INVALID_STATE;
  54. }
  55. portENTER_CRITICAL(&trace_mux);
  56. tracing = false;
  57. mode = mode_param;
  58. count = 0;
  59. total_allocations = 0;
  60. total_frees = 0;
  61. has_overflowed = false;
  62. heap_trace_resume();
  63. portEXIT_CRITICAL(&trace_mux);
  64. return ESP_OK;
  65. }
  66. static esp_err_t set_tracing(bool enable)
  67. {
  68. if (tracing == enable) {
  69. return ESP_ERR_INVALID_STATE;
  70. }
  71. tracing = enable;
  72. return ESP_OK;
  73. }
  74. esp_err_t heap_trace_stop(void)
  75. {
  76. return set_tracing(false);
  77. }
  78. esp_err_t heap_trace_resume(void)
  79. {
  80. return set_tracing(true);
  81. }
  82. size_t heap_trace_get_count(void)
  83. {
  84. return count;
  85. }
  86. esp_err_t heap_trace_get(size_t index, heap_trace_record_t *record)
  87. {
  88. if (record == NULL) {
  89. return ESP_ERR_INVALID_STATE;
  90. }
  91. esp_err_t result = ESP_OK;
  92. portENTER_CRITICAL(&trace_mux);
  93. if (index >= count) {
  94. result = ESP_ERR_INVALID_ARG; /* out of range for 'count' */
  95. } else {
  96. memcpy(record, &buffer[index], sizeof(heap_trace_record_t));
  97. }
  98. portEXIT_CRITICAL(&trace_mux);
  99. return result;
  100. }
  101. void heap_trace_dump(void)
  102. {
  103. size_t delta_size = 0;
  104. size_t delta_allocs = 0;
  105. printf("%u allocations trace (%u entry buffer)\n",
  106. count, total_records);
  107. size_t start_count = count;
  108. for (int i = 0; i < count; i++) {
  109. heap_trace_record_t *rec = &buffer[i];
  110. if (rec->address != NULL) {
  111. printf("%d bytes (@ %p) allocated CPU %d ccount 0x%08x caller ",
  112. rec->size, rec->address, rec->ccount & 1, rec->ccount & ~3);
  113. for (int j = 0; j < STACK_DEPTH && rec->alloced_by[j] != 0; j++) {
  114. printf("%p%s", rec->alloced_by[j],
  115. (j < STACK_DEPTH - 1) ? ":" : "");
  116. }
  117. if (mode != HEAP_TRACE_ALL || STACK_DEPTH == 0 || rec->freed_by[0] == NULL) {
  118. delta_size += rec->size;
  119. delta_allocs++;
  120. printf("\n");
  121. } else {
  122. printf("\nfreed by ");
  123. for (int j = 0; j < STACK_DEPTH; j++) {
  124. printf("%p%s", rec->freed_by[j],
  125. (j < STACK_DEPTH - 1) ? ":" : "\n");
  126. }
  127. }
  128. }
  129. }
  130. if (mode == HEAP_TRACE_ALL) {
  131. printf("%u bytes alive in trace (%u/%u allocations)\n",
  132. delta_size, delta_allocs, heap_trace_get_count());
  133. } else {
  134. printf("%u bytes 'leaked' in trace (%u allocations)\n", delta_size, delta_allocs);
  135. }
  136. printf("total allocations %u total frees %u\n", total_allocations, total_frees);
  137. if (start_count != count) { // only a problem if trace isn't stopped before dumping
  138. printf("(NB: New entries were traced while dumping, so trace dump may have duplicate entries.)\n");
  139. }
  140. if (has_overflowed) {
  141. printf("(NB: Buffer has overflowed, so trace data is incomplete.)\n");
  142. }
  143. }
  144. /* Add a new allocation to the heap trace records */
  145. static IRAM_ATTR void record_allocation(const heap_trace_record_t *record)
  146. {
  147. if (!tracing || record->address == NULL) {
  148. return;
  149. }
  150. portENTER_CRITICAL(&trace_mux);
  151. if (tracing) {
  152. if (count == total_records) {
  153. has_overflowed = true;
  154. /* Move the whole buffer back one slot.
  155. This is a bit slow, compared to treating this buffer as a ringbuffer and rotating a head pointer.
  156. However, ringbuffer code gets tricky when we remove elements in mid-buffer (for leak trace mode) while
  157. trying to keep track of an item count that may overflow.
  158. */
  159. memmove(&buffer[0], &buffer[1], sizeof(heap_trace_record_t) * (total_records -1));
  160. count--;
  161. }
  162. // Copy new record into place
  163. memcpy(&buffer[count], record, sizeof(heap_trace_record_t));
  164. count++;
  165. total_allocations++;
  166. }
  167. portEXIT_CRITICAL(&trace_mux);
  168. }
  169. // remove a record, used when freeing
  170. static void remove_record(int index);
  171. /* record a free event in the heap trace log
  172. For HEAP_TRACE_ALL, this means filling in the freed_by pointer.
  173. For HEAP_TRACE_LEAKS, this means removing the record from the log.
  174. */
  175. static IRAM_ATTR void record_free(void *p, void **callers)
  176. {
  177. if (!tracing || p == NULL) {
  178. return;
  179. }
  180. portENTER_CRITICAL(&trace_mux);
  181. if (tracing && count > 0) {
  182. total_frees++;
  183. /* search backwards for the allocation record matching this free */
  184. int i;
  185. for (i = count - 1; i >= 0; i--) {
  186. if (buffer[i].address == p) {
  187. break;
  188. }
  189. }
  190. if (i >= 0) {
  191. if (mode == HEAP_TRACE_ALL) {
  192. memcpy(buffer[i].freed_by, callers, sizeof(void *) * STACK_DEPTH);
  193. } else { // HEAP_TRACE_LEAKS
  194. // Leak trace mode, once an allocation is freed we remove it from the list
  195. remove_record(i);
  196. }
  197. }
  198. }
  199. portEXIT_CRITICAL(&trace_mux);
  200. }
  201. /* remove the entry at 'index' from the ringbuffer of saved records */
  202. static IRAM_ATTR void remove_record(int index)
  203. {
  204. if (index < count - 1) {
  205. // Remove the buffer entry from the list
  206. memmove(&buffer[index], &buffer[index+1],
  207. sizeof(heap_trace_record_t) * (total_records - index - 1));
  208. } else {
  209. // For last element, just zero it out to avoid ambiguity
  210. memset(&buffer[index], 0, sizeof(heap_trace_record_t));
  211. }
  212. count--;
  213. }
  214. #include "heap_trace.inc"
  215. #endif /*CONFIG_HEAP_TRACING_STANDALONE*/