2021-05-02 22:15:17 -04:00
|
|
|
/*
|
2024-02-17 17:43:23 -05:00
|
|
|
* SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD
|
2021-05-02 22:15:17 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2017-08-16 19:36:00 -04:00
|
|
|
|
|
|
|
// This module implements runtime file I/O API for GCOV.
|
|
|
|
|
2019-12-02 06:01:45 -05:00
|
|
|
#include <string.h>
|
2017-09-30 06:07:19 -04:00
|
|
|
#include "esp_task_wdt.h"
|
2017-08-16 19:36:00 -04:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2019-12-02 06:01:45 -05:00
|
|
|
#include "freertos/semphr.h"
|
2019-05-13 06:02:45 -04:00
|
|
|
#include "soc/timer_periph.h"
|
2017-08-16 19:36:00 -04:00
|
|
|
#include "esp_app_trace.h"
|
2021-04-23 12:10:35 -04:00
|
|
|
#include "esp_freertos_hooks.h"
|
2019-03-15 05:44:27 -04:00
|
|
|
#include "esp_private/dbg_stubs.h"
|
2021-06-29 06:12:01 -04:00
|
|
|
#include "esp_ipc.h"
|
2024-04-01 05:04:25 -04:00
|
|
|
#include "esp_attr.h"
|
2019-12-26 03:30:03 -05:00
|
|
|
#include "hal/wdt_hal.h"
|
2019-12-02 06:01:45 -05:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
|
|
#include "esp32/rom/libc_stubs.h"
|
2020-01-16 22:47:08 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
#include "esp32s2/rom/libc_stubs.h"
|
2019-12-02 06:01:45 -05:00
|
|
|
#endif
|
2017-08-16 19:36:00 -04:00
|
|
|
|
2019-09-13 08:49:11 -04:00
|
|
|
#if CONFIG_APPTRACE_GCOV_ENABLE
|
2018-02-15 12:09:03 -05:00
|
|
|
|
|
|
|
#define ESP_GCOV_DOWN_BUF_SIZE 4200
|
2017-08-16 19:36:00 -04:00
|
|
|
|
|
|
|
#include "esp_log.h"
|
|
|
|
const static char *TAG = "esp_gcov_rtio";
|
2021-04-23 12:10:35 -04:00
|
|
|
static volatile bool s_create_gcov_task = false;
|
|
|
|
static volatile bool s_gcov_task_running = false;
|
2017-08-16 19:36:00 -04:00
|
|
|
|
2019-11-05 06:20:26 -05:00
|
|
|
extern void __gcov_dump(void);
|
|
|
|
extern void __gcov_reset(void);
|
2018-02-15 12:09:03 -05:00
|
|
|
|
2021-04-23 12:10:35 -04:00
|
|
|
void gcov_dump_task(void *pvParameter)
|
2019-12-02 06:01:45 -05:00
|
|
|
{
|
2021-04-23 12:10:35 -04:00
|
|
|
int dump_result = 0;
|
|
|
|
bool *running = (bool *)pvParameter;
|
2019-12-02 06:01:45 -05:00
|
|
|
|
2021-06-29 06:12:01 -04:00
|
|
|
ESP_EARLY_LOGV(TAG, "%s stack use in %d", __FUNCTION__, uxTaskGetStackHighWaterMark(NULL));
|
|
|
|
|
2018-06-28 09:38:17 -04:00
|
|
|
ESP_EARLY_LOGV(TAG, "Alloc apptrace down buf %d bytes", ESP_GCOV_DOWN_BUF_SIZE);
|
|
|
|
void *down_buf = malloc(ESP_GCOV_DOWN_BUF_SIZE);
|
|
|
|
if (down_buf == NULL) {
|
|
|
|
ESP_EARLY_LOGE(TAG, "Could not allocate memory for the buffer");
|
2021-04-23 12:10:35 -04:00
|
|
|
dump_result = ESP_ERR_NO_MEM;
|
|
|
|
goto gcov_exit;
|
2018-06-28 09:38:17 -04:00
|
|
|
}
|
|
|
|
ESP_EARLY_LOGV(TAG, "Config apptrace down buf");
|
|
|
|
esp_apptrace_down_buffer_config(down_buf, ESP_GCOV_DOWN_BUF_SIZE);
|
|
|
|
ESP_EARLY_LOGV(TAG, "Dump data...");
|
|
|
|
__gcov_dump();
|
|
|
|
// reset dump status to allow incremental data accumulation
|
|
|
|
__gcov_reset();
|
|
|
|
free(down_buf);
|
2018-02-15 12:09:03 -05:00
|
|
|
ESP_EARLY_LOGV(TAG, "Finish file transfer session");
|
2021-04-23 12:10:35 -04:00
|
|
|
dump_result = esp_apptrace_fstop(ESP_APPTRACE_DEST_TRAX);
|
|
|
|
if (dump_result != ESP_OK) {
|
|
|
|
ESP_EARLY_LOGE(TAG, "Failed to send files transfer stop cmd (%d)!", dump_result);
|
|
|
|
}
|
|
|
|
|
|
|
|
gcov_exit:
|
|
|
|
ESP_EARLY_LOGV(TAG, "dump_result %d", dump_result);
|
|
|
|
if (running) {
|
|
|
|
*running = false;
|
|
|
|
}
|
2021-06-29 06:12:01 -04:00
|
|
|
|
|
|
|
ESP_EARLY_LOGV(TAG, "%s stack use out %d", __FUNCTION__, uxTaskGetStackHighWaterMark(NULL));
|
|
|
|
|
2021-04-23 12:10:35 -04:00
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
2021-06-29 06:12:01 -04:00
|
|
|
void gcov_create_task(void *arg)
|
2021-04-23 12:10:35 -04:00
|
|
|
{
|
2021-06-29 06:12:01 -04:00
|
|
|
ESP_EARLY_LOGV(TAG, "%s", __FUNCTION__);
|
2023-08-21 18:41:39 -04:00
|
|
|
xTaskCreatePinnedToCore(&gcov_dump_task, "gcov_dump_task", CONFIG_APPTRACE_GCOV_DUMP_TASK_STACK_SIZE,
|
|
|
|
(void *)&s_gcov_task_running, configMAX_PRIORITIES - 1, NULL, 0);
|
2021-06-29 06:12:01 -04:00
|
|
|
}
|
|
|
|
|
2024-04-01 05:04:25 -04:00
|
|
|
static IRAM_ATTR
|
2021-06-29 06:12:01 -04:00
|
|
|
void gcov_create_task_tick_hook(void)
|
|
|
|
{
|
|
|
|
extern esp_err_t esp_ipc_start_gcov_from_isr(uint32_t cpu_id, esp_ipc_func_t func, void* arg);
|
2021-04-23 12:10:35 -04:00
|
|
|
if (s_create_gcov_task) {
|
2021-06-29 06:12:01 -04:00
|
|
|
if (esp_ipc_start_gcov_from_isr(xPortGetCoreID(), &gcov_create_task, NULL) == ESP_OK) {
|
|
|
|
s_create_gcov_task = false;
|
|
|
|
}
|
2018-02-15 12:09:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-02 06:01:45 -05:00
|
|
|
/**
|
2021-05-16 11:51:28 -04:00
|
|
|
* @brief Triggers gcov info dump task
|
2019-12-02 06:01:45 -05:00
|
|
|
* This function is to be called by OpenOCD, not by normal user code.
|
2021-04-23 12:10:35 -04:00
|
|
|
* TODO: what about interrupted flash access (when cache disabled)
|
2019-12-02 06:01:45 -05:00
|
|
|
*
|
|
|
|
* @return ESP_OK on success, otherwise see esp_err_t
|
|
|
|
*/
|
|
|
|
static int esp_dbg_stub_gcov_entry(void)
|
|
|
|
{
|
2021-04-23 12:10:35 -04:00
|
|
|
/* we are in isr context here */
|
|
|
|
s_create_gcov_task = true;
|
2021-05-16 11:51:28 -04:00
|
|
|
return ESP_OK;
|
2019-12-02 06:01:45 -05:00
|
|
|
}
|
|
|
|
|
2022-12-12 04:00:24 -05:00
|
|
|
void gcov_rtio_init(void)
|
2019-12-02 06:01:45 -05:00
|
|
|
{
|
2024-02-17 17:43:23 -05:00
|
|
|
uint32_t stub_entry = 0;
|
2019-12-02 06:01:45 -05:00
|
|
|
ESP_EARLY_LOGV(TAG, "%s", __FUNCTION__);
|
2024-02-17 17:43:23 -05:00
|
|
|
assert(esp_dbg_stub_entry_get(ESP_DBG_STUB_ENTRY_GCOV, &stub_entry) == ESP_OK);
|
|
|
|
if (stub_entry != 0) {
|
|
|
|
/* "__gcov_init()" can be called several times. We must avoid multiple tick hook registration */
|
|
|
|
return;
|
2021-04-23 12:10:35 -04:00
|
|
|
}
|
2024-02-17 17:43:23 -05:00
|
|
|
esp_dbg_stub_entry_set(ESP_DBG_STUB_ENTRY_GCOV, (uint32_t)&esp_dbg_stub_gcov_entry);
|
|
|
|
assert(esp_dbg_stub_entry_get(ESP_DBG_STUB_ENTRY_CAPABILITIES, &stub_entry) == ESP_OK);
|
|
|
|
esp_dbg_stub_entry_set(ESP_DBG_STUB_ENTRY_CAPABILITIES, stub_entry | ESP_DBG_STUB_CAP_GCOV_TASK);
|
2021-06-29 06:12:01 -04:00
|
|
|
esp_register_freertos_tick_hook(gcov_create_task_tick_hook);
|
2019-12-02 06:01:45 -05:00
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void esp_gcov_dump(void)
|
2017-08-16 19:36:00 -04:00
|
|
|
{
|
2021-06-29 06:12:01 -04:00
|
|
|
ESP_EARLY_LOGV(TAG, "%s", __FUNCTION__);
|
|
|
|
|
2017-08-16 19:36:00 -04:00
|
|
|
while (!esp_apptrace_host_is_connected(ESP_APPTRACE_DEST_TRAX)) {
|
2021-04-23 12:10:35 -04:00
|
|
|
vTaskDelay(pdMS_TO_TICKS(10));
|
2017-08-16 19:36:00 -04:00
|
|
|
}
|
|
|
|
|
2021-04-23 12:10:35 -04:00
|
|
|
/* We are not in isr context here. Waiting for the completion is safe */
|
|
|
|
s_gcov_task_running = true;
|
2021-06-29 06:12:01 -04:00
|
|
|
s_create_gcov_task = true;
|
2021-04-23 12:10:35 -04:00
|
|
|
while (s_gcov_task_running) {
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(10));
|
|
|
|
}
|
2017-08-16 19:36:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void *gcov_rtio_fopen(const char *path, const char *mode)
|
|
|
|
{
|
2018-06-28 09:38:17 -04:00
|
|
|
ESP_EARLY_LOGV(TAG, "%s '%s' '%s'", __FUNCTION__, path, mode);
|
2020-04-14 15:36:29 -04:00
|
|
|
void *f = esp_apptrace_fopen(ESP_APPTRACE_DEST_TRAX, path, mode);
|
|
|
|
ESP_EARLY_LOGV(TAG, "%s ret %p", __FUNCTION__, f);
|
|
|
|
return f;
|
2017-08-16 19:36:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int gcov_rtio_fclose(void *stream)
|
|
|
|
{
|
2018-02-15 12:09:03 -05:00
|
|
|
ESP_EARLY_LOGV(TAG, "%s", __FUNCTION__);
|
2017-08-16 19:36:00 -04:00
|
|
|
return esp_apptrace_fclose(ESP_APPTRACE_DEST_TRAX, stream);
|
|
|
|
}
|
|
|
|
|
2018-02-15 12:09:03 -05:00
|
|
|
size_t gcov_rtio_fread(void *ptr, size_t size, size_t nmemb, void *stream)
|
|
|
|
{
|
2021-04-23 12:10:35 -04:00
|
|
|
ESP_EARLY_LOGV(TAG, "%s read %u", __FUNCTION__, size * nmemb);
|
2018-06-28 09:38:17 -04:00
|
|
|
size_t sz = esp_apptrace_fread(ESP_APPTRACE_DEST_TRAX, ptr, size, nmemb, stream);
|
|
|
|
ESP_EARLY_LOGV(TAG, "%s actually read %u", __FUNCTION__, sz);
|
|
|
|
return sz;
|
2018-02-15 12:09:03 -05:00
|
|
|
}
|
|
|
|
|
2017-08-16 19:36:00 -04:00
|
|
|
size_t gcov_rtio_fwrite(const void *ptr, size_t size, size_t nmemb, void *stream)
|
|
|
|
{
|
2018-02-15 12:09:03 -05:00
|
|
|
ESP_EARLY_LOGV(TAG, "%s", __FUNCTION__);
|
2017-08-16 19:36:00 -04:00
|
|
|
return esp_apptrace_fwrite(ESP_APPTRACE_DEST_TRAX, ptr, size, nmemb, stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
int gcov_rtio_fseek(void *stream, long offset, int whence)
|
|
|
|
{
|
2018-02-15 12:09:03 -05:00
|
|
|
int ret = esp_apptrace_fseek(ESP_APPTRACE_DEST_TRAX, stream, offset, whence);
|
2018-06-28 09:38:17 -04:00
|
|
|
ESP_EARLY_LOGV(TAG, "%s(%p %ld %d) = %d", __FUNCTION__, stream, offset, whence, ret);
|
2018-02-15 12:09:03 -05:00
|
|
|
return ret;
|
2017-08-16 19:36:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
long gcov_rtio_ftell(void *stream)
|
|
|
|
{
|
2018-02-15 12:09:03 -05:00
|
|
|
long ret = esp_apptrace_ftell(ESP_APPTRACE_DEST_TRAX, stream);
|
2018-06-28 09:38:17 -04:00
|
|
|
ESP_EARLY_LOGV(TAG, "%s(%p) = %ld", __FUNCTION__, stream, ret);
|
2018-02-15 12:09:03 -05:00
|
|
|
return ret;
|
2017-08-16 19:36:00 -04:00
|
|
|
}
|
2022-12-12 04:00:24 -05:00
|
|
|
|
2023-07-26 06:31:43 -04:00
|
|
|
int gcov_rtio_feof(void *stream)
|
|
|
|
{
|
2023-10-10 10:45:49 -04:00
|
|
|
int ret = esp_apptrace_feof(ESP_APPTRACE_DEST_TRAX, stream);
|
|
|
|
ESP_EARLY_LOGV(TAG, "%s(%p) = %d", __FUNCTION__, stream, ret);
|
|
|
|
return ret;
|
2023-07-26 06:31:43 -04:00
|
|
|
}
|
|
|
|
|
2022-12-12 04:00:24 -05:00
|
|
|
void gcov_rtio_setbuf(void *arg1 __attribute__ ((unused)), void *arg2 __attribute__ ((unused)))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wrappers for Gcov functions */
|
|
|
|
|
|
|
|
extern void __real___gcov_init(void *info);
|
|
|
|
void __wrap___gcov_init(void *info)
|
|
|
|
{
|
|
|
|
__real___gcov_init(info);
|
|
|
|
gcov_rtio_init();
|
|
|
|
}
|
|
|
|
|
2017-08-16 19:36:00 -04:00
|
|
|
#endif
|