gdbstub: Report the actual qemu-user pid
[qemu/kevin.git] / gdbstub / gdbstub.c
blob697dd4bbadec01f617373fe2a4c4894ac5c17a8f
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 size_t len;
358 int i;
359 const char *name;
360 CPUState *cpu = gdb_get_first_cpu_in_process(process);
361 CPUClass *cc = CPU_GET_CLASS(cpu);
363 len = 0;
364 while (p[len] && p[len] != ':')
365 len++;
366 *newp = p + len;
368 name = NULL;
369 if (strncmp(p, "target.xml", len) == 0) {
370 char *buf = process->target_xml;
371 const size_t buf_sz = sizeof(process->target_xml);
373 /* Generate the XML description for this CPU. */
374 if (!buf[0]) {
375 GDBRegisterState *r;
377 pstrcat(buf, buf_sz,
378 "<?xml version=\"1.0\"?>"
379 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
380 "<target>");
381 if (cc->gdb_arch_name) {
382 gchar *arch = cc->gdb_arch_name(cpu);
383 pstrcat(buf, buf_sz, "<architecture>");
384 pstrcat(buf, buf_sz, arch);
385 pstrcat(buf, buf_sz, "</architecture>");
386 g_free(arch);
388 pstrcat(buf, buf_sz, "<xi:include href=\"");
389 pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
390 pstrcat(buf, buf_sz, "\"/>");
391 for (r = cpu->gdb_regs; r; r = r->next) {
392 pstrcat(buf, buf_sz, "<xi:include href=\"");
393 pstrcat(buf, buf_sz, r->xml);
394 pstrcat(buf, buf_sz, "\"/>");
396 pstrcat(buf, buf_sz, "</target>");
398 return buf;
400 if (cc->gdb_get_dynamic_xml) {
401 char *xmlname = g_strndup(p, len);
402 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
404 g_free(xmlname);
405 if (xml) {
406 return xml;
409 for (i = 0; ; i++) {
410 name = xml_builtin[i][0];
411 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
412 break;
414 return name ? xml_builtin[i][1] : NULL;
417 static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
419 CPUClass *cc = CPU_GET_CLASS(cpu);
420 CPUArchState *env = cpu->env_ptr;
421 GDBRegisterState *r;
423 if (reg < cc->gdb_num_core_regs) {
424 return cc->gdb_read_register(cpu, buf, reg);
427 for (r = cpu->gdb_regs; r; r = r->next) {
428 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
429 return r->get_reg(env, buf, reg - r->base_reg);
432 return 0;
435 static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
437 CPUClass *cc = CPU_GET_CLASS(cpu);
438 CPUArchState *env = cpu->env_ptr;
439 GDBRegisterState *r;
441 if (reg < cc->gdb_num_core_regs) {
442 return cc->gdb_write_register(cpu, mem_buf, reg);
445 for (r = cpu->gdb_regs; r; r = r->next) {
446 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
447 return r->set_reg(env, mem_buf, reg - r->base_reg);
450 return 0;
453 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
454 specifies the first register number and these registers are included in
455 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
456 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
459 void gdb_register_coprocessor(CPUState *cpu,
460 gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
461 int num_regs, const char *xml, int g_pos)
463 GDBRegisterState *s;
464 GDBRegisterState **p;
466 p = &cpu->gdb_regs;
467 while (*p) {
468 /* Check for duplicates. */
469 if (strcmp((*p)->xml, xml) == 0)
470 return;
471 p = &(*p)->next;
474 s = g_new0(GDBRegisterState, 1);
475 s->base_reg = cpu->gdb_num_regs;
476 s->num_regs = num_regs;
477 s->get_reg = get_reg;
478 s->set_reg = set_reg;
479 s->xml = xml;
481 /* Add to end of list. */
482 cpu->gdb_num_regs += num_regs;
483 *p = s;
484 if (g_pos) {
485 if (g_pos != s->base_reg) {
486 error_report("Error: Bad gdb register numbering for '%s', "
487 "expected %d got %d", xml, g_pos, s->base_reg);
488 } else {
489 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
494 static void gdb_process_breakpoint_remove_all(GDBProcess *p)
496 CPUState *cpu = gdb_get_first_cpu_in_process(p);
498 while (cpu) {
499 gdb_breakpoint_remove_all(cpu);
500 cpu = gdb_next_cpu_in_process(cpu);
505 static void gdb_set_cpu_pc(vaddr pc)
507 CPUState *cpu = gdbserver_state.c_cpu;
509 cpu_synchronize_state(cpu);
510 cpu_set_pc(cpu, pc);
513 void gdb_append_thread_id(CPUState *cpu, GString *buf)
515 if (gdbserver_state.multiprocess) {
516 g_string_append_printf(buf, "p%02x.%02x",
517 gdb_get_cpu_pid(cpu), gdb_get_cpu_index(cpu));
518 } else {
519 g_string_append_printf(buf, "%02x", gdb_get_cpu_index(cpu));
523 static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
524 uint32_t *pid, uint32_t *tid)
526 unsigned long p, t;
527 int ret;
529 if (*buf == 'p') {
530 buf++;
531 ret = qemu_strtoul(buf, &buf, 16, &p);
533 if (ret) {
534 return GDB_READ_THREAD_ERR;
537 /* Skip '.' */
538 buf++;
539 } else {
540 p = 1;
543 ret = qemu_strtoul(buf, &buf, 16, &t);
545 if (ret) {
546 return GDB_READ_THREAD_ERR;
549 *end_buf = buf;
551 if (p == -1) {
552 return GDB_ALL_PROCESSES;
555 if (pid) {
556 *pid = p;
559 if (t == -1) {
560 return GDB_ALL_THREADS;
563 if (tid) {
564 *tid = t;
567 return GDB_ONE_THREAD;
571 * gdb_handle_vcont - Parses and handles a vCont packet.
572 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
573 * a format error, 0 on success.
575 static int gdb_handle_vcont(const char *p)
577 int res, signal = 0;
578 char cur_action;
579 unsigned long tmp;
580 uint32_t pid, tid;
581 GDBProcess *process;
582 CPUState *cpu;
583 GDBThreadIdKind kind;
584 unsigned int max_cpus = gdb_get_max_cpus();
585 /* uninitialised CPUs stay 0 */
586 g_autofree char *newstates = g_new0(char, max_cpus);
588 /* mark valid CPUs with 1 */
589 CPU_FOREACH(cpu) {
590 newstates[cpu->cpu_index] = 1;
594 * res keeps track of what error we are returning, with -ENOTSUP meaning
595 * that the command is unknown or unsupported, thus returning an empty
596 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
597 * or incorrect parameters passed.
599 res = 0;
600 while (*p) {
601 if (*p++ != ';') {
602 return -ENOTSUP;
605 cur_action = *p++;
606 if (cur_action == 'C' || cur_action == 'S') {
607 cur_action = qemu_tolower(cur_action);
608 res = qemu_strtoul(p, &p, 16, &tmp);
609 if (res) {
610 return res;
612 signal = gdb_signal_to_target(tmp);
613 } else if (cur_action != 'c' && cur_action != 's') {
614 /* unknown/invalid/unsupported command */
615 return -ENOTSUP;
618 if (*p == '\0' || *p == ';') {
620 * No thread specifier, action is on "all threads". The
621 * specification is unclear regarding the process to act on. We
622 * choose all processes.
624 kind = GDB_ALL_PROCESSES;
625 } else if (*p++ == ':') {
626 kind = read_thread_id(p, &p, &pid, &tid);
627 } else {
628 return -ENOTSUP;
631 switch (kind) {
632 case GDB_READ_THREAD_ERR:
633 return -EINVAL;
635 case GDB_ALL_PROCESSES:
636 cpu = gdb_first_attached_cpu();
637 while (cpu) {
638 if (newstates[cpu->cpu_index] == 1) {
639 newstates[cpu->cpu_index] = cur_action;
642 cpu = gdb_next_attached_cpu(cpu);
644 break;
646 case GDB_ALL_THREADS:
647 process = gdb_get_process(pid);
649 if (!process->attached) {
650 return -EINVAL;
653 cpu = gdb_get_first_cpu_in_process(process);
654 while (cpu) {
655 if (newstates[cpu->cpu_index] == 1) {
656 newstates[cpu->cpu_index] = cur_action;
659 cpu = gdb_next_cpu_in_process(cpu);
661 break;
663 case GDB_ONE_THREAD:
664 cpu = gdb_get_cpu(pid, tid);
666 /* invalid CPU/thread specified */
667 if (!cpu) {
668 return -EINVAL;
671 /* only use if no previous match occourred */
672 if (newstates[cpu->cpu_index] == 1) {
673 newstates[cpu->cpu_index] = cur_action;
675 break;
679 gdbserver_state.signal = signal;
680 gdb_continue_partial(newstates);
681 return res;
684 static const char *cmd_next_param(const char *param, const char delimiter)
686 static const char all_delimiters[] = ",;:=";
687 char curr_delimiters[2] = {0};
688 const char *delimiters;
690 if (delimiter == '?') {
691 delimiters = all_delimiters;
692 } else if (delimiter == '0') {
693 return strchr(param, '\0');
694 } else if (delimiter == '.' && *param) {
695 return param + 1;
696 } else {
697 curr_delimiters[0] = delimiter;
698 delimiters = curr_delimiters;
701 param += strcspn(param, delimiters);
702 if (*param) {
703 param++;
705 return param;
708 static int cmd_parse_params(const char *data, const char *schema,
709 GArray *params)
711 const char *curr_schema, *curr_data;
713 g_assert(schema);
714 g_assert(params->len == 0);
716 curr_schema = schema;
717 curr_data = data;
718 while (curr_schema[0] && curr_schema[1] && *curr_data) {
719 GdbCmdVariant this_param;
721 switch (curr_schema[0]) {
722 case 'l':
723 if (qemu_strtoul(curr_data, &curr_data, 16,
724 &this_param.val_ul)) {
725 return -EINVAL;
727 curr_data = cmd_next_param(curr_data, curr_schema[1]);
728 g_array_append_val(params, this_param);
729 break;
730 case 'L':
731 if (qemu_strtou64(curr_data, &curr_data, 16,
732 (uint64_t *)&this_param.val_ull)) {
733 return -EINVAL;
735 curr_data = cmd_next_param(curr_data, curr_schema[1]);
736 g_array_append_val(params, this_param);
737 break;
738 case 's':
739 this_param.data = curr_data;
740 curr_data = cmd_next_param(curr_data, curr_schema[1]);
741 g_array_append_val(params, this_param);
742 break;
743 case 'o':
744 this_param.opcode = *(uint8_t *)curr_data;
745 curr_data = cmd_next_param(curr_data, curr_schema[1]);
746 g_array_append_val(params, this_param);
747 break;
748 case 't':
749 this_param.thread_id.kind =
750 read_thread_id(curr_data, &curr_data,
751 &this_param.thread_id.pid,
752 &this_param.thread_id.tid);
753 curr_data = cmd_next_param(curr_data, curr_schema[1]);
754 g_array_append_val(params, this_param);
755 break;
756 case '?':
757 curr_data = cmd_next_param(curr_data, curr_schema[1]);
758 break;
759 default:
760 return -EINVAL;
762 curr_schema += 2;
765 return 0;
768 typedef void (*GdbCmdHandler)(GArray *params, void *user_ctx);
771 * cmd_startswith -> cmd is compared using startswith
773 * allow_stop_reply -> true iff the gdbstub can respond to this command with a
774 * "stop reply" packet. The list of commands that accept such response is
775 * defined at the GDB Remote Serial Protocol documentation. see:
776 * https://sourceware.org/gdb/onlinedocs/gdb/Stop-Reply-Packets.html#Stop-Reply-Packets.
778 * schema definitions:
779 * Each schema parameter entry consists of 2 chars,
780 * the first char represents the parameter type handling
781 * the second char represents the delimiter for the next parameter
783 * Currently supported schema types:
784 * 'l' -> unsigned long (stored in .val_ul)
785 * 'L' -> unsigned long long (stored in .val_ull)
786 * 's' -> string (stored in .data)
787 * 'o' -> single char (stored in .opcode)
788 * 't' -> thread id (stored in .thread_id)
789 * '?' -> skip according to delimiter
791 * Currently supported delimiters:
792 * '?' -> Stop at any delimiter (",;:=\0")
793 * '0' -> Stop at "\0"
794 * '.' -> Skip 1 char unless reached "\0"
795 * Any other value is treated as the delimiter value itself
797 typedef struct GdbCmdParseEntry {
798 GdbCmdHandler handler;
799 const char *cmd;
800 bool cmd_startswith;
801 const char *schema;
802 bool allow_stop_reply;
803 } GdbCmdParseEntry;
805 static inline int startswith(const char *string, const char *pattern)
807 return !strncmp(string, pattern, strlen(pattern));
810 static int process_string_cmd(void *user_ctx, const char *data,
811 const GdbCmdParseEntry *cmds, int num_cmds)
813 int i;
814 g_autoptr(GArray) params = g_array_new(false, true, sizeof(GdbCmdVariant));
816 if (!cmds) {
817 return -1;
820 for (i = 0; i < num_cmds; i++) {
821 const GdbCmdParseEntry *cmd = &cmds[i];
822 g_assert(cmd->handler && cmd->cmd);
824 if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
825 (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
826 continue;
829 if (cmd->schema) {
830 if (cmd_parse_params(&data[strlen(cmd->cmd)],
831 cmd->schema, params)) {
832 return -1;
836 gdbserver_state.allow_stop_reply = cmd->allow_stop_reply;
837 cmd->handler(params, user_ctx);
838 return 0;
841 return -1;
844 static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd)
846 if (!data) {
847 return;
850 g_string_set_size(gdbserver_state.str_buf, 0);
851 g_byte_array_set_size(gdbserver_state.mem_buf, 0);
853 /* In case there was an error during the command parsing we must
854 * send a NULL packet to indicate the command is not supported */
855 if (process_string_cmd(NULL, data, cmd, 1)) {
856 gdb_put_packet("");
860 static void handle_detach(GArray *params, void *user_ctx)
862 GDBProcess *process;
863 uint32_t pid = 1;
865 if (gdbserver_state.multiprocess) {
866 if (!params->len) {
867 gdb_put_packet("E22");
868 return;
871 pid = get_param(params, 0)->val_ul;
874 process = gdb_get_process(pid);
875 gdb_process_breakpoint_remove_all(process);
876 process->attached = false;
878 if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) {
879 gdbserver_state.c_cpu = gdb_first_attached_cpu();
882 if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) {
883 gdbserver_state.g_cpu = gdb_first_attached_cpu();
886 if (!gdbserver_state.c_cpu) {
887 /* No more process attached */
888 gdb_disable_syscalls();
889 gdb_continue();
891 gdb_put_packet("OK");
894 static void handle_thread_alive(GArray *params, void *user_ctx)
896 CPUState *cpu;
898 if (!params->len) {
899 gdb_put_packet("E22");
900 return;
903 if (get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
904 gdb_put_packet("E22");
905 return;
908 cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
909 get_param(params, 0)->thread_id.tid);
910 if (!cpu) {
911 gdb_put_packet("E22");
912 return;
915 gdb_put_packet("OK");
918 static void handle_continue(GArray *params, void *user_ctx)
920 if (params->len) {
921 gdb_set_cpu_pc(get_param(params, 0)->val_ull);
924 gdbserver_state.signal = 0;
925 gdb_continue();
928 static void handle_cont_with_sig(GArray *params, void *user_ctx)
930 unsigned long signal = 0;
933 * Note: C sig;[addr] is currently unsupported and we simply
934 * omit the addr parameter
936 if (params->len) {
937 signal = get_param(params, 0)->val_ul;
940 gdbserver_state.signal = gdb_signal_to_target(signal);
941 if (gdbserver_state.signal == -1) {
942 gdbserver_state.signal = 0;
944 gdb_continue();
947 static void handle_set_thread(GArray *params, void *user_ctx)
949 CPUState *cpu;
951 if (params->len != 2) {
952 gdb_put_packet("E22");
953 return;
956 if (get_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) {
957 gdb_put_packet("E22");
958 return;
961 if (get_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) {
962 gdb_put_packet("OK");
963 return;
966 cpu = gdb_get_cpu(get_param(params, 1)->thread_id.pid,
967 get_param(params, 1)->thread_id.tid);
968 if (!cpu) {
969 gdb_put_packet("E22");
970 return;
974 * Note: This command is deprecated and modern gdb's will be using the
975 * vCont command instead.
977 switch (get_param(params, 0)->opcode) {
978 case 'c':
979 gdbserver_state.c_cpu = cpu;
980 gdb_put_packet("OK");
981 break;
982 case 'g':
983 gdbserver_state.g_cpu = cpu;
984 gdb_put_packet("OK");
985 break;
986 default:
987 gdb_put_packet("E22");
988 break;
992 static void handle_insert_bp(GArray *params, void *user_ctx)
994 int res;
996 if (params->len != 3) {
997 gdb_put_packet("E22");
998 return;
1001 res = gdb_breakpoint_insert(gdbserver_state.c_cpu,
1002 get_param(params, 0)->val_ul,
1003 get_param(params, 1)->val_ull,
1004 get_param(params, 2)->val_ull);
1005 if (res >= 0) {
1006 gdb_put_packet("OK");
1007 return;
1008 } else if (res == -ENOSYS) {
1009 gdb_put_packet("");
1010 return;
1013 gdb_put_packet("E22");
1016 static void handle_remove_bp(GArray *params, void *user_ctx)
1018 int res;
1020 if (params->len != 3) {
1021 gdb_put_packet("E22");
1022 return;
1025 res = gdb_breakpoint_remove(gdbserver_state.c_cpu,
1026 get_param(params, 0)->val_ul,
1027 get_param(params, 1)->val_ull,
1028 get_param(params, 2)->val_ull);
1029 if (res >= 0) {
1030 gdb_put_packet("OK");
1031 return;
1032 } else if (res == -ENOSYS) {
1033 gdb_put_packet("");
1034 return;
1037 gdb_put_packet("E22");
1041 * handle_set/get_reg
1043 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1044 * This works, but can be very slow. Anything new enough to understand
1045 * XML also knows how to use this properly. However to use this we
1046 * need to define a local XML file as well as be talking to a
1047 * reasonably modern gdb. Responding with an empty packet will cause
1048 * the remote gdb to fallback to older methods.
1051 static void handle_set_reg(GArray *params, void *user_ctx)
1053 int reg_size;
1055 if (!gdb_has_xml) {
1056 gdb_put_packet("");
1057 return;
1060 if (params->len != 2) {
1061 gdb_put_packet("E22");
1062 return;
1065 reg_size = strlen(get_param(params, 1)->data) / 2;
1066 gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 1)->data, reg_size);
1067 gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data,
1068 get_param(params, 0)->val_ull);
1069 gdb_put_packet("OK");
1072 static void handle_get_reg(GArray *params, void *user_ctx)
1074 int reg_size;
1076 if (!gdb_has_xml) {
1077 gdb_put_packet("");
1078 return;
1081 if (!params->len) {
1082 gdb_put_packet("E14");
1083 return;
1086 reg_size = gdb_read_register(gdbserver_state.g_cpu,
1087 gdbserver_state.mem_buf,
1088 get_param(params, 0)->val_ull);
1089 if (!reg_size) {
1090 gdb_put_packet("E14");
1091 return;
1092 } else {
1093 g_byte_array_set_size(gdbserver_state.mem_buf, reg_size);
1096 gdb_memtohex(gdbserver_state.str_buf,
1097 gdbserver_state.mem_buf->data, reg_size);
1098 gdb_put_strbuf();
1101 static void handle_write_mem(GArray *params, void *user_ctx)
1103 if (params->len != 3) {
1104 gdb_put_packet("E22");
1105 return;
1108 /* gdb_hextomem() reads 2*len bytes */
1109 if (get_param(params, 1)->val_ull >
1110 strlen(get_param(params, 2)->data) / 2) {
1111 gdb_put_packet("E22");
1112 return;
1115 gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 2)->data,
1116 get_param(params, 1)->val_ull);
1117 if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu,
1118 get_param(params, 0)->val_ull,
1119 gdbserver_state.mem_buf->data,
1120 gdbserver_state.mem_buf->len, true)) {
1121 gdb_put_packet("E14");
1122 return;
1125 gdb_put_packet("OK");
1128 static void handle_read_mem(GArray *params, void *user_ctx)
1130 if (params->len != 2) {
1131 gdb_put_packet("E22");
1132 return;
1135 /* gdb_memtohex() doubles the required space */
1136 if (get_param(params, 1)->val_ull > MAX_PACKET_LENGTH / 2) {
1137 gdb_put_packet("E22");
1138 return;
1141 g_byte_array_set_size(gdbserver_state.mem_buf,
1142 get_param(params, 1)->val_ull);
1144 if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu,
1145 get_param(params, 0)->val_ull,
1146 gdbserver_state.mem_buf->data,
1147 gdbserver_state.mem_buf->len, false)) {
1148 gdb_put_packet("E14");
1149 return;
1152 gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data,
1153 gdbserver_state.mem_buf->len);
1154 gdb_put_strbuf();
1157 static void handle_write_all_regs(GArray *params, void *user_ctx)
1159 int reg_id;
1160 size_t len;
1161 uint8_t *registers;
1162 int reg_size;
1164 if (!params->len) {
1165 return;
1168 cpu_synchronize_state(gdbserver_state.g_cpu);
1169 len = strlen(get_param(params, 0)->data) / 2;
1170 gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len);
1171 registers = gdbserver_state.mem_buf->data;
1172 for (reg_id = 0;
1173 reg_id < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0;
1174 reg_id++) {
1175 reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, reg_id);
1176 len -= reg_size;
1177 registers += reg_size;
1179 gdb_put_packet("OK");
1182 static void handle_read_all_regs(GArray *params, void *user_ctx)
1184 int reg_id;
1185 size_t len;
1187 cpu_synchronize_state(gdbserver_state.g_cpu);
1188 g_byte_array_set_size(gdbserver_state.mem_buf, 0);
1189 len = 0;
1190 for (reg_id = 0; reg_id < gdbserver_state.g_cpu->gdb_num_g_regs; reg_id++) {
1191 len += gdb_read_register(gdbserver_state.g_cpu,
1192 gdbserver_state.mem_buf,
1193 reg_id);
1195 g_assert(len == gdbserver_state.mem_buf->len);
1197 gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len);
1198 gdb_put_strbuf();
1202 static void handle_step(GArray *params, void *user_ctx)
1204 if (params->len) {
1205 gdb_set_cpu_pc(get_param(params, 0)->val_ull);
1208 cpu_single_step(gdbserver_state.c_cpu, gdbserver_state.sstep_flags);
1209 gdb_continue();
1212 static void handle_backward(GArray *params, void *user_ctx)
1214 if (!gdb_can_reverse()) {
1215 gdb_put_packet("E22");
1217 if (params->len == 1) {
1218 switch (get_param(params, 0)->opcode) {
1219 case 's':
1220 if (replay_reverse_step()) {
1221 gdb_continue();
1222 } else {
1223 gdb_put_packet("E14");
1225 return;
1226 case 'c':
1227 if (replay_reverse_continue()) {
1228 gdb_continue();
1229 } else {
1230 gdb_put_packet("E14");
1232 return;
1236 /* Default invalid command */
1237 gdb_put_packet("");
1240 static void handle_v_cont_query(GArray *params, void *user_ctx)
1242 gdb_put_packet("vCont;c;C;s;S");
1245 static void handle_v_cont(GArray *params, void *user_ctx)
1247 int res;
1249 if (!params->len) {
1250 return;
1253 res = gdb_handle_vcont(get_param(params, 0)->data);
1254 if ((res == -EINVAL) || (res == -ERANGE)) {
1255 gdb_put_packet("E22");
1256 } else if (res) {
1257 gdb_put_packet("");
1261 static void handle_v_attach(GArray *params, void *user_ctx)
1263 GDBProcess *process;
1264 CPUState *cpu;
1266 g_string_assign(gdbserver_state.str_buf, "E22");
1267 if (!params->len) {
1268 goto cleanup;
1271 process = gdb_get_process(get_param(params, 0)->val_ul);
1272 if (!process) {
1273 goto cleanup;
1276 cpu = gdb_get_first_cpu_in_process(process);
1277 if (!cpu) {
1278 goto cleanup;
1281 process->attached = true;
1282 gdbserver_state.g_cpu = cpu;
1283 gdbserver_state.c_cpu = cpu;
1285 if (gdbserver_state.allow_stop_reply) {
1286 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
1287 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
1288 g_string_append_c(gdbserver_state.str_buf, ';');
1289 gdbserver_state.allow_stop_reply = false;
1290 cleanup:
1291 gdb_put_strbuf();
1295 static void handle_v_kill(GArray *params, void *user_ctx)
1297 /* Kill the target */
1298 gdb_put_packet("OK");
1299 error_report("QEMU: Terminated via GDBstub");
1300 gdb_exit(0);
1301 exit(0);
1304 static const GdbCmdParseEntry gdb_v_commands_table[] = {
1305 /* Order is important if has same prefix */
1307 .handler = handle_v_cont_query,
1308 .cmd = "Cont?",
1309 .cmd_startswith = 1
1312 .handler = handle_v_cont,
1313 .cmd = "Cont",
1314 .cmd_startswith = 1,
1315 .allow_stop_reply = true,
1316 .schema = "s0"
1319 .handler = handle_v_attach,
1320 .cmd = "Attach;",
1321 .cmd_startswith = 1,
1322 .allow_stop_reply = true,
1323 .schema = "l0"
1326 .handler = handle_v_kill,
1327 .cmd = "Kill;",
1328 .cmd_startswith = 1
1332 static void handle_v_commands(GArray *params, void *user_ctx)
1334 if (!params->len) {
1335 return;
1338 if (process_string_cmd(NULL, get_param(params, 0)->data,
1339 gdb_v_commands_table,
1340 ARRAY_SIZE(gdb_v_commands_table))) {
1341 gdb_put_packet("");
1345 static void handle_query_qemu_sstepbits(GArray *params, void *user_ctx)
1347 g_string_printf(gdbserver_state.str_buf, "ENABLE=%x", SSTEP_ENABLE);
1349 if (gdbserver_state.supported_sstep_flags & SSTEP_NOIRQ) {
1350 g_string_append_printf(gdbserver_state.str_buf, ",NOIRQ=%x",
1351 SSTEP_NOIRQ);
1354 if (gdbserver_state.supported_sstep_flags & SSTEP_NOTIMER) {
1355 g_string_append_printf(gdbserver_state.str_buf, ",NOTIMER=%x",
1356 SSTEP_NOTIMER);
1359 gdb_put_strbuf();
1362 static void handle_set_qemu_sstep(GArray *params, void *user_ctx)
1364 int new_sstep_flags;
1366 if (!params->len) {
1367 return;
1370 new_sstep_flags = get_param(params, 0)->val_ul;
1372 if (new_sstep_flags & ~gdbserver_state.supported_sstep_flags) {
1373 gdb_put_packet("E22");
1374 return;
1377 gdbserver_state.sstep_flags = new_sstep_flags;
1378 gdb_put_packet("OK");
1381 static void handle_query_qemu_sstep(GArray *params, void *user_ctx)
1383 g_string_printf(gdbserver_state.str_buf, "0x%x",
1384 gdbserver_state.sstep_flags);
1385 gdb_put_strbuf();
1388 static void handle_query_curr_tid(GArray *params, void *user_ctx)
1390 CPUState *cpu;
1391 GDBProcess *process;
1394 * "Current thread" remains vague in the spec, so always return
1395 * the first thread of the current process (gdb returns the
1396 * first thread).
1398 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
1399 cpu = gdb_get_first_cpu_in_process(process);
1400 g_string_assign(gdbserver_state.str_buf, "QC");
1401 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
1402 gdb_put_strbuf();
1405 static void handle_query_threads(GArray *params, void *user_ctx)
1407 if (!gdbserver_state.query_cpu) {
1408 gdb_put_packet("l");
1409 return;
1412 g_string_assign(gdbserver_state.str_buf, "m");
1413 gdb_append_thread_id(gdbserver_state.query_cpu, gdbserver_state.str_buf);
1414 gdb_put_strbuf();
1415 gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu);
1418 static void handle_query_first_threads(GArray *params, void *user_ctx)
1420 gdbserver_state.query_cpu = gdb_first_attached_cpu();
1421 handle_query_threads(params, user_ctx);
1424 static void handle_query_thread_extra(GArray *params, void *user_ctx)
1426 g_autoptr(GString) rs = g_string_new(NULL);
1427 CPUState *cpu;
1429 if (!params->len ||
1430 get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
1431 gdb_put_packet("E22");
1432 return;
1435 cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
1436 get_param(params, 0)->thread_id.tid);
1437 if (!cpu) {
1438 return;
1441 cpu_synchronize_state(cpu);
1443 if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) {
1444 /* Print the CPU model and name in multiprocess mode */
1445 ObjectClass *oc = object_get_class(OBJECT(cpu));
1446 const char *cpu_model = object_class_get_name(oc);
1447 const char *cpu_name =
1448 object_get_canonical_path_component(OBJECT(cpu));
1449 g_string_printf(rs, "%s %s [%s]", cpu_model, cpu_name,
1450 cpu->halted ? "halted " : "running");
1451 } else {
1452 g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index,
1453 cpu->halted ? "halted " : "running");
1455 trace_gdbstub_op_extra_info(rs->str);
1456 gdb_memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len);
1457 gdb_put_strbuf();
1460 static void handle_query_supported(GArray *params, void *user_ctx)
1462 CPUClass *cc;
1464 g_string_printf(gdbserver_state.str_buf, "PacketSize=%x", MAX_PACKET_LENGTH);
1465 cc = CPU_GET_CLASS(first_cpu);
1466 if (cc->gdb_core_xml_file) {
1467 g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+");
1470 if (gdb_can_reverse()) {
1471 g_string_append(gdbserver_state.str_buf,
1472 ";ReverseStep+;ReverseContinue+");
1475 #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX)
1476 if (gdbserver_state.c_cpu->opaque) {
1477 g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+");
1479 #endif
1481 if (params->len &&
1482 strstr(get_param(params, 0)->data, "multiprocess+")) {
1483 gdbserver_state.multiprocess = true;
1486 g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+");
1487 gdb_put_strbuf();
1490 static void handle_query_xfer_features(GArray *params, void *user_ctx)
1492 GDBProcess *process;
1493 CPUClass *cc;
1494 unsigned long len, total_len, addr;
1495 const char *xml;
1496 const char *p;
1498 if (params->len < 3) {
1499 gdb_put_packet("E22");
1500 return;
1503 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
1504 cc = CPU_GET_CLASS(gdbserver_state.g_cpu);
1505 if (!cc->gdb_core_xml_file) {
1506 gdb_put_packet("");
1507 return;
1510 gdb_has_xml = true;
1511 p = get_param(params, 0)->data;
1512 xml = get_feature_xml(p, &p, process);
1513 if (!xml) {
1514 gdb_put_packet("E00");
1515 return;
1518 addr = get_param(params, 1)->val_ul;
1519 len = get_param(params, 2)->val_ul;
1520 total_len = strlen(xml);
1521 if (addr > total_len) {
1522 gdb_put_packet("E00");
1523 return;
1526 if (len > (MAX_PACKET_LENGTH - 5) / 2) {
1527 len = (MAX_PACKET_LENGTH - 5) / 2;
1530 if (len < total_len - addr) {
1531 g_string_assign(gdbserver_state.str_buf, "m");
1532 gdb_memtox(gdbserver_state.str_buf, xml + addr, len);
1533 } else {
1534 g_string_assign(gdbserver_state.str_buf, "l");
1535 gdb_memtox(gdbserver_state.str_buf, xml + addr, total_len - addr);
1538 gdb_put_packet_binary(gdbserver_state.str_buf->str,
1539 gdbserver_state.str_buf->len, true);
1542 static void handle_query_qemu_supported(GArray *params, void *user_ctx)
1544 g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep");
1545 #ifndef CONFIG_USER_ONLY
1546 g_string_append(gdbserver_state.str_buf, ";PhyMemMode");
1547 #endif
1548 gdb_put_strbuf();
1551 static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
1552 /* Order is important if has same prefix */
1554 .handler = handle_query_qemu_sstepbits,
1555 .cmd = "qemu.sstepbits",
1558 .handler = handle_query_qemu_sstep,
1559 .cmd = "qemu.sstep",
1562 .handler = handle_set_qemu_sstep,
1563 .cmd = "qemu.sstep=",
1564 .cmd_startswith = 1,
1565 .schema = "l0"
1569 static const GdbCmdParseEntry gdb_gen_query_table[] = {
1571 .handler = handle_query_curr_tid,
1572 .cmd = "C",
1575 .handler = handle_query_threads,
1576 .cmd = "sThreadInfo",
1579 .handler = handle_query_first_threads,
1580 .cmd = "fThreadInfo",
1583 .handler = handle_query_thread_extra,
1584 .cmd = "ThreadExtraInfo,",
1585 .cmd_startswith = 1,
1586 .schema = "t0"
1588 #ifdef CONFIG_USER_ONLY
1590 .handler = gdb_handle_query_offsets,
1591 .cmd = "Offsets",
1593 #else
1595 .handler = gdb_handle_query_rcmd,
1596 .cmd = "Rcmd,",
1597 .cmd_startswith = 1,
1598 .schema = "s0"
1600 #endif
1602 .handler = handle_query_supported,
1603 .cmd = "Supported:",
1604 .cmd_startswith = 1,
1605 .schema = "s0"
1608 .handler = handle_query_supported,
1609 .cmd = "Supported",
1610 .schema = "s0"
1613 .handler = handle_query_xfer_features,
1614 .cmd = "Xfer:features:read:",
1615 .cmd_startswith = 1,
1616 .schema = "s:l,l0"
1618 #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX)
1620 .handler = gdb_handle_query_xfer_auxv,
1621 .cmd = "Xfer:auxv:read::",
1622 .cmd_startswith = 1,
1623 .schema = "l,l0"
1625 #endif
1627 .handler = gdb_handle_query_attached,
1628 .cmd = "Attached:",
1629 .cmd_startswith = 1
1632 .handler = gdb_handle_query_attached,
1633 .cmd = "Attached",
1636 .handler = handle_query_qemu_supported,
1637 .cmd = "qemu.Supported",
1639 #ifndef CONFIG_USER_ONLY
1641 .handler = gdb_handle_query_qemu_phy_mem_mode,
1642 .cmd = "qemu.PhyMemMode",
1644 #endif
1647 static const GdbCmdParseEntry gdb_gen_set_table[] = {
1648 /* Order is important if has same prefix */
1650 .handler = handle_set_qemu_sstep,
1651 .cmd = "qemu.sstep:",
1652 .cmd_startswith = 1,
1653 .schema = "l0"
1655 #ifndef CONFIG_USER_ONLY
1657 .handler = gdb_handle_set_qemu_phy_mem_mode,
1658 .cmd = "qemu.PhyMemMode:",
1659 .cmd_startswith = 1,
1660 .schema = "l0"
1662 #endif
1665 static void handle_gen_query(GArray *params, void *user_ctx)
1667 if (!params->len) {
1668 return;
1671 if (!process_string_cmd(NULL, get_param(params, 0)->data,
1672 gdb_gen_query_set_common_table,
1673 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
1674 return;
1677 if (process_string_cmd(NULL, get_param(params, 0)->data,
1678 gdb_gen_query_table,
1679 ARRAY_SIZE(gdb_gen_query_table))) {
1680 gdb_put_packet("");
1684 static void handle_gen_set(GArray *params, void *user_ctx)
1686 if (!params->len) {
1687 return;
1690 if (!process_string_cmd(NULL, get_param(params, 0)->data,
1691 gdb_gen_query_set_common_table,
1692 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
1693 return;
1696 if (process_string_cmd(NULL, get_param(params, 0)->data,
1697 gdb_gen_set_table,
1698 ARRAY_SIZE(gdb_gen_set_table))) {
1699 gdb_put_packet("");
1703 static void handle_target_halt(GArray *params, void *user_ctx)
1705 if (gdbserver_state.allow_stop_reply) {
1706 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
1707 gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf);
1708 g_string_append_c(gdbserver_state.str_buf, ';');
1709 gdb_put_strbuf();
1710 gdbserver_state.allow_stop_reply = false;
1713 * Remove all the breakpoints when this query is issued,
1714 * because gdb is doing an initial connect and the state
1715 * should be cleaned up.
1717 gdb_breakpoint_remove_all(gdbserver_state.c_cpu);
1720 static int gdb_handle_packet(const char *line_buf)
1722 const GdbCmdParseEntry *cmd_parser = NULL;
1724 trace_gdbstub_io_command(line_buf);
1726 switch (line_buf[0]) {
1727 case '!':
1728 gdb_put_packet("OK");
1729 break;
1730 case '?':
1732 static const GdbCmdParseEntry target_halted_cmd_desc = {
1733 .handler = handle_target_halt,
1734 .cmd = "?",
1735 .cmd_startswith = 1,
1736 .allow_stop_reply = true,
1738 cmd_parser = &target_halted_cmd_desc;
1740 break;
1741 case 'c':
1743 static const GdbCmdParseEntry continue_cmd_desc = {
1744 .handler = handle_continue,
1745 .cmd = "c",
1746 .cmd_startswith = 1,
1747 .allow_stop_reply = true,
1748 .schema = "L0"
1750 cmd_parser = &continue_cmd_desc;
1752 break;
1753 case 'C':
1755 static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
1756 .handler = handle_cont_with_sig,
1757 .cmd = "C",
1758 .cmd_startswith = 1,
1759 .allow_stop_reply = true,
1760 .schema = "l0"
1762 cmd_parser = &cont_with_sig_cmd_desc;
1764 break;
1765 case 'v':
1767 static const GdbCmdParseEntry v_cmd_desc = {
1768 .handler = handle_v_commands,
1769 .cmd = "v",
1770 .cmd_startswith = 1,
1771 .schema = "s0"
1773 cmd_parser = &v_cmd_desc;
1775 break;
1776 case 'k':
1777 /* Kill the target */
1778 error_report("QEMU: Terminated via GDBstub");
1779 gdb_exit(0);
1780 exit(0);
1781 case 'D':
1783 static const GdbCmdParseEntry detach_cmd_desc = {
1784 .handler = handle_detach,
1785 .cmd = "D",
1786 .cmd_startswith = 1,
1787 .schema = "?.l0"
1789 cmd_parser = &detach_cmd_desc;
1791 break;
1792 case 's':
1794 static const GdbCmdParseEntry step_cmd_desc = {
1795 .handler = handle_step,
1796 .cmd = "s",
1797 .cmd_startswith = 1,
1798 .allow_stop_reply = true,
1799 .schema = "L0"
1801 cmd_parser = &step_cmd_desc;
1803 break;
1804 case 'b':
1806 static const GdbCmdParseEntry backward_cmd_desc = {
1807 .handler = handle_backward,
1808 .cmd = "b",
1809 .cmd_startswith = 1,
1810 .allow_stop_reply = true,
1811 .schema = "o0"
1813 cmd_parser = &backward_cmd_desc;
1815 break;
1816 case 'F':
1818 static const GdbCmdParseEntry file_io_cmd_desc = {
1819 .handler = gdb_handle_file_io,
1820 .cmd = "F",
1821 .cmd_startswith = 1,
1822 .schema = "L,L,o0"
1824 cmd_parser = &file_io_cmd_desc;
1826 break;
1827 case 'g':
1829 static const GdbCmdParseEntry read_all_regs_cmd_desc = {
1830 .handler = handle_read_all_regs,
1831 .cmd = "g",
1832 .cmd_startswith = 1
1834 cmd_parser = &read_all_regs_cmd_desc;
1836 break;
1837 case 'G':
1839 static const GdbCmdParseEntry write_all_regs_cmd_desc = {
1840 .handler = handle_write_all_regs,
1841 .cmd = "G",
1842 .cmd_startswith = 1,
1843 .schema = "s0"
1845 cmd_parser = &write_all_regs_cmd_desc;
1847 break;
1848 case 'm':
1850 static const GdbCmdParseEntry read_mem_cmd_desc = {
1851 .handler = handle_read_mem,
1852 .cmd = "m",
1853 .cmd_startswith = 1,
1854 .schema = "L,L0"
1856 cmd_parser = &read_mem_cmd_desc;
1858 break;
1859 case 'M':
1861 static const GdbCmdParseEntry write_mem_cmd_desc = {
1862 .handler = handle_write_mem,
1863 .cmd = "M",
1864 .cmd_startswith = 1,
1865 .schema = "L,L:s0"
1867 cmd_parser = &write_mem_cmd_desc;
1869 break;
1870 case 'p':
1872 static const GdbCmdParseEntry get_reg_cmd_desc = {
1873 .handler = handle_get_reg,
1874 .cmd = "p",
1875 .cmd_startswith = 1,
1876 .schema = "L0"
1878 cmd_parser = &get_reg_cmd_desc;
1880 break;
1881 case 'P':
1883 static const GdbCmdParseEntry set_reg_cmd_desc = {
1884 .handler = handle_set_reg,
1885 .cmd = "P",
1886 .cmd_startswith = 1,
1887 .schema = "L?s0"
1889 cmd_parser = &set_reg_cmd_desc;
1891 break;
1892 case 'Z':
1894 static const GdbCmdParseEntry insert_bp_cmd_desc = {
1895 .handler = handle_insert_bp,
1896 .cmd = "Z",
1897 .cmd_startswith = 1,
1898 .schema = "l?L?L0"
1900 cmd_parser = &insert_bp_cmd_desc;
1902 break;
1903 case 'z':
1905 static const GdbCmdParseEntry remove_bp_cmd_desc = {
1906 .handler = handle_remove_bp,
1907 .cmd = "z",
1908 .cmd_startswith = 1,
1909 .schema = "l?L?L0"
1911 cmd_parser = &remove_bp_cmd_desc;
1913 break;
1914 case 'H':
1916 static const GdbCmdParseEntry set_thread_cmd_desc = {
1917 .handler = handle_set_thread,
1918 .cmd = "H",
1919 .cmd_startswith = 1,
1920 .schema = "o.t0"
1922 cmd_parser = &set_thread_cmd_desc;
1924 break;
1925 case 'T':
1927 static const GdbCmdParseEntry thread_alive_cmd_desc = {
1928 .handler = handle_thread_alive,
1929 .cmd = "T",
1930 .cmd_startswith = 1,
1931 .schema = "t0"
1933 cmd_parser = &thread_alive_cmd_desc;
1935 break;
1936 case 'q':
1938 static const GdbCmdParseEntry gen_query_cmd_desc = {
1939 .handler = handle_gen_query,
1940 .cmd = "q",
1941 .cmd_startswith = 1,
1942 .schema = "s0"
1944 cmd_parser = &gen_query_cmd_desc;
1946 break;
1947 case 'Q':
1949 static const GdbCmdParseEntry gen_set_cmd_desc = {
1950 .handler = handle_gen_set,
1951 .cmd = "Q",
1952 .cmd_startswith = 1,
1953 .schema = "s0"
1955 cmd_parser = &gen_set_cmd_desc;
1957 break;
1958 default:
1959 /* put empty packet */
1960 gdb_put_packet("");
1961 break;
1964 if (cmd_parser) {
1965 run_cmd_parser(line_buf, cmd_parser);
1968 return RS_IDLE;
1971 void gdb_set_stop_cpu(CPUState *cpu)
1973 GDBProcess *p = gdb_get_cpu_process(cpu);
1975 if (!p->attached) {
1977 * Having a stop CPU corresponding to a process that is not attached
1978 * confuses GDB. So we ignore the request.
1980 return;
1983 gdbserver_state.c_cpu = cpu;
1984 gdbserver_state.g_cpu = cpu;
1987 void gdb_read_byte(uint8_t ch)
1989 uint8_t reply;
1991 gdbserver_state.allow_stop_reply = false;
1992 #ifndef CONFIG_USER_ONLY
1993 if (gdbserver_state.last_packet->len) {
1994 /* Waiting for a response to the last packet. If we see the start
1995 of a new command then abandon the previous response. */
1996 if (ch == '-') {
1997 trace_gdbstub_err_got_nack();
1998 gdb_put_buffer(gdbserver_state.last_packet->data,
1999 gdbserver_state.last_packet->len);
2000 } else if (ch == '+') {
2001 trace_gdbstub_io_got_ack();
2002 } else {
2003 trace_gdbstub_io_got_unexpected(ch);
2006 if (ch == '+' || ch == '$') {
2007 g_byte_array_set_size(gdbserver_state.last_packet, 0);
2009 if (ch != '$')
2010 return;
2012 if (runstate_is_running()) {
2013 /* when the CPU is running, we cannot do anything except stop
2014 it when receiving a char */
2015 vm_stop(RUN_STATE_PAUSED);
2016 } else
2017 #endif
2019 switch(gdbserver_state.state) {
2020 case RS_IDLE:
2021 if (ch == '$') {
2022 /* start of command packet */
2023 gdbserver_state.line_buf_index = 0;
2024 gdbserver_state.line_sum = 0;
2025 gdbserver_state.state = RS_GETLINE;
2026 } else {
2027 trace_gdbstub_err_garbage(ch);
2029 break;
2030 case RS_GETLINE:
2031 if (ch == '}') {
2032 /* start escape sequence */
2033 gdbserver_state.state = RS_GETLINE_ESC;
2034 gdbserver_state.line_sum += ch;
2035 } else if (ch == '*') {
2036 /* start run length encoding sequence */
2037 gdbserver_state.state = RS_GETLINE_RLE;
2038 gdbserver_state.line_sum += ch;
2039 } else if (ch == '#') {
2040 /* end of command, start of checksum*/
2041 gdbserver_state.state = RS_CHKSUM1;
2042 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
2043 trace_gdbstub_err_overrun();
2044 gdbserver_state.state = RS_IDLE;
2045 } else {
2046 /* unescaped command character */
2047 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch;
2048 gdbserver_state.line_sum += ch;
2050 break;
2051 case RS_GETLINE_ESC:
2052 if (ch == '#') {
2053 /* unexpected end of command in escape sequence */
2054 gdbserver_state.state = RS_CHKSUM1;
2055 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
2056 /* command buffer overrun */
2057 trace_gdbstub_err_overrun();
2058 gdbserver_state.state = RS_IDLE;
2059 } else {
2060 /* parse escaped character and leave escape state */
2061 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20;
2062 gdbserver_state.line_sum += ch;
2063 gdbserver_state.state = RS_GETLINE;
2065 break;
2066 case RS_GETLINE_RLE:
2068 * Run-length encoding is explained in "Debugging with GDB /
2069 * Appendix E GDB Remote Serial Protocol / Overview".
2071 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
2072 /* invalid RLE count encoding */
2073 trace_gdbstub_err_invalid_repeat(ch);
2074 gdbserver_state.state = RS_GETLINE;
2075 } else {
2076 /* decode repeat length */
2077 int repeat = ch - ' ' + 3;
2078 if (gdbserver_state.line_buf_index + repeat >= sizeof(gdbserver_state.line_buf) - 1) {
2079 /* that many repeats would overrun the command buffer */
2080 trace_gdbstub_err_overrun();
2081 gdbserver_state.state = RS_IDLE;
2082 } else if (gdbserver_state.line_buf_index < 1) {
2083 /* got a repeat but we have nothing to repeat */
2084 trace_gdbstub_err_invalid_rle();
2085 gdbserver_state.state = RS_GETLINE;
2086 } else {
2087 /* repeat the last character */
2088 memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index,
2089 gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat);
2090 gdbserver_state.line_buf_index += repeat;
2091 gdbserver_state.line_sum += ch;
2092 gdbserver_state.state = RS_GETLINE;
2095 break;
2096 case RS_CHKSUM1:
2097 /* get high hex digit of checksum */
2098 if (!isxdigit(ch)) {
2099 trace_gdbstub_err_checksum_invalid(ch);
2100 gdbserver_state.state = RS_GETLINE;
2101 break;
2103 gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0';
2104 gdbserver_state.line_csum = fromhex(ch) << 4;
2105 gdbserver_state.state = RS_CHKSUM2;
2106 break;
2107 case RS_CHKSUM2:
2108 /* get low hex digit of checksum */
2109 if (!isxdigit(ch)) {
2110 trace_gdbstub_err_checksum_invalid(ch);
2111 gdbserver_state.state = RS_GETLINE;
2112 break;
2114 gdbserver_state.line_csum |= fromhex(ch);
2116 if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) {
2117 trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum);
2118 /* send NAK reply */
2119 reply = '-';
2120 gdb_put_buffer(&reply, 1);
2121 gdbserver_state.state = RS_IDLE;
2122 } else {
2123 /* send ACK reply */
2124 reply = '+';
2125 gdb_put_buffer(&reply, 1);
2126 gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf);
2128 break;
2129 default:
2130 abort();
2136 * Create the process that will contain all the "orphan" CPUs (that are not
2137 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2138 * be attachable and thus will be invisible to the user.
2140 void gdb_create_default_process(GDBState *s)
2142 GDBProcess *process;
2143 int pid;
2145 #ifdef CONFIG_USER_ONLY
2146 assert(gdbserver_state.process_num == 0);
2147 pid = getpid();
2148 #else
2149 if (gdbserver_state.process_num) {
2150 pid = s->processes[s->process_num - 1].pid;
2151 } else {
2152 pid = 0;
2154 /* We need an available PID slot for this process */
2155 assert(pid < UINT32_MAX);
2156 pid++;
2157 #endif
2159 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2160 process = &s->processes[s->process_num - 1];
2161 process->pid = pid;
2162 process->attached = false;
2163 process->target_xml[0] = '\0';