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-or-later
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/commands.h"
34 #include "gdbstub/syscalls.h"
35 #ifdef CONFIG_USER_ONLY
36 #include "accel/tcg/vcpu-state.h"
37 #include "gdbstub/user.h"
39 #include "hw/cpu/cluster.h"
40 #include "hw/boards.h"
42 #include "hw/core/cpu.h"
44 #include "sysemu/hw_accel.h"
45 #include "sysemu/runstate.h"
46 #include "exec/replay-core.h"
47 #include "exec/hwaddr.h"
49 #include "internals.h"
51 typedef struct GDBRegisterState
{
53 gdb_get_reg_cb get_reg
;
54 gdb_set_reg_cb set_reg
;
55 const GDBFeature
*feature
;
58 GDBState gdbserver_state
;
60 void gdb_init_gdbserver_state(void)
62 g_assert(!gdbserver_state
.init
);
63 memset(&gdbserver_state
, 0, sizeof(GDBState
));
64 gdbserver_state
.init
= true;
65 gdbserver_state
.str_buf
= g_string_new(NULL
);
66 gdbserver_state
.mem_buf
= g_byte_array_sized_new(MAX_PACKET_LENGTH
);
67 gdbserver_state
.last_packet
= g_byte_array_sized_new(MAX_PACKET_LENGTH
+ 4);
70 * What single-step modes are supported is accelerator dependent.
71 * By default try to use no IRQs and no timers while single
72 * stepping so as to make single stepping like a typical ICE HW step.
74 gdbserver_state
.supported_sstep_flags
= accel_supported_gdbstub_sstep_flags();
75 gdbserver_state
.sstep_flags
= SSTEP_ENABLE
| SSTEP_NOIRQ
| SSTEP_NOTIMER
;
76 gdbserver_state
.sstep_flags
&= gdbserver_state
.supported_sstep_flags
;
79 /* writes 2*len+1 bytes in buf */
80 void gdb_memtohex(GString
*buf
, const uint8_t *mem
, int len
)
83 for(i
= 0; i
< len
; i
++) {
85 g_string_append_c(buf
, tohex(c
>> 4));
86 g_string_append_c(buf
, tohex(c
& 0xf));
88 g_string_append_c(buf
, '\0');
91 void gdb_hextomem(GByteArray
*mem
, const char *buf
, int len
)
95 for(i
= 0; i
< len
; i
++) {
96 guint8 byte
= fromhex(buf
[0]) << 4 | fromhex(buf
[1]);
97 g_byte_array_append(mem
, &byte
, 1);
102 static void hexdump(const char *buf
, int len
,
103 void (*trace_fn
)(size_t ofs
, char const *text
))
105 char line_buffer
[3 * 16 + 4 + 16 + 1];
108 for (i
= 0; i
< len
|| (i
& 0xF); ++i
) {
109 size_t byte_ofs
= i
& 15;
112 memset(line_buffer
, ' ', 3 * 16 + 4 + 16);
113 line_buffer
[3 * 16 + 4 + 16] = 0;
116 size_t col_group
= (i
>> 2) & 3;
117 size_t hex_col
= byte_ofs
* 3 + col_group
;
118 size_t txt_col
= 3 * 16 + 4 + byte_ofs
;
123 line_buffer
[hex_col
+ 0] = tohex((value
>> 4) & 0xF);
124 line_buffer
[hex_col
+ 1] = tohex((value
>> 0) & 0xF);
125 line_buffer
[txt_col
+ 0] = (value
>= ' ' && value
< 127)
131 trace_fn(i
& -16, line_buffer
);
135 /* return -1 if error, 0 if OK */
136 int gdb_put_packet_binary(const char *buf
, int len
, bool dump
)
141 if (dump
&& trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY
)) {
142 hexdump(buf
, len
, trace_gdbstub_io_binaryreply
);
146 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
147 g_byte_array_append(gdbserver_state
.last_packet
,
148 (const uint8_t *) "$", 1);
149 g_byte_array_append(gdbserver_state
.last_packet
,
150 (const uint8_t *) buf
, len
);
152 for(i
= 0; i
< len
; i
++) {
156 footer
[1] = tohex((csum
>> 4) & 0xf);
157 footer
[2] = tohex((csum
) & 0xf);
158 g_byte_array_append(gdbserver_state
.last_packet
, footer
, 3);
160 gdb_put_buffer(gdbserver_state
.last_packet
->data
,
161 gdbserver_state
.last_packet
->len
);
163 if (gdb_got_immediate_ack()) {
170 /* return -1 if error, 0 if OK */
171 int gdb_put_packet(const char *buf
)
173 trace_gdbstub_io_reply(buf
);
175 return gdb_put_packet_binary(buf
, strlen(buf
), false);
178 void gdb_put_strbuf(void)
180 gdb_put_packet(gdbserver_state
.str_buf
->str
);
183 /* Encode data using the encoding for 'x' packets. */
184 void gdb_memtox(GString
*buf
, const char *mem
, int len
)
191 case '#': case '$': case '*': case '}':
192 g_string_append_c(buf
, '}');
193 g_string_append_c(buf
, c
^ 0x20);
196 g_string_append_c(buf
, c
);
202 static uint32_t gdb_get_cpu_pid(CPUState
*cpu
)
204 #ifdef CONFIG_USER_ONLY
207 if (cpu
->cluster_index
== UNASSIGNED_CLUSTER_INDEX
) {
208 /* Return the default process' PID */
209 int index
= gdbserver_state
.process_num
- 1;
210 return gdbserver_state
.processes
[index
].pid
;
212 return cpu
->cluster_index
+ 1;
216 GDBProcess
*gdb_get_process(uint32_t pid
)
221 /* 0 means any process, we take the first one */
222 return &gdbserver_state
.processes
[0];
225 for (i
= 0; i
< gdbserver_state
.process_num
; i
++) {
226 if (gdbserver_state
.processes
[i
].pid
== pid
) {
227 return &gdbserver_state
.processes
[i
];
234 static GDBProcess
*gdb_get_cpu_process(CPUState
*cpu
)
236 return gdb_get_process(gdb_get_cpu_pid(cpu
));
239 static CPUState
*find_cpu(uint32_t thread_id
)
244 if (gdb_get_cpu_index(cpu
) == thread_id
) {
252 CPUState
*gdb_get_first_cpu_in_process(GDBProcess
*process
)
257 if (gdb_get_cpu_pid(cpu
) == process
->pid
) {
265 static CPUState
*gdb_next_cpu_in_process(CPUState
*cpu
)
267 uint32_t pid
= gdb_get_cpu_pid(cpu
);
271 if (gdb_get_cpu_pid(cpu
) == pid
) {
281 /* Return the cpu following @cpu, while ignoring unattached processes. */
282 static CPUState
*gdb_next_attached_cpu(CPUState
*cpu
)
287 if (gdb_get_cpu_process(cpu
)->attached
) {
297 /* Return the first attached cpu */
298 CPUState
*gdb_first_attached_cpu(void)
300 CPUState
*cpu
= first_cpu
;
301 GDBProcess
*process
= gdb_get_cpu_process(cpu
);
303 if (!process
->attached
) {
304 return gdb_next_attached_cpu(cpu
);
310 static CPUState
*gdb_get_cpu(uint32_t pid
, uint32_t tid
)
316 /* 0 means any process/thread, we take the first attached one */
317 return gdb_first_attached_cpu();
318 } else if (pid
&& !tid
) {
319 /* any thread in a specific process */
320 process
= gdb_get_process(pid
);
322 if (process
== NULL
) {
326 if (!process
->attached
) {
330 return gdb_get_first_cpu_in_process(process
);
332 /* a specific thread */
339 process
= gdb_get_cpu_process(cpu
);
341 if (pid
&& process
->pid
!= pid
) {
345 if (!process
->attached
) {
353 static const char *get_feature_xml(const char *p
, const char **newp
,
356 CPUState
*cpu
= gdb_get_first_cpu_in_process(process
);
357 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
362 * qXfer:features:read:ANNEX:OFFSET,LENGTH'
365 char *term
= strchr(p
, ':');
369 /* Is it the main target xml? */
370 if (strncmp(p
, "target.xml", len
) == 0) {
371 if (!process
->target_xml
) {
372 g_autoptr(GPtrArray
) xml
= g_ptr_array_new_with_free_func(g_free
);
376 g_strdup("<?xml version=\"1.0\"?>"
377 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
380 if (cc
->gdb_arch_name
) {
383 g_markup_printf_escaped("<architecture>%s</architecture>",
384 cc
->gdb_arch_name(cpu
)));
386 for (guint i
= 0; i
< cpu
->gdb_regs
->len
; i
++) {
387 r
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
390 g_markup_printf_escaped("<xi:include href=\"%s\"/>",
391 r
->feature
->xmlname
));
393 g_ptr_array_add(xml
, g_strdup("</target>"));
394 g_ptr_array_add(xml
, NULL
);
396 process
->target_xml
= g_strjoinv(NULL
, (void *)xml
->pdata
);
398 return process
->target_xml
;
400 /* Is it one of the features? */
401 for (guint i
= 0; i
< cpu
->gdb_regs
->len
; i
++) {
402 r
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
403 if (strncmp(p
, r
->feature
->xmlname
, len
) == 0) {
404 return r
->feature
->xml
;
412 void gdb_feature_builder_init(GDBFeatureBuilder
*builder
, GDBFeature
*feature
,
413 const char *name
, const char *xmlname
,
416 char *header
= g_markup_printf_escaped(
417 "<?xml version=\"1.0\"?>"
418 "<!DOCTYPE feature SYSTEM \"gdb-target.dtd\">"
419 "<feature name=\"%s\">",
422 builder
->feature
= feature
;
423 builder
->xml
= g_ptr_array_new();
424 g_ptr_array_add(builder
->xml
, header
);
425 builder
->regs
= g_ptr_array_new();
426 builder
->base_reg
= base_reg
;
427 feature
->xmlname
= xmlname
;
428 feature
->name
= name
;
431 void gdb_feature_builder_append_tag(const GDBFeatureBuilder
*builder
,
432 const char *format
, ...)
435 va_start(ap
, format
);
436 g_ptr_array_add(builder
->xml
, g_markup_vprintf_escaped(format
, ap
));
440 void gdb_feature_builder_append_reg(const GDBFeatureBuilder
*builder
,
447 if (builder
->regs
->len
<= regnum
) {
448 g_ptr_array_set_size(builder
->regs
, regnum
+ 1);
451 builder
->regs
->pdata
[regnum
] = (gpointer
*)name
;
454 gdb_feature_builder_append_tag(
456 "<reg name=\"%s\" bitsize=\"%d\" regnum=\"%d\" type=\"%s\" group=\"%s\"/>",
457 name
, bitsize
, builder
->base_reg
+ regnum
, type
, group
);
459 gdb_feature_builder_append_tag(
461 "<reg name=\"%s\" bitsize=\"%d\" regnum=\"%d\" type=\"%s\"/>",
462 name
, bitsize
, builder
->base_reg
+ regnum
, type
);
466 void gdb_feature_builder_end(const GDBFeatureBuilder
*builder
)
468 g_ptr_array_add(builder
->xml
, (void *)"</feature>");
469 g_ptr_array_add(builder
->xml
, NULL
);
471 builder
->feature
->xml
= g_strjoinv(NULL
, (void *)builder
->xml
->pdata
);
473 for (guint i
= 0; i
< builder
->xml
->len
- 2; i
++) {
474 g_free(g_ptr_array_index(builder
->xml
, i
));
477 g_ptr_array_free(builder
->xml
, TRUE
);
479 builder
->feature
->num_regs
= builder
->regs
->len
;
480 builder
->feature
->regs
= (void *)g_ptr_array_free(builder
->regs
, FALSE
);
483 const GDBFeature
*gdb_find_static_feature(const char *xmlname
)
485 const GDBFeature
*feature
;
487 for (feature
= gdb_static_features
; feature
->xmlname
; feature
++) {
488 if (!strcmp(feature
->xmlname
, xmlname
)) {
493 g_assert_not_reached();
496 GArray
*gdb_get_register_list(CPUState
*cpu
)
498 GArray
*results
= g_array_new(true, true, sizeof(GDBRegDesc
));
500 /* registers are only available once the CPU is initialised */
501 if (!cpu
->gdb_regs
) {
505 for (int f
= 0; f
< cpu
->gdb_regs
->len
; f
++) {
506 GDBRegisterState
*r
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, f
);
507 for (int i
= 0; i
< r
->feature
->num_regs
; i
++) {
508 const char *name
= r
->feature
->regs
[i
];
514 g_array_append_val(results
, desc
);
521 int gdb_read_register(CPUState
*cpu
, GByteArray
*buf
, int reg
)
523 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
526 if (reg
< cc
->gdb_num_core_regs
) {
527 return cc
->gdb_read_register(cpu
, buf
, reg
);
530 for (guint i
= 0; i
< cpu
->gdb_regs
->len
; i
++) {
531 r
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
532 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->feature
->num_regs
) {
533 return r
->get_reg(cpu
, buf
, reg
- r
->base_reg
);
539 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
541 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
544 if (reg
< cc
->gdb_num_core_regs
) {
545 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
548 for (guint i
= 0; i
< cpu
->gdb_regs
->len
; i
++) {
549 r
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
550 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->feature
->num_regs
) {
551 return r
->set_reg(cpu
, mem_buf
, reg
- r
->base_reg
);
557 static void gdb_register_feature(CPUState
*cpu
, int base_reg
,
558 gdb_get_reg_cb get_reg
, gdb_set_reg_cb set_reg
,
559 const GDBFeature
*feature
)
561 GDBRegisterState s
= {
562 .base_reg
= base_reg
,
568 g_array_append_val(cpu
->gdb_regs
, s
);
571 void gdb_init_cpu(CPUState
*cpu
)
573 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
574 const GDBFeature
*feature
;
576 cpu
->gdb_regs
= g_array_new(false, false, sizeof(GDBRegisterState
));
578 if (cc
->gdb_core_xml_file
) {
579 feature
= gdb_find_static_feature(cc
->gdb_core_xml_file
);
580 gdb_register_feature(cpu
, 0,
581 cc
->gdb_read_register
, cc
->gdb_write_register
,
583 cpu
->gdb_num_regs
= cpu
->gdb_num_g_regs
= feature
->num_regs
;
586 if (cc
->gdb_num_core_regs
) {
587 cpu
->gdb_num_regs
= cpu
->gdb_num_g_regs
= cc
->gdb_num_core_regs
;
591 void gdb_register_coprocessor(CPUState
*cpu
,
592 gdb_get_reg_cb get_reg
, gdb_set_reg_cb set_reg
,
593 const GDBFeature
*feature
, int g_pos
)
597 int base_reg
= cpu
->gdb_num_regs
;
599 for (i
= 0; i
< cpu
->gdb_regs
->len
; i
++) {
600 /* Check for duplicates. */
601 s
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
602 if (s
->feature
== feature
) {
607 gdb_register_feature(cpu
, base_reg
, get_reg
, set_reg
, feature
);
609 /* Add to end of list. */
610 cpu
->gdb_num_regs
+= feature
->num_regs
;
612 if (g_pos
!= base_reg
) {
613 error_report("Error: Bad gdb register numbering for '%s', "
614 "expected %d got %d", feature
->xml
, g_pos
, base_reg
);
616 cpu
->gdb_num_g_regs
= cpu
->gdb_num_regs
;
621 void gdb_unregister_coprocessor_all(CPUState
*cpu
)
624 * Safe to nuke everything. GDBRegisterState::xml is static const char so
627 g_array_free(cpu
->gdb_regs
, true);
629 cpu
->gdb_regs
= NULL
;
630 cpu
->gdb_num_regs
= 0;
631 cpu
->gdb_num_g_regs
= 0;
634 static void gdb_process_breakpoint_remove_all(GDBProcess
*p
)
636 CPUState
*cpu
= gdb_get_first_cpu_in_process(p
);
639 gdb_breakpoint_remove_all(cpu
);
640 cpu
= gdb_next_cpu_in_process(cpu
);
645 static void gdb_set_cpu_pc(vaddr pc
)
647 CPUState
*cpu
= gdbserver_state
.c_cpu
;
649 cpu_synchronize_state(cpu
);
653 void gdb_append_thread_id(CPUState
*cpu
, GString
*buf
)
655 if (gdbserver_state
.multiprocess
) {
656 g_string_append_printf(buf
, "p%02x.%02x",
657 gdb_get_cpu_pid(cpu
), gdb_get_cpu_index(cpu
));
659 g_string_append_printf(buf
, "%02x", gdb_get_cpu_index(cpu
));
663 static GDBThreadIdKind
read_thread_id(const char *buf
, const char **end_buf
,
664 uint32_t *pid
, uint32_t *tid
)
671 ret
= qemu_strtoul(buf
, &buf
, 16, &p
);
674 return GDB_READ_THREAD_ERR
;
683 ret
= qemu_strtoul(buf
, &buf
, 16, &t
);
686 return GDB_READ_THREAD_ERR
;
692 return GDB_ALL_PROCESSES
;
700 return GDB_ALL_THREADS
;
707 return GDB_ONE_THREAD
;
711 * gdb_handle_vcont - Parses and handles a vCont packet.
712 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
713 * a format error, 0 on success.
715 static int gdb_handle_vcont(const char *p
)
723 GDBThreadIdKind kind
;
724 unsigned int max_cpus
= gdb_get_max_cpus();
725 /* uninitialised CPUs stay 0 */
726 g_autofree
char *newstates
= g_new0(char, max_cpus
);
728 /* mark valid CPUs with 1 */
730 newstates
[cpu
->cpu_index
] = 1;
734 * res keeps track of what error we are returning, with -ENOTSUP meaning
735 * that the command is unknown or unsupported, thus returning an empty
736 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
737 * or incorrect parameters passed.
742 * target_count and last_target keep track of how many CPUs we are going to
743 * step or resume, and a pointer to the state structure of one of them,
746 int target_count
= 0;
747 CPUState
*last_target
= NULL
;
755 if (cur_action
== 'C' || cur_action
== 'S') {
756 cur_action
= qemu_tolower(cur_action
);
757 res
= qemu_strtoul(p
, &p
, 16, &tmp
);
761 signal
= gdb_signal_to_target(tmp
);
762 } else if (cur_action
!= 'c' && cur_action
!= 's') {
763 /* unknown/invalid/unsupported command */
767 if (*p
== '\0' || *p
== ';') {
769 * No thread specifier, action is on "all threads". The
770 * specification is unclear regarding the process to act on. We
771 * choose all processes.
773 kind
= GDB_ALL_PROCESSES
;
774 } else if (*p
++ == ':') {
775 kind
= read_thread_id(p
, &p
, &pid
, &tid
);
781 case GDB_READ_THREAD_ERR
:
784 case GDB_ALL_PROCESSES
:
785 cpu
= gdb_first_attached_cpu();
787 if (newstates
[cpu
->cpu_index
] == 1) {
788 newstates
[cpu
->cpu_index
] = cur_action
;
794 cpu
= gdb_next_attached_cpu(cpu
);
798 case GDB_ALL_THREADS
:
799 process
= gdb_get_process(pid
);
801 if (!process
->attached
) {
805 cpu
= gdb_get_first_cpu_in_process(process
);
807 if (newstates
[cpu
->cpu_index
] == 1) {
808 newstates
[cpu
->cpu_index
] = cur_action
;
814 cpu
= gdb_next_cpu_in_process(cpu
);
819 cpu
= gdb_get_cpu(pid
, tid
);
821 /* invalid CPU/thread specified */
826 /* only use if no previous match occourred */
827 if (newstates
[cpu
->cpu_index
] == 1) {
828 newstates
[cpu
->cpu_index
] = cur_action
;
838 * if we're about to resume a specific set of CPUs/threads, make it so that
839 * in case execution gets interrupted, we can send GDB a stop reply with a
840 * correct value. it doesn't really matter which CPU we tell GDB the signal
841 * happened in (VM pauses stop all of them anyway), so long as it is one of
842 * the ones we resumed/single stepped here.
844 if (target_count
> 0) {
845 gdbserver_state
.c_cpu
= last_target
;
848 gdbserver_state
.signal
= signal
;
849 gdb_continue_partial(newstates
);
853 static const char *cmd_next_param(const char *param
, const char delimiter
)
855 static const char all_delimiters
[] = ",;:=";
856 char curr_delimiters
[2] = {0};
857 const char *delimiters
;
859 if (delimiter
== '?') {
860 delimiters
= all_delimiters
;
861 } else if (delimiter
== '0') {
862 return strchr(param
, '\0');
863 } else if (delimiter
== '.' && *param
) {
866 curr_delimiters
[0] = delimiter
;
867 delimiters
= curr_delimiters
;
870 param
+= strcspn(param
, delimiters
);
877 static int cmd_parse_params(const char *data
, const char *schema
,
880 const char *curr_schema
, *curr_data
;
883 g_assert(params
->len
== 0);
885 curr_schema
= schema
;
887 while (curr_schema
[0] && curr_schema
[1] && *curr_data
) {
888 GdbCmdVariant this_param
;
890 switch (curr_schema
[0]) {
892 if (qemu_strtoul(curr_data
, &curr_data
, 16,
893 &this_param
.val_ul
)) {
896 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
897 g_array_append_val(params
, this_param
);
900 if (qemu_strtou64(curr_data
, &curr_data
, 16,
901 (uint64_t *)&this_param
.val_ull
)) {
904 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
905 g_array_append_val(params
, this_param
);
908 this_param
.data
= curr_data
;
909 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
910 g_array_append_val(params
, this_param
);
913 this_param
.opcode
= *(uint8_t *)curr_data
;
914 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
915 g_array_append_val(params
, this_param
);
918 this_param
.thread_id
.kind
=
919 read_thread_id(curr_data
, &curr_data
,
920 &this_param
.thread_id
.pid
,
921 &this_param
.thread_id
.tid
);
922 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
923 g_array_append_val(params
, this_param
);
926 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
937 static inline int startswith(const char *string
, const char *pattern
)
939 return !strncmp(string
, pattern
, strlen(pattern
));
942 static bool process_string_cmd(const char *data
,
943 const GdbCmdParseEntry
*cmds
, int num_cmds
)
946 g_autoptr(GArray
) params
= g_array_new(false, true, sizeof(GdbCmdVariant
));
952 for (i
= 0; i
< num_cmds
; i
++) {
953 const GdbCmdParseEntry
*cmd
= &cmds
[i
];
954 void *user_ctx
= NULL
;
955 g_assert(cmd
->handler
&& cmd
->cmd
);
957 if ((cmd
->cmd_startswith
&& !startswith(data
, cmd
->cmd
)) ||
958 (!cmd
->cmd_startswith
&& strcmp(cmd
->cmd
, data
))) {
963 if (cmd_parse_params(&data
[strlen(cmd
->cmd
)],
964 cmd
->schema
, params
)) {
969 if (cmd
->need_cpu_context
) {
970 user_ctx
= (void *)gdbserver_state
.g_cpu
;
973 gdbserver_state
.allow_stop_reply
= cmd
->allow_stop_reply
;
974 cmd
->handler(params
, user_ctx
);
981 static void run_cmd_parser(const char *data
, const GdbCmdParseEntry
*cmd
)
987 g_string_set_size(gdbserver_state
.str_buf
, 0);
988 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
990 /* In case there was an error during the command parsing we must
991 * send a NULL packet to indicate the command is not supported */
992 if (!process_string_cmd(data
, cmd
, 1)) {
997 static void handle_detach(GArray
*params
, void *user_ctx
)
1002 if (gdbserver_state
.multiprocess
) {
1004 gdb_put_packet("E22");
1008 pid
= gdb_get_cmd_param(params
, 0)->val_ul
;
1011 #ifdef CONFIG_USER_ONLY
1012 if (gdb_handle_detach_user(pid
)) {
1017 process
= gdb_get_process(pid
);
1018 gdb_process_breakpoint_remove_all(process
);
1019 process
->attached
= false;
1021 if (pid
== gdb_get_cpu_pid(gdbserver_state
.c_cpu
)) {
1022 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
1025 if (pid
== gdb_get_cpu_pid(gdbserver_state
.g_cpu
)) {
1026 gdbserver_state
.g_cpu
= gdb_first_attached_cpu();
1029 if (!gdbserver_state
.c_cpu
) {
1030 /* No more process attached */
1031 gdb_disable_syscalls();
1034 gdb_put_packet("OK");
1037 static void handle_thread_alive(GArray
*params
, void *user_ctx
)
1042 gdb_put_packet("E22");
1046 if (gdb_get_cmd_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1047 gdb_put_packet("E22");
1051 cpu
= gdb_get_cpu(gdb_get_cmd_param(params
, 0)->thread_id
.pid
,
1052 gdb_get_cmd_param(params
, 0)->thread_id
.tid
);
1054 gdb_put_packet("E22");
1058 gdb_put_packet("OK");
1061 static void handle_continue(GArray
*params
, void *user_ctx
)
1064 gdb_set_cpu_pc(gdb_get_cmd_param(params
, 0)->val_ull
);
1067 gdbserver_state
.signal
= 0;
1071 static void handle_cont_with_sig(GArray
*params
, void *user_ctx
)
1073 unsigned long signal
= 0;
1076 * Note: C sig;[addr] is currently unsupported and we simply
1077 * omit the addr parameter
1080 signal
= gdb_get_cmd_param(params
, 0)->val_ul
;
1083 gdbserver_state
.signal
= gdb_signal_to_target(signal
);
1084 if (gdbserver_state
.signal
== -1) {
1085 gdbserver_state
.signal
= 0;
1090 static void handle_set_thread(GArray
*params
, void *user_ctx
)
1095 if (params
->len
!= 2) {
1096 gdb_put_packet("E22");
1100 if (gdb_get_cmd_param(params
, 1)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1101 gdb_put_packet("E22");
1105 if (gdb_get_cmd_param(params
, 1)->thread_id
.kind
!= GDB_ONE_THREAD
) {
1106 gdb_put_packet("OK");
1110 pid
= gdb_get_cmd_param(params
, 1)->thread_id
.pid
;
1111 tid
= gdb_get_cmd_param(params
, 1)->thread_id
.tid
;
1112 #ifdef CONFIG_USER_ONLY
1113 if (gdb_handle_set_thread_user(pid
, tid
)) {
1117 cpu
= gdb_get_cpu(pid
, tid
);
1119 gdb_put_packet("E22");
1124 * Note: This command is deprecated and modern gdb's will be using the
1125 * vCont command instead.
1127 switch (gdb_get_cmd_param(params
, 0)->opcode
) {
1129 gdbserver_state
.c_cpu
= cpu
;
1130 gdb_put_packet("OK");
1133 gdbserver_state
.g_cpu
= cpu
;
1134 gdb_put_packet("OK");
1137 gdb_put_packet("E22");
1142 static void handle_insert_bp(GArray
*params
, void *user_ctx
)
1146 if (params
->len
!= 3) {
1147 gdb_put_packet("E22");
1151 res
= gdb_breakpoint_insert(gdbserver_state
.c_cpu
,
1152 gdb_get_cmd_param(params
, 0)->val_ul
,
1153 gdb_get_cmd_param(params
, 1)->val_ull
,
1154 gdb_get_cmd_param(params
, 2)->val_ull
);
1156 gdb_put_packet("OK");
1158 } else if (res
== -ENOSYS
) {
1163 gdb_put_packet("E22");
1166 static void handle_remove_bp(GArray
*params
, void *user_ctx
)
1170 if (params
->len
!= 3) {
1171 gdb_put_packet("E22");
1175 res
= gdb_breakpoint_remove(gdbserver_state
.c_cpu
,
1176 gdb_get_cmd_param(params
, 0)->val_ul
,
1177 gdb_get_cmd_param(params
, 1)->val_ull
,
1178 gdb_get_cmd_param(params
, 2)->val_ull
);
1180 gdb_put_packet("OK");
1182 } else if (res
== -ENOSYS
) {
1187 gdb_put_packet("E22");
1191 * handle_set/get_reg
1193 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1194 * This works, but can be very slow. Anything new enough to understand
1195 * XML also knows how to use this properly. However to use this we
1196 * need to define a local XML file as well as be talking to a
1197 * reasonably modern gdb. Responding with an empty packet will cause
1198 * the remote gdb to fallback to older methods.
1201 static void handle_set_reg(GArray
*params
, void *user_ctx
)
1205 if (params
->len
!= 2) {
1206 gdb_put_packet("E22");
1210 reg_size
= strlen(gdb_get_cmd_param(params
, 1)->data
) / 2;
1211 gdb_hextomem(gdbserver_state
.mem_buf
, gdb_get_cmd_param(params
, 1)->data
, reg_size
);
1212 gdb_write_register(gdbserver_state
.g_cpu
, gdbserver_state
.mem_buf
->data
,
1213 gdb_get_cmd_param(params
, 0)->val_ull
);
1214 gdb_put_packet("OK");
1217 static void handle_get_reg(GArray
*params
, void *user_ctx
)
1222 gdb_put_packet("E14");
1226 reg_size
= gdb_read_register(gdbserver_state
.g_cpu
,
1227 gdbserver_state
.mem_buf
,
1228 gdb_get_cmd_param(params
, 0)->val_ull
);
1230 gdb_put_packet("E14");
1233 g_byte_array_set_size(gdbserver_state
.mem_buf
, reg_size
);
1236 gdb_memtohex(gdbserver_state
.str_buf
,
1237 gdbserver_state
.mem_buf
->data
, reg_size
);
1241 static void handle_write_mem(GArray
*params
, void *user_ctx
)
1243 if (params
->len
!= 3) {
1244 gdb_put_packet("E22");
1248 /* gdb_hextomem() reads 2*len bytes */
1249 if (gdb_get_cmd_param(params
, 1)->val_ull
>
1250 strlen(gdb_get_cmd_param(params
, 2)->data
) / 2) {
1251 gdb_put_packet("E22");
1255 gdb_hextomem(gdbserver_state
.mem_buf
, gdb_get_cmd_param(params
, 2)->data
,
1256 gdb_get_cmd_param(params
, 1)->val_ull
);
1257 if (gdb_target_memory_rw_debug(gdbserver_state
.g_cpu
,
1258 gdb_get_cmd_param(params
, 0)->val_ull
,
1259 gdbserver_state
.mem_buf
->data
,
1260 gdbserver_state
.mem_buf
->len
, true)) {
1261 gdb_put_packet("E14");
1265 gdb_put_packet("OK");
1268 static void handle_read_mem(GArray
*params
, void *user_ctx
)
1270 if (params
->len
!= 2) {
1271 gdb_put_packet("E22");
1275 /* gdb_memtohex() doubles the required space */
1276 if (gdb_get_cmd_param(params
, 1)->val_ull
> MAX_PACKET_LENGTH
/ 2) {
1277 gdb_put_packet("E22");
1281 g_byte_array_set_size(gdbserver_state
.mem_buf
,
1282 gdb_get_cmd_param(params
, 1)->val_ull
);
1284 if (gdb_target_memory_rw_debug(gdbserver_state
.g_cpu
,
1285 gdb_get_cmd_param(params
, 0)->val_ull
,
1286 gdbserver_state
.mem_buf
->data
,
1287 gdbserver_state
.mem_buf
->len
, false)) {
1288 gdb_put_packet("E14");
1292 gdb_memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
,
1293 gdbserver_state
.mem_buf
->len
);
1297 static void handle_write_all_regs(GArray
*params
, void *user_ctx
)
1308 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1309 len
= strlen(gdb_get_cmd_param(params
, 0)->data
) / 2;
1310 gdb_hextomem(gdbserver_state
.mem_buf
, gdb_get_cmd_param(params
, 0)->data
, len
);
1311 registers
= gdbserver_state
.mem_buf
->data
;
1313 reg_id
< gdbserver_state
.g_cpu
->gdb_num_g_regs
&& len
> 0;
1315 reg_size
= gdb_write_register(gdbserver_state
.g_cpu
, registers
, reg_id
);
1317 registers
+= reg_size
;
1319 gdb_put_packet("OK");
1322 static void handle_read_all_regs(GArray
*params
, void *user_ctx
)
1327 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1328 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
1330 for (reg_id
= 0; reg_id
< gdbserver_state
.g_cpu
->gdb_num_g_regs
; reg_id
++) {
1331 len
+= gdb_read_register(gdbserver_state
.g_cpu
,
1332 gdbserver_state
.mem_buf
,
1335 g_assert(len
== gdbserver_state
.mem_buf
->len
);
1337 gdb_memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
, len
);
1342 static void handle_step(GArray
*params
, void *user_ctx
)
1345 gdb_set_cpu_pc(gdb_get_cmd_param(params
, 0)->val_ull
);
1348 cpu_single_step(gdbserver_state
.c_cpu
, gdbserver_state
.sstep_flags
);
1352 static void handle_backward(GArray
*params
, void *user_ctx
)
1354 if (!gdb_can_reverse()) {
1355 gdb_put_packet("E22");
1357 if (params
->len
== 1) {
1358 switch (gdb_get_cmd_param(params
, 0)->opcode
) {
1360 if (replay_reverse_step()) {
1363 gdb_put_packet("E14");
1367 if (replay_reverse_continue()) {
1370 gdb_put_packet("E14");
1376 /* Default invalid command */
1380 static void handle_v_cont_query(GArray
*params
, void *user_ctx
)
1382 gdb_put_packet("vCont;c;C;s;S");
1385 static void handle_v_cont(GArray
*params
, void *user_ctx
)
1393 res
= gdb_handle_vcont(gdb_get_cmd_param(params
, 0)->data
);
1394 if ((res
== -EINVAL
) || (res
== -ERANGE
)) {
1395 gdb_put_packet("E22");
1401 static void handle_v_attach(GArray
*params
, void *user_ctx
)
1403 GDBProcess
*process
;
1406 g_string_assign(gdbserver_state
.str_buf
, "E22");
1411 process
= gdb_get_process(gdb_get_cmd_param(params
, 0)->val_ul
);
1416 cpu
= gdb_get_first_cpu_in_process(process
);
1421 process
->attached
= true;
1422 gdbserver_state
.g_cpu
= cpu
;
1423 gdbserver_state
.c_cpu
= cpu
;
1425 if (gdbserver_state
.allow_stop_reply
) {
1426 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
1427 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
1428 g_string_append_c(gdbserver_state
.str_buf
, ';');
1429 gdbserver_state
.allow_stop_reply
= false;
1435 static void handle_v_kill(GArray
*params
, void *user_ctx
)
1437 /* Kill the target */
1438 gdb_put_packet("OK");
1439 error_report("QEMU: Terminated via GDBstub");
1444 static const GdbCmdParseEntry gdb_v_commands_table
[] = {
1445 /* Order is important if has same prefix */
1447 .handler
= handle_v_cont_query
,
1449 .cmd_startswith
= true
1452 .handler
= handle_v_cont
,
1454 .cmd_startswith
= true,
1455 .allow_stop_reply
= true,
1459 .handler
= handle_v_attach
,
1461 .cmd_startswith
= true,
1462 .allow_stop_reply
= true,
1466 .handler
= handle_v_kill
,
1468 .cmd_startswith
= true
1470 #ifdef CONFIG_USER_ONLY
1472 * Host I/O Packets. See [1] for details.
1473 * [1] https://sourceware.org/gdb/onlinedocs/gdb/Host-I_002fO-Packets.html
1476 .handler
= gdb_handle_v_file_open
,
1477 .cmd
= "File:open:",
1478 .cmd_startswith
= true,
1482 .handler
= gdb_handle_v_file_close
,
1483 .cmd
= "File:close:",
1484 .cmd_startswith
= true,
1488 .handler
= gdb_handle_v_file_pread
,
1489 .cmd
= "File:pread:",
1490 .cmd_startswith
= true,
1494 .handler
= gdb_handle_v_file_readlink
,
1495 .cmd
= "File:readlink:",
1496 .cmd_startswith
= true,
1502 static void handle_v_commands(GArray
*params
, void *user_ctx
)
1508 if (!process_string_cmd(gdb_get_cmd_param(params
, 0)->data
,
1509 gdb_v_commands_table
,
1510 ARRAY_SIZE(gdb_v_commands_table
))) {
1515 static void handle_query_qemu_sstepbits(GArray
*params
, void *user_ctx
)
1517 g_string_printf(gdbserver_state
.str_buf
, "ENABLE=%x", SSTEP_ENABLE
);
1519 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOIRQ
) {
1520 g_string_append_printf(gdbserver_state
.str_buf
, ",NOIRQ=%x",
1524 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOTIMER
) {
1525 g_string_append_printf(gdbserver_state
.str_buf
, ",NOTIMER=%x",
1532 static void handle_set_qemu_sstep(GArray
*params
, void *user_ctx
)
1534 int new_sstep_flags
;
1540 new_sstep_flags
= gdb_get_cmd_param(params
, 0)->val_ul
;
1542 if (new_sstep_flags
& ~gdbserver_state
.supported_sstep_flags
) {
1543 gdb_put_packet("E22");
1547 gdbserver_state
.sstep_flags
= new_sstep_flags
;
1548 gdb_put_packet("OK");
1551 static void handle_query_qemu_sstep(GArray
*params
, void *user_ctx
)
1553 g_string_printf(gdbserver_state
.str_buf
, "0x%x",
1554 gdbserver_state
.sstep_flags
);
1558 static void handle_query_curr_tid(GArray
*params
, void *user_ctx
)
1561 GDBProcess
*process
;
1564 * "Current thread" remains vague in the spec, so always return
1565 * the first thread of the current process (gdb returns the
1568 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
1569 cpu
= gdb_get_first_cpu_in_process(process
);
1570 g_string_assign(gdbserver_state
.str_buf
, "QC");
1571 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
1575 static void handle_query_threads(GArray
*params
, void *user_ctx
)
1577 if (!gdbserver_state
.query_cpu
) {
1578 gdb_put_packet("l");
1582 g_string_assign(gdbserver_state
.str_buf
, "m");
1583 gdb_append_thread_id(gdbserver_state
.query_cpu
, gdbserver_state
.str_buf
);
1585 gdbserver_state
.query_cpu
= gdb_next_attached_cpu(gdbserver_state
.query_cpu
);
1588 static void handle_query_first_threads(GArray
*params
, void *user_ctx
)
1590 gdbserver_state
.query_cpu
= gdb_first_attached_cpu();
1591 handle_query_threads(params
, user_ctx
);
1594 static void handle_query_thread_extra(GArray
*params
, void *user_ctx
)
1596 g_autoptr(GString
) rs
= g_string_new(NULL
);
1600 gdb_get_cmd_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1601 gdb_put_packet("E22");
1605 cpu
= gdb_get_cpu(gdb_get_cmd_param(params
, 0)->thread_id
.pid
,
1606 gdb_get_cmd_param(params
, 0)->thread_id
.tid
);
1611 cpu_synchronize_state(cpu
);
1613 if (gdbserver_state
.multiprocess
&& (gdbserver_state
.process_num
> 1)) {
1614 /* Print the CPU model and name in multiprocess mode */
1615 ObjectClass
*oc
= object_get_class(OBJECT(cpu
));
1616 const char *cpu_model
= object_class_get_name(oc
);
1617 const char *cpu_name
=
1618 object_get_canonical_path_component(OBJECT(cpu
));
1619 g_string_printf(rs
, "%s %s [%s]", cpu_model
, cpu_name
,
1620 cpu
->halted
? "halted " : "running");
1622 g_string_printf(rs
, "CPU#%d [%s]", cpu
->cpu_index
,
1623 cpu
->halted
? "halted " : "running");
1625 trace_gdbstub_op_extra_info(rs
->str
);
1626 gdb_memtohex(gdbserver_state
.str_buf
, (uint8_t *)rs
->str
, rs
->len
);
1631 static char **extra_query_flags
;
1633 void gdb_extend_qsupported_features(char *qflags
)
1635 if (!extra_query_flags
) {
1636 extra_query_flags
= g_new0(char *, 2);
1637 extra_query_flags
[0] = g_strdup(qflags
);
1638 } else if (!g_strv_contains((const gchar
* const *) extra_query_flags
,
1640 int len
= g_strv_length(extra_query_flags
);
1641 extra_query_flags
= g_realloc_n(extra_query_flags
, len
+ 2,
1643 extra_query_flags
[len
] = g_strdup(qflags
);
1647 static void handle_query_supported(GArray
*params
, void *user_ctx
)
1651 g_string_printf(gdbserver_state
.str_buf
, "PacketSize=%x", MAX_PACKET_LENGTH
);
1652 cc
= CPU_GET_CLASS(first_cpu
);
1653 if (cc
->gdb_core_xml_file
) {
1654 g_string_append(gdbserver_state
.str_buf
, ";qXfer:features:read+");
1657 if (gdb_can_reverse()) {
1658 g_string_append(gdbserver_state
.str_buf
,
1659 ";ReverseStep+;ReverseContinue+");
1662 #if defined(CONFIG_USER_ONLY)
1663 #if defined(CONFIG_LINUX)
1664 if (get_task_state(gdbserver_state
.c_cpu
)) {
1665 g_string_append(gdbserver_state
.str_buf
, ";qXfer:auxv:read+");
1667 g_string_append(gdbserver_state
.str_buf
, ";QCatchSyscalls+");
1669 g_string_append(gdbserver_state
.str_buf
, ";qXfer:siginfo:read+");
1671 g_string_append(gdbserver_state
.str_buf
, ";qXfer:exec-file:read+");
1675 const char *gdb_supported
= gdb_get_cmd_param(params
, 0)->data
;
1677 if (strstr(gdb_supported
, "multiprocess+")) {
1678 gdbserver_state
.multiprocess
= true;
1680 #if defined(CONFIG_USER_ONLY)
1681 gdb_handle_query_supported_user(gdb_supported
);
1685 g_string_append(gdbserver_state
.str_buf
, ";vContSupported+;multiprocess+");
1687 if (extra_query_flags
) {
1688 int extras
= g_strv_length(extra_query_flags
);
1689 for (int i
= 0; i
< extras
; i
++) {
1690 g_string_append(gdbserver_state
.str_buf
, extra_query_flags
[i
]);
1697 static void handle_query_xfer_features(GArray
*params
, void *user_ctx
)
1699 GDBProcess
*process
;
1701 unsigned long len
, total_len
, addr
;
1705 if (params
->len
< 3) {
1706 gdb_put_packet("E22");
1710 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
1711 cc
= CPU_GET_CLASS(gdbserver_state
.g_cpu
);
1712 if (!cc
->gdb_core_xml_file
) {
1717 p
= gdb_get_cmd_param(params
, 0)->data
;
1718 xml
= get_feature_xml(p
, &p
, process
);
1720 gdb_put_packet("E00");
1724 addr
= gdb_get_cmd_param(params
, 1)->val_ul
;
1725 len
= gdb_get_cmd_param(params
, 2)->val_ul
;
1726 total_len
= strlen(xml
);
1727 if (addr
> total_len
) {
1728 gdb_put_packet("E00");
1732 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
1733 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1736 if (len
< total_len
- addr
) {
1737 g_string_assign(gdbserver_state
.str_buf
, "m");
1738 gdb_memtox(gdbserver_state
.str_buf
, xml
+ addr
, len
);
1740 g_string_assign(gdbserver_state
.str_buf
, "l");
1741 gdb_memtox(gdbserver_state
.str_buf
, xml
+ addr
, total_len
- addr
);
1744 gdb_put_packet_binary(gdbserver_state
.str_buf
->str
,
1745 gdbserver_state
.str_buf
->len
, true);
1748 static void handle_query_qemu_supported(GArray
*params
, void *user_ctx
)
1750 g_string_printf(gdbserver_state
.str_buf
, "sstepbits;sstep");
1751 #ifndef CONFIG_USER_ONLY
1752 g_string_append(gdbserver_state
.str_buf
, ";PhyMemMode");
1757 static const GdbCmdParseEntry gdb_gen_query_set_common_table
[] = {
1758 /* Order is important if has same prefix */
1760 .handler
= handle_query_qemu_sstepbits
,
1761 .cmd
= "qemu.sstepbits",
1764 .handler
= handle_query_qemu_sstep
,
1765 .cmd
= "qemu.sstep",
1768 .handler
= handle_set_qemu_sstep
,
1769 .cmd
= "qemu.sstep=",
1770 .cmd_startswith
= true,
1776 * extend_table() - extend one of the command tables
1777 * @table: the command table to extend (or NULL)
1778 * @extensions: a list of GdbCmdParseEntry pointers
1780 * The entries themselves should be pointers to static const
1781 * GdbCmdParseEntry entries. If the entry is already in the table we
1782 * skip adding it again.
1784 * Returns (a potentially freshly allocated) GPtrArray of GdbCmdParseEntry
1786 static GPtrArray
*extend_table(GPtrArray
*table
, GPtrArray
*extensions
)
1789 table
= g_ptr_array_new();
1792 for (int i
= 0; i
< extensions
->len
; i
++) {
1793 gpointer entry
= g_ptr_array_index(extensions
, i
);
1794 if (!g_ptr_array_find(table
, entry
, NULL
)) {
1795 g_ptr_array_add(table
, entry
);
1803 * process_extended_table() - run through an extended command table
1804 * @table: the command table to check
1807 * returns true if the command was found and executed
1809 static bool process_extended_table(GPtrArray
*table
, const char *data
)
1811 for (int i
= 0; i
< table
->len
; i
++) {
1812 const GdbCmdParseEntry
*entry
= g_ptr_array_index(table
, i
);
1813 if (process_string_cmd(data
, entry
, 1)) {
1821 /* Ptr to GdbCmdParseEntry */
1822 static GPtrArray
*extended_query_table
;
1824 void gdb_extend_query_table(GPtrArray
*new_queries
)
1826 extended_query_table
= extend_table(extended_query_table
, new_queries
);
1829 static const GdbCmdParseEntry gdb_gen_query_table
[] = {
1831 .handler
= handle_query_curr_tid
,
1835 .handler
= handle_query_threads
,
1836 .cmd
= "sThreadInfo",
1839 .handler
= handle_query_first_threads
,
1840 .cmd
= "fThreadInfo",
1843 .handler
= handle_query_thread_extra
,
1844 .cmd
= "ThreadExtraInfo,",
1845 .cmd_startswith
= true,
1848 #ifdef CONFIG_USER_ONLY
1850 .handler
= gdb_handle_query_offsets
,
1855 .handler
= gdb_handle_query_rcmd
,
1857 .cmd_startswith
= true,
1862 .handler
= handle_query_supported
,
1863 .cmd
= "Supported:",
1864 .cmd_startswith
= true,
1868 .handler
= handle_query_supported
,
1873 .handler
= handle_query_xfer_features
,
1874 .cmd
= "Xfer:features:read:",
1875 .cmd_startswith
= true,
1878 #if defined(CONFIG_USER_ONLY)
1879 #if defined(CONFIG_LINUX)
1881 .handler
= gdb_handle_query_xfer_auxv
,
1882 .cmd
= "Xfer:auxv:read::",
1883 .cmd_startswith
= true,
1887 .handler
= gdb_handle_query_xfer_siginfo
,
1888 .cmd
= "Xfer:siginfo:read::",
1889 .cmd_startswith
= true,
1894 .handler
= gdb_handle_query_xfer_exec_file
,
1895 .cmd
= "Xfer:exec-file:read:",
1896 .cmd_startswith
= true,
1901 .handler
= gdb_handle_query_attached
,
1903 .cmd_startswith
= true
1906 .handler
= gdb_handle_query_attached
,
1910 .handler
= handle_query_qemu_supported
,
1911 .cmd
= "qemu.Supported",
1913 #ifndef CONFIG_USER_ONLY
1915 .handler
= gdb_handle_query_qemu_phy_mem_mode
,
1916 .cmd
= "qemu.PhyMemMode",
1921 /* Ptr to GdbCmdParseEntry */
1922 static GPtrArray
*extended_set_table
;
1924 void gdb_extend_set_table(GPtrArray
*new_set
)
1926 extended_set_table
= extend_table(extended_set_table
, new_set
);
1929 static const GdbCmdParseEntry gdb_gen_set_table
[] = {
1930 /* Order is important if has same prefix */
1932 .handler
= handle_set_qemu_sstep
,
1933 .cmd
= "qemu.sstep:",
1934 .cmd_startswith
= true,
1937 #ifndef CONFIG_USER_ONLY
1939 .handler
= gdb_handle_set_qemu_phy_mem_mode
,
1940 .cmd
= "qemu.PhyMemMode:",
1941 .cmd_startswith
= true,
1945 #if defined(CONFIG_USER_ONLY)
1947 .handler
= gdb_handle_set_catch_syscalls
,
1948 .cmd
= "CatchSyscalls:",
1949 .cmd_startswith
= true,
1955 static void handle_gen_query(GArray
*params
, void *user_ctx
)
1963 data
= gdb_get_cmd_param(params
, 0)->data
;
1965 if (process_string_cmd(data
,
1966 gdb_gen_query_set_common_table
,
1967 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
1971 if (process_string_cmd(data
,
1972 gdb_gen_query_table
,
1973 ARRAY_SIZE(gdb_gen_query_table
))) {
1977 if (extended_query_table
&&
1978 process_extended_table(extended_query_table
, data
)) {
1982 /* Can't handle query, return Empty response. */
1986 static void handle_gen_set(GArray
*params
, void *user_ctx
)
1994 data
= gdb_get_cmd_param(params
, 0)->data
;
1996 if (process_string_cmd(data
,
1997 gdb_gen_query_set_common_table
,
1998 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
2002 if (process_string_cmd(data
,
2004 ARRAY_SIZE(gdb_gen_set_table
))) {
2008 if (extended_set_table
&&
2009 process_extended_table(extended_set_table
, data
)) {
2013 /* Can't handle set, return Empty response. */
2017 static void handle_target_halt(GArray
*params
, void *user_ctx
)
2019 if (gdbserver_state
.allow_stop_reply
) {
2020 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
2021 gdb_append_thread_id(gdbserver_state
.c_cpu
, gdbserver_state
.str_buf
);
2022 g_string_append_c(gdbserver_state
.str_buf
, ';');
2024 gdbserver_state
.allow_stop_reply
= false;
2027 * Remove all the breakpoints when this query is issued,
2028 * because gdb is doing an initial connect and the state
2029 * should be cleaned up.
2031 gdb_breakpoint_remove_all(gdbserver_state
.c_cpu
);
2034 static int gdb_handle_packet(const char *line_buf
)
2036 const GdbCmdParseEntry
*cmd_parser
= NULL
;
2038 trace_gdbstub_io_command(line_buf
);
2040 switch (line_buf
[0]) {
2042 gdb_put_packet("OK");
2046 static const GdbCmdParseEntry target_halted_cmd_desc
= {
2047 .handler
= handle_target_halt
,
2049 .cmd_startswith
= true,
2050 .allow_stop_reply
= true,
2052 cmd_parser
= &target_halted_cmd_desc
;
2057 static const GdbCmdParseEntry continue_cmd_desc
= {
2058 .handler
= handle_continue
,
2060 .cmd_startswith
= true,
2061 .allow_stop_reply
= true,
2064 cmd_parser
= &continue_cmd_desc
;
2069 static const GdbCmdParseEntry cont_with_sig_cmd_desc
= {
2070 .handler
= handle_cont_with_sig
,
2072 .cmd_startswith
= true,
2073 .allow_stop_reply
= true,
2076 cmd_parser
= &cont_with_sig_cmd_desc
;
2081 static const GdbCmdParseEntry v_cmd_desc
= {
2082 .handler
= handle_v_commands
,
2084 .cmd_startswith
= true,
2087 cmd_parser
= &v_cmd_desc
;
2091 /* Kill the target */
2092 error_report("QEMU: Terminated via GDBstub");
2098 static const GdbCmdParseEntry detach_cmd_desc
= {
2099 .handler
= handle_detach
,
2101 .cmd_startswith
= true,
2104 cmd_parser
= &detach_cmd_desc
;
2109 static const GdbCmdParseEntry step_cmd_desc
= {
2110 .handler
= handle_step
,
2112 .cmd_startswith
= true,
2113 .allow_stop_reply
= true,
2116 cmd_parser
= &step_cmd_desc
;
2121 static const GdbCmdParseEntry backward_cmd_desc
= {
2122 .handler
= handle_backward
,
2124 .cmd_startswith
= true,
2125 .allow_stop_reply
= true,
2128 cmd_parser
= &backward_cmd_desc
;
2133 static const GdbCmdParseEntry file_io_cmd_desc
= {
2134 .handler
= gdb_handle_file_io
,
2136 .cmd_startswith
= true,
2139 cmd_parser
= &file_io_cmd_desc
;
2144 static const GdbCmdParseEntry read_all_regs_cmd_desc
= {
2145 .handler
= handle_read_all_regs
,
2147 .cmd_startswith
= true
2149 cmd_parser
= &read_all_regs_cmd_desc
;
2154 static const GdbCmdParseEntry write_all_regs_cmd_desc
= {
2155 .handler
= handle_write_all_regs
,
2157 .cmd_startswith
= true,
2160 cmd_parser
= &write_all_regs_cmd_desc
;
2165 static const GdbCmdParseEntry read_mem_cmd_desc
= {
2166 .handler
= handle_read_mem
,
2168 .cmd_startswith
= true,
2171 cmd_parser
= &read_mem_cmd_desc
;
2176 static const GdbCmdParseEntry write_mem_cmd_desc
= {
2177 .handler
= handle_write_mem
,
2179 .cmd_startswith
= true,
2182 cmd_parser
= &write_mem_cmd_desc
;
2187 static const GdbCmdParseEntry get_reg_cmd_desc
= {
2188 .handler
= handle_get_reg
,
2190 .cmd_startswith
= true,
2193 cmd_parser
= &get_reg_cmd_desc
;
2198 static const GdbCmdParseEntry set_reg_cmd_desc
= {
2199 .handler
= handle_set_reg
,
2201 .cmd_startswith
= true,
2204 cmd_parser
= &set_reg_cmd_desc
;
2209 static const GdbCmdParseEntry insert_bp_cmd_desc
= {
2210 .handler
= handle_insert_bp
,
2212 .cmd_startswith
= true,
2215 cmd_parser
= &insert_bp_cmd_desc
;
2220 static const GdbCmdParseEntry remove_bp_cmd_desc
= {
2221 .handler
= handle_remove_bp
,
2223 .cmd_startswith
= true,
2226 cmd_parser
= &remove_bp_cmd_desc
;
2231 static const GdbCmdParseEntry set_thread_cmd_desc
= {
2232 .handler
= handle_set_thread
,
2234 .cmd_startswith
= true,
2237 cmd_parser
= &set_thread_cmd_desc
;
2242 static const GdbCmdParseEntry thread_alive_cmd_desc
= {
2243 .handler
= handle_thread_alive
,
2245 .cmd_startswith
= true,
2248 cmd_parser
= &thread_alive_cmd_desc
;
2253 static const GdbCmdParseEntry gen_query_cmd_desc
= {
2254 .handler
= handle_gen_query
,
2256 .cmd_startswith
= true,
2259 cmd_parser
= &gen_query_cmd_desc
;
2264 static const GdbCmdParseEntry gen_set_cmd_desc
= {
2265 .handler
= handle_gen_set
,
2267 .cmd_startswith
= true,
2270 cmd_parser
= &gen_set_cmd_desc
;
2274 /* put empty packet */
2280 run_cmd_parser(line_buf
, cmd_parser
);
2286 void gdb_set_stop_cpu(CPUState
*cpu
)
2288 GDBProcess
*p
= gdb_get_cpu_process(cpu
);
2292 * Having a stop CPU corresponding to a process that is not attached
2293 * confuses GDB. So we ignore the request.
2298 gdbserver_state
.c_cpu
= cpu
;
2299 gdbserver_state
.g_cpu
= cpu
;
2302 void gdb_read_byte(uint8_t ch
)
2306 gdbserver_state
.allow_stop_reply
= false;
2307 #ifndef CONFIG_USER_ONLY
2308 if (gdbserver_state
.last_packet
->len
) {
2309 /* Waiting for a response to the last packet. If we see the start
2310 of a new command then abandon the previous response. */
2312 trace_gdbstub_err_got_nack();
2313 gdb_put_buffer(gdbserver_state
.last_packet
->data
,
2314 gdbserver_state
.last_packet
->len
);
2315 } else if (ch
== '+') {
2316 trace_gdbstub_io_got_ack();
2318 trace_gdbstub_io_got_unexpected(ch
);
2321 if (ch
== '+' || ch
== '$') {
2322 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
2327 if (runstate_is_running()) {
2329 * When the CPU is running, we cannot do anything except stop
2330 * it when receiving a char. This is expected on a Ctrl-C in the
2331 * gdb client. Because we are in all-stop mode, gdb sends a
2332 * 0x03 byte which is not a usual packet, so we handle it specially
2333 * here, but it does expect a stop reply.
2336 trace_gdbstub_err_unexpected_runpkt(ch
);
2338 gdbserver_state
.allow_stop_reply
= true;
2340 vm_stop(RUN_STATE_PAUSED
);
2344 switch(gdbserver_state
.state
) {
2347 /* start of command packet */
2348 gdbserver_state
.line_buf_index
= 0;
2349 gdbserver_state
.line_sum
= 0;
2350 gdbserver_state
.state
= RS_GETLINE
;
2351 } else if (ch
== '+') {
2353 * do nothing, gdb may preemptively send out ACKs on
2354 * initial connection
2357 trace_gdbstub_err_garbage(ch
);
2362 /* start escape sequence */
2363 gdbserver_state
.state
= RS_GETLINE_ESC
;
2364 gdbserver_state
.line_sum
+= ch
;
2365 } else if (ch
== '*') {
2366 /* start run length encoding sequence */
2367 gdbserver_state
.state
= RS_GETLINE_RLE
;
2368 gdbserver_state
.line_sum
+= ch
;
2369 } else if (ch
== '#') {
2370 /* end of command, start of checksum*/
2371 gdbserver_state
.state
= RS_CHKSUM1
;
2372 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2373 trace_gdbstub_err_overrun();
2374 gdbserver_state
.state
= RS_IDLE
;
2376 /* unescaped command character */
2377 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
;
2378 gdbserver_state
.line_sum
+= ch
;
2381 case RS_GETLINE_ESC
:
2383 /* unexpected end of command in escape sequence */
2384 gdbserver_state
.state
= RS_CHKSUM1
;
2385 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2386 /* command buffer overrun */
2387 trace_gdbstub_err_overrun();
2388 gdbserver_state
.state
= RS_IDLE
;
2390 /* parse escaped character and leave escape state */
2391 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
^ 0x20;
2392 gdbserver_state
.line_sum
+= ch
;
2393 gdbserver_state
.state
= RS_GETLINE
;
2396 case RS_GETLINE_RLE
:
2398 * Run-length encoding is explained in "Debugging with GDB /
2399 * Appendix E GDB Remote Serial Protocol / Overview".
2401 if (ch
< ' ' || ch
== '#' || ch
== '$' || ch
> 126) {
2402 /* invalid RLE count encoding */
2403 trace_gdbstub_err_invalid_repeat(ch
);
2404 gdbserver_state
.state
= RS_GETLINE
;
2406 /* decode repeat length */
2407 int repeat
= ch
- ' ' + 3;
2408 if (gdbserver_state
.line_buf_index
+ repeat
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2409 /* that many repeats would overrun the command buffer */
2410 trace_gdbstub_err_overrun();
2411 gdbserver_state
.state
= RS_IDLE
;
2412 } else if (gdbserver_state
.line_buf_index
< 1) {
2413 /* got a repeat but we have nothing to repeat */
2414 trace_gdbstub_err_invalid_rle();
2415 gdbserver_state
.state
= RS_GETLINE
;
2417 /* repeat the last character */
2418 memset(gdbserver_state
.line_buf
+ gdbserver_state
.line_buf_index
,
2419 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
- 1], repeat
);
2420 gdbserver_state
.line_buf_index
+= repeat
;
2421 gdbserver_state
.line_sum
+= ch
;
2422 gdbserver_state
.state
= RS_GETLINE
;
2427 /* get high hex digit of checksum */
2428 if (!isxdigit(ch
)) {
2429 trace_gdbstub_err_checksum_invalid(ch
);
2430 gdbserver_state
.state
= RS_GETLINE
;
2433 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
] = '\0';
2434 gdbserver_state
.line_csum
= fromhex(ch
) << 4;
2435 gdbserver_state
.state
= RS_CHKSUM2
;
2438 /* get low hex digit of checksum */
2439 if (!isxdigit(ch
)) {
2440 trace_gdbstub_err_checksum_invalid(ch
);
2441 gdbserver_state
.state
= RS_GETLINE
;
2444 gdbserver_state
.line_csum
|= fromhex(ch
);
2446 if (gdbserver_state
.line_csum
!= (gdbserver_state
.line_sum
& 0xff)) {
2447 trace_gdbstub_err_checksum_incorrect(gdbserver_state
.line_sum
, gdbserver_state
.line_csum
);
2448 /* send NAK reply */
2450 gdb_put_buffer(&reply
, 1);
2451 gdbserver_state
.state
= RS_IDLE
;
2453 /* send ACK reply */
2455 gdb_put_buffer(&reply
, 1);
2456 gdbserver_state
.state
= gdb_handle_packet(gdbserver_state
.line_buf
);
2466 * Create the process that will contain all the "orphan" CPUs (that are not
2467 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2468 * be attachable and thus will be invisible to the user.
2470 void gdb_create_default_process(GDBState
*s
)
2472 GDBProcess
*process
;
2475 #ifdef CONFIG_USER_ONLY
2476 assert(gdbserver_state
.process_num
== 0);
2479 if (gdbserver_state
.process_num
) {
2480 pid
= s
->processes
[s
->process_num
- 1].pid
;
2484 /* We need an available PID slot for this process */
2485 assert(pid
< UINT32_MAX
);
2489 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2490 process
= &s
->processes
[s
->process_num
- 1];
2492 process
->attached
= false;
2493 process
->target_xml
= NULL
;