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