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 "accel/tcg/vcpu-state.h"
36 #include "gdbstub/user.h"
38 #include "hw/cpu/cluster.h"
39 #include "hw/boards.h"
41 #include "hw/core/cpu.h"
43 #include "sysemu/hw_accel.h"
44 #include "sysemu/runstate.h"
45 #include "exec/replay-core.h"
46 #include "exec/hwaddr.h"
48 #include "internals.h"
50 typedef struct GDBRegisterState
{
52 gdb_get_reg_cb get_reg
;
53 gdb_set_reg_cb set_reg
;
54 const GDBFeature
*feature
;
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 static const char *get_feature_xml(const char *p
, const char **newp
,
355 CPUState
*cpu
= gdb_get_first_cpu_in_process(process
);
356 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
361 * qXfer:features:read:ANNEX:OFFSET,LENGTH'
364 char *term
= strchr(p
, ':');
368 /* Is it the main target xml? */
369 if (strncmp(p
, "target.xml", len
) == 0) {
370 if (!process
->target_xml
) {
371 g_autoptr(GPtrArray
) xml
= g_ptr_array_new_with_free_func(g_free
);
375 g_strdup("<?xml version=\"1.0\"?>"
376 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
379 if (cc
->gdb_arch_name
) {
382 g_markup_printf_escaped("<architecture>%s</architecture>",
383 cc
->gdb_arch_name(cpu
)));
385 for (guint i
= 0; i
< cpu
->gdb_regs
->len
; i
++) {
386 r
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
389 g_markup_printf_escaped("<xi:include href=\"%s\"/>",
390 r
->feature
->xmlname
));
392 g_ptr_array_add(xml
, g_strdup("</target>"));
393 g_ptr_array_add(xml
, NULL
);
395 process
->target_xml
= g_strjoinv(NULL
, (void *)xml
->pdata
);
397 return process
->target_xml
;
399 /* Is it one of the features? */
400 for (guint i
= 0; i
< cpu
->gdb_regs
->len
; i
++) {
401 r
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
402 if (strncmp(p
, r
->feature
->xmlname
, len
) == 0) {
403 return r
->feature
->xml
;
411 void gdb_feature_builder_init(GDBFeatureBuilder
*builder
, GDBFeature
*feature
,
412 const char *name
, const char *xmlname
,
415 char *header
= g_markup_printf_escaped(
416 "<?xml version=\"1.0\"?>"
417 "<!DOCTYPE feature SYSTEM \"gdb-target.dtd\">"
418 "<feature name=\"%s\">",
421 builder
->feature
= feature
;
422 builder
->xml
= g_ptr_array_new();
423 g_ptr_array_add(builder
->xml
, header
);
424 builder
->regs
= g_ptr_array_new();
425 builder
->base_reg
= base_reg
;
426 feature
->xmlname
= xmlname
;
427 feature
->name
= name
;
430 void gdb_feature_builder_append_tag(const GDBFeatureBuilder
*builder
,
431 const char *format
, ...)
434 va_start(ap
, format
);
435 g_ptr_array_add(builder
->xml
, g_markup_vprintf_escaped(format
, ap
));
439 void gdb_feature_builder_append_reg(const GDBFeatureBuilder
*builder
,
446 if (builder
->regs
->len
<= regnum
) {
447 g_ptr_array_set_size(builder
->regs
, regnum
+ 1);
450 builder
->regs
->pdata
[regnum
] = (gpointer
*)name
;
453 gdb_feature_builder_append_tag(
455 "<reg name=\"%s\" bitsize=\"%d\" regnum=\"%d\" type=\"%s\" group=\"%s\"/>",
456 name
, bitsize
, builder
->base_reg
+ regnum
, type
, group
);
458 gdb_feature_builder_append_tag(
460 "<reg name=\"%s\" bitsize=\"%d\" regnum=\"%d\" type=\"%s\"/>",
461 name
, bitsize
, builder
->base_reg
+ regnum
, type
);
465 void gdb_feature_builder_end(const GDBFeatureBuilder
*builder
)
467 g_ptr_array_add(builder
->xml
, (void *)"</feature>");
468 g_ptr_array_add(builder
->xml
, NULL
);
470 builder
->feature
->xml
= g_strjoinv(NULL
, (void *)builder
->xml
->pdata
);
472 for (guint i
= 0; i
< builder
->xml
->len
- 2; i
++) {
473 g_free(g_ptr_array_index(builder
->xml
, i
));
476 g_ptr_array_free(builder
->xml
, TRUE
);
478 builder
->feature
->num_regs
= builder
->regs
->len
;
479 builder
->feature
->regs
= (void *)g_ptr_array_free(builder
->regs
, FALSE
);
482 const GDBFeature
*gdb_find_static_feature(const char *xmlname
)
484 const GDBFeature
*feature
;
486 for (feature
= gdb_static_features
; feature
->xmlname
; feature
++) {
487 if (!strcmp(feature
->xmlname
, xmlname
)) {
492 g_assert_not_reached();
495 GArray
*gdb_get_register_list(CPUState
*cpu
)
497 GArray
*results
= g_array_new(true, true, sizeof(GDBRegDesc
));
499 /* registers are only available once the CPU is initialised */
500 if (!cpu
->gdb_regs
) {
504 for (int f
= 0; f
< cpu
->gdb_regs
->len
; f
++) {
505 GDBRegisterState
*r
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, f
);
506 for (int i
= 0; i
< r
->feature
->num_regs
; i
++) {
507 const char *name
= r
->feature
->regs
[i
];
513 g_array_append_val(results
, desc
);
520 int gdb_read_register(CPUState
*cpu
, GByteArray
*buf
, int reg
)
522 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
525 if (reg
< cc
->gdb_num_core_regs
) {
526 return cc
->gdb_read_register(cpu
, buf
, reg
);
529 for (guint i
= 0; i
< cpu
->gdb_regs
->len
; i
++) {
530 r
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
531 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->feature
->num_regs
) {
532 return r
->get_reg(cpu
, buf
, reg
- r
->base_reg
);
538 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
540 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
543 if (reg
< cc
->gdb_num_core_regs
) {
544 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
547 for (guint i
= 0; i
< cpu
->gdb_regs
->len
; i
++) {
548 r
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
549 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->feature
->num_regs
) {
550 return r
->set_reg(cpu
, mem_buf
, reg
- r
->base_reg
);
556 static void gdb_register_feature(CPUState
*cpu
, int base_reg
,
557 gdb_get_reg_cb get_reg
, gdb_set_reg_cb set_reg
,
558 const GDBFeature
*feature
)
560 GDBRegisterState s
= {
561 .base_reg
= base_reg
,
567 g_array_append_val(cpu
->gdb_regs
, s
);
570 void gdb_init_cpu(CPUState
*cpu
)
572 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
573 const GDBFeature
*feature
;
575 cpu
->gdb_regs
= g_array_new(false, false, sizeof(GDBRegisterState
));
577 if (cc
->gdb_core_xml_file
) {
578 feature
= gdb_find_static_feature(cc
->gdb_core_xml_file
);
579 gdb_register_feature(cpu
, 0,
580 cc
->gdb_read_register
, cc
->gdb_write_register
,
582 cpu
->gdb_num_regs
= cpu
->gdb_num_g_regs
= feature
->num_regs
;
585 if (cc
->gdb_num_core_regs
) {
586 cpu
->gdb_num_regs
= cpu
->gdb_num_g_regs
= cc
->gdb_num_core_regs
;
590 void gdb_register_coprocessor(CPUState
*cpu
,
591 gdb_get_reg_cb get_reg
, gdb_set_reg_cb set_reg
,
592 const GDBFeature
*feature
, int g_pos
)
596 int base_reg
= cpu
->gdb_num_regs
;
598 for (i
= 0; i
< cpu
->gdb_regs
->len
; i
++) {
599 /* Check for duplicates. */
600 s
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
601 if (s
->feature
== feature
) {
606 gdb_register_feature(cpu
, base_reg
, get_reg
, set_reg
, feature
);
608 /* Add to end of list. */
609 cpu
->gdb_num_regs
+= feature
->num_regs
;
611 if (g_pos
!= base_reg
) {
612 error_report("Error: Bad gdb register numbering for '%s', "
613 "expected %d got %d", feature
->xml
, g_pos
, base_reg
);
615 cpu
->gdb_num_g_regs
= cpu
->gdb_num_regs
;
620 static void gdb_process_breakpoint_remove_all(GDBProcess
*p
)
622 CPUState
*cpu
= gdb_get_first_cpu_in_process(p
);
625 gdb_breakpoint_remove_all(cpu
);
626 cpu
= gdb_next_cpu_in_process(cpu
);
631 static void gdb_set_cpu_pc(vaddr pc
)
633 CPUState
*cpu
= gdbserver_state
.c_cpu
;
635 cpu_synchronize_state(cpu
);
639 void gdb_append_thread_id(CPUState
*cpu
, GString
*buf
)
641 if (gdbserver_state
.multiprocess
) {
642 g_string_append_printf(buf
, "p%02x.%02x",
643 gdb_get_cpu_pid(cpu
), gdb_get_cpu_index(cpu
));
645 g_string_append_printf(buf
, "%02x", gdb_get_cpu_index(cpu
));
649 static GDBThreadIdKind
read_thread_id(const char *buf
, const char **end_buf
,
650 uint32_t *pid
, uint32_t *tid
)
657 ret
= qemu_strtoul(buf
, &buf
, 16, &p
);
660 return GDB_READ_THREAD_ERR
;
669 ret
= qemu_strtoul(buf
, &buf
, 16, &t
);
672 return GDB_READ_THREAD_ERR
;
678 return GDB_ALL_PROCESSES
;
686 return GDB_ALL_THREADS
;
693 return GDB_ONE_THREAD
;
697 * gdb_handle_vcont - Parses and handles a vCont packet.
698 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
699 * a format error, 0 on success.
701 static int gdb_handle_vcont(const char *p
)
709 GDBThreadIdKind kind
;
710 unsigned int max_cpus
= gdb_get_max_cpus();
711 /* uninitialised CPUs stay 0 */
712 g_autofree
char *newstates
= g_new0(char, max_cpus
);
714 /* mark valid CPUs with 1 */
716 newstates
[cpu
->cpu_index
] = 1;
720 * res keeps track of what error we are returning, with -ENOTSUP meaning
721 * that the command is unknown or unsupported, thus returning an empty
722 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
723 * or incorrect parameters passed.
728 * target_count and last_target keep track of how many CPUs we are going to
729 * step or resume, and a pointer to the state structure of one of them,
732 int target_count
= 0;
733 CPUState
*last_target
= NULL
;
741 if (cur_action
== 'C' || cur_action
== 'S') {
742 cur_action
= qemu_tolower(cur_action
);
743 res
= qemu_strtoul(p
, &p
, 16, &tmp
);
747 signal
= gdb_signal_to_target(tmp
);
748 } else if (cur_action
!= 'c' && cur_action
!= 's') {
749 /* unknown/invalid/unsupported command */
753 if (*p
== '\0' || *p
== ';') {
755 * No thread specifier, action is on "all threads". The
756 * specification is unclear regarding the process to act on. We
757 * choose all processes.
759 kind
= GDB_ALL_PROCESSES
;
760 } else if (*p
++ == ':') {
761 kind
= read_thread_id(p
, &p
, &pid
, &tid
);
767 case GDB_READ_THREAD_ERR
:
770 case GDB_ALL_PROCESSES
:
771 cpu
= gdb_first_attached_cpu();
773 if (newstates
[cpu
->cpu_index
] == 1) {
774 newstates
[cpu
->cpu_index
] = cur_action
;
780 cpu
= gdb_next_attached_cpu(cpu
);
784 case GDB_ALL_THREADS
:
785 process
= gdb_get_process(pid
);
787 if (!process
->attached
) {
791 cpu
= gdb_get_first_cpu_in_process(process
);
793 if (newstates
[cpu
->cpu_index
] == 1) {
794 newstates
[cpu
->cpu_index
] = cur_action
;
800 cpu
= gdb_next_cpu_in_process(cpu
);
805 cpu
= gdb_get_cpu(pid
, tid
);
807 /* invalid CPU/thread specified */
812 /* only use if no previous match occourred */
813 if (newstates
[cpu
->cpu_index
] == 1) {
814 newstates
[cpu
->cpu_index
] = cur_action
;
824 * if we're about to resume a specific set of CPUs/threads, make it so that
825 * in case execution gets interrupted, we can send GDB a stop reply with a
826 * correct value. it doesn't really matter which CPU we tell GDB the signal
827 * happened in (VM pauses stop all of them anyway), so long as it is one of
828 * the ones we resumed/single stepped here.
830 if (target_count
> 0) {
831 gdbserver_state
.c_cpu
= last_target
;
834 gdbserver_state
.signal
= signal
;
835 gdb_continue_partial(newstates
);
839 static const char *cmd_next_param(const char *param
, const char delimiter
)
841 static const char all_delimiters
[] = ",;:=";
842 char curr_delimiters
[2] = {0};
843 const char *delimiters
;
845 if (delimiter
== '?') {
846 delimiters
= all_delimiters
;
847 } else if (delimiter
== '0') {
848 return strchr(param
, '\0');
849 } else if (delimiter
== '.' && *param
) {
852 curr_delimiters
[0] = delimiter
;
853 delimiters
= curr_delimiters
;
856 param
+= strcspn(param
, delimiters
);
863 static int cmd_parse_params(const char *data
, const char *schema
,
866 const char *curr_schema
, *curr_data
;
869 g_assert(params
->len
== 0);
871 curr_schema
= schema
;
873 while (curr_schema
[0] && curr_schema
[1] && *curr_data
) {
874 GdbCmdVariant this_param
;
876 switch (curr_schema
[0]) {
878 if (qemu_strtoul(curr_data
, &curr_data
, 16,
879 &this_param
.val_ul
)) {
882 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
883 g_array_append_val(params
, this_param
);
886 if (qemu_strtou64(curr_data
, &curr_data
, 16,
887 (uint64_t *)&this_param
.val_ull
)) {
890 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
891 g_array_append_val(params
, this_param
);
894 this_param
.data
= curr_data
;
895 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
896 g_array_append_val(params
, this_param
);
899 this_param
.opcode
= *(uint8_t *)curr_data
;
900 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
901 g_array_append_val(params
, this_param
);
904 this_param
.thread_id
.kind
=
905 read_thread_id(curr_data
, &curr_data
,
906 &this_param
.thread_id
.pid
,
907 &this_param
.thread_id
.tid
);
908 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
909 g_array_append_val(params
, this_param
);
912 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
923 typedef void (*GdbCmdHandler
)(GArray
*params
, void *user_ctx
);
926 * cmd_startswith -> cmd is compared using startswith
928 * allow_stop_reply -> true iff the gdbstub can respond to this command with a
929 * "stop reply" packet. The list of commands that accept such response is
930 * defined at the GDB Remote Serial Protocol documentation. see:
931 * https://sourceware.org/gdb/onlinedocs/gdb/Stop-Reply-Packets.html#Stop-Reply-Packets.
933 * schema definitions:
934 * Each schema parameter entry consists of 2 chars,
935 * the first char represents the parameter type handling
936 * the second char represents the delimiter for the next parameter
938 * Currently supported schema types:
939 * 'l' -> unsigned long (stored in .val_ul)
940 * 'L' -> unsigned long long (stored in .val_ull)
941 * 's' -> string (stored in .data)
942 * 'o' -> single char (stored in .opcode)
943 * 't' -> thread id (stored in .thread_id)
944 * '?' -> skip according to delimiter
946 * Currently supported delimiters:
947 * '?' -> Stop at any delimiter (",;:=\0")
948 * '0' -> Stop at "\0"
949 * '.' -> Skip 1 char unless reached "\0"
950 * Any other value is treated as the delimiter value itself
952 typedef struct GdbCmdParseEntry
{
953 GdbCmdHandler handler
;
957 bool allow_stop_reply
;
960 static inline int startswith(const char *string
, const char *pattern
)
962 return !strncmp(string
, pattern
, strlen(pattern
));
965 static int process_string_cmd(const char *data
,
966 const GdbCmdParseEntry
*cmds
, int num_cmds
)
969 g_autoptr(GArray
) params
= g_array_new(false, true, sizeof(GdbCmdVariant
));
975 for (i
= 0; i
< num_cmds
; i
++) {
976 const GdbCmdParseEntry
*cmd
= &cmds
[i
];
977 g_assert(cmd
->handler
&& cmd
->cmd
);
979 if ((cmd
->cmd_startswith
&& !startswith(data
, cmd
->cmd
)) ||
980 (!cmd
->cmd_startswith
&& strcmp(cmd
->cmd
, data
))) {
985 if (cmd_parse_params(&data
[strlen(cmd
->cmd
)],
986 cmd
->schema
, params
)) {
991 gdbserver_state
.allow_stop_reply
= cmd
->allow_stop_reply
;
992 cmd
->handler(params
, NULL
);
999 static void run_cmd_parser(const char *data
, const GdbCmdParseEntry
*cmd
)
1005 g_string_set_size(gdbserver_state
.str_buf
, 0);
1006 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
1008 /* In case there was an error during the command parsing we must
1009 * send a NULL packet to indicate the command is not supported */
1010 if (process_string_cmd(data
, cmd
, 1)) {
1015 static void handle_detach(GArray
*params
, void *user_ctx
)
1017 GDBProcess
*process
;
1020 if (gdbserver_state
.multiprocess
) {
1022 gdb_put_packet("E22");
1026 pid
= get_param(params
, 0)->val_ul
;
1029 #ifdef CONFIG_USER_ONLY
1030 if (gdb_handle_detach_user(pid
)) {
1035 process
= gdb_get_process(pid
);
1036 gdb_process_breakpoint_remove_all(process
);
1037 process
->attached
= false;
1039 if (pid
== gdb_get_cpu_pid(gdbserver_state
.c_cpu
)) {
1040 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
1043 if (pid
== gdb_get_cpu_pid(gdbserver_state
.g_cpu
)) {
1044 gdbserver_state
.g_cpu
= gdb_first_attached_cpu();
1047 if (!gdbserver_state
.c_cpu
) {
1048 /* No more process attached */
1049 gdb_disable_syscalls();
1052 gdb_put_packet("OK");
1055 static void handle_thread_alive(GArray
*params
, void *user_ctx
)
1060 gdb_put_packet("E22");
1064 if (get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1065 gdb_put_packet("E22");
1069 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
1070 get_param(params
, 0)->thread_id
.tid
);
1072 gdb_put_packet("E22");
1076 gdb_put_packet("OK");
1079 static void handle_continue(GArray
*params
, void *user_ctx
)
1082 gdb_set_cpu_pc(get_param(params
, 0)->val_ull
);
1085 gdbserver_state
.signal
= 0;
1089 static void handle_cont_with_sig(GArray
*params
, void *user_ctx
)
1091 unsigned long signal
= 0;
1094 * Note: C sig;[addr] is currently unsupported and we simply
1095 * omit the addr parameter
1098 signal
= get_param(params
, 0)->val_ul
;
1101 gdbserver_state
.signal
= gdb_signal_to_target(signal
);
1102 if (gdbserver_state
.signal
== -1) {
1103 gdbserver_state
.signal
= 0;
1108 static void handle_set_thread(GArray
*params
, void *user_ctx
)
1113 if (params
->len
!= 2) {
1114 gdb_put_packet("E22");
1118 if (get_param(params
, 1)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1119 gdb_put_packet("E22");
1123 if (get_param(params
, 1)->thread_id
.kind
!= GDB_ONE_THREAD
) {
1124 gdb_put_packet("OK");
1128 pid
= get_param(params
, 1)->thread_id
.pid
;
1129 tid
= get_param(params
, 1)->thread_id
.tid
;
1130 #ifdef CONFIG_USER_ONLY
1131 if (gdb_handle_set_thread_user(pid
, tid
)) {
1135 cpu
= gdb_get_cpu(pid
, tid
);
1137 gdb_put_packet("E22");
1142 * Note: This command is deprecated and modern gdb's will be using the
1143 * vCont command instead.
1145 switch (get_param(params
, 0)->opcode
) {
1147 gdbserver_state
.c_cpu
= cpu
;
1148 gdb_put_packet("OK");
1151 gdbserver_state
.g_cpu
= cpu
;
1152 gdb_put_packet("OK");
1155 gdb_put_packet("E22");
1160 static void handle_insert_bp(GArray
*params
, void *user_ctx
)
1164 if (params
->len
!= 3) {
1165 gdb_put_packet("E22");
1169 res
= gdb_breakpoint_insert(gdbserver_state
.c_cpu
,
1170 get_param(params
, 0)->val_ul
,
1171 get_param(params
, 1)->val_ull
,
1172 get_param(params
, 2)->val_ull
);
1174 gdb_put_packet("OK");
1176 } else if (res
== -ENOSYS
) {
1181 gdb_put_packet("E22");
1184 static void handle_remove_bp(GArray
*params
, void *user_ctx
)
1188 if (params
->len
!= 3) {
1189 gdb_put_packet("E22");
1193 res
= gdb_breakpoint_remove(gdbserver_state
.c_cpu
,
1194 get_param(params
, 0)->val_ul
,
1195 get_param(params
, 1)->val_ull
,
1196 get_param(params
, 2)->val_ull
);
1198 gdb_put_packet("OK");
1200 } else if (res
== -ENOSYS
) {
1205 gdb_put_packet("E22");
1209 * handle_set/get_reg
1211 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1212 * This works, but can be very slow. Anything new enough to understand
1213 * XML also knows how to use this properly. However to use this we
1214 * need to define a local XML file as well as be talking to a
1215 * reasonably modern gdb. Responding with an empty packet will cause
1216 * the remote gdb to fallback to older methods.
1219 static void handle_set_reg(GArray
*params
, void *user_ctx
)
1223 if (params
->len
!= 2) {
1224 gdb_put_packet("E22");
1228 reg_size
= strlen(get_param(params
, 1)->data
) / 2;
1229 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 1)->data
, reg_size
);
1230 gdb_write_register(gdbserver_state
.g_cpu
, gdbserver_state
.mem_buf
->data
,
1231 get_param(params
, 0)->val_ull
);
1232 gdb_put_packet("OK");
1235 static void handle_get_reg(GArray
*params
, void *user_ctx
)
1240 gdb_put_packet("E14");
1244 reg_size
= gdb_read_register(gdbserver_state
.g_cpu
,
1245 gdbserver_state
.mem_buf
,
1246 get_param(params
, 0)->val_ull
);
1248 gdb_put_packet("E14");
1251 g_byte_array_set_size(gdbserver_state
.mem_buf
, reg_size
);
1254 gdb_memtohex(gdbserver_state
.str_buf
,
1255 gdbserver_state
.mem_buf
->data
, reg_size
);
1259 static void handle_write_mem(GArray
*params
, void *user_ctx
)
1261 if (params
->len
!= 3) {
1262 gdb_put_packet("E22");
1266 /* gdb_hextomem() reads 2*len bytes */
1267 if (get_param(params
, 1)->val_ull
>
1268 strlen(get_param(params
, 2)->data
) / 2) {
1269 gdb_put_packet("E22");
1273 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 2)->data
,
1274 get_param(params
, 1)->val_ull
);
1275 if (gdb_target_memory_rw_debug(gdbserver_state
.g_cpu
,
1276 get_param(params
, 0)->val_ull
,
1277 gdbserver_state
.mem_buf
->data
,
1278 gdbserver_state
.mem_buf
->len
, true)) {
1279 gdb_put_packet("E14");
1283 gdb_put_packet("OK");
1286 static void handle_read_mem(GArray
*params
, void *user_ctx
)
1288 if (params
->len
!= 2) {
1289 gdb_put_packet("E22");
1293 /* gdb_memtohex() doubles the required space */
1294 if (get_param(params
, 1)->val_ull
> MAX_PACKET_LENGTH
/ 2) {
1295 gdb_put_packet("E22");
1299 g_byte_array_set_size(gdbserver_state
.mem_buf
,
1300 get_param(params
, 1)->val_ull
);
1302 if (gdb_target_memory_rw_debug(gdbserver_state
.g_cpu
,
1303 get_param(params
, 0)->val_ull
,
1304 gdbserver_state
.mem_buf
->data
,
1305 gdbserver_state
.mem_buf
->len
, false)) {
1306 gdb_put_packet("E14");
1310 gdb_memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
,
1311 gdbserver_state
.mem_buf
->len
);
1315 static void handle_write_all_regs(GArray
*params
, void *user_ctx
)
1326 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1327 len
= strlen(get_param(params
, 0)->data
) / 2;
1328 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 0)->data
, len
);
1329 registers
= gdbserver_state
.mem_buf
->data
;
1331 reg_id
< gdbserver_state
.g_cpu
->gdb_num_g_regs
&& len
> 0;
1333 reg_size
= gdb_write_register(gdbserver_state
.g_cpu
, registers
, reg_id
);
1335 registers
+= reg_size
;
1337 gdb_put_packet("OK");
1340 static void handle_read_all_regs(GArray
*params
, void *user_ctx
)
1345 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1346 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
1348 for (reg_id
= 0; reg_id
< gdbserver_state
.g_cpu
->gdb_num_g_regs
; reg_id
++) {
1349 len
+= gdb_read_register(gdbserver_state
.g_cpu
,
1350 gdbserver_state
.mem_buf
,
1353 g_assert(len
== gdbserver_state
.mem_buf
->len
);
1355 gdb_memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
, len
);
1360 static void handle_step(GArray
*params
, void *user_ctx
)
1363 gdb_set_cpu_pc(get_param(params
, 0)->val_ull
);
1366 cpu_single_step(gdbserver_state
.c_cpu
, gdbserver_state
.sstep_flags
);
1370 static void handle_backward(GArray
*params
, void *user_ctx
)
1372 if (!gdb_can_reverse()) {
1373 gdb_put_packet("E22");
1375 if (params
->len
== 1) {
1376 switch (get_param(params
, 0)->opcode
) {
1378 if (replay_reverse_step()) {
1381 gdb_put_packet("E14");
1385 if (replay_reverse_continue()) {
1388 gdb_put_packet("E14");
1394 /* Default invalid command */
1398 static void handle_v_cont_query(GArray
*params
, void *user_ctx
)
1400 gdb_put_packet("vCont;c;C;s;S");
1403 static void handle_v_cont(GArray
*params
, void *user_ctx
)
1411 res
= gdb_handle_vcont(get_param(params
, 0)->data
);
1412 if ((res
== -EINVAL
) || (res
== -ERANGE
)) {
1413 gdb_put_packet("E22");
1419 static void handle_v_attach(GArray
*params
, void *user_ctx
)
1421 GDBProcess
*process
;
1424 g_string_assign(gdbserver_state
.str_buf
, "E22");
1429 process
= gdb_get_process(get_param(params
, 0)->val_ul
);
1434 cpu
= gdb_get_first_cpu_in_process(process
);
1439 process
->attached
= true;
1440 gdbserver_state
.g_cpu
= cpu
;
1441 gdbserver_state
.c_cpu
= cpu
;
1443 if (gdbserver_state
.allow_stop_reply
) {
1444 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
1445 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
1446 g_string_append_c(gdbserver_state
.str_buf
, ';');
1447 gdbserver_state
.allow_stop_reply
= false;
1453 static void handle_v_kill(GArray
*params
, void *user_ctx
)
1455 /* Kill the target */
1456 gdb_put_packet("OK");
1457 error_report("QEMU: Terminated via GDBstub");
1462 static const GdbCmdParseEntry gdb_v_commands_table
[] = {
1463 /* Order is important if has same prefix */
1465 .handler
= handle_v_cont_query
,
1470 .handler
= handle_v_cont
,
1472 .cmd_startswith
= 1,
1473 .allow_stop_reply
= true,
1477 .handler
= handle_v_attach
,
1479 .cmd_startswith
= 1,
1480 .allow_stop_reply
= true,
1484 .handler
= handle_v_kill
,
1488 #ifdef CONFIG_USER_ONLY
1490 * Host I/O Packets. See [1] for details.
1491 * [1] https://sourceware.org/gdb/onlinedocs/gdb/Host-I_002fO-Packets.html
1494 .handler
= gdb_handle_v_file_open
,
1495 .cmd
= "File:open:",
1496 .cmd_startswith
= 1,
1500 .handler
= gdb_handle_v_file_close
,
1501 .cmd
= "File:close:",
1502 .cmd_startswith
= 1,
1506 .handler
= gdb_handle_v_file_pread
,
1507 .cmd
= "File:pread:",
1508 .cmd_startswith
= 1,
1512 .handler
= gdb_handle_v_file_readlink
,
1513 .cmd
= "File:readlink:",
1514 .cmd_startswith
= 1,
1520 static void handle_v_commands(GArray
*params
, void *user_ctx
)
1526 if (process_string_cmd(get_param(params
, 0)->data
,
1527 gdb_v_commands_table
,
1528 ARRAY_SIZE(gdb_v_commands_table
))) {
1533 static void handle_query_qemu_sstepbits(GArray
*params
, void *user_ctx
)
1535 g_string_printf(gdbserver_state
.str_buf
, "ENABLE=%x", SSTEP_ENABLE
);
1537 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOIRQ
) {
1538 g_string_append_printf(gdbserver_state
.str_buf
, ",NOIRQ=%x",
1542 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOTIMER
) {
1543 g_string_append_printf(gdbserver_state
.str_buf
, ",NOTIMER=%x",
1550 static void handle_set_qemu_sstep(GArray
*params
, void *user_ctx
)
1552 int new_sstep_flags
;
1558 new_sstep_flags
= get_param(params
, 0)->val_ul
;
1560 if (new_sstep_flags
& ~gdbserver_state
.supported_sstep_flags
) {
1561 gdb_put_packet("E22");
1565 gdbserver_state
.sstep_flags
= new_sstep_flags
;
1566 gdb_put_packet("OK");
1569 static void handle_query_qemu_sstep(GArray
*params
, void *user_ctx
)
1571 g_string_printf(gdbserver_state
.str_buf
, "0x%x",
1572 gdbserver_state
.sstep_flags
);
1576 static void handle_query_curr_tid(GArray
*params
, void *user_ctx
)
1579 GDBProcess
*process
;
1582 * "Current thread" remains vague in the spec, so always return
1583 * the first thread of the current process (gdb returns the
1586 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
1587 cpu
= gdb_get_first_cpu_in_process(process
);
1588 g_string_assign(gdbserver_state
.str_buf
, "QC");
1589 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
1593 static void handle_query_threads(GArray
*params
, void *user_ctx
)
1595 if (!gdbserver_state
.query_cpu
) {
1596 gdb_put_packet("l");
1600 g_string_assign(gdbserver_state
.str_buf
, "m");
1601 gdb_append_thread_id(gdbserver_state
.query_cpu
, gdbserver_state
.str_buf
);
1603 gdbserver_state
.query_cpu
= gdb_next_attached_cpu(gdbserver_state
.query_cpu
);
1606 static void handle_query_first_threads(GArray
*params
, void *user_ctx
)
1608 gdbserver_state
.query_cpu
= gdb_first_attached_cpu();
1609 handle_query_threads(params
, user_ctx
);
1612 static void handle_query_thread_extra(GArray
*params
, void *user_ctx
)
1614 g_autoptr(GString
) rs
= g_string_new(NULL
);
1618 get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1619 gdb_put_packet("E22");
1623 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
1624 get_param(params
, 0)->thread_id
.tid
);
1629 cpu_synchronize_state(cpu
);
1631 if (gdbserver_state
.multiprocess
&& (gdbserver_state
.process_num
> 1)) {
1632 /* Print the CPU model and name in multiprocess mode */
1633 ObjectClass
*oc
= object_get_class(OBJECT(cpu
));
1634 const char *cpu_model
= object_class_get_name(oc
);
1635 const char *cpu_name
=
1636 object_get_canonical_path_component(OBJECT(cpu
));
1637 g_string_printf(rs
, "%s %s [%s]", cpu_model
, cpu_name
,
1638 cpu
->halted
? "halted " : "running");
1640 g_string_printf(rs
, "CPU#%d [%s]", cpu
->cpu_index
,
1641 cpu
->halted
? "halted " : "running");
1643 trace_gdbstub_op_extra_info(rs
->str
);
1644 gdb_memtohex(gdbserver_state
.str_buf
, (uint8_t *)rs
->str
, rs
->len
);
1648 static void handle_query_supported(GArray
*params
, void *user_ctx
)
1652 g_string_printf(gdbserver_state
.str_buf
, "PacketSize=%x", MAX_PACKET_LENGTH
);
1653 cc
= CPU_GET_CLASS(first_cpu
);
1654 if (cc
->gdb_core_xml_file
) {
1655 g_string_append(gdbserver_state
.str_buf
, ";qXfer:features:read+");
1658 if (gdb_can_reverse()) {
1659 g_string_append(gdbserver_state
.str_buf
,
1660 ";ReverseStep+;ReverseContinue+");
1663 #if defined(CONFIG_USER_ONLY)
1664 #if defined(CONFIG_LINUX)
1665 if (get_task_state(gdbserver_state
.c_cpu
)) {
1666 g_string_append(gdbserver_state
.str_buf
, ";qXfer:auxv:read+");
1668 g_string_append(gdbserver_state
.str_buf
, ";QCatchSyscalls+");
1670 g_string_append(gdbserver_state
.str_buf
, ";qXfer:siginfo:read+");
1672 g_string_append(gdbserver_state
.str_buf
, ";qXfer:exec-file:read+");
1676 const char *gdb_supported
= get_param(params
, 0)->data
;
1678 if (strstr(gdb_supported
, "multiprocess+")) {
1679 gdbserver_state
.multiprocess
= true;
1681 #if defined(CONFIG_USER_ONLY)
1682 gdb_handle_query_supported_user(gdb_supported
);
1686 g_string_append(gdbserver_state
.str_buf
, ";vContSupported+;multiprocess+");
1690 static void handle_query_xfer_features(GArray
*params
, void *user_ctx
)
1692 GDBProcess
*process
;
1694 unsigned long len
, total_len
, addr
;
1698 if (params
->len
< 3) {
1699 gdb_put_packet("E22");
1703 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
1704 cc
= CPU_GET_CLASS(gdbserver_state
.g_cpu
);
1705 if (!cc
->gdb_core_xml_file
) {
1710 p
= get_param(params
, 0)->data
;
1711 xml
= get_feature_xml(p
, &p
, process
);
1713 gdb_put_packet("E00");
1717 addr
= get_param(params
, 1)->val_ul
;
1718 len
= get_param(params
, 2)->val_ul
;
1719 total_len
= strlen(xml
);
1720 if (addr
> total_len
) {
1721 gdb_put_packet("E00");
1725 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
1726 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1729 if (len
< total_len
- addr
) {
1730 g_string_assign(gdbserver_state
.str_buf
, "m");
1731 gdb_memtox(gdbserver_state
.str_buf
, xml
+ addr
, len
);
1733 g_string_assign(gdbserver_state
.str_buf
, "l");
1734 gdb_memtox(gdbserver_state
.str_buf
, xml
+ addr
, total_len
- addr
);
1737 gdb_put_packet_binary(gdbserver_state
.str_buf
->str
,
1738 gdbserver_state
.str_buf
->len
, true);
1741 static void handle_query_qemu_supported(GArray
*params
, void *user_ctx
)
1743 g_string_printf(gdbserver_state
.str_buf
, "sstepbits;sstep");
1744 #ifndef CONFIG_USER_ONLY
1745 g_string_append(gdbserver_state
.str_buf
, ";PhyMemMode");
1750 static const GdbCmdParseEntry gdb_gen_query_set_common_table
[] = {
1751 /* Order is important if has same prefix */
1753 .handler
= handle_query_qemu_sstepbits
,
1754 .cmd
= "qemu.sstepbits",
1757 .handler
= handle_query_qemu_sstep
,
1758 .cmd
= "qemu.sstep",
1761 .handler
= handle_set_qemu_sstep
,
1762 .cmd
= "qemu.sstep=",
1763 .cmd_startswith
= 1,
1768 static const GdbCmdParseEntry gdb_gen_query_table
[] = {
1770 .handler
= handle_query_curr_tid
,
1774 .handler
= handle_query_threads
,
1775 .cmd
= "sThreadInfo",
1778 .handler
= handle_query_first_threads
,
1779 .cmd
= "fThreadInfo",
1782 .handler
= handle_query_thread_extra
,
1783 .cmd
= "ThreadExtraInfo,",
1784 .cmd_startswith
= 1,
1787 #ifdef CONFIG_USER_ONLY
1789 .handler
= gdb_handle_query_offsets
,
1794 .handler
= gdb_handle_query_rcmd
,
1796 .cmd_startswith
= 1,
1801 .handler
= handle_query_supported
,
1802 .cmd
= "Supported:",
1803 .cmd_startswith
= 1,
1807 .handler
= handle_query_supported
,
1812 .handler
= handle_query_xfer_features
,
1813 .cmd
= "Xfer:features:read:",
1814 .cmd_startswith
= 1,
1817 #if defined(CONFIG_USER_ONLY)
1818 #if defined(CONFIG_LINUX)
1820 .handler
= gdb_handle_query_xfer_auxv
,
1821 .cmd
= "Xfer:auxv:read::",
1822 .cmd_startswith
= 1,
1826 .handler
= gdb_handle_query_xfer_siginfo
,
1827 .cmd
= "Xfer:siginfo:read::",
1828 .cmd_startswith
= 1,
1833 .handler
= gdb_handle_query_xfer_exec_file
,
1834 .cmd
= "Xfer:exec-file:read:",
1835 .cmd_startswith
= 1,
1840 .handler
= gdb_handle_query_attached
,
1845 .handler
= gdb_handle_query_attached
,
1849 .handler
= handle_query_qemu_supported
,
1850 .cmd
= "qemu.Supported",
1852 #ifndef CONFIG_USER_ONLY
1854 .handler
= gdb_handle_query_qemu_phy_mem_mode
,
1855 .cmd
= "qemu.PhyMemMode",
1860 static const GdbCmdParseEntry gdb_gen_set_table
[] = {
1861 /* Order is important if has same prefix */
1863 .handler
= handle_set_qemu_sstep
,
1864 .cmd
= "qemu.sstep:",
1865 .cmd_startswith
= 1,
1868 #ifndef CONFIG_USER_ONLY
1870 .handler
= gdb_handle_set_qemu_phy_mem_mode
,
1871 .cmd
= "qemu.PhyMemMode:",
1872 .cmd_startswith
= 1,
1876 #if defined(CONFIG_USER_ONLY)
1878 .handler
= gdb_handle_set_catch_syscalls
,
1879 .cmd
= "CatchSyscalls:",
1880 .cmd_startswith
= 1,
1886 static void handle_gen_query(GArray
*params
, void *user_ctx
)
1892 if (!process_string_cmd(get_param(params
, 0)->data
,
1893 gdb_gen_query_set_common_table
,
1894 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
1898 if (process_string_cmd(get_param(params
, 0)->data
,
1899 gdb_gen_query_table
,
1900 ARRAY_SIZE(gdb_gen_query_table
))) {
1905 static void handle_gen_set(GArray
*params
, void *user_ctx
)
1911 if (!process_string_cmd(get_param(params
, 0)->data
,
1912 gdb_gen_query_set_common_table
,
1913 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
1917 if (process_string_cmd(get_param(params
, 0)->data
,
1919 ARRAY_SIZE(gdb_gen_set_table
))) {
1924 static void handle_target_halt(GArray
*params
, void *user_ctx
)
1926 if (gdbserver_state
.allow_stop_reply
) {
1927 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
1928 gdb_append_thread_id(gdbserver_state
.c_cpu
, gdbserver_state
.str_buf
);
1929 g_string_append_c(gdbserver_state
.str_buf
, ';');
1931 gdbserver_state
.allow_stop_reply
= false;
1934 * Remove all the breakpoints when this query is issued,
1935 * because gdb is doing an initial connect and the state
1936 * should be cleaned up.
1938 gdb_breakpoint_remove_all(gdbserver_state
.c_cpu
);
1941 static int gdb_handle_packet(const char *line_buf
)
1943 const GdbCmdParseEntry
*cmd_parser
= NULL
;
1945 trace_gdbstub_io_command(line_buf
);
1947 switch (line_buf
[0]) {
1949 gdb_put_packet("OK");
1953 static const GdbCmdParseEntry target_halted_cmd_desc
= {
1954 .handler
= handle_target_halt
,
1956 .cmd_startswith
= 1,
1957 .allow_stop_reply
= true,
1959 cmd_parser
= &target_halted_cmd_desc
;
1964 static const GdbCmdParseEntry continue_cmd_desc
= {
1965 .handler
= handle_continue
,
1967 .cmd_startswith
= 1,
1968 .allow_stop_reply
= true,
1971 cmd_parser
= &continue_cmd_desc
;
1976 static const GdbCmdParseEntry cont_with_sig_cmd_desc
= {
1977 .handler
= handle_cont_with_sig
,
1979 .cmd_startswith
= 1,
1980 .allow_stop_reply
= true,
1983 cmd_parser
= &cont_with_sig_cmd_desc
;
1988 static const GdbCmdParseEntry v_cmd_desc
= {
1989 .handler
= handle_v_commands
,
1991 .cmd_startswith
= 1,
1994 cmd_parser
= &v_cmd_desc
;
1998 /* Kill the target */
1999 error_report("QEMU: Terminated via GDBstub");
2005 static const GdbCmdParseEntry detach_cmd_desc
= {
2006 .handler
= handle_detach
,
2008 .cmd_startswith
= 1,
2011 cmd_parser
= &detach_cmd_desc
;
2016 static const GdbCmdParseEntry step_cmd_desc
= {
2017 .handler
= handle_step
,
2019 .cmd_startswith
= 1,
2020 .allow_stop_reply
= true,
2023 cmd_parser
= &step_cmd_desc
;
2028 static const GdbCmdParseEntry backward_cmd_desc
= {
2029 .handler
= handle_backward
,
2031 .cmd_startswith
= 1,
2032 .allow_stop_reply
= true,
2035 cmd_parser
= &backward_cmd_desc
;
2040 static const GdbCmdParseEntry file_io_cmd_desc
= {
2041 .handler
= gdb_handle_file_io
,
2043 .cmd_startswith
= 1,
2046 cmd_parser
= &file_io_cmd_desc
;
2051 static const GdbCmdParseEntry read_all_regs_cmd_desc
= {
2052 .handler
= handle_read_all_regs
,
2056 cmd_parser
= &read_all_regs_cmd_desc
;
2061 static const GdbCmdParseEntry write_all_regs_cmd_desc
= {
2062 .handler
= handle_write_all_regs
,
2064 .cmd_startswith
= 1,
2067 cmd_parser
= &write_all_regs_cmd_desc
;
2072 static const GdbCmdParseEntry read_mem_cmd_desc
= {
2073 .handler
= handle_read_mem
,
2075 .cmd_startswith
= 1,
2078 cmd_parser
= &read_mem_cmd_desc
;
2083 static const GdbCmdParseEntry write_mem_cmd_desc
= {
2084 .handler
= handle_write_mem
,
2086 .cmd_startswith
= 1,
2089 cmd_parser
= &write_mem_cmd_desc
;
2094 static const GdbCmdParseEntry get_reg_cmd_desc
= {
2095 .handler
= handle_get_reg
,
2097 .cmd_startswith
= 1,
2100 cmd_parser
= &get_reg_cmd_desc
;
2105 static const GdbCmdParseEntry set_reg_cmd_desc
= {
2106 .handler
= handle_set_reg
,
2108 .cmd_startswith
= 1,
2111 cmd_parser
= &set_reg_cmd_desc
;
2116 static const GdbCmdParseEntry insert_bp_cmd_desc
= {
2117 .handler
= handle_insert_bp
,
2119 .cmd_startswith
= 1,
2122 cmd_parser
= &insert_bp_cmd_desc
;
2127 static const GdbCmdParseEntry remove_bp_cmd_desc
= {
2128 .handler
= handle_remove_bp
,
2130 .cmd_startswith
= 1,
2133 cmd_parser
= &remove_bp_cmd_desc
;
2138 static const GdbCmdParseEntry set_thread_cmd_desc
= {
2139 .handler
= handle_set_thread
,
2141 .cmd_startswith
= 1,
2144 cmd_parser
= &set_thread_cmd_desc
;
2149 static const GdbCmdParseEntry thread_alive_cmd_desc
= {
2150 .handler
= handle_thread_alive
,
2152 .cmd_startswith
= 1,
2155 cmd_parser
= &thread_alive_cmd_desc
;
2160 static const GdbCmdParseEntry gen_query_cmd_desc
= {
2161 .handler
= handle_gen_query
,
2163 .cmd_startswith
= 1,
2166 cmd_parser
= &gen_query_cmd_desc
;
2171 static const GdbCmdParseEntry gen_set_cmd_desc
= {
2172 .handler
= handle_gen_set
,
2174 .cmd_startswith
= 1,
2177 cmd_parser
= &gen_set_cmd_desc
;
2181 /* put empty packet */
2187 run_cmd_parser(line_buf
, cmd_parser
);
2193 void gdb_set_stop_cpu(CPUState
*cpu
)
2195 GDBProcess
*p
= gdb_get_cpu_process(cpu
);
2199 * Having a stop CPU corresponding to a process that is not attached
2200 * confuses GDB. So we ignore the request.
2205 gdbserver_state
.c_cpu
= cpu
;
2206 gdbserver_state
.g_cpu
= cpu
;
2209 void gdb_read_byte(uint8_t ch
)
2213 gdbserver_state
.allow_stop_reply
= false;
2214 #ifndef CONFIG_USER_ONLY
2215 if (gdbserver_state
.last_packet
->len
) {
2216 /* Waiting for a response to the last packet. If we see the start
2217 of a new command then abandon the previous response. */
2219 trace_gdbstub_err_got_nack();
2220 gdb_put_buffer(gdbserver_state
.last_packet
->data
,
2221 gdbserver_state
.last_packet
->len
);
2222 } else if (ch
== '+') {
2223 trace_gdbstub_io_got_ack();
2225 trace_gdbstub_io_got_unexpected(ch
);
2228 if (ch
== '+' || ch
== '$') {
2229 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
2234 if (runstate_is_running()) {
2236 * When the CPU is running, we cannot do anything except stop
2237 * it when receiving a char. This is expected on a Ctrl-C in the
2238 * gdb client. Because we are in all-stop mode, gdb sends a
2239 * 0x03 byte which is not a usual packet, so we handle it specially
2240 * here, but it does expect a stop reply.
2243 trace_gdbstub_err_unexpected_runpkt(ch
);
2245 gdbserver_state
.allow_stop_reply
= true;
2247 vm_stop(RUN_STATE_PAUSED
);
2251 switch(gdbserver_state
.state
) {
2254 /* start of command packet */
2255 gdbserver_state
.line_buf_index
= 0;
2256 gdbserver_state
.line_sum
= 0;
2257 gdbserver_state
.state
= RS_GETLINE
;
2258 } else if (ch
== '+') {
2260 * do nothing, gdb may preemptively send out ACKs on
2261 * initial connection
2264 trace_gdbstub_err_garbage(ch
);
2269 /* start escape sequence */
2270 gdbserver_state
.state
= RS_GETLINE_ESC
;
2271 gdbserver_state
.line_sum
+= ch
;
2272 } else if (ch
== '*') {
2273 /* start run length encoding sequence */
2274 gdbserver_state
.state
= RS_GETLINE_RLE
;
2275 gdbserver_state
.line_sum
+= ch
;
2276 } else if (ch
== '#') {
2277 /* end of command, start of checksum*/
2278 gdbserver_state
.state
= RS_CHKSUM1
;
2279 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2280 trace_gdbstub_err_overrun();
2281 gdbserver_state
.state
= RS_IDLE
;
2283 /* unescaped command character */
2284 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
;
2285 gdbserver_state
.line_sum
+= ch
;
2288 case RS_GETLINE_ESC
:
2290 /* unexpected end of command in escape sequence */
2291 gdbserver_state
.state
= RS_CHKSUM1
;
2292 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2293 /* command buffer overrun */
2294 trace_gdbstub_err_overrun();
2295 gdbserver_state
.state
= RS_IDLE
;
2297 /* parse escaped character and leave escape state */
2298 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
^ 0x20;
2299 gdbserver_state
.line_sum
+= ch
;
2300 gdbserver_state
.state
= RS_GETLINE
;
2303 case RS_GETLINE_RLE
:
2305 * Run-length encoding is explained in "Debugging with GDB /
2306 * Appendix E GDB Remote Serial Protocol / Overview".
2308 if (ch
< ' ' || ch
== '#' || ch
== '$' || ch
> 126) {
2309 /* invalid RLE count encoding */
2310 trace_gdbstub_err_invalid_repeat(ch
);
2311 gdbserver_state
.state
= RS_GETLINE
;
2313 /* decode repeat length */
2314 int repeat
= ch
- ' ' + 3;
2315 if (gdbserver_state
.line_buf_index
+ repeat
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2316 /* that many repeats would overrun the command buffer */
2317 trace_gdbstub_err_overrun();
2318 gdbserver_state
.state
= RS_IDLE
;
2319 } else if (gdbserver_state
.line_buf_index
< 1) {
2320 /* got a repeat but we have nothing to repeat */
2321 trace_gdbstub_err_invalid_rle();
2322 gdbserver_state
.state
= RS_GETLINE
;
2324 /* repeat the last character */
2325 memset(gdbserver_state
.line_buf
+ gdbserver_state
.line_buf_index
,
2326 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
- 1], repeat
);
2327 gdbserver_state
.line_buf_index
+= repeat
;
2328 gdbserver_state
.line_sum
+= ch
;
2329 gdbserver_state
.state
= RS_GETLINE
;
2334 /* get high hex digit of checksum */
2335 if (!isxdigit(ch
)) {
2336 trace_gdbstub_err_checksum_invalid(ch
);
2337 gdbserver_state
.state
= RS_GETLINE
;
2340 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
] = '\0';
2341 gdbserver_state
.line_csum
= fromhex(ch
) << 4;
2342 gdbserver_state
.state
= RS_CHKSUM2
;
2345 /* get low hex digit of checksum */
2346 if (!isxdigit(ch
)) {
2347 trace_gdbstub_err_checksum_invalid(ch
);
2348 gdbserver_state
.state
= RS_GETLINE
;
2351 gdbserver_state
.line_csum
|= fromhex(ch
);
2353 if (gdbserver_state
.line_csum
!= (gdbserver_state
.line_sum
& 0xff)) {
2354 trace_gdbstub_err_checksum_incorrect(gdbserver_state
.line_sum
, gdbserver_state
.line_csum
);
2355 /* send NAK reply */
2357 gdb_put_buffer(&reply
, 1);
2358 gdbserver_state
.state
= RS_IDLE
;
2360 /* send ACK reply */
2362 gdb_put_buffer(&reply
, 1);
2363 gdbserver_state
.state
= gdb_handle_packet(gdbserver_state
.line_buf
);
2373 * Create the process that will contain all the "orphan" CPUs (that are not
2374 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2375 * be attachable and thus will be invisible to the user.
2377 void gdb_create_default_process(GDBState
*s
)
2379 GDBProcess
*process
;
2382 #ifdef CONFIG_USER_ONLY
2383 assert(gdbserver_state
.process_num
== 0);
2386 if (gdbserver_state
.process_num
) {
2387 pid
= s
->processes
[s
->process_num
- 1].pid
;
2391 /* We need an available PID slot for this process */
2392 assert(pid
< UINT32_MAX
);
2396 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2397 process
= &s
->processes
[s
->process_num
- 1];
2399 process
->attached
= false;
2400 process
->target_xml
= NULL
;