4 * Copyright (c) 2003-2005 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
22 #include "qapi/error.h"
23 #include "qemu/error-report.h"
24 #include "qemu/ctype.h"
25 #include "qemu/cutils.h"
26 #include "qemu/module.h"
27 #include "trace-root.h"
28 #ifdef CONFIG_USER_ONLY
31 #include "monitor/monitor.h"
32 #include "chardev/char.h"
33 #include "chardev/char-fe.h"
34 #include "sysemu/sysemu.h"
35 #include "exec/gdbstub.h"
36 #include "hw/cpu/cluster.h"
37 #include "hw/boards.h"
40 #define MAX_PACKET_LENGTH 4096
42 #include "qemu/sockets.h"
43 #include "sysemu/hw_accel.h"
44 #include "sysemu/kvm.h"
45 #include "hw/semihosting/semihost.h"
46 #include "exec/exec-all.h"
48 #ifdef CONFIG_USER_ONLY
49 #define GDB_ATTACHED "0"
51 #define GDB_ATTACHED "1"
54 #ifndef CONFIG_USER_ONLY
55 static int phy_memory_mode;
58 static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
59 uint8_t *buf, int len, bool is_write)
63 #ifndef CONFIG_USER_ONLY
64 if (phy_memory_mode) {
66 cpu_physical_memory_write(addr, buf, len);
68 cpu_physical_memory_read(addr, buf, len);
74 cc = CPU_GET_CLASS(cpu);
75 if (cc->memory_rw_debug) {
76 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
78 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
81 /* Return the GDB index for a given vCPU state.
83 * For user mode this is simply the thread id. In system mode GDB
84 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
86 static inline int cpu_gdb_index(CPUState *cpu)
88 #if defined(CONFIG_USER_ONLY)
89 TaskState *ts = (TaskState *) cpu->opaque;
92 return cpu->cpu_index + 1;
102 GDB_SIGNAL_ALRM = 14,
104 GDB_SIGNAL_XCPU = 24,
105 GDB_SIGNAL_UNKNOWN = 143
108 #ifdef CONFIG_USER_ONLY
110 /* Map target signal numbers to GDB protocol signal numbers and vice
111 * versa. For user emulation's currently supported systems, we can
112 * assume most signals are defined.
115 static int gdb_signal_table[] = {
275 /* In system mode we only need SIGINT and SIGTRAP; other signals
276 are not yet supported. */
283 static int gdb_signal_table[] = {
293 #ifdef CONFIG_USER_ONLY
294 static int target_signal_to_gdb (int sig)
297 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
298 if (gdb_signal_table[i] == sig)
300 return GDB_SIGNAL_UNKNOWN;
304 static int gdb_signal_to_target (int sig)
306 if (sig < ARRAY_SIZE (gdb_signal_table))
307 return gdb_signal_table[sig];
312 typedef struct GDBRegisterState {
318 struct GDBRegisterState *next;
321 typedef struct GDBProcess {
325 char target_xml[1024];
337 typedef struct GDBState {
338 CPUState *c_cpu; /* current CPU for step/continue ops */
339 CPUState *g_cpu; /* current CPU for other ops */
340 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
341 enum RSState state; /* parsing state */
342 char line_buf[MAX_PACKET_LENGTH];
344 int line_sum; /* running checksum */
345 int line_csum; /* checksum at the end of the packet */
346 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
349 #ifdef CONFIG_USER_ONLY
357 GDBProcess *processes;
359 char syscall_buf[256];
360 gdb_syscall_complete_cb current_syscall_cb;
363 /* By default use no IRQs and no timers while single stepping so as to
364 * make single stepping like an ICE HW step.
366 static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
368 static GDBState *gdbserver_state;
372 #ifdef CONFIG_USER_ONLY
373 /* XXX: This is not thread safe. Do we care? */
374 static int gdbserver_fd = -1;
376 static int get_char(GDBState *s)
382 ret = qemu_recv(s->fd, &ch, 1, 0);
384 if (errno == ECONNRESET)
388 } else if (ret == 0) {
406 /* Decide if either remote gdb syscalls or native file IO should be used. */
407 int use_gdb_syscalls(void)
409 SemihostingTarget target = semihosting_get_target();
410 if (target == SEMIHOSTING_TARGET_NATIVE) {
411 /* -semihosting-config target=native */
413 } else if (target == SEMIHOSTING_TARGET_GDB) {
414 /* -semihosting-config target=gdb */
418 /* -semihosting-config target=auto */
419 /* On the first call check if gdb is connected and remember. */
420 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
421 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
424 return gdb_syscall_mode == GDB_SYS_ENABLED;
427 /* Resume execution. */
428 static inline void gdb_continue(GDBState *s)
431 #ifdef CONFIG_USER_ONLY
432 s->running_state = 1;
433 trace_gdbstub_op_continue();
435 if (!runstate_needs_reset()) {
436 trace_gdbstub_op_continue();
443 * Resume execution, per CPU actions. For user-mode emulation it's
444 * equivalent to gdb_continue.
446 static int gdb_continue_partial(GDBState *s, char *newstates)
450 #ifdef CONFIG_USER_ONLY
452 * This is not exactly accurate, but it's an improvement compared to the
453 * previous situation, where only one CPU would be single-stepped.
456 if (newstates[cpu->cpu_index] == 's') {
457 trace_gdbstub_op_stepping(cpu->cpu_index);
458 cpu_single_step(cpu, sstep_flags);
461 s->running_state = 1;
465 if (!runstate_needs_reset()) {
466 if (vm_prepare_start()) {
471 switch (newstates[cpu->cpu_index]) {
474 break; /* nothing to do here */
476 trace_gdbstub_op_stepping(cpu->cpu_index);
477 cpu_single_step(cpu, sstep_flags);
482 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
493 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
499 static void put_buffer(GDBState *s, const uint8_t *buf, int len)
501 #ifdef CONFIG_USER_ONLY
505 ret = send(s->fd, buf, len, 0);
515 /* XXX this blocks entire thread. Rewrite to use
516 * qemu_chr_fe_write and background I/O callbacks */
517 qemu_chr_fe_write_all(&s->chr, buf, len);
521 static inline int fromhex(int v)
523 if (v >= '0' && v <= '9')
525 else if (v >= 'A' && v <= 'F')
527 else if (v >= 'a' && v <= 'f')
533 static inline int tohex(int v)
541 /* writes 2*len+1 bytes in buf */
542 static void memtohex(char *buf, const uint8_t *mem, int len)
547 for(i = 0; i < len; i++) {
549 *q++ = tohex(c >> 4);
550 *q++ = tohex(c & 0xf);
555 static void hextomem(uint8_t *mem, const char *buf, int len)
559 for(i = 0; i < len; i++) {
560 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
565 static void hexdump(const char *buf, int len,
566 void (*trace_fn)(size_t ofs, char const *text))
568 char line_buffer[3 * 16 + 4 + 16 + 1];
571 for (i = 0; i < len || (i & 0xF); ++i) {
572 size_t byte_ofs = i & 15;
575 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
576 line_buffer[3 * 16 + 4 + 16] = 0;
579 size_t col_group = (i >> 2) & 3;
580 size_t hex_col = byte_ofs * 3 + col_group;
581 size_t txt_col = 3 * 16 + 4 + byte_ofs;
586 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
587 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
588 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
594 trace_fn(i & -16, line_buffer);
598 /* return -1 if error, 0 if OK */
599 static int put_packet_binary(GDBState *s, const char *buf, int len, bool dump)
604 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
605 hexdump(buf, len, trace_gdbstub_io_binaryreply);
614 for(i = 0; i < len; i++) {
618 *(p++) = tohex((csum >> 4) & 0xf);
619 *(p++) = tohex((csum) & 0xf);
621 s->last_packet_len = p - s->last_packet;
622 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
624 #ifdef CONFIG_USER_ONLY
637 /* return -1 if error, 0 if OK */
638 static int put_packet(GDBState *s, const char *buf)
640 trace_gdbstub_io_reply(buf);
642 return put_packet_binary(s, buf, strlen(buf), false);
645 /* Encode data using the encoding for 'x' packets. */
646 static int memtox(char *buf, const char *mem, int len)
654 case '#': case '$': case '*': case '}':
666 static uint32_t gdb_get_cpu_pid(const GDBState *s, CPUState *cpu)
668 /* TODO: In user mode, we should use the task state PID */
669 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
670 /* Return the default process' PID */
671 return s->processes[s->process_num - 1].pid;
673 return cpu->cluster_index + 1;
676 static GDBProcess *gdb_get_process(const GDBState *s, uint32_t pid)
681 /* 0 means any process, we take the first one */
682 return &s->processes[0];
685 for (i = 0; i < s->process_num; i++) {
686 if (s->processes[i].pid == pid) {
687 return &s->processes[i];
694 static GDBProcess *gdb_get_cpu_process(const GDBState *s, CPUState *cpu)
696 return gdb_get_process(s, gdb_get_cpu_pid(s, cpu));
699 static CPUState *find_cpu(uint32_t thread_id)
704 if (cpu_gdb_index(cpu) == thread_id) {
712 static CPUState *get_first_cpu_in_process(const GDBState *s,
718 if (gdb_get_cpu_pid(s, cpu) == process->pid) {
726 static CPUState *gdb_next_cpu_in_process(const GDBState *s, CPUState *cpu)
728 uint32_t pid = gdb_get_cpu_pid(s, cpu);
732 if (gdb_get_cpu_pid(s, cpu) == pid) {
742 /* Return the cpu following @cpu, while ignoring unattached processes. */
743 static CPUState *gdb_next_attached_cpu(const GDBState *s, CPUState *cpu)
748 if (gdb_get_cpu_process(s, cpu)->attached) {
758 /* Return the first attached cpu */
759 static CPUState *gdb_first_attached_cpu(const GDBState *s)
761 CPUState *cpu = first_cpu;
762 GDBProcess *process = gdb_get_cpu_process(s, cpu);
764 if (!process->attached) {
765 return gdb_next_attached_cpu(s, cpu);
771 static CPUState *gdb_get_cpu(const GDBState *s, uint32_t pid, uint32_t tid)
777 /* 0 means any process/thread, we take the first attached one */
778 return gdb_first_attached_cpu(s);
779 } else if (pid && !tid) {
780 /* any thread in a specific process */
781 process = gdb_get_process(s, pid);
783 if (process == NULL) {
787 if (!process->attached) {
791 return get_first_cpu_in_process(s, process);
793 /* a specific thread */
800 process = gdb_get_cpu_process(s, cpu);
802 if (pid && process->pid != pid) {
806 if (!process->attached) {
814 static const char *get_feature_xml(const GDBState *s, const char *p,
815 const char **newp, GDBProcess *process)
820 CPUState *cpu = get_first_cpu_in_process(s, process);
821 CPUClass *cc = CPU_GET_CLASS(cpu);
824 while (p[len] && p[len] != ':')
829 if (strncmp(p, "target.xml", len) == 0) {
830 char *buf = process->target_xml;
831 const size_t buf_sz = sizeof(process->target_xml);
833 /* Generate the XML description for this CPU. */
838 "<?xml version=\"1.0\"?>"
839 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
841 if (cc->gdb_arch_name) {
842 gchar *arch = cc->gdb_arch_name(cpu);
843 pstrcat(buf, buf_sz, "<architecture>");
844 pstrcat(buf, buf_sz, arch);
845 pstrcat(buf, buf_sz, "</architecture>");
848 pstrcat(buf, buf_sz, "<xi:include href=\"");
849 pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
850 pstrcat(buf, buf_sz, "\"/>");
851 for (r = cpu->gdb_regs; r; r = r->next) {
852 pstrcat(buf, buf_sz, "<xi:include href=\"");
853 pstrcat(buf, buf_sz, r->xml);
854 pstrcat(buf, buf_sz, "\"/>");
856 pstrcat(buf, buf_sz, "</target>");
860 if (cc->gdb_get_dynamic_xml) {
861 char *xmlname = g_strndup(p, len);
862 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
870 name = xml_builtin[i][0];
871 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
874 return name ? xml_builtin[i][1] : NULL;
877 static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
879 CPUClass *cc = CPU_GET_CLASS(cpu);
880 CPUArchState *env = cpu->env_ptr;
883 if (reg < cc->gdb_num_core_regs) {
884 return cc->gdb_read_register(cpu, mem_buf, reg);
887 for (r = cpu->gdb_regs; r; r = r->next) {
888 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
889 return r->get_reg(env, mem_buf, reg - r->base_reg);
895 static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
897 CPUClass *cc = CPU_GET_CLASS(cpu);
898 CPUArchState *env = cpu->env_ptr;
901 if (reg < cc->gdb_num_core_regs) {
902 return cc->gdb_write_register(cpu, mem_buf, reg);
905 for (r = cpu->gdb_regs; r; r = r->next) {
906 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
907 return r->set_reg(env, mem_buf, reg - r->base_reg);
913 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
914 specifies the first register number and these registers are included in
915 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
916 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
919 void gdb_register_coprocessor(CPUState *cpu,
920 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
921 int num_regs, const char *xml, int g_pos)
924 GDBRegisterState **p;
928 /* Check for duplicates. */
929 if (strcmp((*p)->xml, xml) == 0)
934 s = g_new0(GDBRegisterState, 1);
935 s->base_reg = cpu->gdb_num_regs;
936 s->num_regs = num_regs;
937 s->get_reg = get_reg;
938 s->set_reg = set_reg;
941 /* Add to end of list. */
942 cpu->gdb_num_regs += num_regs;
945 if (g_pos != s->base_reg) {
946 error_report("Error: Bad gdb register numbering for '%s', "
947 "expected %d got %d", xml, g_pos, s->base_reg);
949 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
954 #ifndef CONFIG_USER_ONLY
955 /* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
956 static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
958 static const int xlat[] = {
959 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
960 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
961 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
964 CPUClass *cc = CPU_GET_CLASS(cpu);
965 int cputype = xlat[gdbtype];
967 if (cc->gdb_stop_before_watchpoint) {
968 cputype |= BP_STOP_BEFORE_ACCESS;
974 static int gdb_breakpoint_insert(int type, target_ulong addr, target_ulong len)
980 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
984 case GDB_BREAKPOINT_SW:
985 case GDB_BREAKPOINT_HW:
987 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
993 #ifndef CONFIG_USER_ONLY
994 case GDB_WATCHPOINT_WRITE:
995 case GDB_WATCHPOINT_READ:
996 case GDB_WATCHPOINT_ACCESS:
998 err = cpu_watchpoint_insert(cpu, addr, len,
999 xlat_gdb_type(cpu, type), NULL);
1011 static int gdb_breakpoint_remove(int type, target_ulong addr, target_ulong len)
1016 if (kvm_enabled()) {
1017 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
1021 case GDB_BREAKPOINT_SW:
1022 case GDB_BREAKPOINT_HW:
1024 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
1030 #ifndef CONFIG_USER_ONLY
1031 case GDB_WATCHPOINT_WRITE:
1032 case GDB_WATCHPOINT_READ:
1033 case GDB_WATCHPOINT_ACCESS:
1035 err = cpu_watchpoint_remove(cpu, addr, len,
1036 xlat_gdb_type(cpu, type));
1047 static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu)
1049 cpu_breakpoint_remove_all(cpu, BP_GDB);
1050 #ifndef CONFIG_USER_ONLY
1051 cpu_watchpoint_remove_all(cpu, BP_GDB);
1055 static void gdb_process_breakpoint_remove_all(const GDBState *s, GDBProcess *p)
1057 CPUState *cpu = get_first_cpu_in_process(s, p);
1060 gdb_cpu_breakpoint_remove_all(cpu);
1061 cpu = gdb_next_cpu_in_process(s, cpu);
1065 static void gdb_breakpoint_remove_all(void)
1069 if (kvm_enabled()) {
1070 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
1075 gdb_cpu_breakpoint_remove_all(cpu);
1079 static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
1081 CPUState *cpu = s->c_cpu;
1083 cpu_synchronize_state(cpu);
1084 cpu_set_pc(cpu, pc);
1087 static char *gdb_fmt_thread_id(const GDBState *s, CPUState *cpu,
1088 char *buf, size_t buf_size)
1090 if (s->multiprocess) {
1091 snprintf(buf, buf_size, "p%02x.%02x",
1092 gdb_get_cpu_pid(s, cpu), cpu_gdb_index(cpu));
1094 snprintf(buf, buf_size, "%02x", cpu_gdb_index(cpu));
1100 typedef enum GDBThreadIdKind {
1102 GDB_ALL_THREADS, /* One process, all threads */
1107 static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1108 uint32_t *pid, uint32_t *tid)
1115 ret = qemu_strtoul(buf, &buf, 16, &p);
1118 return GDB_READ_THREAD_ERR;
1127 ret = qemu_strtoul(buf, &buf, 16, &t);
1130 return GDB_READ_THREAD_ERR;
1136 return GDB_ALL_PROCESSES;
1144 return GDB_ALL_THREADS;
1151 return GDB_ONE_THREAD;
1155 * gdb_handle_vcont - Parses and handles a vCont packet.
1156 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1157 * a format error, 0 on success.
1159 static int gdb_handle_vcont(GDBState *s, const char *p)
1161 int res, signal = 0;
1166 GDBProcess *process;
1168 GDBThreadIdKind kind;
1169 #ifdef CONFIG_USER_ONLY
1170 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
1173 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1176 MachineState *ms = MACHINE(qdev_get_machine());
1177 unsigned int max_cpus = ms->smp.max_cpus;
1179 /* uninitialised CPUs stay 0 */
1180 newstates = g_new0(char, max_cpus);
1182 /* mark valid CPUs with 1 */
1184 newstates[cpu->cpu_index] = 1;
1188 * res keeps track of what error we are returning, with -ENOTSUP meaning
1189 * that the command is unknown or unsupported, thus returning an empty
1190 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1191 * or incorrect parameters passed.
1201 if (cur_action == 'C' || cur_action == 'S') {
1202 cur_action = qemu_tolower(cur_action);
1203 res = qemu_strtoul(p + 1, &p, 16, &tmp);
1207 signal = gdb_signal_to_target(tmp);
1208 } else if (cur_action != 'c' && cur_action != 's') {
1209 /* unknown/invalid/unsupported command */
1214 if (*p == '\0' || *p == ';') {
1216 * No thread specifier, action is on "all threads". The
1217 * specification is unclear regarding the process to act on. We
1218 * choose all processes.
1220 kind = GDB_ALL_PROCESSES;
1221 } else if (*p++ == ':') {
1222 kind = read_thread_id(p, &p, &pid, &tid);
1229 case GDB_READ_THREAD_ERR:
1233 case GDB_ALL_PROCESSES:
1234 cpu = gdb_first_attached_cpu(s);
1236 if (newstates[cpu->cpu_index] == 1) {
1237 newstates[cpu->cpu_index] = cur_action;
1240 cpu = gdb_next_attached_cpu(s, cpu);
1244 case GDB_ALL_THREADS:
1245 process = gdb_get_process(s, pid);
1247 if (!process->attached) {
1252 cpu = get_first_cpu_in_process(s, process);
1254 if (newstates[cpu->cpu_index] == 1) {
1255 newstates[cpu->cpu_index] = cur_action;
1258 cpu = gdb_next_cpu_in_process(s, cpu);
1262 case GDB_ONE_THREAD:
1263 cpu = gdb_get_cpu(s, pid, tid);
1265 /* invalid CPU/thread specified */
1271 /* only use if no previous match occourred */
1272 if (newstates[cpu->cpu_index] == 1) {
1273 newstates[cpu->cpu_index] = cur_action;
1279 gdb_continue_partial(s, newstates);
1287 typedef union GdbCmdVariant {
1290 unsigned long val_ul;
1291 unsigned long long val_ull;
1293 GDBThreadIdKind kind;
1299 static const char *cmd_next_param(const char *param, const char delimiter)
1301 static const char all_delimiters[] = ",;:=";
1302 char curr_delimiters[2] = {0};
1303 const char *delimiters;
1305 if (delimiter == '?') {
1306 delimiters = all_delimiters;
1307 } else if (delimiter == '0') {
1308 return strchr(param, '\0');
1309 } else if (delimiter == '.' && *param) {
1312 curr_delimiters[0] = delimiter;
1313 delimiters = curr_delimiters;
1316 param += strcspn(param, delimiters);
1323 static int cmd_parse_params(const char *data, const char *schema,
1324 GdbCmdVariant *params, int *num_params)
1327 const char *curr_schema, *curr_data;
1335 curr_schema = schema;
1338 while (curr_schema[0] && curr_schema[1] && *curr_data) {
1339 switch (curr_schema[0]) {
1341 if (qemu_strtoul(curr_data, &curr_data, 16,
1342 ¶ms[curr_param].val_ul)) {
1346 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1349 if (qemu_strtou64(curr_data, &curr_data, 16,
1350 (uint64_t *)¶ms[curr_param].val_ull)) {
1354 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1357 params[curr_param].data = curr_data;
1359 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1362 params[curr_param].opcode = *(uint8_t *)curr_data;
1364 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1367 params[curr_param].thread_id.kind =
1368 read_thread_id(curr_data, &curr_data,
1369 ¶ms[curr_param].thread_id.pid,
1370 ¶ms[curr_param].thread_id.tid);
1372 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1375 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1383 *num_params = curr_param;
1387 typedef struct GdbCmdContext {
1389 GdbCmdVariant *params;
1391 uint8_t mem_buf[MAX_PACKET_LENGTH];
1392 char str_buf[MAX_PACKET_LENGTH + 1];
1395 typedef void (*GdbCmdHandler)(GdbCmdContext *gdb_ctx, void *user_ctx);
1398 * cmd_startswith -> cmd is compared using startswith
1401 * schema definitions:
1402 * Each schema parameter entry consists of 2 chars,
1403 * the first char represents the parameter type handling
1404 * the second char represents the delimiter for the next parameter
1406 * Currently supported schema types:
1407 * 'l' -> unsigned long (stored in .val_ul)
1408 * 'L' -> unsigned long long (stored in .val_ull)
1409 * 's' -> string (stored in .data)
1410 * 'o' -> single char (stored in .opcode)
1411 * 't' -> thread id (stored in .thread_id)
1412 * '?' -> skip according to delimiter
1414 * Currently supported delimiters:
1415 * '?' -> Stop at any delimiter (",;:=\0")
1416 * '0' -> Stop at "\0"
1417 * '.' -> Skip 1 char unless reached "\0"
1418 * Any other value is treated as the delimiter value itself
1420 typedef struct GdbCmdParseEntry {
1421 GdbCmdHandler handler;
1423 bool cmd_startswith;
1427 static inline int startswith(const char *string, const char *pattern)
1429 return !strncmp(string, pattern, strlen(pattern));
1432 static int process_string_cmd(GDBState *s, void *user_ctx, const char *data,
1433 const GdbCmdParseEntry *cmds, int num_cmds)
1435 int i, schema_len, max_num_params = 0;
1436 GdbCmdContext gdb_ctx;
1442 for (i = 0; i < num_cmds; i++) {
1443 const GdbCmdParseEntry *cmd = &cmds[i];
1444 g_assert(cmd->handler && cmd->cmd);
1446 if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
1447 (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
1452 schema_len = strlen(cmd->schema);
1453 if (schema_len % 2) {
1457 max_num_params = schema_len / 2;
1461 (GdbCmdVariant *)alloca(sizeof(*gdb_ctx.params) * max_num_params);
1462 memset(gdb_ctx.params, 0, sizeof(*gdb_ctx.params) * max_num_params);
1464 if (cmd_parse_params(&data[strlen(cmd->cmd)], cmd->schema,
1465 gdb_ctx.params, &gdb_ctx.num_params)) {
1470 cmd->handler(&gdb_ctx, user_ctx);
1477 static void run_cmd_parser(GDBState *s, const char *data,
1478 const GdbCmdParseEntry *cmd)
1484 /* In case there was an error during the command parsing we must
1485 * send a NULL packet to indicate the command is not supported */
1486 if (process_string_cmd(s, NULL, data, cmd, 1)) {
1491 static void handle_detach(GdbCmdContext *gdb_ctx, void *user_ctx)
1493 GDBProcess *process;
1494 GDBState *s = gdb_ctx->s;
1497 if (s->multiprocess) {
1498 if (!gdb_ctx->num_params) {
1499 put_packet(s, "E22");
1503 pid = gdb_ctx->params[0].val_ul;
1506 process = gdb_get_process(s, pid);
1507 gdb_process_breakpoint_remove_all(s, process);
1508 process->attached = false;
1510 if (pid == gdb_get_cpu_pid(s, s->c_cpu)) {
1511 s->c_cpu = gdb_first_attached_cpu(s);
1514 if (pid == gdb_get_cpu_pid(s, s->g_cpu)) {
1515 s->g_cpu = gdb_first_attached_cpu(s);
1519 /* No more process attached */
1520 gdb_syscall_mode = GDB_SYS_DISABLED;
1523 put_packet(s, "OK");
1526 static void handle_thread_alive(GdbCmdContext *gdb_ctx, void *user_ctx)
1530 if (!gdb_ctx->num_params) {
1531 put_packet(gdb_ctx->s, "E22");
1535 if (gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
1536 put_packet(gdb_ctx->s, "E22");
1540 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[0].thread_id.pid,
1541 gdb_ctx->params[0].thread_id.tid);
1543 put_packet(gdb_ctx->s, "E22");
1547 put_packet(gdb_ctx->s, "OK");
1550 static void handle_continue(GdbCmdContext *gdb_ctx, void *user_ctx)
1552 if (gdb_ctx->num_params) {
1553 gdb_set_cpu_pc(gdb_ctx->s, gdb_ctx->params[0].val_ull);
1556 gdb_ctx->s->signal = 0;
1557 gdb_continue(gdb_ctx->s);
1560 static void handle_cont_with_sig(GdbCmdContext *gdb_ctx, void *user_ctx)
1562 unsigned long signal = 0;
1565 * Note: C sig;[addr] is currently unsupported and we simply
1566 * omit the addr parameter
1568 if (gdb_ctx->num_params) {
1569 signal = gdb_ctx->params[0].val_ul;
1572 gdb_ctx->s->signal = gdb_signal_to_target(signal);
1573 if (gdb_ctx->s->signal == -1) {
1574 gdb_ctx->s->signal = 0;
1576 gdb_continue(gdb_ctx->s);
1579 static void handle_set_thread(GdbCmdContext *gdb_ctx, void *user_ctx)
1583 if (gdb_ctx->num_params != 2) {
1584 put_packet(gdb_ctx->s, "E22");
1588 if (gdb_ctx->params[1].thread_id.kind == GDB_READ_THREAD_ERR) {
1589 put_packet(gdb_ctx->s, "E22");
1593 if (gdb_ctx->params[1].thread_id.kind != GDB_ONE_THREAD) {
1594 put_packet(gdb_ctx->s, "OK");
1598 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[1].thread_id.pid,
1599 gdb_ctx->params[1].thread_id.tid);
1601 put_packet(gdb_ctx->s, "E22");
1606 * Note: This command is deprecated and modern gdb's will be using the
1607 * vCont command instead.
1609 switch (gdb_ctx->params[0].opcode) {
1611 gdb_ctx->s->c_cpu = cpu;
1612 put_packet(gdb_ctx->s, "OK");
1615 gdb_ctx->s->g_cpu = cpu;
1616 put_packet(gdb_ctx->s, "OK");
1619 put_packet(gdb_ctx->s, "E22");
1624 static void handle_insert_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1628 if (gdb_ctx->num_params != 3) {
1629 put_packet(gdb_ctx->s, "E22");
1633 res = gdb_breakpoint_insert(gdb_ctx->params[0].val_ul,
1634 gdb_ctx->params[1].val_ull,
1635 gdb_ctx->params[2].val_ull);
1637 put_packet(gdb_ctx->s, "OK");
1639 } else if (res == -ENOSYS) {
1640 put_packet(gdb_ctx->s, "");
1644 put_packet(gdb_ctx->s, "E22");
1647 static void handle_remove_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1651 if (gdb_ctx->num_params != 3) {
1652 put_packet(gdb_ctx->s, "E22");
1656 res = gdb_breakpoint_remove(gdb_ctx->params[0].val_ul,
1657 gdb_ctx->params[1].val_ull,
1658 gdb_ctx->params[2].val_ull);
1660 put_packet(gdb_ctx->s, "OK");
1662 } else if (res == -ENOSYS) {
1663 put_packet(gdb_ctx->s, "");
1667 put_packet(gdb_ctx->s, "E22");
1670 static void handle_set_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1675 put_packet(gdb_ctx->s, "E00");
1679 if (gdb_ctx->num_params != 2) {
1680 put_packet(gdb_ctx->s, "E22");
1684 reg_size = strlen(gdb_ctx->params[1].data) / 2;
1685 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[1].data, reg_size);
1686 gdb_write_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf,
1687 gdb_ctx->params[0].val_ull);
1688 put_packet(gdb_ctx->s, "OK");
1691 static void handle_get_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1696 * Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1697 * This works, but can be very slow. Anything new enough to
1698 * understand XML also knows how to use this properly.
1701 put_packet(gdb_ctx->s, "");
1705 if (!gdb_ctx->num_params) {
1706 put_packet(gdb_ctx->s, "E14");
1710 reg_size = gdb_read_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf,
1711 gdb_ctx->params[0].val_ull);
1713 put_packet(gdb_ctx->s, "E14");
1717 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, reg_size);
1718 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1721 static void handle_write_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1723 if (gdb_ctx->num_params != 3) {
1724 put_packet(gdb_ctx->s, "E22");
1728 /* hextomem() reads 2*len bytes */
1729 if (gdb_ctx->params[1].val_ull > strlen(gdb_ctx->params[2].data) / 2) {
1730 put_packet(gdb_ctx->s, "E22");
1734 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[2].data,
1735 gdb_ctx->params[1].val_ull);
1736 if (target_memory_rw_debug(gdb_ctx->s->g_cpu, gdb_ctx->params[0].val_ull,
1738 gdb_ctx->params[1].val_ull, true)) {
1739 put_packet(gdb_ctx->s, "E14");
1743 put_packet(gdb_ctx->s, "OK");
1746 static void handle_read_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1748 if (gdb_ctx->num_params != 2) {
1749 put_packet(gdb_ctx->s, "E22");
1753 /* memtohex() doubles the required space */
1754 if (gdb_ctx->params[1].val_ull > MAX_PACKET_LENGTH / 2) {
1755 put_packet(gdb_ctx->s, "E22");
1759 if (target_memory_rw_debug(gdb_ctx->s->g_cpu, gdb_ctx->params[0].val_ull,
1761 gdb_ctx->params[1].val_ull, false)) {
1762 put_packet(gdb_ctx->s, "E14");
1766 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, gdb_ctx->params[1].val_ull);
1767 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1770 static void handle_write_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1772 target_ulong addr, len;
1776 if (!gdb_ctx->num_params) {
1780 cpu_synchronize_state(gdb_ctx->s->g_cpu);
1781 registers = gdb_ctx->mem_buf;
1782 len = strlen(gdb_ctx->params[0].data) / 2;
1783 hextomem(registers, gdb_ctx->params[0].data, len);
1784 for (addr = 0; addr < gdb_ctx->s->g_cpu->gdb_num_g_regs && len > 0;
1786 reg_size = gdb_write_register(gdb_ctx->s->g_cpu, registers, addr);
1788 registers += reg_size;
1790 put_packet(gdb_ctx->s, "OK");
1793 static void handle_read_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1795 target_ulong addr, len;
1797 cpu_synchronize_state(gdb_ctx->s->g_cpu);
1799 for (addr = 0; addr < gdb_ctx->s->g_cpu->gdb_num_g_regs; addr++) {
1800 len += gdb_read_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf + len,
1804 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, len);
1805 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1808 static void handle_file_io(GdbCmdContext *gdb_ctx, void *user_ctx)
1810 if (gdb_ctx->num_params >= 2 && gdb_ctx->s->current_syscall_cb) {
1811 target_ulong ret, err;
1813 ret = (target_ulong)gdb_ctx->params[0].val_ull;
1814 err = (target_ulong)gdb_ctx->params[1].val_ull;
1815 gdb_ctx->s->current_syscall_cb(gdb_ctx->s->c_cpu, ret, err);
1816 gdb_ctx->s->current_syscall_cb = NULL;
1819 if (gdb_ctx->num_params >= 3 && gdb_ctx->params[2].opcode == (uint8_t)'C') {
1820 put_packet(gdb_ctx->s, "T02");
1824 gdb_continue(gdb_ctx->s);
1827 static void handle_step(GdbCmdContext *gdb_ctx, void *user_ctx)
1829 if (gdb_ctx->num_params) {
1830 gdb_set_cpu_pc(gdb_ctx->s, (target_ulong)gdb_ctx->params[0].val_ull);
1833 cpu_single_step(gdb_ctx->s->c_cpu, sstep_flags);
1834 gdb_continue(gdb_ctx->s);
1837 static void handle_v_cont_query(GdbCmdContext *gdb_ctx, void *user_ctx)
1839 put_packet(gdb_ctx->s, "vCont;c;C;s;S");
1842 static void handle_v_cont(GdbCmdContext *gdb_ctx, void *user_ctx)
1846 if (!gdb_ctx->num_params) {
1850 res = gdb_handle_vcont(gdb_ctx->s, gdb_ctx->params[0].data);
1851 if ((res == -EINVAL) || (res == -ERANGE)) {
1852 put_packet(gdb_ctx->s, "E22");
1854 put_packet(gdb_ctx->s, "");
1858 static void handle_v_attach(GdbCmdContext *gdb_ctx, void *user_ctx)
1860 GDBProcess *process;
1864 pstrcpy(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "E22");
1865 if (!gdb_ctx->num_params) {
1869 process = gdb_get_process(gdb_ctx->s, gdb_ctx->params[0].val_ul);
1874 cpu = get_first_cpu_in_process(gdb_ctx->s, process);
1879 process->attached = true;
1880 gdb_ctx->s->g_cpu = cpu;
1881 gdb_ctx->s->c_cpu = cpu;
1883 gdb_fmt_thread_id(gdb_ctx->s, cpu, thread_id, sizeof(thread_id));
1884 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "T%02xthread:%s;",
1885 GDB_SIGNAL_TRAP, thread_id);
1887 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1890 static void handle_v_kill(GdbCmdContext *gdb_ctx, void *user_ctx)
1892 /* Kill the target */
1893 put_packet(gdb_ctx->s, "OK");
1894 error_report("QEMU: Terminated via GDBstub");
1898 static GdbCmdParseEntry gdb_v_commands_table[] = {
1899 /* Order is important if has same prefix */
1901 .handler = handle_v_cont_query,
1906 .handler = handle_v_cont,
1908 .cmd_startswith = 1,
1912 .handler = handle_v_attach,
1914 .cmd_startswith = 1,
1918 .handler = handle_v_kill,
1924 static void handle_v_commands(GdbCmdContext *gdb_ctx, void *user_ctx)
1926 if (!gdb_ctx->num_params) {
1930 if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
1931 gdb_v_commands_table,
1932 ARRAY_SIZE(gdb_v_commands_table))) {
1933 put_packet(gdb_ctx->s, "");
1937 static void handle_query_qemu_sstepbits(GdbCmdContext *gdb_ctx, void *user_ctx)
1939 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
1940 "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", SSTEP_ENABLE,
1941 SSTEP_NOIRQ, SSTEP_NOTIMER);
1942 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1945 static void handle_set_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
1947 if (!gdb_ctx->num_params) {
1951 sstep_flags = gdb_ctx->params[0].val_ul;
1952 put_packet(gdb_ctx->s, "OK");
1955 static void handle_query_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
1957 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "0x%x", sstep_flags);
1958 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1961 static void handle_query_curr_tid(GdbCmdContext *gdb_ctx, void *user_ctx)
1964 GDBProcess *process;
1968 * "Current thread" remains vague in the spec, so always return
1969 * the first thread of the current process (gdb returns the
1972 process = gdb_get_cpu_process(gdb_ctx->s, gdb_ctx->s->g_cpu);
1973 cpu = get_first_cpu_in_process(gdb_ctx->s, process);
1974 gdb_fmt_thread_id(gdb_ctx->s, cpu, thread_id, sizeof(thread_id));
1975 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "QC%s", thread_id);
1976 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1979 static void handle_query_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
1983 if (!gdb_ctx->s->query_cpu) {
1984 put_packet(gdb_ctx->s, "l");
1988 gdb_fmt_thread_id(gdb_ctx->s, gdb_ctx->s->query_cpu, thread_id,
1990 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "m%s", thread_id);
1991 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1992 gdb_ctx->s->query_cpu =
1993 gdb_next_attached_cpu(gdb_ctx->s, gdb_ctx->s->query_cpu);
1996 static void handle_query_first_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
1998 gdb_ctx->s->query_cpu = gdb_first_attached_cpu(gdb_ctx->s);
1999 handle_query_threads(gdb_ctx, user_ctx);
2002 static void handle_query_thread_extra(GdbCmdContext *gdb_ctx, void *user_ctx)
2007 if (!gdb_ctx->num_params ||
2008 gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
2009 put_packet(gdb_ctx->s, "E22");
2013 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[0].thread_id.pid,
2014 gdb_ctx->params[0].thread_id.tid);
2019 cpu_synchronize_state(cpu);
2021 if (gdb_ctx->s->multiprocess && (gdb_ctx->s->process_num > 1)) {
2022 /* Print the CPU model and name in multiprocess mode */
2023 ObjectClass *oc = object_get_class(OBJECT(cpu));
2024 const char *cpu_model = object_class_get_name(oc);
2025 char *cpu_name = object_get_canonical_path_component(OBJECT(cpu));
2026 len = snprintf((char *)gdb_ctx->mem_buf, sizeof(gdb_ctx->str_buf) / 2,
2027 "%s %s [%s]", cpu_model, cpu_name,
2028 cpu->halted ? "halted " : "running");
2031 /* memtohex() doubles the required space */
2032 len = snprintf((char *)gdb_ctx->mem_buf, sizeof(gdb_ctx->str_buf) / 2,
2033 "CPU#%d [%s]", cpu->cpu_index,
2034 cpu->halted ? "halted " : "running");
2036 trace_gdbstub_op_extra_info((char *)gdb_ctx->mem_buf);
2037 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, len);
2038 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2041 #ifdef CONFIG_USER_ONLY
2042 static void handle_query_offsets(GdbCmdContext *gdb_ctx, void *user_ctx)
2046 ts = gdb_ctx->s->c_cpu->opaque;
2047 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
2048 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
2049 ";Bss=" TARGET_ABI_FMT_lx,
2050 ts->info->code_offset,
2051 ts->info->data_offset,
2052 ts->info->data_offset);
2053 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2056 static void handle_query_rcmd(GdbCmdContext *gdb_ctx, void *user_ctx)
2060 if (!gdb_ctx->num_params) {
2061 put_packet(gdb_ctx->s, "E22");
2065 len = strlen(gdb_ctx->params[0].data);
2067 put_packet(gdb_ctx->s, "E01");
2072 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[0].data, len);
2073 gdb_ctx->mem_buf[len++] = 0;
2074 qemu_chr_be_write(gdb_ctx->s->mon_chr, gdb_ctx->mem_buf, len);
2075 put_packet(gdb_ctx->s, "OK");
2080 static void handle_query_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2084 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "PacketSize=%x",
2086 cc = CPU_GET_CLASS(first_cpu);
2087 if (cc->gdb_core_xml_file) {
2088 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
2089 ";qXfer:features:read+");
2092 if (gdb_ctx->num_params &&
2093 strstr(gdb_ctx->params[0].data, "multiprocess+")) {
2094 gdb_ctx->s->multiprocess = true;
2097 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), ";multiprocess+");
2098 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2101 static void handle_query_xfer_features(GdbCmdContext *gdb_ctx, void *user_ctx)
2103 GDBProcess *process;
2105 unsigned long len, total_len, addr;
2109 if (gdb_ctx->num_params < 3) {
2110 put_packet(gdb_ctx->s, "E22");
2114 process = gdb_get_cpu_process(gdb_ctx->s, gdb_ctx->s->g_cpu);
2115 cc = CPU_GET_CLASS(gdb_ctx->s->g_cpu);
2116 if (!cc->gdb_core_xml_file) {
2117 put_packet(gdb_ctx->s, "");
2122 p = gdb_ctx->params[0].data;
2123 xml = get_feature_xml(gdb_ctx->s, p, &p, process);
2125 put_packet(gdb_ctx->s, "E00");
2129 addr = gdb_ctx->params[1].val_ul;
2130 len = gdb_ctx->params[2].val_ul;
2131 total_len = strlen(xml);
2132 if (addr > total_len) {
2133 put_packet(gdb_ctx->s, "E00");
2137 if (len > (MAX_PACKET_LENGTH - 5) / 2) {
2138 len = (MAX_PACKET_LENGTH - 5) / 2;
2141 if (len < total_len - addr) {
2142 gdb_ctx->str_buf[0] = 'm';
2143 len = memtox(gdb_ctx->str_buf + 1, xml + addr, len);
2145 gdb_ctx->str_buf[0] = 'l';
2146 len = memtox(gdb_ctx->str_buf + 1, xml + addr, total_len - addr);
2149 put_packet_binary(gdb_ctx->s, gdb_ctx->str_buf, len + 1, true);
2152 static void handle_query_attached(GdbCmdContext *gdb_ctx, void *user_ctx)
2154 put_packet(gdb_ctx->s, GDB_ATTACHED);
2157 static void handle_query_qemu_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2159 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "sstepbits;sstep");
2160 #ifndef CONFIG_USER_ONLY
2161 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), ";PhyMemMode");
2163 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2166 #ifndef CONFIG_USER_ONLY
2167 static void handle_query_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx,
2170 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "%d", phy_memory_mode);
2171 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2174 static void handle_set_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx, void *user_ctx)
2176 if (!gdb_ctx->num_params) {
2177 put_packet(gdb_ctx->s, "E22");
2181 if (!gdb_ctx->params[0].val_ul) {
2182 phy_memory_mode = 0;
2184 phy_memory_mode = 1;
2186 put_packet(gdb_ctx->s, "OK");
2190 static GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
2191 /* Order is important if has same prefix */
2193 .handler = handle_query_qemu_sstepbits,
2194 .cmd = "qemu.sstepbits",
2197 .handler = handle_query_qemu_sstep,
2198 .cmd = "qemu.sstep",
2201 .handler = handle_set_qemu_sstep,
2202 .cmd = "qemu.sstep=",
2203 .cmd_startswith = 1,
2208 static GdbCmdParseEntry gdb_gen_query_table[] = {
2210 .handler = handle_query_curr_tid,
2214 .handler = handle_query_threads,
2215 .cmd = "sThreadInfo",
2218 .handler = handle_query_first_threads,
2219 .cmd = "fThreadInfo",
2222 .handler = handle_query_thread_extra,
2223 .cmd = "ThreadExtraInfo,",
2224 .cmd_startswith = 1,
2227 #ifdef CONFIG_USER_ONLY
2229 .handler = handle_query_offsets,
2234 .handler = handle_query_rcmd,
2236 .cmd_startswith = 1,
2241 .handler = handle_query_supported,
2242 .cmd = "Supported:",
2243 .cmd_startswith = 1,
2247 .handler = handle_query_supported,
2252 .handler = handle_query_xfer_features,
2253 .cmd = "Xfer:features:read:",
2254 .cmd_startswith = 1,
2258 .handler = handle_query_attached,
2263 .handler = handle_query_attached,
2267 .handler = handle_query_qemu_supported,
2268 .cmd = "qemu.Supported",
2270 #ifndef CONFIG_USER_ONLY
2272 .handler = handle_query_qemu_phy_mem_mode,
2273 .cmd = "qemu.PhyMemMode",
2278 static GdbCmdParseEntry gdb_gen_set_table[] = {
2279 /* Order is important if has same prefix */
2281 .handler = handle_set_qemu_sstep,
2282 .cmd = "qemu.sstep:",
2283 .cmd_startswith = 1,
2286 #ifndef CONFIG_USER_ONLY
2288 .handler = handle_set_qemu_phy_mem_mode,
2289 .cmd = "qemu.PhyMemMode:",
2290 .cmd_startswith = 1,
2296 static void handle_gen_query(GdbCmdContext *gdb_ctx, void *user_ctx)
2298 if (!gdb_ctx->num_params) {
2302 if (!process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2303 gdb_gen_query_set_common_table,
2304 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2308 if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2309 gdb_gen_query_table,
2310 ARRAY_SIZE(gdb_gen_query_table))) {
2311 put_packet(gdb_ctx->s, "");
2315 static void handle_gen_set(GdbCmdContext *gdb_ctx, void *user_ctx)
2317 if (!gdb_ctx->num_params) {
2321 if (!process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2322 gdb_gen_query_set_common_table,
2323 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2327 if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2329 ARRAY_SIZE(gdb_gen_set_table))) {
2330 put_packet(gdb_ctx->s, "");
2334 static void handle_target_halt(GdbCmdContext *gdb_ctx, void *user_ctx)
2338 gdb_fmt_thread_id(gdb_ctx->s, gdb_ctx->s->c_cpu, thread_id,
2340 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "T%02xthread:%s;",
2341 GDB_SIGNAL_TRAP, thread_id);
2342 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2344 * Remove all the breakpoints when this query is issued,
2345 * because gdb is doing an initial connect and the state
2346 * should be cleaned up.
2348 gdb_breakpoint_remove_all();
2351 static int gdb_handle_packet(GDBState *s, const char *line_buf)
2353 const GdbCmdParseEntry *cmd_parser = NULL;
2355 trace_gdbstub_io_command(line_buf);
2357 switch (line_buf[0]) {
2359 put_packet(s, "OK");
2363 static const GdbCmdParseEntry target_halted_cmd_desc = {
2364 .handler = handle_target_halt,
2368 cmd_parser = &target_halted_cmd_desc;
2373 static const GdbCmdParseEntry continue_cmd_desc = {
2374 .handler = handle_continue,
2376 .cmd_startswith = 1,
2379 cmd_parser = &continue_cmd_desc;
2384 static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
2385 .handler = handle_cont_with_sig,
2387 .cmd_startswith = 1,
2390 cmd_parser = &cont_with_sig_cmd_desc;
2395 static const GdbCmdParseEntry v_cmd_desc = {
2396 .handler = handle_v_commands,
2398 .cmd_startswith = 1,
2401 cmd_parser = &v_cmd_desc;
2405 /* Kill the target */
2406 error_report("QEMU: Terminated via GDBstub");
2410 static const GdbCmdParseEntry detach_cmd_desc = {
2411 .handler = handle_detach,
2413 .cmd_startswith = 1,
2416 cmd_parser = &detach_cmd_desc;
2421 static const GdbCmdParseEntry step_cmd_desc = {
2422 .handler = handle_step,
2424 .cmd_startswith = 1,
2427 cmd_parser = &step_cmd_desc;
2432 static const GdbCmdParseEntry file_io_cmd_desc = {
2433 .handler = handle_file_io,
2435 .cmd_startswith = 1,
2438 cmd_parser = &file_io_cmd_desc;
2443 static const GdbCmdParseEntry read_all_regs_cmd_desc = {
2444 .handler = handle_read_all_regs,
2448 cmd_parser = &read_all_regs_cmd_desc;
2453 static const GdbCmdParseEntry write_all_regs_cmd_desc = {
2454 .handler = handle_write_all_regs,
2456 .cmd_startswith = 1,
2459 cmd_parser = &write_all_regs_cmd_desc;
2464 static const GdbCmdParseEntry read_mem_cmd_desc = {
2465 .handler = handle_read_mem,
2467 .cmd_startswith = 1,
2470 cmd_parser = &read_mem_cmd_desc;
2475 static const GdbCmdParseEntry write_mem_cmd_desc = {
2476 .handler = handle_write_mem,
2478 .cmd_startswith = 1,
2481 cmd_parser = &write_mem_cmd_desc;
2486 static const GdbCmdParseEntry get_reg_cmd_desc = {
2487 .handler = handle_get_reg,
2489 .cmd_startswith = 1,
2492 cmd_parser = &get_reg_cmd_desc;
2497 static const GdbCmdParseEntry set_reg_cmd_desc = {
2498 .handler = handle_set_reg,
2500 .cmd_startswith = 1,
2503 cmd_parser = &set_reg_cmd_desc;
2508 static const GdbCmdParseEntry insert_bp_cmd_desc = {
2509 .handler = handle_insert_bp,
2511 .cmd_startswith = 1,
2514 cmd_parser = &insert_bp_cmd_desc;
2519 static const GdbCmdParseEntry remove_bp_cmd_desc = {
2520 .handler = handle_remove_bp,
2522 .cmd_startswith = 1,
2525 cmd_parser = &remove_bp_cmd_desc;
2530 static const GdbCmdParseEntry set_thread_cmd_desc = {
2531 .handler = handle_set_thread,
2533 .cmd_startswith = 1,
2536 cmd_parser = &set_thread_cmd_desc;
2541 static const GdbCmdParseEntry thread_alive_cmd_desc = {
2542 .handler = handle_thread_alive,
2544 .cmd_startswith = 1,
2547 cmd_parser = &thread_alive_cmd_desc;
2552 static const GdbCmdParseEntry gen_query_cmd_desc = {
2553 .handler = handle_gen_query,
2555 .cmd_startswith = 1,
2558 cmd_parser = &gen_query_cmd_desc;
2563 static const GdbCmdParseEntry gen_set_cmd_desc = {
2564 .handler = handle_gen_set,
2566 .cmd_startswith = 1,
2569 cmd_parser = &gen_set_cmd_desc;
2573 /* put empty packet */
2578 run_cmd_parser(s, line_buf, cmd_parser);
2583 void gdb_set_stop_cpu(CPUState *cpu)
2585 GDBProcess *p = gdb_get_cpu_process(gdbserver_state, cpu);
2589 * Having a stop CPU corresponding to a process that is not attached
2590 * confuses GDB. So we ignore the request.
2595 gdbserver_state->c_cpu = cpu;
2596 gdbserver_state->g_cpu = cpu;
2599 #ifndef CONFIG_USER_ONLY
2600 static void gdb_vm_state_change(void *opaque, int running, RunState state)
2602 GDBState *s = gdbserver_state;
2603 CPUState *cpu = s->c_cpu;
2609 if (running || s->state == RS_INACTIVE) {
2612 /* Is there a GDB syscall waiting to be sent? */
2613 if (s->current_syscall_cb) {
2614 put_packet(s, s->syscall_buf);
2619 /* No process attached */
2623 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id));
2626 case RUN_STATE_DEBUG:
2627 if (cpu->watchpoint_hit) {
2628 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
2639 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
2640 (target_ulong)cpu->watchpoint_hit->vaddr);
2641 snprintf(buf, sizeof(buf),
2642 "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
2643 GDB_SIGNAL_TRAP, thread_id, type,
2644 (target_ulong)cpu->watchpoint_hit->vaddr);
2645 cpu->watchpoint_hit = NULL;
2648 trace_gdbstub_hit_break();
2651 ret = GDB_SIGNAL_TRAP;
2653 case RUN_STATE_PAUSED:
2654 trace_gdbstub_hit_paused();
2655 ret = GDB_SIGNAL_INT;
2657 case RUN_STATE_SHUTDOWN:
2658 trace_gdbstub_hit_shutdown();
2659 ret = GDB_SIGNAL_QUIT;
2661 case RUN_STATE_IO_ERROR:
2662 trace_gdbstub_hit_io_error();
2663 ret = GDB_SIGNAL_IO;
2665 case RUN_STATE_WATCHDOG:
2666 trace_gdbstub_hit_watchdog();
2667 ret = GDB_SIGNAL_ALRM;
2669 case RUN_STATE_INTERNAL_ERROR:
2670 trace_gdbstub_hit_internal_error();
2671 ret = GDB_SIGNAL_ABRT;
2673 case RUN_STATE_SAVE_VM:
2674 case RUN_STATE_RESTORE_VM:
2676 case RUN_STATE_FINISH_MIGRATE:
2677 ret = GDB_SIGNAL_XCPU;
2680 trace_gdbstub_hit_unknown(state);
2681 ret = GDB_SIGNAL_UNKNOWN;
2684 gdb_set_stop_cpu(cpu);
2685 snprintf(buf, sizeof(buf), "T%02xthread:%s;", ret, thread_id);
2690 /* disable single step if it was enabled */
2691 cpu_single_step(cpu, 0);
2695 /* Send a gdb syscall request.
2696 This accepts limited printf-style format specifiers, specifically:
2697 %x - target_ulong argument printed in hex.
2698 %lx - 64-bit argument printed in hex.
2699 %s - string pointer (target_ulong) and length (int) pair. */
2700 void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
2708 s = gdbserver_state;
2711 s->current_syscall_cb = cb;
2712 #ifndef CONFIG_USER_ONLY
2713 vm_stop(RUN_STATE_DEBUG);
2716 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
2723 addr = va_arg(va, target_ulong);
2724 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
2727 if (*(fmt++) != 'x')
2729 i64 = va_arg(va, uint64_t);
2730 p += snprintf(p, p_end - p, "%" PRIx64, i64);
2733 addr = va_arg(va, target_ulong);
2734 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
2735 addr, va_arg(va, int));
2739 error_report("gdbstub: Bad syscall format string '%s'",
2748 #ifdef CONFIG_USER_ONLY
2749 put_packet(s, s->syscall_buf);
2750 /* Return control to gdb for it to process the syscall request.
2751 * Since the protocol requires that gdb hands control back to us
2752 * using a "here are the results" F packet, we don't need to check
2753 * gdb_handlesig's return value (which is the signal to deliver if
2754 * execution was resumed via a continue packet).
2756 gdb_handlesig(s->c_cpu, 0);
2758 /* In this case wait to send the syscall packet until notification that
2759 the CPU has stopped. This must be done because if the packet is sent
2760 now the reply from the syscall request could be received while the CPU
2761 is still in the running state, which can cause packets to be dropped
2762 and state transition 'T' packets to be sent while the syscall is still
2764 qemu_cpu_kick(s->c_cpu);
2768 void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
2773 gdb_do_syscallv(cb, fmt, va);
2777 static void gdb_read_byte(GDBState *s, uint8_t ch)
2781 #ifndef CONFIG_USER_ONLY
2782 if (s->last_packet_len) {
2783 /* Waiting for a response to the last packet. If we see the start
2784 of a new command then abandon the previous response. */
2786 trace_gdbstub_err_got_nack();
2787 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
2788 } else if (ch == '+') {
2789 trace_gdbstub_io_got_ack();
2791 trace_gdbstub_io_got_unexpected(ch);
2794 if (ch == '+' || ch == '$')
2795 s->last_packet_len = 0;
2799 if (runstate_is_running()) {
2800 /* when the CPU is running, we cannot do anything except stop
2801 it when receiving a char */
2802 vm_stop(RUN_STATE_PAUSED);
2809 /* start of command packet */
2810 s->line_buf_index = 0;
2812 s->state = RS_GETLINE;
2814 trace_gdbstub_err_garbage(ch);
2819 /* start escape sequence */
2820 s->state = RS_GETLINE_ESC;
2822 } else if (ch == '*') {
2823 /* start run length encoding sequence */
2824 s->state = RS_GETLINE_RLE;
2826 } else if (ch == '#') {
2827 /* end of command, start of checksum*/
2828 s->state = RS_CHKSUM1;
2829 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2830 trace_gdbstub_err_overrun();
2833 /* unescaped command character */
2834 s->line_buf[s->line_buf_index++] = ch;
2838 case RS_GETLINE_ESC:
2840 /* unexpected end of command in escape sequence */
2841 s->state = RS_CHKSUM1;
2842 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2843 /* command buffer overrun */
2844 trace_gdbstub_err_overrun();
2847 /* parse escaped character and leave escape state */
2848 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
2850 s->state = RS_GETLINE;
2853 case RS_GETLINE_RLE:
2855 * Run-length encoding is explained in "Debugging with GDB /
2856 * Appendix E GDB Remote Serial Protocol / Overview".
2858 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
2859 /* invalid RLE count encoding */
2860 trace_gdbstub_err_invalid_repeat(ch);
2861 s->state = RS_GETLINE;
2863 /* decode repeat length */
2864 int repeat = ch - ' ' + 3;
2865 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
2866 /* that many repeats would overrun the command buffer */
2867 trace_gdbstub_err_overrun();
2869 } else if (s->line_buf_index < 1) {
2870 /* got a repeat but we have nothing to repeat */
2871 trace_gdbstub_err_invalid_rle();
2872 s->state = RS_GETLINE;
2874 /* repeat the last character */
2875 memset(s->line_buf + s->line_buf_index,
2876 s->line_buf[s->line_buf_index - 1], repeat);
2877 s->line_buf_index += repeat;
2879 s->state = RS_GETLINE;
2884 /* get high hex digit of checksum */
2885 if (!isxdigit(ch)) {
2886 trace_gdbstub_err_checksum_invalid(ch);
2887 s->state = RS_GETLINE;
2890 s->line_buf[s->line_buf_index] = '\0';
2891 s->line_csum = fromhex(ch) << 4;
2892 s->state = RS_CHKSUM2;
2895 /* get low hex digit of checksum */
2896 if (!isxdigit(ch)) {
2897 trace_gdbstub_err_checksum_invalid(ch);
2898 s->state = RS_GETLINE;
2901 s->line_csum |= fromhex(ch);
2903 if (s->line_csum != (s->line_sum & 0xff)) {
2904 trace_gdbstub_err_checksum_incorrect(s->line_sum, s->line_csum);
2905 /* send NAK reply */
2907 put_buffer(s, &reply, 1);
2910 /* send ACK reply */
2912 put_buffer(s, &reply, 1);
2913 s->state = gdb_handle_packet(s, s->line_buf);
2922 /* Tell the remote gdb that the process has exited. */
2923 void gdb_exit(CPUArchState *env, int code)
2928 s = gdbserver_state;
2932 #ifdef CONFIG_USER_ONLY
2933 if (gdbserver_fd < 0 || s->fd < 0) {
2938 trace_gdbstub_op_exiting((uint8_t)code);
2940 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
2943 #ifndef CONFIG_USER_ONLY
2944 qemu_chr_fe_deinit(&s->chr, true);
2949 * Create the process that will contain all the "orphan" CPUs (that are not
2950 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2951 * be attachable and thus will be invisible to the user.
2953 static void create_default_process(GDBState *s)
2955 GDBProcess *process;
2958 if (s->process_num) {
2959 max_pid = s->processes[s->process_num - 1].pid;
2962 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2963 process = &s->processes[s->process_num - 1];
2965 /* We need an available PID slot for this process */
2966 assert(max_pid < UINT32_MAX);
2968 process->pid = max_pid + 1;
2969 process->attached = false;
2970 process->target_xml[0] = '\0';
2973 #ifdef CONFIG_USER_ONLY
2975 gdb_handlesig(CPUState *cpu, int sig)
2981 s = gdbserver_state;
2982 if (gdbserver_fd < 0 || s->fd < 0) {
2986 /* disable single step if it was enabled */
2987 cpu_single_step(cpu, 0);
2991 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
2994 /* put_packet() might have detected that the peer terminated the
3002 s->running_state = 0;
3003 while (s->running_state == 0) {
3004 n = read(s->fd, buf, 256);
3008 for (i = 0; i < n; i++) {
3009 gdb_read_byte(s, buf[i]);
3012 /* XXX: Connection closed. Should probably wait for another
3013 connection before continuing. */
3026 /* Tell the remote gdb that the process has exited due to SIG. */
3027 void gdb_signalled(CPUArchState *env, int sig)
3032 s = gdbserver_state;
3033 if (gdbserver_fd < 0 || s->fd < 0) {
3037 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
3041 static bool gdb_accept(void)
3044 struct sockaddr_in sockaddr;
3049 len = sizeof(sockaddr);
3050 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
3051 if (fd < 0 && errno != EINTR) {
3054 } else if (fd >= 0) {
3055 qemu_set_cloexec(fd);
3060 /* set short latency */
3061 if (socket_set_nodelay(fd)) {
3062 perror("setsockopt");
3067 s = g_malloc0(sizeof(GDBState));
3068 create_default_process(s);
3069 s->processes[0].attached = true;
3070 s->c_cpu = gdb_first_attached_cpu(s);
3071 s->g_cpu = s->c_cpu;
3073 gdb_has_xml = false;
3075 gdbserver_state = s;
3079 static int gdbserver_open(int port)
3081 struct sockaddr_in sockaddr;
3084 fd = socket(PF_INET, SOCK_STREAM, 0);
3089 qemu_set_cloexec(fd);
3091 socket_set_fast_reuse(fd);
3093 sockaddr.sin_family = AF_INET;
3094 sockaddr.sin_port = htons(port);
3095 sockaddr.sin_addr.s_addr = 0;
3096 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
3102 ret = listen(fd, 1);
3111 int gdbserver_start(int port)
3113 gdbserver_fd = gdbserver_open(port);
3114 if (gdbserver_fd < 0)
3116 /* accept connections */
3117 if (!gdb_accept()) {
3118 close(gdbserver_fd);
3125 /* Disable gdb stub for child processes. */
3126 void gdbserver_fork(CPUState *cpu)
3128 GDBState *s = gdbserver_state;
3130 if (gdbserver_fd < 0 || s->fd < 0) {
3135 cpu_breakpoint_remove_all(cpu, BP_GDB);
3136 cpu_watchpoint_remove_all(cpu, BP_GDB);
3139 static int gdb_chr_can_receive(void *opaque)
3141 /* We can handle an arbitrarily large amount of data.
3142 Pick the maximum packet size, which is as good as anything. */
3143 return MAX_PACKET_LENGTH;
3146 static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
3150 for (i = 0; i < size; i++) {
3151 gdb_read_byte(gdbserver_state, buf[i]);
3155 static void gdb_chr_event(void *opaque, int event)
3158 GDBState *s = (GDBState *) opaque;
3161 case CHR_EVENT_OPENED:
3162 /* Start with first process attached, others detached */
3163 for (i = 0; i < s->process_num; i++) {
3164 s->processes[i].attached = !i;
3167 s->c_cpu = gdb_first_attached_cpu(s);
3168 s->g_cpu = s->c_cpu;
3170 vm_stop(RUN_STATE_PAUSED);
3171 gdb_has_xml = false;
3178 static void gdb_monitor_output(GDBState *s, const char *msg, int len)
3180 char buf[MAX_PACKET_LENGTH];
3183 if (len > (MAX_PACKET_LENGTH/2) - 1)
3184 len = (MAX_PACKET_LENGTH/2) - 1;
3185 memtohex(buf + 1, (uint8_t *)msg, len);
3189 static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
3191 const char *p = (const char *)buf;
3194 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
3196 if (len <= max_sz) {
3197 gdb_monitor_output(gdbserver_state, p, len);
3200 gdb_monitor_output(gdbserver_state, p, max_sz);
3208 static void gdb_sigterm_handler(int signal)
3210 if (runstate_is_running()) {
3211 vm_stop(RUN_STATE_PAUSED);
3216 static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
3217 bool *be_opened, Error **errp)
3222 static void char_gdb_class_init(ObjectClass *oc, void *data)
3224 ChardevClass *cc = CHARDEV_CLASS(oc);
3226 cc->internal = true;
3227 cc->open = gdb_monitor_open;
3228 cc->chr_write = gdb_monitor_write;
3231 #define TYPE_CHARDEV_GDB "chardev-gdb"
3233 static const TypeInfo char_gdb_type_info = {
3234 .name = TYPE_CHARDEV_GDB,
3235 .parent = TYPE_CHARDEV,
3236 .class_init = char_gdb_class_init,
3239 static int find_cpu_clusters(Object *child, void *opaque)
3241 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
3242 GDBState *s = (GDBState *) opaque;
3243 CPUClusterState *cluster = CPU_CLUSTER(child);
3244 GDBProcess *process;
3246 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
3248 process = &s->processes[s->process_num - 1];
3251 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
3252 * runtime, we enforce here that the machine does not use a cluster ID
3253 * that would lead to PID 0.
3255 assert(cluster->cluster_id != UINT32_MAX);
3256 process->pid = cluster->cluster_id + 1;
3257 process->attached = false;
3258 process->target_xml[0] = '\0';
3263 return object_child_foreach(child, find_cpu_clusters, opaque);
3266 static int pid_order(const void *a, const void *b)
3268 GDBProcess *pa = (GDBProcess *) a;
3269 GDBProcess *pb = (GDBProcess *) b;
3271 if (pa->pid < pb->pid) {
3273 } else if (pa->pid > pb->pid) {
3280 static void create_processes(GDBState *s)
3282 object_child_foreach(object_get_root(), find_cpu_clusters, s);
3286 qsort(s->processes, s->process_num, sizeof(s->processes[0]), pid_order);
3289 create_default_process(s);
3292 static void cleanup_processes(GDBState *s)
3294 g_free(s->processes);
3296 s->processes = NULL;
3299 int gdbserver_start(const char *device)
3301 trace_gdbstub_op_start(device);
3304 char gdbstub_device_name[128];
3305 Chardev *chr = NULL;
3309 error_report("gdbstub: meaningless to attach gdb to a "
3310 "machine without any CPU.");
3316 if (strcmp(device, "none") != 0) {
3317 if (strstart(device, "tcp:", NULL)) {
3318 /* enforce required TCP attributes */
3319 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
3320 "%s,nowait,nodelay,server", device);
3321 device = gdbstub_device_name;
3324 else if (strcmp(device, "stdio") == 0) {
3325 struct sigaction act;
3327 memset(&act, 0, sizeof(act));
3328 act.sa_handler = gdb_sigterm_handler;
3329 sigaction(SIGINT, &act, NULL);
3333 * FIXME: it's a bit weird to allow using a mux chardev here
3334 * and implicitly setup a monitor. We may want to break this.
3336 chr = qemu_chr_new_noreplay("gdb", device, true, NULL);
3341 s = gdbserver_state;
3343 s = g_malloc0(sizeof(GDBState));
3344 gdbserver_state = s;
3346 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
3348 /* Initialize a monitor terminal for gdb */
3349 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
3350 NULL, NULL, &error_abort);
3351 monitor_init_hmp(mon_chr, false);
3353 qemu_chr_fe_deinit(&s->chr, true);
3354 mon_chr = s->mon_chr;
3355 cleanup_processes(s);
3356 memset(s, 0, sizeof(GDBState));
3357 s->mon_chr = mon_chr;
3360 create_processes(s);
3363 qemu_chr_fe_init(&s->chr, chr, &error_abort);
3364 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
3365 gdb_chr_event, NULL, s, NULL, true);
3367 s->state = chr ? RS_IDLE : RS_INACTIVE;
3368 s->mon_chr = mon_chr;
3369 s->current_syscall_cb = NULL;
3374 void gdbserver_cleanup(void)
3376 if (gdbserver_state) {
3377 put_packet(gdbserver_state, "W00");
3381 static void register_types(void)
3383 type_register_static(&char_gdb_type_info);
3386 type_init(register_types);