xref: /openbmc/qemu/gdbstub/internals.h (revision c16de0d9)
1 /*
2  * gdbstub internals
3  *
4  * Copyright (c) 2022 Linaro Ltd
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8 
9 #ifndef GDBSTUB_INTERNALS_H
10 #define GDBSTUB_INTERNALS_H
11 
12 #include "exec/cpu-common.h"
13 
14 #define MAX_PACKET_LENGTH 4096
15 
16 /*
17  * Shared structures and definitions
18  */
19 
20 enum {
21     GDB_SIGNAL_0 = 0,
22     GDB_SIGNAL_INT = 2,
23     GDB_SIGNAL_QUIT = 3,
24     GDB_SIGNAL_TRAP = 5,
25     GDB_SIGNAL_ABRT = 6,
26     GDB_SIGNAL_ALRM = 14,
27     GDB_SIGNAL_STOP = 17,
28     GDB_SIGNAL_IO = 23,
29     GDB_SIGNAL_XCPU = 24,
30     GDB_SIGNAL_UNKNOWN = 143
31 };
32 
33 typedef struct GDBProcess {
34     uint32_t pid;
35     bool attached;
36     char *target_xml;
37 } GDBProcess;
38 
39 enum RSState {
40     RS_INACTIVE,
41     RS_IDLE,
42     RS_GETLINE,
43     RS_GETLINE_ESC,
44     RS_GETLINE_RLE,
45     RS_CHKSUM1,
46     RS_CHKSUM2,
47 };
48 
49 typedef struct GDBState {
50     bool init;       /* have we been initialised? */
51     CPUState *c_cpu; /* current CPU for step/continue ops */
52     CPUState *g_cpu; /* current CPU for other ops */
53     CPUState *query_cpu; /* for q{f|s}ThreadInfo */
54     enum RSState state; /* parsing state */
55     char line_buf[MAX_PACKET_LENGTH];
56     int line_buf_index;
57     int line_sum; /* running checksum */
58     int line_csum; /* checksum at the end of the packet */
59     GByteArray *last_packet;
60     int signal;
61     bool multiprocess;
62     GDBProcess *processes;
63     int process_num;
64     GString *str_buf;
65     GByteArray *mem_buf;
66     int sstep_flags;
67     int supported_sstep_flags;
68     /*
69      * Whether we are allowed to send a stop reply packet at this moment.
70      * Must be set off after sending the stop reply itself.
71      */
72     bool allow_stop_reply;
73 } GDBState;
74 
75 /* lives in main gdbstub.c */
76 extern GDBState gdbserver_state;
77 
78 /*
79  * Inline utility function, convert from int to hex and back
80  */
81 
82 static inline int fromhex(int v)
83 {
84     if (v >= '0' && v <= '9') {
85         return v - '0';
86     } else if (v >= 'A' && v <= 'F') {
87         return v - 'A' + 10;
88     } else if (v >= 'a' && v <= 'f') {
89         return v - 'a' + 10;
90     } else {
91         return 0;
92     }
93 }
94 
95 static inline int tohex(int v)
96 {
97     if (v < 10) {
98         return v + '0';
99     } else {
100         return v - 10 + 'a';
101     }
102 }
103 
104 /*
105  * Connection helpers for both system and user backends
106  */
107 
108 void gdb_put_strbuf(void);
109 int gdb_put_packet(const char *buf);
110 int gdb_put_packet_binary(const char *buf, int len, bool dump);
111 void gdb_hextomem(GByteArray *mem, const char *buf, int len);
112 void gdb_memtohex(GString *buf, const uint8_t *mem, int len);
113 void gdb_memtox(GString *buf, const char *mem, int len);
114 void gdb_read_byte(uint8_t ch);
115 
116 /*
117  * Packet acknowledgement - we handle this slightly differently
118  * between user and softmmu mode, mainly to deal with the differences
119  * between the flexible chardev and the direct fd approaches.
120  *
121  * We currently don't support a negotiated QStartNoAckMode
122  */
123 
124 /**
125  * gdb_got_immediate_ack() - check ok to continue
126  *
127  * Returns true to continue, false to re-transmit for user only, the
128  * softmmu stub always returns true.
129  */
130 bool gdb_got_immediate_ack(void);
131 /* utility helpers */
132 GDBProcess *gdb_get_process(uint32_t pid);
133 CPUState *gdb_get_first_cpu_in_process(GDBProcess *process);
134 CPUState *gdb_first_attached_cpu(void);
135 void gdb_append_thread_id(CPUState *cpu, GString *buf);
136 int gdb_get_cpu_index(CPUState *cpu);
137 unsigned int gdb_get_max_cpus(void); /* both */
138 bool gdb_can_reverse(void); /* softmmu, stub for user */
139 
140 void gdb_create_default_process(GDBState *s);
141 
142 /* signal mapping, common for softmmu, specialised for user-mode */
143 int gdb_signal_to_target(int sig);
144 int gdb_target_signal_to_gdb(int sig);
145 
146 int gdb_get_char(void); /* user only */
147 
148 /**
149  * gdb_continue() - handle continue in mode specific way.
150  */
151 void gdb_continue(void);
152 
153 /**
154  * gdb_continue_partial() - handle partial continue in mode specific way.
155  */
156 int gdb_continue_partial(char *newstates);
157 
158 /*
159  * Helpers with separate softmmu and user implementations
160  */
161 void gdb_put_buffer(const uint8_t *buf, int len);
162 
163 /*
164  * Command handlers - either specialised or softmmu or user only
165  */
166 void gdb_init_gdbserver_state(void);
167 
168 typedef enum GDBThreadIdKind {
169     GDB_ONE_THREAD = 0,
170     GDB_ALL_THREADS,     /* One process, all threads */
171     GDB_ALL_PROCESSES,
172     GDB_READ_THREAD_ERR
173 } GDBThreadIdKind;
174 
175 typedef union GdbCmdVariant {
176     const char *data;
177     uint8_t opcode;
178     unsigned long val_ul;
179     unsigned long long val_ull;
180     struct {
181         GDBThreadIdKind kind;
182         uint32_t pid;
183         uint32_t tid;
184     } thread_id;
185 } GdbCmdVariant;
186 
187 #define get_param(p, i)    (&g_array_index(p, GdbCmdVariant, i))
188 
189 void gdb_handle_query_rcmd(GArray *params, void *user_ctx); /* softmmu */
190 void gdb_handle_query_offsets(GArray *params, void *user_ctx); /* user */
191 void gdb_handle_query_xfer_auxv(GArray *params, void *user_ctx); /*user */
192 void gdb_handle_v_file_open(GArray *params, void *user_ctx); /* user */
193 void gdb_handle_v_file_close(GArray *params, void *user_ctx); /* user */
194 void gdb_handle_v_file_pread(GArray *params, void *user_ctx); /* user */
195 void gdb_handle_v_file_readlink(GArray *params, void *user_ctx); /* user */
196 void gdb_handle_query_xfer_exec_file(GArray *params, void *user_ctx); /* user */
197 
198 void gdb_handle_query_attached(GArray *params, void *user_ctx); /* both */
199 
200 /* softmmu only */
201 void gdb_handle_query_qemu_phy_mem_mode(GArray *params, void *user_ctx);
202 void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *user_ctx);
203 
204 /* sycall handling */
205 void gdb_handle_file_io(GArray *params, void *user_ctx);
206 bool gdb_handled_syscall(void);
207 void gdb_disable_syscalls(void);
208 void gdb_syscall_reset(void);
209 
210 /* user/softmmu specific syscall handling */
211 void gdb_syscall_handling(const char *syscall_packet);
212 
213 /*
214  * Break/Watch point support - there is an implementation for softmmu
215  * and user mode.
216  */
217 bool gdb_supports_guest_debug(void);
218 int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len);
219 int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len);
220 void gdb_breakpoint_remove_all(CPUState *cs);
221 
222 /**
223  * gdb_target_memory_rw_debug() - handle debug access to memory
224  * @cs: CPUState
225  * @addr: nominal address, could be an entire physical address
226  * @buf: data
227  * @len: length of access
228  * @is_write: is it a write operation
229  *
230  * This function is specialised depending on the mode we are running
231  * in. For system guests we can switch the interpretation of the
232  * address to a physical address.
233  */
234 int gdb_target_memory_rw_debug(CPUState *cs, hwaddr addr,
235                                uint8_t *buf, int len, bool is_write);
236 
237 #endif /* GDBSTUB_INTERNALS_H */
238