1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26#include "qemu/osdep.h"
27#include "qemu-common.h"
28#include "qapi/error.h"
29#include "qemu/error-report.h"
30#include "qemu/ctype.h"
31#include "qemu/cutils.h"
32#include "qemu/module.h"
33#include "trace-root.h"
34#ifdef CONFIG_USER_ONLY
35#include "qemu.h"
36#else
37#include "monitor/monitor.h"
38#include "chardev/char.h"
39#include "chardev/char-fe.h"
40#include "sysemu/sysemu.h"
41#include "exec/gdbstub.h"
42#include "hw/cpu/cluster.h"
43#include "hw/boards.h"
44#endif
45
46#define MAX_PACKET_LENGTH 4096
47
48#include "qemu/sockets.h"
49#include "sysemu/hw_accel.h"
50#include "sysemu/kvm.h"
51#include "sysemu/runstate.h"
52#include "hw/semihosting/semihost.h"
53#include "exec/exec-all.h"
54
55#ifdef CONFIG_USER_ONLY
56#define GDB_ATTACHED "0"
57#else
58#define GDB_ATTACHED "1"
59#endif
60
61#ifndef CONFIG_USER_ONLY
62static int phy_memory_mode;
63#endif
64
65static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
66 uint8_t *buf, int len, bool is_write)
67{
68 CPUClass *cc;
69
70#ifndef CONFIG_USER_ONLY
71 if (phy_memory_mode) {
72 if (is_write) {
73 cpu_physical_memory_write(addr, buf, len);
74 } else {
75 cpu_physical_memory_read(addr, buf, len);
76 }
77 return 0;
78 }
79#endif
80
81 cc = CPU_GET_CLASS(cpu);
82 if (cc->memory_rw_debug) {
83 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
84 }
85 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
86}
87
88
89
90
91
92
93static inline int cpu_gdb_index(CPUState *cpu)
94{
95#if defined(CONFIG_USER_ONLY)
96 TaskState *ts = (TaskState *) cpu->opaque;
97 return ts->ts_tid;
98#else
99 return cpu->cpu_index + 1;
100#endif
101}
102
103enum {
104 GDB_SIGNAL_0 = 0,
105 GDB_SIGNAL_INT = 2,
106 GDB_SIGNAL_QUIT = 3,
107 GDB_SIGNAL_TRAP = 5,
108 GDB_SIGNAL_ABRT = 6,
109 GDB_SIGNAL_ALRM = 14,
110 GDB_SIGNAL_IO = 23,
111 GDB_SIGNAL_XCPU = 24,
112 GDB_SIGNAL_UNKNOWN = 143
113};
114
115#ifdef CONFIG_USER_ONLY
116
117
118
119
120
121
122static int gdb_signal_table[] = {
123 0,
124 TARGET_SIGHUP,
125 TARGET_SIGINT,
126 TARGET_SIGQUIT,
127 TARGET_SIGILL,
128 TARGET_SIGTRAP,
129 TARGET_SIGABRT,
130 -1,
131 TARGET_SIGFPE,
132 TARGET_SIGKILL,
133 TARGET_SIGBUS,
134 TARGET_SIGSEGV,
135 TARGET_SIGSYS,
136 TARGET_SIGPIPE,
137 TARGET_SIGALRM,
138 TARGET_SIGTERM,
139 TARGET_SIGURG,
140 TARGET_SIGSTOP,
141 TARGET_SIGTSTP,
142 TARGET_SIGCONT,
143 TARGET_SIGCHLD,
144 TARGET_SIGTTIN,
145 TARGET_SIGTTOU,
146 TARGET_SIGIO,
147 TARGET_SIGXCPU,
148 TARGET_SIGXFSZ,
149 TARGET_SIGVTALRM,
150 TARGET_SIGPROF,
151 TARGET_SIGWINCH,
152 -1,
153 TARGET_SIGUSR1,
154 TARGET_SIGUSR2,
155#ifdef TARGET_SIGPWR
156 TARGET_SIGPWR,
157#else
158 -1,
159#endif
160 -1,
161 -1,
162 -1,
163 -1,
164 -1,
165 -1,
166 -1,
167 -1,
168 -1,
169 -1,
170 -1,
171 -1,
172#ifdef __SIGRTMIN
173 __SIGRTMIN + 1,
174 __SIGRTMIN + 2,
175 __SIGRTMIN + 3,
176 __SIGRTMIN + 4,
177 __SIGRTMIN + 5,
178 __SIGRTMIN + 6,
179 __SIGRTMIN + 7,
180 __SIGRTMIN + 8,
181 __SIGRTMIN + 9,
182 __SIGRTMIN + 10,
183 __SIGRTMIN + 11,
184 __SIGRTMIN + 12,
185 __SIGRTMIN + 13,
186 __SIGRTMIN + 14,
187 __SIGRTMIN + 15,
188 __SIGRTMIN + 16,
189 __SIGRTMIN + 17,
190 __SIGRTMIN + 18,
191 __SIGRTMIN + 19,
192 __SIGRTMIN + 20,
193 __SIGRTMIN + 21,
194 __SIGRTMIN + 22,
195 __SIGRTMIN + 23,
196 __SIGRTMIN + 24,
197 __SIGRTMIN + 25,
198 __SIGRTMIN + 26,
199 __SIGRTMIN + 27,
200 __SIGRTMIN + 28,
201 __SIGRTMIN + 29,
202 __SIGRTMIN + 30,
203 __SIGRTMIN + 31,
204 -1,
205 __SIGRTMIN,
206 __SIGRTMIN + 32,
207 __SIGRTMIN + 33,
208 __SIGRTMIN + 34,
209 __SIGRTMIN + 35,
210 __SIGRTMIN + 36,
211 __SIGRTMIN + 37,
212 __SIGRTMIN + 38,
213 __SIGRTMIN + 39,
214 __SIGRTMIN + 40,
215 __SIGRTMIN + 41,
216 __SIGRTMIN + 42,
217 __SIGRTMIN + 43,
218 __SIGRTMIN + 44,
219 __SIGRTMIN + 45,
220 __SIGRTMIN + 46,
221 __SIGRTMIN + 47,
222 __SIGRTMIN + 48,
223 __SIGRTMIN + 49,
224 __SIGRTMIN + 50,
225 __SIGRTMIN + 51,
226 __SIGRTMIN + 52,
227 __SIGRTMIN + 53,
228 __SIGRTMIN + 54,
229 __SIGRTMIN + 55,
230 __SIGRTMIN + 56,
231 __SIGRTMIN + 57,
232 __SIGRTMIN + 58,
233 __SIGRTMIN + 59,
234 __SIGRTMIN + 60,
235 __SIGRTMIN + 61,
236 __SIGRTMIN + 62,
237 __SIGRTMIN + 63,
238 __SIGRTMIN + 64,
239 __SIGRTMIN + 65,
240 __SIGRTMIN + 66,
241 __SIGRTMIN + 67,
242 __SIGRTMIN + 68,
243 __SIGRTMIN + 69,
244 __SIGRTMIN + 70,
245 __SIGRTMIN + 71,
246 __SIGRTMIN + 72,
247 __SIGRTMIN + 73,
248 __SIGRTMIN + 74,
249 __SIGRTMIN + 75,
250 __SIGRTMIN + 76,
251 __SIGRTMIN + 77,
252 __SIGRTMIN + 78,
253 __SIGRTMIN + 79,
254 __SIGRTMIN + 80,
255 __SIGRTMIN + 81,
256 __SIGRTMIN + 82,
257 __SIGRTMIN + 83,
258 __SIGRTMIN + 84,
259 __SIGRTMIN + 85,
260 __SIGRTMIN + 86,
261 __SIGRTMIN + 87,
262 __SIGRTMIN + 88,
263 __SIGRTMIN + 89,
264 __SIGRTMIN + 90,
265 __SIGRTMIN + 91,
266 __SIGRTMIN + 92,
267 __SIGRTMIN + 93,
268 __SIGRTMIN + 94,
269 __SIGRTMIN + 95,
270 -1,
271 -1,
272 -1,
273 -1,
274 -1,
275 -1,
276 -1,
277 -1,
278 -1
279#endif
280};
281#else
282
283
284
285enum {
286 TARGET_SIGINT = 2,
287 TARGET_SIGTRAP = 5
288};
289
290static int gdb_signal_table[] = {
291 -1,
292 -1,
293 TARGET_SIGINT,
294 -1,
295 -1,
296 TARGET_SIGTRAP
297};
298#endif
299
300#ifdef CONFIG_USER_ONLY
301static int target_signal_to_gdb (int sig)
302{
303 int i;
304 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
305 if (gdb_signal_table[i] == sig)
306 return i;
307 return GDB_SIGNAL_UNKNOWN;
308}
309#endif
310
311static int gdb_signal_to_target (int sig)
312{
313 if (sig < ARRAY_SIZE (gdb_signal_table))
314 return gdb_signal_table[sig];
315 else
316 return -1;
317}
318
319typedef struct GDBRegisterState {
320 int base_reg;
321 int num_regs;
322 gdb_reg_cb get_reg;
323 gdb_reg_cb set_reg;
324 const char *xml;
325 struct GDBRegisterState *next;
326} GDBRegisterState;
327
328typedef struct GDBProcess {
329 uint32_t pid;
330 bool attached;
331
332 char target_xml[1024];
333} GDBProcess;
334
335enum RSState {
336 RS_INACTIVE,
337 RS_IDLE,
338 RS_GETLINE,
339 RS_GETLINE_ESC,
340 RS_GETLINE_RLE,
341 RS_CHKSUM1,
342 RS_CHKSUM2,
343};
344typedef struct GDBState {
345 CPUState *c_cpu;
346 CPUState *g_cpu;
347 CPUState *query_cpu;
348 enum RSState state;
349 char line_buf[MAX_PACKET_LENGTH];
350 int line_buf_index;
351 int line_sum;
352 int line_csum;
353 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
354 int last_packet_len;
355 int signal;
356#ifdef CONFIG_USER_ONLY
357 int fd;
358 int running_state;
359#else
360 CharBackend chr;
361 Chardev *mon_chr;
362#endif
363 bool multiprocess;
364 GDBProcess *processes;
365 int process_num;
366 char syscall_buf[256];
367 gdb_syscall_complete_cb current_syscall_cb;
368} GDBState;
369
370
371
372
373static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
374
375static GDBState *gdbserver_state;
376
377bool gdb_has_xml;
378
379#ifdef CONFIG_USER_ONLY
380
381static int gdbserver_fd = -1;
382
383static int get_char(GDBState *s)
384{
385 uint8_t ch;
386 int ret;
387
388 for(;;) {
389 ret = qemu_recv(s->fd, &ch, 1, 0);
390 if (ret < 0) {
391 if (errno == ECONNRESET)
392 s->fd = -1;
393 if (errno != EINTR)
394 return -1;
395 } else if (ret == 0) {
396 close(s->fd);
397 s->fd = -1;
398 return -1;
399 } else {
400 break;
401 }
402 }
403 return ch;
404}
405#endif
406
407static enum {
408 GDB_SYS_UNKNOWN,
409 GDB_SYS_ENABLED,
410 GDB_SYS_DISABLED,
411} gdb_syscall_mode;
412
413
414int use_gdb_syscalls(void)
415{
416 SemihostingTarget target = semihosting_get_target();
417 if (target == SEMIHOSTING_TARGET_NATIVE) {
418
419 return false;
420 } else if (target == SEMIHOSTING_TARGET_GDB) {
421
422 return true;
423 }
424
425
426
427 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
428 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
429 : GDB_SYS_DISABLED);
430 }
431 return gdb_syscall_mode == GDB_SYS_ENABLED;
432}
433
434
435static inline void gdb_continue(GDBState *s)
436{
437
438#ifdef CONFIG_USER_ONLY
439 s->running_state = 1;
440 trace_gdbstub_op_continue();
441#else
442 if (!runstate_needs_reset()) {
443 trace_gdbstub_op_continue();
444 vm_start();
445 }
446#endif
447}
448
449
450
451
452
453static int gdb_continue_partial(GDBState *s, char *newstates)
454{
455 CPUState *cpu;
456 int res = 0;
457#ifdef CONFIG_USER_ONLY
458
459
460
461
462 CPU_FOREACH(cpu) {
463 if (newstates[cpu->cpu_index] == 's') {
464 trace_gdbstub_op_stepping(cpu->cpu_index);
465 cpu_single_step(cpu, sstep_flags);
466 }
467 }
468 s->running_state = 1;
469#else
470 int flag = 0;
471
472 if (!runstate_needs_reset()) {
473 if (vm_prepare_start()) {
474 return 0;
475 }
476
477 CPU_FOREACH(cpu) {
478 switch (newstates[cpu->cpu_index]) {
479 case 0:
480 case 1:
481 break;
482 case 's':
483 trace_gdbstub_op_stepping(cpu->cpu_index);
484 cpu_single_step(cpu, sstep_flags);
485 cpu_resume(cpu);
486 flag = 1;
487 break;
488 case 'c':
489 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
490 cpu_resume(cpu);
491 flag = 1;
492 break;
493 default:
494 res = -1;
495 break;
496 }
497 }
498 }
499 if (flag) {
500 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
501 }
502#endif
503 return res;
504}
505
506static void put_buffer(GDBState *s, const uint8_t *buf, int len)
507{
508#ifdef CONFIG_USER_ONLY
509 int ret;
510
511 while (len > 0) {
512 ret = send(s->fd, buf, len, 0);
513 if (ret < 0) {
514 if (errno != EINTR)
515 return;
516 } else {
517 buf += ret;
518 len -= ret;
519 }
520 }
521#else
522
523
524 qemu_chr_fe_write_all(&s->chr, buf, len);
525#endif
526}
527
528static inline int fromhex(int v)
529{
530 if (v >= '0' && v <= '9')
531 return v - '0';
532 else if (v >= 'A' && v <= 'F')
533 return v - 'A' + 10;
534 else if (v >= 'a' && v <= 'f')
535 return v - 'a' + 10;
536 else
537 return 0;
538}
539
540static inline int tohex(int v)
541{
542 if (v < 10)
543 return v + '0';
544 else
545 return v - 10 + 'a';
546}
547
548
549static void memtohex(char *buf, const uint8_t *mem, int len)
550{
551 int i, c;
552 char *q;
553 q = buf;
554 for(i = 0; i < len; i++) {
555 c = mem[i];
556 *q++ = tohex(c >> 4);
557 *q++ = tohex(c & 0xf);
558 }
559 *q = '\0';
560}
561
562static void hextomem(uint8_t *mem, const char *buf, int len)
563{
564 int i;
565
566 for(i = 0; i < len; i++) {
567 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
568 buf += 2;
569 }
570}
571
572static void hexdump(const char *buf, int len,
573 void (*trace_fn)(size_t ofs, char const *text))
574{
575 char line_buffer[3 * 16 + 4 + 16 + 1];
576
577 size_t i;
578 for (i = 0; i < len || (i & 0xF); ++i) {
579 size_t byte_ofs = i & 15;
580
581 if (byte_ofs == 0) {
582 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
583 line_buffer[3 * 16 + 4 + 16] = 0;
584 }
585
586 size_t col_group = (i >> 2) & 3;
587 size_t hex_col = byte_ofs * 3 + col_group;
588 size_t txt_col = 3 * 16 + 4 + byte_ofs;
589
590 if (i < len) {
591 char value = buf[i];
592
593 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
594 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
595 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
596 ? value
597 : '.';
598 }
599
600 if (byte_ofs == 0xF)
601 trace_fn(i & -16, line_buffer);
602 }
603}
604
605
606static int put_packet_binary(GDBState *s, const char *buf, int len, bool dump)
607{
608 int csum, i;
609 uint8_t *p;
610
611 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
612 hexdump(buf, len, trace_gdbstub_io_binaryreply);
613 }
614
615 for(;;) {
616 p = s->last_packet;
617 *(p++) = '$';
618 memcpy(p, buf, len);
619 p += len;
620 csum = 0;
621 for(i = 0; i < len; i++) {
622 csum += buf[i];
623 }
624 *(p++) = '#';
625 *(p++) = tohex((csum >> 4) & 0xf);
626 *(p++) = tohex((csum) & 0xf);
627
628 s->last_packet_len = p - s->last_packet;
629 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
630
631#ifdef CONFIG_USER_ONLY
632 i = get_char(s);
633 if (i < 0)
634 return -1;
635 if (i == '+')
636 break;
637#else
638 break;
639#endif
640 }
641 return 0;
642}
643
644
645static int put_packet(GDBState *s, const char *buf)
646{
647 trace_gdbstub_io_reply(buf);
648
649 return put_packet_binary(s, buf, strlen(buf), false);
650}
651
652
653static int memtox(char *buf, const char *mem, int len)
654{
655 char *p = buf;
656 char c;
657
658 while (len--) {
659 c = *(mem++);
660 switch (c) {
661 case '#': case '$': case '*': case '}':
662 *(p++) = '}';
663 *(p++) = c ^ 0x20;
664 break;
665 default:
666 *(p++) = c;
667 break;
668 }
669 }
670 return p - buf;
671}
672
673static uint32_t gdb_get_cpu_pid(const GDBState *s, CPUState *cpu)
674{
675
676 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
677
678 return s->processes[s->process_num - 1].pid;
679 }
680 return cpu->cluster_index + 1;
681}
682
683static GDBProcess *gdb_get_process(const GDBState *s, uint32_t pid)
684{
685 int i;
686
687 if (!pid) {
688
689 return &s->processes[0];
690 }
691
692 for (i = 0; i < s->process_num; i++) {
693 if (s->processes[i].pid == pid) {
694 return &s->processes[i];
695 }
696 }
697
698 return NULL;
699}
700
701static GDBProcess *gdb_get_cpu_process(const GDBState *s, CPUState *cpu)
702{
703 return gdb_get_process(s, gdb_get_cpu_pid(s, cpu));
704}
705
706static CPUState *find_cpu(uint32_t thread_id)
707{
708 CPUState *cpu;
709
710 CPU_FOREACH(cpu) {
711 if (cpu_gdb_index(cpu) == thread_id) {
712 return cpu;
713 }
714 }
715
716 return NULL;
717}
718
719static CPUState *get_first_cpu_in_process(const GDBState *s,
720 GDBProcess *process)
721{
722 CPUState *cpu;
723
724 CPU_FOREACH(cpu) {
725 if (gdb_get_cpu_pid(s, cpu) == process->pid) {
726 return cpu;
727 }
728 }
729
730 return NULL;
731}
732
733static CPUState *gdb_next_cpu_in_process(const GDBState *s, CPUState *cpu)
734{
735 uint32_t pid = gdb_get_cpu_pid(s, cpu);
736 cpu = CPU_NEXT(cpu);
737
738 while (cpu) {
739 if (gdb_get_cpu_pid(s, cpu) == pid) {
740 break;
741 }
742
743 cpu = CPU_NEXT(cpu);
744 }
745
746 return cpu;
747}
748
749
750static CPUState *gdb_next_attached_cpu(const GDBState *s, CPUState *cpu)
751{
752 cpu = CPU_NEXT(cpu);
753
754 while (cpu) {
755 if (gdb_get_cpu_process(s, cpu)->attached) {
756 break;
757 }
758
759 cpu = CPU_NEXT(cpu);
760 }
761
762 return cpu;
763}
764
765
766static CPUState *gdb_first_attached_cpu(const GDBState *s)
767{
768 CPUState *cpu = first_cpu;
769 GDBProcess *process = gdb_get_cpu_process(s, cpu);
770
771 if (!process->attached) {
772 return gdb_next_attached_cpu(s, cpu);
773 }
774
775 return cpu;
776}
777
778static CPUState *gdb_get_cpu(const GDBState *s, uint32_t pid, uint32_t tid)
779{
780 GDBProcess *process;
781 CPUState *cpu;
782
783 if (!pid && !tid) {
784
785 return gdb_first_attached_cpu(s);
786 } else if (pid && !tid) {
787
788 process = gdb_get_process(s, pid);
789
790 if (process == NULL) {
791 return NULL;
792 }
793
794 if (!process->attached) {
795 return NULL;
796 }
797
798 return get_first_cpu_in_process(s, process);
799 } else {
800
801 cpu = find_cpu(tid);
802
803 if (cpu == NULL) {
804 return NULL;
805 }
806
807 process = gdb_get_cpu_process(s, cpu);
808
809 if (pid && process->pid != pid) {
810 return NULL;
811 }
812
813 if (!process->attached) {
814 return NULL;
815 }
816
817 return cpu;
818 }
819}
820
821static const char *get_feature_xml(const GDBState *s, const char *p,
822 const char **newp, GDBProcess *process)
823{
824 size_t len;
825 int i;
826 const char *name;
827 CPUState *cpu = get_first_cpu_in_process(s, process);
828 CPUClass *cc = CPU_GET_CLASS(cpu);
829
830 len = 0;
831 while (p[len] && p[len] != ':')
832 len++;
833 *newp = p + len;
834
835 name = NULL;
836 if (strncmp(p, "target.xml", len) == 0) {
837 char *buf = process->target_xml;
838 const size_t buf_sz = sizeof(process->target_xml);
839
840
841 if (!buf[0]) {
842 GDBRegisterState *r;
843
844 pstrcat(buf, buf_sz,
845 "<?xml version=\"1.0\"?>"
846 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
847 "<target>");
848 if (cc->gdb_arch_name) {
849 gchar *arch = cc->gdb_arch_name(cpu);
850 pstrcat(buf, buf_sz, "<architecture>");
851 pstrcat(buf, buf_sz, arch);
852 pstrcat(buf, buf_sz, "</architecture>");
853 g_free(arch);
854 }
855 pstrcat(buf, buf_sz, "<xi:include href=\"");
856 pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
857 pstrcat(buf, buf_sz, "\"/>");
858 for (r = cpu->gdb_regs; r; r = r->next) {
859 pstrcat(buf, buf_sz, "<xi:include href=\"");
860 pstrcat(buf, buf_sz, r->xml);
861 pstrcat(buf, buf_sz, "\"/>");
862 }
863 pstrcat(buf, buf_sz, "</target>");
864 }
865 return buf;
866 }
867 if (cc->gdb_get_dynamic_xml) {
868 char *xmlname = g_strndup(p, len);
869 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
870
871 g_free(xmlname);
872 if (xml) {
873 return xml;
874 }
875 }
876 for (i = 0; ; i++) {
877 name = xml_builtin[i][0];
878 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
879 break;
880 }
881 return name ? xml_builtin[i][1] : NULL;
882}
883
884static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
885{
886 CPUClass *cc = CPU_GET_CLASS(cpu);
887 CPUArchState *env = cpu->env_ptr;
888 GDBRegisterState *r;
889
890 if (reg < cc->gdb_num_core_regs) {
891 return cc->gdb_read_register(cpu, mem_buf, reg);
892 }
893
894 for (r = cpu->gdb_regs; r; r = r->next) {
895 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
896 return r->get_reg(env, mem_buf, reg - r->base_reg);
897 }
898 }
899 return 0;
900}
901
902static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
903{
904 CPUClass *cc = CPU_GET_CLASS(cpu);
905 CPUArchState *env = cpu->env_ptr;
906 GDBRegisterState *r;
907
908 if (reg < cc->gdb_num_core_regs) {
909 return cc->gdb_write_register(cpu, mem_buf, reg);
910 }
911
912 for (r = cpu->gdb_regs; r; r = r->next) {
913 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
914 return r->set_reg(env, mem_buf, reg - r->base_reg);
915 }
916 }
917 return 0;
918}
919
920
921
922
923
924
925
926void gdb_register_coprocessor(CPUState *cpu,
927 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
928 int num_regs, const char *xml, int g_pos)
929{
930 GDBRegisterState *s;
931 GDBRegisterState **p;
932
933 p = &cpu->gdb_regs;
934 while (*p) {
935
936 if (strcmp((*p)->xml, xml) == 0)
937 return;
938 p = &(*p)->next;
939 }
940
941 s = g_new0(GDBRegisterState, 1);
942 s->base_reg = cpu->gdb_num_regs;
943 s->num_regs = num_regs;
944 s->get_reg = get_reg;
945 s->set_reg = set_reg;
946 s->xml = xml;
947
948
949 cpu->gdb_num_regs += num_regs;
950 *p = s;
951 if (g_pos) {
952 if (g_pos != s->base_reg) {
953 error_report("Error: Bad gdb register numbering for '%s', "
954 "expected %d got %d", xml, g_pos, s->base_reg);
955 } else {
956 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
957 }
958 }
959}
960
961#ifndef CONFIG_USER_ONLY
962
963static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
964{
965 static const int xlat[] = {
966 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
967 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
968 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
969 };
970
971 CPUClass *cc = CPU_GET_CLASS(cpu);
972 int cputype = xlat[gdbtype];
973
974 if (cc->gdb_stop_before_watchpoint) {
975 cputype |= BP_STOP_BEFORE_ACCESS;
976 }
977 return cputype;
978}
979#endif
980
981static int gdb_breakpoint_insert(int type, target_ulong addr, target_ulong len)
982{
983 CPUState *cpu;
984 int err = 0;
985
986 if (kvm_enabled()) {
987 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
988 }
989
990 switch (type) {
991 case GDB_BREAKPOINT_SW:
992 case GDB_BREAKPOINT_HW:
993 CPU_FOREACH(cpu) {
994 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
995 if (err) {
996 break;
997 }
998 }
999 return err;
1000#ifndef CONFIG_USER_ONLY
1001 case GDB_WATCHPOINT_WRITE:
1002 case GDB_WATCHPOINT_READ:
1003 case GDB_WATCHPOINT_ACCESS:
1004 CPU_FOREACH(cpu) {
1005 err = cpu_watchpoint_insert(cpu, addr, len,
1006 xlat_gdb_type(cpu, type), NULL);
1007 if (err) {
1008 break;
1009 }
1010 }
1011 return err;
1012#endif
1013 default:
1014 return -ENOSYS;
1015 }
1016}
1017
1018static int gdb_breakpoint_remove(int type, target_ulong addr, target_ulong len)
1019{
1020 CPUState *cpu;
1021 int err = 0;
1022
1023 if (kvm_enabled()) {
1024 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
1025 }
1026
1027 switch (type) {
1028 case GDB_BREAKPOINT_SW:
1029 case GDB_BREAKPOINT_HW:
1030 CPU_FOREACH(cpu) {
1031 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
1032 if (err) {
1033 break;
1034 }
1035 }
1036 return err;
1037#ifndef CONFIG_USER_ONLY
1038 case GDB_WATCHPOINT_WRITE:
1039 case GDB_WATCHPOINT_READ:
1040 case GDB_WATCHPOINT_ACCESS:
1041 CPU_FOREACH(cpu) {
1042 err = cpu_watchpoint_remove(cpu, addr, len,
1043 xlat_gdb_type(cpu, type));
1044 if (err)
1045 break;
1046 }
1047 return err;
1048#endif
1049 default:
1050 return -ENOSYS;
1051 }
1052}
1053
1054static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu)
1055{
1056 cpu_breakpoint_remove_all(cpu, BP_GDB);
1057#ifndef CONFIG_USER_ONLY
1058 cpu_watchpoint_remove_all(cpu, BP_GDB);
1059#endif
1060}
1061
1062static void gdb_process_breakpoint_remove_all(const GDBState *s, GDBProcess *p)
1063{
1064 CPUState *cpu = get_first_cpu_in_process(s, p);
1065
1066 while (cpu) {
1067 gdb_cpu_breakpoint_remove_all(cpu);
1068 cpu = gdb_next_cpu_in_process(s, cpu);
1069 }
1070}
1071
1072static void gdb_breakpoint_remove_all(void)
1073{
1074 CPUState *cpu;
1075
1076 if (kvm_enabled()) {
1077 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
1078 return;
1079 }
1080
1081 CPU_FOREACH(cpu) {
1082 gdb_cpu_breakpoint_remove_all(cpu);
1083 }
1084}
1085
1086static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
1087{
1088 CPUState *cpu = s->c_cpu;
1089
1090 cpu_synchronize_state(cpu);
1091 cpu_set_pc(cpu, pc);
1092}
1093
1094static char *gdb_fmt_thread_id(const GDBState *s, CPUState *cpu,
1095 char *buf, size_t buf_size)
1096{
1097 if (s->multiprocess) {
1098 snprintf(buf, buf_size, "p%02x.%02x",
1099 gdb_get_cpu_pid(s, cpu), cpu_gdb_index(cpu));
1100 } else {
1101 snprintf(buf, buf_size, "%02x", cpu_gdb_index(cpu));
1102 }
1103
1104 return buf;
1105}
1106
1107typedef enum GDBThreadIdKind {
1108 GDB_ONE_THREAD = 0,
1109 GDB_ALL_THREADS,
1110 GDB_ALL_PROCESSES,
1111 GDB_READ_THREAD_ERR
1112} GDBThreadIdKind;
1113
1114static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1115 uint32_t *pid, uint32_t *tid)
1116{
1117 unsigned long p, t;
1118 int ret;
1119
1120 if (*buf == 'p') {
1121 buf++;
1122 ret = qemu_strtoul(buf, &buf, 16, &p);
1123
1124 if (ret) {
1125 return GDB_READ_THREAD_ERR;
1126 }
1127
1128
1129 buf++;
1130 } else {
1131 p = 1;
1132 }
1133
1134 ret = qemu_strtoul(buf, &buf, 16, &t);
1135
1136 if (ret) {
1137 return GDB_READ_THREAD_ERR;
1138 }
1139
1140 *end_buf = buf;
1141
1142 if (p == -1) {
1143 return GDB_ALL_PROCESSES;
1144 }
1145
1146 if (pid) {
1147 *pid = p;
1148 }
1149
1150 if (t == -1) {
1151 return GDB_ALL_THREADS;
1152 }
1153
1154 if (tid) {
1155 *tid = t;
1156 }
1157
1158 return GDB_ONE_THREAD;
1159}
1160
1161
1162
1163
1164
1165
1166static int gdb_handle_vcont(GDBState *s, const char *p)
1167{
1168 int res, signal = 0;
1169 char cur_action;
1170 char *newstates;
1171 unsigned long tmp;
1172 uint32_t pid, tid;
1173 GDBProcess *process;
1174 CPUState *cpu;
1175 GDBThreadIdKind kind;
1176#ifdef CONFIG_USER_ONLY
1177 int max_cpus = 1;
1178
1179 CPU_FOREACH(cpu) {
1180 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1181 }
1182#else
1183 MachineState *ms = MACHINE(qdev_get_machine());
1184 unsigned int max_cpus = ms->smp.max_cpus;
1185#endif
1186
1187 newstates = g_new0(char, max_cpus);
1188
1189
1190 CPU_FOREACH(cpu) {
1191 newstates[cpu->cpu_index] = 1;
1192 }
1193
1194
1195
1196
1197
1198
1199
1200 res = 0;
1201 while (*p) {
1202 if (*p++ != ';') {
1203 res = -ENOTSUP;
1204 goto out;
1205 }
1206
1207 cur_action = *p++;
1208 if (cur_action == 'C' || cur_action == 'S') {
1209 cur_action = qemu_tolower(cur_action);
1210 res = qemu_strtoul(p + 1, &p, 16, &tmp);
1211 if (res) {
1212 goto out;
1213 }
1214 signal = gdb_signal_to_target(tmp);
1215 } else if (cur_action != 'c' && cur_action != 's') {
1216
1217 res = -ENOTSUP;
1218 goto out;
1219 }
1220
1221 if (*p == '\0' || *p == ';') {
1222
1223
1224
1225
1226
1227 kind = GDB_ALL_PROCESSES;
1228 } else if (*p++ == ':') {
1229 kind = read_thread_id(p, &p, &pid, &tid);
1230 } else {
1231 res = -ENOTSUP;
1232 goto out;
1233 }
1234
1235 switch (kind) {
1236 case GDB_READ_THREAD_ERR:
1237 res = -EINVAL;
1238 goto out;
1239
1240 case GDB_ALL_PROCESSES:
1241 cpu = gdb_first_attached_cpu(s);
1242 while (cpu) {
1243 if (newstates[cpu->cpu_index] == 1) {
1244 newstates[cpu->cpu_index] = cur_action;
1245 }
1246
1247 cpu = gdb_next_attached_cpu(s, cpu);
1248 }
1249 break;
1250
1251 case GDB_ALL_THREADS:
1252 process = gdb_get_process(s, pid);
1253
1254 if (!process->attached) {
1255 res = -EINVAL;
1256 goto out;
1257 }
1258
1259 cpu = get_first_cpu_in_process(s, process);
1260 while (cpu) {
1261 if (newstates[cpu->cpu_index] == 1) {
1262 newstates[cpu->cpu_index] = cur_action;
1263 }
1264
1265 cpu = gdb_next_cpu_in_process(s, cpu);
1266 }
1267 break;
1268
1269 case GDB_ONE_THREAD:
1270 cpu = gdb_get_cpu(s, pid, tid);
1271
1272
1273 if (!cpu) {
1274 res = -EINVAL;
1275 goto out;
1276 }
1277
1278
1279 if (newstates[cpu->cpu_index] == 1) {
1280 newstates[cpu->cpu_index] = cur_action;
1281 }
1282 break;
1283 }
1284 }
1285 s->signal = signal;
1286 gdb_continue_partial(s, newstates);
1287
1288out:
1289 g_free(newstates);
1290
1291 return res;
1292}
1293
1294typedef union GdbCmdVariant {
1295 const char *data;
1296 uint8_t opcode;
1297 unsigned long val_ul;
1298 unsigned long long val_ull;
1299 struct {
1300 GDBThreadIdKind kind;
1301 uint32_t pid;
1302 uint32_t tid;
1303 } thread_id;
1304} GdbCmdVariant;
1305
1306static const char *cmd_next_param(const char *param, const char delimiter)
1307{
1308 static const char all_delimiters[] = ",;:=";
1309 char curr_delimiters[2] = {0};
1310 const char *delimiters;
1311
1312 if (delimiter == '?') {
1313 delimiters = all_delimiters;
1314 } else if (delimiter == '0') {
1315 return strchr(param, '\0');
1316 } else if (delimiter == '.' && *param) {
1317 return param + 1;
1318 } else {
1319 curr_delimiters[0] = delimiter;
1320 delimiters = curr_delimiters;
1321 }
1322
1323 param += strcspn(param, delimiters);
1324 if (*param) {
1325 param++;
1326 }
1327 return param;
1328}
1329
1330static int cmd_parse_params(const char *data, const char *schema,
1331 GdbCmdVariant *params, int *num_params)
1332{
1333 int curr_param;
1334 const char *curr_schema, *curr_data;
1335
1336 *num_params = 0;
1337
1338 if (!schema) {
1339 return 0;
1340 }
1341
1342 curr_schema = schema;
1343 curr_param = 0;
1344 curr_data = data;
1345 while (curr_schema[0] && curr_schema[1] && *curr_data) {
1346 switch (curr_schema[0]) {
1347 case 'l':
1348 if (qemu_strtoul(curr_data, &curr_data, 16,
1349 ¶ms[curr_param].val_ul)) {
1350 return -EINVAL;
1351 }
1352 curr_param++;
1353 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1354 break;
1355 case 'L':
1356 if (qemu_strtou64(curr_data, &curr_data, 16,
1357 (uint64_t *)¶ms[curr_param].val_ull)) {
1358 return -EINVAL;
1359 }
1360 curr_param++;
1361 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1362 break;
1363 case 's':
1364 params[curr_param].data = curr_data;
1365 curr_param++;
1366 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1367 break;
1368 case 'o':
1369 params[curr_param].opcode = *(uint8_t *)curr_data;
1370 curr_param++;
1371 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1372 break;
1373 case 't':
1374 params[curr_param].thread_id.kind =
1375 read_thread_id(curr_data, &curr_data,
1376 ¶ms[curr_param].thread_id.pid,
1377 ¶ms[curr_param].thread_id.tid);
1378 curr_param++;
1379 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1380 break;
1381 case '?':
1382 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1383 break;
1384 default:
1385 return -EINVAL;
1386 }
1387 curr_schema += 2;
1388 }
1389
1390 *num_params = curr_param;
1391 return 0;
1392}
1393
1394typedef struct GdbCmdContext {
1395 GDBState *s;
1396 GdbCmdVariant *params;
1397 int num_params;
1398 uint8_t mem_buf[MAX_PACKET_LENGTH];
1399 char str_buf[MAX_PACKET_LENGTH + 1];
1400} GdbCmdContext;
1401
1402typedef void (*GdbCmdHandler)(GdbCmdContext *gdb_ctx, void *user_ctx);
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427typedef struct GdbCmdParseEntry {
1428 GdbCmdHandler handler;
1429 const char *cmd;
1430 bool cmd_startswith;
1431 const char *schema;
1432} GdbCmdParseEntry;
1433
1434static inline int startswith(const char *string, const char *pattern)
1435{
1436 return !strncmp(string, pattern, strlen(pattern));
1437}
1438
1439static int process_string_cmd(GDBState *s, void *user_ctx, const char *data,
1440 const GdbCmdParseEntry *cmds, int num_cmds)
1441{
1442 int i, schema_len, max_num_params = 0;
1443 GdbCmdContext gdb_ctx;
1444
1445 if (!cmds) {
1446 return -1;
1447 }
1448
1449 for (i = 0; i < num_cmds; i++) {
1450 const GdbCmdParseEntry *cmd = &cmds[i];
1451 g_assert(cmd->handler && cmd->cmd);
1452
1453 if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
1454 (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
1455 continue;
1456 }
1457
1458 if (cmd->schema) {
1459 schema_len = strlen(cmd->schema);
1460 if (schema_len % 2) {
1461 return -2;
1462 }
1463
1464 max_num_params = schema_len / 2;
1465 }
1466
1467 gdb_ctx.params =
1468 (GdbCmdVariant *)alloca(sizeof(*gdb_ctx.params) * max_num_params);
1469 memset(gdb_ctx.params, 0, sizeof(*gdb_ctx.params) * max_num_params);
1470
1471 if (cmd_parse_params(&data[strlen(cmd->cmd)], cmd->schema,
1472 gdb_ctx.params, &gdb_ctx.num_params)) {
1473 return -1;
1474 }
1475
1476 gdb_ctx.s = s;
1477 cmd->handler(&gdb_ctx, user_ctx);
1478 return 0;
1479 }
1480
1481 return -1;
1482}
1483
1484static void run_cmd_parser(GDBState *s, const char *data,
1485 const GdbCmdParseEntry *cmd)
1486{
1487 if (!data) {
1488 return;
1489 }
1490
1491
1492
1493 if (process_string_cmd(s, NULL, data, cmd, 1)) {
1494 put_packet(s, "");
1495 }
1496}
1497
1498static void handle_detach(GdbCmdContext *gdb_ctx, void *user_ctx)
1499{
1500 GDBProcess *process;
1501 GDBState *s = gdb_ctx->s;
1502 uint32_t pid = 1;
1503
1504 if (s->multiprocess) {
1505 if (!gdb_ctx->num_params) {
1506 put_packet(s, "E22");
1507 return;
1508 }
1509
1510 pid = gdb_ctx->params[0].val_ul;
1511 }
1512
1513 process = gdb_get_process(s, pid);
1514 gdb_process_breakpoint_remove_all(s, process);
1515 process->attached = false;
1516
1517 if (pid == gdb_get_cpu_pid(s, s->c_cpu)) {
1518 s->c_cpu = gdb_first_attached_cpu(s);
1519 }
1520
1521 if (pid == gdb_get_cpu_pid(s, s->g_cpu)) {
1522 s->g_cpu = gdb_first_attached_cpu(s);
1523 }
1524
1525 if (!s->c_cpu) {
1526
1527 gdb_syscall_mode = GDB_SYS_DISABLED;
1528 gdb_continue(s);
1529 }
1530 put_packet(s, "OK");
1531}
1532
1533static void handle_thread_alive(GdbCmdContext *gdb_ctx, void *user_ctx)
1534{
1535 CPUState *cpu;
1536
1537 if (!gdb_ctx->num_params) {
1538 put_packet(gdb_ctx->s, "E22");
1539 return;
1540 }
1541
1542 if (gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
1543 put_packet(gdb_ctx->s, "E22");
1544 return;
1545 }
1546
1547 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[0].thread_id.pid,
1548 gdb_ctx->params[0].thread_id.tid);
1549 if (!cpu) {
1550 put_packet(gdb_ctx->s, "E22");
1551 return;
1552 }
1553
1554 put_packet(gdb_ctx->s, "OK");
1555}
1556
1557static void handle_continue(GdbCmdContext *gdb_ctx, void *user_ctx)
1558{
1559 if (gdb_ctx->num_params) {
1560 gdb_set_cpu_pc(gdb_ctx->s, gdb_ctx->params[0].val_ull);
1561 }
1562
1563 gdb_ctx->s->signal = 0;
1564 gdb_continue(gdb_ctx->s);
1565}
1566
1567static void handle_cont_with_sig(GdbCmdContext *gdb_ctx, void *user_ctx)
1568{
1569 unsigned long signal = 0;
1570
1571
1572
1573
1574
1575 if (gdb_ctx->num_params) {
1576 signal = gdb_ctx->params[0].val_ul;
1577 }
1578
1579 gdb_ctx->s->signal = gdb_signal_to_target(signal);
1580 if (gdb_ctx->s->signal == -1) {
1581 gdb_ctx->s->signal = 0;
1582 }
1583 gdb_continue(gdb_ctx->s);
1584}
1585
1586static void handle_set_thread(GdbCmdContext *gdb_ctx, void *user_ctx)
1587{
1588 CPUState *cpu;
1589
1590 if (gdb_ctx->num_params != 2) {
1591 put_packet(gdb_ctx->s, "E22");
1592 return;
1593 }
1594
1595 if (gdb_ctx->params[1].thread_id.kind == GDB_READ_THREAD_ERR) {
1596 put_packet(gdb_ctx->s, "E22");
1597 return;
1598 }
1599
1600 if (gdb_ctx->params[1].thread_id.kind != GDB_ONE_THREAD) {
1601 put_packet(gdb_ctx->s, "OK");
1602 return;
1603 }
1604
1605 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[1].thread_id.pid,
1606 gdb_ctx->params[1].thread_id.tid);
1607 if (!cpu) {
1608 put_packet(gdb_ctx->s, "E22");
1609 return;
1610 }
1611
1612
1613
1614
1615
1616 switch (gdb_ctx->params[0].opcode) {
1617 case 'c':
1618 gdb_ctx->s->c_cpu = cpu;
1619 put_packet(gdb_ctx->s, "OK");
1620 break;
1621 case 'g':
1622 gdb_ctx->s->g_cpu = cpu;
1623 put_packet(gdb_ctx->s, "OK");
1624 break;
1625 default:
1626 put_packet(gdb_ctx->s, "E22");
1627 break;
1628 }
1629}
1630
1631static void handle_insert_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1632{
1633 int res;
1634
1635 if (gdb_ctx->num_params != 3) {
1636 put_packet(gdb_ctx->s, "E22");
1637 return;
1638 }
1639
1640 res = gdb_breakpoint_insert(gdb_ctx->params[0].val_ul,
1641 gdb_ctx->params[1].val_ull,
1642 gdb_ctx->params[2].val_ull);
1643 if (res >= 0) {
1644 put_packet(gdb_ctx->s, "OK");
1645 return;
1646 } else if (res == -ENOSYS) {
1647 put_packet(gdb_ctx->s, "");
1648 return;
1649 }
1650
1651 put_packet(gdb_ctx->s, "E22");
1652}
1653
1654static void handle_remove_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1655{
1656 int res;
1657
1658 if (gdb_ctx->num_params != 3) {
1659 put_packet(gdb_ctx->s, "E22");
1660 return;
1661 }
1662
1663 res = gdb_breakpoint_remove(gdb_ctx->params[0].val_ul,
1664 gdb_ctx->params[1].val_ull,
1665 gdb_ctx->params[2].val_ull);
1666 if (res >= 0) {
1667 put_packet(gdb_ctx->s, "OK");
1668 return;
1669 } else if (res == -ENOSYS) {
1670 put_packet(gdb_ctx->s, "");
1671 return;
1672 }
1673
1674 put_packet(gdb_ctx->s, "E22");
1675}
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688static void handle_set_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1689{
1690 int reg_size;
1691
1692 if (!gdb_has_xml) {
1693 put_packet(gdb_ctx->s, "");
1694 return;
1695 }
1696
1697 if (gdb_ctx->num_params != 2) {
1698 put_packet(gdb_ctx->s, "E22");
1699 return;
1700 }
1701
1702 reg_size = strlen(gdb_ctx->params[1].data) / 2;
1703 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[1].data, reg_size);
1704 gdb_write_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf,
1705 gdb_ctx->params[0].val_ull);
1706 put_packet(gdb_ctx->s, "OK");
1707}
1708
1709static void handle_get_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1710{
1711 int reg_size;
1712
1713 if (!gdb_has_xml) {
1714 put_packet(gdb_ctx->s, "");
1715 return;
1716 }
1717
1718 if (!gdb_ctx->num_params) {
1719 put_packet(gdb_ctx->s, "E14");
1720 return;
1721 }
1722
1723 reg_size = gdb_read_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf,
1724 gdb_ctx->params[0].val_ull);
1725 if (!reg_size) {
1726 put_packet(gdb_ctx->s, "E14");
1727 return;
1728 }
1729
1730 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, reg_size);
1731 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1732}
1733
1734static void handle_write_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1735{
1736 if (gdb_ctx->num_params != 3) {
1737 put_packet(gdb_ctx->s, "E22");
1738 return;
1739 }
1740
1741
1742 if (gdb_ctx->params[1].val_ull > strlen(gdb_ctx->params[2].data) / 2) {
1743 put_packet(gdb_ctx->s, "E22");
1744 return;
1745 }
1746
1747 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[2].data,
1748 gdb_ctx->params[1].val_ull);
1749 if (target_memory_rw_debug(gdb_ctx->s->g_cpu, gdb_ctx->params[0].val_ull,
1750 gdb_ctx->mem_buf,
1751 gdb_ctx->params[1].val_ull, true)) {
1752 put_packet(gdb_ctx->s, "E14");
1753 return;
1754 }
1755
1756 put_packet(gdb_ctx->s, "OK");
1757}
1758
1759static void handle_read_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1760{
1761 if (gdb_ctx->num_params != 2) {
1762 put_packet(gdb_ctx->s, "E22");
1763 return;
1764 }
1765
1766
1767 if (gdb_ctx->params[1].val_ull > MAX_PACKET_LENGTH / 2) {
1768 put_packet(gdb_ctx->s, "E22");
1769 return;
1770 }
1771
1772 if (target_memory_rw_debug(gdb_ctx->s->g_cpu, gdb_ctx->params[0].val_ull,
1773 gdb_ctx->mem_buf,
1774 gdb_ctx->params[1].val_ull, false)) {
1775 put_packet(gdb_ctx->s, "E14");
1776 return;
1777 }
1778
1779 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, gdb_ctx->params[1].val_ull);
1780 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1781}
1782
1783static void handle_write_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1784{
1785 target_ulong addr, len;
1786 uint8_t *registers;
1787 int reg_size;
1788
1789 if (!gdb_ctx->num_params) {
1790 return;
1791 }
1792
1793 cpu_synchronize_state(gdb_ctx->s->g_cpu);
1794 registers = gdb_ctx->mem_buf;
1795 len = strlen(gdb_ctx->params[0].data) / 2;
1796 hextomem(registers, gdb_ctx->params[0].data, len);
1797 for (addr = 0; addr < gdb_ctx->s->g_cpu->gdb_num_g_regs && len > 0;
1798 addr++) {
1799 reg_size = gdb_write_register(gdb_ctx->s->g_cpu, registers, addr);
1800 len -= reg_size;
1801 registers += reg_size;
1802 }
1803 put_packet(gdb_ctx->s, "OK");
1804}
1805
1806static void handle_read_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1807{
1808 target_ulong addr, len;
1809
1810 cpu_synchronize_state(gdb_ctx->s->g_cpu);
1811 len = 0;
1812 for (addr = 0; addr < gdb_ctx->s->g_cpu->gdb_num_g_regs; addr++) {
1813 len += gdb_read_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf + len,
1814 addr);
1815 }
1816
1817 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, len);
1818 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1819}
1820
1821static void handle_file_io(GdbCmdContext *gdb_ctx, void *user_ctx)
1822{
1823 if (gdb_ctx->num_params >= 1 && gdb_ctx->s->current_syscall_cb) {
1824 target_ulong ret, err;
1825
1826 ret = (target_ulong)gdb_ctx->params[0].val_ull;
1827 if (gdb_ctx->num_params >= 2) {
1828 err = (target_ulong)gdb_ctx->params[1].val_ull;
1829 } else {
1830 err = 0;
1831 }
1832 gdb_ctx->s->current_syscall_cb(gdb_ctx->s->c_cpu, ret, err);
1833 gdb_ctx->s->current_syscall_cb = NULL;
1834 }
1835
1836 if (gdb_ctx->num_params >= 3 && gdb_ctx->params[2].opcode == (uint8_t)'C') {
1837 put_packet(gdb_ctx->s, "T02");
1838 return;
1839 }
1840
1841 gdb_continue(gdb_ctx->s);
1842}
1843
1844static void handle_step(GdbCmdContext *gdb_ctx, void *user_ctx)
1845{
1846 if (gdb_ctx->num_params) {
1847 gdb_set_cpu_pc(gdb_ctx->s, (target_ulong)gdb_ctx->params[0].val_ull);
1848 }
1849
1850 cpu_single_step(gdb_ctx->s->c_cpu, sstep_flags);
1851 gdb_continue(gdb_ctx->s);
1852}
1853
1854static void handle_v_cont_query(GdbCmdContext *gdb_ctx, void *user_ctx)
1855{
1856 put_packet(gdb_ctx->s, "vCont;c;C;s;S");
1857}
1858
1859static void handle_v_cont(GdbCmdContext *gdb_ctx, void *user_ctx)
1860{
1861 int res;
1862
1863 if (!gdb_ctx->num_params) {
1864 return;
1865 }
1866
1867 res = gdb_handle_vcont(gdb_ctx->s, gdb_ctx->params[0].data);
1868 if ((res == -EINVAL) || (res == -ERANGE)) {
1869 put_packet(gdb_ctx->s, "E22");
1870 } else if (res) {
1871 put_packet(gdb_ctx->s, "");
1872 }
1873}
1874
1875static void handle_v_attach(GdbCmdContext *gdb_ctx, void *user_ctx)
1876{
1877 GDBProcess *process;
1878 CPUState *cpu;
1879 char thread_id[16];
1880
1881 pstrcpy(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "E22");
1882 if (!gdb_ctx->num_params) {
1883 goto cleanup;
1884 }
1885
1886 process = gdb_get_process(gdb_ctx->s, gdb_ctx->params[0].val_ul);
1887 if (!process) {
1888 goto cleanup;
1889 }
1890
1891 cpu = get_first_cpu_in_process(gdb_ctx->s, process);
1892 if (!cpu) {
1893 goto cleanup;
1894 }
1895
1896 process->attached = true;
1897 gdb_ctx->s->g_cpu = cpu;
1898 gdb_ctx->s->c_cpu = cpu;
1899
1900 gdb_fmt_thread_id(gdb_ctx->s, cpu, thread_id, sizeof(thread_id));
1901 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "T%02xthread:%s;",
1902 GDB_SIGNAL_TRAP, thread_id);
1903cleanup:
1904 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1905}
1906
1907static void handle_v_kill(GdbCmdContext *gdb_ctx, void *user_ctx)
1908{
1909
1910 put_packet(gdb_ctx->s, "OK");
1911 error_report("QEMU: Terminated via GDBstub");
1912 exit(0);
1913}
1914
1915static GdbCmdParseEntry gdb_v_commands_table[] = {
1916
1917 {
1918 .handler = handle_v_cont_query,
1919 .cmd = "Cont?",
1920 .cmd_startswith = 1
1921 },
1922 {
1923 .handler = handle_v_cont,
1924 .cmd = "Cont",
1925 .cmd_startswith = 1,
1926 .schema = "s0"
1927 },
1928 {
1929 .handler = handle_v_attach,
1930 .cmd = "Attach;",
1931 .cmd_startswith = 1,
1932 .schema = "l0"
1933 },
1934 {
1935 .handler = handle_v_kill,
1936 .cmd = "Kill;",
1937 .cmd_startswith = 1
1938 },
1939};
1940
1941static void handle_v_commands(GdbCmdContext *gdb_ctx, void *user_ctx)
1942{
1943 if (!gdb_ctx->num_params) {
1944 return;
1945 }
1946
1947 if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
1948 gdb_v_commands_table,
1949 ARRAY_SIZE(gdb_v_commands_table))) {
1950 put_packet(gdb_ctx->s, "");
1951 }
1952}
1953
1954static void handle_query_qemu_sstepbits(GdbCmdContext *gdb_ctx, void *user_ctx)
1955{
1956 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
1957 "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", SSTEP_ENABLE,
1958 SSTEP_NOIRQ, SSTEP_NOTIMER);
1959 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1960}
1961
1962static void handle_set_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
1963{
1964 if (!gdb_ctx->num_params) {
1965 return;
1966 }
1967
1968 sstep_flags = gdb_ctx->params[0].val_ul;
1969 put_packet(gdb_ctx->s, "OK");
1970}
1971
1972static void handle_query_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
1973{
1974 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "0x%x", sstep_flags);
1975 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1976}
1977
1978static void handle_query_curr_tid(GdbCmdContext *gdb_ctx, void *user_ctx)
1979{
1980 CPUState *cpu;
1981 GDBProcess *process;
1982 char thread_id[16];
1983
1984
1985
1986
1987
1988
1989 process = gdb_get_cpu_process(gdb_ctx->s, gdb_ctx->s->g_cpu);
1990 cpu = get_first_cpu_in_process(gdb_ctx->s, process);
1991 gdb_fmt_thread_id(gdb_ctx->s, cpu, thread_id, sizeof(thread_id));
1992 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "QC%s", thread_id);
1993 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1994}
1995
1996static void handle_query_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
1997{
1998 char thread_id[16];
1999
2000 if (!gdb_ctx->s->query_cpu) {
2001 put_packet(gdb_ctx->s, "l");
2002 return;
2003 }
2004
2005 gdb_fmt_thread_id(gdb_ctx->s, gdb_ctx->s->query_cpu, thread_id,
2006 sizeof(thread_id));
2007 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "m%s", thread_id);
2008 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2009 gdb_ctx->s->query_cpu =
2010 gdb_next_attached_cpu(gdb_ctx->s, gdb_ctx->s->query_cpu);
2011}
2012
2013static void handle_query_first_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
2014{
2015 gdb_ctx->s->query_cpu = gdb_first_attached_cpu(gdb_ctx->s);
2016 handle_query_threads(gdb_ctx, user_ctx);
2017}
2018
2019static void handle_query_thread_extra(GdbCmdContext *gdb_ctx, void *user_ctx)
2020{
2021 CPUState *cpu;
2022 int len;
2023
2024 if (!gdb_ctx->num_params ||
2025 gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
2026 put_packet(gdb_ctx->s, "E22");
2027 return;
2028 }
2029
2030 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[0].thread_id.pid,
2031 gdb_ctx->params[0].thread_id.tid);
2032 if (!cpu) {
2033 return;
2034 }
2035
2036 cpu_synchronize_state(cpu);
2037
2038 if (gdb_ctx->s->multiprocess && (gdb_ctx->s->process_num > 1)) {
2039
2040 ObjectClass *oc = object_get_class(OBJECT(cpu));
2041 const char *cpu_model = object_class_get_name(oc);
2042 char *cpu_name = object_get_canonical_path_component(OBJECT(cpu));
2043
2044 if (cpu->gdb_id) {
2045 len = snprintf((char *)gdb_ctx->mem_buf, sizeof(gdb_ctx->str_buf) / 2,
2046 "%s [%s]", cpu->gdb_id,
2047 cpu->halted ? "halted " : "running");
2048 } else {
2049 len = snprintf((char *)gdb_ctx->mem_buf, sizeof(gdb_ctx->str_buf) / 2,
2050 "%s %s [%s]", cpu_model, cpu_name,
2051 cpu->halted ? "halted " : "running");
2052 }
2053 g_free(cpu_name);
2054 } else {
2055
2056 len = snprintf((char *)gdb_ctx->mem_buf, sizeof(gdb_ctx->str_buf) / 2,
2057 "CPU#%d [%s]", cpu->cpu_index,
2058 cpu->halted ? "halted " : "running");
2059 }
2060 trace_gdbstub_op_extra_info((char *)gdb_ctx->mem_buf);
2061 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, len);
2062 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2063}
2064
2065#ifdef CONFIG_USER_ONLY
2066static void handle_query_offsets(GdbCmdContext *gdb_ctx, void *user_ctx)
2067{
2068 TaskState *ts;
2069
2070 ts = gdb_ctx->s->c_cpu->opaque;
2071 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
2072 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
2073 ";Bss=" TARGET_ABI_FMT_lx,
2074 ts->info->code_offset,
2075 ts->info->data_offset,
2076 ts->info->data_offset);
2077 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2078}
2079#else
2080static void handle_query_rcmd(GdbCmdContext *gdb_ctx, void *user_ctx)
2081{
2082 int len;
2083
2084 if (!gdb_ctx->num_params) {
2085 put_packet(gdb_ctx->s, "E22");
2086 return;
2087 }
2088
2089 len = strlen(gdb_ctx->params[0].data);
2090 if (len % 2) {
2091 put_packet(gdb_ctx->s, "E01");
2092 return;
2093 }
2094
2095 len = len / 2;
2096 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[0].data, len);
2097 gdb_ctx->mem_buf[len++] = 0;
2098 qemu_chr_be_write(gdb_ctx->s->mon_chr, gdb_ctx->mem_buf, len);
2099 put_packet(gdb_ctx->s, "OK");
2100
2101}
2102#endif
2103
2104static void handle_query_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2105{
2106 CPUClass *cc;
2107
2108 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "PacketSize=%x",
2109 MAX_PACKET_LENGTH);
2110 cc = CPU_GET_CLASS(first_cpu);
2111 if (cc->gdb_core_xml_file) {
2112 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
2113 ";qXfer:features:read+");
2114 }
2115
2116 if (gdb_ctx->num_params &&
2117 strstr(gdb_ctx->params[0].data, "multiprocess+")) {
2118 gdb_ctx->s->multiprocess = true;
2119 }
2120
2121 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), ";multiprocess+");
2122 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2123}
2124
2125static void handle_query_xfer_features(GdbCmdContext *gdb_ctx, void *user_ctx)
2126{
2127 GDBProcess *process;
2128 CPUClass *cc;
2129 unsigned long len, total_len, addr;
2130 const char *xml;
2131 const char *p;
2132
2133 if (gdb_ctx->num_params < 3) {
2134 put_packet(gdb_ctx->s, "E22");
2135 return;
2136 }
2137
2138 process = gdb_get_cpu_process(gdb_ctx->s, gdb_ctx->s->g_cpu);
2139 cc = CPU_GET_CLASS(gdb_ctx->s->g_cpu);
2140 if (!cc->gdb_core_xml_file) {
2141 put_packet(gdb_ctx->s, "");
2142 return;
2143 }
2144
2145 gdb_has_xml = true;
2146 p = gdb_ctx->params[0].data;
2147 xml = get_feature_xml(gdb_ctx->s, p, &p, process);
2148 if (!xml) {
2149 put_packet(gdb_ctx->s, "E00");
2150 return;
2151 }
2152
2153 addr = gdb_ctx->params[1].val_ul;
2154 len = gdb_ctx->params[2].val_ul;
2155 total_len = strlen(xml);
2156 if (addr > total_len) {
2157 put_packet(gdb_ctx->s, "E00");
2158 return;
2159 }
2160
2161 if (len > (MAX_PACKET_LENGTH - 5) / 2) {
2162 len = (MAX_PACKET_LENGTH - 5) / 2;
2163 }
2164
2165 if (len < total_len - addr) {
2166 gdb_ctx->str_buf[0] = 'm';
2167 len = memtox(gdb_ctx->str_buf + 1, xml + addr, len);
2168 } else {
2169 gdb_ctx->str_buf[0] = 'l';
2170 len = memtox(gdb_ctx->str_buf + 1, xml + addr, total_len - addr);
2171 }
2172
2173 put_packet_binary(gdb_ctx->s, gdb_ctx->str_buf, len + 1, true);
2174}
2175
2176static void handle_query_attached(GdbCmdContext *gdb_ctx, void *user_ctx)
2177{
2178 put_packet(gdb_ctx->s, GDB_ATTACHED);
2179}
2180
2181static void handle_query_qemu_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2182{
2183 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "sstepbits;sstep");
2184#ifndef CONFIG_USER_ONLY
2185 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), ";PhyMemMode");
2186#endif
2187 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2188}
2189
2190#ifndef CONFIG_USER_ONLY
2191static void handle_query_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx,
2192 void *user_ctx)
2193{
2194 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "%d", phy_memory_mode);
2195 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2196}
2197
2198static void handle_set_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx, void *user_ctx)
2199{
2200 if (!gdb_ctx->num_params) {
2201 put_packet(gdb_ctx->s, "E22");
2202 return;
2203 }
2204
2205 if (!gdb_ctx->params[0].val_ul) {
2206 phy_memory_mode = 0;
2207 } else {
2208 phy_memory_mode = 1;
2209 }
2210 put_packet(gdb_ctx->s, "OK");
2211}
2212#endif
2213
2214static GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
2215
2216 {
2217 .handler = handle_query_qemu_sstepbits,
2218 .cmd = "qemu.sstepbits",
2219 },
2220 {
2221 .handler = handle_query_qemu_sstep,
2222 .cmd = "qemu.sstep",
2223 },
2224 {
2225 .handler = handle_set_qemu_sstep,
2226 .cmd = "qemu.sstep=",
2227 .cmd_startswith = 1,
2228 .schema = "l0"
2229 },
2230};
2231
2232static GdbCmdParseEntry gdb_gen_query_table[] = {
2233 {
2234 .handler = handle_query_curr_tid,
2235 .cmd = "C",
2236 },
2237 {
2238 .handler = handle_query_threads,
2239 .cmd = "sThreadInfo",
2240 },
2241 {
2242 .handler = handle_query_first_threads,
2243 .cmd = "fThreadInfo",
2244 },
2245 {
2246 .handler = handle_query_thread_extra,
2247 .cmd = "ThreadExtraInfo,",
2248 .cmd_startswith = 1,
2249 .schema = "t0"
2250 },
2251#ifdef CONFIG_USER_ONLY
2252 {
2253 .handler = handle_query_offsets,
2254 .cmd = "Offsets",
2255 },
2256#else
2257 {
2258 .handler = handle_query_rcmd,
2259 .cmd = "Rcmd,",
2260 .cmd_startswith = 1,
2261 .schema = "s0"
2262 },
2263#endif
2264 {
2265 .handler = handle_query_supported,
2266 .cmd = "Supported:",
2267 .cmd_startswith = 1,
2268 .schema = "s0"
2269 },
2270 {
2271 .handler = handle_query_supported,
2272 .cmd = "Supported",
2273 .schema = "s0"
2274 },
2275 {
2276 .handler = handle_query_xfer_features,
2277 .cmd = "Xfer:features:read:",
2278 .cmd_startswith = 1,
2279 .schema = "s:l,l0"
2280 },
2281 {
2282 .handler = handle_query_attached,
2283 .cmd = "Attached:",
2284 .cmd_startswith = 1
2285 },
2286 {
2287 .handler = handle_query_attached,
2288 .cmd = "Attached",
2289 },
2290 {
2291 .handler = handle_query_qemu_supported,
2292 .cmd = "qemu.Supported",
2293 },
2294#ifndef CONFIG_USER_ONLY
2295 {
2296 .handler = handle_query_qemu_phy_mem_mode,
2297 .cmd = "qemu.PhyMemMode",
2298 },
2299#endif
2300};
2301
2302static GdbCmdParseEntry gdb_gen_set_table[] = {
2303
2304 {
2305 .handler = handle_set_qemu_sstep,
2306 .cmd = "qemu.sstep:",
2307 .cmd_startswith = 1,
2308 .schema = "l0"
2309 },
2310#ifndef CONFIG_USER_ONLY
2311 {
2312 .handler = handle_set_qemu_phy_mem_mode,
2313 .cmd = "qemu.PhyMemMode:",
2314 .cmd_startswith = 1,
2315 .schema = "l0"
2316 },
2317#endif
2318};
2319
2320static void handle_gen_query(GdbCmdContext *gdb_ctx, void *user_ctx)
2321{
2322 if (!gdb_ctx->num_params) {
2323 return;
2324 }
2325
2326 if (!process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2327 gdb_gen_query_set_common_table,
2328 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2329 return;
2330 }
2331
2332 if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2333 gdb_gen_query_table,
2334 ARRAY_SIZE(gdb_gen_query_table))) {
2335 put_packet(gdb_ctx->s, "");
2336 }
2337}
2338
2339static void handle_gen_set(GdbCmdContext *gdb_ctx, void *user_ctx)
2340{
2341 if (!gdb_ctx->num_params) {
2342 return;
2343 }
2344
2345 if (!process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2346 gdb_gen_query_set_common_table,
2347 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2348 return;
2349 }
2350
2351 if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2352 gdb_gen_set_table,
2353 ARRAY_SIZE(gdb_gen_set_table))) {
2354 put_packet(gdb_ctx->s, "");
2355 }
2356}
2357
2358static void handle_target_halt(GdbCmdContext *gdb_ctx, void *user_ctx)
2359{
2360 char thread_id[16];
2361
2362 gdb_fmt_thread_id(gdb_ctx->s, gdb_ctx->s->c_cpu, thread_id,
2363 sizeof(thread_id));
2364 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "T%02xthread:%s;",
2365 GDB_SIGNAL_TRAP, thread_id);
2366 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2367
2368
2369
2370
2371
2372 gdb_breakpoint_remove_all();
2373}
2374
2375static int gdb_handle_packet(GDBState *s, const char *line_buf)
2376{
2377 const GdbCmdParseEntry *cmd_parser = NULL;
2378
2379 trace_gdbstub_io_command(line_buf);
2380
2381 switch (line_buf[0]) {
2382 case '!':
2383 put_packet(s, "OK");
2384 break;
2385 case '?':
2386 {
2387 static const GdbCmdParseEntry target_halted_cmd_desc = {
2388 .handler = handle_target_halt,
2389 .cmd = "?",
2390 .cmd_startswith = 1
2391 };
2392 cmd_parser = &target_halted_cmd_desc;
2393 }
2394 break;
2395 case 'c':
2396 {
2397 static const GdbCmdParseEntry continue_cmd_desc = {
2398 .handler = handle_continue,
2399 .cmd = "c",
2400 .cmd_startswith = 1,
2401 .schema = "L0"
2402 };
2403 cmd_parser = &continue_cmd_desc;
2404 }
2405 break;
2406 case 'C':
2407 {
2408 static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
2409 .handler = handle_cont_with_sig,
2410 .cmd = "C",
2411 .cmd_startswith = 1,
2412 .schema = "l0"
2413 };
2414 cmd_parser = &cont_with_sig_cmd_desc;
2415 }
2416 break;
2417 case 'v':
2418 {
2419 static const GdbCmdParseEntry v_cmd_desc = {
2420 .handler = handle_v_commands,
2421 .cmd = "v",
2422 .cmd_startswith = 1,
2423 .schema = "s0"
2424 };
2425 cmd_parser = &v_cmd_desc;
2426 }
2427 break;
2428 case 'k':
2429
2430 error_report("QEMU: Terminated via GDBstub");
2431 exit(0);
2432 case 'D':
2433 {
2434 static const GdbCmdParseEntry detach_cmd_desc = {
2435 .handler = handle_detach,
2436 .cmd = "D",
2437 .cmd_startswith = 1,
2438 .schema = "?.l0"
2439 };
2440 cmd_parser = &detach_cmd_desc;
2441 }
2442 break;
2443 case 's':
2444 {
2445 static const GdbCmdParseEntry step_cmd_desc = {
2446 .handler = handle_step,
2447 .cmd = "s",
2448 .cmd_startswith = 1,
2449 .schema = "L0"
2450 };
2451 cmd_parser = &step_cmd_desc;
2452 }
2453 break;
2454 case 'F':
2455 {
2456 static const GdbCmdParseEntry file_io_cmd_desc = {
2457 .handler = handle_file_io,
2458 .cmd = "F",
2459 .cmd_startswith = 1,
2460 .schema = "L,L,o0"
2461 };
2462 cmd_parser = &file_io_cmd_desc;
2463 }
2464 break;
2465 case 'g':
2466 {
2467 static const GdbCmdParseEntry read_all_regs_cmd_desc = {
2468 .handler = handle_read_all_regs,
2469 .cmd = "g",
2470 .cmd_startswith = 1
2471 };
2472 cmd_parser = &read_all_regs_cmd_desc;
2473 }
2474 break;
2475 case 'G':
2476 {
2477 static const GdbCmdParseEntry write_all_regs_cmd_desc = {
2478 .handler = handle_write_all_regs,
2479 .cmd = "G",
2480 .cmd_startswith = 1,
2481 .schema = "s0"
2482 };
2483 cmd_parser = &write_all_regs_cmd_desc;
2484 }
2485 break;
2486 case 'm':
2487 {
2488 static const GdbCmdParseEntry read_mem_cmd_desc = {
2489 .handler = handle_read_mem,
2490 .cmd = "m",
2491 .cmd_startswith = 1,
2492 .schema = "L,L0"
2493 };
2494 cmd_parser = &read_mem_cmd_desc;
2495 }
2496 break;
2497 case 'M':
2498 {
2499 static const GdbCmdParseEntry write_mem_cmd_desc = {
2500 .handler = handle_write_mem,
2501 .cmd = "M",
2502 .cmd_startswith = 1,
2503 .schema = "L,L:s0"
2504 };
2505 cmd_parser = &write_mem_cmd_desc;
2506 }
2507 break;
2508 case 'p':
2509 {
2510 static const GdbCmdParseEntry get_reg_cmd_desc = {
2511 .handler = handle_get_reg,
2512 .cmd = "p",
2513 .cmd_startswith = 1,
2514 .schema = "L0"
2515 };
2516 cmd_parser = &get_reg_cmd_desc;
2517 }
2518 break;
2519 case 'P':
2520 {
2521 static const GdbCmdParseEntry set_reg_cmd_desc = {
2522 .handler = handle_set_reg,
2523 .cmd = "P",
2524 .cmd_startswith = 1,
2525 .schema = "L?s0"
2526 };
2527 cmd_parser = &set_reg_cmd_desc;
2528 }
2529 break;
2530 case 'Z':
2531 {
2532 static const GdbCmdParseEntry insert_bp_cmd_desc = {
2533 .handler = handle_insert_bp,
2534 .cmd = "Z",
2535 .cmd_startswith = 1,
2536 .schema = "l?L?L0"
2537 };
2538 cmd_parser = &insert_bp_cmd_desc;
2539 }
2540 break;
2541 case 'z':
2542 {
2543 static const GdbCmdParseEntry remove_bp_cmd_desc = {
2544 .handler = handle_remove_bp,
2545 .cmd = "z",
2546 .cmd_startswith = 1,
2547 .schema = "l?L?L0"
2548 };
2549 cmd_parser = &remove_bp_cmd_desc;
2550 }
2551 break;
2552 case 'H':
2553 {
2554 static const GdbCmdParseEntry set_thread_cmd_desc = {
2555 .handler = handle_set_thread,
2556 .cmd = "H",
2557 .cmd_startswith = 1,
2558 .schema = "o.t0"
2559 };
2560 cmd_parser = &set_thread_cmd_desc;
2561 }
2562 break;
2563 case 'T':
2564 {
2565 static const GdbCmdParseEntry thread_alive_cmd_desc = {
2566 .handler = handle_thread_alive,
2567 .cmd = "T",
2568 .cmd_startswith = 1,
2569 .schema = "t0"
2570 };
2571 cmd_parser = &thread_alive_cmd_desc;
2572 }
2573 break;
2574 case 'q':
2575 {
2576 static const GdbCmdParseEntry gen_query_cmd_desc = {
2577 .handler = handle_gen_query,
2578 .cmd = "q",
2579 .cmd_startswith = 1,
2580 .schema = "s0"
2581 };
2582 cmd_parser = &gen_query_cmd_desc;
2583 }
2584 break;
2585 case 'Q':
2586 {
2587 static const GdbCmdParseEntry gen_set_cmd_desc = {
2588 .handler = handle_gen_set,
2589 .cmd = "Q",
2590 .cmd_startswith = 1,
2591 .schema = "s0"
2592 };
2593 cmd_parser = &gen_set_cmd_desc;
2594 }
2595 break;
2596 default:
2597
2598 put_packet(s, "");
2599 break;
2600 }
2601
2602 if (cmd_parser) {
2603 run_cmd_parser(s, line_buf, cmd_parser);
2604 }
2605
2606 return RS_IDLE;
2607}
2608
2609void gdb_set_stop_cpu(CPUState *cpu)
2610{
2611 GDBProcess *p = gdb_get_cpu_process(gdbserver_state, cpu);
2612
2613 if (!p->attached) {
2614
2615
2616
2617
2618 return;
2619 }
2620
2621 gdbserver_state->c_cpu = cpu;
2622 gdbserver_state->g_cpu = cpu;
2623}
2624
2625#ifndef CONFIG_USER_ONLY
2626static void gdb_vm_state_change(void *opaque, int running, RunState state)
2627{
2628 GDBState *s = gdbserver_state;
2629 CPUState *cpu = s->c_cpu;
2630 char buf[256];
2631 char thread_id[16];
2632 const char *type;
2633 int ret;
2634
2635 if (running || s->state == RS_INACTIVE) {
2636 return;
2637 }
2638
2639 if (s->current_syscall_cb) {
2640 put_packet(s, s->syscall_buf);
2641 return;
2642 }
2643
2644 if (cpu == NULL) {
2645
2646 return;
2647 }
2648
2649 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id));
2650
2651 switch (state) {
2652 case RUN_STATE_DEBUG:
2653 if (cpu->watchpoint_hit) {
2654 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
2655 case BP_MEM_READ:
2656 type = "r";
2657 break;
2658 case BP_MEM_ACCESS:
2659 type = "a";
2660 break;
2661 default:
2662 type = "";
2663 break;
2664 }
2665 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
2666 (target_ulong)cpu->watchpoint_hit->vaddr);
2667 snprintf(buf, sizeof(buf),
2668 "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
2669 GDB_SIGNAL_TRAP, thread_id, type,
2670 (target_ulong)cpu->watchpoint_hit->vaddr);
2671 cpu->watchpoint_hit = NULL;
2672 goto send_packet;
2673 } else {
2674 trace_gdbstub_hit_break();
2675 }
2676 tb_flush(cpu);
2677 ret = GDB_SIGNAL_TRAP;
2678 break;
2679 case RUN_STATE_PAUSED:
2680 trace_gdbstub_hit_paused();
2681 ret = GDB_SIGNAL_INT;
2682 break;
2683 case RUN_STATE_SHUTDOWN:
2684 trace_gdbstub_hit_shutdown();
2685 ret = GDB_SIGNAL_QUIT;
2686 break;
2687 case RUN_STATE_IO_ERROR:
2688 trace_gdbstub_hit_io_error();
2689 ret = GDB_SIGNAL_IO;
2690 break;
2691 case RUN_STATE_WATCHDOG:
2692 trace_gdbstub_hit_watchdog();
2693 ret = GDB_SIGNAL_ALRM;
2694 break;
2695 case RUN_STATE_INTERNAL_ERROR:
2696 trace_gdbstub_hit_internal_error();
2697 ret = GDB_SIGNAL_ABRT;
2698 break;
2699 case RUN_STATE_SAVE_VM:
2700 case RUN_STATE_RESTORE_VM:
2701 return;
2702 case RUN_STATE_FINISH_MIGRATE:
2703 ret = GDB_SIGNAL_XCPU;
2704 break;
2705 default:
2706 trace_gdbstub_hit_unknown(state);
2707 ret = GDB_SIGNAL_UNKNOWN;
2708 break;
2709 }
2710 gdb_set_stop_cpu(cpu);
2711 snprintf(buf, sizeof(buf), "T%02xthread:%s;", ret, thread_id);
2712
2713send_packet:
2714 put_packet(s, buf);
2715
2716
2717 cpu_single_step(cpu, 0);
2718}
2719#endif
2720
2721
2722
2723
2724
2725
2726void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
2727{
2728 char *p;
2729 char *p_end;
2730 target_ulong addr;
2731 uint64_t i64;
2732 GDBState *s;
2733
2734 s = gdbserver_state;
2735 if (!s)
2736 return;
2737 s->current_syscall_cb = cb;
2738#ifndef CONFIG_USER_ONLY
2739 vm_stop(RUN_STATE_DEBUG);
2740#endif
2741 p = s->syscall_buf;
2742 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
2743 *(p++) = 'F';
2744 while (*fmt) {
2745 if (*fmt == '%') {
2746 fmt++;
2747 switch (*fmt++) {
2748 case 'x':
2749 addr = va_arg(va, target_ulong);
2750 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
2751 break;
2752 case 'l':
2753 if (*(fmt++) != 'x')
2754 goto bad_format;
2755 i64 = va_arg(va, uint64_t);
2756 p += snprintf(p, p_end - p, "%" PRIx64, i64);
2757 break;
2758 case 's':
2759 addr = va_arg(va, target_ulong);
2760 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
2761 addr, va_arg(va, int));
2762 break;
2763 default:
2764 bad_format:
2765 error_report("gdbstub: Bad syscall format string '%s'",
2766 fmt - 1);
2767 break;
2768 }
2769 } else {
2770 *(p++) = *(fmt++);
2771 }
2772 }
2773 *p = 0;
2774#ifdef CONFIG_USER_ONLY
2775 put_packet(s, s->syscall_buf);
2776
2777
2778
2779
2780
2781
2782 gdb_handlesig(s->c_cpu, 0);
2783#else
2784
2785
2786
2787
2788
2789
2790 qemu_cpu_kick(s->c_cpu);
2791#endif
2792}
2793
2794void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
2795{
2796 va_list va;
2797
2798 va_start(va, fmt);
2799 gdb_do_syscallv(cb, fmt, va);
2800 va_end(va);
2801}
2802
2803static void gdb_read_byte(GDBState *s, uint8_t ch)
2804{
2805 uint8_t reply;
2806
2807#ifndef CONFIG_USER_ONLY
2808 if (s->last_packet_len) {
2809
2810
2811 if (ch == '-') {
2812 trace_gdbstub_err_got_nack();
2813 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
2814 } else if (ch == '+') {
2815 trace_gdbstub_io_got_ack();
2816 } else {
2817 trace_gdbstub_io_got_unexpected(ch);
2818 }
2819
2820 if (ch == '+' || ch == '$')
2821 s->last_packet_len = 0;
2822 if (ch != '$')
2823 return;
2824 }
2825 if (runstate_is_running()) {
2826
2827
2828 vm_stop(RUN_STATE_PAUSED);
2829 } else
2830#endif
2831 {
2832 switch(s->state) {
2833 case RS_IDLE:
2834 if (ch == '$') {
2835
2836 s->line_buf_index = 0;
2837 s->line_sum = 0;
2838 s->state = RS_GETLINE;
2839 } else {
2840 trace_gdbstub_err_garbage(ch);
2841 }
2842 break;
2843 case RS_GETLINE:
2844 if (ch == '}') {
2845
2846 s->state = RS_GETLINE_ESC;
2847 s->line_sum += ch;
2848 } else if (ch == '*') {
2849
2850 s->state = RS_GETLINE_RLE;
2851 s->line_sum += ch;
2852 } else if (ch == '#') {
2853
2854 s->state = RS_CHKSUM1;
2855 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2856 trace_gdbstub_err_overrun();
2857 s->state = RS_IDLE;
2858 } else {
2859
2860 s->line_buf[s->line_buf_index++] = ch;
2861 s->line_sum += ch;
2862 }
2863 break;
2864 case RS_GETLINE_ESC:
2865 if (ch == '#') {
2866
2867 s->state = RS_CHKSUM1;
2868 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2869
2870 trace_gdbstub_err_overrun();
2871 s->state = RS_IDLE;
2872 } else {
2873
2874 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
2875 s->line_sum += ch;
2876 s->state = RS_GETLINE;
2877 }
2878 break;
2879 case RS_GETLINE_RLE:
2880
2881
2882
2883
2884 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
2885
2886 trace_gdbstub_err_invalid_repeat(ch);
2887 s->state = RS_GETLINE;
2888 } else {
2889
2890 int repeat = ch - ' ' + 3;
2891 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
2892
2893 trace_gdbstub_err_overrun();
2894 s->state = RS_IDLE;
2895 } else if (s->line_buf_index < 1) {
2896
2897 trace_gdbstub_err_invalid_rle();
2898 s->state = RS_GETLINE;
2899 } else {
2900
2901 memset(s->line_buf + s->line_buf_index,
2902 s->line_buf[s->line_buf_index - 1], repeat);
2903 s->line_buf_index += repeat;
2904 s->line_sum += ch;
2905 s->state = RS_GETLINE;
2906 }
2907 }
2908 break;
2909 case RS_CHKSUM1:
2910
2911 if (!isxdigit(ch)) {
2912 trace_gdbstub_err_checksum_invalid(ch);
2913 s->state = RS_GETLINE;
2914 break;
2915 }
2916 s->line_buf[s->line_buf_index] = '\0';
2917 s->line_csum = fromhex(ch) << 4;
2918 s->state = RS_CHKSUM2;
2919 break;
2920 case RS_CHKSUM2:
2921
2922 if (!isxdigit(ch)) {
2923 trace_gdbstub_err_checksum_invalid(ch);
2924 s->state = RS_GETLINE;
2925 break;
2926 }
2927 s->line_csum |= fromhex(ch);
2928
2929 if (s->line_csum != (s->line_sum & 0xff)) {
2930 trace_gdbstub_err_checksum_incorrect(s->line_sum, s->line_csum);
2931
2932 reply = '-';
2933 put_buffer(s, &reply, 1);
2934 s->state = RS_IDLE;
2935 } else {
2936
2937 reply = '+';
2938 put_buffer(s, &reply, 1);
2939 s->state = gdb_handle_packet(s, s->line_buf);
2940 }
2941 break;
2942 default:
2943 abort();
2944 }
2945 }
2946}
2947
2948
2949void gdb_exit(CPUArchState *env, int code)
2950{
2951 GDBState *s;
2952 char buf[4];
2953
2954 s = gdbserver_state;
2955 if (!s) {
2956 return;
2957 }
2958#ifdef CONFIG_USER_ONLY
2959 if (gdbserver_fd < 0 || s->fd < 0) {
2960 return;
2961 }
2962#endif
2963
2964 trace_gdbstub_op_exiting((uint8_t)code);
2965
2966 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
2967 put_packet(s, buf);
2968
2969#ifndef CONFIG_USER_ONLY
2970 qemu_chr_fe_deinit(&s->chr, true);
2971#endif
2972}
2973
2974
2975
2976
2977
2978
2979static void create_default_process(GDBState *s)
2980{
2981 GDBProcess *process;
2982 int max_pid = 0;
2983
2984 if (s->process_num) {
2985 max_pid = s->processes[s->process_num - 1].pid;
2986 }
2987
2988 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2989 process = &s->processes[s->process_num - 1];
2990
2991
2992 assert(max_pid < UINT32_MAX);
2993
2994 process->pid = max_pid + 1;
2995 process->attached = false;
2996 process->target_xml[0] = '\0';
2997}
2998
2999#ifdef CONFIG_USER_ONLY
3000int
3001gdb_handlesig(CPUState *cpu, int sig)
3002{
3003 GDBState *s;
3004 char buf[256];
3005 int n;
3006
3007 s = gdbserver_state;
3008 if (gdbserver_fd < 0 || s->fd < 0) {
3009 return sig;
3010 }
3011
3012
3013 cpu_single_step(cpu, 0);
3014 tb_flush(cpu);
3015
3016 if (sig != 0) {
3017 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
3018 put_packet(s, buf);
3019 }
3020
3021
3022 if (s->fd < 0) {
3023 return sig;
3024 }
3025
3026 sig = 0;
3027 s->state = RS_IDLE;
3028 s->running_state = 0;
3029 while (s->running_state == 0) {
3030 n = read(s->fd, buf, 256);
3031 if (n > 0) {
3032 int i;
3033
3034 for (i = 0; i < n; i++) {
3035 gdb_read_byte(s, buf[i]);
3036 }
3037 } else {
3038
3039
3040 if (n == 0) {
3041 close(s->fd);
3042 }
3043 s->fd = -1;
3044 return sig;
3045 }
3046 }
3047 sig = s->signal;
3048 s->signal = 0;
3049 return sig;
3050}
3051
3052
3053void gdb_signalled(CPUArchState *env, int sig)
3054{
3055 GDBState *s;
3056 char buf[4];
3057
3058 s = gdbserver_state;
3059 if (gdbserver_fd < 0 || s->fd < 0) {
3060 return;
3061 }
3062
3063 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
3064 put_packet(s, buf);
3065}
3066
3067static bool gdb_accept(void)
3068{
3069 GDBState *s;
3070 struct sockaddr_in sockaddr;
3071 socklen_t len;
3072 int fd;
3073
3074 for(;;) {
3075 len = sizeof(sockaddr);
3076 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
3077 if (fd < 0 && errno != EINTR) {
3078 perror("accept");
3079 return false;
3080 } else if (fd >= 0) {
3081 qemu_set_cloexec(fd);
3082 break;
3083 }
3084 }
3085
3086
3087 if (socket_set_nodelay(fd)) {
3088 perror("setsockopt");
3089 close(fd);
3090 return false;
3091 }
3092
3093 s = g_malloc0(sizeof(GDBState));
3094 create_default_process(s);
3095 s->processes[0].attached = true;
3096 s->c_cpu = gdb_first_attached_cpu(s);
3097 s->g_cpu = s->c_cpu;
3098 s->fd = fd;
3099 gdb_has_xml = false;
3100
3101 gdbserver_state = s;
3102 return true;
3103}
3104
3105static int gdbserver_open(int port)
3106{
3107 struct sockaddr_in sockaddr;
3108 int fd, ret;
3109
3110 fd = socket(PF_INET, SOCK_STREAM, 0);
3111 if (fd < 0) {
3112 perror("socket");
3113 return -1;
3114 }
3115 qemu_set_cloexec(fd);
3116
3117 socket_set_fast_reuse(fd);
3118
3119 sockaddr.sin_family = AF_INET;
3120 sockaddr.sin_port = htons(port);
3121 sockaddr.sin_addr.s_addr = 0;
3122 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
3123 if (ret < 0) {
3124 perror("bind");
3125 close(fd);
3126 return -1;
3127 }
3128 ret = listen(fd, 1);
3129 if (ret < 0) {
3130 perror("listen");
3131 close(fd);
3132 return -1;
3133 }
3134 return fd;
3135}
3136
3137int gdbserver_start(int port)
3138{
3139 gdbserver_fd = gdbserver_open(port);
3140 if (gdbserver_fd < 0)
3141 return -1;
3142
3143 if (!gdb_accept()) {
3144 close(gdbserver_fd);
3145 gdbserver_fd = -1;
3146 return -1;
3147 }
3148 return 0;
3149}
3150
3151
3152void gdbserver_fork(CPUState *cpu)
3153{
3154 GDBState *s = gdbserver_state;
3155
3156 if (gdbserver_fd < 0 || s->fd < 0) {
3157 return;
3158 }
3159 close(s->fd);
3160 s->fd = -1;
3161 cpu_breakpoint_remove_all(cpu, BP_GDB);
3162 cpu_watchpoint_remove_all(cpu, BP_GDB);
3163}
3164#else
3165static int gdb_chr_can_receive(void *opaque)
3166{
3167
3168
3169 return MAX_PACKET_LENGTH;
3170}
3171
3172static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
3173{
3174 int i;
3175
3176 for (i = 0; i < size; i++) {
3177 gdb_read_byte(gdbserver_state, buf[i]);
3178 }
3179}
3180
3181static void gdb_chr_event(void *opaque, int event)
3182{
3183 int i;
3184 GDBState *s = (GDBState *) opaque;
3185
3186 switch (event) {
3187 case CHR_EVENT_OPENED:
3188
3189 for (i = 0; i < s->process_num; i++) {
3190 s->processes[i].attached = !i;
3191 }
3192
3193 s->c_cpu = gdb_first_attached_cpu(s);
3194 s->g_cpu = s->c_cpu;
3195
3196 vm_stop(RUN_STATE_PAUSED);
3197 gdb_has_xml = false;
3198 break;
3199 default:
3200 break;
3201 }
3202}
3203
3204static void gdb_monitor_output(GDBState *s, const char *msg, int len)
3205{
3206 char buf[MAX_PACKET_LENGTH];
3207
3208 buf[0] = 'O';
3209 if (len > (MAX_PACKET_LENGTH/2) - 1)
3210 len = (MAX_PACKET_LENGTH/2) - 1;
3211 memtohex(buf + 1, (uint8_t *)msg, len);
3212 put_packet(s, buf);
3213}
3214
3215static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
3216{
3217 const char *p = (const char *)buf;
3218 int max_sz;
3219
3220 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
3221 for (;;) {
3222 if (len <= max_sz) {
3223 gdb_monitor_output(gdbserver_state, p, len);
3224 break;
3225 }
3226 gdb_monitor_output(gdbserver_state, p, max_sz);
3227 p += max_sz;
3228 len -= max_sz;
3229 }
3230 return len;
3231}
3232
3233#ifndef _WIN32
3234static void gdb_sigterm_handler(int signal)
3235{
3236 if (runstate_is_running()) {
3237 vm_stop(RUN_STATE_PAUSED);
3238 }
3239}
3240#endif
3241
3242static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
3243 bool *be_opened, Error **errp)
3244{
3245 *be_opened = false;
3246}
3247
3248static void char_gdb_class_init(ObjectClass *oc, void *data)
3249{
3250 ChardevClass *cc = CHARDEV_CLASS(oc);
3251
3252 cc->internal = true;
3253 cc->open = gdb_monitor_open;
3254 cc->chr_write = gdb_monitor_write;
3255}
3256
3257#define TYPE_CHARDEV_GDB "chardev-gdb"
3258
3259static const TypeInfo char_gdb_type_info = {
3260 .name = TYPE_CHARDEV_GDB,
3261 .parent = TYPE_CHARDEV,
3262 .class_init = char_gdb_class_init,
3263};
3264
3265static int find_cpu_clusters(Object *child, void *opaque)
3266{
3267 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
3268 GDBState *s = (GDBState *) opaque;
3269 CPUClusterState *cluster = CPU_CLUSTER(child);
3270 GDBProcess *process;
3271
3272 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
3273
3274 process = &s->processes[s->process_num - 1];
3275
3276
3277
3278
3279
3280
3281 assert(cluster->cluster_id != UINT32_MAX);
3282 process->pid = cluster->cluster_id + 1;
3283 process->attached = false;
3284 process->target_xml[0] = '\0';
3285
3286 return 0;
3287 }
3288
3289 return object_child_foreach(child, find_cpu_clusters, opaque);
3290}
3291
3292static int pid_order(const void *a, const void *b)
3293{
3294 GDBProcess *pa = (GDBProcess *) a;
3295 GDBProcess *pb = (GDBProcess *) b;
3296
3297 if (pa->pid < pb->pid) {
3298 return -1;
3299 } else if (pa->pid > pb->pid) {
3300 return 1;
3301 } else {
3302 return 0;
3303 }
3304}
3305
3306static void create_processes(GDBState *s)
3307{
3308 object_child_foreach(object_get_root(), find_cpu_clusters, s);
3309
3310 if (s->processes) {
3311
3312 qsort(s->processes, s->process_num, sizeof(s->processes[0]), pid_order);
3313 }
3314
3315 create_default_process(s);
3316}
3317
3318static void cleanup_processes(GDBState *s)
3319{
3320 g_free(s->processes);
3321 s->process_num = 0;
3322 s->processes = NULL;
3323}
3324
3325int gdbserver_start(const char *device)
3326{
3327 trace_gdbstub_op_start(device);
3328
3329 GDBState *s;
3330 char gdbstub_device_name[128];
3331 Chardev *chr = NULL;
3332 Chardev *mon_chr;
3333
3334 if (!first_cpu) {
3335 error_report("gdbstub: meaningless to attach gdb to a "
3336 "machine without any CPU.");
3337 return -1;
3338 }
3339
3340 if (!device)
3341 return -1;
3342 if (strcmp(device, "none") != 0) {
3343 if (strstart(device, "tcp:", NULL)) {
3344
3345 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
3346 "%s,nowait,nodelay,server", device);
3347 device = gdbstub_device_name;
3348 }
3349#ifndef _WIN32
3350 else if (strcmp(device, "stdio") == 0) {
3351 struct sigaction act;
3352
3353 memset(&act, 0, sizeof(act));
3354 act.sa_handler = gdb_sigterm_handler;
3355 sigaction(SIGINT, &act, NULL);
3356 }
3357#endif
3358
3359
3360
3361
3362 chr = qemu_chr_new_noreplay("gdb", device, true, NULL);
3363 if (!chr)
3364 return -1;
3365 }
3366
3367 s = gdbserver_state;
3368 if (!s) {
3369 s = g_malloc0(sizeof(GDBState));
3370 gdbserver_state = s;
3371
3372 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
3373
3374
3375 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
3376 NULL, NULL, &error_abort);
3377 monitor_init_hmp(mon_chr, false);
3378 } else {
3379 qemu_chr_fe_deinit(&s->chr, true);
3380 mon_chr = s->mon_chr;
3381 cleanup_processes(s);
3382 memset(s, 0, sizeof(GDBState));
3383 s->mon_chr = mon_chr;
3384 }
3385
3386 create_processes(s);
3387
3388 if (chr) {
3389 qemu_chr_fe_init(&s->chr, chr, &error_abort);
3390 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
3391 gdb_chr_event, NULL, s, NULL, true);
3392 }
3393 s->state = chr ? RS_IDLE : RS_INACTIVE;
3394 s->mon_chr = mon_chr;
3395 s->current_syscall_cb = NULL;
3396
3397 return 0;
3398}
3399
3400void gdbserver_cleanup(void)
3401{
3402 if (gdbserver_state) {
3403 put_packet(gdbserver_state, "W00");
3404 }
3405}
3406
3407static void register_types(void)
3408{
3409 type_register_static(&char_gdb_type_info);
3410}
3411
3412type_init(register_types);
3413#endif
3414