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