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