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
;
78 /* writes 2*len+1 bytes in buf */
79 void gdb_memtohex(GString
*buf
, const uint8_t *mem
, int len
)
82 for(i
= 0; i
< len
; i
++) {
84 g_string_append_c(buf
, tohex(c
>> 4));
85 g_string_append_c(buf
, tohex(c
& 0xf));
87 g_string_append_c(buf
, '\0');
90 void gdb_hextomem(GByteArray
*mem
, const char *buf
, int len
)
94 for(i
= 0; i
< len
; i
++) {
95 guint8 byte
= fromhex(buf
[0]) << 4 | fromhex(buf
[1]);
96 g_byte_array_append(mem
, &byte
, 1);
101 static void hexdump(const char *buf
, int len
,
102 void (*trace_fn
)(size_t ofs
, char const *text
))
104 char line_buffer
[3 * 16 + 4 + 16 + 1];
107 for (i
= 0; i
< len
|| (i
& 0xF); ++i
) {
108 size_t byte_ofs
= i
& 15;
111 memset(line_buffer
, ' ', 3 * 16 + 4 + 16);
112 line_buffer
[3 * 16 + 4 + 16] = 0;
115 size_t col_group
= (i
>> 2) & 3;
116 size_t hex_col
= byte_ofs
* 3 + col_group
;
117 size_t txt_col
= 3 * 16 + 4 + byte_ofs
;
122 line_buffer
[hex_col
+ 0] = tohex((value
>> 4) & 0xF);
123 line_buffer
[hex_col
+ 1] = tohex((value
>> 0) & 0xF);
124 line_buffer
[txt_col
+ 0] = (value
>= ' ' && value
< 127)
130 trace_fn(i
& -16, line_buffer
);
134 /* return -1 if error, 0 if OK */
135 int gdb_put_packet_binary(const char *buf
, int len
, bool dump
)
140 if (dump
&& trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY
)) {
141 hexdump(buf
, len
, trace_gdbstub_io_binaryreply
);
145 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
146 g_byte_array_append(gdbserver_state
.last_packet
,
147 (const uint8_t *) "$", 1);
148 g_byte_array_append(gdbserver_state
.last_packet
,
149 (const uint8_t *) buf
, len
);
151 for(i
= 0; i
< len
; i
++) {
155 footer
[1] = tohex((csum
>> 4) & 0xf);
156 footer
[2] = tohex((csum
) & 0xf);
157 g_byte_array_append(gdbserver_state
.last_packet
, footer
, 3);
159 gdb_put_buffer(gdbserver_state
.last_packet
->data
,
160 gdbserver_state
.last_packet
->len
);
162 if (gdb_got_immediate_ack()) {
169 /* return -1 if error, 0 if OK */
170 int gdb_put_packet(const char *buf
)
172 trace_gdbstub_io_reply(buf
);
174 return gdb_put_packet_binary(buf
, strlen(buf
), false);
177 void gdb_put_strbuf(void)
179 gdb_put_packet(gdbserver_state
.str_buf
->str
);
182 /* Encode data using the encoding for 'x' packets. */
183 void gdb_memtox(GString
*buf
, const char *mem
, int len
)
190 case '#': case '$': case '*': case '}':
191 g_string_append_c(buf
, '}');
192 g_string_append_c(buf
, c
^ 0x20);
195 g_string_append_c(buf
, c
);
201 static uint32_t gdb_get_cpu_pid(CPUState
*cpu
)
203 #ifdef CONFIG_USER_ONLY
206 if (cpu
->cluster_index
== UNASSIGNED_CLUSTER_INDEX
) {
207 /* Return the default process' PID */
208 int index
= gdbserver_state
.process_num
- 1;
209 return gdbserver_state
.processes
[index
].pid
;
211 return cpu
->cluster_index
+ 1;
215 GDBProcess
*gdb_get_process(uint32_t pid
)
220 /* 0 means any process, we take the first one */
221 return &gdbserver_state
.processes
[0];
224 for (i
= 0; i
< gdbserver_state
.process_num
; i
++) {
225 if (gdbserver_state
.processes
[i
].pid
== pid
) {
226 return &gdbserver_state
.processes
[i
];
233 static GDBProcess
*gdb_get_cpu_process(CPUState
*cpu
)
235 return gdb_get_process(gdb_get_cpu_pid(cpu
));
238 static CPUState
*find_cpu(uint32_t thread_id
)
243 if (gdb_get_cpu_index(cpu
) == thread_id
) {
251 CPUState
*gdb_get_first_cpu_in_process(GDBProcess
*process
)
256 if (gdb_get_cpu_pid(cpu
) == process
->pid
) {
264 static CPUState
*gdb_next_cpu_in_process(CPUState
*cpu
)
266 uint32_t pid
= gdb_get_cpu_pid(cpu
);
270 if (gdb_get_cpu_pid(cpu
) == pid
) {
280 /* Return the cpu following @cpu, while ignoring unattached processes. */
281 static CPUState
*gdb_next_attached_cpu(CPUState
*cpu
)
286 if (gdb_get_cpu_process(cpu
)->attached
) {
296 /* Return the first attached cpu */
297 CPUState
*gdb_first_attached_cpu(void)
299 CPUState
*cpu
= first_cpu
;
300 GDBProcess
*process
= gdb_get_cpu_process(cpu
);
302 if (!process
->attached
) {
303 return gdb_next_attached_cpu(cpu
);
309 static CPUState
*gdb_get_cpu(uint32_t pid
, uint32_t tid
)
315 /* 0 means any process/thread, we take the first attached one */
316 return gdb_first_attached_cpu();
317 } else if (pid
&& !tid
) {
318 /* any thread in a specific process */
319 process
= gdb_get_process(pid
);
321 if (process
== NULL
) {
325 if (!process
->attached
) {
329 return gdb_get_first_cpu_in_process(process
);
331 /* a specific thread */
338 process
= gdb_get_cpu_process(cpu
);
340 if (pid
&& process
->pid
!= pid
) {
344 if (!process
->attached
) {
352 bool gdb_has_xml(void)
354 return !!gdb_get_cpu_process(gdbserver_state
.g_cpu
)->target_xml
;
357 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
);
365 * qXfer:features:read:ANNEX:OFFSET,LENGTH'
368 char *term
= strchr(p
, ':');
372 /* Is it the main target xml? */
373 if (strncmp(p
, "target.xml", len
) == 0) {
374 if (!process
->target_xml
) {
376 GString
*xml
= g_string_new("<?xml version=\"1.0\"?>");
379 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
382 if (cc
->gdb_arch_name
) {
383 g_autofree gchar
*arch
= cc
->gdb_arch_name(cpu
);
384 g_string_append_printf(xml
,
385 "<architecture>%s</architecture>",
388 g_string_append(xml
, "<xi:include href=\"");
389 g_string_append(xml
, cc
->gdb_core_xml_file
);
390 g_string_append(xml
, "\"/>");
391 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
392 g_string_append(xml
, "<xi:include href=\"");
393 g_string_append(xml
, r
->xml
);
394 g_string_append(xml
, "\"/>");
396 g_string_append(xml
, "</target>");
398 process
->target_xml
= g_string_free(xml
, false);
399 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; xml_builtin
[i
][0]; i
++) {
412 const char *name
= xml_builtin
[i
][0];
413 if ((strncmp(name
, p
, len
) == 0) &&
414 strlen(name
) == len
) {
415 return xml_builtin
[i
][1];
423 static int gdb_read_register(CPUState
*cpu
, GByteArray
*buf
, int reg
)
425 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
426 CPUArchState
*env
= cpu
->env_ptr
;
429 if (reg
< cc
->gdb_num_core_regs
) {
430 return cc
->gdb_read_register(cpu
, buf
, reg
);
433 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
434 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
435 return r
->get_reg(env
, buf
, reg
- r
->base_reg
);
441 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
443 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
444 CPUArchState
*env
= cpu
->env_ptr
;
447 if (reg
< cc
->gdb_num_core_regs
) {
448 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
451 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
452 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
453 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
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.
602 * target_count and last_target keep track of how many CPUs we are going to
603 * step or resume, and a pointer to the state structure of one of them,
606 int target_count
= 0;
607 CPUState
*last_target
= NULL
;
615 if (cur_action
== 'C' || cur_action
== 'S') {
616 cur_action
= qemu_tolower(cur_action
);
617 res
= qemu_strtoul(p
, &p
, 16, &tmp
);
621 signal
= gdb_signal_to_target(tmp
);
622 } else if (cur_action
!= 'c' && cur_action
!= 's') {
623 /* unknown/invalid/unsupported command */
627 if (*p
== '\0' || *p
== ';') {
629 * No thread specifier, action is on "all threads". The
630 * specification is unclear regarding the process to act on. We
631 * choose all processes.
633 kind
= GDB_ALL_PROCESSES
;
634 } else if (*p
++ == ':') {
635 kind
= read_thread_id(p
, &p
, &pid
, &tid
);
641 case GDB_READ_THREAD_ERR
:
644 case GDB_ALL_PROCESSES
:
645 cpu
= gdb_first_attached_cpu();
647 if (newstates
[cpu
->cpu_index
] == 1) {
648 newstates
[cpu
->cpu_index
] = cur_action
;
654 cpu
= gdb_next_attached_cpu(cpu
);
658 case GDB_ALL_THREADS
:
659 process
= gdb_get_process(pid
);
661 if (!process
->attached
) {
665 cpu
= gdb_get_first_cpu_in_process(process
);
667 if (newstates
[cpu
->cpu_index
] == 1) {
668 newstates
[cpu
->cpu_index
] = cur_action
;
674 cpu
= gdb_next_cpu_in_process(cpu
);
679 cpu
= gdb_get_cpu(pid
, tid
);
681 /* invalid CPU/thread specified */
686 /* only use if no previous match occourred */
687 if (newstates
[cpu
->cpu_index
] == 1) {
688 newstates
[cpu
->cpu_index
] = cur_action
;
698 * if we're about to resume a specific set of CPUs/threads, make it so that
699 * in case execution gets interrupted, we can send GDB a stop reply with a
700 * correct value. it doesn't really matter which CPU we tell GDB the signal
701 * happened in (VM pauses stop all of them anyway), so long as it is one of
702 * the ones we resumed/single stepped here.
704 if (target_count
> 0) {
705 gdbserver_state
.c_cpu
= last_target
;
708 gdbserver_state
.signal
= signal
;
709 gdb_continue_partial(newstates
);
713 static const char *cmd_next_param(const char *param
, const char delimiter
)
715 static const char all_delimiters
[] = ",;:=";
716 char curr_delimiters
[2] = {0};
717 const char *delimiters
;
719 if (delimiter
== '?') {
720 delimiters
= all_delimiters
;
721 } else if (delimiter
== '0') {
722 return strchr(param
, '\0');
723 } else if (delimiter
== '.' && *param
) {
726 curr_delimiters
[0] = delimiter
;
727 delimiters
= curr_delimiters
;
730 param
+= strcspn(param
, delimiters
);
737 static int cmd_parse_params(const char *data
, const char *schema
,
740 const char *curr_schema
, *curr_data
;
743 g_assert(params
->len
== 0);
745 curr_schema
= schema
;
747 while (curr_schema
[0] && curr_schema
[1] && *curr_data
) {
748 GdbCmdVariant this_param
;
750 switch (curr_schema
[0]) {
752 if (qemu_strtoul(curr_data
, &curr_data
, 16,
753 &this_param
.val_ul
)) {
756 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
757 g_array_append_val(params
, this_param
);
760 if (qemu_strtou64(curr_data
, &curr_data
, 16,
761 (uint64_t *)&this_param
.val_ull
)) {
764 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
765 g_array_append_val(params
, this_param
);
768 this_param
.data
= curr_data
;
769 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
770 g_array_append_val(params
, this_param
);
773 this_param
.opcode
= *(uint8_t *)curr_data
;
774 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
775 g_array_append_val(params
, this_param
);
778 this_param
.thread_id
.kind
=
779 read_thread_id(curr_data
, &curr_data
,
780 &this_param
.thread_id
.pid
,
781 &this_param
.thread_id
.tid
);
782 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
783 g_array_append_val(params
, this_param
);
786 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
797 typedef void (*GdbCmdHandler
)(GArray
*params
, void *user_ctx
);
800 * cmd_startswith -> cmd is compared using startswith
802 * allow_stop_reply -> true iff the gdbstub can respond to this command with a
803 * "stop reply" packet. The list of commands that accept such response is
804 * defined at the GDB Remote Serial Protocol documentation. see:
805 * https://sourceware.org/gdb/onlinedocs/gdb/Stop-Reply-Packets.html#Stop-Reply-Packets.
807 * schema definitions:
808 * Each schema parameter entry consists of 2 chars,
809 * the first char represents the parameter type handling
810 * the second char represents the delimiter for the next parameter
812 * Currently supported schema types:
813 * 'l' -> unsigned long (stored in .val_ul)
814 * 'L' -> unsigned long long (stored in .val_ull)
815 * 's' -> string (stored in .data)
816 * 'o' -> single char (stored in .opcode)
817 * 't' -> thread id (stored in .thread_id)
818 * '?' -> skip according to delimiter
820 * Currently supported delimiters:
821 * '?' -> Stop at any delimiter (",;:=\0")
822 * '0' -> Stop at "\0"
823 * '.' -> Skip 1 char unless reached "\0"
824 * Any other value is treated as the delimiter value itself
826 typedef struct GdbCmdParseEntry
{
827 GdbCmdHandler handler
;
831 bool allow_stop_reply
;
834 static inline int startswith(const char *string
, const char *pattern
)
836 return !strncmp(string
, pattern
, strlen(pattern
));
839 static int process_string_cmd(const char *data
,
840 const GdbCmdParseEntry
*cmds
, int num_cmds
)
843 g_autoptr(GArray
) params
= g_array_new(false, true, sizeof(GdbCmdVariant
));
849 for (i
= 0; i
< num_cmds
; i
++) {
850 const GdbCmdParseEntry
*cmd
= &cmds
[i
];
851 g_assert(cmd
->handler
&& cmd
->cmd
);
853 if ((cmd
->cmd_startswith
&& !startswith(data
, cmd
->cmd
)) ||
854 (!cmd
->cmd_startswith
&& strcmp(cmd
->cmd
, data
))) {
859 if (cmd_parse_params(&data
[strlen(cmd
->cmd
)],
860 cmd
->schema
, params
)) {
865 gdbserver_state
.allow_stop_reply
= cmd
->allow_stop_reply
;
866 cmd
->handler(params
, NULL
);
873 static void run_cmd_parser(const char *data
, const GdbCmdParseEntry
*cmd
)
879 g_string_set_size(gdbserver_state
.str_buf
, 0);
880 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
882 /* In case there was an error during the command parsing we must
883 * send a NULL packet to indicate the command is not supported */
884 if (process_string_cmd(data
, cmd
, 1)) {
889 static void handle_detach(GArray
*params
, void *user_ctx
)
894 if (gdbserver_state
.multiprocess
) {
896 gdb_put_packet("E22");
900 pid
= get_param(params
, 0)->val_ul
;
903 process
= gdb_get_process(pid
);
904 gdb_process_breakpoint_remove_all(process
);
905 process
->attached
= false;
907 if (pid
== gdb_get_cpu_pid(gdbserver_state
.c_cpu
)) {
908 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
911 if (pid
== gdb_get_cpu_pid(gdbserver_state
.g_cpu
)) {
912 gdbserver_state
.g_cpu
= gdb_first_attached_cpu();
915 if (!gdbserver_state
.c_cpu
) {
916 /* No more process attached */
917 gdb_disable_syscalls();
920 gdb_put_packet("OK");
923 static void handle_thread_alive(GArray
*params
, void *user_ctx
)
928 gdb_put_packet("E22");
932 if (get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
933 gdb_put_packet("E22");
937 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
938 get_param(params
, 0)->thread_id
.tid
);
940 gdb_put_packet("E22");
944 gdb_put_packet("OK");
947 static void handle_continue(GArray
*params
, void *user_ctx
)
950 gdb_set_cpu_pc(get_param(params
, 0)->val_ull
);
953 gdbserver_state
.signal
= 0;
957 static void handle_cont_with_sig(GArray
*params
, void *user_ctx
)
959 unsigned long signal
= 0;
962 * Note: C sig;[addr] is currently unsupported and we simply
963 * omit the addr parameter
966 signal
= get_param(params
, 0)->val_ul
;
969 gdbserver_state
.signal
= gdb_signal_to_target(signal
);
970 if (gdbserver_state
.signal
== -1) {
971 gdbserver_state
.signal
= 0;
976 static void handle_set_thread(GArray
*params
, void *user_ctx
)
980 if (params
->len
!= 2) {
981 gdb_put_packet("E22");
985 if (get_param(params
, 1)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
986 gdb_put_packet("E22");
990 if (get_param(params
, 1)->thread_id
.kind
!= GDB_ONE_THREAD
) {
991 gdb_put_packet("OK");
995 cpu
= gdb_get_cpu(get_param(params
, 1)->thread_id
.pid
,
996 get_param(params
, 1)->thread_id
.tid
);
998 gdb_put_packet("E22");
1003 * Note: This command is deprecated and modern gdb's will be using the
1004 * vCont command instead.
1006 switch (get_param(params
, 0)->opcode
) {
1008 gdbserver_state
.c_cpu
= cpu
;
1009 gdb_put_packet("OK");
1012 gdbserver_state
.g_cpu
= cpu
;
1013 gdb_put_packet("OK");
1016 gdb_put_packet("E22");
1021 static void handle_insert_bp(GArray
*params
, void *user_ctx
)
1025 if (params
->len
!= 3) {
1026 gdb_put_packet("E22");
1030 res
= gdb_breakpoint_insert(gdbserver_state
.c_cpu
,
1031 get_param(params
, 0)->val_ul
,
1032 get_param(params
, 1)->val_ull
,
1033 get_param(params
, 2)->val_ull
);
1035 gdb_put_packet("OK");
1037 } else if (res
== -ENOSYS
) {
1042 gdb_put_packet("E22");
1045 static void handle_remove_bp(GArray
*params
, void *user_ctx
)
1049 if (params
->len
!= 3) {
1050 gdb_put_packet("E22");
1054 res
= gdb_breakpoint_remove(gdbserver_state
.c_cpu
,
1055 get_param(params
, 0)->val_ul
,
1056 get_param(params
, 1)->val_ull
,
1057 get_param(params
, 2)->val_ull
);
1059 gdb_put_packet("OK");
1061 } else if (res
== -ENOSYS
) {
1066 gdb_put_packet("E22");
1070 * handle_set/get_reg
1072 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1073 * This works, but can be very slow. Anything new enough to understand
1074 * XML also knows how to use this properly. However to use this we
1075 * need to define a local XML file as well as be talking to a
1076 * reasonably modern gdb. Responding with an empty packet will cause
1077 * the remote gdb to fallback to older methods.
1080 static void handle_set_reg(GArray
*params
, void *user_ctx
)
1084 if (!gdb_get_cpu_process(gdbserver_state
.g_cpu
)->target_xml
) {
1089 if (params
->len
!= 2) {
1090 gdb_put_packet("E22");
1094 reg_size
= strlen(get_param(params
, 1)->data
) / 2;
1095 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 1)->data
, reg_size
);
1096 gdb_write_register(gdbserver_state
.g_cpu
, gdbserver_state
.mem_buf
->data
,
1097 get_param(params
, 0)->val_ull
);
1098 gdb_put_packet("OK");
1101 static void handle_get_reg(GArray
*params
, void *user_ctx
)
1105 if (!gdb_get_cpu_process(gdbserver_state
.g_cpu
)->target_xml
) {
1111 gdb_put_packet("E14");
1115 reg_size
= gdb_read_register(gdbserver_state
.g_cpu
,
1116 gdbserver_state
.mem_buf
,
1117 get_param(params
, 0)->val_ull
);
1119 gdb_put_packet("E14");
1122 g_byte_array_set_size(gdbserver_state
.mem_buf
, reg_size
);
1125 gdb_memtohex(gdbserver_state
.str_buf
,
1126 gdbserver_state
.mem_buf
->data
, reg_size
);
1130 static void handle_write_mem(GArray
*params
, void *user_ctx
)
1132 if (params
->len
!= 3) {
1133 gdb_put_packet("E22");
1137 /* gdb_hextomem() reads 2*len bytes */
1138 if (get_param(params
, 1)->val_ull
>
1139 strlen(get_param(params
, 2)->data
) / 2) {
1140 gdb_put_packet("E22");
1144 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 2)->data
,
1145 get_param(params
, 1)->val_ull
);
1146 if (gdb_target_memory_rw_debug(gdbserver_state
.g_cpu
,
1147 get_param(params
, 0)->val_ull
,
1148 gdbserver_state
.mem_buf
->data
,
1149 gdbserver_state
.mem_buf
->len
, true)) {
1150 gdb_put_packet("E14");
1154 gdb_put_packet("OK");
1157 static void handle_read_mem(GArray
*params
, void *user_ctx
)
1159 if (params
->len
!= 2) {
1160 gdb_put_packet("E22");
1164 /* gdb_memtohex() doubles the required space */
1165 if (get_param(params
, 1)->val_ull
> MAX_PACKET_LENGTH
/ 2) {
1166 gdb_put_packet("E22");
1170 g_byte_array_set_size(gdbserver_state
.mem_buf
,
1171 get_param(params
, 1)->val_ull
);
1173 if (gdb_target_memory_rw_debug(gdbserver_state
.g_cpu
,
1174 get_param(params
, 0)->val_ull
,
1175 gdbserver_state
.mem_buf
->data
,
1176 gdbserver_state
.mem_buf
->len
, false)) {
1177 gdb_put_packet("E14");
1181 gdb_memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
,
1182 gdbserver_state
.mem_buf
->len
);
1186 static void handle_write_all_regs(GArray
*params
, void *user_ctx
)
1197 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1198 len
= strlen(get_param(params
, 0)->data
) / 2;
1199 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 0)->data
, len
);
1200 registers
= gdbserver_state
.mem_buf
->data
;
1202 reg_id
< gdbserver_state
.g_cpu
->gdb_num_g_regs
&& len
> 0;
1204 reg_size
= gdb_write_register(gdbserver_state
.g_cpu
, registers
, reg_id
);
1206 registers
+= reg_size
;
1208 gdb_put_packet("OK");
1211 static void handle_read_all_regs(GArray
*params
, void *user_ctx
)
1216 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1217 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
1219 for (reg_id
= 0; reg_id
< gdbserver_state
.g_cpu
->gdb_num_g_regs
; reg_id
++) {
1220 len
+= gdb_read_register(gdbserver_state
.g_cpu
,
1221 gdbserver_state
.mem_buf
,
1224 g_assert(len
== gdbserver_state
.mem_buf
->len
);
1226 gdb_memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
, len
);
1231 static void handle_step(GArray
*params
, void *user_ctx
)
1234 gdb_set_cpu_pc(get_param(params
, 0)->val_ull
);
1237 cpu_single_step(gdbserver_state
.c_cpu
, gdbserver_state
.sstep_flags
);
1241 static void handle_backward(GArray
*params
, void *user_ctx
)
1243 if (!gdb_can_reverse()) {
1244 gdb_put_packet("E22");
1246 if (params
->len
== 1) {
1247 switch (get_param(params
, 0)->opcode
) {
1249 if (replay_reverse_step()) {
1252 gdb_put_packet("E14");
1256 if (replay_reverse_continue()) {
1259 gdb_put_packet("E14");
1265 /* Default invalid command */
1269 static void handle_v_cont_query(GArray
*params
, void *user_ctx
)
1271 gdb_put_packet("vCont;c;C;s;S");
1274 static void handle_v_cont(GArray
*params
, void *user_ctx
)
1282 res
= gdb_handle_vcont(get_param(params
, 0)->data
);
1283 if ((res
== -EINVAL
) || (res
== -ERANGE
)) {
1284 gdb_put_packet("E22");
1290 static void handle_v_attach(GArray
*params
, void *user_ctx
)
1292 GDBProcess
*process
;
1295 g_string_assign(gdbserver_state
.str_buf
, "E22");
1300 process
= gdb_get_process(get_param(params
, 0)->val_ul
);
1305 cpu
= gdb_get_first_cpu_in_process(process
);
1310 process
->attached
= true;
1311 gdbserver_state
.g_cpu
= cpu
;
1312 gdbserver_state
.c_cpu
= cpu
;
1314 if (gdbserver_state
.allow_stop_reply
) {
1315 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
1316 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
1317 g_string_append_c(gdbserver_state
.str_buf
, ';');
1318 gdbserver_state
.allow_stop_reply
= false;
1324 static void handle_v_kill(GArray
*params
, void *user_ctx
)
1326 /* Kill the target */
1327 gdb_put_packet("OK");
1328 error_report("QEMU: Terminated via GDBstub");
1333 static const GdbCmdParseEntry gdb_v_commands_table
[] = {
1334 /* Order is important if has same prefix */
1336 .handler
= handle_v_cont_query
,
1341 .handler
= handle_v_cont
,
1343 .cmd_startswith
= 1,
1344 .allow_stop_reply
= true,
1348 .handler
= handle_v_attach
,
1350 .cmd_startswith
= 1,
1351 .allow_stop_reply
= true,
1355 .handler
= handle_v_kill
,
1359 #ifdef CONFIG_USER_ONLY
1361 * Host I/O Packets. See [1] for details.
1362 * [1] https://sourceware.org/gdb/onlinedocs/gdb/Host-I_002fO-Packets.html
1365 .handler
= gdb_handle_v_file_open
,
1366 .cmd
= "File:open:",
1367 .cmd_startswith
= 1,
1371 .handler
= gdb_handle_v_file_close
,
1372 .cmd
= "File:close:",
1373 .cmd_startswith
= 1,
1377 .handler
= gdb_handle_v_file_pread
,
1378 .cmd
= "File:pread:",
1379 .cmd_startswith
= 1,
1383 .handler
= gdb_handle_v_file_readlink
,
1384 .cmd
= "File:readlink:",
1385 .cmd_startswith
= 1,
1391 static void handle_v_commands(GArray
*params
, void *user_ctx
)
1397 if (process_string_cmd(get_param(params
, 0)->data
,
1398 gdb_v_commands_table
,
1399 ARRAY_SIZE(gdb_v_commands_table
))) {
1404 static void handle_query_qemu_sstepbits(GArray
*params
, void *user_ctx
)
1406 g_string_printf(gdbserver_state
.str_buf
, "ENABLE=%x", SSTEP_ENABLE
);
1408 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOIRQ
) {
1409 g_string_append_printf(gdbserver_state
.str_buf
, ",NOIRQ=%x",
1413 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOTIMER
) {
1414 g_string_append_printf(gdbserver_state
.str_buf
, ",NOTIMER=%x",
1421 static void handle_set_qemu_sstep(GArray
*params
, void *user_ctx
)
1423 int new_sstep_flags
;
1429 new_sstep_flags
= get_param(params
, 0)->val_ul
;
1431 if (new_sstep_flags
& ~gdbserver_state
.supported_sstep_flags
) {
1432 gdb_put_packet("E22");
1436 gdbserver_state
.sstep_flags
= new_sstep_flags
;
1437 gdb_put_packet("OK");
1440 static void handle_query_qemu_sstep(GArray
*params
, void *user_ctx
)
1442 g_string_printf(gdbserver_state
.str_buf
, "0x%x",
1443 gdbserver_state
.sstep_flags
);
1447 static void handle_query_curr_tid(GArray
*params
, void *user_ctx
)
1450 GDBProcess
*process
;
1453 * "Current thread" remains vague in the spec, so always return
1454 * the first thread of the current process (gdb returns the
1457 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
1458 cpu
= gdb_get_first_cpu_in_process(process
);
1459 g_string_assign(gdbserver_state
.str_buf
, "QC");
1460 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
1464 static void handle_query_threads(GArray
*params
, void *user_ctx
)
1466 if (!gdbserver_state
.query_cpu
) {
1467 gdb_put_packet("l");
1471 g_string_assign(gdbserver_state
.str_buf
, "m");
1472 gdb_append_thread_id(gdbserver_state
.query_cpu
, gdbserver_state
.str_buf
);
1474 gdbserver_state
.query_cpu
= gdb_next_attached_cpu(gdbserver_state
.query_cpu
);
1477 static void handle_query_first_threads(GArray
*params
, void *user_ctx
)
1479 gdbserver_state
.query_cpu
= gdb_first_attached_cpu();
1480 handle_query_threads(params
, user_ctx
);
1483 static void handle_query_thread_extra(GArray
*params
, void *user_ctx
)
1485 g_autoptr(GString
) rs
= g_string_new(NULL
);
1489 get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1490 gdb_put_packet("E22");
1494 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
1495 get_param(params
, 0)->thread_id
.tid
);
1500 cpu_synchronize_state(cpu
);
1502 if (gdbserver_state
.multiprocess
&& (gdbserver_state
.process_num
> 1)) {
1503 /* Print the CPU model and name in multiprocess mode */
1504 ObjectClass
*oc
= object_get_class(OBJECT(cpu
));
1505 const char *cpu_model
= object_class_get_name(oc
);
1506 const char *cpu_name
=
1507 object_get_canonical_path_component(OBJECT(cpu
));
1508 g_string_printf(rs
, "%s %s [%s]", cpu_model
, cpu_name
,
1509 cpu
->halted
? "halted " : "running");
1511 g_string_printf(rs
, "CPU#%d [%s]", cpu
->cpu_index
,
1512 cpu
->halted
? "halted " : "running");
1514 trace_gdbstub_op_extra_info(rs
->str
);
1515 gdb_memtohex(gdbserver_state
.str_buf
, (uint8_t *)rs
->str
, rs
->len
);
1519 static void handle_query_supported(GArray
*params
, void *user_ctx
)
1523 g_string_printf(gdbserver_state
.str_buf
, "PacketSize=%x", MAX_PACKET_LENGTH
);
1524 cc
= CPU_GET_CLASS(first_cpu
);
1525 if (cc
->gdb_core_xml_file
) {
1526 g_string_append(gdbserver_state
.str_buf
, ";qXfer:features:read+");
1529 if (gdb_can_reverse()) {
1530 g_string_append(gdbserver_state
.str_buf
,
1531 ";ReverseStep+;ReverseContinue+");
1534 #if defined(CONFIG_USER_ONLY)
1535 #if defined(CONFIG_LINUX)
1536 if (gdbserver_state
.c_cpu
->opaque
) {
1537 g_string_append(gdbserver_state
.str_buf
, ";qXfer:auxv:read+");
1540 g_string_append(gdbserver_state
.str_buf
, ";qXfer:exec-file:read+");
1544 strstr(get_param(params
, 0)->data
, "multiprocess+")) {
1545 gdbserver_state
.multiprocess
= true;
1548 g_string_append(gdbserver_state
.str_buf
, ";vContSupported+;multiprocess+");
1552 static void handle_query_xfer_features(GArray
*params
, void *user_ctx
)
1554 GDBProcess
*process
;
1556 unsigned long len
, total_len
, addr
;
1560 if (params
->len
< 3) {
1561 gdb_put_packet("E22");
1565 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
1566 cc
= CPU_GET_CLASS(gdbserver_state
.g_cpu
);
1567 if (!cc
->gdb_core_xml_file
) {
1572 p
= get_param(params
, 0)->data
;
1573 xml
= get_feature_xml(p
, &p
, process
);
1575 gdb_put_packet("E00");
1579 addr
= get_param(params
, 1)->val_ul
;
1580 len
= get_param(params
, 2)->val_ul
;
1581 total_len
= strlen(xml
);
1582 if (addr
> total_len
) {
1583 gdb_put_packet("E00");
1587 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
1588 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1591 if (len
< total_len
- addr
) {
1592 g_string_assign(gdbserver_state
.str_buf
, "m");
1593 gdb_memtox(gdbserver_state
.str_buf
, xml
+ addr
, len
);
1595 g_string_assign(gdbserver_state
.str_buf
, "l");
1596 gdb_memtox(gdbserver_state
.str_buf
, xml
+ addr
, total_len
- addr
);
1599 gdb_put_packet_binary(gdbserver_state
.str_buf
->str
,
1600 gdbserver_state
.str_buf
->len
, true);
1603 static void handle_query_qemu_supported(GArray
*params
, void *user_ctx
)
1605 g_string_printf(gdbserver_state
.str_buf
, "sstepbits;sstep");
1606 #ifndef CONFIG_USER_ONLY
1607 g_string_append(gdbserver_state
.str_buf
, ";PhyMemMode");
1612 static const GdbCmdParseEntry gdb_gen_query_set_common_table
[] = {
1613 /* Order is important if has same prefix */
1615 .handler
= handle_query_qemu_sstepbits
,
1616 .cmd
= "qemu.sstepbits",
1619 .handler
= handle_query_qemu_sstep
,
1620 .cmd
= "qemu.sstep",
1623 .handler
= handle_set_qemu_sstep
,
1624 .cmd
= "qemu.sstep=",
1625 .cmd_startswith
= 1,
1630 static const GdbCmdParseEntry gdb_gen_query_table
[] = {
1632 .handler
= handle_query_curr_tid
,
1636 .handler
= handle_query_threads
,
1637 .cmd
= "sThreadInfo",
1640 .handler
= handle_query_first_threads
,
1641 .cmd
= "fThreadInfo",
1644 .handler
= handle_query_thread_extra
,
1645 .cmd
= "ThreadExtraInfo,",
1646 .cmd_startswith
= 1,
1649 #ifdef CONFIG_USER_ONLY
1651 .handler
= gdb_handle_query_offsets
,
1656 .handler
= gdb_handle_query_rcmd
,
1658 .cmd_startswith
= 1,
1663 .handler
= handle_query_supported
,
1664 .cmd
= "Supported:",
1665 .cmd_startswith
= 1,
1669 .handler
= handle_query_supported
,
1674 .handler
= handle_query_xfer_features
,
1675 .cmd
= "Xfer:features:read:",
1676 .cmd_startswith
= 1,
1679 #if defined(CONFIG_USER_ONLY)
1680 #if defined(CONFIG_LINUX)
1682 .handler
= gdb_handle_query_xfer_auxv
,
1683 .cmd
= "Xfer:auxv:read::",
1684 .cmd_startswith
= 1,
1689 .handler
= gdb_handle_query_xfer_exec_file
,
1690 .cmd
= "Xfer:exec-file:read:",
1691 .cmd_startswith
= 1,
1696 .handler
= gdb_handle_query_attached
,
1701 .handler
= gdb_handle_query_attached
,
1705 .handler
= handle_query_qemu_supported
,
1706 .cmd
= "qemu.Supported",
1708 #ifndef CONFIG_USER_ONLY
1710 .handler
= gdb_handle_query_qemu_phy_mem_mode
,
1711 .cmd
= "qemu.PhyMemMode",
1716 static const GdbCmdParseEntry gdb_gen_set_table
[] = {
1717 /* Order is important if has same prefix */
1719 .handler
= handle_set_qemu_sstep
,
1720 .cmd
= "qemu.sstep:",
1721 .cmd_startswith
= 1,
1724 #ifndef CONFIG_USER_ONLY
1726 .handler
= gdb_handle_set_qemu_phy_mem_mode
,
1727 .cmd
= "qemu.PhyMemMode:",
1728 .cmd_startswith
= 1,
1734 static void handle_gen_query(GArray
*params
, void *user_ctx
)
1740 if (!process_string_cmd(get_param(params
, 0)->data
,
1741 gdb_gen_query_set_common_table
,
1742 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
1746 if (process_string_cmd(get_param(params
, 0)->data
,
1747 gdb_gen_query_table
,
1748 ARRAY_SIZE(gdb_gen_query_table
))) {
1753 static void handle_gen_set(GArray
*params
, void *user_ctx
)
1759 if (!process_string_cmd(get_param(params
, 0)->data
,
1760 gdb_gen_query_set_common_table
,
1761 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
1765 if (process_string_cmd(get_param(params
, 0)->data
,
1767 ARRAY_SIZE(gdb_gen_set_table
))) {
1772 static void handle_target_halt(GArray
*params
, void *user_ctx
)
1774 if (gdbserver_state
.allow_stop_reply
) {
1775 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
1776 gdb_append_thread_id(gdbserver_state
.c_cpu
, gdbserver_state
.str_buf
);
1777 g_string_append_c(gdbserver_state
.str_buf
, ';');
1779 gdbserver_state
.allow_stop_reply
= false;
1782 * Remove all the breakpoints when this query is issued,
1783 * because gdb is doing an initial connect and the state
1784 * should be cleaned up.
1786 gdb_breakpoint_remove_all(gdbserver_state
.c_cpu
);
1789 static int gdb_handle_packet(const char *line_buf
)
1791 const GdbCmdParseEntry
*cmd_parser
= NULL
;
1793 trace_gdbstub_io_command(line_buf
);
1795 switch (line_buf
[0]) {
1797 gdb_put_packet("OK");
1801 static const GdbCmdParseEntry target_halted_cmd_desc
= {
1802 .handler
= handle_target_halt
,
1804 .cmd_startswith
= 1,
1805 .allow_stop_reply
= true,
1807 cmd_parser
= &target_halted_cmd_desc
;
1812 static const GdbCmdParseEntry continue_cmd_desc
= {
1813 .handler
= handle_continue
,
1815 .cmd_startswith
= 1,
1816 .allow_stop_reply
= true,
1819 cmd_parser
= &continue_cmd_desc
;
1824 static const GdbCmdParseEntry cont_with_sig_cmd_desc
= {
1825 .handler
= handle_cont_with_sig
,
1827 .cmd_startswith
= 1,
1828 .allow_stop_reply
= true,
1831 cmd_parser
= &cont_with_sig_cmd_desc
;
1836 static const GdbCmdParseEntry v_cmd_desc
= {
1837 .handler
= handle_v_commands
,
1839 .cmd_startswith
= 1,
1842 cmd_parser
= &v_cmd_desc
;
1846 /* Kill the target */
1847 error_report("QEMU: Terminated via GDBstub");
1852 static const GdbCmdParseEntry detach_cmd_desc
= {
1853 .handler
= handle_detach
,
1855 .cmd_startswith
= 1,
1858 cmd_parser
= &detach_cmd_desc
;
1863 static const GdbCmdParseEntry step_cmd_desc
= {
1864 .handler
= handle_step
,
1866 .cmd_startswith
= 1,
1867 .allow_stop_reply
= true,
1870 cmd_parser
= &step_cmd_desc
;
1875 static const GdbCmdParseEntry backward_cmd_desc
= {
1876 .handler
= handle_backward
,
1878 .cmd_startswith
= 1,
1879 .allow_stop_reply
= true,
1882 cmd_parser
= &backward_cmd_desc
;
1887 static const GdbCmdParseEntry file_io_cmd_desc
= {
1888 .handler
= gdb_handle_file_io
,
1890 .cmd_startswith
= 1,
1893 cmd_parser
= &file_io_cmd_desc
;
1898 static const GdbCmdParseEntry read_all_regs_cmd_desc
= {
1899 .handler
= handle_read_all_regs
,
1903 cmd_parser
= &read_all_regs_cmd_desc
;
1908 static const GdbCmdParseEntry write_all_regs_cmd_desc
= {
1909 .handler
= handle_write_all_regs
,
1911 .cmd_startswith
= 1,
1914 cmd_parser
= &write_all_regs_cmd_desc
;
1919 static const GdbCmdParseEntry read_mem_cmd_desc
= {
1920 .handler
= handle_read_mem
,
1922 .cmd_startswith
= 1,
1925 cmd_parser
= &read_mem_cmd_desc
;
1930 static const GdbCmdParseEntry write_mem_cmd_desc
= {
1931 .handler
= handle_write_mem
,
1933 .cmd_startswith
= 1,
1936 cmd_parser
= &write_mem_cmd_desc
;
1941 static const GdbCmdParseEntry get_reg_cmd_desc
= {
1942 .handler
= handle_get_reg
,
1944 .cmd_startswith
= 1,
1947 cmd_parser
= &get_reg_cmd_desc
;
1952 static const GdbCmdParseEntry set_reg_cmd_desc
= {
1953 .handler
= handle_set_reg
,
1955 .cmd_startswith
= 1,
1958 cmd_parser
= &set_reg_cmd_desc
;
1963 static const GdbCmdParseEntry insert_bp_cmd_desc
= {
1964 .handler
= handle_insert_bp
,
1966 .cmd_startswith
= 1,
1969 cmd_parser
= &insert_bp_cmd_desc
;
1974 static const GdbCmdParseEntry remove_bp_cmd_desc
= {
1975 .handler
= handle_remove_bp
,
1977 .cmd_startswith
= 1,
1980 cmd_parser
= &remove_bp_cmd_desc
;
1985 static const GdbCmdParseEntry set_thread_cmd_desc
= {
1986 .handler
= handle_set_thread
,
1988 .cmd_startswith
= 1,
1991 cmd_parser
= &set_thread_cmd_desc
;
1996 static const GdbCmdParseEntry thread_alive_cmd_desc
= {
1997 .handler
= handle_thread_alive
,
1999 .cmd_startswith
= 1,
2002 cmd_parser
= &thread_alive_cmd_desc
;
2007 static const GdbCmdParseEntry gen_query_cmd_desc
= {
2008 .handler
= handle_gen_query
,
2010 .cmd_startswith
= 1,
2013 cmd_parser
= &gen_query_cmd_desc
;
2018 static const GdbCmdParseEntry gen_set_cmd_desc
= {
2019 .handler
= handle_gen_set
,
2021 .cmd_startswith
= 1,
2024 cmd_parser
= &gen_set_cmd_desc
;
2028 /* put empty packet */
2034 run_cmd_parser(line_buf
, cmd_parser
);
2040 void gdb_set_stop_cpu(CPUState
*cpu
)
2042 GDBProcess
*p
= gdb_get_cpu_process(cpu
);
2046 * Having a stop CPU corresponding to a process that is not attached
2047 * confuses GDB. So we ignore the request.
2052 gdbserver_state
.c_cpu
= cpu
;
2053 gdbserver_state
.g_cpu
= cpu
;
2056 void gdb_read_byte(uint8_t ch
)
2060 gdbserver_state
.allow_stop_reply
= false;
2061 #ifndef CONFIG_USER_ONLY
2062 if (gdbserver_state
.last_packet
->len
) {
2063 /* Waiting for a response to the last packet. If we see the start
2064 of a new command then abandon the previous response. */
2066 trace_gdbstub_err_got_nack();
2067 gdb_put_buffer(gdbserver_state
.last_packet
->data
,
2068 gdbserver_state
.last_packet
->len
);
2069 } else if (ch
== '+') {
2070 trace_gdbstub_io_got_ack();
2072 trace_gdbstub_io_got_unexpected(ch
);
2075 if (ch
== '+' || ch
== '$') {
2076 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
2081 if (runstate_is_running()) {
2083 * When the CPU is running, we cannot do anything except stop
2084 * it when receiving a char. This is expected on a Ctrl-C in the
2085 * gdb client. Because we are in all-stop mode, gdb sends a
2086 * 0x03 byte which is not a usual packet, so we handle it specially
2087 * here, but it does expect a stop reply.
2090 trace_gdbstub_err_unexpected_runpkt(ch
);
2092 gdbserver_state
.allow_stop_reply
= true;
2094 vm_stop(RUN_STATE_PAUSED
);
2098 switch(gdbserver_state
.state
) {
2101 /* start of command packet */
2102 gdbserver_state
.line_buf_index
= 0;
2103 gdbserver_state
.line_sum
= 0;
2104 gdbserver_state
.state
= RS_GETLINE
;
2105 } else if (ch
== '+') {
2107 * do nothing, gdb may preemptively send out ACKs on
2108 * initial connection
2111 trace_gdbstub_err_garbage(ch
);
2116 /* start escape sequence */
2117 gdbserver_state
.state
= RS_GETLINE_ESC
;
2118 gdbserver_state
.line_sum
+= ch
;
2119 } else if (ch
== '*') {
2120 /* start run length encoding sequence */
2121 gdbserver_state
.state
= RS_GETLINE_RLE
;
2122 gdbserver_state
.line_sum
+= ch
;
2123 } else if (ch
== '#') {
2124 /* end of command, start of checksum*/
2125 gdbserver_state
.state
= RS_CHKSUM1
;
2126 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2127 trace_gdbstub_err_overrun();
2128 gdbserver_state
.state
= RS_IDLE
;
2130 /* unescaped command character */
2131 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
;
2132 gdbserver_state
.line_sum
+= ch
;
2135 case RS_GETLINE_ESC
:
2137 /* unexpected end of command in escape sequence */
2138 gdbserver_state
.state
= RS_CHKSUM1
;
2139 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2140 /* command buffer overrun */
2141 trace_gdbstub_err_overrun();
2142 gdbserver_state
.state
= RS_IDLE
;
2144 /* parse escaped character and leave escape state */
2145 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
^ 0x20;
2146 gdbserver_state
.line_sum
+= ch
;
2147 gdbserver_state
.state
= RS_GETLINE
;
2150 case RS_GETLINE_RLE
:
2152 * Run-length encoding is explained in "Debugging with GDB /
2153 * Appendix E GDB Remote Serial Protocol / Overview".
2155 if (ch
< ' ' || ch
== '#' || ch
== '$' || ch
> 126) {
2156 /* invalid RLE count encoding */
2157 trace_gdbstub_err_invalid_repeat(ch
);
2158 gdbserver_state
.state
= RS_GETLINE
;
2160 /* decode repeat length */
2161 int repeat
= ch
- ' ' + 3;
2162 if (gdbserver_state
.line_buf_index
+ repeat
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2163 /* that many repeats would overrun the command buffer */
2164 trace_gdbstub_err_overrun();
2165 gdbserver_state
.state
= RS_IDLE
;
2166 } else if (gdbserver_state
.line_buf_index
< 1) {
2167 /* got a repeat but we have nothing to repeat */
2168 trace_gdbstub_err_invalid_rle();
2169 gdbserver_state
.state
= RS_GETLINE
;
2171 /* repeat the last character */
2172 memset(gdbserver_state
.line_buf
+ gdbserver_state
.line_buf_index
,
2173 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
- 1], repeat
);
2174 gdbserver_state
.line_buf_index
+= repeat
;
2175 gdbserver_state
.line_sum
+= ch
;
2176 gdbserver_state
.state
= RS_GETLINE
;
2181 /* get high hex digit of checksum */
2182 if (!isxdigit(ch
)) {
2183 trace_gdbstub_err_checksum_invalid(ch
);
2184 gdbserver_state
.state
= RS_GETLINE
;
2187 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
] = '\0';
2188 gdbserver_state
.line_csum
= fromhex(ch
) << 4;
2189 gdbserver_state
.state
= RS_CHKSUM2
;
2192 /* get low hex digit of checksum */
2193 if (!isxdigit(ch
)) {
2194 trace_gdbstub_err_checksum_invalid(ch
);
2195 gdbserver_state
.state
= RS_GETLINE
;
2198 gdbserver_state
.line_csum
|= fromhex(ch
);
2200 if (gdbserver_state
.line_csum
!= (gdbserver_state
.line_sum
& 0xff)) {
2201 trace_gdbstub_err_checksum_incorrect(gdbserver_state
.line_sum
, gdbserver_state
.line_csum
);
2202 /* send NAK reply */
2204 gdb_put_buffer(&reply
, 1);
2205 gdbserver_state
.state
= RS_IDLE
;
2207 /* send ACK reply */
2209 gdb_put_buffer(&reply
, 1);
2210 gdbserver_state
.state
= gdb_handle_packet(gdbserver_state
.line_buf
);
2220 * Create the process that will contain all the "orphan" CPUs (that are not
2221 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2222 * be attachable and thus will be invisible to the user.
2224 void gdb_create_default_process(GDBState
*s
)
2226 GDBProcess
*process
;
2229 #ifdef CONFIG_USER_ONLY
2230 assert(gdbserver_state
.process_num
== 0);
2233 if (gdbserver_state
.process_num
) {
2234 pid
= s
->processes
[s
->process_num
- 1].pid
;
2238 /* We need an available PID slot for this process */
2239 assert(pid
< UINT32_MAX
);
2243 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2244 process
= &s
->processes
[s
->process_num
- 1];
2246 process
->attached
= false;
2247 process
->target_xml
= NULL
;