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