]> git.proxmox.com Git - mirror_qemu.git/blame - gdbstub.c
gdbstub: reject unsupported flags in handle_set_qemu_sstep
[mirror_qemu.git] / 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"
a8d25326 27#include "qemu-common.h"
da34e65c 28#include "qapi/error.h"
508b4ecc 29#include "qemu/error-report.h"
856dfd8a 30#include "qemu/ctype.h"
f348b6d1 31#include "qemu/cutils.h"
0b8fa32f 32#include "qemu/module.h"
243af022 33#include "trace/trace-root.h"
85b4fa0c 34#include "exec/gdbstub.h"
f348b6d1 35#ifdef CONFIG_USER_ONLY
1fddef4b
FB
36#include "qemu.h"
37#else
83c9089e 38#include "monitor/monitor.h"
8228e353 39#include "chardev/char.h"
4d43a603 40#include "chardev/char-fe.h"
8f468636 41#include "hw/cpu/cluster.h"
5cc8767d 42#include "hw/boards.h"
1fddef4b 43#endif
67b915a5 44
56aebc89
PB
45#define MAX_PACKET_LENGTH 4096
46
1de7afc9 47#include "qemu/sockets.h"
b3946626 48#include "sysemu/hw_accel.h"
9c17d615 49#include "sysemu/kvm.h"
54d31236 50#include "sysemu/runstate.h"
6b5fe137 51#include "semihosting/semihost.h"
63c91552 52#include "exec/exec-all.h"
fda8458b 53#include "sysemu/replay.h"
ca587a8e 54
a3919386
JK
55#ifdef CONFIG_USER_ONLY
56#define GDB_ATTACHED "0"
57#else
58#define GDB_ATTACHED "1"
59#endif
60
ab4752ec
JD
61#ifndef CONFIG_USER_ONLY
62static int phy_memory_mode;
63#endif
64
f3659eee
AF
65static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
66 uint8_t *buf, int len, bool is_write)
44520db1 67{
ab4752ec 68 CPUClass *cc;
f3659eee 69
ab4752ec
JD
70#ifndef CONFIG_USER_ONLY
71 if (phy_memory_mode) {
72 if (is_write) {
73 cpu_physical_memory_write(addr, buf, len);
74 } else {
75 cpu_physical_memory_read(addr, buf, len);
76 }
77 return 0;
78 }
79#endif
80
81 cc = CPU_GET_CLASS(cpu);
f3659eee
AF
82 if (cc->memory_rw_debug) {
83 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
84 }
85 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
44520db1 86}
ca587a8e 87
d2a6c857
AB
88/* Return the GDB index for a given vCPU state.
89 *
90 * For user mode this is simply the thread id. In system mode GDB
91 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
92 */
93static inline int cpu_gdb_index(CPUState *cpu)
94{
95#if defined(CONFIG_USER_ONLY)
bd88c780 96 TaskState *ts = (TaskState *) cpu->opaque;
a8e537fa 97 return ts ? ts->ts_tid : -1;
d2a6c857
AB
98#else
99 return cpu->cpu_index + 1;
100#endif
101}
102
ca587a8e
AJ
103enum {
104 GDB_SIGNAL_0 = 0,
105 GDB_SIGNAL_INT = 2,
425189a8 106 GDB_SIGNAL_QUIT = 3,
ca587a8e 107 GDB_SIGNAL_TRAP = 5,
425189a8
JK
108 GDB_SIGNAL_ABRT = 6,
109 GDB_SIGNAL_ALRM = 14,
110 GDB_SIGNAL_IO = 23,
111 GDB_SIGNAL_XCPU = 24,
ca587a8e
AJ
112 GDB_SIGNAL_UNKNOWN = 143
113};
114
115#ifdef CONFIG_USER_ONLY
116
117/* Map target signal numbers to GDB protocol signal numbers and vice
118 * versa. For user emulation's currently supported systems, we can
119 * assume most signals are defined.
120 */
121
122static int gdb_signal_table[] = {
123 0,
124 TARGET_SIGHUP,
125 TARGET_SIGINT,
126 TARGET_SIGQUIT,
127 TARGET_SIGILL,
128 TARGET_SIGTRAP,
129 TARGET_SIGABRT,
130 -1, /* SIGEMT */
131 TARGET_SIGFPE,
132 TARGET_SIGKILL,
133 TARGET_SIGBUS,
134 TARGET_SIGSEGV,
135 TARGET_SIGSYS,
136 TARGET_SIGPIPE,
137 TARGET_SIGALRM,
138 TARGET_SIGTERM,
139 TARGET_SIGURG,
140 TARGET_SIGSTOP,
141 TARGET_SIGTSTP,
142 TARGET_SIGCONT,
143 TARGET_SIGCHLD,
144 TARGET_SIGTTIN,
145 TARGET_SIGTTOU,
146 TARGET_SIGIO,
147 TARGET_SIGXCPU,
148 TARGET_SIGXFSZ,
149 TARGET_SIGVTALRM,
150 TARGET_SIGPROF,
151 TARGET_SIGWINCH,
152 -1, /* SIGLOST */
153 TARGET_SIGUSR1,
154 TARGET_SIGUSR2,
c72d5bf8 155#ifdef TARGET_SIGPWR
ca587a8e 156 TARGET_SIGPWR,
c72d5bf8
BS
157#else
158 -1,
159#endif
ca587a8e
AJ
160 -1, /* SIGPOLL */
161 -1,
162 -1,
163 -1,
164 -1,
165 -1,
166 -1,
167 -1,
168 -1,
169 -1,
170 -1,
171 -1,
c72d5bf8 172#ifdef __SIGRTMIN
ca587a8e
AJ
173 __SIGRTMIN + 1,
174 __SIGRTMIN + 2,
175 __SIGRTMIN + 3,
176 __SIGRTMIN + 4,
177 __SIGRTMIN + 5,
178 __SIGRTMIN + 6,
179 __SIGRTMIN + 7,
180 __SIGRTMIN + 8,
181 __SIGRTMIN + 9,
182 __SIGRTMIN + 10,
183 __SIGRTMIN + 11,
184 __SIGRTMIN + 12,
185 __SIGRTMIN + 13,
186 __SIGRTMIN + 14,
187 __SIGRTMIN + 15,
188 __SIGRTMIN + 16,
189 __SIGRTMIN + 17,
190 __SIGRTMIN + 18,
191 __SIGRTMIN + 19,
192 __SIGRTMIN + 20,
193 __SIGRTMIN + 21,
194 __SIGRTMIN + 22,
195 __SIGRTMIN + 23,
196 __SIGRTMIN + 24,
197 __SIGRTMIN + 25,
198 __SIGRTMIN + 26,
199 __SIGRTMIN + 27,
200 __SIGRTMIN + 28,
201 __SIGRTMIN + 29,
202 __SIGRTMIN + 30,
203 __SIGRTMIN + 31,
204 -1, /* SIGCANCEL */
205 __SIGRTMIN,
206 __SIGRTMIN + 32,
207 __SIGRTMIN + 33,
208 __SIGRTMIN + 34,
209 __SIGRTMIN + 35,
210 __SIGRTMIN + 36,
211 __SIGRTMIN + 37,
212 __SIGRTMIN + 38,
213 __SIGRTMIN + 39,
214 __SIGRTMIN + 40,
215 __SIGRTMIN + 41,
216 __SIGRTMIN + 42,
217 __SIGRTMIN + 43,
218 __SIGRTMIN + 44,
219 __SIGRTMIN + 45,
220 __SIGRTMIN + 46,
221 __SIGRTMIN + 47,
222 __SIGRTMIN + 48,
223 __SIGRTMIN + 49,
224 __SIGRTMIN + 50,
225 __SIGRTMIN + 51,
226 __SIGRTMIN + 52,
227 __SIGRTMIN + 53,
228 __SIGRTMIN + 54,
229 __SIGRTMIN + 55,
230 __SIGRTMIN + 56,
231 __SIGRTMIN + 57,
232 __SIGRTMIN + 58,
233 __SIGRTMIN + 59,
234 __SIGRTMIN + 60,
235 __SIGRTMIN + 61,
236 __SIGRTMIN + 62,
237 __SIGRTMIN + 63,
238 __SIGRTMIN + 64,
239 __SIGRTMIN + 65,
240 __SIGRTMIN + 66,
241 __SIGRTMIN + 67,
242 __SIGRTMIN + 68,
243 __SIGRTMIN + 69,
244 __SIGRTMIN + 70,
245 __SIGRTMIN + 71,
246 __SIGRTMIN + 72,
247 __SIGRTMIN + 73,
248 __SIGRTMIN + 74,
249 __SIGRTMIN + 75,
250 __SIGRTMIN + 76,
251 __SIGRTMIN + 77,
252 __SIGRTMIN + 78,
253 __SIGRTMIN + 79,
254 __SIGRTMIN + 80,
255 __SIGRTMIN + 81,
256 __SIGRTMIN + 82,
257 __SIGRTMIN + 83,
258 __SIGRTMIN + 84,
259 __SIGRTMIN + 85,
260 __SIGRTMIN + 86,
261 __SIGRTMIN + 87,
262 __SIGRTMIN + 88,
263 __SIGRTMIN + 89,
264 __SIGRTMIN + 90,
265 __SIGRTMIN + 91,
266 __SIGRTMIN + 92,
267 __SIGRTMIN + 93,
268 __SIGRTMIN + 94,
269 __SIGRTMIN + 95,
270 -1, /* SIGINFO */
271 -1, /* UNKNOWN */
272 -1, /* DEFAULT */
273 -1,
274 -1,
275 -1,
276 -1,
277 -1,
278 -1
c72d5bf8 279#endif
ca587a8e 280};
8f447cc7 281#else
ca587a8e
AJ
282/* In system mode we only need SIGINT and SIGTRAP; other signals
283 are not yet supported. */
284
285enum {
286 TARGET_SIGINT = 2,
287 TARGET_SIGTRAP = 5
288};
289
290static int gdb_signal_table[] = {
291 -1,
292 -1,
293 TARGET_SIGINT,
294 -1,
295 -1,
296 TARGET_SIGTRAP
297};
298#endif
299
300#ifdef CONFIG_USER_ONLY
301static int target_signal_to_gdb (int sig)
302{
303 int i;
304 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
305 if (gdb_signal_table[i] == sig)
306 return i;
307 return GDB_SIGNAL_UNKNOWN;
308}
8f447cc7 309#endif
b4608c04 310
ca587a8e
AJ
311static int gdb_signal_to_target (int sig)
312{
313 if (sig < ARRAY_SIZE (gdb_signal_table))
314 return gdb_signal_table[sig];
315 else
316 return -1;
317}
318
56aebc89
PB
319typedef struct GDBRegisterState {
320 int base_reg;
321 int num_regs;
a010bdbe
AB
322 gdb_get_reg_cb get_reg;
323 gdb_set_reg_cb set_reg;
56aebc89
PB
324 const char *xml;
325 struct GDBRegisterState *next;
326} GDBRegisterState;
327
8f468636
LM
328typedef struct GDBProcess {
329 uint32_t pid;
330 bool attached;
c145eeae
LM
331
332 char target_xml[1024];
8f468636
LM
333} GDBProcess;
334
858693c6 335enum RSState {
36556b20 336 RS_INACTIVE,
858693c6
FB
337 RS_IDLE,
338 RS_GETLINE,
4bf43122
DG
339 RS_GETLINE_ESC,
340 RS_GETLINE_RLE,
858693c6
FB
341 RS_CHKSUM1,
342 RS_CHKSUM2,
343};
858693c6 344typedef struct GDBState {
8d98c445 345 bool init; /* have we been initialised? */
2e0f2cfb
AF
346 CPUState *c_cpu; /* current CPU for step/continue ops */
347 CPUState *g_cpu; /* current CPU for other ops */
52f34623 348 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
41625033 349 enum RSState state; /* parsing state */
56aebc89 350 char line_buf[MAX_PACKET_LENGTH];
858693c6 351 int line_buf_index;
4bf43122
DG
352 int line_sum; /* running checksum */
353 int line_csum; /* checksum at the end of the packet */
d116e813 354 GByteArray *last_packet;
1f487ee9 355 int signal;
41625033 356#ifdef CONFIG_USER_ONLY
4046d913 357 int fd;
fcedd920 358 char *socket_path;
41625033 359 int running_state;
4046d913 360#else
32a6ebec 361 CharBackend chr;
0ec7b3e7 362 Chardev *mon_chr;
41625033 363#endif
8f468636
LM
364 bool multiprocess;
365 GDBProcess *processes;
366 int process_num;
cdb432b2
MI
367 char syscall_buf[256];
368 gdb_syscall_complete_cb current_syscall_cb;
308f9e88 369 GString *str_buf;
4a25f1b9 370 GByteArray *mem_buf;
ecd39d62
ML
371 int sstep_flags;
372 int supported_sstep_flags;
858693c6 373} GDBState;
b4608c04 374
8d98c445
AB
375static GDBState gdbserver_state;
376
377static void init_gdbserver_state(void)
378{
379 g_assert(!gdbserver_state.init);
380 memset(&gdbserver_state, 0, sizeof(GDBState));
381 gdbserver_state.init = true;
308f9e88 382 gdbserver_state.str_buf = g_string_new(NULL);
4a25f1b9 383 gdbserver_state.mem_buf = g_byte_array_sized_new(MAX_PACKET_LENGTH);
d116e813 384 gdbserver_state.last_packet = g_byte_array_sized_new(MAX_PACKET_LENGTH + 4);
ecd39d62
ML
385
386 /*
387 * In replay mode all events will come from the log and can't be
388 * suppressed otherwise we would break determinism. However as those
389 * events are tied to the number of executed instructions we won't see
390 * them occurring every time we single step.
391 */
392 if (replay_mode != REPLAY_MODE_NONE) {
393 gdbserver_state.supported_sstep_flags = SSTEP_ENABLE;
394 } else {
395 gdbserver_state.supported_sstep_flags =
396 SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER;
397 }
398
399 /*
400 * By default use no IRQs and no timers while single stepping so as to
401 * make single stepping like an ICE HW step.
402 */
403 gdbserver_state.sstep_flags = gdbserver_state.supported_sstep_flags;
404
8d98c445
AB
405}
406
407#ifndef CONFIG_USER_ONLY
408static void reset_gdbserver_state(void)
409{
410 g_free(gdbserver_state.processes);
411 gdbserver_state.processes = NULL;
412 gdbserver_state.process_num = 0;
413}
414#endif
880a7578 415
5b50e790 416bool gdb_has_xml;
56aebc89 417
1fddef4b 418#ifdef CONFIG_USER_ONLY
4046d913 419
a346af3e 420static int get_char(void)
b4608c04
FB
421{
422 uint8_t ch;
423 int ret;
424
425 for(;;) {
a346af3e 426 ret = qemu_recv(gdbserver_state.fd, &ch, 1, 0);
b4608c04 427 if (ret < 0) {
1f487ee9 428 if (errno == ECONNRESET)
a346af3e 429 gdbserver_state.fd = -1;
5819e3e0 430 if (errno != EINTR)
b4608c04
FB
431 return -1;
432 } else if (ret == 0) {
a346af3e
AB
433 close(gdbserver_state.fd);
434 gdbserver_state.fd = -1;
b4608c04
FB
435 return -1;
436 } else {
437 break;
438 }
439 }
440 return ch;
441}
4046d913 442#endif
b4608c04 443
654efcf3 444static enum {
a2d1ebaf
PB
445 GDB_SYS_UNKNOWN,
446 GDB_SYS_ENABLED,
447 GDB_SYS_DISABLED,
448} gdb_syscall_mode;
449
a38bb079 450/* Decide if either remote gdb syscalls or native file IO should be used. */
a2d1ebaf
PB
451int use_gdb_syscalls(void)
452{
cfe67cef
LA
453 SemihostingTarget target = semihosting_get_target();
454 if (target == SEMIHOSTING_TARGET_NATIVE) {
a38bb079
LI
455 /* -semihosting-config target=native */
456 return false;
cfe67cef 457 } else if (target == SEMIHOSTING_TARGET_GDB) {
a38bb079
LI
458 /* -semihosting-config target=gdb */
459 return true;
460 }
461
462 /* -semihosting-config target=auto */
463 /* On the first call check if gdb is connected and remember. */
a2d1ebaf 464 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
8d98c445
AB
465 gdb_syscall_mode = gdbserver_state.init ?
466 GDB_SYS_ENABLED : GDB_SYS_DISABLED;
a2d1ebaf
PB
467 }
468 return gdb_syscall_mode == GDB_SYS_ENABLED;
469}
470
ed12f5b4
AB
471static bool stub_can_reverse(void)
472{
473#ifdef CONFIG_USER_ONLY
474 return false;
475#else
476 return replay_mode == REPLAY_MODE_PLAY;
477#endif
478}
479
ba70a624 480/* Resume execution. */
a346af3e 481static inline void gdb_continue(void)
ba70a624 482{
5c9522b3 483
ba70a624 484#ifdef CONFIG_USER_ONLY
a346af3e 485 gdbserver_state.running_state = 1;
5c9522b3 486 trace_gdbstub_op_continue();
ba70a624 487#else
26ac7a31 488 if (!runstate_needs_reset()) {
5c9522b3 489 trace_gdbstub_op_continue();
87f25c12
PB
490 vm_start();
491 }
ba70a624
EI
492#endif
493}
494
544177ad
CI
495/*
496 * Resume execution, per CPU actions. For user-mode emulation it's
497 * equivalent to gdb_continue.
498 */
a346af3e 499static int gdb_continue_partial(char *newstates)
544177ad
CI
500{
501 CPUState *cpu;
502 int res = 0;
503#ifdef CONFIG_USER_ONLY
504 /*
505 * This is not exactly accurate, but it's an improvement compared to the
506 * previous situation, where only one CPU would be single-stepped.
507 */
508 CPU_FOREACH(cpu) {
509 if (newstates[cpu->cpu_index] == 's') {
5c9522b3 510 trace_gdbstub_op_stepping(cpu->cpu_index);
ecd39d62 511 cpu_single_step(cpu, gdbserver_state.sstep_flags);
544177ad
CI
512 }
513 }
a346af3e 514 gdbserver_state.running_state = 1;
544177ad
CI
515#else
516 int flag = 0;
517
518 if (!runstate_needs_reset()) {
519 if (vm_prepare_start()) {
520 return 0;
521 }
522
523 CPU_FOREACH(cpu) {
524 switch (newstates[cpu->cpu_index]) {
525 case 0:
526 case 1:
527 break; /* nothing to do here */
528 case 's':
5c9522b3 529 trace_gdbstub_op_stepping(cpu->cpu_index);
ecd39d62 530 cpu_single_step(cpu, gdbserver_state.sstep_flags);
544177ad
CI
531 cpu_resume(cpu);
532 flag = 1;
533 break;
534 case 'c':
5c9522b3 535 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
544177ad
CI
536 cpu_resume(cpu);
537 flag = 1;
538 break;
539 default:
540 res = -1;
541 break;
542 }
543 }
544 }
545 if (flag) {
546 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
547 }
548#endif
549 return res;
550}
551
a346af3e 552static void put_buffer(const uint8_t *buf, int len)
b4608c04 553{
4046d913 554#ifdef CONFIG_USER_ONLY
b4608c04
FB
555 int ret;
556
557 while (len > 0) {
a346af3e 558 ret = send(gdbserver_state.fd, buf, len, 0);
b4608c04 559 if (ret < 0) {
5819e3e0 560 if (errno != EINTR)
b4608c04
FB
561 return;
562 } else {
563 buf += ret;
564 len -= ret;
565 }
566 }
4046d913 567#else
6ab3fc32
DB
568 /* XXX this blocks entire thread. Rewrite to use
569 * qemu_chr_fe_write and background I/O callbacks */
a346af3e 570 qemu_chr_fe_write_all(&gdbserver_state.chr, buf, len);
4046d913 571#endif
b4608c04
FB
572}
573
574static inline int fromhex(int v)
575{
576 if (v >= '0' && v <= '9')
577 return v - '0';
578 else if (v >= 'A' && v <= 'F')
579 return v - 'A' + 10;
580 else if (v >= 'a' && v <= 'f')
581 return v - 'a' + 10;
582 else
583 return 0;
584}
585
586static inline int tohex(int v)
587{
588 if (v < 10)
589 return v + '0';
590 else
591 return v - 10 + 'a';
592}
593
9005774b 594/* writes 2*len+1 bytes in buf */
308f9e88 595static void memtohex(GString *buf, const uint8_t *mem, int len)
b4608c04
FB
596{
597 int i, c;
b4608c04
FB
598 for(i = 0; i < len; i++) {
599 c = mem[i];
308f9e88
AB
600 g_string_append_c(buf, tohex(c >> 4));
601 g_string_append_c(buf, tohex(c & 0xf));
b4608c04 602 }
308f9e88 603 g_string_append_c(buf, '\0');
b4608c04
FB
604}
605
4a25f1b9 606static void hextomem(GByteArray *mem, const char *buf, int len)
b4608c04
FB
607{
608 int i;
609
610 for(i = 0; i < len; i++) {
4a25f1b9
AB
611 guint8 byte = fromhex(buf[0]) << 4 | fromhex(buf[1]);
612 g_byte_array_append(mem, &byte, 1);
b4608c04
FB
613 buf += 2;
614 }
615}
616
5c9522b3
DG
617static void hexdump(const char *buf, int len,
618 void (*trace_fn)(size_t ofs, char const *text))
619{
620 char line_buffer[3 * 16 + 4 + 16 + 1];
621
622 size_t i;
623 for (i = 0; i < len || (i & 0xF); ++i) {
624 size_t byte_ofs = i & 15;
625
626 if (byte_ofs == 0) {
627 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
628 line_buffer[3 * 16 + 4 + 16] = 0;
629 }
630
631 size_t col_group = (i >> 2) & 3;
632 size_t hex_col = byte_ofs * 3 + col_group;
633 size_t txt_col = 3 * 16 + 4 + byte_ofs;
634
635 if (i < len) {
636 char value = buf[i];
637
638 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
639 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
640 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
641 ? value
642 : '.';
643 }
644
645 if (byte_ofs == 0xF)
646 trace_fn(i & -16, line_buffer);
647 }
648}
649
b4608c04 650/* return -1 if error, 0 if OK */
a346af3e 651static int put_packet_binary(const char *buf, int len, bool dump)
b4608c04 652{
56aebc89 653 int csum, i;
d116e813 654 uint8_t footer[3];
b4608c04 655
5c9522b3
DG
656 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
657 hexdump(buf, len, trace_gdbstub_io_binaryreply);
658 }
659
b4608c04 660 for(;;) {
d116e813
DH
661 g_byte_array_set_size(gdbserver_state.last_packet, 0);
662 g_byte_array_append(gdbserver_state.last_packet,
663 (const uint8_t *) "$", 1);
664 g_byte_array_append(gdbserver_state.last_packet,
665 (const uint8_t *) buf, len);
b4608c04
FB
666 csum = 0;
667 for(i = 0; i < len; i++) {
668 csum += buf[i];
669 }
d116e813
DH
670 footer[0] = '#';
671 footer[1] = tohex((csum >> 4) & 0xf);
672 footer[2] = tohex((csum) & 0xf);
673 g_byte_array_append(gdbserver_state.last_packet, footer, 3);
b4608c04 674
d116e813
DH
675 put_buffer(gdbserver_state.last_packet->data,
676 gdbserver_state.last_packet->len);
b4608c04 677
4046d913 678#ifdef CONFIG_USER_ONLY
a346af3e 679 i = get_char();
4046d913 680 if (i < 0)
b4608c04 681 return -1;
4046d913 682 if (i == '+')
b4608c04 683 break;
4046d913
PB
684#else
685 break;
686#endif
b4608c04
FB
687 }
688 return 0;
689}
690
56aebc89 691/* return -1 if error, 0 if OK */
a346af3e 692static int put_packet(const char *buf)
56aebc89 693{
5c9522b3 694 trace_gdbstub_io_reply(buf);
79808573 695
a346af3e 696 return put_packet_binary(buf, strlen(buf), false);
56aebc89
PB
697}
698
308f9e88
AB
699static void put_strbuf(void)
700{
701 put_packet(gdbserver_state.str_buf->str);
702}
703
56aebc89 704/* Encode data using the encoding for 'x' packets. */
308f9e88 705static void memtox(GString *buf, const char *mem, int len)
56aebc89 706{
56aebc89
PB
707 char c;
708
709 while (len--) {
710 c = *(mem++);
711 switch (c) {
712 case '#': case '$': case '*': case '}':
308f9e88
AB
713 g_string_append_c(buf, '}');
714 g_string_append_c(buf, c ^ 0x20);
56aebc89
PB
715 break;
716 default:
308f9e88 717 g_string_append_c(buf, c);
56aebc89
PB
718 break;
719 }
720 }
56aebc89 721}
f1ccf904 722
a346af3e 723static uint32_t gdb_get_cpu_pid(CPUState *cpu)
1a227336 724{
46f5abc0
PM
725 /* TODO: In user mode, we should use the task state PID */
726 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
1a227336 727 /* Return the default process' PID */
a346af3e
AB
728 int index = gdbserver_state.process_num - 1;
729 return gdbserver_state.processes[index].pid;
1a227336 730 }
46f5abc0 731 return cpu->cluster_index + 1;
1a227336
LM
732}
733
a346af3e 734static GDBProcess *gdb_get_process(uint32_t pid)
7d8c87da
LM
735{
736 int i;
737
738 if (!pid) {
739 /* 0 means any process, we take the first one */
a346af3e 740 return &gdbserver_state.processes[0];
7d8c87da
LM
741 }
742
a346af3e
AB
743 for (i = 0; i < gdbserver_state.process_num; i++) {
744 if (gdbserver_state.processes[i].pid == pid) {
745 return &gdbserver_state.processes[i];
7d8c87da
LM
746 }
747 }
748
749 return NULL;
750}
751
a346af3e 752static GDBProcess *gdb_get_cpu_process(CPUState *cpu)
7d8c87da 753{
a346af3e 754 return gdb_get_process(gdb_get_cpu_pid(cpu));
7d8c87da
LM
755}
756
757static CPUState *find_cpu(uint32_t thread_id)
758{
759 CPUState *cpu;
760
761 CPU_FOREACH(cpu) {
762 if (cpu_gdb_index(cpu) == thread_id) {
763 return cpu;
764 }
765 }
766
767 return NULL;
768}
769
a346af3e 770static CPUState *get_first_cpu_in_process(GDBProcess *process)
e40e5204
LM
771{
772 CPUState *cpu;
773
774 CPU_FOREACH(cpu) {
a346af3e 775 if (gdb_get_cpu_pid(cpu) == process->pid) {
e40e5204
LM
776 return cpu;
777 }
778 }
779
780 return NULL;
781}
782
a346af3e 783static CPUState *gdb_next_cpu_in_process(CPUState *cpu)
e40e5204 784{
a346af3e 785 uint32_t pid = gdb_get_cpu_pid(cpu);
e40e5204
LM
786 cpu = CPU_NEXT(cpu);
787
788 while (cpu) {
a346af3e 789 if (gdb_get_cpu_pid(cpu) == pid) {
e40e5204
LM
790 break;
791 }
792
793 cpu = CPU_NEXT(cpu);
794 }
795
796 return cpu;
797}
798
e40e5204 799/* Return the cpu following @cpu, while ignoring unattached processes. */
a346af3e 800static CPUState *gdb_next_attached_cpu(CPUState *cpu)
e40e5204
LM
801{
802 cpu = CPU_NEXT(cpu);
803
804 while (cpu) {
a346af3e 805 if (gdb_get_cpu_process(cpu)->attached) {
e40e5204
LM
806 break;
807 }
808
809 cpu = CPU_NEXT(cpu);
810 }
811
812 return cpu;
813}
814
815/* Return the first attached cpu */
a346af3e 816static CPUState *gdb_first_attached_cpu(void)
e40e5204
LM
817{
818 CPUState *cpu = first_cpu;
a346af3e 819 GDBProcess *process = gdb_get_cpu_process(cpu);
e40e5204
LM
820
821 if (!process->attached) {
a346af3e 822 return gdb_next_attached_cpu(cpu);
e40e5204
LM
823 }
824
825 return cpu;
826}
827
a346af3e 828static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid)
ab65eed3
LM
829{
830 GDBProcess *process;
831 CPUState *cpu;
832
833 if (!pid && !tid) {
834 /* 0 means any process/thread, we take the first attached one */
a346af3e 835 return gdb_first_attached_cpu();
ab65eed3
LM
836 } else if (pid && !tid) {
837 /* any thread in a specific process */
a346af3e 838 process = gdb_get_process(pid);
ab65eed3
LM
839
840 if (process == NULL) {
841 return NULL;
842 }
843
844 if (!process->attached) {
845 return NULL;
846 }
847
a346af3e 848 return get_first_cpu_in_process(process);
ab65eed3
LM
849 } else {
850 /* a specific thread */
851 cpu = find_cpu(tid);
852
853 if (cpu == NULL) {
854 return NULL;
855 }
856
a346af3e 857 process = gdb_get_cpu_process(cpu);
ab65eed3
LM
858
859 if (pid && process->pid != pid) {
860 return NULL;
861 }
862
863 if (!process->attached) {
864 return NULL;
865 }
866
867 return cpu;
868 }
869}
870
a346af3e
AB
871static const char *get_feature_xml(const char *p, const char **newp,
872 GDBProcess *process)
56aebc89 873{
56aebc89
PB
874 size_t len;
875 int i;
876 const char *name;
a346af3e 877 CPUState *cpu = get_first_cpu_in_process(process);
c145eeae 878 CPUClass *cc = CPU_GET_CLASS(cpu);
56aebc89
PB
879
880 len = 0;
881 while (p[len] && p[len] != ':')
882 len++;
883 *newp = p + len;
884
885 name = NULL;
886 if (strncmp(p, "target.xml", len) == 0) {
c145eeae
LM
887 char *buf = process->target_xml;
888 const size_t buf_sz = sizeof(process->target_xml);
889
56aebc89 890 /* Generate the XML description for this CPU. */
c145eeae 891 if (!buf[0]) {
56aebc89
PB
892 GDBRegisterState *r;
893
c145eeae 894 pstrcat(buf, buf_sz,
b3820e6c
DH
895 "<?xml version=\"1.0\"?>"
896 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
897 "<target>");
898 if (cc->gdb_arch_name) {
899 gchar *arch = cc->gdb_arch_name(cpu);
c145eeae
LM
900 pstrcat(buf, buf_sz, "<architecture>");
901 pstrcat(buf, buf_sz, arch);
902 pstrcat(buf, buf_sz, "</architecture>");
b3820e6c
DH
903 g_free(arch);
904 }
c145eeae
LM
905 pstrcat(buf, buf_sz, "<xi:include href=\"");
906 pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
907 pstrcat(buf, buf_sz, "\"/>");
eac8b355 908 for (r = cpu->gdb_regs; r; r = r->next) {
c145eeae
LM
909 pstrcat(buf, buf_sz, "<xi:include href=\"");
910 pstrcat(buf, buf_sz, r->xml);
911 pstrcat(buf, buf_sz, "\"/>");
56aebc89 912 }
c145eeae 913 pstrcat(buf, buf_sz, "</target>");
56aebc89 914 }
c145eeae 915 return buf;
56aebc89 916 }
200bf5b7 917 if (cc->gdb_get_dynamic_xml) {
200bf5b7
AB
918 char *xmlname = g_strndup(p, len);
919 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
920
921 g_free(xmlname);
922 if (xml) {
923 return xml;
924 }
925 }
56aebc89
PB
926 for (i = 0; ; i++) {
927 name = xml_builtin[i][0];
928 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
929 break;
930 }
931 return name ? xml_builtin[i][1] : NULL;
932}
f1ccf904 933
a010bdbe 934static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
56aebc89 935{
a0e372f0 936 CPUClass *cc = CPU_GET_CLASS(cpu);
385b9f0e 937 CPUArchState *env = cpu->env_ptr;
56aebc89 938 GDBRegisterState *r;
f1ccf904 939
a0e372f0 940 if (reg < cc->gdb_num_core_regs) {
a010bdbe 941 return cc->gdb_read_register(cpu, buf, reg);
a0e372f0 942 }
f1ccf904 943
eac8b355 944 for (r = cpu->gdb_regs; r; r = r->next) {
56aebc89 945 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
a010bdbe 946 return r->get_reg(env, buf, reg - r->base_reg);
56aebc89
PB
947 }
948 }
949 return 0;
f1ccf904
TS
950}
951
385b9f0e 952static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
f1ccf904 953{
a0e372f0 954 CPUClass *cc = CPU_GET_CLASS(cpu);
385b9f0e 955 CPUArchState *env = cpu->env_ptr;
56aebc89 956 GDBRegisterState *r;
f1ccf904 957
a0e372f0 958 if (reg < cc->gdb_num_core_regs) {
5b50e790 959 return cc->gdb_write_register(cpu, mem_buf, reg);
a0e372f0 960 }
56aebc89 961
eac8b355 962 for (r = cpu->gdb_regs; r; r = r->next) {
56aebc89
PB
963 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
964 return r->set_reg(env, mem_buf, reg - r->base_reg);
965 }
966 }
6da41eaf
FB
967 return 0;
968}
969
56aebc89
PB
970/* Register a supplemental set of CPU registers. If g_pos is nonzero it
971 specifies the first register number and these registers are included in
972 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
973 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
974 */
975
22169d41 976void gdb_register_coprocessor(CPUState *cpu,
a010bdbe 977 gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
22169d41 978 int num_regs, const char *xml, int g_pos)
6da41eaf 979{
56aebc89
PB
980 GDBRegisterState *s;
981 GDBRegisterState **p;
56aebc89 982
eac8b355 983 p = &cpu->gdb_regs;
56aebc89
PB
984 while (*p) {
985 /* Check for duplicates. */
986 if (strcmp((*p)->xml, xml) == 0)
987 return;
988 p = &(*p)->next;
989 }
9643c25f
SW
990
991 s = g_new0(GDBRegisterState, 1);
a0e372f0 992 s->base_reg = cpu->gdb_num_regs;
9643c25f
SW
993 s->num_regs = num_regs;
994 s->get_reg = get_reg;
995 s->set_reg = set_reg;
996 s->xml = xml;
997
56aebc89 998 /* Add to end of list. */
a0e372f0 999 cpu->gdb_num_regs += num_regs;
56aebc89
PB
1000 *p = s;
1001 if (g_pos) {
1002 if (g_pos != s->base_reg) {
7ae6c571
ZY
1003 error_report("Error: Bad gdb register numbering for '%s', "
1004 "expected %d got %d", xml, g_pos, s->base_reg);
35143f01
AF
1005 } else {
1006 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
56aebc89
PB
1007 }
1008 }
6da41eaf
FB
1009}
1010
a1d1bb31 1011#ifndef CONFIG_USER_ONLY
2472b6c0
PM
1012/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
1013static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
1014{
1015 static const int xlat[] = {
1016 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
1017 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
1018 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
1019 };
1020
1021 CPUClass *cc = CPU_GET_CLASS(cpu);
1022 int cputype = xlat[gdbtype];
1023
1024 if (cc->gdb_stop_before_watchpoint) {
1025 cputype |= BP_STOP_BEFORE_ACCESS;
1026 }
1027 return cputype;
1028}
a1d1bb31
AL
1029#endif
1030
77f6ce50 1031static int gdb_breakpoint_insert(int type, target_ulong addr, target_ulong len)
a1d1bb31 1032{
182735ef 1033 CPUState *cpu;
880a7578
AL
1034 int err = 0;
1035
62278814 1036 if (kvm_enabled()) {
8d98c445 1037 return kvm_insert_breakpoint(gdbserver_state.c_cpu, addr, len, type);
62278814 1038 }
e22a25c9 1039
a1d1bb31
AL
1040 switch (type) {
1041 case GDB_BREAKPOINT_SW:
1042 case GDB_BREAKPOINT_HW:
bdc44640 1043 CPU_FOREACH(cpu) {
b3310ab3
AF
1044 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
1045 if (err) {
880a7578 1046 break;
b3310ab3 1047 }
880a7578
AL
1048 }
1049 return err;
a1d1bb31
AL
1050#ifndef CONFIG_USER_ONLY
1051 case GDB_WATCHPOINT_WRITE:
1052 case GDB_WATCHPOINT_READ:
1053 case GDB_WATCHPOINT_ACCESS:
bdc44640 1054 CPU_FOREACH(cpu) {
2472b6c0
PM
1055 err = cpu_watchpoint_insert(cpu, addr, len,
1056 xlat_gdb_type(cpu, type), NULL);
1057 if (err) {
880a7578 1058 break;
2472b6c0 1059 }
880a7578
AL
1060 }
1061 return err;
a1d1bb31
AL
1062#endif
1063 default:
1064 return -ENOSYS;
1065 }
1066}
1067
77f6ce50 1068static int gdb_breakpoint_remove(int type, target_ulong addr, target_ulong len)
a1d1bb31 1069{
182735ef 1070 CPUState *cpu;
880a7578
AL
1071 int err = 0;
1072
62278814 1073 if (kvm_enabled()) {
8d98c445 1074 return kvm_remove_breakpoint(gdbserver_state.c_cpu, addr, len, type);
62278814 1075 }
e22a25c9 1076
a1d1bb31
AL
1077 switch (type) {
1078 case GDB_BREAKPOINT_SW:
1079 case GDB_BREAKPOINT_HW:
bdc44640 1080 CPU_FOREACH(cpu) {
b3310ab3
AF
1081 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
1082 if (err) {
880a7578 1083 break;
b3310ab3 1084 }
880a7578
AL
1085 }
1086 return err;
a1d1bb31
AL
1087#ifndef CONFIG_USER_ONLY
1088 case GDB_WATCHPOINT_WRITE:
1089 case GDB_WATCHPOINT_READ:
1090 case GDB_WATCHPOINT_ACCESS:
bdc44640 1091 CPU_FOREACH(cpu) {
2472b6c0
PM
1092 err = cpu_watchpoint_remove(cpu, addr, len,
1093 xlat_gdb_type(cpu, type));
880a7578
AL
1094 if (err)
1095 break;
1096 }
1097 return err;
a1d1bb31
AL
1098#endif
1099 default:
1100 return -ENOSYS;
1101 }
1102}
1103
546f3c67
LM
1104static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu)
1105{
1106 cpu_breakpoint_remove_all(cpu, BP_GDB);
1107#ifndef CONFIG_USER_ONLY
1108 cpu_watchpoint_remove_all(cpu, BP_GDB);
1109#endif
1110}
1111
a346af3e 1112static void gdb_process_breakpoint_remove_all(GDBProcess *p)
546f3c67 1113{
a346af3e 1114 CPUState *cpu = get_first_cpu_in_process(p);
546f3c67
LM
1115
1116 while (cpu) {
1117 gdb_cpu_breakpoint_remove_all(cpu);
a346af3e 1118 cpu = gdb_next_cpu_in_process(cpu);
546f3c67
LM
1119 }
1120}
1121
880a7578 1122static void gdb_breakpoint_remove_all(void)
a1d1bb31 1123{
182735ef 1124 CPUState *cpu;
880a7578 1125
e22a25c9 1126 if (kvm_enabled()) {
8d98c445 1127 kvm_remove_all_breakpoints(gdbserver_state.c_cpu);
e22a25c9
AL
1128 return;
1129 }
1130
bdc44640 1131 CPU_FOREACH(cpu) {
546f3c67 1132 gdb_cpu_breakpoint_remove_all(cpu);
880a7578 1133 }
a1d1bb31
AL
1134}
1135
a346af3e 1136static void gdb_set_cpu_pc(target_ulong pc)
fab9d284 1137{
a346af3e 1138 CPUState *cpu = gdbserver_state.c_cpu;
f45748f1
AF
1139
1140 cpu_synchronize_state(cpu);
4a2b24ed 1141 cpu_set_pc(cpu, pc);
fab9d284
AJ
1142}
1143
308f9e88 1144static void gdb_append_thread_id(CPUState *cpu, GString *buf)
1a227336 1145{
a346af3e 1146 if (gdbserver_state.multiprocess) {
308f9e88
AB
1147 g_string_append_printf(buf, "p%02x.%02x",
1148 gdb_get_cpu_pid(cpu), cpu_gdb_index(cpu));
1a227336 1149 } else {
308f9e88 1150 g_string_append_printf(buf, "%02x", cpu_gdb_index(cpu));
1a227336 1151 }
1a227336
LM
1152}
1153
7d8c87da
LM
1154typedef enum GDBThreadIdKind {
1155 GDB_ONE_THREAD = 0,
1156 GDB_ALL_THREADS, /* One process, all threads */
1157 GDB_ALL_PROCESSES,
1158 GDB_READ_THREAD_ERR
1159} GDBThreadIdKind;
1160
1161static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1162 uint32_t *pid, uint32_t *tid)
1163{
1164 unsigned long p, t;
1165 int ret;
1166
1167 if (*buf == 'p') {
1168 buf++;
1169 ret = qemu_strtoul(buf, &buf, 16, &p);
1170
1171 if (ret) {
1172 return GDB_READ_THREAD_ERR;
1173 }
1174
1175 /* Skip '.' */
1176 buf++;
1177 } else {
1178 p = 1;
1179 }
1180
1181 ret = qemu_strtoul(buf, &buf, 16, &t);
1182
1183 if (ret) {
1184 return GDB_READ_THREAD_ERR;
1185 }
1186
1187 *end_buf = buf;
1188
1189 if (p == -1) {
1190 return GDB_ALL_PROCESSES;
1191 }
1192
1193 if (pid) {
1194 *pid = p;
1195 }
1196
1197 if (t == -1) {
1198 return GDB_ALL_THREADS;
1199 }
1200
1201 if (tid) {
1202 *tid = t;
1203 }
1204
1205 return GDB_ONE_THREAD;
1206}
1207
544177ad
CI
1208/**
1209 * gdb_handle_vcont - Parses and handles a vCont packet.
1210 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1211 * a format error, 0 on success.
1212 */
a346af3e 1213static int gdb_handle_vcont(const char *p)
544177ad 1214{
e40e5204 1215 int res, signal = 0;
544177ad
CI
1216 char cur_action;
1217 char *newstates;
1218 unsigned long tmp;
e40e5204
LM
1219 uint32_t pid, tid;
1220 GDBProcess *process;
544177ad 1221 CPUState *cpu;
c99ef792 1222 GDBThreadIdKind kind;
544177ad
CI
1223#ifdef CONFIG_USER_ONLY
1224 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
1225
1226 CPU_FOREACH(cpu) {
1227 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1228 }
5cc8767d
LX
1229#else
1230 MachineState *ms = MACHINE(qdev_get_machine());
1231 unsigned int max_cpus = ms->smp.max_cpus;
544177ad
CI
1232#endif
1233 /* uninitialised CPUs stay 0 */
1234 newstates = g_new0(char, max_cpus);
1235
1236 /* mark valid CPUs with 1 */
1237 CPU_FOREACH(cpu) {
1238 newstates[cpu->cpu_index] = 1;
1239 }
1240
1241 /*
1242 * res keeps track of what error we are returning, with -ENOTSUP meaning
1243 * that the command is unknown or unsupported, thus returning an empty
1244 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1245 * or incorrect parameters passed.
1246 */
1247 res = 0;
1248 while (*p) {
1249 if (*p++ != ';') {
1250 res = -ENOTSUP;
1251 goto out;
1252 }
1253
1254 cur_action = *p++;
1255 if (cur_action == 'C' || cur_action == 'S') {
95a5befc 1256 cur_action = qemu_tolower(cur_action);
3ddd9036 1257 res = qemu_strtoul(p, &p, 16, &tmp);
544177ad
CI
1258 if (res) {
1259 goto out;
1260 }
1261 signal = gdb_signal_to_target(tmp);
1262 } else if (cur_action != 'c' && cur_action != 's') {
1263 /* unknown/invalid/unsupported command */
1264 res = -ENOTSUP;
1265 goto out;
1266 }
e40e5204 1267
c99ef792
LM
1268 if (*p == '\0' || *p == ';') {
1269 /*
1270 * No thread specifier, action is on "all threads". The
1271 * specification is unclear regarding the process to act on. We
1272 * choose all processes.
1273 */
1274 kind = GDB_ALL_PROCESSES;
1275 } else if (*p++ == ':') {
1276 kind = read_thread_id(p, &p, &pid, &tid);
1277 } else {
e40e5204
LM
1278 res = -ENOTSUP;
1279 goto out;
1280 }
1281
c99ef792 1282 switch (kind) {
e40e5204
LM
1283 case GDB_READ_THREAD_ERR:
1284 res = -EINVAL;
1285 goto out;
1286
1287 case GDB_ALL_PROCESSES:
a346af3e 1288 cpu = gdb_first_attached_cpu();
e40e5204
LM
1289 while (cpu) {
1290 if (newstates[cpu->cpu_index] == 1) {
1291 newstates[cpu->cpu_index] = cur_action;
544177ad 1292 }
e40e5204 1293
a346af3e 1294 cpu = gdb_next_attached_cpu(cpu);
544177ad 1295 }
e40e5204
LM
1296 break;
1297
1298 case GDB_ALL_THREADS:
a346af3e 1299 process = gdb_get_process(pid);
e40e5204
LM
1300
1301 if (!process->attached) {
1302 res = -EINVAL;
544177ad
CI
1303 goto out;
1304 }
5a6a1ad1 1305
a346af3e 1306 cpu = get_first_cpu_in_process(process);
e40e5204
LM
1307 while (cpu) {
1308 if (newstates[cpu->cpu_index] == 1) {
1309 newstates[cpu->cpu_index] = cur_action;
1310 }
1311
a346af3e 1312 cpu = gdb_next_cpu_in_process(cpu);
e40e5204
LM
1313 }
1314 break;
1315
1316 case GDB_ONE_THREAD:
a346af3e 1317 cpu = gdb_get_cpu(pid, tid);
544177ad 1318
544177ad 1319 /* invalid CPU/thread specified */
5a6a1ad1 1320 if (!cpu) {
544177ad
CI
1321 res = -EINVAL;
1322 goto out;
1323 }
5a6a1ad1 1324
544177ad
CI
1325 /* only use if no previous match occourred */
1326 if (newstates[cpu->cpu_index] == 1) {
1327 newstates[cpu->cpu_index] = cur_action;
1328 }
e40e5204 1329 break;
544177ad
CI
1330 }
1331 }
a346af3e
AB
1332 gdbserver_state.signal = signal;
1333 gdb_continue_partial(newstates);
544177ad
CI
1334
1335out:
1336 g_free(newstates);
1337
1338 return res;
1339}
1340
d14055dc
JD
1341typedef union GdbCmdVariant {
1342 const char *data;
1343 uint8_t opcode;
1344 unsigned long val_ul;
1345 unsigned long long val_ull;
1346 struct {
1347 GDBThreadIdKind kind;
1348 uint32_t pid;
1349 uint32_t tid;
1350 } thread_id;
1351} GdbCmdVariant;
1352
26a16181
AB
1353#define get_param(p, i) (&g_array_index(p, GdbCmdVariant, i))
1354
d14055dc
JD
1355static const char *cmd_next_param(const char *param, const char delimiter)
1356{
1357 static const char all_delimiters[] = ",;:=";
1358 char curr_delimiters[2] = {0};
1359 const char *delimiters;
1360
1361 if (delimiter == '?') {
1362 delimiters = all_delimiters;
1363 } else if (delimiter == '0') {
1364 return strchr(param, '\0');
1365 } else if (delimiter == '.' && *param) {
1366 return param + 1;
1367 } else {
1368 curr_delimiters[0] = delimiter;
1369 delimiters = curr_delimiters;
1370 }
1371
1372 param += strcspn(param, delimiters);
1373 if (*param) {
1374 param++;
1375 }
1376 return param;
1377}
1378
1379static int cmd_parse_params(const char *data, const char *schema,
26a16181 1380 GArray *params)
d14055dc 1381{
d14055dc
JD
1382 const char *curr_schema, *curr_data;
1383
26a16181
AB
1384 g_assert(schema);
1385 g_assert(params->len == 0);
d14055dc
JD
1386
1387 curr_schema = schema;
d14055dc
JD
1388 curr_data = data;
1389 while (curr_schema[0] && curr_schema[1] && *curr_data) {
26a16181
AB
1390 GdbCmdVariant this_param;
1391
d14055dc
JD
1392 switch (curr_schema[0]) {
1393 case 'l':
1394 if (qemu_strtoul(curr_data, &curr_data, 16,
26a16181 1395 &this_param.val_ul)) {
d14055dc
JD
1396 return -EINVAL;
1397 }
d14055dc 1398 curr_data = cmd_next_param(curr_data, curr_schema[1]);
26a16181 1399 g_array_append_val(params, this_param);
d14055dc
JD
1400 break;
1401 case 'L':
1402 if (qemu_strtou64(curr_data, &curr_data, 16,
26a16181 1403 (uint64_t *)&this_param.val_ull)) {
d14055dc
JD
1404 return -EINVAL;
1405 }
d14055dc 1406 curr_data = cmd_next_param(curr_data, curr_schema[1]);
26a16181 1407 g_array_append_val(params, this_param);
d14055dc
JD
1408 break;
1409 case 's':
26a16181 1410 this_param.data = curr_data;
d14055dc 1411 curr_data = cmd_next_param(curr_data, curr_schema[1]);
26a16181 1412 g_array_append_val(params, this_param);
d14055dc
JD
1413 break;
1414 case 'o':
26a16181 1415 this_param.opcode = *(uint8_t *)curr_data;
d14055dc 1416 curr_data = cmd_next_param(curr_data, curr_schema[1]);
26a16181 1417 g_array_append_val(params, this_param);
d14055dc
JD
1418 break;
1419 case 't':
26a16181 1420 this_param.thread_id.kind =
d14055dc 1421 read_thread_id(curr_data, &curr_data,
26a16181
AB
1422 &this_param.thread_id.pid,
1423 &this_param.thread_id.tid);
d14055dc 1424 curr_data = cmd_next_param(curr_data, curr_schema[1]);
26a16181 1425 g_array_append_val(params, this_param);
d14055dc
JD
1426 break;
1427 case '?':
1428 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1429 break;
1430 default:
1431 return -EINVAL;
1432 }
1433 curr_schema += 2;
1434 }
1435
d14055dc
JD
1436 return 0;
1437}
1438
26a16181 1439typedef void (*GdbCmdHandler)(GArray *params, void *user_ctx);
d14055dc
JD
1440
1441/*
1442 * cmd_startswith -> cmd is compared using startswith
1443 *
1444 *
1445 * schema definitions:
1446 * Each schema parameter entry consists of 2 chars,
1447 * the first char represents the parameter type handling
1448 * the second char represents the delimiter for the next parameter
1449 *
1450 * Currently supported schema types:
1451 * 'l' -> unsigned long (stored in .val_ul)
1452 * 'L' -> unsigned long long (stored in .val_ull)
1453 * 's' -> string (stored in .data)
1454 * 'o' -> single char (stored in .opcode)
1455 * 't' -> thread id (stored in .thread_id)
1456 * '?' -> skip according to delimiter
1457 *
1458 * Currently supported delimiters:
1459 * '?' -> Stop at any delimiter (",;:=\0")
1460 * '0' -> Stop at "\0"
1461 * '.' -> Skip 1 char unless reached "\0"
1462 * Any other value is treated as the delimiter value itself
1463 */
1464typedef struct GdbCmdParseEntry {
1465 GdbCmdHandler handler;
1466 const char *cmd;
1467 bool cmd_startswith;
1468 const char *schema;
1469} GdbCmdParseEntry;
1470
1471static inline int startswith(const char *string, const char *pattern)
1472{
1473 return !strncmp(string, pattern, strlen(pattern));
1474}
1475
a346af3e 1476static int process_string_cmd(void *user_ctx, const char *data,
d14055dc
JD
1477 const GdbCmdParseEntry *cmds, int num_cmds)
1478{
26a16181
AB
1479 int i;
1480 g_autoptr(GArray) params = g_array_new(false, true, sizeof(GdbCmdVariant));
d14055dc
JD
1481
1482 if (!cmds) {
1483 return -1;
1484 }
1485
1486 for (i = 0; i < num_cmds; i++) {
1487 const GdbCmdParseEntry *cmd = &cmds[i];
1488 g_assert(cmd->handler && cmd->cmd);
1489
1490 if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
1491 (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
1492 continue;
1493 }
1494
1495 if (cmd->schema) {
26a16181
AB
1496 if (cmd_parse_params(&data[strlen(cmd->cmd)],
1497 cmd->schema, params)) {
1498 return -1;
d14055dc 1499 }
d14055dc
JD
1500 }
1501
26a16181 1502 cmd->handler(params, user_ctx);
d14055dc
JD
1503 return 0;
1504 }
1505
1506 return -1;
1507}
1508
a346af3e 1509static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd)
3e2c1261
JD
1510{
1511 if (!data) {
1512 return;
1513 }
1514
308f9e88 1515 g_string_set_size(gdbserver_state.str_buf, 0);
4a25f1b9 1516 g_byte_array_set_size(gdbserver_state.mem_buf, 0);
308f9e88 1517
3e2c1261
JD
1518 /* In case there was an error during the command parsing we must
1519 * send a NULL packet to indicate the command is not supported */
a346af3e
AB
1520 if (process_string_cmd(NULL, data, cmd, 1)) {
1521 put_packet("");
3e2c1261
JD
1522 }
1523}
1524
26a16181 1525static void handle_detach(GArray *params, void *user_ctx)
3e2c1261
JD
1526{
1527 GDBProcess *process;
3e2c1261
JD
1528 uint32_t pid = 1;
1529
a346af3e 1530 if (gdbserver_state.multiprocess) {
26a16181 1531 if (!params->len) {
a346af3e 1532 put_packet("E22");
3e2c1261
JD
1533 return;
1534 }
1535
26a16181 1536 pid = get_param(params, 0)->val_ul;
3e2c1261
JD
1537 }
1538
a346af3e
AB
1539 process = gdb_get_process(pid);
1540 gdb_process_breakpoint_remove_all(process);
3e2c1261
JD
1541 process->attached = false;
1542
a346af3e
AB
1543 if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) {
1544 gdbserver_state.c_cpu = gdb_first_attached_cpu();
3e2c1261
JD
1545 }
1546
a346af3e
AB
1547 if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) {
1548 gdbserver_state.g_cpu = gdb_first_attached_cpu();
3e2c1261
JD
1549 }
1550
a346af3e 1551 if (!gdbserver_state.c_cpu) {
3e2c1261
JD
1552 /* No more process attached */
1553 gdb_syscall_mode = GDB_SYS_DISABLED;
a346af3e 1554 gdb_continue();
3e2c1261 1555 }
a346af3e 1556 put_packet("OK");
3e2c1261
JD
1557}
1558
26a16181 1559static void handle_thread_alive(GArray *params, void *user_ctx)
44ffded0
JD
1560{
1561 CPUState *cpu;
1562
26a16181 1563 if (!params->len) {
a346af3e 1564 put_packet("E22");
44ffded0
JD
1565 return;
1566 }
1567
26a16181 1568 if (get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
a346af3e 1569 put_packet("E22");
44ffded0
JD
1570 return;
1571 }
1572
26a16181
AB
1573 cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
1574 get_param(params, 0)->thread_id.tid);
44ffded0 1575 if (!cpu) {
a346af3e 1576 put_packet("E22");
44ffded0
JD
1577 return;
1578 }
1579
a346af3e 1580 put_packet("OK");
44ffded0
JD
1581}
1582
26a16181 1583static void handle_continue(GArray *params, void *user_ctx)
4d6e3fe2 1584{
26a16181
AB
1585 if (params->len) {
1586 gdb_set_cpu_pc(get_param(params, 0)->val_ull);
4d6e3fe2
JD
1587 }
1588
a346af3e
AB
1589 gdbserver_state.signal = 0;
1590 gdb_continue();
4d6e3fe2
JD
1591}
1592
26a16181 1593static void handle_cont_with_sig(GArray *params, void *user_ctx)
ccc47d5d
JD
1594{
1595 unsigned long signal = 0;
1596
1597 /*
1598 * Note: C sig;[addr] is currently unsupported and we simply
1599 * omit the addr parameter
1600 */
26a16181
AB
1601 if (params->len) {
1602 signal = get_param(params, 0)->val_ul;
ccc47d5d
JD
1603 }
1604
a346af3e
AB
1605 gdbserver_state.signal = gdb_signal_to_target(signal);
1606 if (gdbserver_state.signal == -1) {
1607 gdbserver_state.signal = 0;
ccc47d5d 1608 }
a346af3e 1609 gdb_continue();
ccc47d5d
JD
1610}
1611
26a16181 1612static void handle_set_thread(GArray *params, void *user_ctx)
3a9651d6
JD
1613{
1614 CPUState *cpu;
1615
26a16181 1616 if (params->len != 2) {
a346af3e 1617 put_packet("E22");
3a9651d6
JD
1618 return;
1619 }
1620
26a16181 1621 if (get_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) {
a346af3e 1622 put_packet("E22");
3a9651d6
JD
1623 return;
1624 }
1625
26a16181 1626 if (get_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) {
a346af3e 1627 put_packet("OK");
3a9651d6
JD
1628 return;
1629 }
1630
26a16181
AB
1631 cpu = gdb_get_cpu(get_param(params, 1)->thread_id.pid,
1632 get_param(params, 1)->thread_id.tid);
3a9651d6 1633 if (!cpu) {
a346af3e 1634 put_packet("E22");
3a9651d6
JD
1635 return;
1636 }
1637
1638 /*
1639 * Note: This command is deprecated and modern gdb's will be using the
1640 * vCont command instead.
1641 */
26a16181 1642 switch (get_param(params, 0)->opcode) {
3a9651d6 1643 case 'c':
a346af3e
AB
1644 gdbserver_state.c_cpu = cpu;
1645 put_packet("OK");
3a9651d6
JD
1646 break;
1647 case 'g':
a346af3e
AB
1648 gdbserver_state.g_cpu = cpu;
1649 put_packet("OK");
3a9651d6
JD
1650 break;
1651 default:
a346af3e 1652 put_packet("E22");
3a9651d6
JD
1653 break;
1654 }
1655}
1656
26a16181 1657static void handle_insert_bp(GArray *params, void *user_ctx)
77f6ce50
JD
1658{
1659 int res;
1660
26a16181 1661 if (params->len != 3) {
a346af3e 1662 put_packet("E22");
77f6ce50
JD
1663 return;
1664 }
1665
26a16181
AB
1666 res = gdb_breakpoint_insert(get_param(params, 0)->val_ul,
1667 get_param(params, 1)->val_ull,
1668 get_param(params, 2)->val_ull);
77f6ce50 1669 if (res >= 0) {
a346af3e 1670 put_packet("OK");
77f6ce50
JD
1671 return;
1672 } else if (res == -ENOSYS) {
a346af3e 1673 put_packet("");
77f6ce50
JD
1674 return;
1675 }
1676
a346af3e 1677 put_packet("E22");
77f6ce50
JD
1678}
1679
26a16181 1680static void handle_remove_bp(GArray *params, void *user_ctx)
77f6ce50
JD
1681{
1682 int res;
1683
26a16181 1684 if (params->len != 3) {
a346af3e 1685 put_packet("E22");
77f6ce50
JD
1686 return;
1687 }
1688
26a16181
AB
1689 res = gdb_breakpoint_remove(get_param(params, 0)->val_ul,
1690 get_param(params, 1)->val_ull,
1691 get_param(params, 2)->val_ull);
77f6ce50 1692 if (res >= 0) {
a346af3e 1693 put_packet("OK");
77f6ce50
JD
1694 return;
1695 } else if (res == -ENOSYS) {
a346af3e 1696 put_packet("");
77f6ce50
JD
1697 return;
1698 }
1699
a346af3e 1700 put_packet("E22");
77f6ce50
JD
1701}
1702
94b2a62b
AB
1703/*
1704 * handle_set/get_reg
1705 *
1706 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1707 * This works, but can be very slow. Anything new enough to understand
1708 * XML also knows how to use this properly. However to use this we
1709 * need to define a local XML file as well as be talking to a
1710 * reasonably modern gdb. Responding with an empty packet will cause
1711 * the remote gdb to fallback to older methods.
1712 */
1713
26a16181 1714static void handle_set_reg(GArray *params, void *user_ctx)
62b3320b
JD
1715{
1716 int reg_size;
1717
1718 if (!gdb_has_xml) {
a346af3e 1719 put_packet("");
62b3320b
JD
1720 return;
1721 }
1722
26a16181 1723 if (params->len != 2) {
a346af3e 1724 put_packet("E22");
62b3320b
JD
1725 return;
1726 }
1727
26a16181
AB
1728 reg_size = strlen(get_param(params, 1)->data) / 2;
1729 hextomem(gdbserver_state.mem_buf, get_param(params, 1)->data, reg_size);
4a25f1b9 1730 gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data,
26a16181 1731 get_param(params, 0)->val_ull);
a346af3e 1732 put_packet("OK");
62b3320b
JD
1733}
1734
26a16181 1735static void handle_get_reg(GArray *params, void *user_ctx)
5d0e57bd
JD
1736{
1737 int reg_size;
1738
5d0e57bd 1739 if (!gdb_has_xml) {
a346af3e 1740 put_packet("");
5d0e57bd
JD
1741 return;
1742 }
1743
26a16181 1744 if (!params->len) {
a346af3e 1745 put_packet("E14");
5d0e57bd
JD
1746 return;
1747 }
1748
4a25f1b9 1749 reg_size = gdb_read_register(gdbserver_state.g_cpu,
a010bdbe 1750 gdbserver_state.mem_buf,
26a16181 1751 get_param(params, 0)->val_ull);
5d0e57bd 1752 if (!reg_size) {
a346af3e 1753 put_packet("E14");
5d0e57bd 1754 return;
4a25f1b9
AB
1755 } else {
1756 g_byte_array_set_size(gdbserver_state.mem_buf, reg_size);
5d0e57bd
JD
1757 }
1758
4a25f1b9 1759 memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, reg_size);
308f9e88 1760 put_strbuf();
5d0e57bd
JD
1761}
1762
26a16181 1763static void handle_write_mem(GArray *params, void *user_ctx)
cc0ecc78 1764{
26a16181 1765 if (params->len != 3) {
a346af3e 1766 put_packet("E22");
cc0ecc78
JD
1767 return;
1768 }
1769
1770 /* hextomem() reads 2*len bytes */
26a16181
AB
1771 if (get_param(params, 1)->val_ull >
1772 strlen(get_param(params, 2)->data) / 2) {
a346af3e 1773 put_packet("E22");
cc0ecc78
JD
1774 return;
1775 }
1776
26a16181
AB
1777 hextomem(gdbserver_state.mem_buf, get_param(params, 2)->data,
1778 get_param(params, 1)->val_ull);
1779 if (target_memory_rw_debug(gdbserver_state.g_cpu,
1780 get_param(params, 0)->val_ull,
4a25f1b9
AB
1781 gdbserver_state.mem_buf->data,
1782 gdbserver_state.mem_buf->len, true)) {
a346af3e 1783 put_packet("E14");
cc0ecc78
JD
1784 return;
1785 }
1786
a346af3e 1787 put_packet("OK");
cc0ecc78
JD
1788}
1789
26a16181 1790static void handle_read_mem(GArray *params, void *user_ctx)
da92e236 1791{
26a16181 1792 if (params->len != 2) {
a346af3e 1793 put_packet("E22");
da92e236
JD
1794 return;
1795 }
1796
1797 /* memtohex() doubles the required space */
26a16181 1798 if (get_param(params, 1)->val_ull > MAX_PACKET_LENGTH / 2) {
a346af3e 1799 put_packet("E22");
da92e236
JD
1800 return;
1801 }
1802
26a16181
AB
1803 g_byte_array_set_size(gdbserver_state.mem_buf,
1804 get_param(params, 1)->val_ull);
4a25f1b9 1805
26a16181
AB
1806 if (target_memory_rw_debug(gdbserver_state.g_cpu,
1807 get_param(params, 0)->val_ull,
4a25f1b9
AB
1808 gdbserver_state.mem_buf->data,
1809 gdbserver_state.mem_buf->len, false)) {
a346af3e 1810 put_packet("E14");
da92e236
JD
1811 return;
1812 }
1813
4a25f1b9
AB
1814 memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data,
1815 gdbserver_state.mem_buf->len);
308f9e88 1816 put_strbuf();
da92e236
JD
1817}
1818
26a16181 1819static void handle_write_all_regs(GArray *params, void *user_ctx)
287ca120
JD
1820{
1821 target_ulong addr, len;
1822 uint8_t *registers;
1823 int reg_size;
1824
26a16181 1825 if (!params->len) {
287ca120
JD
1826 return;
1827 }
1828
a346af3e 1829 cpu_synchronize_state(gdbserver_state.g_cpu);
26a16181
AB
1830 len = strlen(get_param(params, 0)->data) / 2;
1831 hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len);
4a25f1b9 1832 registers = gdbserver_state.mem_buf->data;
a346af3e 1833 for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0;
287ca120 1834 addr++) {
a346af3e 1835 reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, addr);
287ca120
JD
1836 len -= reg_size;
1837 registers += reg_size;
1838 }
a346af3e 1839 put_packet("OK");
287ca120
JD
1840}
1841
26a16181 1842static void handle_read_all_regs(GArray *params, void *user_ctx)
397d1370
JD
1843{
1844 target_ulong addr, len;
1845
a346af3e 1846 cpu_synchronize_state(gdbserver_state.g_cpu);
a010bdbe 1847 g_byte_array_set_size(gdbserver_state.mem_buf, 0);
397d1370 1848 len = 0;
a346af3e 1849 for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs; addr++) {
4a25f1b9 1850 len += gdb_read_register(gdbserver_state.g_cpu,
a010bdbe 1851 gdbserver_state.mem_buf,
397d1370
JD
1852 addr);
1853 }
a010bdbe 1854 g_assert(len == gdbserver_state.mem_buf->len);
397d1370 1855
4a25f1b9 1856 memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len);
308f9e88 1857 put_strbuf();
397d1370
JD
1858}
1859
26a16181 1860static void handle_file_io(GArray *params, void *user_ctx)
4b20fab1 1861{
26a16181 1862 if (params->len >= 1 && gdbserver_state.current_syscall_cb) {
4b20fab1
JD
1863 target_ulong ret, err;
1864
26a16181
AB
1865 ret = (target_ulong)get_param(params, 0)->val_ull;
1866 if (params->len >= 2) {
1867 err = (target_ulong)get_param(params, 1)->val_ull;
c6ee9521
SL
1868 } else {
1869 err = 0;
1870 }
a346af3e
AB
1871 gdbserver_state.current_syscall_cb(gdbserver_state.c_cpu, ret, err);
1872 gdbserver_state.current_syscall_cb = NULL;
4b20fab1
JD
1873 }
1874
26a16181 1875 if (params->len >= 3 && get_param(params, 2)->opcode == (uint8_t)'C') {
a346af3e 1876 put_packet("T02");
4b20fab1
JD
1877 return;
1878 }
1879
a346af3e 1880 gdb_continue();
4b20fab1
JD
1881}
1882
26a16181 1883static void handle_step(GArray *params, void *user_ctx)
933f80dd 1884{
26a16181
AB
1885 if (params->len) {
1886 gdb_set_cpu_pc((target_ulong)get_param(params, 0)->val_ull);
933f80dd
JD
1887 }
1888
ecd39d62 1889 cpu_single_step(gdbserver_state.c_cpu, gdbserver_state.sstep_flags);
a346af3e 1890 gdb_continue();
933f80dd
JD
1891}
1892
26a16181 1893static void handle_backward(GArray *params, void *user_ctx)
fda8458b 1894{
ed12f5b4 1895 if (!stub_can_reverse()) {
fda8458b
PD
1896 put_packet("E22");
1897 }
26a16181
AB
1898 if (params->len == 1) {
1899 switch (get_param(params, 0)->opcode) {
fda8458b
PD
1900 case 's':
1901 if (replay_reverse_step()) {
1902 gdb_continue();
1903 } else {
1904 put_packet("E14");
1905 }
1906 return;
cda38259
PD
1907 case 'c':
1908 if (replay_reverse_continue()) {
1909 gdb_continue();
1910 } else {
1911 put_packet("E14");
1912 }
1913 return;
fda8458b
PD
1914 }
1915 }
1916
1917 /* Default invalid command */
1918 put_packet("");
1919}
1920
26a16181 1921static void handle_v_cont_query(GArray *params, void *user_ctx)
8536ec02 1922{
a346af3e 1923 put_packet("vCont;c;C;s;S");
8536ec02
JD
1924}
1925
26a16181 1926static void handle_v_cont(GArray *params, void *user_ctx)
8536ec02
JD
1927{
1928 int res;
1929
26a16181 1930 if (!params->len) {
8536ec02
JD
1931 return;
1932 }
1933
26a16181 1934 res = gdb_handle_vcont(get_param(params, 0)->data);
8536ec02 1935 if ((res == -EINVAL) || (res == -ERANGE)) {
a346af3e 1936 put_packet("E22");
8536ec02 1937 } else if (res) {
a346af3e 1938 put_packet("");
8536ec02
JD
1939 }
1940}
1941
26a16181 1942static void handle_v_attach(GArray *params, void *user_ctx)
8536ec02
JD
1943{
1944 GDBProcess *process;
1945 CPUState *cpu;
8536ec02 1946
308f9e88 1947 g_string_assign(gdbserver_state.str_buf, "E22");
26a16181 1948 if (!params->len) {
8536ec02
JD
1949 goto cleanup;
1950 }
1951
26a16181 1952 process = gdb_get_process(get_param(params, 0)->val_ul);
8536ec02
JD
1953 if (!process) {
1954 goto cleanup;
1955 }
1956
a346af3e 1957 cpu = get_first_cpu_in_process(process);
8536ec02
JD
1958 if (!cpu) {
1959 goto cleanup;
1960 }
1961
1962 process->attached = true;
a346af3e
AB
1963 gdbserver_state.g_cpu = cpu;
1964 gdbserver_state.c_cpu = cpu;
8536ec02 1965
308f9e88
AB
1966 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
1967 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
1968 g_string_append_c(gdbserver_state.str_buf, ';');
8536ec02 1969cleanup:
308f9e88 1970 put_strbuf();
8536ec02
JD
1971}
1972
26a16181 1973static void handle_v_kill(GArray *params, void *user_ctx)
8536ec02
JD
1974{
1975 /* Kill the target */
a346af3e 1976 put_packet("OK");
8536ec02 1977 error_report("QEMU: Terminated via GDBstub");
b9e10c6c 1978 gdb_exit(0);
8536ec02
JD
1979 exit(0);
1980}
1981
305bea06 1982static const GdbCmdParseEntry gdb_v_commands_table[] = {
8536ec02
JD
1983 /* Order is important if has same prefix */
1984 {
1985 .handler = handle_v_cont_query,
1986 .cmd = "Cont?",
1987 .cmd_startswith = 1
1988 },
1989 {
1990 .handler = handle_v_cont,
1991 .cmd = "Cont",
1992 .cmd_startswith = 1,
1993 .schema = "s0"
1994 },
1995 {
1996 .handler = handle_v_attach,
1997 .cmd = "Attach;",
1998 .cmd_startswith = 1,
1999 .schema = "l0"
2000 },
2001 {
2002 .handler = handle_v_kill,
2003 .cmd = "Kill;",
2004 .cmd_startswith = 1
2005 },
2006};
2007
26a16181 2008static void handle_v_commands(GArray *params, void *user_ctx)
8536ec02 2009{
26a16181 2010 if (!params->len) {
8536ec02
JD
2011 return;
2012 }
2013
26a16181 2014 if (process_string_cmd(NULL, get_param(params, 0)->data,
8536ec02
JD
2015 gdb_v_commands_table,
2016 ARRAY_SIZE(gdb_v_commands_table))) {
a346af3e 2017 put_packet("");
8536ec02
JD
2018 }
2019}
2020
26a16181 2021static void handle_query_qemu_sstepbits(GArray *params, void *user_ctx)
2704efad 2022{
ecd39d62
ML
2023 g_string_printf(gdbserver_state.str_buf, "ENABLE=%x", SSTEP_ENABLE);
2024
2025 if (gdbserver_state.supported_sstep_flags & SSTEP_NOIRQ) {
2026 g_string_append_printf(gdbserver_state.str_buf, ",NOIRQ=%x",
2027 SSTEP_NOIRQ);
2028 }
2029
2030 if (gdbserver_state.supported_sstep_flags & SSTEP_NOTIMER) {
2031 g_string_append_printf(gdbserver_state.str_buf, ",NOTIMER=%x",
2032 SSTEP_NOTIMER);
2033 }
2034
308f9e88 2035 put_strbuf();
2704efad
JD
2036}
2037
26a16181 2038static void handle_set_qemu_sstep(GArray *params, void *user_ctx)
2704efad 2039{
ecd39d62
ML
2040 int new_sstep_flags;
2041
26a16181 2042 if (!params->len) {
2704efad
JD
2043 return;
2044 }
2045
ecd39d62
ML
2046 new_sstep_flags = get_param(params, 0)->val_ul;
2047
2048 if (new_sstep_flags & ~gdbserver_state.supported_sstep_flags) {
2049 put_packet("E22");
2050 return;
2051 }
2052
2053 gdbserver_state.sstep_flags = new_sstep_flags;
a346af3e 2054 put_packet("OK");
2704efad
JD
2055}
2056
26a16181 2057static void handle_query_qemu_sstep(GArray *params, void *user_ctx)
2704efad 2058{
ecd39d62
ML
2059 g_string_printf(gdbserver_state.str_buf, "0x%x",
2060 gdbserver_state.sstep_flags);
308f9e88 2061 put_strbuf();
2704efad
JD
2062}
2063
26a16181 2064static void handle_query_curr_tid(GArray *params, void *user_ctx)
b4608c04 2065{
2e0f2cfb 2066 CPUState *cpu;
2704efad 2067 GDBProcess *process;
2704efad
JD
2068
2069 /*
2070 * "Current thread" remains vague in the spec, so always return
2071 * the first thread of the current process (gdb returns the
2072 * first thread).
2073 */
a346af3e
AB
2074 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
2075 cpu = get_first_cpu_in_process(process);
308f9e88
AB
2076 g_string_assign(gdbserver_state.str_buf, "QC");
2077 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
2078 put_strbuf();
2704efad
JD
2079}
2080
26a16181 2081static void handle_query_threads(GArray *params, void *user_ctx)
2704efad 2082{
a346af3e
AB
2083 if (!gdbserver_state.query_cpu) {
2084 put_packet("l");
2704efad
JD
2085 return;
2086 }
2087
308f9e88
AB
2088 g_string_assign(gdbserver_state.str_buf, "m");
2089 gdb_append_thread_id(gdbserver_state.query_cpu, gdbserver_state.str_buf);
2090 put_strbuf();
a346af3e 2091 gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu);
2704efad
JD
2092}
2093
26a16181 2094static void handle_query_first_threads(GArray *params, void *user_ctx)
2704efad 2095{
a346af3e 2096 gdbserver_state.query_cpu = gdb_first_attached_cpu();
26a16181 2097 handle_query_threads(params, user_ctx);
2704efad
JD
2098}
2099
26a16181 2100static void handle_query_thread_extra(GArray *params, void *user_ctx)
2704efad 2101{
308f9e88 2102 g_autoptr(GString) rs = g_string_new(NULL);
2704efad 2103 CPUState *cpu;
2704efad 2104
26a16181
AB
2105 if (!params->len ||
2106 get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
a346af3e 2107 put_packet("E22");
2704efad
JD
2108 return;
2109 }
2110
26a16181
AB
2111 cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
2112 get_param(params, 0)->thread_id.tid);
2704efad
JD
2113 if (!cpu) {
2114 return;
2115 }
2116
2117 cpu_synchronize_state(cpu);
2118
a346af3e 2119 if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) {
2704efad
JD
2120 /* Print the CPU model and name in multiprocess mode */
2121 ObjectClass *oc = object_get_class(OBJECT(cpu));
2122 const char *cpu_model = object_class_get_name(oc);
7a309cc9 2123 const char *cpu_name =
076b2fad 2124 object_get_canonical_path_component(OBJECT(cpu));
308f9e88
AB
2125 g_string_printf(rs, "%s %s [%s]", cpu_model, cpu_name,
2126 cpu->halted ? "halted " : "running");
2704efad 2127 } else {
308f9e88 2128 g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index,
2704efad
JD
2129 cpu->halted ? "halted " : "running");
2130 }
308f9e88
AB
2131 trace_gdbstub_op_extra_info(rs->str);
2132 memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len);
2133 put_strbuf();
2704efad
JD
2134}
2135
2136#ifdef CONFIG_USER_ONLY
26a16181 2137static void handle_query_offsets(GArray *params, void *user_ctx)
2704efad
JD
2138{
2139 TaskState *ts;
2140
a346af3e 2141 ts = gdbserver_state.c_cpu->opaque;
308f9e88
AB
2142 g_string_printf(gdbserver_state.str_buf,
2143 "Text=" TARGET_ABI_FMT_lx
2144 ";Data=" TARGET_ABI_FMT_lx
2145 ";Bss=" TARGET_ABI_FMT_lx,
2146 ts->info->code_offset,
2147 ts->info->data_offset,
2148 ts->info->data_offset);
2149 put_strbuf();
2704efad
JD
2150}
2151#else
26a16181 2152static void handle_query_rcmd(GArray *params, void *user_ctx)
2704efad 2153{
4a25f1b9 2154 const guint8 zero = 0;
2704efad
JD
2155 int len;
2156
26a16181 2157 if (!params->len) {
a346af3e 2158 put_packet("E22");
2704efad
JD
2159 return;
2160 }
2161
26a16181 2162 len = strlen(get_param(params, 0)->data);
2704efad 2163 if (len % 2) {
a346af3e 2164 put_packet("E01");
2704efad
JD
2165 return;
2166 }
2167
4a25f1b9 2168 g_assert(gdbserver_state.mem_buf->len == 0);
2704efad 2169 len = len / 2;
26a16181 2170 hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len);
4a25f1b9
AB
2171 g_byte_array_append(gdbserver_state.mem_buf, &zero, 1);
2172 qemu_chr_be_write(gdbserver_state.mon_chr, gdbserver_state.mem_buf->data,
2173 gdbserver_state.mem_buf->len);
a346af3e 2174 put_packet("OK");
2704efad
JD
2175}
2176#endif
2177
26a16181 2178static void handle_query_supported(GArray *params, void *user_ctx)
2704efad
JD
2179{
2180 CPUClass *cc;
2181
308f9e88 2182 g_string_printf(gdbserver_state.str_buf, "PacketSize=%x", MAX_PACKET_LENGTH);
2704efad
JD
2183 cc = CPU_GET_CLASS(first_cpu);
2184 if (cc->gdb_core_xml_file) {
308f9e88 2185 g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+");
2704efad
JD
2186 }
2187
ed12f5b4 2188 if (stub_can_reverse()) {
cda38259
PD
2189 g_string_append(gdbserver_state.str_buf,
2190 ";ReverseStep+;ReverseContinue+");
fda8458b
PD
2191 }
2192
51c623b0
LY
2193#ifdef CONFIG_USER_ONLY
2194 if (gdbserver_state.c_cpu->opaque) {
2195 g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+");
2196 }
2197#endif
2198
26a16181
AB
2199 if (params->len &&
2200 strstr(get_param(params, 0)->data, "multiprocess+")) {
a346af3e 2201 gdbserver_state.multiprocess = true;
2704efad
JD
2202 }
2203
3bc2609d 2204 g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+");
308f9e88 2205 put_strbuf();
2704efad
JD
2206}
2207
26a16181 2208static void handle_query_xfer_features(GArray *params, void *user_ctx)
2704efad 2209{
c145eeae 2210 GDBProcess *process;
5b24c641 2211 CPUClass *cc;
2704efad
JD
2212 unsigned long len, total_len, addr;
2213 const char *xml;
b4608c04 2214 const char *p;
2704efad 2215
26a16181 2216 if (params->len < 3) {
a346af3e 2217 put_packet("E22");
2704efad
JD
2218 return;
2219 }
2220
a346af3e
AB
2221 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
2222 cc = CPU_GET_CLASS(gdbserver_state.g_cpu);
2704efad 2223 if (!cc->gdb_core_xml_file) {
a346af3e 2224 put_packet("");
2704efad
JD
2225 return;
2226 }
2227
2228 gdb_has_xml = true;
26a16181 2229 p = get_param(params, 0)->data;
a346af3e 2230 xml = get_feature_xml(p, &p, process);
2704efad 2231 if (!xml) {
a346af3e 2232 put_packet("E00");
2704efad
JD
2233 return;
2234 }
2235
26a16181
AB
2236 addr = get_param(params, 1)->val_ul;
2237 len = get_param(params, 2)->val_ul;
2704efad
JD
2238 total_len = strlen(xml);
2239 if (addr > total_len) {
a346af3e 2240 put_packet("E00");
2704efad
JD
2241 return;
2242 }
2243
2244 if (len > (MAX_PACKET_LENGTH - 5) / 2) {
2245 len = (MAX_PACKET_LENGTH - 5) / 2;
2246 }
2247
2248 if (len < total_len - addr) {
308f9e88
AB
2249 g_string_assign(gdbserver_state.str_buf, "m");
2250 memtox(gdbserver_state.str_buf, xml + addr, len);
2704efad 2251 } else {
308f9e88
AB
2252 g_string_assign(gdbserver_state.str_buf, "l");
2253 memtox(gdbserver_state.str_buf, xml + addr, total_len - addr);
2704efad
JD
2254 }
2255
308f9e88
AB
2256 put_packet_binary(gdbserver_state.str_buf->str,
2257 gdbserver_state.str_buf->len, true);
2704efad
JD
2258}
2259
51c623b0 2260#if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX_USER)
26a16181 2261static void handle_query_xfer_auxv(GArray *params, void *user_ctx)
51c623b0
LY
2262{
2263 TaskState *ts;
2264 unsigned long offset, len, saved_auxv, auxv_len;
51c623b0 2265
26a16181 2266 if (params->len < 2) {
51c623b0
LY
2267 put_packet("E22");
2268 return;
2269 }
2270
26a16181
AB
2271 offset = get_param(params, 0)->val_ul;
2272 len = get_param(params, 1)->val_ul;
51c623b0
LY
2273 ts = gdbserver_state.c_cpu->opaque;
2274 saved_auxv = ts->info->saved_auxv;
2275 auxv_len = ts->info->auxv_len;
6e3dd757
RH
2276
2277 if (offset >= auxv_len) {
51c623b0
LY
2278 put_packet("E00");
2279 return;
2280 }
2281
2282 if (len > (MAX_PACKET_LENGTH - 5) / 2) {
2283 len = (MAX_PACKET_LENGTH - 5) / 2;
2284 }
2285
2286 if (len < auxv_len - offset) {
2287 g_string_assign(gdbserver_state.str_buf, "m");
51c623b0
LY
2288 } else {
2289 g_string_assign(gdbserver_state.str_buf, "l");
6e3dd757
RH
2290 len = auxv_len - offset;
2291 }
2292
2293 g_byte_array_set_size(gdbserver_state.mem_buf, len);
2294 if (target_memory_rw_debug(gdbserver_state.g_cpu, saved_auxv + offset,
2295 gdbserver_state.mem_buf->data, len, false)) {
2296 put_packet("E14");
2297 return;
51c623b0
LY
2298 }
2299
6e3dd757
RH
2300 memtox(gdbserver_state.str_buf,
2301 (const char *)gdbserver_state.mem_buf->data, len);
51c623b0
LY
2302 put_packet_binary(gdbserver_state.str_buf->str,
2303 gdbserver_state.str_buf->len, true);
2304}
2305#endif
2306
26a16181 2307static void handle_query_attached(GArray *params, void *user_ctx)
2704efad 2308{
a346af3e 2309 put_packet(GDB_ATTACHED);
2704efad
JD
2310}
2311
26a16181 2312static void handle_query_qemu_supported(GArray *params, void *user_ctx)
2704efad 2313{
308f9e88 2314 g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep");
ab4752ec 2315#ifndef CONFIG_USER_ONLY
308f9e88 2316 g_string_append(gdbserver_state.str_buf, ";PhyMemMode");
ab4752ec 2317#endif
308f9e88 2318 put_strbuf();
ab4752ec
JD
2319}
2320
2321#ifndef CONFIG_USER_ONLY
26a16181 2322static void handle_query_qemu_phy_mem_mode(GArray *params,
ab4752ec
JD
2323 void *user_ctx)
2324{
308f9e88
AB
2325 g_string_printf(gdbserver_state.str_buf, "%d", phy_memory_mode);
2326 put_strbuf();
ab4752ec
JD
2327}
2328
26a16181 2329static void handle_set_qemu_phy_mem_mode(GArray *params, void *user_ctx)
ab4752ec 2330{
26a16181 2331 if (!params->len) {
a346af3e 2332 put_packet("E22");
ab4752ec
JD
2333 return;
2334 }
2335
26a16181 2336 if (!get_param(params, 0)->val_ul) {
ab4752ec
JD
2337 phy_memory_mode = 0;
2338 } else {
2339 phy_memory_mode = 1;
2340 }
a346af3e 2341 put_packet("OK");
2704efad 2342}
ab4752ec 2343#endif
2704efad 2344
305bea06 2345static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
2704efad
JD
2346 /* Order is important if has same prefix */
2347 {
2348 .handler = handle_query_qemu_sstepbits,
2349 .cmd = "qemu.sstepbits",
2350 },
2351 {
2352 .handler = handle_query_qemu_sstep,
2353 .cmd = "qemu.sstep",
2354 },
2355 {
2356 .handler = handle_set_qemu_sstep,
2357 .cmd = "qemu.sstep=",
2358 .cmd_startswith = 1,
2359 .schema = "l0"
2360 },
2361};
2362
305bea06 2363static const GdbCmdParseEntry gdb_gen_query_table[] = {
2704efad
JD
2364 {
2365 .handler = handle_query_curr_tid,
2366 .cmd = "C",
2367 },
2368 {
2369 .handler = handle_query_threads,
2370 .cmd = "sThreadInfo",
2371 },
2372 {
2373 .handler = handle_query_first_threads,
2374 .cmd = "fThreadInfo",
2375 },
2376 {
2377 .handler = handle_query_thread_extra,
2378 .cmd = "ThreadExtraInfo,",
2379 .cmd_startswith = 1,
2380 .schema = "t0"
2381 },
2382#ifdef CONFIG_USER_ONLY
2383 {
2384 .handler = handle_query_offsets,
2385 .cmd = "Offsets",
2386 },
2387#else
2388 {
2389 .handler = handle_query_rcmd,
2390 .cmd = "Rcmd,",
2391 .cmd_startswith = 1,
2392 .schema = "s0"
2393 },
2394#endif
2395 {
2396 .handler = handle_query_supported,
2397 .cmd = "Supported:",
2398 .cmd_startswith = 1,
2399 .schema = "s0"
2400 },
2401 {
2402 .handler = handle_query_supported,
2403 .cmd = "Supported",
2404 .schema = "s0"
2405 },
2406 {
2407 .handler = handle_query_xfer_features,
2408 .cmd = "Xfer:features:read:",
2409 .cmd_startswith = 1,
2410 .schema = "s:l,l0"
2411 },
51c623b0
LY
2412#if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX_USER)
2413 {
2414 .handler = handle_query_xfer_auxv,
2415 .cmd = "Xfer:auxv:read::",
2416 .cmd_startswith = 1,
2417 .schema = "l,l0"
2418 },
2419#endif
2704efad
JD
2420 {
2421 .handler = handle_query_attached,
2422 .cmd = "Attached:",
2423 .cmd_startswith = 1
2424 },
2425 {
2426 .handler = handle_query_attached,
2427 .cmd = "Attached",
2428 },
2429 {
2430 .handler = handle_query_qemu_supported,
2431 .cmd = "qemu.Supported",
2432 },
ab4752ec
JD
2433#ifndef CONFIG_USER_ONLY
2434 {
2435 .handler = handle_query_qemu_phy_mem_mode,
2436 .cmd = "qemu.PhyMemMode",
2437 },
2438#endif
2704efad
JD
2439};
2440
305bea06 2441static const GdbCmdParseEntry gdb_gen_set_table[] = {
2704efad
JD
2442 /* Order is important if has same prefix */
2443 {
2444 .handler = handle_set_qemu_sstep,
2445 .cmd = "qemu.sstep:",
2446 .cmd_startswith = 1,
2447 .schema = "l0"
2448 },
ab4752ec
JD
2449#ifndef CONFIG_USER_ONLY
2450 {
2451 .handler = handle_set_qemu_phy_mem_mode,
2452 .cmd = "qemu.PhyMemMode:",
2453 .cmd_startswith = 1,
2454 .schema = "l0"
2455 },
2456#endif
2704efad
JD
2457};
2458
26a16181 2459static void handle_gen_query(GArray *params, void *user_ctx)
2704efad 2460{
26a16181 2461 if (!params->len) {
2704efad
JD
2462 return;
2463 }
2464
26a16181 2465 if (!process_string_cmd(NULL, get_param(params, 0)->data,
2704efad
JD
2466 gdb_gen_query_set_common_table,
2467 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2468 return;
2469 }
2470
26a16181 2471 if (process_string_cmd(NULL, get_param(params, 0)->data,
2704efad
JD
2472 gdb_gen_query_table,
2473 ARRAY_SIZE(gdb_gen_query_table))) {
a346af3e 2474 put_packet("");
2704efad
JD
2475 }
2476}
2477
26a16181 2478static void handle_gen_set(GArray *params, void *user_ctx)
2704efad 2479{
26a16181 2480 if (!params->len) {
2704efad
JD
2481 return;
2482 }
2483
26a16181 2484 if (!process_string_cmd(NULL, get_param(params, 0)->data,
2704efad
JD
2485 gdb_gen_query_set_common_table,
2486 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2487 return;
2488 }
2489
26a16181 2490 if (process_string_cmd(NULL, get_param(params, 0)->data,
2704efad
JD
2491 gdb_gen_set_table,
2492 ARRAY_SIZE(gdb_gen_set_table))) {
a346af3e 2493 put_packet("");
2704efad
JD
2494 }
2495}
2496
26a16181 2497static void handle_target_halt(GArray *params, void *user_ctx)
7009d579 2498{
308f9e88
AB
2499 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
2500 gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf);
2501 g_string_append_c(gdbserver_state.str_buf, ';');
2502 put_strbuf();
7009d579
JD
2503 /*
2504 * Remove all the breakpoints when this query is issued,
2505 * because gdb is doing an initial connect and the state
2506 * should be cleaned up.
2507 */
2508 gdb_breakpoint_remove_all();
2509}
2510
a346af3e 2511static int gdb_handle_packet(const char *line_buf)
2704efad 2512{
3e2c1261 2513 const GdbCmdParseEntry *cmd_parser = NULL;
3b46e624 2514
5c9522b3 2515 trace_gdbstub_io_command(line_buf);
118e2268 2516
3f1cbac7 2517 switch (line_buf[0]) {
53fd6554 2518 case '!':
a346af3e 2519 put_packet("OK");
53fd6554 2520 break;
858693c6 2521 case '?':
7009d579
JD
2522 {
2523 static const GdbCmdParseEntry target_halted_cmd_desc = {
2524 .handler = handle_target_halt,
2525 .cmd = "?",
2526 .cmd_startswith = 1
2527 };
2528 cmd_parser = &target_halted_cmd_desc;
2529 }
858693c6
FB
2530 break;
2531 case 'c':
4d6e3fe2
JD
2532 {
2533 static const GdbCmdParseEntry continue_cmd_desc = {
2534 .handler = handle_continue,
2535 .cmd = "c",
2536 .cmd_startswith = 1,
2537 .schema = "L0"
2538 };
2539 cmd_parser = &continue_cmd_desc;
858693c6 2540 }
4d6e3fe2 2541 break;
1f487ee9 2542 case 'C':
ccc47d5d
JD
2543 {
2544 static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
2545 .handler = handle_cont_with_sig,
2546 .cmd = "C",
2547 .cmd_startswith = 1,
2548 .schema = "l0"
2549 };
2550 cmd_parser = &cont_with_sig_cmd_desc;
2551 }
2552 break;
dd32aa10 2553 case 'v':
8536ec02
JD
2554 {
2555 static const GdbCmdParseEntry v_cmd_desc = {
2556 .handler = handle_v_commands,
2557 .cmd = "v",
2558 .cmd_startswith = 1,
2559 .schema = "s0"
2560 };
2561 cmd_parser = &v_cmd_desc;
dd32aa10 2562 }
8536ec02 2563 break;
7d03f82f
EI
2564 case 'k':
2565 /* Kill the target */
7ae6c571 2566 error_report("QEMU: Terminated via GDBstub");
b9e10c6c 2567 gdb_exit(0);
7d03f82f
EI
2568 exit(0);
2569 case 'D':
3e2c1261
JD
2570 {
2571 static const GdbCmdParseEntry detach_cmd_desc = {
2572 .handler = handle_detach,
2573 .cmd = "D",
2574 .cmd_startswith = 1,
2575 .schema = "?.l0"
2576 };
2577 cmd_parser = &detach_cmd_desc;
546f3c67 2578 }
7d03f82f 2579 break;
858693c6 2580 case 's':
933f80dd
JD
2581 {
2582 static const GdbCmdParseEntry step_cmd_desc = {
2583 .handler = handle_step,
2584 .cmd = "s",
2585 .cmd_startswith = 1,
2586 .schema = "L0"
2587 };
2588 cmd_parser = &step_cmd_desc;
858693c6 2589 }
933f80dd 2590 break;
fda8458b
PD
2591 case 'b':
2592 {
2593 static const GdbCmdParseEntry backward_cmd_desc = {
2594 .handler = handle_backward,
2595 .cmd = "b",
2596 .cmd_startswith = 1,
2597 .schema = "o0"
2598 };
2599 cmd_parser = &backward_cmd_desc;
2600 }
2601 break;
a2d1ebaf
PB
2602 case 'F':
2603 {
4b20fab1
JD
2604 static const GdbCmdParseEntry file_io_cmd_desc = {
2605 .handler = handle_file_io,
2606 .cmd = "F",
2607 .cmd_startswith = 1,
2608 .schema = "L,L,o0"
2609 };
2610 cmd_parser = &file_io_cmd_desc;
a2d1ebaf
PB
2611 }
2612 break;
858693c6 2613 case 'g':
397d1370
JD
2614 {
2615 static const GdbCmdParseEntry read_all_regs_cmd_desc = {
2616 .handler = handle_read_all_regs,
2617 .cmd = "g",
2618 .cmd_startswith = 1
2619 };
2620 cmd_parser = &read_all_regs_cmd_desc;
56aebc89 2621 }
858693c6
FB
2622 break;
2623 case 'G':
287ca120
JD
2624 {
2625 static const GdbCmdParseEntry write_all_regs_cmd_desc = {
2626 .handler = handle_write_all_regs,
2627 .cmd = "G",
2628 .cmd_startswith = 1,
2629 .schema = "s0"
2630 };
2631 cmd_parser = &write_all_regs_cmd_desc;
56aebc89 2632 }
858693c6
FB
2633 break;
2634 case 'm':
da92e236
JD
2635 {
2636 static const GdbCmdParseEntry read_mem_cmd_desc = {
2637 .handler = handle_read_mem,
2638 .cmd = "m",
2639 .cmd_startswith = 1,
2640 .schema = "L,L0"
2641 };
2642 cmd_parser = &read_mem_cmd_desc;
6f970bd9 2643 }
858693c6
FB
2644 break;
2645 case 'M':
cc0ecc78
JD
2646 {
2647 static const GdbCmdParseEntry write_mem_cmd_desc = {
2648 .handler = handle_write_mem,
2649 .cmd = "M",
2650 .cmd_startswith = 1,
2651 .schema = "L,L:s0"
2652 };
2653 cmd_parser = &write_mem_cmd_desc;
44520db1 2654 }
858693c6 2655 break;
56aebc89 2656 case 'p':
5d0e57bd
JD
2657 {
2658 static const GdbCmdParseEntry get_reg_cmd_desc = {
2659 .handler = handle_get_reg,
2660 .cmd = "p",
2661 .cmd_startswith = 1,
2662 .schema = "L0"
2663 };
2664 cmd_parser = &get_reg_cmd_desc;
56aebc89
PB
2665 }
2666 break;
2667 case 'P':
62b3320b
JD
2668 {
2669 static const GdbCmdParseEntry set_reg_cmd_desc = {
2670 .handler = handle_set_reg,
2671 .cmd = "P",
2672 .cmd_startswith = 1,
2673 .schema = "L?s0"
2674 };
2675 cmd_parser = &set_reg_cmd_desc;
2676 }
56aebc89 2677 break;
858693c6 2678 case 'Z':
77f6ce50
JD
2679 {
2680 static const GdbCmdParseEntry insert_bp_cmd_desc = {
2681 .handler = handle_insert_bp,
2682 .cmd = "Z",
2683 .cmd_startswith = 1,
2684 .schema = "l?L?L0"
2685 };
2686 cmd_parser = &insert_bp_cmd_desc;
2687 }
2688 break;
858693c6 2689 case 'z':
77f6ce50
JD
2690 {
2691 static const GdbCmdParseEntry remove_bp_cmd_desc = {
2692 .handler = handle_remove_bp,
2693 .cmd = "z",
2694 .cmd_startswith = 1,
2695 .schema = "l?L?L0"
2696 };
2697 cmd_parser = &remove_bp_cmd_desc;
2698 }
858693c6 2699 break;
880a7578 2700 case 'H':
3a9651d6
JD
2701 {
2702 static const GdbCmdParseEntry set_thread_cmd_desc = {
2703 .handler = handle_set_thread,
2704 .cmd = "H",
2705 .cmd_startswith = 1,
2706 .schema = "o.t0"
2707 };
2708 cmd_parser = &set_thread_cmd_desc;
880a7578
AL
2709 }
2710 break;
2711 case 'T':
44ffded0
JD
2712 {
2713 static const GdbCmdParseEntry thread_alive_cmd_desc = {
2714 .handler = handle_thread_alive,
2715 .cmd = "T",
2716 .cmd_startswith = 1,
2717 .schema = "t0"
2718 };
2719 cmd_parser = &thread_alive_cmd_desc;
1e9fa730 2720 }
880a7578 2721 break;
978efd6a 2722 case 'q':
2704efad
JD
2723 {
2724 static const GdbCmdParseEntry gen_query_cmd_desc = {
2725 .handler = handle_gen_query,
2726 .cmd = "q",
2727 .cmd_startswith = 1,
2728 .schema = "s0"
2729 };
2730 cmd_parser = &gen_query_cmd_desc;
56aebc89 2731 }
2704efad
JD
2732 break;
2733 case 'Q':
2734 {
2735 static const GdbCmdParseEntry gen_set_cmd_desc = {
2736 .handler = handle_gen_set,
2737 .cmd = "Q",
2738 .cmd_startswith = 1,
2739 .schema = "s0"
2740 };
2741 cmd_parser = &gen_set_cmd_desc;
a3919386 2742 }
2704efad 2743 break;
858693c6 2744 default:
858693c6 2745 /* put empty packet */
a346af3e 2746 put_packet("");
858693c6
FB
2747 break;
2748 }
3e2c1261 2749
2bdec398 2750 if (cmd_parser) {
a346af3e 2751 run_cmd_parser(line_buf, cmd_parser);
2bdec398 2752 }
3e2c1261 2753
858693c6
FB
2754 return RS_IDLE;
2755}
2756
64f6b346 2757void gdb_set_stop_cpu(CPUState *cpu)
880a7578 2758{
a346af3e 2759 GDBProcess *p = gdb_get_cpu_process(cpu);
160d858d
LM
2760
2761 if (!p->attached) {
2762 /*
2763 * Having a stop CPU corresponding to a process that is not attached
2764 * confuses GDB. So we ignore the request.
2765 */
2766 return;
2767 }
2768
8d98c445
AB
2769 gdbserver_state.c_cpu = cpu;
2770 gdbserver_state.g_cpu = cpu;
880a7578
AL
2771}
2772
1fddef4b 2773#ifndef CONFIG_USER_ONLY
538f0497 2774static void gdb_vm_state_change(void *opaque, bool running, RunState state)
858693c6 2775{
a346af3e 2776 CPUState *cpu = gdbserver_state.c_cpu;
308f9e88
AB
2777 g_autoptr(GString) buf = g_string_new(NULL);
2778 g_autoptr(GString) tid = g_string_new(NULL);
d6fc1b39 2779 const char *type;
858693c6
FB
2780 int ret;
2781
a346af3e 2782 if (running || gdbserver_state.state == RS_INACTIVE) {
cdb432b2
MI
2783 return;
2784 }
2785 /* Is there a GDB syscall waiting to be sent? */
a346af3e
AB
2786 if (gdbserver_state.current_syscall_cb) {
2787 put_packet(gdbserver_state.syscall_buf);
a2d1ebaf 2788 return;
e07bbac5 2789 }
95567c27
LM
2790
2791 if (cpu == NULL) {
2792 /* No process attached */
2793 return;
2794 }
2795
308f9e88 2796 gdb_append_thread_id(cpu, tid);
95567c27 2797
1dfb4dd9 2798 switch (state) {
0461d5a6 2799 case RUN_STATE_DEBUG:
ff4700b0
AF
2800 if (cpu->watchpoint_hit) {
2801 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
a1d1bb31 2802 case BP_MEM_READ:
d6fc1b39
AL
2803 type = "r";
2804 break;
a1d1bb31 2805 case BP_MEM_ACCESS:
d6fc1b39
AL
2806 type = "a";
2807 break;
2808 default:
2809 type = "";
2810 break;
2811 }
5c9522b3
DG
2812 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
2813 (target_ulong)cpu->watchpoint_hit->vaddr);
308f9e88
AB
2814 g_string_printf(buf, "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
2815 GDB_SIGNAL_TRAP, tid->str, type,
2816 (target_ulong)cpu->watchpoint_hit->vaddr);
ff4700b0 2817 cpu->watchpoint_hit = NULL;
425189a8 2818 goto send_packet;
5c9522b3
DG
2819 } else {
2820 trace_gdbstub_hit_break();
6658ffb8 2821 }
bbd77c18 2822 tb_flush(cpu);
ca587a8e 2823 ret = GDB_SIGNAL_TRAP;
425189a8 2824 break;
0461d5a6 2825 case RUN_STATE_PAUSED:
5c9522b3 2826 trace_gdbstub_hit_paused();
9781e040 2827 ret = GDB_SIGNAL_INT;
425189a8 2828 break;
0461d5a6 2829 case RUN_STATE_SHUTDOWN:
5c9522b3 2830 trace_gdbstub_hit_shutdown();
425189a8
JK
2831 ret = GDB_SIGNAL_QUIT;
2832 break;
0461d5a6 2833 case RUN_STATE_IO_ERROR:
5c9522b3 2834 trace_gdbstub_hit_io_error();
425189a8
JK
2835 ret = GDB_SIGNAL_IO;
2836 break;
0461d5a6 2837 case RUN_STATE_WATCHDOG:
5c9522b3 2838 trace_gdbstub_hit_watchdog();
425189a8
JK
2839 ret = GDB_SIGNAL_ALRM;
2840 break;
0461d5a6 2841 case RUN_STATE_INTERNAL_ERROR:
5c9522b3 2842 trace_gdbstub_hit_internal_error();
425189a8
JK
2843 ret = GDB_SIGNAL_ABRT;
2844 break;
0461d5a6
LC
2845 case RUN_STATE_SAVE_VM:
2846 case RUN_STATE_RESTORE_VM:
425189a8 2847 return;
0461d5a6 2848 case RUN_STATE_FINISH_MIGRATE:
425189a8
JK
2849 ret = GDB_SIGNAL_XCPU;
2850 break;
2851 default:
5c9522b3 2852 trace_gdbstub_hit_unknown(state);
425189a8
JK
2853 ret = GDB_SIGNAL_UNKNOWN;
2854 break;
bbeb7b5c 2855 }
226d007d 2856 gdb_set_stop_cpu(cpu);
308f9e88 2857 g_string_printf(buf, "T%02xthread:%s;", ret, tid->str);
425189a8
JK
2858
2859send_packet:
308f9e88 2860 put_packet(buf->str);
425189a8
JK
2861
2862 /* disable single step if it was enabled */
3825b28f 2863 cpu_single_step(cpu, 0);
858693c6 2864}
1fddef4b 2865#endif
858693c6 2866
a2d1ebaf
PB
2867/* Send a gdb syscall request.
2868 This accepts limited printf-style format specifiers, specifically:
a87295e8
PB
2869 %x - target_ulong argument printed in hex.
2870 %lx - 64-bit argument printed in hex.
2871 %s - string pointer (target_ulong) and length (int) pair. */
19239b39 2872void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
a2d1ebaf 2873{
a2d1ebaf 2874 char *p;
cdb432b2 2875 char *p_end;
a2d1ebaf 2876 target_ulong addr;
a87295e8 2877 uint64_t i64;
a2d1ebaf 2878
a346af3e 2879 if (!gdbserver_state.init) {
a2d1ebaf 2880 return;
a346af3e 2881 }
8d98c445
AB
2882
2883 gdbserver_state.current_syscall_cb = cb;
a2d1ebaf 2884#ifndef CONFIG_USER_ONLY
0461d5a6 2885 vm_stop(RUN_STATE_DEBUG);
a2d1ebaf 2886#endif
8d98c445
AB
2887 p = &gdbserver_state.syscall_buf[0];
2888 p_end = &gdbserver_state.syscall_buf[sizeof(gdbserver_state.syscall_buf)];
a2d1ebaf
PB
2889 *(p++) = 'F';
2890 while (*fmt) {
2891 if (*fmt == '%') {
2892 fmt++;
2893 switch (*fmt++) {
2894 case 'x':
2895 addr = va_arg(va, target_ulong);
cdb432b2 2896 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
a2d1ebaf 2897 break;
a87295e8
PB
2898 case 'l':
2899 if (*(fmt++) != 'x')
2900 goto bad_format;
2901 i64 = va_arg(va, uint64_t);
cdb432b2 2902 p += snprintf(p, p_end - p, "%" PRIx64, i64);
a87295e8 2903 break;
a2d1ebaf
PB
2904 case 's':
2905 addr = va_arg(va, target_ulong);
cdb432b2 2906 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
363a37d5 2907 addr, va_arg(va, int));
a2d1ebaf
PB
2908 break;
2909 default:
a87295e8 2910 bad_format:
7ae6c571
ZY
2911 error_report("gdbstub: Bad syscall format string '%s'",
2912 fmt - 1);
a2d1ebaf
PB
2913 break;
2914 }
2915 } else {
2916 *(p++) = *(fmt++);
2917 }
2918 }
8a93e02a 2919 *p = 0;
a2d1ebaf 2920#ifdef CONFIG_USER_ONLY
a346af3e 2921 put_packet(gdbserver_state.syscall_buf);
4f710866
PM
2922 /* Return control to gdb for it to process the syscall request.
2923 * Since the protocol requires that gdb hands control back to us
2924 * using a "here are the results" F packet, we don't need to check
2925 * gdb_handlesig's return value (which is the signal to deliver if
2926 * execution was resumed via a continue packet).
2927 */
8d98c445 2928 gdb_handlesig(gdbserver_state.c_cpu, 0);
a2d1ebaf 2929#else
cdb432b2
MI
2930 /* In this case wait to send the syscall packet until notification that
2931 the CPU has stopped. This must be done because if the packet is sent
2932 now the reply from the syscall request could be received while the CPU
2933 is still in the running state, which can cause packets to be dropped
2934 and state transition 'T' packets to be sent while the syscall is still
2935 being processed. */
8d98c445 2936 qemu_cpu_kick(gdbserver_state.c_cpu);
a2d1ebaf
PB
2937#endif
2938}
2939
19239b39
PM
2940void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
2941{
2942 va_list va;
2943
2944 va_start(va, fmt);
2945 gdb_do_syscallv(cb, fmt, va);
2946 va_end(va);
2947}
2948
a346af3e 2949static void gdb_read_byte(uint8_t ch)
858693c6 2950{
60fe76f3 2951 uint8_t reply;
858693c6 2952
1fddef4b 2953#ifndef CONFIG_USER_ONLY
d116e813 2954 if (gdbserver_state.last_packet->len) {
4046d913
PB
2955 /* Waiting for a response to the last packet. If we see the start
2956 of a new command then abandon the previous response. */
2957 if (ch == '-') {
5c9522b3 2958 trace_gdbstub_err_got_nack();
d116e813
DH
2959 put_buffer(gdbserver_state.last_packet->data,
2960 gdbserver_state.last_packet->len);
118e2268 2961 } else if (ch == '+') {
5c9522b3 2962 trace_gdbstub_io_got_ack();
118e2268 2963 } else {
33c846ef 2964 trace_gdbstub_io_got_unexpected(ch);
4046d913 2965 }
118e2268 2966
d116e813
DH
2967 if (ch == '+' || ch == '$') {
2968 g_byte_array_set_size(gdbserver_state.last_packet, 0);
2969 }
4046d913
PB
2970 if (ch != '$')
2971 return;
2972 }
1354869c 2973 if (runstate_is_running()) {
858693c6
FB
2974 /* when the CPU is running, we cannot do anything except stop
2975 it when receiving a char */
0461d5a6 2976 vm_stop(RUN_STATE_PAUSED);
5fafdf24 2977 } else
1fddef4b 2978#endif
41625033 2979 {
a346af3e 2980 switch(gdbserver_state.state) {
858693c6
FB
2981 case RS_IDLE:
2982 if (ch == '$') {
4bf43122 2983 /* start of command packet */
a346af3e
AB
2984 gdbserver_state.line_buf_index = 0;
2985 gdbserver_state.line_sum = 0;
2986 gdbserver_state.state = RS_GETLINE;
4bf43122 2987 } else {
33c846ef 2988 trace_gdbstub_err_garbage(ch);
c33a346e 2989 }
b4608c04 2990 break;
858693c6 2991 case RS_GETLINE:
4bf43122
DG
2992 if (ch == '}') {
2993 /* start escape sequence */
a346af3e
AB
2994 gdbserver_state.state = RS_GETLINE_ESC;
2995 gdbserver_state.line_sum += ch;
4bf43122
DG
2996 } else if (ch == '*') {
2997 /* start run length encoding sequence */
a346af3e
AB
2998 gdbserver_state.state = RS_GETLINE_RLE;
2999 gdbserver_state.line_sum += ch;
4bf43122
DG
3000 } else if (ch == '#') {
3001 /* end of command, start of checksum*/
a346af3e
AB
3002 gdbserver_state.state = RS_CHKSUM1;
3003 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
5c9522b3 3004 trace_gdbstub_err_overrun();
a346af3e 3005 gdbserver_state.state = RS_IDLE;
4bf43122
DG
3006 } else {
3007 /* unescaped command character */
a346af3e
AB
3008 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch;
3009 gdbserver_state.line_sum += ch;
4bf43122
DG
3010 }
3011 break;
3012 case RS_GETLINE_ESC:
858693c6 3013 if (ch == '#') {
4bf43122 3014 /* unexpected end of command in escape sequence */
a346af3e
AB
3015 gdbserver_state.state = RS_CHKSUM1;
3016 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
4bf43122 3017 /* command buffer overrun */
5c9522b3 3018 trace_gdbstub_err_overrun();
a346af3e 3019 gdbserver_state.state = RS_IDLE;
4c3a88a2 3020 } else {
4bf43122 3021 /* parse escaped character and leave escape state */
a346af3e
AB
3022 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20;
3023 gdbserver_state.line_sum += ch;
3024 gdbserver_state.state = RS_GETLINE;
4bf43122
DG
3025 }
3026 break;
3027 case RS_GETLINE_RLE:
046aba16
MA
3028 /*
3029 * Run-length encoding is explained in "Debugging with GDB /
3030 * Appendix E GDB Remote Serial Protocol / Overview".
3031 */
3032 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
4bf43122 3033 /* invalid RLE count encoding */
33c846ef 3034 trace_gdbstub_err_invalid_repeat(ch);
a346af3e 3035 gdbserver_state.state = RS_GETLINE;
4bf43122
DG
3036 } else {
3037 /* decode repeat length */
33c846ef 3038 int repeat = ch - ' ' + 3;
a346af3e 3039 if (gdbserver_state.line_buf_index + repeat >= sizeof(gdbserver_state.line_buf) - 1) {
4bf43122 3040 /* that many repeats would overrun the command buffer */
5c9522b3 3041 trace_gdbstub_err_overrun();
a346af3e
AB
3042 gdbserver_state.state = RS_IDLE;
3043 } else if (gdbserver_state.line_buf_index < 1) {
4bf43122 3044 /* got a repeat but we have nothing to repeat */
5c9522b3 3045 trace_gdbstub_err_invalid_rle();
a346af3e 3046 gdbserver_state.state = RS_GETLINE;
4bf43122
DG
3047 } else {
3048 /* repeat the last character */
a346af3e
AB
3049 memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index,
3050 gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat);
3051 gdbserver_state.line_buf_index += repeat;
3052 gdbserver_state.line_sum += ch;
3053 gdbserver_state.state = RS_GETLINE;
4bf43122 3054 }
4c3a88a2
FB
3055 }
3056 break;
858693c6 3057 case RS_CHKSUM1:
4bf43122
DG
3058 /* get high hex digit of checksum */
3059 if (!isxdigit(ch)) {
33c846ef 3060 trace_gdbstub_err_checksum_invalid(ch);
a346af3e 3061 gdbserver_state.state = RS_GETLINE;
4bf43122
DG
3062 break;
3063 }
a346af3e
AB
3064 gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0';
3065 gdbserver_state.line_csum = fromhex(ch) << 4;
3066 gdbserver_state.state = RS_CHKSUM2;
858693c6
FB
3067 break;
3068 case RS_CHKSUM2:
4bf43122
DG
3069 /* get low hex digit of checksum */
3070 if (!isxdigit(ch)) {
33c846ef 3071 trace_gdbstub_err_checksum_invalid(ch);
a346af3e 3072 gdbserver_state.state = RS_GETLINE;
4bf43122 3073 break;
858693c6 3074 }
a346af3e 3075 gdbserver_state.line_csum |= fromhex(ch);
4bf43122 3076
a346af3e
AB
3077 if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) {
3078 trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum);
4bf43122 3079 /* send NAK reply */
60fe76f3 3080 reply = '-';
a346af3e
AB
3081 put_buffer(&reply, 1);
3082 gdbserver_state.state = RS_IDLE;
4c3a88a2 3083 } else {
4bf43122 3084 /* send ACK reply */
60fe76f3 3085 reply = '+';
a346af3e
AB
3086 put_buffer(&reply, 1);
3087 gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf);
4c3a88a2
FB
3088 }
3089 break;
a2d1ebaf
PB
3090 default:
3091 abort();
858693c6
FB
3092 }
3093 }
3094}
3095
0e1c9c54 3096/* Tell the remote gdb that the process has exited. */
ad9dcb20 3097void gdb_exit(int code)
0e1c9c54 3098{
0e1c9c54
PB
3099 char buf[4];
3100
8d98c445 3101 if (!gdbserver_state.init) {
0e1c9c54
PB
3102 return;
3103 }
3104#ifdef CONFIG_USER_ONLY
fcedd920
AB
3105 if (gdbserver_state.socket_path) {
3106 unlink(gdbserver_state.socket_path);
3107 }
e0a1e208 3108 if (gdbserver_state.fd < 0) {
0e1c9c54
PB
3109 return;
3110 }
3111#endif
3112
5c9522b3
DG
3113 trace_gdbstub_op_exiting((uint8_t)code);
3114
0e1c9c54 3115 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
a346af3e 3116 put_packet(buf);
e2af15b2
FC
3117
3118#ifndef CONFIG_USER_ONLY
8d98c445 3119 qemu_chr_fe_deinit(&gdbserver_state.chr, true);
e2af15b2 3120#endif
0e1c9c54
PB
3121}
3122
8f468636
LM
3123/*
3124 * Create the process that will contain all the "orphan" CPUs (that are not
3125 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
3126 * be attachable and thus will be invisible to the user.
3127 */
3128static void create_default_process(GDBState *s)
3129{
3130 GDBProcess *process;
3131 int max_pid = 0;
3132
a346af3e 3133 if (gdbserver_state.process_num) {
8f468636
LM
3134 max_pid = s->processes[s->process_num - 1].pid;
3135 }
3136
3137 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
3138 process = &s->processes[s->process_num - 1];
3139
3140 /* We need an available PID slot for this process */
3141 assert(max_pid < UINT32_MAX);
3142
3143 process->pid = max_pid + 1;
3144 process->attached = false;
c145eeae 3145 process->target_xml[0] = '\0';
8f468636
LM
3146}
3147
1fddef4b
FB
3148#ifdef CONFIG_USER_ONLY
3149int
db6b81d4 3150gdb_handlesig(CPUState *cpu, int sig)
1fddef4b 3151{
5ca666c7
AF
3152 char buf[256];
3153 int n;
1fddef4b 3154
e0a1e208 3155 if (!gdbserver_state.init || gdbserver_state.fd < 0) {
5ca666c7
AF
3156 return sig;
3157 }
1fddef4b 3158
5ca666c7 3159 /* disable single step if it was enabled */
3825b28f 3160 cpu_single_step(cpu, 0);
bbd77c18 3161 tb_flush(cpu);
1fddef4b 3162
5ca666c7 3163 if (sig != 0) {
4a82be77
PL
3164 gdb_set_stop_cpu(cpu);
3165 g_string_printf(gdbserver_state.str_buf,
3166 "T%02xthread:", target_signal_to_gdb(sig));
3167 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
3168 g_string_append_c(gdbserver_state.str_buf, ';');
3169 put_strbuf();
5ca666c7
AF
3170 }
3171 /* put_packet() might have detected that the peer terminated the
3172 connection. */
8d98c445 3173 if (gdbserver_state.fd < 0) {
5ca666c7
AF
3174 return sig;
3175 }
1fddef4b 3176
5ca666c7 3177 sig = 0;
8d98c445
AB
3178 gdbserver_state.state = RS_IDLE;
3179 gdbserver_state.running_state = 0;
3180 while (gdbserver_state.running_state == 0) {
3181 n = read(gdbserver_state.fd, buf, 256);
5ca666c7
AF
3182 if (n > 0) {
3183 int i;
3184
3185 for (i = 0; i < n; i++) {
a346af3e 3186 gdb_read_byte(buf[i]);
5ca666c7 3187 }
5819e3e0 3188 } else {
5ca666c7
AF
3189 /* XXX: Connection closed. Should probably wait for another
3190 connection before continuing. */
5819e3e0 3191 if (n == 0) {
8d98c445 3192 close(gdbserver_state.fd);
5819e3e0 3193 }
8d98c445 3194 gdbserver_state.fd = -1;
5ca666c7 3195 return sig;
1fddef4b 3196 }
5ca666c7 3197 }
8d98c445
AB
3198 sig = gdbserver_state.signal;
3199 gdbserver_state.signal = 0;
5ca666c7 3200 return sig;
1fddef4b 3201}
e9009676 3202
ca587a8e 3203/* Tell the remote gdb that the process has exited due to SIG. */
9349b4f9 3204void gdb_signalled(CPUArchState *env, int sig)
ca587a8e 3205{
5ca666c7 3206 char buf[4];
ca587a8e 3207
e0a1e208 3208 if (!gdbserver_state.init || gdbserver_state.fd < 0) {
5ca666c7
AF
3209 return;
3210 }
ca587a8e 3211
5ca666c7 3212 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
a346af3e 3213 put_packet(buf);
ca587a8e 3214}
1fddef4b 3215
fcedd920
AB
3216static void gdb_accept_init(int fd)
3217{
3218 init_gdbserver_state();
3219 create_default_process(&gdbserver_state);
3220 gdbserver_state.processes[0].attached = true;
3221 gdbserver_state.c_cpu = gdb_first_attached_cpu();
3222 gdbserver_state.g_cpu = gdbserver_state.c_cpu;
3223 gdbserver_state.fd = fd;
3224 gdb_has_xml = false;
3225}
3226
3227static bool gdb_accept_socket(int gdb_fd)
3228{
3229 int fd;
3230
3231 for(;;) {
3232 fd = accept(gdb_fd, NULL, NULL);
3233 if (fd < 0 && errno != EINTR) {
3234 perror("accept socket");
3235 return false;
3236 } else if (fd >= 0) {
3237 qemu_set_cloexec(fd);
3238 break;
3239 }
3240 }
3241
3242 gdb_accept_init(fd);
3243 return true;
3244}
3245
3246static int gdbserver_open_socket(const char *path)
3247{
fdcdf54d 3248 struct sockaddr_un sockaddr = {};
fcedd920
AB
3249 int fd, ret;
3250
3251 fd = socket(AF_UNIX, SOCK_STREAM, 0);
3252 if (fd < 0) {
3253 perror("create socket");
3254 return -1;
3255 }
3256
3257 sockaddr.sun_family = AF_UNIX;
3258 pstrcpy(sockaddr.sun_path, sizeof(sockaddr.sun_path) - 1, path);
3259 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
3260 if (ret < 0) {
3261 perror("bind socket");
3262 close(fd);
3263 return -1;
3264 }
3265 ret = listen(fd, 1);
3266 if (ret < 0) {
3267 perror("listen socket");
3268 close(fd);
3269 return -1;
3270 }
3271
3272 return fd;
3273}
3274
3275static bool gdb_accept_tcp(int gdb_fd)
858693c6 3276{
fdcdf54d 3277 struct sockaddr_in sockaddr = {};
858693c6 3278 socklen_t len;
bf1c852a 3279 int fd;
858693c6
FB
3280
3281 for(;;) {
3282 len = sizeof(sockaddr);
e0a1e208 3283 fd = accept(gdb_fd, (struct sockaddr *)&sockaddr, &len);
858693c6
FB
3284 if (fd < 0 && errno != EINTR) {
3285 perror("accept");
2f652224 3286 return false;
858693c6 3287 } else if (fd >= 0) {
f5bdd781 3288 qemu_set_cloexec(fd);
b4608c04
FB
3289 break;
3290 }
3291 }
858693c6
FB
3292
3293 /* set short latency */
2f652224
PM
3294 if (socket_set_nodelay(fd)) {
3295 perror("setsockopt");
ead75d84 3296 close(fd);
2f652224
PM
3297 return false;
3298 }
3b46e624 3299
fcedd920 3300 gdb_accept_init(fd);
2f652224 3301 return true;
858693c6
FB
3302}
3303
fcedd920 3304static int gdbserver_open_port(int port)
858693c6
FB
3305{
3306 struct sockaddr_in sockaddr;
6669ca13 3307 int fd, ret;
858693c6
FB
3308
3309 fd = socket(PF_INET, SOCK_STREAM, 0);
3310 if (fd < 0) {
3311 perror("socket");
3312 return -1;
3313 }
f5bdd781 3314 qemu_set_cloexec(fd);
858693c6 3315
6669ca13 3316 socket_set_fast_reuse(fd);
858693c6
FB
3317
3318 sockaddr.sin_family = AF_INET;
3319 sockaddr.sin_port = htons(port);
3320 sockaddr.sin_addr.s_addr = 0;
3321 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
3322 if (ret < 0) {
3323 perror("bind");
bb16172c 3324 close(fd);
858693c6
FB
3325 return -1;
3326 }
96165b9e 3327 ret = listen(fd, 1);
858693c6
FB
3328 if (ret < 0) {
3329 perror("listen");
bb16172c 3330 close(fd);
858693c6
FB
3331 return -1;
3332 }
fcedd920 3333
858693c6
FB
3334 return fd;
3335}
3336
fcedd920 3337int gdbserver_start(const char *port_or_path)
858693c6 3338{
fcedd920
AB
3339 int port = g_ascii_strtoull(port_or_path, NULL, 10);
3340 int gdb_fd;
3341
3342 if (port > 0) {
3343 gdb_fd = gdbserver_open_port(port);
3344 } else {
3345 gdb_fd = gdbserver_open_socket(port_or_path);
3346 }
3347
e0a1e208 3348 if (gdb_fd < 0) {
858693c6 3349 return -1;
e0a1e208 3350 }
fcedd920
AB
3351
3352 if (port > 0 && gdb_accept_tcp(gdb_fd)) {
3353 return 0;
3354 } else if (gdb_accept_socket(gdb_fd)) {
3355 gdbserver_state.socket_path = g_strdup(port_or_path);
3356 return 0;
2f652224 3357 }
fcedd920
AB
3358
3359 /* gone wrong */
3360 close(gdb_fd);
3361 return -1;
4046d913 3362}
2b1319c8
AJ
3363
3364/* Disable gdb stub for child processes. */
f7ec7f7b 3365void gdbserver_fork(CPUState *cpu)
2b1319c8 3366{
e0a1e208 3367 if (!gdbserver_state.init || gdbserver_state.fd < 0) {
75a34036
AF
3368 return;
3369 }
8d98c445
AB
3370 close(gdbserver_state.fd);
3371 gdbserver_state.fd = -1;
b3310ab3 3372 cpu_breakpoint_remove_all(cpu, BP_GDB);
75a34036 3373 cpu_watchpoint_remove_all(cpu, BP_GDB);
2b1319c8 3374}
1fddef4b 3375#else
aa1f17c1 3376static int gdb_chr_can_receive(void *opaque)
4046d913 3377{
56aebc89
PB
3378 /* We can handle an arbitrarily large amount of data.
3379 Pick the maximum packet size, which is as good as anything. */
3380 return MAX_PACKET_LENGTH;
4046d913
PB
3381}
3382
aa1f17c1 3383static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
4046d913 3384{
4046d913
PB
3385 int i;
3386
3387 for (i = 0; i < size; i++) {
a346af3e 3388 gdb_read_byte(buf[i]);
4046d913
PB
3389 }
3390}
3391
083b266f 3392static void gdb_chr_event(void *opaque, QEMUChrEvent event)
4046d913 3393{
970ed906
LM
3394 int i;
3395 GDBState *s = (GDBState *) opaque;
3396
4046d913 3397 switch (event) {
b6b8df56 3398 case CHR_EVENT_OPENED:
970ed906
LM
3399 /* Start with first process attached, others detached */
3400 for (i = 0; i < s->process_num; i++) {
3401 s->processes[i].attached = !i;
3402 }
3403
a346af3e 3404 s->c_cpu = gdb_first_attached_cpu();
970ed906
LM
3405 s->g_cpu = s->c_cpu;
3406
0461d5a6 3407 vm_stop(RUN_STATE_PAUSED);
56357d80 3408 replay_gdb_attached();
5b50e790 3409 gdb_has_xml = false;
4046d913
PB
3410 break;
3411 default:
3412 break;
3413 }
3414}
3415
0ec7b3e7 3416static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
8a34a0fb 3417{
d86b4672
DH
3418 g_autoptr(GString) hex_buf = g_string_new("O");
3419 memtohex(hex_buf, buf, len);
3420 put_packet(hex_buf->str);
8a34a0fb
AL
3421 return len;
3422}
3423
59030a8c
AL
3424#ifndef _WIN32
3425static void gdb_sigterm_handler(int signal)
3426{
1354869c 3427 if (runstate_is_running()) {
0461d5a6 3428 vm_stop(RUN_STATE_PAUSED);
e07bbac5 3429 }
59030a8c
AL
3430}
3431#endif
3432
777357d7
MAL
3433static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
3434 bool *be_opened, Error **errp)
3435{
3436 *be_opened = false;
3437}
3438
3439static void char_gdb_class_init(ObjectClass *oc, void *data)
3440{
3441 ChardevClass *cc = CHARDEV_CLASS(oc);
3442
3443 cc->internal = true;
3444 cc->open = gdb_monitor_open;
3445 cc->chr_write = gdb_monitor_write;
3446}
3447
3448#define TYPE_CHARDEV_GDB "chardev-gdb"
3449
3450static const TypeInfo char_gdb_type_info = {
3451 .name = TYPE_CHARDEV_GDB,
3452 .parent = TYPE_CHARDEV,
3453 .class_init = char_gdb_class_init,
3454};
3455
8f468636
LM
3456static int find_cpu_clusters(Object *child, void *opaque)
3457{
3458 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
3459 GDBState *s = (GDBState *) opaque;
3460 CPUClusterState *cluster = CPU_CLUSTER(child);
3461 GDBProcess *process;
3462
3463 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
3464
3465 process = &s->processes[s->process_num - 1];
3466
3467 /*
3468 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
3469 * runtime, we enforce here that the machine does not use a cluster ID
3470 * that would lead to PID 0.
3471 */
3472 assert(cluster->cluster_id != UINT32_MAX);
3473 process->pid = cluster->cluster_id + 1;
3474 process->attached = false;
c145eeae 3475 process->target_xml[0] = '\0';
8f468636
LM
3476
3477 return 0;
3478 }
3479
3480 return object_child_foreach(child, find_cpu_clusters, opaque);
3481}
3482
3483static int pid_order(const void *a, const void *b)
3484{
3485 GDBProcess *pa = (GDBProcess *) a;
3486 GDBProcess *pb = (GDBProcess *) b;
3487
3488 if (pa->pid < pb->pid) {
3489 return -1;
3490 } else if (pa->pid > pb->pid) {
3491 return 1;
3492 } else {
3493 return 0;
3494 }
3495}
3496
3497static void create_processes(GDBState *s)
3498{
3499 object_child_foreach(object_get_root(), find_cpu_clusters, s);
3500
a346af3e 3501 if (gdbserver_state.processes) {
8f468636 3502 /* Sort by PID */
a346af3e 3503 qsort(gdbserver_state.processes, gdbserver_state.process_num, sizeof(gdbserver_state.processes[0]), pid_order);
8f468636
LM
3504 }
3505
3506 create_default_process(s);
3507}
3508
59030a8c 3509int gdbserver_start(const char *device)
4046d913 3510{
5c9522b3
DG
3511 trace_gdbstub_op_start(device);
3512
59030a8c 3513 char gdbstub_device_name[128];
0ec7b3e7
MAL
3514 Chardev *chr = NULL;
3515 Chardev *mon_chr;
cfc3475a 3516
508b4ecc
ZY
3517 if (!first_cpu) {
3518 error_report("gdbstub: meaningless to attach gdb to a "
3519 "machine without any CPU.");
3520 return -1;
3521 }
3522
59030a8c
AL
3523 if (!device)
3524 return -1;
3525 if (strcmp(device, "none") != 0) {
3526 if (strstart(device, "tcp:", NULL)) {
3527 /* enforce required TCP attributes */
3528 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
a9b1315f 3529 "%s,wait=off,nodelay=on,server=on", device);
59030a8c 3530 device = gdbstub_device_name;
36556b20 3531 }
59030a8c
AL
3532#ifndef _WIN32
3533 else if (strcmp(device, "stdio") == 0) {
3534 struct sigaction act;
4046d913 3535
59030a8c
AL
3536 memset(&act, 0, sizeof(act));
3537 act.sa_handler = gdb_sigterm_handler;
3538 sigaction(SIGINT, &act, NULL);
3539 }
3540#endif
95e30b2a
MAL
3541 /*
3542 * FIXME: it's a bit weird to allow using a mux chardev here
3543 * and implicitly setup a monitor. We may want to break this.
3544 */
4ad6f6cb 3545 chr = qemu_chr_new_noreplay("gdb", device, true, NULL);
36556b20
AL
3546 if (!chr)
3547 return -1;
cfc3475a
PB
3548 }
3549
8d98c445
AB
3550 if (!gdbserver_state.init) {
3551 init_gdbserver_state();
4046d913 3552
36556b20
AL
3553 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
3554
3555 /* Initialize a monitor terminal for gdb */
777357d7 3556 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
4ad6f6cb 3557 NULL, NULL, &error_abort);
8e9119a8 3558 monitor_init_hmp(mon_chr, false, &error_abort);
36556b20 3559 } else {
8d98c445
AB
3560 qemu_chr_fe_deinit(&gdbserver_state.chr, true);
3561 mon_chr = gdbserver_state.mon_chr;
3562 reset_gdbserver_state();
36556b20 3563 }
8f468636 3564
8d98c445 3565 create_processes(&gdbserver_state);
8f468636 3566
32a6ebec 3567 if (chr) {
8d98c445
AB
3568 qemu_chr_fe_init(&gdbserver_state.chr, chr, &error_abort);
3569 qemu_chr_fe_set_handlers(&gdbserver_state.chr, gdb_chr_can_receive,
3570 gdb_chr_receive, gdb_chr_event,
3571 NULL, &gdbserver_state, NULL, true);
32a6ebec 3572 }
8d98c445
AB
3573 gdbserver_state.state = chr ? RS_IDLE : RS_INACTIVE;
3574 gdbserver_state.mon_chr = mon_chr;
3575 gdbserver_state.current_syscall_cb = NULL;
8a34a0fb 3576
b4608c04
FB
3577 return 0;
3578}
777357d7
MAL
3579
3580static void register_types(void)
3581{
3582 type_register_static(&char_gdb_type_info);
3583}
3584
3585type_init(register_types);
4046d913 3586#endif