]>
Commit | Line | Data |
---|---|---|
ae7467b1 AB |
1 | /* |
2 | * gdbstub internals | |
3 | * | |
4 | * Copyright (c) 2022 Linaro Ltd | |
5 | * | |
6 | * SPDX-License-Identifier: GPL-2.0-or-later | |
7 | */ | |
8 | ||
97748558 AB |
9 | #ifndef GDBSTUB_INTERNALS_H |
10 | #define GDBSTUB_INTERNALS_H | |
ae7467b1 | 11 | |
55b5b8e9 PMD |
12 | #include "exec/cpu-common.h" |
13 | ||
9f56787c AB |
14 | #define MAX_PACKET_LENGTH 4096 |
15 | ||
16 | /* | |
17 | * Shared structures and definitions | |
18 | */ | |
19 | ||
b6fa2ec2 AB |
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_IO = 23, | |
28 | GDB_SIGNAL_XCPU = 24, | |
29 | GDB_SIGNAL_UNKNOWN = 143 | |
30 | }; | |
31 | ||
9f56787c AB |
32 | typedef struct GDBProcess { |
33 | uint32_t pid; | |
34 | bool attached; | |
35 | ||
36 | char target_xml[1024]; | |
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 | char syscall_buf[256]; | |
65 | gdb_syscall_complete_cb current_syscall_cb; | |
66 | GString *str_buf; | |
67 | GByteArray *mem_buf; | |
68 | int sstep_flags; | |
69 | int supported_sstep_flags; | |
70 | } GDBState; | |
71 | ||
b6fa2ec2 AB |
72 | /* lives in main gdbstub.c */ |
73 | extern GDBState gdbserver_state; | |
1678ea04 AB |
74 | |
75 | /* | |
76 | * Inline utility function, convert from int to hex and back | |
77 | */ | |
78 | ||
79 | static inline int fromhex(int v) | |
80 | { | |
81 | if (v >= '0' && v <= '9') { | |
82 | return v - '0'; | |
83 | } else if (v >= 'A' && v <= 'F') { | |
84 | return v - 'A' + 10; | |
85 | } else if (v >= 'a' && v <= 'f') { | |
86 | return v - 'a' + 10; | |
87 | } else { | |
88 | return 0; | |
89 | } | |
90 | } | |
91 | ||
92 | static inline int tohex(int v) | |
93 | { | |
94 | if (v < 10) { | |
95 | return v + '0'; | |
96 | } else { | |
97 | return v - 10 + 'a'; | |
98 | } | |
99 | } | |
100 | ||
36e067b2 AB |
101 | /* |
102 | * Connection helpers for both softmmu and user backends | |
103 | */ | |
104 | ||
105 | void gdb_put_strbuf(void); | |
106 | int gdb_put_packet(const char *buf); | |
107 | int gdb_put_packet_binary(const char *buf, int len, bool dump); | |
108 | void gdb_hextomem(GByteArray *mem, const char *buf, int len); | |
109 | void gdb_memtohex(GString *buf, const uint8_t *mem, int len); | |
110 | void gdb_memtox(GString *buf, const char *mem, int len); | |
111 | void gdb_read_byte(uint8_t ch); | |
112 | ||
a7e0f9bd AB |
113 | /* |
114 | * Packet acknowledgement - we handle this slightly differently | |
115 | * between user and softmmu mode, mainly to deal with the differences | |
116 | * between the flexible chardev and the direct fd approaches. | |
117 | * | |
118 | * We currently don't support a negotiated QStartNoAckMode | |
119 | */ | |
120 | ||
121 | /** | |
122 | * gdb_got_immediate_ack() - check ok to continue | |
123 | * | |
124 | * Returns true to continue, false to re-transmit for user only, the | |
125 | * softmmu stub always returns true. | |
126 | */ | |
127 | bool gdb_got_immediate_ack(void); | |
36e067b2 AB |
128 | /* utility helpers */ |
129 | CPUState *gdb_first_attached_cpu(void); | |
130 | void gdb_append_thread_id(CPUState *cpu, GString *buf); | |
131 | int gdb_get_cpu_index(CPUState *cpu); | |
132 | ||
36e067b2 AB |
133 | void gdb_create_default_process(GDBState *s); |
134 | ||
d96bf49b AB |
135 | /* signal mapping, common for softmmu, specialised for user-mode */ |
136 | int gdb_signal_to_target(int sig); | |
137 | int gdb_target_signal_to_gdb(int sig); | |
138 | ||
139 | int gdb_get_char(void); /* user only */ | |
140 | ||
141 | /** | |
142 | * gdb_continue() - handle continue in mode specific way. | |
143 | */ | |
144 | void gdb_continue(void); | |
145 | ||
146 | /** | |
147 | * gdb_continue_partial() - handle partial continue in mode specific way. | |
148 | */ | |
149 | int gdb_continue_partial(char *newstates); | |
150 | ||
36e067b2 AB |
151 | /* |
152 | * Helpers with separate softmmu and user implementations | |
153 | */ | |
154 | void gdb_put_buffer(const uint8_t *buf, int len); | |
155 | ||
b6fa2ec2 | 156 | /* |
8a2025b3 | 157 | * Command handlers - either specialised or softmmu or user only |
b6fa2ec2 AB |
158 | */ |
159 | void gdb_init_gdbserver_state(void); | |
160 | ||
161 | typedef enum GDBThreadIdKind { | |
162 | GDB_ONE_THREAD = 0, | |
163 | GDB_ALL_THREADS, /* One process, all threads */ | |
164 | GDB_ALL_PROCESSES, | |
165 | GDB_READ_THREAD_ERR | |
166 | } GDBThreadIdKind; | |
167 | ||
168 | typedef union GdbCmdVariant { | |
169 | const char *data; | |
170 | uint8_t opcode; | |
171 | unsigned long val_ul; | |
172 | unsigned long long val_ull; | |
173 | struct { | |
174 | GDBThreadIdKind kind; | |
175 | uint32_t pid; | |
176 | uint32_t tid; | |
177 | } thread_id; | |
178 | } GdbCmdVariant; | |
179 | ||
180 | #define get_param(p, i) (&g_array_index(p, GdbCmdVariant, i)) | |
181 | ||
182 | void gdb_handle_query_rcmd(GArray *params, void *user_ctx); /* softmmu */ | |
d96bf49b AB |
183 | void gdb_handle_query_offsets(GArray *params, void *user_ctx); /* user */ |
184 | void gdb_handle_query_xfer_auxv(GArray *params, void *user_ctx); /*user */ | |
b6fa2ec2 | 185 | |
8a2025b3 AB |
186 | void gdb_handle_query_attached(GArray *params, void *user_ctx); /* both */ |
187 | ||
589a5867 AB |
188 | /* softmmu only */ |
189 | void gdb_handle_query_qemu_phy_mem_mode(GArray *params, void *user_ctx); | |
190 | void gdb_handle_set_qemu_phy_mem_mode(GArray *params, void *user_ctx); | |
191 | ||
9f56787c AB |
192 | /* |
193 | * Break/Watch point support - there is an implementation for softmmu | |
194 | * and user mode. | |
195 | */ | |
a48e7d9e | 196 | bool gdb_supports_guest_debug(void); |
55b5b8e9 PMD |
197 | int gdb_breakpoint_insert(CPUState *cs, int type, vaddr addr, vaddr len); |
198 | int gdb_breakpoint_remove(CPUState *cs, int type, vaddr addr, vaddr len); | |
ae7467b1 AB |
199 | void gdb_breakpoint_remove_all(CPUState *cs); |
200 | ||
589a5867 AB |
201 | /** |
202 | * gdb_target_memory_rw_debug() - handle debug access to memory | |
203 | * @cs: CPUState | |
204 | * @addr: nominal address, could be an entire physical address | |
205 | * @buf: data | |
206 | * @len: length of access | |
207 | * @is_write: is it a write operation | |
208 | * | |
209 | * This function is specialised depending on the mode we are running | |
210 | * in. For softmmu guests we can switch the interpretation of the | |
211 | * address to a physical address. | |
212 | */ | |
213 | int gdb_target_memory_rw_debug(CPUState *cs, hwaddr addr, | |
214 | uint8_t *buf, int len, bool is_write); | |
215 | ||
97748558 | 216 | #endif /* GDBSTUB_INTERNALS_H */ |