heap_trace.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. #include "soc/soc_memory_layout.h"
  16. #include "esp_attr.h"
  17. /* Encode the CPU ID in the LSB of the ccount value */
  18. inline static uint32_t get_ccount(void)
  19. {
  20. uint32_t ccount = xthal_get_ccount() & ~3;
  21. #ifndef CONFIG_FREERTOS_UNICORE
  22. ccount |= xPortGetCoreID();
  23. #endif
  24. return ccount;
  25. }
  26. // Caller is 2 stack frames deeper than we care about
  27. #define STACK_OFFSET 2
  28. #define TEST_STACK(N) do { \
  29. if (STACK_DEPTH == N) { \
  30. return; \
  31. } \
  32. callers[N] = __builtin_return_address(N+STACK_OFFSET); \
  33. if (!esp_ptr_executable(callers[N])) { \
  34. return; \
  35. } \
  36. } while(0)
  37. /* Static function to read the call stack for a traced heap call.
  38. Calls to __builtin_return_address are "unrolled" via TEST_STACK macro as gcc requires the
  39. argument to be a compile-time constant.
  40. */
  41. static IRAM_ATTR __attribute__((noinline)) void get_call_stack(void **callers)
  42. {
  43. bzero(callers, sizeof(void *) * STACK_DEPTH);
  44. TEST_STACK(0);
  45. TEST_STACK(1);
  46. TEST_STACK(2);
  47. TEST_STACK(3);
  48. TEST_STACK(4);
  49. TEST_STACK(5);
  50. TEST_STACK(6);
  51. TEST_STACK(7);
  52. TEST_STACK(8);
  53. TEST_STACK(9);
  54. }
  55. _Static_assert(STACK_DEPTH >= 0 && STACK_DEPTH <= 10, "CONFIG_HEAP_TRACING_STACK_DEPTH must be in range 0-10");
  56. typedef enum {
  57. TRACE_MALLOC_CAPS,
  58. TRACE_MALLOC_DEFAULT
  59. } trace_malloc_mode_t;
  60. void *__real_heap_caps_malloc(size_t size, uint32_t caps);
  61. void *__real_heap_caps_malloc_default( size_t size );
  62. void *__real_heap_caps_realloc_default( void *ptr, size_t size );
  63. /* trace any 'malloc' event */
  64. static IRAM_ATTR __attribute__((noinline)) void *trace_malloc(size_t size, uint32_t caps, trace_malloc_mode_t mode)
  65. {
  66. uint32_t ccount = get_ccount();
  67. void *p;
  68. if ( mode == TRACE_MALLOC_CAPS ) {
  69. p = __real_heap_caps_malloc(size, caps);
  70. } else { //TRACE_MALLOC_DEFAULT
  71. p = __real_heap_caps_malloc_default(size);
  72. }
  73. heap_trace_record_t rec = {
  74. .address = p,
  75. .ccount = ccount,
  76. .size = size,
  77. };
  78. get_call_stack(rec.alloced_by);
  79. record_allocation(&rec);
  80. return p;
  81. }
  82. void __real_heap_caps_free(void *p);
  83. /* trace any 'free' event */
  84. static IRAM_ATTR __attribute__((noinline)) void trace_free(void *p)
  85. {
  86. void *callers[STACK_DEPTH];
  87. get_call_stack(callers);
  88. record_free(p, callers);
  89. __real_heap_caps_free(p);
  90. }
  91. void * __real_heap_caps_realloc(void *p, size_t size, uint32_t caps);
  92. /* trace any 'realloc' event */
  93. static IRAM_ATTR __attribute__((noinline)) void *trace_realloc(void *p, size_t size, uint32_t caps, trace_malloc_mode_t mode)
  94. {
  95. void *callers[STACK_DEPTH];
  96. uint32_t ccount = get_ccount();
  97. void *r;
  98. /* trace realloc as free-then-alloc */
  99. get_call_stack(callers);
  100. record_free(p, callers);
  101. if (mode == TRACE_MALLOC_CAPS ) {
  102. r = __real_heap_caps_realloc(p, size, caps);
  103. } else { //TRACE_MALLOC_DEFAULT
  104. r = __real_heap_caps_realloc_default(p, size);
  105. }
  106. /* realloc with zero size is a free */
  107. if (size != 0) {
  108. heap_trace_record_t rec = {
  109. .address = r,
  110. .ccount = ccount,
  111. .size = size,
  112. };
  113. memcpy(rec.alloced_by, callers, sizeof(void *) * STACK_DEPTH);
  114. record_allocation(&rec);
  115. }
  116. return r;
  117. }
  118. /* Note: this changes the behaviour of libc malloc/realloc/free a bit,
  119. as they no longer go via the libc functions in ROM. But more or less
  120. the same in the end. */
  121. IRAM_ATTR void *__wrap_malloc(size_t size)
  122. {
  123. return trace_malloc(size, 0, TRACE_MALLOC_DEFAULT);
  124. }
  125. IRAM_ATTR void __wrap_free(void *p)
  126. {
  127. trace_free(p);
  128. }
  129. IRAM_ATTR void *__wrap_realloc(void *p, size_t size)
  130. {
  131. return trace_realloc(p, size, 0, TRACE_MALLOC_DEFAULT);
  132. }
  133. IRAM_ATTR void *__wrap_calloc(size_t nmemb, size_t size)
  134. {
  135. size = size * nmemb;
  136. void *result = trace_malloc(size, 0, TRACE_MALLOC_DEFAULT);
  137. if (result != NULL) {
  138. memset(result, 0, size);
  139. }
  140. return result;
  141. }
  142. IRAM_ATTR void *__wrap_heap_caps_malloc(size_t size, uint32_t caps)
  143. {
  144. return trace_malloc(size, caps, TRACE_MALLOC_CAPS);
  145. }
  146. void __wrap_heap_caps_free(void *p) __attribute__((alias("__wrap_free")));
  147. IRAM_ATTR void *__wrap_heap_caps_realloc(void *p, size_t size, uint32_t caps)
  148. {
  149. return trace_realloc(p, size, caps, TRACE_MALLOC_CAPS);
  150. }
  151. IRAM_ATTR void *__wrap_heap_caps_malloc_default( size_t size )
  152. {
  153. return trace_malloc(size, 0, TRACE_MALLOC_DEFAULT);
  154. }
  155. IRAM_ATTR void *__wrap_heap_caps_realloc_default( void *ptr, size_t size )
  156. {
  157. return trace_realloc(ptr, size, 0, TRACE_MALLOC_DEFAULT);
  158. }