4 * This implements a subset of the remote protocol as described in:
6 * https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
8 * Copyright (c) 2003-2005 Fabrice Bellard
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 * SPDX-License-Identifier: LGPL-2.0+
26 #include "qemu/osdep.h"
27 #include "qemu/ctype.h"
28 #include "qemu/cutils.h"
29 #include "qemu/module.h"
30 #include "qemu/error-report.h"
32 #include "exec/gdbstub.h"
33 #include "gdbstub/syscalls.h"
34 #ifdef CONFIG_USER_ONLY
35 #include "gdbstub/user.h"
37 #include "hw/cpu/cluster.h"
38 #include "hw/boards.h"
41 #include "sysemu/hw_accel.h"
42 #include "sysemu/runstate.h"
43 #include "exec/replay-core.h"
44 #include "exec/hwaddr.h"
46 #include "internals.h"
48 typedef struct GDBRegisterState
{
51 gdb_get_reg_cb get_reg
;
52 gdb_set_reg_cb set_reg
;
56 GDBState gdbserver_state
;
58 void gdb_init_gdbserver_state(void)
60 g_assert(!gdbserver_state
.init
);
61 memset(&gdbserver_state
, 0, sizeof(GDBState
));
62 gdbserver_state
.init
= true;
63 gdbserver_state
.str_buf
= g_string_new(NULL
);
64 gdbserver_state
.mem_buf
= g_byte_array_sized_new(MAX_PACKET_LENGTH
);
65 gdbserver_state
.last_packet
= g_byte_array_sized_new(MAX_PACKET_LENGTH
+ 4);
68 * What single-step modes are supported is accelerator dependent.
69 * By default try to use no IRQs and no timers while single
70 * stepping so as to make single stepping like a typical ICE HW step.
72 gdbserver_state
.supported_sstep_flags
= accel_supported_gdbstub_sstep_flags();
73 gdbserver_state
.sstep_flags
= SSTEP_ENABLE
| SSTEP_NOIRQ
| SSTEP_NOTIMER
;
74 gdbserver_state
.sstep_flags
&= gdbserver_state
.supported_sstep_flags
;
77 /* writes 2*len+1 bytes in buf */
78 void gdb_memtohex(GString
*buf
, const uint8_t *mem
, int len
)
81 for(i
= 0; i
< len
; i
++) {
83 g_string_append_c(buf
, tohex(c
>> 4));
84 g_string_append_c(buf
, tohex(c
& 0xf));
86 g_string_append_c(buf
, '\0');
89 void gdb_hextomem(GByteArray
*mem
, const char *buf
, int len
)
93 for(i
= 0; i
< len
; i
++) {
94 guint8 byte
= fromhex(buf
[0]) << 4 | fromhex(buf
[1]);
95 g_byte_array_append(mem
, &byte
, 1);
100 static void hexdump(const char *buf
, int len
,
101 void (*trace_fn
)(size_t ofs
, char const *text
))
103 char line_buffer
[3 * 16 + 4 + 16 + 1];
106 for (i
= 0; i
< len
|| (i
& 0xF); ++i
) {
107 size_t byte_ofs
= i
& 15;
110 memset(line_buffer
, ' ', 3 * 16 + 4 + 16);
111 line_buffer
[3 * 16 + 4 + 16] = 0;
114 size_t col_group
= (i
>> 2) & 3;
115 size_t hex_col
= byte_ofs
* 3 + col_group
;
116 size_t txt_col
= 3 * 16 + 4 + byte_ofs
;
121 line_buffer
[hex_col
+ 0] = tohex((value
>> 4) & 0xF);
122 line_buffer
[hex_col
+ 1] = tohex((value
>> 0) & 0xF);
123 line_buffer
[txt_col
+ 0] = (value
>= ' ' && value
< 127)
129 trace_fn(i
& -16, line_buffer
);
133 /* return -1 if error, 0 if OK */
134 int gdb_put_packet_binary(const char *buf
, int len
, bool dump
)
139 if (dump
&& trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY
)) {
140 hexdump(buf
, len
, trace_gdbstub_io_binaryreply
);
144 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
145 g_byte_array_append(gdbserver_state
.last_packet
,
146 (const uint8_t *) "$", 1);
147 g_byte_array_append(gdbserver_state
.last_packet
,
148 (const uint8_t *) buf
, len
);
150 for(i
= 0; i
< len
; i
++) {
154 footer
[1] = tohex((csum
>> 4) & 0xf);
155 footer
[2] = tohex((csum
) & 0xf);
156 g_byte_array_append(gdbserver_state
.last_packet
, footer
, 3);
158 gdb_put_buffer(gdbserver_state
.last_packet
->data
,
159 gdbserver_state
.last_packet
->len
);
161 if (gdb_got_immediate_ack()) {
168 /* return -1 if error, 0 if OK */
169 int gdb_put_packet(const char *buf
)
171 trace_gdbstub_io_reply(buf
);
173 return gdb_put_packet_binary(buf
, strlen(buf
), false);
176 void gdb_put_strbuf(void)
178 gdb_put_packet(gdbserver_state
.str_buf
->str
);
181 /* Encode data using the encoding for 'x' packets. */
182 void gdb_memtox(GString
*buf
, const char *mem
, int len
)
189 case '#': case '$': case '*': case '}':
190 g_string_append_c(buf
, '}');
191 g_string_append_c(buf
, c
^ 0x20);
194 g_string_append_c(buf
, c
);
200 static uint32_t gdb_get_cpu_pid(CPUState
*cpu
)
202 #ifdef CONFIG_USER_ONLY
205 if (cpu
->cluster_index
== UNASSIGNED_CLUSTER_INDEX
) {
206 /* Return the default process' PID */
207 int index
= gdbserver_state
.process_num
- 1;
208 return gdbserver_state
.processes
[index
].pid
;
210 return cpu
->cluster_index
+ 1;
214 GDBProcess
*gdb_get_process(uint32_t pid
)
219 /* 0 means any process, we take the first one */
220 return &gdbserver_state
.processes
[0];
223 for (i
= 0; i
< gdbserver_state
.process_num
; i
++) {
224 if (gdbserver_state
.processes
[i
].pid
== pid
) {
225 return &gdbserver_state
.processes
[i
];
232 static GDBProcess
*gdb_get_cpu_process(CPUState
*cpu
)
234 return gdb_get_process(gdb_get_cpu_pid(cpu
));
237 static CPUState
*find_cpu(uint32_t thread_id
)
242 if (gdb_get_cpu_index(cpu
) == thread_id
) {
250 CPUState
*gdb_get_first_cpu_in_process(GDBProcess
*process
)
255 if (gdb_get_cpu_pid(cpu
) == process
->pid
) {
263 static CPUState
*gdb_next_cpu_in_process(CPUState
*cpu
)
265 uint32_t pid
= gdb_get_cpu_pid(cpu
);
269 if (gdb_get_cpu_pid(cpu
) == pid
) {
279 /* Return the cpu following @cpu, while ignoring unattached processes. */
280 static CPUState
*gdb_next_attached_cpu(CPUState
*cpu
)
285 if (gdb_get_cpu_process(cpu
)->attached
) {
295 /* Return the first attached cpu */
296 CPUState
*gdb_first_attached_cpu(void)
298 CPUState
*cpu
= first_cpu
;
299 GDBProcess
*process
= gdb_get_cpu_process(cpu
);
301 if (!process
->attached
) {
302 return gdb_next_attached_cpu(cpu
);
308 static CPUState
*gdb_get_cpu(uint32_t pid
, uint32_t tid
)
314 /* 0 means any process/thread, we take the first attached one */
315 return gdb_first_attached_cpu();
316 } else if (pid
&& !tid
) {
317 /* any thread in a specific process */
318 process
= gdb_get_process(pid
);
320 if (process
== NULL
) {
324 if (!process
->attached
) {
328 return gdb_get_first_cpu_in_process(process
);
330 /* a specific thread */
337 process
= gdb_get_cpu_process(cpu
);
339 if (pid
&& process
->pid
!= pid
) {
343 if (!process
->attached
) {
351 static const char *get_feature_xml(const char *p
, const char **newp
,
354 CPUState
*cpu
= gdb_get_first_cpu_in_process(process
);
355 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
359 * qXfer:features:read:ANNEX:OFFSET,LENGTH'
362 char *term
= strchr(p
, ':');
366 /* Is it the main target xml? */
367 if (strncmp(p
, "target.xml", len
) == 0) {
368 if (!process
->target_xml
) {
370 g_autoptr(GPtrArray
) xml
= g_ptr_array_new_with_free_func(g_free
);
374 g_strdup("<?xml version=\"1.0\"?>"
375 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
378 if (cc
->gdb_arch_name
) {
381 g_markup_printf_escaped("<architecture>%s</architecture>",
382 cc
->gdb_arch_name(cpu
)));
386 g_markup_printf_escaped("<xi:include href=\"%s\"/>",
387 cc
->gdb_core_xml_file
));
389 for (guint i
= 0; i
< cpu
->gdb_regs
->len
; i
++) {
390 r
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
393 g_markup_printf_escaped("<xi:include href=\"%s\"/>",
397 g_ptr_array_add(xml
, g_strdup("</target>"));
398 g_ptr_array_add(xml
, NULL
);
400 process
->target_xml
= g_strjoinv(NULL
, (void *)xml
->pdata
);
402 return process
->target_xml
;
404 /* Is it dynamically generated by the target? */
405 if (cc
->gdb_get_dynamic_xml
) {
406 g_autofree
char *xmlname
= g_strndup(p
, len
);
407 const char *xml
= cc
->gdb_get_dynamic_xml(cpu
, xmlname
);
412 /* Is it one of the encoded gdb-xml/ files? */
413 for (int i
= 0; gdb_static_features
[i
].xmlname
; i
++) {
414 const char *name
= gdb_static_features
[i
].xmlname
;
415 if ((strncmp(name
, p
, len
) == 0) &&
416 strlen(name
) == len
) {
417 return gdb_static_features
[i
].xml
;
425 static int gdb_read_register(CPUState
*cpu
, GByteArray
*buf
, int reg
)
427 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
428 CPUArchState
*env
= cpu_env(cpu
);
431 if (reg
< cc
->gdb_num_core_regs
) {
432 return cc
->gdb_read_register(cpu
, buf
, reg
);
436 for (guint i
= 0; i
< cpu
->gdb_regs
->len
; i
++) {
437 r
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
438 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
439 return r
->get_reg(env
, buf
, reg
- r
->base_reg
);
446 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
448 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
449 CPUArchState
*env
= cpu_env(cpu
);
452 if (reg
< cc
->gdb_num_core_regs
) {
453 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
457 for (guint i
= 0; i
< cpu
->gdb_regs
->len
; i
++) {
458 r
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
459 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
460 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
467 void gdb_register_coprocessor(CPUState
*cpu
,
468 gdb_get_reg_cb get_reg
, gdb_set_reg_cb set_reg
,
469 int num_regs
, const char *xml
, int g_pos
)
475 for (i
= 0; i
< cpu
->gdb_regs
->len
; i
++) {
476 /* Check for duplicates. */
477 s
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
478 if (strcmp(s
->xml
, xml
) == 0) {
483 cpu
->gdb_regs
= g_array_new(false, false, sizeof(GDBRegisterState
));
487 g_array_set_size(cpu
->gdb_regs
, i
+ 1);
488 s
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
489 s
->base_reg
= cpu
->gdb_num_regs
;
490 s
->num_regs
= num_regs
;
491 s
->get_reg
= get_reg
;
492 s
->set_reg
= set_reg
;
495 /* Add to end of list. */
496 cpu
->gdb_num_regs
+= num_regs
;
498 if (g_pos
!= s
->base_reg
) {
499 error_report("Error: Bad gdb register numbering for '%s', "
500 "expected %d got %d", xml
, g_pos
, s
->base_reg
);
502 cpu
->gdb_num_g_regs
= cpu
->gdb_num_regs
;
507 static void gdb_process_breakpoint_remove_all(GDBProcess
*p
)
509 CPUState
*cpu
= gdb_get_first_cpu_in_process(p
);
512 gdb_breakpoint_remove_all(cpu
);
513 cpu
= gdb_next_cpu_in_process(cpu
);
518 static void gdb_set_cpu_pc(vaddr pc
)
520 CPUState
*cpu
= gdbserver_state
.c_cpu
;
522 cpu_synchronize_state(cpu
);
526 void gdb_append_thread_id(CPUState
*cpu
, GString
*buf
)
528 if (gdbserver_state
.multiprocess
) {
529 g_string_append_printf(buf
, "p%02x.%02x",
530 gdb_get_cpu_pid(cpu
), gdb_get_cpu_index(cpu
));
532 g_string_append_printf(buf
, "%02x", gdb_get_cpu_index(cpu
));
536 static GDBThreadIdKind
read_thread_id(const char *buf
, const char **end_buf
,
537 uint32_t *pid
, uint32_t *tid
)
544 ret
= qemu_strtoul(buf
, &buf
, 16, &p
);
547 return GDB_READ_THREAD_ERR
;
556 ret
= qemu_strtoul(buf
, &buf
, 16, &t
);
559 return GDB_READ_THREAD_ERR
;
565 return GDB_ALL_PROCESSES
;
573 return GDB_ALL_THREADS
;
580 return GDB_ONE_THREAD
;
584 * gdb_handle_vcont - Parses and handles a vCont packet.
585 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
586 * a format error, 0 on success.
588 static int gdb_handle_vcont(const char *p
)
596 GDBThreadIdKind kind
;
597 unsigned int max_cpus
= gdb_get_max_cpus();
598 /* uninitialised CPUs stay 0 */
599 g_autofree
char *newstates
= g_new0(char, max_cpus
);
601 /* mark valid CPUs with 1 */
603 newstates
[cpu
->cpu_index
] = 1;
607 * res keeps track of what error we are returning, with -ENOTSUP meaning
608 * that the command is unknown or unsupported, thus returning an empty
609 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
610 * or incorrect parameters passed.
615 * target_count and last_target keep track of how many CPUs we are going to
616 * step or resume, and a pointer to the state structure of one of them,
619 int target_count
= 0;
620 CPUState
*last_target
= NULL
;
628 if (cur_action
== 'C' || cur_action
== 'S') {
629 cur_action
= qemu_tolower(cur_action
);
630 res
= qemu_strtoul(p
, &p
, 16, &tmp
);
634 signal
= gdb_signal_to_target(tmp
);
635 } else if (cur_action
!= 'c' && cur_action
!= 's') {
636 /* unknown/invalid/unsupported command */
640 if (*p
== '\0' || *p
== ';') {
642 * No thread specifier, action is on "all threads". The
643 * specification is unclear regarding the process to act on. We
644 * choose all processes.
646 kind
= GDB_ALL_PROCESSES
;
647 } else if (*p
++ == ':') {
648 kind
= read_thread_id(p
, &p
, &pid
, &tid
);
654 case GDB_READ_THREAD_ERR
:
657 case GDB_ALL_PROCESSES
:
658 cpu
= gdb_first_attached_cpu();
660 if (newstates
[cpu
->cpu_index
] == 1) {
661 newstates
[cpu
->cpu_index
] = cur_action
;
667 cpu
= gdb_next_attached_cpu(cpu
);
671 case GDB_ALL_THREADS
:
672 process
= gdb_get_process(pid
);
674 if (!process
->attached
) {
678 cpu
= gdb_get_first_cpu_in_process(process
);
680 if (newstates
[cpu
->cpu_index
] == 1) {
681 newstates
[cpu
->cpu_index
] = cur_action
;
687 cpu
= gdb_next_cpu_in_process(cpu
);
692 cpu
= gdb_get_cpu(pid
, tid
);
694 /* invalid CPU/thread specified */
699 /* only use if no previous match occourred */
700 if (newstates
[cpu
->cpu_index
] == 1) {
701 newstates
[cpu
->cpu_index
] = cur_action
;
711 * if we're about to resume a specific set of CPUs/threads, make it so that
712 * in case execution gets interrupted, we can send GDB a stop reply with a
713 * correct value. it doesn't really matter which CPU we tell GDB the signal
714 * happened in (VM pauses stop all of them anyway), so long as it is one of
715 * the ones we resumed/single stepped here.
717 if (target_count
> 0) {
718 gdbserver_state
.c_cpu
= last_target
;
721 gdbserver_state
.signal
= signal
;
722 gdb_continue_partial(newstates
);
726 static const char *cmd_next_param(const char *param
, const char delimiter
)
728 static const char all_delimiters
[] = ",;:=";
729 char curr_delimiters
[2] = {0};
730 const char *delimiters
;
732 if (delimiter
== '?') {
733 delimiters
= all_delimiters
;
734 } else if (delimiter
== '0') {
735 return strchr(param
, '\0');
736 } else if (delimiter
== '.' && *param
) {
739 curr_delimiters
[0] = delimiter
;
740 delimiters
= curr_delimiters
;
743 param
+= strcspn(param
, delimiters
);
750 static int cmd_parse_params(const char *data
, const char *schema
,
753 const char *curr_schema
, *curr_data
;
756 g_assert(params
->len
== 0);
758 curr_schema
= schema
;
760 while (curr_schema
[0] && curr_schema
[1] && *curr_data
) {
761 GdbCmdVariant this_param
;
763 switch (curr_schema
[0]) {
765 if (qemu_strtoul(curr_data
, &curr_data
, 16,
766 &this_param
.val_ul
)) {
769 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
770 g_array_append_val(params
, this_param
);
773 if (qemu_strtou64(curr_data
, &curr_data
, 16,
774 (uint64_t *)&this_param
.val_ull
)) {
777 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
778 g_array_append_val(params
, this_param
);
781 this_param
.data
= curr_data
;
782 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
783 g_array_append_val(params
, this_param
);
786 this_param
.opcode
= *(uint8_t *)curr_data
;
787 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
788 g_array_append_val(params
, this_param
);
791 this_param
.thread_id
.kind
=
792 read_thread_id(curr_data
, &curr_data
,
793 &this_param
.thread_id
.pid
,
794 &this_param
.thread_id
.tid
);
795 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
796 g_array_append_val(params
, this_param
);
799 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
810 typedef void (*GdbCmdHandler
)(GArray
*params
, void *user_ctx
);
813 * cmd_startswith -> cmd is compared using startswith
815 * allow_stop_reply -> true iff the gdbstub can respond to this command with a
816 * "stop reply" packet. The list of commands that accept such response is
817 * defined at the GDB Remote Serial Protocol documentation. see:
818 * https://sourceware.org/gdb/onlinedocs/gdb/Stop-Reply-Packets.html#Stop-Reply-Packets.
820 * schema definitions:
821 * Each schema parameter entry consists of 2 chars,
822 * the first char represents the parameter type handling
823 * the second char represents the delimiter for the next parameter
825 * Currently supported schema types:
826 * 'l' -> unsigned long (stored in .val_ul)
827 * 'L' -> unsigned long long (stored in .val_ull)
828 * 's' -> string (stored in .data)
829 * 'o' -> single char (stored in .opcode)
830 * 't' -> thread id (stored in .thread_id)
831 * '?' -> skip according to delimiter
833 * Currently supported delimiters:
834 * '?' -> Stop at any delimiter (",;:=\0")
835 * '0' -> Stop at "\0"
836 * '.' -> Skip 1 char unless reached "\0"
837 * Any other value is treated as the delimiter value itself
839 typedef struct GdbCmdParseEntry
{
840 GdbCmdHandler handler
;
844 bool allow_stop_reply
;
847 static inline int startswith(const char *string
, const char *pattern
)
849 return !strncmp(string
, pattern
, strlen(pattern
));
852 static int process_string_cmd(const char *data
,
853 const GdbCmdParseEntry
*cmds
, int num_cmds
)
856 g_autoptr(GArray
) params
= g_array_new(false, true, sizeof(GdbCmdVariant
));
862 for (i
= 0; i
< num_cmds
; i
++) {
863 const GdbCmdParseEntry
*cmd
= &cmds
[i
];
864 g_assert(cmd
->handler
&& cmd
->cmd
);
866 if ((cmd
->cmd_startswith
&& !startswith(data
, cmd
->cmd
)) ||
867 (!cmd
->cmd_startswith
&& strcmp(cmd
->cmd
, data
))) {
872 if (cmd_parse_params(&data
[strlen(cmd
->cmd
)],
873 cmd
->schema
, params
)) {
878 gdbserver_state
.allow_stop_reply
= cmd
->allow_stop_reply
;
879 cmd
->handler(params
, NULL
);
886 static void run_cmd_parser(const char *data
, const GdbCmdParseEntry
*cmd
)
892 g_string_set_size(gdbserver_state
.str_buf
, 0);
893 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
895 /* In case there was an error during the command parsing we must
896 * send a NULL packet to indicate the command is not supported */
897 if (process_string_cmd(data
, cmd
, 1)) {
902 static void handle_detach(GArray
*params
, void *user_ctx
)
907 if (gdbserver_state
.multiprocess
) {
909 gdb_put_packet("E22");
913 pid
= get_param(params
, 0)->val_ul
;
916 process
= gdb_get_process(pid
);
917 gdb_process_breakpoint_remove_all(process
);
918 process
->attached
= false;
920 if (pid
== gdb_get_cpu_pid(gdbserver_state
.c_cpu
)) {
921 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
924 if (pid
== gdb_get_cpu_pid(gdbserver_state
.g_cpu
)) {
925 gdbserver_state
.g_cpu
= gdb_first_attached_cpu();
928 if (!gdbserver_state
.c_cpu
) {
929 /* No more process attached */
930 gdb_disable_syscalls();
933 gdb_put_packet("OK");
936 static void handle_thread_alive(GArray
*params
, void *user_ctx
)
941 gdb_put_packet("E22");
945 if (get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
946 gdb_put_packet("E22");
950 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
951 get_param(params
, 0)->thread_id
.tid
);
953 gdb_put_packet("E22");
957 gdb_put_packet("OK");
960 static void handle_continue(GArray
*params
, void *user_ctx
)
963 gdb_set_cpu_pc(get_param(params
, 0)->val_ull
);
966 gdbserver_state
.signal
= 0;
970 static void handle_cont_with_sig(GArray
*params
, void *user_ctx
)
972 unsigned long signal
= 0;
975 * Note: C sig;[addr] is currently unsupported and we simply
976 * omit the addr parameter
979 signal
= get_param(params
, 0)->val_ul
;
982 gdbserver_state
.signal
= gdb_signal_to_target(signal
);
983 if (gdbserver_state
.signal
== -1) {
984 gdbserver_state
.signal
= 0;
989 static void handle_set_thread(GArray
*params
, void *user_ctx
)
993 if (params
->len
!= 2) {
994 gdb_put_packet("E22");
998 if (get_param(params
, 1)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
999 gdb_put_packet("E22");
1003 if (get_param(params
, 1)->thread_id
.kind
!= GDB_ONE_THREAD
) {
1004 gdb_put_packet("OK");
1008 cpu
= gdb_get_cpu(get_param(params
, 1)->thread_id
.pid
,
1009 get_param(params
, 1)->thread_id
.tid
);
1011 gdb_put_packet("E22");
1016 * Note: This command is deprecated and modern gdb's will be using the
1017 * vCont command instead.
1019 switch (get_param(params
, 0)->opcode
) {
1021 gdbserver_state
.c_cpu
= cpu
;
1022 gdb_put_packet("OK");
1025 gdbserver_state
.g_cpu
= cpu
;
1026 gdb_put_packet("OK");
1029 gdb_put_packet("E22");
1034 static void handle_insert_bp(GArray
*params
, void *user_ctx
)
1038 if (params
->len
!= 3) {
1039 gdb_put_packet("E22");
1043 res
= gdb_breakpoint_insert(gdbserver_state
.c_cpu
,
1044 get_param(params
, 0)->val_ul
,
1045 get_param(params
, 1)->val_ull
,
1046 get_param(params
, 2)->val_ull
);
1048 gdb_put_packet("OK");
1050 } else if (res
== -ENOSYS
) {
1055 gdb_put_packet("E22");
1058 static void handle_remove_bp(GArray
*params
, void *user_ctx
)
1062 if (params
->len
!= 3) {
1063 gdb_put_packet("E22");
1067 res
= gdb_breakpoint_remove(gdbserver_state
.c_cpu
,
1068 get_param(params
, 0)->val_ul
,
1069 get_param(params
, 1)->val_ull
,
1070 get_param(params
, 2)->val_ull
);
1072 gdb_put_packet("OK");
1074 } else if (res
== -ENOSYS
) {
1079 gdb_put_packet("E22");
1083 * handle_set/get_reg
1085 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1086 * This works, but can be very slow. Anything new enough to understand
1087 * XML also knows how to use this properly. However to use this we
1088 * need to define a local XML file as well as be talking to a
1089 * reasonably modern gdb. Responding with an empty packet will cause
1090 * the remote gdb to fallback to older methods.
1093 static void handle_set_reg(GArray
*params
, void *user_ctx
)
1097 if (params
->len
!= 2) {
1098 gdb_put_packet("E22");
1102 reg_size
= strlen(get_param(params
, 1)->data
) / 2;
1103 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 1)->data
, reg_size
);
1104 gdb_write_register(gdbserver_state
.g_cpu
, gdbserver_state
.mem_buf
->data
,
1105 get_param(params
, 0)->val_ull
);
1106 gdb_put_packet("OK");
1109 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
) {
1575 p
= get_param(params
, 0)->data
;
1576 xml
= get_feature_xml(p
, &p
, process
);
1578 gdb_put_packet("E00");
1582 addr
= get_param(params
, 1)->val_ul
;
1583 len
= get_param(params
, 2)->val_ul
;
1584 total_len
= strlen(xml
);
1585 if (addr
> total_len
) {
1586 gdb_put_packet("E00");
1590 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
1591 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1594 if (len
< total_len
- addr
) {
1595 g_string_assign(gdbserver_state
.str_buf
, "m");
1596 gdb_memtox(gdbserver_state
.str_buf
, xml
+ addr
, len
);
1598 g_string_assign(gdbserver_state
.str_buf
, "l");
1599 gdb_memtox(gdbserver_state
.str_buf
, xml
+ addr
, total_len
- addr
);
1602 gdb_put_packet_binary(gdbserver_state
.str_buf
->str
,
1603 gdbserver_state
.str_buf
->len
, true);
1606 static void handle_query_qemu_supported(GArray
*params
, void *user_ctx
)
1608 g_string_printf(gdbserver_state
.str_buf
, "sstepbits;sstep");
1609 #ifndef CONFIG_USER_ONLY
1610 g_string_append(gdbserver_state
.str_buf
, ";PhyMemMode");
1615 static const GdbCmdParseEntry gdb_gen_query_set_common_table
[] = {
1616 /* Order is important if has same prefix */
1618 .handler
= handle_query_qemu_sstepbits
,
1619 .cmd
= "qemu.sstepbits",
1622 .handler
= handle_query_qemu_sstep
,
1623 .cmd
= "qemu.sstep",
1626 .handler
= handle_set_qemu_sstep
,
1627 .cmd
= "qemu.sstep=",
1628 .cmd_startswith
= 1,
1633 static const GdbCmdParseEntry gdb_gen_query_table
[] = {
1635 .handler
= handle_query_curr_tid
,
1639 .handler
= handle_query_threads
,
1640 .cmd
= "sThreadInfo",
1643 .handler
= handle_query_first_threads
,
1644 .cmd
= "fThreadInfo",
1647 .handler
= handle_query_thread_extra
,
1648 .cmd
= "ThreadExtraInfo,",
1649 .cmd_startswith
= 1,
1652 #ifdef CONFIG_USER_ONLY
1654 .handler
= gdb_handle_query_offsets
,
1659 .handler
= gdb_handle_query_rcmd
,
1661 .cmd_startswith
= 1,
1666 .handler
= handle_query_supported
,
1667 .cmd
= "Supported:",
1668 .cmd_startswith
= 1,
1672 .handler
= handle_query_supported
,
1677 .handler
= handle_query_xfer_features
,
1678 .cmd
= "Xfer:features:read:",
1679 .cmd_startswith
= 1,
1682 #if defined(CONFIG_USER_ONLY)
1683 #if defined(CONFIG_LINUX)
1685 .handler
= gdb_handle_query_xfer_auxv
,
1686 .cmd
= "Xfer:auxv:read::",
1687 .cmd_startswith
= 1,
1692 .handler
= gdb_handle_query_xfer_exec_file
,
1693 .cmd
= "Xfer:exec-file:read:",
1694 .cmd_startswith
= 1,
1699 .handler
= gdb_handle_query_attached
,
1704 .handler
= gdb_handle_query_attached
,
1708 .handler
= handle_query_qemu_supported
,
1709 .cmd
= "qemu.Supported",
1711 #ifndef CONFIG_USER_ONLY
1713 .handler
= gdb_handle_query_qemu_phy_mem_mode
,
1714 .cmd
= "qemu.PhyMemMode",
1719 static const GdbCmdParseEntry gdb_gen_set_table
[] = {
1720 /* Order is important if has same prefix */
1722 .handler
= handle_set_qemu_sstep
,
1723 .cmd
= "qemu.sstep:",
1724 .cmd_startswith
= 1,
1727 #ifndef CONFIG_USER_ONLY
1729 .handler
= gdb_handle_set_qemu_phy_mem_mode
,
1730 .cmd
= "qemu.PhyMemMode:",
1731 .cmd_startswith
= 1,
1737 static void handle_gen_query(GArray
*params
, void *user_ctx
)
1743 if (!process_string_cmd(get_param(params
, 0)->data
,
1744 gdb_gen_query_set_common_table
,
1745 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
1749 if (process_string_cmd(get_param(params
, 0)->data
,
1750 gdb_gen_query_table
,
1751 ARRAY_SIZE(gdb_gen_query_table
))) {
1756 static void handle_gen_set(GArray
*params
, void *user_ctx
)
1762 if (!process_string_cmd(get_param(params
, 0)->data
,
1763 gdb_gen_query_set_common_table
,
1764 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
1768 if (process_string_cmd(get_param(params
, 0)->data
,
1770 ARRAY_SIZE(gdb_gen_set_table
))) {
1775 static void handle_target_halt(GArray
*params
, void *user_ctx
)
1777 if (gdbserver_state
.allow_stop_reply
) {
1778 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
1779 gdb_append_thread_id(gdbserver_state
.c_cpu
, gdbserver_state
.str_buf
);
1780 g_string_append_c(gdbserver_state
.str_buf
, ';');
1782 gdbserver_state
.allow_stop_reply
= false;
1785 * Remove all the breakpoints when this query is issued,
1786 * because gdb is doing an initial connect and the state
1787 * should be cleaned up.
1789 gdb_breakpoint_remove_all(gdbserver_state
.c_cpu
);
1792 static int gdb_handle_packet(const char *line_buf
)
1794 const GdbCmdParseEntry
*cmd_parser
= NULL
;
1796 trace_gdbstub_io_command(line_buf
);
1798 switch (line_buf
[0]) {
1800 gdb_put_packet("OK");
1804 static const GdbCmdParseEntry target_halted_cmd_desc
= {
1805 .handler
= handle_target_halt
,
1807 .cmd_startswith
= 1,
1808 .allow_stop_reply
= true,
1810 cmd_parser
= &target_halted_cmd_desc
;
1815 static const GdbCmdParseEntry continue_cmd_desc
= {
1816 .handler
= handle_continue
,
1818 .cmd_startswith
= 1,
1819 .allow_stop_reply
= true,
1822 cmd_parser
= &continue_cmd_desc
;
1827 static const GdbCmdParseEntry cont_with_sig_cmd_desc
= {
1828 .handler
= handle_cont_with_sig
,
1830 .cmd_startswith
= 1,
1831 .allow_stop_reply
= true,
1834 cmd_parser
= &cont_with_sig_cmd_desc
;
1839 static const GdbCmdParseEntry v_cmd_desc
= {
1840 .handler
= handle_v_commands
,
1842 .cmd_startswith
= 1,
1845 cmd_parser
= &v_cmd_desc
;
1849 /* Kill the target */
1850 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
;