heap_trace.inc 5.9 KB

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