1 // Copyright 2015-2019 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 #include <string.h>
16 #include "esp_gdbstub.h"
17 #include "esp_gdbstub_common.h"
18 #include "soc/cpu.h"
19 #include "sdkconfig.h"
20 #include "freertos/FreeRTOS.h"
21 #include "freertos/task.h"
22 
init_regfile(esp_gdbstub_gdb_regfile_t * dst)23 static void init_regfile(esp_gdbstub_gdb_regfile_t *dst)
24 {
25     memset(dst, 0, sizeof(*dst));
26 }
27 
esp_gdbstub_frame_to_regfile(const esp_gdbstub_frame_t * frame,esp_gdbstub_gdb_regfile_t * dst)28 void esp_gdbstub_frame_to_regfile(const esp_gdbstub_frame_t *frame, esp_gdbstub_gdb_regfile_t *dst)
29 {
30     init_regfile(dst);
31     dst->pc = frame->mepc;
32 
33     // We omit register x0 here since it's the zero register and always hard-wired to 0.
34     // See The RISC-V Instruction Set Manual Volume I: Unprivileged ISA Document Version 20191213 for more details.
35     memcpy(&(dst->x[1]), &frame->ra, sizeof(uint32_t) * 31);
36 }
37 
38 #ifdef CONFIG_ESP_GDBSTUB_SUPPORT_TASKS
39 
40 /* Represents FreeRTOS TCB structure */
41 typedef struct {
42     uint8_t *top_of_stack;
43     /* Other members aren't needed */
44 } dummy_tcb_t;
45 
46 
esp_gdbstub_tcb_to_regfile(TaskHandle_t tcb,esp_gdbstub_gdb_regfile_t * dst)47 void esp_gdbstub_tcb_to_regfile(TaskHandle_t tcb, esp_gdbstub_gdb_regfile_t *dst)
48 {
49     const dummy_tcb_t *dummy_tcb = (const dummy_tcb_t *) tcb;
50 
51     const RvExcFrame *frame = (RvExcFrame *) dummy_tcb->top_of_stack;
52     esp_gdbstub_frame_to_regfile(frame, dst);
53 }
54 
55 #endif // CONFIG_ESP_GDBSTUB_SUPPORT_TASKS
56 
esp_gdbstub_get_signal(const esp_gdbstub_frame_t * frame)57 int esp_gdbstub_get_signal(const esp_gdbstub_frame_t *frame)
58 {
59       return 5; // SIGTRAP, see IDF-2490
60 }
61