1 // Copyright 2017 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 
15 // This module implements runtime file I/O API for GCOV.
16 
17 #include <string.h>
18 #include "esp_task_wdt.h"
19 #include "freertos/FreeRTOS.h"
20 #include "freertos/task.h"
21 #include "freertos/semphr.h"
22 #include "soc/cpu.h"
23 #include "soc/timer_periph.h"
24 #include "esp_app_trace.h"
25 #include "esp_private/dbg_stubs.h"
26 #include "hal/wdt_hal.h"
27 #if CONFIG_IDF_TARGET_ESP32
28 #include "esp32/rom/libc_stubs.h"
29 #elif CONFIG_IDF_TARGET_ESP32S2
30 #include "esp32s2/rom/libc_stubs.h"
31 #endif
32 
33 #if CONFIG_APPTRACE_GCOV_ENABLE
34 
35 #define ESP_GCOV_DOWN_BUF_SIZE  4200
36 
37 #define LOG_LOCAL_LEVEL CONFIG_LOG_DEFAULT_LEVEL
38 #include "esp_log.h"
39 const static char *TAG = "esp_gcov_rtio";
40 
41 extern void __gcov_dump(void);
42 extern void __gcov_reset(void);
43 
44 static struct syscall_stub_table s_gcov_stub_table;
45 
46 
gcov_stub_lock_try_acquire_recursive(_lock_t * lock)47 static int gcov_stub_lock_try_acquire_recursive(_lock_t *lock)
48 {
49     if (*lock && uxSemaphoreGetCount((xSemaphoreHandle)(*lock)) == 0) {
50         // we can do nothing here, gcov dump is initiated with some resource locked
51         // which is also used by gcov functions
52         ESP_EARLY_LOGE(TAG, "Lock 0x%x is busy during GCOV dump! System state can be inconsistent after dump!", lock);
53     }
54     return pdTRUE;
55 }
56 
gcov_stub_lock_acquire_recursive(_lock_t * lock)57 static void gcov_stub_lock_acquire_recursive(_lock_t *lock)
58 {
59     gcov_stub_lock_try_acquire_recursive(lock);
60 }
61 
gcov_stub_lock_release_recursive(_lock_t * lock)62 static void gcov_stub_lock_release_recursive(_lock_t *lock)
63 {
64 }
65 
esp_dbg_stub_gcov_dump_do(void)66 static int esp_dbg_stub_gcov_dump_do(void)
67 {
68     int ret = ESP_OK;
69     FILE* old_stderr = stderr;
70     FILE* old_stdout = stdout;
71     static struct syscall_stub_table *old_tables[portNUM_PROCESSORS];
72 
73     old_tables[0] = syscall_table_ptr_pro;
74 #if portNUM_PROCESSORS > 1
75     old_tables[1] = syscall_table_ptr_app;
76 #endif
77     ESP_EARLY_LOGV(TAG, "Alloc apptrace down buf %d bytes", ESP_GCOV_DOWN_BUF_SIZE);
78     void *down_buf = malloc(ESP_GCOV_DOWN_BUF_SIZE);
79     if (down_buf == NULL) {
80         ESP_EARLY_LOGE(TAG, "Could not allocate memory for the buffer");
81         return ESP_ERR_NO_MEM;
82     }
83     ESP_EARLY_LOGV(TAG, "Config apptrace down buf");
84     esp_apptrace_down_buffer_config(down_buf, ESP_GCOV_DOWN_BUF_SIZE);
85     ESP_EARLY_LOGV(TAG, "Dump data...");
86     // incase of dual-core chip APP and PRO CPUs share the same table, so it is safe to save only PRO's table
87     memcpy(&s_gcov_stub_table, syscall_table_ptr_pro, sizeof(s_gcov_stub_table));
88     s_gcov_stub_table._lock_acquire_recursive = &gcov_stub_lock_acquire_recursive;
89     s_gcov_stub_table._lock_release_recursive = &gcov_stub_lock_release_recursive;
90     s_gcov_stub_table._lock_try_acquire_recursive = &gcov_stub_lock_try_acquire_recursive,
91     syscall_table_ptr_pro = &s_gcov_stub_table;
92 #if portNUM_PROCESSORS > 1
93     syscall_table_ptr_app = &s_gcov_stub_table;
94 #endif
95     stderr = (FILE*) &__sf_fake_stderr;
96     stdout = (FILE*) &__sf_fake_stdout;
97     __gcov_dump();
98     // reset dump status to allow incremental data accumulation
99     __gcov_reset();
100     stdout = old_stdout;
101     stderr = old_stderr;
102     syscall_table_ptr_pro = old_tables[0];
103 #if portNUM_PROCESSORS > 1
104     syscall_table_ptr_app = old_tables[1];
105 #endif
106     ESP_EARLY_LOGV(TAG, "Free apptrace down buf");
107     free(down_buf);
108     ESP_EARLY_LOGV(TAG, "Finish file transfer session");
109     ret = esp_apptrace_fstop(ESP_APPTRACE_DEST_TRAX);
110     if (ret != ESP_OK) {
111         ESP_EARLY_LOGE(TAG, "Failed to send files transfer stop cmd (%d)!", ret);
112     }
113     ESP_EARLY_LOGV(TAG, "exit %d", ret);
114     return ret;
115 }
116 
117 /**
118  * @brief Triggers gcov info dump.
119  *        This function is to be called by OpenOCD, not by normal user code.
120  * TODO: what about interrupted flash access (when cache disabled)???
121  *
122  * @return ESP_OK on success, otherwise see esp_err_t
123  */
esp_dbg_stub_gcov_entry(void)124 static int esp_dbg_stub_gcov_entry(void)
125 {
126     return esp_dbg_stub_gcov_dump_do();
127 }
128 
gcov_rtio_atexit(void (* function)(void))129 int gcov_rtio_atexit(void (*function)(void) __attribute__ ((unused)))
130 {
131     ESP_EARLY_LOGV(TAG, "%s", __FUNCTION__);
132     esp_dbg_stub_entry_set(ESP_DBG_STUB_ENTRY_GCOV, (uint32_t)&esp_dbg_stub_gcov_entry);
133     return 0;
134 }
135 
esp_gcov_dump(void)136 void esp_gcov_dump(void)
137 {
138     // disable IRQs on this CPU, other CPU is halted by OpenOCD
139     unsigned irq_state = portENTER_CRITICAL_NESTED();
140 #if !CONFIG_FREERTOS_UNICORE
141     int other_core = xPortGetCoreID() ? 0 : 1;
142     esp_cpu_stall(other_core);
143 #endif
144     while (!esp_apptrace_host_is_connected(ESP_APPTRACE_DEST_TRAX)) {
145         wdt_hal_context_t twdt = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0};
146         wdt_hal_context_t iwdt = {.inst = WDT_MWDT1, .mwdt_dev = &TIMERG1};
147         //Feed the Task Watchdog (TG0) to prevent it from timing out
148         wdt_hal_write_protect_disable(&twdt);
149         wdt_hal_feed(&twdt);
150         wdt_hal_write_protect_enable(&twdt);
151         //Likewise, feed the Interrupt Watchdog (TG1) to prevent a reboot
152         wdt_hal_write_protect_disable(&iwdt);
153         wdt_hal_feed(&iwdt);
154         wdt_hal_write_protect_enable(&iwdt);
155     }
156 
157     esp_dbg_stub_gcov_dump_do();
158 #if !CONFIG_FREERTOS_UNICORE
159     esp_cpu_unstall(other_core);
160 #endif
161     portEXIT_CRITICAL_NESTED(irq_state);
162 }
163 
gcov_rtio_fopen(const char * path,const char * mode)164 void *gcov_rtio_fopen(const char *path, const char *mode)
165 {
166     ESP_EARLY_LOGV(TAG, "%s '%s' '%s'", __FUNCTION__, path, mode);
167     void *f = esp_apptrace_fopen(ESP_APPTRACE_DEST_TRAX, path, mode);
168     ESP_EARLY_LOGV(TAG, "%s ret %p", __FUNCTION__, f);
169     return f;
170 }
171 
gcov_rtio_fclose(void * stream)172 int gcov_rtio_fclose(void *stream)
173 {
174     ESP_EARLY_LOGV(TAG, "%s", __FUNCTION__);
175     return esp_apptrace_fclose(ESP_APPTRACE_DEST_TRAX, stream);
176 }
177 
gcov_rtio_fread(void * ptr,size_t size,size_t nmemb,void * stream)178 size_t gcov_rtio_fread(void *ptr, size_t size, size_t nmemb, void *stream)
179 {
180     ESP_EARLY_LOGV(TAG, "%s read %u", __FUNCTION__, size*nmemb);
181     size_t sz = esp_apptrace_fread(ESP_APPTRACE_DEST_TRAX, ptr, size, nmemb, stream);
182     ESP_EARLY_LOGV(TAG, "%s actually read %u", __FUNCTION__, sz);
183     return sz;
184 }
185 
gcov_rtio_fwrite(const void * ptr,size_t size,size_t nmemb,void * stream)186 size_t gcov_rtio_fwrite(const void *ptr, size_t size, size_t nmemb, void *stream)
187 {
188     ESP_EARLY_LOGV(TAG, "%s", __FUNCTION__);
189     return esp_apptrace_fwrite(ESP_APPTRACE_DEST_TRAX, ptr, size, nmemb, stream);
190 }
191 
gcov_rtio_fseek(void * stream,long offset,int whence)192 int gcov_rtio_fseek(void *stream, long offset, int whence)
193 {
194     int ret = esp_apptrace_fseek(ESP_APPTRACE_DEST_TRAX, stream, offset, whence);
195     ESP_EARLY_LOGV(TAG, "%s(%p %ld %d) = %d", __FUNCTION__, stream, offset, whence, ret);
196     return ret;
197 }
198 
gcov_rtio_ftell(void * stream)199 long gcov_rtio_ftell(void *stream)
200 {
201     long ret = esp_apptrace_ftell(ESP_APPTRACE_DEST_TRAX, stream);
202     ESP_EARLY_LOGV(TAG, "%s(%p) = %ld", __FUNCTION__, stream, ret);
203     return ret;
204 }
205 #endif
206