heap_trace.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 <sys/param.h>
  15. #include <sdkconfig.h>
  16. #define HEAP_TRACE_SRCFILE /* don't warn on inclusion here */
  17. #include "esp_heap_trace.h"
  18. #undef HEAP_TRACE_SRCFILE
  19. #include "esp_heap_caps.h"
  20. #include "esp_attr.h"
  21. #include "freertos/FreeRTOS.h"
  22. #include "freertos/task.h"
  23. #include "soc/soc_memory_layout.h"
  24. #include "heap_private.h"
  25. #define STACK_DEPTH CONFIG_HEAP_TRACING_STACK_DEPTH
  26. static portMUX_TYPE trace_mux = portMUX_INITIALIZER_UNLOCKED;
  27. static bool tracing;
  28. static heap_trace_mode_t mode;
  29. /* Buffer used for records, starting at offset 0
  30. */
  31. static heap_trace_record_t *buffer;
  32. static size_t total_records;
  33. /* Count of entries logged in the buffer.
  34. Maximum total_records
  35. */
  36. static size_t count;
  37. /* Actual number of allocations logged */
  38. static size_t total_allocations;
  39. /* Actual number of frees logged */
  40. static size_t total_frees;
  41. /* Has the buffer overflowed and lost trace entries? */
  42. static bool has_overflowed = false;
  43. esp_err_t heap_trace_init_standalone(heap_trace_record_t *record_buffer, size_t num_records)
  44. {
  45. #ifndef CONFIG_HEAP_TRACING
  46. return ESP_ERR_NOT_SUPPORTED;
  47. #endif
  48. if (tracing) {
  49. return ESP_ERR_INVALID_STATE;
  50. }
  51. buffer = record_buffer;
  52. total_records = num_records;
  53. memset(buffer, 0, num_records * sizeof(heap_trace_record_t));
  54. return ESP_OK;
  55. }
  56. esp_err_t heap_trace_start(heap_trace_mode_t mode_param)
  57. {
  58. #ifndef CONFIG_HEAP_TRACING
  59. return ESP_ERR_NOT_SUPPORTED;
  60. #endif
  61. if (buffer == NULL || total_records == 0) {
  62. return ESP_ERR_INVALID_STATE;
  63. }
  64. portENTER_CRITICAL(&trace_mux);
  65. tracing = false;
  66. mode = mode_param;
  67. count = 0;
  68. total_allocations = 0;
  69. total_frees = 0;
  70. has_overflowed = false;
  71. heap_trace_resume();
  72. portEXIT_CRITICAL(&trace_mux);
  73. return ESP_OK;
  74. }
  75. static esp_err_t set_tracing(bool enable)
  76. {
  77. #ifndef CONFIG_HEAP_TRACING
  78. return ESP_ERR_NOT_SUPPORTED;
  79. #endif
  80. if (tracing == enable) {
  81. return ESP_ERR_INVALID_STATE;
  82. }
  83. tracing = enable;
  84. return ESP_OK;
  85. }
  86. esp_err_t heap_trace_stop(void)
  87. {
  88. return set_tracing(false);
  89. }
  90. esp_err_t heap_trace_resume(void)
  91. {
  92. return set_tracing(true);
  93. }
  94. size_t heap_trace_get_count(void)
  95. {
  96. return count;
  97. }
  98. esp_err_t heap_trace_get(size_t index, heap_trace_record_t *record)
  99. {
  100. #ifndef CONFIG_HEAP_TRACING
  101. return ESP_ERR_NOT_SUPPORTED;
  102. #endif
  103. if (record == NULL) {
  104. return ESP_ERR_INVALID_STATE;
  105. }
  106. esp_err_t result = ESP_OK;
  107. portENTER_CRITICAL(&trace_mux);
  108. if (index >= count) {
  109. result = ESP_ERR_INVALID_ARG; /* out of range for 'count' */
  110. } else {
  111. memcpy(record, &buffer[index], sizeof(heap_trace_record_t));
  112. }
  113. portEXIT_CRITICAL(&trace_mux);
  114. return result;
  115. }
  116. void heap_trace_dump(void)
  117. {
  118. #ifndef CONFIG_HEAP_TRACING
  119. printf("no data, heap tracing is disabled.\n");
  120. return;
  121. #endif
  122. size_t delta_size = 0;
  123. size_t delta_allocs = 0;
  124. printf("%u allocations trace (%u entry buffer)\n",
  125. count, total_records);
  126. size_t start_count = count;
  127. for (int i = 0; i < count; i++) {
  128. heap_trace_record_t *rec = &buffer[i];
  129. if (rec->address != NULL) {
  130. printf("%d bytes (@ %p) allocated CPU %d ccount 0x%08x caller ",
  131. rec->size, rec->address, rec->ccount & 1, rec->ccount & ~3);
  132. for (int j = 0; j < STACK_DEPTH && rec->alloced_by[j] != 0; j++) {
  133. printf("%p%s", rec->alloced_by[j],
  134. (j < STACK_DEPTH - 1) ? ":" : "");
  135. }
  136. if (mode != HEAP_TRACE_ALL || STACK_DEPTH == 0 || rec->freed_by[0] == NULL) {
  137. delta_size += rec->size;
  138. delta_allocs++;
  139. printf("\n");
  140. } else {
  141. printf("\nfreed by ");
  142. for (int j = 0; j < STACK_DEPTH; j++) {
  143. printf("%p%s", rec->freed_by[j],
  144. (j < STACK_DEPTH - 1) ? ":" : "\n");
  145. }
  146. }
  147. }
  148. }
  149. if (mode == HEAP_TRACE_ALL) {
  150. printf("%u bytes alive in trace (%u/%u allocations)\n",
  151. delta_size, delta_allocs, heap_trace_get_count());
  152. } else {
  153. printf("%u bytes 'leaked' in trace (%u allocations)\n", delta_size, delta_allocs);
  154. }
  155. printf("total allocations %u total frees %u\n", total_allocations, total_frees);
  156. if (start_count != count) { // only a problem if trace isn't stopped before dumping
  157. printf("(NB: New entries were traced while dumping, so trace dump may have duplicate entries.)\n");
  158. }
  159. if (has_overflowed) {
  160. printf("(NB: Buffer has overflowed, so trace data is incomplete.)\n");
  161. }
  162. }
  163. /* Add a new allocation to the heap trace records */
  164. static IRAM_ATTR void record_allocation(const heap_trace_record_t *record)
  165. {
  166. portENTER_CRITICAL(&trace_mux);
  167. if (tracing) {
  168. if (count == total_records) {
  169. has_overflowed = true;
  170. /* Move the whole buffer back one slot.
  171. This is a bit slow, compared to treating this buffer as a ringbuffer and rotating a head pointer.
  172. However, ringbuffer code gets tricky when we remove elements in mid-buffer (for leak trace mode) while
  173. trying to keep track of an item count that may overflow.
  174. */
  175. memmove(&buffer[0], &buffer[1], sizeof(heap_trace_record_t) * (total_records -1));
  176. count--;
  177. }
  178. // Copy new record into place
  179. memcpy(&buffer[count], record, sizeof(heap_trace_record_t));
  180. count++;
  181. total_allocations++;
  182. }
  183. portEXIT_CRITICAL(&trace_mux);
  184. }
  185. // remove a record, used when freeing
  186. static void remove_record(int index);
  187. /* record a free event in the heap trace log
  188. For HEAP_TRACE_ALL, this means filling in the freed_by pointer.
  189. For HEAP_TRACE_LEAKS, this means removing the record from the log.
  190. */
  191. static IRAM_ATTR void record_free(void *p, void **callers)
  192. {
  193. portENTER_CRITICAL(&trace_mux);
  194. if (tracing && count > 0) {
  195. total_frees++;
  196. /* search backwards for the allocation record matching this free */
  197. int i;
  198. for (i = count - 1; i >= 0; i--) {
  199. if (buffer[i].address == p) {
  200. break;
  201. }
  202. }
  203. if (i >= 0) {
  204. if (mode == HEAP_TRACE_ALL) {
  205. memcpy(buffer[i].freed_by, callers, sizeof(void *) * STACK_DEPTH);
  206. } else { // HEAP_TRACE_LEAKS
  207. // Leak trace mode, once an allocation is freed we remove it from the list
  208. remove_record(i);
  209. }
  210. }
  211. }
  212. portEXIT_CRITICAL(&trace_mux);
  213. }
  214. /* remove the entry at 'index' from the ringbuffer of saved records */
  215. static IRAM_ATTR void remove_record(int index)
  216. {
  217. if (index < count - 1) {
  218. // Remove the buffer entry from the list
  219. memmove(&buffer[index], &buffer[index+1],
  220. sizeof(heap_trace_record_t) * (total_records - index - 1));
  221. } else {
  222. // For last element, just zero it out to avoid ambiguity
  223. memset(&buffer[index], 0, sizeof(heap_trace_record_t));
  224. }
  225. count--;
  226. }
  227. /* Encode the CPU ID in the LSB of the ccount value */
  228. inline static uint32_t get_ccount(void)
  229. {
  230. uint32_t ccount = xthal_get_ccount() & ~3;
  231. #ifndef CONFIG_FREERTOS_UNICORE
  232. ccount |= xPortGetCoreID();
  233. #endif
  234. return ccount;
  235. }
  236. // Caller is 2 stack frames deeper than we care about
  237. #define STACK_OFFSET 2
  238. #define TEST_STACK(N) do { \
  239. if (STACK_DEPTH == N) { \
  240. return; \
  241. } \
  242. callers[N] = __builtin_return_address(N+STACK_OFFSET); \
  243. if (!esp_ptr_executable(callers[N])) { \
  244. return; \
  245. } \
  246. } while(0);
  247. /* Static function to read the call stack for a traced heap call.
  248. Calls to __builtin_return_address are "unrolled" via TEST_STACK macro as gcc requires the
  249. argument to be a compile-time constant.
  250. */
  251. static IRAM_ATTR __attribute__((noinline)) void get_call_stack(void **callers)
  252. {
  253. bzero(callers, sizeof(void *) * STACK_DEPTH);
  254. TEST_STACK(0);
  255. TEST_STACK(1);
  256. TEST_STACK(2);
  257. TEST_STACK(3);
  258. TEST_STACK(4);
  259. TEST_STACK(5);
  260. TEST_STACK(6);
  261. TEST_STACK(7);
  262. TEST_STACK(8);
  263. TEST_STACK(9);
  264. }
  265. _Static_assert(STACK_DEPTH >= 0 && STACK_DEPTH <= 10, "CONFIG_HEAP_TRACING_STACK_DEPTH must be in range 0-10");
  266. typedef enum {
  267. TRACE_MALLOC_CAPS,
  268. TRACE_MALLOC_DEFAULT
  269. } trace_malloc_mode_t;
  270. void *__real_heap_caps_malloc(size_t size, uint32_t caps);
  271. void *__real_heap_caps_malloc_default( size_t size );
  272. void *__real_heap_caps_realloc_default( void *ptr, size_t size );
  273. /* trace any 'malloc' event */
  274. static IRAM_ATTR __attribute__((noinline)) void *trace_malloc(size_t size, uint32_t caps, trace_malloc_mode_t mode)
  275. {
  276. uint32_t ccount = get_ccount();
  277. void *p;
  278. if ( mode == TRACE_MALLOC_CAPS ) {
  279. p = __real_heap_caps_malloc(size, caps);
  280. } else { //TRACE_MALLOC_DEFAULT
  281. p = __real_heap_caps_malloc_default(size);
  282. }
  283. if (tracing && p != NULL) {
  284. heap_trace_record_t rec = {
  285. .address = p,
  286. .ccount = ccount,
  287. .size = size,
  288. };
  289. get_call_stack(rec.alloced_by);
  290. record_allocation(&rec);
  291. }
  292. return p;
  293. }
  294. void __real_heap_caps_free(void *p);
  295. /* trace any 'free' event */
  296. static IRAM_ATTR __attribute__((noinline)) void trace_free(void *p)
  297. {
  298. if (tracing && p != NULL) {
  299. void *callers[STACK_DEPTH];
  300. get_call_stack(callers);
  301. record_free(p, callers);
  302. }
  303. __real_heap_caps_free(p);
  304. }
  305. void * __real_heap_caps_realloc(void *p, size_t size, uint32_t caps);
  306. /* trace any 'realloc' event */
  307. static IRAM_ATTR __attribute__((noinline)) void *trace_realloc(void *p, size_t size, uint32_t caps, trace_malloc_mode_t mode)
  308. {
  309. void *callers[STACK_DEPTH];
  310. uint32_t ccount = get_ccount();
  311. if (tracing && p != NULL && size == 0) {
  312. get_call_stack(callers);
  313. record_free(p, callers);
  314. }
  315. void *r;
  316. if (mode == TRACE_MALLOC_CAPS ) {
  317. r = __real_heap_caps_realloc(p, size, caps);
  318. } else { //TRACE_MALLOC_DEFAULT
  319. r = __real_heap_caps_realloc_default(p, size);
  320. }
  321. if (tracing && r != NULL) {
  322. get_call_stack(callers);
  323. if (p != NULL) {
  324. /* trace realloc as free-then-alloc */
  325. record_free(p, callers);
  326. }
  327. heap_trace_record_t rec = {
  328. .address = r,
  329. .ccount = ccount,
  330. .size = size,
  331. };
  332. memcpy(rec.alloced_by, callers, sizeof(void *) * STACK_DEPTH);
  333. record_allocation(&rec);
  334. }
  335. return r;
  336. }
  337. /* Note: this changes the behaviour of libc malloc/realloc/free a bit,
  338. as they no longer go via the libc functions in ROM. But more or less
  339. the same in the end. */
  340. IRAM_ATTR void *__wrap_malloc(size_t size)
  341. {
  342. return trace_malloc(size, 0, TRACE_MALLOC_DEFAULT);
  343. }
  344. IRAM_ATTR void __wrap_free(void *p)
  345. {
  346. trace_free(p);
  347. }
  348. IRAM_ATTR void *__wrap_realloc(void *p, size_t size)
  349. {
  350. return trace_realloc(p, size, 0, TRACE_MALLOC_DEFAULT);
  351. }
  352. IRAM_ATTR void *__wrap_calloc(size_t nmemb, size_t size)
  353. {
  354. size = size * nmemb;
  355. void *result = trace_malloc(size, 0, TRACE_MALLOC_DEFAULT);
  356. if (result != NULL) {
  357. memset(result, 0, size);
  358. }
  359. return result;
  360. }
  361. IRAM_ATTR void *__wrap_heap_caps_malloc(size_t size, uint32_t caps)
  362. {
  363. return trace_malloc(size, caps, TRACE_MALLOC_CAPS);
  364. }
  365. void __wrap_heap_caps_free(void *p) __attribute__((alias("__wrap_free")));
  366. IRAM_ATTR void *__wrap_heap_caps_realloc(void *p, size_t size, uint32_t caps)
  367. {
  368. return trace_realloc(p, size, caps, TRACE_MALLOC_CAPS);
  369. }
  370. IRAM_ATTR void *__wrap_heap_caps_malloc_default( size_t size )
  371. {
  372. return trace_malloc(size, 0, TRACE_MALLOC_DEFAULT);
  373. }
  374. IRAM_ATTR void *__wrap_heap_caps_realloc_default( void *ptr, size_t size )
  375. {
  376. return trace_realloc(ptr, size, 0, TRACE_MALLOC_DEFAULT);
  377. }