]> git.ipfire.org Git - thirdparty/qemu.git/blame - gdbstub.c
gdbstub: add multiprocess support to 'H' and 'T' packets
[thirdparty/qemu.git] / gdbstub.c
CommitLineData
b4608c04
FB
1/*
2 * gdb server stub
5fafdf24 3 *
3475187d 4 * Copyright (c) 2003-2005 Fabrice Bellard
b4608c04
FB
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
8167ee88 17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
b4608c04 18 */
d38ea87a 19#include "qemu/osdep.h"
da34e65c 20#include "qapi/error.h"
508b4ecc 21#include "qemu/error-report.h"
f348b6d1 22#include "qemu/cutils.h"
5c9522b3 23#include "trace-root.h"
f348b6d1 24#ifdef CONFIG_USER_ONLY
1fddef4b
FB
25#include "qemu.h"
26#else
83c9089e 27#include "monitor/monitor.h"
8228e353 28#include "chardev/char.h"
4d43a603 29#include "chardev/char-fe.h"
9c17d615 30#include "sysemu/sysemu.h"
022c62cb 31#include "exec/gdbstub.h"
8f468636 32#include "hw/cpu/cluster.h"
1fddef4b 33#endif
67b915a5 34
56aebc89
PB
35#define MAX_PACKET_LENGTH 4096
36
1de7afc9 37#include "qemu/sockets.h"
b3946626 38#include "sysemu/hw_accel.h"
9c17d615 39#include "sysemu/kvm.h"
cfe67cef 40#include "exec/semihost.h"
63c91552 41#include "exec/exec-all.h"
ca587a8e 42
a3919386
JK
43#ifdef CONFIG_USER_ONLY
44#define GDB_ATTACHED "0"
45#else
46#define GDB_ATTACHED "1"
47#endif
48
f3659eee
AF
49static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
50 uint8_t *buf, int len, bool is_write)
44520db1 51{
f3659eee
AF
52 CPUClass *cc = CPU_GET_CLASS(cpu);
53
54 if (cc->memory_rw_debug) {
55 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
56 }
57 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
44520db1 58}
ca587a8e 59
d2a6c857
AB
60/* Return the GDB index for a given vCPU state.
61 *
62 * For user mode this is simply the thread id. In system mode GDB
63 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
64 */
65static inline int cpu_gdb_index(CPUState *cpu)
66{
67#if defined(CONFIG_USER_ONLY)
bd88c780
AB
68 TaskState *ts = (TaskState *) cpu->opaque;
69 return ts->ts_tid;
d2a6c857
AB
70#else
71 return cpu->cpu_index + 1;
72#endif
73}
74
ca587a8e
AJ
75enum {
76 GDB_SIGNAL_0 = 0,
77 GDB_SIGNAL_INT = 2,
425189a8 78 GDB_SIGNAL_QUIT = 3,
ca587a8e 79 GDB_SIGNAL_TRAP = 5,
425189a8
JK
80 GDB_SIGNAL_ABRT = 6,
81 GDB_SIGNAL_ALRM = 14,
82 GDB_SIGNAL_IO = 23,
83 GDB_SIGNAL_XCPU = 24,
ca587a8e
AJ
84 GDB_SIGNAL_UNKNOWN = 143
85};
86
87#ifdef CONFIG_USER_ONLY
88
89/* Map target signal numbers to GDB protocol signal numbers and vice
90 * versa. For user emulation's currently supported systems, we can
91 * assume most signals are defined.
92 */
93
94static int gdb_signal_table[] = {
95 0,
96 TARGET_SIGHUP,
97 TARGET_SIGINT,
98 TARGET_SIGQUIT,
99 TARGET_SIGILL,
100 TARGET_SIGTRAP,
101 TARGET_SIGABRT,
102 -1, /* SIGEMT */
103 TARGET_SIGFPE,
104 TARGET_SIGKILL,
105 TARGET_SIGBUS,
106 TARGET_SIGSEGV,
107 TARGET_SIGSYS,
108 TARGET_SIGPIPE,
109 TARGET_SIGALRM,
110 TARGET_SIGTERM,
111 TARGET_SIGURG,
112 TARGET_SIGSTOP,
113 TARGET_SIGTSTP,
114 TARGET_SIGCONT,
115 TARGET_SIGCHLD,
116 TARGET_SIGTTIN,
117 TARGET_SIGTTOU,
118 TARGET_SIGIO,
119 TARGET_SIGXCPU,
120 TARGET_SIGXFSZ,
121 TARGET_SIGVTALRM,
122 TARGET_SIGPROF,
123 TARGET_SIGWINCH,
124 -1, /* SIGLOST */
125 TARGET_SIGUSR1,
126 TARGET_SIGUSR2,
c72d5bf8 127#ifdef TARGET_SIGPWR
ca587a8e 128 TARGET_SIGPWR,
c72d5bf8
BS
129#else
130 -1,
131#endif
ca587a8e
AJ
132 -1, /* SIGPOLL */
133 -1,
134 -1,
135 -1,
136 -1,
137 -1,
138 -1,
139 -1,
140 -1,
141 -1,
142 -1,
143 -1,
c72d5bf8 144#ifdef __SIGRTMIN
ca587a8e
AJ
145 __SIGRTMIN + 1,
146 __SIGRTMIN + 2,
147 __SIGRTMIN + 3,
148 __SIGRTMIN + 4,
149 __SIGRTMIN + 5,
150 __SIGRTMIN + 6,
151 __SIGRTMIN + 7,
152 __SIGRTMIN + 8,
153 __SIGRTMIN + 9,
154 __SIGRTMIN + 10,
155 __SIGRTMIN + 11,
156 __SIGRTMIN + 12,
157 __SIGRTMIN + 13,
158 __SIGRTMIN + 14,
159 __SIGRTMIN + 15,
160 __SIGRTMIN + 16,
161 __SIGRTMIN + 17,
162 __SIGRTMIN + 18,
163 __SIGRTMIN + 19,
164 __SIGRTMIN + 20,
165 __SIGRTMIN + 21,
166 __SIGRTMIN + 22,
167 __SIGRTMIN + 23,
168 __SIGRTMIN + 24,
169 __SIGRTMIN + 25,
170 __SIGRTMIN + 26,
171 __SIGRTMIN + 27,
172 __SIGRTMIN + 28,
173 __SIGRTMIN + 29,
174 __SIGRTMIN + 30,
175 __SIGRTMIN + 31,
176 -1, /* SIGCANCEL */
177 __SIGRTMIN,
178 __SIGRTMIN + 32,
179 __SIGRTMIN + 33,
180 __SIGRTMIN + 34,
181 __SIGRTMIN + 35,
182 __SIGRTMIN + 36,
183 __SIGRTMIN + 37,
184 __SIGRTMIN + 38,
185 __SIGRTMIN + 39,
186 __SIGRTMIN + 40,
187 __SIGRTMIN + 41,
188 __SIGRTMIN + 42,
189 __SIGRTMIN + 43,
190 __SIGRTMIN + 44,
191 __SIGRTMIN + 45,
192 __SIGRTMIN + 46,
193 __SIGRTMIN + 47,
194 __SIGRTMIN + 48,
195 __SIGRTMIN + 49,
196 __SIGRTMIN + 50,
197 __SIGRTMIN + 51,
198 __SIGRTMIN + 52,
199 __SIGRTMIN + 53,
200 __SIGRTMIN + 54,
201 __SIGRTMIN + 55,
202 __SIGRTMIN + 56,
203 __SIGRTMIN + 57,
204 __SIGRTMIN + 58,
205 __SIGRTMIN + 59,
206 __SIGRTMIN + 60,
207 __SIGRTMIN + 61,
208 __SIGRTMIN + 62,
209 __SIGRTMIN + 63,
210 __SIGRTMIN + 64,
211 __SIGRTMIN + 65,
212 __SIGRTMIN + 66,
213 __SIGRTMIN + 67,
214 __SIGRTMIN + 68,
215 __SIGRTMIN + 69,
216 __SIGRTMIN + 70,
217 __SIGRTMIN + 71,
218 __SIGRTMIN + 72,
219 __SIGRTMIN + 73,
220 __SIGRTMIN + 74,
221 __SIGRTMIN + 75,
222 __SIGRTMIN + 76,
223 __SIGRTMIN + 77,
224 __SIGRTMIN + 78,
225 __SIGRTMIN + 79,
226 __SIGRTMIN + 80,
227 __SIGRTMIN + 81,
228 __SIGRTMIN + 82,
229 __SIGRTMIN + 83,
230 __SIGRTMIN + 84,
231 __SIGRTMIN + 85,
232 __SIGRTMIN + 86,
233 __SIGRTMIN + 87,
234 __SIGRTMIN + 88,
235 __SIGRTMIN + 89,
236 __SIGRTMIN + 90,
237 __SIGRTMIN + 91,
238 __SIGRTMIN + 92,
239 __SIGRTMIN + 93,
240 __SIGRTMIN + 94,
241 __SIGRTMIN + 95,
242 -1, /* SIGINFO */
243 -1, /* UNKNOWN */
244 -1, /* DEFAULT */
245 -1,
246 -1,
247 -1,
248 -1,
249 -1,
250 -1
c72d5bf8 251#endif
ca587a8e 252};
8f447cc7 253#else
ca587a8e
AJ
254/* In system mode we only need SIGINT and SIGTRAP; other signals
255 are not yet supported. */
256
257enum {
258 TARGET_SIGINT = 2,
259 TARGET_SIGTRAP = 5
260};
261
262static int gdb_signal_table[] = {
263 -1,
264 -1,
265 TARGET_SIGINT,
266 -1,
267 -1,
268 TARGET_SIGTRAP
269};
270#endif
271
272#ifdef CONFIG_USER_ONLY
273static int target_signal_to_gdb (int sig)
274{
275 int i;
276 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
277 if (gdb_signal_table[i] == sig)
278 return i;
279 return GDB_SIGNAL_UNKNOWN;
280}
8f447cc7 281#endif
b4608c04 282
ca587a8e
AJ
283static int gdb_signal_to_target (int sig)
284{
285 if (sig < ARRAY_SIZE (gdb_signal_table))
286 return gdb_signal_table[sig];
287 else
288 return -1;
289}
290
56aebc89
PB
291typedef struct GDBRegisterState {
292 int base_reg;
293 int num_regs;
294 gdb_reg_cb get_reg;
295 gdb_reg_cb set_reg;
296 const char *xml;
297 struct GDBRegisterState *next;
298} GDBRegisterState;
299
8f468636
LM
300typedef struct GDBProcess {
301 uint32_t pid;
302 bool attached;
303} GDBProcess;
304
858693c6 305enum RSState {
36556b20 306 RS_INACTIVE,
858693c6
FB
307 RS_IDLE,
308 RS_GETLINE,
4bf43122
DG
309 RS_GETLINE_ESC,
310 RS_GETLINE_RLE,
858693c6
FB
311 RS_CHKSUM1,
312 RS_CHKSUM2,
313};
858693c6 314typedef struct GDBState {
2e0f2cfb
AF
315 CPUState *c_cpu; /* current CPU for step/continue ops */
316 CPUState *g_cpu; /* current CPU for other ops */
52f34623 317 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
41625033 318 enum RSState state; /* parsing state */
56aebc89 319 char line_buf[MAX_PACKET_LENGTH];
858693c6 320 int line_buf_index;
4bf43122
DG
321 int line_sum; /* running checksum */
322 int line_csum; /* checksum at the end of the packet */
56aebc89 323 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
4046d913 324 int last_packet_len;
1f487ee9 325 int signal;
41625033 326#ifdef CONFIG_USER_ONLY
4046d913 327 int fd;
41625033 328 int running_state;
4046d913 329#else
32a6ebec 330 CharBackend chr;
0ec7b3e7 331 Chardev *mon_chr;
41625033 332#endif
8f468636
LM
333 bool multiprocess;
334 GDBProcess *processes;
335 int process_num;
cdb432b2
MI
336 char syscall_buf[256];
337 gdb_syscall_complete_cb current_syscall_cb;
858693c6 338} GDBState;
b4608c04 339
60897d36
EI
340/* By default use no IRQs and no timers while single stepping so as to
341 * make single stepping like an ICE HW step.
342 */
343static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
344
880a7578
AL
345static GDBState *gdbserver_state;
346
5b50e790 347bool gdb_has_xml;
56aebc89 348
1fddef4b 349#ifdef CONFIG_USER_ONLY
4046d913
PB
350/* XXX: This is not thread safe. Do we care? */
351static int gdbserver_fd = -1;
352
858693c6 353static int get_char(GDBState *s)
b4608c04
FB
354{
355 uint8_t ch;
356 int ret;
357
358 for(;;) {
00aa0040 359 ret = qemu_recv(s->fd, &ch, 1, 0);
b4608c04 360 if (ret < 0) {
1f487ee9
EI
361 if (errno == ECONNRESET)
362 s->fd = -1;
5819e3e0 363 if (errno != EINTR)
b4608c04
FB
364 return -1;
365 } else if (ret == 0) {
1f487ee9
EI
366 close(s->fd);
367 s->fd = -1;
b4608c04
FB
368 return -1;
369 } else {
370 break;
371 }
372 }
373 return ch;
374}
4046d913 375#endif
b4608c04 376
654efcf3 377static enum {
a2d1ebaf
PB
378 GDB_SYS_UNKNOWN,
379 GDB_SYS_ENABLED,
380 GDB_SYS_DISABLED,
381} gdb_syscall_mode;
382
a38bb079 383/* Decide if either remote gdb syscalls or native file IO should be used. */
a2d1ebaf
PB
384int use_gdb_syscalls(void)
385{
cfe67cef
LA
386 SemihostingTarget target = semihosting_get_target();
387 if (target == SEMIHOSTING_TARGET_NATIVE) {
a38bb079
LI
388 /* -semihosting-config target=native */
389 return false;
cfe67cef 390 } else if (target == SEMIHOSTING_TARGET_GDB) {
a38bb079
LI
391 /* -semihosting-config target=gdb */
392 return true;
393 }
394
395 /* -semihosting-config target=auto */
396 /* On the first call check if gdb is connected and remember. */
a2d1ebaf 397 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
880a7578
AL
398 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
399 : GDB_SYS_DISABLED);
a2d1ebaf
PB
400 }
401 return gdb_syscall_mode == GDB_SYS_ENABLED;
402}
403
ba70a624
EI
404/* Resume execution. */
405static inline void gdb_continue(GDBState *s)
406{
5c9522b3 407
ba70a624
EI
408#ifdef CONFIG_USER_ONLY
409 s->running_state = 1;
5c9522b3 410 trace_gdbstub_op_continue();
ba70a624 411#else
26ac7a31 412 if (!runstate_needs_reset()) {
5c9522b3 413 trace_gdbstub_op_continue();
87f25c12
PB
414 vm_start();
415 }
ba70a624
EI
416#endif
417}
418
544177ad
CI
419/*
420 * Resume execution, per CPU actions. For user-mode emulation it's
421 * equivalent to gdb_continue.
422 */
423static int gdb_continue_partial(GDBState *s, char *newstates)
424{
425 CPUState *cpu;
426 int res = 0;
427#ifdef CONFIG_USER_ONLY
428 /*
429 * This is not exactly accurate, but it's an improvement compared to the
430 * previous situation, where only one CPU would be single-stepped.
431 */
432 CPU_FOREACH(cpu) {
433 if (newstates[cpu->cpu_index] == 's') {
5c9522b3 434 trace_gdbstub_op_stepping(cpu->cpu_index);
544177ad
CI
435 cpu_single_step(cpu, sstep_flags);
436 }
437 }
438 s->running_state = 1;
439#else
440 int flag = 0;
441
442 if (!runstate_needs_reset()) {
443 if (vm_prepare_start()) {
444 return 0;
445 }
446
447 CPU_FOREACH(cpu) {
448 switch (newstates[cpu->cpu_index]) {
449 case 0:
450 case 1:
451 break; /* nothing to do here */
452 case 's':
5c9522b3 453 trace_gdbstub_op_stepping(cpu->cpu_index);
544177ad
CI
454 cpu_single_step(cpu, sstep_flags);
455 cpu_resume(cpu);
456 flag = 1;
457 break;
458 case 'c':
5c9522b3 459 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
544177ad
CI
460 cpu_resume(cpu);
461 flag = 1;
462 break;
463 default:
464 res = -1;
465 break;
466 }
467 }
468 }
469 if (flag) {
470 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
471 }
472#endif
473 return res;
474}
475
858693c6 476static void put_buffer(GDBState *s, const uint8_t *buf, int len)
b4608c04 477{
4046d913 478#ifdef CONFIG_USER_ONLY
b4608c04
FB
479 int ret;
480
481 while (len > 0) {
8f447cc7 482 ret = send(s->fd, buf, len, 0);
b4608c04 483 if (ret < 0) {
5819e3e0 484 if (errno != EINTR)
b4608c04
FB
485 return;
486 } else {
487 buf += ret;
488 len -= ret;
489 }
490 }
4046d913 491#else
6ab3fc32
DB
492 /* XXX this blocks entire thread. Rewrite to use
493 * qemu_chr_fe_write and background I/O callbacks */
5345fdb4 494 qemu_chr_fe_write_all(&s->chr, buf, len);
4046d913 495#endif
b4608c04
FB
496}
497
498static inline int fromhex(int v)
499{
500 if (v >= '0' && v <= '9')
501 return v - '0';
502 else if (v >= 'A' && v <= 'F')
503 return v - 'A' + 10;
504 else if (v >= 'a' && v <= 'f')
505 return v - 'a' + 10;
506 else
507 return 0;
508}
509
510static inline int tohex(int v)
511{
512 if (v < 10)
513 return v + '0';
514 else
515 return v - 10 + 'a';
516}
517
9005774b 518/* writes 2*len+1 bytes in buf */
b4608c04
FB
519static void memtohex(char *buf, const uint8_t *mem, int len)
520{
521 int i, c;
522 char *q;
523 q = buf;
524 for(i = 0; i < len; i++) {
525 c = mem[i];
526 *q++ = tohex(c >> 4);
527 *q++ = tohex(c & 0xf);
528 }
529 *q = '\0';
530}
531
532static void hextomem(uint8_t *mem, const char *buf, int len)
533{
534 int i;
535
536 for(i = 0; i < len; i++) {
537 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
538 buf += 2;
539 }
540}
541
5c9522b3
DG
542static void hexdump(const char *buf, int len,
543 void (*trace_fn)(size_t ofs, char const *text))
544{
545 char line_buffer[3 * 16 + 4 + 16 + 1];
546
547 size_t i;
548 for (i = 0; i < len || (i & 0xF); ++i) {
549 size_t byte_ofs = i & 15;
550
551 if (byte_ofs == 0) {
552 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
553 line_buffer[3 * 16 + 4 + 16] = 0;
554 }
555
556 size_t col_group = (i >> 2) & 3;
557 size_t hex_col = byte_ofs * 3 + col_group;
558 size_t txt_col = 3 * 16 + 4 + byte_ofs;
559
560 if (i < len) {
561 char value = buf[i];
562
563 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
564 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
565 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
566 ? value
567 : '.';
568 }
569
570 if (byte_ofs == 0xF)
571 trace_fn(i & -16, line_buffer);
572 }
573}
574
b4608c04 575/* return -1 if error, 0 if OK */
5c9522b3 576static int put_packet_binary(GDBState *s, const char *buf, int len, bool dump)
b4608c04 577{
56aebc89 578 int csum, i;
60fe76f3 579 uint8_t *p;
b4608c04 580
5c9522b3
DG
581 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
582 hexdump(buf, len, trace_gdbstub_io_binaryreply);
583 }
584
b4608c04 585 for(;;) {
4046d913
PB
586 p = s->last_packet;
587 *(p++) = '$';
4046d913
PB
588 memcpy(p, buf, len);
589 p += len;
b4608c04
FB
590 csum = 0;
591 for(i = 0; i < len; i++) {
592 csum += buf[i];
593 }
4046d913
PB
594 *(p++) = '#';
595 *(p++) = tohex((csum >> 4) & 0xf);
596 *(p++) = tohex((csum) & 0xf);
b4608c04 597
4046d913 598 s->last_packet_len = p - s->last_packet;
ffe8ab83 599 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
b4608c04 600
4046d913
PB
601#ifdef CONFIG_USER_ONLY
602 i = get_char(s);
603 if (i < 0)
b4608c04 604 return -1;
4046d913 605 if (i == '+')
b4608c04 606 break;
4046d913
PB
607#else
608 break;
609#endif
b4608c04
FB
610 }
611 return 0;
612}
613
56aebc89
PB
614/* return -1 if error, 0 if OK */
615static int put_packet(GDBState *s, const char *buf)
616{
5c9522b3 617 trace_gdbstub_io_reply(buf);
79808573 618
5c9522b3 619 return put_packet_binary(s, buf, strlen(buf), false);
56aebc89
PB
620}
621
56aebc89
PB
622/* Encode data using the encoding for 'x' packets. */
623static int memtox(char *buf, const char *mem, int len)
624{
625 char *p = buf;
626 char c;
627
628 while (len--) {
629 c = *(mem++);
630 switch (c) {
631 case '#': case '$': case '*': case '}':
632 *(p++) = '}';
633 *(p++) = c ^ 0x20;
634 break;
635 default:
636 *(p++) = c;
637 break;
638 }
639 }
640 return p - buf;
641}
f1ccf904 642
1a227336
LM
643static uint32_t gdb_get_cpu_pid(const GDBState *s, CPUState *cpu)
644{
645#ifndef CONFIG_USER_ONLY
646 gchar *path, *name = NULL;
647 Object *obj;
648 CPUClusterState *cluster;
649 uint32_t ret;
650
651 path = object_get_canonical_path(OBJECT(cpu));
652
653 if (path == NULL) {
654 /* Return the default process' PID */
655 ret = s->processes[s->process_num - 1].pid;
656 goto out;
657 }
658
659 name = object_get_canonical_path_component(OBJECT(cpu));
660 assert(name != NULL);
661
662 /*
663 * Retrieve the CPU parent path by removing the last '/' and the CPU name
664 * from the CPU canonical path.
665 */
666 path[strlen(path) - strlen(name) - 1] = '\0';
667
668 obj = object_resolve_path_type(path, TYPE_CPU_CLUSTER, NULL);
669
670 if (obj == NULL) {
671 /* Return the default process' PID */
672 ret = s->processes[s->process_num - 1].pid;
673 goto out;
674 }
675
676 cluster = CPU_CLUSTER(obj);
677 ret = cluster->cluster_id + 1;
678
679out:
680 g_free(name);
681 g_free(path);
682
683 return ret;
684
685#else
686 /* TODO: In user mode, we should use the task state PID */
687 return s->processes[s->process_num - 1].pid;
688#endif
689}
690
7d8c87da
LM
691static GDBProcess *gdb_get_process(const GDBState *s, uint32_t pid)
692{
693 int i;
694
695 if (!pid) {
696 /* 0 means any process, we take the first one */
697 return &s->processes[0];
698 }
699
700 for (i = 0; i < s->process_num; i++) {
701 if (s->processes[i].pid == pid) {
702 return &s->processes[i];
703 }
704 }
705
706 return NULL;
707}
708
709static GDBProcess *gdb_get_cpu_process(const GDBState *s, CPUState *cpu)
710{
711 return gdb_get_process(s, gdb_get_cpu_pid(s, cpu));
712}
713
714static CPUState *find_cpu(uint32_t thread_id)
715{
716 CPUState *cpu;
717
718 CPU_FOREACH(cpu) {
719 if (cpu_gdb_index(cpu) == thread_id) {
720 return cpu;
721 }
722 }
723
724 return NULL;
725}
726
727static CPUState *gdb_get_cpu(const GDBState *s, uint32_t pid, uint32_t tid)
728{
729 GDBProcess *process;
730 CPUState *cpu;
731
732 if (!tid) {
733 /* 0 means any thread, we take the first one */
734 tid = 1;
735 }
736
737 cpu = find_cpu(tid);
738
739 if (cpu == NULL) {
740 return NULL;
741 }
742
743 process = gdb_get_cpu_process(s, cpu);
744
745 if (process->pid != pid) {
746 return NULL;
747 }
748
749 if (!process->attached) {
750 return NULL;
751 }
752
753 return cpu;
754}
755
5b24c641
AF
756static const char *get_feature_xml(const char *p, const char **newp,
757 CPUClass *cc)
56aebc89 758{
56aebc89
PB
759 size_t len;
760 int i;
761 const char *name;
762 static char target_xml[1024];
763
764 len = 0;
765 while (p[len] && p[len] != ':')
766 len++;
767 *newp = p + len;
768
769 name = NULL;
770 if (strncmp(p, "target.xml", len) == 0) {
771 /* Generate the XML description for this CPU. */
772 if (!target_xml[0]) {
773 GDBRegisterState *r;
eac8b355 774 CPUState *cpu = first_cpu;
56aebc89 775
b3820e6c
DH
776 pstrcat(target_xml, sizeof(target_xml),
777 "<?xml version=\"1.0\"?>"
778 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
779 "<target>");
780 if (cc->gdb_arch_name) {
781 gchar *arch = cc->gdb_arch_name(cpu);
782 pstrcat(target_xml, sizeof(target_xml), "<architecture>");
783 pstrcat(target_xml, sizeof(target_xml), arch);
784 pstrcat(target_xml, sizeof(target_xml), "</architecture>");
785 g_free(arch);
786 }
787 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
788 pstrcat(target_xml, sizeof(target_xml), cc->gdb_core_xml_file);
789 pstrcat(target_xml, sizeof(target_xml), "\"/>");
eac8b355 790 for (r = cpu->gdb_regs; r; r = r->next) {
2dc766da
BS
791 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
792 pstrcat(target_xml, sizeof(target_xml), r->xml);
793 pstrcat(target_xml, sizeof(target_xml), "\"/>");
56aebc89 794 }
2dc766da 795 pstrcat(target_xml, sizeof(target_xml), "</target>");
56aebc89
PB
796 }
797 return target_xml;
798 }
200bf5b7
AB
799 if (cc->gdb_get_dynamic_xml) {
800 CPUState *cpu = first_cpu;
801 char *xmlname = g_strndup(p, len);
802 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
803
804 g_free(xmlname);
805 if (xml) {
806 return xml;
807 }
808 }
56aebc89
PB
809 for (i = 0; ; i++) {
810 name = xml_builtin[i][0];
811 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
812 break;
813 }
814 return name ? xml_builtin[i][1] : NULL;
815}
f1ccf904 816
385b9f0e 817static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
56aebc89 818{
a0e372f0 819 CPUClass *cc = CPU_GET_CLASS(cpu);
385b9f0e 820 CPUArchState *env = cpu->env_ptr;
56aebc89 821 GDBRegisterState *r;
f1ccf904 822
a0e372f0 823 if (reg < cc->gdb_num_core_regs) {
5b50e790 824 return cc->gdb_read_register(cpu, mem_buf, reg);
a0e372f0 825 }
f1ccf904 826
eac8b355 827 for (r = cpu->gdb_regs; r; r = r->next) {
56aebc89
PB
828 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
829 return r->get_reg(env, mem_buf, reg - r->base_reg);
830 }
831 }
832 return 0;
f1ccf904
TS
833}
834
385b9f0e 835static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
f1ccf904 836{
a0e372f0 837 CPUClass *cc = CPU_GET_CLASS(cpu);
385b9f0e 838 CPUArchState *env = cpu->env_ptr;
56aebc89 839 GDBRegisterState *r;
f1ccf904 840
a0e372f0 841 if (reg < cc->gdb_num_core_regs) {
5b50e790 842 return cc->gdb_write_register(cpu, mem_buf, reg);
a0e372f0 843 }
56aebc89 844
eac8b355 845 for (r = cpu->gdb_regs; r; r = r->next) {
56aebc89
PB
846 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
847 return r->set_reg(env, mem_buf, reg - r->base_reg);
848 }
849 }
6da41eaf
FB
850 return 0;
851}
852
56aebc89
PB
853/* Register a supplemental set of CPU registers. If g_pos is nonzero it
854 specifies the first register number and these registers are included in
855 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
856 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
857 */
858
22169d41
AF
859void gdb_register_coprocessor(CPUState *cpu,
860 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
861 int num_regs, const char *xml, int g_pos)
6da41eaf 862{
56aebc89
PB
863 GDBRegisterState *s;
864 GDBRegisterState **p;
56aebc89 865
eac8b355 866 p = &cpu->gdb_regs;
56aebc89
PB
867 while (*p) {
868 /* Check for duplicates. */
869 if (strcmp((*p)->xml, xml) == 0)
870 return;
871 p = &(*p)->next;
872 }
9643c25f
SW
873
874 s = g_new0(GDBRegisterState, 1);
a0e372f0 875 s->base_reg = cpu->gdb_num_regs;
9643c25f
SW
876 s->num_regs = num_regs;
877 s->get_reg = get_reg;
878 s->set_reg = set_reg;
879 s->xml = xml;
880
56aebc89 881 /* Add to end of list. */
a0e372f0 882 cpu->gdb_num_regs += num_regs;
56aebc89
PB
883 *p = s;
884 if (g_pos) {
885 if (g_pos != s->base_reg) {
7ae6c571
ZY
886 error_report("Error: Bad gdb register numbering for '%s', "
887 "expected %d got %d", xml, g_pos, s->base_reg);
35143f01
AF
888 } else {
889 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
56aebc89
PB
890 }
891 }
6da41eaf
FB
892}
893
a1d1bb31 894#ifndef CONFIG_USER_ONLY
2472b6c0
PM
895/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
896static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
897{
898 static const int xlat[] = {
899 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
900 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
901 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
902 };
903
904 CPUClass *cc = CPU_GET_CLASS(cpu);
905 int cputype = xlat[gdbtype];
906
907 if (cc->gdb_stop_before_watchpoint) {
908 cputype |= BP_STOP_BEFORE_ACCESS;
909 }
910 return cputype;
911}
a1d1bb31
AL
912#endif
913
880a7578 914static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
a1d1bb31 915{
182735ef 916 CPUState *cpu;
880a7578
AL
917 int err = 0;
918
62278814 919 if (kvm_enabled()) {
2e0f2cfb 920 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
62278814 921 }
e22a25c9 922
a1d1bb31
AL
923 switch (type) {
924 case GDB_BREAKPOINT_SW:
925 case GDB_BREAKPOINT_HW:
bdc44640 926 CPU_FOREACH(cpu) {
b3310ab3
AF
927 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
928 if (err) {
880a7578 929 break;
b3310ab3 930 }
880a7578
AL
931 }
932 return err;
a1d1bb31
AL
933#ifndef CONFIG_USER_ONLY
934 case GDB_WATCHPOINT_WRITE:
935 case GDB_WATCHPOINT_READ:
936 case GDB_WATCHPOINT_ACCESS:
bdc44640 937 CPU_FOREACH(cpu) {
2472b6c0
PM
938 err = cpu_watchpoint_insert(cpu, addr, len,
939 xlat_gdb_type(cpu, type), NULL);
940 if (err) {
880a7578 941 break;
2472b6c0 942 }
880a7578
AL
943 }
944 return err;
a1d1bb31
AL
945#endif
946 default:
947 return -ENOSYS;
948 }
949}
950
880a7578 951static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
a1d1bb31 952{
182735ef 953 CPUState *cpu;
880a7578
AL
954 int err = 0;
955
62278814 956 if (kvm_enabled()) {
2e0f2cfb 957 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
62278814 958 }
e22a25c9 959
a1d1bb31
AL
960 switch (type) {
961 case GDB_BREAKPOINT_SW:
962 case GDB_BREAKPOINT_HW:
bdc44640 963 CPU_FOREACH(cpu) {
b3310ab3
AF
964 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
965 if (err) {
880a7578 966 break;
b3310ab3 967 }
880a7578
AL
968 }
969 return err;
a1d1bb31
AL
970#ifndef CONFIG_USER_ONLY
971 case GDB_WATCHPOINT_WRITE:
972 case GDB_WATCHPOINT_READ:
973 case GDB_WATCHPOINT_ACCESS:
bdc44640 974 CPU_FOREACH(cpu) {
2472b6c0
PM
975 err = cpu_watchpoint_remove(cpu, addr, len,
976 xlat_gdb_type(cpu, type));
880a7578
AL
977 if (err)
978 break;
979 }
980 return err;
a1d1bb31
AL
981#endif
982 default:
983 return -ENOSYS;
984 }
985}
986
880a7578 987static void gdb_breakpoint_remove_all(void)
a1d1bb31 988{
182735ef 989 CPUState *cpu;
880a7578 990
e22a25c9 991 if (kvm_enabled()) {
2e0f2cfb 992 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
e22a25c9
AL
993 return;
994 }
995
bdc44640 996 CPU_FOREACH(cpu) {
b3310ab3 997 cpu_breakpoint_remove_all(cpu, BP_GDB);
a1d1bb31 998#ifndef CONFIG_USER_ONLY
75a34036 999 cpu_watchpoint_remove_all(cpu, BP_GDB);
a1d1bb31 1000#endif
880a7578 1001 }
a1d1bb31
AL
1002}
1003
fab9d284
AJ
1004static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
1005{
2e0f2cfb 1006 CPUState *cpu = s->c_cpu;
f45748f1
AF
1007
1008 cpu_synchronize_state(cpu);
4a2b24ed 1009 cpu_set_pc(cpu, pc);
fab9d284
AJ
1010}
1011
1a227336
LM
1012static char *gdb_fmt_thread_id(const GDBState *s, CPUState *cpu,
1013 char *buf, size_t buf_size)
1014{
1015 if (s->multiprocess) {
1016 snprintf(buf, buf_size, "p%02x.%02x",
1017 gdb_get_cpu_pid(s, cpu), cpu_gdb_index(cpu));
1018 } else {
1019 snprintf(buf, buf_size, "%02x", cpu_gdb_index(cpu));
1020 }
1021
1022 return buf;
1023}
1024
7d8c87da
LM
1025typedef enum GDBThreadIdKind {
1026 GDB_ONE_THREAD = 0,
1027 GDB_ALL_THREADS, /* One process, all threads */
1028 GDB_ALL_PROCESSES,
1029 GDB_READ_THREAD_ERR
1030} GDBThreadIdKind;
1031
1032static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1033 uint32_t *pid, uint32_t *tid)
1034{
1035 unsigned long p, t;
1036 int ret;
1037
1038 if (*buf == 'p') {
1039 buf++;
1040 ret = qemu_strtoul(buf, &buf, 16, &p);
1041
1042 if (ret) {
1043 return GDB_READ_THREAD_ERR;
1044 }
1045
1046 /* Skip '.' */
1047 buf++;
1048 } else {
1049 p = 1;
1050 }
1051
1052 ret = qemu_strtoul(buf, &buf, 16, &t);
1053
1054 if (ret) {
1055 return GDB_READ_THREAD_ERR;
1056 }
1057
1058 *end_buf = buf;
1059
1060 if (p == -1) {
1061 return GDB_ALL_PROCESSES;
1062 }
1063
1064 if (pid) {
1065 *pid = p;
1066 }
1067
1068 if (t == -1) {
1069 return GDB_ALL_THREADS;
1070 }
1071
1072 if (tid) {
1073 *tid = t;
1074 }
1075
1076 return GDB_ONE_THREAD;
1077}
1078
4dabe747
JK
1079static int is_query_packet(const char *p, const char *query, char separator)
1080{
1081 unsigned int query_len = strlen(query);
1082
1083 return strncmp(p, query, query_len) == 0 &&
1084 (p[query_len] == '\0' || p[query_len] == separator);
1085}
1086
544177ad
CI
1087/**
1088 * gdb_handle_vcont - Parses and handles a vCont packet.
1089 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1090 * a format error, 0 on success.
1091 */
1092static int gdb_handle_vcont(GDBState *s, const char *p)
1093{
1094 int res, idx, signal = 0;
1095 char cur_action;
1096 char *newstates;
1097 unsigned long tmp;
1098 CPUState *cpu;
1099#ifdef CONFIG_USER_ONLY
1100 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
1101
1102 CPU_FOREACH(cpu) {
1103 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1104 }
1105#endif
1106 /* uninitialised CPUs stay 0 */
1107 newstates = g_new0(char, max_cpus);
1108
1109 /* mark valid CPUs with 1 */
1110 CPU_FOREACH(cpu) {
1111 newstates[cpu->cpu_index] = 1;
1112 }
1113
1114 /*
1115 * res keeps track of what error we are returning, with -ENOTSUP meaning
1116 * that the command is unknown or unsupported, thus returning an empty
1117 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1118 * or incorrect parameters passed.
1119 */
1120 res = 0;
1121 while (*p) {
1122 if (*p++ != ';') {
1123 res = -ENOTSUP;
1124 goto out;
1125 }
1126
1127 cur_action = *p++;
1128 if (cur_action == 'C' || cur_action == 'S') {
95a5befc 1129 cur_action = qemu_tolower(cur_action);
544177ad
CI
1130 res = qemu_strtoul(p + 1, &p, 16, &tmp);
1131 if (res) {
1132 goto out;
1133 }
1134 signal = gdb_signal_to_target(tmp);
1135 } else if (cur_action != 'c' && cur_action != 's') {
1136 /* unknown/invalid/unsupported command */
1137 res = -ENOTSUP;
1138 goto out;
1139 }
1140 /* thread specification. special values: (none), -1 = all; 0 = any */
1141 if ((p[0] == ':' && p[1] == '-' && p[2] == '1') || (p[0] != ':')) {
1142 if (*p == ':') {
1143 p += 3;
1144 }
1145 for (idx = 0; idx < max_cpus; idx++) {
1146 if (newstates[idx] == 1) {
1147 newstates[idx] = cur_action;
1148 }
1149 }
1150 } else if (*p == ':') {
1151 p++;
1152 res = qemu_strtoul(p, &p, 16, &tmp);
1153 if (res) {
1154 goto out;
1155 }
5a6a1ad1 1156
544177ad 1157 /* 0 means any thread, so we pick the first valid CPU */
5a6a1ad1 1158 cpu = tmp ? find_cpu(tmp) : first_cpu;
544177ad 1159
544177ad 1160 /* invalid CPU/thread specified */
5a6a1ad1 1161 if (!cpu) {
544177ad
CI
1162 res = -EINVAL;
1163 goto out;
1164 }
5a6a1ad1 1165
544177ad
CI
1166 /* only use if no previous match occourred */
1167 if (newstates[cpu->cpu_index] == 1) {
1168 newstates[cpu->cpu_index] = cur_action;
1169 }
1170 }
1171 }
1172 s->signal = signal;
1173 gdb_continue_partial(s, newstates);
1174
1175out:
1176 g_free(newstates);
1177
1178 return res;
1179}
1180
880a7578 1181static int gdb_handle_packet(GDBState *s, const char *line_buf)
b4608c04 1182{
2e0f2cfb 1183 CPUState *cpu;
5b24c641 1184 CPUClass *cc;
b4608c04 1185 const char *p;
1e9fa730 1186 uint32_t thread;
7d8c87da 1187 uint32_t pid, tid;
1e9fa730 1188 int ch, reg_size, type, res;
56aebc89 1189 uint8_t mem_buf[MAX_PACKET_LENGTH];
9005774b 1190 char buf[sizeof(mem_buf) + 1 /* trailing NUL */];
1a227336 1191 char thread_id[16];
56aebc89 1192 uint8_t *registers;
9d9754a3 1193 target_ulong addr, len;
7d8c87da 1194 GDBThreadIdKind thread_kind;
3b46e624 1195
5c9522b3 1196 trace_gdbstub_io_command(line_buf);
118e2268 1197
858693c6
FB
1198 p = line_buf;
1199 ch = *p++;
1200 switch(ch) {
1201 case '?':
1fddef4b 1202 /* TODO: Make this return the correct value for user-mode. */
1a227336
LM
1203 snprintf(buf, sizeof(buf), "T%02xthread:%s;", GDB_SIGNAL_TRAP,
1204 gdb_fmt_thread_id(s, s->c_cpu, thread_id, sizeof(thread_id)));
858693c6 1205 put_packet(s, buf);
7d03f82f
EI
1206 /* Remove all the breakpoints when this query is issued,
1207 * because gdb is doing and initial connect and the state
1208 * should be cleaned up.
1209 */
880a7578 1210 gdb_breakpoint_remove_all();
858693c6
FB
1211 break;
1212 case 'c':
1213 if (*p != '\0') {
9d9754a3 1214 addr = strtoull(p, (char **)&p, 16);
fab9d284 1215 gdb_set_cpu_pc(s, addr);
858693c6 1216 }
ca587a8e 1217 s->signal = 0;
ba70a624 1218 gdb_continue(s);
5c9522b3 1219 return RS_IDLE;
1f487ee9 1220 case 'C':
ca587a8e
AJ
1221 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
1222 if (s->signal == -1)
1223 s->signal = 0;
1f487ee9
EI
1224 gdb_continue(s);
1225 return RS_IDLE;
dd32aa10
JK
1226 case 'v':
1227 if (strncmp(p, "Cont", 4) == 0) {
dd32aa10
JK
1228 p += 4;
1229 if (*p == '?') {
1230 put_packet(s, "vCont;c;C;s;S");
1231 break;
1232 }
544177ad
CI
1233
1234 res = gdb_handle_vcont(s, p);
1235
dd32aa10 1236 if (res) {
544177ad
CI
1237 if ((res == -EINVAL) || (res == -ERANGE)) {
1238 put_packet(s, "E22");
1239 break;
dd32aa10 1240 }
544177ad 1241 goto unknown_command;
dd32aa10
JK
1242 }
1243 break;
1244 } else {
1245 goto unknown_command;
1246 }
7d03f82f
EI
1247 case 'k':
1248 /* Kill the target */
7ae6c571 1249 error_report("QEMU: Terminated via GDBstub");
7d03f82f
EI
1250 exit(0);
1251 case 'D':
1252 /* Detach packet */
880a7578 1253 gdb_breakpoint_remove_all();
7ea06da3 1254 gdb_syscall_mode = GDB_SYS_DISABLED;
7d03f82f
EI
1255 gdb_continue(s);
1256 put_packet(s, "OK");
1257 break;
858693c6
FB
1258 case 's':
1259 if (*p != '\0') {
8fac5803 1260 addr = strtoull(p, (char **)&p, 16);
fab9d284 1261 gdb_set_cpu_pc(s, addr);
858693c6 1262 }
2e0f2cfb 1263 cpu_single_step(s->c_cpu, sstep_flags);
ba70a624 1264 gdb_continue(s);
5c9522b3 1265 return RS_IDLE;
a2d1ebaf
PB
1266 case 'F':
1267 {
1268 target_ulong ret;
1269 target_ulong err;
1270
1271 ret = strtoull(p, (char **)&p, 16);
1272 if (*p == ',') {
1273 p++;
1274 err = strtoull(p, (char **)&p, 16);
1275 } else {
1276 err = 0;
1277 }
1278 if (*p == ',')
1279 p++;
1280 type = *p;
cdb432b2 1281 if (s->current_syscall_cb) {
2e0f2cfb 1282 s->current_syscall_cb(s->c_cpu, ret, err);
cdb432b2
MI
1283 s->current_syscall_cb = NULL;
1284 }
a2d1ebaf
PB
1285 if (type == 'C') {
1286 put_packet(s, "T02");
1287 } else {
ba70a624 1288 gdb_continue(s);
a2d1ebaf
PB
1289 }
1290 }
1291 break;
858693c6 1292 case 'g':
2e0f2cfb 1293 cpu_synchronize_state(s->g_cpu);
56aebc89 1294 len = 0;
35143f01 1295 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
2e0f2cfb 1296 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
56aebc89
PB
1297 len += reg_size;
1298 }
1299 memtohex(buf, mem_buf, len);
858693c6
FB
1300 put_packet(s, buf);
1301 break;
1302 case 'G':
2e0f2cfb 1303 cpu_synchronize_state(s->g_cpu);
56aebc89 1304 registers = mem_buf;
858693c6
FB
1305 len = strlen(p) / 2;
1306 hextomem((uint8_t *)registers, p, len);
35143f01 1307 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
2e0f2cfb 1308 reg_size = gdb_write_register(s->g_cpu, registers, addr);
56aebc89
PB
1309 len -= reg_size;
1310 registers += reg_size;
1311 }
858693c6
FB
1312 put_packet(s, "OK");
1313 break;
1314 case 'm':
9d9754a3 1315 addr = strtoull(p, (char **)&p, 16);
858693c6
FB
1316 if (*p == ',')
1317 p++;
9d9754a3 1318 len = strtoull(p, NULL, 16);
5accecb3
KW
1319
1320 /* memtohex() doubles the required space */
1321 if (len > MAX_PACKET_LENGTH / 2) {
1322 put_packet (s, "E22");
1323 break;
1324 }
1325
2e0f2cfb 1326 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
6f970bd9
FB
1327 put_packet (s, "E14");
1328 } else {
1329 memtohex(buf, mem_buf, len);
1330 put_packet(s, buf);
1331 }
858693c6
FB
1332 break;
1333 case 'M':
9d9754a3 1334 addr = strtoull(p, (char **)&p, 16);
858693c6
FB
1335 if (*p == ',')
1336 p++;
9d9754a3 1337 len = strtoull(p, (char **)&p, 16);
b328f873 1338 if (*p == ':')
858693c6 1339 p++;
5accecb3
KW
1340
1341 /* hextomem() reads 2*len bytes */
1342 if (len > strlen(p) / 2) {
1343 put_packet (s, "E22");
1344 break;
1345 }
858693c6 1346 hextomem(mem_buf, p, len);
2e0f2cfb 1347 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
f3659eee 1348 true) != 0) {
905f20b1 1349 put_packet(s, "E14");
44520db1 1350 } else {
858693c6 1351 put_packet(s, "OK");
44520db1 1352 }
858693c6 1353 break;
56aebc89
PB
1354 case 'p':
1355 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1356 This works, but can be very slow. Anything new enough to
1357 understand XML also knows how to use this properly. */
1358 if (!gdb_has_xml)
1359 goto unknown_command;
1360 addr = strtoull(p, (char **)&p, 16);
2e0f2cfb 1361 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
56aebc89
PB
1362 if (reg_size) {
1363 memtohex(buf, mem_buf, reg_size);
1364 put_packet(s, buf);
1365 } else {
1366 put_packet(s, "E14");
1367 }
1368 break;
1369 case 'P':
1370 if (!gdb_has_xml)
1371 goto unknown_command;
1372 addr = strtoull(p, (char **)&p, 16);
1373 if (*p == '=')
1374 p++;
1375 reg_size = strlen(p) / 2;
1376 hextomem(mem_buf, p, reg_size);
2e0f2cfb 1377 gdb_write_register(s->g_cpu, mem_buf, addr);
56aebc89
PB
1378 put_packet(s, "OK");
1379 break;
858693c6 1380 case 'Z':
858693c6
FB
1381 case 'z':
1382 type = strtoul(p, (char **)&p, 16);
1383 if (*p == ',')
1384 p++;
9d9754a3 1385 addr = strtoull(p, (char **)&p, 16);
858693c6
FB
1386 if (*p == ',')
1387 p++;
9d9754a3 1388 len = strtoull(p, (char **)&p, 16);
a1d1bb31 1389 if (ch == 'Z')
880a7578 1390 res = gdb_breakpoint_insert(addr, len, type);
a1d1bb31 1391 else
880a7578 1392 res = gdb_breakpoint_remove(addr, len, type);
a1d1bb31
AL
1393 if (res >= 0)
1394 put_packet(s, "OK");
1395 else if (res == -ENOSYS)
0f459d16 1396 put_packet(s, "");
a1d1bb31
AL
1397 else
1398 put_packet(s, "E22");
858693c6 1399 break;
880a7578
AL
1400 case 'H':
1401 type = *p++;
7d8c87da
LM
1402
1403 thread_kind = read_thread_id(p, &p, &pid, &tid);
1404 if (thread_kind == GDB_READ_THREAD_ERR) {
1405 put_packet(s, "E22");
1406 break;
1407 }
1408
1409 if (thread_kind != GDB_ONE_THREAD) {
880a7578
AL
1410 put_packet(s, "OK");
1411 break;
1412 }
7d8c87da 1413 cpu = gdb_get_cpu(s, pid, tid);
2e0f2cfb 1414 if (cpu == NULL) {
880a7578
AL
1415 put_packet(s, "E22");
1416 break;
1417 }
1418 switch (type) {
1419 case 'c':
2e0f2cfb 1420 s->c_cpu = cpu;
880a7578
AL
1421 put_packet(s, "OK");
1422 break;
1423 case 'g':
2e0f2cfb 1424 s->g_cpu = cpu;
880a7578
AL
1425 put_packet(s, "OK");
1426 break;
1427 default:
1428 put_packet(s, "E22");
1429 break;
1430 }
1431 break;
1432 case 'T':
7d8c87da
LM
1433 thread_kind = read_thread_id(p, &p, &pid, &tid);
1434 if (thread_kind == GDB_READ_THREAD_ERR) {
1435 put_packet(s, "E22");
1436 break;
1437 }
1438 cpu = gdb_get_cpu(s, pid, tid);
1e9fa730 1439
2e0f2cfb 1440 if (cpu != NULL) {
1e9fa730
NF
1441 put_packet(s, "OK");
1442 } else {
880a7578 1443 put_packet(s, "E22");
1e9fa730 1444 }
880a7578 1445 break;
978efd6a 1446 case 'q':
60897d36
EI
1447 case 'Q':
1448 /* parse any 'q' packets here */
1449 if (!strcmp(p,"qemu.sstepbits")) {
1450 /* Query Breakpoint bit definitions */
363a37d5
BS
1451 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1452 SSTEP_ENABLE,
1453 SSTEP_NOIRQ,
1454 SSTEP_NOTIMER);
60897d36
EI
1455 put_packet(s, buf);
1456 break;
4dabe747 1457 } else if (is_query_packet(p, "qemu.sstep", '=')) {
60897d36
EI
1458 /* Display or change the sstep_flags */
1459 p += 10;
1460 if (*p != '=') {
1461 /* Display current setting */
363a37d5 1462 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
60897d36
EI
1463 put_packet(s, buf);
1464 break;
1465 }
1466 p++;
1467 type = strtoul(p, (char **)&p, 16);
1468 sstep_flags = type;
1469 put_packet(s, "OK");
1470 break;
880a7578
AL
1471 } else if (strcmp(p,"C") == 0) {
1472 /* "Current thread" remains vague in the spec, so always return
1473 * the first CPU (gdb returns the first thread). */
1474 put_packet(s, "QC1");
1475 break;
1476 } else if (strcmp(p,"fThreadInfo") == 0) {
52f34623 1477 s->query_cpu = first_cpu;
880a7578
AL
1478 goto report_cpuinfo;
1479 } else if (strcmp(p,"sThreadInfo") == 0) {
1480 report_cpuinfo:
1481 if (s->query_cpu) {
d2a6c857 1482 snprintf(buf, sizeof(buf), "m%x", cpu_gdb_index(s->query_cpu));
880a7578 1483 put_packet(s, buf);
bdc44640 1484 s->query_cpu = CPU_NEXT(s->query_cpu);
880a7578
AL
1485 } else
1486 put_packet(s, "l");
1487 break;
1488 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1489 thread = strtoull(p+16, (char **)&p, 16);
2e0f2cfb
AF
1490 cpu = find_cpu(thread);
1491 if (cpu != NULL) {
cb446eca 1492 cpu_synchronize_state(cpu);
5accecb3
KW
1493 /* memtohex() doubles the required space */
1494 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
55e5c285 1495 "CPU#%d [%s]", cpu->cpu_index,
259186a7 1496 cpu->halted ? "halted " : "running");
5c9522b3 1497 trace_gdbstub_op_extra_info((char *)mem_buf);
1e9fa730
NF
1498 memtohex(buf, mem_buf, len);
1499 put_packet(s, buf);
1500 }
880a7578 1501 break;
60897d36 1502 }
0b8a988c 1503#ifdef CONFIG_USER_ONLY
070949f3 1504 else if (strcmp(p, "Offsets") == 0) {
0429a971 1505 TaskState *ts = s->c_cpu->opaque;
978efd6a 1506
363a37d5
BS
1507 snprintf(buf, sizeof(buf),
1508 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1509 ";Bss=" TARGET_ABI_FMT_lx,
1510 ts->info->code_offset,
1511 ts->info->data_offset,
1512 ts->info->data_offset);
978efd6a
PB
1513 put_packet(s, buf);
1514 break;
1515 }
0b8a988c 1516#else /* !CONFIG_USER_ONLY */
8a34a0fb
AL
1517 else if (strncmp(p, "Rcmd,", 5) == 0) {
1518 int len = strlen(p + 5);
1519
1520 if ((len % 2) != 0) {
1521 put_packet(s, "E01");
1522 break;
1523 }
8a34a0fb 1524 len = len / 2;
5accecb3 1525 hextomem(mem_buf, p + 5, len);
8a34a0fb 1526 mem_buf[len++] = 0;
fa5efccb 1527 qemu_chr_be_write(s->mon_chr, mem_buf, len);
8a34a0fb
AL
1528 put_packet(s, "OK");
1529 break;
1530 }
0b8a988c 1531#endif /* !CONFIG_USER_ONLY */
4dabe747 1532 if (is_query_packet(p, "Supported", ':')) {
5b3715bf 1533 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
5b24c641
AF
1534 cc = CPU_GET_CLASS(first_cpu);
1535 if (cc->gdb_core_xml_file != NULL) {
1536 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1537 }
56aebc89
PB
1538 put_packet(s, buf);
1539 break;
1540 }
56aebc89
PB
1541 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1542 const char *xml;
1543 target_ulong total_len;
1544
5b24c641
AF
1545 cc = CPU_GET_CLASS(first_cpu);
1546 if (cc->gdb_core_xml_file == NULL) {
1547 goto unknown_command;
1548 }
1549
5b50e790 1550 gdb_has_xml = true;
56aebc89 1551 p += 19;
5b24c641 1552 xml = get_feature_xml(p, &p, cc);
56aebc89 1553 if (!xml) {
5b3715bf 1554 snprintf(buf, sizeof(buf), "E00");
56aebc89
PB
1555 put_packet(s, buf);
1556 break;
1557 }
1558
1559 if (*p == ':')
1560 p++;
1561 addr = strtoul(p, (char **)&p, 16);
1562 if (*p == ',')
1563 p++;
1564 len = strtoul(p, (char **)&p, 16);
1565
1566 total_len = strlen(xml);
1567 if (addr > total_len) {
5b3715bf 1568 snprintf(buf, sizeof(buf), "E00");
56aebc89
PB
1569 put_packet(s, buf);
1570 break;
1571 }
1572 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1573 len = (MAX_PACKET_LENGTH - 5) / 2;
1574 if (len < total_len - addr) {
1575 buf[0] = 'm';
1576 len = memtox(buf + 1, xml + addr, len);
1577 } else {
1578 buf[0] = 'l';
1579 len = memtox(buf + 1, xml + addr, total_len - addr);
1580 }
5c9522b3 1581 put_packet_binary(s, buf, len + 1, true);
56aebc89
PB
1582 break;
1583 }
a3919386
JK
1584 if (is_query_packet(p, "Attached", ':')) {
1585 put_packet(s, GDB_ATTACHED);
1586 break;
1587 }
56aebc89
PB
1588 /* Unrecognised 'q' command. */
1589 goto unknown_command;
1590
858693c6 1591 default:
56aebc89 1592 unknown_command:
858693c6
FB
1593 /* put empty packet */
1594 buf[0] = '\0';
1595 put_packet(s, buf);
1596 break;
1597 }
1598 return RS_IDLE;
1599}
1600
64f6b346 1601void gdb_set_stop_cpu(CPUState *cpu)
880a7578 1602{
2e0f2cfb
AF
1603 gdbserver_state->c_cpu = cpu;
1604 gdbserver_state->g_cpu = cpu;
880a7578
AL
1605}
1606
1fddef4b 1607#ifndef CONFIG_USER_ONLY
1dfb4dd9 1608static void gdb_vm_state_change(void *opaque, int running, RunState state)
858693c6 1609{
880a7578 1610 GDBState *s = gdbserver_state;
2e0f2cfb 1611 CPUState *cpu = s->c_cpu;
858693c6 1612 char buf[256];
d6fc1b39 1613 const char *type;
858693c6
FB
1614 int ret;
1615
cdb432b2
MI
1616 if (running || s->state == RS_INACTIVE) {
1617 return;
1618 }
1619 /* Is there a GDB syscall waiting to be sent? */
1620 if (s->current_syscall_cb) {
1621 put_packet(s, s->syscall_buf);
a2d1ebaf 1622 return;
e07bbac5 1623 }
1dfb4dd9 1624 switch (state) {
0461d5a6 1625 case RUN_STATE_DEBUG:
ff4700b0
AF
1626 if (cpu->watchpoint_hit) {
1627 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
a1d1bb31 1628 case BP_MEM_READ:
d6fc1b39
AL
1629 type = "r";
1630 break;
a1d1bb31 1631 case BP_MEM_ACCESS:
d6fc1b39
AL
1632 type = "a";
1633 break;
1634 default:
1635 type = "";
1636 break;
1637 }
5c9522b3
DG
1638 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
1639 (target_ulong)cpu->watchpoint_hit->vaddr);
880a7578
AL
1640 snprintf(buf, sizeof(buf),
1641 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
d2a6c857 1642 GDB_SIGNAL_TRAP, cpu_gdb_index(cpu), type,
ff4700b0
AF
1643 (target_ulong)cpu->watchpoint_hit->vaddr);
1644 cpu->watchpoint_hit = NULL;
425189a8 1645 goto send_packet;
5c9522b3
DG
1646 } else {
1647 trace_gdbstub_hit_break();
6658ffb8 1648 }
bbd77c18 1649 tb_flush(cpu);
ca587a8e 1650 ret = GDB_SIGNAL_TRAP;
425189a8 1651 break;
0461d5a6 1652 case RUN_STATE_PAUSED:
5c9522b3 1653 trace_gdbstub_hit_paused();
9781e040 1654 ret = GDB_SIGNAL_INT;
425189a8 1655 break;
0461d5a6 1656 case RUN_STATE_SHUTDOWN:
5c9522b3 1657 trace_gdbstub_hit_shutdown();
425189a8
JK
1658 ret = GDB_SIGNAL_QUIT;
1659 break;
0461d5a6 1660 case RUN_STATE_IO_ERROR:
5c9522b3 1661 trace_gdbstub_hit_io_error();
425189a8
JK
1662 ret = GDB_SIGNAL_IO;
1663 break;
0461d5a6 1664 case RUN_STATE_WATCHDOG:
5c9522b3 1665 trace_gdbstub_hit_watchdog();
425189a8
JK
1666 ret = GDB_SIGNAL_ALRM;
1667 break;
0461d5a6 1668 case RUN_STATE_INTERNAL_ERROR:
5c9522b3 1669 trace_gdbstub_hit_internal_error();
425189a8
JK
1670 ret = GDB_SIGNAL_ABRT;
1671 break;
0461d5a6
LC
1672 case RUN_STATE_SAVE_VM:
1673 case RUN_STATE_RESTORE_VM:
425189a8 1674 return;
0461d5a6 1675 case RUN_STATE_FINISH_MIGRATE:
425189a8
JK
1676 ret = GDB_SIGNAL_XCPU;
1677 break;
1678 default:
5c9522b3 1679 trace_gdbstub_hit_unknown(state);
425189a8
JK
1680 ret = GDB_SIGNAL_UNKNOWN;
1681 break;
bbeb7b5c 1682 }
226d007d 1683 gdb_set_stop_cpu(cpu);
d2a6c857 1684 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_gdb_index(cpu));
425189a8
JK
1685
1686send_packet:
858693c6 1687 put_packet(s, buf);
425189a8
JK
1688
1689 /* disable single step if it was enabled */
3825b28f 1690 cpu_single_step(cpu, 0);
858693c6 1691}
1fddef4b 1692#endif
858693c6 1693
a2d1ebaf
PB
1694/* Send a gdb syscall request.
1695 This accepts limited printf-style format specifiers, specifically:
a87295e8
PB
1696 %x - target_ulong argument printed in hex.
1697 %lx - 64-bit argument printed in hex.
1698 %s - string pointer (target_ulong) and length (int) pair. */
19239b39 1699void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
a2d1ebaf 1700{
a2d1ebaf 1701 char *p;
cdb432b2 1702 char *p_end;
a2d1ebaf 1703 target_ulong addr;
a87295e8 1704 uint64_t i64;
a2d1ebaf
PB
1705 GDBState *s;
1706
880a7578 1707 s = gdbserver_state;
a2d1ebaf
PB
1708 if (!s)
1709 return;
cdb432b2 1710 s->current_syscall_cb = cb;
a2d1ebaf 1711#ifndef CONFIG_USER_ONLY
0461d5a6 1712 vm_stop(RUN_STATE_DEBUG);
a2d1ebaf 1713#endif
cdb432b2
MI
1714 p = s->syscall_buf;
1715 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
a2d1ebaf
PB
1716 *(p++) = 'F';
1717 while (*fmt) {
1718 if (*fmt == '%') {
1719 fmt++;
1720 switch (*fmt++) {
1721 case 'x':
1722 addr = va_arg(va, target_ulong);
cdb432b2 1723 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
a2d1ebaf 1724 break;
a87295e8
PB
1725 case 'l':
1726 if (*(fmt++) != 'x')
1727 goto bad_format;
1728 i64 = va_arg(va, uint64_t);
cdb432b2 1729 p += snprintf(p, p_end - p, "%" PRIx64, i64);
a87295e8 1730 break;
a2d1ebaf
PB
1731 case 's':
1732 addr = va_arg(va, target_ulong);
cdb432b2 1733 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
363a37d5 1734 addr, va_arg(va, int));
a2d1ebaf
PB
1735 break;
1736 default:
a87295e8 1737 bad_format:
7ae6c571
ZY
1738 error_report("gdbstub: Bad syscall format string '%s'",
1739 fmt - 1);
a2d1ebaf
PB
1740 break;
1741 }
1742 } else {
1743 *(p++) = *(fmt++);
1744 }
1745 }
8a93e02a 1746 *p = 0;
a2d1ebaf 1747#ifdef CONFIG_USER_ONLY
cdb432b2 1748 put_packet(s, s->syscall_buf);
4f710866
PM
1749 /* Return control to gdb for it to process the syscall request.
1750 * Since the protocol requires that gdb hands control back to us
1751 * using a "here are the results" F packet, we don't need to check
1752 * gdb_handlesig's return value (which is the signal to deliver if
1753 * execution was resumed via a continue packet).
1754 */
2e0f2cfb 1755 gdb_handlesig(s->c_cpu, 0);
a2d1ebaf 1756#else
cdb432b2
MI
1757 /* In this case wait to send the syscall packet until notification that
1758 the CPU has stopped. This must be done because if the packet is sent
1759 now the reply from the syscall request could be received while the CPU
1760 is still in the running state, which can cause packets to be dropped
1761 and state transition 'T' packets to be sent while the syscall is still
1762 being processed. */
9102deda 1763 qemu_cpu_kick(s->c_cpu);
a2d1ebaf
PB
1764#endif
1765}
1766
19239b39
PM
1767void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
1768{
1769 va_list va;
1770
1771 va_start(va, fmt);
1772 gdb_do_syscallv(cb, fmt, va);
1773 va_end(va);
1774}
1775
6a00d601 1776static void gdb_read_byte(GDBState *s, int ch)
858693c6 1777{
60fe76f3 1778 uint8_t reply;
858693c6 1779
1fddef4b 1780#ifndef CONFIG_USER_ONLY
4046d913
PB
1781 if (s->last_packet_len) {
1782 /* Waiting for a response to the last packet. If we see the start
1783 of a new command then abandon the previous response. */
1784 if (ch == '-') {
5c9522b3 1785 trace_gdbstub_err_got_nack();
ffe8ab83 1786 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
118e2268 1787 } else if (ch == '+') {
5c9522b3 1788 trace_gdbstub_io_got_ack();
118e2268 1789 } else {
5c9522b3 1790 trace_gdbstub_io_got_unexpected((uint8_t)ch);
4046d913 1791 }
118e2268 1792
4046d913
PB
1793 if (ch == '+' || ch == '$')
1794 s->last_packet_len = 0;
1795 if (ch != '$')
1796 return;
1797 }
1354869c 1798 if (runstate_is_running()) {
858693c6
FB
1799 /* when the CPU is running, we cannot do anything except stop
1800 it when receiving a char */
0461d5a6 1801 vm_stop(RUN_STATE_PAUSED);
5fafdf24 1802 } else
1fddef4b 1803#endif
41625033 1804 {
858693c6
FB
1805 switch(s->state) {
1806 case RS_IDLE:
1807 if (ch == '$') {
4bf43122 1808 /* start of command packet */
858693c6 1809 s->line_buf_index = 0;
4bf43122 1810 s->line_sum = 0;
858693c6 1811 s->state = RS_GETLINE;
4bf43122 1812 } else {
5c9522b3 1813 trace_gdbstub_err_garbage((uint8_t)ch);
c33a346e 1814 }
b4608c04 1815 break;
858693c6 1816 case RS_GETLINE:
4bf43122
DG
1817 if (ch == '}') {
1818 /* start escape sequence */
1819 s->state = RS_GETLINE_ESC;
1820 s->line_sum += ch;
1821 } else if (ch == '*') {
1822 /* start run length encoding sequence */
1823 s->state = RS_GETLINE_RLE;
1824 s->line_sum += ch;
1825 } else if (ch == '#') {
1826 /* end of command, start of checksum*/
1827 s->state = RS_CHKSUM1;
1828 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
5c9522b3 1829 trace_gdbstub_err_overrun();
4bf43122
DG
1830 s->state = RS_IDLE;
1831 } else {
1832 /* unescaped command character */
1833 s->line_buf[s->line_buf_index++] = ch;
1834 s->line_sum += ch;
1835 }
1836 break;
1837 case RS_GETLINE_ESC:
858693c6 1838 if (ch == '#') {
4bf43122
DG
1839 /* unexpected end of command in escape sequence */
1840 s->state = RS_CHKSUM1;
858693c6 1841 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
4bf43122 1842 /* command buffer overrun */
5c9522b3 1843 trace_gdbstub_err_overrun();
858693c6 1844 s->state = RS_IDLE;
4c3a88a2 1845 } else {
4bf43122
DG
1846 /* parse escaped character and leave escape state */
1847 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
1848 s->line_sum += ch;
1849 s->state = RS_GETLINE;
1850 }
1851 break;
1852 case RS_GETLINE_RLE:
1853 if (ch < ' ') {
1854 /* invalid RLE count encoding */
5c9522b3 1855 trace_gdbstub_err_invalid_repeat((uint8_t)ch);
4bf43122
DG
1856 s->state = RS_GETLINE;
1857 } else {
1858 /* decode repeat length */
1859 int repeat = (unsigned char)ch - ' ' + 3;
1860 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
1861 /* that many repeats would overrun the command buffer */
5c9522b3 1862 trace_gdbstub_err_overrun();
4bf43122
DG
1863 s->state = RS_IDLE;
1864 } else if (s->line_buf_index < 1) {
1865 /* got a repeat but we have nothing to repeat */
5c9522b3 1866 trace_gdbstub_err_invalid_rle();
4bf43122
DG
1867 s->state = RS_GETLINE;
1868 } else {
1869 /* repeat the last character */
1870 memset(s->line_buf + s->line_buf_index,
1871 s->line_buf[s->line_buf_index - 1], repeat);
1872 s->line_buf_index += repeat;
1873 s->line_sum += ch;
1874 s->state = RS_GETLINE;
1875 }
4c3a88a2
FB
1876 }
1877 break;
858693c6 1878 case RS_CHKSUM1:
4bf43122
DG
1879 /* get high hex digit of checksum */
1880 if (!isxdigit(ch)) {
5c9522b3 1881 trace_gdbstub_err_checksum_invalid((uint8_t)ch);
4bf43122
DG
1882 s->state = RS_GETLINE;
1883 break;
1884 }
858693c6
FB
1885 s->line_buf[s->line_buf_index] = '\0';
1886 s->line_csum = fromhex(ch) << 4;
1887 s->state = RS_CHKSUM2;
1888 break;
1889 case RS_CHKSUM2:
4bf43122
DG
1890 /* get low hex digit of checksum */
1891 if (!isxdigit(ch)) {
5c9522b3 1892 trace_gdbstub_err_checksum_invalid((uint8_t)ch);
4bf43122
DG
1893 s->state = RS_GETLINE;
1894 break;
858693c6 1895 }
4bf43122
DG
1896 s->line_csum |= fromhex(ch);
1897
1898 if (s->line_csum != (s->line_sum & 0xff)) {
5c9522b3 1899 trace_gdbstub_err_checksum_incorrect(s->line_sum, s->line_csum);
4bf43122 1900 /* send NAK reply */
60fe76f3
TS
1901 reply = '-';
1902 put_buffer(s, &reply, 1);
858693c6 1903 s->state = RS_IDLE;
4c3a88a2 1904 } else {
4bf43122 1905 /* send ACK reply */
60fe76f3
TS
1906 reply = '+';
1907 put_buffer(s, &reply, 1);
880a7578 1908 s->state = gdb_handle_packet(s, s->line_buf);
4c3a88a2
FB
1909 }
1910 break;
a2d1ebaf
PB
1911 default:
1912 abort();
858693c6
FB
1913 }
1914 }
1915}
1916
0e1c9c54 1917/* Tell the remote gdb that the process has exited. */
9349b4f9 1918void gdb_exit(CPUArchState *env, int code)
0e1c9c54
PB
1919{
1920 GDBState *s;
1921 char buf[4];
1922
1923 s = gdbserver_state;
1924 if (!s) {
1925 return;
1926 }
1927#ifdef CONFIG_USER_ONLY
1928 if (gdbserver_fd < 0 || s->fd < 0) {
1929 return;
1930 }
1931#endif
1932
5c9522b3
DG
1933 trace_gdbstub_op_exiting((uint8_t)code);
1934
0e1c9c54
PB
1935 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
1936 put_packet(s, buf);
e2af15b2
FC
1937
1938#ifndef CONFIG_USER_ONLY
1ce2610c 1939 qemu_chr_fe_deinit(&s->chr, true);
e2af15b2 1940#endif
0e1c9c54
PB
1941}
1942
8f468636
LM
1943/*
1944 * Create the process that will contain all the "orphan" CPUs (that are not
1945 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
1946 * be attachable and thus will be invisible to the user.
1947 */
1948static void create_default_process(GDBState *s)
1949{
1950 GDBProcess *process;
1951 int max_pid = 0;
1952
1953 if (s->process_num) {
1954 max_pid = s->processes[s->process_num - 1].pid;
1955 }
1956
1957 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
1958 process = &s->processes[s->process_num - 1];
1959
1960 /* We need an available PID slot for this process */
1961 assert(max_pid < UINT32_MAX);
1962
1963 process->pid = max_pid + 1;
1964 process->attached = false;
1965}
1966
1fddef4b
FB
1967#ifdef CONFIG_USER_ONLY
1968int
db6b81d4 1969gdb_handlesig(CPUState *cpu, int sig)
1fddef4b 1970{
5ca666c7
AF
1971 GDBState *s;
1972 char buf[256];
1973 int n;
1fddef4b 1974
5ca666c7
AF
1975 s = gdbserver_state;
1976 if (gdbserver_fd < 0 || s->fd < 0) {
1977 return sig;
1978 }
1fddef4b 1979
5ca666c7 1980 /* disable single step if it was enabled */
3825b28f 1981 cpu_single_step(cpu, 0);
bbd77c18 1982 tb_flush(cpu);
1fddef4b 1983
5ca666c7
AF
1984 if (sig != 0) {
1985 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
1986 put_packet(s, buf);
1987 }
1988 /* put_packet() might have detected that the peer terminated the
1989 connection. */
1990 if (s->fd < 0) {
1991 return sig;
1992 }
1fddef4b 1993
5ca666c7
AF
1994 sig = 0;
1995 s->state = RS_IDLE;
1996 s->running_state = 0;
1997 while (s->running_state == 0) {
1998 n = read(s->fd, buf, 256);
1999 if (n > 0) {
2000 int i;
2001
2002 for (i = 0; i < n; i++) {
2003 gdb_read_byte(s, buf[i]);
2004 }
5819e3e0 2005 } else {
5ca666c7
AF
2006 /* XXX: Connection closed. Should probably wait for another
2007 connection before continuing. */
5819e3e0
PW
2008 if (n == 0) {
2009 close(s->fd);
2010 }
2011 s->fd = -1;
5ca666c7 2012 return sig;
1fddef4b 2013 }
5ca666c7
AF
2014 }
2015 sig = s->signal;
2016 s->signal = 0;
2017 return sig;
1fddef4b 2018}
e9009676 2019
ca587a8e 2020/* Tell the remote gdb that the process has exited due to SIG. */
9349b4f9 2021void gdb_signalled(CPUArchState *env, int sig)
ca587a8e 2022{
5ca666c7
AF
2023 GDBState *s;
2024 char buf[4];
ca587a8e 2025
5ca666c7
AF
2026 s = gdbserver_state;
2027 if (gdbserver_fd < 0 || s->fd < 0) {
2028 return;
2029 }
ca587a8e 2030
5ca666c7
AF
2031 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
2032 put_packet(s, buf);
ca587a8e 2033}
1fddef4b 2034
2f652224 2035static bool gdb_accept(void)
858693c6
FB
2036{
2037 GDBState *s;
2038 struct sockaddr_in sockaddr;
2039 socklen_t len;
bf1c852a 2040 int fd;
858693c6
FB
2041
2042 for(;;) {
2043 len = sizeof(sockaddr);
2044 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
2045 if (fd < 0 && errno != EINTR) {
2046 perror("accept");
2f652224 2047 return false;
858693c6 2048 } else if (fd >= 0) {
f5bdd781 2049 qemu_set_cloexec(fd);
b4608c04
FB
2050 break;
2051 }
2052 }
858693c6
FB
2053
2054 /* set short latency */
2f652224
PM
2055 if (socket_set_nodelay(fd)) {
2056 perror("setsockopt");
ead75d84 2057 close(fd);
2f652224
PM
2058 return false;
2059 }
3b46e624 2060
7267c094 2061 s = g_malloc0(sizeof(GDBState));
2e0f2cfb
AF
2062 s->c_cpu = first_cpu;
2063 s->g_cpu = first_cpu;
8f468636 2064 create_default_process(s);
858693c6 2065 s->fd = fd;
5b50e790 2066 gdb_has_xml = false;
858693c6 2067
880a7578 2068 gdbserver_state = s;
2f652224 2069 return true;
858693c6
FB
2070}
2071
2072static int gdbserver_open(int port)
2073{
2074 struct sockaddr_in sockaddr;
6669ca13 2075 int fd, ret;
858693c6
FB
2076
2077 fd = socket(PF_INET, SOCK_STREAM, 0);
2078 if (fd < 0) {
2079 perror("socket");
2080 return -1;
2081 }
f5bdd781 2082 qemu_set_cloexec(fd);
858693c6 2083
6669ca13 2084 socket_set_fast_reuse(fd);
858693c6
FB
2085
2086 sockaddr.sin_family = AF_INET;
2087 sockaddr.sin_port = htons(port);
2088 sockaddr.sin_addr.s_addr = 0;
2089 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
2090 if (ret < 0) {
2091 perror("bind");
bb16172c 2092 close(fd);
858693c6
FB
2093 return -1;
2094 }
96165b9e 2095 ret = listen(fd, 1);
858693c6
FB
2096 if (ret < 0) {
2097 perror("listen");
bb16172c 2098 close(fd);
858693c6
FB
2099 return -1;
2100 }
858693c6
FB
2101 return fd;
2102}
2103
2104int gdbserver_start(int port)
2105{
2106 gdbserver_fd = gdbserver_open(port);
2107 if (gdbserver_fd < 0)
2108 return -1;
2109 /* accept connections */
2f652224
PM
2110 if (!gdb_accept()) {
2111 close(gdbserver_fd);
2112 gdbserver_fd = -1;
2113 return -1;
2114 }
4046d913
PB
2115 return 0;
2116}
2b1319c8
AJ
2117
2118/* Disable gdb stub for child processes. */
f7ec7f7b 2119void gdbserver_fork(CPUState *cpu)
2b1319c8
AJ
2120{
2121 GDBState *s = gdbserver_state;
75a34036
AF
2122
2123 if (gdbserver_fd < 0 || s->fd < 0) {
2124 return;
2125 }
2b1319c8
AJ
2126 close(s->fd);
2127 s->fd = -1;
b3310ab3 2128 cpu_breakpoint_remove_all(cpu, BP_GDB);
75a34036 2129 cpu_watchpoint_remove_all(cpu, BP_GDB);
2b1319c8 2130}
1fddef4b 2131#else
aa1f17c1 2132static int gdb_chr_can_receive(void *opaque)
4046d913 2133{
56aebc89
PB
2134 /* We can handle an arbitrarily large amount of data.
2135 Pick the maximum packet size, which is as good as anything. */
2136 return MAX_PACKET_LENGTH;
4046d913
PB
2137}
2138
aa1f17c1 2139static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
4046d913 2140{
4046d913
PB
2141 int i;
2142
2143 for (i = 0; i < size; i++) {
880a7578 2144 gdb_read_byte(gdbserver_state, buf[i]);
4046d913
PB
2145 }
2146}
2147
2148static void gdb_chr_event(void *opaque, int event)
2149{
2150 switch (event) {
b6b8df56 2151 case CHR_EVENT_OPENED:
0461d5a6 2152 vm_stop(RUN_STATE_PAUSED);
5b50e790 2153 gdb_has_xml = false;
4046d913
PB
2154 break;
2155 default:
2156 break;
2157 }
2158}
2159
8a34a0fb
AL
2160static void gdb_monitor_output(GDBState *s, const char *msg, int len)
2161{
2162 char buf[MAX_PACKET_LENGTH];
2163
2164 buf[0] = 'O';
2165 if (len > (MAX_PACKET_LENGTH/2) - 1)
2166 len = (MAX_PACKET_LENGTH/2) - 1;
2167 memtohex(buf + 1, (uint8_t *)msg, len);
2168 put_packet(s, buf);
2169}
2170
0ec7b3e7 2171static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
8a34a0fb
AL
2172{
2173 const char *p = (const char *)buf;
2174 int max_sz;
2175
2176 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
2177 for (;;) {
2178 if (len <= max_sz) {
2179 gdb_monitor_output(gdbserver_state, p, len);
2180 break;
2181 }
2182 gdb_monitor_output(gdbserver_state, p, max_sz);
2183 p += max_sz;
2184 len -= max_sz;
2185 }
2186 return len;
2187}
2188
59030a8c
AL
2189#ifndef _WIN32
2190static void gdb_sigterm_handler(int signal)
2191{
1354869c 2192 if (runstate_is_running()) {
0461d5a6 2193 vm_stop(RUN_STATE_PAUSED);
e07bbac5 2194 }
59030a8c
AL
2195}
2196#endif
2197
777357d7
MAL
2198static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
2199 bool *be_opened, Error **errp)
2200{
2201 *be_opened = false;
2202}
2203
2204static void char_gdb_class_init(ObjectClass *oc, void *data)
2205{
2206 ChardevClass *cc = CHARDEV_CLASS(oc);
2207
2208 cc->internal = true;
2209 cc->open = gdb_monitor_open;
2210 cc->chr_write = gdb_monitor_write;
2211}
2212
2213#define TYPE_CHARDEV_GDB "chardev-gdb"
2214
2215static const TypeInfo char_gdb_type_info = {
2216 .name = TYPE_CHARDEV_GDB,
2217 .parent = TYPE_CHARDEV,
2218 .class_init = char_gdb_class_init,
2219};
2220
8f468636
LM
2221static int find_cpu_clusters(Object *child, void *opaque)
2222{
2223 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
2224 GDBState *s = (GDBState *) opaque;
2225 CPUClusterState *cluster = CPU_CLUSTER(child);
2226 GDBProcess *process;
2227
2228 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2229
2230 process = &s->processes[s->process_num - 1];
2231
2232 /*
2233 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
2234 * runtime, we enforce here that the machine does not use a cluster ID
2235 * that would lead to PID 0.
2236 */
2237 assert(cluster->cluster_id != UINT32_MAX);
2238 process->pid = cluster->cluster_id + 1;
2239 process->attached = false;
2240
2241 return 0;
2242 }
2243
2244 return object_child_foreach(child, find_cpu_clusters, opaque);
2245}
2246
2247static int pid_order(const void *a, const void *b)
2248{
2249 GDBProcess *pa = (GDBProcess *) a;
2250 GDBProcess *pb = (GDBProcess *) b;
2251
2252 if (pa->pid < pb->pid) {
2253 return -1;
2254 } else if (pa->pid > pb->pid) {
2255 return 1;
2256 } else {
2257 return 0;
2258 }
2259}
2260
2261static void create_processes(GDBState *s)
2262{
2263 object_child_foreach(object_get_root(), find_cpu_clusters, s);
2264
2265 if (s->processes) {
2266 /* Sort by PID */
2267 qsort(s->processes, s->process_num, sizeof(s->processes[0]), pid_order);
2268 }
2269
2270 create_default_process(s);
2271}
2272
2273static void cleanup_processes(GDBState *s)
2274{
2275 g_free(s->processes);
2276 s->process_num = 0;
2277 s->processes = NULL;
2278}
2279
59030a8c 2280int gdbserver_start(const char *device)
4046d913 2281{
5c9522b3
DG
2282 trace_gdbstub_op_start(device);
2283
4046d913 2284 GDBState *s;
59030a8c 2285 char gdbstub_device_name[128];
0ec7b3e7
MAL
2286 Chardev *chr = NULL;
2287 Chardev *mon_chr;
cfc3475a 2288
508b4ecc
ZY
2289 if (!first_cpu) {
2290 error_report("gdbstub: meaningless to attach gdb to a "
2291 "machine without any CPU.");
2292 return -1;
2293 }
2294
59030a8c
AL
2295 if (!device)
2296 return -1;
2297 if (strcmp(device, "none") != 0) {
2298 if (strstart(device, "tcp:", NULL)) {
2299 /* enforce required TCP attributes */
2300 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
2301 "%s,nowait,nodelay,server", device);
2302 device = gdbstub_device_name;
36556b20 2303 }
59030a8c
AL
2304#ifndef _WIN32
2305 else if (strcmp(device, "stdio") == 0) {
2306 struct sigaction act;
4046d913 2307
59030a8c
AL
2308 memset(&act, 0, sizeof(act));
2309 act.sa_handler = gdb_sigterm_handler;
2310 sigaction(SIGINT, &act, NULL);
2311 }
2312#endif
95e30b2a
MAL
2313 /*
2314 * FIXME: it's a bit weird to allow using a mux chardev here
2315 * and implicitly setup a monitor. We may want to break this.
2316 */
2317 chr = qemu_chr_new_noreplay("gdb", device, true);
36556b20
AL
2318 if (!chr)
2319 return -1;
cfc3475a
PB
2320 }
2321
36556b20
AL
2322 s = gdbserver_state;
2323 if (!s) {
7267c094 2324 s = g_malloc0(sizeof(GDBState));
36556b20 2325 gdbserver_state = s;
4046d913 2326
36556b20
AL
2327 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
2328
2329 /* Initialize a monitor terminal for gdb */
777357d7
MAL
2330 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
2331 NULL, &error_abort);
36556b20
AL
2332 monitor_init(mon_chr, 0);
2333 } else {
1ce2610c 2334 qemu_chr_fe_deinit(&s->chr, true);
36556b20 2335 mon_chr = s->mon_chr;
8f468636 2336 cleanup_processes(s);
36556b20 2337 memset(s, 0, sizeof(GDBState));
32a6ebec 2338 s->mon_chr = mon_chr;
36556b20 2339 }
2e0f2cfb
AF
2340 s->c_cpu = first_cpu;
2341 s->g_cpu = first_cpu;
8f468636
LM
2342
2343 create_processes(s);
2344
32a6ebec
MAL
2345 if (chr) {
2346 qemu_chr_fe_init(&s->chr, chr, &error_abort);
5345fdb4 2347 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
81517ba3 2348 gdb_chr_event, NULL, NULL, NULL, true);
32a6ebec 2349 }
36556b20
AL
2350 s->state = chr ? RS_IDLE : RS_INACTIVE;
2351 s->mon_chr = mon_chr;
cdb432b2 2352 s->current_syscall_cb = NULL;
8a34a0fb 2353
b4608c04
FB
2354 return 0;
2355}
777357d7 2356
1bb982b8
KF
2357void gdbserver_cleanup(void)
2358{
2359 if (gdbserver_state) {
2360 put_packet(gdbserver_state, "W00");
2361 }
2362}
2363
777357d7
MAL
2364static void register_types(void)
2365{
2366 type_register_static(&char_gdb_type_info);
2367}
2368
2369type_init(register_types);
4046d913 2370#endif