1 /*
2  * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 // This module implements runtime file I/O API for GCOV.
8 
9 #include <string.h>
10 #include "esp_task_wdt.h"
11 #include "freertos/FreeRTOS.h"
12 #include "freertos/task.h"
13 #include "freertos/semphr.h"
14 #include "soc/cpu.h"
15 #include "soc/timer_periph.h"
16 #include "esp_app_trace.h"
17 #include "esp_freertos_hooks.h"
18 #include "esp_private/dbg_stubs.h"
19 #include "esp_ipc.h"
20 #include "hal/wdt_hal.h"
21 #if CONFIG_IDF_TARGET_ESP32
22 #include "esp32/rom/libc_stubs.h"
23 #elif CONFIG_IDF_TARGET_ESP32S2
24 #include "esp32s2/rom/libc_stubs.h"
25 #endif
26 
27 #if CONFIG_APPTRACE_GCOV_ENABLE
28 
29 #define ESP_GCOV_DOWN_BUF_SIZE  4200
30 
31 #include "esp_log.h"
32 const static char *TAG = "esp_gcov_rtio";
33 static volatile bool s_create_gcov_task = false;
34 static volatile bool s_gcov_task_running = false;
35 
36 extern void __gcov_dump(void);
37 extern void __gcov_reset(void);
38 
gcov_dump_task(void * pvParameter)39 void gcov_dump_task(void *pvParameter)
40 {
41     int dump_result = 0;
42     bool *running = (bool *)pvParameter;
43 
44     ESP_EARLY_LOGV(TAG, "%s stack use in %d", __FUNCTION__, uxTaskGetStackHighWaterMark(NULL));
45 
46     ESP_EARLY_LOGV(TAG, "Alloc apptrace down buf %d bytes", ESP_GCOV_DOWN_BUF_SIZE);
47     void *down_buf = malloc(ESP_GCOV_DOWN_BUF_SIZE);
48     if (down_buf == NULL) {
49         ESP_EARLY_LOGE(TAG, "Could not allocate memory for the buffer");
50         dump_result = ESP_ERR_NO_MEM;
51         goto gcov_exit;
52     }
53     ESP_EARLY_LOGV(TAG, "Config apptrace down buf");
54     esp_apptrace_down_buffer_config(down_buf, ESP_GCOV_DOWN_BUF_SIZE);
55     ESP_EARLY_LOGV(TAG, "Dump data...");
56     __gcov_dump();
57     // reset dump status to allow incremental data accumulation
58     __gcov_reset();
59     free(down_buf);
60     ESP_EARLY_LOGV(TAG, "Finish file transfer session");
61     dump_result = esp_apptrace_fstop(ESP_APPTRACE_DEST_TRAX);
62     if (dump_result != ESP_OK) {
63         ESP_EARLY_LOGE(TAG, "Failed to send files transfer stop cmd (%d)!", dump_result);
64     }
65 
66 gcov_exit:
67     ESP_EARLY_LOGV(TAG, "dump_result %d", dump_result);
68     if (running) {
69         *running = false;
70     }
71 
72     ESP_EARLY_LOGV(TAG, "%s stack use out %d", __FUNCTION__, uxTaskGetStackHighWaterMark(NULL));
73 
74     vTaskDelete(NULL);
75 }
76 
gcov_create_task(void * arg)77 void gcov_create_task(void *arg)
78 {
79     ESP_EARLY_LOGV(TAG, "%s", __FUNCTION__);
80     xTaskCreatePinnedToCore(&gcov_dump_task, "gcov_dump_task", 2048, (void *)&s_gcov_task_running, configMAX_PRIORITIES - 1, NULL, 0);
81 }
82 
gcov_create_task_tick_hook(void)83 void gcov_create_task_tick_hook(void)
84 {
85     extern esp_err_t esp_ipc_start_gcov_from_isr(uint32_t cpu_id, esp_ipc_func_t func, void* arg);
86     if (s_create_gcov_task) {
87         if (esp_ipc_start_gcov_from_isr(xPortGetCoreID(), &gcov_create_task, NULL) == ESP_OK) {
88             s_create_gcov_task = false;
89         }
90     }
91 }
92 
93 /**
94  * @brief Triggers gcov info dump task
95  *        This function is to be called by OpenOCD, not by normal user code.
96  * TODO: what about interrupted flash access (when cache disabled)
97  *
98  * @return ESP_OK on success, otherwise see esp_err_t
99  */
esp_dbg_stub_gcov_entry(void)100 static int esp_dbg_stub_gcov_entry(void)
101 {
102     /* we are in isr context here */
103     s_create_gcov_task = true;
104     return ESP_OK;
105 }
106 
gcov_rtio_atexit(void (* function)(void))107 int gcov_rtio_atexit(void (*function)(void) __attribute__ ((unused)))
108 {
109     uint32_t capabilities = 0;
110     ESP_EARLY_LOGV(TAG, "%s", __FUNCTION__);
111     esp_dbg_stub_entry_set(ESP_DBG_STUB_ENTRY_GCOV, (uint32_t)&esp_dbg_stub_gcov_entry);
112     if (esp_dbg_stub_entry_get(ESP_DBG_STUB_ENTRY_CAPABILITIES, &capabilities) == ESP_OK) {
113         esp_dbg_stub_entry_set(ESP_DBG_STUB_ENTRY_CAPABILITIES, capabilities | ESP_DBG_STUB_CAP_GCOV_TASK);
114     }
115     esp_register_freertos_tick_hook(gcov_create_task_tick_hook);
116     return ESP_OK;
117 }
118 
esp_gcov_dump(void)119 void esp_gcov_dump(void)
120 {
121     ESP_EARLY_LOGV(TAG, "%s", __FUNCTION__);
122 
123     while (!esp_apptrace_host_is_connected(ESP_APPTRACE_DEST_TRAX)) {
124         vTaskDelay(pdMS_TO_TICKS(10));
125     }
126 
127     /* We are not in isr context here. Waiting for the completion is safe */
128     s_gcov_task_running = true;
129     s_create_gcov_task = true;
130     while (s_gcov_task_running) {
131         vTaskDelay(pdMS_TO_TICKS(10));
132     }
133 }
134 
gcov_rtio_fopen(const char * path,const char * mode)135 void *gcov_rtio_fopen(const char *path, const char *mode)
136 {
137     ESP_EARLY_LOGV(TAG, "%s '%s' '%s'", __FUNCTION__, path, mode);
138     void *f = esp_apptrace_fopen(ESP_APPTRACE_DEST_TRAX, path, mode);
139     ESP_EARLY_LOGV(TAG, "%s ret %p", __FUNCTION__, f);
140     return f;
141 }
142 
gcov_rtio_fclose(void * stream)143 int gcov_rtio_fclose(void *stream)
144 {
145     ESP_EARLY_LOGV(TAG, "%s", __FUNCTION__);
146     return esp_apptrace_fclose(ESP_APPTRACE_DEST_TRAX, stream);
147 }
148 
gcov_rtio_fread(void * ptr,size_t size,size_t nmemb,void * stream)149 size_t gcov_rtio_fread(void *ptr, size_t size, size_t nmemb, void *stream)
150 {
151     ESP_EARLY_LOGV(TAG, "%s read %u", __FUNCTION__, size * nmemb);
152     size_t sz = esp_apptrace_fread(ESP_APPTRACE_DEST_TRAX, ptr, size, nmemb, stream);
153     ESP_EARLY_LOGV(TAG, "%s actually read %u", __FUNCTION__, sz);
154     return sz;
155 }
156 
gcov_rtio_fwrite(const void * ptr,size_t size,size_t nmemb,void * stream)157 size_t gcov_rtio_fwrite(const void *ptr, size_t size, size_t nmemb, void *stream)
158 {
159     ESP_EARLY_LOGV(TAG, "%s", __FUNCTION__);
160     return esp_apptrace_fwrite(ESP_APPTRACE_DEST_TRAX, ptr, size, nmemb, stream);
161 }
162 
gcov_rtio_fseek(void * stream,long offset,int whence)163 int gcov_rtio_fseek(void *stream, long offset, int whence)
164 {
165     int ret = esp_apptrace_fseek(ESP_APPTRACE_DEST_TRAX, stream, offset, whence);
166     ESP_EARLY_LOGV(TAG, "%s(%p %ld %d) = %d", __FUNCTION__, stream, offset, whence, ret);
167     return ret;
168 }
169 
gcov_rtio_ftell(void * stream)170 long gcov_rtio_ftell(void *stream)
171 {
172     long ret = esp_apptrace_ftell(ESP_APPTRACE_DEST_TRAX, stream);
173     ESP_EARLY_LOGV(TAG, "%s(%p) = %ld", __FUNCTION__, stream, ret);
174     return ret;
175 }
176 #endif
177