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