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