1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include "qemu/osdep.h"
20#include "qapi/error.h"
21#include "qemu/cutils.h"
22#include "cpu.h"
23#include "exec/semihost.h"
24#include "exec/exec-all.h"
25
26
27#ifdef CONFIG_USER_ONLY
28#include <stdlib.h>
29#include <stdio.h>
30#include <stdarg.h>
31#include <string.h>
32#include <errno.h>
33#include <unistd.h>
34#include <fcntl.h>
35
36#include "qemu.h"
37#else
38#include "monitor/monitor.h"
39#include "sysemu/char.h"
40#include "sysemu/sysemu.h"
41#include "exec/gdbstub.h"
42#include "hw/remote-port.h"
43#endif
44
45#define MAX_PACKET_LENGTH 4096
46
47#include "cpu.h"
48#include "qemu/sockets.h"
49#include "sysemu/kvm.h"
50
51static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
52 uint8_t *buf, int len, bool is_write)
53{
54 CPUClass *cc = CPU_GET_CLASS(cpu);
55
56 if (cc->memory_rw_debug) {
57 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
58 }
59 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
60}
61
62enum {
63 GDB_SIGNAL_0 = 0,
64 GDB_SIGNAL_INT = 2,
65 GDB_SIGNAL_QUIT = 3,
66 GDB_SIGNAL_TRAP = 5,
67 GDB_SIGNAL_ABRT = 6,
68 GDB_SIGNAL_ALRM = 14,
69 GDB_SIGNAL_IO = 23,
70 GDB_SIGNAL_XCPU = 24,
71 GDB_SIGNAL_UNKNOWN = 143
72};
73
74#ifdef CONFIG_USER_ONLY
75
76
77
78
79
80
81static int gdb_signal_table[] = {
82 0,
83 TARGET_SIGHUP,
84 TARGET_SIGINT,
85 TARGET_SIGQUIT,
86 TARGET_SIGILL,
87 TARGET_SIGTRAP,
88 TARGET_SIGABRT,
89 -1,
90 TARGET_SIGFPE,
91 TARGET_SIGKILL,
92 TARGET_SIGBUS,
93 TARGET_SIGSEGV,
94 TARGET_SIGSYS,
95 TARGET_SIGPIPE,
96 TARGET_SIGALRM,
97 TARGET_SIGTERM,
98 TARGET_SIGURG,
99 TARGET_SIGSTOP,
100 TARGET_SIGTSTP,
101 TARGET_SIGCONT,
102 TARGET_SIGCHLD,
103 TARGET_SIGTTIN,
104 TARGET_SIGTTOU,
105 TARGET_SIGIO,
106 TARGET_SIGXCPU,
107 TARGET_SIGXFSZ,
108 TARGET_SIGVTALRM,
109 TARGET_SIGPROF,
110 TARGET_SIGWINCH,
111 -1,
112 TARGET_SIGUSR1,
113 TARGET_SIGUSR2,
114#ifdef TARGET_SIGPWR
115 TARGET_SIGPWR,
116#else
117 -1,
118#endif
119 -1,
120 -1,
121 -1,
122 -1,
123 -1,
124 -1,
125 -1,
126 -1,
127 -1,
128 -1,
129 -1,
130 -1,
131#ifdef __SIGRTMIN
132 __SIGRTMIN + 1,
133 __SIGRTMIN + 2,
134 __SIGRTMIN + 3,
135 __SIGRTMIN + 4,
136 __SIGRTMIN + 5,
137 __SIGRTMIN + 6,
138 __SIGRTMIN + 7,
139 __SIGRTMIN + 8,
140 __SIGRTMIN + 9,
141 __SIGRTMIN + 10,
142 __SIGRTMIN + 11,
143 __SIGRTMIN + 12,
144 __SIGRTMIN + 13,
145 __SIGRTMIN + 14,
146 __SIGRTMIN + 15,
147 __SIGRTMIN + 16,
148 __SIGRTMIN + 17,
149 __SIGRTMIN + 18,
150 __SIGRTMIN + 19,
151 __SIGRTMIN + 20,
152 __SIGRTMIN + 21,
153 __SIGRTMIN + 22,
154 __SIGRTMIN + 23,
155 __SIGRTMIN + 24,
156 __SIGRTMIN + 25,
157 __SIGRTMIN + 26,
158 __SIGRTMIN + 27,
159 __SIGRTMIN + 28,
160 __SIGRTMIN + 29,
161 __SIGRTMIN + 30,
162 __SIGRTMIN + 31,
163 -1,
164 __SIGRTMIN,
165 __SIGRTMIN + 32,
166 __SIGRTMIN + 33,
167 __SIGRTMIN + 34,
168 __SIGRTMIN + 35,
169 __SIGRTMIN + 36,
170 __SIGRTMIN + 37,
171 __SIGRTMIN + 38,
172 __SIGRTMIN + 39,
173 __SIGRTMIN + 40,
174 __SIGRTMIN + 41,
175 __SIGRTMIN + 42,
176 __SIGRTMIN + 43,
177 __SIGRTMIN + 44,
178 __SIGRTMIN + 45,
179 __SIGRTMIN + 46,
180 __SIGRTMIN + 47,
181 __SIGRTMIN + 48,
182 __SIGRTMIN + 49,
183 __SIGRTMIN + 50,
184 __SIGRTMIN + 51,
185 __SIGRTMIN + 52,
186 __SIGRTMIN + 53,
187 __SIGRTMIN + 54,
188 __SIGRTMIN + 55,
189 __SIGRTMIN + 56,
190 __SIGRTMIN + 57,
191 __SIGRTMIN + 58,
192 __SIGRTMIN + 59,
193 __SIGRTMIN + 60,
194 __SIGRTMIN + 61,
195 __SIGRTMIN + 62,
196 __SIGRTMIN + 63,
197 __SIGRTMIN + 64,
198 __SIGRTMIN + 65,
199 __SIGRTMIN + 66,
200 __SIGRTMIN + 67,
201 __SIGRTMIN + 68,
202 __SIGRTMIN + 69,
203 __SIGRTMIN + 70,
204 __SIGRTMIN + 71,
205 __SIGRTMIN + 72,
206 __SIGRTMIN + 73,
207 __SIGRTMIN + 74,
208 __SIGRTMIN + 75,
209 __SIGRTMIN + 76,
210 __SIGRTMIN + 77,
211 __SIGRTMIN + 78,
212 __SIGRTMIN + 79,
213 __SIGRTMIN + 80,
214 __SIGRTMIN + 81,
215 __SIGRTMIN + 82,
216 __SIGRTMIN + 83,
217 __SIGRTMIN + 84,
218 __SIGRTMIN + 85,
219 __SIGRTMIN + 86,
220 __SIGRTMIN + 87,
221 __SIGRTMIN + 88,
222 __SIGRTMIN + 89,
223 __SIGRTMIN + 90,
224 __SIGRTMIN + 91,
225 __SIGRTMIN + 92,
226 __SIGRTMIN + 93,
227 __SIGRTMIN + 94,
228 __SIGRTMIN + 95,
229 -1,
230 -1,
231 -1,
232 -1,
233 -1,
234 -1,
235 -1,
236 -1,
237 -1
238#endif
239};
240#else
241
242
243
244enum {
245 TARGET_SIGINT = 2,
246 TARGET_SIGTRAP = 5
247};
248
249static int gdb_signal_table[] = {
250 -1,
251 -1,
252 TARGET_SIGINT,
253 -1,
254 -1,
255 TARGET_SIGTRAP
256};
257#endif
258
259#ifdef CONFIG_USER_ONLY
260static int target_signal_to_gdb (int sig)
261{
262 int i;
263 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
264 if (gdb_signal_table[i] == sig)
265 return i;
266 return GDB_SIGNAL_UNKNOWN;
267}
268#endif
269
270static int gdb_signal_to_target (int sig)
271{
272 if (sig < ARRAY_SIZE (gdb_signal_table))
273 return gdb_signal_table[sig];
274 else
275 return -1;
276}
277
278
279
280typedef struct GDBRegisterState {
281 int base_reg;
282 int num_regs;
283 gdb_reg_cb get_reg;
284 gdb_reg_cb set_reg;
285 const char *xml;
286 struct GDBRegisterState *next;
287} GDBRegisterState;
288
289enum RSState {
290 RS_INACTIVE,
291 RS_IDLE,
292 RS_GETLINE,
293 RS_CHKSUM1,
294 RS_CHKSUM2,
295};
296
297
298typedef struct GDBCluster {
299 struct {
300 CPUState *first;
301 CPUState *last;
302 } cpus;
303 bool attached;
304} GDBCluster;
305
306typedef struct GDBState {
307 GDBCluster *clusters;
308 int num_clusters;
309 int cur_cluster;
310
311 CPUState *c_cpu;
312 CPUState *g_cpu;
313 CPUState *query_cpu;
314 int query_cluster;
315 enum RSState state;
316 char line_buf[MAX_PACKET_LENGTH];
317 int line_buf_index;
318 int line_csum;
319 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
320 int last_packet_len;
321 int signal;
322 bool client_connected;
323 bool multiprocess;
324 char threadid_str[64];
325 bool break_on_guest_error;
326 bool breakpoints_per_core;
327#ifdef CONFIG_USER_ONLY
328 int fd;
329 int running_state;
330#else
331 CharDriverState *chr;
332 CharDriverState *mon_chr;
333#endif
334 char syscall_buf[256];
335 gdb_syscall_complete_cb current_syscall_cb;
336} GDBState;
337
338
339
340
341static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
342
343static GDBState *gdbserver_state;
344
345bool gdb_has_xml;
346
347static void gdb_output(GDBState *s, const char *msg, int len);
348int semihosting_target = SEMIHOSTING_TARGET_AUTO;
349
350#ifdef CONFIG_USER_ONLY
351
352static int gdbserver_fd = -1;
353
354static int get_char(GDBState *s)
355{
356 uint8_t ch;
357 int ret;
358
359 for(;;) {
360 ret = qemu_recv(s->fd, &ch, 1, 0);
361 if (ret < 0) {
362 if (errno == ECONNRESET)
363 s->fd = -1;
364 if (errno != EINTR && errno != EAGAIN)
365 return -1;
366 } else if (ret == 0) {
367 close(s->fd);
368 s->fd = -1;
369 return -1;
370 } else {
371 break;
372 }
373 }
374 return ch;
375}
376#endif
377
378static enum {
379 GDB_SYS_UNKNOWN,
380 GDB_SYS_ENABLED,
381 GDB_SYS_DISABLED,
382} gdb_syscall_mode;
383
384
385int use_gdb_syscalls(void)
386{
387 if (semihosting_target == SEMIHOSTING_TARGET_NATIVE) {
388
389 return false;
390 } else if (semihosting_target == SEMIHOSTING_TARGET_GDB) {
391
392 return true;
393 }
394
395
396
397 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
398 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
399 : GDB_SYS_DISABLED);
400 }
401 return gdb_syscall_mode == GDB_SYS_ENABLED;
402}
403
404
405static inline void gdb_continue(GDBState *s)
406{
407#ifdef CONFIG_USER_ONLY
408 s->running_state = 1;
409#else
410 if (!runstate_needs_reset()) {
411 vm_start();
412 }
413#endif
414}
415
416static void put_buffer(GDBState *s, const uint8_t *buf, int len)
417{
418#ifdef CONFIG_USER_ONLY
419 int ret;
420
421 while (len > 0) {
422 ret = send(s->fd, buf, len, 0);
423 if (ret < 0) {
424 if (errno != EINTR && errno != EAGAIN)
425 return;
426 } else {
427 buf += ret;
428 len -= ret;
429 }
430 }
431#else
432 qemu_chr_fe_write(s->chr, buf, len);
433#endif
434}
435
436static inline int fromhex(int v)
437{
438 if (v >= '0' && v <= '9')
439 return v - '0';
440 else if (v >= 'A' && v <= 'F')
441 return v - 'A' + 10;
442 else if (v >= 'a' && v <= 'f')
443 return v - 'a' + 10;
444 else
445 return 0;
446}
447
448static inline int tohex(int v)
449{
450 if (v < 10)
451 return v + '0';
452 else
453 return v - 10 + 'a';
454}
455
456static void memtohex(char *buf, const uint8_t *mem, int len)
457{
458 int i, c;
459 char *q;
460 q = buf;
461 for(i = 0; i < len; i++) {
462 c = mem[i];
463 *q++ = tohex(c >> 4);
464 *q++ = tohex(c & 0xf);
465 }
466 *q = '\0';
467}
468
469static void hextomem(uint8_t *mem, const char *buf, int len)
470{
471 int i;
472
473 for(i = 0; i < len; i++) {
474 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
475 buf += 2;
476 }
477}
478
479
480static int put_packet_binary(GDBState *s, const char *buf, int len)
481{
482 int csum, i;
483 uint8_t *p;
484
485 for(;;) {
486 p = s->last_packet;
487 *(p++) = '$';
488 memcpy(p, buf, len);
489 p += len;
490 csum = 0;
491 for(i = 0; i < len; i++) {
492 csum += buf[i];
493 }
494 *(p++) = '#';
495 *(p++) = tohex((csum >> 4) & 0xf);
496 *(p++) = tohex((csum) & 0xf);
497
498 s->last_packet_len = p - s->last_packet;
499 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
500
501#ifdef CONFIG_USER_ONLY
502 i = get_char(s);
503 if (i < 0)
504 return -1;
505 if (i == '+')
506 break;
507#else
508 break;
509#endif
510 }
511 return 0;
512}
513
514
515static int put_packet(GDBState *s, const char *buf)
516{
517#ifdef DEBUG_GDB
518 printf("reply='%s'\n", buf);
519#endif
520
521 return put_packet_binary(s, buf, strlen(buf));
522}
523
524
525static int memtox(char *buf, const char *mem, int len)
526{
527 char *p = buf;
528 char c;
529
530 while (len--) {
531 c = *(mem++);
532 switch (c) {
533 case '#': case '$': case '*': case '}':
534 *(p++) = '}';
535 *(p++) = c ^ 0x20;
536 break;
537 default:
538 *(p++) = c;
539 break;
540 }
541 }
542 return p - buf;
543}
544
545static const char *get_feature_xml(const char *p, const char **newp,
546 CPUClass *cc, CPUState *cpu)
547{
548 size_t len;
549 int i;
550 const char *name;
551 static char target_xml[1024];
552
553 len = 0;
554 while (p[len] && p[len] != ':')
555 len++;
556 *newp = p + len;
557
558 name = NULL;
559 if (strncmp(p, "target.xml", len) == 0) {
560
561 if (1 || !target_xml[0]) {
562 GDBRegisterState *r;
563
564 snprintf(target_xml, sizeof(target_xml),
565 "<?xml version=\"1.0\"?>"
566 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
567 "<target>"
568 "<xi:include href=\"%s\"/>",
569 cc->gdb_core_xml_file);
570
571 for (r = cpu->gdb_regs; r; r = r->next) {
572 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
573 pstrcat(target_xml, sizeof(target_xml), r->xml);
574 pstrcat(target_xml, sizeof(target_xml), "\"/>");
575 }
576 if (cc->gdb_arch_name) {
577 gchar *arch = cc->gdb_arch_name(cpu);
578 pstrcat(target_xml, sizeof(target_xml), "<architecture>");
579 pstrcat(target_xml, sizeof(target_xml), arch);
580 pstrcat(target_xml, sizeof(target_xml), "</architecture>");
581 g_free(arch);
582 }
583 pstrcat(target_xml, sizeof(target_xml), "</target>");
584 }
585 return target_xml;
586 }
587 for (i = 0; ; i++) {
588 name = xml_builtin[i][0];
589 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
590 break;
591 }
592 return name ? xml_builtin[i][1] : NULL;
593}
594
595static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
596{
597 CPUClass *cc = CPU_GET_CLASS(cpu);
598 CPUArchState *env = cpu->env_ptr;
599 GDBRegisterState *r;
600
601 if (reg < cc->gdb_num_core_regs) {
602 return cc->gdb_read_register(cpu, mem_buf, reg);
603 }
604
605 for (r = cpu->gdb_regs; r; r = r->next) {
606 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
607 return r->get_reg(env, mem_buf, reg - r->base_reg);
608 }
609 }
610 return 0;
611}
612
613static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
614{
615 CPUClass *cc = CPU_GET_CLASS(cpu);
616 CPUArchState *env = cpu->env_ptr;
617 GDBRegisterState *r;
618
619 if (reg < cc->gdb_num_core_regs) {
620 return cc->gdb_write_register(cpu, mem_buf, reg);
621 }
622
623 for (r = cpu->gdb_regs; r; r = r->next) {
624 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
625 return r->set_reg(env, mem_buf, reg - r->base_reg);
626 }
627 }
628 return 0;
629}
630
631
632
633
634
635
636
637void gdb_register_coprocessor(CPUState *cpu,
638 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
639 int num_regs, const char *xml, int g_pos)
640{
641 GDBRegisterState *s;
642 GDBRegisterState **p;
643
644 p = &cpu->gdb_regs;
645 while (*p) {
646
647 if (strcmp((*p)->xml, xml) == 0)
648 return;
649 p = &(*p)->next;
650 }
651
652 s = g_new0(GDBRegisterState, 1);
653 s->base_reg = cpu->gdb_num_regs;
654 s->num_regs = num_regs;
655 s->get_reg = get_reg;
656 s->set_reg = set_reg;
657 s->xml = xml;
658
659
660 cpu->gdb_num_regs += num_regs;
661 *p = s;
662 if (g_pos) {
663 if (g_pos != s->base_reg) {
664 fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
665 "Expected %d got %d\n", xml, g_pos, s->base_reg);
666 } else {
667 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
668 }
669 }
670}
671
672#ifndef CONFIG_USER_ONLY
673
674static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
675{
676 static const int xlat[] = {
677 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
678 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
679 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
680 };
681
682 CPUClass *cc = CPU_GET_CLASS(cpu);
683 int cputype = xlat[gdbtype];
684
685 if (cc->gdb_stop_before_watchpoint) {
686 cputype |= BP_STOP_BEFORE_ACCESS;
687 }
688 return cputype;
689}
690#endif
691
692static int gdb_breakpoint_insert(GDBState *s,
693 target_ulong addr, target_ulong len, int type)
694{
695 CPUState *cpu;
696 int err = 0;
697
698 if (kvm_enabled()) {
699 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
700 }
701
702 switch (type) {
703 case GDB_BREAKPOINT_SW:
704 case GDB_BREAKPOINT_HW:
705 if (s->breakpoints_per_core) {
706 cpu_breakpoint_insert(s->c_cpu, addr, BP_GDB, NULL);
707 return 0;
708 }
709 CPU_FOREACH(cpu) {
710 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
711 if (err) {
712 break;
713 }
714 }
715 return err;
716#ifndef CONFIG_USER_ONLY
717 case GDB_WATCHPOINT_WRITE:
718 case GDB_WATCHPOINT_READ:
719 case GDB_WATCHPOINT_ACCESS:
720 CPU_FOREACH(cpu) {
721 err = cpu_watchpoint_insert(cpu, addr, len,
722 xlat_gdb_type(cpu, type), NULL);
723 if (err) {
724 break;
725 }
726 }
727 return err;
728#endif
729 default:
730 return -ENOSYS;
731 }
732}
733
734static int gdb_breakpoint_remove(GDBState *s,
735 target_ulong addr, target_ulong len, int type)
736{
737 CPUState *cpu;
738 int err = 0;
739
740 if (kvm_enabled()) {
741 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
742 }
743
744 switch (type) {
745 case GDB_BREAKPOINT_SW:
746 case GDB_BREAKPOINT_HW:
747 if (s->breakpoints_per_core) {
748 err = cpu_breakpoint_remove(s->c_cpu, addr, BP_GDB);
749 return err;
750 }
751 CPU_FOREACH(cpu) {
752 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
753 if (err) {
754 break;
755 }
756 }
757 return err;
758#ifndef CONFIG_USER_ONLY
759 case GDB_WATCHPOINT_WRITE:
760 case GDB_WATCHPOINT_READ:
761 case GDB_WATCHPOINT_ACCESS:
762 CPU_FOREACH(cpu) {
763 err = cpu_watchpoint_remove(cpu, addr, len,
764 xlat_gdb_type(cpu, type));
765 if (err)
766 break;
767 }
768 return err;
769#endif
770 default:
771 return -ENOSYS;
772 }
773}
774
775static void gdb_breakpoint_remove_all(void)
776{
777 CPUState *cpu;
778
779 if (kvm_enabled()) {
780 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
781 return;
782 }
783
784 CPU_FOREACH(cpu) {
785 cpu_breakpoint_remove_all(cpu, BP_GDB);
786#ifndef CONFIG_USER_ONLY
787 cpu_watchpoint_remove_all(cpu, BP_GDB);
788#endif
789 }
790}
791
792static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
793{
794 CPUState *cpu = s->c_cpu;
795 CPUClass *cc = CPU_GET_CLASS(cpu);
796
797 cpu_synchronize_state(cpu);
798 if (cc->set_pc) {
799 cc->set_pc(cpu, pc);
800 }
801}
802
803static CPUState *find_cpu(GDBState *s, int32_t pid, int32_t thread_id)
804{
805 GDBCluster *cl;
806 CPUState *cpu;
807
808 if (pid <= 0) {
809 pid = 1;
810 }
811
812 cl = &s->clusters[pid - 1];
813 cpu = cl->cpus.first;
814
815 while (cpu) {
816 if (cpu_index(cpu) == thread_id || thread_id <= 0) {
817 return cpu;
818 }
819
820 if (cpu == cl->cpus.last) {
821 break;
822 }
823 cpu = CPU_NEXT(cpu);
824 }
825
826 return NULL;
827}
828
829static void gdb_monitor(GDBState *s, const char *line)
830{
831 unsigned long val;
832 const char *p = line;
833
834 fprintf(stderr, "%s: %s\n", __func__, line);
835 if (strncmp(p,"break_on_guest_error",20) == 0) {
836 p += 20;
837 fprintf(stderr, "p %s\n", p);
838 if (*p == '=') {
839 p++;
840 val = strtoul(p, (char **)&p, 16);
841 s->break_on_guest_error = !!val;
842 }
843 gdb_output(s, s->break_on_guest_error ? "1\n" : "0\n", 2);
844 put_packet(s, "OK");
845 }
846}
847
848#define MAX_PLIST 8 * 1024
849static char *gdb_get_process_list(GDBState *s)
850{
851 CPUState *cpu;
852 char *buf;
853 unsigned int i;
854 int len;
855
856 buf = g_malloc0(MAX_PLIST);
857
858#undef HEADER
859#define HEADER "<?xml version=\"1.0\"?>\n" \
860 "<!DOCTYPE target SYSTEM \"osdata.dtd\">\n<osdata type=\"processes\">\n"
861
862 pstrcat(buf, MAX_PLIST, HEADER);
863 for (i = 0; i < s->num_clusters; i++) {
864 char lbuf[64];
865 unsigned int num_cores = 0;
866
867 len = snprintf(lbuf, sizeof(lbuf),
868 "<item>\n <column name=\"pid\">%u</column>\n <column name=\"cores\">",
869 i + 1);
870 pstrcat(buf, MAX_PLIST, lbuf);
871
872 cpu = s->clusters[i].cpus.first;
873 while (cpu) {
874 len = snprintf(lbuf, sizeof(lbuf), "%s%u",
875 num_cores ? "," : "", cpu_index(cpu));
876 pstrcat(buf, MAX_PLIST, lbuf);
877 if (cpu == s->clusters[i].cpus.last) {
878 break;
879 }
880 cpu = CPU_NEXT(cpu);
881 num_cores++;
882 }
883 pstrcat(buf, MAX_PLIST, "</column>\n</item>\n");
884 }
885 len = len;
886 pstrcat(buf, MAX_PLIST, "</osdata>");
887 return buf;
888}
889
890static void gdb_thread_id(const char *p, const char **next_p,
891 int32_t *pid_p, int32_t *tid_p)
892{
893
894
895
896 uint32_t pid = 1, tid;
897 bool extended = *p == 'p';
898
899 if (extended) {
900 p++;
901 pid = strtoull(p, (char **)&p, 16);
902 p++;
903 }
904 tid = strtoull(p, (char **)&p, 16);
905
906 if (pid <= 0) {
907 pid = 1;
908 }
909
910 if (pid_p) {
911 *pid_p = pid;
912 }
913 if (tid_p) {
914 *tid_p = tid;
915 }
916 if (next_p) {
917 *next_p = p;
918 }
919}
920
921static const char *gdb_gen_thread_id(GDBState *s, uint32_t pid, uint32_t tid)
922{
923 static char id[64];
924 unsigned int pos = 0;
925
926 if (s->multiprocess) {
927 pos += snprintf(id, sizeof(id), "p%x.", pid);
928 }
929 snprintf(id + pos, sizeof(id) - pos, "%x", tid);
930 return id;
931}
932
933static void gdb_match_supported(GDBState *s, const char *p)
934{
935 p = strchr(p, ':');
936 while (p) {
937 p++;
938 if (strncmp(p, "multiprocess", 12) == 0) {
939 s->multiprocess = true;
940 }
941 p = strchr(p, ':');
942 }
943}
944
945static int gdb_handle_packet(GDBState *s, const char *line_buf)
946{
947 GDBCluster *cl = &s->clusters[s->cur_cluster];
948 CPUState *cpu;
949 CPUClass *cc;
950 const char *p;
951 int32_t cluster, thread;
952 int ch, reg_size, type, res;
953 char buf[MAX_PACKET_LENGTH];
954 uint8_t mem_buf[MAX_PACKET_LENGTH];
955 uint8_t *registers;
956 target_ulong addr, len;
957
958#ifdef DEBUG_GDB
959 printf("command='%s'\n", line_buf);
960#endif
961 p = line_buf;
962 ch = *p++;
963 switch(ch) {
964 case '!':
965 put_packet(s, "OK");
966 break;
967 case '?':
968 s->cur_cluster = 0;
969 cl = &s->clusters[s->cur_cluster];
970 s->c_cpu = cl->cpus.first;
971 s->g_cpu = cl->cpus.first;
972
973 snprintf(buf, sizeof(buf), "T%02xthread:%s;", GDB_SIGNAL_TRAP,
974 gdb_gen_thread_id(s, s->cur_cluster + 1, cpu_index(s->c_cpu)));
975 put_packet(s, buf);
976
977
978
979
980 gdb_breakpoint_remove_all();
981 break;
982 case 'c':
983 if (*p != '\0') {
984 addr = strtoull(p, (char **)&p, 16);
985 gdb_set_cpu_pc(s, addr);
986 }
987 s->signal = 0;
988 gdb_continue(s);
989 return RS_IDLE;
990 case 'C':
991 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
992 if (s->signal == -1)
993 s->signal = 0;
994 gdb_continue(s);
995 return RS_IDLE;
996 case 'v':
997 if (strncmp(p, "Cont", 4) == 0) {
998 int res_signal, res_thread;
999
1000 p += 4;
1001 if (*p == '?') {
1002 put_packet(s, "vCont;c;C;s;S");
1003 break;
1004 }
1005 res = 0;
1006 res_signal = 0;
1007 res_thread = 0;
1008 while (*p) {
1009 int action, signal;
1010
1011 if (*p++ != ';') {
1012 res = 0;
1013 break;
1014 }
1015 action = *p++;
1016 signal = 0;
1017 if (action == 'C' || action == 'S') {
1018 signal = gdb_signal_to_target(strtoul(p, (char **)&p, 16));
1019 if (signal == -1) {
1020 signal = 0;
1021 }
1022 } else if (action != 'c' && action != 's') {
1023 res = 0;
1024 break;
1025 }
1026 thread = 0;
1027 if (*p == ':') {
1028 gdb_thread_id(p + 1, &p, &cluster, &thread);
1029 }
1030 action = tolower(action);
1031 if (res == 0 || (res == 'c' && action == 's')) {
1032 res = action;
1033 res_signal = signal;
1034 res_thread = thread;
1035 }
1036 }
1037 if (res) {
1038 if (res_thread != -1 && res_thread != 0) {
1039 cpu = find_cpu(s, cluster, res_thread);
1040 if (cpu == NULL) {
1041 put_packet(s, "E22");
1042 break;
1043 }
1044 s->cur_cluster = cluster - 1;
1045 cl = &s->clusters[s->cur_cluster];
1046 s->c_cpu = cpu;
1047 }
1048 if (res == 's') {
1049 cpu_single_step(s->c_cpu, sstep_flags);
1050 }
1051 s->signal = res_signal;
1052 gdb_continue(s);
1053 return RS_IDLE;
1054 }
1055 break;
1056 } else if (strncmp(p, "Attach", 6) == 0) {
1057 cluster = s->num_clusters + 1;
1058 p += 6;
1059 if (*p == ';') {
1060 p++;
1061 cluster = strtoull(p, (char **)&p, 16);
1062 }
1063 if (cluster <= s->num_clusters) {
1064 s->cur_cluster = cluster - 1;
1065 cl = &s->clusters[s->cur_cluster];
1066 cl->attached = true;
1067 s->c_cpu = cl->cpus.first;
1068 s->g_cpu = cl->cpus.first;
1069 snprintf(buf, sizeof(buf), "T%02xthread:%s;",
1070 GDB_SIGNAL_TRAP,
1071 gdb_gen_thread_id(s, cluster, cpu_index(s->c_cpu)));
1072
1073 put_packet(s, buf);
1074 } else {
1075 put_packet(s, "E22");
1076 }
1077 break;
1078 } else {
1079 goto unknown_command;
1080 }
1081 case 'k':
1082#ifdef CONFIG_USER_ONLY
1083
1084 fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
1085 exit(0);
1086#endif
1087 case 'D':
1088
1089 if (*p == ';') {
1090 cluster = strtoull(p + 1, (char **)&p, 16);
1091 s->clusters[cluster - 1].attached = false;
1092
1093 put_packet(s, "OK");
1094 break;
1095 }
1096 gdb_breakpoint_remove_all();
1097 gdb_syscall_mode = GDB_SYS_DISABLED;
1098 gdb_continue(s);
1099 put_packet(s, "OK");
1100 break;
1101 case 's':
1102 if (*p != '\0') {
1103 addr = strtoull(p, (char **)&p, 16);
1104 gdb_set_cpu_pc(s, addr);
1105 }
1106 cpu_single_step(s->c_cpu, sstep_flags);
1107 gdb_continue(s);
1108 return RS_IDLE;
1109 case 'F':
1110 {
1111 target_ulong ret;
1112 target_ulong err;
1113
1114 ret = strtoull(p, (char **)&p, 16);
1115 if (*p == ',') {
1116 p++;
1117 err = strtoull(p, (char **)&p, 16);
1118 } else {
1119 err = 0;
1120 }
1121 if (*p == ',')
1122 p++;
1123 type = *p;
1124 if (s->current_syscall_cb) {
1125 s->current_syscall_cb(s->c_cpu, ret, err);
1126 s->current_syscall_cb = NULL;
1127 }
1128 if (type == 'C') {
1129 put_packet(s, "T02");
1130 } else {
1131 gdb_continue(s);
1132 }
1133 }
1134 break;
1135 case 'g':
1136 cpu_synchronize_state(s->g_cpu);
1137 len = 0;
1138 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
1139 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
1140 len += reg_size;
1141 }
1142 memtohex(buf, mem_buf, len);
1143 put_packet(s, buf);
1144 break;
1145 case 'G':
1146 cpu_synchronize_state(s->g_cpu);
1147 registers = mem_buf;
1148 len = strlen(p) / 2;
1149 hextomem((uint8_t *)registers, p, len);
1150 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
1151 reg_size = gdb_write_register(s->g_cpu, registers, addr);
1152 len -= reg_size;
1153 registers += reg_size;
1154 }
1155 put_packet(s, "OK");
1156 break;
1157 case 'm':
1158 addr = strtoull(p, (char **)&p, 16);
1159 if (*p == ',')
1160 p++;
1161 len = strtoull(p, NULL, 16);
1162 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
1163 put_packet (s, "E14");
1164 } else {
1165 memtohex(buf, mem_buf, len);
1166 put_packet(s, buf);
1167 }
1168 break;
1169 case 'M':
1170 addr = strtoull(p, (char **)&p, 16);
1171 if (*p == ',')
1172 p++;
1173 len = strtoull(p, (char **)&p, 16);
1174 if (*p == ':')
1175 p++;
1176 hextomem(mem_buf, p, len);
1177 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
1178 true) != 0) {
1179 put_packet(s, "E14");
1180 } else {
1181 put_packet(s, "OK");
1182 }
1183 break;
1184 case 'p':
1185
1186
1187
1188 if (!gdb_has_xml)
1189 goto unknown_command;
1190 addr = strtoull(p, (char **)&p, 16);
1191 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
1192 if (reg_size) {
1193 memtohex(buf, mem_buf, reg_size);
1194 put_packet(s, buf);
1195 } else {
1196 put_packet(s, "E14");
1197 }
1198 break;
1199 case 'P':
1200 if (!gdb_has_xml)
1201 goto unknown_command;
1202 addr = strtoull(p, (char **)&p, 16);
1203 if (*p == '=')
1204 p++;
1205 reg_size = strlen(p) / 2;
1206 hextomem(mem_buf, p, reg_size);
1207 gdb_write_register(s->g_cpu, mem_buf, addr);
1208 put_packet(s, "OK");
1209 break;
1210 case 'Z':
1211 case 'z':
1212 type = strtoul(p, (char **)&p, 16);
1213 if (*p == ',')
1214 p++;
1215 addr = strtoull(p, (char **)&p, 16);
1216 if (*p == ',')
1217 p++;
1218 len = strtoull(p, (char **)&p, 16);
1219 if (ch == 'Z')
1220 res = gdb_breakpoint_insert(s, addr, len, type);
1221 else
1222 res = gdb_breakpoint_remove(s, addr, len, type);
1223 if (res >= 0)
1224 put_packet(s, "OK");
1225 else if (res == -ENOSYS)
1226 put_packet(s, "");
1227 else
1228 put_packet(s, "E22");
1229 break;
1230 case 'H':
1231 type = *p++;
1232 gdb_thread_id(p, &p, &cluster, &thread);
1233 cpu = find_cpu(s, cluster, thread);
1234 if (cpu == NULL) {
1235 put_packet(s, "E22");
1236 break;
1237 }
1238 switch (type) {
1239 case 'c':
1240 s->cur_cluster = cluster - 1;
1241 cl = &s->clusters[s->cur_cluster];
1242 s->c_cpu = cpu;
1243 put_packet(s, "OK");
1244 break;
1245 case 'g':
1246 s->g_cpu = cpu;
1247 put_packet(s, "OK");
1248 break;
1249 default:
1250 put_packet(s, "E22");
1251 break;
1252 }
1253 break;
1254 case 'T':
1255 gdb_thread_id(p, &p, &cluster, &thread);
1256 cpu = find_cpu(s, cluster, thread);
1257
1258 if (cpu != NULL) {
1259 put_packet(s, "OK");
1260 } else {
1261 put_packet(s, "E22");
1262 }
1263 break;
1264 case 'q':
1265 case 'Q':
1266
1267 if (!strcmp(p,"qemu.sstepbits")) {
1268
1269 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1270 SSTEP_ENABLE,
1271 SSTEP_NOIRQ,
1272 SSTEP_NOTIMER);
1273 put_packet(s, buf);
1274 break;
1275 } else if (strncmp(p,"qemu.sstep",10) == 0) {
1276
1277 p += 10;
1278 if (*p != '=') {
1279
1280 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
1281 put_packet(s, buf);
1282 break;
1283 }
1284 p++;
1285 type = strtoul(p, (char **)&p, 16);
1286 sstep_flags = type;
1287 put_packet(s, "OK");
1288 break;
1289 } else if (strncmp(p,"qemu.bps-per-core",17) == 0) {
1290 p += 17;
1291 if (*p != '=') {
1292
1293 snprintf(buf, sizeof(buf), "%d", s->breakpoints_per_core);
1294 put_packet(s, buf);
1295 break;
1296 }
1297 p++;
1298 s->breakpoints_per_core = strtoul(p, (char **)&p, 0);
1299 put_packet(s, "OK");
1300 break;
1301 } else if (strncmp(p,"qemu.debug-context",18) == 0) {
1302 unsigned int i = 0, len = 0, found = 0;
1303 cc = CPU_GET_CLASS(s->g_cpu);
1304
1305
1306 p += 18;
1307 if (*p != '=') {
1308 *buf = '\0';
1309 while (cc->debug_contexts && cc->debug_contexts[i]) {
1310 len += snprintf(buf + len, sizeof(buf) - len,
1311 "%s%s", i == 0 ? "" : ",",
1312 cc->debug_contexts[i]);
1313 i++;
1314 }
1315 put_packet(s, buf);
1316 break;
1317 }
1318 p++;
1319 if (cc->set_debug_context) {
1320 while (cc->debug_contexts && cc->debug_contexts[i]) {
1321 if (strcmp(p, cc->debug_contexts[i]) == 0) {
1322 cc->set_debug_context(s->g_cpu, i);
1323 put_packet(s, "OK");
1324 found = 1;
1325 break;
1326 }
1327 i++;
1328 }
1329 }
1330 if (!found) {
1331 put_packet(s, "E22");
1332 }
1333 break;
1334 } else if (strcmp(p,"C") == 0) {
1335
1336
1337 snprintf(buf, sizeof(buf), "C%s",
1338 gdb_gen_thread_id(s, s->cur_cluster + 1, cpu_index(cl->cpus.first)));
1339 put_packet(s, buf);
1340 break;
1341 } else if (strcmp(p,"fThreadInfo") == 0) {
1342 s->query_cluster = 0;
1343 s->query_cpu = s->clusters[s->query_cluster].cpus.first;
1344 goto report_cpuinfo;
1345 } else if (strcmp(p,"sThreadInfo") == 0) {
1346 report_cpuinfo:
1347 if (s->query_cpu) {
1348 snprintf(buf, sizeof(buf), "m%s",
1349 gdb_gen_thread_id(s, s->query_cluster + 1, cpu_index(s->query_cpu)));
1350 put_packet(s, buf);
1351 if (s->query_cpu == s->clusters[s->query_cluster].cpus.last) {
1352 s->query_cluster++;
1353 if (s->query_cluster == s->num_clusters) {
1354 s->query_cluster = 0;
1355 }
1356 s->query_cpu = NULL;
1357 if (s->clusters[s->query_cluster].attached) {
1358 s->query_cpu = s->clusters[s->query_cluster].cpus.first;
1359 }
1360 } else {
1361 s->query_cpu = CPU_NEXT(s->query_cpu);
1362 }
1363 } else
1364 put_packet(s, "l");
1365 break;
1366 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1367 gdb_thread_id(p + 16, &p, &cluster, &thread);
1368 cpu = find_cpu(s, cluster, thread);
1369 if (cpu != NULL) {
1370 cpu_synchronize_state(cpu);
1371 if (!cpu->gdb_id) {
1372 const char *name = object_get_canonical_path(OBJECT(cpu));
1373
1374 len = snprintf((char *)mem_buf, sizeof(mem_buf),
1375 "CPU#%d %s", cpu->cpu_index, name);
1376 } else {
1377 len = snprintf((char *)mem_buf, sizeof(mem_buf),
1378 "%s", cpu->gdb_id);
1379 }
1380 len += snprintf((char *)mem_buf + len, sizeof(mem_buf) - len,
1381 " [%s]", cpu->halted ? "halted " : "running");
1382 memtohex(buf, mem_buf, len);
1383 put_packet(s, buf);
1384 } else {
1385 put_packet(s, "E22");
1386 }
1387 break;
1388 }
1389#ifdef CONFIG_USER_ONLY
1390 else if (strncmp(p, "Offsets", 7) == 0) {
1391 TaskState *ts = s->c_cpu->opaque;
1392
1393 snprintf(buf, sizeof(buf),
1394 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1395 ";Bss=" TARGET_ABI_FMT_lx,
1396 ts->info->code_offset,
1397 ts->info->data_offset,
1398 ts->info->data_offset);
1399 put_packet(s, buf);
1400 break;
1401 }
1402#endif
1403 else if (strncmp(p, "Rcmd,", 5) == 0) {
1404 int len = strlen(p + 5);
1405
1406 if ((len % 2) != 0) {
1407 put_packet(s, "E01");
1408 break;
1409 }
1410 hextomem(mem_buf, p + 5, len);
1411 len = len / 2;
1412 mem_buf[len++] = 0;
1413 if (strncmp((char *) mem_buf, "gdbmon.", 7) == 0) {
1414
1415 p = (char *) &mem_buf[7];
1416 gdb_monitor(s, p);
1417 break;
1418 }
1419#ifndef CONFIG_USER_ONLY
1420 qemu_chr_be_write(s->mon_chr, mem_buf, len);
1421 put_packet(s, "OK");
1422#endif
1423 break;
1424 }
1425 if (strncmp(p, "Attached", 8) == 0) {
1426 put_packet(s, "1");
1427 break;
1428 }
1429 if (strncmp(p, "Supported", 9) == 0) {
1430 gdb_match_supported(s, p + 9);
1431
1432 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
1433 cc = CPU_GET_CLASS(cl->cpus.first);
1434 if (cc->gdb_core_xml_file != NULL) {
1435 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1436 }
1437 pstrcat(buf, sizeof(buf), ";qXfer:osdata:read+");
1438 pstrcat(buf, sizeof(buf), ";multiprocess+");
1439 put_packet(s, buf);
1440 break;
1441 }
1442 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1443 const char *xml;
1444 target_ulong total_len;
1445
1446 cc = CPU_GET_CLASS(s->g_cpu);
1447 if (cc->gdb_core_xml_file == NULL) {
1448 goto unknown_command;
1449 }
1450
1451 gdb_has_xml = true;
1452 p += 19;
1453 xml = get_feature_xml(p, &p, cc, s->g_cpu);
1454 if (!xml) {
1455 snprintf(buf, sizeof(buf), "E00");
1456 put_packet(s, buf);
1457 break;
1458 }
1459
1460 if (*p == ':')
1461 p++;
1462 addr = strtoul(p, (char **)&p, 16);
1463 if (*p == ',')
1464 p++;
1465 len = strtoul(p, (char **)&p, 16);
1466
1467 total_len = strlen(xml);
1468 if (addr > total_len) {
1469 snprintf(buf, sizeof(buf), "E00");
1470 put_packet(s, buf);
1471 break;
1472 }
1473 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1474 len = (MAX_PACKET_LENGTH - 5) / 2;
1475 if (len < total_len - addr) {
1476 buf[0] = 'm';
1477 len = memtox(buf + 1, xml + addr, len);
1478 } else {
1479 buf[0] = 'l';
1480 len = memtox(buf + 1, xml + addr, total_len - addr);
1481 }
1482 put_packet_binary(s, buf, len + 1);
1483 break;
1484 }
1485
1486 if (strncmp(p, "Xfer:osdata:read:processes:", 27) == 0) {
1487 const char *plist;
1488 target_ulong total_len;
1489
1490 plist = gdb_get_process_list(s);
1491
1492 p += 27;
1493 if (*p == ':')
1494 p++;
1495 addr = strtoul(p, (char **)&p, 16);
1496 if (*p == ',')
1497 p++;
1498 len = strtoul(p, (char **)&p, 16);
1499
1500 total_len = strlen(plist);
1501 if (addr > total_len) {
1502 snprintf(buf, sizeof(buf), "E00");
1503 put_packet(s, buf);
1504 break;
1505 }
1506
1507 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1508 len = (MAX_PACKET_LENGTH - 5) / 2;
1509 if (len < total_len - addr) {
1510 buf[0] = 'm';
1511 len = memtox(buf + 1, plist + addr, len);
1512 } else {
1513 buf[0] = 'l';
1514 len = memtox(buf + 1, plist + addr, total_len - addr);
1515 }
1516 put_packet_binary(s, buf, len + 1);
1517 g_free((void *) plist);
1518 break;
1519 }
1520
1521 goto unknown_command;
1522
1523 default:
1524 unknown_command:
1525
1526 buf[0] = '\0';
1527 put_packet(s, buf);
1528 break;
1529 }
1530 return RS_IDLE;
1531}
1532
1533static void gdb_output(GDBState *s, const char *msg, int len)
1534{
1535 char buf[MAX_PACKET_LENGTH];
1536
1537 buf[0] = 'O';
1538 if (len > (MAX_PACKET_LENGTH/2) - 1) {
1539 len = (MAX_PACKET_LENGTH/2) - 1;
1540 }
1541 memtohex(buf + 1, (uint8_t *)msg, len);
1542 put_packet(s, buf);
1543}
1544
1545void gdb_set_stop_cpu(CPUState *cpu)
1546{
1547 GDBState *s = gdbserver_state;
1548 int i;
1549
1550 if (!gdbserver_state) {
1551 return;
1552 }
1553
1554 for (i = 1; i <= s->num_clusters; i++) {
1555 if (find_cpu(s, i, cpu_index(cpu))) {
1556 s->cur_cluster = i - 1;
1557 break;
1558 }
1559 }
1560
1561 gdbserver_state->c_cpu = cpu;
1562 gdbserver_state->g_cpu = cpu;
1563}
1564
1565#ifndef CONFIG_USER_ONLY
1566static void gdb_vm_state_change(void *opaque, int running, RunState state)
1567{
1568 GDBState *s = gdbserver_state;
1569 CPUState *cpu = s->c_cpu;
1570 char buf[256];
1571 const char *type;
1572 int ret;
1573
1574 if (running || s->state == RS_INACTIVE) {
1575 return;
1576 }
1577
1578 if (s->current_syscall_cb) {
1579 put_packet(s, s->syscall_buf);
1580 return;
1581 }
1582 switch (state) {
1583 case RUN_STATE_DEBUG:
1584 if (cpu->watchpoint_hit) {
1585 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
1586 case BP_MEM_READ:
1587 type = "r";
1588 break;
1589 case BP_MEM_ACCESS:
1590 type = "a";
1591 break;
1592 default:
1593 type = "";
1594 break;
1595 }
1596 snprintf(buf, sizeof(buf),
1597 "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
1598 GDB_SIGNAL_TRAP,
1599 gdb_gen_thread_id(s, s->cur_cluster + 1, cpu_index(cpu)),
1600 type,
1601 (target_ulong)cpu->watchpoint_hit->vaddr);
1602 cpu->watchpoint_hit = NULL;
1603 goto send_packet;
1604 }
1605 tb_flush(cpu);
1606 ret = GDB_SIGNAL_TRAP;
1607 break;
1608 case RUN_STATE_PAUSED:
1609 ret = GDB_SIGNAL_INT;
1610 break;
1611 case RUN_STATE_SHUTDOWN:
1612 ret = GDB_SIGNAL_QUIT;
1613 break;
1614 case RUN_STATE_IO_ERROR:
1615 ret = GDB_SIGNAL_IO;
1616 break;
1617 case RUN_STATE_WATCHDOG:
1618 ret = GDB_SIGNAL_ALRM;
1619 break;
1620 case RUN_STATE_INTERNAL_ERROR:
1621 ret = GDB_SIGNAL_ABRT;
1622 break;
1623 case RUN_STATE_SAVE_VM:
1624 case RUN_STATE_RESTORE_VM:
1625 return;
1626 case RUN_STATE_FINISH_MIGRATE:
1627 ret = GDB_SIGNAL_XCPU;
1628 break;
1629 default:
1630 ret = GDB_SIGNAL_UNKNOWN;
1631 break;
1632 }
1633 snprintf(buf, sizeof(buf), "T%02xthread:%s;", ret,
1634 gdb_gen_thread_id(s, s->cur_cluster + 1, cpu_index(cpu)));
1635
1636send_packet:
1637 put_packet(s, buf);
1638
1639
1640 cpu_single_step(cpu, 0);
1641}
1642#endif
1643
1644
1645
1646
1647
1648
1649void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
1650{
1651 char *p;
1652 char *p_end;
1653 target_ulong addr;
1654 uint64_t i64;
1655 GDBState *s;
1656
1657 s = gdbserver_state;
1658 if (!s)
1659 return;
1660 s->current_syscall_cb = cb;
1661#ifndef CONFIG_USER_ONLY
1662 vm_stop(RUN_STATE_DEBUG);
1663#endif
1664 p = s->syscall_buf;
1665 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
1666 *(p++) = 'F';
1667 while (*fmt) {
1668 if (*fmt == '%') {
1669 fmt++;
1670 switch (*fmt++) {
1671 case 'x':
1672 addr = va_arg(va, target_ulong);
1673 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
1674 break;
1675 case 'l':
1676 if (*(fmt++) != 'x')
1677 goto bad_format;
1678 i64 = va_arg(va, uint64_t);
1679 p += snprintf(p, p_end - p, "%" PRIx64, i64);
1680 break;
1681 case 's':
1682 addr = va_arg(va, target_ulong);
1683 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
1684 addr, va_arg(va, int));
1685 break;
1686 default:
1687 bad_format:
1688 fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
1689 fmt - 1);
1690 break;
1691 }
1692 } else {
1693 *(p++) = *(fmt++);
1694 }
1695 }
1696 *p = 0;
1697#ifdef CONFIG_USER_ONLY
1698 put_packet(s, s->syscall_buf);
1699 gdb_handlesig(s->c_cpu, 0);
1700#else
1701
1702
1703
1704
1705
1706
1707 qemu_cpu_kick(s->c_cpu);
1708#endif
1709}
1710
1711void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
1712{
1713 va_list va;
1714
1715 va_start(va, fmt);
1716 gdb_do_syscallv(cb, fmt, va);
1717 va_end(va);
1718}
1719
1720static void gdb_read_byte(GDBState *s, int ch)
1721{
1722 int i, csum;
1723 uint8_t reply;
1724
1725#ifndef CONFIG_USER_ONLY
1726 if (s->last_packet_len) {
1727
1728
1729 if (ch == '-') {
1730#ifdef DEBUG_GDB
1731 printf("Got NACK, retransmitting\n");
1732#endif
1733 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
1734 }
1735#ifdef DEBUG_GDB
1736 else if (ch == '+')
1737 printf("Got ACK\n");
1738 else
1739 printf("Got '%c' when expecting ACK/NACK\n", ch);
1740#endif
1741 if (ch == '+' || ch == '$')
1742 s->last_packet_len = 0;
1743 if (ch != '$')
1744 return;
1745 }
1746 if (runstate_is_running()) {
1747
1748
1749 vm_stop(RUN_STATE_PAUSED);
1750 } else
1751#endif
1752 {
1753 switch(s->state) {
1754 case RS_IDLE:
1755 if (ch == '$') {
1756 s->line_buf_index = 0;
1757 s->state = RS_GETLINE;
1758 }
1759 break;
1760 case RS_GETLINE:
1761 if (ch == '#') {
1762 s->state = RS_CHKSUM1;
1763 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1764 s->state = RS_IDLE;
1765 } else {
1766 s->line_buf[s->line_buf_index++] = ch;
1767 }
1768 break;
1769 case RS_CHKSUM1:
1770 s->line_buf[s->line_buf_index] = '\0';
1771 s->line_csum = fromhex(ch) << 4;
1772 s->state = RS_CHKSUM2;
1773 break;
1774 case RS_CHKSUM2:
1775 s->line_csum |= fromhex(ch);
1776 csum = 0;
1777 for(i = 0; i < s->line_buf_index; i++) {
1778 csum += s->line_buf[i];
1779 }
1780 if (s->line_csum != (csum & 0xff)) {
1781 reply = '-';
1782 put_buffer(s, &reply, 1);
1783 s->state = RS_IDLE;
1784 } else {
1785 reply = '+';
1786 put_buffer(s, &reply, 1);
1787 s->state = gdb_handle_packet(s, s->line_buf);
1788#ifdef CONFIG_REMOTE_PORT
1789 bool tw_en = rp_time_warp_enable(false);
1790 rp_time_warp_enable(tw_en);
1791#endif
1792 }
1793 break;
1794 default:
1795 abort();
1796 }
1797 }
1798}
1799
1800
1801void gdb_exit(CPUArchState *env, int code)
1802{
1803 GDBState *s;
1804 char buf[4];
1805
1806 s = gdbserver_state;
1807 if (!s) {
1808 return;
1809 }
1810#ifdef CONFIG_USER_ONLY
1811 if (gdbserver_fd < 0 || s->fd < 0) {
1812 return;
1813 }
1814#endif
1815
1816 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
1817 put_packet(s, buf);
1818
1819#ifndef CONFIG_USER_ONLY
1820 if (s->chr) {
1821 qemu_chr_delete(s->chr);
1822 }
1823#endif
1824}
1825
1826#ifdef CONFIG_USER_ONLY
1827int
1828gdb_queuesig (void)
1829{
1830 GDBState *s;
1831
1832 s = gdbserver_state;
1833
1834 if (gdbserver_fd < 0 || s->fd < 0)
1835 return 0;
1836 else
1837 return 1;
1838}
1839
1840int
1841gdb_handlesig(CPUState *cpu, int sig)
1842{
1843 GDBState *s;
1844 char buf[256];
1845 int n;
1846
1847 s = gdbserver_state;
1848 if (gdbserver_fd < 0 || s->fd < 0) {
1849 return sig;
1850 }
1851
1852
1853 cpu_single_step(cpu, 0);
1854 tb_flush(cpu);
1855
1856 if (sig != 0) {
1857 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
1858 put_packet(s, buf);
1859 }
1860
1861
1862 if (s->fd < 0) {
1863 return sig;
1864 }
1865
1866 sig = 0;
1867 s->state = RS_IDLE;
1868 s->running_state = 0;
1869 while (s->running_state == 0) {
1870 n = read(s->fd, buf, 256);
1871 if (n > 0) {
1872 int i;
1873
1874 for (i = 0; i < n; i++) {
1875 gdb_read_byte(s, buf[i]);
1876 }
1877 } else if (n == 0 || errno != EAGAIN) {
1878
1879
1880 s->client_connected = false;
1881 return sig;
1882 }
1883 }
1884 sig = s->signal;
1885 s->signal = 0;
1886 return sig;
1887}
1888
1889
1890void gdb_signalled(CPUArchState *env, int sig)
1891{
1892 GDBState *s;
1893 char buf[4];
1894
1895 s = gdbserver_state;
1896 if (gdbserver_fd < 0 || s->fd < 0) {
1897 return;
1898 }
1899
1900 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
1901 put_packet(s, buf);
1902}
1903
1904static void gdb_accept(void)
1905{
1906 GDBState *s;
1907 struct sockaddr_in sockaddr;
1908 socklen_t len;
1909 int fd;
1910
1911 for(;;) {
1912 len = sizeof(sockaddr);
1913 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1914 if (fd < 0 && errno != EINTR) {
1915 perror("accept");
1916 return;
1917 } else if (fd >= 0) {
1918#ifndef _WIN32
1919 fcntl(fd, F_SETFD, FD_CLOEXEC);
1920#endif
1921 break;
1922 }
1923 }
1924
1925
1926 socket_set_nodelay(fd);
1927
1928 s = g_malloc0(sizeof(GDBState));
1929 s->c_cpu = first_cpu;
1930 s->g_cpu = first_cpu;
1931 s->fd = fd;
1932 gdb_has_xml = false;
1933
1934 gdbserver_state = s;
1935 s->client_connected = true;
1936 fcntl(fd, F_SETFL, O_NONBLOCK);
1937}
1938
1939static int gdbserver_open(int port)
1940{
1941 struct sockaddr_in sockaddr;
1942 int fd, ret;
1943
1944 fd = socket(PF_INET, SOCK_STREAM, 0);
1945 if (fd < 0) {
1946 perror("socket");
1947 return -1;
1948 }
1949#ifndef _WIN32
1950 fcntl(fd, F_SETFD, FD_CLOEXEC);
1951#endif
1952
1953 socket_set_fast_reuse(fd);
1954
1955 sockaddr.sin_family = AF_INET;
1956 sockaddr.sin_port = htons(port);
1957 sockaddr.sin_addr.s_addr = 0;
1958 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1959 if (ret < 0) {
1960 perror("bind");
1961 close(fd);
1962 return -1;
1963 }
1964 ret = listen(fd, 0);
1965 if (ret < 0) {
1966 perror("listen");
1967 close(fd);
1968 return -1;
1969 }
1970 return fd;
1971}
1972
1973int gdbserver_start(int port)
1974{
1975 gdbserver_fd = gdbserver_open(port);
1976 if (gdbserver_fd < 0)
1977 return -1;
1978
1979 gdb_accept();
1980 return 0;
1981}
1982
1983
1984void gdbserver_fork(CPUState *cpu)
1985{
1986 GDBState *s = gdbserver_state;
1987
1988 if (gdbserver_fd < 0 || s->fd < 0) {
1989 return;
1990 }
1991 close(s->fd);
1992 s->fd = -1;
1993 cpu_breakpoint_remove_all(cpu, BP_GDB);
1994 cpu_watchpoint_remove_all(cpu, BP_GDB);
1995}
1996#else
1997static void gdb_autosplit_cpus(GDBState *s)
1998{
1999
2000
2001
2002
2003
2004 CPUState *cpu = first_cpu;
2005 CPUState *cpu_prev = NULL;
2006
2007 assert(s->clusters == NULL);
2008 assert(s->num_clusters == 0);
2009
2010 while (cpu) {
2011 if (!cpu_prev || (CPU_GET_CLASS(cpu) != CPU_GET_CLASS(cpu_prev))) {
2012
2013 s->clusters = g_renew(typeof(s->clusters[0]), s->clusters,
2014 s->num_clusters + 1);
2015 if (s->num_clusters) {
2016 s->clusters[s->num_clusters - 1].cpus.last = cpu_prev;
2017 }
2018 s->clusters[s->num_clusters].attached = false;
2019 s->clusters[s->num_clusters].cpus.first = cpu;
2020 s->num_clusters++;
2021 }
2022 cpu_prev = cpu;
2023 cpu = CPU_NEXT(cpu);
2024 }
2025 s->clusters[s->num_clusters - 1].cpus.last = cpu_prev;
2026
2027#ifdef DEBUG_GDB
2028 {
2029 unsigned int i;
2030
2031 for (i = 0; i < s->num_clusters; i++) {
2032 cpu = s->clusters[i].cpus.first;
2033 while (cpu) {
2034 CPUClass *cc = CPU_GET_CLASS(cpu);
2035 const char *name = object_get_canonical_path(OBJECT(cpu));
2036 qemu_log("Cluster%d: CPU%d %s xml=%s\n", i, cpu->cpu_index,
2037 name, cc->gdb_core_xml_file);
2038 if (cpu == s->clusters[i].cpus.last) {
2039 break;
2040 }
2041 cpu = CPU_NEXT(cpu);
2042 }
2043 }
2044 }
2045#endif
2046}
2047
2048static int gdb_chr_can_receive(void *opaque)
2049{
2050
2051
2052 return MAX_PACKET_LENGTH;
2053}
2054
2055static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
2056{
2057 int i;
2058
2059 for (i = 0; i < size; i++) {
2060 gdb_read_byte(gdbserver_state, buf[i]);
2061 }
2062}
2063
2064static void gdb_chr_event(void *opaque, int event)
2065{
2066 switch (event) {
2067 case CHR_EVENT_OPENED:
2068 vm_stop(RUN_STATE_PAUSED);
2069 gdb_has_xml = false;
2070 if (gdbserver_state) {
2071 gdbserver_state->multiprocess = false;
2072 gdbserver_state->client_connected = true;
2073 }
2074 break;
2075 case CHR_EVENT_CLOSED: {
2076 if (gdbserver_state) {
2077 gdbserver_state->client_connected = false;
2078 }
2079 break;
2080 }
2081 default:
2082 break;
2083 }
2084}
2085
2086static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
2087{
2088 const char *p = (const char *)buf;
2089 int max_sz;
2090
2091 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
2092 for (;;) {
2093 if (len <= max_sz) {
2094 gdb_output(gdbserver_state, p, len);
2095 break;
2096 }
2097 gdb_output(gdbserver_state, p, max_sz);
2098 p += max_sz;
2099 len -= max_sz;
2100 }
2101 return len;
2102}
2103
2104#ifndef _WIN32
2105static void gdb_sigterm_handler(int signal)
2106{
2107 if (runstate_is_running()) {
2108 vm_stop(RUN_STATE_PAUSED);
2109 }
2110}
2111#endif
2112
2113int gdbserver_start(const char *device)
2114{
2115 GDBState *s;
2116 char gdbstub_device_name[128];
2117 CharDriverState *chr = NULL;
2118 CharDriverState *mon_chr;
2119 ChardevCommon common = { 0 };
2120
2121 if (!device)
2122 return -1;
2123 if (strcmp(device, "none") != 0) {
2124 if (strstart(device, "tcp:", NULL)) {
2125
2126 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
2127 "%s,nowait,nodelay,server", device);
2128 device = gdbstub_device_name;
2129 }
2130#ifndef _WIN32
2131 else if (strcmp(device, "stdio") == 0) {
2132 struct sigaction act;
2133
2134 memset(&act, 0, sizeof(act));
2135 act.sa_handler = gdb_sigterm_handler;
2136 sigaction(SIGINT, &act, NULL);
2137 }
2138#endif
2139 chr = qemu_chr_new("gdb", device, NULL);
2140 if (!chr)
2141 return -1;
2142
2143 qemu_chr_fe_claim_no_fail(chr);
2144 qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
2145 gdb_chr_event, NULL);
2146 }
2147
2148 s = gdbserver_state;
2149 if (!s) {
2150 s = g_malloc0(sizeof(GDBState));
2151 gdbserver_state = s;
2152
2153 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
2154
2155
2156 mon_chr = qemu_chr_alloc(&common, &error_abort);
2157 mon_chr->chr_write = gdb_monitor_write;
2158 monitor_init(mon_chr, 0);
2159 } else {
2160 if (s->chr)
2161 qemu_chr_delete(s->chr);
2162 mon_chr = s->mon_chr;
2163 memset(s, 0, sizeof(GDBState));
2164 }
2165 gdb_autosplit_cpus(s);
2166 s->c_cpu = first_cpu;
2167 s->g_cpu = first_cpu;
2168 s->chr = chr;
2169 s->state = chr ? RS_IDLE : RS_INACTIVE;
2170 s->mon_chr = mon_chr;
2171 s->current_syscall_cb = NULL;
2172
2173 return 0;
2174}
2175#endif
2176