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 /* TODO: In user mode, we should use the task state PID */
206 if (cpu
->cluster_index
== UNASSIGNED_CLUSTER_INDEX
) {
207 /* Return the default process' PID */
208 int index
= gdbserver_state
.process_num
- 1;
209 return gdbserver_state
.processes
[index
].pid
;
211 return cpu
->cluster_index
+ 1;
214 static GDBProcess
*gdb_get_process(uint32_t pid
)
219 /* 0 means any process, we take the first one */
220 return &gdbserver_state
.processes
[0];
223 for (i
= 0; i
< gdbserver_state
.process_num
; i
++) {
224 if (gdbserver_state
.processes
[i
].pid
== pid
) {
225 return &gdbserver_state
.processes
[i
];
232 static GDBProcess
*gdb_get_cpu_process(CPUState
*cpu
)
234 return gdb_get_process(gdb_get_cpu_pid(cpu
));
237 static CPUState
*find_cpu(uint32_t thread_id
)
242 if (gdb_get_cpu_index(cpu
) == thread_id
) {
250 static CPUState
*get_first_cpu_in_process(GDBProcess
*process
)
255 if (gdb_get_cpu_pid(cpu
) == process
->pid
) {
263 static CPUState
*gdb_next_cpu_in_process(CPUState
*cpu
)
265 uint32_t pid
= gdb_get_cpu_pid(cpu
);
269 if (gdb_get_cpu_pid(cpu
) == pid
) {
279 /* Return the cpu following @cpu, while ignoring unattached processes. */
280 static CPUState
*gdb_next_attached_cpu(CPUState
*cpu
)
285 if (gdb_get_cpu_process(cpu
)->attached
) {
295 /* Return the first attached cpu */
296 CPUState
*gdb_first_attached_cpu(void)
298 CPUState
*cpu
= first_cpu
;
299 GDBProcess
*process
= gdb_get_cpu_process(cpu
);
301 if (!process
->attached
) {
302 return gdb_next_attached_cpu(cpu
);
308 static CPUState
*gdb_get_cpu(uint32_t pid
, uint32_t tid
)
314 /* 0 means any process/thread, we take the first attached one */
315 return gdb_first_attached_cpu();
316 } else if (pid
&& !tid
) {
317 /* any thread in a specific process */
318 process
= gdb_get_process(pid
);
320 if (process
== NULL
) {
324 if (!process
->attached
) {
328 return get_first_cpu_in_process(process
);
330 /* a specific thread */
337 process
= gdb_get_cpu_process(cpu
);
339 if (pid
&& process
->pid
!= pid
) {
343 if (!process
->attached
) {
351 static const char *get_feature_xml(const char *p
, const char **newp
,
357 CPUState
*cpu
= get_first_cpu_in_process(process
);
358 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
361 while (p
[len
] && p
[len
] != ':')
366 if (strncmp(p
, "target.xml", len
) == 0) {
367 char *buf
= process
->target_xml
;
368 const size_t buf_sz
= sizeof(process
->target_xml
);
370 /* Generate the XML description for this CPU. */
375 "<?xml version=\"1.0\"?>"
376 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
378 if (cc
->gdb_arch_name
) {
379 gchar
*arch
= cc
->gdb_arch_name(cpu
);
380 pstrcat(buf
, buf_sz
, "<architecture>");
381 pstrcat(buf
, buf_sz
, arch
);
382 pstrcat(buf
, buf_sz
, "</architecture>");
385 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
386 pstrcat(buf
, buf_sz
, cc
->gdb_core_xml_file
);
387 pstrcat(buf
, buf_sz
, "\"/>");
388 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
389 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
390 pstrcat(buf
, buf_sz
, r
->xml
);
391 pstrcat(buf
, buf_sz
, "\"/>");
393 pstrcat(buf
, buf_sz
, "</target>");
397 if (cc
->gdb_get_dynamic_xml
) {
398 char *xmlname
= g_strndup(p
, len
);
399 const char *xml
= cc
->gdb_get_dynamic_xml(cpu
, xmlname
);
407 name
= xml_builtin
[i
][0];
408 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
411 return name
? xml_builtin
[i
][1] : NULL
;
414 static int gdb_read_register(CPUState
*cpu
, GByteArray
*buf
, int reg
)
416 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
417 CPUArchState
*env
= cpu
->env_ptr
;
420 if (reg
< cc
->gdb_num_core_regs
) {
421 return cc
->gdb_read_register(cpu
, buf
, reg
);
424 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
425 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
426 return r
->get_reg(env
, buf
, reg
- r
->base_reg
);
432 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
434 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
435 CPUArchState
*env
= cpu
->env_ptr
;
438 if (reg
< cc
->gdb_num_core_regs
) {
439 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
442 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
443 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
444 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
450 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
451 specifies the first register number and these registers are included in
452 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
453 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
456 void gdb_register_coprocessor(CPUState
*cpu
,
457 gdb_get_reg_cb get_reg
, gdb_set_reg_cb set_reg
,
458 int num_regs
, const char *xml
, int g_pos
)
461 GDBRegisterState
**p
;
465 /* Check for duplicates. */
466 if (strcmp((*p
)->xml
, xml
) == 0)
471 s
= g_new0(GDBRegisterState
, 1);
472 s
->base_reg
= cpu
->gdb_num_regs
;
473 s
->num_regs
= num_regs
;
474 s
->get_reg
= get_reg
;
475 s
->set_reg
= set_reg
;
478 /* Add to end of list. */
479 cpu
->gdb_num_regs
+= num_regs
;
482 if (g_pos
!= s
->base_reg
) {
483 error_report("Error: Bad gdb register numbering for '%s', "
484 "expected %d got %d", xml
, g_pos
, s
->base_reg
);
486 cpu
->gdb_num_g_regs
= cpu
->gdb_num_regs
;
491 static void gdb_process_breakpoint_remove_all(GDBProcess
*p
)
493 CPUState
*cpu
= get_first_cpu_in_process(p
);
496 gdb_breakpoint_remove_all(cpu
);
497 cpu
= gdb_next_cpu_in_process(cpu
);
502 static void gdb_set_cpu_pc(vaddr pc
)
504 CPUState
*cpu
= gdbserver_state
.c_cpu
;
506 cpu_synchronize_state(cpu
);
510 void gdb_append_thread_id(CPUState
*cpu
, GString
*buf
)
512 if (gdbserver_state
.multiprocess
) {
513 g_string_append_printf(buf
, "p%02x.%02x",
514 gdb_get_cpu_pid(cpu
), gdb_get_cpu_index(cpu
));
516 g_string_append_printf(buf
, "%02x", gdb_get_cpu_index(cpu
));
520 static GDBThreadIdKind
read_thread_id(const char *buf
, const char **end_buf
,
521 uint32_t *pid
, uint32_t *tid
)
528 ret
= qemu_strtoul(buf
, &buf
, 16, &p
);
531 return GDB_READ_THREAD_ERR
;
540 ret
= qemu_strtoul(buf
, &buf
, 16, &t
);
543 return GDB_READ_THREAD_ERR
;
549 return GDB_ALL_PROCESSES
;
557 return GDB_ALL_THREADS
;
564 return GDB_ONE_THREAD
;
568 * gdb_handle_vcont - Parses and handles a vCont packet.
569 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
570 * a format error, 0 on success.
572 static int gdb_handle_vcont(const char *p
)
581 GDBThreadIdKind kind
;
582 unsigned int max_cpus
= gdb_get_max_cpus();
583 /* uninitialised CPUs stay 0 */
584 newstates
= g_new0(char, max_cpus
);
586 /* mark valid CPUs with 1 */
588 newstates
[cpu
->cpu_index
] = 1;
592 * res keeps track of what error we are returning, with -ENOTSUP meaning
593 * that the command is unknown or unsupported, thus returning an empty
594 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
595 * or incorrect parameters passed.
605 if (cur_action
== 'C' || cur_action
== 'S') {
606 cur_action
= qemu_tolower(cur_action
);
607 res
= qemu_strtoul(p
, &p
, 16, &tmp
);
611 signal
= gdb_signal_to_target(tmp
);
612 } else if (cur_action
!= 'c' && cur_action
!= 's') {
613 /* 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
);
633 case GDB_READ_THREAD_ERR
:
637 case GDB_ALL_PROCESSES
:
638 cpu
= gdb_first_attached_cpu();
640 if (newstates
[cpu
->cpu_index
] == 1) {
641 newstates
[cpu
->cpu_index
] = cur_action
;
644 cpu
= gdb_next_attached_cpu(cpu
);
648 case GDB_ALL_THREADS
:
649 process
= gdb_get_process(pid
);
651 if (!process
->attached
) {
656 cpu
= get_first_cpu_in_process(process
);
658 if (newstates
[cpu
->cpu_index
] == 1) {
659 newstates
[cpu
->cpu_index
] = cur_action
;
662 cpu
= gdb_next_cpu_in_process(cpu
);
667 cpu
= gdb_get_cpu(pid
, tid
);
669 /* invalid CPU/thread specified */
675 /* only use if no previous match occourred */
676 if (newstates
[cpu
->cpu_index
] == 1) {
677 newstates
[cpu
->cpu_index
] = cur_action
;
682 gdbserver_state
.signal
= signal
;
683 gdb_continue_partial(newstates
);
691 static const char *cmd_next_param(const char *param
, const char delimiter
)
693 static const char all_delimiters
[] = ",;:=";
694 char curr_delimiters
[2] = {0};
695 const char *delimiters
;
697 if (delimiter
== '?') {
698 delimiters
= all_delimiters
;
699 } else if (delimiter
== '0') {
700 return strchr(param
, '\0');
701 } else if (delimiter
== '.' && *param
) {
704 curr_delimiters
[0] = delimiter
;
705 delimiters
= curr_delimiters
;
708 param
+= strcspn(param
, delimiters
);
715 static int cmd_parse_params(const char *data
, const char *schema
,
718 const char *curr_schema
, *curr_data
;
721 g_assert(params
->len
== 0);
723 curr_schema
= schema
;
725 while (curr_schema
[0] && curr_schema
[1] && *curr_data
) {
726 GdbCmdVariant this_param
;
728 switch (curr_schema
[0]) {
730 if (qemu_strtoul(curr_data
, &curr_data
, 16,
731 &this_param
.val_ul
)) {
734 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
735 g_array_append_val(params
, this_param
);
738 if (qemu_strtou64(curr_data
, &curr_data
, 16,
739 (uint64_t *)&this_param
.val_ull
)) {
742 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
743 g_array_append_val(params
, this_param
);
746 this_param
.data
= curr_data
;
747 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
748 g_array_append_val(params
, this_param
);
751 this_param
.opcode
= *(uint8_t *)curr_data
;
752 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
753 g_array_append_val(params
, this_param
);
756 this_param
.thread_id
.kind
=
757 read_thread_id(curr_data
, &curr_data
,
758 &this_param
.thread_id
.pid
,
759 &this_param
.thread_id
.tid
);
760 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
761 g_array_append_val(params
, this_param
);
764 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
775 typedef void (*GdbCmdHandler
)(GArray
*params
, void *user_ctx
);
778 * cmd_startswith -> cmd is compared using startswith
781 * schema definitions:
782 * Each schema parameter entry consists of 2 chars,
783 * the first char represents the parameter type handling
784 * the second char represents the delimiter for the next parameter
786 * Currently supported schema types:
787 * 'l' -> unsigned long (stored in .val_ul)
788 * 'L' -> unsigned long long (stored in .val_ull)
789 * 's' -> string (stored in .data)
790 * 'o' -> single char (stored in .opcode)
791 * 't' -> thread id (stored in .thread_id)
792 * '?' -> skip according to delimiter
794 * Currently supported delimiters:
795 * '?' -> Stop at any delimiter (",;:=\0")
796 * '0' -> Stop at "\0"
797 * '.' -> Skip 1 char unless reached "\0"
798 * Any other value is treated as the delimiter value itself
800 typedef struct GdbCmdParseEntry
{
801 GdbCmdHandler handler
;
807 static inline int startswith(const char *string
, const char *pattern
)
809 return !strncmp(string
, pattern
, strlen(pattern
));
812 static int process_string_cmd(void *user_ctx
, const char *data
,
813 const GdbCmdParseEntry
*cmds
, int num_cmds
)
816 g_autoptr(GArray
) params
= g_array_new(false, true, sizeof(GdbCmdVariant
));
822 for (i
= 0; i
< num_cmds
; i
++) {
823 const GdbCmdParseEntry
*cmd
= &cmds
[i
];
824 g_assert(cmd
->handler
&& cmd
->cmd
);
826 if ((cmd
->cmd_startswith
&& !startswith(data
, cmd
->cmd
)) ||
827 (!cmd
->cmd_startswith
&& strcmp(cmd
->cmd
, data
))) {
832 if (cmd_parse_params(&data
[strlen(cmd
->cmd
)],
833 cmd
->schema
, params
)) {
838 cmd
->handler(params
, user_ctx
);
845 static void run_cmd_parser(const char *data
, const GdbCmdParseEntry
*cmd
)
851 g_string_set_size(gdbserver_state
.str_buf
, 0);
852 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
854 /* In case there was an error during the command parsing we must
855 * send a NULL packet to indicate the command is not supported */
856 if (process_string_cmd(NULL
, data
, cmd
, 1)) {
861 static void handle_detach(GArray
*params
, void *user_ctx
)
866 if (gdbserver_state
.multiprocess
) {
868 gdb_put_packet("E22");
872 pid
= get_param(params
, 0)->val_ul
;
875 process
= gdb_get_process(pid
);
876 gdb_process_breakpoint_remove_all(process
);
877 process
->attached
= false;
879 if (pid
== gdb_get_cpu_pid(gdbserver_state
.c_cpu
)) {
880 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
883 if (pid
== gdb_get_cpu_pid(gdbserver_state
.g_cpu
)) {
884 gdbserver_state
.g_cpu
= gdb_first_attached_cpu();
887 if (!gdbserver_state
.c_cpu
) {
888 /* No more process attached */
889 gdb_disable_syscalls();
892 gdb_put_packet("OK");
895 static void handle_thread_alive(GArray
*params
, void *user_ctx
)
900 gdb_put_packet("E22");
904 if (get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
905 gdb_put_packet("E22");
909 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
910 get_param(params
, 0)->thread_id
.tid
);
912 gdb_put_packet("E22");
916 gdb_put_packet("OK");
919 static void handle_continue(GArray
*params
, void *user_ctx
)
922 gdb_set_cpu_pc(get_param(params
, 0)->val_ull
);
925 gdbserver_state
.signal
= 0;
929 static void handle_cont_with_sig(GArray
*params
, void *user_ctx
)
931 unsigned long signal
= 0;
934 * Note: C sig;[addr] is currently unsupported and we simply
935 * omit the addr parameter
938 signal
= get_param(params
, 0)->val_ul
;
941 gdbserver_state
.signal
= gdb_signal_to_target(signal
);
942 if (gdbserver_state
.signal
== -1) {
943 gdbserver_state
.signal
= 0;
948 static void handle_set_thread(GArray
*params
, void *user_ctx
)
952 if (params
->len
!= 2) {
953 gdb_put_packet("E22");
957 if (get_param(params
, 1)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
958 gdb_put_packet("E22");
962 if (get_param(params
, 1)->thread_id
.kind
!= GDB_ONE_THREAD
) {
963 gdb_put_packet("OK");
967 cpu
= gdb_get_cpu(get_param(params
, 1)->thread_id
.pid
,
968 get_param(params
, 1)->thread_id
.tid
);
970 gdb_put_packet("E22");
975 * Note: This command is deprecated and modern gdb's will be using the
976 * vCont command instead.
978 switch (get_param(params
, 0)->opcode
) {
980 gdbserver_state
.c_cpu
= cpu
;
981 gdb_put_packet("OK");
984 gdbserver_state
.g_cpu
= cpu
;
985 gdb_put_packet("OK");
988 gdb_put_packet("E22");
993 static void handle_insert_bp(GArray
*params
, void *user_ctx
)
997 if (params
->len
!= 3) {
998 gdb_put_packet("E22");
1002 res
= gdb_breakpoint_insert(gdbserver_state
.c_cpu
,
1003 get_param(params
, 0)->val_ul
,
1004 get_param(params
, 1)->val_ull
,
1005 get_param(params
, 2)->val_ull
);
1007 gdb_put_packet("OK");
1009 } else if (res
== -ENOSYS
) {
1014 gdb_put_packet("E22");
1017 static void handle_remove_bp(GArray
*params
, void *user_ctx
)
1021 if (params
->len
!= 3) {
1022 gdb_put_packet("E22");
1026 res
= gdb_breakpoint_remove(gdbserver_state
.c_cpu
,
1027 get_param(params
, 0)->val_ul
,
1028 get_param(params
, 1)->val_ull
,
1029 get_param(params
, 2)->val_ull
);
1031 gdb_put_packet("OK");
1033 } else if (res
== -ENOSYS
) {
1038 gdb_put_packet("E22");
1042 * handle_set/get_reg
1044 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1045 * This works, but can be very slow. Anything new enough to understand
1046 * XML also knows how to use this properly. However to use this we
1047 * need to define a local XML file as well as be talking to a
1048 * reasonably modern gdb. Responding with an empty packet will cause
1049 * the remote gdb to fallback to older methods.
1052 static void handle_set_reg(GArray
*params
, void *user_ctx
)
1061 if (params
->len
!= 2) {
1062 gdb_put_packet("E22");
1066 reg_size
= strlen(get_param(params
, 1)->data
) / 2;
1067 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 1)->data
, reg_size
);
1068 gdb_write_register(gdbserver_state
.g_cpu
, gdbserver_state
.mem_buf
->data
,
1069 get_param(params
, 0)->val_ull
);
1070 gdb_put_packet("OK");
1073 static void handle_get_reg(GArray
*params
, void *user_ctx
)
1083 gdb_put_packet("E14");
1087 reg_size
= gdb_read_register(gdbserver_state
.g_cpu
,
1088 gdbserver_state
.mem_buf
,
1089 get_param(params
, 0)->val_ull
);
1091 gdb_put_packet("E14");
1094 g_byte_array_set_size(gdbserver_state
.mem_buf
, reg_size
);
1097 gdb_memtohex(gdbserver_state
.str_buf
,
1098 gdbserver_state
.mem_buf
->data
, reg_size
);
1102 static void handle_write_mem(GArray
*params
, void *user_ctx
)
1104 if (params
->len
!= 3) {
1105 gdb_put_packet("E22");
1109 /* gdb_hextomem() reads 2*len bytes */
1110 if (get_param(params
, 1)->val_ull
>
1111 strlen(get_param(params
, 2)->data
) / 2) {
1112 gdb_put_packet("E22");
1116 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 2)->data
,
1117 get_param(params
, 1)->val_ull
);
1118 if (gdb_target_memory_rw_debug(gdbserver_state
.g_cpu
,
1119 get_param(params
, 0)->val_ull
,
1120 gdbserver_state
.mem_buf
->data
,
1121 gdbserver_state
.mem_buf
->len
, true)) {
1122 gdb_put_packet("E14");
1126 gdb_put_packet("OK");
1129 static void handle_read_mem(GArray
*params
, void *user_ctx
)
1131 if (params
->len
!= 2) {
1132 gdb_put_packet("E22");
1136 /* gdb_memtohex() doubles the required space */
1137 if (get_param(params
, 1)->val_ull
> MAX_PACKET_LENGTH
/ 2) {
1138 gdb_put_packet("E22");
1142 g_byte_array_set_size(gdbserver_state
.mem_buf
,
1143 get_param(params
, 1)->val_ull
);
1145 if (gdb_target_memory_rw_debug(gdbserver_state
.g_cpu
,
1146 get_param(params
, 0)->val_ull
,
1147 gdbserver_state
.mem_buf
->data
,
1148 gdbserver_state
.mem_buf
->len
, false)) {
1149 gdb_put_packet("E14");
1153 gdb_memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
,
1154 gdbserver_state
.mem_buf
->len
);
1158 static void handle_write_all_regs(GArray
*params
, void *user_ctx
)
1169 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1170 len
= strlen(get_param(params
, 0)->data
) / 2;
1171 gdb_hextomem(gdbserver_state
.mem_buf
, get_param(params
, 0)->data
, len
);
1172 registers
= gdbserver_state
.mem_buf
->data
;
1174 reg_id
< gdbserver_state
.g_cpu
->gdb_num_g_regs
&& len
> 0;
1176 reg_size
= gdb_write_register(gdbserver_state
.g_cpu
, registers
, reg_id
);
1178 registers
+= reg_size
;
1180 gdb_put_packet("OK");
1183 static void handle_read_all_regs(GArray
*params
, void *user_ctx
)
1188 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1189 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
1191 for (reg_id
= 0; reg_id
< gdbserver_state
.g_cpu
->gdb_num_g_regs
; reg_id
++) {
1192 len
+= gdb_read_register(gdbserver_state
.g_cpu
,
1193 gdbserver_state
.mem_buf
,
1196 g_assert(len
== gdbserver_state
.mem_buf
->len
);
1198 gdb_memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
, len
);
1203 static void handle_step(GArray
*params
, void *user_ctx
)
1206 gdb_set_cpu_pc(get_param(params
, 0)->val_ull
);
1209 cpu_single_step(gdbserver_state
.c_cpu
, gdbserver_state
.sstep_flags
);
1213 static void handle_backward(GArray
*params
, void *user_ctx
)
1215 if (!gdb_can_reverse()) {
1216 gdb_put_packet("E22");
1218 if (params
->len
== 1) {
1219 switch (get_param(params
, 0)->opcode
) {
1221 if (replay_reverse_step()) {
1224 gdb_put_packet("E14");
1228 if (replay_reverse_continue()) {
1231 gdb_put_packet("E14");
1237 /* Default invalid command */
1241 static void handle_v_cont_query(GArray
*params
, void *user_ctx
)
1243 gdb_put_packet("vCont;c;C;s;S");
1246 static void handle_v_cont(GArray
*params
, void *user_ctx
)
1254 res
= gdb_handle_vcont(get_param(params
, 0)->data
);
1255 if ((res
== -EINVAL
) || (res
== -ERANGE
)) {
1256 gdb_put_packet("E22");
1262 static void handle_v_attach(GArray
*params
, void *user_ctx
)
1264 GDBProcess
*process
;
1267 g_string_assign(gdbserver_state
.str_buf
, "E22");
1272 process
= gdb_get_process(get_param(params
, 0)->val_ul
);
1277 cpu
= get_first_cpu_in_process(process
);
1282 process
->attached
= true;
1283 gdbserver_state
.g_cpu
= cpu
;
1284 gdbserver_state
.c_cpu
= cpu
;
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
, ';');
1293 static void handle_v_kill(GArray
*params
, void *user_ctx
)
1295 /* Kill the target */
1296 gdb_put_packet("OK");
1297 error_report("QEMU: Terminated via GDBstub");
1302 static const GdbCmdParseEntry gdb_v_commands_table
[] = {
1303 /* Order is important if has same prefix */
1305 .handler
= handle_v_cont_query
,
1310 .handler
= handle_v_cont
,
1312 .cmd_startswith
= 1,
1316 .handler
= handle_v_attach
,
1318 .cmd_startswith
= 1,
1322 .handler
= handle_v_kill
,
1328 static void handle_v_commands(GArray
*params
, void *user_ctx
)
1334 if (process_string_cmd(NULL
, get_param(params
, 0)->data
,
1335 gdb_v_commands_table
,
1336 ARRAY_SIZE(gdb_v_commands_table
))) {
1341 static void handle_query_qemu_sstepbits(GArray
*params
, void *user_ctx
)
1343 g_string_printf(gdbserver_state
.str_buf
, "ENABLE=%x", SSTEP_ENABLE
);
1345 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOIRQ
) {
1346 g_string_append_printf(gdbserver_state
.str_buf
, ",NOIRQ=%x",
1350 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOTIMER
) {
1351 g_string_append_printf(gdbserver_state
.str_buf
, ",NOTIMER=%x",
1358 static void handle_set_qemu_sstep(GArray
*params
, void *user_ctx
)
1360 int new_sstep_flags
;
1366 new_sstep_flags
= get_param(params
, 0)->val_ul
;
1368 if (new_sstep_flags
& ~gdbserver_state
.supported_sstep_flags
) {
1369 gdb_put_packet("E22");
1373 gdbserver_state
.sstep_flags
= new_sstep_flags
;
1374 gdb_put_packet("OK");
1377 static void handle_query_qemu_sstep(GArray
*params
, void *user_ctx
)
1379 g_string_printf(gdbserver_state
.str_buf
, "0x%x",
1380 gdbserver_state
.sstep_flags
);
1384 static void handle_query_curr_tid(GArray
*params
, void *user_ctx
)
1387 GDBProcess
*process
;
1390 * "Current thread" remains vague in the spec, so always return
1391 * the first thread of the current process (gdb returns the
1394 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
1395 cpu
= get_first_cpu_in_process(process
);
1396 g_string_assign(gdbserver_state
.str_buf
, "QC");
1397 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
1401 static void handle_query_threads(GArray
*params
, void *user_ctx
)
1403 if (!gdbserver_state
.query_cpu
) {
1404 gdb_put_packet("l");
1408 g_string_assign(gdbserver_state
.str_buf
, "m");
1409 gdb_append_thread_id(gdbserver_state
.query_cpu
, gdbserver_state
.str_buf
);
1411 gdbserver_state
.query_cpu
= gdb_next_attached_cpu(gdbserver_state
.query_cpu
);
1414 static void handle_query_first_threads(GArray
*params
, void *user_ctx
)
1416 gdbserver_state
.query_cpu
= gdb_first_attached_cpu();
1417 handle_query_threads(params
, user_ctx
);
1420 static void handle_query_thread_extra(GArray
*params
, void *user_ctx
)
1422 g_autoptr(GString
) rs
= g_string_new(NULL
);
1426 get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1427 gdb_put_packet("E22");
1431 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
1432 get_param(params
, 0)->thread_id
.tid
);
1437 cpu_synchronize_state(cpu
);
1439 if (gdbserver_state
.multiprocess
&& (gdbserver_state
.process_num
> 1)) {
1440 /* Print the CPU model and name in multiprocess mode */
1441 ObjectClass
*oc
= object_get_class(OBJECT(cpu
));
1442 const char *cpu_model
= object_class_get_name(oc
);
1443 const char *cpu_name
=
1444 object_get_canonical_path_component(OBJECT(cpu
));
1445 g_string_printf(rs
, "%s %s [%s]", cpu_model
, cpu_name
,
1446 cpu
->halted
? "halted " : "running");
1448 g_string_printf(rs
, "CPU#%d [%s]", cpu
->cpu_index
,
1449 cpu
->halted
? "halted " : "running");
1451 trace_gdbstub_op_extra_info(rs
->str
);
1452 gdb_memtohex(gdbserver_state
.str_buf
, (uint8_t *)rs
->str
, rs
->len
);
1456 static void handle_query_supported(GArray
*params
, void *user_ctx
)
1460 g_string_printf(gdbserver_state
.str_buf
, "PacketSize=%x", MAX_PACKET_LENGTH
);
1461 cc
= CPU_GET_CLASS(first_cpu
);
1462 if (cc
->gdb_core_xml_file
) {
1463 g_string_append(gdbserver_state
.str_buf
, ";qXfer:features:read+");
1466 if (gdb_can_reverse()) {
1467 g_string_append(gdbserver_state
.str_buf
,
1468 ";ReverseStep+;ReverseContinue+");
1471 #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX)
1472 if (gdbserver_state
.c_cpu
->opaque
) {
1473 g_string_append(gdbserver_state
.str_buf
, ";qXfer:auxv:read+");
1478 strstr(get_param(params
, 0)->data
, "multiprocess+")) {
1479 gdbserver_state
.multiprocess
= true;
1482 g_string_append(gdbserver_state
.str_buf
, ";vContSupported+;multiprocess+");
1486 static void handle_query_xfer_features(GArray
*params
, void *user_ctx
)
1488 GDBProcess
*process
;
1490 unsigned long len
, total_len
, addr
;
1494 if (params
->len
< 3) {
1495 gdb_put_packet("E22");
1499 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
1500 cc
= CPU_GET_CLASS(gdbserver_state
.g_cpu
);
1501 if (!cc
->gdb_core_xml_file
) {
1507 p
= get_param(params
, 0)->data
;
1508 xml
= get_feature_xml(p
, &p
, process
);
1510 gdb_put_packet("E00");
1514 addr
= get_param(params
, 1)->val_ul
;
1515 len
= get_param(params
, 2)->val_ul
;
1516 total_len
= strlen(xml
);
1517 if (addr
> total_len
) {
1518 gdb_put_packet("E00");
1522 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
1523 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1526 if (len
< total_len
- addr
) {
1527 g_string_assign(gdbserver_state
.str_buf
, "m");
1528 gdb_memtox(gdbserver_state
.str_buf
, xml
+ addr
, len
);
1530 g_string_assign(gdbserver_state
.str_buf
, "l");
1531 gdb_memtox(gdbserver_state
.str_buf
, xml
+ addr
, total_len
- addr
);
1534 gdb_put_packet_binary(gdbserver_state
.str_buf
->str
,
1535 gdbserver_state
.str_buf
->len
, true);
1538 static void handle_query_qemu_supported(GArray
*params
, void *user_ctx
)
1540 g_string_printf(gdbserver_state
.str_buf
, "sstepbits;sstep");
1541 #ifndef CONFIG_USER_ONLY
1542 g_string_append(gdbserver_state
.str_buf
, ";PhyMemMode");
1547 static const GdbCmdParseEntry gdb_gen_query_set_common_table
[] = {
1548 /* Order is important if has same prefix */
1550 .handler
= handle_query_qemu_sstepbits
,
1551 .cmd
= "qemu.sstepbits",
1554 .handler
= handle_query_qemu_sstep
,
1555 .cmd
= "qemu.sstep",
1558 .handler
= handle_set_qemu_sstep
,
1559 .cmd
= "qemu.sstep=",
1560 .cmd_startswith
= 1,
1565 static const GdbCmdParseEntry gdb_gen_query_table
[] = {
1567 .handler
= handle_query_curr_tid
,
1571 .handler
= handle_query_threads
,
1572 .cmd
= "sThreadInfo",
1575 .handler
= handle_query_first_threads
,
1576 .cmd
= "fThreadInfo",
1579 .handler
= handle_query_thread_extra
,
1580 .cmd
= "ThreadExtraInfo,",
1581 .cmd_startswith
= 1,
1584 #ifdef CONFIG_USER_ONLY
1586 .handler
= gdb_handle_query_offsets
,
1591 .handler
= gdb_handle_query_rcmd
,
1593 .cmd_startswith
= 1,
1598 .handler
= handle_query_supported
,
1599 .cmd
= "Supported:",
1600 .cmd_startswith
= 1,
1604 .handler
= handle_query_supported
,
1609 .handler
= handle_query_xfer_features
,
1610 .cmd
= "Xfer:features:read:",
1611 .cmd_startswith
= 1,
1614 #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX)
1616 .handler
= gdb_handle_query_xfer_auxv
,
1617 .cmd
= "Xfer:auxv:read::",
1618 .cmd_startswith
= 1,
1623 .handler
= gdb_handle_query_attached
,
1628 .handler
= gdb_handle_query_attached
,
1632 .handler
= handle_query_qemu_supported
,
1633 .cmd
= "qemu.Supported",
1635 #ifndef CONFIG_USER_ONLY
1637 .handler
= gdb_handle_query_qemu_phy_mem_mode
,
1638 .cmd
= "qemu.PhyMemMode",
1643 static const GdbCmdParseEntry gdb_gen_set_table
[] = {
1644 /* Order is important if has same prefix */
1646 .handler
= handle_set_qemu_sstep
,
1647 .cmd
= "qemu.sstep:",
1648 .cmd_startswith
= 1,
1651 #ifndef CONFIG_USER_ONLY
1653 .handler
= gdb_handle_set_qemu_phy_mem_mode
,
1654 .cmd
= "qemu.PhyMemMode:",
1655 .cmd_startswith
= 1,
1661 static void handle_gen_query(GArray
*params
, void *user_ctx
)
1667 if (!process_string_cmd(NULL
, get_param(params
, 0)->data
,
1668 gdb_gen_query_set_common_table
,
1669 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
1673 if (process_string_cmd(NULL
, get_param(params
, 0)->data
,
1674 gdb_gen_query_table
,
1675 ARRAY_SIZE(gdb_gen_query_table
))) {
1680 static void handle_gen_set(GArray
*params
, void *user_ctx
)
1686 if (!process_string_cmd(NULL
, get_param(params
, 0)->data
,
1687 gdb_gen_query_set_common_table
,
1688 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
1692 if (process_string_cmd(NULL
, get_param(params
, 0)->data
,
1694 ARRAY_SIZE(gdb_gen_set_table
))) {
1699 static void handle_target_halt(GArray
*params
, void *user_ctx
)
1701 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
1702 gdb_append_thread_id(gdbserver_state
.c_cpu
, gdbserver_state
.str_buf
);
1703 g_string_append_c(gdbserver_state
.str_buf
, ';');
1706 * Remove all the breakpoints when this query is issued,
1707 * because gdb is doing an initial connect and the state
1708 * should be cleaned up.
1710 gdb_breakpoint_remove_all(gdbserver_state
.c_cpu
);
1713 static int gdb_handle_packet(const char *line_buf
)
1715 const GdbCmdParseEntry
*cmd_parser
= NULL
;
1717 trace_gdbstub_io_command(line_buf
);
1719 switch (line_buf
[0]) {
1721 gdb_put_packet("OK");
1725 static const GdbCmdParseEntry target_halted_cmd_desc
= {
1726 .handler
= handle_target_halt
,
1730 cmd_parser
= &target_halted_cmd_desc
;
1735 static const GdbCmdParseEntry continue_cmd_desc
= {
1736 .handler
= handle_continue
,
1738 .cmd_startswith
= 1,
1741 cmd_parser
= &continue_cmd_desc
;
1746 static const GdbCmdParseEntry cont_with_sig_cmd_desc
= {
1747 .handler
= handle_cont_with_sig
,
1749 .cmd_startswith
= 1,
1752 cmd_parser
= &cont_with_sig_cmd_desc
;
1757 static const GdbCmdParseEntry v_cmd_desc
= {
1758 .handler
= handle_v_commands
,
1760 .cmd_startswith
= 1,
1763 cmd_parser
= &v_cmd_desc
;
1767 /* Kill the target */
1768 error_report("QEMU: Terminated via GDBstub");
1773 static const GdbCmdParseEntry detach_cmd_desc
= {
1774 .handler
= handle_detach
,
1776 .cmd_startswith
= 1,
1779 cmd_parser
= &detach_cmd_desc
;
1784 static const GdbCmdParseEntry step_cmd_desc
= {
1785 .handler
= handle_step
,
1787 .cmd_startswith
= 1,
1790 cmd_parser
= &step_cmd_desc
;
1795 static const GdbCmdParseEntry backward_cmd_desc
= {
1796 .handler
= handle_backward
,
1798 .cmd_startswith
= 1,
1801 cmd_parser
= &backward_cmd_desc
;
1806 static const GdbCmdParseEntry file_io_cmd_desc
= {
1807 .handler
= gdb_handle_file_io
,
1809 .cmd_startswith
= 1,
1812 cmd_parser
= &file_io_cmd_desc
;
1817 static const GdbCmdParseEntry read_all_regs_cmd_desc
= {
1818 .handler
= handle_read_all_regs
,
1822 cmd_parser
= &read_all_regs_cmd_desc
;
1827 static const GdbCmdParseEntry write_all_regs_cmd_desc
= {
1828 .handler
= handle_write_all_regs
,
1830 .cmd_startswith
= 1,
1833 cmd_parser
= &write_all_regs_cmd_desc
;
1838 static const GdbCmdParseEntry read_mem_cmd_desc
= {
1839 .handler
= handle_read_mem
,
1841 .cmd_startswith
= 1,
1844 cmd_parser
= &read_mem_cmd_desc
;
1849 static const GdbCmdParseEntry write_mem_cmd_desc
= {
1850 .handler
= handle_write_mem
,
1852 .cmd_startswith
= 1,
1855 cmd_parser
= &write_mem_cmd_desc
;
1860 static const GdbCmdParseEntry get_reg_cmd_desc
= {
1861 .handler
= handle_get_reg
,
1863 .cmd_startswith
= 1,
1866 cmd_parser
= &get_reg_cmd_desc
;
1871 static const GdbCmdParseEntry set_reg_cmd_desc
= {
1872 .handler
= handle_set_reg
,
1874 .cmd_startswith
= 1,
1877 cmd_parser
= &set_reg_cmd_desc
;
1882 static const GdbCmdParseEntry insert_bp_cmd_desc
= {
1883 .handler
= handle_insert_bp
,
1885 .cmd_startswith
= 1,
1888 cmd_parser
= &insert_bp_cmd_desc
;
1893 static const GdbCmdParseEntry remove_bp_cmd_desc
= {
1894 .handler
= handle_remove_bp
,
1896 .cmd_startswith
= 1,
1899 cmd_parser
= &remove_bp_cmd_desc
;
1904 static const GdbCmdParseEntry set_thread_cmd_desc
= {
1905 .handler
= handle_set_thread
,
1907 .cmd_startswith
= 1,
1910 cmd_parser
= &set_thread_cmd_desc
;
1915 static const GdbCmdParseEntry thread_alive_cmd_desc
= {
1916 .handler
= handle_thread_alive
,
1918 .cmd_startswith
= 1,
1921 cmd_parser
= &thread_alive_cmd_desc
;
1926 static const GdbCmdParseEntry gen_query_cmd_desc
= {
1927 .handler
= handle_gen_query
,
1929 .cmd_startswith
= 1,
1932 cmd_parser
= &gen_query_cmd_desc
;
1937 static const GdbCmdParseEntry gen_set_cmd_desc
= {
1938 .handler
= handle_gen_set
,
1940 .cmd_startswith
= 1,
1943 cmd_parser
= &gen_set_cmd_desc
;
1947 /* put empty packet */
1953 run_cmd_parser(line_buf
, cmd_parser
);
1959 void gdb_set_stop_cpu(CPUState
*cpu
)
1961 GDBProcess
*p
= gdb_get_cpu_process(cpu
);
1965 * Having a stop CPU corresponding to a process that is not attached
1966 * confuses GDB. So we ignore the request.
1971 gdbserver_state
.c_cpu
= cpu
;
1972 gdbserver_state
.g_cpu
= cpu
;
1975 void gdb_read_byte(uint8_t ch
)
1979 #ifndef CONFIG_USER_ONLY
1980 if (gdbserver_state
.last_packet
->len
) {
1981 /* Waiting for a response to the last packet. If we see the start
1982 of a new command then abandon the previous response. */
1984 trace_gdbstub_err_got_nack();
1985 gdb_put_buffer(gdbserver_state
.last_packet
->data
,
1986 gdbserver_state
.last_packet
->len
);
1987 } else if (ch
== '+') {
1988 trace_gdbstub_io_got_ack();
1990 trace_gdbstub_io_got_unexpected(ch
);
1993 if (ch
== '+' || ch
== '$') {
1994 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
1999 if (runstate_is_running()) {
2000 /* when the CPU is running, we cannot do anything except stop
2001 it when receiving a char */
2002 vm_stop(RUN_STATE_PAUSED
);
2006 switch(gdbserver_state
.state
) {
2009 /* start of command packet */
2010 gdbserver_state
.line_buf_index
= 0;
2011 gdbserver_state
.line_sum
= 0;
2012 gdbserver_state
.state
= RS_GETLINE
;
2014 trace_gdbstub_err_garbage(ch
);
2019 /* start escape sequence */
2020 gdbserver_state
.state
= RS_GETLINE_ESC
;
2021 gdbserver_state
.line_sum
+= ch
;
2022 } else if (ch
== '*') {
2023 /* start run length encoding sequence */
2024 gdbserver_state
.state
= RS_GETLINE_RLE
;
2025 gdbserver_state
.line_sum
+= ch
;
2026 } else if (ch
== '#') {
2027 /* end of command, start of checksum*/
2028 gdbserver_state
.state
= RS_CHKSUM1
;
2029 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2030 trace_gdbstub_err_overrun();
2031 gdbserver_state
.state
= RS_IDLE
;
2033 /* unescaped command character */
2034 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
;
2035 gdbserver_state
.line_sum
+= ch
;
2038 case RS_GETLINE_ESC
:
2040 /* unexpected end of command in escape sequence */
2041 gdbserver_state
.state
= RS_CHKSUM1
;
2042 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2043 /* command buffer overrun */
2044 trace_gdbstub_err_overrun();
2045 gdbserver_state
.state
= RS_IDLE
;
2047 /* parse escaped character and leave escape state */
2048 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
^ 0x20;
2049 gdbserver_state
.line_sum
+= ch
;
2050 gdbserver_state
.state
= RS_GETLINE
;
2053 case RS_GETLINE_RLE
:
2055 * Run-length encoding is explained in "Debugging with GDB /
2056 * Appendix E GDB Remote Serial Protocol / Overview".
2058 if (ch
< ' ' || ch
== '#' || ch
== '$' || ch
> 126) {
2059 /* invalid RLE count encoding */
2060 trace_gdbstub_err_invalid_repeat(ch
);
2061 gdbserver_state
.state
= RS_GETLINE
;
2063 /* decode repeat length */
2064 int repeat
= ch
- ' ' + 3;
2065 if (gdbserver_state
.line_buf_index
+ repeat
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2066 /* that many repeats would overrun the command buffer */
2067 trace_gdbstub_err_overrun();
2068 gdbserver_state
.state
= RS_IDLE
;
2069 } else if (gdbserver_state
.line_buf_index
< 1) {
2070 /* got a repeat but we have nothing to repeat */
2071 trace_gdbstub_err_invalid_rle();
2072 gdbserver_state
.state
= RS_GETLINE
;
2074 /* repeat the last character */
2075 memset(gdbserver_state
.line_buf
+ gdbserver_state
.line_buf_index
,
2076 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
- 1], repeat
);
2077 gdbserver_state
.line_buf_index
+= repeat
;
2078 gdbserver_state
.line_sum
+= ch
;
2079 gdbserver_state
.state
= RS_GETLINE
;
2084 /* get high hex digit of checksum */
2085 if (!isxdigit(ch
)) {
2086 trace_gdbstub_err_checksum_invalid(ch
);
2087 gdbserver_state
.state
= RS_GETLINE
;
2090 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
] = '\0';
2091 gdbserver_state
.line_csum
= fromhex(ch
) << 4;
2092 gdbserver_state
.state
= RS_CHKSUM2
;
2095 /* get low hex digit of checksum */
2096 if (!isxdigit(ch
)) {
2097 trace_gdbstub_err_checksum_invalid(ch
);
2098 gdbserver_state
.state
= RS_GETLINE
;
2101 gdbserver_state
.line_csum
|= fromhex(ch
);
2103 if (gdbserver_state
.line_csum
!= (gdbserver_state
.line_sum
& 0xff)) {
2104 trace_gdbstub_err_checksum_incorrect(gdbserver_state
.line_sum
, gdbserver_state
.line_csum
);
2105 /* send NAK reply */
2107 gdb_put_buffer(&reply
, 1);
2108 gdbserver_state
.state
= RS_IDLE
;
2110 /* send ACK reply */
2112 gdb_put_buffer(&reply
, 1);
2113 gdbserver_state
.state
= gdb_handle_packet(gdbserver_state
.line_buf
);
2123 * Create the process that will contain all the "orphan" CPUs (that are not
2124 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2125 * be attachable and thus will be invisible to the user.
2127 void gdb_create_default_process(GDBState
*s
)
2129 GDBProcess
*process
;
2132 if (gdbserver_state
.process_num
) {
2133 max_pid
= s
->processes
[s
->process_num
- 1].pid
;
2136 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2137 process
= &s
->processes
[s
->process_num
- 1];
2139 /* We need an available PID slot for this process */
2140 assert(max_pid
< UINT32_MAX
);
2142 process
->pid
= max_pid
+ 1;
2143 process
->attached
= false;
2144 process
->target_xml
[0] = '\0';