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