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