]> git.proxmox.com Git - mirror_qemu.git/blame - gdbstub.c
target-cris: Factor out CPUClass::gdb_read_register() hook for v10
[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
bc7d0e66
PB
371 if (runstate_check(RUN_STATE_GUEST_PANICKED)) {
372 runstate_set(RUN_STATE_DEBUG);
373 }
26ac7a31 374 if (!runstate_needs_reset()) {
87f25c12
PB
375 vm_start();
376 }
ba70a624
EI
377#endif
378}
379
858693c6 380static void put_buffer(GDBState *s, const uint8_t *buf, int len)
b4608c04 381{
4046d913 382#ifdef CONFIG_USER_ONLY
b4608c04
FB
383 int ret;
384
385 while (len > 0) {
8f447cc7 386 ret = send(s->fd, buf, len, 0);
b4608c04
FB
387 if (ret < 0) {
388 if (errno != EINTR && errno != EAGAIN)
389 return;
390 } else {
391 buf += ret;
392 len -= ret;
393 }
394 }
4046d913 395#else
2cc6e0a1 396 qemu_chr_fe_write(s->chr, buf, len);
4046d913 397#endif
b4608c04
FB
398}
399
400static inline int fromhex(int v)
401{
402 if (v >= '0' && v <= '9')
403 return v - '0';
404 else if (v >= 'A' && v <= 'F')
405 return v - 'A' + 10;
406 else if (v >= 'a' && v <= 'f')
407 return v - 'a' + 10;
408 else
409 return 0;
410}
411
412static inline int tohex(int v)
413{
414 if (v < 10)
415 return v + '0';
416 else
417 return v - 10 + 'a';
418}
419
420static void memtohex(char *buf, const uint8_t *mem, int len)
421{
422 int i, c;
423 char *q;
424 q = buf;
425 for(i = 0; i < len; i++) {
426 c = mem[i];
427 *q++ = tohex(c >> 4);
428 *q++ = tohex(c & 0xf);
429 }
430 *q = '\0';
431}
432
433static void hextomem(uint8_t *mem, const char *buf, int len)
434{
435 int i;
436
437 for(i = 0; i < len; i++) {
438 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
439 buf += 2;
440 }
441}
442
b4608c04 443/* return -1 if error, 0 if OK */
56aebc89 444static int put_packet_binary(GDBState *s, const char *buf, int len)
b4608c04 445{
56aebc89 446 int csum, i;
60fe76f3 447 uint8_t *p;
b4608c04 448
b4608c04 449 for(;;) {
4046d913
PB
450 p = s->last_packet;
451 *(p++) = '$';
4046d913
PB
452 memcpy(p, buf, len);
453 p += len;
b4608c04
FB
454 csum = 0;
455 for(i = 0; i < len; i++) {
456 csum += buf[i];
457 }
4046d913
PB
458 *(p++) = '#';
459 *(p++) = tohex((csum >> 4) & 0xf);
460 *(p++) = tohex((csum) & 0xf);
b4608c04 461
4046d913 462 s->last_packet_len = p - s->last_packet;
ffe8ab83 463 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
b4608c04 464
4046d913
PB
465#ifdef CONFIG_USER_ONLY
466 i = get_char(s);
467 if (i < 0)
b4608c04 468 return -1;
4046d913 469 if (i == '+')
b4608c04 470 break;
4046d913
PB
471#else
472 break;
473#endif
b4608c04
FB
474 }
475 return 0;
476}
477
56aebc89
PB
478/* return -1 if error, 0 if OK */
479static int put_packet(GDBState *s, const char *buf)
480{
481#ifdef DEBUG_GDB
482 printf("reply='%s'\n", buf);
483#endif
79808573 484
56aebc89
PB
485 return put_packet_binary(s, buf, strlen(buf));
486}
487
5b50e790 488#if defined(TARGET_PPC)
9e62fd7f 489
e571cb47
AJ
490#if defined (TARGET_PPC64)
491#define GDB_CORE_XML "power64-core.xml"
492#else
493#define GDB_CORE_XML "power-core.xml"
494#endif
9e62fd7f 495
1fddef4b 496#elif defined (TARGET_ARM)
6da41eaf 497
56aebc89 498#define GDB_CORE_XML "arm-core.xml"
e6e5906b 499
56aebc89 500#elif defined (TARGET_M68K)
6f970bd9 501
56aebc89 502#define GDB_CORE_XML "cf-core.xml"
6f970bd9 503
56aebc89 504#endif
f1ccf904 505
56aebc89
PB
506#ifdef GDB_CORE_XML
507/* Encode data using the encoding for 'x' packets. */
508static int memtox(char *buf, const char *mem, int len)
509{
510 char *p = buf;
511 char c;
512
513 while (len--) {
514 c = *(mem++);
515 switch (c) {
516 case '#': case '$': case '*': case '}':
517 *(p++) = '}';
518 *(p++) = c ^ 0x20;
519 break;
520 default:
521 *(p++) = c;
522 break;
523 }
524 }
525 return p - buf;
526}
f1ccf904 527
3faf778e 528static const char *get_feature_xml(const char *p, const char **newp)
56aebc89 529{
56aebc89
PB
530 size_t len;
531 int i;
532 const char *name;
533 static char target_xml[1024];
534
535 len = 0;
536 while (p[len] && p[len] != ':')
537 len++;
538 *newp = p + len;
539
540 name = NULL;
541 if (strncmp(p, "target.xml", len) == 0) {
542 /* Generate the XML description for this CPU. */
543 if (!target_xml[0]) {
544 GDBRegisterState *r;
eac8b355 545 CPUState *cpu = first_cpu;
56aebc89 546
5b3715bf
BS
547 snprintf(target_xml, sizeof(target_xml),
548 "<?xml version=\"1.0\"?>"
549 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
550 "<target>"
551 "<xi:include href=\"%s\"/>",
552 GDB_CORE_XML);
56aebc89 553
eac8b355 554 for (r = cpu->gdb_regs; r; r = r->next) {
2dc766da
BS
555 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
556 pstrcat(target_xml, sizeof(target_xml), r->xml);
557 pstrcat(target_xml, sizeof(target_xml), "\"/>");
56aebc89 558 }
2dc766da 559 pstrcat(target_xml, sizeof(target_xml), "</target>");
56aebc89
PB
560 }
561 return target_xml;
562 }
563 for (i = 0; ; i++) {
564 name = xml_builtin[i][0];
565 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
566 break;
567 }
568 return name ? xml_builtin[i][1] : NULL;
569}
570#endif
f1ccf904 571
385b9f0e 572static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
56aebc89 573{
a0e372f0 574 CPUClass *cc = CPU_GET_CLASS(cpu);
385b9f0e 575 CPUArchState *env = cpu->env_ptr;
56aebc89 576 GDBRegisterState *r;
f1ccf904 577
a0e372f0 578 if (reg < cc->gdb_num_core_regs) {
5b50e790 579 return cc->gdb_read_register(cpu, mem_buf, reg);
a0e372f0 580 }
f1ccf904 581
eac8b355 582 for (r = cpu->gdb_regs; r; r = r->next) {
56aebc89
PB
583 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
584 return r->get_reg(env, mem_buf, reg - r->base_reg);
585 }
586 }
587 return 0;
f1ccf904
TS
588}
589
385b9f0e 590static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
f1ccf904 591{
a0e372f0 592 CPUClass *cc = CPU_GET_CLASS(cpu);
385b9f0e 593 CPUArchState *env = cpu->env_ptr;
56aebc89 594 GDBRegisterState *r;
f1ccf904 595
a0e372f0 596 if (reg < cc->gdb_num_core_regs) {
5b50e790 597 return cc->gdb_write_register(cpu, mem_buf, reg);
a0e372f0 598 }
56aebc89 599
eac8b355 600 for (r = cpu->gdb_regs; r; r = r->next) {
56aebc89
PB
601 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
602 return r->set_reg(env, mem_buf, reg - r->base_reg);
603 }
604 }
6da41eaf
FB
605 return 0;
606}
607
56aebc89
PB
608/* Register a supplemental set of CPU registers. If g_pos is nonzero it
609 specifies the first register number and these registers are included in
610 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
611 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
612 */
613
22169d41
AF
614void gdb_register_coprocessor(CPUState *cpu,
615 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
616 int num_regs, const char *xml, int g_pos)
6da41eaf 617{
56aebc89
PB
618 GDBRegisterState *s;
619 GDBRegisterState **p;
56aebc89 620
eac8b355 621 p = &cpu->gdb_regs;
56aebc89
PB
622 while (*p) {
623 /* Check for duplicates. */
624 if (strcmp((*p)->xml, xml) == 0)
625 return;
626 p = &(*p)->next;
627 }
9643c25f
SW
628
629 s = g_new0(GDBRegisterState, 1);
a0e372f0 630 s->base_reg = cpu->gdb_num_regs;
9643c25f
SW
631 s->num_regs = num_regs;
632 s->get_reg = get_reg;
633 s->set_reg = set_reg;
634 s->xml = xml;
635
56aebc89 636 /* Add to end of list. */
a0e372f0 637 cpu->gdb_num_regs += num_regs;
56aebc89
PB
638 *p = s;
639 if (g_pos) {
640 if (g_pos != s->base_reg) {
641 fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
642 "Expected %d got %d\n", xml, g_pos, s->base_reg);
56aebc89
PB
643 }
644 }
6da41eaf
FB
645}
646
a1d1bb31
AL
647#ifndef CONFIG_USER_ONLY
648static const int xlat_gdb_type[] = {
649 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
650 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
651 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
652};
653#endif
654
880a7578 655static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
a1d1bb31 656{
182735ef 657 CPUState *cpu;
9349b4f9 658 CPUArchState *env;
880a7578
AL
659 int err = 0;
660
62278814 661 if (kvm_enabled()) {
2e0f2cfb 662 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
62278814 663 }
e22a25c9 664
a1d1bb31
AL
665 switch (type) {
666 case GDB_BREAKPOINT_SW:
667 case GDB_BREAKPOINT_HW:
182735ef
AF
668 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
669 env = cpu->env_ptr;
880a7578
AL
670 err = cpu_breakpoint_insert(env, addr, BP_GDB, NULL);
671 if (err)
672 break;
673 }
674 return err;
a1d1bb31
AL
675#ifndef CONFIG_USER_ONLY
676 case GDB_WATCHPOINT_WRITE:
677 case GDB_WATCHPOINT_READ:
678 case GDB_WATCHPOINT_ACCESS:
182735ef
AF
679 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
680 env = cpu->env_ptr;
880a7578
AL
681 err = cpu_watchpoint_insert(env, addr, len, xlat_gdb_type[type],
682 NULL);
683 if (err)
684 break;
685 }
686 return err;
a1d1bb31
AL
687#endif
688 default:
689 return -ENOSYS;
690 }
691}
692
880a7578 693static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
a1d1bb31 694{
182735ef 695 CPUState *cpu;
9349b4f9 696 CPUArchState *env;
880a7578
AL
697 int err = 0;
698
62278814 699 if (kvm_enabled()) {
2e0f2cfb 700 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
62278814 701 }
e22a25c9 702
a1d1bb31
AL
703 switch (type) {
704 case GDB_BREAKPOINT_SW:
705 case GDB_BREAKPOINT_HW:
182735ef
AF
706 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
707 env = cpu->env_ptr;
880a7578
AL
708 err = cpu_breakpoint_remove(env, addr, BP_GDB);
709 if (err)
710 break;
711 }
712 return err;
a1d1bb31
AL
713#ifndef CONFIG_USER_ONLY
714 case GDB_WATCHPOINT_WRITE:
715 case GDB_WATCHPOINT_READ:
716 case GDB_WATCHPOINT_ACCESS:
182735ef
AF
717 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
718 env = cpu->env_ptr;
880a7578
AL
719 err = cpu_watchpoint_remove(env, addr, len, xlat_gdb_type[type]);
720 if (err)
721 break;
722 }
723 return err;
a1d1bb31
AL
724#endif
725 default:
726 return -ENOSYS;
727 }
728}
729
880a7578 730static void gdb_breakpoint_remove_all(void)
a1d1bb31 731{
182735ef 732 CPUState *cpu;
9349b4f9 733 CPUArchState *env;
880a7578 734
e22a25c9 735 if (kvm_enabled()) {
2e0f2cfb 736 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
e22a25c9
AL
737 return;
738 }
739
182735ef
AF
740 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
741 env = cpu->env_ptr;
880a7578 742 cpu_breakpoint_remove_all(env, BP_GDB);
a1d1bb31 743#ifndef CONFIG_USER_ONLY
880a7578 744 cpu_watchpoint_remove_all(env, BP_GDB);
a1d1bb31 745#endif
880a7578 746 }
a1d1bb31
AL
747}
748
fab9d284
AJ
749static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
750{
2e0f2cfb 751 CPUState *cpu = s->c_cpu;
f45748f1
AF
752 CPUClass *cc = CPU_GET_CLASS(cpu);
753
754 cpu_synchronize_state(cpu);
755 if (cc->set_pc) {
756 cc->set_pc(cpu, pc);
ff1d1977 757 }
fab9d284
AJ
758}
759
2e0f2cfb 760static CPUState *find_cpu(uint32_t thread_id)
1e9fa730 761{
0d34282f 762 CPUState *cpu;
1e9fa730 763
182735ef 764 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
aa48dd93 765 if (cpu_index(cpu) == thread_id) {
2e0f2cfb 766 return cpu;
aa48dd93 767 }
1e9fa730 768 }
aa48dd93
AF
769
770 return NULL;
1e9fa730
NF
771}
772
880a7578 773static int gdb_handle_packet(GDBState *s, const char *line_buf)
b4608c04 774{
2e0f2cfb 775 CPUState *cpu;
b4608c04 776 const char *p;
1e9fa730
NF
777 uint32_t thread;
778 int ch, reg_size, type, res;
56aebc89
PB
779 char buf[MAX_PACKET_LENGTH];
780 uint8_t mem_buf[MAX_PACKET_LENGTH];
781 uint8_t *registers;
9d9754a3 782 target_ulong addr, len;
3b46e624 783
858693c6
FB
784#ifdef DEBUG_GDB
785 printf("command='%s'\n", line_buf);
786#endif
787 p = line_buf;
788 ch = *p++;
789 switch(ch) {
790 case '?':
1fddef4b 791 /* TODO: Make this return the correct value for user-mode. */
ca587a8e 792 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
2e0f2cfb 793 cpu_index(s->c_cpu));
858693c6 794 put_packet(s, buf);
7d03f82f
EI
795 /* Remove all the breakpoints when this query is issued,
796 * because gdb is doing and initial connect and the state
797 * should be cleaned up.
798 */
880a7578 799 gdb_breakpoint_remove_all();
858693c6
FB
800 break;
801 case 'c':
802 if (*p != '\0') {
9d9754a3 803 addr = strtoull(p, (char **)&p, 16);
fab9d284 804 gdb_set_cpu_pc(s, addr);
858693c6 805 }
ca587a8e 806 s->signal = 0;
ba70a624 807 gdb_continue(s);
41625033 808 return RS_IDLE;
1f487ee9 809 case 'C':
ca587a8e
AJ
810 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
811 if (s->signal == -1)
812 s->signal = 0;
1f487ee9
EI
813 gdb_continue(s);
814 return RS_IDLE;
dd32aa10
JK
815 case 'v':
816 if (strncmp(p, "Cont", 4) == 0) {
817 int res_signal, res_thread;
818
819 p += 4;
820 if (*p == '?') {
821 put_packet(s, "vCont;c;C;s;S");
822 break;
823 }
824 res = 0;
825 res_signal = 0;
826 res_thread = 0;
827 while (*p) {
828 int action, signal;
829
830 if (*p++ != ';') {
831 res = 0;
832 break;
833 }
834 action = *p++;
835 signal = 0;
836 if (action == 'C' || action == 'S') {
837 signal = strtoul(p, (char **)&p, 16);
838 } else if (action != 'c' && action != 's') {
839 res = 0;
840 break;
841 }
842 thread = 0;
843 if (*p == ':') {
844 thread = strtoull(p+1, (char **)&p, 16);
845 }
846 action = tolower(action);
847 if (res == 0 || (res == 'c' && action == 's')) {
848 res = action;
849 res_signal = signal;
850 res_thread = thread;
851 }
852 }
853 if (res) {
854 if (res_thread != -1 && res_thread != 0) {
2e0f2cfb
AF
855 cpu = find_cpu(res_thread);
856 if (cpu == NULL) {
dd32aa10
JK
857 put_packet(s, "E22");
858 break;
859 }
2e0f2cfb 860 s->c_cpu = cpu;
dd32aa10
JK
861 }
862 if (res == 's') {
2e0f2cfb 863 cpu_single_step(s->c_cpu, sstep_flags);
dd32aa10
JK
864 }
865 s->signal = res_signal;
866 gdb_continue(s);
867 return RS_IDLE;
868 }
869 break;
870 } else {
871 goto unknown_command;
872 }
7d03f82f 873 case 'k':
00e94dbc 874#ifdef CONFIG_USER_ONLY
7d03f82f
EI
875 /* Kill the target */
876 fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
877 exit(0);
00e94dbc 878#endif
7d03f82f
EI
879 case 'D':
880 /* Detach packet */
880a7578 881 gdb_breakpoint_remove_all();
7ea06da3 882 gdb_syscall_mode = GDB_SYS_DISABLED;
7d03f82f
EI
883 gdb_continue(s);
884 put_packet(s, "OK");
885 break;
858693c6
FB
886 case 's':
887 if (*p != '\0') {
8fac5803 888 addr = strtoull(p, (char **)&p, 16);
fab9d284 889 gdb_set_cpu_pc(s, addr);
858693c6 890 }
2e0f2cfb 891 cpu_single_step(s->c_cpu, sstep_flags);
ba70a624 892 gdb_continue(s);
41625033 893 return RS_IDLE;
a2d1ebaf
PB
894 case 'F':
895 {
896 target_ulong ret;
897 target_ulong err;
898
899 ret = strtoull(p, (char **)&p, 16);
900 if (*p == ',') {
901 p++;
902 err = strtoull(p, (char **)&p, 16);
903 } else {
904 err = 0;
905 }
906 if (*p == ',')
907 p++;
908 type = *p;
cdb432b2 909 if (s->current_syscall_cb) {
2e0f2cfb 910 s->current_syscall_cb(s->c_cpu, ret, err);
cdb432b2
MI
911 s->current_syscall_cb = NULL;
912 }
a2d1ebaf
PB
913 if (type == 'C') {
914 put_packet(s, "T02");
915 } else {
ba70a624 916 gdb_continue(s);
a2d1ebaf
PB
917 }
918 }
919 break;
858693c6 920 case 'g':
2e0f2cfb 921 cpu_synchronize_state(s->g_cpu);
56aebc89 922 len = 0;
a0e372f0 923 for (addr = 0; addr < s->g_cpu->gdb_num_regs; addr++) {
2e0f2cfb 924 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
56aebc89
PB
925 len += reg_size;
926 }
927 memtohex(buf, mem_buf, len);
858693c6
FB
928 put_packet(s, buf);
929 break;
930 case 'G':
2e0f2cfb 931 cpu_synchronize_state(s->g_cpu);
56aebc89 932 registers = mem_buf;
858693c6
FB
933 len = strlen(p) / 2;
934 hextomem((uint8_t *)registers, p, len);
a0e372f0 935 for (addr = 0; addr < s->g_cpu->gdb_num_regs && len > 0; addr++) {
2e0f2cfb 936 reg_size = gdb_write_register(s->g_cpu, registers, addr);
56aebc89
PB
937 len -= reg_size;
938 registers += reg_size;
939 }
858693c6
FB
940 put_packet(s, "OK");
941 break;
942 case 'm':
9d9754a3 943 addr = strtoull(p, (char **)&p, 16);
858693c6
FB
944 if (*p == ',')
945 p++;
9d9754a3 946 len = strtoull(p, NULL, 16);
2e0f2cfb 947 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
6f970bd9
FB
948 put_packet (s, "E14");
949 } else {
950 memtohex(buf, mem_buf, len);
951 put_packet(s, buf);
952 }
858693c6
FB
953 break;
954 case 'M':
9d9754a3 955 addr = strtoull(p, (char **)&p, 16);
858693c6
FB
956 if (*p == ',')
957 p++;
9d9754a3 958 len = strtoull(p, (char **)&p, 16);
b328f873 959 if (*p == ':')
858693c6
FB
960 p++;
961 hextomem(mem_buf, p, len);
2e0f2cfb 962 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
f3659eee 963 true) != 0) {
905f20b1 964 put_packet(s, "E14");
44520db1 965 } else {
858693c6 966 put_packet(s, "OK");
44520db1 967 }
858693c6 968 break;
56aebc89
PB
969 case 'p':
970 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
971 This works, but can be very slow. Anything new enough to
972 understand XML also knows how to use this properly. */
973 if (!gdb_has_xml)
974 goto unknown_command;
975 addr = strtoull(p, (char **)&p, 16);
2e0f2cfb 976 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
56aebc89
PB
977 if (reg_size) {
978 memtohex(buf, mem_buf, reg_size);
979 put_packet(s, buf);
980 } else {
981 put_packet(s, "E14");
982 }
983 break;
984 case 'P':
985 if (!gdb_has_xml)
986 goto unknown_command;
987 addr = strtoull(p, (char **)&p, 16);
988 if (*p == '=')
989 p++;
990 reg_size = strlen(p) / 2;
991 hextomem(mem_buf, p, reg_size);
2e0f2cfb 992 gdb_write_register(s->g_cpu, mem_buf, addr);
56aebc89
PB
993 put_packet(s, "OK");
994 break;
858693c6 995 case 'Z':
858693c6
FB
996 case 'z':
997 type = strtoul(p, (char **)&p, 16);
998 if (*p == ',')
999 p++;
9d9754a3 1000 addr = strtoull(p, (char **)&p, 16);
858693c6
FB
1001 if (*p == ',')
1002 p++;
9d9754a3 1003 len = strtoull(p, (char **)&p, 16);
a1d1bb31 1004 if (ch == 'Z')
880a7578 1005 res = gdb_breakpoint_insert(addr, len, type);
a1d1bb31 1006 else
880a7578 1007 res = gdb_breakpoint_remove(addr, len, type);
a1d1bb31
AL
1008 if (res >= 0)
1009 put_packet(s, "OK");
1010 else if (res == -ENOSYS)
0f459d16 1011 put_packet(s, "");
a1d1bb31
AL
1012 else
1013 put_packet(s, "E22");
858693c6 1014 break;
880a7578
AL
1015 case 'H':
1016 type = *p++;
1017 thread = strtoull(p, (char **)&p, 16);
1018 if (thread == -1 || thread == 0) {
1019 put_packet(s, "OK");
1020 break;
1021 }
2e0f2cfb
AF
1022 cpu = find_cpu(thread);
1023 if (cpu == NULL) {
880a7578
AL
1024 put_packet(s, "E22");
1025 break;
1026 }
1027 switch (type) {
1028 case 'c':
2e0f2cfb 1029 s->c_cpu = cpu;
880a7578
AL
1030 put_packet(s, "OK");
1031 break;
1032 case 'g':
2e0f2cfb 1033 s->g_cpu = cpu;
880a7578
AL
1034 put_packet(s, "OK");
1035 break;
1036 default:
1037 put_packet(s, "E22");
1038 break;
1039 }
1040 break;
1041 case 'T':
1042 thread = strtoull(p, (char **)&p, 16);
2e0f2cfb 1043 cpu = find_cpu(thread);
1e9fa730 1044
2e0f2cfb 1045 if (cpu != NULL) {
1e9fa730
NF
1046 put_packet(s, "OK");
1047 } else {
880a7578 1048 put_packet(s, "E22");
1e9fa730 1049 }
880a7578 1050 break;
978efd6a 1051 case 'q':
60897d36
EI
1052 case 'Q':
1053 /* parse any 'q' packets here */
1054 if (!strcmp(p,"qemu.sstepbits")) {
1055 /* Query Breakpoint bit definitions */
363a37d5
BS
1056 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1057 SSTEP_ENABLE,
1058 SSTEP_NOIRQ,
1059 SSTEP_NOTIMER);
60897d36
EI
1060 put_packet(s, buf);
1061 break;
1062 } else if (strncmp(p,"qemu.sstep",10) == 0) {
1063 /* Display or change the sstep_flags */
1064 p += 10;
1065 if (*p != '=') {
1066 /* Display current setting */
363a37d5 1067 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
60897d36
EI
1068 put_packet(s, buf);
1069 break;
1070 }
1071 p++;
1072 type = strtoul(p, (char **)&p, 16);
1073 sstep_flags = type;
1074 put_packet(s, "OK");
1075 break;
880a7578
AL
1076 } else if (strcmp(p,"C") == 0) {
1077 /* "Current thread" remains vague in the spec, so always return
1078 * the first CPU (gdb returns the first thread). */
1079 put_packet(s, "QC1");
1080 break;
1081 } else if (strcmp(p,"fThreadInfo") == 0) {
52f34623 1082 s->query_cpu = first_cpu;
880a7578
AL
1083 goto report_cpuinfo;
1084 } else if (strcmp(p,"sThreadInfo") == 0) {
1085 report_cpuinfo:
1086 if (s->query_cpu) {
52f34623 1087 snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu));
880a7578 1088 put_packet(s, buf);
52f34623 1089 s->query_cpu = s->query_cpu->next_cpu;
880a7578
AL
1090 } else
1091 put_packet(s, "l");
1092 break;
1093 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1094 thread = strtoull(p+16, (char **)&p, 16);
2e0f2cfb
AF
1095 cpu = find_cpu(thread);
1096 if (cpu != NULL) {
cb446eca 1097 cpu_synchronize_state(cpu);
1e9fa730 1098 len = snprintf((char *)mem_buf, sizeof(mem_buf),
55e5c285 1099 "CPU#%d [%s]", cpu->cpu_index,
259186a7 1100 cpu->halted ? "halted " : "running");
1e9fa730
NF
1101 memtohex(buf, mem_buf, len);
1102 put_packet(s, buf);
1103 }
880a7578 1104 break;
60897d36 1105 }
0b8a988c 1106#ifdef CONFIG_USER_ONLY
60897d36 1107 else if (strncmp(p, "Offsets", 7) == 0) {
2e0f2cfb
AF
1108 CPUArchState *env = s->c_cpu->env_ptr;
1109 TaskState *ts = env->opaque;
978efd6a 1110
363a37d5
BS
1111 snprintf(buf, sizeof(buf),
1112 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1113 ";Bss=" TARGET_ABI_FMT_lx,
1114 ts->info->code_offset,
1115 ts->info->data_offset,
1116 ts->info->data_offset);
978efd6a
PB
1117 put_packet(s, buf);
1118 break;
1119 }
0b8a988c 1120#else /* !CONFIG_USER_ONLY */
8a34a0fb
AL
1121 else if (strncmp(p, "Rcmd,", 5) == 0) {
1122 int len = strlen(p + 5);
1123
1124 if ((len % 2) != 0) {
1125 put_packet(s, "E01");
1126 break;
1127 }
1128 hextomem(mem_buf, p + 5, len);
1129 len = len / 2;
1130 mem_buf[len++] = 0;
fa5efccb 1131 qemu_chr_be_write(s->mon_chr, mem_buf, len);
8a34a0fb
AL
1132 put_packet(s, "OK");
1133 break;
1134 }
0b8a988c 1135#endif /* !CONFIG_USER_ONLY */
56aebc89 1136 if (strncmp(p, "Supported", 9) == 0) {
5b3715bf 1137 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
56aebc89 1138#ifdef GDB_CORE_XML
2dc766da 1139 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
56aebc89
PB
1140#endif
1141 put_packet(s, buf);
1142 break;
1143 }
1144#ifdef GDB_CORE_XML
1145 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1146 const char *xml;
1147 target_ulong total_len;
1148
5b50e790 1149 gdb_has_xml = true;
56aebc89 1150 p += 19;
880a7578 1151 xml = get_feature_xml(p, &p);
56aebc89 1152 if (!xml) {
5b3715bf 1153 snprintf(buf, sizeof(buf), "E00");
56aebc89
PB
1154 put_packet(s, buf);
1155 break;
1156 }
1157
1158 if (*p == ':')
1159 p++;
1160 addr = strtoul(p, (char **)&p, 16);
1161 if (*p == ',')
1162 p++;
1163 len = strtoul(p, (char **)&p, 16);
1164
1165 total_len = strlen(xml);
1166 if (addr > total_len) {
5b3715bf 1167 snprintf(buf, sizeof(buf), "E00");
56aebc89
PB
1168 put_packet(s, buf);
1169 break;
1170 }
1171 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1172 len = (MAX_PACKET_LENGTH - 5) / 2;
1173 if (len < total_len - addr) {
1174 buf[0] = 'm';
1175 len = memtox(buf + 1, xml + addr, len);
1176 } else {
1177 buf[0] = 'l';
1178 len = memtox(buf + 1, xml + addr, total_len - addr);
1179 }
1180 put_packet_binary(s, buf, len + 1);
1181 break;
1182 }
1183#endif
1184 /* Unrecognised 'q' command. */
1185 goto unknown_command;
1186
858693c6 1187 default:
56aebc89 1188 unknown_command:
858693c6
FB
1189 /* put empty packet */
1190 buf[0] = '\0';
1191 put_packet(s, buf);
1192 break;
1193 }
1194 return RS_IDLE;
1195}
1196
64f6b346 1197void gdb_set_stop_cpu(CPUState *cpu)
880a7578 1198{
2e0f2cfb
AF
1199 gdbserver_state->c_cpu = cpu;
1200 gdbserver_state->g_cpu = cpu;
880a7578
AL
1201}
1202
1fddef4b 1203#ifndef CONFIG_USER_ONLY
1dfb4dd9 1204static void gdb_vm_state_change(void *opaque, int running, RunState state)
858693c6 1205{
880a7578 1206 GDBState *s = gdbserver_state;
2e0f2cfb
AF
1207 CPUArchState *env = s->c_cpu->env_ptr;
1208 CPUState *cpu = s->c_cpu;
858693c6 1209 char buf[256];
d6fc1b39 1210 const char *type;
858693c6
FB
1211 int ret;
1212
cdb432b2
MI
1213 if (running || s->state == RS_INACTIVE) {
1214 return;
1215 }
1216 /* Is there a GDB syscall waiting to be sent? */
1217 if (s->current_syscall_cb) {
1218 put_packet(s, s->syscall_buf);
a2d1ebaf 1219 return;
e07bbac5 1220 }
1dfb4dd9 1221 switch (state) {
0461d5a6 1222 case RUN_STATE_DEBUG:
880a7578
AL
1223 if (env->watchpoint_hit) {
1224 switch (env->watchpoint_hit->flags & BP_MEM_ACCESS) {
a1d1bb31 1225 case BP_MEM_READ:
d6fc1b39
AL
1226 type = "r";
1227 break;
a1d1bb31 1228 case BP_MEM_ACCESS:
d6fc1b39
AL
1229 type = "a";
1230 break;
1231 default:
1232 type = "";
1233 break;
1234 }
880a7578
AL
1235 snprintf(buf, sizeof(buf),
1236 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
0d34282f 1237 GDB_SIGNAL_TRAP, cpu_index(cpu), type,
880a7578 1238 env->watchpoint_hit->vaddr);
880a7578 1239 env->watchpoint_hit = NULL;
425189a8 1240 goto send_packet;
6658ffb8 1241 }
425189a8 1242 tb_flush(env);
ca587a8e 1243 ret = GDB_SIGNAL_TRAP;
425189a8 1244 break;
0461d5a6 1245 case RUN_STATE_PAUSED:
9781e040 1246 ret = GDB_SIGNAL_INT;
425189a8 1247 break;
0461d5a6 1248 case RUN_STATE_SHUTDOWN:
425189a8
JK
1249 ret = GDB_SIGNAL_QUIT;
1250 break;
0461d5a6 1251 case RUN_STATE_IO_ERROR:
425189a8
JK
1252 ret = GDB_SIGNAL_IO;
1253 break;
0461d5a6 1254 case RUN_STATE_WATCHDOG:
425189a8
JK
1255 ret = GDB_SIGNAL_ALRM;
1256 break;
0461d5a6 1257 case RUN_STATE_INTERNAL_ERROR:
425189a8
JK
1258 ret = GDB_SIGNAL_ABRT;
1259 break;
0461d5a6
LC
1260 case RUN_STATE_SAVE_VM:
1261 case RUN_STATE_RESTORE_VM:
425189a8 1262 return;
0461d5a6 1263 case RUN_STATE_FINISH_MIGRATE:
425189a8
JK
1264 ret = GDB_SIGNAL_XCPU;
1265 break;
1266 default:
1267 ret = GDB_SIGNAL_UNKNOWN;
1268 break;
bbeb7b5c 1269 }
0d34282f 1270 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(cpu));
425189a8
JK
1271
1272send_packet:
858693c6 1273 put_packet(s, buf);
425189a8
JK
1274
1275 /* disable single step if it was enabled */
3825b28f 1276 cpu_single_step(cpu, 0);
858693c6 1277}
1fddef4b 1278#endif
858693c6 1279
a2d1ebaf
PB
1280/* Send a gdb syscall request.
1281 This accepts limited printf-style format specifiers, specifically:
a87295e8
PB
1282 %x - target_ulong argument printed in hex.
1283 %lx - 64-bit argument printed in hex.
1284 %s - string pointer (target_ulong) and length (int) pair. */
7ccfb2eb 1285void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
a2d1ebaf
PB
1286{
1287 va_list va;
a2d1ebaf 1288 char *p;
cdb432b2 1289 char *p_end;
a2d1ebaf 1290 target_ulong addr;
a87295e8 1291 uint64_t i64;
a2d1ebaf
PB
1292 GDBState *s;
1293
880a7578 1294 s = gdbserver_state;
a2d1ebaf
PB
1295 if (!s)
1296 return;
cdb432b2 1297 s->current_syscall_cb = cb;
a2d1ebaf 1298#ifndef CONFIG_USER_ONLY
0461d5a6 1299 vm_stop(RUN_STATE_DEBUG);
a2d1ebaf 1300#endif
a2d1ebaf 1301 va_start(va, fmt);
cdb432b2
MI
1302 p = s->syscall_buf;
1303 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
a2d1ebaf
PB
1304 *(p++) = 'F';
1305 while (*fmt) {
1306 if (*fmt == '%') {
1307 fmt++;
1308 switch (*fmt++) {
1309 case 'x':
1310 addr = va_arg(va, target_ulong);
cdb432b2 1311 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
a2d1ebaf 1312 break;
a87295e8
PB
1313 case 'l':
1314 if (*(fmt++) != 'x')
1315 goto bad_format;
1316 i64 = va_arg(va, uint64_t);
cdb432b2 1317 p += snprintf(p, p_end - p, "%" PRIx64, i64);
a87295e8 1318 break;
a2d1ebaf
PB
1319 case 's':
1320 addr = va_arg(va, target_ulong);
cdb432b2 1321 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
363a37d5 1322 addr, va_arg(va, int));
a2d1ebaf
PB
1323 break;
1324 default:
a87295e8 1325 bad_format:
a2d1ebaf
PB
1326 fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
1327 fmt - 1);
1328 break;
1329 }
1330 } else {
1331 *(p++) = *(fmt++);
1332 }
1333 }
8a93e02a 1334 *p = 0;
a2d1ebaf 1335 va_end(va);
a2d1ebaf 1336#ifdef CONFIG_USER_ONLY
cdb432b2 1337 put_packet(s, s->syscall_buf);
2e0f2cfb 1338 gdb_handlesig(s->c_cpu, 0);
a2d1ebaf 1339#else
cdb432b2
MI
1340 /* In this case wait to send the syscall packet until notification that
1341 the CPU has stopped. This must be done because if the packet is sent
1342 now the reply from the syscall request could be received while the CPU
1343 is still in the running state, which can cause packets to be dropped
1344 and state transition 'T' packets to be sent while the syscall is still
1345 being processed. */
2e0f2cfb 1346 cpu_exit(s->c_cpu);
a2d1ebaf
PB
1347#endif
1348}
1349
6a00d601 1350static void gdb_read_byte(GDBState *s, int ch)
858693c6
FB
1351{
1352 int i, csum;
60fe76f3 1353 uint8_t reply;
858693c6 1354
1fddef4b 1355#ifndef CONFIG_USER_ONLY
4046d913
PB
1356 if (s->last_packet_len) {
1357 /* Waiting for a response to the last packet. If we see the start
1358 of a new command then abandon the previous response. */
1359 if (ch == '-') {
1360#ifdef DEBUG_GDB
1361 printf("Got NACK, retransmitting\n");
1362#endif
ffe8ab83 1363 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
4046d913
PB
1364 }
1365#ifdef DEBUG_GDB
1366 else if (ch == '+')
1367 printf("Got ACK\n");
1368 else
1369 printf("Got '%c' when expecting ACK/NACK\n", ch);
1370#endif
1371 if (ch == '+' || ch == '$')
1372 s->last_packet_len = 0;
1373 if (ch != '$')
1374 return;
1375 }
1354869c 1376 if (runstate_is_running()) {
858693c6
FB
1377 /* when the CPU is running, we cannot do anything except stop
1378 it when receiving a char */
0461d5a6 1379 vm_stop(RUN_STATE_PAUSED);
5fafdf24 1380 } else
1fddef4b 1381#endif
41625033 1382 {
858693c6
FB
1383 switch(s->state) {
1384 case RS_IDLE:
1385 if (ch == '$') {
1386 s->line_buf_index = 0;
1387 s->state = RS_GETLINE;
c33a346e 1388 }
b4608c04 1389 break;
858693c6
FB
1390 case RS_GETLINE:
1391 if (ch == '#') {
1392 s->state = RS_CHKSUM1;
1393 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1394 s->state = RS_IDLE;
4c3a88a2 1395 } else {
858693c6 1396 s->line_buf[s->line_buf_index++] = ch;
4c3a88a2
FB
1397 }
1398 break;
858693c6
FB
1399 case RS_CHKSUM1:
1400 s->line_buf[s->line_buf_index] = '\0';
1401 s->line_csum = fromhex(ch) << 4;
1402 s->state = RS_CHKSUM2;
1403 break;
1404 case RS_CHKSUM2:
1405 s->line_csum |= fromhex(ch);
1406 csum = 0;
1407 for(i = 0; i < s->line_buf_index; i++) {
1408 csum += s->line_buf[i];
1409 }
1410 if (s->line_csum != (csum & 0xff)) {
60fe76f3
TS
1411 reply = '-';
1412 put_buffer(s, &reply, 1);
858693c6 1413 s->state = RS_IDLE;
4c3a88a2 1414 } else {
60fe76f3
TS
1415 reply = '+';
1416 put_buffer(s, &reply, 1);
880a7578 1417 s->state = gdb_handle_packet(s, s->line_buf);
4c3a88a2
FB
1418 }
1419 break;
a2d1ebaf
PB
1420 default:
1421 abort();
858693c6
FB
1422 }
1423 }
1424}
1425
0e1c9c54 1426/* Tell the remote gdb that the process has exited. */
9349b4f9 1427void gdb_exit(CPUArchState *env, int code)
0e1c9c54
PB
1428{
1429 GDBState *s;
1430 char buf[4];
1431
1432 s = gdbserver_state;
1433 if (!s) {
1434 return;
1435 }
1436#ifdef CONFIG_USER_ONLY
1437 if (gdbserver_fd < 0 || s->fd < 0) {
1438 return;
1439 }
1440#endif
1441
1442 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
1443 put_packet(s, buf);
e2af15b2
FC
1444
1445#ifndef CONFIG_USER_ONLY
1446 if (s->chr) {
70f24fb6 1447 qemu_chr_delete(s->chr);
e2af15b2
FC
1448 }
1449#endif
0e1c9c54
PB
1450}
1451
1fddef4b 1452#ifdef CONFIG_USER_ONLY
ca587a8e
AJ
1453int
1454gdb_queuesig (void)
1455{
1456 GDBState *s;
1457
1458 s = gdbserver_state;
1459
1460 if (gdbserver_fd < 0 || s->fd < 0)
1461 return 0;
1462 else
1463 return 1;
1464}
1465
1fddef4b 1466int
db6b81d4 1467gdb_handlesig(CPUState *cpu, int sig)
1fddef4b 1468{
db6b81d4 1469 CPUArchState *env = cpu->env_ptr;
5ca666c7
AF
1470 GDBState *s;
1471 char buf[256];
1472 int n;
1fddef4b 1473
5ca666c7
AF
1474 s = gdbserver_state;
1475 if (gdbserver_fd < 0 || s->fd < 0) {
1476 return sig;
1477 }
1fddef4b 1478
5ca666c7 1479 /* disable single step if it was enabled */
3825b28f 1480 cpu_single_step(cpu, 0);
5ca666c7 1481 tb_flush(env);
1fddef4b 1482
5ca666c7
AF
1483 if (sig != 0) {
1484 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
1485 put_packet(s, buf);
1486 }
1487 /* put_packet() might have detected that the peer terminated the
1488 connection. */
1489 if (s->fd < 0) {
1490 return sig;
1491 }
1fddef4b 1492
5ca666c7
AF
1493 sig = 0;
1494 s->state = RS_IDLE;
1495 s->running_state = 0;
1496 while (s->running_state == 0) {
1497 n = read(s->fd, buf, 256);
1498 if (n > 0) {
1499 int i;
1500
1501 for (i = 0; i < n; i++) {
1502 gdb_read_byte(s, buf[i]);
1503 }
1504 } else if (n == 0 || errno != EAGAIN) {
1505 /* XXX: Connection closed. Should probably wait for another
1506 connection before continuing. */
1507 return sig;
1fddef4b 1508 }
5ca666c7
AF
1509 }
1510 sig = s->signal;
1511 s->signal = 0;
1512 return sig;
1fddef4b 1513}
e9009676 1514
ca587a8e 1515/* Tell the remote gdb that the process has exited due to SIG. */
9349b4f9 1516void gdb_signalled(CPUArchState *env, int sig)
ca587a8e 1517{
5ca666c7
AF
1518 GDBState *s;
1519 char buf[4];
ca587a8e 1520
5ca666c7
AF
1521 s = gdbserver_state;
1522 if (gdbserver_fd < 0 || s->fd < 0) {
1523 return;
1524 }
ca587a8e 1525
5ca666c7
AF
1526 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
1527 put_packet(s, buf);
ca587a8e 1528}
1fddef4b 1529
880a7578 1530static void gdb_accept(void)
858693c6
FB
1531{
1532 GDBState *s;
1533 struct sockaddr_in sockaddr;
1534 socklen_t len;
bf1c852a 1535 int fd;
858693c6
FB
1536
1537 for(;;) {
1538 len = sizeof(sockaddr);
1539 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1540 if (fd < 0 && errno != EINTR) {
1541 perror("accept");
1542 return;
1543 } else if (fd >= 0) {
40ff6d7e
KW
1544#ifndef _WIN32
1545 fcntl(fd, F_SETFD, FD_CLOEXEC);
1546#endif
b4608c04
FB
1547 break;
1548 }
1549 }
858693c6
FB
1550
1551 /* set short latency */
bf1c852a 1552 socket_set_nodelay(fd);
3b46e624 1553
7267c094 1554 s = g_malloc0(sizeof(GDBState));
2e0f2cfb
AF
1555 s->c_cpu = first_cpu;
1556 s->g_cpu = first_cpu;
858693c6 1557 s->fd = fd;
5b50e790 1558 gdb_has_xml = false;
858693c6 1559
880a7578 1560 gdbserver_state = s;
a2d1ebaf 1561
858693c6 1562 fcntl(fd, F_SETFL, O_NONBLOCK);
858693c6
FB
1563}
1564
1565static int gdbserver_open(int port)
1566{
1567 struct sockaddr_in sockaddr;
1568 int fd, val, ret;
1569
1570 fd = socket(PF_INET, SOCK_STREAM, 0);
1571 if (fd < 0) {
1572 perror("socket");
1573 return -1;
1574 }
40ff6d7e
KW
1575#ifndef _WIN32
1576 fcntl(fd, F_SETFD, FD_CLOEXEC);
1577#endif
858693c6
FB
1578
1579 /* allow fast reuse */
1580 val = 1;
9957fc7f 1581 qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
858693c6
FB
1582
1583 sockaddr.sin_family = AF_INET;
1584 sockaddr.sin_port = htons(port);
1585 sockaddr.sin_addr.s_addr = 0;
1586 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1587 if (ret < 0) {
1588 perror("bind");
bb16172c 1589 close(fd);
858693c6
FB
1590 return -1;
1591 }
1592 ret = listen(fd, 0);
1593 if (ret < 0) {
1594 perror("listen");
bb16172c 1595 close(fd);
858693c6
FB
1596 return -1;
1597 }
858693c6
FB
1598 return fd;
1599}
1600
1601int gdbserver_start(int port)
1602{
1603 gdbserver_fd = gdbserver_open(port);
1604 if (gdbserver_fd < 0)
1605 return -1;
1606 /* accept connections */
880a7578 1607 gdb_accept();
4046d913
PB
1608 return 0;
1609}
2b1319c8
AJ
1610
1611/* Disable gdb stub for child processes. */
9349b4f9 1612void gdbserver_fork(CPUArchState *env)
2b1319c8
AJ
1613{
1614 GDBState *s = gdbserver_state;
9f6164d6 1615 if (gdbserver_fd < 0 || s->fd < 0)
2b1319c8
AJ
1616 return;
1617 close(s->fd);
1618 s->fd = -1;
1619 cpu_breakpoint_remove_all(env, BP_GDB);
1620 cpu_watchpoint_remove_all(env, BP_GDB);
1621}
1fddef4b 1622#else
aa1f17c1 1623static int gdb_chr_can_receive(void *opaque)
4046d913 1624{
56aebc89
PB
1625 /* We can handle an arbitrarily large amount of data.
1626 Pick the maximum packet size, which is as good as anything. */
1627 return MAX_PACKET_LENGTH;
4046d913
PB
1628}
1629
aa1f17c1 1630static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
4046d913 1631{
4046d913
PB
1632 int i;
1633
1634 for (i = 0; i < size; i++) {
880a7578 1635 gdb_read_byte(gdbserver_state, buf[i]);
4046d913
PB
1636 }
1637}
1638
1639static void gdb_chr_event(void *opaque, int event)
1640{
1641 switch (event) {
b6b8df56 1642 case CHR_EVENT_OPENED:
0461d5a6 1643 vm_stop(RUN_STATE_PAUSED);
5b50e790 1644 gdb_has_xml = false;
4046d913
PB
1645 break;
1646 default:
1647 break;
1648 }
1649}
1650
8a34a0fb
AL
1651static void gdb_monitor_output(GDBState *s, const char *msg, int len)
1652{
1653 char buf[MAX_PACKET_LENGTH];
1654
1655 buf[0] = 'O';
1656 if (len > (MAX_PACKET_LENGTH/2) - 1)
1657 len = (MAX_PACKET_LENGTH/2) - 1;
1658 memtohex(buf + 1, (uint8_t *)msg, len);
1659 put_packet(s, buf);
1660}
1661
1662static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
1663{
1664 const char *p = (const char *)buf;
1665 int max_sz;
1666
1667 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
1668 for (;;) {
1669 if (len <= max_sz) {
1670 gdb_monitor_output(gdbserver_state, p, len);
1671 break;
1672 }
1673 gdb_monitor_output(gdbserver_state, p, max_sz);
1674 p += max_sz;
1675 len -= max_sz;
1676 }
1677 return len;
1678}
1679
59030a8c
AL
1680#ifndef _WIN32
1681static void gdb_sigterm_handler(int signal)
1682{
1354869c 1683 if (runstate_is_running()) {
0461d5a6 1684 vm_stop(RUN_STATE_PAUSED);
e07bbac5 1685 }
59030a8c
AL
1686}
1687#endif
1688
1689int gdbserver_start(const char *device)
4046d913
PB
1690{
1691 GDBState *s;
59030a8c 1692 char gdbstub_device_name[128];
36556b20
AL
1693 CharDriverState *chr = NULL;
1694 CharDriverState *mon_chr;
cfc3475a 1695
59030a8c
AL
1696 if (!device)
1697 return -1;
1698 if (strcmp(device, "none") != 0) {
1699 if (strstart(device, "tcp:", NULL)) {
1700 /* enforce required TCP attributes */
1701 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
1702 "%s,nowait,nodelay,server", device);
1703 device = gdbstub_device_name;
36556b20 1704 }
59030a8c
AL
1705#ifndef _WIN32
1706 else if (strcmp(device, "stdio") == 0) {
1707 struct sigaction act;
4046d913 1708
59030a8c
AL
1709 memset(&act, 0, sizeof(act));
1710 act.sa_handler = gdb_sigterm_handler;
1711 sigaction(SIGINT, &act, NULL);
1712 }
1713#endif
27143a44 1714 chr = qemu_chr_new("gdb", device, NULL);
36556b20
AL
1715 if (!chr)
1716 return -1;
1717
456d6069 1718 qemu_chr_fe_claim_no_fail(chr);
36556b20
AL
1719 qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
1720 gdb_chr_event, NULL);
cfc3475a
PB
1721 }
1722
36556b20
AL
1723 s = gdbserver_state;
1724 if (!s) {
7267c094 1725 s = g_malloc0(sizeof(GDBState));
36556b20 1726 gdbserver_state = s;
4046d913 1727
36556b20
AL
1728 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
1729
1730 /* Initialize a monitor terminal for gdb */
7267c094 1731 mon_chr = g_malloc0(sizeof(*mon_chr));
36556b20
AL
1732 mon_chr->chr_write = gdb_monitor_write;
1733 monitor_init(mon_chr, 0);
1734 } else {
1735 if (s->chr)
70f24fb6 1736 qemu_chr_delete(s->chr);
36556b20
AL
1737 mon_chr = s->mon_chr;
1738 memset(s, 0, sizeof(GDBState));
1739 }
2e0f2cfb
AF
1740 s->c_cpu = first_cpu;
1741 s->g_cpu = first_cpu;
4046d913 1742 s->chr = chr;
36556b20
AL
1743 s->state = chr ? RS_IDLE : RS_INACTIVE;
1744 s->mon_chr = mon_chr;
cdb432b2 1745 s->current_syscall_cb = NULL;
8a34a0fb 1746
b4608c04
FB
1747 return 0;
1748}
4046d913 1749#endif