1 // Copyright 2018 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
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include <sdkconfig.h>
15
16 #define HEAP_TRACE_SRCFILE /* don't warn on inclusion here */
17 #include "esp_heap_trace.h"
18 #undef HEAP_TRACE_SRCFILE
19
20 #if CONFIG_SYSVIEW_ENABLE
21 #include "esp_app_trace.h"
22 #include "esp_sysview_trace.h"
23 #endif
24
25 #define STACK_DEPTH CONFIG_HEAP_TRACING_STACK_DEPTH
26
27 #ifdef CONFIG_HEAP_TRACING_TOHOST
28
29 #if !CONFIG_SYSVIEW_ENABLE
30 #error None of the heap tracing backends is enabled! You must enable SystemView compatible tracing to use this feature.
31 #endif
32
33 static bool s_tracing;
34
heap_trace_init_tohost(void)35 esp_err_t heap_trace_init_tohost(void)
36 {
37 if (s_tracing) {
38 return ESP_ERR_INVALID_STATE;
39 }
40 return ESP_OK;
41 }
42
heap_trace_start(heap_trace_mode_t mode_param)43 esp_err_t heap_trace_start(heap_trace_mode_t mode_param)
44 {
45 #if CONFIG_SYSVIEW_ENABLE
46 esp_err_t ret = esp_sysview_heap_trace_start((uint32_t)-1);
47 if (ret != ESP_OK) {
48 return ret;
49 }
50 #endif
51 s_tracing = true;
52 return ESP_OK;
53 }
54
heap_trace_stop(void)55 esp_err_t heap_trace_stop(void)
56 {
57 esp_err_t ret = ESP_ERR_NOT_SUPPORTED;
58 #if CONFIG_SYSVIEW_ENABLE
59 ret = esp_sysview_heap_trace_stop();
60 #endif
61 s_tracing = false;
62 return ret;
63 }
64
heap_trace_resume(void)65 esp_err_t heap_trace_resume(void)
66 {
67 return heap_trace_start(HEAP_TRACE_ALL);
68 }
69
heap_trace_get_count(void)70 size_t heap_trace_get_count(void)
71 {
72 return 0;
73 }
74
heap_trace_get(size_t index,heap_trace_record_t * record)75 esp_err_t heap_trace_get(size_t index, heap_trace_record_t *record)
76 {
77 return ESP_ERR_NOT_SUPPORTED;
78 }
79
heap_trace_dump(void)80 void heap_trace_dump(void)
81 {
82 return;
83 }
84
85 /* Add a new allocation to the heap trace records */
record_allocation(const heap_trace_record_t * record)86 static IRAM_ATTR void record_allocation(const heap_trace_record_t *record)
87 {
88 if (!s_tracing) {
89 return;
90 }
91 #if CONFIG_SYSVIEW_ENABLE
92 esp_sysview_heap_trace_alloc(record->address, record->size, record->alloced_by);
93 #endif
94 }
95
96 /* record a free event in the heap trace log
97
98 For HEAP_TRACE_ALL, this means filling in the freed_by pointer.
99 For HEAP_TRACE_LEAKS, this means removing the record from the log.
100 */
record_free(void * p,void ** callers)101 static IRAM_ATTR void record_free(void *p, void **callers)
102 {
103 if (!s_tracing) {
104 return;
105 }
106 #if CONFIG_SYSVIEW_ENABLE
107 esp_sysview_heap_trace_free(p, callers);
108 #endif
109 }
110
111 #include "heap_trace.inc"
112
113 #endif /*CONFIG_HEAP_TRACING_TOHOST*/
114