2017-08-16 19:36:00 -04:00
|
|
|
// Copyright 2017 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// 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"
|
2017-08-16 19:36:00 -04:00
|
|
|
#include "soc/cpu.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"
|
2019-03-15 05:44:27 -04:00
|
|
|
#include "esp_private/dbg_stubs.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
|
|
|
|
|
|
|
#define LOG_LOCAL_LEVEL CONFIG_LOG_DEFAULT_LEVEL
|
|
|
|
#include "esp_log.h"
|
|
|
|
const static char *TAG = "esp_gcov_rtio";
|
|
|
|
|
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
|
|
|
|
2019-12-02 06:01:45 -05:00
|
|
|
static struct syscall_stub_table s_gcov_stub_table;
|
|
|
|
|
|
|
|
|
|
|
|
static int gcov_stub_lock_try_acquire_recursive(_lock_t *lock)
|
|
|
|
{
|
|
|
|
if (*lock && uxSemaphoreGetCount((xSemaphoreHandle)(*lock)) == 0) {
|
|
|
|
// we can do nothing here, gcov dump is initiated with some resource locked
|
|
|
|
// which is also used by gcov functions
|
|
|
|
ESP_EARLY_LOGE(TAG, "Lock 0x%x is busy during GCOV dump! System state can be inconsistent after dump!", lock);
|
|
|
|
}
|
|
|
|
return pdTRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gcov_stub_lock_acquire_recursive(_lock_t *lock)
|
|
|
|
{
|
|
|
|
gcov_stub_lock_try_acquire_recursive(lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void gcov_stub_lock_release_recursive(_lock_t *lock)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-15 12:09:03 -05:00
|
|
|
static int esp_dbg_stub_gcov_dump_do(void)
|
|
|
|
{
|
|
|
|
int ret = ESP_OK;
|
2019-12-02 06:01:45 -05:00
|
|
|
FILE* old_stderr = stderr;
|
|
|
|
FILE* old_stdout = stdout;
|
2020-04-14 15:36:29 -04:00
|
|
|
static struct syscall_stub_table *old_tables[portNUM_PROCESSORS];
|
2018-02-15 12:09:03 -05:00
|
|
|
|
2020-04-14 15:36:29 -04:00
|
|
|
old_tables[0] = syscall_table_ptr_pro;
|
|
|
|
#if portNUM_PROCESSORS > 1
|
|
|
|
old_tables[1] = syscall_table_ptr_app;
|
|
|
|
#endif
|
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");
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
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...");
|
2019-12-02 06:01:45 -05:00
|
|
|
// incase of dual-core chip APP and PRO CPUs share the same table, so it is safe to save only PRO's table
|
2020-04-14 15:36:29 -04:00
|
|
|
memcpy(&s_gcov_stub_table, syscall_table_ptr_pro, sizeof(s_gcov_stub_table));
|
2019-12-02 06:01:45 -05:00
|
|
|
s_gcov_stub_table._lock_acquire_recursive = &gcov_stub_lock_acquire_recursive;
|
|
|
|
s_gcov_stub_table._lock_release_recursive = &gcov_stub_lock_release_recursive;
|
|
|
|
s_gcov_stub_table._lock_try_acquire_recursive = &gcov_stub_lock_try_acquire_recursive,
|
|
|
|
syscall_table_ptr_pro = &s_gcov_stub_table;
|
2020-04-14 15:36:29 -04:00
|
|
|
#if portNUM_PROCESSORS > 1
|
|
|
|
syscall_table_ptr_app = &s_gcov_stub_table;
|
|
|
|
#endif
|
2019-12-02 06:01:45 -05:00
|
|
|
stderr = (FILE*) &__sf_fake_stderr;
|
|
|
|
stdout = (FILE*) &__sf_fake_stdout;
|
2018-06-28 09:38:17 -04:00
|
|
|
__gcov_dump();
|
|
|
|
// reset dump status to allow incremental data accumulation
|
|
|
|
__gcov_reset();
|
2019-12-02 06:01:45 -05:00
|
|
|
stdout = old_stdout;
|
|
|
|
stderr = old_stderr;
|
2020-04-14 15:36:29 -04:00
|
|
|
syscall_table_ptr_pro = old_tables[0];
|
|
|
|
#if portNUM_PROCESSORS > 1
|
|
|
|
syscall_table_ptr_app = old_tables[1];
|
|
|
|
#endif
|
2018-06-28 09:38:17 -04:00
|
|
|
ESP_EARLY_LOGV(TAG, "Free apptrace down buf");
|
|
|
|
free(down_buf);
|
2018-02-15 12:09:03 -05:00
|
|
|
ESP_EARLY_LOGV(TAG, "Finish file transfer session");
|
|
|
|
ret = esp_apptrace_fstop(ESP_APPTRACE_DEST_TRAX);
|
|
|
|
if (ret != ESP_OK) {
|
|
|
|
ESP_EARLY_LOGE(TAG, "Failed to send files transfer stop cmd (%d)!", ret);
|
|
|
|
}
|
2020-04-14 15:36:29 -04:00
|
|
|
ESP_EARLY_LOGV(TAG, "exit %d", ret);
|
2018-02-15 12:09:03 -05:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
* TODO: what about interrupted flash access (when cache disabled)???
|
|
|
|
*
|
|
|
|
* @return ESP_OK on success, otherwise see esp_err_t
|
|
|
|
*/
|
|
|
|
static int esp_dbg_stub_gcov_entry(void)
|
|
|
|
{
|
2021-05-16 11:51:28 -04:00
|
|
|
s_do_dump = true;
|
|
|
|
return ESP_OK;
|
2019-12-02 06:01:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int gcov_rtio_atexit(void (*function)(void) __attribute__ ((unused)))
|
|
|
|
{
|
2021-05-16 11:51:28 -04:00
|
|
|
uint32_t capabilities = 0;
|
2019-12-02 06:01:45 -05:00
|
|
|
ESP_EARLY_LOGV(TAG, "%s", __FUNCTION__);
|
|
|
|
esp_dbg_stub_entry_set(ESP_DBG_STUB_ENTRY_GCOV, (uint32_t)&esp_dbg_stub_gcov_entry);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void esp_gcov_dump(void)
|
2017-08-16 19:36:00 -04:00
|
|
|
{
|
2018-06-28 09:38:17 -04:00
|
|
|
// disable IRQs on this CPU, other CPU is halted by OpenOCD
|
|
|
|
unsigned irq_state = portENTER_CRITICAL_NESTED();
|
|
|
|
#if !CONFIG_FREERTOS_UNICORE
|
2018-02-15 12:09:03 -05:00
|
|
|
int other_core = xPortGetCoreID() ? 0 : 1;
|
|
|
|
esp_cpu_stall(other_core);
|
2017-08-16 19:36:00 -04:00
|
|
|
#endif
|
|
|
|
while (!esp_apptrace_host_is_connected(ESP_APPTRACE_DEST_TRAX)) {
|
2019-12-26 03:30:03 -05:00
|
|
|
wdt_hal_context_t twdt = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0};
|
|
|
|
wdt_hal_context_t iwdt = {.inst = WDT_MWDT1, .mwdt_dev = &TIMERG1};
|
|
|
|
//Feed the Task Watchdog (TG0) to prevent it from timing out
|
|
|
|
wdt_hal_write_protect_disable(&twdt);
|
|
|
|
wdt_hal_feed(&twdt);
|
|
|
|
wdt_hal_write_protect_enable(&twdt);
|
|
|
|
//Likewise, feed the Interrupt Watchdog (TG1) to prevent a reboot
|
|
|
|
wdt_hal_write_protect_disable(&iwdt);
|
|
|
|
wdt_hal_feed(&iwdt);
|
|
|
|
wdt_hal_write_protect_enable(&iwdt);
|
2017-08-16 19:36:00 -04:00
|
|
|
}
|
|
|
|
|
2018-02-15 12:09:03 -05:00
|
|
|
esp_dbg_stub_gcov_dump_do();
|
2018-06-28 09:38:17 -04:00
|
|
|
#if !CONFIG_FREERTOS_UNICORE
|
2018-02-15 12:09:03 -05:00
|
|
|
esp_cpu_unstall(other_core);
|
|
|
|
#endif
|
2018-06-28 09:38:17 -04:00
|
|
|
portEXIT_CRITICAL_NESTED(irq_state);
|
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)
|
|
|
|
{
|
2018-06-28 09:38:17 -04:00
|
|
|
ESP_EARLY_LOGV(TAG, "%s read %u", __FUNCTION__, size*nmemb);
|
|
|
|
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
|
|
|
}
|
|
|
|
#endif
|