gdbstub: refactor get_feature_xml
[qemu/kevin.git] / gdbstub / gdbstub.c
blob729e54139a4d155e2f20789f589cd3559288e474
1 /*
2 * gdb server stub
4 * This implements a subset of the remote protocol as described in:
6 * https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
8 * Copyright (c) 2003-2005 Fabrice Bellard
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 * SPDX-License-Identifier: LGPL-2.0+
26 #include "qemu/osdep.h"
27 #include "qemu/ctype.h"
28 #include "qemu/cutils.h"
29 #include "qemu/module.h"
30 #include "qemu/error-report.h"
31 #include "trace.h"
32 #include "exec/gdbstub.h"
33 #include "gdbstub/syscalls.h"
34 #ifdef CONFIG_USER_ONLY
35 #include "gdbstub/user.h"
36 #else
37 #include "hw/cpu/cluster.h"
38 #include "hw/boards.h"
39 #endif
41 #include "sysemu/hw_accel.h"
42 #include "sysemu/runstate.h"
43 #include "exec/replay-core.h"
44 #include "exec/hwaddr.h"
46 #include "internals.h"
48 typedef struct GDBRegisterState {
49 int base_reg;
50 int num_regs;
51 gdb_get_reg_cb get_reg;
52 gdb_set_reg_cb set_reg;
53 const char *xml;
54 struct GDBRegisterState *next;
55 } GDBRegisterState;
57 GDBState gdbserver_state;
59 void gdb_init_gdbserver_state(void)
61 g_assert(!gdbserver_state.init);
62 memset(&gdbserver_state, 0, sizeof(GDBState));
63 gdbserver_state.init = true;
64 gdbserver_state.str_buf = g_string_new(NULL);
65 gdbserver_state.mem_buf = g_byte_array_sized_new(MAX_PACKET_LENGTH);
66 gdbserver_state.last_packet = g_byte_array_sized_new(MAX_PACKET_LENGTH + 4);
69 * What single-step modes are supported is accelerator dependent.
70 * By default try to use no IRQs and no timers while single
71 * stepping so as to make single stepping like a typical ICE HW step.
73 gdbserver_state.supported_sstep_flags = accel_supported_gdbstub_sstep_flags();
74 gdbserver_state.sstep_flags = SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER;
75 gdbserver_state.sstep_flags &= gdbserver_state.supported_sstep_flags;
78 bool gdb_has_xml;
80 /* writes 2*len+1 bytes in buf */
81 void gdb_memtohex(GString *buf, const uint8_t *mem, int len)
83 int i, c;
84 for(i = 0; i < len; i++) {
85 c = mem[i];
86 g_string_append_c(buf, tohex(c >> 4));
87 g_string_append_c(buf, tohex(c & 0xf));
89 g_string_append_c(buf, '\0');
92 void gdb_hextomem(GByteArray *mem, const char *buf, int len)
94 int i;
96 for(i = 0; i < len; i++) {
97 guint8 byte = fromhex(buf[0]) << 4 | fromhex(buf[1]);
98 g_byte_array_append(mem, &byte, 1);
99 buf += 2;
103 static void hexdump(const char *buf, int len,
104 void (*trace_fn)(size_t ofs, char const *text))
106 char line_buffer[3 * 16 + 4 + 16 + 1];
108 size_t i;
109 for (i = 0; i < len || (i & 0xF); ++i) {
110 size_t byte_ofs = i & 15;
112 if (byte_ofs == 0) {
113 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
114 line_buffer[3 * 16 + 4 + 16] = 0;
117 size_t col_group = (i >> 2) & 3;
118 size_t hex_col = byte_ofs * 3 + col_group;
119 size_t txt_col = 3 * 16 + 4 + byte_ofs;
121 if (i < len) {
122 char value = buf[i];
124 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
125 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
126 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
127 ? value
128 : '.';
131 if (byte_ofs == 0xF)
132 trace_fn(i & -16, line_buffer);
136 /* return -1 if error, 0 if OK */
137 int gdb_put_packet_binary(const char *buf, int len, bool dump)
139 int csum, i;
140 uint8_t footer[3];
142 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
143 hexdump(buf, len, trace_gdbstub_io_binaryreply);
146 for(;;) {
147 g_byte_array_set_size(gdbserver_state.last_packet, 0);
148 g_byte_array_append(gdbserver_state.last_packet,
149 (const uint8_t *) "$", 1);
150 g_byte_array_append(gdbserver_state.last_packet,
151 (const uint8_t *) buf, len);
152 csum = 0;
153 for(i = 0; i < len; i++) {
154 csum += buf[i];
156 footer[0] = '#';
157 footer[1] = tohex((csum >> 4) & 0xf);
158 footer[2] = tohex((csum) & 0xf);
159 g_byte_array_append(gdbserver_state.last_packet, footer, 3);
161 gdb_put_buffer(gdbserver_state.last_packet->data,
162 gdbserver_state.last_packet->len);
164 if (gdb_got_immediate_ack()) {
165 break;
168 return 0;
171 /* return -1 if error, 0 if OK */
172 int gdb_put_packet(const char *buf)
174 trace_gdbstub_io_reply(buf);
176 return gdb_put_packet_binary(buf, strlen(buf), false);
179 void gdb_put_strbuf(void)
181 gdb_put_packet(gdbserver_state.str_buf->str);
184 /* Encode data using the encoding for 'x' packets. */
185 void gdb_memtox(GString *buf, const char *mem, int len)
187 char c;
189 while (len--) {
190 c = *(mem++);
191 switch (c) {
192 case '#': case '$': case '*': case '}':
193 g_string_append_c(buf, '}');
194 g_string_append_c(buf, c ^ 0x20);
195 break;
196 default:
197 g_string_append_c(buf, c);
198 break;
203 static uint32_t gdb_get_cpu_pid(CPUState *cpu)
205 #ifdef CONFIG_USER_ONLY
206 return getpid();
207 #else
208 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
209 /* Return the default process' PID */
210 int index = gdbserver_state.process_num - 1;
211 return gdbserver_state.processes[index].pid;
213 return cpu->cluster_index + 1;
214 #endif
217 GDBProcess *gdb_get_process(uint32_t pid)
219 int i;
221 if (!pid) {
222 /* 0 means any process, we take the first one */
223 return &gdbserver_state.processes[0];
226 for (i = 0; i < gdbserver_state.process_num; i++) {
227 if (gdbserver_state.processes[i].pid == pid) {
228 return &gdbserver_state.processes[i];
232 return NULL;
235 static GDBProcess *gdb_get_cpu_process(CPUState *cpu)
237 return gdb_get_process(gdb_get_cpu_pid(cpu));
240 static CPUState *find_cpu(uint32_t thread_id)
242 CPUState *cpu;
244 CPU_FOREACH(cpu) {
245 if (gdb_get_cpu_index(cpu) == thread_id) {
246 return cpu;
250 return NULL;
253 CPUState *gdb_get_first_cpu_in_process(GDBProcess *process)
255 CPUState *cpu;
257 CPU_FOREACH(cpu) {
258 if (gdb_get_cpu_pid(cpu) == process->pid) {
259 return cpu;
263 return NULL;
266 static CPUState *gdb_next_cpu_in_process(CPUState *cpu)
268 uint32_t pid = gdb_get_cpu_pid(cpu);
269 cpu = CPU_NEXT(cpu);
271 while (cpu) {
272 if (gdb_get_cpu_pid(cpu) == pid) {
273 break;
276 cpu = CPU_NEXT(cpu);
279 return cpu;
282 /* Return the cpu following @cpu, while ignoring unattached processes. */
283 static CPUState *gdb_next_attached_cpu(CPUState *cpu)
285 cpu = CPU_NEXT(cpu);
287 while (cpu) {
288 if (gdb_get_cpu_process(cpu)->attached) {
289 break;
292 cpu = CPU_NEXT(cpu);
295 return cpu;
298 /* Return the first attached cpu */
299 CPUState *gdb_first_attached_cpu(void)
301 CPUState *cpu = first_cpu;
302 GDBProcess *process = gdb_get_cpu_process(cpu);
304 if (!process->attached) {
305 return gdb_next_attached_cpu(cpu);
308 return cpu;
311 static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid)
313 GDBProcess *process;
314 CPUState *cpu;
316 if (!pid && !tid) {
317 /* 0 means any process/thread, we take the first attached one */
318 return gdb_first_attached_cpu();
319 } else if (pid && !tid) {
320 /* any thread in a specific process */
321 process = gdb_get_process(pid);
323 if (process == NULL) {
324 return NULL;
327 if (!process->attached) {
328 return NULL;
331 return gdb_get_first_cpu_in_process(process);
332 } else {
333 /* a specific thread */
334 cpu = find_cpu(tid);
336 if (cpu == NULL) {
337 return NULL;
340 process = gdb_get_cpu_process(cpu);
342 if (pid && process->pid != pid) {
343 return NULL;
346 if (!process->attached) {
347 return NULL;
350 return cpu;
354 static const char *get_feature_xml(const char *p, const char **newp,
355 GDBProcess *process)
357 CPUState *cpu = gdb_get_first_cpu_in_process(process);
358 CPUClass *cc = CPU_GET_CLASS(cpu);
359 size_t len;
362 * qXfer:features:read:ANNEX:OFFSET,LENGTH'
363 * ^p ^newp
365 char *term = strchr(p, ':');
366 *newp = term + 1;
367 len = term - p;
369 /* Is it the main target xml? */
370 if (strncmp(p, "target.xml", len) == 0) {
371 if (!process->target_xml) {
372 GDBRegisterState *r;
373 GString *xml = g_string_new("<?xml version=\"1.0\"?>");
375 g_string_append(xml,
376 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
377 "<target>");
379 if (cc->gdb_arch_name) {
380 g_autofree gchar *arch = cc->gdb_arch_name(cpu);
381 g_string_append_printf(xml,
382 "<architecture>%s</architecture>",
383 arch);
385 g_string_append(xml, "<xi:include href=\"");
386 g_string_append(xml, cc->gdb_core_xml_file);
387 g_string_append(xml, "\"/>");
388 for (r = cpu->gdb_regs; r; r = r->next) {
389 g_string_append(xml, "<xi:include href=\"");
390 g_string_append(xml, r->xml);
391 g_string_append(xml, "\"/>");
393 g_string_append(xml, "</target>");
395 process->target_xml = g_string_free(xml, false);
396 return process->target_xml;
399 /* Is it dynamically generated by the target? */
400 if (cc->gdb_get_dynamic_xml) {
401 g_autofree char *xmlname = g_strndup(p, len);
402 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
403 if (xml) {
404 return xml;
407 /* Is it one of the encoded gdb-xml/ files? */
408 for (int i = 0; xml_builtin[i][0]; i++) {
409 const char *name = xml_builtin[i][0];
410 if ((strncmp(name, p, len) == 0) &&
411 strlen(name) == len) {
412 return xml_builtin[i][1];
416 /* failed */
417 return NULL;
420 static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
422 CPUClass *cc = CPU_GET_CLASS(cpu);
423 CPUArchState *env = cpu->env_ptr;
424 GDBRegisterState *r;
426 if (reg < cc->gdb_num_core_regs) {
427 return cc->gdb_read_register(cpu, buf, reg);
430 for (r = cpu->gdb_regs; r; r = r->next) {
431 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
432 return r->get_reg(env, buf, reg - r->base_reg);
435 return 0;
438 static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
440 CPUClass *cc = CPU_GET_CLASS(cpu);
441 CPUArchState *env = cpu->env_ptr;
442 GDBRegisterState *r;
444 if (reg < cc->gdb_num_core_regs) {
445 return cc->gdb_write_register(cpu, mem_buf, reg);
448 for (r = cpu->gdb_regs; r; r = r->next) {
449 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
450 return r->set_reg(env, mem_buf, reg - r->base_reg);
453 return 0;
456 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
457 specifies the first register number and these registers are included in
458 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
459 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
462 void gdb_register_coprocessor(CPUState *cpu,
463 gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
464 int num_regs, const char *xml, int g_pos)
466 GDBRegisterState *s;
467 GDBRegisterState **p;
469 p = &cpu->gdb_regs;
470 while (*p) {
471 /* Check for duplicates. */
472 if (strcmp((*p)->xml, xml) == 0)
473 return;
474 p = &(*p)->next;
477 s = g_new0(GDBRegisterState, 1);
478 s->base_reg = cpu->gdb_num_regs;
479 s->num_regs = num_regs;
480 s->get_reg = get_reg;
481 s->set_reg = set_reg;
482 s->xml = xml;
484 /* Add to end of list. */
485 cpu->gdb_num_regs += num_regs;
486 *p = s;
487 if (g_pos) {
488 if (g_pos != s->base_reg) {
489 error_report("Error: Bad gdb register numbering for '%s', "
490 "expected %d got %d", xml, g_pos, s->base_reg);
491 } else {
492 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
497 static void gdb_process_breakpoint_remove_all(GDBProcess *p)
499 CPUState *cpu = gdb_get_first_cpu_in_process(p);
501 while (cpu) {
502 gdb_breakpoint_remove_all(cpu);
503 cpu = gdb_next_cpu_in_process(cpu);
508 static void gdb_set_cpu_pc(vaddr pc)
510 CPUState *cpu = gdbserver_state.c_cpu;
512 cpu_synchronize_state(cpu);
513 cpu_set_pc(cpu, pc);
516 void gdb_append_thread_id(CPUState *cpu, GString *buf)
518 if (gdbserver_state.multiprocess) {
519 g_string_append_printf(buf, "p%02x.%02x",
520 gdb_get_cpu_pid(cpu), gdb_get_cpu_index(cpu));
521 } else {
522 g_string_append_printf(buf, "%02x", gdb_get_cpu_index(cpu));
526 static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
527 uint32_t *pid, uint32_t *tid)
529 unsigned long p, t;
530 int ret;
532 if (*buf == 'p') {
533 buf++;
534 ret = qemu_strtoul(buf, &buf, 16, &p);
536 if (ret) {
537 return GDB_READ_THREAD_ERR;
540 /* Skip '.' */
541 buf++;
542 } else {
543 p = 0;
546 ret = qemu_strtoul(buf, &buf, 16, &t);
548 if (ret) {
549 return GDB_READ_THREAD_ERR;
552 *end_buf = buf;
554 if (p == -1) {
555 return GDB_ALL_PROCESSES;
558 if (pid) {
559 *pid = p;
562 if (t == -1) {
563 return GDB_ALL_THREADS;
566 if (tid) {
567 *tid = t;
570 return GDB_ONE_THREAD;
574 * gdb_handle_vcont - Parses and handles a vCont packet.
575 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
576 * a format error, 0 on success.
578 static int gdb_handle_vcont(const char *p)
580 int res, signal = 0;
581 char cur_action;
582 unsigned long tmp;
583 uint32_t pid, tid;
584 GDBProcess *process;
585 CPUState *cpu;
586 GDBThreadIdKind kind;
587 unsigned int max_cpus = gdb_get_max_cpus();
588 /* uninitialised CPUs stay 0 */
589 g_autofree char *newstates = g_new0(char, max_cpus);
591 /* mark valid CPUs with 1 */
592 CPU_FOREACH(cpu) {
593 newstates[cpu->cpu_index] = 1;
597 * res keeps track of what error we are returning, with -ENOTSUP meaning
598 * that the command is unknown or unsupported, thus returning an empty
599 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
600 * or incorrect parameters passed.
602 res = 0;
605 * target_count and last_target keep track of how many CPUs we are going to
606 * step or resume, and a pointer to the state structure of one of them,
607 * respectivelly
609 int target_count = 0;
610 CPUState *last_target = NULL;
612 while (*p) {
613 if (*p++ != ';') {
614 return -ENOTSUP;
617 cur_action = *p++;
618 if (cur_action == 'C' || cur_action == 'S') {
619 cur_action = qemu_tolower(cur_action);
620 res = qemu_strtoul(p, &p, 16, &tmp);
621 if (res) {
622 return res;
624 signal = gdb_signal_to_target(tmp);
625 } else if (cur_action != 'c' && cur_action != 's') {
626 /* unknown/invalid/unsupported command */
627 return -ENOTSUP;
630 if (*p == '\0' || *p == ';') {
632 * No thread specifier, action is on "all threads". The
633 * specification is unclear regarding the process to act on. We
634 * choose all processes.
636 kind = GDB_ALL_PROCESSES;
637 } else if (*p++ == ':') {
638 kind = read_thread_id(p, &p, &pid, &tid);
639 } else {
640 return -ENOTSUP;
643 switch (kind) {
644 case GDB_READ_THREAD_ERR:
645 return -EINVAL;
647 case GDB_ALL_PROCESSES:
648 cpu = gdb_first_attached_cpu();
649 while (cpu) {
650 if (newstates[cpu->cpu_index] == 1) {
651 newstates[cpu->cpu_index] = cur_action;
653 target_count++;
654 last_target = cpu;
657 cpu = gdb_next_attached_cpu(cpu);
659 break;
661 case GDB_ALL_THREADS:
662 process = gdb_get_process(pid);
664 if (!process->attached) {
665 return -EINVAL;
668 cpu = gdb_get_first_cpu_in_process(process);
669 while (cpu) {
670 if (newstates[cpu->cpu_index] == 1) {
671 newstates[cpu->cpu_index] = cur_action;
673 target_count++;
674 last_target = cpu;
677 cpu = gdb_next_cpu_in_process(cpu);
679 break;
681 case GDB_ONE_THREAD:
682 cpu = gdb_get_cpu(pid, tid);
684 /* invalid CPU/thread specified */
685 if (!cpu) {
686 return -EINVAL;
689 /* only use if no previous match occourred */
690 if (newstates[cpu->cpu_index] == 1) {
691 newstates[cpu->cpu_index] = cur_action;
693 target_count++;
694 last_target = cpu;
696 break;
701 * if we're about to resume a specific set of CPUs/threads, make it so that
702 * in case execution gets interrupted, we can send GDB a stop reply with a
703 * correct value. it doesn't really matter which CPU we tell GDB the signal
704 * happened in (VM pauses stop all of them anyway), so long as it is one of
705 * the ones we resumed/single stepped here.
707 if (target_count > 0) {
708 gdbserver_state.c_cpu = last_target;
711 gdbserver_state.signal = signal;
712 gdb_continue_partial(newstates);
713 return res;
716 static const char *cmd_next_param(const char *param, const char delimiter)
718 static const char all_delimiters[] = ",;:=";
719 char curr_delimiters[2] = {0};
720 const char *delimiters;
722 if (delimiter == '?') {
723 delimiters = all_delimiters;
724 } else if (delimiter == '0') {
725 return strchr(param, '\0');
726 } else if (delimiter == '.' && *param) {
727 return param + 1;
728 } else {
729 curr_delimiters[0] = delimiter;
730 delimiters = curr_delimiters;
733 param += strcspn(param, delimiters);
734 if (*param) {
735 param++;
737 return param;
740 static int cmd_parse_params(const char *data, const char *schema,
741 GArray *params)
743 const char *curr_schema, *curr_data;
745 g_assert(schema);
746 g_assert(params->len == 0);
748 curr_schema = schema;
749 curr_data = data;
750 while (curr_schema[0] && curr_schema[1] && *curr_data) {
751 GdbCmdVariant this_param;
753 switch (curr_schema[0]) {
754 case 'l':
755 if (qemu_strtoul(curr_data, &curr_data, 16,
756 &this_param.val_ul)) {
757 return -EINVAL;
759 curr_data = cmd_next_param(curr_data, curr_schema[1]);
760 g_array_append_val(params, this_param);
761 break;
762 case 'L':
763 if (qemu_strtou64(curr_data, &curr_data, 16,
764 (uint64_t *)&this_param.val_ull)) {
765 return -EINVAL;
767 curr_data = cmd_next_param(curr_data, curr_schema[1]);
768 g_array_append_val(params, this_param);
769 break;
770 case 's':
771 this_param.data = curr_data;
772 curr_data = cmd_next_param(curr_data, curr_schema[1]);
773 g_array_append_val(params, this_param);
774 break;
775 case 'o':
776 this_param.opcode = *(uint8_t *)curr_data;
777 curr_data = cmd_next_param(curr_data, curr_schema[1]);
778 g_array_append_val(params, this_param);
779 break;
780 case 't':
781 this_param.thread_id.kind =
782 read_thread_id(curr_data, &curr_data,
783 &this_param.thread_id.pid,
784 &this_param.thread_id.tid);
785 curr_data = cmd_next_param(curr_data, curr_schema[1]);
786 g_array_append_val(params, this_param);
787 break;
788 case '?':
789 curr_data = cmd_next_param(curr_data, curr_schema[1]);
790 break;
791 default:
792 return -EINVAL;
794 curr_schema += 2;
797 return 0;
800 typedef void (*GdbCmdHandler)(GArray *params, void *user_ctx);
803 * cmd_startswith -> cmd is compared using startswith
805 * allow_stop_reply -> true iff the gdbstub can respond to this command with a
806 * "stop reply" packet. The list of commands that accept such response is
807 * defined at the GDB Remote Serial Protocol documentation. see:
808 * https://sourceware.org/gdb/onlinedocs/gdb/Stop-Reply-Packets.html#Stop-Reply-Packets.
810 * schema definitions:
811 * Each schema parameter entry consists of 2 chars,
812 * the first char represents the parameter type handling
813 * the second char represents the delimiter for the next parameter
815 * Currently supported schema types:
816 * 'l' -> unsigned long (stored in .val_ul)
817 * 'L' -> unsigned long long (stored in .val_ull)
818 * 's' -> string (stored in .data)
819 * 'o' -> single char (stored in .opcode)
820 * 't' -> thread id (stored in .thread_id)
821 * '?' -> skip according to delimiter
823 * Currently supported delimiters:
824 * '?' -> Stop at any delimiter (",;:=\0")
825 * '0' -> Stop at "\0"
826 * '.' -> Skip 1 char unless reached "\0"
827 * Any other value is treated as the delimiter value itself
829 typedef struct GdbCmdParseEntry {
830 GdbCmdHandler handler;
831 const char *cmd;
832 bool cmd_startswith;
833 const char *schema;
834 bool allow_stop_reply;
835 } GdbCmdParseEntry;
837 static inline int startswith(const char *string, const char *pattern)
839 return !strncmp(string, pattern, strlen(pattern));
842 static int process_string_cmd(const char *data,
843 const GdbCmdParseEntry *cmds, int num_cmds)
845 int i;
846 g_autoptr(GArray) params = g_array_new(false, true, sizeof(GdbCmdVariant));
848 if (!cmds) {
849 return -1;
852 for (i = 0; i < num_cmds; i++) {
853 const GdbCmdParseEntry *cmd = &cmds[i];
854 g_assert(cmd->handler && cmd->cmd);
856 if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
857 (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
858 continue;
861 if (cmd->schema) {
862 if (cmd_parse_params(&data[strlen(cmd->cmd)],
863 cmd->schema, params)) {
864 return -1;
868 gdbserver_state.allow_stop_reply = cmd->allow_stop_reply;
869 cmd->handler(params, NULL);
870 return 0;
873 return -1;
876 static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd)
878 if (!data) {
879 return;
882 g_string_set_size(gdbserver_state.str_buf, 0);
883 g_byte_array_set_size(gdbserver_state.mem_buf, 0);
885 /* In case there was an error during the command parsing we must
886 * send a NULL packet to indicate the command is not supported */
887 if (process_string_cmd(data, cmd, 1)) {
888 gdb_put_packet("");
892 static void handle_detach(GArray *params, void *user_ctx)
894 GDBProcess *process;
895 uint32_t pid = 1;
897 if (gdbserver_state.multiprocess) {
898 if (!params->len) {
899 gdb_put_packet("E22");
900 return;
903 pid = get_param(params, 0)->val_ul;
906 process = gdb_get_process(pid);
907 gdb_process_breakpoint_remove_all(process);
908 process->attached = false;
910 if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) {
911 gdbserver_state.c_cpu = gdb_first_attached_cpu();
914 if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) {
915 gdbserver_state.g_cpu = gdb_first_attached_cpu();
918 if (!gdbserver_state.c_cpu) {
919 /* No more process attached */
920 gdb_disable_syscalls();
921 gdb_continue();
923 gdb_put_packet("OK");
926 static void handle_thread_alive(GArray *params, void *user_ctx)
928 CPUState *cpu;
930 if (!params->len) {
931 gdb_put_packet("E22");
932 return;
935 if (get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
936 gdb_put_packet("E22");
937 return;
940 cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
941 get_param(params, 0)->thread_id.tid);
942 if (!cpu) {
943 gdb_put_packet("E22");
944 return;
947 gdb_put_packet("OK");
950 static void handle_continue(GArray *params, void *user_ctx)
952 if (params->len) {
953 gdb_set_cpu_pc(get_param(params, 0)->val_ull);
956 gdbserver_state.signal = 0;
957 gdb_continue();
960 static void handle_cont_with_sig(GArray *params, void *user_ctx)
962 unsigned long signal = 0;
965 * Note: C sig;[addr] is currently unsupported and we simply
966 * omit the addr parameter
968 if (params->len) {
969 signal = get_param(params, 0)->val_ul;
972 gdbserver_state.signal = gdb_signal_to_target(signal);
973 if (gdbserver_state.signal == -1) {
974 gdbserver_state.signal = 0;
976 gdb_continue();
979 static void handle_set_thread(GArray *params, void *user_ctx)
981 CPUState *cpu;
983 if (params->len != 2) {
984 gdb_put_packet("E22");
985 return;
988 if (get_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) {
989 gdb_put_packet("E22");
990 return;
993 if (get_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) {
994 gdb_put_packet("OK");
995 return;
998 cpu = gdb_get_cpu(get_param(params, 1)->thread_id.pid,
999 get_param(params, 1)->thread_id.tid);
1000 if (!cpu) {
1001 gdb_put_packet("E22");
1002 return;
1006 * Note: This command is deprecated and modern gdb's will be using the
1007 * vCont command instead.
1009 switch (get_param(params, 0)->opcode) {
1010 case 'c':
1011 gdbserver_state.c_cpu = cpu;
1012 gdb_put_packet("OK");
1013 break;
1014 case 'g':
1015 gdbserver_state.g_cpu = cpu;
1016 gdb_put_packet("OK");
1017 break;
1018 default:
1019 gdb_put_packet("E22");
1020 break;
1024 static void handle_insert_bp(GArray *params, void *user_ctx)
1026 int res;
1028 if (params->len != 3) {
1029 gdb_put_packet("E22");
1030 return;
1033 res = gdb_breakpoint_insert(gdbserver_state.c_cpu,
1034 get_param(params, 0)->val_ul,
1035 get_param(params, 1)->val_ull,
1036 get_param(params, 2)->val_ull);
1037 if (res >= 0) {
1038 gdb_put_packet("OK");
1039 return;
1040 } else if (res == -ENOSYS) {
1041 gdb_put_packet("");
1042 return;
1045 gdb_put_packet("E22");
1048 static void handle_remove_bp(GArray *params, void *user_ctx)
1050 int res;
1052 if (params->len != 3) {
1053 gdb_put_packet("E22");
1054 return;
1057 res = gdb_breakpoint_remove(gdbserver_state.c_cpu,
1058 get_param(params, 0)->val_ul,
1059 get_param(params, 1)->val_ull,
1060 get_param(params, 2)->val_ull);
1061 if (res >= 0) {
1062 gdb_put_packet("OK");
1063 return;
1064 } else if (res == -ENOSYS) {
1065 gdb_put_packet("");
1066 return;
1069 gdb_put_packet("E22");
1073 * handle_set/get_reg
1075 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1076 * This works, but can be very slow. Anything new enough to understand
1077 * XML also knows how to use this properly. However to use this we
1078 * need to define a local XML file as well as be talking to a
1079 * reasonably modern gdb. Responding with an empty packet will cause
1080 * the remote gdb to fallback to older methods.
1083 static void handle_set_reg(GArray *params, void *user_ctx)
1085 int reg_size;
1087 if (!gdb_has_xml) {
1088 gdb_put_packet("");
1089 return;
1092 if (params->len != 2) {
1093 gdb_put_packet("E22");
1094 return;
1097 reg_size = strlen(get_param(params, 1)->data) / 2;
1098 gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 1)->data, reg_size);
1099 gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data,
1100 get_param(params, 0)->val_ull);
1101 gdb_put_packet("OK");
1104 static void handle_get_reg(GArray *params, void *user_ctx)
1106 int reg_size;
1108 if (!gdb_has_xml) {
1109 gdb_put_packet("");
1110 return;
1113 if (!params->len) {
1114 gdb_put_packet("E14");
1115 return;
1118 reg_size = gdb_read_register(gdbserver_state.g_cpu,
1119 gdbserver_state.mem_buf,
1120 get_param(params, 0)->val_ull);
1121 if (!reg_size) {
1122 gdb_put_packet("E14");
1123 return;
1124 } else {
1125 g_byte_array_set_size(gdbserver_state.mem_buf, reg_size);
1128 gdb_memtohex(gdbserver_state.str_buf,
1129 gdbserver_state.mem_buf->data, reg_size);
1130 gdb_put_strbuf();
1133 static void handle_write_mem(GArray *params, void *user_ctx)
1135 if (params->len != 3) {
1136 gdb_put_packet("E22");
1137 return;
1140 /* gdb_hextomem() reads 2*len bytes */
1141 if (get_param(params, 1)->val_ull >
1142 strlen(get_param(params, 2)->data) / 2) {
1143 gdb_put_packet("E22");
1144 return;
1147 gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 2)->data,
1148 get_param(params, 1)->val_ull);
1149 if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu,
1150 get_param(params, 0)->val_ull,
1151 gdbserver_state.mem_buf->data,
1152 gdbserver_state.mem_buf->len, true)) {
1153 gdb_put_packet("E14");
1154 return;
1157 gdb_put_packet("OK");
1160 static void handle_read_mem(GArray *params, void *user_ctx)
1162 if (params->len != 2) {
1163 gdb_put_packet("E22");
1164 return;
1167 /* gdb_memtohex() doubles the required space */
1168 if (get_param(params, 1)->val_ull > MAX_PACKET_LENGTH / 2) {
1169 gdb_put_packet("E22");
1170 return;
1173 g_byte_array_set_size(gdbserver_state.mem_buf,
1174 get_param(params, 1)->val_ull);
1176 if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu,
1177 get_param(params, 0)->val_ull,
1178 gdbserver_state.mem_buf->data,
1179 gdbserver_state.mem_buf->len, false)) {
1180 gdb_put_packet("E14");
1181 return;
1184 gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data,
1185 gdbserver_state.mem_buf->len);
1186 gdb_put_strbuf();
1189 static void handle_write_all_regs(GArray *params, void *user_ctx)
1191 int reg_id;
1192 size_t len;
1193 uint8_t *registers;
1194 int reg_size;
1196 if (!params->len) {
1197 return;
1200 cpu_synchronize_state(gdbserver_state.g_cpu);
1201 len = strlen(get_param(params, 0)->data) / 2;
1202 gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len);
1203 registers = gdbserver_state.mem_buf->data;
1204 for (reg_id = 0;
1205 reg_id < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0;
1206 reg_id++) {
1207 reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, reg_id);
1208 len -= reg_size;
1209 registers += reg_size;
1211 gdb_put_packet("OK");
1214 static void handle_read_all_regs(GArray *params, void *user_ctx)
1216 int reg_id;
1217 size_t len;
1219 cpu_synchronize_state(gdbserver_state.g_cpu);
1220 g_byte_array_set_size(gdbserver_state.mem_buf, 0);
1221 len = 0;
1222 for (reg_id = 0; reg_id < gdbserver_state.g_cpu->gdb_num_g_regs; reg_id++) {
1223 len += gdb_read_register(gdbserver_state.g_cpu,
1224 gdbserver_state.mem_buf,
1225 reg_id);
1227 g_assert(len == gdbserver_state.mem_buf->len);
1229 gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len);
1230 gdb_put_strbuf();
1234 static void handle_step(GArray *params, void *user_ctx)
1236 if (params->len) {
1237 gdb_set_cpu_pc(get_param(params, 0)->val_ull);
1240 cpu_single_step(gdbserver_state.c_cpu, gdbserver_state.sstep_flags);
1241 gdb_continue();
1244 static void handle_backward(GArray *params, void *user_ctx)
1246 if (!gdb_can_reverse()) {
1247 gdb_put_packet("E22");
1249 if (params->len == 1) {
1250 switch (get_param(params, 0)->opcode) {
1251 case 's':
1252 if (replay_reverse_step()) {
1253 gdb_continue();
1254 } else {
1255 gdb_put_packet("E14");
1257 return;
1258 case 'c':
1259 if (replay_reverse_continue()) {
1260 gdb_continue();
1261 } else {
1262 gdb_put_packet("E14");
1264 return;
1268 /* Default invalid command */
1269 gdb_put_packet("");
1272 static void handle_v_cont_query(GArray *params, void *user_ctx)
1274 gdb_put_packet("vCont;c;C;s;S");
1277 static void handle_v_cont(GArray *params, void *user_ctx)
1279 int res;
1281 if (!params->len) {
1282 return;
1285 res = gdb_handle_vcont(get_param(params, 0)->data);
1286 if ((res == -EINVAL) || (res == -ERANGE)) {
1287 gdb_put_packet("E22");
1288 } else if (res) {
1289 gdb_put_packet("");
1293 static void handle_v_attach(GArray *params, void *user_ctx)
1295 GDBProcess *process;
1296 CPUState *cpu;
1298 g_string_assign(gdbserver_state.str_buf, "E22");
1299 if (!params->len) {
1300 goto cleanup;
1303 process = gdb_get_process(get_param(params, 0)->val_ul);
1304 if (!process) {
1305 goto cleanup;
1308 cpu = gdb_get_first_cpu_in_process(process);
1309 if (!cpu) {
1310 goto cleanup;
1313 process->attached = true;
1314 gdbserver_state.g_cpu = cpu;
1315 gdbserver_state.c_cpu = cpu;
1317 if (gdbserver_state.allow_stop_reply) {
1318 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
1319 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
1320 g_string_append_c(gdbserver_state.str_buf, ';');
1321 gdbserver_state.allow_stop_reply = false;
1322 cleanup:
1323 gdb_put_strbuf();
1327 static void handle_v_kill(GArray *params, void *user_ctx)
1329 /* Kill the target */
1330 gdb_put_packet("OK");
1331 error_report("QEMU: Terminated via GDBstub");
1332 gdb_exit(0);
1333 exit(0);
1336 static const GdbCmdParseEntry gdb_v_commands_table[] = {
1337 /* Order is important if has same prefix */
1339 .handler = handle_v_cont_query,
1340 .cmd = "Cont?",
1341 .cmd_startswith = 1
1344 .handler = handle_v_cont,
1345 .cmd = "Cont",
1346 .cmd_startswith = 1,
1347 .allow_stop_reply = true,
1348 .schema = "s0"
1351 .handler = handle_v_attach,
1352 .cmd = "Attach;",
1353 .cmd_startswith = 1,
1354 .allow_stop_reply = true,
1355 .schema = "l0"
1358 .handler = handle_v_kill,
1359 .cmd = "Kill;",
1360 .cmd_startswith = 1
1362 #ifdef CONFIG_USER_ONLY
1364 * Host I/O Packets. See [1] for details.
1365 * [1] https://sourceware.org/gdb/onlinedocs/gdb/Host-I_002fO-Packets.html
1368 .handler = gdb_handle_v_file_open,
1369 .cmd = "File:open:",
1370 .cmd_startswith = 1,
1371 .schema = "s,L,L0"
1374 .handler = gdb_handle_v_file_close,
1375 .cmd = "File:close:",
1376 .cmd_startswith = 1,
1377 .schema = "l0"
1380 .handler = gdb_handle_v_file_pread,
1381 .cmd = "File:pread:",
1382 .cmd_startswith = 1,
1383 .schema = "l,L,L0"
1386 .handler = gdb_handle_v_file_readlink,
1387 .cmd = "File:readlink:",
1388 .cmd_startswith = 1,
1389 .schema = "s0"
1391 #endif
1394 static void handle_v_commands(GArray *params, void *user_ctx)
1396 if (!params->len) {
1397 return;
1400 if (process_string_cmd(get_param(params, 0)->data,
1401 gdb_v_commands_table,
1402 ARRAY_SIZE(gdb_v_commands_table))) {
1403 gdb_put_packet("");
1407 static void handle_query_qemu_sstepbits(GArray *params, void *user_ctx)
1409 g_string_printf(gdbserver_state.str_buf, "ENABLE=%x", SSTEP_ENABLE);
1411 if (gdbserver_state.supported_sstep_flags & SSTEP_NOIRQ) {
1412 g_string_append_printf(gdbserver_state.str_buf, ",NOIRQ=%x",
1413 SSTEP_NOIRQ);
1416 if (gdbserver_state.supported_sstep_flags & SSTEP_NOTIMER) {
1417 g_string_append_printf(gdbserver_state.str_buf, ",NOTIMER=%x",
1418 SSTEP_NOTIMER);
1421 gdb_put_strbuf();
1424 static void handle_set_qemu_sstep(GArray *params, void *user_ctx)
1426 int new_sstep_flags;
1428 if (!params->len) {
1429 return;
1432 new_sstep_flags = get_param(params, 0)->val_ul;
1434 if (new_sstep_flags & ~gdbserver_state.supported_sstep_flags) {
1435 gdb_put_packet("E22");
1436 return;
1439 gdbserver_state.sstep_flags = new_sstep_flags;
1440 gdb_put_packet("OK");
1443 static void handle_query_qemu_sstep(GArray *params, void *user_ctx)
1445 g_string_printf(gdbserver_state.str_buf, "0x%x",
1446 gdbserver_state.sstep_flags);
1447 gdb_put_strbuf();
1450 static void handle_query_curr_tid(GArray *params, void *user_ctx)
1452 CPUState *cpu;
1453 GDBProcess *process;
1456 * "Current thread" remains vague in the spec, so always return
1457 * the first thread of the current process (gdb returns the
1458 * first thread).
1460 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
1461 cpu = gdb_get_first_cpu_in_process(process);
1462 g_string_assign(gdbserver_state.str_buf, "QC");
1463 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
1464 gdb_put_strbuf();
1467 static void handle_query_threads(GArray *params, void *user_ctx)
1469 if (!gdbserver_state.query_cpu) {
1470 gdb_put_packet("l");
1471 return;
1474 g_string_assign(gdbserver_state.str_buf, "m");
1475 gdb_append_thread_id(gdbserver_state.query_cpu, gdbserver_state.str_buf);
1476 gdb_put_strbuf();
1477 gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu);
1480 static void handle_query_first_threads(GArray *params, void *user_ctx)
1482 gdbserver_state.query_cpu = gdb_first_attached_cpu();
1483 handle_query_threads(params, user_ctx);
1486 static void handle_query_thread_extra(GArray *params, void *user_ctx)
1488 g_autoptr(GString) rs = g_string_new(NULL);
1489 CPUState *cpu;
1491 if (!params->len ||
1492 get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
1493 gdb_put_packet("E22");
1494 return;
1497 cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
1498 get_param(params, 0)->thread_id.tid);
1499 if (!cpu) {
1500 return;
1503 cpu_synchronize_state(cpu);
1505 if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) {
1506 /* Print the CPU model and name in multiprocess mode */
1507 ObjectClass *oc = object_get_class(OBJECT(cpu));
1508 const char *cpu_model = object_class_get_name(oc);
1509 const char *cpu_name =
1510 object_get_canonical_path_component(OBJECT(cpu));
1511 g_string_printf(rs, "%s %s [%s]", cpu_model, cpu_name,
1512 cpu->halted ? "halted " : "running");
1513 } else {
1514 g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index,
1515 cpu->halted ? "halted " : "running");
1517 trace_gdbstub_op_extra_info(rs->str);
1518 gdb_memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len);
1519 gdb_put_strbuf();
1522 static void handle_query_supported(GArray *params, void *user_ctx)
1524 CPUClass *cc;
1526 g_string_printf(gdbserver_state.str_buf, "PacketSize=%x", MAX_PACKET_LENGTH);
1527 cc = CPU_GET_CLASS(first_cpu);
1528 if (cc->gdb_core_xml_file) {
1529 g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+");
1532 if (gdb_can_reverse()) {
1533 g_string_append(gdbserver_state.str_buf,
1534 ";ReverseStep+;ReverseContinue+");
1537 #if defined(CONFIG_USER_ONLY)
1538 #if defined(CONFIG_LINUX)
1539 if (gdbserver_state.c_cpu->opaque) {
1540 g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+");
1542 #endif
1543 g_string_append(gdbserver_state.str_buf, ";qXfer:exec-file:read+");
1544 #endif
1546 if (params->len &&
1547 strstr(get_param(params, 0)->data, "multiprocess+")) {
1548 gdbserver_state.multiprocess = true;
1551 g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+");
1552 gdb_put_strbuf();
1555 static void handle_query_xfer_features(GArray *params, void *user_ctx)
1557 GDBProcess *process;
1558 CPUClass *cc;
1559 unsigned long len, total_len, addr;
1560 const char *xml;
1561 const char *p;
1563 if (params->len < 3) {
1564 gdb_put_packet("E22");
1565 return;
1568 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
1569 cc = CPU_GET_CLASS(gdbserver_state.g_cpu);
1570 if (!cc->gdb_core_xml_file) {
1571 gdb_put_packet("");
1572 return;
1575 gdb_has_xml = true;
1576 p = get_param(params, 0)->data;
1577 xml = get_feature_xml(p, &p, process);
1578 if (!xml) {
1579 gdb_put_packet("E00");
1580 return;
1583 addr = get_param(params, 1)->val_ul;
1584 len = get_param(params, 2)->val_ul;
1585 total_len = strlen(xml);
1586 if (addr > total_len) {
1587 gdb_put_packet("E00");
1588 return;
1591 if (len > (MAX_PACKET_LENGTH - 5) / 2) {
1592 len = (MAX_PACKET_LENGTH - 5) / 2;
1595 if (len < total_len - addr) {
1596 g_string_assign(gdbserver_state.str_buf, "m");
1597 gdb_memtox(gdbserver_state.str_buf, xml + addr, len);
1598 } else {
1599 g_string_assign(gdbserver_state.str_buf, "l");
1600 gdb_memtox(gdbserver_state.str_buf, xml + addr, total_len - addr);
1603 gdb_put_packet_binary(gdbserver_state.str_buf->str,
1604 gdbserver_state.str_buf->len, true);
1607 static void handle_query_qemu_supported(GArray *params, void *user_ctx)
1609 g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep");
1610 #ifndef CONFIG_USER_ONLY
1611 g_string_append(gdbserver_state.str_buf, ";PhyMemMode");
1612 #endif
1613 gdb_put_strbuf();
1616 static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
1617 /* Order is important if has same prefix */
1619 .handler = handle_query_qemu_sstepbits,
1620 .cmd = "qemu.sstepbits",
1623 .handler = handle_query_qemu_sstep,
1624 .cmd = "qemu.sstep",
1627 .handler = handle_set_qemu_sstep,
1628 .cmd = "qemu.sstep=",
1629 .cmd_startswith = 1,
1630 .schema = "l0"
1634 static const GdbCmdParseEntry gdb_gen_query_table[] = {
1636 .handler = handle_query_curr_tid,
1637 .cmd = "C",
1640 .handler = handle_query_threads,
1641 .cmd = "sThreadInfo",
1644 .handler = handle_query_first_threads,
1645 .cmd = "fThreadInfo",
1648 .handler = handle_query_thread_extra,
1649 .cmd = "ThreadExtraInfo,",
1650 .cmd_startswith = 1,
1651 .schema = "t0"
1653 #ifdef CONFIG_USER_ONLY
1655 .handler = gdb_handle_query_offsets,
1656 .cmd = "Offsets",
1658 #else
1660 .handler = gdb_handle_query_rcmd,
1661 .cmd = "Rcmd,",
1662 .cmd_startswith = 1,
1663 .schema = "s0"
1665 #endif
1667 .handler = handle_query_supported,
1668 .cmd = "Supported:",
1669 .cmd_startswith = 1,
1670 .schema = "s0"
1673 .handler = handle_query_supported,
1674 .cmd = "Supported",
1675 .schema = "s0"
1678 .handler = handle_query_xfer_features,
1679 .cmd = "Xfer:features:read:",
1680 .cmd_startswith = 1,
1681 .schema = "s:l,l0"
1683 #if defined(CONFIG_USER_ONLY)
1684 #if defined(CONFIG_LINUX)
1686 .handler = gdb_handle_query_xfer_auxv,
1687 .cmd = "Xfer:auxv:read::",
1688 .cmd_startswith = 1,
1689 .schema = "l,l0"
1691 #endif
1693 .handler = gdb_handle_query_xfer_exec_file,
1694 .cmd = "Xfer:exec-file:read:",
1695 .cmd_startswith = 1,
1696 .schema = "l:l,l0"
1698 #endif
1700 .handler = gdb_handle_query_attached,
1701 .cmd = "Attached:",
1702 .cmd_startswith = 1
1705 .handler = gdb_handle_query_attached,
1706 .cmd = "Attached",
1709 .handler = handle_query_qemu_supported,
1710 .cmd = "qemu.Supported",
1712 #ifndef CONFIG_USER_ONLY
1714 .handler = gdb_handle_query_qemu_phy_mem_mode,
1715 .cmd = "qemu.PhyMemMode",
1717 #endif
1720 static const GdbCmdParseEntry gdb_gen_set_table[] = {
1721 /* Order is important if has same prefix */
1723 .handler = handle_set_qemu_sstep,
1724 .cmd = "qemu.sstep:",
1725 .cmd_startswith = 1,
1726 .schema = "l0"
1728 #ifndef CONFIG_USER_ONLY
1730 .handler = gdb_handle_set_qemu_phy_mem_mode,
1731 .cmd = "qemu.PhyMemMode:",
1732 .cmd_startswith = 1,
1733 .schema = "l0"
1735 #endif
1738 static void handle_gen_query(GArray *params, void *user_ctx)
1740 if (!params->len) {
1741 return;
1744 if (!process_string_cmd(get_param(params, 0)->data,
1745 gdb_gen_query_set_common_table,
1746 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
1747 return;
1750 if (process_string_cmd(get_param(params, 0)->data,
1751 gdb_gen_query_table,
1752 ARRAY_SIZE(gdb_gen_query_table))) {
1753 gdb_put_packet("");
1757 static void handle_gen_set(GArray *params, void *user_ctx)
1759 if (!params->len) {
1760 return;
1763 if (!process_string_cmd(get_param(params, 0)->data,
1764 gdb_gen_query_set_common_table,
1765 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
1766 return;
1769 if (process_string_cmd(get_param(params, 0)->data,
1770 gdb_gen_set_table,
1771 ARRAY_SIZE(gdb_gen_set_table))) {
1772 gdb_put_packet("");
1776 static void handle_target_halt(GArray *params, void *user_ctx)
1778 if (gdbserver_state.allow_stop_reply) {
1779 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
1780 gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf);
1781 g_string_append_c(gdbserver_state.str_buf, ';');
1782 gdb_put_strbuf();
1783 gdbserver_state.allow_stop_reply = false;
1786 * Remove all the breakpoints when this query is issued,
1787 * because gdb is doing an initial connect and the state
1788 * should be cleaned up.
1790 gdb_breakpoint_remove_all(gdbserver_state.c_cpu);
1793 static int gdb_handle_packet(const char *line_buf)
1795 const GdbCmdParseEntry *cmd_parser = NULL;
1797 trace_gdbstub_io_command(line_buf);
1799 switch (line_buf[0]) {
1800 case '!':
1801 gdb_put_packet("OK");
1802 break;
1803 case '?':
1805 static const GdbCmdParseEntry target_halted_cmd_desc = {
1806 .handler = handle_target_halt,
1807 .cmd = "?",
1808 .cmd_startswith = 1,
1809 .allow_stop_reply = true,
1811 cmd_parser = &target_halted_cmd_desc;
1813 break;
1814 case 'c':
1816 static const GdbCmdParseEntry continue_cmd_desc = {
1817 .handler = handle_continue,
1818 .cmd = "c",
1819 .cmd_startswith = 1,
1820 .allow_stop_reply = true,
1821 .schema = "L0"
1823 cmd_parser = &continue_cmd_desc;
1825 break;
1826 case 'C':
1828 static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
1829 .handler = handle_cont_with_sig,
1830 .cmd = "C",
1831 .cmd_startswith = 1,
1832 .allow_stop_reply = true,
1833 .schema = "l0"
1835 cmd_parser = &cont_with_sig_cmd_desc;
1837 break;
1838 case 'v':
1840 static const GdbCmdParseEntry v_cmd_desc = {
1841 .handler = handle_v_commands,
1842 .cmd = "v",
1843 .cmd_startswith = 1,
1844 .schema = "s0"
1846 cmd_parser = &v_cmd_desc;
1848 break;
1849 case 'k':
1850 /* Kill the target */
1851 error_report("QEMU: Terminated via GDBstub");
1852 gdb_exit(0);
1853 exit(0);
1854 case 'D':
1856 static const GdbCmdParseEntry detach_cmd_desc = {
1857 .handler = handle_detach,
1858 .cmd = "D",
1859 .cmd_startswith = 1,
1860 .schema = "?.l0"
1862 cmd_parser = &detach_cmd_desc;
1864 break;
1865 case 's':
1867 static const GdbCmdParseEntry step_cmd_desc = {
1868 .handler = handle_step,
1869 .cmd = "s",
1870 .cmd_startswith = 1,
1871 .allow_stop_reply = true,
1872 .schema = "L0"
1874 cmd_parser = &step_cmd_desc;
1876 break;
1877 case 'b':
1879 static const GdbCmdParseEntry backward_cmd_desc = {
1880 .handler = handle_backward,
1881 .cmd = "b",
1882 .cmd_startswith = 1,
1883 .allow_stop_reply = true,
1884 .schema = "o0"
1886 cmd_parser = &backward_cmd_desc;
1888 break;
1889 case 'F':
1891 static const GdbCmdParseEntry file_io_cmd_desc = {
1892 .handler = gdb_handle_file_io,
1893 .cmd = "F",
1894 .cmd_startswith = 1,
1895 .schema = "L,L,o0"
1897 cmd_parser = &file_io_cmd_desc;
1899 break;
1900 case 'g':
1902 static const GdbCmdParseEntry read_all_regs_cmd_desc = {
1903 .handler = handle_read_all_regs,
1904 .cmd = "g",
1905 .cmd_startswith = 1
1907 cmd_parser = &read_all_regs_cmd_desc;
1909 break;
1910 case 'G':
1912 static const GdbCmdParseEntry write_all_regs_cmd_desc = {
1913 .handler = handle_write_all_regs,
1914 .cmd = "G",
1915 .cmd_startswith = 1,
1916 .schema = "s0"
1918 cmd_parser = &write_all_regs_cmd_desc;
1920 break;
1921 case 'm':
1923 static const GdbCmdParseEntry read_mem_cmd_desc = {
1924 .handler = handle_read_mem,
1925 .cmd = "m",
1926 .cmd_startswith = 1,
1927 .schema = "L,L0"
1929 cmd_parser = &read_mem_cmd_desc;
1931 break;
1932 case 'M':
1934 static const GdbCmdParseEntry write_mem_cmd_desc = {
1935 .handler = handle_write_mem,
1936 .cmd = "M",
1937 .cmd_startswith = 1,
1938 .schema = "L,L:s0"
1940 cmd_parser = &write_mem_cmd_desc;
1942 break;
1943 case 'p':
1945 static const GdbCmdParseEntry get_reg_cmd_desc = {
1946 .handler = handle_get_reg,
1947 .cmd = "p",
1948 .cmd_startswith = 1,
1949 .schema = "L0"
1951 cmd_parser = &get_reg_cmd_desc;
1953 break;
1954 case 'P':
1956 static const GdbCmdParseEntry set_reg_cmd_desc = {
1957 .handler = handle_set_reg,
1958 .cmd = "P",
1959 .cmd_startswith = 1,
1960 .schema = "L?s0"
1962 cmd_parser = &set_reg_cmd_desc;
1964 break;
1965 case 'Z':
1967 static const GdbCmdParseEntry insert_bp_cmd_desc = {
1968 .handler = handle_insert_bp,
1969 .cmd = "Z",
1970 .cmd_startswith = 1,
1971 .schema = "l?L?L0"
1973 cmd_parser = &insert_bp_cmd_desc;
1975 break;
1976 case 'z':
1978 static const GdbCmdParseEntry remove_bp_cmd_desc = {
1979 .handler = handle_remove_bp,
1980 .cmd = "z",
1981 .cmd_startswith = 1,
1982 .schema = "l?L?L0"
1984 cmd_parser = &remove_bp_cmd_desc;
1986 break;
1987 case 'H':
1989 static const GdbCmdParseEntry set_thread_cmd_desc = {
1990 .handler = handle_set_thread,
1991 .cmd = "H",
1992 .cmd_startswith = 1,
1993 .schema = "o.t0"
1995 cmd_parser = &set_thread_cmd_desc;
1997 break;
1998 case 'T':
2000 static const GdbCmdParseEntry thread_alive_cmd_desc = {
2001 .handler = handle_thread_alive,
2002 .cmd = "T",
2003 .cmd_startswith = 1,
2004 .schema = "t0"
2006 cmd_parser = &thread_alive_cmd_desc;
2008 break;
2009 case 'q':
2011 static const GdbCmdParseEntry gen_query_cmd_desc = {
2012 .handler = handle_gen_query,
2013 .cmd = "q",
2014 .cmd_startswith = 1,
2015 .schema = "s0"
2017 cmd_parser = &gen_query_cmd_desc;
2019 break;
2020 case 'Q':
2022 static const GdbCmdParseEntry gen_set_cmd_desc = {
2023 .handler = handle_gen_set,
2024 .cmd = "Q",
2025 .cmd_startswith = 1,
2026 .schema = "s0"
2028 cmd_parser = &gen_set_cmd_desc;
2030 break;
2031 default:
2032 /* put empty packet */
2033 gdb_put_packet("");
2034 break;
2037 if (cmd_parser) {
2038 run_cmd_parser(line_buf, cmd_parser);
2041 return RS_IDLE;
2044 void gdb_set_stop_cpu(CPUState *cpu)
2046 GDBProcess *p = gdb_get_cpu_process(cpu);
2048 if (!p->attached) {
2050 * Having a stop CPU corresponding to a process that is not attached
2051 * confuses GDB. So we ignore the request.
2053 return;
2056 gdbserver_state.c_cpu = cpu;
2057 gdbserver_state.g_cpu = cpu;
2060 void gdb_read_byte(uint8_t ch)
2062 uint8_t reply;
2064 gdbserver_state.allow_stop_reply = false;
2065 #ifndef CONFIG_USER_ONLY
2066 if (gdbserver_state.last_packet->len) {
2067 /* Waiting for a response to the last packet. If we see the start
2068 of a new command then abandon the previous response. */
2069 if (ch == '-') {
2070 trace_gdbstub_err_got_nack();
2071 gdb_put_buffer(gdbserver_state.last_packet->data,
2072 gdbserver_state.last_packet->len);
2073 } else if (ch == '+') {
2074 trace_gdbstub_io_got_ack();
2075 } else {
2076 trace_gdbstub_io_got_unexpected(ch);
2079 if (ch == '+' || ch == '$') {
2080 g_byte_array_set_size(gdbserver_state.last_packet, 0);
2082 if (ch != '$')
2083 return;
2085 if (runstate_is_running()) {
2087 * When the CPU is running, we cannot do anything except stop
2088 * it when receiving a char. This is expected on a Ctrl-C in the
2089 * gdb client. Because we are in all-stop mode, gdb sends a
2090 * 0x03 byte which is not a usual packet, so we handle it specially
2091 * here, but it does expect a stop reply.
2093 if (ch != 0x03) {
2094 trace_gdbstub_err_unexpected_runpkt(ch);
2095 } else {
2096 gdbserver_state.allow_stop_reply = true;
2098 vm_stop(RUN_STATE_PAUSED);
2099 } else
2100 #endif
2102 switch(gdbserver_state.state) {
2103 case RS_IDLE:
2104 if (ch == '$') {
2105 /* start of command packet */
2106 gdbserver_state.line_buf_index = 0;
2107 gdbserver_state.line_sum = 0;
2108 gdbserver_state.state = RS_GETLINE;
2109 } else if (ch == '+') {
2111 * do nothing, gdb may preemptively send out ACKs on
2112 * initial connection
2114 } else {
2115 trace_gdbstub_err_garbage(ch);
2117 break;
2118 case RS_GETLINE:
2119 if (ch == '}') {
2120 /* start escape sequence */
2121 gdbserver_state.state = RS_GETLINE_ESC;
2122 gdbserver_state.line_sum += ch;
2123 } else if (ch == '*') {
2124 /* start run length encoding sequence */
2125 gdbserver_state.state = RS_GETLINE_RLE;
2126 gdbserver_state.line_sum += ch;
2127 } else if (ch == '#') {
2128 /* end of command, start of checksum*/
2129 gdbserver_state.state = RS_CHKSUM1;
2130 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
2131 trace_gdbstub_err_overrun();
2132 gdbserver_state.state = RS_IDLE;
2133 } else {
2134 /* unescaped command character */
2135 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch;
2136 gdbserver_state.line_sum += ch;
2138 break;
2139 case RS_GETLINE_ESC:
2140 if (ch == '#') {
2141 /* unexpected end of command in escape sequence */
2142 gdbserver_state.state = RS_CHKSUM1;
2143 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
2144 /* command buffer overrun */
2145 trace_gdbstub_err_overrun();
2146 gdbserver_state.state = RS_IDLE;
2147 } else {
2148 /* parse escaped character and leave escape state */
2149 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20;
2150 gdbserver_state.line_sum += ch;
2151 gdbserver_state.state = RS_GETLINE;
2153 break;
2154 case RS_GETLINE_RLE:
2156 * Run-length encoding is explained in "Debugging with GDB /
2157 * Appendix E GDB Remote Serial Protocol / Overview".
2159 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
2160 /* invalid RLE count encoding */
2161 trace_gdbstub_err_invalid_repeat(ch);
2162 gdbserver_state.state = RS_GETLINE;
2163 } else {
2164 /* decode repeat length */
2165 int repeat = ch - ' ' + 3;
2166 if (gdbserver_state.line_buf_index + repeat >= sizeof(gdbserver_state.line_buf) - 1) {
2167 /* that many repeats would overrun the command buffer */
2168 trace_gdbstub_err_overrun();
2169 gdbserver_state.state = RS_IDLE;
2170 } else if (gdbserver_state.line_buf_index < 1) {
2171 /* got a repeat but we have nothing to repeat */
2172 trace_gdbstub_err_invalid_rle();
2173 gdbserver_state.state = RS_GETLINE;
2174 } else {
2175 /* repeat the last character */
2176 memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index,
2177 gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat);
2178 gdbserver_state.line_buf_index += repeat;
2179 gdbserver_state.line_sum += ch;
2180 gdbserver_state.state = RS_GETLINE;
2183 break;
2184 case RS_CHKSUM1:
2185 /* get high hex digit of checksum */
2186 if (!isxdigit(ch)) {
2187 trace_gdbstub_err_checksum_invalid(ch);
2188 gdbserver_state.state = RS_GETLINE;
2189 break;
2191 gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0';
2192 gdbserver_state.line_csum = fromhex(ch) << 4;
2193 gdbserver_state.state = RS_CHKSUM2;
2194 break;
2195 case RS_CHKSUM2:
2196 /* get low hex digit of checksum */
2197 if (!isxdigit(ch)) {
2198 trace_gdbstub_err_checksum_invalid(ch);
2199 gdbserver_state.state = RS_GETLINE;
2200 break;
2202 gdbserver_state.line_csum |= fromhex(ch);
2204 if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) {
2205 trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum);
2206 /* send NAK reply */
2207 reply = '-';
2208 gdb_put_buffer(&reply, 1);
2209 gdbserver_state.state = RS_IDLE;
2210 } else {
2211 /* send ACK reply */
2212 reply = '+';
2213 gdb_put_buffer(&reply, 1);
2214 gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf);
2216 break;
2217 default:
2218 abort();
2224 * Create the process that will contain all the "orphan" CPUs (that are not
2225 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2226 * be attachable and thus will be invisible to the user.
2228 void gdb_create_default_process(GDBState *s)
2230 GDBProcess *process;
2231 int pid;
2233 #ifdef CONFIG_USER_ONLY
2234 assert(gdbserver_state.process_num == 0);
2235 pid = getpid();
2236 #else
2237 if (gdbserver_state.process_num) {
2238 pid = s->processes[s->process_num - 1].pid;
2239 } else {
2240 pid = 0;
2242 /* We need an available PID slot for this process */
2243 assert(pid < UINT32_MAX);
2244 pid++;
2245 #endif
2247 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2248 process = &s->processes[s->process_num - 1];
2249 process->pid = pid;
2250 process->attached = false;
2251 process->target_xml = NULL;