heap_trace.inc 5.8 KB

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