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