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
,
357 CPUState
*cpu
= gdb_get_first_cpu_in_process(process
);
358 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
362 * qXfer:features:read:ANNEX:OFFSET,LENGTH'
365 char *term
= strchr(p
, ':');
369 /* Is it the main target xml? */
370 if (strncmp(p
, "target.xml", len
) == 0) {
371 if (!process
->target_xml
) {
373 GString
*xml
= g_string_new("<?xml version=\"1.0\"?>");
376 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
379 if (cc
->gdb_arch_name
) {
380 g_autofree gchar
*arch
= cc
->gdb_arch_name(cpu
);
381 g_string_append_printf(xml
,
382 "<architecture>%s</architecture>",
385 g_string_append(xml
, "<xi:include href=\"");
386 g_string_append(xml
, cc
->gdb_core_xml_file
);
387 g_string_append(xml
, "\"/>");
388 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
389 g_string_append(xml
, "<xi:include href=\"");
390 g_string_append(xml
, r
->xml
);
391 g_string_append(xml
, "\"/>");
393 g_string_append(xml
, "</target>");
395 process
->target_xml
= g_string_free(xml
, false);
396 return process
->target_xml
;
399 /* Is it dynamically generated by the target? */
400 if (cc
->gdb_get_dynamic_xml
) {
401 g_autofree
char *xmlname
= g_strndup(p
, len
);
402 const char *xml
= cc
->gdb_get_dynamic_xml(cpu
, xmlname
);
407 /* Is it one of the encoded gdb-xml/ files? */
408 for (int i
= 0; xml_builtin
[i
][0]; i
++) {
409 const char *name
= xml_builtin
[i
][0];
410 if ((strncmp(name
, p
, len
) == 0) &&
411 strlen(name
) == len
) {
412 return xml_builtin
[i
][1];
420 static int gdb_read_register(CPUState
*cpu
, GByteArray
*buf
, int reg
)
422 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
423 CPUArchState
*env
= cpu
->env_ptr
;
426 if (reg
< cc
->gdb_num_core_regs
) {
427 return cc
->gdb_read_register(cpu
, buf
, reg
);
430 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
431 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
432 return r
->get_reg(env
, buf
, reg
- r
->base_reg
);
438 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
440 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
441 CPUArchState
*env
= cpu
->env_ptr
;
444 if (reg
< cc
->gdb_num_core_regs
) {
445 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
448 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
449 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
450 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
456 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
457 specifies the first register number and these registers are included in
458 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
459 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
462 void gdb_register_coprocessor(CPUState
*cpu
,
463 gdb_get_reg_cb get_reg
, gdb_set_reg_cb set_reg
,
464 int num_regs
, const char *xml
, int g_pos
)
467 GDBRegisterState
**p
;
471 /* Check for duplicates. */
472 if (strcmp((*p
)->xml
, xml
) == 0)
477 s
= g_new0(GDBRegisterState
, 1);
478 s
->base_reg
= cpu
->gdb_num_regs
;
479 s
->num_regs
= num_regs
;
480 s
->get_reg
= get_reg
;
481 s
->set_reg
= set_reg
;
484 /* Add to end of list. */
485 cpu
->gdb_num_regs
+= num_regs
;
488 if (g_pos
!= s
->base_reg
) {
489 error_report("Error: Bad gdb register numbering for '%s', "
490 "expected %d got %d", xml
, g_pos
, s
->base_reg
);
492 cpu
->gdb_num_g_regs
= cpu
->gdb_num_regs
;
497 static void gdb_process_breakpoint_remove_all(GDBProcess
*p
)
499 CPUState
*cpu
= gdb_get_first_cpu_in_process(p
);
502 gdb_breakpoint_remove_all(cpu
);
503 cpu
= gdb_next_cpu_in_process(cpu
);
508 static void gdb_set_cpu_pc(vaddr pc
)
510 CPUState
*cpu
= gdbserver_state
.c_cpu
;
512 cpu_synchronize_state(cpu
);
516 void gdb_append_thread_id(CPUState
*cpu
, GString
*buf
)
518 if (gdbserver_state
.multiprocess
) {
519 g_string_append_printf(buf
, "p%02x.%02x",
520 gdb_get_cpu_pid(cpu
), gdb_get_cpu_index(cpu
));
522 g_string_append_printf(buf
, "%02x", gdb_get_cpu_index(cpu
));
526 static GDBThreadIdKind
read_thread_id(const char *buf
, const char **end_buf
,
527 uint32_t *pid
, uint32_t *tid
)
534 ret
= qemu_strtoul(buf
, &buf
, 16, &p
);
537 return GDB_READ_THREAD_ERR
;
546 ret
= qemu_strtoul(buf
, &buf
, 16, &t
);
549 return GDB_READ_THREAD_ERR
;
555 return GDB_ALL_PROCESSES
;
563 return GDB_ALL_THREADS
;
570 return GDB_ONE_THREAD
;
574 * gdb_handle_vcont - Parses and handles a vCont packet.
575 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
576 * a format error, 0 on success.
578 static int gdb_handle_vcont(const char *p
)
586 GDBThreadIdKind kind
;
587 unsigned int max_cpus
= gdb_get_max_cpus();
588 /* uninitialised CPUs stay 0 */
589 g_autofree
char *newstates
= g_new0(char, max_cpus
);
591 /* mark valid CPUs with 1 */
593 newstates
[cpu
->cpu_index
] = 1;
597 * res keeps track of what error we are returning, with -ENOTSUP meaning
598 * that the command is unknown or unsupported, thus returning an empty
599 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
600 * or incorrect parameters passed.
605 * target_count and last_target keep track of how many CPUs we are going to
606 * step or resume, and a pointer to the state structure of one of them,
609 int target_count
= 0;
610 CPUState
*last_target
= NULL
;
618 if (cur_action
== 'C' || cur_action
== 'S') {
619 cur_action
= qemu_tolower(cur_action
);
620 res
= qemu_strtoul(p
, &p
, 16, &tmp
);
624 signal
= gdb_signal_to_target(tmp
);
625 } else if (cur_action
!= 'c' && cur_action
!= 's') {
626 /* unknown/invalid/unsupported command */
630 if (*p
== '\0' || *p
== ';') {
632 * No thread specifier, action is on "all threads". The
633 * specification is unclear regarding the process to act on. We
634 * choose all processes.
636 kind
= GDB_ALL_PROCESSES
;
637 } else if (*p
++ == ':') {
638 kind
= read_thread_id(p
, &p
, &pid
, &tid
);
644 case GDB_READ_THREAD_ERR
:
647 case GDB_ALL_PROCESSES
:
648 cpu
= gdb_first_attached_cpu();
650 if (newstates
[cpu
->cpu_index
] == 1) {
651 newstates
[cpu
->cpu_index
] = cur_action
;
657 cpu
= gdb_next_attached_cpu(cpu
);
661 case GDB_ALL_THREADS
:
662 process
= gdb_get_process(pid
);
664 if (!process
->attached
) {
668 cpu
= gdb_get_first_cpu_in_process(process
);
670 if (newstates
[cpu
->cpu_index
] == 1) {
671 newstates
[cpu
->cpu_index
] = cur_action
;
677 cpu
= gdb_next_cpu_in_process(cpu
);
682 cpu
= gdb_get_cpu(pid
, tid
);
684 /* invalid CPU/thread specified */
689 /* only use if no previous match occourred */
690 if (newstates
[cpu
->cpu_index
] == 1) {
691 newstates
[cpu
->cpu_index
] = cur_action
;
701 * if we're about to resume a specific set of CPUs/threads, make it so that
702 * in case execution gets interrupted, we can send GDB a stop reply with a
703 * correct value. it doesn't really matter which CPU we tell GDB the signal
704 * happened in (VM pauses stop all of them anyway), so long as it is one of
705 * the ones we resumed/single stepped here.
707 if (target_count
> 0) {
708 gdbserver_state
.c_cpu
= last_target
;
711 gdbserver_state
.signal
= signal
;
712 gdb_continue_partial(newstates
);
716 static const char *cmd_next_param(const char *param
, const char delimiter
)
718 static const char all_delimiters
[] = ",;:=";
719 char curr_delimiters
[2] = {0};
720 const char *delimiters
;
722 if (delimiter
== '?') {
723 delimiters
= all_delimiters
;
724 } else if (delimiter
== '0') {
725 return strchr(param
, '\0');
726 } else if (delimiter
== '.' && *param
) {
729 curr_delimiters
[0] = delimiter
;
730 delimiters
= curr_delimiters
;
733 param
+= strcspn(param
, delimiters
);
740 static int cmd_parse_params(const char *data
, const char *schema
,
743 const char *curr_schema
, *curr_data
;
746 g_assert(params
->len
== 0);
748 curr_schema
= schema
;
750 while (curr_schema
[0] && curr_schema
[1] && *curr_data
) {
751 GdbCmdVariant this_param
;
753 switch (curr_schema
[0]) {
755 if (qemu_strtoul(curr_data
, &curr_data
, 16,
756 &this_param
.val_ul
)) {
759 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
760 g_array_append_val(params
, this_param
);
763 if (qemu_strtou64(curr_data
, &curr_data
, 16,
764 (uint64_t *)&this_param
.val_ull
)) {
767 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
768 g_array_append_val(params
, this_param
);
771 this_param
.data
= curr_data
;
772 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
773 g_array_append_val(params
, this_param
);
776 this_param
.opcode
= *(uint8_t *)curr_data
;
777 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
778 g_array_append_val(params
, this_param
);
781 this_param
.thread_id
.kind
=
782 read_thread_id(curr_data
, &curr_data
,
783 &this_param
.thread_id
.pid
,
784 &this_param
.thread_id
.tid
);
785 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
786 g_array_append_val(params
, this_param
);
789 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
800 typedef void (*GdbCmdHandler
)(GArray
*params
, void *user_ctx
);
803 * cmd_startswith -> cmd is compared using startswith
805 * allow_stop_reply -> true iff the gdbstub can respond to this command with a
806 * "stop reply" packet. The list of commands that accept such response is
807 * defined at the GDB Remote Serial Protocol documentation. see:
808 * https://sourceware.org/gdb/onlinedocs/gdb/Stop-Reply-Packets.html#Stop-Reply-Packets.
810 * schema definitions:
811 * Each schema parameter entry consists of 2 chars,
812 * the first char represents the parameter type handling
813 * the second char represents the delimiter for the next parameter
815 * Currently supported schema types:
816 * 'l' -> unsigned long (stored in .val_ul)
817 * 'L' -> unsigned long long (stored in .val_ull)
818 * 's' -> string (stored in .data)
819 * 'o' -> single char (stored in .opcode)
820 * 't' -> thread id (stored in .thread_id)
821 * '?' -> skip according to delimiter
823 * Currently supported delimiters:
824 * '?' -> Stop at any delimiter (",;:=\0")
825 * '0' -> Stop at "\0"
826 * '.' -> Skip 1 char unless reached "\0"
827 * Any other value is treated as the delimiter value itself
829 typedef struct GdbCmdParseEntry
{
830 GdbCmdHandler handler
;
834 bool allow_stop_reply
;
837 static inline int startswith(const char *string
, const char *pattern
)
839 return !strncmp(string
, pattern
, strlen(pattern
));
842 static int process_string_cmd(const char *data
,
843 const GdbCmdParseEntry
*cmds
, int num_cmds
)
846 g_autoptr(GArray
) params
= g_array_new(false, true, sizeof(GdbCmdVariant
));
852 for (i
= 0; i
< num_cmds
; i
++) {
853 const GdbCmdParseEntry
*cmd
= &cmds
[i
];
854 g_assert(cmd
->handler
&& cmd
->cmd
);
856 if ((cmd
->cmd_startswith
&& !startswith(data
, cmd
->cmd
)) ||
857 (!cmd
->cmd_startswith
&& strcmp(cmd
->cmd
, data
))) {
862 if (cmd_parse_params(&data
[strlen(cmd
->cmd
)],
863 cmd
->schema
, params
)) {
868 gdbserver_state
.allow_stop_reply
= cmd
->allow_stop_reply
;
869 cmd
->handler(params
, NULL
);
876 static void run_cmd_parser(const char *data
, const GdbCmdParseEntry
*cmd
)
882 g_string_set_size(gdbserver_state
.str_buf
, 0);
883 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
885 /* In case there was an error during the command parsing we must
886 * send a NULL packet to indicate the command is not supported */
887 if (process_string_cmd(data
, cmd
, 1)) {
892 static void handle_detach(GArray
*params
, void *user_ctx
)
897 if (gdbserver_state
.multiprocess
) {
899 gdb_put_packet("E22");
903 pid
= get_param(params
, 0)->val_ul
;
906 process
= gdb_get_process(pid
);
907 gdb_process_breakpoint_remove_all(process
);
908 process
->attached
= false;
910 if (pid
== gdb_get_cpu_pid(gdbserver_state
.c_cpu
)) {
911 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
914 if (pid
== gdb_get_cpu_pid(gdbserver_state
.g_cpu
)) {
915 gdbserver_state
.g_cpu
= gdb_first_attached_cpu();
918 if (!gdbserver_state
.c_cpu
) {
919 /* No more process attached */
920 gdb_disable_syscalls();
923 gdb_put_packet("OK");
926 static void handle_thread_alive(GArray
*params
, void *user_ctx
)
931 gdb_put_packet("E22");
935 if (get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
936 gdb_put_packet("E22");
940 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
941 get_param(params
, 0)->thread_id
.tid
);
943 gdb_put_packet("E22");
947 gdb_put_packet("OK");
950 static void handle_continue(GArray
*params
, void *user_ctx
)
953 gdb_set_cpu_pc(get_param(params
, 0)->val_ull
);
956 gdbserver_state
.signal
= 0;
960 static void handle_cont_with_sig(GArray
*params
, void *user_ctx
)
962 unsigned long signal
= 0;
965 * Note: C sig;[addr] is currently unsupported and we simply
966 * omit the addr parameter
969 signal
= get_param(params
, 0)->val_ul
;
972 gdbserver_state
.signal
= gdb_signal_to_target(signal
);
973 if (gdbserver_state
.signal
== -1) {
974 gdbserver_state
.signal
= 0;
979 static void handle_set_thread(GArray
*params
, void *user_ctx
)
983 if (params
->len
!= 2) {
984 gdb_put_packet("E22");
988 if (get_param(params
, 1)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
989 gdb_put_packet("E22");
993 if (get_param(params
, 1)->thread_id
.kind
!= GDB_ONE_THREAD
) {
994 gdb_put_packet("OK");
998 cpu
= gdb_get_cpu(get_param(params
, 1)->thread_id
.pid
,
999 get_param(params
, 1)->thread_id
.tid
);
1001 gdb_put_packet("E22");
1006 * Note: This command is deprecated and modern gdb's will be using the
1007 * vCont command instead.
1009 switch (get_param(params
, 0)->opcode
) {
1011 gdbserver_state
.c_cpu
= cpu
;
1012 gdb_put_packet("OK");
1015 gdbserver_state
.g_cpu
= cpu
;
1016 gdb_put_packet("OK");
1019 gdb_put_packet("E22");
1024 static void handle_insert_bp(GArray
*params
, void *user_ctx
)
1028 if (params
->len
!= 3) {
1029 gdb_put_packet("E22");
1033 res
= gdb_breakpoint_insert(gdbserver_state
.c_cpu
,
1034 get_param(params
, 0)->val_ul
,
1035 get_param(params
, 1)->val_ull
,
1036 get_param(params
, 2)->val_ull
);
1038 gdb_put_packet("OK");
1040 } else if (res
== -ENOSYS
) {
1045 gdb_put_packet("E22");
1048 static void handle_remove_bp(GArray
*params
, void *user_ctx
)
1052 if (params
->len
!= 3) {
1053 gdb_put_packet("E22");
1057 res
= gdb_breakpoint_remove(gdbserver_state
.c_cpu
,
1058 get_param(params
, 0)->val_ul
,
1059 get_param(params
, 1)->val_ull
,
1060 get_param(params
, 2)->val_ull
);
1062 gdb_put_packet("OK");
1064 } else if (res
== -ENOSYS
) {
1069 gdb_put_packet("E22");
1073 * handle_set/get_reg
1075 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1076 * This works, but can be very slow. Anything new enough to understand
1077 * XML also knows how to use this properly. However to use this we
1078 * need to define a local XML file as well as be talking to a
1079 * reasonably modern gdb. Responding with an empty packet will cause
1080 * the remote gdb to fallback to older methods.
1083 static void handle_set_reg(GArray
*params
, void *user_ctx
)
1092 if (params
->len
!= 2) {
1093 gdb_put_packet("E22");
1097 reg_size
= strlen(get_param(params
, 1)->data
) / 2;
1098 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 1)->data
, reg_size
);
1099 gdb_write_register(gdbserver_state
.g_cpu
, gdbserver_state
.mem_buf
->data
,
1100 get_param(params
, 0)->val_ull
);
1101 gdb_put_packet("OK");
1104 static void handle_get_reg(GArray
*params
, void *user_ctx
)
1114 gdb_put_packet("E14");
1118 reg_size
= gdb_read_register(gdbserver_state
.g_cpu
,
1119 gdbserver_state
.mem_buf
,
1120 get_param(params
, 0)->val_ull
);
1122 gdb_put_packet("E14");
1125 g_byte_array_set_size(gdbserver_state
.mem_buf
, reg_size
);
1128 gdb_memtohex(gdbserver_state
.str_buf
,
1129 gdbserver_state
.mem_buf
->data
, reg_size
);
1133 static void handle_write_mem(GArray
*params
, void *user_ctx
)
1135 if (params
->len
!= 3) {
1136 gdb_put_packet("E22");
1140 /* gdb_hextomem() reads 2*len bytes */
1141 if (get_param(params
, 1)->val_ull
>
1142 strlen(get_param(params
, 2)->data
) / 2) {
1143 gdb_put_packet("E22");
1147 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 2)->data
,
1148 get_param(params
, 1)->val_ull
);
1149 if (gdb_target_memory_rw_debug(gdbserver_state
.g_cpu
,
1150 get_param(params
, 0)->val_ull
,
1151 gdbserver_state
.mem_buf
->data
,
1152 gdbserver_state
.mem_buf
->len
, true)) {
1153 gdb_put_packet("E14");
1157 gdb_put_packet("OK");
1160 static void handle_read_mem(GArray
*params
, void *user_ctx
)
1162 if (params
->len
!= 2) {
1163 gdb_put_packet("E22");
1167 /* gdb_memtohex() doubles the required space */
1168 if (get_param(params
, 1)->val_ull
> MAX_PACKET_LENGTH
/ 2) {
1169 gdb_put_packet("E22");
1173 g_byte_array_set_size(gdbserver_state
.mem_buf
,
1174 get_param(params
, 1)->val_ull
);
1176 if (gdb_target_memory_rw_debug(gdbserver_state
.g_cpu
,
1177 get_param(params
, 0)->val_ull
,
1178 gdbserver_state
.mem_buf
->data
,
1179 gdbserver_state
.mem_buf
->len
, false)) {
1180 gdb_put_packet("E14");
1184 gdb_memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
,
1185 gdbserver_state
.mem_buf
->len
);
1189 static void handle_write_all_regs(GArray
*params
, void *user_ctx
)
1200 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1201 len
= strlen(get_param(params
, 0)->data
) / 2;
1202 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 0)->data
, len
);
1203 registers
= gdbserver_state
.mem_buf
->data
;
1205 reg_id
< gdbserver_state
.g_cpu
->gdb_num_g_regs
&& len
> 0;
1207 reg_size
= gdb_write_register(gdbserver_state
.g_cpu
, registers
, reg_id
);
1209 registers
+= reg_size
;
1211 gdb_put_packet("OK");
1214 static void handle_read_all_regs(GArray
*params
, void *user_ctx
)
1219 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1220 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
1222 for (reg_id
= 0; reg_id
< gdbserver_state
.g_cpu
->gdb_num_g_regs
; reg_id
++) {
1223 len
+= gdb_read_register(gdbserver_state
.g_cpu
,
1224 gdbserver_state
.mem_buf
,
1227 g_assert(len
== gdbserver_state
.mem_buf
->len
);
1229 gdb_memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
, len
);
1234 static void handle_step(GArray
*params
, void *user_ctx
)
1237 gdb_set_cpu_pc(get_param(params
, 0)->val_ull
);
1240 cpu_single_step(gdbserver_state
.c_cpu
, gdbserver_state
.sstep_flags
);
1244 static void handle_backward(GArray
*params
, void *user_ctx
)
1246 if (!gdb_can_reverse()) {
1247 gdb_put_packet("E22");
1249 if (params
->len
== 1) {
1250 switch (get_param(params
, 0)->opcode
) {
1252 if (replay_reverse_step()) {
1255 gdb_put_packet("E14");
1259 if (replay_reverse_continue()) {
1262 gdb_put_packet("E14");
1268 /* Default invalid command */
1272 static void handle_v_cont_query(GArray
*params
, void *user_ctx
)
1274 gdb_put_packet("vCont;c;C;s;S");
1277 static void handle_v_cont(GArray
*params
, void *user_ctx
)
1285 res
= gdb_handle_vcont(get_param(params
, 0)->data
);
1286 if ((res
== -EINVAL
) || (res
== -ERANGE
)) {
1287 gdb_put_packet("E22");
1293 static void handle_v_attach(GArray
*params
, void *user_ctx
)
1295 GDBProcess
*process
;
1298 g_string_assign(gdbserver_state
.str_buf
, "E22");
1303 process
= gdb_get_process(get_param(params
, 0)->val_ul
);
1308 cpu
= gdb_get_first_cpu_in_process(process
);
1313 process
->attached
= true;
1314 gdbserver_state
.g_cpu
= cpu
;
1315 gdbserver_state
.c_cpu
= cpu
;
1317 if (gdbserver_state
.allow_stop_reply
) {
1318 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
1319 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
1320 g_string_append_c(gdbserver_state
.str_buf
, ';');
1321 gdbserver_state
.allow_stop_reply
= false;
1327 static void handle_v_kill(GArray
*params
, void *user_ctx
)
1329 /* Kill the target */
1330 gdb_put_packet("OK");
1331 error_report("QEMU: Terminated via GDBstub");
1336 static const GdbCmdParseEntry gdb_v_commands_table
[] = {
1337 /* Order is important if has same prefix */
1339 .handler
= handle_v_cont_query
,
1344 .handler
= handle_v_cont
,
1346 .cmd_startswith
= 1,
1347 .allow_stop_reply
= true,
1351 .handler
= handle_v_attach
,
1353 .cmd_startswith
= 1,
1354 .allow_stop_reply
= true,
1358 .handler
= handle_v_kill
,
1362 #ifdef CONFIG_USER_ONLY
1364 * Host I/O Packets. See [1] for details.
1365 * [1] https://sourceware.org/gdb/onlinedocs/gdb/Host-I_002fO-Packets.html
1368 .handler
= gdb_handle_v_file_open
,
1369 .cmd
= "File:open:",
1370 .cmd_startswith
= 1,
1374 .handler
= gdb_handle_v_file_close
,
1375 .cmd
= "File:close:",
1376 .cmd_startswith
= 1,
1380 .handler
= gdb_handle_v_file_pread
,
1381 .cmd
= "File:pread:",
1382 .cmd_startswith
= 1,
1386 .handler
= gdb_handle_v_file_readlink
,
1387 .cmd
= "File:readlink:",
1388 .cmd_startswith
= 1,
1394 static void handle_v_commands(GArray
*params
, void *user_ctx
)
1400 if (process_string_cmd(get_param(params
, 0)->data
,
1401 gdb_v_commands_table
,
1402 ARRAY_SIZE(gdb_v_commands_table
))) {
1407 static void handle_query_qemu_sstepbits(GArray
*params
, void *user_ctx
)
1409 g_string_printf(gdbserver_state
.str_buf
, "ENABLE=%x", SSTEP_ENABLE
);
1411 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOIRQ
) {
1412 g_string_append_printf(gdbserver_state
.str_buf
, ",NOIRQ=%x",
1416 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOTIMER
) {
1417 g_string_append_printf(gdbserver_state
.str_buf
, ",NOTIMER=%x",
1424 static void handle_set_qemu_sstep(GArray
*params
, void *user_ctx
)
1426 int new_sstep_flags
;
1432 new_sstep_flags
= get_param(params
, 0)->val_ul
;
1434 if (new_sstep_flags
& ~gdbserver_state
.supported_sstep_flags
) {
1435 gdb_put_packet("E22");
1439 gdbserver_state
.sstep_flags
= new_sstep_flags
;
1440 gdb_put_packet("OK");
1443 static void handle_query_qemu_sstep(GArray
*params
, void *user_ctx
)
1445 g_string_printf(gdbserver_state
.str_buf
, "0x%x",
1446 gdbserver_state
.sstep_flags
);
1450 static void handle_query_curr_tid(GArray
*params
, void *user_ctx
)
1453 GDBProcess
*process
;
1456 * "Current thread" remains vague in the spec, so always return
1457 * the first thread of the current process (gdb returns the
1460 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
1461 cpu
= gdb_get_first_cpu_in_process(process
);
1462 g_string_assign(gdbserver_state
.str_buf
, "QC");
1463 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
1467 static void handle_query_threads(GArray
*params
, void *user_ctx
)
1469 if (!gdbserver_state
.query_cpu
) {
1470 gdb_put_packet("l");
1474 g_string_assign(gdbserver_state
.str_buf
, "m");
1475 gdb_append_thread_id(gdbserver_state
.query_cpu
, gdbserver_state
.str_buf
);
1477 gdbserver_state
.query_cpu
= gdb_next_attached_cpu(gdbserver_state
.query_cpu
);
1480 static void handle_query_first_threads(GArray
*params
, void *user_ctx
)
1482 gdbserver_state
.query_cpu
= gdb_first_attached_cpu();
1483 handle_query_threads(params
, user_ctx
);
1486 static void handle_query_thread_extra(GArray
*params
, void *user_ctx
)
1488 g_autoptr(GString
) rs
= g_string_new(NULL
);
1492 get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1493 gdb_put_packet("E22");
1497 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
1498 get_param(params
, 0)->thread_id
.tid
);
1503 cpu_synchronize_state(cpu
);
1505 if (gdbserver_state
.multiprocess
&& (gdbserver_state
.process_num
> 1)) {
1506 /* Print the CPU model and name in multiprocess mode */
1507 ObjectClass
*oc
= object_get_class(OBJECT(cpu
));
1508 const char *cpu_model
= object_class_get_name(oc
);
1509 const char *cpu_name
=
1510 object_get_canonical_path_component(OBJECT(cpu
));
1511 g_string_printf(rs
, "%s %s [%s]", cpu_model
, cpu_name
,
1512 cpu
->halted
? "halted " : "running");
1514 g_string_printf(rs
, "CPU#%d [%s]", cpu
->cpu_index
,
1515 cpu
->halted
? "halted " : "running");
1517 trace_gdbstub_op_extra_info(rs
->str
);
1518 gdb_memtohex(gdbserver_state
.str_buf
, (uint8_t *)rs
->str
, rs
->len
);
1522 static void handle_query_supported(GArray
*params
, void *user_ctx
)
1526 g_string_printf(gdbserver_state
.str_buf
, "PacketSize=%x", MAX_PACKET_LENGTH
);
1527 cc
= CPU_GET_CLASS(first_cpu
);
1528 if (cc
->gdb_core_xml_file
) {
1529 g_string_append(gdbserver_state
.str_buf
, ";qXfer:features:read+");
1532 if (gdb_can_reverse()) {
1533 g_string_append(gdbserver_state
.str_buf
,
1534 ";ReverseStep+;ReverseContinue+");
1537 #if defined(CONFIG_USER_ONLY)
1538 #if defined(CONFIG_LINUX)
1539 if (gdbserver_state
.c_cpu
->opaque
) {
1540 g_string_append(gdbserver_state
.str_buf
, ";qXfer:auxv:read+");
1543 g_string_append(gdbserver_state
.str_buf
, ";qXfer:exec-file:read+");
1547 strstr(get_param(params
, 0)->data
, "multiprocess+")) {
1548 gdbserver_state
.multiprocess
= true;
1551 g_string_append(gdbserver_state
.str_buf
, ";vContSupported+;multiprocess+");
1555 static void handle_query_xfer_features(GArray
*params
, void *user_ctx
)
1557 GDBProcess
*process
;
1559 unsigned long len
, total_len
, addr
;
1563 if (params
->len
< 3) {
1564 gdb_put_packet("E22");
1568 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
1569 cc
= CPU_GET_CLASS(gdbserver_state
.g_cpu
);
1570 if (!cc
->gdb_core_xml_file
) {
1576 p
= get_param(params
, 0)->data
;
1577 xml
= get_feature_xml(p
, &p
, process
);
1579 gdb_put_packet("E00");
1583 addr
= get_param(params
, 1)->val_ul
;
1584 len
= get_param(params
, 2)->val_ul
;
1585 total_len
= strlen(xml
);
1586 if (addr
> total_len
) {
1587 gdb_put_packet("E00");
1591 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
1592 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1595 if (len
< total_len
- addr
) {
1596 g_string_assign(gdbserver_state
.str_buf
, "m");
1597 gdb_memtox(gdbserver_state
.str_buf
, xml
+ addr
, len
);
1599 g_string_assign(gdbserver_state
.str_buf
, "l");
1600 gdb_memtox(gdbserver_state
.str_buf
, xml
+ addr
, total_len
- addr
);
1603 gdb_put_packet_binary(gdbserver_state
.str_buf
->str
,
1604 gdbserver_state
.str_buf
->len
, true);
1607 static void handle_query_qemu_supported(GArray
*params
, void *user_ctx
)
1609 g_string_printf(gdbserver_state
.str_buf
, "sstepbits;sstep");
1610 #ifndef CONFIG_USER_ONLY
1611 g_string_append(gdbserver_state
.str_buf
, ";PhyMemMode");
1616 static const GdbCmdParseEntry gdb_gen_query_set_common_table
[] = {
1617 /* Order is important if has same prefix */
1619 .handler
= handle_query_qemu_sstepbits
,
1620 .cmd
= "qemu.sstepbits",
1623 .handler
= handle_query_qemu_sstep
,
1624 .cmd
= "qemu.sstep",
1627 .handler
= handle_set_qemu_sstep
,
1628 .cmd
= "qemu.sstep=",
1629 .cmd_startswith
= 1,
1634 static const GdbCmdParseEntry gdb_gen_query_table
[] = {
1636 .handler
= handle_query_curr_tid
,
1640 .handler
= handle_query_threads
,
1641 .cmd
= "sThreadInfo",
1644 .handler
= handle_query_first_threads
,
1645 .cmd
= "fThreadInfo",
1648 .handler
= handle_query_thread_extra
,
1649 .cmd
= "ThreadExtraInfo,",
1650 .cmd_startswith
= 1,
1653 #ifdef CONFIG_USER_ONLY
1655 .handler
= gdb_handle_query_offsets
,
1660 .handler
= gdb_handle_query_rcmd
,
1662 .cmd_startswith
= 1,
1667 .handler
= handle_query_supported
,
1668 .cmd
= "Supported:",
1669 .cmd_startswith
= 1,
1673 .handler
= handle_query_supported
,
1678 .handler
= handle_query_xfer_features
,
1679 .cmd
= "Xfer:features:read:",
1680 .cmd_startswith
= 1,
1683 #if defined(CONFIG_USER_ONLY)
1684 #if defined(CONFIG_LINUX)
1686 .handler
= gdb_handle_query_xfer_auxv
,
1687 .cmd
= "Xfer:auxv:read::",
1688 .cmd_startswith
= 1,
1693 .handler
= gdb_handle_query_xfer_exec_file
,
1694 .cmd
= "Xfer:exec-file:read:",
1695 .cmd_startswith
= 1,
1700 .handler
= gdb_handle_query_attached
,
1705 .handler
= gdb_handle_query_attached
,
1709 .handler
= handle_query_qemu_supported
,
1710 .cmd
= "qemu.Supported",
1712 #ifndef CONFIG_USER_ONLY
1714 .handler
= gdb_handle_query_qemu_phy_mem_mode
,
1715 .cmd
= "qemu.PhyMemMode",
1720 static const GdbCmdParseEntry gdb_gen_set_table
[] = {
1721 /* Order is important if has same prefix */
1723 .handler
= handle_set_qemu_sstep
,
1724 .cmd
= "qemu.sstep:",
1725 .cmd_startswith
= 1,
1728 #ifndef CONFIG_USER_ONLY
1730 .handler
= gdb_handle_set_qemu_phy_mem_mode
,
1731 .cmd
= "qemu.PhyMemMode:",
1732 .cmd_startswith
= 1,
1738 static void handle_gen_query(GArray
*params
, void *user_ctx
)
1744 if (!process_string_cmd(get_param(params
, 0)->data
,
1745 gdb_gen_query_set_common_table
,
1746 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
1750 if (process_string_cmd(get_param(params
, 0)->data
,
1751 gdb_gen_query_table
,
1752 ARRAY_SIZE(gdb_gen_query_table
))) {
1757 static void handle_gen_set(GArray
*params
, void *user_ctx
)
1763 if (!process_string_cmd(get_param(params
, 0)->data
,
1764 gdb_gen_query_set_common_table
,
1765 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
1769 if (process_string_cmd(get_param(params
, 0)->data
,
1771 ARRAY_SIZE(gdb_gen_set_table
))) {
1776 static void handle_target_halt(GArray
*params
, void *user_ctx
)
1778 if (gdbserver_state
.allow_stop_reply
) {
1779 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
1780 gdb_append_thread_id(gdbserver_state
.c_cpu
, gdbserver_state
.str_buf
);
1781 g_string_append_c(gdbserver_state
.str_buf
, ';');
1783 gdbserver_state
.allow_stop_reply
= false;
1786 * Remove all the breakpoints when this query is issued,
1787 * because gdb is doing an initial connect and the state
1788 * should be cleaned up.
1790 gdb_breakpoint_remove_all(gdbserver_state
.c_cpu
);
1793 static int gdb_handle_packet(const char *line_buf
)
1795 const GdbCmdParseEntry
*cmd_parser
= NULL
;
1797 trace_gdbstub_io_command(line_buf
);
1799 switch (line_buf
[0]) {
1801 gdb_put_packet("OK");
1805 static const GdbCmdParseEntry target_halted_cmd_desc
= {
1806 .handler
= handle_target_halt
,
1808 .cmd_startswith
= 1,
1809 .allow_stop_reply
= true,
1811 cmd_parser
= &target_halted_cmd_desc
;
1816 static const GdbCmdParseEntry continue_cmd_desc
= {
1817 .handler
= handle_continue
,
1819 .cmd_startswith
= 1,
1820 .allow_stop_reply
= true,
1823 cmd_parser
= &continue_cmd_desc
;
1828 static const GdbCmdParseEntry cont_with_sig_cmd_desc
= {
1829 .handler
= handle_cont_with_sig
,
1831 .cmd_startswith
= 1,
1832 .allow_stop_reply
= true,
1835 cmd_parser
= &cont_with_sig_cmd_desc
;
1840 static const GdbCmdParseEntry v_cmd_desc
= {
1841 .handler
= handle_v_commands
,
1843 .cmd_startswith
= 1,
1846 cmd_parser
= &v_cmd_desc
;
1850 /* Kill the target */
1851 error_report("QEMU: Terminated via GDBstub");
1856 static const GdbCmdParseEntry detach_cmd_desc
= {
1857 .handler
= handle_detach
,
1859 .cmd_startswith
= 1,
1862 cmd_parser
= &detach_cmd_desc
;
1867 static const GdbCmdParseEntry step_cmd_desc
= {
1868 .handler
= handle_step
,
1870 .cmd_startswith
= 1,
1871 .allow_stop_reply
= true,
1874 cmd_parser
= &step_cmd_desc
;
1879 static const GdbCmdParseEntry backward_cmd_desc
= {
1880 .handler
= handle_backward
,
1882 .cmd_startswith
= 1,
1883 .allow_stop_reply
= true,
1886 cmd_parser
= &backward_cmd_desc
;
1891 static const GdbCmdParseEntry file_io_cmd_desc
= {
1892 .handler
= gdb_handle_file_io
,
1894 .cmd_startswith
= 1,
1897 cmd_parser
= &file_io_cmd_desc
;
1902 static const GdbCmdParseEntry read_all_regs_cmd_desc
= {
1903 .handler
= handle_read_all_regs
,
1907 cmd_parser
= &read_all_regs_cmd_desc
;
1912 static const GdbCmdParseEntry write_all_regs_cmd_desc
= {
1913 .handler
= handle_write_all_regs
,
1915 .cmd_startswith
= 1,
1918 cmd_parser
= &write_all_regs_cmd_desc
;
1923 static const GdbCmdParseEntry read_mem_cmd_desc
= {
1924 .handler
= handle_read_mem
,
1926 .cmd_startswith
= 1,
1929 cmd_parser
= &read_mem_cmd_desc
;
1934 static const GdbCmdParseEntry write_mem_cmd_desc
= {
1935 .handler
= handle_write_mem
,
1937 .cmd_startswith
= 1,
1940 cmd_parser
= &write_mem_cmd_desc
;
1945 static const GdbCmdParseEntry get_reg_cmd_desc
= {
1946 .handler
= handle_get_reg
,
1948 .cmd_startswith
= 1,
1951 cmd_parser
= &get_reg_cmd_desc
;
1956 static const GdbCmdParseEntry set_reg_cmd_desc
= {
1957 .handler
= handle_set_reg
,
1959 .cmd_startswith
= 1,
1962 cmd_parser
= &set_reg_cmd_desc
;
1967 static const GdbCmdParseEntry insert_bp_cmd_desc
= {
1968 .handler
= handle_insert_bp
,
1970 .cmd_startswith
= 1,
1973 cmd_parser
= &insert_bp_cmd_desc
;
1978 static const GdbCmdParseEntry remove_bp_cmd_desc
= {
1979 .handler
= handle_remove_bp
,
1981 .cmd_startswith
= 1,
1984 cmd_parser
= &remove_bp_cmd_desc
;
1989 static const GdbCmdParseEntry set_thread_cmd_desc
= {
1990 .handler
= handle_set_thread
,
1992 .cmd_startswith
= 1,
1995 cmd_parser
= &set_thread_cmd_desc
;
2000 static const GdbCmdParseEntry thread_alive_cmd_desc
= {
2001 .handler
= handle_thread_alive
,
2003 .cmd_startswith
= 1,
2006 cmd_parser
= &thread_alive_cmd_desc
;
2011 static const GdbCmdParseEntry gen_query_cmd_desc
= {
2012 .handler
= handle_gen_query
,
2014 .cmd_startswith
= 1,
2017 cmd_parser
= &gen_query_cmd_desc
;
2022 static const GdbCmdParseEntry gen_set_cmd_desc
= {
2023 .handler
= handle_gen_set
,
2025 .cmd_startswith
= 1,
2028 cmd_parser
= &gen_set_cmd_desc
;
2032 /* put empty packet */
2038 run_cmd_parser(line_buf
, cmd_parser
);
2044 void gdb_set_stop_cpu(CPUState
*cpu
)
2046 GDBProcess
*p
= gdb_get_cpu_process(cpu
);
2050 * Having a stop CPU corresponding to a process that is not attached
2051 * confuses GDB. So we ignore the request.
2056 gdbserver_state
.c_cpu
= cpu
;
2057 gdbserver_state
.g_cpu
= cpu
;
2060 void gdb_read_byte(uint8_t ch
)
2064 gdbserver_state
.allow_stop_reply
= false;
2065 #ifndef CONFIG_USER_ONLY
2066 if (gdbserver_state
.last_packet
->len
) {
2067 /* Waiting for a response to the last packet. If we see the start
2068 of a new command then abandon the previous response. */
2070 trace_gdbstub_err_got_nack();
2071 gdb_put_buffer(gdbserver_state
.last_packet
->data
,
2072 gdbserver_state
.last_packet
->len
);
2073 } else if (ch
== '+') {
2074 trace_gdbstub_io_got_ack();
2076 trace_gdbstub_io_got_unexpected(ch
);
2079 if (ch
== '+' || ch
== '$') {
2080 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
2085 if (runstate_is_running()) {
2087 * When the CPU is running, we cannot do anything except stop
2088 * it when receiving a char. This is expected on a Ctrl-C in the
2089 * gdb client. Because we are in all-stop mode, gdb sends a
2090 * 0x03 byte which is not a usual packet, so we handle it specially
2091 * here, but it does expect a stop reply.
2094 trace_gdbstub_err_unexpected_runpkt(ch
);
2096 gdbserver_state
.allow_stop_reply
= true;
2098 vm_stop(RUN_STATE_PAUSED
);
2102 switch(gdbserver_state
.state
) {
2105 /* start of command packet */
2106 gdbserver_state
.line_buf_index
= 0;
2107 gdbserver_state
.line_sum
= 0;
2108 gdbserver_state
.state
= RS_GETLINE
;
2109 } else if (ch
== '+') {
2111 * do nothing, gdb may preemptively send out ACKs on
2112 * initial connection
2115 trace_gdbstub_err_garbage(ch
);
2120 /* start escape sequence */
2121 gdbserver_state
.state
= RS_GETLINE_ESC
;
2122 gdbserver_state
.line_sum
+= ch
;
2123 } else if (ch
== '*') {
2124 /* start run length encoding sequence */
2125 gdbserver_state
.state
= RS_GETLINE_RLE
;
2126 gdbserver_state
.line_sum
+= ch
;
2127 } else if (ch
== '#') {
2128 /* end of command, start of checksum*/
2129 gdbserver_state
.state
= RS_CHKSUM1
;
2130 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2131 trace_gdbstub_err_overrun();
2132 gdbserver_state
.state
= RS_IDLE
;
2134 /* unescaped command character */
2135 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
;
2136 gdbserver_state
.line_sum
+= ch
;
2139 case RS_GETLINE_ESC
:
2141 /* unexpected end of command in escape sequence */
2142 gdbserver_state
.state
= RS_CHKSUM1
;
2143 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2144 /* command buffer overrun */
2145 trace_gdbstub_err_overrun();
2146 gdbserver_state
.state
= RS_IDLE
;
2148 /* parse escaped character and leave escape state */
2149 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
^ 0x20;
2150 gdbserver_state
.line_sum
+= ch
;
2151 gdbserver_state
.state
= RS_GETLINE
;
2154 case RS_GETLINE_RLE
:
2156 * Run-length encoding is explained in "Debugging with GDB /
2157 * Appendix E GDB Remote Serial Protocol / Overview".
2159 if (ch
< ' ' || ch
== '#' || ch
== '$' || ch
> 126) {
2160 /* invalid RLE count encoding */
2161 trace_gdbstub_err_invalid_repeat(ch
);
2162 gdbserver_state
.state
= RS_GETLINE
;
2164 /* decode repeat length */
2165 int repeat
= ch
- ' ' + 3;
2166 if (gdbserver_state
.line_buf_index
+ repeat
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2167 /* that many repeats would overrun the command buffer */
2168 trace_gdbstub_err_overrun();
2169 gdbserver_state
.state
= RS_IDLE
;
2170 } else if (gdbserver_state
.line_buf_index
< 1) {
2171 /* got a repeat but we have nothing to repeat */
2172 trace_gdbstub_err_invalid_rle();
2173 gdbserver_state
.state
= RS_GETLINE
;
2175 /* repeat the last character */
2176 memset(gdbserver_state
.line_buf
+ gdbserver_state
.line_buf_index
,
2177 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
- 1], repeat
);
2178 gdbserver_state
.line_buf_index
+= repeat
;
2179 gdbserver_state
.line_sum
+= ch
;
2180 gdbserver_state
.state
= RS_GETLINE
;
2185 /* get high hex digit of checksum */
2186 if (!isxdigit(ch
)) {
2187 trace_gdbstub_err_checksum_invalid(ch
);
2188 gdbserver_state
.state
= RS_GETLINE
;
2191 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
] = '\0';
2192 gdbserver_state
.line_csum
= fromhex(ch
) << 4;
2193 gdbserver_state
.state
= RS_CHKSUM2
;
2196 /* get low hex digit of checksum */
2197 if (!isxdigit(ch
)) {
2198 trace_gdbstub_err_checksum_invalid(ch
);
2199 gdbserver_state
.state
= RS_GETLINE
;
2202 gdbserver_state
.line_csum
|= fromhex(ch
);
2204 if (gdbserver_state
.line_csum
!= (gdbserver_state
.line_sum
& 0xff)) {
2205 trace_gdbstub_err_checksum_incorrect(gdbserver_state
.line_sum
, gdbserver_state
.line_csum
);
2206 /* send NAK reply */
2208 gdb_put_buffer(&reply
, 1);
2209 gdbserver_state
.state
= RS_IDLE
;
2211 /* send ACK reply */
2213 gdb_put_buffer(&reply
, 1);
2214 gdbserver_state
.state
= gdb_handle_packet(gdbserver_state
.line_buf
);
2224 * Create the process that will contain all the "orphan" CPUs (that are not
2225 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2226 * be attachable and thus will be invisible to the user.
2228 void gdb_create_default_process(GDBState
*s
)
2230 GDBProcess
*process
;
2233 #ifdef CONFIG_USER_ONLY
2234 assert(gdbserver_state
.process_num
== 0);
2237 if (gdbserver_state
.process_num
) {
2238 pid
= s
->processes
[s
->process_num
- 1].pid
;
2242 /* We need an available PID slot for this process */
2243 assert(pid
< UINT32_MAX
);
2247 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2248 process
= &s
->processes
[s
->process_num
- 1];
2250 process
->attached
= false;
2251 process
->target_xml
= NULL
;