4 * This implements a subset of the remote protocol as described in:
6 * https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
8 * Copyright (c) 2003-2005 Fabrice Bellard
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 * SPDX-License-Identifier: LGPL-2.0+
26 #include "qemu/osdep.h"
27 #include "qemu/ctype.h"
28 #include "qemu/cutils.h"
29 #include "qemu/module.h"
30 #include "qemu/error-report.h"
32 #include "exec/gdbstub.h"
33 #include "gdbstub/syscalls.h"
34 #ifdef CONFIG_USER_ONLY
35 #include "gdbstub/user.h"
37 #include "hw/cpu/cluster.h"
38 #include "hw/boards.h"
41 #include "sysemu/hw_accel.h"
42 #include "sysemu/runstate.h"
43 #include "exec/replay-core.h"
44 #include "exec/hwaddr.h"
46 #include "internals.h"
48 typedef struct GDBRegisterState
{
51 gdb_get_reg_cb get_reg
;
52 gdb_set_reg_cb set_reg
;
54 struct GDBRegisterState
*next
;
57 GDBState gdbserver_state
;
59 void gdb_init_gdbserver_state(void)
61 g_assert(!gdbserver_state
.init
);
62 memset(&gdbserver_state
, 0, sizeof(GDBState
));
63 gdbserver_state
.init
= true;
64 gdbserver_state
.str_buf
= g_string_new(NULL
);
65 gdbserver_state
.mem_buf
= g_byte_array_sized_new(MAX_PACKET_LENGTH
);
66 gdbserver_state
.last_packet
= g_byte_array_sized_new(MAX_PACKET_LENGTH
+ 4);
69 * What single-step modes are supported is accelerator dependent.
70 * By default try to use no IRQs and no timers while single
71 * stepping so as to make single stepping like a typical ICE HW step.
73 gdbserver_state
.supported_sstep_flags
= accel_supported_gdbstub_sstep_flags();
74 gdbserver_state
.sstep_flags
= SSTEP_ENABLE
| SSTEP_NOIRQ
| SSTEP_NOTIMER
;
75 gdbserver_state
.sstep_flags
&= gdbserver_state
.supported_sstep_flags
;
80 /* writes 2*len+1 bytes in buf */
81 void gdb_memtohex(GString
*buf
, const uint8_t *mem
, int len
)
84 for(i
= 0; i
< len
; i
++) {
86 g_string_append_c(buf
, tohex(c
>> 4));
87 g_string_append_c(buf
, tohex(c
& 0xf));
89 g_string_append_c(buf
, '\0');
92 void gdb_hextomem(GByteArray
*mem
, const char *buf
, int len
)
96 for(i
= 0; i
< len
; i
++) {
97 guint8 byte
= fromhex(buf
[0]) << 4 | fromhex(buf
[1]);
98 g_byte_array_append(mem
, &byte
, 1);
103 static void hexdump(const char *buf
, int len
,
104 void (*trace_fn
)(size_t ofs
, char const *text
))
106 char line_buffer
[3 * 16 + 4 + 16 + 1];
109 for (i
= 0; i
< len
|| (i
& 0xF); ++i
) {
110 size_t byte_ofs
= i
& 15;
113 memset(line_buffer
, ' ', 3 * 16 + 4 + 16);
114 line_buffer
[3 * 16 + 4 + 16] = 0;
117 size_t col_group
= (i
>> 2) & 3;
118 size_t hex_col
= byte_ofs
* 3 + col_group
;
119 size_t txt_col
= 3 * 16 + 4 + byte_ofs
;
124 line_buffer
[hex_col
+ 0] = tohex((value
>> 4) & 0xF);
125 line_buffer
[hex_col
+ 1] = tohex((value
>> 0) & 0xF);
126 line_buffer
[txt_col
+ 0] = (value
>= ' ' && value
< 127)
132 trace_fn(i
& -16, line_buffer
);
136 /* return -1 if error, 0 if OK */
137 int gdb_put_packet_binary(const char *buf
, int len
, bool dump
)
142 if (dump
&& trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY
)) {
143 hexdump(buf
, len
, trace_gdbstub_io_binaryreply
);
147 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
148 g_byte_array_append(gdbserver_state
.last_packet
,
149 (const uint8_t *) "$", 1);
150 g_byte_array_append(gdbserver_state
.last_packet
,
151 (const uint8_t *) buf
, len
);
153 for(i
= 0; i
< len
; i
++) {
157 footer
[1] = tohex((csum
>> 4) & 0xf);
158 footer
[2] = tohex((csum
) & 0xf);
159 g_byte_array_append(gdbserver_state
.last_packet
, footer
, 3);
161 gdb_put_buffer(gdbserver_state
.last_packet
->data
,
162 gdbserver_state
.last_packet
->len
);
164 if (gdb_got_immediate_ack()) {
171 /* return -1 if error, 0 if OK */
172 int gdb_put_packet(const char *buf
)
174 trace_gdbstub_io_reply(buf
);
176 return gdb_put_packet_binary(buf
, strlen(buf
), false);
179 void gdb_put_strbuf(void)
181 gdb_put_packet(gdbserver_state
.str_buf
->str
);
184 /* Encode data using the encoding for 'x' packets. */
185 void gdb_memtox(GString
*buf
, const char *mem
, int len
)
192 case '#': case '$': case '*': case '}':
193 g_string_append_c(buf
, '}');
194 g_string_append_c(buf
, c
^ 0x20);
197 g_string_append_c(buf
, c
);
203 static uint32_t gdb_get_cpu_pid(CPUState
*cpu
)
205 #ifdef CONFIG_USER_ONLY
208 if (cpu
->cluster_index
== UNASSIGNED_CLUSTER_INDEX
) {
209 /* Return the default process' PID */
210 int index
= gdbserver_state
.process_num
- 1;
211 return gdbserver_state
.processes
[index
].pid
;
213 return cpu
->cluster_index
+ 1;
217 GDBProcess
*gdb_get_process(uint32_t pid
)
222 /* 0 means any process, we take the first one */
223 return &gdbserver_state
.processes
[0];
226 for (i
= 0; i
< gdbserver_state
.process_num
; i
++) {
227 if (gdbserver_state
.processes
[i
].pid
== pid
) {
228 return &gdbserver_state
.processes
[i
];
235 static GDBProcess
*gdb_get_cpu_process(CPUState
*cpu
)
237 return gdb_get_process(gdb_get_cpu_pid(cpu
));
240 static CPUState
*find_cpu(uint32_t thread_id
)
245 if (gdb_get_cpu_index(cpu
) == thread_id
) {
253 CPUState
*gdb_get_first_cpu_in_process(GDBProcess
*process
)
258 if (gdb_get_cpu_pid(cpu
) == process
->pid
) {
266 static CPUState
*gdb_next_cpu_in_process(CPUState
*cpu
)
268 uint32_t pid
= gdb_get_cpu_pid(cpu
);
272 if (gdb_get_cpu_pid(cpu
) == pid
) {
282 /* Return the cpu following @cpu, while ignoring unattached processes. */
283 static CPUState
*gdb_next_attached_cpu(CPUState
*cpu
)
288 if (gdb_get_cpu_process(cpu
)->attached
) {
298 /* Return the first attached cpu */
299 CPUState
*gdb_first_attached_cpu(void)
301 CPUState
*cpu
= first_cpu
;
302 GDBProcess
*process
= gdb_get_cpu_process(cpu
);
304 if (!process
->attached
) {
305 return gdb_next_attached_cpu(cpu
);
311 static CPUState
*gdb_get_cpu(uint32_t pid
, uint32_t tid
)
317 /* 0 means any process/thread, we take the first attached one */
318 return gdb_first_attached_cpu();
319 } else if (pid
&& !tid
) {
320 /* any thread in a specific process */
321 process
= gdb_get_process(pid
);
323 if (process
== NULL
) {
327 if (!process
->attached
) {
331 return gdb_get_first_cpu_in_process(process
);
333 /* a specific thread */
340 process
= gdb_get_cpu_process(cpu
);
342 if (pid
&& process
->pid
!= pid
) {
346 if (!process
->attached
) {
354 static const char *get_feature_xml(const char *p
, const char **newp
,
360 CPUState
*cpu
= gdb_get_first_cpu_in_process(process
);
361 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
364 while (p
[len
] && p
[len
] != ':')
369 if (strncmp(p
, "target.xml", len
) == 0) {
370 char *buf
= process
->target_xml
;
371 const size_t buf_sz
= sizeof(process
->target_xml
);
373 /* Generate the XML description for this CPU. */
378 "<?xml version=\"1.0\"?>"
379 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
381 if (cc
->gdb_arch_name
) {
382 gchar
*arch
= cc
->gdb_arch_name(cpu
);
383 pstrcat(buf
, buf_sz
, "<architecture>");
384 pstrcat(buf
, buf_sz
, arch
);
385 pstrcat(buf
, buf_sz
, "</architecture>");
388 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
389 pstrcat(buf
, buf_sz
, cc
->gdb_core_xml_file
);
390 pstrcat(buf
, buf_sz
, "\"/>");
391 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
392 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
393 pstrcat(buf
, buf_sz
, r
->xml
);
394 pstrcat(buf
, buf_sz
, "\"/>");
396 pstrcat(buf
, buf_sz
, "</target>");
400 if (cc
->gdb_get_dynamic_xml
) {
401 char *xmlname
= g_strndup(p
, len
);
402 const char *xml
= cc
->gdb_get_dynamic_xml(cpu
, xmlname
);
410 name
= xml_builtin
[i
][0];
411 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
414 return name
? xml_builtin
[i
][1] : NULL
;
417 static int gdb_read_register(CPUState
*cpu
, GByteArray
*buf
, int reg
)
419 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
420 CPUArchState
*env
= cpu
->env_ptr
;
423 if (reg
< cc
->gdb_num_core_regs
) {
424 return cc
->gdb_read_register(cpu
, buf
, reg
);
427 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
428 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
429 return r
->get_reg(env
, buf
, reg
- r
->base_reg
);
435 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
437 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
438 CPUArchState
*env
= cpu
->env_ptr
;
441 if (reg
< cc
->gdb_num_core_regs
) {
442 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
445 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
446 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
447 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
453 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
454 specifies the first register number and these registers are included in
455 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
456 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
459 void gdb_register_coprocessor(CPUState
*cpu
,
460 gdb_get_reg_cb get_reg
, gdb_set_reg_cb set_reg
,
461 int num_regs
, const char *xml
, int g_pos
)
464 GDBRegisterState
**p
;
468 /* Check for duplicates. */
469 if (strcmp((*p
)->xml
, xml
) == 0)
474 s
= g_new0(GDBRegisterState
, 1);
475 s
->base_reg
= cpu
->gdb_num_regs
;
476 s
->num_regs
= num_regs
;
477 s
->get_reg
= get_reg
;
478 s
->set_reg
= set_reg
;
481 /* Add to end of list. */
482 cpu
->gdb_num_regs
+= num_regs
;
485 if (g_pos
!= s
->base_reg
) {
486 error_report("Error: Bad gdb register numbering for '%s', "
487 "expected %d got %d", xml
, g_pos
, s
->base_reg
);
489 cpu
->gdb_num_g_regs
= cpu
->gdb_num_regs
;
494 static void gdb_process_breakpoint_remove_all(GDBProcess
*p
)
496 CPUState
*cpu
= gdb_get_first_cpu_in_process(p
);
499 gdb_breakpoint_remove_all(cpu
);
500 cpu
= gdb_next_cpu_in_process(cpu
);
505 static void gdb_set_cpu_pc(vaddr pc
)
507 CPUState
*cpu
= gdbserver_state
.c_cpu
;
509 cpu_synchronize_state(cpu
);
513 void gdb_append_thread_id(CPUState
*cpu
, GString
*buf
)
515 if (gdbserver_state
.multiprocess
) {
516 g_string_append_printf(buf
, "p%02x.%02x",
517 gdb_get_cpu_pid(cpu
), gdb_get_cpu_index(cpu
));
519 g_string_append_printf(buf
, "%02x", gdb_get_cpu_index(cpu
));
523 static GDBThreadIdKind
read_thread_id(const char *buf
, const char **end_buf
,
524 uint32_t *pid
, uint32_t *tid
)
531 ret
= qemu_strtoul(buf
, &buf
, 16, &p
);
534 return GDB_READ_THREAD_ERR
;
543 ret
= qemu_strtoul(buf
, &buf
, 16, &t
);
546 return GDB_READ_THREAD_ERR
;
552 return GDB_ALL_PROCESSES
;
560 return GDB_ALL_THREADS
;
567 return GDB_ONE_THREAD
;
571 * gdb_handle_vcont - Parses and handles a vCont packet.
572 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
573 * a format error, 0 on success.
575 static int gdb_handle_vcont(const char *p
)
583 GDBThreadIdKind kind
;
584 unsigned int max_cpus
= gdb_get_max_cpus();
585 /* uninitialised CPUs stay 0 */
586 g_autofree
char *newstates
= g_new0(char, max_cpus
);
588 /* mark valid CPUs with 1 */
590 newstates
[cpu
->cpu_index
] = 1;
594 * res keeps track of what error we are returning, with -ENOTSUP meaning
595 * that the command is unknown or unsupported, thus returning an empty
596 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
597 * or incorrect parameters passed.
606 if (cur_action
== 'C' || cur_action
== 'S') {
607 cur_action
= qemu_tolower(cur_action
);
608 res
= qemu_strtoul(p
, &p
, 16, &tmp
);
612 signal
= gdb_signal_to_target(tmp
);
613 } else if (cur_action
!= 'c' && cur_action
!= 's') {
614 /* unknown/invalid/unsupported command */
618 if (*p
== '\0' || *p
== ';') {
620 * No thread specifier, action is on "all threads". The
621 * specification is unclear regarding the process to act on. We
622 * choose all processes.
624 kind
= GDB_ALL_PROCESSES
;
625 } else if (*p
++ == ':') {
626 kind
= read_thread_id(p
, &p
, &pid
, &tid
);
632 case GDB_READ_THREAD_ERR
:
635 case GDB_ALL_PROCESSES
:
636 cpu
= gdb_first_attached_cpu();
638 if (newstates
[cpu
->cpu_index
] == 1) {
639 newstates
[cpu
->cpu_index
] = cur_action
;
642 cpu
= gdb_next_attached_cpu(cpu
);
646 case GDB_ALL_THREADS
:
647 process
= gdb_get_process(pid
);
649 if (!process
->attached
) {
653 cpu
= gdb_get_first_cpu_in_process(process
);
655 if (newstates
[cpu
->cpu_index
] == 1) {
656 newstates
[cpu
->cpu_index
] = cur_action
;
659 cpu
= gdb_next_cpu_in_process(cpu
);
664 cpu
= gdb_get_cpu(pid
, tid
);
666 /* invalid CPU/thread specified */
671 /* only use if no previous match occourred */
672 if (newstates
[cpu
->cpu_index
] == 1) {
673 newstates
[cpu
->cpu_index
] = cur_action
;
679 gdbserver_state
.signal
= signal
;
680 gdb_continue_partial(newstates
);
684 static const char *cmd_next_param(const char *param
, const char delimiter
)
686 static const char all_delimiters
[] = ",;:=";
687 char curr_delimiters
[2] = {0};
688 const char *delimiters
;
690 if (delimiter
== '?') {
691 delimiters
= all_delimiters
;
692 } else if (delimiter
== '0') {
693 return strchr(param
, '\0');
694 } else if (delimiter
== '.' && *param
) {
697 curr_delimiters
[0] = delimiter
;
698 delimiters
= curr_delimiters
;
701 param
+= strcspn(param
, delimiters
);
708 static int cmd_parse_params(const char *data
, const char *schema
,
711 const char *curr_schema
, *curr_data
;
714 g_assert(params
->len
== 0);
716 curr_schema
= schema
;
718 while (curr_schema
[0] && curr_schema
[1] && *curr_data
) {
719 GdbCmdVariant this_param
;
721 switch (curr_schema
[0]) {
723 if (qemu_strtoul(curr_data
, &curr_data
, 16,
724 &this_param
.val_ul
)) {
727 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
728 g_array_append_val(params
, this_param
);
731 if (qemu_strtou64(curr_data
, &curr_data
, 16,
732 (uint64_t *)&this_param
.val_ull
)) {
735 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
736 g_array_append_val(params
, this_param
);
739 this_param
.data
= curr_data
;
740 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
741 g_array_append_val(params
, this_param
);
744 this_param
.opcode
= *(uint8_t *)curr_data
;
745 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
746 g_array_append_val(params
, this_param
);
749 this_param
.thread_id
.kind
=
750 read_thread_id(curr_data
, &curr_data
,
751 &this_param
.thread_id
.pid
,
752 &this_param
.thread_id
.tid
);
753 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
754 g_array_append_val(params
, this_param
);
757 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
768 typedef void (*GdbCmdHandler
)(GArray
*params
, void *user_ctx
);
771 * cmd_startswith -> cmd is compared using startswith
773 * allow_stop_reply -> true iff the gdbstub can respond to this command with a
774 * "stop reply" packet. The list of commands that accept such response is
775 * defined at the GDB Remote Serial Protocol documentation. see:
776 * https://sourceware.org/gdb/onlinedocs/gdb/Stop-Reply-Packets.html#Stop-Reply-Packets.
778 * schema definitions:
779 * Each schema parameter entry consists of 2 chars,
780 * the first char represents the parameter type handling
781 * the second char represents the delimiter for the next parameter
783 * Currently supported schema types:
784 * 'l' -> unsigned long (stored in .val_ul)
785 * 'L' -> unsigned long long (stored in .val_ull)
786 * 's' -> string (stored in .data)
787 * 'o' -> single char (stored in .opcode)
788 * 't' -> thread id (stored in .thread_id)
789 * '?' -> skip according to delimiter
791 * Currently supported delimiters:
792 * '?' -> Stop at any delimiter (",;:=\0")
793 * '0' -> Stop at "\0"
794 * '.' -> Skip 1 char unless reached "\0"
795 * Any other value is treated as the delimiter value itself
797 typedef struct GdbCmdParseEntry
{
798 GdbCmdHandler handler
;
802 bool allow_stop_reply
;
805 static inline int startswith(const char *string
, const char *pattern
)
807 return !strncmp(string
, pattern
, strlen(pattern
));
810 static int process_string_cmd(void *user_ctx
, const char *data
,
811 const GdbCmdParseEntry
*cmds
, int num_cmds
)
814 g_autoptr(GArray
) params
= g_array_new(false, true, sizeof(GdbCmdVariant
));
820 for (i
= 0; i
< num_cmds
; i
++) {
821 const GdbCmdParseEntry
*cmd
= &cmds
[i
];
822 g_assert(cmd
->handler
&& cmd
->cmd
);
824 if ((cmd
->cmd_startswith
&& !startswith(data
, cmd
->cmd
)) ||
825 (!cmd
->cmd_startswith
&& strcmp(cmd
->cmd
, data
))) {
830 if (cmd_parse_params(&data
[strlen(cmd
->cmd
)],
831 cmd
->schema
, params
)) {
836 gdbserver_state
.allow_stop_reply
= cmd
->allow_stop_reply
;
837 cmd
->handler(params
, user_ctx
);
844 static void run_cmd_parser(const char *data
, const GdbCmdParseEntry
*cmd
)
850 g_string_set_size(gdbserver_state
.str_buf
, 0);
851 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
853 /* In case there was an error during the command parsing we must
854 * send a NULL packet to indicate the command is not supported */
855 if (process_string_cmd(NULL
, data
, cmd
, 1)) {
860 static void handle_detach(GArray
*params
, void *user_ctx
)
865 if (gdbserver_state
.multiprocess
) {
867 gdb_put_packet("E22");
871 pid
= get_param(params
, 0)->val_ul
;
874 process
= gdb_get_process(pid
);
875 gdb_process_breakpoint_remove_all(process
);
876 process
->attached
= false;
878 if (pid
== gdb_get_cpu_pid(gdbserver_state
.c_cpu
)) {
879 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
882 if (pid
== gdb_get_cpu_pid(gdbserver_state
.g_cpu
)) {
883 gdbserver_state
.g_cpu
= gdb_first_attached_cpu();
886 if (!gdbserver_state
.c_cpu
) {
887 /* No more process attached */
888 gdb_disable_syscalls();
891 gdb_put_packet("OK");
894 static void handle_thread_alive(GArray
*params
, void *user_ctx
)
899 gdb_put_packet("E22");
903 if (get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
904 gdb_put_packet("E22");
908 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
909 get_param(params
, 0)->thread_id
.tid
);
911 gdb_put_packet("E22");
915 gdb_put_packet("OK");
918 static void handle_continue(GArray
*params
, void *user_ctx
)
921 gdb_set_cpu_pc(get_param(params
, 0)->val_ull
);
924 gdbserver_state
.signal
= 0;
928 static void handle_cont_with_sig(GArray
*params
, void *user_ctx
)
930 unsigned long signal
= 0;
933 * Note: C sig;[addr] is currently unsupported and we simply
934 * omit the addr parameter
937 signal
= get_param(params
, 0)->val_ul
;
940 gdbserver_state
.signal
= gdb_signal_to_target(signal
);
941 if (gdbserver_state
.signal
== -1) {
942 gdbserver_state
.signal
= 0;
947 static void handle_set_thread(GArray
*params
, void *user_ctx
)
951 if (params
->len
!= 2) {
952 gdb_put_packet("E22");
956 if (get_param(params
, 1)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
957 gdb_put_packet("E22");
961 if (get_param(params
, 1)->thread_id
.kind
!= GDB_ONE_THREAD
) {
962 gdb_put_packet("OK");
966 cpu
= gdb_get_cpu(get_param(params
, 1)->thread_id
.pid
,
967 get_param(params
, 1)->thread_id
.tid
);
969 gdb_put_packet("E22");
974 * Note: This command is deprecated and modern gdb's will be using the
975 * vCont command instead.
977 switch (get_param(params
, 0)->opcode
) {
979 gdbserver_state
.c_cpu
= cpu
;
980 gdb_put_packet("OK");
983 gdbserver_state
.g_cpu
= cpu
;
984 gdb_put_packet("OK");
987 gdb_put_packet("E22");
992 static void handle_insert_bp(GArray
*params
, void *user_ctx
)
996 if (params
->len
!= 3) {
997 gdb_put_packet("E22");
1001 res
= gdb_breakpoint_insert(gdbserver_state
.c_cpu
,
1002 get_param(params
, 0)->val_ul
,
1003 get_param(params
, 1)->val_ull
,
1004 get_param(params
, 2)->val_ull
);
1006 gdb_put_packet("OK");
1008 } else if (res
== -ENOSYS
) {
1013 gdb_put_packet("E22");
1016 static void handle_remove_bp(GArray
*params
, void *user_ctx
)
1020 if (params
->len
!= 3) {
1021 gdb_put_packet("E22");
1025 res
= gdb_breakpoint_remove(gdbserver_state
.c_cpu
,
1026 get_param(params
, 0)->val_ul
,
1027 get_param(params
, 1)->val_ull
,
1028 get_param(params
, 2)->val_ull
);
1030 gdb_put_packet("OK");
1032 } else if (res
== -ENOSYS
) {
1037 gdb_put_packet("E22");
1041 * handle_set/get_reg
1043 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1044 * This works, but can be very slow. Anything new enough to understand
1045 * XML also knows how to use this properly. However to use this we
1046 * need to define a local XML file as well as be talking to a
1047 * reasonably modern gdb. Responding with an empty packet will cause
1048 * the remote gdb to fallback to older methods.
1051 static void handle_set_reg(GArray
*params
, void *user_ctx
)
1060 if (params
->len
!= 2) {
1061 gdb_put_packet("E22");
1065 reg_size
= strlen(get_param(params
, 1)->data
) / 2;
1066 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 1)->data
, reg_size
);
1067 gdb_write_register(gdbserver_state
.g_cpu
, gdbserver_state
.mem_buf
->data
,
1068 get_param(params
, 0)->val_ull
);
1069 gdb_put_packet("OK");
1072 static void handle_get_reg(GArray
*params
, void *user_ctx
)
1082 gdb_put_packet("E14");
1086 reg_size
= gdb_read_register(gdbserver_state
.g_cpu
,
1087 gdbserver_state
.mem_buf
,
1088 get_param(params
, 0)->val_ull
);
1090 gdb_put_packet("E14");
1093 g_byte_array_set_size(gdbserver_state
.mem_buf
, reg_size
);
1096 gdb_memtohex(gdbserver_state
.str_buf
,
1097 gdbserver_state
.mem_buf
->data
, reg_size
);
1101 static void handle_write_mem(GArray
*params
, void *user_ctx
)
1103 if (params
->len
!= 3) {
1104 gdb_put_packet("E22");
1108 /* gdb_hextomem() reads 2*len bytes */
1109 if (get_param(params
, 1)->val_ull
>
1110 strlen(get_param(params
, 2)->data
) / 2) {
1111 gdb_put_packet("E22");
1115 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 2)->data
,
1116 get_param(params
, 1)->val_ull
);
1117 if (gdb_target_memory_rw_debug(gdbserver_state
.g_cpu
,
1118 get_param(params
, 0)->val_ull
,
1119 gdbserver_state
.mem_buf
->data
,
1120 gdbserver_state
.mem_buf
->len
, true)) {
1121 gdb_put_packet("E14");
1125 gdb_put_packet("OK");
1128 static void handle_read_mem(GArray
*params
, void *user_ctx
)
1130 if (params
->len
!= 2) {
1131 gdb_put_packet("E22");
1135 /* gdb_memtohex() doubles the required space */
1136 if (get_param(params
, 1)->val_ull
> MAX_PACKET_LENGTH
/ 2) {
1137 gdb_put_packet("E22");
1141 g_byte_array_set_size(gdbserver_state
.mem_buf
,
1142 get_param(params
, 1)->val_ull
);
1144 if (gdb_target_memory_rw_debug(gdbserver_state
.g_cpu
,
1145 get_param(params
, 0)->val_ull
,
1146 gdbserver_state
.mem_buf
->data
,
1147 gdbserver_state
.mem_buf
->len
, false)) {
1148 gdb_put_packet("E14");
1152 gdb_memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
,
1153 gdbserver_state
.mem_buf
->len
);
1157 static void handle_write_all_regs(GArray
*params
, void *user_ctx
)
1168 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1169 len
= strlen(get_param(params
, 0)->data
) / 2;
1170 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 0)->data
, len
);
1171 registers
= gdbserver_state
.mem_buf
->data
;
1173 reg_id
< gdbserver_state
.g_cpu
->gdb_num_g_regs
&& len
> 0;
1175 reg_size
= gdb_write_register(gdbserver_state
.g_cpu
, registers
, reg_id
);
1177 registers
+= reg_size
;
1179 gdb_put_packet("OK");
1182 static void handle_read_all_regs(GArray
*params
, void *user_ctx
)
1187 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1188 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
1190 for (reg_id
= 0; reg_id
< gdbserver_state
.g_cpu
->gdb_num_g_regs
; reg_id
++) {
1191 len
+= gdb_read_register(gdbserver_state
.g_cpu
,
1192 gdbserver_state
.mem_buf
,
1195 g_assert(len
== gdbserver_state
.mem_buf
->len
);
1197 gdb_memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
, len
);
1202 static void handle_step(GArray
*params
, void *user_ctx
)
1205 gdb_set_cpu_pc(get_param(params
, 0)->val_ull
);
1208 cpu_single_step(gdbserver_state
.c_cpu
, gdbserver_state
.sstep_flags
);
1212 static void handle_backward(GArray
*params
, void *user_ctx
)
1214 if (!gdb_can_reverse()) {
1215 gdb_put_packet("E22");
1217 if (params
->len
== 1) {
1218 switch (get_param(params
, 0)->opcode
) {
1220 if (replay_reverse_step()) {
1223 gdb_put_packet("E14");
1227 if (replay_reverse_continue()) {
1230 gdb_put_packet("E14");
1236 /* Default invalid command */
1240 static void handle_v_cont_query(GArray
*params
, void *user_ctx
)
1242 gdb_put_packet("vCont;c;C;s;S");
1245 static void handle_v_cont(GArray
*params
, void *user_ctx
)
1253 res
= gdb_handle_vcont(get_param(params
, 0)->data
);
1254 if ((res
== -EINVAL
) || (res
== -ERANGE
)) {
1255 gdb_put_packet("E22");
1261 static void handle_v_attach(GArray
*params
, void *user_ctx
)
1263 GDBProcess
*process
;
1266 g_string_assign(gdbserver_state
.str_buf
, "E22");
1271 process
= gdb_get_process(get_param(params
, 0)->val_ul
);
1276 cpu
= gdb_get_first_cpu_in_process(process
);
1281 process
->attached
= true;
1282 gdbserver_state
.g_cpu
= cpu
;
1283 gdbserver_state
.c_cpu
= cpu
;
1285 if (gdbserver_state
.allow_stop_reply
) {
1286 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
1287 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
1288 g_string_append_c(gdbserver_state
.str_buf
, ';');
1289 gdbserver_state
.allow_stop_reply
= false;
1295 static void handle_v_kill(GArray
*params
, void *user_ctx
)
1297 /* Kill the target */
1298 gdb_put_packet("OK");
1299 error_report("QEMU: Terminated via GDBstub");
1304 static const GdbCmdParseEntry gdb_v_commands_table
[] = {
1305 /* Order is important if has same prefix */
1307 .handler
= handle_v_cont_query
,
1312 .handler
= handle_v_cont
,
1314 .cmd_startswith
= 1,
1315 .allow_stop_reply
= true,
1319 .handler
= handle_v_attach
,
1321 .cmd_startswith
= 1,
1322 .allow_stop_reply
= true,
1326 .handler
= handle_v_kill
,
1332 static void handle_v_commands(GArray
*params
, void *user_ctx
)
1338 if (process_string_cmd(NULL
, get_param(params
, 0)->data
,
1339 gdb_v_commands_table
,
1340 ARRAY_SIZE(gdb_v_commands_table
))) {
1345 static void handle_query_qemu_sstepbits(GArray
*params
, void *user_ctx
)
1347 g_string_printf(gdbserver_state
.str_buf
, "ENABLE=%x", SSTEP_ENABLE
);
1349 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOIRQ
) {
1350 g_string_append_printf(gdbserver_state
.str_buf
, ",NOIRQ=%x",
1354 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOTIMER
) {
1355 g_string_append_printf(gdbserver_state
.str_buf
, ",NOTIMER=%x",
1362 static void handle_set_qemu_sstep(GArray
*params
, void *user_ctx
)
1364 int new_sstep_flags
;
1370 new_sstep_flags
= get_param(params
, 0)->val_ul
;
1372 if (new_sstep_flags
& ~gdbserver_state
.supported_sstep_flags
) {
1373 gdb_put_packet("E22");
1377 gdbserver_state
.sstep_flags
= new_sstep_flags
;
1378 gdb_put_packet("OK");
1381 static void handle_query_qemu_sstep(GArray
*params
, void *user_ctx
)
1383 g_string_printf(gdbserver_state
.str_buf
, "0x%x",
1384 gdbserver_state
.sstep_flags
);
1388 static void handle_query_curr_tid(GArray
*params
, void *user_ctx
)
1391 GDBProcess
*process
;
1394 * "Current thread" remains vague in the spec, so always return
1395 * the first thread of the current process (gdb returns the
1398 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
1399 cpu
= gdb_get_first_cpu_in_process(process
);
1400 g_string_assign(gdbserver_state
.str_buf
, "QC");
1401 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
1405 static void handle_query_threads(GArray
*params
, void *user_ctx
)
1407 if (!gdbserver_state
.query_cpu
) {
1408 gdb_put_packet("l");
1412 g_string_assign(gdbserver_state
.str_buf
, "m");
1413 gdb_append_thread_id(gdbserver_state
.query_cpu
, gdbserver_state
.str_buf
);
1415 gdbserver_state
.query_cpu
= gdb_next_attached_cpu(gdbserver_state
.query_cpu
);
1418 static void handle_query_first_threads(GArray
*params
, void *user_ctx
)
1420 gdbserver_state
.query_cpu
= gdb_first_attached_cpu();
1421 handle_query_threads(params
, user_ctx
);
1424 static void handle_query_thread_extra(GArray
*params
, void *user_ctx
)
1426 g_autoptr(GString
) rs
= g_string_new(NULL
);
1430 get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1431 gdb_put_packet("E22");
1435 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
1436 get_param(params
, 0)->thread_id
.tid
);
1441 cpu_synchronize_state(cpu
);
1443 if (gdbserver_state
.multiprocess
&& (gdbserver_state
.process_num
> 1)) {
1444 /* Print the CPU model and name in multiprocess mode */
1445 ObjectClass
*oc
= object_get_class(OBJECT(cpu
));
1446 const char *cpu_model
= object_class_get_name(oc
);
1447 const char *cpu_name
=
1448 object_get_canonical_path_component(OBJECT(cpu
));
1449 g_string_printf(rs
, "%s %s [%s]", cpu_model
, cpu_name
,
1450 cpu
->halted
? "halted " : "running");
1452 g_string_printf(rs
, "CPU#%d [%s]", cpu
->cpu_index
,
1453 cpu
->halted
? "halted " : "running");
1455 trace_gdbstub_op_extra_info(rs
->str
);
1456 gdb_memtohex(gdbserver_state
.str_buf
, (uint8_t *)rs
->str
, rs
->len
);
1460 static void handle_query_supported(GArray
*params
, void *user_ctx
)
1464 g_string_printf(gdbserver_state
.str_buf
, "PacketSize=%x", MAX_PACKET_LENGTH
);
1465 cc
= CPU_GET_CLASS(first_cpu
);
1466 if (cc
->gdb_core_xml_file
) {
1467 g_string_append(gdbserver_state
.str_buf
, ";qXfer:features:read+");
1470 if (gdb_can_reverse()) {
1471 g_string_append(gdbserver_state
.str_buf
,
1472 ";ReverseStep+;ReverseContinue+");
1475 #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX)
1476 if (gdbserver_state
.c_cpu
->opaque
) {
1477 g_string_append(gdbserver_state
.str_buf
, ";qXfer:auxv:read+");
1482 strstr(get_param(params
, 0)->data
, "multiprocess+")) {
1483 gdbserver_state
.multiprocess
= true;
1486 g_string_append(gdbserver_state
.str_buf
, ";vContSupported+;multiprocess+");
1490 static void handle_query_xfer_features(GArray
*params
, void *user_ctx
)
1492 GDBProcess
*process
;
1494 unsigned long len
, total_len
, addr
;
1498 if (params
->len
< 3) {
1499 gdb_put_packet("E22");
1503 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
1504 cc
= CPU_GET_CLASS(gdbserver_state
.g_cpu
);
1505 if (!cc
->gdb_core_xml_file
) {
1511 p
= get_param(params
, 0)->data
;
1512 xml
= get_feature_xml(p
, &p
, process
);
1514 gdb_put_packet("E00");
1518 addr
= get_param(params
, 1)->val_ul
;
1519 len
= get_param(params
, 2)->val_ul
;
1520 total_len
= strlen(xml
);
1521 if (addr
> total_len
) {
1522 gdb_put_packet("E00");
1526 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
1527 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1530 if (len
< total_len
- addr
) {
1531 g_string_assign(gdbserver_state
.str_buf
, "m");
1532 gdb_memtox(gdbserver_state
.str_buf
, xml
+ addr
, len
);
1534 g_string_assign(gdbserver_state
.str_buf
, "l");
1535 gdb_memtox(gdbserver_state
.str_buf
, xml
+ addr
, total_len
- addr
);
1538 gdb_put_packet_binary(gdbserver_state
.str_buf
->str
,
1539 gdbserver_state
.str_buf
->len
, true);
1542 static void handle_query_qemu_supported(GArray
*params
, void *user_ctx
)
1544 g_string_printf(gdbserver_state
.str_buf
, "sstepbits;sstep");
1545 #ifndef CONFIG_USER_ONLY
1546 g_string_append(gdbserver_state
.str_buf
, ";PhyMemMode");
1551 static const GdbCmdParseEntry gdb_gen_query_set_common_table
[] = {
1552 /* Order is important if has same prefix */
1554 .handler
= handle_query_qemu_sstepbits
,
1555 .cmd
= "qemu.sstepbits",
1558 .handler
= handle_query_qemu_sstep
,
1559 .cmd
= "qemu.sstep",
1562 .handler
= handle_set_qemu_sstep
,
1563 .cmd
= "qemu.sstep=",
1564 .cmd_startswith
= 1,
1569 static const GdbCmdParseEntry gdb_gen_query_table
[] = {
1571 .handler
= handle_query_curr_tid
,
1575 .handler
= handle_query_threads
,
1576 .cmd
= "sThreadInfo",
1579 .handler
= handle_query_first_threads
,
1580 .cmd
= "fThreadInfo",
1583 .handler
= handle_query_thread_extra
,
1584 .cmd
= "ThreadExtraInfo,",
1585 .cmd_startswith
= 1,
1588 #ifdef CONFIG_USER_ONLY
1590 .handler
= gdb_handle_query_offsets
,
1595 .handler
= gdb_handle_query_rcmd
,
1597 .cmd_startswith
= 1,
1602 .handler
= handle_query_supported
,
1603 .cmd
= "Supported:",
1604 .cmd_startswith
= 1,
1608 .handler
= handle_query_supported
,
1613 .handler
= handle_query_xfer_features
,
1614 .cmd
= "Xfer:features:read:",
1615 .cmd_startswith
= 1,
1618 #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX)
1620 .handler
= gdb_handle_query_xfer_auxv
,
1621 .cmd
= "Xfer:auxv:read::",
1622 .cmd_startswith
= 1,
1627 .handler
= gdb_handle_query_attached
,
1632 .handler
= gdb_handle_query_attached
,
1636 .handler
= handle_query_qemu_supported
,
1637 .cmd
= "qemu.Supported",
1639 #ifndef CONFIG_USER_ONLY
1641 .handler
= gdb_handle_query_qemu_phy_mem_mode
,
1642 .cmd
= "qemu.PhyMemMode",
1647 static const GdbCmdParseEntry gdb_gen_set_table
[] = {
1648 /* Order is important if has same prefix */
1650 .handler
= handle_set_qemu_sstep
,
1651 .cmd
= "qemu.sstep:",
1652 .cmd_startswith
= 1,
1655 #ifndef CONFIG_USER_ONLY
1657 .handler
= gdb_handle_set_qemu_phy_mem_mode
,
1658 .cmd
= "qemu.PhyMemMode:",
1659 .cmd_startswith
= 1,
1665 static void handle_gen_query(GArray
*params
, void *user_ctx
)
1671 if (!process_string_cmd(NULL
, get_param(params
, 0)->data
,
1672 gdb_gen_query_set_common_table
,
1673 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
1677 if (process_string_cmd(NULL
, get_param(params
, 0)->data
,
1678 gdb_gen_query_table
,
1679 ARRAY_SIZE(gdb_gen_query_table
))) {
1684 static void handle_gen_set(GArray
*params
, void *user_ctx
)
1690 if (!process_string_cmd(NULL
, get_param(params
, 0)->data
,
1691 gdb_gen_query_set_common_table
,
1692 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
1696 if (process_string_cmd(NULL
, get_param(params
, 0)->data
,
1698 ARRAY_SIZE(gdb_gen_set_table
))) {
1703 static void handle_target_halt(GArray
*params
, void *user_ctx
)
1705 if (gdbserver_state
.allow_stop_reply
) {
1706 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
1707 gdb_append_thread_id(gdbserver_state
.c_cpu
, gdbserver_state
.str_buf
);
1708 g_string_append_c(gdbserver_state
.str_buf
, ';');
1710 gdbserver_state
.allow_stop_reply
= false;
1713 * Remove all the breakpoints when this query is issued,
1714 * because gdb is doing an initial connect and the state
1715 * should be cleaned up.
1717 gdb_breakpoint_remove_all(gdbserver_state
.c_cpu
);
1720 static int gdb_handle_packet(const char *line_buf
)
1722 const GdbCmdParseEntry
*cmd_parser
= NULL
;
1724 trace_gdbstub_io_command(line_buf
);
1726 switch (line_buf
[0]) {
1728 gdb_put_packet("OK");
1732 static const GdbCmdParseEntry target_halted_cmd_desc
= {
1733 .handler
= handle_target_halt
,
1735 .cmd_startswith
= 1,
1736 .allow_stop_reply
= true,
1738 cmd_parser
= &target_halted_cmd_desc
;
1743 static const GdbCmdParseEntry continue_cmd_desc
= {
1744 .handler
= handle_continue
,
1746 .cmd_startswith
= 1,
1747 .allow_stop_reply
= true,
1750 cmd_parser
= &continue_cmd_desc
;
1755 static const GdbCmdParseEntry cont_with_sig_cmd_desc
= {
1756 .handler
= handle_cont_with_sig
,
1758 .cmd_startswith
= 1,
1759 .allow_stop_reply
= true,
1762 cmd_parser
= &cont_with_sig_cmd_desc
;
1767 static const GdbCmdParseEntry v_cmd_desc
= {
1768 .handler
= handle_v_commands
,
1770 .cmd_startswith
= 1,
1773 cmd_parser
= &v_cmd_desc
;
1777 /* Kill the target */
1778 error_report("QEMU: Terminated via GDBstub");
1783 static const GdbCmdParseEntry detach_cmd_desc
= {
1784 .handler
= handle_detach
,
1786 .cmd_startswith
= 1,
1789 cmd_parser
= &detach_cmd_desc
;
1794 static const GdbCmdParseEntry step_cmd_desc
= {
1795 .handler
= handle_step
,
1797 .cmd_startswith
= 1,
1798 .allow_stop_reply
= true,
1801 cmd_parser
= &step_cmd_desc
;
1806 static const GdbCmdParseEntry backward_cmd_desc
= {
1807 .handler
= handle_backward
,
1809 .cmd_startswith
= 1,
1810 .allow_stop_reply
= true,
1813 cmd_parser
= &backward_cmd_desc
;
1818 static const GdbCmdParseEntry file_io_cmd_desc
= {
1819 .handler
= gdb_handle_file_io
,
1821 .cmd_startswith
= 1,
1824 cmd_parser
= &file_io_cmd_desc
;
1829 static const GdbCmdParseEntry read_all_regs_cmd_desc
= {
1830 .handler
= handle_read_all_regs
,
1834 cmd_parser
= &read_all_regs_cmd_desc
;
1839 static const GdbCmdParseEntry write_all_regs_cmd_desc
= {
1840 .handler
= handle_write_all_regs
,
1842 .cmd_startswith
= 1,
1845 cmd_parser
= &write_all_regs_cmd_desc
;
1850 static const GdbCmdParseEntry read_mem_cmd_desc
= {
1851 .handler
= handle_read_mem
,
1853 .cmd_startswith
= 1,
1856 cmd_parser
= &read_mem_cmd_desc
;
1861 static const GdbCmdParseEntry write_mem_cmd_desc
= {
1862 .handler
= handle_write_mem
,
1864 .cmd_startswith
= 1,
1867 cmd_parser
= &write_mem_cmd_desc
;
1872 static const GdbCmdParseEntry get_reg_cmd_desc
= {
1873 .handler
= handle_get_reg
,
1875 .cmd_startswith
= 1,
1878 cmd_parser
= &get_reg_cmd_desc
;
1883 static const GdbCmdParseEntry set_reg_cmd_desc
= {
1884 .handler
= handle_set_reg
,
1886 .cmd_startswith
= 1,
1889 cmd_parser
= &set_reg_cmd_desc
;
1894 static const GdbCmdParseEntry insert_bp_cmd_desc
= {
1895 .handler
= handle_insert_bp
,
1897 .cmd_startswith
= 1,
1900 cmd_parser
= &insert_bp_cmd_desc
;
1905 static const GdbCmdParseEntry remove_bp_cmd_desc
= {
1906 .handler
= handle_remove_bp
,
1908 .cmd_startswith
= 1,
1911 cmd_parser
= &remove_bp_cmd_desc
;
1916 static const GdbCmdParseEntry set_thread_cmd_desc
= {
1917 .handler
= handle_set_thread
,
1919 .cmd_startswith
= 1,
1922 cmd_parser
= &set_thread_cmd_desc
;
1927 static const GdbCmdParseEntry thread_alive_cmd_desc
= {
1928 .handler
= handle_thread_alive
,
1930 .cmd_startswith
= 1,
1933 cmd_parser
= &thread_alive_cmd_desc
;
1938 static const GdbCmdParseEntry gen_query_cmd_desc
= {
1939 .handler
= handle_gen_query
,
1941 .cmd_startswith
= 1,
1944 cmd_parser
= &gen_query_cmd_desc
;
1949 static const GdbCmdParseEntry gen_set_cmd_desc
= {
1950 .handler
= handle_gen_set
,
1952 .cmd_startswith
= 1,
1955 cmd_parser
= &gen_set_cmd_desc
;
1959 /* put empty packet */
1965 run_cmd_parser(line_buf
, cmd_parser
);
1971 void gdb_set_stop_cpu(CPUState
*cpu
)
1973 GDBProcess
*p
= gdb_get_cpu_process(cpu
);
1977 * Having a stop CPU corresponding to a process that is not attached
1978 * confuses GDB. So we ignore the request.
1983 gdbserver_state
.c_cpu
= cpu
;
1984 gdbserver_state
.g_cpu
= cpu
;
1987 void gdb_read_byte(uint8_t ch
)
1991 gdbserver_state
.allow_stop_reply
= false;
1992 #ifndef CONFIG_USER_ONLY
1993 if (gdbserver_state
.last_packet
->len
) {
1994 /* Waiting for a response to the last packet. If we see the start
1995 of a new command then abandon the previous response. */
1997 trace_gdbstub_err_got_nack();
1998 gdb_put_buffer(gdbserver_state
.last_packet
->data
,
1999 gdbserver_state
.last_packet
->len
);
2000 } else if (ch
== '+') {
2001 trace_gdbstub_io_got_ack();
2003 trace_gdbstub_io_got_unexpected(ch
);
2006 if (ch
== '+' || ch
== '$') {
2007 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
2012 if (runstate_is_running()) {
2013 /* when the CPU is running, we cannot do anything except stop
2014 it when receiving a char */
2015 vm_stop(RUN_STATE_PAUSED
);
2019 switch(gdbserver_state
.state
) {
2022 /* start of command packet */
2023 gdbserver_state
.line_buf_index
= 0;
2024 gdbserver_state
.line_sum
= 0;
2025 gdbserver_state
.state
= RS_GETLINE
;
2027 trace_gdbstub_err_garbage(ch
);
2032 /* start escape sequence */
2033 gdbserver_state
.state
= RS_GETLINE_ESC
;
2034 gdbserver_state
.line_sum
+= ch
;
2035 } else if (ch
== '*') {
2036 /* start run length encoding sequence */
2037 gdbserver_state
.state
= RS_GETLINE_RLE
;
2038 gdbserver_state
.line_sum
+= ch
;
2039 } else if (ch
== '#') {
2040 /* end of command, start of checksum*/
2041 gdbserver_state
.state
= RS_CHKSUM1
;
2042 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2043 trace_gdbstub_err_overrun();
2044 gdbserver_state
.state
= RS_IDLE
;
2046 /* unescaped command character */
2047 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
;
2048 gdbserver_state
.line_sum
+= ch
;
2051 case RS_GETLINE_ESC
:
2053 /* unexpected end of command in escape sequence */
2054 gdbserver_state
.state
= RS_CHKSUM1
;
2055 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2056 /* command buffer overrun */
2057 trace_gdbstub_err_overrun();
2058 gdbserver_state
.state
= RS_IDLE
;
2060 /* parse escaped character and leave escape state */
2061 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
^ 0x20;
2062 gdbserver_state
.line_sum
+= ch
;
2063 gdbserver_state
.state
= RS_GETLINE
;
2066 case RS_GETLINE_RLE
:
2068 * Run-length encoding is explained in "Debugging with GDB /
2069 * Appendix E GDB Remote Serial Protocol / Overview".
2071 if (ch
< ' ' || ch
== '#' || ch
== '$' || ch
> 126) {
2072 /* invalid RLE count encoding */
2073 trace_gdbstub_err_invalid_repeat(ch
);
2074 gdbserver_state
.state
= RS_GETLINE
;
2076 /* decode repeat length */
2077 int repeat
= ch
- ' ' + 3;
2078 if (gdbserver_state
.line_buf_index
+ repeat
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2079 /* that many repeats would overrun the command buffer */
2080 trace_gdbstub_err_overrun();
2081 gdbserver_state
.state
= RS_IDLE
;
2082 } else if (gdbserver_state
.line_buf_index
< 1) {
2083 /* got a repeat but we have nothing to repeat */
2084 trace_gdbstub_err_invalid_rle();
2085 gdbserver_state
.state
= RS_GETLINE
;
2087 /* repeat the last character */
2088 memset(gdbserver_state
.line_buf
+ gdbserver_state
.line_buf_index
,
2089 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
- 1], repeat
);
2090 gdbserver_state
.line_buf_index
+= repeat
;
2091 gdbserver_state
.line_sum
+= ch
;
2092 gdbserver_state
.state
= RS_GETLINE
;
2097 /* get high hex digit of checksum */
2098 if (!isxdigit(ch
)) {
2099 trace_gdbstub_err_checksum_invalid(ch
);
2100 gdbserver_state
.state
= RS_GETLINE
;
2103 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
] = '\0';
2104 gdbserver_state
.line_csum
= fromhex(ch
) << 4;
2105 gdbserver_state
.state
= RS_CHKSUM2
;
2108 /* get low hex digit of checksum */
2109 if (!isxdigit(ch
)) {
2110 trace_gdbstub_err_checksum_invalid(ch
);
2111 gdbserver_state
.state
= RS_GETLINE
;
2114 gdbserver_state
.line_csum
|= fromhex(ch
);
2116 if (gdbserver_state
.line_csum
!= (gdbserver_state
.line_sum
& 0xff)) {
2117 trace_gdbstub_err_checksum_incorrect(gdbserver_state
.line_sum
, gdbserver_state
.line_csum
);
2118 /* send NAK reply */
2120 gdb_put_buffer(&reply
, 1);
2121 gdbserver_state
.state
= RS_IDLE
;
2123 /* send ACK reply */
2125 gdb_put_buffer(&reply
, 1);
2126 gdbserver_state
.state
= gdb_handle_packet(gdbserver_state
.line_buf
);
2136 * Create the process that will contain all the "orphan" CPUs (that are not
2137 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2138 * be attachable and thus will be invisible to the user.
2140 void gdb_create_default_process(GDBState
*s
)
2142 GDBProcess
*process
;
2145 #ifdef CONFIG_USER_ONLY
2146 assert(gdbserver_state
.process_num
== 0);
2149 if (gdbserver_state
.process_num
) {
2150 pid
= s
->processes
[s
->process_num
- 1].pid
;
2154 /* We need an available PID slot for this process */
2155 assert(pid
< UINT32_MAX
);
2159 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2160 process
= &s
->processes
[s
->process_num
- 1];
2162 process
->attached
= false;
2163 process
->target_xml
[0] = '\0';