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