]>
Commit | Line | Data |
---|---|---|
b4608c04 FB |
1 | /* |
2 | * gdb server stub | |
5fafdf24 | 3 | * |
42a09596 AB |
4 | * This implements a subset of the remote protocol as described in: |
5 | * | |
6 | * https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html | |
7 | * | |
3475187d | 8 | * Copyright (c) 2003-2005 Fabrice Bellard |
b4608c04 FB |
9 | * |
10 | * This library is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU Lesser General Public | |
12 | * License as published by the Free Software Foundation; either | |
13 | * version 2 of the License, or (at your option) any later version. | |
14 | * | |
15 | * This library is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
18 | * Lesser General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU Lesser General Public | |
8167ee88 | 21 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
42a09596 AB |
22 | * |
23 | * SPDX-License-Identifier: LGPL-2.0+ | |
b4608c04 | 24 | */ |
856dfd8a | 25 | |
d38ea87a | 26 | #include "qemu/osdep.h" |
856dfd8a | 27 | #include "qemu/ctype.h" |
f348b6d1 | 28 | #include "qemu/cutils.h" |
0b8fa32f | 29 | #include "qemu/module.h" |
cc37d98b | 30 | #include "qemu/error-report.h" |
842b42df | 31 | #include "trace.h" |
85b4fa0c | 32 | #include "exec/gdbstub.h" |
c566080c | 33 | #include "gdbstub/syscalls.h" |
f348b6d1 | 34 | #ifdef CONFIG_USER_ONLY |
d96bf49b | 35 | #include "gdbstub/user.h" |
1fddef4b | 36 | #else |
8f468636 | 37 | #include "hw/cpu/cluster.h" |
5cc8767d | 38 | #include "hw/boards.h" |
1fddef4b | 39 | #endif |
67b915a5 | 40 | |
b3946626 | 41 | #include "sysemu/hw_accel.h" |
54d31236 | 42 | #include "sysemu/runstate.h" |
5b5968c4 | 43 | #include "exec/replay-core.h" |
548c9609 | 44 | #include "exec/hwaddr.h" |
ca587a8e | 45 | |
ae7467b1 AB |
46 | #include "internals.h" |
47 | ||
56aebc89 PB |
48 | typedef struct GDBRegisterState { |
49 | int base_reg; | |
50 | int num_regs; | |
a010bdbe AB |
51 | gdb_get_reg_cb get_reg; |
52 | gdb_set_reg_cb set_reg; | |
56aebc89 | 53 | const char *xml; |
56aebc89 PB |
54 | } GDBRegisterState; |
55 | ||
b6fa2ec2 | 56 | GDBState gdbserver_state; |
8d98c445 | 57 | |
36e067b2 | 58 | void gdb_init_gdbserver_state(void) |
8d98c445 AB |
59 | { |
60 | g_assert(!gdbserver_state.init); | |
61 | memset(&gdbserver_state, 0, sizeof(GDBState)); | |
62 | gdbserver_state.init = true; | |
308f9e88 | 63 | gdbserver_state.str_buf = g_string_new(NULL); |
4a25f1b9 | 64 | gdbserver_state.mem_buf = g_byte_array_sized_new(MAX_PACKET_LENGTH); |
d116e813 | 65 | gdbserver_state.last_packet = g_byte_array_sized_new(MAX_PACKET_LENGTH + 4); |
ecd39d62 ML |
66 | |
67 | /* | |
3b7a9388 AB |
68 | * What single-step modes are supported is accelerator dependent. |
69 | * By default try to use no IRQs and no timers while single | |
70 | * stepping so as to make single stepping like a typical ICE HW step. | |
ecd39d62 | 71 | */ |
3b7a9388 | 72 | gdbserver_state.supported_sstep_flags = accel_supported_gdbstub_sstep_flags(); |
12bc5b4c ML |
73 | gdbserver_state.sstep_flags = SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER; |
74 | gdbserver_state.sstep_flags &= gdbserver_state.supported_sstep_flags; | |
8d98c445 AB |
75 | } |
76 | ||
9005774b | 77 | /* writes 2*len+1 bytes in buf */ |
36e067b2 | 78 | void gdb_memtohex(GString *buf, const uint8_t *mem, int len) |
b4608c04 FB |
79 | { |
80 | int i, c; | |
b4608c04 FB |
81 | for(i = 0; i < len; i++) { |
82 | c = mem[i]; | |
308f9e88 AB |
83 | g_string_append_c(buf, tohex(c >> 4)); |
84 | g_string_append_c(buf, tohex(c & 0xf)); | |
b4608c04 | 85 | } |
308f9e88 | 86 | g_string_append_c(buf, '\0'); |
b4608c04 FB |
87 | } |
88 | ||
36e067b2 | 89 | void gdb_hextomem(GByteArray *mem, const char *buf, int len) |
b4608c04 FB |
90 | { |
91 | int i; | |
92 | ||
93 | for(i = 0; i < len; i++) { | |
4a25f1b9 AB |
94 | guint8 byte = fromhex(buf[0]) << 4 | fromhex(buf[1]); |
95 | g_byte_array_append(mem, &byte, 1); | |
b4608c04 FB |
96 | buf += 2; |
97 | } | |
98 | } | |
99 | ||
5c9522b3 DG |
100 | static void hexdump(const char *buf, int len, |
101 | void (*trace_fn)(size_t ofs, char const *text)) | |
102 | { | |
103 | char line_buffer[3 * 16 + 4 + 16 + 1]; | |
104 | ||
105 | size_t i; | |
106 | for (i = 0; i < len || (i & 0xF); ++i) { | |
107 | size_t byte_ofs = i & 15; | |
108 | ||
109 | if (byte_ofs == 0) { | |
110 | memset(line_buffer, ' ', 3 * 16 + 4 + 16); | |
111 | line_buffer[3 * 16 + 4 + 16] = 0; | |
112 | } | |
113 | ||
114 | size_t col_group = (i >> 2) & 3; | |
115 | size_t hex_col = byte_ofs * 3 + col_group; | |
116 | size_t txt_col = 3 * 16 + 4 + byte_ofs; | |
117 | ||
118 | if (i < len) { | |
119 | char value = buf[i]; | |
120 | ||
121 | line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF); | |
122 | line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF); | |
123 | line_buffer[txt_col + 0] = (value >= ' ' && value < 127) | |
124 | ? value | |
125 | : '.'; | |
126 | } | |
127 | ||
128 | if (byte_ofs == 0xF) | |
129 | trace_fn(i & -16, line_buffer); | |
130 | } | |
131 | } | |
132 | ||
b4608c04 | 133 | /* return -1 if error, 0 if OK */ |
36e067b2 | 134 | int gdb_put_packet_binary(const char *buf, int len, bool dump) |
b4608c04 | 135 | { |
56aebc89 | 136 | int csum, i; |
d116e813 | 137 | uint8_t footer[3]; |
b4608c04 | 138 | |
5c9522b3 DG |
139 | if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) { |
140 | hexdump(buf, len, trace_gdbstub_io_binaryreply); | |
141 | } | |
142 | ||
b4608c04 | 143 | for(;;) { |
d116e813 DH |
144 | g_byte_array_set_size(gdbserver_state.last_packet, 0); |
145 | g_byte_array_append(gdbserver_state.last_packet, | |
146 | (const uint8_t *) "$", 1); | |
147 | g_byte_array_append(gdbserver_state.last_packet, | |
148 | (const uint8_t *) buf, len); | |
b4608c04 FB |
149 | csum = 0; |
150 | for(i = 0; i < len; i++) { | |
151 | csum += buf[i]; | |
152 | } | |
d116e813 DH |
153 | footer[0] = '#'; |
154 | footer[1] = tohex((csum >> 4) & 0xf); | |
155 | footer[2] = tohex((csum) & 0xf); | |
156 | g_byte_array_append(gdbserver_state.last_packet, footer, 3); | |
b4608c04 | 157 | |
36e067b2 | 158 | gdb_put_buffer(gdbserver_state.last_packet->data, |
d116e813 | 159 | gdbserver_state.last_packet->len); |
b4608c04 | 160 | |
a7e0f9bd | 161 | if (gdb_got_immediate_ack()) { |
b4608c04 | 162 | break; |
a7e0f9bd | 163 | } |
b4608c04 FB |
164 | } |
165 | return 0; | |
166 | } | |
167 | ||
56aebc89 | 168 | /* return -1 if error, 0 if OK */ |
36e067b2 | 169 | int gdb_put_packet(const char *buf) |
56aebc89 | 170 | { |
5c9522b3 | 171 | trace_gdbstub_io_reply(buf); |
79808573 | 172 | |
36e067b2 | 173 | return gdb_put_packet_binary(buf, strlen(buf), false); |
56aebc89 PB |
174 | } |
175 | ||
36e067b2 | 176 | void gdb_put_strbuf(void) |
308f9e88 | 177 | { |
36e067b2 | 178 | gdb_put_packet(gdbserver_state.str_buf->str); |
308f9e88 AB |
179 | } |
180 | ||
56aebc89 | 181 | /* Encode data using the encoding for 'x' packets. */ |
36e067b2 | 182 | void gdb_memtox(GString *buf, const char *mem, int len) |
56aebc89 | 183 | { |
56aebc89 PB |
184 | char c; |
185 | ||
186 | while (len--) { | |
187 | c = *(mem++); | |
188 | switch (c) { | |
189 | case '#': case '$': case '*': case '}': | |
308f9e88 AB |
190 | g_string_append_c(buf, '}'); |
191 | g_string_append_c(buf, c ^ 0x20); | |
56aebc89 PB |
192 | break; |
193 | default: | |
308f9e88 | 194 | g_string_append_c(buf, c); |
56aebc89 PB |
195 | break; |
196 | } | |
197 | } | |
56aebc89 | 198 | } |
f1ccf904 | 199 | |
a346af3e | 200 | static uint32_t gdb_get_cpu_pid(CPUState *cpu) |
1a227336 | 201 | { |
dc14a7a6 IL |
202 | #ifdef CONFIG_USER_ONLY |
203 | return getpid(); | |
204 | #else | |
46f5abc0 | 205 | if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) { |
1a227336 | 206 | /* Return the default process' PID */ |
a346af3e AB |
207 | int index = gdbserver_state.process_num - 1; |
208 | return gdbserver_state.processes[index].pid; | |
1a227336 | 209 | } |
46f5abc0 | 210 | return cpu->cluster_index + 1; |
dc14a7a6 | 211 | #endif |
1a227336 LM |
212 | } |
213 | ||
a3fcc111 | 214 | GDBProcess *gdb_get_process(uint32_t pid) |
7d8c87da LM |
215 | { |
216 | int i; | |
217 | ||
218 | if (!pid) { | |
219 | /* 0 means any process, we take the first one */ | |
a346af3e | 220 | return &gdbserver_state.processes[0]; |
7d8c87da LM |
221 | } |
222 | ||
a346af3e AB |
223 | for (i = 0; i < gdbserver_state.process_num; i++) { |
224 | if (gdbserver_state.processes[i].pid == pid) { | |
225 | return &gdbserver_state.processes[i]; | |
7d8c87da LM |
226 | } |
227 | } | |
228 | ||
229 | return NULL; | |
230 | } | |
231 | ||
a346af3e | 232 | static GDBProcess *gdb_get_cpu_process(CPUState *cpu) |
7d8c87da | 233 | { |
a346af3e | 234 | return gdb_get_process(gdb_get_cpu_pid(cpu)); |
7d8c87da LM |
235 | } |
236 | ||
237 | static CPUState *find_cpu(uint32_t thread_id) | |
238 | { | |
239 | CPUState *cpu; | |
240 | ||
241 | CPU_FOREACH(cpu) { | |
36e067b2 | 242 | if (gdb_get_cpu_index(cpu) == thread_id) { |
7d8c87da LM |
243 | return cpu; |
244 | } | |
245 | } | |
246 | ||
247 | return NULL; | |
248 | } | |
249 | ||
a3fcc111 | 250 | CPUState *gdb_get_first_cpu_in_process(GDBProcess *process) |
e40e5204 LM |
251 | { |
252 | CPUState *cpu; | |
253 | ||
254 | CPU_FOREACH(cpu) { | |
a346af3e | 255 | if (gdb_get_cpu_pid(cpu) == process->pid) { |
e40e5204 LM |
256 | return cpu; |
257 | } | |
258 | } | |
259 | ||
260 | return NULL; | |
261 | } | |
262 | ||
a346af3e | 263 | static CPUState *gdb_next_cpu_in_process(CPUState *cpu) |
e40e5204 | 264 | { |
a346af3e | 265 | uint32_t pid = gdb_get_cpu_pid(cpu); |
e40e5204 LM |
266 | cpu = CPU_NEXT(cpu); |
267 | ||
268 | while (cpu) { | |
a346af3e | 269 | if (gdb_get_cpu_pid(cpu) == pid) { |
e40e5204 LM |
270 | break; |
271 | } | |
272 | ||
273 | cpu = CPU_NEXT(cpu); | |
274 | } | |
275 | ||
276 | return cpu; | |
277 | } | |
278 | ||
e40e5204 | 279 | /* Return the cpu following @cpu, while ignoring unattached processes. */ |
a346af3e | 280 | static CPUState *gdb_next_attached_cpu(CPUState *cpu) |
e40e5204 LM |
281 | { |
282 | cpu = CPU_NEXT(cpu); | |
283 | ||
284 | while (cpu) { | |
a346af3e | 285 | if (gdb_get_cpu_process(cpu)->attached) { |
e40e5204 LM |
286 | break; |
287 | } | |
288 | ||
289 | cpu = CPU_NEXT(cpu); | |
290 | } | |
291 | ||
292 | return cpu; | |
293 | } | |
294 | ||
295 | /* Return the first attached cpu */ | |
36e067b2 | 296 | CPUState *gdb_first_attached_cpu(void) |
e40e5204 LM |
297 | { |
298 | CPUState *cpu = first_cpu; | |
a346af3e | 299 | GDBProcess *process = gdb_get_cpu_process(cpu); |
e40e5204 LM |
300 | |
301 | if (!process->attached) { | |
a346af3e | 302 | return gdb_next_attached_cpu(cpu); |
e40e5204 LM |
303 | } |
304 | ||
305 | return cpu; | |
306 | } | |
307 | ||
a346af3e | 308 | static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid) |
ab65eed3 LM |
309 | { |
310 | GDBProcess *process; | |
311 | CPUState *cpu; | |
312 | ||
313 | if (!pid && !tid) { | |
314 | /* 0 means any process/thread, we take the first attached one */ | |
a346af3e | 315 | return gdb_first_attached_cpu(); |
ab65eed3 LM |
316 | } else if (pid && !tid) { |
317 | /* any thread in a specific process */ | |
a346af3e | 318 | process = gdb_get_process(pid); |
ab65eed3 LM |
319 | |
320 | if (process == NULL) { | |
321 | return NULL; | |
322 | } | |
323 | ||
324 | if (!process->attached) { | |
325 | return NULL; | |
326 | } | |
327 | ||
a3fcc111 | 328 | return gdb_get_first_cpu_in_process(process); |
ab65eed3 LM |
329 | } else { |
330 | /* a specific thread */ | |
331 | cpu = find_cpu(tid); | |
332 | ||
333 | if (cpu == NULL) { | |
334 | return NULL; | |
335 | } | |
336 | ||
a346af3e | 337 | process = gdb_get_cpu_process(cpu); |
ab65eed3 LM |
338 | |
339 | if (pid && process->pid != pid) { | |
340 | return NULL; | |
341 | } | |
342 | ||
343 | if (!process->attached) { | |
344 | return NULL; | |
345 | } | |
346 | ||
347 | return cpu; | |
348 | } | |
349 | } | |
350 | ||
a346af3e AB |
351 | static const char *get_feature_xml(const char *p, const char **newp, |
352 | GDBProcess *process) | |
56aebc89 | 353 | { |
a3fcc111 | 354 | CPUState *cpu = gdb_get_first_cpu_in_process(process); |
c145eeae | 355 | CPUClass *cc = CPU_GET_CLASS(cpu); |
56e534bd | 356 | size_t len; |
56aebc89 | 357 | |
56e534bd AB |
358 | /* |
359 | * qXfer:features:read:ANNEX:OFFSET,LENGTH' | |
360 | * ^p ^newp | |
361 | */ | |
362 | char *term = strchr(p, ':'); | |
363 | *newp = term + 1; | |
364 | len = term - p; | |
56aebc89 | 365 | |
56e534bd | 366 | /* Is it the main target xml? */ |
56aebc89 | 367 | if (strncmp(p, "target.xml", len) == 0) { |
56e534bd | 368 | if (!process->target_xml) { |
56aebc89 | 369 | GDBRegisterState *r; |
6d8f77a6 | 370 | g_autoptr(GPtrArray) xml = g_ptr_array_new_with_free_func(g_free); |
56e534bd | 371 | |
6d8f77a6 AO |
372 | g_ptr_array_add( |
373 | xml, | |
374 | g_strdup("<?xml version=\"1.0\"?>" | |
375 | "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">" | |
376 | "<target>")); | |
56aebc89 | 377 | |
b3820e6c | 378 | if (cc->gdb_arch_name) { |
6d8f77a6 AO |
379 | g_ptr_array_add( |
380 | xml, | |
381 | g_markup_printf_escaped("<architecture>%s</architecture>", | |
382 | cc->gdb_arch_name(cpu))); | |
b3820e6c | 383 | } |
6d8f77a6 AO |
384 | g_ptr_array_add( |
385 | xml, | |
386 | g_markup_printf_escaped("<xi:include href=\"%s\"/>", | |
387 | cc->gdb_core_xml_file)); | |
dbd6623c AO |
388 | if (cpu->gdb_regs) { |
389 | for (guint i = 0; i < cpu->gdb_regs->len; i++) { | |
390 | r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); | |
391 | g_ptr_array_add( | |
392 | xml, | |
393 | g_markup_printf_escaped("<xi:include href=\"%s\"/>", | |
394 | r->xml)); | |
395 | } | |
56aebc89 | 396 | } |
6d8f77a6 AO |
397 | g_ptr_array_add(xml, g_strdup("</target>")); |
398 | g_ptr_array_add(xml, NULL); | |
56e534bd | 399 | |
6d8f77a6 | 400 | process->target_xml = g_strjoinv(NULL, (void *)xml->pdata); |
56aebc89 | 401 | } |
5d1ab242 | 402 | return process->target_xml; |
56aebc89 | 403 | } |
56e534bd | 404 | /* Is it dynamically generated by the target? */ |
200bf5b7 | 405 | if (cc->gdb_get_dynamic_xml) { |
56e534bd | 406 | g_autofree char *xmlname = g_strndup(p, len); |
200bf5b7 | 407 | const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname); |
200bf5b7 AB |
408 | if (xml) { |
409 | return xml; | |
410 | } | |
411 | } | |
56e534bd | 412 | /* Is it one of the encoded gdb-xml/ files? */ |
956af7da AO |
413 | for (int i = 0; gdb_static_features[i].xmlname; i++) { |
414 | const char *name = gdb_static_features[i].xmlname; | |
56e534bd AB |
415 | if ((strncmp(name, p, len) == 0) && |
416 | strlen(name) == len) { | |
956af7da | 417 | return gdb_static_features[i].xml; |
56e534bd | 418 | } |
56aebc89 | 419 | } |
56e534bd AB |
420 | |
421 | /* failed */ | |
422 | return NULL; | |
56aebc89 | 423 | } |
f1ccf904 | 424 | |
e84f4524 AO |
425 | void gdb_feature_builder_init(GDBFeatureBuilder *builder, GDBFeature *feature, |
426 | const char *name, const char *xmlname, | |
427 | int base_reg) | |
428 | { | |
429 | char *header = g_markup_printf_escaped( | |
430 | "<?xml version=\"1.0\"?>" | |
431 | "<!DOCTYPE feature SYSTEM \"gdb-target.dtd\">" | |
432 | "<feature name=\"%s\">", | |
433 | name); | |
434 | ||
435 | builder->feature = feature; | |
436 | builder->xml = g_ptr_array_new(); | |
437 | g_ptr_array_add(builder->xml, header); | |
438 | builder->base_reg = base_reg; | |
439 | feature->xmlname = xmlname; | |
440 | feature->num_regs = 0; | |
441 | } | |
442 | ||
443 | void gdb_feature_builder_append_tag(const GDBFeatureBuilder *builder, | |
444 | const char *format, ...) | |
445 | { | |
446 | va_list ap; | |
447 | va_start(ap, format); | |
448 | g_ptr_array_add(builder->xml, g_markup_vprintf_escaped(format, ap)); | |
449 | va_end(ap); | |
450 | } | |
451 | ||
452 | void gdb_feature_builder_append_reg(const GDBFeatureBuilder *builder, | |
453 | const char *name, | |
454 | int bitsize, | |
455 | int regnum, | |
456 | const char *type, | |
457 | const char *group) | |
458 | { | |
459 | if (builder->feature->num_regs < regnum) { | |
460 | builder->feature->num_regs = regnum; | |
461 | } | |
462 | ||
463 | if (group) { | |
464 | gdb_feature_builder_append_tag( | |
465 | builder, | |
466 | "<reg name=\"%s\" bitsize=\"%d\" regnum=\"%d\" type=\"%s\" group=\"%s\"/>", | |
467 | name, bitsize, builder->base_reg + regnum, type, group); | |
468 | } else { | |
469 | gdb_feature_builder_append_tag( | |
470 | builder, | |
471 | "<reg name=\"%s\" bitsize=\"%d\" regnum=\"%d\" type=\"%s\"/>", | |
472 | name, bitsize, builder->base_reg + regnum, type); | |
473 | } | |
474 | } | |
475 | ||
476 | void gdb_feature_builder_end(const GDBFeatureBuilder *builder) | |
477 | { | |
478 | g_ptr_array_add(builder->xml, (void *)"</feature>"); | |
479 | g_ptr_array_add(builder->xml, NULL); | |
480 | ||
481 | builder->feature->xml = g_strjoinv(NULL, (void *)builder->xml->pdata); | |
482 | ||
483 | for (guint i = 0; i < builder->xml->len - 2; i++) { | |
484 | g_free(g_ptr_array_index(builder->xml, i)); | |
485 | } | |
486 | ||
487 | g_ptr_array_free(builder->xml, TRUE); | |
488 | } | |
489 | ||
1218b68e AO |
490 | const GDBFeature *gdb_find_static_feature(const char *xmlname) |
491 | { | |
492 | const GDBFeature *feature; | |
493 | ||
494 | for (feature = gdb_static_features; feature->xmlname; feature++) { | |
495 | if (!strcmp(feature->xmlname, xmlname)) { | |
496 | return feature; | |
497 | } | |
498 | } | |
499 | ||
500 | g_assert_not_reached(); | |
501 | } | |
502 | ||
a010bdbe | 503 | static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg) |
56aebc89 | 504 | { |
a0e372f0 | 505 | CPUClass *cc = CPU_GET_CLASS(cpu); |
b77af26e | 506 | CPUArchState *env = cpu_env(cpu); |
56aebc89 | 507 | GDBRegisterState *r; |
f1ccf904 | 508 | |
a0e372f0 | 509 | if (reg < cc->gdb_num_core_regs) { |
a010bdbe | 510 | return cc->gdb_read_register(cpu, buf, reg); |
a0e372f0 | 511 | } |
f1ccf904 | 512 | |
dbd6623c AO |
513 | if (cpu->gdb_regs) { |
514 | for (guint i = 0; i < cpu->gdb_regs->len; i++) { | |
515 | r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); | |
516 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { | |
517 | return r->get_reg(env, buf, reg - r->base_reg); | |
518 | } | |
56aebc89 PB |
519 | } |
520 | } | |
521 | return 0; | |
f1ccf904 TS |
522 | } |
523 | ||
385b9f0e | 524 | static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg) |
f1ccf904 | 525 | { |
a0e372f0 | 526 | CPUClass *cc = CPU_GET_CLASS(cpu); |
b77af26e | 527 | CPUArchState *env = cpu_env(cpu); |
56aebc89 | 528 | GDBRegisterState *r; |
f1ccf904 | 529 | |
a0e372f0 | 530 | if (reg < cc->gdb_num_core_regs) { |
5b50e790 | 531 | return cc->gdb_write_register(cpu, mem_buf, reg); |
a0e372f0 | 532 | } |
56aebc89 | 533 | |
dbd6623c AO |
534 | if (cpu->gdb_regs) { |
535 | for (guint i = 0; i < cpu->gdb_regs->len; i++) { | |
536 | r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); | |
537 | if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) { | |
538 | return r->set_reg(env, mem_buf, reg - r->base_reg); | |
539 | } | |
56aebc89 PB |
540 | } |
541 | } | |
6da41eaf FB |
542 | return 0; |
543 | } | |
544 | ||
22169d41 | 545 | void gdb_register_coprocessor(CPUState *cpu, |
a010bdbe | 546 | gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg, |
22169d41 | 547 | int num_regs, const char *xml, int g_pos) |
6da41eaf | 548 | { |
56aebc89 | 549 | GDBRegisterState *s; |
73c392c2 AO |
550 | guint i; |
551 | ||
552 | if (cpu->gdb_regs) { | |
553 | for (i = 0; i < cpu->gdb_regs->len; i++) { | |
554 | /* Check for duplicates. */ | |
555 | s = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); | |
556 | if (strcmp(s->xml, xml) == 0) { | |
557 | return; | |
558 | } | |
559 | } | |
560 | } else { | |
561 | cpu->gdb_regs = g_array_new(false, false, sizeof(GDBRegisterState)); | |
562 | i = 0; | |
56aebc89 | 563 | } |
9643c25f | 564 | |
73c392c2 AO |
565 | g_array_set_size(cpu->gdb_regs, i + 1); |
566 | s = &g_array_index(cpu->gdb_regs, GDBRegisterState, i); | |
a0e372f0 | 567 | s->base_reg = cpu->gdb_num_regs; |
9643c25f SW |
568 | s->num_regs = num_regs; |
569 | s->get_reg = get_reg; | |
570 | s->set_reg = set_reg; | |
571 | s->xml = xml; | |
572 | ||
56aebc89 | 573 | /* Add to end of list. */ |
a0e372f0 | 574 | cpu->gdb_num_regs += num_regs; |
56aebc89 PB |
575 | if (g_pos) { |
576 | if (g_pos != s->base_reg) { | |
7ae6c571 ZY |
577 | error_report("Error: Bad gdb register numbering for '%s', " |
578 | "expected %d got %d", xml, g_pos, s->base_reg); | |
35143f01 AF |
579 | } else { |
580 | cpu->gdb_num_g_regs = cpu->gdb_num_regs; | |
56aebc89 PB |
581 | } |
582 | } | |
6da41eaf FB |
583 | } |
584 | ||
a346af3e | 585 | static void gdb_process_breakpoint_remove_all(GDBProcess *p) |
546f3c67 | 586 | { |
a3fcc111 | 587 | CPUState *cpu = gdb_get_first_cpu_in_process(p); |
546f3c67 LM |
588 | |
589 | while (cpu) { | |
ae7467b1 | 590 | gdb_breakpoint_remove_all(cpu); |
a346af3e | 591 | cpu = gdb_next_cpu_in_process(cpu); |
546f3c67 LM |
592 | } |
593 | } | |
594 | ||
a1d1bb31 | 595 | |
b428ad12 | 596 | static void gdb_set_cpu_pc(vaddr pc) |
fab9d284 | 597 | { |
a346af3e | 598 | CPUState *cpu = gdbserver_state.c_cpu; |
f45748f1 AF |
599 | |
600 | cpu_synchronize_state(cpu); | |
4a2b24ed | 601 | cpu_set_pc(cpu, pc); |
fab9d284 AJ |
602 | } |
603 | ||
36e067b2 | 604 | void gdb_append_thread_id(CPUState *cpu, GString *buf) |
1a227336 | 605 | { |
a346af3e | 606 | if (gdbserver_state.multiprocess) { |
308f9e88 | 607 | g_string_append_printf(buf, "p%02x.%02x", |
36e067b2 | 608 | gdb_get_cpu_pid(cpu), gdb_get_cpu_index(cpu)); |
1a227336 | 609 | } else { |
36e067b2 | 610 | g_string_append_printf(buf, "%02x", gdb_get_cpu_index(cpu)); |
1a227336 | 611 | } |
1a227336 LM |
612 | } |
613 | ||
7d8c87da LM |
614 | static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf, |
615 | uint32_t *pid, uint32_t *tid) | |
616 | { | |
617 | unsigned long p, t; | |
618 | int ret; | |
619 | ||
620 | if (*buf == 'p') { | |
621 | buf++; | |
622 | ret = qemu_strtoul(buf, &buf, 16, &p); | |
623 | ||
624 | if (ret) { | |
625 | return GDB_READ_THREAD_ERR; | |
626 | } | |
627 | ||
628 | /* Skip '.' */ | |
629 | buf++; | |
630 | } else { | |
6c78de6e | 631 | p = 0; |
7d8c87da LM |
632 | } |
633 | ||
634 | ret = qemu_strtoul(buf, &buf, 16, &t); | |
635 | ||
636 | if (ret) { | |
637 | return GDB_READ_THREAD_ERR; | |
638 | } | |
639 | ||
640 | *end_buf = buf; | |
641 | ||
642 | if (p == -1) { | |
643 | return GDB_ALL_PROCESSES; | |
644 | } | |
645 | ||
646 | if (pid) { | |
647 | *pid = p; | |
648 | } | |
649 | ||
650 | if (t == -1) { | |
651 | return GDB_ALL_THREADS; | |
652 | } | |
653 | ||
654 | if (tid) { | |
655 | *tid = t; | |
656 | } | |
657 | ||
658 | return GDB_ONE_THREAD; | |
659 | } | |
660 | ||
544177ad CI |
661 | /** |
662 | * gdb_handle_vcont - Parses and handles a vCont packet. | |
663 | * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is | |
664 | * a format error, 0 on success. | |
665 | */ | |
a346af3e | 666 | static int gdb_handle_vcont(const char *p) |
544177ad | 667 | { |
e40e5204 | 668 | int res, signal = 0; |
544177ad | 669 | char cur_action; |
544177ad | 670 | unsigned long tmp; |
e40e5204 LM |
671 | uint32_t pid, tid; |
672 | GDBProcess *process; | |
544177ad | 673 | CPUState *cpu; |
c99ef792 | 674 | GDBThreadIdKind kind; |
7ea0c33d | 675 | unsigned int max_cpus = gdb_get_max_cpus(); |
544177ad | 676 | /* uninitialised CPUs stay 0 */ |
2261b73c | 677 | g_autofree char *newstates = g_new0(char, max_cpus); |
544177ad CI |
678 | |
679 | /* mark valid CPUs with 1 */ | |
680 | CPU_FOREACH(cpu) { | |
681 | newstates[cpu->cpu_index] = 1; | |
682 | } | |
683 | ||
684 | /* | |
685 | * res keeps track of what error we are returning, with -ENOTSUP meaning | |
686 | * that the command is unknown or unsupported, thus returning an empty | |
687 | * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid, | |
688 | * or incorrect parameters passed. | |
689 | */ | |
690 | res = 0; | |
761e3c10 MBB |
691 | |
692 | /* | |
693 | * target_count and last_target keep track of how many CPUs we are going to | |
694 | * step or resume, and a pointer to the state structure of one of them, | |
695 | * respectivelly | |
696 | */ | |
697 | int target_count = 0; | |
698 | CPUState *last_target = NULL; | |
699 | ||
544177ad CI |
700 | while (*p) { |
701 | if (*p++ != ';') { | |
2261b73c | 702 | return -ENOTSUP; |
544177ad CI |
703 | } |
704 | ||
705 | cur_action = *p++; | |
706 | if (cur_action == 'C' || cur_action == 'S') { | |
95a5befc | 707 | cur_action = qemu_tolower(cur_action); |
3ddd9036 | 708 | res = qemu_strtoul(p, &p, 16, &tmp); |
544177ad | 709 | if (res) { |
2261b73c | 710 | return res; |
544177ad CI |
711 | } |
712 | signal = gdb_signal_to_target(tmp); | |
713 | } else if (cur_action != 'c' && cur_action != 's') { | |
714 | /* unknown/invalid/unsupported command */ | |
2261b73c | 715 | return -ENOTSUP; |
544177ad | 716 | } |
e40e5204 | 717 | |
c99ef792 LM |
718 | if (*p == '\0' || *p == ';') { |
719 | /* | |
720 | * No thread specifier, action is on "all threads". The | |
721 | * specification is unclear regarding the process to act on. We | |
722 | * choose all processes. | |
723 | */ | |
724 | kind = GDB_ALL_PROCESSES; | |
725 | } else if (*p++ == ':') { | |
726 | kind = read_thread_id(p, &p, &pid, &tid); | |
727 | } else { | |
2261b73c | 728 | return -ENOTSUP; |
e40e5204 LM |
729 | } |
730 | ||
c99ef792 | 731 | switch (kind) { |
e40e5204 | 732 | case GDB_READ_THREAD_ERR: |
2261b73c | 733 | return -EINVAL; |
e40e5204 LM |
734 | |
735 | case GDB_ALL_PROCESSES: | |
a346af3e | 736 | cpu = gdb_first_attached_cpu(); |
e40e5204 LM |
737 | while (cpu) { |
738 | if (newstates[cpu->cpu_index] == 1) { | |
739 | newstates[cpu->cpu_index] = cur_action; | |
761e3c10 MBB |
740 | |
741 | target_count++; | |
742 | last_target = cpu; | |
544177ad | 743 | } |
e40e5204 | 744 | |
a346af3e | 745 | cpu = gdb_next_attached_cpu(cpu); |
544177ad | 746 | } |
e40e5204 LM |
747 | break; |
748 | ||
749 | case GDB_ALL_THREADS: | |
a346af3e | 750 | process = gdb_get_process(pid); |
e40e5204 LM |
751 | |
752 | if (!process->attached) { | |
2261b73c | 753 | return -EINVAL; |
544177ad | 754 | } |
5a6a1ad1 | 755 | |
a3fcc111 | 756 | cpu = gdb_get_first_cpu_in_process(process); |
e40e5204 LM |
757 | while (cpu) { |
758 | if (newstates[cpu->cpu_index] == 1) { | |
759 | newstates[cpu->cpu_index] = cur_action; | |
761e3c10 MBB |
760 | |
761 | target_count++; | |
762 | last_target = cpu; | |
e40e5204 LM |
763 | } |
764 | ||
a346af3e | 765 | cpu = gdb_next_cpu_in_process(cpu); |
e40e5204 LM |
766 | } |
767 | break; | |
768 | ||
769 | case GDB_ONE_THREAD: | |
a346af3e | 770 | cpu = gdb_get_cpu(pid, tid); |
544177ad | 771 | |
544177ad | 772 | /* invalid CPU/thread specified */ |
5a6a1ad1 | 773 | if (!cpu) { |
2261b73c | 774 | return -EINVAL; |
544177ad | 775 | } |
5a6a1ad1 | 776 | |
544177ad CI |
777 | /* only use if no previous match occourred */ |
778 | if (newstates[cpu->cpu_index] == 1) { | |
779 | newstates[cpu->cpu_index] = cur_action; | |
761e3c10 MBB |
780 | |
781 | target_count++; | |
782 | last_target = cpu; | |
544177ad | 783 | } |
e40e5204 | 784 | break; |
544177ad CI |
785 | } |
786 | } | |
2261b73c | 787 | |
761e3c10 MBB |
788 | /* |
789 | * if we're about to resume a specific set of CPUs/threads, make it so that | |
790 | * in case execution gets interrupted, we can send GDB a stop reply with a | |
791 | * correct value. it doesn't really matter which CPU we tell GDB the signal | |
792 | * happened in (VM pauses stop all of them anyway), so long as it is one of | |
793 | * the ones we resumed/single stepped here. | |
794 | */ | |
795 | if (target_count > 0) { | |
796 | gdbserver_state.c_cpu = last_target; | |
797 | } | |
798 | ||
a346af3e AB |
799 | gdbserver_state.signal = signal; |
800 | gdb_continue_partial(newstates); | |
544177ad CI |
801 | return res; |
802 | } | |
803 | ||
d14055dc JD |
804 | static const char *cmd_next_param(const char *param, const char delimiter) |
805 | { | |
806 | static const char all_delimiters[] = ",;:="; | |
807 | char curr_delimiters[2] = {0}; | |
808 | const char *delimiters; | |
809 | ||
810 | if (delimiter == '?') { | |
811 | delimiters = all_delimiters; | |
812 | } else if (delimiter == '0') { | |
813 | return strchr(param, '\0'); | |
814 | } else if (delimiter == '.' && *param) { | |
815 | return param + 1; | |
816 | } else { | |
817 | curr_delimiters[0] = delimiter; | |
818 | delimiters = curr_delimiters; | |
819 | } | |
820 | ||
821 | param += strcspn(param, delimiters); | |
822 | if (*param) { | |
823 | param++; | |
824 | } | |
825 | return param; | |
826 | } | |
827 | ||
828 | static int cmd_parse_params(const char *data, const char *schema, | |
26a16181 | 829 | GArray *params) |
d14055dc | 830 | { |
d14055dc JD |
831 | const char *curr_schema, *curr_data; |
832 | ||
26a16181 AB |
833 | g_assert(schema); |
834 | g_assert(params->len == 0); | |
d14055dc JD |
835 | |
836 | curr_schema = schema; | |
d14055dc JD |
837 | curr_data = data; |
838 | while (curr_schema[0] && curr_schema[1] && *curr_data) { | |
26a16181 AB |
839 | GdbCmdVariant this_param; |
840 | ||
d14055dc JD |
841 | switch (curr_schema[0]) { |
842 | case 'l': | |
843 | if (qemu_strtoul(curr_data, &curr_data, 16, | |
26a16181 | 844 | &this_param.val_ul)) { |
d14055dc JD |
845 | return -EINVAL; |
846 | } | |
d14055dc | 847 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
26a16181 | 848 | g_array_append_val(params, this_param); |
d14055dc JD |
849 | break; |
850 | case 'L': | |
851 | if (qemu_strtou64(curr_data, &curr_data, 16, | |
26a16181 | 852 | (uint64_t *)&this_param.val_ull)) { |
d14055dc JD |
853 | return -EINVAL; |
854 | } | |
d14055dc | 855 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
26a16181 | 856 | g_array_append_val(params, this_param); |
d14055dc JD |
857 | break; |
858 | case 's': | |
26a16181 | 859 | this_param.data = curr_data; |
d14055dc | 860 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
26a16181 | 861 | g_array_append_val(params, this_param); |
d14055dc JD |
862 | break; |
863 | case 'o': | |
26a16181 | 864 | this_param.opcode = *(uint8_t *)curr_data; |
d14055dc | 865 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
26a16181 | 866 | g_array_append_val(params, this_param); |
d14055dc JD |
867 | break; |
868 | case 't': | |
26a16181 | 869 | this_param.thread_id.kind = |
d14055dc | 870 | read_thread_id(curr_data, &curr_data, |
26a16181 AB |
871 | &this_param.thread_id.pid, |
872 | &this_param.thread_id.tid); | |
d14055dc | 873 | curr_data = cmd_next_param(curr_data, curr_schema[1]); |
26a16181 | 874 | g_array_append_val(params, this_param); |
d14055dc JD |
875 | break; |
876 | case '?': | |
877 | curr_data = cmd_next_param(curr_data, curr_schema[1]); | |
878 | break; | |
879 | default: | |
880 | return -EINVAL; | |
881 | } | |
882 | curr_schema += 2; | |
883 | } | |
884 | ||
d14055dc JD |
885 | return 0; |
886 | } | |
887 | ||
26a16181 | 888 | typedef void (*GdbCmdHandler)(GArray *params, void *user_ctx); |
d14055dc JD |
889 | |
890 | /* | |
891 | * cmd_startswith -> cmd is compared using startswith | |
892 | * | |
75837005 MTB |
893 | * allow_stop_reply -> true iff the gdbstub can respond to this command with a |
894 | * "stop reply" packet. The list of commands that accept such response is | |
895 | * defined at the GDB Remote Serial Protocol documentation. see: | |
896 | * https://sourceware.org/gdb/onlinedocs/gdb/Stop-Reply-Packets.html#Stop-Reply-Packets. | |
d14055dc JD |
897 | * |
898 | * schema definitions: | |
899 | * Each schema parameter entry consists of 2 chars, | |
900 | * the first char represents the parameter type handling | |
901 | * the second char represents the delimiter for the next parameter | |
902 | * | |
903 | * Currently supported schema types: | |
904 | * 'l' -> unsigned long (stored in .val_ul) | |
905 | * 'L' -> unsigned long long (stored in .val_ull) | |
906 | * 's' -> string (stored in .data) | |
907 | * 'o' -> single char (stored in .opcode) | |
908 | * 't' -> thread id (stored in .thread_id) | |
909 | * '?' -> skip according to delimiter | |
910 | * | |
911 | * Currently supported delimiters: | |
912 | * '?' -> Stop at any delimiter (",;:=\0") | |
913 | * '0' -> Stop at "\0" | |
914 | * '.' -> Skip 1 char unless reached "\0" | |
915 | * Any other value is treated as the delimiter value itself | |
916 | */ | |
917 | typedef struct GdbCmdParseEntry { | |
918 | GdbCmdHandler handler; | |
919 | const char *cmd; | |
920 | bool cmd_startswith; | |
921 | const char *schema; | |
75837005 | 922 | bool allow_stop_reply; |
d14055dc JD |
923 | } GdbCmdParseEntry; |
924 | ||
925 | static inline int startswith(const char *string, const char *pattern) | |
926 | { | |
927 | return !strncmp(string, pattern, strlen(pattern)); | |
928 | } | |
929 | ||
5b030993 | 930 | static int process_string_cmd(const char *data, |
d14055dc JD |
931 | const GdbCmdParseEntry *cmds, int num_cmds) |
932 | { | |
26a16181 AB |
933 | int i; |
934 | g_autoptr(GArray) params = g_array_new(false, true, sizeof(GdbCmdVariant)); | |
d14055dc JD |
935 | |
936 | if (!cmds) { | |
937 | return -1; | |
938 | } | |
939 | ||
940 | for (i = 0; i < num_cmds; i++) { | |
941 | const GdbCmdParseEntry *cmd = &cmds[i]; | |
942 | g_assert(cmd->handler && cmd->cmd); | |
943 | ||
944 | if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) || | |
945 | (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) { | |
946 | continue; | |
947 | } | |
948 | ||
949 | if (cmd->schema) { | |
26a16181 AB |
950 | if (cmd_parse_params(&data[strlen(cmd->cmd)], |
951 | cmd->schema, params)) { | |
952 | return -1; | |
d14055dc | 953 | } |
d14055dc JD |
954 | } |
955 | ||
75837005 | 956 | gdbserver_state.allow_stop_reply = cmd->allow_stop_reply; |
5b030993 | 957 | cmd->handler(params, NULL); |
d14055dc JD |
958 | return 0; |
959 | } | |
960 | ||
961 | return -1; | |
962 | } | |
963 | ||
a346af3e | 964 | static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd) |
3e2c1261 JD |
965 | { |
966 | if (!data) { | |
967 | return; | |
968 | } | |
969 | ||
308f9e88 | 970 | g_string_set_size(gdbserver_state.str_buf, 0); |
4a25f1b9 | 971 | g_byte_array_set_size(gdbserver_state.mem_buf, 0); |
308f9e88 | 972 | |
3e2c1261 JD |
973 | /* In case there was an error during the command parsing we must |
974 | * send a NULL packet to indicate the command is not supported */ | |
5b030993 | 975 | if (process_string_cmd(data, cmd, 1)) { |
36e067b2 | 976 | gdb_put_packet(""); |
3e2c1261 JD |
977 | } |
978 | } | |
979 | ||
26a16181 | 980 | static void handle_detach(GArray *params, void *user_ctx) |
3e2c1261 JD |
981 | { |
982 | GDBProcess *process; | |
3e2c1261 JD |
983 | uint32_t pid = 1; |
984 | ||
a346af3e | 985 | if (gdbserver_state.multiprocess) { |
26a16181 | 986 | if (!params->len) { |
36e067b2 | 987 | gdb_put_packet("E22"); |
3e2c1261 JD |
988 | return; |
989 | } | |
990 | ||
26a16181 | 991 | pid = get_param(params, 0)->val_ul; |
3e2c1261 JD |
992 | } |
993 | ||
a346af3e AB |
994 | process = gdb_get_process(pid); |
995 | gdb_process_breakpoint_remove_all(process); | |
3e2c1261 JD |
996 | process->attached = false; |
997 | ||
a346af3e AB |
998 | if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) { |
999 | gdbserver_state.c_cpu = gdb_first_attached_cpu(); | |
3e2c1261 JD |
1000 | } |
1001 | ||
a346af3e AB |
1002 | if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) { |
1003 | gdbserver_state.g_cpu = gdb_first_attached_cpu(); | |
3e2c1261 JD |
1004 | } |
1005 | ||
a346af3e | 1006 | if (!gdbserver_state.c_cpu) { |
3e2c1261 | 1007 | /* No more process attached */ |
c566080c | 1008 | gdb_disable_syscalls(); |
a346af3e | 1009 | gdb_continue(); |
3e2c1261 | 1010 | } |
36e067b2 | 1011 | gdb_put_packet("OK"); |
3e2c1261 JD |
1012 | } |
1013 | ||
26a16181 | 1014 | static void handle_thread_alive(GArray *params, void *user_ctx) |
44ffded0 JD |
1015 | { |
1016 | CPUState *cpu; | |
1017 | ||
26a16181 | 1018 | if (!params->len) { |
36e067b2 | 1019 | gdb_put_packet("E22"); |
44ffded0 JD |
1020 | return; |
1021 | } | |
1022 | ||
26a16181 | 1023 | if (get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) { |
36e067b2 | 1024 | gdb_put_packet("E22"); |
44ffded0 JD |
1025 | return; |
1026 | } | |
1027 | ||
26a16181 AB |
1028 | cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid, |
1029 | get_param(params, 0)->thread_id.tid); | |
44ffded0 | 1030 | if (!cpu) { |
36e067b2 | 1031 | gdb_put_packet("E22"); |
44ffded0 JD |
1032 | return; |
1033 | } | |
1034 | ||
36e067b2 | 1035 | gdb_put_packet("OK"); |
44ffded0 JD |
1036 | } |
1037 | ||
26a16181 | 1038 | static void handle_continue(GArray *params, void *user_ctx) |
4d6e3fe2 | 1039 | { |
26a16181 AB |
1040 | if (params->len) { |
1041 | gdb_set_cpu_pc(get_param(params, 0)->val_ull); | |
4d6e3fe2 JD |
1042 | } |
1043 | ||
a346af3e AB |
1044 | gdbserver_state.signal = 0; |
1045 | gdb_continue(); | |
4d6e3fe2 JD |
1046 | } |
1047 | ||
26a16181 | 1048 | static void handle_cont_with_sig(GArray *params, void *user_ctx) |
ccc47d5d JD |
1049 | { |
1050 | unsigned long signal = 0; | |
1051 | ||
1052 | /* | |
1053 | * Note: C sig;[addr] is currently unsupported and we simply | |
1054 | * omit the addr parameter | |
1055 | */ | |
26a16181 AB |
1056 | if (params->len) { |
1057 | signal = get_param(params, 0)->val_ul; | |
ccc47d5d JD |
1058 | } |
1059 | ||
a346af3e AB |
1060 | gdbserver_state.signal = gdb_signal_to_target(signal); |
1061 | if (gdbserver_state.signal == -1) { | |
1062 | gdbserver_state.signal = 0; | |
ccc47d5d | 1063 | } |
a346af3e | 1064 | gdb_continue(); |
ccc47d5d JD |
1065 | } |
1066 | ||
26a16181 | 1067 | static void handle_set_thread(GArray *params, void *user_ctx) |
3a9651d6 JD |
1068 | { |
1069 | CPUState *cpu; | |
1070 | ||
26a16181 | 1071 | if (params->len != 2) { |
36e067b2 | 1072 | gdb_put_packet("E22"); |
3a9651d6 JD |
1073 | return; |
1074 | } | |
1075 | ||
26a16181 | 1076 | if (get_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) { |
36e067b2 | 1077 | gdb_put_packet("E22"); |
3a9651d6 JD |
1078 | return; |
1079 | } | |
1080 | ||
26a16181 | 1081 | if (get_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) { |
36e067b2 | 1082 | gdb_put_packet("OK"); |
3a9651d6 JD |
1083 | return; |
1084 | } | |
1085 | ||
26a16181 AB |
1086 | cpu = gdb_get_cpu(get_param(params, 1)->thread_id.pid, |
1087 | get_param(params, 1)->thread_id.tid); | |
3a9651d6 | 1088 | if (!cpu) { |
36e067b2 | 1089 | gdb_put_packet("E22"); |
3a9651d6 JD |
1090 | return; |
1091 | } | |
1092 | ||
1093 | /* | |
1094 | * Note: This command is deprecated and modern gdb's will be using the | |
1095 | * vCont command instead. | |
1096 | */ | |
26a16181 | 1097 | switch (get_param(params, 0)->opcode) { |
3a9651d6 | 1098 | case 'c': |
a346af3e | 1099 | gdbserver_state.c_cpu = cpu; |
36e067b2 | 1100 | gdb_put_packet("OK"); |
3a9651d6 JD |
1101 | break; |
1102 | case 'g': | |
a346af3e | 1103 | gdbserver_state.g_cpu = cpu; |
36e067b2 | 1104 | gdb_put_packet("OK"); |
3a9651d6 JD |
1105 | break; |
1106 | default: | |
36e067b2 | 1107 | gdb_put_packet("E22"); |
3a9651d6 JD |
1108 | break; |
1109 | } | |
1110 | } | |
1111 | ||
26a16181 | 1112 | static void handle_insert_bp(GArray *params, void *user_ctx) |
77f6ce50 JD |
1113 | { |
1114 | int res; | |
1115 | ||
26a16181 | 1116 | if (params->len != 3) { |
36e067b2 | 1117 | gdb_put_packet("E22"); |
77f6ce50 JD |
1118 | return; |
1119 | } | |
1120 | ||
ae7467b1 AB |
1121 | res = gdb_breakpoint_insert(gdbserver_state.c_cpu, |
1122 | get_param(params, 0)->val_ul, | |
26a16181 AB |
1123 | get_param(params, 1)->val_ull, |
1124 | get_param(params, 2)->val_ull); | |
77f6ce50 | 1125 | if (res >= 0) { |
36e067b2 | 1126 | gdb_put_packet("OK"); |
77f6ce50 JD |
1127 | return; |
1128 | } else if (res == -ENOSYS) { | |
36e067b2 | 1129 | gdb_put_packet(""); |
77f6ce50 JD |
1130 | return; |
1131 | } | |
1132 | ||
36e067b2 | 1133 | gdb_put_packet("E22"); |
77f6ce50 JD |
1134 | } |
1135 | ||
26a16181 | 1136 | static void handle_remove_bp(GArray *params, void *user_ctx) |
77f6ce50 JD |
1137 | { |
1138 | int res; | |
1139 | ||
26a16181 | 1140 | if (params->len != 3) { |
36e067b2 | 1141 | gdb_put_packet("E22"); |
77f6ce50 JD |
1142 | return; |
1143 | } | |
1144 | ||
ae7467b1 AB |
1145 | res = gdb_breakpoint_remove(gdbserver_state.c_cpu, |
1146 | get_param(params, 0)->val_ul, | |
26a16181 AB |
1147 | get_param(params, 1)->val_ull, |
1148 | get_param(params, 2)->val_ull); | |
77f6ce50 | 1149 | if (res >= 0) { |
36e067b2 | 1150 | gdb_put_packet("OK"); |
77f6ce50 JD |
1151 | return; |
1152 | } else if (res == -ENOSYS) { | |
36e067b2 | 1153 | gdb_put_packet(""); |
77f6ce50 JD |
1154 | return; |
1155 | } | |
1156 | ||
36e067b2 | 1157 | gdb_put_packet("E22"); |
77f6ce50 JD |
1158 | } |
1159 | ||
94b2a62b AB |
1160 | /* |
1161 | * handle_set/get_reg | |
1162 | * | |
1163 | * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available. | |
1164 | * This works, but can be very slow. Anything new enough to understand | |
1165 | * XML also knows how to use this properly. However to use this we | |
1166 | * need to define a local XML file as well as be talking to a | |
1167 | * reasonably modern gdb. Responding with an empty packet will cause | |
1168 | * the remote gdb to fallback to older methods. | |
1169 | */ | |
1170 | ||
26a16181 | 1171 | static void handle_set_reg(GArray *params, void *user_ctx) |
62b3320b JD |
1172 | { |
1173 | int reg_size; | |
1174 | ||
26a16181 | 1175 | if (params->len != 2) { |
36e067b2 | 1176 | gdb_put_packet("E22"); |
62b3320b JD |
1177 | return; |
1178 | } | |
1179 | ||
26a16181 | 1180 | reg_size = strlen(get_param(params, 1)->data) / 2; |
36e067b2 | 1181 | gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 1)->data, reg_size); |
4a25f1b9 | 1182 | gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data, |
26a16181 | 1183 | get_param(params, 0)->val_ull); |
36e067b2 | 1184 | gdb_put_packet("OK"); |
62b3320b JD |
1185 | } |
1186 | ||
26a16181 | 1187 | static void handle_get_reg(GArray *params, void *user_ctx) |
5d0e57bd JD |
1188 | { |
1189 | int reg_size; | |
1190 | ||
26a16181 | 1191 | if (!params->len) { |
36e067b2 | 1192 | gdb_put_packet("E14"); |
5d0e57bd JD |
1193 | return; |
1194 | } | |
1195 | ||
4a25f1b9 | 1196 | reg_size = gdb_read_register(gdbserver_state.g_cpu, |
a010bdbe | 1197 | gdbserver_state.mem_buf, |
26a16181 | 1198 | get_param(params, 0)->val_ull); |
5d0e57bd | 1199 | if (!reg_size) { |
36e067b2 | 1200 | gdb_put_packet("E14"); |
5d0e57bd | 1201 | return; |
4a25f1b9 AB |
1202 | } else { |
1203 | g_byte_array_set_size(gdbserver_state.mem_buf, reg_size); | |
5d0e57bd JD |
1204 | } |
1205 | ||
36e067b2 AB |
1206 | gdb_memtohex(gdbserver_state.str_buf, |
1207 | gdbserver_state.mem_buf->data, reg_size); | |
1208 | gdb_put_strbuf(); | |
5d0e57bd JD |
1209 | } |
1210 | ||
26a16181 | 1211 | static void handle_write_mem(GArray *params, void *user_ctx) |
cc0ecc78 | 1212 | { |
26a16181 | 1213 | if (params->len != 3) { |
36e067b2 | 1214 | gdb_put_packet("E22"); |
cc0ecc78 JD |
1215 | return; |
1216 | } | |
1217 | ||
36e067b2 | 1218 | /* gdb_hextomem() reads 2*len bytes */ |
26a16181 AB |
1219 | if (get_param(params, 1)->val_ull > |
1220 | strlen(get_param(params, 2)->data) / 2) { | |
36e067b2 | 1221 | gdb_put_packet("E22"); |
cc0ecc78 JD |
1222 | return; |
1223 | } | |
1224 | ||
36e067b2 | 1225 | gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 2)->data, |
589a5867 AB |
1226 | get_param(params, 1)->val_ull); |
1227 | if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu, | |
1228 | get_param(params, 0)->val_ull, | |
1229 | gdbserver_state.mem_buf->data, | |
1230 | gdbserver_state.mem_buf->len, true)) { | |
36e067b2 | 1231 | gdb_put_packet("E14"); |
cc0ecc78 JD |
1232 | return; |
1233 | } | |
1234 | ||
36e067b2 | 1235 | gdb_put_packet("OK"); |
cc0ecc78 JD |
1236 | } |
1237 | ||
26a16181 | 1238 | static void handle_read_mem(GArray *params, void *user_ctx) |
da92e236 | 1239 | { |
26a16181 | 1240 | if (params->len != 2) { |
36e067b2 | 1241 | gdb_put_packet("E22"); |
da92e236 JD |
1242 | return; |
1243 | } | |
1244 | ||
36e067b2 | 1245 | /* gdb_memtohex() doubles the required space */ |
26a16181 | 1246 | if (get_param(params, 1)->val_ull > MAX_PACKET_LENGTH / 2) { |
36e067b2 | 1247 | gdb_put_packet("E22"); |
da92e236 JD |
1248 | return; |
1249 | } | |
1250 | ||
26a16181 AB |
1251 | g_byte_array_set_size(gdbserver_state.mem_buf, |
1252 | get_param(params, 1)->val_ull); | |
4a25f1b9 | 1253 | |
589a5867 AB |
1254 | if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu, |
1255 | get_param(params, 0)->val_ull, | |
1256 | gdbserver_state.mem_buf->data, | |
1257 | gdbserver_state.mem_buf->len, false)) { | |
36e067b2 | 1258 | gdb_put_packet("E14"); |
da92e236 JD |
1259 | return; |
1260 | } | |
1261 | ||
36e067b2 | 1262 | gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, |
4a25f1b9 | 1263 | gdbserver_state.mem_buf->len); |
36e067b2 | 1264 | gdb_put_strbuf(); |
da92e236 JD |
1265 | } |
1266 | ||
26a16181 | 1267 | static void handle_write_all_regs(GArray *params, void *user_ctx) |
287ca120 | 1268 | { |
379b42e8 AB |
1269 | int reg_id; |
1270 | size_t len; | |
287ca120 JD |
1271 | uint8_t *registers; |
1272 | int reg_size; | |
1273 | ||
26a16181 | 1274 | if (!params->len) { |
287ca120 JD |
1275 | return; |
1276 | } | |
1277 | ||
a346af3e | 1278 | cpu_synchronize_state(gdbserver_state.g_cpu); |
26a16181 | 1279 | len = strlen(get_param(params, 0)->data) / 2; |
36e067b2 | 1280 | gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len); |
4a25f1b9 | 1281 | registers = gdbserver_state.mem_buf->data; |
379b42e8 AB |
1282 | for (reg_id = 0; |
1283 | reg_id < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0; | |
1284 | reg_id++) { | |
1285 | reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, reg_id); | |
287ca120 JD |
1286 | len -= reg_size; |
1287 | registers += reg_size; | |
1288 | } | |
36e067b2 | 1289 | gdb_put_packet("OK"); |
287ca120 JD |
1290 | } |
1291 | ||
26a16181 | 1292 | static void handle_read_all_regs(GArray *params, void *user_ctx) |
397d1370 | 1293 | { |
379b42e8 AB |
1294 | int reg_id; |
1295 | size_t len; | |
397d1370 | 1296 | |
a346af3e | 1297 | cpu_synchronize_state(gdbserver_state.g_cpu); |
a010bdbe | 1298 | g_byte_array_set_size(gdbserver_state.mem_buf, 0); |
397d1370 | 1299 | len = 0; |
379b42e8 | 1300 | for (reg_id = 0; reg_id < gdbserver_state.g_cpu->gdb_num_g_regs; reg_id++) { |
4a25f1b9 | 1301 | len += gdb_read_register(gdbserver_state.g_cpu, |
a010bdbe | 1302 | gdbserver_state.mem_buf, |
379b42e8 | 1303 | reg_id); |
397d1370 | 1304 | } |
a010bdbe | 1305 | g_assert(len == gdbserver_state.mem_buf->len); |
397d1370 | 1306 | |
36e067b2 AB |
1307 | gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len); |
1308 | gdb_put_strbuf(); | |
397d1370 JD |
1309 | } |
1310 | ||
4b20fab1 | 1311 | |
26a16181 | 1312 | static void handle_step(GArray *params, void *user_ctx) |
933f80dd | 1313 | { |
26a16181 | 1314 | if (params->len) { |
b428ad12 | 1315 | gdb_set_cpu_pc(get_param(params, 0)->val_ull); |
933f80dd JD |
1316 | } |
1317 | ||
ecd39d62 | 1318 | cpu_single_step(gdbserver_state.c_cpu, gdbserver_state.sstep_flags); |
a346af3e | 1319 | gdb_continue(); |
933f80dd JD |
1320 | } |
1321 | ||
26a16181 | 1322 | static void handle_backward(GArray *params, void *user_ctx) |
fda8458b | 1323 | { |
505601d5 | 1324 | if (!gdb_can_reverse()) { |
36e067b2 | 1325 | gdb_put_packet("E22"); |
fda8458b | 1326 | } |
26a16181 AB |
1327 | if (params->len == 1) { |
1328 | switch (get_param(params, 0)->opcode) { | |
fda8458b PD |
1329 | case 's': |
1330 | if (replay_reverse_step()) { | |
1331 | gdb_continue(); | |
1332 | } else { | |
36e067b2 | 1333 | gdb_put_packet("E14"); |
fda8458b PD |
1334 | } |
1335 | return; | |
cda38259 PD |
1336 | case 'c': |
1337 | if (replay_reverse_continue()) { | |
1338 | gdb_continue(); | |
1339 | } else { | |
36e067b2 | 1340 | gdb_put_packet("E14"); |
cda38259 PD |
1341 | } |
1342 | return; | |
fda8458b PD |
1343 | } |
1344 | } | |
1345 | ||
1346 | /* Default invalid command */ | |
36e067b2 | 1347 | gdb_put_packet(""); |
fda8458b PD |
1348 | } |
1349 | ||
26a16181 | 1350 | static void handle_v_cont_query(GArray *params, void *user_ctx) |
8536ec02 | 1351 | { |
36e067b2 | 1352 | gdb_put_packet("vCont;c;C;s;S"); |
8536ec02 JD |
1353 | } |
1354 | ||
26a16181 | 1355 | static void handle_v_cont(GArray *params, void *user_ctx) |
8536ec02 JD |
1356 | { |
1357 | int res; | |
1358 | ||
26a16181 | 1359 | if (!params->len) { |
8536ec02 JD |
1360 | return; |
1361 | } | |
1362 | ||
26a16181 | 1363 | res = gdb_handle_vcont(get_param(params, 0)->data); |
8536ec02 | 1364 | if ((res == -EINVAL) || (res == -ERANGE)) { |
36e067b2 | 1365 | gdb_put_packet("E22"); |
8536ec02 | 1366 | } else if (res) { |
36e067b2 | 1367 | gdb_put_packet(""); |
8536ec02 JD |
1368 | } |
1369 | } | |
1370 | ||
26a16181 | 1371 | static void handle_v_attach(GArray *params, void *user_ctx) |
8536ec02 JD |
1372 | { |
1373 | GDBProcess *process; | |
1374 | CPUState *cpu; | |
8536ec02 | 1375 | |
308f9e88 | 1376 | g_string_assign(gdbserver_state.str_buf, "E22"); |
26a16181 | 1377 | if (!params->len) { |
8536ec02 JD |
1378 | goto cleanup; |
1379 | } | |
1380 | ||
26a16181 | 1381 | process = gdb_get_process(get_param(params, 0)->val_ul); |
8536ec02 JD |
1382 | if (!process) { |
1383 | goto cleanup; | |
1384 | } | |
1385 | ||
a3fcc111 | 1386 | cpu = gdb_get_first_cpu_in_process(process); |
8536ec02 JD |
1387 | if (!cpu) { |
1388 | goto cleanup; | |
1389 | } | |
1390 | ||
1391 | process->attached = true; | |
a346af3e AB |
1392 | gdbserver_state.g_cpu = cpu; |
1393 | gdbserver_state.c_cpu = cpu; | |
8536ec02 | 1394 | |
75837005 MTB |
1395 | if (gdbserver_state.allow_stop_reply) { |
1396 | g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP); | |
1397 | gdb_append_thread_id(cpu, gdbserver_state.str_buf); | |
1398 | g_string_append_c(gdbserver_state.str_buf, ';'); | |
1399 | gdbserver_state.allow_stop_reply = false; | |
8536ec02 | 1400 | cleanup: |
75837005 MTB |
1401 | gdb_put_strbuf(); |
1402 | } | |
8536ec02 JD |
1403 | } |
1404 | ||
26a16181 | 1405 | static void handle_v_kill(GArray *params, void *user_ctx) |
8536ec02 JD |
1406 | { |
1407 | /* Kill the target */ | |
36e067b2 | 1408 | gdb_put_packet("OK"); |
8536ec02 | 1409 | error_report("QEMU: Terminated via GDBstub"); |
b9e10c6c | 1410 | gdb_exit(0); |
e216256a | 1411 | gdb_qemu_exit(0); |
8536ec02 JD |
1412 | } |
1413 | ||
305bea06 | 1414 | static const GdbCmdParseEntry gdb_v_commands_table[] = { |
8536ec02 JD |
1415 | /* Order is important if has same prefix */ |
1416 | { | |
1417 | .handler = handle_v_cont_query, | |
1418 | .cmd = "Cont?", | |
1419 | .cmd_startswith = 1 | |
1420 | }, | |
1421 | { | |
1422 | .handler = handle_v_cont, | |
1423 | .cmd = "Cont", | |
1424 | .cmd_startswith = 1, | |
75837005 | 1425 | .allow_stop_reply = true, |
8536ec02 JD |
1426 | .schema = "s0" |
1427 | }, | |
1428 | { | |
1429 | .handler = handle_v_attach, | |
1430 | .cmd = "Attach;", | |
1431 | .cmd_startswith = 1, | |
75837005 | 1432 | .allow_stop_reply = true, |
8536ec02 JD |
1433 | .schema = "l0" |
1434 | }, | |
1435 | { | |
1436 | .handler = handle_v_kill, | |
1437 | .cmd = "Kill;", | |
1438 | .cmd_startswith = 1 | |
1439 | }, | |
e282010b IL |
1440 | #ifdef CONFIG_USER_ONLY |
1441 | /* | |
1442 | * Host I/O Packets. See [1] for details. | |
1443 | * [1] https://sourceware.org/gdb/onlinedocs/gdb/Host-I_002fO-Packets.html | |
1444 | */ | |
1445 | { | |
1446 | .handler = gdb_handle_v_file_open, | |
1447 | .cmd = "File:open:", | |
1448 | .cmd_startswith = 1, | |
1449 | .schema = "s,L,L0" | |
1450 | }, | |
1451 | { | |
1452 | .handler = gdb_handle_v_file_close, | |
1453 | .cmd = "File:close:", | |
1454 | .cmd_startswith = 1, | |
1455 | .schema = "l0" | |
1456 | }, | |
1457 | { | |
1458 | .handler = gdb_handle_v_file_pread, | |
1459 | .cmd = "File:pread:", | |
1460 | .cmd_startswith = 1, | |
1461 | .schema = "l,L,L0" | |
1462 | }, | |
1463 | { | |
1464 | .handler = gdb_handle_v_file_readlink, | |
1465 | .cmd = "File:readlink:", | |
1466 | .cmd_startswith = 1, | |
1467 | .schema = "s0" | |
1468 | }, | |
1469 | #endif | |
8536ec02 JD |
1470 | }; |
1471 | ||
26a16181 | 1472 | static void handle_v_commands(GArray *params, void *user_ctx) |
8536ec02 | 1473 | { |
26a16181 | 1474 | if (!params->len) { |
8536ec02 JD |
1475 | return; |
1476 | } | |
1477 | ||
5b030993 | 1478 | if (process_string_cmd(get_param(params, 0)->data, |
8536ec02 JD |
1479 | gdb_v_commands_table, |
1480 | ARRAY_SIZE(gdb_v_commands_table))) { | |
36e067b2 | 1481 | gdb_put_packet(""); |
8536ec02 JD |
1482 | } |
1483 | } | |
1484 | ||
26a16181 | 1485 | static void handle_query_qemu_sstepbits(GArray *params, void *user_ctx) |
2704efad | 1486 | { |
ecd39d62 ML |
1487 | g_string_printf(gdbserver_state.str_buf, "ENABLE=%x", SSTEP_ENABLE); |
1488 | ||
1489 | if (gdbserver_state.supported_sstep_flags & SSTEP_NOIRQ) { | |
1490 | g_string_append_printf(gdbserver_state.str_buf, ",NOIRQ=%x", | |
1491 | SSTEP_NOIRQ); | |
1492 | } | |
1493 | ||
1494 | if (gdbserver_state.supported_sstep_flags & SSTEP_NOTIMER) { | |
1495 | g_string_append_printf(gdbserver_state.str_buf, ",NOTIMER=%x", | |
1496 | SSTEP_NOTIMER); | |
1497 | } | |
1498 | ||
36e067b2 | 1499 | gdb_put_strbuf(); |
2704efad JD |
1500 | } |
1501 | ||
26a16181 | 1502 | static void handle_set_qemu_sstep(GArray *params, void *user_ctx) |
2704efad | 1503 | { |
ecd39d62 ML |
1504 | int new_sstep_flags; |
1505 | ||
26a16181 | 1506 | if (!params->len) { |
2704efad JD |
1507 | return; |
1508 | } | |
1509 | ||
ecd39d62 ML |
1510 | new_sstep_flags = get_param(params, 0)->val_ul; |
1511 | ||
1512 | if (new_sstep_flags & ~gdbserver_state.supported_sstep_flags) { | |
36e067b2 | 1513 | gdb_put_packet("E22"); |
ecd39d62 ML |
1514 | return; |
1515 | } | |
1516 | ||
1517 | gdbserver_state.sstep_flags = new_sstep_flags; | |
36e067b2 | 1518 | gdb_put_packet("OK"); |
2704efad JD |
1519 | } |
1520 | ||
26a16181 | 1521 | static void handle_query_qemu_sstep(GArray *params, void *user_ctx) |
2704efad | 1522 | { |
ecd39d62 ML |
1523 | g_string_printf(gdbserver_state.str_buf, "0x%x", |
1524 | gdbserver_state.sstep_flags); | |
36e067b2 | 1525 | gdb_put_strbuf(); |
2704efad JD |
1526 | } |
1527 | ||
26a16181 | 1528 | static void handle_query_curr_tid(GArray *params, void *user_ctx) |
b4608c04 | 1529 | { |
2e0f2cfb | 1530 | CPUState *cpu; |
2704efad | 1531 | GDBProcess *process; |
2704efad JD |
1532 | |
1533 | /* | |
1534 | * "Current thread" remains vague in the spec, so always return | |
1535 | * the first thread of the current process (gdb returns the | |
1536 | * first thread). | |
1537 | */ | |
a346af3e | 1538 | process = gdb_get_cpu_process(gdbserver_state.g_cpu); |
a3fcc111 | 1539 | cpu = gdb_get_first_cpu_in_process(process); |
308f9e88 AB |
1540 | g_string_assign(gdbserver_state.str_buf, "QC"); |
1541 | gdb_append_thread_id(cpu, gdbserver_state.str_buf); | |
36e067b2 | 1542 | gdb_put_strbuf(); |
2704efad JD |
1543 | } |
1544 | ||
26a16181 | 1545 | static void handle_query_threads(GArray *params, void *user_ctx) |
2704efad | 1546 | { |
a346af3e | 1547 | if (!gdbserver_state.query_cpu) { |
36e067b2 | 1548 | gdb_put_packet("l"); |
2704efad JD |
1549 | return; |
1550 | } | |
1551 | ||
308f9e88 AB |
1552 | g_string_assign(gdbserver_state.str_buf, "m"); |
1553 | gdb_append_thread_id(gdbserver_state.query_cpu, gdbserver_state.str_buf); | |
36e067b2 | 1554 | gdb_put_strbuf(); |
a346af3e | 1555 | gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu); |
2704efad JD |
1556 | } |
1557 | ||
26a16181 | 1558 | static void handle_query_first_threads(GArray *params, void *user_ctx) |
2704efad | 1559 | { |
a346af3e | 1560 | gdbserver_state.query_cpu = gdb_first_attached_cpu(); |
26a16181 | 1561 | handle_query_threads(params, user_ctx); |
2704efad JD |
1562 | } |
1563 | ||
26a16181 | 1564 | static void handle_query_thread_extra(GArray *params, void *user_ctx) |
2704efad | 1565 | { |
308f9e88 | 1566 | g_autoptr(GString) rs = g_string_new(NULL); |
2704efad | 1567 | CPUState *cpu; |
2704efad | 1568 | |
26a16181 AB |
1569 | if (!params->len || |
1570 | get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) { | |
36e067b2 | 1571 | gdb_put_packet("E22"); |
2704efad JD |
1572 | return; |
1573 | } | |
1574 | ||
26a16181 AB |
1575 | cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid, |
1576 | get_param(params, 0)->thread_id.tid); | |
2704efad JD |
1577 | if (!cpu) { |
1578 | return; | |
1579 | } | |
1580 | ||
1581 | cpu_synchronize_state(cpu); | |
1582 | ||
a346af3e | 1583 | if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) { |
2704efad JD |
1584 | /* Print the CPU model and name in multiprocess mode */ |
1585 | ObjectClass *oc = object_get_class(OBJECT(cpu)); | |
1586 | const char *cpu_model = object_class_get_name(oc); | |
7a309cc9 | 1587 | const char *cpu_name = |
076b2fad | 1588 | object_get_canonical_path_component(OBJECT(cpu)); |
308f9e88 AB |
1589 | g_string_printf(rs, "%s %s [%s]", cpu_model, cpu_name, |
1590 | cpu->halted ? "halted " : "running"); | |
2704efad | 1591 | } else { |
308f9e88 | 1592 | g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index, |
2704efad JD |
1593 | cpu->halted ? "halted " : "running"); |
1594 | } | |
308f9e88 | 1595 | trace_gdbstub_op_extra_info(rs->str); |
36e067b2 AB |
1596 | gdb_memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len); |
1597 | gdb_put_strbuf(); | |
2704efad JD |
1598 | } |
1599 | ||
26a16181 | 1600 | static void handle_query_supported(GArray *params, void *user_ctx) |
2704efad JD |
1601 | { |
1602 | CPUClass *cc; | |
1603 | ||
308f9e88 | 1604 | g_string_printf(gdbserver_state.str_buf, "PacketSize=%x", MAX_PACKET_LENGTH); |
2704efad JD |
1605 | cc = CPU_GET_CLASS(first_cpu); |
1606 | if (cc->gdb_core_xml_file) { | |
308f9e88 | 1607 | g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+"); |
2704efad JD |
1608 | } |
1609 | ||
505601d5 | 1610 | if (gdb_can_reverse()) { |
cda38259 PD |
1611 | g_string_append(gdbserver_state.str_buf, |
1612 | ";ReverseStep+;ReverseContinue+"); | |
fda8458b PD |
1613 | } |
1614 | ||
e282010b IL |
1615 | #if defined(CONFIG_USER_ONLY) |
1616 | #if defined(CONFIG_LINUX) | |
51c623b0 LY |
1617 | if (gdbserver_state.c_cpu->opaque) { |
1618 | g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+"); | |
1619 | } | |
1620 | #endif | |
e282010b IL |
1621 | g_string_append(gdbserver_state.str_buf, ";qXfer:exec-file:read+"); |
1622 | #endif | |
51c623b0 | 1623 | |
26a16181 AB |
1624 | if (params->len && |
1625 | strstr(get_param(params, 0)->data, "multiprocess+")) { | |
a346af3e | 1626 | gdbserver_state.multiprocess = true; |
2704efad JD |
1627 | } |
1628 | ||
3bc2609d | 1629 | g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+"); |
36e067b2 | 1630 | gdb_put_strbuf(); |
2704efad JD |
1631 | } |
1632 | ||
26a16181 | 1633 | static void handle_query_xfer_features(GArray *params, void *user_ctx) |
2704efad | 1634 | { |
c145eeae | 1635 | GDBProcess *process; |
5b24c641 | 1636 | CPUClass *cc; |
2704efad JD |
1637 | unsigned long len, total_len, addr; |
1638 | const char *xml; | |
b4608c04 | 1639 | const char *p; |
2704efad | 1640 | |
26a16181 | 1641 | if (params->len < 3) { |
36e067b2 | 1642 | gdb_put_packet("E22"); |
2704efad JD |
1643 | return; |
1644 | } | |
1645 | ||
a346af3e AB |
1646 | process = gdb_get_cpu_process(gdbserver_state.g_cpu); |
1647 | cc = CPU_GET_CLASS(gdbserver_state.g_cpu); | |
2704efad | 1648 | if (!cc->gdb_core_xml_file) { |
36e067b2 | 1649 | gdb_put_packet(""); |
2704efad JD |
1650 | return; |
1651 | } | |
1652 | ||
26a16181 | 1653 | p = get_param(params, 0)->data; |
a346af3e | 1654 | xml = get_feature_xml(p, &p, process); |
2704efad | 1655 | if (!xml) { |
36e067b2 | 1656 | gdb_put_packet("E00"); |
2704efad JD |
1657 | return; |
1658 | } | |
1659 | ||
26a16181 AB |
1660 | addr = get_param(params, 1)->val_ul; |
1661 | len = get_param(params, 2)->val_ul; | |
2704efad JD |
1662 | total_len = strlen(xml); |
1663 | if (addr > total_len) { | |
36e067b2 | 1664 | gdb_put_packet("E00"); |
2704efad JD |
1665 | return; |
1666 | } | |
1667 | ||
1668 | if (len > (MAX_PACKET_LENGTH - 5) / 2) { | |
1669 | len = (MAX_PACKET_LENGTH - 5) / 2; | |
1670 | } | |
1671 | ||
1672 | if (len < total_len - addr) { | |
308f9e88 | 1673 | g_string_assign(gdbserver_state.str_buf, "m"); |
36e067b2 | 1674 | gdb_memtox(gdbserver_state.str_buf, xml + addr, len); |
2704efad | 1675 | } else { |
308f9e88 | 1676 | g_string_assign(gdbserver_state.str_buf, "l"); |
36e067b2 | 1677 | gdb_memtox(gdbserver_state.str_buf, xml + addr, total_len - addr); |
2704efad JD |
1678 | } |
1679 | ||
36e067b2 | 1680 | gdb_put_packet_binary(gdbserver_state.str_buf->str, |
308f9e88 | 1681 | gdbserver_state.str_buf->len, true); |
2704efad JD |
1682 | } |
1683 | ||
26a16181 | 1684 | static void handle_query_qemu_supported(GArray *params, void *user_ctx) |
2704efad | 1685 | { |
308f9e88 | 1686 | g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep"); |
ab4752ec | 1687 | #ifndef CONFIG_USER_ONLY |
308f9e88 | 1688 | g_string_append(gdbserver_state.str_buf, ";PhyMemMode"); |
ab4752ec | 1689 | #endif |
36e067b2 | 1690 | gdb_put_strbuf(); |
ab4752ec JD |
1691 | } |
1692 | ||
305bea06 | 1693 | static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = { |
2704efad JD |
1694 | /* Order is important if has same prefix */ |
1695 | { | |
1696 | .handler = handle_query_qemu_sstepbits, | |
1697 | .cmd = "qemu.sstepbits", | |
1698 | }, | |
1699 | { | |
1700 | .handler = handle_query_qemu_sstep, | |
1701 | .cmd = "qemu.sstep", | |
1702 | }, | |
1703 | { | |
1704 | .handler = handle_set_qemu_sstep, | |
1705 | .cmd = "qemu.sstep=", | |
1706 | .cmd_startswith = 1, | |
1707 | .schema = "l0" | |
1708 | }, | |
1709 | }; | |
1710 | ||
305bea06 | 1711 | static const GdbCmdParseEntry gdb_gen_query_table[] = { |
2704efad JD |
1712 | { |
1713 | .handler = handle_query_curr_tid, | |
1714 | .cmd = "C", | |
1715 | }, | |
1716 | { | |
1717 | .handler = handle_query_threads, | |
1718 | .cmd = "sThreadInfo", | |
1719 | }, | |
1720 | { | |
1721 | .handler = handle_query_first_threads, | |
1722 | .cmd = "fThreadInfo", | |
1723 | }, | |
1724 | { | |
1725 | .handler = handle_query_thread_extra, | |
1726 | .cmd = "ThreadExtraInfo,", | |
1727 | .cmd_startswith = 1, | |
1728 | .schema = "t0" | |
1729 | }, | |
1730 | #ifdef CONFIG_USER_ONLY | |
1731 | { | |
d96bf49b | 1732 | .handler = gdb_handle_query_offsets, |
2704efad JD |
1733 | .cmd = "Offsets", |
1734 | }, | |
1735 | #else | |
1736 | { | |
b6fa2ec2 | 1737 | .handler = gdb_handle_query_rcmd, |
2704efad JD |
1738 | .cmd = "Rcmd,", |
1739 | .cmd_startswith = 1, | |
1740 | .schema = "s0" | |
1741 | }, | |
1742 | #endif | |
1743 | { | |
1744 | .handler = handle_query_supported, | |
1745 | .cmd = "Supported:", | |
1746 | .cmd_startswith = 1, | |
1747 | .schema = "s0" | |
1748 | }, | |
1749 | { | |
1750 | .handler = handle_query_supported, | |
1751 | .cmd = "Supported", | |
1752 | .schema = "s0" | |
1753 | }, | |
1754 | { | |
1755 | .handler = handle_query_xfer_features, | |
1756 | .cmd = "Xfer:features:read:", | |
1757 | .cmd_startswith = 1, | |
1758 | .schema = "s:l,l0" | |
1759 | }, | |
e282010b IL |
1760 | #if defined(CONFIG_USER_ONLY) |
1761 | #if defined(CONFIG_LINUX) | |
51c623b0 | 1762 | { |
d96bf49b | 1763 | .handler = gdb_handle_query_xfer_auxv, |
51c623b0 LY |
1764 | .cmd = "Xfer:auxv:read::", |
1765 | .cmd_startswith = 1, | |
1766 | .schema = "l,l0" | |
1767 | }, | |
e282010b IL |
1768 | #endif |
1769 | { | |
1770 | .handler = gdb_handle_query_xfer_exec_file, | |
1771 | .cmd = "Xfer:exec-file:read:", | |
1772 | .cmd_startswith = 1, | |
1773 | .schema = "l:l,l0" | |
1774 | }, | |
51c623b0 | 1775 | #endif |
2704efad | 1776 | { |
8a2025b3 | 1777 | .handler = gdb_handle_query_attached, |
2704efad JD |
1778 | .cmd = "Attached:", |
1779 | .cmd_startswith = 1 | |
1780 | }, | |
1781 | { | |
8a2025b3 | 1782 | .handler = gdb_handle_query_attached, |
2704efad JD |
1783 | .cmd = "Attached", |
1784 | }, | |
1785 | { | |
1786 | .handler = handle_query_qemu_supported, | |
1787 | .cmd = "qemu.Supported", | |
1788 | }, | |
ab4752ec JD |
1789 | #ifndef CONFIG_USER_ONLY |
1790 | { | |
589a5867 | 1791 | .handler = gdb_handle_query_qemu_phy_mem_mode, |
ab4752ec JD |
1792 | .cmd = "qemu.PhyMemMode", |
1793 | }, | |
1794 | #endif | |
2704efad JD |
1795 | }; |
1796 | ||
305bea06 | 1797 | static const GdbCmdParseEntry gdb_gen_set_table[] = { |
2704efad JD |
1798 | /* Order is important if has same prefix */ |
1799 | { | |
1800 | .handler = handle_set_qemu_sstep, | |
1801 | .cmd = "qemu.sstep:", | |
1802 | .cmd_startswith = 1, | |
1803 | .schema = "l0" | |
1804 | }, | |
ab4752ec JD |
1805 | #ifndef CONFIG_USER_ONLY |
1806 | { | |
589a5867 | 1807 | .handler = gdb_handle_set_qemu_phy_mem_mode, |
ab4752ec JD |
1808 | .cmd = "qemu.PhyMemMode:", |
1809 | .cmd_startswith = 1, | |
1810 | .schema = "l0" | |
1811 | }, | |
1812 | #endif | |
2704efad JD |
1813 | }; |
1814 | ||
26a16181 | 1815 | static void handle_gen_query(GArray *params, void *user_ctx) |
2704efad | 1816 | { |
26a16181 | 1817 | if (!params->len) { |
2704efad JD |
1818 | return; |
1819 | } | |
1820 | ||
5b030993 | 1821 | if (!process_string_cmd(get_param(params, 0)->data, |
2704efad JD |
1822 | gdb_gen_query_set_common_table, |
1823 | ARRAY_SIZE(gdb_gen_query_set_common_table))) { | |
1824 | return; | |
1825 | } | |
1826 | ||
5b030993 | 1827 | if (process_string_cmd(get_param(params, 0)->data, |
2704efad JD |
1828 | gdb_gen_query_table, |
1829 | ARRAY_SIZE(gdb_gen_query_table))) { | |
36e067b2 | 1830 | gdb_put_packet(""); |
2704efad JD |
1831 | } |
1832 | } | |
1833 | ||
26a16181 | 1834 | static void handle_gen_set(GArray *params, void *user_ctx) |
2704efad | 1835 | { |
26a16181 | 1836 | if (!params->len) { |
2704efad JD |
1837 | return; |
1838 | } | |
1839 | ||
5b030993 | 1840 | if (!process_string_cmd(get_param(params, 0)->data, |
2704efad JD |
1841 | gdb_gen_query_set_common_table, |
1842 | ARRAY_SIZE(gdb_gen_query_set_common_table))) { | |
1843 | return; | |
1844 | } | |
1845 | ||
5b030993 | 1846 | if (process_string_cmd(get_param(params, 0)->data, |
2704efad JD |
1847 | gdb_gen_set_table, |
1848 | ARRAY_SIZE(gdb_gen_set_table))) { | |
36e067b2 | 1849 | gdb_put_packet(""); |
2704efad JD |
1850 | } |
1851 | } | |
1852 | ||
26a16181 | 1853 | static void handle_target_halt(GArray *params, void *user_ctx) |
7009d579 | 1854 | { |
75837005 MTB |
1855 | if (gdbserver_state.allow_stop_reply) { |
1856 | g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP); | |
1857 | gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf); | |
1858 | g_string_append_c(gdbserver_state.str_buf, ';'); | |
1859 | gdb_put_strbuf(); | |
1860 | gdbserver_state.allow_stop_reply = false; | |
1861 | } | |
7009d579 JD |
1862 | /* |
1863 | * Remove all the breakpoints when this query is issued, | |
1864 | * because gdb is doing an initial connect and the state | |
1865 | * should be cleaned up. | |
1866 | */ | |
ae7467b1 | 1867 | gdb_breakpoint_remove_all(gdbserver_state.c_cpu); |
7009d579 JD |
1868 | } |
1869 | ||
a346af3e | 1870 | static int gdb_handle_packet(const char *line_buf) |
2704efad | 1871 | { |
3e2c1261 | 1872 | const GdbCmdParseEntry *cmd_parser = NULL; |
3b46e624 | 1873 | |
5c9522b3 | 1874 | trace_gdbstub_io_command(line_buf); |
118e2268 | 1875 | |
3f1cbac7 | 1876 | switch (line_buf[0]) { |
53fd6554 | 1877 | case '!': |
36e067b2 | 1878 | gdb_put_packet("OK"); |
53fd6554 | 1879 | break; |
858693c6 | 1880 | case '?': |
7009d579 JD |
1881 | { |
1882 | static const GdbCmdParseEntry target_halted_cmd_desc = { | |
1883 | .handler = handle_target_halt, | |
1884 | .cmd = "?", | |
75837005 MTB |
1885 | .cmd_startswith = 1, |
1886 | .allow_stop_reply = true, | |
7009d579 JD |
1887 | }; |
1888 | cmd_parser = &target_halted_cmd_desc; | |
1889 | } | |
858693c6 FB |
1890 | break; |
1891 | case 'c': | |
4d6e3fe2 JD |
1892 | { |
1893 | static const GdbCmdParseEntry continue_cmd_desc = { | |
1894 | .handler = handle_continue, | |
1895 | .cmd = "c", | |
1896 | .cmd_startswith = 1, | |
75837005 | 1897 | .allow_stop_reply = true, |
4d6e3fe2 JD |
1898 | .schema = "L0" |
1899 | }; | |
1900 | cmd_parser = &continue_cmd_desc; | |
858693c6 | 1901 | } |
4d6e3fe2 | 1902 | break; |
1f487ee9 | 1903 | case 'C': |
ccc47d5d JD |
1904 | { |
1905 | static const GdbCmdParseEntry cont_with_sig_cmd_desc = { | |
1906 | .handler = handle_cont_with_sig, | |
1907 | .cmd = "C", | |
1908 | .cmd_startswith = 1, | |
75837005 | 1909 | .allow_stop_reply = true, |
ccc47d5d JD |
1910 | .schema = "l0" |
1911 | }; | |
1912 | cmd_parser = &cont_with_sig_cmd_desc; | |
1913 | } | |
1914 | break; | |
dd32aa10 | 1915 | case 'v': |
8536ec02 JD |
1916 | { |
1917 | static const GdbCmdParseEntry v_cmd_desc = { | |
1918 | .handler = handle_v_commands, | |
1919 | .cmd = "v", | |
1920 | .cmd_startswith = 1, | |
1921 | .schema = "s0" | |
1922 | }; | |
1923 | cmd_parser = &v_cmd_desc; | |
dd32aa10 | 1924 | } |
8536ec02 | 1925 | break; |
7d03f82f EI |
1926 | case 'k': |
1927 | /* Kill the target */ | |
7ae6c571 | 1928 | error_report("QEMU: Terminated via GDBstub"); |
b9e10c6c | 1929 | gdb_exit(0); |
e216256a CC |
1930 | gdb_qemu_exit(0); |
1931 | break; | |
7d03f82f | 1932 | case 'D': |
3e2c1261 JD |
1933 | { |
1934 | static const GdbCmdParseEntry detach_cmd_desc = { | |
1935 | .handler = handle_detach, | |
1936 | .cmd = "D", | |
1937 | .cmd_startswith = 1, | |
1938 | .schema = "?.l0" | |
1939 | }; | |
1940 | cmd_parser = &detach_cmd_desc; | |
546f3c67 | 1941 | } |
7d03f82f | 1942 | break; |
858693c6 | 1943 | case 's': |
933f80dd JD |
1944 | { |
1945 | static const GdbCmdParseEntry step_cmd_desc = { | |
1946 | .handler = handle_step, | |
1947 | .cmd = "s", | |
1948 | .cmd_startswith = 1, | |
75837005 | 1949 | .allow_stop_reply = true, |
933f80dd JD |
1950 | .schema = "L0" |
1951 | }; | |
1952 | cmd_parser = &step_cmd_desc; | |
858693c6 | 1953 | } |
933f80dd | 1954 | break; |
fda8458b PD |
1955 | case 'b': |
1956 | { | |
1957 | static const GdbCmdParseEntry backward_cmd_desc = { | |
1958 | .handler = handle_backward, | |
1959 | .cmd = "b", | |
1960 | .cmd_startswith = 1, | |
3b72d681 | 1961 | .allow_stop_reply = true, |
fda8458b PD |
1962 | .schema = "o0" |
1963 | }; | |
1964 | cmd_parser = &backward_cmd_desc; | |
1965 | } | |
1966 | break; | |
a2d1ebaf PB |
1967 | case 'F': |
1968 | { | |
4b20fab1 | 1969 | static const GdbCmdParseEntry file_io_cmd_desc = { |
c566080c | 1970 | .handler = gdb_handle_file_io, |
4b20fab1 JD |
1971 | .cmd = "F", |
1972 | .cmd_startswith = 1, | |
1973 | .schema = "L,L,o0" | |
1974 | }; | |
1975 | cmd_parser = &file_io_cmd_desc; | |
a2d1ebaf PB |
1976 | } |
1977 | break; | |
858693c6 | 1978 | case 'g': |
397d1370 JD |
1979 | { |
1980 | static const GdbCmdParseEntry read_all_regs_cmd_desc = { | |
1981 | .handler = handle_read_all_regs, | |
1982 | .cmd = "g", | |
1983 | .cmd_startswith = 1 | |
1984 | }; | |
1985 | cmd_parser = &read_all_regs_cmd_desc; | |
56aebc89 | 1986 | } |
858693c6 FB |
1987 | break; |
1988 | case 'G': | |
287ca120 JD |
1989 | { |
1990 | static const GdbCmdParseEntry write_all_regs_cmd_desc = { | |
1991 | .handler = handle_write_all_regs, | |
1992 | .cmd = "G", | |
1993 | .cmd_startswith = 1, | |
1994 | .schema = "s0" | |
1995 | }; | |
1996 | cmd_parser = &write_all_regs_cmd_desc; | |
56aebc89 | 1997 | } |
858693c6 FB |
1998 | break; |
1999 | case 'm': | |
da92e236 JD |
2000 | { |
2001 | static const GdbCmdParseEntry read_mem_cmd_desc = { | |
2002 | .handler = handle_read_mem, | |
2003 | .cmd = "m", | |
2004 | .cmd_startswith = 1, | |
2005 | .schema = "L,L0" | |
2006 | }; | |
2007 | cmd_parser = &read_mem_cmd_desc; | |
6f970bd9 | 2008 | } |
858693c6 FB |
2009 | break; |
2010 | case 'M': | |
cc0ecc78 JD |
2011 | { |
2012 | static const GdbCmdParseEntry write_mem_cmd_desc = { | |
2013 | .handler = handle_write_mem, | |
2014 | .cmd = "M", | |
2015 | .cmd_startswith = 1, | |
2016 | .schema = "L,L:s0" | |
2017 | }; | |
2018 | cmd_parser = &write_mem_cmd_desc; | |
44520db1 | 2019 | } |
858693c6 | 2020 | break; |
56aebc89 | 2021 | case 'p': |
5d0e57bd JD |
2022 | { |
2023 | static const GdbCmdParseEntry get_reg_cmd_desc = { | |
2024 | .handler = handle_get_reg, | |
2025 | .cmd = "p", | |
2026 | .cmd_startswith = 1, | |
2027 | .schema = "L0" | |
2028 | }; | |
2029 | cmd_parser = &get_reg_cmd_desc; | |
56aebc89 PB |
2030 | } |
2031 | break; | |
2032 | case 'P': | |
62b3320b JD |
2033 | { |
2034 | static const GdbCmdParseEntry set_reg_cmd_desc = { | |
2035 | .handler = handle_set_reg, | |
2036 | .cmd = "P", | |
2037 | .cmd_startswith = 1, | |
2038 | .schema = "L?s0" | |
2039 | }; | |
2040 | cmd_parser = &set_reg_cmd_desc; | |
2041 | } | |
56aebc89 | 2042 | break; |
858693c6 | 2043 | case 'Z': |
77f6ce50 JD |
2044 | { |
2045 | static const GdbCmdParseEntry insert_bp_cmd_desc = { | |
2046 | .handler = handle_insert_bp, | |
2047 | .cmd = "Z", | |
2048 | .cmd_startswith = 1, | |
2049 | .schema = "l?L?L0" | |
2050 | }; | |
2051 | cmd_parser = &insert_bp_cmd_desc; | |
2052 | } | |
2053 | break; | |
858693c6 | 2054 | case 'z': |
77f6ce50 JD |
2055 | { |
2056 | static const GdbCmdParseEntry remove_bp_cmd_desc = { | |
2057 | .handler = handle_remove_bp, | |
2058 | .cmd = "z", | |
2059 | .cmd_startswith = 1, | |
2060 | .schema = "l?L?L0" | |
2061 | }; | |
2062 | cmd_parser = &remove_bp_cmd_desc; | |
2063 | } | |
858693c6 | 2064 | break; |
880a7578 | 2065 | case 'H': |
3a9651d6 JD |
2066 | { |
2067 | static const GdbCmdParseEntry set_thread_cmd_desc = { | |
2068 | .handler = handle_set_thread, | |
2069 | .cmd = "H", | |
2070 | .cmd_startswith = 1, | |
2071 | .schema = "o.t0" | |
2072 | }; | |
2073 | cmd_parser = &set_thread_cmd_desc; | |
880a7578 AL |
2074 | } |
2075 | break; | |
2076 | case 'T': | |
44ffded0 JD |
2077 | { |
2078 | static const GdbCmdParseEntry thread_alive_cmd_desc = { | |
2079 | .handler = handle_thread_alive, | |
2080 | .cmd = "T", | |
2081 | .cmd_startswith = 1, | |
2082 | .schema = "t0" | |
2083 | }; | |
2084 | cmd_parser = &thread_alive_cmd_desc; | |
1e9fa730 | 2085 | } |
880a7578 | 2086 | break; |
978efd6a | 2087 | case 'q': |
2704efad JD |
2088 | { |
2089 | static const GdbCmdParseEntry gen_query_cmd_desc = { | |
2090 | .handler = handle_gen_query, | |
2091 | .cmd = "q", | |
2092 | .cmd_startswith = 1, | |
2093 | .schema = "s0" | |
2094 | }; | |
2095 | cmd_parser = &gen_query_cmd_desc; | |
56aebc89 | 2096 | } |
2704efad JD |
2097 | break; |
2098 | case 'Q': | |
2099 | { | |
2100 | static const GdbCmdParseEntry gen_set_cmd_desc = { | |
2101 | .handler = handle_gen_set, | |
2102 | .cmd = "Q", | |
2103 | .cmd_startswith = 1, | |
2104 | .schema = "s0" | |
2105 | }; | |
2106 | cmd_parser = &gen_set_cmd_desc; | |
a3919386 | 2107 | } |
2704efad | 2108 | break; |
858693c6 | 2109 | default: |
858693c6 | 2110 | /* put empty packet */ |
36e067b2 | 2111 | gdb_put_packet(""); |
858693c6 FB |
2112 | break; |
2113 | } | |
3e2c1261 | 2114 | |
2bdec398 | 2115 | if (cmd_parser) { |
a346af3e | 2116 | run_cmd_parser(line_buf, cmd_parser); |
2bdec398 | 2117 | } |
3e2c1261 | 2118 | |
858693c6 FB |
2119 | return RS_IDLE; |
2120 | } | |
2121 | ||
64f6b346 | 2122 | void gdb_set_stop_cpu(CPUState *cpu) |
880a7578 | 2123 | { |
a346af3e | 2124 | GDBProcess *p = gdb_get_cpu_process(cpu); |
160d858d LM |
2125 | |
2126 | if (!p->attached) { | |
2127 | /* | |
2128 | * Having a stop CPU corresponding to a process that is not attached | |
2129 | * confuses GDB. So we ignore the request. | |
2130 | */ | |
2131 | return; | |
2132 | } | |
2133 | ||
8d98c445 AB |
2134 | gdbserver_state.c_cpu = cpu; |
2135 | gdbserver_state.g_cpu = cpu; | |
880a7578 AL |
2136 | } |
2137 | ||
36e067b2 | 2138 | void gdb_read_byte(uint8_t ch) |
858693c6 | 2139 | { |
60fe76f3 | 2140 | uint8_t reply; |
858693c6 | 2141 | |
75837005 | 2142 | gdbserver_state.allow_stop_reply = false; |
1fddef4b | 2143 | #ifndef CONFIG_USER_ONLY |
d116e813 | 2144 | if (gdbserver_state.last_packet->len) { |
4046d913 PB |
2145 | /* Waiting for a response to the last packet. If we see the start |
2146 | of a new command then abandon the previous response. */ | |
2147 | if (ch == '-') { | |
5c9522b3 | 2148 | trace_gdbstub_err_got_nack(); |
36e067b2 | 2149 | gdb_put_buffer(gdbserver_state.last_packet->data, |
d116e813 | 2150 | gdbserver_state.last_packet->len); |
118e2268 | 2151 | } else if (ch == '+') { |
5c9522b3 | 2152 | trace_gdbstub_io_got_ack(); |
118e2268 | 2153 | } else { |
33c846ef | 2154 | trace_gdbstub_io_got_unexpected(ch); |
4046d913 | 2155 | } |
118e2268 | 2156 | |
d116e813 DH |
2157 | if (ch == '+' || ch == '$') { |
2158 | g_byte_array_set_size(gdbserver_state.last_packet, 0); | |
2159 | } | |
4046d913 PB |
2160 | if (ch != '$') |
2161 | return; | |
2162 | } | |
1354869c | 2163 | if (runstate_is_running()) { |
108e8180 NP |
2164 | /* |
2165 | * When the CPU is running, we cannot do anything except stop | |
2166 | * it when receiving a char. This is expected on a Ctrl-C in the | |
2167 | * gdb client. Because we are in all-stop mode, gdb sends a | |
2168 | * 0x03 byte which is not a usual packet, so we handle it specially | |
2169 | * here, but it does expect a stop reply. | |
2170 | */ | |
2171 | if (ch != 0x03) { | |
3869eb7e AB |
2172 | trace_gdbstub_err_unexpected_runpkt(ch); |
2173 | } else { | |
2174 | gdbserver_state.allow_stop_reply = true; | |
108e8180 | 2175 | } |
0461d5a6 | 2176 | vm_stop(RUN_STATE_PAUSED); |
5fafdf24 | 2177 | } else |
1fddef4b | 2178 | #endif |
41625033 | 2179 | { |
a346af3e | 2180 | switch(gdbserver_state.state) { |
858693c6 FB |
2181 | case RS_IDLE: |
2182 | if (ch == '$') { | |
4bf43122 | 2183 | /* start of command packet */ |
a346af3e AB |
2184 | gdbserver_state.line_buf_index = 0; |
2185 | gdbserver_state.line_sum = 0; | |
2186 | gdbserver_state.state = RS_GETLINE; | |
f1b0f894 AB |
2187 | } else if (ch == '+') { |
2188 | /* | |
2189 | * do nothing, gdb may preemptively send out ACKs on | |
2190 | * initial connection | |
2191 | */ | |
4bf43122 | 2192 | } else { |
33c846ef | 2193 | trace_gdbstub_err_garbage(ch); |
c33a346e | 2194 | } |
b4608c04 | 2195 | break; |
858693c6 | 2196 | case RS_GETLINE: |
4bf43122 DG |
2197 | if (ch == '}') { |
2198 | /* start escape sequence */ | |
a346af3e AB |
2199 | gdbserver_state.state = RS_GETLINE_ESC; |
2200 | gdbserver_state.line_sum += ch; | |
4bf43122 DG |
2201 | } else if (ch == '*') { |
2202 | /* start run length encoding sequence */ | |
a346af3e AB |
2203 | gdbserver_state.state = RS_GETLINE_RLE; |
2204 | gdbserver_state.line_sum += ch; | |
4bf43122 DG |
2205 | } else if (ch == '#') { |
2206 | /* end of command, start of checksum*/ | |
a346af3e AB |
2207 | gdbserver_state.state = RS_CHKSUM1; |
2208 | } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) { | |
5c9522b3 | 2209 | trace_gdbstub_err_overrun(); |
a346af3e | 2210 | gdbserver_state.state = RS_IDLE; |
4bf43122 DG |
2211 | } else { |
2212 | /* unescaped command character */ | |
a346af3e AB |
2213 | gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch; |
2214 | gdbserver_state.line_sum += ch; | |
4bf43122 DG |
2215 | } |
2216 | break; | |
2217 | case RS_GETLINE_ESC: | |
858693c6 | 2218 | if (ch == '#') { |
4bf43122 | 2219 | /* unexpected end of command in escape sequence */ |
a346af3e AB |
2220 | gdbserver_state.state = RS_CHKSUM1; |
2221 | } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) { | |
4bf43122 | 2222 | /* command buffer overrun */ |
5c9522b3 | 2223 | trace_gdbstub_err_overrun(); |
a346af3e | 2224 | gdbserver_state.state = RS_IDLE; |
4c3a88a2 | 2225 | } else { |
4bf43122 | 2226 | /* parse escaped character and leave escape state */ |
a346af3e AB |
2227 | gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20; |
2228 | gdbserver_state.line_sum += ch; | |
2229 | gdbserver_state.state = RS_GETLINE; | |
4bf43122 DG |
2230 | } |
2231 | break; | |
2232 | case RS_GETLINE_RLE: | |
046aba16 MA |
2233 | /* |
2234 | * Run-length encoding is explained in "Debugging with GDB / | |
2235 | * Appendix E GDB Remote Serial Protocol / Overview". | |
2236 | */ | |
2237 | if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) { | |
4bf43122 | 2238 | /* invalid RLE count encoding */ |
33c846ef | 2239 | trace_gdbstub_err_invalid_repeat(ch); |
a346af3e | 2240 | gdbserver_state.state = RS_GETLINE; |
4bf43122 DG |
2241 | } else { |
2242 | /* decode repeat length */ | |
33c846ef | 2243 | int repeat = ch - ' ' + 3; |
a346af3e | 2244 | if (gdbserver_state.line_buf_index + repeat >= sizeof(gdbserver_state.line_buf) - 1) { |
4bf43122 | 2245 | /* that many repeats would overrun the command buffer */ |
5c9522b3 | 2246 | trace_gdbstub_err_overrun(); |
a346af3e AB |
2247 | gdbserver_state.state = RS_IDLE; |
2248 | } else if (gdbserver_state.line_buf_index < 1) { | |
4bf43122 | 2249 | /* got a repeat but we have nothing to repeat */ |
5c9522b3 | 2250 | trace_gdbstub_err_invalid_rle(); |
a346af3e | 2251 | gdbserver_state.state = RS_GETLINE; |
4bf43122 DG |
2252 | } else { |
2253 | /* repeat the last character */ | |
a346af3e AB |
2254 | memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index, |
2255 | gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat); | |
2256 | gdbserver_state.line_buf_index += repeat; | |
2257 | gdbserver_state.line_sum += ch; | |
2258 | gdbserver_state.state = RS_GETLINE; | |
4bf43122 | 2259 | } |
4c3a88a2 FB |
2260 | } |
2261 | break; | |
858693c6 | 2262 | case RS_CHKSUM1: |
4bf43122 DG |
2263 | /* get high hex digit of checksum */ |
2264 | if (!isxdigit(ch)) { | |
33c846ef | 2265 | trace_gdbstub_err_checksum_invalid(ch); |
a346af3e | 2266 | gdbserver_state.state = RS_GETLINE; |
4bf43122 DG |
2267 | break; |
2268 | } | |
a346af3e AB |
2269 | gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0'; |
2270 | gdbserver_state.line_csum = fromhex(ch) << 4; | |
2271 | gdbserver_state.state = RS_CHKSUM2; | |
858693c6 FB |
2272 | break; |
2273 | case RS_CHKSUM2: | |
4bf43122 DG |
2274 | /* get low hex digit of checksum */ |
2275 | if (!isxdigit(ch)) { | |
33c846ef | 2276 | trace_gdbstub_err_checksum_invalid(ch); |
a346af3e | 2277 | gdbserver_state.state = RS_GETLINE; |
4bf43122 | 2278 | break; |
858693c6 | 2279 | } |
a346af3e | 2280 | gdbserver_state.line_csum |= fromhex(ch); |
4bf43122 | 2281 | |
a346af3e AB |
2282 | if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) { |
2283 | trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum); | |
4bf43122 | 2284 | /* send NAK reply */ |
60fe76f3 | 2285 | reply = '-'; |
36e067b2 | 2286 | gdb_put_buffer(&reply, 1); |
a346af3e | 2287 | gdbserver_state.state = RS_IDLE; |
4c3a88a2 | 2288 | } else { |
4bf43122 | 2289 | /* send ACK reply */ |
60fe76f3 | 2290 | reply = '+'; |
36e067b2 | 2291 | gdb_put_buffer(&reply, 1); |
a346af3e | 2292 | gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf); |
4c3a88a2 FB |
2293 | } |
2294 | break; | |
a2d1ebaf PB |
2295 | default: |
2296 | abort(); | |
858693c6 FB |
2297 | } |
2298 | } | |
2299 | } | |
2300 | ||
8f468636 LM |
2301 | /* |
2302 | * Create the process that will contain all the "orphan" CPUs (that are not | |
2303 | * part of a CPU cluster). Note that if this process contains no CPUs, it won't | |
2304 | * be attachable and thus will be invisible to the user. | |
2305 | */ | |
36e067b2 | 2306 | void gdb_create_default_process(GDBState *s) |
8f468636 LM |
2307 | { |
2308 | GDBProcess *process; | |
dc14a7a6 | 2309 | int pid; |
8f468636 | 2310 | |
dc14a7a6 IL |
2311 | #ifdef CONFIG_USER_ONLY |
2312 | assert(gdbserver_state.process_num == 0); | |
2313 | pid = getpid(); | |
2314 | #else | |
a346af3e | 2315 | if (gdbserver_state.process_num) { |
dc14a7a6 IL |
2316 | pid = s->processes[s->process_num - 1].pid; |
2317 | } else { | |
2318 | pid = 0; | |
8f468636 | 2319 | } |
dc14a7a6 IL |
2320 | /* We need an available PID slot for this process */ |
2321 | assert(pid < UINT32_MAX); | |
2322 | pid++; | |
2323 | #endif | |
8f468636 LM |
2324 | |
2325 | s->processes = g_renew(GDBProcess, s->processes, ++s->process_num); | |
2326 | process = &s->processes[s->process_num - 1]; | |
dc14a7a6 | 2327 | process->pid = pid; |
8f468636 | 2328 | process->attached = false; |
56e534bd | 2329 | process->target_xml = NULL; |
8f468636 LM |
2330 | } |
2331 |