s390x/sclpconsole: Fix and simplify interrupt injection
[qemu.git] / gdbstub.c
blob8afe0b701cd59f0738e2ee8ab39aa9f5239dc809
1 /*
2 * gdb server stub
4 * Copyright (c) 2003-2005 Fabrice Bellard
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.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "config.h"
20 #include "qemu-common.h"
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>
28 #include <fcntl.h>
30 #include "qemu.h"
31 #else
32 #include "monitor/monitor.h"
33 #include "sysemu/char.h"
34 #include "sysemu/sysemu.h"
35 #include "exec/gdbstub.h"
36 #endif
38 #define MAX_PACKET_LENGTH 4096
40 #include "cpu.h"
41 #include "qemu/sockets.h"
42 #include "sysemu/kvm.h"
44 static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
45 uint8_t *buf, int len, bool is_write)
47 CPUClass *cc = CPU_GET_CLASS(cpu);
49 if (cc->memory_rw_debug) {
50 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
52 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
55 enum {
56 GDB_SIGNAL_0 = 0,
57 GDB_SIGNAL_INT = 2,
58 GDB_SIGNAL_QUIT = 3,
59 GDB_SIGNAL_TRAP = 5,
60 GDB_SIGNAL_ABRT = 6,
61 GDB_SIGNAL_ALRM = 14,
62 GDB_SIGNAL_IO = 23,
63 GDB_SIGNAL_XCPU = 24,
64 GDB_SIGNAL_UNKNOWN = 143
67 #ifdef CONFIG_USER_ONLY
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.
74 static int gdb_signal_table[] = {
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,
107 #ifdef TARGET_SIGPWR
108 TARGET_SIGPWR,
109 #else
111 #endif
112 -1, /* SIGPOLL */
124 #ifdef __SIGRTMIN
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 */
231 #endif
233 #else
234 /* In system mode we only need SIGINT and SIGTRAP; other signals
235 are not yet supported. */
237 enum {
238 TARGET_SIGINT = 2,
239 TARGET_SIGTRAP = 5
242 static int gdb_signal_table[] = {
245 TARGET_SIGINT,
248 TARGET_SIGTRAP
250 #endif
252 #ifdef CONFIG_USER_ONLY
253 static int target_signal_to_gdb (int sig)
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;
261 #endif
263 static int gdb_signal_to_target (int sig)
265 if (sig < ARRAY_SIZE (gdb_signal_table))
266 return gdb_signal_table[sig];
267 else
268 return -1;
271 //#define DEBUG_GDB
273 typedef 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;
282 enum RSState {
283 RS_INACTIVE,
284 RS_IDLE,
285 RS_GETLINE,
286 RS_CHKSUM1,
287 RS_CHKSUM2,
289 typedef struct GDBState {
290 CPUState *c_cpu; /* current CPU for step/continue ops */
291 CPUState *g_cpu; /* current CPU for other ops */
292 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
293 enum RSState state; /* parsing state */
294 char line_buf[MAX_PACKET_LENGTH];
295 int line_buf_index;
296 int line_csum;
297 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
298 int last_packet_len;
299 int signal;
300 #ifdef CONFIG_USER_ONLY
301 int fd;
302 int running_state;
303 #else
304 CharDriverState *chr;
305 CharDriverState *mon_chr;
306 #endif
307 char syscall_buf[256];
308 gdb_syscall_complete_cb current_syscall_cb;
309 } GDBState;
311 /* By default use no IRQs and no timers while single stepping so as to
312 * make single stepping like an ICE HW step.
314 static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
316 static GDBState *gdbserver_state;
318 bool gdb_has_xml;
320 #ifdef CONFIG_USER_ONLY
321 /* XXX: This is not thread safe. Do we care? */
322 static int gdbserver_fd = -1;
324 static int get_char(GDBState *s)
326 uint8_t ch;
327 int ret;
329 for(;;) {
330 ret = qemu_recv(s->fd, &ch, 1, 0);
331 if (ret < 0) {
332 if (errno == ECONNRESET)
333 s->fd = -1;
334 if (errno != EINTR && errno != EAGAIN)
335 return -1;
336 } else if (ret == 0) {
337 close(s->fd);
338 s->fd = -1;
339 return -1;
340 } else {
341 break;
344 return ch;
346 #endif
348 static enum {
349 GDB_SYS_UNKNOWN,
350 GDB_SYS_ENABLED,
351 GDB_SYS_DISABLED,
352 } gdb_syscall_mode;
354 /* If gdb is connected when the first semihosting syscall occurs then use
355 remote gdb syscalls. Otherwise use native file IO. */
356 int use_gdb_syscalls(void)
358 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
359 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
360 : GDB_SYS_DISABLED);
362 return gdb_syscall_mode == GDB_SYS_ENABLED;
365 /* Resume execution. */
366 static inline void gdb_continue(GDBState *s)
368 #ifdef CONFIG_USER_ONLY
369 s->running_state = 1;
370 #else
371 if (!runstate_needs_reset()) {
372 vm_start();
374 #endif
377 static void put_buffer(GDBState *s, const uint8_t *buf, int len)
379 #ifdef CONFIG_USER_ONLY
380 int ret;
382 while (len > 0) {
383 ret = send(s->fd, buf, len, 0);
384 if (ret < 0) {
385 if (errno != EINTR && errno != EAGAIN)
386 return;
387 } else {
388 buf += ret;
389 len -= ret;
392 #else
393 qemu_chr_fe_write(s->chr, buf, len);
394 #endif
397 static inline int fromhex(int v)
399 if (v >= '0' && v <= '9')
400 return v - '0';
401 else if (v >= 'A' && v <= 'F')
402 return v - 'A' + 10;
403 else if (v >= 'a' && v <= 'f')
404 return v - 'a' + 10;
405 else
406 return 0;
409 static inline int tohex(int v)
411 if (v < 10)
412 return v + '0';
413 else
414 return v - 10 + 'a';
417 static void memtohex(char *buf, const uint8_t *mem, int len)
419 int i, c;
420 char *q;
421 q = buf;
422 for(i = 0; i < len; i++) {
423 c = mem[i];
424 *q++ = tohex(c >> 4);
425 *q++ = tohex(c & 0xf);
427 *q = '\0';
430 static void hextomem(uint8_t *mem, const char *buf, int len)
432 int i;
434 for(i = 0; i < len; i++) {
435 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
436 buf += 2;
440 /* return -1 if error, 0 if OK */
441 static int put_packet_binary(GDBState *s, const char *buf, int len)
443 int csum, i;
444 uint8_t *p;
446 for(;;) {
447 p = s->last_packet;
448 *(p++) = '$';
449 memcpy(p, buf, len);
450 p += len;
451 csum = 0;
452 for(i = 0; i < len; i++) {
453 csum += buf[i];
455 *(p++) = '#';
456 *(p++) = tohex((csum >> 4) & 0xf);
457 *(p++) = tohex((csum) & 0xf);
459 s->last_packet_len = p - s->last_packet;
460 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
462 #ifdef CONFIG_USER_ONLY
463 i = get_char(s);
464 if (i < 0)
465 return -1;
466 if (i == '+')
467 break;
468 #else
469 break;
470 #endif
472 return 0;
475 /* return -1 if error, 0 if OK */
476 static int put_packet(GDBState *s, const char *buf)
478 #ifdef DEBUG_GDB
479 printf("reply='%s'\n", buf);
480 #endif
482 return put_packet_binary(s, buf, strlen(buf));
485 /* Encode data using the encoding for 'x' packets. */
486 static int memtox(char *buf, const char *mem, int len)
488 char *p = buf;
489 char c;
491 while (len--) {
492 c = *(mem++);
493 switch (c) {
494 case '#': case '$': case '*': case '}':
495 *(p++) = '}';
496 *(p++) = c ^ 0x20;
497 break;
498 default:
499 *(p++) = c;
500 break;
503 return p - buf;
506 static const char *get_feature_xml(const char *p, const char **newp,
507 CPUClass *cc)
509 size_t len;
510 int i;
511 const char *name;
512 static char target_xml[1024];
514 len = 0;
515 while (p[len] && p[len] != ':')
516 len++;
517 *newp = p + len;
519 name = NULL;
520 if (strncmp(p, "target.xml", len) == 0) {
521 /* Generate the XML description for this CPU. */
522 if (!target_xml[0]) {
523 GDBRegisterState *r;
524 CPUState *cpu = first_cpu;
526 snprintf(target_xml, sizeof(target_xml),
527 "<?xml version=\"1.0\"?>"
528 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
529 "<target>"
530 "<xi:include href=\"%s\"/>",
531 cc->gdb_core_xml_file);
533 for (r = cpu->gdb_regs; r; r = r->next) {
534 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
535 pstrcat(target_xml, sizeof(target_xml), r->xml);
536 pstrcat(target_xml, sizeof(target_xml), "\"/>");
538 pstrcat(target_xml, sizeof(target_xml), "</target>");
540 return target_xml;
542 for (i = 0; ; i++) {
543 name = xml_builtin[i][0];
544 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
545 break;
547 return name ? xml_builtin[i][1] : NULL;
550 static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
552 CPUClass *cc = CPU_GET_CLASS(cpu);
553 CPUArchState *env = cpu->env_ptr;
554 GDBRegisterState *r;
556 if (reg < cc->gdb_num_core_regs) {
557 return cc->gdb_read_register(cpu, mem_buf, reg);
560 for (r = cpu->gdb_regs; r; r = r->next) {
561 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
562 return r->get_reg(env, mem_buf, reg - r->base_reg);
565 return 0;
568 static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
570 CPUClass *cc = CPU_GET_CLASS(cpu);
571 CPUArchState *env = cpu->env_ptr;
572 GDBRegisterState *r;
574 if (reg < cc->gdb_num_core_regs) {
575 return cc->gdb_write_register(cpu, mem_buf, reg);
578 for (r = cpu->gdb_regs; r; r = r->next) {
579 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
580 return r->set_reg(env, mem_buf, reg - r->base_reg);
583 return 0;
586 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
587 specifies the first register number and these registers are included in
588 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
589 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
592 void gdb_register_coprocessor(CPUState *cpu,
593 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
594 int num_regs, const char *xml, int g_pos)
596 GDBRegisterState *s;
597 GDBRegisterState **p;
599 p = &cpu->gdb_regs;
600 while (*p) {
601 /* Check for duplicates. */
602 if (strcmp((*p)->xml, xml) == 0)
603 return;
604 p = &(*p)->next;
607 s = g_new0(GDBRegisterState, 1);
608 s->base_reg = cpu->gdb_num_regs;
609 s->num_regs = num_regs;
610 s->get_reg = get_reg;
611 s->set_reg = set_reg;
612 s->xml = xml;
614 /* Add to end of list. */
615 cpu->gdb_num_regs += num_regs;
616 *p = s;
617 if (g_pos) {
618 if (g_pos != s->base_reg) {
619 fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
620 "Expected %d got %d\n", xml, g_pos, s->base_reg);
621 } else {
622 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
627 #ifndef CONFIG_USER_ONLY
628 static const int xlat_gdb_type[] = {
629 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
630 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
631 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
633 #endif
635 static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
637 CPUState *cpu;
638 int err = 0;
640 if (kvm_enabled()) {
641 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
644 switch (type) {
645 case GDB_BREAKPOINT_SW:
646 case GDB_BREAKPOINT_HW:
647 CPU_FOREACH(cpu) {
648 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
649 if (err) {
650 break;
653 return err;
654 #ifndef CONFIG_USER_ONLY
655 case GDB_WATCHPOINT_WRITE:
656 case GDB_WATCHPOINT_READ:
657 case GDB_WATCHPOINT_ACCESS:
658 CPU_FOREACH(cpu) {
659 err = cpu_watchpoint_insert(cpu, addr, len, xlat_gdb_type[type],
660 NULL);
661 if (err)
662 break;
664 return err;
665 #endif
666 default:
667 return -ENOSYS;
671 static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
673 CPUState *cpu;
674 int err = 0;
676 if (kvm_enabled()) {
677 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
680 switch (type) {
681 case GDB_BREAKPOINT_SW:
682 case GDB_BREAKPOINT_HW:
683 CPU_FOREACH(cpu) {
684 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
685 if (err) {
686 break;
689 return err;
690 #ifndef CONFIG_USER_ONLY
691 case GDB_WATCHPOINT_WRITE:
692 case GDB_WATCHPOINT_READ:
693 case GDB_WATCHPOINT_ACCESS:
694 CPU_FOREACH(cpu) {
695 err = cpu_watchpoint_remove(cpu, addr, len, xlat_gdb_type[type]);
696 if (err)
697 break;
699 return err;
700 #endif
701 default:
702 return -ENOSYS;
706 static void gdb_breakpoint_remove_all(void)
708 CPUState *cpu;
710 if (kvm_enabled()) {
711 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
712 return;
715 CPU_FOREACH(cpu) {
716 cpu_breakpoint_remove_all(cpu, BP_GDB);
717 #ifndef CONFIG_USER_ONLY
718 cpu_watchpoint_remove_all(cpu, BP_GDB);
719 #endif
723 static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
725 CPUState *cpu = s->c_cpu;
726 CPUClass *cc = CPU_GET_CLASS(cpu);
728 cpu_synchronize_state(cpu);
729 if (cc->set_pc) {
730 cc->set_pc(cpu, pc);
734 static CPUState *find_cpu(uint32_t thread_id)
736 CPUState *cpu;
738 CPU_FOREACH(cpu) {
739 if (cpu_index(cpu) == thread_id) {
740 return cpu;
744 return NULL;
747 static int gdb_handle_packet(GDBState *s, const char *line_buf)
749 CPUState *cpu;
750 CPUClass *cc;
751 const char *p;
752 uint32_t thread;
753 int ch, reg_size, type, res;
754 char buf[MAX_PACKET_LENGTH];
755 uint8_t mem_buf[MAX_PACKET_LENGTH];
756 uint8_t *registers;
757 target_ulong addr, len;
759 #ifdef DEBUG_GDB
760 printf("command='%s'\n", line_buf);
761 #endif
762 p = line_buf;
763 ch = *p++;
764 switch(ch) {
765 case '?':
766 /* TODO: Make this return the correct value for user-mode. */
767 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
768 cpu_index(s->c_cpu));
769 put_packet(s, buf);
770 /* Remove all the breakpoints when this query is issued,
771 * because gdb is doing and initial connect and the state
772 * should be cleaned up.
774 gdb_breakpoint_remove_all();
775 break;
776 case 'c':
777 if (*p != '\0') {
778 addr = strtoull(p, (char **)&p, 16);
779 gdb_set_cpu_pc(s, addr);
781 s->signal = 0;
782 gdb_continue(s);
783 return RS_IDLE;
784 case 'C':
785 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
786 if (s->signal == -1)
787 s->signal = 0;
788 gdb_continue(s);
789 return RS_IDLE;
790 case 'v':
791 if (strncmp(p, "Cont", 4) == 0) {
792 int res_signal, res_thread;
794 p += 4;
795 if (*p == '?') {
796 put_packet(s, "vCont;c;C;s;S");
797 break;
799 res = 0;
800 res_signal = 0;
801 res_thread = 0;
802 while (*p) {
803 int action, signal;
805 if (*p++ != ';') {
806 res = 0;
807 break;
809 action = *p++;
810 signal = 0;
811 if (action == 'C' || action == 'S') {
812 signal = strtoul(p, (char **)&p, 16);
813 } else if (action != 'c' && action != 's') {
814 res = 0;
815 break;
817 thread = 0;
818 if (*p == ':') {
819 thread = strtoull(p+1, (char **)&p, 16);
821 action = tolower(action);
822 if (res == 0 || (res == 'c' && action == 's')) {
823 res = action;
824 res_signal = signal;
825 res_thread = thread;
828 if (res) {
829 if (res_thread != -1 && res_thread != 0) {
830 cpu = find_cpu(res_thread);
831 if (cpu == NULL) {
832 put_packet(s, "E22");
833 break;
835 s->c_cpu = cpu;
837 if (res == 's') {
838 cpu_single_step(s->c_cpu, sstep_flags);
840 s->signal = res_signal;
841 gdb_continue(s);
842 return RS_IDLE;
844 break;
845 } else {
846 goto unknown_command;
848 case 'k':
849 #ifdef CONFIG_USER_ONLY
850 /* Kill the target */
851 fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
852 exit(0);
853 #endif
854 case 'D':
855 /* Detach packet */
856 gdb_breakpoint_remove_all();
857 gdb_syscall_mode = GDB_SYS_DISABLED;
858 gdb_continue(s);
859 put_packet(s, "OK");
860 break;
861 case 's':
862 if (*p != '\0') {
863 addr = strtoull(p, (char **)&p, 16);
864 gdb_set_cpu_pc(s, addr);
866 cpu_single_step(s->c_cpu, sstep_flags);
867 gdb_continue(s);
868 return RS_IDLE;
869 case 'F':
871 target_ulong ret;
872 target_ulong err;
874 ret = strtoull(p, (char **)&p, 16);
875 if (*p == ',') {
876 p++;
877 err = strtoull(p, (char **)&p, 16);
878 } else {
879 err = 0;
881 if (*p == ',')
882 p++;
883 type = *p;
884 if (s->current_syscall_cb) {
885 s->current_syscall_cb(s->c_cpu, ret, err);
886 s->current_syscall_cb = NULL;
888 if (type == 'C') {
889 put_packet(s, "T02");
890 } else {
891 gdb_continue(s);
894 break;
895 case 'g':
896 cpu_synchronize_state(s->g_cpu);
897 len = 0;
898 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
899 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
900 len += reg_size;
902 memtohex(buf, mem_buf, len);
903 put_packet(s, buf);
904 break;
905 case 'G':
906 cpu_synchronize_state(s->g_cpu);
907 registers = mem_buf;
908 len = strlen(p) / 2;
909 hextomem((uint8_t *)registers, p, len);
910 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
911 reg_size = gdb_write_register(s->g_cpu, registers, addr);
912 len -= reg_size;
913 registers += reg_size;
915 put_packet(s, "OK");
916 break;
917 case 'm':
918 addr = strtoull(p, (char **)&p, 16);
919 if (*p == ',')
920 p++;
921 len = strtoull(p, NULL, 16);
922 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
923 put_packet (s, "E14");
924 } else {
925 memtohex(buf, mem_buf, len);
926 put_packet(s, buf);
928 break;
929 case 'M':
930 addr = strtoull(p, (char **)&p, 16);
931 if (*p == ',')
932 p++;
933 len = strtoull(p, (char **)&p, 16);
934 if (*p == ':')
935 p++;
936 hextomem(mem_buf, p, len);
937 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
938 true) != 0) {
939 put_packet(s, "E14");
940 } else {
941 put_packet(s, "OK");
943 break;
944 case 'p':
945 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
946 This works, but can be very slow. Anything new enough to
947 understand XML also knows how to use this properly. */
948 if (!gdb_has_xml)
949 goto unknown_command;
950 addr = strtoull(p, (char **)&p, 16);
951 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
952 if (reg_size) {
953 memtohex(buf, mem_buf, reg_size);
954 put_packet(s, buf);
955 } else {
956 put_packet(s, "E14");
958 break;
959 case 'P':
960 if (!gdb_has_xml)
961 goto unknown_command;
962 addr = strtoull(p, (char **)&p, 16);
963 if (*p == '=')
964 p++;
965 reg_size = strlen(p) / 2;
966 hextomem(mem_buf, p, reg_size);
967 gdb_write_register(s->g_cpu, mem_buf, addr);
968 put_packet(s, "OK");
969 break;
970 case 'Z':
971 case 'z':
972 type = strtoul(p, (char **)&p, 16);
973 if (*p == ',')
974 p++;
975 addr = strtoull(p, (char **)&p, 16);
976 if (*p == ',')
977 p++;
978 len = strtoull(p, (char **)&p, 16);
979 if (ch == 'Z')
980 res = gdb_breakpoint_insert(addr, len, type);
981 else
982 res = gdb_breakpoint_remove(addr, len, type);
983 if (res >= 0)
984 put_packet(s, "OK");
985 else if (res == -ENOSYS)
986 put_packet(s, "");
987 else
988 put_packet(s, "E22");
989 break;
990 case 'H':
991 type = *p++;
992 thread = strtoull(p, (char **)&p, 16);
993 if (thread == -1 || thread == 0) {
994 put_packet(s, "OK");
995 break;
997 cpu = find_cpu(thread);
998 if (cpu == NULL) {
999 put_packet(s, "E22");
1000 break;
1002 switch (type) {
1003 case 'c':
1004 s->c_cpu = cpu;
1005 put_packet(s, "OK");
1006 break;
1007 case 'g':
1008 s->g_cpu = cpu;
1009 put_packet(s, "OK");
1010 break;
1011 default:
1012 put_packet(s, "E22");
1013 break;
1015 break;
1016 case 'T':
1017 thread = strtoull(p, (char **)&p, 16);
1018 cpu = find_cpu(thread);
1020 if (cpu != NULL) {
1021 put_packet(s, "OK");
1022 } else {
1023 put_packet(s, "E22");
1025 break;
1026 case 'q':
1027 case 'Q':
1028 /* parse any 'q' packets here */
1029 if (!strcmp(p,"qemu.sstepbits")) {
1030 /* Query Breakpoint bit definitions */
1031 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1032 SSTEP_ENABLE,
1033 SSTEP_NOIRQ,
1034 SSTEP_NOTIMER);
1035 put_packet(s, buf);
1036 break;
1037 } else if (strncmp(p,"qemu.sstep",10) == 0) {
1038 /* Display or change the sstep_flags */
1039 p += 10;
1040 if (*p != '=') {
1041 /* Display current setting */
1042 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
1043 put_packet(s, buf);
1044 break;
1046 p++;
1047 type = strtoul(p, (char **)&p, 16);
1048 sstep_flags = type;
1049 put_packet(s, "OK");
1050 break;
1051 } else if (strcmp(p,"C") == 0) {
1052 /* "Current thread" remains vague in the spec, so always return
1053 * the first CPU (gdb returns the first thread). */
1054 put_packet(s, "QC1");
1055 break;
1056 } else if (strcmp(p,"fThreadInfo") == 0) {
1057 s->query_cpu = first_cpu;
1058 goto report_cpuinfo;
1059 } else if (strcmp(p,"sThreadInfo") == 0) {
1060 report_cpuinfo:
1061 if (s->query_cpu) {
1062 snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu));
1063 put_packet(s, buf);
1064 s->query_cpu = CPU_NEXT(s->query_cpu);
1065 } else
1066 put_packet(s, "l");
1067 break;
1068 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1069 thread = strtoull(p+16, (char **)&p, 16);
1070 cpu = find_cpu(thread);
1071 if (cpu != NULL) {
1072 cpu_synchronize_state(cpu);
1073 len = snprintf((char *)mem_buf, sizeof(mem_buf),
1074 "CPU#%d [%s]", cpu->cpu_index,
1075 cpu->halted ? "halted " : "running");
1076 memtohex(buf, mem_buf, len);
1077 put_packet(s, buf);
1079 break;
1081 #ifdef CONFIG_USER_ONLY
1082 else if (strncmp(p, "Offsets", 7) == 0) {
1083 TaskState *ts = s->c_cpu->opaque;
1085 snprintf(buf, sizeof(buf),
1086 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1087 ";Bss=" TARGET_ABI_FMT_lx,
1088 ts->info->code_offset,
1089 ts->info->data_offset,
1090 ts->info->data_offset);
1091 put_packet(s, buf);
1092 break;
1094 #else /* !CONFIG_USER_ONLY */
1095 else if (strncmp(p, "Rcmd,", 5) == 0) {
1096 int len = strlen(p + 5);
1098 if ((len % 2) != 0) {
1099 put_packet(s, "E01");
1100 break;
1102 hextomem(mem_buf, p + 5, len);
1103 len = len / 2;
1104 mem_buf[len++] = 0;
1105 qemu_chr_be_write(s->mon_chr, mem_buf, len);
1106 put_packet(s, "OK");
1107 break;
1109 #endif /* !CONFIG_USER_ONLY */
1110 if (strncmp(p, "Supported", 9) == 0) {
1111 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
1112 cc = CPU_GET_CLASS(first_cpu);
1113 if (cc->gdb_core_xml_file != NULL) {
1114 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1116 put_packet(s, buf);
1117 break;
1119 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1120 const char *xml;
1121 target_ulong total_len;
1123 cc = CPU_GET_CLASS(first_cpu);
1124 if (cc->gdb_core_xml_file == NULL) {
1125 goto unknown_command;
1128 gdb_has_xml = true;
1129 p += 19;
1130 xml = get_feature_xml(p, &p, cc);
1131 if (!xml) {
1132 snprintf(buf, sizeof(buf), "E00");
1133 put_packet(s, buf);
1134 break;
1137 if (*p == ':')
1138 p++;
1139 addr = strtoul(p, (char **)&p, 16);
1140 if (*p == ',')
1141 p++;
1142 len = strtoul(p, (char **)&p, 16);
1144 total_len = strlen(xml);
1145 if (addr > total_len) {
1146 snprintf(buf, sizeof(buf), "E00");
1147 put_packet(s, buf);
1148 break;
1150 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1151 len = (MAX_PACKET_LENGTH - 5) / 2;
1152 if (len < total_len - addr) {
1153 buf[0] = 'm';
1154 len = memtox(buf + 1, xml + addr, len);
1155 } else {
1156 buf[0] = 'l';
1157 len = memtox(buf + 1, xml + addr, total_len - addr);
1159 put_packet_binary(s, buf, len + 1);
1160 break;
1162 /* Unrecognised 'q' command. */
1163 goto unknown_command;
1165 default:
1166 unknown_command:
1167 /* put empty packet */
1168 buf[0] = '\0';
1169 put_packet(s, buf);
1170 break;
1172 return RS_IDLE;
1175 void gdb_set_stop_cpu(CPUState *cpu)
1177 gdbserver_state->c_cpu = cpu;
1178 gdbserver_state->g_cpu = cpu;
1181 #ifndef CONFIG_USER_ONLY
1182 static void gdb_vm_state_change(void *opaque, int running, RunState state)
1184 GDBState *s = gdbserver_state;
1185 CPUArchState *env = s->c_cpu->env_ptr;
1186 CPUState *cpu = s->c_cpu;
1187 char buf[256];
1188 const char *type;
1189 int ret;
1191 if (running || s->state == RS_INACTIVE) {
1192 return;
1194 /* Is there a GDB syscall waiting to be sent? */
1195 if (s->current_syscall_cb) {
1196 put_packet(s, s->syscall_buf);
1197 return;
1199 switch (state) {
1200 case RUN_STATE_DEBUG:
1201 if (cpu->watchpoint_hit) {
1202 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
1203 case BP_MEM_READ:
1204 type = "r";
1205 break;
1206 case BP_MEM_ACCESS:
1207 type = "a";
1208 break;
1209 default:
1210 type = "";
1211 break;
1213 snprintf(buf, sizeof(buf),
1214 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
1215 GDB_SIGNAL_TRAP, cpu_index(cpu), type,
1216 (target_ulong)cpu->watchpoint_hit->vaddr);
1217 cpu->watchpoint_hit = NULL;
1218 goto send_packet;
1220 tb_flush(env);
1221 ret = GDB_SIGNAL_TRAP;
1222 break;
1223 case RUN_STATE_PAUSED:
1224 ret = GDB_SIGNAL_INT;
1225 break;
1226 case RUN_STATE_SHUTDOWN:
1227 ret = GDB_SIGNAL_QUIT;
1228 break;
1229 case RUN_STATE_IO_ERROR:
1230 ret = GDB_SIGNAL_IO;
1231 break;
1232 case RUN_STATE_WATCHDOG:
1233 ret = GDB_SIGNAL_ALRM;
1234 break;
1235 case RUN_STATE_INTERNAL_ERROR:
1236 ret = GDB_SIGNAL_ABRT;
1237 break;
1238 case RUN_STATE_SAVE_VM:
1239 case RUN_STATE_RESTORE_VM:
1240 return;
1241 case RUN_STATE_FINISH_MIGRATE:
1242 ret = GDB_SIGNAL_XCPU;
1243 break;
1244 default:
1245 ret = GDB_SIGNAL_UNKNOWN;
1246 break;
1248 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(cpu));
1250 send_packet:
1251 put_packet(s, buf);
1253 /* disable single step if it was enabled */
1254 cpu_single_step(cpu, 0);
1256 #endif
1258 /* Send a gdb syscall request.
1259 This accepts limited printf-style format specifiers, specifically:
1260 %x - target_ulong argument printed in hex.
1261 %lx - 64-bit argument printed in hex.
1262 %s - string pointer (target_ulong) and length (int) pair. */
1263 void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
1265 va_list va;
1266 char *p;
1267 char *p_end;
1268 target_ulong addr;
1269 uint64_t i64;
1270 GDBState *s;
1272 s = gdbserver_state;
1273 if (!s)
1274 return;
1275 s->current_syscall_cb = cb;
1276 #ifndef CONFIG_USER_ONLY
1277 vm_stop(RUN_STATE_DEBUG);
1278 #endif
1279 va_start(va, fmt);
1280 p = s->syscall_buf;
1281 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
1282 *(p++) = 'F';
1283 while (*fmt) {
1284 if (*fmt == '%') {
1285 fmt++;
1286 switch (*fmt++) {
1287 case 'x':
1288 addr = va_arg(va, target_ulong);
1289 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
1290 break;
1291 case 'l':
1292 if (*(fmt++) != 'x')
1293 goto bad_format;
1294 i64 = va_arg(va, uint64_t);
1295 p += snprintf(p, p_end - p, "%" PRIx64, i64);
1296 break;
1297 case 's':
1298 addr = va_arg(va, target_ulong);
1299 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
1300 addr, va_arg(va, int));
1301 break;
1302 default:
1303 bad_format:
1304 fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
1305 fmt - 1);
1306 break;
1308 } else {
1309 *(p++) = *(fmt++);
1312 *p = 0;
1313 va_end(va);
1314 #ifdef CONFIG_USER_ONLY
1315 put_packet(s, s->syscall_buf);
1316 gdb_handlesig(s->c_cpu, 0);
1317 #else
1318 /* In this case wait to send the syscall packet until notification that
1319 the CPU has stopped. This must be done because if the packet is sent
1320 now the reply from the syscall request could be received while the CPU
1321 is still in the running state, which can cause packets to be dropped
1322 and state transition 'T' packets to be sent while the syscall is still
1323 being processed. */
1324 cpu_exit(s->c_cpu);
1325 #endif
1328 static void gdb_read_byte(GDBState *s, int ch)
1330 int i, csum;
1331 uint8_t reply;
1333 #ifndef CONFIG_USER_ONLY
1334 if (s->last_packet_len) {
1335 /* Waiting for a response to the last packet. If we see the start
1336 of a new command then abandon the previous response. */
1337 if (ch == '-') {
1338 #ifdef DEBUG_GDB
1339 printf("Got NACK, retransmitting\n");
1340 #endif
1341 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
1343 #ifdef DEBUG_GDB
1344 else if (ch == '+')
1345 printf("Got ACK\n");
1346 else
1347 printf("Got '%c' when expecting ACK/NACK\n", ch);
1348 #endif
1349 if (ch == '+' || ch == '$')
1350 s->last_packet_len = 0;
1351 if (ch != '$')
1352 return;
1354 if (runstate_is_running()) {
1355 /* when the CPU is running, we cannot do anything except stop
1356 it when receiving a char */
1357 vm_stop(RUN_STATE_PAUSED);
1358 } else
1359 #endif
1361 switch(s->state) {
1362 case RS_IDLE:
1363 if (ch == '$') {
1364 s->line_buf_index = 0;
1365 s->state = RS_GETLINE;
1367 break;
1368 case RS_GETLINE:
1369 if (ch == '#') {
1370 s->state = RS_CHKSUM1;
1371 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1372 s->state = RS_IDLE;
1373 } else {
1374 s->line_buf[s->line_buf_index++] = ch;
1376 break;
1377 case RS_CHKSUM1:
1378 s->line_buf[s->line_buf_index] = '\0';
1379 s->line_csum = fromhex(ch) << 4;
1380 s->state = RS_CHKSUM2;
1381 break;
1382 case RS_CHKSUM2:
1383 s->line_csum |= fromhex(ch);
1384 csum = 0;
1385 for(i = 0; i < s->line_buf_index; i++) {
1386 csum += s->line_buf[i];
1388 if (s->line_csum != (csum & 0xff)) {
1389 reply = '-';
1390 put_buffer(s, &reply, 1);
1391 s->state = RS_IDLE;
1392 } else {
1393 reply = '+';
1394 put_buffer(s, &reply, 1);
1395 s->state = gdb_handle_packet(s, s->line_buf);
1397 break;
1398 default:
1399 abort();
1404 /* Tell the remote gdb that the process has exited. */
1405 void gdb_exit(CPUArchState *env, int code)
1407 GDBState *s;
1408 char buf[4];
1410 s = gdbserver_state;
1411 if (!s) {
1412 return;
1414 #ifdef CONFIG_USER_ONLY
1415 if (gdbserver_fd < 0 || s->fd < 0) {
1416 return;
1418 #endif
1420 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
1421 put_packet(s, buf);
1423 #ifndef CONFIG_USER_ONLY
1424 if (s->chr) {
1425 qemu_chr_delete(s->chr);
1427 #endif
1430 #ifdef CONFIG_USER_ONLY
1432 gdb_queuesig (void)
1434 GDBState *s;
1436 s = gdbserver_state;
1438 if (gdbserver_fd < 0 || s->fd < 0)
1439 return 0;
1440 else
1441 return 1;
1445 gdb_handlesig(CPUState *cpu, int sig)
1447 CPUArchState *env = cpu->env_ptr;
1448 GDBState *s;
1449 char buf[256];
1450 int n;
1452 s = gdbserver_state;
1453 if (gdbserver_fd < 0 || s->fd < 0) {
1454 return sig;
1457 /* disable single step if it was enabled */
1458 cpu_single_step(cpu, 0);
1459 tb_flush(env);
1461 if (sig != 0) {
1462 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
1463 put_packet(s, buf);
1465 /* put_packet() might have detected that the peer terminated the
1466 connection. */
1467 if (s->fd < 0) {
1468 return sig;
1471 sig = 0;
1472 s->state = RS_IDLE;
1473 s->running_state = 0;
1474 while (s->running_state == 0) {
1475 n = read(s->fd, buf, 256);
1476 if (n > 0) {
1477 int i;
1479 for (i = 0; i < n; i++) {
1480 gdb_read_byte(s, buf[i]);
1482 } else if (n == 0 || errno != EAGAIN) {
1483 /* XXX: Connection closed. Should probably wait for another
1484 connection before continuing. */
1485 return sig;
1488 sig = s->signal;
1489 s->signal = 0;
1490 return sig;
1493 /* Tell the remote gdb that the process has exited due to SIG. */
1494 void gdb_signalled(CPUArchState *env, int sig)
1496 GDBState *s;
1497 char buf[4];
1499 s = gdbserver_state;
1500 if (gdbserver_fd < 0 || s->fd < 0) {
1501 return;
1504 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
1505 put_packet(s, buf);
1508 static void gdb_accept(void)
1510 GDBState *s;
1511 struct sockaddr_in sockaddr;
1512 socklen_t len;
1513 int fd;
1515 for(;;) {
1516 len = sizeof(sockaddr);
1517 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1518 if (fd < 0 && errno != EINTR) {
1519 perror("accept");
1520 return;
1521 } else if (fd >= 0) {
1522 #ifndef _WIN32
1523 fcntl(fd, F_SETFD, FD_CLOEXEC);
1524 #endif
1525 break;
1529 /* set short latency */
1530 socket_set_nodelay(fd);
1532 s = g_malloc0(sizeof(GDBState));
1533 s->c_cpu = first_cpu;
1534 s->g_cpu = first_cpu;
1535 s->fd = fd;
1536 gdb_has_xml = false;
1538 gdbserver_state = s;
1540 fcntl(fd, F_SETFL, O_NONBLOCK);
1543 static int gdbserver_open(int port)
1545 struct sockaddr_in sockaddr;
1546 int fd, ret;
1548 fd = socket(PF_INET, SOCK_STREAM, 0);
1549 if (fd < 0) {
1550 perror("socket");
1551 return -1;
1553 #ifndef _WIN32
1554 fcntl(fd, F_SETFD, FD_CLOEXEC);
1555 #endif
1557 socket_set_fast_reuse(fd);
1559 sockaddr.sin_family = AF_INET;
1560 sockaddr.sin_port = htons(port);
1561 sockaddr.sin_addr.s_addr = 0;
1562 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1563 if (ret < 0) {
1564 perror("bind");
1565 close(fd);
1566 return -1;
1568 ret = listen(fd, 0);
1569 if (ret < 0) {
1570 perror("listen");
1571 close(fd);
1572 return -1;
1574 return fd;
1577 int gdbserver_start(int port)
1579 gdbserver_fd = gdbserver_open(port);
1580 if (gdbserver_fd < 0)
1581 return -1;
1582 /* accept connections */
1583 gdb_accept();
1584 return 0;
1587 /* Disable gdb stub for child processes. */
1588 void gdbserver_fork(CPUArchState *env)
1590 CPUState *cpu = ENV_GET_CPU(env);
1591 GDBState *s = gdbserver_state;
1593 if (gdbserver_fd < 0 || s->fd < 0) {
1594 return;
1596 close(s->fd);
1597 s->fd = -1;
1598 cpu_breakpoint_remove_all(cpu, BP_GDB);
1599 cpu_watchpoint_remove_all(cpu, BP_GDB);
1601 #else
1602 static int gdb_chr_can_receive(void *opaque)
1604 /* We can handle an arbitrarily large amount of data.
1605 Pick the maximum packet size, which is as good as anything. */
1606 return MAX_PACKET_LENGTH;
1609 static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
1611 int i;
1613 for (i = 0; i < size; i++) {
1614 gdb_read_byte(gdbserver_state, buf[i]);
1618 static void gdb_chr_event(void *opaque, int event)
1620 switch (event) {
1621 case CHR_EVENT_OPENED:
1622 vm_stop(RUN_STATE_PAUSED);
1623 gdb_has_xml = false;
1624 break;
1625 default:
1626 break;
1630 static void gdb_monitor_output(GDBState *s, const char *msg, int len)
1632 char buf[MAX_PACKET_LENGTH];
1634 buf[0] = 'O';
1635 if (len > (MAX_PACKET_LENGTH/2) - 1)
1636 len = (MAX_PACKET_LENGTH/2) - 1;
1637 memtohex(buf + 1, (uint8_t *)msg, len);
1638 put_packet(s, buf);
1641 static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
1643 const char *p = (const char *)buf;
1644 int max_sz;
1646 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
1647 for (;;) {
1648 if (len <= max_sz) {
1649 gdb_monitor_output(gdbserver_state, p, len);
1650 break;
1652 gdb_monitor_output(gdbserver_state, p, max_sz);
1653 p += max_sz;
1654 len -= max_sz;
1656 return len;
1659 #ifndef _WIN32
1660 static void gdb_sigterm_handler(int signal)
1662 if (runstate_is_running()) {
1663 vm_stop(RUN_STATE_PAUSED);
1666 #endif
1668 int gdbserver_start(const char *device)
1670 GDBState *s;
1671 char gdbstub_device_name[128];
1672 CharDriverState *chr = NULL;
1673 CharDriverState *mon_chr;
1675 if (!device)
1676 return -1;
1677 if (strcmp(device, "none") != 0) {
1678 if (strstart(device, "tcp:", NULL)) {
1679 /* enforce required TCP attributes */
1680 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
1681 "%s,nowait,nodelay,server", device);
1682 device = gdbstub_device_name;
1684 #ifndef _WIN32
1685 else if (strcmp(device, "stdio") == 0) {
1686 struct sigaction act;
1688 memset(&act, 0, sizeof(act));
1689 act.sa_handler = gdb_sigterm_handler;
1690 sigaction(SIGINT, &act, NULL);
1692 #endif
1693 chr = qemu_chr_new("gdb", device, NULL);
1694 if (!chr)
1695 return -1;
1697 qemu_chr_fe_claim_no_fail(chr);
1698 qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
1699 gdb_chr_event, NULL);
1702 s = gdbserver_state;
1703 if (!s) {
1704 s = g_malloc0(sizeof(GDBState));
1705 gdbserver_state = s;
1707 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
1709 /* Initialize a monitor terminal for gdb */
1710 mon_chr = g_malloc0(sizeof(*mon_chr));
1711 mon_chr->chr_write = gdb_monitor_write;
1712 monitor_init(mon_chr, 0);
1713 } else {
1714 if (s->chr)
1715 qemu_chr_delete(s->chr);
1716 mon_chr = s->mon_chr;
1717 memset(s, 0, sizeof(GDBState));
1719 s->c_cpu = first_cpu;
1720 s->g_cpu = first_cpu;
1721 s->chr = chr;
1722 s->state = chr ? RS_IDLE : RS_INACTIVE;
1723 s->mon_chr = mon_chr;
1724 s->current_syscall_cb = NULL;
1726 return 0;
1728 #endif