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"
32 #include "exec/gdbstub.h"
33 #include "gdbstub/syscalls.h"
34 #ifdef CONFIG_USER_ONLY
35 #include "gdbstub/user.h"
37 #include "hw/cpu/cluster.h"
38 #include "hw/boards.h"
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 {
51 gdb_get_reg_cb get_reg;
52 gdb_set_reg_cb set_reg;
56 GDBState gdbserver_state;
58 void gdb_init_gdbserver_state(void)
60 g_assert(!gdbserver_state.init);
61 memset(&gdbserver_state, 0, sizeof(GDBState));
62 gdbserver_state.init = true;
63 gdbserver_state.str_buf = g_string_new(NULL);
64 gdbserver_state.mem_buf = g_byte_array_sized_new(MAX_PACKET_LENGTH);
65 gdbserver_state.last_packet = g_byte_array_sized_new(MAX_PACKET_LENGTH + 4);
68 * What single-step modes are supported is accelerator dependent.
69 * By default try to use no IRQs and no timers while single
70 * stepping so as to make single stepping like a typical ICE HW step.
72 gdbserver_state.supported_sstep_flags = accel_supported_gdbstub_sstep_flags();
73 gdbserver_state.sstep_flags = SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER;
74 gdbserver_state.sstep_flags &= gdbserver_state.supported_sstep_flags;
77 /* writes 2*len+1 bytes in buf */
78 void gdb_memtohex(GString *buf, const uint8_t *mem, int len)
81 for(i = 0; i < len; i++) {
83 g_string_append_c(buf, tohex(c >> 4));
84 g_string_append_c(buf, tohex(c & 0xf));
86 g_string_append_c(buf, '\0');
89 void gdb_hextomem(GByteArray *mem, const char *buf, int len)
93 for(i = 0; i < len; i++) {
94 guint8 byte = fromhex(buf[0]) << 4 | fromhex(buf[1]);
95 g_byte_array_append(mem, &byte, 1);
100 static void hexdump(const char *buf, int len,
101 void (*trace_fn)(size_t ofs, char const *text))
103 char line_buffer[3 * 16 + 4 + 16 + 1];
106 for (i = 0; i < len || (i & 0xF); ++i) {
107 size_t byte_ofs = i & 15;
110 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
111 line_buffer[3 * 16 + 4 + 16] = 0;
114 size_t col_group = (i >> 2) & 3;
115 size_t hex_col = byte_ofs * 3 + col_group;
116 size_t txt_col = 3 * 16 + 4 + byte_ofs;
121 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
122 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
123 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
129 trace_fn(i & -16, line_buffer);
133 /* return -1 if error, 0 if OK */
134 int gdb_put_packet_binary(const char *buf, int len, bool dump)
139 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
140 hexdump(buf, len, trace_gdbstub_io_binaryreply);
144 g_byte_array_set_size(gdbserver_state.last_packet, 0);
145 g_byte_array_append(gdbserver_state.last_packet,
146 (const uint8_t *) "$", 1);
147 g_byte_array_append(gdbserver_state.last_packet,
148 (const uint8_t *) buf, len);
150 for(i = 0; i < len; i++) {
154 footer[1] = tohex((csum >> 4) & 0xf);
155 footer[2] = tohex((csum) & 0xf);
156 g_byte_array_append(gdbserver_state.last_packet, footer, 3);
158 gdb_put_buffer(gdbserver_state.last_packet->data,
159 gdbserver_state.last_packet->len);
161 if (gdb_got_immediate_ack()) {
168 /* return -1 if error, 0 if OK */
169 int gdb_put_packet(const char *buf)
171 trace_gdbstub_io_reply(buf);
173 return gdb_put_packet_binary(buf, strlen(buf), false);
176 void gdb_put_strbuf(void)
178 gdb_put_packet(gdbserver_state.str_buf->str);
181 /* Encode data using the encoding for 'x' packets. */
182 void gdb_memtox(GString *buf, const char *mem, int len)
189 case '#': case '$': case '*': case '}':
190 g_string_append_c(buf, '}');
191 g_string_append_c(buf, c ^ 0x20);
194 g_string_append_c(buf, c);
200 static uint32_t gdb_get_cpu_pid(CPUState *cpu)
202 #ifdef CONFIG_USER_ONLY
205 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
206 /* Return the default process' PID */
207 int index = gdbserver_state.process_num - 1;
208 return gdbserver_state.processes[index].pid;
210 return cpu->cluster_index + 1;
214 GDBProcess *gdb_get_process(uint32_t pid)
219 /* 0 means any process, we take the first one */
220 return &gdbserver_state.processes[0];
223 for (i = 0; i < gdbserver_state.process_num; i++) {
224 if (gdbserver_state.processes[i].pid == pid) {
225 return &gdbserver_state.processes[i];
232 static GDBProcess *gdb_get_cpu_process(CPUState *cpu)
234 return gdb_get_process(gdb_get_cpu_pid(cpu));
237 static CPUState *find_cpu(uint32_t thread_id)
242 if (gdb_get_cpu_index(cpu) == thread_id) {
250 CPUState *gdb_get_first_cpu_in_process(GDBProcess *process)
255 if (gdb_get_cpu_pid(cpu) == process->pid) {
263 static CPUState *gdb_next_cpu_in_process(CPUState *cpu)
265 uint32_t pid = gdb_get_cpu_pid(cpu);
269 if (gdb_get_cpu_pid(cpu) == pid) {
279 /* Return the cpu following @cpu, while ignoring unattached processes. */
280 static CPUState *gdb_next_attached_cpu(CPUState *cpu)
285 if (gdb_get_cpu_process(cpu)->attached) {
295 /* Return the first attached cpu */
296 CPUState *gdb_first_attached_cpu(void)
298 CPUState *cpu = first_cpu;
299 GDBProcess *process = gdb_get_cpu_process(cpu);
301 if (!process->attached) {
302 return gdb_next_attached_cpu(cpu);
308 static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid)
314 /* 0 means any process/thread, we take the first attached one */
315 return gdb_first_attached_cpu();
316 } else if (pid && !tid) {
317 /* any thread in a specific process */
318 process = gdb_get_process(pid);
320 if (process == NULL) {
324 if (!process->attached) {
328 return gdb_get_first_cpu_in_process(process);
330 /* a specific thread */
337 process = gdb_get_cpu_process(cpu);
339 if (pid && process->pid != pid) {
343 if (!process->attached) {
351 static const char *get_feature_xml(const char *p, const char **newp,
354 CPUState *cpu = gdb_get_first_cpu_in_process(process);
355 CPUClass *cc = CPU_GET_CLASS(cpu);
359 * qXfer:features:read:ANNEX:OFFSET,LENGTH'
362 char *term = strchr(p, ':');
366 /* Is it the main target xml? */
367 if (strncmp(p, "target.xml", len) == 0) {
368 if (!process->target_xml) {
370 g_autoptr(GPtrArray) xml = g_ptr_array_new_with_free_func(g_free);
374 g_strdup("<?xml version=\"1.0\"?>"
375 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
378 if (cc->gdb_arch_name) {
381 g_markup_printf_escaped("<architecture>%s</architecture>",
382 cc->gdb_arch_name(cpu)));
386 g_markup_printf_escaped("<xi:include href=\"%s\"/>",
387 cc->gdb_core_xml_file));
388 for (guint i = 0; i < cpu->gdb_regs->len; i++) {
389 r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
392 g_markup_printf_escaped("<xi:include href=\"%s\"/>",
395 g_ptr_array_add(xml, g_strdup("</target>"));
396 g_ptr_array_add(xml, NULL);
398 process->target_xml = g_strjoinv(NULL, (void *)xml->pdata);
400 return process->target_xml;
402 /* Is it dynamically generated by the target? */
403 if (cc->gdb_get_dynamic_xml) {
404 g_autofree char *xmlname = g_strndup(p, len);
405 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
410 /* Is it one of the encoded gdb-xml/ files? */
411 for (int i = 0; gdb_static_features[i].xmlname; i++) {
412 const char *name = gdb_static_features[i].xmlname;
413 if ((strncmp(name, p, len) == 0) &&
414 strlen(name) == len) {
415 return gdb_static_features[i].xml;
423 static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
425 CPUClass *cc = CPU_GET_CLASS(cpu);
426 CPUArchState *env = cpu_env(cpu);
429 if (reg < cc->gdb_num_core_regs) {
430 return cc->gdb_read_register(cpu, buf, reg);
433 for (guint i = 0; i < cpu->gdb_regs->len; i++) {
434 r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
435 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
436 return r->get_reg(env, buf, reg - r->base_reg);
442 static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
444 CPUClass *cc = CPU_GET_CLASS(cpu);
445 CPUArchState *env = cpu_env(cpu);
448 if (reg < cc->gdb_num_core_regs) {
449 return cc->gdb_write_register(cpu, mem_buf, reg);
452 for (guint i = 0; i < cpu->gdb_regs->len; i++) {
453 r = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
454 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
455 return r->set_reg(env, mem_buf, reg - r->base_reg);
461 void gdb_register_coprocessor(CPUState *cpu,
462 gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
463 int num_regs, const char *xml, int g_pos)
469 for (i = 0; i < cpu->gdb_regs->len; i++) {
470 /* Check for duplicates. */
471 s = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
472 if (strcmp(s->xml, xml) == 0) {
477 cpu->gdb_regs = g_array_new(false, false, sizeof(GDBRegisterState));
481 g_array_set_size(cpu->gdb_regs, i + 1);
482 s = &g_array_index(cpu->gdb_regs, GDBRegisterState, i);
483 s->base_reg = cpu->gdb_num_regs;
484 s->num_regs = num_regs;
485 s->get_reg = get_reg;
486 s->set_reg = set_reg;
489 /* Add to end of list. */
490 cpu->gdb_num_regs += num_regs;
492 if (g_pos != s->base_reg) {
493 error_report("Error: Bad gdb register numbering for '%s', "
494 "expected %d got %d", xml, g_pos, s->base_reg);
496 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
501 static void gdb_process_breakpoint_remove_all(GDBProcess *p)
503 CPUState *cpu = gdb_get_first_cpu_in_process(p);
506 gdb_breakpoint_remove_all(cpu);
507 cpu = gdb_next_cpu_in_process(cpu);
512 static void gdb_set_cpu_pc(vaddr pc)
514 CPUState *cpu = gdbserver_state.c_cpu;
516 cpu_synchronize_state(cpu);
520 void gdb_append_thread_id(CPUState *cpu, GString *buf)
522 if (gdbserver_state.multiprocess) {
523 g_string_append_printf(buf, "p%02x.%02x",
524 gdb_get_cpu_pid(cpu), gdb_get_cpu_index(cpu));
526 g_string_append_printf(buf, "%02x", gdb_get_cpu_index(cpu));
530 static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
531 uint32_t *pid, uint32_t *tid)
538 ret = qemu_strtoul(buf, &buf, 16, &p);
541 return GDB_READ_THREAD_ERR;
550 ret = qemu_strtoul(buf, &buf, 16, &t);
553 return GDB_READ_THREAD_ERR;
559 return GDB_ALL_PROCESSES;
567 return GDB_ALL_THREADS;
574 return GDB_ONE_THREAD;
578 * gdb_handle_vcont - Parses and handles a vCont packet.
579 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
580 * a format error, 0 on success.
582 static int gdb_handle_vcont(const char *p)
590 GDBThreadIdKind kind;
591 unsigned int max_cpus = gdb_get_max_cpus();
592 /* uninitialised CPUs stay 0 */
593 g_autofree char *newstates = g_new0(char, max_cpus);
595 /* mark valid CPUs with 1 */
597 newstates[cpu->cpu_index] = 1;
601 * res keeps track of what error we are returning, with -ENOTSUP meaning
602 * that the command is unknown or unsupported, thus returning an empty
603 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
604 * or incorrect parameters passed.
609 * target_count and last_target keep track of how many CPUs we are going to
610 * step or resume, and a pointer to the state structure of one of them,
613 int target_count = 0;
614 CPUState *last_target = NULL;
622 if (cur_action == 'C' || cur_action == 'S') {
623 cur_action = qemu_tolower(cur_action);
624 res = qemu_strtoul(p, &p, 16, &tmp);
628 signal = gdb_signal_to_target(tmp);
629 } else if (cur_action != 'c' && cur_action != 's') {
630 /* unknown/invalid/unsupported command */
634 if (*p == '\0' || *p == ';') {
636 * No thread specifier, action is on "all threads". The
637 * specification is unclear regarding the process to act on. We
638 * choose all processes.
640 kind = GDB_ALL_PROCESSES;
641 } else if (*p++ == ':') {
642 kind = read_thread_id(p, &p, &pid, &tid);
648 case GDB_READ_THREAD_ERR:
651 case GDB_ALL_PROCESSES:
652 cpu = gdb_first_attached_cpu();
654 if (newstates[cpu->cpu_index] == 1) {
655 newstates[cpu->cpu_index] = cur_action;
661 cpu = gdb_next_attached_cpu(cpu);
665 case GDB_ALL_THREADS:
666 process = gdb_get_process(pid);
668 if (!process->attached) {
672 cpu = gdb_get_first_cpu_in_process(process);
674 if (newstates[cpu->cpu_index] == 1) {
675 newstates[cpu->cpu_index] = cur_action;
681 cpu = gdb_next_cpu_in_process(cpu);
686 cpu = gdb_get_cpu(pid, tid);
688 /* invalid CPU/thread specified */
693 /* only use if no previous match occourred */
694 if (newstates[cpu->cpu_index] == 1) {
695 newstates[cpu->cpu_index] = cur_action;
705 * if we're about to resume a specific set of CPUs/threads, make it so that
706 * in case execution gets interrupted, we can send GDB a stop reply with a
707 * correct value. it doesn't really matter which CPU we tell GDB the signal
708 * happened in (VM pauses stop all of them anyway), so long as it is one of
709 * the ones we resumed/single stepped here.
711 if (target_count > 0) {
712 gdbserver_state.c_cpu = last_target;
715 gdbserver_state.signal = signal;
716 gdb_continue_partial(newstates);
720 static const char *cmd_next_param(const char *param, const char delimiter)
722 static const char all_delimiters[] = ",;:=";
723 char curr_delimiters[2] = {0};
724 const char *delimiters;
726 if (delimiter == '?') {
727 delimiters = all_delimiters;
728 } else if (delimiter == '0') {
729 return strchr(param, '\0');
730 } else if (delimiter == '.' && *param) {
733 curr_delimiters[0] = delimiter;
734 delimiters = curr_delimiters;
737 param += strcspn(param, delimiters);
744 static int cmd_parse_params(const char *data, const char *schema,
747 const char *curr_schema, *curr_data;
750 g_assert(params->len == 0);
752 curr_schema = schema;
754 while (curr_schema[0] && curr_schema[1] && *curr_data) {
755 GdbCmdVariant this_param;
757 switch (curr_schema[0]) {
759 if (qemu_strtoul(curr_data, &curr_data, 16,
760 &this_param.val_ul)) {
763 curr_data = cmd_next_param(curr_data, curr_schema[1]);
764 g_array_append_val(params, this_param);
767 if (qemu_strtou64(curr_data, &curr_data, 16,
768 (uint64_t *)&this_param.val_ull)) {
771 curr_data = cmd_next_param(curr_data, curr_schema[1]);
772 g_array_append_val(params, this_param);
775 this_param.data = curr_data;
776 curr_data = cmd_next_param(curr_data, curr_schema[1]);
777 g_array_append_val(params, this_param);
780 this_param.opcode = *(uint8_t *)curr_data;
781 curr_data = cmd_next_param(curr_data, curr_schema[1]);
782 g_array_append_val(params, this_param);
785 this_param.thread_id.kind =
786 read_thread_id(curr_data, &curr_data,
787 &this_param.thread_id.pid,
788 &this_param.thread_id.tid);
789 curr_data = cmd_next_param(curr_data, curr_schema[1]);
790 g_array_append_val(params, this_param);
793 curr_data = cmd_next_param(curr_data, curr_schema[1]);
804 typedef void (*GdbCmdHandler)(GArray *params, void *user_ctx);
807 * cmd_startswith -> cmd is compared using startswith
809 * allow_stop_reply -> true iff the gdbstub can respond to this command with a
810 * "stop reply" packet. The list of commands that accept such response is
811 * defined at the GDB Remote Serial Protocol documentation. see:
812 * https://sourceware.org/gdb/onlinedocs/gdb/Stop-Reply-Packets.html#Stop-Reply-Packets.
814 * schema definitions:
815 * Each schema parameter entry consists of 2 chars,
816 * the first char represents the parameter type handling
817 * the second char represents the delimiter for the next parameter
819 * Currently supported schema types:
820 * 'l' -> unsigned long (stored in .val_ul)
821 * 'L' -> unsigned long long (stored in .val_ull)
822 * 's' -> string (stored in .data)
823 * 'o' -> single char (stored in .opcode)
824 * 't' -> thread id (stored in .thread_id)
825 * '?' -> skip according to delimiter
827 * Currently supported delimiters:
828 * '?' -> Stop at any delimiter (",;:=\0")
829 * '0' -> Stop at "\0"
830 * '.' -> Skip 1 char unless reached "\0"
831 * Any other value is treated as the delimiter value itself
833 typedef struct GdbCmdParseEntry {
834 GdbCmdHandler handler;
838 bool allow_stop_reply;
841 static inline int startswith(const char *string, const char *pattern)
843 return !strncmp(string, pattern, strlen(pattern));
846 static int process_string_cmd(const char *data,
847 const GdbCmdParseEntry *cmds, int num_cmds)
850 g_autoptr(GArray) params = g_array_new(false, true, sizeof(GdbCmdVariant));
856 for (i = 0; i < num_cmds; i++) {
857 const GdbCmdParseEntry *cmd = &cmds[i];
858 g_assert(cmd->handler && cmd->cmd);
860 if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
861 (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
866 if (cmd_parse_params(&data[strlen(cmd->cmd)],
867 cmd->schema, params)) {
872 gdbserver_state.allow_stop_reply = cmd->allow_stop_reply;
873 cmd->handler(params, NULL);
880 static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd)
886 g_string_set_size(gdbserver_state.str_buf, 0);
887 g_byte_array_set_size(gdbserver_state.mem_buf, 0);
889 /* In case there was an error during the command parsing we must
890 * send a NULL packet to indicate the command is not supported */
891 if (process_string_cmd(data, cmd, 1)) {
896 static void handle_detach(GArray *params, void *user_ctx)
901 if (gdbserver_state.multiprocess) {
903 gdb_put_packet("E22");
907 pid = get_param(params, 0)->val_ul;
910 process = gdb_get_process(pid);
911 gdb_process_breakpoint_remove_all(process);
912 process->attached = false;
914 if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) {
915 gdbserver_state.c_cpu = gdb_first_attached_cpu();
918 if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) {
919 gdbserver_state.g_cpu = gdb_first_attached_cpu();
922 if (!gdbserver_state.c_cpu) {
923 /* No more process attached */
924 gdb_disable_syscalls();
927 gdb_put_packet("OK");
930 static void handle_thread_alive(GArray *params, void *user_ctx)
935 gdb_put_packet("E22");
939 if (get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
940 gdb_put_packet("E22");
944 cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
945 get_param(params, 0)->thread_id.tid);
947 gdb_put_packet("E22");
951 gdb_put_packet("OK");
954 static void handle_continue(GArray *params, void *user_ctx)
957 gdb_set_cpu_pc(get_param(params, 0)->val_ull);
960 gdbserver_state.signal = 0;
964 static void handle_cont_with_sig(GArray *params, void *user_ctx)
966 unsigned long signal = 0;
969 * Note: C sig;[addr] is currently unsupported and we simply
970 * omit the addr parameter
973 signal = get_param(params, 0)->val_ul;
976 gdbserver_state.signal = gdb_signal_to_target(signal);
977 if (gdbserver_state.signal == -1) {
978 gdbserver_state.signal = 0;
983 static void handle_set_thread(GArray *params, void *user_ctx)
987 if (params->len != 2) {
988 gdb_put_packet("E22");
992 if (get_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) {
993 gdb_put_packet("E22");
997 if (get_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) {
998 gdb_put_packet("OK");
1002 cpu = gdb_get_cpu(get_param(params, 1)->thread_id.pid,
1003 get_param(params, 1)->thread_id.tid);
1005 gdb_put_packet("E22");
1010 * Note: This command is deprecated and modern gdb's will be using the
1011 * vCont command instead.
1013 switch (get_param(params, 0)->opcode) {
1015 gdbserver_state.c_cpu = cpu;
1016 gdb_put_packet("OK");
1019 gdbserver_state.g_cpu = cpu;
1020 gdb_put_packet("OK");
1023 gdb_put_packet("E22");
1028 static void handle_insert_bp(GArray *params, void *user_ctx)
1032 if (params->len != 3) {
1033 gdb_put_packet("E22");
1037 res = gdb_breakpoint_insert(gdbserver_state.c_cpu,
1038 get_param(params, 0)->val_ul,
1039 get_param(params, 1)->val_ull,
1040 get_param(params, 2)->val_ull);
1042 gdb_put_packet("OK");
1044 } else if (res == -ENOSYS) {
1049 gdb_put_packet("E22");
1052 static void handle_remove_bp(GArray *params, void *user_ctx)
1056 if (params->len != 3) {
1057 gdb_put_packet("E22");
1061 res = gdb_breakpoint_remove(gdbserver_state.c_cpu,
1062 get_param(params, 0)->val_ul,
1063 get_param(params, 1)->val_ull,
1064 get_param(params, 2)->val_ull);
1066 gdb_put_packet("OK");
1068 } else if (res == -ENOSYS) {
1073 gdb_put_packet("E22");
1077 * handle_set/get_reg
1079 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1080 * This works, but can be very slow. Anything new enough to understand
1081 * XML also knows how to use this properly. However to use this we
1082 * need to define a local XML file as well as be talking to a
1083 * reasonably modern gdb. Responding with an empty packet will cause
1084 * the remote gdb to fallback to older methods.
1087 static void handle_set_reg(GArray *params, void *user_ctx)
1091 if (params->len != 2) {
1092 gdb_put_packet("E22");
1096 reg_size = strlen(get_param(params, 1)->data) / 2;
1097 gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 1)->data, reg_size);
1098 gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data,
1099 get_param(params, 0)->val_ull);
1100 gdb_put_packet("OK");
1103 static void handle_get_reg(GArray *params, void *user_ctx)
1108 gdb_put_packet("E14");
1112 reg_size = gdb_read_register(gdbserver_state.g_cpu,
1113 gdbserver_state.mem_buf,
1114 get_param(params, 0)->val_ull);
1116 gdb_put_packet("E14");
1119 g_byte_array_set_size(gdbserver_state.mem_buf, reg_size);
1122 gdb_memtohex(gdbserver_state.str_buf,
1123 gdbserver_state.mem_buf->data, reg_size);
1127 static void handle_write_mem(GArray *params, void *user_ctx)
1129 if (params->len != 3) {
1130 gdb_put_packet("E22");
1134 /* gdb_hextomem() reads 2*len bytes */
1135 if (get_param(params, 1)->val_ull >
1136 strlen(get_param(params, 2)->data) / 2) {
1137 gdb_put_packet("E22");
1141 gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 2)->data,
1142 get_param(params, 1)->val_ull);
1143 if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu,
1144 get_param(params, 0)->val_ull,
1145 gdbserver_state.mem_buf->data,
1146 gdbserver_state.mem_buf->len, true)) {
1147 gdb_put_packet("E14");
1151 gdb_put_packet("OK");
1154 static void handle_read_mem(GArray *params, void *user_ctx)
1156 if (params->len != 2) {
1157 gdb_put_packet("E22");
1161 /* gdb_memtohex() doubles the required space */
1162 if (get_param(params, 1)->val_ull > MAX_PACKET_LENGTH / 2) {
1163 gdb_put_packet("E22");
1167 g_byte_array_set_size(gdbserver_state.mem_buf,
1168 get_param(params, 1)->val_ull);
1170 if (gdb_target_memory_rw_debug(gdbserver_state.g_cpu,
1171 get_param(params, 0)->val_ull,
1172 gdbserver_state.mem_buf->data,
1173 gdbserver_state.mem_buf->len, false)) {
1174 gdb_put_packet("E14");
1178 gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data,
1179 gdbserver_state.mem_buf->len);
1183 static void handle_write_all_regs(GArray *params, void *user_ctx)
1194 cpu_synchronize_state(gdbserver_state.g_cpu);
1195 len = strlen(get_param(params, 0)->data) / 2;
1196 gdb_hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len);
1197 registers = gdbserver_state.mem_buf->data;
1199 reg_id < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0;
1201 reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, reg_id);
1203 registers += reg_size;
1205 gdb_put_packet("OK");
1208 static void handle_read_all_regs(GArray *params, void *user_ctx)
1213 cpu_synchronize_state(gdbserver_state.g_cpu);
1214 g_byte_array_set_size(gdbserver_state.mem_buf, 0);
1216 for (reg_id = 0; reg_id < gdbserver_state.g_cpu->gdb_num_g_regs; reg_id++) {
1217 len += gdb_read_register(gdbserver_state.g_cpu,
1218 gdbserver_state.mem_buf,
1221 g_assert(len == gdbserver_state.mem_buf->len);
1223 gdb_memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len);
1228 static void handle_step(GArray *params, void *user_ctx)
1231 gdb_set_cpu_pc(get_param(params, 0)->val_ull);
1234 cpu_single_step(gdbserver_state.c_cpu, gdbserver_state.sstep_flags);
1238 static void handle_backward(GArray *params, void *user_ctx)
1240 if (!gdb_can_reverse()) {
1241 gdb_put_packet("E22");
1243 if (params->len == 1) {
1244 switch (get_param(params, 0)->opcode) {
1246 if (replay_reverse_step()) {
1249 gdb_put_packet("E14");
1253 if (replay_reverse_continue()) {
1256 gdb_put_packet("E14");
1262 /* Default invalid command */
1266 static void handle_v_cont_query(GArray *params, void *user_ctx)
1268 gdb_put_packet("vCont;c;C;s;S");
1271 static void handle_v_cont(GArray *params, void *user_ctx)
1279 res = gdb_handle_vcont(get_param(params, 0)->data);
1280 if ((res == -EINVAL) || (res == -ERANGE)) {
1281 gdb_put_packet("E22");
1287 static void handle_v_attach(GArray *params, void *user_ctx)
1289 GDBProcess *process;
1292 g_string_assign(gdbserver_state.str_buf, "E22");
1297 process = gdb_get_process(get_param(params, 0)->val_ul);
1302 cpu = gdb_get_first_cpu_in_process(process);
1307 process->attached = true;
1308 gdbserver_state.g_cpu = cpu;
1309 gdbserver_state.c_cpu = cpu;
1311 if (gdbserver_state.allow_stop_reply) {
1312 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
1313 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
1314 g_string_append_c(gdbserver_state.str_buf, ';');
1315 gdbserver_state.allow_stop_reply = false;
1321 static void handle_v_kill(GArray *params, void *user_ctx)
1323 /* Kill the target */
1324 gdb_put_packet("OK");
1325 error_report("QEMU: Terminated via GDBstub");
1330 static const GdbCmdParseEntry gdb_v_commands_table[] = {
1331 /* Order is important if has same prefix */
1333 .handler = handle_v_cont_query,
1338 .handler = handle_v_cont,
1340 .cmd_startswith = 1,
1341 .allow_stop_reply = true,
1345 .handler = handle_v_attach,
1347 .cmd_startswith = 1,
1348 .allow_stop_reply = true,
1352 .handler = handle_v_kill,
1356 #ifdef CONFIG_USER_ONLY
1358 * Host I/O Packets. See [1] for details.
1359 * [1] https://sourceware.org/gdb/onlinedocs/gdb/Host-I_002fO-Packets.html
1362 .handler = gdb_handle_v_file_open,
1363 .cmd = "File:open:",
1364 .cmd_startswith = 1,
1368 .handler = gdb_handle_v_file_close,
1369 .cmd = "File:close:",
1370 .cmd_startswith = 1,
1374 .handler = gdb_handle_v_file_pread,
1375 .cmd = "File:pread:",
1376 .cmd_startswith = 1,
1380 .handler = gdb_handle_v_file_readlink,
1381 .cmd = "File:readlink:",
1382 .cmd_startswith = 1,
1388 static void handle_v_commands(GArray *params, void *user_ctx)
1394 if (process_string_cmd(get_param(params, 0)->data,
1395 gdb_v_commands_table,
1396 ARRAY_SIZE(gdb_v_commands_table))) {
1401 static void handle_query_qemu_sstepbits(GArray *params, void *user_ctx)
1403 g_string_printf(gdbserver_state.str_buf, "ENABLE=%x", SSTEP_ENABLE);
1405 if (gdbserver_state.supported_sstep_flags & SSTEP_NOIRQ) {
1406 g_string_append_printf(gdbserver_state.str_buf, ",NOIRQ=%x",
1410 if (gdbserver_state.supported_sstep_flags & SSTEP_NOTIMER) {
1411 g_string_append_printf(gdbserver_state.str_buf, ",NOTIMER=%x",
1418 static void handle_set_qemu_sstep(GArray *params, void *user_ctx)
1420 int new_sstep_flags;
1426 new_sstep_flags = get_param(params, 0)->val_ul;
1428 if (new_sstep_flags & ~gdbserver_state.supported_sstep_flags) {
1429 gdb_put_packet("E22");
1433 gdbserver_state.sstep_flags = new_sstep_flags;
1434 gdb_put_packet("OK");
1437 static void handle_query_qemu_sstep(GArray *params, void *user_ctx)
1439 g_string_printf(gdbserver_state.str_buf, "0x%x",
1440 gdbserver_state.sstep_flags);
1444 static void handle_query_curr_tid(GArray *params, void *user_ctx)
1447 GDBProcess *process;
1450 * "Current thread" remains vague in the spec, so always return
1451 * the first thread of the current process (gdb returns the
1454 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
1455 cpu = gdb_get_first_cpu_in_process(process);
1456 g_string_assign(gdbserver_state.str_buf, "QC");
1457 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
1461 static void handle_query_threads(GArray *params, void *user_ctx)
1463 if (!gdbserver_state.query_cpu) {
1464 gdb_put_packet("l");
1468 g_string_assign(gdbserver_state.str_buf, "m");
1469 gdb_append_thread_id(gdbserver_state.query_cpu, gdbserver_state.str_buf);
1471 gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu);
1474 static void handle_query_first_threads(GArray *params, void *user_ctx)
1476 gdbserver_state.query_cpu = gdb_first_attached_cpu();
1477 handle_query_threads(params, user_ctx);
1480 static void handle_query_thread_extra(GArray *params, void *user_ctx)
1482 g_autoptr(GString) rs = g_string_new(NULL);
1486 get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
1487 gdb_put_packet("E22");
1491 cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
1492 get_param(params, 0)->thread_id.tid);
1497 cpu_synchronize_state(cpu);
1499 if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) {
1500 /* Print the CPU model and name in multiprocess mode */
1501 ObjectClass *oc = object_get_class(OBJECT(cpu));
1502 const char *cpu_model = object_class_get_name(oc);
1503 const char *cpu_name =
1504 object_get_canonical_path_component(OBJECT(cpu));
1505 g_string_printf(rs, "%s %s [%s]", cpu_model, cpu_name,
1506 cpu->halted ? "halted " : "running");
1508 g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index,
1509 cpu->halted ? "halted " : "running");
1511 trace_gdbstub_op_extra_info(rs->str);
1512 gdb_memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len);
1516 static void handle_query_supported(GArray *params, void *user_ctx)
1520 g_string_printf(gdbserver_state.str_buf, "PacketSize=%x", MAX_PACKET_LENGTH);
1521 cc = CPU_GET_CLASS(first_cpu);
1522 if (cc->gdb_core_xml_file) {
1523 g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+");
1526 if (gdb_can_reverse()) {
1527 g_string_append(gdbserver_state.str_buf,
1528 ";ReverseStep+;ReverseContinue+");
1531 #if defined(CONFIG_USER_ONLY)
1532 #if defined(CONFIG_LINUX)
1533 if (gdbserver_state.c_cpu->opaque) {
1534 g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+");
1537 g_string_append(gdbserver_state.str_buf, ";qXfer:exec-file:read+");
1541 strstr(get_param(params, 0)->data, "multiprocess+")) {
1542 gdbserver_state.multiprocess = true;
1545 g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+");
1549 static void handle_query_xfer_features(GArray *params, void *user_ctx)
1551 GDBProcess *process;
1553 unsigned long len, total_len, addr;
1557 if (params->len < 3) {
1558 gdb_put_packet("E22");
1562 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
1563 cc = CPU_GET_CLASS(gdbserver_state.g_cpu);
1564 if (!cc->gdb_core_xml_file) {
1569 p = get_param(params, 0)->data;
1570 xml = get_feature_xml(p, &p, process);
1572 gdb_put_packet("E00");
1576 addr = get_param(params, 1)->val_ul;
1577 len = get_param(params, 2)->val_ul;
1578 total_len = strlen(xml);
1579 if (addr > total_len) {
1580 gdb_put_packet("E00");
1584 if (len > (MAX_PACKET_LENGTH - 5) / 2) {
1585 len = (MAX_PACKET_LENGTH - 5) / 2;
1588 if (len < total_len - addr) {
1589 g_string_assign(gdbserver_state.str_buf, "m");
1590 gdb_memtox(gdbserver_state.str_buf, xml + addr, len);
1592 g_string_assign(gdbserver_state.str_buf, "l");
1593 gdb_memtox(gdbserver_state.str_buf, xml + addr, total_len - addr);
1596 gdb_put_packet_binary(gdbserver_state.str_buf->str,
1597 gdbserver_state.str_buf->len, true);
1600 static void handle_query_qemu_supported(GArray *params, void *user_ctx)
1602 g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep");
1603 #ifndef CONFIG_USER_ONLY
1604 g_string_append(gdbserver_state.str_buf, ";PhyMemMode");
1609 static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
1610 /* Order is important if has same prefix */
1612 .handler = handle_query_qemu_sstepbits,
1613 .cmd = "qemu.sstepbits",
1616 .handler = handle_query_qemu_sstep,
1617 .cmd = "qemu.sstep",
1620 .handler = handle_set_qemu_sstep,
1621 .cmd = "qemu.sstep=",
1622 .cmd_startswith = 1,
1627 static const GdbCmdParseEntry gdb_gen_query_table[] = {
1629 .handler = handle_query_curr_tid,
1633 .handler = handle_query_threads,
1634 .cmd = "sThreadInfo",
1637 .handler = handle_query_first_threads,
1638 .cmd = "fThreadInfo",
1641 .handler = handle_query_thread_extra,
1642 .cmd = "ThreadExtraInfo,",
1643 .cmd_startswith = 1,
1646 #ifdef CONFIG_USER_ONLY
1648 .handler = gdb_handle_query_offsets,
1653 .handler = gdb_handle_query_rcmd,
1655 .cmd_startswith = 1,
1660 .handler = handle_query_supported,
1661 .cmd = "Supported:",
1662 .cmd_startswith = 1,
1666 .handler = handle_query_supported,
1671 .handler = handle_query_xfer_features,
1672 .cmd = "Xfer:features:read:",
1673 .cmd_startswith = 1,
1676 #if defined(CONFIG_USER_ONLY)
1677 #if defined(CONFIG_LINUX)
1679 .handler = gdb_handle_query_xfer_auxv,
1680 .cmd = "Xfer:auxv:read::",
1681 .cmd_startswith = 1,
1686 .handler = gdb_handle_query_xfer_exec_file,
1687 .cmd = "Xfer:exec-file:read:",
1688 .cmd_startswith = 1,
1693 .handler = gdb_handle_query_attached,
1698 .handler = gdb_handle_query_attached,
1702 .handler = handle_query_qemu_supported,
1703 .cmd = "qemu.Supported",
1705 #ifndef CONFIG_USER_ONLY
1707 .handler = gdb_handle_query_qemu_phy_mem_mode,
1708 .cmd = "qemu.PhyMemMode",
1713 static const GdbCmdParseEntry gdb_gen_set_table[] = {
1714 /* Order is important if has same prefix */
1716 .handler = handle_set_qemu_sstep,
1717 .cmd = "qemu.sstep:",
1718 .cmd_startswith = 1,
1721 #ifndef CONFIG_USER_ONLY
1723 .handler = gdb_handle_set_qemu_phy_mem_mode,
1724 .cmd = "qemu.PhyMemMode:",
1725 .cmd_startswith = 1,
1731 static void handle_gen_query(GArray *params, void *user_ctx)
1737 if (!process_string_cmd(get_param(params, 0)->data,
1738 gdb_gen_query_set_common_table,
1739 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
1743 if (process_string_cmd(get_param(params, 0)->data,
1744 gdb_gen_query_table,
1745 ARRAY_SIZE(gdb_gen_query_table))) {
1750 static void handle_gen_set(GArray *params, void *user_ctx)
1756 if (!process_string_cmd(get_param(params, 0)->data,
1757 gdb_gen_query_set_common_table,
1758 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
1762 if (process_string_cmd(get_param(params, 0)->data,
1764 ARRAY_SIZE(gdb_gen_set_table))) {
1769 static void handle_target_halt(GArray *params, void *user_ctx)
1771 if (gdbserver_state.allow_stop_reply) {
1772 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
1773 gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf);
1774 g_string_append_c(gdbserver_state.str_buf, ';');
1776 gdbserver_state.allow_stop_reply = false;
1779 * Remove all the breakpoints when this query is issued,
1780 * because gdb is doing an initial connect and the state
1781 * should be cleaned up.
1783 gdb_breakpoint_remove_all(gdbserver_state.c_cpu);
1786 static int gdb_handle_packet(const char *line_buf)
1788 const GdbCmdParseEntry *cmd_parser = NULL;
1790 trace_gdbstub_io_command(line_buf);
1792 switch (line_buf[0]) {
1794 gdb_put_packet("OK");
1798 static const GdbCmdParseEntry target_halted_cmd_desc = {
1799 .handler = handle_target_halt,
1801 .cmd_startswith = 1,
1802 .allow_stop_reply = true,
1804 cmd_parser = &target_halted_cmd_desc;
1809 static const GdbCmdParseEntry continue_cmd_desc = {
1810 .handler = handle_continue,
1812 .cmd_startswith = 1,
1813 .allow_stop_reply = true,
1816 cmd_parser = &continue_cmd_desc;
1821 static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
1822 .handler = handle_cont_with_sig,
1824 .cmd_startswith = 1,
1825 .allow_stop_reply = true,
1828 cmd_parser = &cont_with_sig_cmd_desc;
1833 static const GdbCmdParseEntry v_cmd_desc = {
1834 .handler = handle_v_commands,
1836 .cmd_startswith = 1,
1839 cmd_parser = &v_cmd_desc;
1843 /* Kill the target */
1844 error_report("QEMU: Terminated via GDBstub");
1850 static const GdbCmdParseEntry detach_cmd_desc = {
1851 .handler = handle_detach,
1853 .cmd_startswith = 1,
1856 cmd_parser = &detach_cmd_desc;
1861 static const GdbCmdParseEntry step_cmd_desc = {
1862 .handler = handle_step,
1864 .cmd_startswith = 1,
1865 .allow_stop_reply = true,
1868 cmd_parser = &step_cmd_desc;
1873 static const GdbCmdParseEntry backward_cmd_desc = {
1874 .handler = handle_backward,
1876 .cmd_startswith = 1,
1877 .allow_stop_reply = true,
1880 cmd_parser = &backward_cmd_desc;
1885 static const GdbCmdParseEntry file_io_cmd_desc = {
1886 .handler = gdb_handle_file_io,
1888 .cmd_startswith = 1,
1891 cmd_parser = &file_io_cmd_desc;
1896 static const GdbCmdParseEntry read_all_regs_cmd_desc = {
1897 .handler = handle_read_all_regs,
1901 cmd_parser = &read_all_regs_cmd_desc;
1906 static const GdbCmdParseEntry write_all_regs_cmd_desc = {
1907 .handler = handle_write_all_regs,
1909 .cmd_startswith = 1,
1912 cmd_parser = &write_all_regs_cmd_desc;
1917 static const GdbCmdParseEntry read_mem_cmd_desc = {
1918 .handler = handle_read_mem,
1920 .cmd_startswith = 1,
1923 cmd_parser = &read_mem_cmd_desc;
1928 static const GdbCmdParseEntry write_mem_cmd_desc = {
1929 .handler = handle_write_mem,
1931 .cmd_startswith = 1,
1934 cmd_parser = &write_mem_cmd_desc;
1939 static const GdbCmdParseEntry get_reg_cmd_desc = {
1940 .handler = handle_get_reg,
1942 .cmd_startswith = 1,
1945 cmd_parser = &get_reg_cmd_desc;
1950 static const GdbCmdParseEntry set_reg_cmd_desc = {
1951 .handler = handle_set_reg,
1953 .cmd_startswith = 1,
1956 cmd_parser = &set_reg_cmd_desc;
1961 static const GdbCmdParseEntry insert_bp_cmd_desc = {
1962 .handler = handle_insert_bp,
1964 .cmd_startswith = 1,
1967 cmd_parser = &insert_bp_cmd_desc;
1972 static const GdbCmdParseEntry remove_bp_cmd_desc = {
1973 .handler = handle_remove_bp,
1975 .cmd_startswith = 1,
1978 cmd_parser = &remove_bp_cmd_desc;
1983 static const GdbCmdParseEntry set_thread_cmd_desc = {
1984 .handler = handle_set_thread,
1986 .cmd_startswith = 1,
1989 cmd_parser = &set_thread_cmd_desc;
1994 static const GdbCmdParseEntry thread_alive_cmd_desc = {
1995 .handler = handle_thread_alive,
1997 .cmd_startswith = 1,
2000 cmd_parser = &thread_alive_cmd_desc;
2005 static const GdbCmdParseEntry gen_query_cmd_desc = {
2006 .handler = handle_gen_query,
2008 .cmd_startswith = 1,
2011 cmd_parser = &gen_query_cmd_desc;
2016 static const GdbCmdParseEntry gen_set_cmd_desc = {
2017 .handler = handle_gen_set,
2019 .cmd_startswith = 1,
2022 cmd_parser = &gen_set_cmd_desc;
2026 /* put empty packet */
2032 run_cmd_parser(line_buf, cmd_parser);
2038 void gdb_set_stop_cpu(CPUState *cpu)
2040 GDBProcess *p = gdb_get_cpu_process(cpu);
2044 * Having a stop CPU corresponding to a process that is not attached
2045 * confuses GDB. So we ignore the request.
2050 gdbserver_state.c_cpu = cpu;
2051 gdbserver_state.g_cpu = cpu;
2054 void gdb_read_byte(uint8_t ch)
2058 gdbserver_state.allow_stop_reply = false;
2059 #ifndef CONFIG_USER_ONLY
2060 if (gdbserver_state.last_packet->len) {
2061 /* Waiting for a response to the last packet. If we see the start
2062 of a new command then abandon the previous response. */
2064 trace_gdbstub_err_got_nack();
2065 gdb_put_buffer(gdbserver_state.last_packet->data,
2066 gdbserver_state.last_packet->len);
2067 } else if (ch == '+') {
2068 trace_gdbstub_io_got_ack();
2070 trace_gdbstub_io_got_unexpected(ch);
2073 if (ch == '+' || ch == '$') {
2074 g_byte_array_set_size(gdbserver_state.last_packet, 0);
2079 if (runstate_is_running()) {
2081 * When the CPU is running, we cannot do anything except stop
2082 * it when receiving a char. This is expected on a Ctrl-C in the
2083 * gdb client. Because we are in all-stop mode, gdb sends a
2084 * 0x03 byte which is not a usual packet, so we handle it specially
2085 * here, but it does expect a stop reply.
2088 trace_gdbstub_err_unexpected_runpkt(ch);
2090 gdbserver_state.allow_stop_reply = true;
2092 vm_stop(RUN_STATE_PAUSED);
2096 switch(gdbserver_state.state) {
2099 /* start of command packet */
2100 gdbserver_state.line_buf_index = 0;
2101 gdbserver_state.line_sum = 0;
2102 gdbserver_state.state = RS_GETLINE;
2103 } else if (ch == '+') {
2105 * do nothing, gdb may preemptively send out ACKs on
2106 * initial connection
2109 trace_gdbstub_err_garbage(ch);
2114 /* start escape sequence */
2115 gdbserver_state.state = RS_GETLINE_ESC;
2116 gdbserver_state.line_sum += ch;
2117 } else if (ch == '*') {
2118 /* start run length encoding sequence */
2119 gdbserver_state.state = RS_GETLINE_RLE;
2120 gdbserver_state.line_sum += ch;
2121 } else if (ch == '#') {
2122 /* end of command, start of checksum*/
2123 gdbserver_state.state = RS_CHKSUM1;
2124 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
2125 trace_gdbstub_err_overrun();
2126 gdbserver_state.state = RS_IDLE;
2128 /* unescaped command character */
2129 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch;
2130 gdbserver_state.line_sum += ch;
2133 case RS_GETLINE_ESC:
2135 /* unexpected end of command in escape sequence */
2136 gdbserver_state.state = RS_CHKSUM1;
2137 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
2138 /* command buffer overrun */
2139 trace_gdbstub_err_overrun();
2140 gdbserver_state.state = RS_IDLE;
2142 /* parse escaped character and leave escape state */
2143 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20;
2144 gdbserver_state.line_sum += ch;
2145 gdbserver_state.state = RS_GETLINE;
2148 case RS_GETLINE_RLE:
2150 * Run-length encoding is explained in "Debugging with GDB /
2151 * Appendix E GDB Remote Serial Protocol / Overview".
2153 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
2154 /* invalid RLE count encoding */
2155 trace_gdbstub_err_invalid_repeat(ch);
2156 gdbserver_state.state = RS_GETLINE;
2158 /* decode repeat length */
2159 int repeat = ch - ' ' + 3;
2160 if (gdbserver_state.line_buf_index + repeat >= sizeof(gdbserver_state.line_buf) - 1) {
2161 /* that many repeats would overrun the command buffer */
2162 trace_gdbstub_err_overrun();
2163 gdbserver_state.state = RS_IDLE;
2164 } else if (gdbserver_state.line_buf_index < 1) {
2165 /* got a repeat but we have nothing to repeat */
2166 trace_gdbstub_err_invalid_rle();
2167 gdbserver_state.state = RS_GETLINE;
2169 /* repeat the last character */
2170 memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index,
2171 gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat);
2172 gdbserver_state.line_buf_index += repeat;
2173 gdbserver_state.line_sum += ch;
2174 gdbserver_state.state = RS_GETLINE;
2179 /* get high hex digit of checksum */
2180 if (!isxdigit(ch)) {
2181 trace_gdbstub_err_checksum_invalid(ch);
2182 gdbserver_state.state = RS_GETLINE;
2185 gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0';
2186 gdbserver_state.line_csum = fromhex(ch) << 4;
2187 gdbserver_state.state = RS_CHKSUM2;
2190 /* get low hex digit of checksum */
2191 if (!isxdigit(ch)) {
2192 trace_gdbstub_err_checksum_invalid(ch);
2193 gdbserver_state.state = RS_GETLINE;
2196 gdbserver_state.line_csum |= fromhex(ch);
2198 if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) {
2199 trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum);
2200 /* send NAK reply */
2202 gdb_put_buffer(&reply, 1);
2203 gdbserver_state.state = RS_IDLE;
2205 /* send ACK reply */
2207 gdb_put_buffer(&reply, 1);
2208 gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf);
2218 * Create the process that will contain all the "orphan" CPUs (that are not
2219 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2220 * be attachable and thus will be invisible to the user.
2222 void gdb_create_default_process(GDBState *s)
2224 GDBProcess *process;
2227 #ifdef CONFIG_USER_ONLY
2228 assert(gdbserver_state.process_num == 0);
2231 if (gdbserver_state.process_num) {
2232 pid = s->processes[s->process_num - 1].pid;
2236 /* We need an available PID slot for this process */
2237 assert(pid < UINT32_MAX);
2241 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2242 process = &s->processes[s->process_num - 1];
2244 process->attached = false;
2245 process->target_xml = NULL;