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
;
54 struct GDBRegisterState
*next
;
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
;
80 /* writes 2*len+1 bytes in buf */
81 void gdb_memtohex(GString
*buf
, const uint8_t *mem
, int len
)
84 for(i
= 0; i
< len
; 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
)
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);
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];
109 for (i
= 0; i
< len
|| (i
& 0xF); ++i
) {
110 size_t byte_ofs
= i
& 15;
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
;
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)
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
)
142 if (dump
&& trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY
)) {
143 hexdump(buf
, len
, trace_gdbstub_io_binaryreply
);
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
);
153 for(i
= 0; i
< len
; i
++) {
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()) {
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
)
192 case '#': case '$': case '*': case '}':
193 g_string_append_c(buf
, '}');
194 g_string_append_c(buf
, c
^ 0x20);
197 g_string_append_c(buf
, c
);
203 static uint32_t gdb_get_cpu_pid(CPUState
*cpu
)
205 #ifdef CONFIG_USER_ONLY
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;
217 GDBProcess
*gdb_get_process(uint32_t 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
];
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
)
245 if (gdb_get_cpu_index(cpu
) == thread_id
) {
253 CPUState
*gdb_get_first_cpu_in_process(GDBProcess
*process
)
258 if (gdb_get_cpu_pid(cpu
) == process
->pid
) {
266 static CPUState
*gdb_next_cpu_in_process(CPUState
*cpu
)
268 uint32_t pid
= gdb_get_cpu_pid(cpu
);
272 if (gdb_get_cpu_pid(cpu
) == pid
) {
282 /* Return the cpu following @cpu, while ignoring unattached processes. */
283 static CPUState
*gdb_next_attached_cpu(CPUState
*cpu
)
288 if (gdb_get_cpu_process(cpu
)->attached
) {
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
);
311 static CPUState
*gdb_get_cpu(uint32_t pid
, uint32_t 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
) {
327 if (!process
->attached
) {
331 return gdb_get_first_cpu_in_process(process
);
333 /* a specific thread */
340 process
= gdb_get_cpu_process(cpu
);
342 if (pid
&& process
->pid
!= pid
) {
346 if (!process
->attached
) {
354 static const char *get_feature_xml(const char *p
, const char **newp
,
360 CPUState
*cpu
= gdb_get_first_cpu_in_process(process
);
361 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
364 while (p
[len
] && p
[len
] != ':')
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. */
378 "<?xml version=\"1.0\"?>"
379 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
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>");
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>");
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
);
410 name
= xml_builtin
[i
][0];
411 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
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
;
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
);
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
;
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
);
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
)
464 GDBRegisterState
**p
;
468 /* Check for duplicates. */
469 if (strcmp((*p
)->xml
, xml
) == 0)
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
;
481 /* Add to end of list. */
482 cpu
->gdb_num_regs
+= num_regs
;
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
);
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
);
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
);
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
));
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
)
531 ret
= qemu_strtoul(buf
, &buf
, 16, &p
);
534 return GDB_READ_THREAD_ERR
;
543 ret
= qemu_strtoul(buf
, &buf
, 16, &t
);
546 return GDB_READ_THREAD_ERR
;
552 return GDB_ALL_PROCESSES
;
560 return GDB_ALL_THREADS
;
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
)
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 */
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.
606 if (cur_action
== 'C' || cur_action
== 'S') {
607 cur_action
= qemu_tolower(cur_action
);
608 res
= qemu_strtoul(p
, &p
, 16, &tmp
);
612 signal
= gdb_signal_to_target(tmp
);
613 } else if (cur_action
!= 'c' && cur_action
!= 's') {
614 /* unknown/invalid/unsupported command */
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
);
632 case GDB_READ_THREAD_ERR
:
635 case GDB_ALL_PROCESSES
:
636 cpu
= gdb_first_attached_cpu();
638 if (newstates
[cpu
->cpu_index
] == 1) {
639 newstates
[cpu
->cpu_index
] = cur_action
;
642 cpu
= gdb_next_attached_cpu(cpu
);
646 case GDB_ALL_THREADS
:
647 process
= gdb_get_process(pid
);
649 if (!process
->attached
) {
653 cpu
= gdb_get_first_cpu_in_process(process
);
655 if (newstates
[cpu
->cpu_index
] == 1) {
656 newstates
[cpu
->cpu_index
] = cur_action
;
659 cpu
= gdb_next_cpu_in_process(cpu
);
664 cpu
= gdb_get_cpu(pid
, tid
);
666 /* invalid CPU/thread specified */
671 /* only use if no previous match occourred */
672 if (newstates
[cpu
->cpu_index
] == 1) {
673 newstates
[cpu
->cpu_index
] = cur_action
;
679 gdbserver_state
.signal
= signal
;
680 gdb_continue_partial(newstates
);
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
) {
697 curr_delimiters
[0] = delimiter
;
698 delimiters
= curr_delimiters
;
701 param
+= strcspn(param
, delimiters
);
708 static int cmd_parse_params(const char *data
, const char *schema
,
711 const char *curr_schema
, *curr_data
;
714 g_assert(params
->len
== 0);
716 curr_schema
= schema
;
718 while (curr_schema
[0] && curr_schema
[1] && *curr_data
) {
719 GdbCmdVariant this_param
;
721 switch (curr_schema
[0]) {
723 if (qemu_strtoul(curr_data
, &curr_data
, 16,
724 &this_param
.val_ul
)) {
727 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
728 g_array_append_val(params
, this_param
);
731 if (qemu_strtou64(curr_data
, &curr_data
, 16,
732 (uint64_t *)&this_param
.val_ull
)) {
735 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
736 g_array_append_val(params
, this_param
);
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
);
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
);
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
);
757 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
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
;
802 bool allow_stop_reply
;
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
)
814 g_autoptr(GArray
) params
= g_array_new(false, true, sizeof(GdbCmdVariant
));
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
))) {
830 if (cmd_parse_params(&data
[strlen(cmd
->cmd
)],
831 cmd
->schema
, params
)) {
836 gdbserver_state
.allow_stop_reply
= cmd
->allow_stop_reply
;
837 cmd
->handler(params
, user_ctx
);
844 static void run_cmd_parser(const char *data
, const GdbCmdParseEntry
*cmd
)
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)) {
860 static void handle_detach(GArray
*params
, void *user_ctx
)
865 if (gdbserver_state
.multiprocess
) {
867 gdb_put_packet("E22");
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();
891 gdb_put_packet("OK");
894 static void handle_thread_alive(GArray
*params
, void *user_ctx
)
899 gdb_put_packet("E22");
903 if (get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
904 gdb_put_packet("E22");
908 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
909 get_param(params
, 0)->thread_id
.tid
);
911 gdb_put_packet("E22");
915 gdb_put_packet("OK");
918 static void handle_continue(GArray
*params
, void *user_ctx
)
921 gdb_set_cpu_pc(get_param(params
, 0)->val_ull
);
924 gdbserver_state
.signal
= 0;
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
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;
947 static void handle_set_thread(GArray
*params
, void *user_ctx
)
951 if (params
->len
!= 2) {
952 gdb_put_packet("E22");
956 if (get_param(params
, 1)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
957 gdb_put_packet("E22");
961 if (get_param(params
, 1)->thread_id
.kind
!= GDB_ONE_THREAD
) {
962 gdb_put_packet("OK");
966 cpu
= gdb_get_cpu(get_param(params
, 1)->thread_id
.pid
,
967 get_param(params
, 1)->thread_id
.tid
);
969 gdb_put_packet("E22");
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
) {
979 gdbserver_state
.c_cpu
= cpu
;
980 gdb_put_packet("OK");
983 gdbserver_state
.g_cpu
= cpu
;
984 gdb_put_packet("OK");
987 gdb_put_packet("E22");
992 static void handle_insert_bp(GArray
*params
, void *user_ctx
)
996 if (params
->len
!= 3) {
997 gdb_put_packet("E22");
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
);
1006 gdb_put_packet("OK");
1008 } else if (res
== -ENOSYS
) {
1013 gdb_put_packet("E22");
1016 static void handle_remove_bp(GArray
*params
, void *user_ctx
)
1020 if (params
->len
!= 3) {
1021 gdb_put_packet("E22");
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
);
1030 gdb_put_packet("OK");
1032 } else if (res
== -ENOSYS
) {
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
)
1060 if (params
->len
!= 2) {
1061 gdb_put_packet("E22");
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
)
1082 gdb_put_packet("E14");
1086 reg_size
= gdb_read_register(gdbserver_state
.g_cpu
,
1087 gdbserver_state
.mem_buf
,
1088 get_param(params
, 0)->val_ull
);
1090 gdb_put_packet("E14");
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
);
1101 static void handle_write_mem(GArray
*params
, void *user_ctx
)
1103 if (params
->len
!= 3) {
1104 gdb_put_packet("E22");
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");
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");
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");
1135 /* gdb_memtohex() doubles the required space */
1136 if (get_param(params
, 1)->val_ull
> MAX_PACKET_LENGTH
/ 2) {
1137 gdb_put_packet("E22");
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");
1152 gdb_memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
,
1153 gdbserver_state
.mem_buf
->len
);
1157 static void handle_write_all_regs(GArray
*params
, void *user_ctx
)
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
;
1173 reg_id
< gdbserver_state
.g_cpu
->gdb_num_g_regs
&& len
> 0;
1175 reg_size
= gdb_write_register(gdbserver_state
.g_cpu
, registers
, reg_id
);
1177 registers
+= reg_size
;
1179 gdb_put_packet("OK");
1182 static void handle_read_all_regs(GArray
*params
, void *user_ctx
)
1187 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1188 g_byte_array_set_size(gdbserver_state
.mem_buf
, 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
,
1195 g_assert(len
== gdbserver_state
.mem_buf
->len
);
1197 gdb_memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
, len
);
1202 static void handle_step(GArray
*params
, void *user_ctx
)
1205 gdb_set_cpu_pc(get_param(params
, 0)->val_ull
);
1208 cpu_single_step(gdbserver_state
.c_cpu
, gdbserver_state
.sstep_flags
);
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
) {
1220 if (replay_reverse_step()) {
1223 gdb_put_packet("E14");
1227 if (replay_reverse_continue()) {
1230 gdb_put_packet("E14");
1236 /* Default invalid command */
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
)
1253 res
= gdb_handle_vcont(get_param(params
, 0)->data
);
1254 if ((res
== -EINVAL
) || (res
== -ERANGE
)) {
1255 gdb_put_packet("E22");
1261 static void handle_v_attach(GArray
*params
, void *user_ctx
)
1263 GDBProcess
*process
;
1266 g_string_assign(gdbserver_state
.str_buf
, "E22");
1271 process
= gdb_get_process(get_param(params
, 0)->val_ul
);
1276 cpu
= gdb_get_first_cpu_in_process(process
);
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;
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");
1304 static const GdbCmdParseEntry gdb_v_commands_table
[] = {
1305 /* Order is important if has same prefix */
1307 .handler
= handle_v_cont_query
,
1312 .handler
= handle_v_cont
,
1314 .cmd_startswith
= 1,
1315 .allow_stop_reply
= true,
1319 .handler
= handle_v_attach
,
1321 .cmd_startswith
= 1,
1322 .allow_stop_reply
= true,
1326 .handler
= handle_v_kill
,
1330 #ifdef CONFIG_USER_ONLY
1332 * Host I/O Packets. See [1] for details.
1333 * [1] https://sourceware.org/gdb/onlinedocs/gdb/Host-I_002fO-Packets.html
1336 .handler
= gdb_handle_v_file_open
,
1337 .cmd
= "File:open:",
1338 .cmd_startswith
= 1,
1342 .handler
= gdb_handle_v_file_close
,
1343 .cmd
= "File:close:",
1344 .cmd_startswith
= 1,
1348 .handler
= gdb_handle_v_file_pread
,
1349 .cmd
= "File:pread:",
1350 .cmd_startswith
= 1,
1354 .handler
= gdb_handle_v_file_readlink
,
1355 .cmd
= "File:readlink:",
1356 .cmd_startswith
= 1,
1362 static void handle_v_commands(GArray
*params
, void *user_ctx
)
1368 if (process_string_cmd(NULL
, get_param(params
, 0)->data
,
1369 gdb_v_commands_table
,
1370 ARRAY_SIZE(gdb_v_commands_table
))) {
1375 static void handle_query_qemu_sstepbits(GArray
*params
, void *user_ctx
)
1377 g_string_printf(gdbserver_state
.str_buf
, "ENABLE=%x", SSTEP_ENABLE
);
1379 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOIRQ
) {
1380 g_string_append_printf(gdbserver_state
.str_buf
, ",NOIRQ=%x",
1384 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOTIMER
) {
1385 g_string_append_printf(gdbserver_state
.str_buf
, ",NOTIMER=%x",
1392 static void handle_set_qemu_sstep(GArray
*params
, void *user_ctx
)
1394 int new_sstep_flags
;
1400 new_sstep_flags
= get_param(params
, 0)->val_ul
;
1402 if (new_sstep_flags
& ~gdbserver_state
.supported_sstep_flags
) {
1403 gdb_put_packet("E22");
1407 gdbserver_state
.sstep_flags
= new_sstep_flags
;
1408 gdb_put_packet("OK");
1411 static void handle_query_qemu_sstep(GArray
*params
, void *user_ctx
)
1413 g_string_printf(gdbserver_state
.str_buf
, "0x%x",
1414 gdbserver_state
.sstep_flags
);
1418 static void handle_query_curr_tid(GArray
*params
, void *user_ctx
)
1421 GDBProcess
*process
;
1424 * "Current thread" remains vague in the spec, so always return
1425 * the first thread of the current process (gdb returns the
1428 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
1429 cpu
= gdb_get_first_cpu_in_process(process
);
1430 g_string_assign(gdbserver_state
.str_buf
, "QC");
1431 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
1435 static void handle_query_threads(GArray
*params
, void *user_ctx
)
1437 if (!gdbserver_state
.query_cpu
) {
1438 gdb_put_packet("l");
1442 g_string_assign(gdbserver_state
.str_buf
, "m");
1443 gdb_append_thread_id(gdbserver_state
.query_cpu
, gdbserver_state
.str_buf
);
1445 gdbserver_state
.query_cpu
= gdb_next_attached_cpu(gdbserver_state
.query_cpu
);
1448 static void handle_query_first_threads(GArray
*params
, void *user_ctx
)
1450 gdbserver_state
.query_cpu
= gdb_first_attached_cpu();
1451 handle_query_threads(params
, user_ctx
);
1454 static void handle_query_thread_extra(GArray
*params
, void *user_ctx
)
1456 g_autoptr(GString
) rs
= g_string_new(NULL
);
1460 get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1461 gdb_put_packet("E22");
1465 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
1466 get_param(params
, 0)->thread_id
.tid
);
1471 cpu_synchronize_state(cpu
);
1473 if (gdbserver_state
.multiprocess
&& (gdbserver_state
.process_num
> 1)) {
1474 /* Print the CPU model and name in multiprocess mode */
1475 ObjectClass
*oc
= object_get_class(OBJECT(cpu
));
1476 const char *cpu_model
= object_class_get_name(oc
);
1477 const char *cpu_name
=
1478 object_get_canonical_path_component(OBJECT(cpu
));
1479 g_string_printf(rs
, "%s %s [%s]", cpu_model
, cpu_name
,
1480 cpu
->halted
? "halted " : "running");
1482 g_string_printf(rs
, "CPU#%d [%s]", cpu
->cpu_index
,
1483 cpu
->halted
? "halted " : "running");
1485 trace_gdbstub_op_extra_info(rs
->str
);
1486 gdb_memtohex(gdbserver_state
.str_buf
, (uint8_t *)rs
->str
, rs
->len
);
1490 static void handle_query_supported(GArray
*params
, void *user_ctx
)
1494 g_string_printf(gdbserver_state
.str_buf
, "PacketSize=%x", MAX_PACKET_LENGTH
);
1495 cc
= CPU_GET_CLASS(first_cpu
);
1496 if (cc
->gdb_core_xml_file
) {
1497 g_string_append(gdbserver_state
.str_buf
, ";qXfer:features:read+");
1500 if (gdb_can_reverse()) {
1501 g_string_append(gdbserver_state
.str_buf
,
1502 ";ReverseStep+;ReverseContinue+");
1505 #if defined(CONFIG_USER_ONLY)
1506 #if defined(CONFIG_LINUX)
1507 if (gdbserver_state
.c_cpu
->opaque
) {
1508 g_string_append(gdbserver_state
.str_buf
, ";qXfer:auxv:read+");
1511 g_string_append(gdbserver_state
.str_buf
, ";qXfer:exec-file:read+");
1515 strstr(get_param(params
, 0)->data
, "multiprocess+")) {
1516 gdbserver_state
.multiprocess
= true;
1519 g_string_append(gdbserver_state
.str_buf
, ";vContSupported+;multiprocess+");
1523 static void handle_query_xfer_features(GArray
*params
, void *user_ctx
)
1525 GDBProcess
*process
;
1527 unsigned long len
, total_len
, addr
;
1531 if (params
->len
< 3) {
1532 gdb_put_packet("E22");
1536 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
1537 cc
= CPU_GET_CLASS(gdbserver_state
.g_cpu
);
1538 if (!cc
->gdb_core_xml_file
) {
1544 p
= get_param(params
, 0)->data
;
1545 xml
= get_feature_xml(p
, &p
, process
);
1547 gdb_put_packet("E00");
1551 addr
= get_param(params
, 1)->val_ul
;
1552 len
= get_param(params
, 2)->val_ul
;
1553 total_len
= strlen(xml
);
1554 if (addr
> total_len
) {
1555 gdb_put_packet("E00");
1559 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
1560 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1563 if (len
< total_len
- addr
) {
1564 g_string_assign(gdbserver_state
.str_buf
, "m");
1565 gdb_memtox(gdbserver_state
.str_buf
, xml
+ addr
, len
);
1567 g_string_assign(gdbserver_state
.str_buf
, "l");
1568 gdb_memtox(gdbserver_state
.str_buf
, xml
+ addr
, total_len
- addr
);
1571 gdb_put_packet_binary(gdbserver_state
.str_buf
->str
,
1572 gdbserver_state
.str_buf
->len
, true);
1575 static void handle_query_qemu_supported(GArray
*params
, void *user_ctx
)
1577 g_string_printf(gdbserver_state
.str_buf
, "sstepbits;sstep");
1578 #ifndef CONFIG_USER_ONLY
1579 g_string_append(gdbserver_state
.str_buf
, ";PhyMemMode");
1584 static const GdbCmdParseEntry gdb_gen_query_set_common_table
[] = {
1585 /* Order is important if has same prefix */
1587 .handler
= handle_query_qemu_sstepbits
,
1588 .cmd
= "qemu.sstepbits",
1591 .handler
= handle_query_qemu_sstep
,
1592 .cmd
= "qemu.sstep",
1595 .handler
= handle_set_qemu_sstep
,
1596 .cmd
= "qemu.sstep=",
1597 .cmd_startswith
= 1,
1602 static const GdbCmdParseEntry gdb_gen_query_table
[] = {
1604 .handler
= handle_query_curr_tid
,
1608 .handler
= handle_query_threads
,
1609 .cmd
= "sThreadInfo",
1612 .handler
= handle_query_first_threads
,
1613 .cmd
= "fThreadInfo",
1616 .handler
= handle_query_thread_extra
,
1617 .cmd
= "ThreadExtraInfo,",
1618 .cmd_startswith
= 1,
1621 #ifdef CONFIG_USER_ONLY
1623 .handler
= gdb_handle_query_offsets
,
1628 .handler
= gdb_handle_query_rcmd
,
1630 .cmd_startswith
= 1,
1635 .handler
= handle_query_supported
,
1636 .cmd
= "Supported:",
1637 .cmd_startswith
= 1,
1641 .handler
= handle_query_supported
,
1646 .handler
= handle_query_xfer_features
,
1647 .cmd
= "Xfer:features:read:",
1648 .cmd_startswith
= 1,
1651 #if defined(CONFIG_USER_ONLY)
1652 #if defined(CONFIG_LINUX)
1654 .handler
= gdb_handle_query_xfer_auxv
,
1655 .cmd
= "Xfer:auxv:read::",
1656 .cmd_startswith
= 1,
1661 .handler
= gdb_handle_query_xfer_exec_file
,
1662 .cmd
= "Xfer:exec-file:read:",
1663 .cmd_startswith
= 1,
1668 .handler
= gdb_handle_query_attached
,
1673 .handler
= gdb_handle_query_attached
,
1677 .handler
= handle_query_qemu_supported
,
1678 .cmd
= "qemu.Supported",
1680 #ifndef CONFIG_USER_ONLY
1682 .handler
= gdb_handle_query_qemu_phy_mem_mode
,
1683 .cmd
= "qemu.PhyMemMode",
1688 static const GdbCmdParseEntry gdb_gen_set_table
[] = {
1689 /* Order is important if has same prefix */
1691 .handler
= handle_set_qemu_sstep
,
1692 .cmd
= "qemu.sstep:",
1693 .cmd_startswith
= 1,
1696 #ifndef CONFIG_USER_ONLY
1698 .handler
= gdb_handle_set_qemu_phy_mem_mode
,
1699 .cmd
= "qemu.PhyMemMode:",
1700 .cmd_startswith
= 1,
1706 static void handle_gen_query(GArray
*params
, void *user_ctx
)
1712 if (!process_string_cmd(NULL
, get_param(params
, 0)->data
,
1713 gdb_gen_query_set_common_table
,
1714 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
1718 if (process_string_cmd(NULL
, get_param(params
, 0)->data
,
1719 gdb_gen_query_table
,
1720 ARRAY_SIZE(gdb_gen_query_table
))) {
1725 static void handle_gen_set(GArray
*params
, void *user_ctx
)
1731 if (!process_string_cmd(NULL
, get_param(params
, 0)->data
,
1732 gdb_gen_query_set_common_table
,
1733 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
1737 if (process_string_cmd(NULL
, get_param(params
, 0)->data
,
1739 ARRAY_SIZE(gdb_gen_set_table
))) {
1744 static void handle_target_halt(GArray
*params
, void *user_ctx
)
1746 if (gdbserver_state
.allow_stop_reply
) {
1747 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
1748 gdb_append_thread_id(gdbserver_state
.c_cpu
, gdbserver_state
.str_buf
);
1749 g_string_append_c(gdbserver_state
.str_buf
, ';');
1751 gdbserver_state
.allow_stop_reply
= false;
1754 * Remove all the breakpoints when this query is issued,
1755 * because gdb is doing an initial connect and the state
1756 * should be cleaned up.
1758 gdb_breakpoint_remove_all(gdbserver_state
.c_cpu
);
1761 static int gdb_handle_packet(const char *line_buf
)
1763 const GdbCmdParseEntry
*cmd_parser
= NULL
;
1765 trace_gdbstub_io_command(line_buf
);
1767 switch (line_buf
[0]) {
1769 gdb_put_packet("OK");
1773 static const GdbCmdParseEntry target_halted_cmd_desc
= {
1774 .handler
= handle_target_halt
,
1776 .cmd_startswith
= 1,
1777 .allow_stop_reply
= true,
1779 cmd_parser
= &target_halted_cmd_desc
;
1784 static const GdbCmdParseEntry continue_cmd_desc
= {
1785 .handler
= handle_continue
,
1787 .cmd_startswith
= 1,
1788 .allow_stop_reply
= true,
1791 cmd_parser
= &continue_cmd_desc
;
1796 static const GdbCmdParseEntry cont_with_sig_cmd_desc
= {
1797 .handler
= handle_cont_with_sig
,
1799 .cmd_startswith
= 1,
1800 .allow_stop_reply
= true,
1803 cmd_parser
= &cont_with_sig_cmd_desc
;
1808 static const GdbCmdParseEntry v_cmd_desc
= {
1809 .handler
= handle_v_commands
,
1811 .cmd_startswith
= 1,
1814 cmd_parser
= &v_cmd_desc
;
1818 /* Kill the target */
1819 error_report("QEMU: Terminated via GDBstub");
1824 static const GdbCmdParseEntry detach_cmd_desc
= {
1825 .handler
= handle_detach
,
1827 .cmd_startswith
= 1,
1830 cmd_parser
= &detach_cmd_desc
;
1835 static const GdbCmdParseEntry step_cmd_desc
= {
1836 .handler
= handle_step
,
1838 .cmd_startswith
= 1,
1839 .allow_stop_reply
= true,
1842 cmd_parser
= &step_cmd_desc
;
1847 static const GdbCmdParseEntry backward_cmd_desc
= {
1848 .handler
= handle_backward
,
1850 .cmd_startswith
= 1,
1851 .allow_stop_reply
= true,
1854 cmd_parser
= &backward_cmd_desc
;
1859 static const GdbCmdParseEntry file_io_cmd_desc
= {
1860 .handler
= gdb_handle_file_io
,
1862 .cmd_startswith
= 1,
1865 cmd_parser
= &file_io_cmd_desc
;
1870 static const GdbCmdParseEntry read_all_regs_cmd_desc
= {
1871 .handler
= handle_read_all_regs
,
1875 cmd_parser
= &read_all_regs_cmd_desc
;
1880 static const GdbCmdParseEntry write_all_regs_cmd_desc
= {
1881 .handler
= handle_write_all_regs
,
1883 .cmd_startswith
= 1,
1886 cmd_parser
= &write_all_regs_cmd_desc
;
1891 static const GdbCmdParseEntry read_mem_cmd_desc
= {
1892 .handler
= handle_read_mem
,
1894 .cmd_startswith
= 1,
1897 cmd_parser
= &read_mem_cmd_desc
;
1902 static const GdbCmdParseEntry write_mem_cmd_desc
= {
1903 .handler
= handle_write_mem
,
1905 .cmd_startswith
= 1,
1908 cmd_parser
= &write_mem_cmd_desc
;
1913 static const GdbCmdParseEntry get_reg_cmd_desc
= {
1914 .handler
= handle_get_reg
,
1916 .cmd_startswith
= 1,
1919 cmd_parser
= &get_reg_cmd_desc
;
1924 static const GdbCmdParseEntry set_reg_cmd_desc
= {
1925 .handler
= handle_set_reg
,
1927 .cmd_startswith
= 1,
1930 cmd_parser
= &set_reg_cmd_desc
;
1935 static const GdbCmdParseEntry insert_bp_cmd_desc
= {
1936 .handler
= handle_insert_bp
,
1938 .cmd_startswith
= 1,
1941 cmd_parser
= &insert_bp_cmd_desc
;
1946 static const GdbCmdParseEntry remove_bp_cmd_desc
= {
1947 .handler
= handle_remove_bp
,
1949 .cmd_startswith
= 1,
1952 cmd_parser
= &remove_bp_cmd_desc
;
1957 static const GdbCmdParseEntry set_thread_cmd_desc
= {
1958 .handler
= handle_set_thread
,
1960 .cmd_startswith
= 1,
1963 cmd_parser
= &set_thread_cmd_desc
;
1968 static const GdbCmdParseEntry thread_alive_cmd_desc
= {
1969 .handler
= handle_thread_alive
,
1971 .cmd_startswith
= 1,
1974 cmd_parser
= &thread_alive_cmd_desc
;
1979 static const GdbCmdParseEntry gen_query_cmd_desc
= {
1980 .handler
= handle_gen_query
,
1982 .cmd_startswith
= 1,
1985 cmd_parser
= &gen_query_cmd_desc
;
1990 static const GdbCmdParseEntry gen_set_cmd_desc
= {
1991 .handler
= handle_gen_set
,
1993 .cmd_startswith
= 1,
1996 cmd_parser
= &gen_set_cmd_desc
;
2000 /* put empty packet */
2006 run_cmd_parser(line_buf
, cmd_parser
);
2012 void gdb_set_stop_cpu(CPUState
*cpu
)
2014 GDBProcess
*p
= gdb_get_cpu_process(cpu
);
2018 * Having a stop CPU corresponding to a process that is not attached
2019 * confuses GDB. So we ignore the request.
2024 gdbserver_state
.c_cpu
= cpu
;
2025 gdbserver_state
.g_cpu
= cpu
;
2028 void gdb_read_byte(uint8_t ch
)
2032 gdbserver_state
.allow_stop_reply
= false;
2033 #ifndef CONFIG_USER_ONLY
2034 if (gdbserver_state
.last_packet
->len
) {
2035 /* Waiting for a response to the last packet. If we see the start
2036 of a new command then abandon the previous response. */
2038 trace_gdbstub_err_got_nack();
2039 gdb_put_buffer(gdbserver_state
.last_packet
->data
,
2040 gdbserver_state
.last_packet
->len
);
2041 } else if (ch
== '+') {
2042 trace_gdbstub_io_got_ack();
2044 trace_gdbstub_io_got_unexpected(ch
);
2047 if (ch
== '+' || ch
== '$') {
2048 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
2053 if (runstate_is_running()) {
2055 * When the CPU is running, we cannot do anything except stop
2056 * it when receiving a char. This is expected on a Ctrl-C in the
2057 * gdb client. Because we are in all-stop mode, gdb sends a
2058 * 0x03 byte which is not a usual packet, so we handle it specially
2059 * here, but it does expect a stop reply.
2062 trace_gdbstub_err_unexpected_runpkt(ch
);
2064 gdbserver_state
.allow_stop_reply
= true;
2066 vm_stop(RUN_STATE_PAUSED
);
2070 switch(gdbserver_state
.state
) {
2073 /* start of command packet */
2074 gdbserver_state
.line_buf_index
= 0;
2075 gdbserver_state
.line_sum
= 0;
2076 gdbserver_state
.state
= RS_GETLINE
;
2077 } else if (ch
== '+') {
2079 * do nothing, gdb may preemptively send out ACKs on
2080 * initial connection
2083 trace_gdbstub_err_garbage(ch
);
2088 /* start escape sequence */
2089 gdbserver_state
.state
= RS_GETLINE_ESC
;
2090 gdbserver_state
.line_sum
+= ch
;
2091 } else if (ch
== '*') {
2092 /* start run length encoding sequence */
2093 gdbserver_state
.state
= RS_GETLINE_RLE
;
2094 gdbserver_state
.line_sum
+= ch
;
2095 } else if (ch
== '#') {
2096 /* end of command, start of checksum*/
2097 gdbserver_state
.state
= RS_CHKSUM1
;
2098 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2099 trace_gdbstub_err_overrun();
2100 gdbserver_state
.state
= RS_IDLE
;
2102 /* unescaped command character */
2103 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
;
2104 gdbserver_state
.line_sum
+= ch
;
2107 case RS_GETLINE_ESC
:
2109 /* unexpected end of command in escape sequence */
2110 gdbserver_state
.state
= RS_CHKSUM1
;
2111 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2112 /* command buffer overrun */
2113 trace_gdbstub_err_overrun();
2114 gdbserver_state
.state
= RS_IDLE
;
2116 /* parse escaped character and leave escape state */
2117 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
^ 0x20;
2118 gdbserver_state
.line_sum
+= ch
;
2119 gdbserver_state
.state
= RS_GETLINE
;
2122 case RS_GETLINE_RLE
:
2124 * Run-length encoding is explained in "Debugging with GDB /
2125 * Appendix E GDB Remote Serial Protocol / Overview".
2127 if (ch
< ' ' || ch
== '#' || ch
== '$' || ch
> 126) {
2128 /* invalid RLE count encoding */
2129 trace_gdbstub_err_invalid_repeat(ch
);
2130 gdbserver_state
.state
= RS_GETLINE
;
2132 /* decode repeat length */
2133 int repeat
= ch
- ' ' + 3;
2134 if (gdbserver_state
.line_buf_index
+ repeat
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2135 /* that many repeats would overrun the command buffer */
2136 trace_gdbstub_err_overrun();
2137 gdbserver_state
.state
= RS_IDLE
;
2138 } else if (gdbserver_state
.line_buf_index
< 1) {
2139 /* got a repeat but we have nothing to repeat */
2140 trace_gdbstub_err_invalid_rle();
2141 gdbserver_state
.state
= RS_GETLINE
;
2143 /* repeat the last character */
2144 memset(gdbserver_state
.line_buf
+ gdbserver_state
.line_buf_index
,
2145 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
- 1], repeat
);
2146 gdbserver_state
.line_buf_index
+= repeat
;
2147 gdbserver_state
.line_sum
+= ch
;
2148 gdbserver_state
.state
= RS_GETLINE
;
2153 /* get high hex digit of checksum */
2154 if (!isxdigit(ch
)) {
2155 trace_gdbstub_err_checksum_invalid(ch
);
2156 gdbserver_state
.state
= RS_GETLINE
;
2159 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
] = '\0';
2160 gdbserver_state
.line_csum
= fromhex(ch
) << 4;
2161 gdbserver_state
.state
= RS_CHKSUM2
;
2164 /* get low hex digit of checksum */
2165 if (!isxdigit(ch
)) {
2166 trace_gdbstub_err_checksum_invalid(ch
);
2167 gdbserver_state
.state
= RS_GETLINE
;
2170 gdbserver_state
.line_csum
|= fromhex(ch
);
2172 if (gdbserver_state
.line_csum
!= (gdbserver_state
.line_sum
& 0xff)) {
2173 trace_gdbstub_err_checksum_incorrect(gdbserver_state
.line_sum
, gdbserver_state
.line_csum
);
2174 /* send NAK reply */
2176 gdb_put_buffer(&reply
, 1);
2177 gdbserver_state
.state
= RS_IDLE
;
2179 /* send ACK reply */
2181 gdb_put_buffer(&reply
, 1);
2182 gdbserver_state
.state
= gdb_handle_packet(gdbserver_state
.line_buf
);
2192 * Create the process that will contain all the "orphan" CPUs (that are not
2193 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2194 * be attachable and thus will be invisible to the user.
2196 void gdb_create_default_process(GDBState
*s
)
2198 GDBProcess
*process
;
2201 #ifdef CONFIG_USER_ONLY
2202 assert(gdbserver_state
.process_num
== 0);
2205 if (gdbserver_state
.process_num
) {
2206 pid
= s
->processes
[s
->process_num
- 1].pid
;
2210 /* We need an available PID slot for this process */
2211 assert(pid
< UINT32_MAX
);
2215 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2216 process
= &s
->processes
[s
->process_num
- 1];
2218 process
->attached
= false;
2219 process
->target_xml
[0] = '\0';