4 * This implements a subset of the remote protocol as described in:
6 * https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
8 * Copyright (c) 2003-2005 Fabrice Bellard
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 * SPDX-License-Identifier: LGPL-2.0+
26 #include "qemu/osdep.h"
27 #include "qemu/ctype.h"
28 #include "qemu/cutils.h"
29 #include "qemu/module.h"
30 #include "qemu/error-report.h"
32 #include "exec/gdbstub.h"
33 #include "gdbstub/syscalls.h"
34 #ifdef CONFIG_USER_ONLY
35 #include "gdbstub/user.h"
37 #include "hw/cpu/cluster.h"
38 #include "hw/boards.h"
41 #include "sysemu/hw_accel.h"
42 #include "sysemu/runstate.h"
43 #include "exec/replay-core.h"
44 #include "exec/hwaddr.h"
46 #include "internals.h"
48 typedef struct GDBRegisterState
{
50 gdb_get_reg_cb get_reg
;
51 gdb_set_reg_cb set_reg
;
52 const GDBFeature
*feature
;
55 GDBState gdbserver_state
;
57 void gdb_init_gdbserver_state(void)
59 g_assert(!gdbserver_state
.init
);
60 memset(&gdbserver_state
, 0, sizeof(GDBState
));
61 gdbserver_state
.init
= true;
62 gdbserver_state
.str_buf
= g_string_new(NULL
);
63 gdbserver_state
.mem_buf
= g_byte_array_sized_new(MAX_PACKET_LENGTH
);
64 gdbserver_state
.last_packet
= g_byte_array_sized_new(MAX_PACKET_LENGTH
+ 4);
67 * What single-step modes are supported is accelerator dependent.
68 * By default try to use no IRQs and no timers while single
69 * stepping so as to make single stepping like a typical ICE HW step.
71 gdbserver_state
.supported_sstep_flags
= accel_supported_gdbstub_sstep_flags();
72 gdbserver_state
.sstep_flags
= SSTEP_ENABLE
| SSTEP_NOIRQ
| SSTEP_NOTIMER
;
73 gdbserver_state
.sstep_flags
&= gdbserver_state
.supported_sstep_flags
;
76 /* writes 2*len+1 bytes in buf */
77 void gdb_memtohex(GString
*buf
, const uint8_t *mem
, int len
)
80 for(i
= 0; i
< len
; i
++) {
82 g_string_append_c(buf
, tohex(c
>> 4));
83 g_string_append_c(buf
, tohex(c
& 0xf));
85 g_string_append_c(buf
, '\0');
88 void gdb_hextomem(GByteArray
*mem
, const char *buf
, int len
)
92 for(i
= 0; i
< len
; i
++) {
93 guint8 byte
= fromhex(buf
[0]) << 4 | fromhex(buf
[1]);
94 g_byte_array_append(mem
, &byte
, 1);
99 static void hexdump(const char *buf
, int len
,
100 void (*trace_fn
)(size_t ofs
, char const *text
))
102 char line_buffer
[3 * 16 + 4 + 16 + 1];
105 for (i
= 0; i
< len
|| (i
& 0xF); ++i
) {
106 size_t byte_ofs
= i
& 15;
109 memset(line_buffer
, ' ', 3 * 16 + 4 + 16);
110 line_buffer
[3 * 16 + 4 + 16] = 0;
113 size_t col_group
= (i
>> 2) & 3;
114 size_t hex_col
= byte_ofs
* 3 + col_group
;
115 size_t txt_col
= 3 * 16 + 4 + byte_ofs
;
120 line_buffer
[hex_col
+ 0] = tohex((value
>> 4) & 0xF);
121 line_buffer
[hex_col
+ 1] = tohex((value
>> 0) & 0xF);
122 line_buffer
[txt_col
+ 0] = (value
>= ' ' && value
< 127)
128 trace_fn(i
& -16, line_buffer
);
132 /* return -1 if error, 0 if OK */
133 int gdb_put_packet_binary(const char *buf
, int len
, bool dump
)
138 if (dump
&& trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY
)) {
139 hexdump(buf
, len
, trace_gdbstub_io_binaryreply
);
143 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
144 g_byte_array_append(gdbserver_state
.last_packet
,
145 (const uint8_t *) "$", 1);
146 g_byte_array_append(gdbserver_state
.last_packet
,
147 (const uint8_t *) buf
, len
);
149 for(i
= 0; i
< len
; i
++) {
153 footer
[1] = tohex((csum
>> 4) & 0xf);
154 footer
[2] = tohex((csum
) & 0xf);
155 g_byte_array_append(gdbserver_state
.last_packet
, footer
, 3);
157 gdb_put_buffer(gdbserver_state
.last_packet
->data
,
158 gdbserver_state
.last_packet
->len
);
160 if (gdb_got_immediate_ack()) {
167 /* return -1 if error, 0 if OK */
168 int gdb_put_packet(const char *buf
)
170 trace_gdbstub_io_reply(buf
);
172 return gdb_put_packet_binary(buf
, strlen(buf
), false);
175 void gdb_put_strbuf(void)
177 gdb_put_packet(gdbserver_state
.str_buf
->str
);
180 /* Encode data using the encoding for 'x' packets. */
181 void gdb_memtox(GString
*buf
, const char *mem
, int len
)
188 case '#': case '$': case '*': case '}':
189 g_string_append_c(buf
, '}');
190 g_string_append_c(buf
, c
^ 0x20);
193 g_string_append_c(buf
, c
);
199 static uint32_t gdb_get_cpu_pid(CPUState
*cpu
)
201 #ifdef CONFIG_USER_ONLY
204 if (cpu
->cluster_index
== UNASSIGNED_CLUSTER_INDEX
) {
205 /* Return the default process' PID */
206 int index
= gdbserver_state
.process_num
- 1;
207 return gdbserver_state
.processes
[index
].pid
;
209 return cpu
->cluster_index
+ 1;
213 GDBProcess
*gdb_get_process(uint32_t pid
)
218 /* 0 means any process, we take the first one */
219 return &gdbserver_state
.processes
[0];
222 for (i
= 0; i
< gdbserver_state
.process_num
; i
++) {
223 if (gdbserver_state
.processes
[i
].pid
== pid
) {
224 return &gdbserver_state
.processes
[i
];
231 static GDBProcess
*gdb_get_cpu_process(CPUState
*cpu
)
233 return gdb_get_process(gdb_get_cpu_pid(cpu
));
236 static CPUState
*find_cpu(uint32_t thread_id
)
241 if (gdb_get_cpu_index(cpu
) == thread_id
) {
249 CPUState
*gdb_get_first_cpu_in_process(GDBProcess
*process
)
254 if (gdb_get_cpu_pid(cpu
) == process
->pid
) {
262 static CPUState
*gdb_next_cpu_in_process(CPUState
*cpu
)
264 uint32_t pid
= gdb_get_cpu_pid(cpu
);
268 if (gdb_get_cpu_pid(cpu
) == pid
) {
278 /* Return the cpu following @cpu, while ignoring unattached processes. */
279 static CPUState
*gdb_next_attached_cpu(CPUState
*cpu
)
284 if (gdb_get_cpu_process(cpu
)->attached
) {
294 /* Return the first attached cpu */
295 CPUState
*gdb_first_attached_cpu(void)
297 CPUState
*cpu
= first_cpu
;
298 GDBProcess
*process
= gdb_get_cpu_process(cpu
);
300 if (!process
->attached
) {
301 return gdb_next_attached_cpu(cpu
);
307 static CPUState
*gdb_get_cpu(uint32_t pid
, uint32_t tid
)
313 /* 0 means any process/thread, we take the first attached one */
314 return gdb_first_attached_cpu();
315 } else if (pid
&& !tid
) {
316 /* any thread in a specific process */
317 process
= gdb_get_process(pid
);
319 if (process
== NULL
) {
323 if (!process
->attached
) {
327 return gdb_get_first_cpu_in_process(process
);
329 /* a specific thread */
336 process
= gdb_get_cpu_process(cpu
);
338 if (pid
&& process
->pid
!= pid
) {
342 if (!process
->attached
) {
350 static const char *get_feature_xml(const char *p
, const char **newp
,
353 CPUState
*cpu
= gdb_get_first_cpu_in_process(process
);
354 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
359 * qXfer:features:read:ANNEX:OFFSET,LENGTH'
362 char *term
= strchr(p
, ':');
366 /* Is it the main target xml? */
367 if (strncmp(p
, "target.xml", len
) == 0) {
368 if (!process
->target_xml
) {
369 g_autoptr(GPtrArray
) xml
= g_ptr_array_new_with_free_func(g_free
);
373 g_strdup("<?xml version=\"1.0\"?>"
374 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
377 if (cc
->gdb_arch_name
) {
380 g_markup_printf_escaped("<architecture>%s</architecture>",
381 cc
->gdb_arch_name(cpu
)));
383 for (guint i
= 0; i
< cpu
->gdb_regs
->len
; i
++) {
384 r
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
387 g_markup_printf_escaped("<xi:include href=\"%s\"/>",
388 r
->feature
->xmlname
));
390 g_ptr_array_add(xml
, g_strdup("</target>"));
391 g_ptr_array_add(xml
, NULL
);
393 process
->target_xml
= g_strjoinv(NULL
, (void *)xml
->pdata
);
395 return process
->target_xml
;
397 /* Is it one of the features? */
398 for (guint i
= 0; i
< cpu
->gdb_regs
->len
; i
++) {
399 r
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
400 if (strncmp(p
, r
->feature
->xmlname
, len
) == 0) {
401 return r
->feature
->xml
;
409 void gdb_feature_builder_init(GDBFeatureBuilder
*builder
, GDBFeature
*feature
,
410 const char *name
, const char *xmlname
,
413 char *header
= g_markup_printf_escaped(
414 "<?xml version=\"1.0\"?>"
415 "<!DOCTYPE feature SYSTEM \"gdb-target.dtd\">"
416 "<feature name=\"%s\">",
419 builder
->feature
= feature
;
420 builder
->xml
= g_ptr_array_new();
421 g_ptr_array_add(builder
->xml
, header
);
422 builder
->regs
= g_ptr_array_new();
423 builder
->base_reg
= base_reg
;
424 feature
->xmlname
= xmlname
;
425 feature
->name
= name
;
428 void gdb_feature_builder_append_tag(const GDBFeatureBuilder
*builder
,
429 const char *format
, ...)
432 va_start(ap
, format
);
433 g_ptr_array_add(builder
->xml
, g_markup_vprintf_escaped(format
, ap
));
437 void gdb_feature_builder_append_reg(const GDBFeatureBuilder
*builder
,
444 if (builder
->regs
->len
<= regnum
) {
445 g_ptr_array_set_size(builder
->regs
, regnum
+ 1);
448 builder
->regs
->pdata
[regnum
] = (gpointer
*)name
;
451 gdb_feature_builder_append_tag(
453 "<reg name=\"%s\" bitsize=\"%d\" regnum=\"%d\" type=\"%s\" group=\"%s\"/>",
454 name
, bitsize
, builder
->base_reg
+ regnum
, type
, group
);
456 gdb_feature_builder_append_tag(
458 "<reg name=\"%s\" bitsize=\"%d\" regnum=\"%d\" type=\"%s\"/>",
459 name
, bitsize
, builder
->base_reg
+ regnum
, type
);
463 void gdb_feature_builder_end(const GDBFeatureBuilder
*builder
)
465 g_ptr_array_add(builder
->xml
, (void *)"</feature>");
466 g_ptr_array_add(builder
->xml
, NULL
);
468 builder
->feature
->xml
= g_strjoinv(NULL
, (void *)builder
->xml
->pdata
);
470 for (guint i
= 0; i
< builder
->xml
->len
- 2; i
++) {
471 g_free(g_ptr_array_index(builder
->xml
, i
));
474 g_ptr_array_free(builder
->xml
, TRUE
);
476 builder
->feature
->num_regs
= builder
->regs
->len
;
477 builder
->feature
->regs
= (void *)g_ptr_array_free(builder
->regs
, FALSE
);
480 const GDBFeature
*gdb_find_static_feature(const char *xmlname
)
482 const GDBFeature
*feature
;
484 for (feature
= gdb_static_features
; feature
->xmlname
; feature
++) {
485 if (!strcmp(feature
->xmlname
, xmlname
)) {
490 g_assert_not_reached();
493 GArray
*gdb_get_register_list(CPUState
*cpu
)
495 GArray
*results
= g_array_new(true, true, sizeof(GDBRegDesc
));
497 /* registers are only available once the CPU is initialised */
498 if (!cpu
->gdb_regs
) {
502 for (int f
= 0; f
< cpu
->gdb_regs
->len
; f
++) {
503 GDBRegisterState
*r
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, f
);
504 for (int i
= 0; i
< r
->feature
->num_regs
; i
++) {
505 const char *name
= r
->feature
->regs
[i
];
511 g_array_append_val(results
, desc
);
518 int gdb_read_register(CPUState
*cpu
, GByteArray
*buf
, int reg
)
520 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
523 if (reg
< cc
->gdb_num_core_regs
) {
524 return cc
->gdb_read_register(cpu
, buf
, reg
);
527 for (guint i
= 0; i
< cpu
->gdb_regs
->len
; i
++) {
528 r
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
529 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->feature
->num_regs
) {
530 return r
->get_reg(cpu
, buf
, reg
- r
->base_reg
);
536 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
538 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
541 if (reg
< cc
->gdb_num_core_regs
) {
542 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
545 for (guint i
= 0; i
< cpu
->gdb_regs
->len
; i
++) {
546 r
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
547 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->feature
->num_regs
) {
548 return r
->set_reg(cpu
, mem_buf
, reg
- r
->base_reg
);
554 static void gdb_register_feature(CPUState
*cpu
, int base_reg
,
555 gdb_get_reg_cb get_reg
, gdb_set_reg_cb set_reg
,
556 const GDBFeature
*feature
)
558 GDBRegisterState s
= {
559 .base_reg
= base_reg
,
565 g_array_append_val(cpu
->gdb_regs
, s
);
568 void gdb_init_cpu(CPUState
*cpu
)
570 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
571 const GDBFeature
*feature
;
573 cpu
->gdb_regs
= g_array_new(false, false, sizeof(GDBRegisterState
));
575 if (cc
->gdb_core_xml_file
) {
576 feature
= gdb_find_static_feature(cc
->gdb_core_xml_file
);
577 gdb_register_feature(cpu
, 0,
578 cc
->gdb_read_register
, cc
->gdb_write_register
,
580 cpu
->gdb_num_regs
= cpu
->gdb_num_g_regs
= feature
->num_regs
;
583 if (cc
->gdb_num_core_regs
) {
584 cpu
->gdb_num_regs
= cpu
->gdb_num_g_regs
= cc
->gdb_num_core_regs
;
588 void gdb_register_coprocessor(CPUState
*cpu
,
589 gdb_get_reg_cb get_reg
, gdb_set_reg_cb set_reg
,
590 const GDBFeature
*feature
, int g_pos
)
594 int base_reg
= cpu
->gdb_num_regs
;
596 for (i
= 0; i
< cpu
->gdb_regs
->len
; i
++) {
597 /* Check for duplicates. */
598 s
= &g_array_index(cpu
->gdb_regs
, GDBRegisterState
, i
);
599 if (s
->feature
== feature
) {
604 gdb_register_feature(cpu
, base_reg
, get_reg
, set_reg
, feature
);
606 /* Add to end of list. */
607 cpu
->gdb_num_regs
+= feature
->num_regs
;
609 if (g_pos
!= base_reg
) {
610 error_report("Error: Bad gdb register numbering for '%s', "
611 "expected %d got %d", feature
->xml
, g_pos
, base_reg
);
613 cpu
->gdb_num_g_regs
= cpu
->gdb_num_regs
;
618 static void gdb_process_breakpoint_remove_all(GDBProcess
*p
)
620 CPUState
*cpu
= gdb_get_first_cpu_in_process(p
);
623 gdb_breakpoint_remove_all(cpu
);
624 cpu
= gdb_next_cpu_in_process(cpu
);
629 static void gdb_set_cpu_pc(vaddr pc
)
631 CPUState
*cpu
= gdbserver_state
.c_cpu
;
633 cpu_synchronize_state(cpu
);
637 void gdb_append_thread_id(CPUState
*cpu
, GString
*buf
)
639 if (gdbserver_state
.multiprocess
) {
640 g_string_append_printf(buf
, "p%02x.%02x",
641 gdb_get_cpu_pid(cpu
), gdb_get_cpu_index(cpu
));
643 g_string_append_printf(buf
, "%02x", gdb_get_cpu_index(cpu
));
647 static GDBThreadIdKind
read_thread_id(const char *buf
, const char **end_buf
,
648 uint32_t *pid
, uint32_t *tid
)
655 ret
= qemu_strtoul(buf
, &buf
, 16, &p
);
658 return GDB_READ_THREAD_ERR
;
667 ret
= qemu_strtoul(buf
, &buf
, 16, &t
);
670 return GDB_READ_THREAD_ERR
;
676 return GDB_ALL_PROCESSES
;
684 return GDB_ALL_THREADS
;
691 return GDB_ONE_THREAD
;
695 * gdb_handle_vcont - Parses and handles a vCont packet.
696 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
697 * a format error, 0 on success.
699 static int gdb_handle_vcont(const char *p
)
707 GDBThreadIdKind kind
;
708 unsigned int max_cpus
= gdb_get_max_cpus();
709 /* uninitialised CPUs stay 0 */
710 g_autofree
char *newstates
= g_new0(char, max_cpus
);
712 /* mark valid CPUs with 1 */
714 newstates
[cpu
->cpu_index
] = 1;
718 * res keeps track of what error we are returning, with -ENOTSUP meaning
719 * that the command is unknown or unsupported, thus returning an empty
720 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
721 * or incorrect parameters passed.
726 * target_count and last_target keep track of how many CPUs we are going to
727 * step or resume, and a pointer to the state structure of one of them,
730 int target_count
= 0;
731 CPUState
*last_target
= NULL
;
739 if (cur_action
== 'C' || cur_action
== 'S') {
740 cur_action
= qemu_tolower(cur_action
);
741 res
= qemu_strtoul(p
, &p
, 16, &tmp
);
745 signal
= gdb_signal_to_target(tmp
);
746 } else if (cur_action
!= 'c' && cur_action
!= 's') {
747 /* unknown/invalid/unsupported command */
751 if (*p
== '\0' || *p
== ';') {
753 * No thread specifier, action is on "all threads". The
754 * specification is unclear regarding the process to act on. We
755 * choose all processes.
757 kind
= GDB_ALL_PROCESSES
;
758 } else if (*p
++ == ':') {
759 kind
= read_thread_id(p
, &p
, &pid
, &tid
);
765 case GDB_READ_THREAD_ERR
:
768 case GDB_ALL_PROCESSES
:
769 cpu
= gdb_first_attached_cpu();
771 if (newstates
[cpu
->cpu_index
] == 1) {
772 newstates
[cpu
->cpu_index
] = cur_action
;
778 cpu
= gdb_next_attached_cpu(cpu
);
782 case GDB_ALL_THREADS
:
783 process
= gdb_get_process(pid
);
785 if (!process
->attached
) {
789 cpu
= gdb_get_first_cpu_in_process(process
);
791 if (newstates
[cpu
->cpu_index
] == 1) {
792 newstates
[cpu
->cpu_index
] = cur_action
;
798 cpu
= gdb_next_cpu_in_process(cpu
);
803 cpu
= gdb_get_cpu(pid
, tid
);
805 /* invalid CPU/thread specified */
810 /* only use if no previous match occourred */
811 if (newstates
[cpu
->cpu_index
] == 1) {
812 newstates
[cpu
->cpu_index
] = cur_action
;
822 * if we're about to resume a specific set of CPUs/threads, make it so that
823 * in case execution gets interrupted, we can send GDB a stop reply with a
824 * correct value. it doesn't really matter which CPU we tell GDB the signal
825 * happened in (VM pauses stop all of them anyway), so long as it is one of
826 * the ones we resumed/single stepped here.
828 if (target_count
> 0) {
829 gdbserver_state
.c_cpu
= last_target
;
832 gdbserver_state
.signal
= signal
;
833 gdb_continue_partial(newstates
);
837 static const char *cmd_next_param(const char *param
, const char delimiter
)
839 static const char all_delimiters
[] = ",;:=";
840 char curr_delimiters
[2] = {0};
841 const char *delimiters
;
843 if (delimiter
== '?') {
844 delimiters
= all_delimiters
;
845 } else if (delimiter
== '0') {
846 return strchr(param
, '\0');
847 } else if (delimiter
== '.' && *param
) {
850 curr_delimiters
[0] = delimiter
;
851 delimiters
= curr_delimiters
;
854 param
+= strcspn(param
, delimiters
);
861 static int cmd_parse_params(const char *data
, const char *schema
,
864 const char *curr_schema
, *curr_data
;
867 g_assert(params
->len
== 0);
869 curr_schema
= schema
;
871 while (curr_schema
[0] && curr_schema
[1] && *curr_data
) {
872 GdbCmdVariant this_param
;
874 switch (curr_schema
[0]) {
876 if (qemu_strtoul(curr_data
, &curr_data
, 16,
877 &this_param
.val_ul
)) {
880 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
881 g_array_append_val(params
, this_param
);
884 if (qemu_strtou64(curr_data
, &curr_data
, 16,
885 (uint64_t *)&this_param
.val_ull
)) {
888 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
889 g_array_append_val(params
, this_param
);
892 this_param
.data
= curr_data
;
893 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
894 g_array_append_val(params
, this_param
);
897 this_param
.opcode
= *(uint8_t *)curr_data
;
898 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
899 g_array_append_val(params
, this_param
);
902 this_param
.thread_id
.kind
=
903 read_thread_id(curr_data
, &curr_data
,
904 &this_param
.thread_id
.pid
,
905 &this_param
.thread_id
.tid
);
906 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
907 g_array_append_val(params
, this_param
);
910 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
921 typedef void (*GdbCmdHandler
)(GArray
*params
, void *user_ctx
);
924 * cmd_startswith -> cmd is compared using startswith
926 * allow_stop_reply -> true iff the gdbstub can respond to this command with a
927 * "stop reply" packet. The list of commands that accept such response is
928 * defined at the GDB Remote Serial Protocol documentation. see:
929 * https://sourceware.org/gdb/onlinedocs/gdb/Stop-Reply-Packets.html#Stop-Reply-Packets.
931 * schema definitions:
932 * Each schema parameter entry consists of 2 chars,
933 * the first char represents the parameter type handling
934 * the second char represents the delimiter for the next parameter
936 * Currently supported schema types:
937 * 'l' -> unsigned long (stored in .val_ul)
938 * 'L' -> unsigned long long (stored in .val_ull)
939 * 's' -> string (stored in .data)
940 * 'o' -> single char (stored in .opcode)
941 * 't' -> thread id (stored in .thread_id)
942 * '?' -> skip according to delimiter
944 * Currently supported delimiters:
945 * '?' -> Stop at any delimiter (",;:=\0")
946 * '0' -> Stop at "\0"
947 * '.' -> Skip 1 char unless reached "\0"
948 * Any other value is treated as the delimiter value itself
950 typedef struct GdbCmdParseEntry
{
951 GdbCmdHandler handler
;
955 bool allow_stop_reply
;
958 static inline int startswith(const char *string
, const char *pattern
)
960 return !strncmp(string
, pattern
, strlen(pattern
));
963 static int process_string_cmd(const char *data
,
964 const GdbCmdParseEntry
*cmds
, int num_cmds
)
967 g_autoptr(GArray
) params
= g_array_new(false, true, sizeof(GdbCmdVariant
));
973 for (i
= 0; i
< num_cmds
; i
++) {
974 const GdbCmdParseEntry
*cmd
= &cmds
[i
];
975 g_assert(cmd
->handler
&& cmd
->cmd
);
977 if ((cmd
->cmd_startswith
&& !startswith(data
, cmd
->cmd
)) ||
978 (!cmd
->cmd_startswith
&& strcmp(cmd
->cmd
, data
))) {
983 if (cmd_parse_params(&data
[strlen(cmd
->cmd
)],
984 cmd
->schema
, params
)) {
989 gdbserver_state
.allow_stop_reply
= cmd
->allow_stop_reply
;
990 cmd
->handler(params
, NULL
);
997 static void run_cmd_parser(const char *data
, const GdbCmdParseEntry
*cmd
)
1003 g_string_set_size(gdbserver_state
.str_buf
, 0);
1004 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
1006 /* In case there was an error during the command parsing we must
1007 * send a NULL packet to indicate the command is not supported */
1008 if (process_string_cmd(data
, cmd
, 1)) {
1013 static void handle_detach(GArray
*params
, void *user_ctx
)
1015 GDBProcess
*process
;
1018 if (gdbserver_state
.multiprocess
) {
1020 gdb_put_packet("E22");
1024 pid
= get_param(params
, 0)->val_ul
;
1027 #ifdef CONFIG_USER_ONLY
1028 if (gdb_handle_detach_user(pid
)) {
1033 process
= gdb_get_process(pid
);
1034 gdb_process_breakpoint_remove_all(process
);
1035 process
->attached
= false;
1037 if (pid
== gdb_get_cpu_pid(gdbserver_state
.c_cpu
)) {
1038 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
1041 if (pid
== gdb_get_cpu_pid(gdbserver_state
.g_cpu
)) {
1042 gdbserver_state
.g_cpu
= gdb_first_attached_cpu();
1045 if (!gdbserver_state
.c_cpu
) {
1046 /* No more process attached */
1047 gdb_disable_syscalls();
1050 gdb_put_packet("OK");
1053 static void handle_thread_alive(GArray
*params
, void *user_ctx
)
1058 gdb_put_packet("E22");
1062 if (get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1063 gdb_put_packet("E22");
1067 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
1068 get_param(params
, 0)->thread_id
.tid
);
1070 gdb_put_packet("E22");
1074 gdb_put_packet("OK");
1077 static void handle_continue(GArray
*params
, void *user_ctx
)
1080 gdb_set_cpu_pc(get_param(params
, 0)->val_ull
);
1083 gdbserver_state
.signal
= 0;
1087 static void handle_cont_with_sig(GArray
*params
, void *user_ctx
)
1089 unsigned long signal
= 0;
1092 * Note: C sig;[addr] is currently unsupported and we simply
1093 * omit the addr parameter
1096 signal
= get_param(params
, 0)->val_ul
;
1099 gdbserver_state
.signal
= gdb_signal_to_target(signal
);
1100 if (gdbserver_state
.signal
== -1) {
1101 gdbserver_state
.signal
= 0;
1106 static void handle_set_thread(GArray
*params
, void *user_ctx
)
1111 if (params
->len
!= 2) {
1112 gdb_put_packet("E22");
1116 if (get_param(params
, 1)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1117 gdb_put_packet("E22");
1121 if (get_param(params
, 1)->thread_id
.kind
!= GDB_ONE_THREAD
) {
1122 gdb_put_packet("OK");
1126 pid
= get_param(params
, 1)->thread_id
.pid
;
1127 tid
= get_param(params
, 1)->thread_id
.tid
;
1128 #ifdef CONFIG_USER_ONLY
1129 if (gdb_handle_set_thread_user(pid
, tid
)) {
1133 cpu
= gdb_get_cpu(pid
, tid
);
1135 gdb_put_packet("E22");
1140 * Note: This command is deprecated and modern gdb's will be using the
1141 * vCont command instead.
1143 switch (get_param(params
, 0)->opcode
) {
1145 gdbserver_state
.c_cpu
= cpu
;
1146 gdb_put_packet("OK");
1149 gdbserver_state
.g_cpu
= cpu
;
1150 gdb_put_packet("OK");
1153 gdb_put_packet("E22");
1158 static void handle_insert_bp(GArray
*params
, void *user_ctx
)
1162 if (params
->len
!= 3) {
1163 gdb_put_packet("E22");
1167 res
= gdb_breakpoint_insert(gdbserver_state
.c_cpu
,
1168 get_param(params
, 0)->val_ul
,
1169 get_param(params
, 1)->val_ull
,
1170 get_param(params
, 2)->val_ull
);
1172 gdb_put_packet("OK");
1174 } else if (res
== -ENOSYS
) {
1179 gdb_put_packet("E22");
1182 static void handle_remove_bp(GArray
*params
, void *user_ctx
)
1186 if (params
->len
!= 3) {
1187 gdb_put_packet("E22");
1191 res
= gdb_breakpoint_remove(gdbserver_state
.c_cpu
,
1192 get_param(params
, 0)->val_ul
,
1193 get_param(params
, 1)->val_ull
,
1194 get_param(params
, 2)->val_ull
);
1196 gdb_put_packet("OK");
1198 } else if (res
== -ENOSYS
) {
1203 gdb_put_packet("E22");
1207 * handle_set/get_reg
1209 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1210 * This works, but can be very slow. Anything new enough to understand
1211 * XML also knows how to use this properly. However to use this we
1212 * need to define a local XML file as well as be talking to a
1213 * reasonably modern gdb. Responding with an empty packet will cause
1214 * the remote gdb to fallback to older methods.
1217 static void handle_set_reg(GArray
*params
, void *user_ctx
)
1221 if (params
->len
!= 2) {
1222 gdb_put_packet("E22");
1226 reg_size
= strlen(get_param(params
, 1)->data
) / 2;
1227 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 1)->data
, reg_size
);
1228 gdb_write_register(gdbserver_state
.g_cpu
, gdbserver_state
.mem_buf
->data
,
1229 get_param(params
, 0)->val_ull
);
1230 gdb_put_packet("OK");
1233 static void handle_get_reg(GArray
*params
, void *user_ctx
)
1238 gdb_put_packet("E14");
1242 reg_size
= gdb_read_register(gdbserver_state
.g_cpu
,
1243 gdbserver_state
.mem_buf
,
1244 get_param(params
, 0)->val_ull
);
1246 gdb_put_packet("E14");
1249 g_byte_array_set_size(gdbserver_state
.mem_buf
, reg_size
);
1252 gdb_memtohex(gdbserver_state
.str_buf
,
1253 gdbserver_state
.mem_buf
->data
, reg_size
);
1257 static void handle_write_mem(GArray
*params
, void *user_ctx
)
1259 if (params
->len
!= 3) {
1260 gdb_put_packet("E22");
1264 /* gdb_hextomem() reads 2*len bytes */
1265 if (get_param(params
, 1)->val_ull
>
1266 strlen(get_param(params
, 2)->data
) / 2) {
1267 gdb_put_packet("E22");
1271 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 2)->data
,
1272 get_param(params
, 1)->val_ull
);
1273 if (gdb_target_memory_rw_debug(gdbserver_state
.g_cpu
,
1274 get_param(params
, 0)->val_ull
,
1275 gdbserver_state
.mem_buf
->data
,
1276 gdbserver_state
.mem_buf
->len
, true)) {
1277 gdb_put_packet("E14");
1281 gdb_put_packet("OK");
1284 static void handle_read_mem(GArray
*params
, void *user_ctx
)
1286 if (params
->len
!= 2) {
1287 gdb_put_packet("E22");
1291 /* gdb_memtohex() doubles the required space */
1292 if (get_param(params
, 1)->val_ull
> MAX_PACKET_LENGTH
/ 2) {
1293 gdb_put_packet("E22");
1297 g_byte_array_set_size(gdbserver_state
.mem_buf
,
1298 get_param(params
, 1)->val_ull
);
1300 if (gdb_target_memory_rw_debug(gdbserver_state
.g_cpu
,
1301 get_param(params
, 0)->val_ull
,
1302 gdbserver_state
.mem_buf
->data
,
1303 gdbserver_state
.mem_buf
->len
, false)) {
1304 gdb_put_packet("E14");
1308 gdb_memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
,
1309 gdbserver_state
.mem_buf
->len
);
1313 static void handle_write_all_regs(GArray
*params
, void *user_ctx
)
1324 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1325 len
= strlen(get_param(params
, 0)->data
) / 2;
1326 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 0)->data
, len
);
1327 registers
= gdbserver_state
.mem_buf
->data
;
1329 reg_id
< gdbserver_state
.g_cpu
->gdb_num_g_regs
&& len
> 0;
1331 reg_size
= gdb_write_register(gdbserver_state
.g_cpu
, registers
, reg_id
);
1333 registers
+= reg_size
;
1335 gdb_put_packet("OK");
1338 static void handle_read_all_regs(GArray
*params
, void *user_ctx
)
1343 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1344 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
1346 for (reg_id
= 0; reg_id
< gdbserver_state
.g_cpu
->gdb_num_g_regs
; reg_id
++) {
1347 len
+= gdb_read_register(gdbserver_state
.g_cpu
,
1348 gdbserver_state
.mem_buf
,
1351 g_assert(len
== gdbserver_state
.mem_buf
->len
);
1353 gdb_memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
, len
);
1358 static void handle_step(GArray
*params
, void *user_ctx
)
1361 gdb_set_cpu_pc(get_param(params
, 0)->val_ull
);
1364 cpu_single_step(gdbserver_state
.c_cpu
, gdbserver_state
.sstep_flags
);
1368 static void handle_backward(GArray
*params
, void *user_ctx
)
1370 if (!gdb_can_reverse()) {
1371 gdb_put_packet("E22");
1373 if (params
->len
== 1) {
1374 switch (get_param(params
, 0)->opcode
) {
1376 if (replay_reverse_step()) {
1379 gdb_put_packet("E14");
1383 if (replay_reverse_continue()) {
1386 gdb_put_packet("E14");
1392 /* Default invalid command */
1396 static void handle_v_cont_query(GArray
*params
, void *user_ctx
)
1398 gdb_put_packet("vCont;c;C;s;S");
1401 static void handle_v_cont(GArray
*params
, void *user_ctx
)
1409 res
= gdb_handle_vcont(get_param(params
, 0)->data
);
1410 if ((res
== -EINVAL
) || (res
== -ERANGE
)) {
1411 gdb_put_packet("E22");
1417 static void handle_v_attach(GArray
*params
, void *user_ctx
)
1419 GDBProcess
*process
;
1422 g_string_assign(gdbserver_state
.str_buf
, "E22");
1427 process
= gdb_get_process(get_param(params
, 0)->val_ul
);
1432 cpu
= gdb_get_first_cpu_in_process(process
);
1437 process
->attached
= true;
1438 gdbserver_state
.g_cpu
= cpu
;
1439 gdbserver_state
.c_cpu
= cpu
;
1441 if (gdbserver_state
.allow_stop_reply
) {
1442 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
1443 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
1444 g_string_append_c(gdbserver_state
.str_buf
, ';');
1445 gdbserver_state
.allow_stop_reply
= false;
1451 static void handle_v_kill(GArray
*params
, void *user_ctx
)
1453 /* Kill the target */
1454 gdb_put_packet("OK");
1455 error_report("QEMU: Terminated via GDBstub");
1460 static const GdbCmdParseEntry gdb_v_commands_table
[] = {
1461 /* Order is important if has same prefix */
1463 .handler
= handle_v_cont_query
,
1468 .handler
= handle_v_cont
,
1470 .cmd_startswith
= 1,
1471 .allow_stop_reply
= true,
1475 .handler
= handle_v_attach
,
1477 .cmd_startswith
= 1,
1478 .allow_stop_reply
= true,
1482 .handler
= handle_v_kill
,
1486 #ifdef CONFIG_USER_ONLY
1488 * Host I/O Packets. See [1] for details.
1489 * [1] https://sourceware.org/gdb/onlinedocs/gdb/Host-I_002fO-Packets.html
1492 .handler
= gdb_handle_v_file_open
,
1493 .cmd
= "File:open:",
1494 .cmd_startswith
= 1,
1498 .handler
= gdb_handle_v_file_close
,
1499 .cmd
= "File:close:",
1500 .cmd_startswith
= 1,
1504 .handler
= gdb_handle_v_file_pread
,
1505 .cmd
= "File:pread:",
1506 .cmd_startswith
= 1,
1510 .handler
= gdb_handle_v_file_readlink
,
1511 .cmd
= "File:readlink:",
1512 .cmd_startswith
= 1,
1518 static void handle_v_commands(GArray
*params
, void *user_ctx
)
1524 if (process_string_cmd(get_param(params
, 0)->data
,
1525 gdb_v_commands_table
,
1526 ARRAY_SIZE(gdb_v_commands_table
))) {
1531 static void handle_query_qemu_sstepbits(GArray
*params
, void *user_ctx
)
1533 g_string_printf(gdbserver_state
.str_buf
, "ENABLE=%x", SSTEP_ENABLE
);
1535 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOIRQ
) {
1536 g_string_append_printf(gdbserver_state
.str_buf
, ",NOIRQ=%x",
1540 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOTIMER
) {
1541 g_string_append_printf(gdbserver_state
.str_buf
, ",NOTIMER=%x",
1548 static void handle_set_qemu_sstep(GArray
*params
, void *user_ctx
)
1550 int new_sstep_flags
;
1556 new_sstep_flags
= get_param(params
, 0)->val_ul
;
1558 if (new_sstep_flags
& ~gdbserver_state
.supported_sstep_flags
) {
1559 gdb_put_packet("E22");
1563 gdbserver_state
.sstep_flags
= new_sstep_flags
;
1564 gdb_put_packet("OK");
1567 static void handle_query_qemu_sstep(GArray
*params
, void *user_ctx
)
1569 g_string_printf(gdbserver_state
.str_buf
, "0x%x",
1570 gdbserver_state
.sstep_flags
);
1574 static void handle_query_curr_tid(GArray
*params
, void *user_ctx
)
1577 GDBProcess
*process
;
1580 * "Current thread" remains vague in the spec, so always return
1581 * the first thread of the current process (gdb returns the
1584 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
1585 cpu
= gdb_get_first_cpu_in_process(process
);
1586 g_string_assign(gdbserver_state
.str_buf
, "QC");
1587 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
1591 static void handle_query_threads(GArray
*params
, void *user_ctx
)
1593 if (!gdbserver_state
.query_cpu
) {
1594 gdb_put_packet("l");
1598 g_string_assign(gdbserver_state
.str_buf
, "m");
1599 gdb_append_thread_id(gdbserver_state
.query_cpu
, gdbserver_state
.str_buf
);
1601 gdbserver_state
.query_cpu
= gdb_next_attached_cpu(gdbserver_state
.query_cpu
);
1604 static void handle_query_first_threads(GArray
*params
, void *user_ctx
)
1606 gdbserver_state
.query_cpu
= gdb_first_attached_cpu();
1607 handle_query_threads(params
, user_ctx
);
1610 static void handle_query_thread_extra(GArray
*params
, void *user_ctx
)
1612 g_autoptr(GString
) rs
= g_string_new(NULL
);
1616 get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1617 gdb_put_packet("E22");
1621 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
1622 get_param(params
, 0)->thread_id
.tid
);
1627 cpu_synchronize_state(cpu
);
1629 if (gdbserver_state
.multiprocess
&& (gdbserver_state
.process_num
> 1)) {
1630 /* Print the CPU model and name in multiprocess mode */
1631 ObjectClass
*oc
= object_get_class(OBJECT(cpu
));
1632 const char *cpu_model
= object_class_get_name(oc
);
1633 const char *cpu_name
=
1634 object_get_canonical_path_component(OBJECT(cpu
));
1635 g_string_printf(rs
, "%s %s [%s]", cpu_model
, cpu_name
,
1636 cpu
->halted
? "halted " : "running");
1638 g_string_printf(rs
, "CPU#%d [%s]", cpu
->cpu_index
,
1639 cpu
->halted
? "halted " : "running");
1641 trace_gdbstub_op_extra_info(rs
->str
);
1642 gdb_memtohex(gdbserver_state
.str_buf
, (uint8_t *)rs
->str
, rs
->len
);
1646 static void handle_query_supported(GArray
*params
, void *user_ctx
)
1650 g_string_printf(gdbserver_state
.str_buf
, "PacketSize=%x", MAX_PACKET_LENGTH
);
1651 cc
= CPU_GET_CLASS(first_cpu
);
1652 if (cc
->gdb_core_xml_file
) {
1653 g_string_append(gdbserver_state
.str_buf
, ";qXfer:features:read+");
1656 if (gdb_can_reverse()) {
1657 g_string_append(gdbserver_state
.str_buf
,
1658 ";ReverseStep+;ReverseContinue+");
1661 #if defined(CONFIG_USER_ONLY)
1662 #if defined(CONFIG_LINUX)
1663 if (gdbserver_state
.c_cpu
->opaque
) {
1664 g_string_append(gdbserver_state
.str_buf
, ";qXfer:auxv:read+");
1666 g_string_append(gdbserver_state
.str_buf
, ";QCatchSyscalls+");
1668 g_string_append(gdbserver_state
.str_buf
, ";qXfer:siginfo:read+");
1670 g_string_append(gdbserver_state
.str_buf
, ";qXfer:exec-file:read+");
1674 const char *gdb_supported
= get_param(params
, 0)->data
;
1676 if (strstr(gdb_supported
, "multiprocess+")) {
1677 gdbserver_state
.multiprocess
= true;
1679 #if defined(CONFIG_USER_ONLY)
1680 gdb_handle_query_supported_user(gdb_supported
);
1684 g_string_append(gdbserver_state
.str_buf
, ";vContSupported+;multiprocess+");
1688 static void handle_query_xfer_features(GArray
*params
, void *user_ctx
)
1690 GDBProcess
*process
;
1692 unsigned long len
, total_len
, addr
;
1696 if (params
->len
< 3) {
1697 gdb_put_packet("E22");
1701 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
1702 cc
= CPU_GET_CLASS(gdbserver_state
.g_cpu
);
1703 if (!cc
->gdb_core_xml_file
) {
1708 p
= get_param(params
, 0)->data
;
1709 xml
= get_feature_xml(p
, &p
, process
);
1711 gdb_put_packet("E00");
1715 addr
= get_param(params
, 1)->val_ul
;
1716 len
= get_param(params
, 2)->val_ul
;
1717 total_len
= strlen(xml
);
1718 if (addr
> total_len
) {
1719 gdb_put_packet("E00");
1723 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
1724 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1727 if (len
< total_len
- addr
) {
1728 g_string_assign(gdbserver_state
.str_buf
, "m");
1729 gdb_memtox(gdbserver_state
.str_buf
, xml
+ addr
, len
);
1731 g_string_assign(gdbserver_state
.str_buf
, "l");
1732 gdb_memtox(gdbserver_state
.str_buf
, xml
+ addr
, total_len
- addr
);
1735 gdb_put_packet_binary(gdbserver_state
.str_buf
->str
,
1736 gdbserver_state
.str_buf
->len
, true);
1739 static void handle_query_qemu_supported(GArray
*params
, void *user_ctx
)
1741 g_string_printf(gdbserver_state
.str_buf
, "sstepbits;sstep");
1742 #ifndef CONFIG_USER_ONLY
1743 g_string_append(gdbserver_state
.str_buf
, ";PhyMemMode");
1748 static const GdbCmdParseEntry gdb_gen_query_set_common_table
[] = {
1749 /* Order is important if has same prefix */
1751 .handler
= handle_query_qemu_sstepbits
,
1752 .cmd
= "qemu.sstepbits",
1755 .handler
= handle_query_qemu_sstep
,
1756 .cmd
= "qemu.sstep",
1759 .handler
= handle_set_qemu_sstep
,
1760 .cmd
= "qemu.sstep=",
1761 .cmd_startswith
= 1,
1766 static const GdbCmdParseEntry gdb_gen_query_table
[] = {
1768 .handler
= handle_query_curr_tid
,
1772 .handler
= handle_query_threads
,
1773 .cmd
= "sThreadInfo",
1776 .handler
= handle_query_first_threads
,
1777 .cmd
= "fThreadInfo",
1780 .handler
= handle_query_thread_extra
,
1781 .cmd
= "ThreadExtraInfo,",
1782 .cmd_startswith
= 1,
1785 #ifdef CONFIG_USER_ONLY
1787 .handler
= gdb_handle_query_offsets
,
1792 .handler
= gdb_handle_query_rcmd
,
1794 .cmd_startswith
= 1,
1799 .handler
= handle_query_supported
,
1800 .cmd
= "Supported:",
1801 .cmd_startswith
= 1,
1805 .handler
= handle_query_supported
,
1810 .handler
= handle_query_xfer_features
,
1811 .cmd
= "Xfer:features:read:",
1812 .cmd_startswith
= 1,
1815 #if defined(CONFIG_USER_ONLY)
1816 #if defined(CONFIG_LINUX)
1818 .handler
= gdb_handle_query_xfer_auxv
,
1819 .cmd
= "Xfer:auxv:read::",
1820 .cmd_startswith
= 1,
1824 .handler
= gdb_handle_query_xfer_siginfo
,
1825 .cmd
= "Xfer:siginfo:read::",
1826 .cmd_startswith
= 1,
1831 .handler
= gdb_handle_query_xfer_exec_file
,
1832 .cmd
= "Xfer:exec-file:read:",
1833 .cmd_startswith
= 1,
1838 .handler
= gdb_handle_query_attached
,
1843 .handler
= gdb_handle_query_attached
,
1847 .handler
= handle_query_qemu_supported
,
1848 .cmd
= "qemu.Supported",
1850 #ifndef CONFIG_USER_ONLY
1852 .handler
= gdb_handle_query_qemu_phy_mem_mode
,
1853 .cmd
= "qemu.PhyMemMode",
1858 static const GdbCmdParseEntry gdb_gen_set_table
[] = {
1859 /* Order is important if has same prefix */
1861 .handler
= handle_set_qemu_sstep
,
1862 .cmd
= "qemu.sstep:",
1863 .cmd_startswith
= 1,
1866 #ifndef CONFIG_USER_ONLY
1868 .handler
= gdb_handle_set_qemu_phy_mem_mode
,
1869 .cmd
= "qemu.PhyMemMode:",
1870 .cmd_startswith
= 1,
1874 #if defined(CONFIG_USER_ONLY)
1876 .handler
= gdb_handle_set_catch_syscalls
,
1877 .cmd
= "CatchSyscalls:",
1878 .cmd_startswith
= 1,
1884 static void handle_gen_query(GArray
*params
, void *user_ctx
)
1890 if (!process_string_cmd(get_param(params
, 0)->data
,
1891 gdb_gen_query_set_common_table
,
1892 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
1896 if (process_string_cmd(get_param(params
, 0)->data
,
1897 gdb_gen_query_table
,
1898 ARRAY_SIZE(gdb_gen_query_table
))) {
1903 static void handle_gen_set(GArray
*params
, void *user_ctx
)
1909 if (!process_string_cmd(get_param(params
, 0)->data
,
1910 gdb_gen_query_set_common_table
,
1911 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
1915 if (process_string_cmd(get_param(params
, 0)->data
,
1917 ARRAY_SIZE(gdb_gen_set_table
))) {
1922 static void handle_target_halt(GArray
*params
, void *user_ctx
)
1924 if (gdbserver_state
.allow_stop_reply
) {
1925 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
1926 gdb_append_thread_id(gdbserver_state
.c_cpu
, gdbserver_state
.str_buf
);
1927 g_string_append_c(gdbserver_state
.str_buf
, ';');
1929 gdbserver_state
.allow_stop_reply
= false;
1932 * Remove all the breakpoints when this query is issued,
1933 * because gdb is doing an initial connect and the state
1934 * should be cleaned up.
1936 gdb_breakpoint_remove_all(gdbserver_state
.c_cpu
);
1939 static int gdb_handle_packet(const char *line_buf
)
1941 const GdbCmdParseEntry
*cmd_parser
= NULL
;
1943 trace_gdbstub_io_command(line_buf
);
1945 switch (line_buf
[0]) {
1947 gdb_put_packet("OK");
1951 static const GdbCmdParseEntry target_halted_cmd_desc
= {
1952 .handler
= handle_target_halt
,
1954 .cmd_startswith
= 1,
1955 .allow_stop_reply
= true,
1957 cmd_parser
= &target_halted_cmd_desc
;
1962 static const GdbCmdParseEntry continue_cmd_desc
= {
1963 .handler
= handle_continue
,
1965 .cmd_startswith
= 1,
1966 .allow_stop_reply
= true,
1969 cmd_parser
= &continue_cmd_desc
;
1974 static const GdbCmdParseEntry cont_with_sig_cmd_desc
= {
1975 .handler
= handle_cont_with_sig
,
1977 .cmd_startswith
= 1,
1978 .allow_stop_reply
= true,
1981 cmd_parser
= &cont_with_sig_cmd_desc
;
1986 static const GdbCmdParseEntry v_cmd_desc
= {
1987 .handler
= handle_v_commands
,
1989 .cmd_startswith
= 1,
1992 cmd_parser
= &v_cmd_desc
;
1996 /* Kill the target */
1997 error_report("QEMU: Terminated via GDBstub");
2003 static const GdbCmdParseEntry detach_cmd_desc
= {
2004 .handler
= handle_detach
,
2006 .cmd_startswith
= 1,
2009 cmd_parser
= &detach_cmd_desc
;
2014 static const GdbCmdParseEntry step_cmd_desc
= {
2015 .handler
= handle_step
,
2017 .cmd_startswith
= 1,
2018 .allow_stop_reply
= true,
2021 cmd_parser
= &step_cmd_desc
;
2026 static const GdbCmdParseEntry backward_cmd_desc
= {
2027 .handler
= handle_backward
,
2029 .cmd_startswith
= 1,
2030 .allow_stop_reply
= true,
2033 cmd_parser
= &backward_cmd_desc
;
2038 static const GdbCmdParseEntry file_io_cmd_desc
= {
2039 .handler
= gdb_handle_file_io
,
2041 .cmd_startswith
= 1,
2044 cmd_parser
= &file_io_cmd_desc
;
2049 static const GdbCmdParseEntry read_all_regs_cmd_desc
= {
2050 .handler
= handle_read_all_regs
,
2054 cmd_parser
= &read_all_regs_cmd_desc
;
2059 static const GdbCmdParseEntry write_all_regs_cmd_desc
= {
2060 .handler
= handle_write_all_regs
,
2062 .cmd_startswith
= 1,
2065 cmd_parser
= &write_all_regs_cmd_desc
;
2070 static const GdbCmdParseEntry read_mem_cmd_desc
= {
2071 .handler
= handle_read_mem
,
2073 .cmd_startswith
= 1,
2076 cmd_parser
= &read_mem_cmd_desc
;
2081 static const GdbCmdParseEntry write_mem_cmd_desc
= {
2082 .handler
= handle_write_mem
,
2084 .cmd_startswith
= 1,
2087 cmd_parser
= &write_mem_cmd_desc
;
2092 static const GdbCmdParseEntry get_reg_cmd_desc
= {
2093 .handler
= handle_get_reg
,
2095 .cmd_startswith
= 1,
2098 cmd_parser
= &get_reg_cmd_desc
;
2103 static const GdbCmdParseEntry set_reg_cmd_desc
= {
2104 .handler
= handle_set_reg
,
2106 .cmd_startswith
= 1,
2109 cmd_parser
= &set_reg_cmd_desc
;
2114 static const GdbCmdParseEntry insert_bp_cmd_desc
= {
2115 .handler
= handle_insert_bp
,
2117 .cmd_startswith
= 1,
2120 cmd_parser
= &insert_bp_cmd_desc
;
2125 static const GdbCmdParseEntry remove_bp_cmd_desc
= {
2126 .handler
= handle_remove_bp
,
2128 .cmd_startswith
= 1,
2131 cmd_parser
= &remove_bp_cmd_desc
;
2136 static const GdbCmdParseEntry set_thread_cmd_desc
= {
2137 .handler
= handle_set_thread
,
2139 .cmd_startswith
= 1,
2142 cmd_parser
= &set_thread_cmd_desc
;
2147 static const GdbCmdParseEntry thread_alive_cmd_desc
= {
2148 .handler
= handle_thread_alive
,
2150 .cmd_startswith
= 1,
2153 cmd_parser
= &thread_alive_cmd_desc
;
2158 static const GdbCmdParseEntry gen_query_cmd_desc
= {
2159 .handler
= handle_gen_query
,
2161 .cmd_startswith
= 1,
2164 cmd_parser
= &gen_query_cmd_desc
;
2169 static const GdbCmdParseEntry gen_set_cmd_desc
= {
2170 .handler
= handle_gen_set
,
2172 .cmd_startswith
= 1,
2175 cmd_parser
= &gen_set_cmd_desc
;
2179 /* put empty packet */
2185 run_cmd_parser(line_buf
, cmd_parser
);
2191 void gdb_set_stop_cpu(CPUState
*cpu
)
2193 GDBProcess
*p
= gdb_get_cpu_process(cpu
);
2197 * Having a stop CPU corresponding to a process that is not attached
2198 * confuses GDB. So we ignore the request.
2203 gdbserver_state
.c_cpu
= cpu
;
2204 gdbserver_state
.g_cpu
= cpu
;
2207 void gdb_read_byte(uint8_t ch
)
2211 gdbserver_state
.allow_stop_reply
= false;
2212 #ifndef CONFIG_USER_ONLY
2213 if (gdbserver_state
.last_packet
->len
) {
2214 /* Waiting for a response to the last packet. If we see the start
2215 of a new command then abandon the previous response. */
2217 trace_gdbstub_err_got_nack();
2218 gdb_put_buffer(gdbserver_state
.last_packet
->data
,
2219 gdbserver_state
.last_packet
->len
);
2220 } else if (ch
== '+') {
2221 trace_gdbstub_io_got_ack();
2223 trace_gdbstub_io_got_unexpected(ch
);
2226 if (ch
== '+' || ch
== '$') {
2227 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
2232 if (runstate_is_running()) {
2234 * When the CPU is running, we cannot do anything except stop
2235 * it when receiving a char. This is expected on a Ctrl-C in the
2236 * gdb client. Because we are in all-stop mode, gdb sends a
2237 * 0x03 byte which is not a usual packet, so we handle it specially
2238 * here, but it does expect a stop reply.
2241 trace_gdbstub_err_unexpected_runpkt(ch
);
2243 gdbserver_state
.allow_stop_reply
= true;
2245 vm_stop(RUN_STATE_PAUSED
);
2249 switch(gdbserver_state
.state
) {
2252 /* start of command packet */
2253 gdbserver_state
.line_buf_index
= 0;
2254 gdbserver_state
.line_sum
= 0;
2255 gdbserver_state
.state
= RS_GETLINE
;
2256 } else if (ch
== '+') {
2258 * do nothing, gdb may preemptively send out ACKs on
2259 * initial connection
2262 trace_gdbstub_err_garbage(ch
);
2267 /* start escape sequence */
2268 gdbserver_state
.state
= RS_GETLINE_ESC
;
2269 gdbserver_state
.line_sum
+= ch
;
2270 } else if (ch
== '*') {
2271 /* start run length encoding sequence */
2272 gdbserver_state
.state
= RS_GETLINE_RLE
;
2273 gdbserver_state
.line_sum
+= ch
;
2274 } else if (ch
== '#') {
2275 /* end of command, start of checksum*/
2276 gdbserver_state
.state
= RS_CHKSUM1
;
2277 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2278 trace_gdbstub_err_overrun();
2279 gdbserver_state
.state
= RS_IDLE
;
2281 /* unescaped command character */
2282 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
;
2283 gdbserver_state
.line_sum
+= ch
;
2286 case RS_GETLINE_ESC
:
2288 /* unexpected end of command in escape sequence */
2289 gdbserver_state
.state
= RS_CHKSUM1
;
2290 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2291 /* command buffer overrun */
2292 trace_gdbstub_err_overrun();
2293 gdbserver_state
.state
= RS_IDLE
;
2295 /* parse escaped character and leave escape state */
2296 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
^ 0x20;
2297 gdbserver_state
.line_sum
+= ch
;
2298 gdbserver_state
.state
= RS_GETLINE
;
2301 case RS_GETLINE_RLE
:
2303 * Run-length encoding is explained in "Debugging with GDB /
2304 * Appendix E GDB Remote Serial Protocol / Overview".
2306 if (ch
< ' ' || ch
== '#' || ch
== '$' || ch
> 126) {
2307 /* invalid RLE count encoding */
2308 trace_gdbstub_err_invalid_repeat(ch
);
2309 gdbserver_state
.state
= RS_GETLINE
;
2311 /* decode repeat length */
2312 int repeat
= ch
- ' ' + 3;
2313 if (gdbserver_state
.line_buf_index
+ repeat
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2314 /* that many repeats would overrun the command buffer */
2315 trace_gdbstub_err_overrun();
2316 gdbserver_state
.state
= RS_IDLE
;
2317 } else if (gdbserver_state
.line_buf_index
< 1) {
2318 /* got a repeat but we have nothing to repeat */
2319 trace_gdbstub_err_invalid_rle();
2320 gdbserver_state
.state
= RS_GETLINE
;
2322 /* repeat the last character */
2323 memset(gdbserver_state
.line_buf
+ gdbserver_state
.line_buf_index
,
2324 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
- 1], repeat
);
2325 gdbserver_state
.line_buf_index
+= repeat
;
2326 gdbserver_state
.line_sum
+= ch
;
2327 gdbserver_state
.state
= RS_GETLINE
;
2332 /* get high hex digit of checksum */
2333 if (!isxdigit(ch
)) {
2334 trace_gdbstub_err_checksum_invalid(ch
);
2335 gdbserver_state
.state
= RS_GETLINE
;
2338 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
] = '\0';
2339 gdbserver_state
.line_csum
= fromhex(ch
) << 4;
2340 gdbserver_state
.state
= RS_CHKSUM2
;
2343 /* get low hex digit of checksum */
2344 if (!isxdigit(ch
)) {
2345 trace_gdbstub_err_checksum_invalid(ch
);
2346 gdbserver_state
.state
= RS_GETLINE
;
2349 gdbserver_state
.line_csum
|= fromhex(ch
);
2351 if (gdbserver_state
.line_csum
!= (gdbserver_state
.line_sum
& 0xff)) {
2352 trace_gdbstub_err_checksum_incorrect(gdbserver_state
.line_sum
, gdbserver_state
.line_csum
);
2353 /* send NAK reply */
2355 gdb_put_buffer(&reply
, 1);
2356 gdbserver_state
.state
= RS_IDLE
;
2358 /* send ACK reply */
2360 gdb_put_buffer(&reply
, 1);
2361 gdbserver_state
.state
= gdb_handle_packet(gdbserver_state
.line_buf
);
2371 * Create the process that will contain all the "orphan" CPUs (that are not
2372 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2373 * be attachable and thus will be invisible to the user.
2375 void gdb_create_default_process(GDBState
*s
)
2377 GDBProcess
*process
;
2380 #ifdef CONFIG_USER_ONLY
2381 assert(gdbserver_state
.process_num
== 0);
2384 if (gdbserver_state
.process_num
) {
2385 pid
= s
->processes
[s
->process_num
- 1].pid
;
2389 /* We need an available PID slot for this process */
2390 assert(pid
< UINT32_MAX
);
2394 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2395 process
= &s
->processes
[s
->process_num
- 1];
2397 process
->attached
= false;
2398 process
->target_xml
= NULL
;