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