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