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-common.h"
28 #include "qapi/error.h"
29 #include "qemu/error-report.h"
30 #include "qemu/ctype.h"
31 #include "qemu/cutils.h"
32 #include "qemu/module.h"
33 #include "trace-root.h"
34 #ifdef CONFIG_USER_ONLY
37 #include "monitor/monitor.h"
38 #include "chardev/char.h"
39 #include "chardev/char-fe.h"
40 #include "sysemu/sysemu.h"
41 #include "exec/gdbstub.h"
42 #include "hw/cpu/cluster.h"
43 #include "hw/boards.h"
46 #define MAX_PACKET_LENGTH 4096
48 #include "qemu/sockets.h"
49 #include "sysemu/hw_accel.h"
50 #include "sysemu/kvm.h"
51 #include "sysemu/runstate.h"
52 #include "hw/semihosting/semihost.h"
53 #include "exec/exec-all.h"
55 #ifdef CONFIG_USER_ONLY
56 #define GDB_ATTACHED "0"
58 #define GDB_ATTACHED "1"
61 #ifndef CONFIG_USER_ONLY
62 static int phy_memory_mode
;
65 static inline int target_memory_rw_debug(CPUState
*cpu
, target_ulong addr
,
66 uint8_t *buf
, int len
, bool is_write
)
70 #ifndef CONFIG_USER_ONLY
71 if (phy_memory_mode
) {
73 cpu_physical_memory_write(addr
, buf
, len
);
75 cpu_physical_memory_read(addr
, buf
, len
);
81 cc
= CPU_GET_CLASS(cpu
);
82 if (cc
->memory_rw_debug
) {
83 return cc
->memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
85 return cpu_memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
88 /* Return the GDB index for a given vCPU state.
90 * For user mode this is simply the thread id. In system mode GDB
91 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
93 static inline int cpu_gdb_index(CPUState
*cpu
)
95 #if defined(CONFIG_USER_ONLY)
96 TaskState
*ts
= (TaskState
*) cpu
->opaque
;
99 return cpu
->cpu_index
+ 1;
109 GDB_SIGNAL_ALRM
= 14,
111 GDB_SIGNAL_XCPU
= 24,
112 GDB_SIGNAL_UNKNOWN
= 143
115 #ifdef CONFIG_USER_ONLY
117 /* Map target signal numbers to GDB protocol signal numbers and vice
118 * versa. For user emulation's currently supported systems, we can
119 * assume most signals are defined.
122 static int gdb_signal_table
[] = {
282 /* In system mode we only need SIGINT and SIGTRAP; other signals
283 are not yet supported. */
290 static int gdb_signal_table
[] = {
300 #ifdef CONFIG_USER_ONLY
301 static int target_signal_to_gdb (int sig
)
304 for (i
= 0; i
< ARRAY_SIZE (gdb_signal_table
); i
++)
305 if (gdb_signal_table
[i
] == sig
)
307 return GDB_SIGNAL_UNKNOWN
;
311 static int gdb_signal_to_target (int sig
)
313 if (sig
< ARRAY_SIZE (gdb_signal_table
))
314 return gdb_signal_table
[sig
];
319 typedef struct GDBRegisterState
{
325 struct GDBRegisterState
*next
;
328 typedef struct GDBProcess
{
332 char target_xml
[1024];
344 typedef struct GDBState
{
345 CPUState
*c_cpu
; /* current CPU for step/continue ops */
346 CPUState
*g_cpu
; /* current CPU for other ops */
347 CPUState
*query_cpu
; /* for q{f|s}ThreadInfo */
348 enum RSState state
; /* parsing state */
349 char line_buf
[MAX_PACKET_LENGTH
];
351 int line_sum
; /* running checksum */
352 int line_csum
; /* checksum at the end of the packet */
353 uint8_t last_packet
[MAX_PACKET_LENGTH
+ 4];
356 #ifdef CONFIG_USER_ONLY
364 GDBProcess
*processes
;
366 char syscall_buf
[256];
367 gdb_syscall_complete_cb current_syscall_cb
;
370 /* By default use no IRQs and no timers while single stepping so as to
371 * make single stepping like an ICE HW step.
373 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
375 static GDBState
*gdbserver_state
;
379 #ifdef CONFIG_USER_ONLY
380 /* XXX: This is not thread safe. Do we care? */
381 static int gdbserver_fd
= -1;
383 static int get_char(GDBState
*s
)
389 ret
= qemu_recv(s
->fd
, &ch
, 1, 0);
391 if (errno
== ECONNRESET
)
395 } else if (ret
== 0) {
413 /* Decide if either remote gdb syscalls or native file IO should be used. */
414 int use_gdb_syscalls(void)
416 SemihostingTarget target
= semihosting_get_target();
417 if (target
== SEMIHOSTING_TARGET_NATIVE
) {
418 /* -semihosting-config target=native */
420 } else if (target
== SEMIHOSTING_TARGET_GDB
) {
421 /* -semihosting-config target=gdb */
425 /* -semihosting-config target=auto */
426 /* On the first call check if gdb is connected and remember. */
427 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
428 gdb_syscall_mode
= (gdbserver_state
? GDB_SYS_ENABLED
431 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
434 /* Resume execution. */
435 static inline void gdb_continue(GDBState
*s
)
438 #ifdef CONFIG_USER_ONLY
439 s
->running_state
= 1;
440 trace_gdbstub_op_continue();
442 if (!runstate_needs_reset()) {
443 trace_gdbstub_op_continue();
450 * Resume execution, per CPU actions. For user-mode emulation it's
451 * equivalent to gdb_continue.
453 static int gdb_continue_partial(GDBState
*s
, char *newstates
)
457 #ifdef CONFIG_USER_ONLY
459 * This is not exactly accurate, but it's an improvement compared to the
460 * previous situation, where only one CPU would be single-stepped.
463 if (newstates
[cpu
->cpu_index
] == 's') {
464 trace_gdbstub_op_stepping(cpu
->cpu_index
);
465 cpu_single_step(cpu
, sstep_flags
);
468 s
->running_state
= 1;
472 if (!runstate_needs_reset()) {
473 if (vm_prepare_start()) {
478 switch (newstates
[cpu
->cpu_index
]) {
481 break; /* nothing to do here */
483 trace_gdbstub_op_stepping(cpu
->cpu_index
);
484 cpu_single_step(cpu
, sstep_flags
);
489 trace_gdbstub_op_continue_cpu(cpu
->cpu_index
);
500 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, true);
506 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
508 #ifdef CONFIG_USER_ONLY
512 ret
= send(s
->fd
, buf
, len
, 0);
522 /* XXX this blocks entire thread. Rewrite to use
523 * qemu_chr_fe_write and background I/O callbacks */
524 qemu_chr_fe_write_all(&s
->chr
, buf
, len
);
528 static inline int fromhex(int v
)
530 if (v
>= '0' && v
<= '9')
532 else if (v
>= 'A' && v
<= 'F')
534 else if (v
>= 'a' && v
<= 'f')
540 static inline int tohex(int v
)
548 /* writes 2*len+1 bytes in buf */
549 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
554 for(i
= 0; i
< len
; i
++) {
556 *q
++ = tohex(c
>> 4);
557 *q
++ = tohex(c
& 0xf);
562 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
566 for(i
= 0; i
< len
; i
++) {
567 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
572 static void hexdump(const char *buf
, int len
,
573 void (*trace_fn
)(size_t ofs
, char const *text
))
575 char line_buffer
[3 * 16 + 4 + 16 + 1];
578 for (i
= 0; i
< len
|| (i
& 0xF); ++i
) {
579 size_t byte_ofs
= i
& 15;
582 memset(line_buffer
, ' ', 3 * 16 + 4 + 16);
583 line_buffer
[3 * 16 + 4 + 16] = 0;
586 size_t col_group
= (i
>> 2) & 3;
587 size_t hex_col
= byte_ofs
* 3 + col_group
;
588 size_t txt_col
= 3 * 16 + 4 + byte_ofs
;
593 line_buffer
[hex_col
+ 0] = tohex((value
>> 4) & 0xF);
594 line_buffer
[hex_col
+ 1] = tohex((value
>> 0) & 0xF);
595 line_buffer
[txt_col
+ 0] = (value
>= ' ' && value
< 127)
601 trace_fn(i
& -16, line_buffer
);
605 /* return -1 if error, 0 if OK */
606 static int put_packet_binary(GDBState
*s
, const char *buf
, int len
, bool dump
)
611 if (dump
&& trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY
)) {
612 hexdump(buf
, len
, trace_gdbstub_io_binaryreply
);
621 for(i
= 0; i
< len
; i
++) {
625 *(p
++) = tohex((csum
>> 4) & 0xf);
626 *(p
++) = tohex((csum
) & 0xf);
628 s
->last_packet_len
= p
- s
->last_packet
;
629 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
631 #ifdef CONFIG_USER_ONLY
644 /* return -1 if error, 0 if OK */
645 static int put_packet(GDBState
*s
, const char *buf
)
647 trace_gdbstub_io_reply(buf
);
649 return put_packet_binary(s
, buf
, strlen(buf
), false);
652 /* Encode data using the encoding for 'x' packets. */
653 static int memtox(char *buf
, const char *mem
, int len
)
661 case '#': case '$': case '*': case '}':
673 static uint32_t gdb_get_cpu_pid(const GDBState
*s
, CPUState
*cpu
)
675 /* TODO: In user mode, we should use the task state PID */
676 if (cpu
->cluster_index
== UNASSIGNED_CLUSTER_INDEX
) {
677 /* Return the default process' PID */
678 return s
->processes
[s
->process_num
- 1].pid
;
680 return cpu
->cluster_index
+ 1;
683 static GDBProcess
*gdb_get_process(const GDBState
*s
, uint32_t pid
)
688 /* 0 means any process, we take the first one */
689 return &s
->processes
[0];
692 for (i
= 0; i
< s
->process_num
; i
++) {
693 if (s
->processes
[i
].pid
== pid
) {
694 return &s
->processes
[i
];
701 static GDBProcess
*gdb_get_cpu_process(const GDBState
*s
, CPUState
*cpu
)
703 return gdb_get_process(s
, gdb_get_cpu_pid(s
, cpu
));
706 static CPUState
*find_cpu(uint32_t thread_id
)
711 if (cpu_gdb_index(cpu
) == thread_id
) {
719 static CPUState
*get_first_cpu_in_process(const GDBState
*s
,
725 if (gdb_get_cpu_pid(s
, cpu
) == process
->pid
) {
733 static CPUState
*gdb_next_cpu_in_process(const GDBState
*s
, CPUState
*cpu
)
735 uint32_t pid
= gdb_get_cpu_pid(s
, cpu
);
739 if (gdb_get_cpu_pid(s
, cpu
) == pid
) {
749 /* Return the cpu following @cpu, while ignoring unattached processes. */
750 static CPUState
*gdb_next_attached_cpu(const GDBState
*s
, CPUState
*cpu
)
755 if (gdb_get_cpu_process(s
, cpu
)->attached
) {
765 /* Return the first attached cpu */
766 static CPUState
*gdb_first_attached_cpu(const GDBState
*s
)
768 CPUState
*cpu
= first_cpu
;
769 GDBProcess
*process
= gdb_get_cpu_process(s
, cpu
);
771 if (!process
->attached
) {
772 return gdb_next_attached_cpu(s
, cpu
);
778 static CPUState
*gdb_get_cpu(const GDBState
*s
, uint32_t pid
, uint32_t tid
)
784 /* 0 means any process/thread, we take the first attached one */
785 return gdb_first_attached_cpu(s
);
786 } else if (pid
&& !tid
) {
787 /* any thread in a specific process */
788 process
= gdb_get_process(s
, pid
);
790 if (process
== NULL
) {
794 if (!process
->attached
) {
798 return get_first_cpu_in_process(s
, process
);
800 /* a specific thread */
807 process
= gdb_get_cpu_process(s
, cpu
);
809 if (pid
&& process
->pid
!= pid
) {
813 if (!process
->attached
) {
821 static const char *get_feature_xml(const GDBState
*s
, const char *p
,
822 const char **newp
, GDBProcess
*process
)
827 CPUState
*cpu
= get_first_cpu_in_process(s
, process
);
828 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
831 while (p
[len
] && p
[len
] != ':')
836 if (strncmp(p
, "target.xml", len
) == 0) {
837 char *buf
= process
->target_xml
;
838 const size_t buf_sz
= sizeof(process
->target_xml
);
840 /* Generate the XML description for this CPU. */
845 "<?xml version=\"1.0\"?>"
846 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
848 if (cc
->gdb_arch_name
) {
849 gchar
*arch
= cc
->gdb_arch_name(cpu
);
850 pstrcat(buf
, buf_sz
, "<architecture>");
851 pstrcat(buf
, buf_sz
, arch
);
852 pstrcat(buf
, buf_sz
, "</architecture>");
855 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
856 pstrcat(buf
, buf_sz
, cc
->gdb_core_xml_file
);
857 pstrcat(buf
, buf_sz
, "\"/>");
858 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
859 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
860 pstrcat(buf
, buf_sz
, r
->xml
);
861 pstrcat(buf
, buf_sz
, "\"/>");
863 pstrcat(buf
, buf_sz
, "</target>");
867 if (cc
->gdb_get_dynamic_xml
) {
868 char *xmlname
= g_strndup(p
, len
);
869 const char *xml
= cc
->gdb_get_dynamic_xml(cpu
, xmlname
);
877 name
= xml_builtin
[i
][0];
878 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
881 return name
? xml_builtin
[i
][1] : NULL
;
884 static int gdb_read_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
886 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
887 CPUArchState
*env
= cpu
->env_ptr
;
890 if (reg
< cc
->gdb_num_core_regs
) {
891 return cc
->gdb_read_register(cpu
, mem_buf
, reg
);
894 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
895 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
896 return r
->get_reg(env
, mem_buf
, reg
- r
->base_reg
);
902 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
904 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
905 CPUArchState
*env
= cpu
->env_ptr
;
908 if (reg
< cc
->gdb_num_core_regs
) {
909 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
912 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
913 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
914 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
920 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
921 specifies the first register number and these registers are included in
922 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
923 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
926 void gdb_register_coprocessor(CPUState
*cpu
,
927 gdb_reg_cb get_reg
, gdb_reg_cb set_reg
,
928 int num_regs
, const char *xml
, int g_pos
)
931 GDBRegisterState
**p
;
935 /* Check for duplicates. */
936 if (strcmp((*p
)->xml
, xml
) == 0)
941 s
= g_new0(GDBRegisterState
, 1);
942 s
->base_reg
= cpu
->gdb_num_regs
;
943 s
->num_regs
= num_regs
;
944 s
->get_reg
= get_reg
;
945 s
->set_reg
= set_reg
;
948 /* Add to end of list. */
949 cpu
->gdb_num_regs
+= num_regs
;
952 if (g_pos
!= s
->base_reg
) {
953 error_report("Error: Bad gdb register numbering for '%s', "
954 "expected %d got %d", xml
, g_pos
, s
->base_reg
);
956 cpu
->gdb_num_g_regs
= cpu
->gdb_num_regs
;
961 #ifndef CONFIG_USER_ONLY
962 /* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
963 static inline int xlat_gdb_type(CPUState
*cpu
, int gdbtype
)
965 static const int xlat
[] = {
966 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
967 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
968 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
971 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
972 int cputype
= xlat
[gdbtype
];
974 if (cc
->gdb_stop_before_watchpoint
) {
975 cputype
|= BP_STOP_BEFORE_ACCESS
;
981 static int gdb_breakpoint_insert(int type
, target_ulong addr
, target_ulong len
)
987 return kvm_insert_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
991 case GDB_BREAKPOINT_SW
:
992 case GDB_BREAKPOINT_HW
:
994 err
= cpu_breakpoint_insert(cpu
, addr
, BP_GDB
, NULL
);
1000 #ifndef CONFIG_USER_ONLY
1001 case GDB_WATCHPOINT_WRITE
:
1002 case GDB_WATCHPOINT_READ
:
1003 case GDB_WATCHPOINT_ACCESS
:
1005 err
= cpu_watchpoint_insert(cpu
, addr
, len
,
1006 xlat_gdb_type(cpu
, type
), NULL
);
1018 static int gdb_breakpoint_remove(int type
, target_ulong addr
, target_ulong len
)
1023 if (kvm_enabled()) {
1024 return kvm_remove_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1028 case GDB_BREAKPOINT_SW
:
1029 case GDB_BREAKPOINT_HW
:
1031 err
= cpu_breakpoint_remove(cpu
, addr
, BP_GDB
);
1037 #ifndef CONFIG_USER_ONLY
1038 case GDB_WATCHPOINT_WRITE
:
1039 case GDB_WATCHPOINT_READ
:
1040 case GDB_WATCHPOINT_ACCESS
:
1042 err
= cpu_watchpoint_remove(cpu
, addr
, len
,
1043 xlat_gdb_type(cpu
, type
));
1054 static inline void gdb_cpu_breakpoint_remove_all(CPUState
*cpu
)
1056 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
1057 #ifndef CONFIG_USER_ONLY
1058 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
1062 static void gdb_process_breakpoint_remove_all(const GDBState
*s
, GDBProcess
*p
)
1064 CPUState
*cpu
= get_first_cpu_in_process(s
, p
);
1067 gdb_cpu_breakpoint_remove_all(cpu
);
1068 cpu
= gdb_next_cpu_in_process(s
, cpu
);
1072 static void gdb_breakpoint_remove_all(void)
1076 if (kvm_enabled()) {
1077 kvm_remove_all_breakpoints(gdbserver_state
->c_cpu
);
1082 gdb_cpu_breakpoint_remove_all(cpu
);
1086 static void gdb_set_cpu_pc(GDBState
*s
, target_ulong pc
)
1088 CPUState
*cpu
= s
->c_cpu
;
1090 cpu_synchronize_state(cpu
);
1091 cpu_set_pc(cpu
, pc
);
1094 static char *gdb_fmt_thread_id(const GDBState
*s
, CPUState
*cpu
,
1095 char *buf
, size_t buf_size
)
1097 if (s
->multiprocess
) {
1098 snprintf(buf
, buf_size
, "p%02x.%02x",
1099 gdb_get_cpu_pid(s
, cpu
), cpu_gdb_index(cpu
));
1101 snprintf(buf
, buf_size
, "%02x", cpu_gdb_index(cpu
));
1107 typedef enum GDBThreadIdKind
{
1109 GDB_ALL_THREADS
, /* One process, all threads */
1114 static GDBThreadIdKind
read_thread_id(const char *buf
, const char **end_buf
,
1115 uint32_t *pid
, uint32_t *tid
)
1122 ret
= qemu_strtoul(buf
, &buf
, 16, &p
);
1125 return GDB_READ_THREAD_ERR
;
1134 ret
= qemu_strtoul(buf
, &buf
, 16, &t
);
1137 return GDB_READ_THREAD_ERR
;
1143 return GDB_ALL_PROCESSES
;
1151 return GDB_ALL_THREADS
;
1158 return GDB_ONE_THREAD
;
1162 * gdb_handle_vcont - Parses and handles a vCont packet.
1163 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1164 * a format error, 0 on success.
1166 static int gdb_handle_vcont(GDBState
*s
, const char *p
)
1168 int res
, signal
= 0;
1173 GDBProcess
*process
;
1175 GDBThreadIdKind kind
;
1176 #ifdef CONFIG_USER_ONLY
1177 int max_cpus
= 1; /* global variable max_cpus exists only in system mode */
1180 max_cpus
= max_cpus
<= cpu
->cpu_index
? cpu
->cpu_index
+ 1 : max_cpus
;
1183 MachineState
*ms
= MACHINE(qdev_get_machine());
1184 unsigned int max_cpus
= ms
->smp
.max_cpus
;
1186 /* uninitialised CPUs stay 0 */
1187 newstates
= g_new0(char, max_cpus
);
1189 /* mark valid CPUs with 1 */
1191 newstates
[cpu
->cpu_index
] = 1;
1195 * res keeps track of what error we are returning, with -ENOTSUP meaning
1196 * that the command is unknown or unsupported, thus returning an empty
1197 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1198 * or incorrect parameters passed.
1208 if (cur_action
== 'C' || cur_action
== 'S') {
1209 cur_action
= qemu_tolower(cur_action
);
1210 res
= qemu_strtoul(p
+ 1, &p
, 16, &tmp
);
1214 signal
= gdb_signal_to_target(tmp
);
1215 } else if (cur_action
!= 'c' && cur_action
!= 's') {
1216 /* unknown/invalid/unsupported command */
1221 if (*p
== '\0' || *p
== ';') {
1223 * No thread specifier, action is on "all threads". The
1224 * specification is unclear regarding the process to act on. We
1225 * choose all processes.
1227 kind
= GDB_ALL_PROCESSES
;
1228 } else if (*p
++ == ':') {
1229 kind
= read_thread_id(p
, &p
, &pid
, &tid
);
1236 case GDB_READ_THREAD_ERR
:
1240 case GDB_ALL_PROCESSES
:
1241 cpu
= gdb_first_attached_cpu(s
);
1243 if (newstates
[cpu
->cpu_index
] == 1) {
1244 newstates
[cpu
->cpu_index
] = cur_action
;
1247 cpu
= gdb_next_attached_cpu(s
, cpu
);
1251 case GDB_ALL_THREADS
:
1252 process
= gdb_get_process(s
, pid
);
1254 if (!process
->attached
) {
1259 cpu
= get_first_cpu_in_process(s
, process
);
1261 if (newstates
[cpu
->cpu_index
] == 1) {
1262 newstates
[cpu
->cpu_index
] = cur_action
;
1265 cpu
= gdb_next_cpu_in_process(s
, cpu
);
1269 case GDB_ONE_THREAD
:
1270 cpu
= gdb_get_cpu(s
, pid
, tid
);
1272 /* invalid CPU/thread specified */
1278 /* only use if no previous match occourred */
1279 if (newstates
[cpu
->cpu_index
] == 1) {
1280 newstates
[cpu
->cpu_index
] = cur_action
;
1286 gdb_continue_partial(s
, newstates
);
1294 typedef union GdbCmdVariant
{
1297 unsigned long val_ul
;
1298 unsigned long long val_ull
;
1300 GDBThreadIdKind kind
;
1306 static const char *cmd_next_param(const char *param
, const char delimiter
)
1308 static const char all_delimiters
[] = ",;:=";
1309 char curr_delimiters
[2] = {0};
1310 const char *delimiters
;
1312 if (delimiter
== '?') {
1313 delimiters
= all_delimiters
;
1314 } else if (delimiter
== '0') {
1315 return strchr(param
, '\0');
1316 } else if (delimiter
== '.' && *param
) {
1319 curr_delimiters
[0] = delimiter
;
1320 delimiters
= curr_delimiters
;
1323 param
+= strcspn(param
, delimiters
);
1330 static int cmd_parse_params(const char *data
, const char *schema
,
1331 GdbCmdVariant
*params
, int *num_params
)
1334 const char *curr_schema
, *curr_data
;
1342 curr_schema
= schema
;
1345 while (curr_schema
[0] && curr_schema
[1] && *curr_data
) {
1346 switch (curr_schema
[0]) {
1348 if (qemu_strtoul(curr_data
, &curr_data
, 16,
1349 ¶ms
[curr_param
].val_ul
)) {
1353 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1356 if (qemu_strtou64(curr_data
, &curr_data
, 16,
1357 (uint64_t *)¶ms
[curr_param
].val_ull
)) {
1361 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1364 params
[curr_param
].data
= curr_data
;
1366 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1369 params
[curr_param
].opcode
= *(uint8_t *)curr_data
;
1371 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1374 params
[curr_param
].thread_id
.kind
=
1375 read_thread_id(curr_data
, &curr_data
,
1376 ¶ms
[curr_param
].thread_id
.pid
,
1377 ¶ms
[curr_param
].thread_id
.tid
);
1379 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1382 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1390 *num_params
= curr_param
;
1394 typedef struct GdbCmdContext
{
1396 GdbCmdVariant
*params
;
1398 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1399 char str_buf
[MAX_PACKET_LENGTH
+ 1];
1402 typedef void (*GdbCmdHandler
)(GdbCmdContext
*gdb_ctx
, void *user_ctx
);
1405 * cmd_startswith -> cmd is compared using startswith
1408 * schema definitions:
1409 * Each schema parameter entry consists of 2 chars,
1410 * the first char represents the parameter type handling
1411 * the second char represents the delimiter for the next parameter
1413 * Currently supported schema types:
1414 * 'l' -> unsigned long (stored in .val_ul)
1415 * 'L' -> unsigned long long (stored in .val_ull)
1416 * 's' -> string (stored in .data)
1417 * 'o' -> single char (stored in .opcode)
1418 * 't' -> thread id (stored in .thread_id)
1419 * '?' -> skip according to delimiter
1421 * Currently supported delimiters:
1422 * '?' -> Stop at any delimiter (",;:=\0")
1423 * '0' -> Stop at "\0"
1424 * '.' -> Skip 1 char unless reached "\0"
1425 * Any other value is treated as the delimiter value itself
1427 typedef struct GdbCmdParseEntry
{
1428 GdbCmdHandler handler
;
1430 bool cmd_startswith
;
1434 static inline int startswith(const char *string
, const char *pattern
)
1436 return !strncmp(string
, pattern
, strlen(pattern
));
1439 static int process_string_cmd(GDBState
*s
, void *user_ctx
, const char *data
,
1440 const GdbCmdParseEntry
*cmds
, int num_cmds
)
1442 int i
, schema_len
, max_num_params
= 0;
1443 GdbCmdContext gdb_ctx
;
1449 for (i
= 0; i
< num_cmds
; i
++) {
1450 const GdbCmdParseEntry
*cmd
= &cmds
[i
];
1451 g_assert(cmd
->handler
&& cmd
->cmd
);
1453 if ((cmd
->cmd_startswith
&& !startswith(data
, cmd
->cmd
)) ||
1454 (!cmd
->cmd_startswith
&& strcmp(cmd
->cmd
, data
))) {
1459 schema_len
= strlen(cmd
->schema
);
1460 if (schema_len
% 2) {
1464 max_num_params
= schema_len
/ 2;
1468 (GdbCmdVariant
*)alloca(sizeof(*gdb_ctx
.params
) * max_num_params
);
1469 memset(gdb_ctx
.params
, 0, sizeof(*gdb_ctx
.params
) * max_num_params
);
1471 if (cmd_parse_params(&data
[strlen(cmd
->cmd
)], cmd
->schema
,
1472 gdb_ctx
.params
, &gdb_ctx
.num_params
)) {
1477 cmd
->handler(&gdb_ctx
, user_ctx
);
1484 static void run_cmd_parser(GDBState
*s
, const char *data
,
1485 const GdbCmdParseEntry
*cmd
)
1491 /* In case there was an error during the command parsing we must
1492 * send a NULL packet to indicate the command is not supported */
1493 if (process_string_cmd(s
, NULL
, data
, cmd
, 1)) {
1498 static void handle_detach(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1500 GDBProcess
*process
;
1501 GDBState
*s
= gdb_ctx
->s
;
1504 if (s
->multiprocess
) {
1505 if (!gdb_ctx
->num_params
) {
1506 put_packet(s
, "E22");
1510 pid
= gdb_ctx
->params
[0].val_ul
;
1513 process
= gdb_get_process(s
, pid
);
1514 gdb_process_breakpoint_remove_all(s
, process
);
1515 process
->attached
= false;
1517 if (pid
== gdb_get_cpu_pid(s
, s
->c_cpu
)) {
1518 s
->c_cpu
= gdb_first_attached_cpu(s
);
1521 if (pid
== gdb_get_cpu_pid(s
, s
->g_cpu
)) {
1522 s
->g_cpu
= gdb_first_attached_cpu(s
);
1526 /* No more process attached */
1527 gdb_syscall_mode
= GDB_SYS_DISABLED
;
1530 put_packet(s
, "OK");
1533 static void handle_thread_alive(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1537 if (!gdb_ctx
->num_params
) {
1538 put_packet(gdb_ctx
->s
, "E22");
1542 if (gdb_ctx
->params
[0].thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1543 put_packet(gdb_ctx
->s
, "E22");
1547 cpu
= gdb_get_cpu(gdb_ctx
->s
, gdb_ctx
->params
[0].thread_id
.pid
,
1548 gdb_ctx
->params
[0].thread_id
.tid
);
1550 put_packet(gdb_ctx
->s
, "E22");
1554 put_packet(gdb_ctx
->s
, "OK");
1557 static void handle_continue(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1559 if (gdb_ctx
->num_params
) {
1560 gdb_set_cpu_pc(gdb_ctx
->s
, gdb_ctx
->params
[0].val_ull
);
1563 gdb_ctx
->s
->signal
= 0;
1564 gdb_continue(gdb_ctx
->s
);
1567 static void handle_cont_with_sig(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1569 unsigned long signal
= 0;
1572 * Note: C sig;[addr] is currently unsupported and we simply
1573 * omit the addr parameter
1575 if (gdb_ctx
->num_params
) {
1576 signal
= gdb_ctx
->params
[0].val_ul
;
1579 gdb_ctx
->s
->signal
= gdb_signal_to_target(signal
);
1580 if (gdb_ctx
->s
->signal
== -1) {
1581 gdb_ctx
->s
->signal
= 0;
1583 gdb_continue(gdb_ctx
->s
);
1586 static void handle_set_thread(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1590 if (gdb_ctx
->num_params
!= 2) {
1591 put_packet(gdb_ctx
->s
, "E22");
1595 if (gdb_ctx
->params
[1].thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1596 put_packet(gdb_ctx
->s
, "E22");
1600 if (gdb_ctx
->params
[1].thread_id
.kind
!= GDB_ONE_THREAD
) {
1601 put_packet(gdb_ctx
->s
, "OK");
1605 cpu
= gdb_get_cpu(gdb_ctx
->s
, gdb_ctx
->params
[1].thread_id
.pid
,
1606 gdb_ctx
->params
[1].thread_id
.tid
);
1608 put_packet(gdb_ctx
->s
, "E22");
1613 * Note: This command is deprecated and modern gdb's will be using the
1614 * vCont command instead.
1616 switch (gdb_ctx
->params
[0].opcode
) {
1618 gdb_ctx
->s
->c_cpu
= cpu
;
1619 put_packet(gdb_ctx
->s
, "OK");
1622 gdb_ctx
->s
->g_cpu
= cpu
;
1623 put_packet(gdb_ctx
->s
, "OK");
1626 put_packet(gdb_ctx
->s
, "E22");
1631 static void handle_insert_bp(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1635 if (gdb_ctx
->num_params
!= 3) {
1636 put_packet(gdb_ctx
->s
, "E22");
1640 res
= gdb_breakpoint_insert(gdb_ctx
->params
[0].val_ul
,
1641 gdb_ctx
->params
[1].val_ull
,
1642 gdb_ctx
->params
[2].val_ull
);
1644 put_packet(gdb_ctx
->s
, "OK");
1646 } else if (res
== -ENOSYS
) {
1647 put_packet(gdb_ctx
->s
, "");
1651 put_packet(gdb_ctx
->s
, "E22");
1654 static void handle_remove_bp(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1658 if (gdb_ctx
->num_params
!= 3) {
1659 put_packet(gdb_ctx
->s
, "E22");
1663 res
= gdb_breakpoint_remove(gdb_ctx
->params
[0].val_ul
,
1664 gdb_ctx
->params
[1].val_ull
,
1665 gdb_ctx
->params
[2].val_ull
);
1667 put_packet(gdb_ctx
->s
, "OK");
1669 } else if (res
== -ENOSYS
) {
1670 put_packet(gdb_ctx
->s
, "");
1674 put_packet(gdb_ctx
->s
, "E22");
1678 * handle_set/get_reg
1680 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1681 * This works, but can be very slow. Anything new enough to understand
1682 * XML also knows how to use this properly. However to use this we
1683 * need to define a local XML file as well as be talking to a
1684 * reasonably modern gdb. Responding with an empty packet will cause
1685 * the remote gdb to fallback to older methods.
1688 static void handle_set_reg(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1693 put_packet(gdb_ctx
->s
, "");
1697 if (gdb_ctx
->num_params
!= 2) {
1698 put_packet(gdb_ctx
->s
, "E22");
1702 reg_size
= strlen(gdb_ctx
->params
[1].data
) / 2;
1703 hextomem(gdb_ctx
->mem_buf
, gdb_ctx
->params
[1].data
, reg_size
);
1704 gdb_write_register(gdb_ctx
->s
->g_cpu
, gdb_ctx
->mem_buf
,
1705 gdb_ctx
->params
[0].val_ull
);
1706 put_packet(gdb_ctx
->s
, "OK");
1709 static void handle_get_reg(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1714 put_packet(gdb_ctx
->s
, "");
1718 if (!gdb_ctx
->num_params
) {
1719 put_packet(gdb_ctx
->s
, "E14");
1723 reg_size
= gdb_read_register(gdb_ctx
->s
->g_cpu
, gdb_ctx
->mem_buf
,
1724 gdb_ctx
->params
[0].val_ull
);
1726 put_packet(gdb_ctx
->s
, "E14");
1730 memtohex(gdb_ctx
->str_buf
, gdb_ctx
->mem_buf
, reg_size
);
1731 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1734 static void handle_write_mem(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1736 if (gdb_ctx
->num_params
!= 3) {
1737 put_packet(gdb_ctx
->s
, "E22");
1741 /* hextomem() reads 2*len bytes */
1742 if (gdb_ctx
->params
[1].val_ull
> strlen(gdb_ctx
->params
[2].data
) / 2) {
1743 put_packet(gdb_ctx
->s
, "E22");
1747 hextomem(gdb_ctx
->mem_buf
, gdb_ctx
->params
[2].data
,
1748 gdb_ctx
->params
[1].val_ull
);
1749 if (target_memory_rw_debug(gdb_ctx
->s
->g_cpu
, gdb_ctx
->params
[0].val_ull
,
1751 gdb_ctx
->params
[1].val_ull
, true)) {
1752 put_packet(gdb_ctx
->s
, "E14");
1756 put_packet(gdb_ctx
->s
, "OK");
1759 static void handle_read_mem(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1761 if (gdb_ctx
->num_params
!= 2) {
1762 put_packet(gdb_ctx
->s
, "E22");
1766 /* memtohex() doubles the required space */
1767 if (gdb_ctx
->params
[1].val_ull
> MAX_PACKET_LENGTH
/ 2) {
1768 put_packet(gdb_ctx
->s
, "E22");
1772 if (target_memory_rw_debug(gdb_ctx
->s
->g_cpu
, gdb_ctx
->params
[0].val_ull
,
1774 gdb_ctx
->params
[1].val_ull
, false)) {
1775 put_packet(gdb_ctx
->s
, "E14");
1779 memtohex(gdb_ctx
->str_buf
, gdb_ctx
->mem_buf
, gdb_ctx
->params
[1].val_ull
);
1780 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1783 static void handle_write_all_regs(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1785 target_ulong addr
, len
;
1789 if (!gdb_ctx
->num_params
) {
1793 cpu_synchronize_state(gdb_ctx
->s
->g_cpu
);
1794 registers
= gdb_ctx
->mem_buf
;
1795 len
= strlen(gdb_ctx
->params
[0].data
) / 2;
1796 hextomem(registers
, gdb_ctx
->params
[0].data
, len
);
1797 for (addr
= 0; addr
< gdb_ctx
->s
->g_cpu
->gdb_num_g_regs
&& len
> 0;
1799 reg_size
= gdb_write_register(gdb_ctx
->s
->g_cpu
, registers
, addr
);
1801 registers
+= reg_size
;
1803 put_packet(gdb_ctx
->s
, "OK");
1806 static void handle_read_all_regs(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1808 target_ulong addr
, len
;
1810 cpu_synchronize_state(gdb_ctx
->s
->g_cpu
);
1812 for (addr
= 0; addr
< gdb_ctx
->s
->g_cpu
->gdb_num_g_regs
; addr
++) {
1813 len
+= gdb_read_register(gdb_ctx
->s
->g_cpu
, gdb_ctx
->mem_buf
+ len
,
1817 memtohex(gdb_ctx
->str_buf
, gdb_ctx
->mem_buf
, len
);
1818 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1821 static void handle_file_io(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1823 if (gdb_ctx
->num_params
>= 1 && gdb_ctx
->s
->current_syscall_cb
) {
1824 target_ulong ret
, err
;
1826 ret
= (target_ulong
)gdb_ctx
->params
[0].val_ull
;
1827 if (gdb_ctx
->num_params
>= 2) {
1828 err
= (target_ulong
)gdb_ctx
->params
[1].val_ull
;
1832 gdb_ctx
->s
->current_syscall_cb(gdb_ctx
->s
->c_cpu
, ret
, err
);
1833 gdb_ctx
->s
->current_syscall_cb
= NULL
;
1836 if (gdb_ctx
->num_params
>= 3 && gdb_ctx
->params
[2].opcode
== (uint8_t)'C') {
1837 put_packet(gdb_ctx
->s
, "T02");
1841 gdb_continue(gdb_ctx
->s
);
1844 static void handle_step(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1846 if (gdb_ctx
->num_params
) {
1847 gdb_set_cpu_pc(gdb_ctx
->s
, (target_ulong
)gdb_ctx
->params
[0].val_ull
);
1850 cpu_single_step(gdb_ctx
->s
->c_cpu
, sstep_flags
);
1851 gdb_continue(gdb_ctx
->s
);
1854 static void handle_v_cont_query(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1856 put_packet(gdb_ctx
->s
, "vCont;c;C;s;S");
1859 static void handle_v_cont(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1863 if (!gdb_ctx
->num_params
) {
1867 res
= gdb_handle_vcont(gdb_ctx
->s
, gdb_ctx
->params
[0].data
);
1868 if ((res
== -EINVAL
) || (res
== -ERANGE
)) {
1869 put_packet(gdb_ctx
->s
, "E22");
1871 put_packet(gdb_ctx
->s
, "");
1875 static void handle_v_attach(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1877 GDBProcess
*process
;
1881 pstrcpy(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "E22");
1882 if (!gdb_ctx
->num_params
) {
1886 process
= gdb_get_process(gdb_ctx
->s
, gdb_ctx
->params
[0].val_ul
);
1891 cpu
= get_first_cpu_in_process(gdb_ctx
->s
, process
);
1896 process
->attached
= true;
1897 gdb_ctx
->s
->g_cpu
= cpu
;
1898 gdb_ctx
->s
->c_cpu
= cpu
;
1900 gdb_fmt_thread_id(gdb_ctx
->s
, cpu
, thread_id
, sizeof(thread_id
));
1901 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "T%02xthread:%s;",
1902 GDB_SIGNAL_TRAP
, thread_id
);
1904 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1907 static void handle_v_kill(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1909 /* Kill the target */
1910 put_packet(gdb_ctx
->s
, "OK");
1911 error_report("QEMU: Terminated via GDBstub");
1915 static GdbCmdParseEntry gdb_v_commands_table
[] = {
1916 /* Order is important if has same prefix */
1918 .handler
= handle_v_cont_query
,
1923 .handler
= handle_v_cont
,
1925 .cmd_startswith
= 1,
1929 .handler
= handle_v_attach
,
1931 .cmd_startswith
= 1,
1935 .handler
= handle_v_kill
,
1941 static void handle_v_commands(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1943 if (!gdb_ctx
->num_params
) {
1947 if (process_string_cmd(gdb_ctx
->s
, NULL
, gdb_ctx
->params
[0].data
,
1948 gdb_v_commands_table
,
1949 ARRAY_SIZE(gdb_v_commands_table
))) {
1950 put_packet(gdb_ctx
->s
, "");
1954 static void handle_query_qemu_sstepbits(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1956 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
),
1957 "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", SSTEP_ENABLE
,
1958 SSTEP_NOIRQ
, SSTEP_NOTIMER
);
1959 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1962 static void handle_set_qemu_sstep(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1964 if (!gdb_ctx
->num_params
) {
1968 sstep_flags
= gdb_ctx
->params
[0].val_ul
;
1969 put_packet(gdb_ctx
->s
, "OK");
1972 static void handle_query_qemu_sstep(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1974 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "0x%x", sstep_flags
);
1975 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1978 static void handle_query_curr_tid(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1981 GDBProcess
*process
;
1985 * "Current thread" remains vague in the spec, so always return
1986 * the first thread of the current process (gdb returns the
1989 process
= gdb_get_cpu_process(gdb_ctx
->s
, gdb_ctx
->s
->g_cpu
);
1990 cpu
= get_first_cpu_in_process(gdb_ctx
->s
, process
);
1991 gdb_fmt_thread_id(gdb_ctx
->s
, cpu
, thread_id
, sizeof(thread_id
));
1992 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "QC%s", thread_id
);
1993 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1996 static void handle_query_threads(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2000 if (!gdb_ctx
->s
->query_cpu
) {
2001 put_packet(gdb_ctx
->s
, "l");
2005 gdb_fmt_thread_id(gdb_ctx
->s
, gdb_ctx
->s
->query_cpu
, thread_id
,
2007 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "m%s", thread_id
);
2008 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
2009 gdb_ctx
->s
->query_cpu
=
2010 gdb_next_attached_cpu(gdb_ctx
->s
, gdb_ctx
->s
->query_cpu
);
2013 static void handle_query_first_threads(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2015 gdb_ctx
->s
->query_cpu
= gdb_first_attached_cpu(gdb_ctx
->s
);
2016 handle_query_threads(gdb_ctx
, user_ctx
);
2019 static void handle_query_thread_extra(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2024 if (!gdb_ctx
->num_params
||
2025 gdb_ctx
->params
[0].thread_id
.kind
== GDB_READ_THREAD_ERR
) {
2026 put_packet(gdb_ctx
->s
, "E22");
2030 cpu
= gdb_get_cpu(gdb_ctx
->s
, gdb_ctx
->params
[0].thread_id
.pid
,
2031 gdb_ctx
->params
[0].thread_id
.tid
);
2036 cpu_synchronize_state(cpu
);
2038 if (gdb_ctx
->s
->multiprocess
&& (gdb_ctx
->s
->process_num
> 1)) {
2039 /* Print the CPU model and name in multiprocess mode */
2040 ObjectClass
*oc
= object_get_class(OBJECT(cpu
));
2041 const char *cpu_model
= object_class_get_name(oc
);
2042 char *cpu_name
= object_get_canonical_path_component(OBJECT(cpu
));
2043 len
= snprintf((char *)gdb_ctx
->mem_buf
, sizeof(gdb_ctx
->str_buf
) / 2,
2044 "%s %s [%s]", cpu_model
, cpu_name
,
2045 cpu
->halted
? "halted " : "running");
2048 /* memtohex() doubles the required space */
2049 len
= snprintf((char *)gdb_ctx
->mem_buf
, sizeof(gdb_ctx
->str_buf
) / 2,
2050 "CPU#%d [%s]", cpu
->cpu_index
,
2051 cpu
->halted
? "halted " : "running");
2053 trace_gdbstub_op_extra_info((char *)gdb_ctx
->mem_buf
);
2054 memtohex(gdb_ctx
->str_buf
, gdb_ctx
->mem_buf
, len
);
2055 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
2058 #ifdef CONFIG_USER_ONLY
2059 static void handle_query_offsets(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2063 ts
= gdb_ctx
->s
->c_cpu
->opaque
;
2064 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
),
2065 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
2066 ";Bss=" TARGET_ABI_FMT_lx
,
2067 ts
->info
->code_offset
,
2068 ts
->info
->data_offset
,
2069 ts
->info
->data_offset
);
2070 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
2073 static void handle_query_rcmd(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2077 if (!gdb_ctx
->num_params
) {
2078 put_packet(gdb_ctx
->s
, "E22");
2082 len
= strlen(gdb_ctx
->params
[0].data
);
2084 put_packet(gdb_ctx
->s
, "E01");
2089 hextomem(gdb_ctx
->mem_buf
, gdb_ctx
->params
[0].data
, len
);
2090 gdb_ctx
->mem_buf
[len
++] = 0;
2091 qemu_chr_be_write(gdb_ctx
->s
->mon_chr
, gdb_ctx
->mem_buf
, len
);
2092 put_packet(gdb_ctx
->s
, "OK");
2097 static void handle_query_supported(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2101 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "PacketSize=%x",
2103 cc
= CPU_GET_CLASS(first_cpu
);
2104 if (cc
->gdb_core_xml_file
) {
2105 pstrcat(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
),
2106 ";qXfer:features:read+");
2109 if (gdb_ctx
->num_params
&&
2110 strstr(gdb_ctx
->params
[0].data
, "multiprocess+")) {
2111 gdb_ctx
->s
->multiprocess
= true;
2114 pstrcat(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), ";multiprocess+");
2115 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
2118 static void handle_query_xfer_features(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2120 GDBProcess
*process
;
2122 unsigned long len
, total_len
, addr
;
2126 if (gdb_ctx
->num_params
< 3) {
2127 put_packet(gdb_ctx
->s
, "E22");
2131 process
= gdb_get_cpu_process(gdb_ctx
->s
, gdb_ctx
->s
->g_cpu
);
2132 cc
= CPU_GET_CLASS(gdb_ctx
->s
->g_cpu
);
2133 if (!cc
->gdb_core_xml_file
) {
2134 put_packet(gdb_ctx
->s
, "");
2139 p
= gdb_ctx
->params
[0].data
;
2140 xml
= get_feature_xml(gdb_ctx
->s
, p
, &p
, process
);
2142 put_packet(gdb_ctx
->s
, "E00");
2146 addr
= gdb_ctx
->params
[1].val_ul
;
2147 len
= gdb_ctx
->params
[2].val_ul
;
2148 total_len
= strlen(xml
);
2149 if (addr
> total_len
) {
2150 put_packet(gdb_ctx
->s
, "E00");
2154 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
2155 len
= (MAX_PACKET_LENGTH
- 5) / 2;
2158 if (len
< total_len
- addr
) {
2159 gdb_ctx
->str_buf
[0] = 'm';
2160 len
= memtox(gdb_ctx
->str_buf
+ 1, xml
+ addr
, len
);
2162 gdb_ctx
->str_buf
[0] = 'l';
2163 len
= memtox(gdb_ctx
->str_buf
+ 1, xml
+ addr
, total_len
- addr
);
2166 put_packet_binary(gdb_ctx
->s
, gdb_ctx
->str_buf
, len
+ 1, true);
2169 static void handle_query_attached(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2171 put_packet(gdb_ctx
->s
, GDB_ATTACHED
);
2174 static void handle_query_qemu_supported(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2176 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "sstepbits;sstep");
2177 #ifndef CONFIG_USER_ONLY
2178 pstrcat(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), ";PhyMemMode");
2180 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
2183 #ifndef CONFIG_USER_ONLY
2184 static void handle_query_qemu_phy_mem_mode(GdbCmdContext
*gdb_ctx
,
2187 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "%d", phy_memory_mode
);
2188 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
2191 static void handle_set_qemu_phy_mem_mode(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2193 if (!gdb_ctx
->num_params
) {
2194 put_packet(gdb_ctx
->s
, "E22");
2198 if (!gdb_ctx
->params
[0].val_ul
) {
2199 phy_memory_mode
= 0;
2201 phy_memory_mode
= 1;
2203 put_packet(gdb_ctx
->s
, "OK");
2207 static GdbCmdParseEntry gdb_gen_query_set_common_table
[] = {
2208 /* Order is important if has same prefix */
2210 .handler
= handle_query_qemu_sstepbits
,
2211 .cmd
= "qemu.sstepbits",
2214 .handler
= handle_query_qemu_sstep
,
2215 .cmd
= "qemu.sstep",
2218 .handler
= handle_set_qemu_sstep
,
2219 .cmd
= "qemu.sstep=",
2220 .cmd_startswith
= 1,
2225 static GdbCmdParseEntry gdb_gen_query_table
[] = {
2227 .handler
= handle_query_curr_tid
,
2231 .handler
= handle_query_threads
,
2232 .cmd
= "sThreadInfo",
2235 .handler
= handle_query_first_threads
,
2236 .cmd
= "fThreadInfo",
2239 .handler
= handle_query_thread_extra
,
2240 .cmd
= "ThreadExtraInfo,",
2241 .cmd_startswith
= 1,
2244 #ifdef CONFIG_USER_ONLY
2246 .handler
= handle_query_offsets
,
2251 .handler
= handle_query_rcmd
,
2253 .cmd_startswith
= 1,
2258 .handler
= handle_query_supported
,
2259 .cmd
= "Supported:",
2260 .cmd_startswith
= 1,
2264 .handler
= handle_query_supported
,
2269 .handler
= handle_query_xfer_features
,
2270 .cmd
= "Xfer:features:read:",
2271 .cmd_startswith
= 1,
2275 .handler
= handle_query_attached
,
2280 .handler
= handle_query_attached
,
2284 .handler
= handle_query_qemu_supported
,
2285 .cmd
= "qemu.Supported",
2287 #ifndef CONFIG_USER_ONLY
2289 .handler
= handle_query_qemu_phy_mem_mode
,
2290 .cmd
= "qemu.PhyMemMode",
2295 static GdbCmdParseEntry gdb_gen_set_table
[] = {
2296 /* Order is important if has same prefix */
2298 .handler
= handle_set_qemu_sstep
,
2299 .cmd
= "qemu.sstep:",
2300 .cmd_startswith
= 1,
2303 #ifndef CONFIG_USER_ONLY
2305 .handler
= handle_set_qemu_phy_mem_mode
,
2306 .cmd
= "qemu.PhyMemMode:",
2307 .cmd_startswith
= 1,
2313 static void handle_gen_query(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2315 if (!gdb_ctx
->num_params
) {
2319 if (!process_string_cmd(gdb_ctx
->s
, NULL
, gdb_ctx
->params
[0].data
,
2320 gdb_gen_query_set_common_table
,
2321 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
2325 if (process_string_cmd(gdb_ctx
->s
, NULL
, gdb_ctx
->params
[0].data
,
2326 gdb_gen_query_table
,
2327 ARRAY_SIZE(gdb_gen_query_table
))) {
2328 put_packet(gdb_ctx
->s
, "");
2332 static void handle_gen_set(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2334 if (!gdb_ctx
->num_params
) {
2338 if (!process_string_cmd(gdb_ctx
->s
, NULL
, gdb_ctx
->params
[0].data
,
2339 gdb_gen_query_set_common_table
,
2340 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
2344 if (process_string_cmd(gdb_ctx
->s
, NULL
, gdb_ctx
->params
[0].data
,
2346 ARRAY_SIZE(gdb_gen_set_table
))) {
2347 put_packet(gdb_ctx
->s
, "");
2351 static void handle_target_halt(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2355 gdb_fmt_thread_id(gdb_ctx
->s
, gdb_ctx
->s
->c_cpu
, thread_id
,
2357 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "T%02xthread:%s;",
2358 GDB_SIGNAL_TRAP
, thread_id
);
2359 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
2361 * Remove all the breakpoints when this query is issued,
2362 * because gdb is doing an initial connect and the state
2363 * should be cleaned up.
2365 gdb_breakpoint_remove_all();
2368 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
2370 const GdbCmdParseEntry
*cmd_parser
= NULL
;
2372 trace_gdbstub_io_command(line_buf
);
2374 switch (line_buf
[0]) {
2376 put_packet(s
, "OK");
2380 static const GdbCmdParseEntry target_halted_cmd_desc
= {
2381 .handler
= handle_target_halt
,
2385 cmd_parser
= &target_halted_cmd_desc
;
2390 static const GdbCmdParseEntry continue_cmd_desc
= {
2391 .handler
= handle_continue
,
2393 .cmd_startswith
= 1,
2396 cmd_parser
= &continue_cmd_desc
;
2401 static const GdbCmdParseEntry cont_with_sig_cmd_desc
= {
2402 .handler
= handle_cont_with_sig
,
2404 .cmd_startswith
= 1,
2407 cmd_parser
= &cont_with_sig_cmd_desc
;
2412 static const GdbCmdParseEntry v_cmd_desc
= {
2413 .handler
= handle_v_commands
,
2415 .cmd_startswith
= 1,
2418 cmd_parser
= &v_cmd_desc
;
2422 /* Kill the target */
2423 error_report("QEMU: Terminated via GDBstub");
2427 static const GdbCmdParseEntry detach_cmd_desc
= {
2428 .handler
= handle_detach
,
2430 .cmd_startswith
= 1,
2433 cmd_parser
= &detach_cmd_desc
;
2438 static const GdbCmdParseEntry step_cmd_desc
= {
2439 .handler
= handle_step
,
2441 .cmd_startswith
= 1,
2444 cmd_parser
= &step_cmd_desc
;
2449 static const GdbCmdParseEntry file_io_cmd_desc
= {
2450 .handler
= handle_file_io
,
2452 .cmd_startswith
= 1,
2455 cmd_parser
= &file_io_cmd_desc
;
2460 static const GdbCmdParseEntry read_all_regs_cmd_desc
= {
2461 .handler
= handle_read_all_regs
,
2465 cmd_parser
= &read_all_regs_cmd_desc
;
2470 static const GdbCmdParseEntry write_all_regs_cmd_desc
= {
2471 .handler
= handle_write_all_regs
,
2473 .cmd_startswith
= 1,
2476 cmd_parser
= &write_all_regs_cmd_desc
;
2481 static const GdbCmdParseEntry read_mem_cmd_desc
= {
2482 .handler
= handle_read_mem
,
2484 .cmd_startswith
= 1,
2487 cmd_parser
= &read_mem_cmd_desc
;
2492 static const GdbCmdParseEntry write_mem_cmd_desc
= {
2493 .handler
= handle_write_mem
,
2495 .cmd_startswith
= 1,
2498 cmd_parser
= &write_mem_cmd_desc
;
2503 static const GdbCmdParseEntry get_reg_cmd_desc
= {
2504 .handler
= handle_get_reg
,
2506 .cmd_startswith
= 1,
2509 cmd_parser
= &get_reg_cmd_desc
;
2514 static const GdbCmdParseEntry set_reg_cmd_desc
= {
2515 .handler
= handle_set_reg
,
2517 .cmd_startswith
= 1,
2520 cmd_parser
= &set_reg_cmd_desc
;
2525 static const GdbCmdParseEntry insert_bp_cmd_desc
= {
2526 .handler
= handle_insert_bp
,
2528 .cmd_startswith
= 1,
2531 cmd_parser
= &insert_bp_cmd_desc
;
2536 static const GdbCmdParseEntry remove_bp_cmd_desc
= {
2537 .handler
= handle_remove_bp
,
2539 .cmd_startswith
= 1,
2542 cmd_parser
= &remove_bp_cmd_desc
;
2547 static const GdbCmdParseEntry set_thread_cmd_desc
= {
2548 .handler
= handle_set_thread
,
2550 .cmd_startswith
= 1,
2553 cmd_parser
= &set_thread_cmd_desc
;
2558 static const GdbCmdParseEntry thread_alive_cmd_desc
= {
2559 .handler
= handle_thread_alive
,
2561 .cmd_startswith
= 1,
2564 cmd_parser
= &thread_alive_cmd_desc
;
2569 static const GdbCmdParseEntry gen_query_cmd_desc
= {
2570 .handler
= handle_gen_query
,
2572 .cmd_startswith
= 1,
2575 cmd_parser
= &gen_query_cmd_desc
;
2580 static const GdbCmdParseEntry gen_set_cmd_desc
= {
2581 .handler
= handle_gen_set
,
2583 .cmd_startswith
= 1,
2586 cmd_parser
= &gen_set_cmd_desc
;
2590 /* put empty packet */
2596 run_cmd_parser(s
, line_buf
, cmd_parser
);
2602 void gdb_set_stop_cpu(CPUState
*cpu
)
2604 GDBProcess
*p
= gdb_get_cpu_process(gdbserver_state
, cpu
);
2608 * Having a stop CPU corresponding to a process that is not attached
2609 * confuses GDB. So we ignore the request.
2614 gdbserver_state
->c_cpu
= cpu
;
2615 gdbserver_state
->g_cpu
= cpu
;
2618 #ifndef CONFIG_USER_ONLY
2619 static void gdb_vm_state_change(void *opaque
, int running
, RunState state
)
2621 GDBState
*s
= gdbserver_state
;
2622 CPUState
*cpu
= s
->c_cpu
;
2628 if (running
|| s
->state
== RS_INACTIVE
) {
2631 /* Is there a GDB syscall waiting to be sent? */
2632 if (s
->current_syscall_cb
) {
2633 put_packet(s
, s
->syscall_buf
);
2638 /* No process attached */
2642 gdb_fmt_thread_id(s
, cpu
, thread_id
, sizeof(thread_id
));
2645 case RUN_STATE_DEBUG
:
2646 if (cpu
->watchpoint_hit
) {
2647 switch (cpu
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
2658 trace_gdbstub_hit_watchpoint(type
, cpu_gdb_index(cpu
),
2659 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
2660 snprintf(buf
, sizeof(buf
),
2661 "T%02xthread:%s;%swatch:" TARGET_FMT_lx
";",
2662 GDB_SIGNAL_TRAP
, thread_id
, type
,
2663 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
2664 cpu
->watchpoint_hit
= NULL
;
2667 trace_gdbstub_hit_break();
2670 ret
= GDB_SIGNAL_TRAP
;
2672 case RUN_STATE_PAUSED
:
2673 trace_gdbstub_hit_paused();
2674 ret
= GDB_SIGNAL_INT
;
2676 case RUN_STATE_SHUTDOWN
:
2677 trace_gdbstub_hit_shutdown();
2678 ret
= GDB_SIGNAL_QUIT
;
2680 case RUN_STATE_IO_ERROR
:
2681 trace_gdbstub_hit_io_error();
2682 ret
= GDB_SIGNAL_IO
;
2684 case RUN_STATE_WATCHDOG
:
2685 trace_gdbstub_hit_watchdog();
2686 ret
= GDB_SIGNAL_ALRM
;
2688 case RUN_STATE_INTERNAL_ERROR
:
2689 trace_gdbstub_hit_internal_error();
2690 ret
= GDB_SIGNAL_ABRT
;
2692 case RUN_STATE_SAVE_VM
:
2693 case RUN_STATE_RESTORE_VM
:
2695 case RUN_STATE_FINISH_MIGRATE
:
2696 ret
= GDB_SIGNAL_XCPU
;
2699 trace_gdbstub_hit_unknown(state
);
2700 ret
= GDB_SIGNAL_UNKNOWN
;
2703 gdb_set_stop_cpu(cpu
);
2704 snprintf(buf
, sizeof(buf
), "T%02xthread:%s;", ret
, thread_id
);
2709 /* disable single step if it was enabled */
2710 cpu_single_step(cpu
, 0);
2714 /* Send a gdb syscall request.
2715 This accepts limited printf-style format specifiers, specifically:
2716 %x - target_ulong argument printed in hex.
2717 %lx - 64-bit argument printed in hex.
2718 %s - string pointer (target_ulong) and length (int) pair. */
2719 void gdb_do_syscallv(gdb_syscall_complete_cb cb
, const char *fmt
, va_list va
)
2727 s
= gdbserver_state
;
2730 s
->current_syscall_cb
= cb
;
2731 #ifndef CONFIG_USER_ONLY
2732 vm_stop(RUN_STATE_DEBUG
);
2735 p_end
= &s
->syscall_buf
[sizeof(s
->syscall_buf
)];
2742 addr
= va_arg(va
, target_ulong
);
2743 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
, addr
);
2746 if (*(fmt
++) != 'x')
2748 i64
= va_arg(va
, uint64_t);
2749 p
+= snprintf(p
, p_end
- p
, "%" PRIx64
, i64
);
2752 addr
= va_arg(va
, target_ulong
);
2753 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
"/%x",
2754 addr
, va_arg(va
, int));
2758 error_report("gdbstub: Bad syscall format string '%s'",
2767 #ifdef CONFIG_USER_ONLY
2768 put_packet(s
, s
->syscall_buf
);
2769 /* Return control to gdb for it to process the syscall request.
2770 * Since the protocol requires that gdb hands control back to us
2771 * using a "here are the results" F packet, we don't need to check
2772 * gdb_handlesig's return value (which is the signal to deliver if
2773 * execution was resumed via a continue packet).
2775 gdb_handlesig(s
->c_cpu
, 0);
2777 /* In this case wait to send the syscall packet until notification that
2778 the CPU has stopped. This must be done because if the packet is sent
2779 now the reply from the syscall request could be received while the CPU
2780 is still in the running state, which can cause packets to be dropped
2781 and state transition 'T' packets to be sent while the syscall is still
2783 qemu_cpu_kick(s
->c_cpu
);
2787 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
2792 gdb_do_syscallv(cb
, fmt
, va
);
2796 static void gdb_read_byte(GDBState
*s
, uint8_t ch
)
2800 #ifndef CONFIG_USER_ONLY
2801 if (s
->last_packet_len
) {
2802 /* Waiting for a response to the last packet. If we see the start
2803 of a new command then abandon the previous response. */
2805 trace_gdbstub_err_got_nack();
2806 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
2807 } else if (ch
== '+') {
2808 trace_gdbstub_io_got_ack();
2810 trace_gdbstub_io_got_unexpected(ch
);
2813 if (ch
== '+' || ch
== '$')
2814 s
->last_packet_len
= 0;
2818 if (runstate_is_running()) {
2819 /* when the CPU is running, we cannot do anything except stop
2820 it when receiving a char */
2821 vm_stop(RUN_STATE_PAUSED
);
2828 /* start of command packet */
2829 s
->line_buf_index
= 0;
2831 s
->state
= RS_GETLINE
;
2833 trace_gdbstub_err_garbage(ch
);
2838 /* start escape sequence */
2839 s
->state
= RS_GETLINE_ESC
;
2841 } else if (ch
== '*') {
2842 /* start run length encoding sequence */
2843 s
->state
= RS_GETLINE_RLE
;
2845 } else if (ch
== '#') {
2846 /* end of command, start of checksum*/
2847 s
->state
= RS_CHKSUM1
;
2848 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2849 trace_gdbstub_err_overrun();
2852 /* unescaped command character */
2853 s
->line_buf
[s
->line_buf_index
++] = ch
;
2857 case RS_GETLINE_ESC
:
2859 /* unexpected end of command in escape sequence */
2860 s
->state
= RS_CHKSUM1
;
2861 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2862 /* command buffer overrun */
2863 trace_gdbstub_err_overrun();
2866 /* parse escaped character and leave escape state */
2867 s
->line_buf
[s
->line_buf_index
++] = ch
^ 0x20;
2869 s
->state
= RS_GETLINE
;
2872 case RS_GETLINE_RLE
:
2874 * Run-length encoding is explained in "Debugging with GDB /
2875 * Appendix E GDB Remote Serial Protocol / Overview".
2877 if (ch
< ' ' || ch
== '#' || ch
== '$' || ch
> 126) {
2878 /* invalid RLE count encoding */
2879 trace_gdbstub_err_invalid_repeat(ch
);
2880 s
->state
= RS_GETLINE
;
2882 /* decode repeat length */
2883 int repeat
= ch
- ' ' + 3;
2884 if (s
->line_buf_index
+ repeat
>= sizeof(s
->line_buf
) - 1) {
2885 /* that many repeats would overrun the command buffer */
2886 trace_gdbstub_err_overrun();
2888 } else if (s
->line_buf_index
< 1) {
2889 /* got a repeat but we have nothing to repeat */
2890 trace_gdbstub_err_invalid_rle();
2891 s
->state
= RS_GETLINE
;
2893 /* repeat the last character */
2894 memset(s
->line_buf
+ s
->line_buf_index
,
2895 s
->line_buf
[s
->line_buf_index
- 1], repeat
);
2896 s
->line_buf_index
+= repeat
;
2898 s
->state
= RS_GETLINE
;
2903 /* get high hex digit of checksum */
2904 if (!isxdigit(ch
)) {
2905 trace_gdbstub_err_checksum_invalid(ch
);
2906 s
->state
= RS_GETLINE
;
2909 s
->line_buf
[s
->line_buf_index
] = '\0';
2910 s
->line_csum
= fromhex(ch
) << 4;
2911 s
->state
= RS_CHKSUM2
;
2914 /* get low hex digit of checksum */
2915 if (!isxdigit(ch
)) {
2916 trace_gdbstub_err_checksum_invalid(ch
);
2917 s
->state
= RS_GETLINE
;
2920 s
->line_csum
|= fromhex(ch
);
2922 if (s
->line_csum
!= (s
->line_sum
& 0xff)) {
2923 trace_gdbstub_err_checksum_incorrect(s
->line_sum
, s
->line_csum
);
2924 /* send NAK reply */
2926 put_buffer(s
, &reply
, 1);
2929 /* send ACK reply */
2931 put_buffer(s
, &reply
, 1);
2932 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
2941 /* Tell the remote gdb that the process has exited. */
2942 void gdb_exit(CPUArchState
*env
, int code
)
2947 s
= gdbserver_state
;
2951 #ifdef CONFIG_USER_ONLY
2952 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2957 trace_gdbstub_op_exiting((uint8_t)code
);
2959 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
2962 #ifndef CONFIG_USER_ONLY
2963 qemu_chr_fe_deinit(&s
->chr
, true);
2968 * Create the process that will contain all the "orphan" CPUs (that are not
2969 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2970 * be attachable and thus will be invisible to the user.
2972 static void create_default_process(GDBState
*s
)
2974 GDBProcess
*process
;
2977 if (s
->process_num
) {
2978 max_pid
= s
->processes
[s
->process_num
- 1].pid
;
2981 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2982 process
= &s
->processes
[s
->process_num
- 1];
2984 /* We need an available PID slot for this process */
2985 assert(max_pid
< UINT32_MAX
);
2987 process
->pid
= max_pid
+ 1;
2988 process
->attached
= false;
2989 process
->target_xml
[0] = '\0';
2992 #ifdef CONFIG_USER_ONLY
2994 gdb_handlesig(CPUState
*cpu
, int sig
)
3000 s
= gdbserver_state
;
3001 if (gdbserver_fd
< 0 || s
->fd
< 0) {
3005 /* disable single step if it was enabled */
3006 cpu_single_step(cpu
, 0);
3010 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb(sig
));
3013 /* put_packet() might have detected that the peer terminated the
3021 s
->running_state
= 0;
3022 while (s
->running_state
== 0) {
3023 n
= read(s
->fd
, buf
, 256);
3027 for (i
= 0; i
< n
; i
++) {
3028 gdb_read_byte(s
, buf
[i
]);
3031 /* XXX: Connection closed. Should probably wait for another
3032 connection before continuing. */
3045 /* Tell the remote gdb that the process has exited due to SIG. */
3046 void gdb_signalled(CPUArchState
*env
, int sig
)
3051 s
= gdbserver_state
;
3052 if (gdbserver_fd
< 0 || s
->fd
< 0) {
3056 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb(sig
));
3060 static bool gdb_accept(void)
3063 struct sockaddr_in sockaddr
;
3068 len
= sizeof(sockaddr
);
3069 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
3070 if (fd
< 0 && errno
!= EINTR
) {
3073 } else if (fd
>= 0) {
3074 qemu_set_cloexec(fd
);
3079 /* set short latency */
3080 if (socket_set_nodelay(fd
)) {
3081 perror("setsockopt");
3086 s
= g_malloc0(sizeof(GDBState
));
3087 create_default_process(s
);
3088 s
->processes
[0].attached
= true;
3089 s
->c_cpu
= gdb_first_attached_cpu(s
);
3090 s
->g_cpu
= s
->c_cpu
;
3092 gdb_has_xml
= false;
3094 gdbserver_state
= s
;
3098 static int gdbserver_open(int port
)
3100 struct sockaddr_in sockaddr
;
3103 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3108 qemu_set_cloexec(fd
);
3110 socket_set_fast_reuse(fd
);
3112 sockaddr
.sin_family
= AF_INET
;
3113 sockaddr
.sin_port
= htons(port
);
3114 sockaddr
.sin_addr
.s_addr
= 0;
3115 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
3121 ret
= listen(fd
, 1);
3130 int gdbserver_start(int port
)
3132 gdbserver_fd
= gdbserver_open(port
);
3133 if (gdbserver_fd
< 0)
3135 /* accept connections */
3136 if (!gdb_accept()) {
3137 close(gdbserver_fd
);
3144 /* Disable gdb stub for child processes. */
3145 void gdbserver_fork(CPUState
*cpu
)
3147 GDBState
*s
= gdbserver_state
;
3149 if (gdbserver_fd
< 0 || s
->fd
< 0) {
3154 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
3155 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
3158 static int gdb_chr_can_receive(void *opaque
)
3160 /* We can handle an arbitrarily large amount of data.
3161 Pick the maximum packet size, which is as good as anything. */
3162 return MAX_PACKET_LENGTH
;
3165 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
3169 for (i
= 0; i
< size
; i
++) {
3170 gdb_read_byte(gdbserver_state
, buf
[i
]);
3174 static void gdb_chr_event(void *opaque
, QEMUChrEvent event
)
3177 GDBState
*s
= (GDBState
*) opaque
;
3180 case CHR_EVENT_OPENED
:
3181 /* Start with first process attached, others detached */
3182 for (i
= 0; i
< s
->process_num
; i
++) {
3183 s
->processes
[i
].attached
= !i
;
3186 s
->c_cpu
= gdb_first_attached_cpu(s
);
3187 s
->g_cpu
= s
->c_cpu
;
3189 vm_stop(RUN_STATE_PAUSED
);
3190 gdb_has_xml
= false;
3197 static void gdb_monitor_output(GDBState
*s
, const char *msg
, int len
)
3199 char buf
[MAX_PACKET_LENGTH
];
3202 if (len
> (MAX_PACKET_LENGTH
/2) - 1)
3203 len
= (MAX_PACKET_LENGTH
/2) - 1;
3204 memtohex(buf
+ 1, (uint8_t *)msg
, len
);
3208 static int gdb_monitor_write(Chardev
*chr
, const uint8_t *buf
, int len
)
3210 const char *p
= (const char *)buf
;
3213 max_sz
= (sizeof(gdbserver_state
->last_packet
) - 2) / 2;
3215 if (len
<= max_sz
) {
3216 gdb_monitor_output(gdbserver_state
, p
, len
);
3219 gdb_monitor_output(gdbserver_state
, p
, max_sz
);
3227 static void gdb_sigterm_handler(int signal
)
3229 if (runstate_is_running()) {
3230 vm_stop(RUN_STATE_PAUSED
);
3235 static void gdb_monitor_open(Chardev
*chr
, ChardevBackend
*backend
,
3236 bool *be_opened
, Error
**errp
)
3241 static void char_gdb_class_init(ObjectClass
*oc
, void *data
)
3243 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
3245 cc
->internal
= true;
3246 cc
->open
= gdb_monitor_open
;
3247 cc
->chr_write
= gdb_monitor_write
;
3250 #define TYPE_CHARDEV_GDB "chardev-gdb"
3252 static const TypeInfo char_gdb_type_info
= {
3253 .name
= TYPE_CHARDEV_GDB
,
3254 .parent
= TYPE_CHARDEV
,
3255 .class_init
= char_gdb_class_init
,
3258 static int find_cpu_clusters(Object
*child
, void *opaque
)
3260 if (object_dynamic_cast(child
, TYPE_CPU_CLUSTER
)) {
3261 GDBState
*s
= (GDBState
*) opaque
;
3262 CPUClusterState
*cluster
= CPU_CLUSTER(child
);
3263 GDBProcess
*process
;
3265 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
3267 process
= &s
->processes
[s
->process_num
- 1];
3270 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
3271 * runtime, we enforce here that the machine does not use a cluster ID
3272 * that would lead to PID 0.
3274 assert(cluster
->cluster_id
!= UINT32_MAX
);
3275 process
->pid
= cluster
->cluster_id
+ 1;
3276 process
->attached
= false;
3277 process
->target_xml
[0] = '\0';
3282 return object_child_foreach(child
, find_cpu_clusters
, opaque
);
3285 static int pid_order(const void *a
, const void *b
)
3287 GDBProcess
*pa
= (GDBProcess
*) a
;
3288 GDBProcess
*pb
= (GDBProcess
*) b
;
3290 if (pa
->pid
< pb
->pid
) {
3292 } else if (pa
->pid
> pb
->pid
) {
3299 static void create_processes(GDBState
*s
)
3301 object_child_foreach(object_get_root(), find_cpu_clusters
, s
);
3305 qsort(s
->processes
, s
->process_num
, sizeof(s
->processes
[0]), pid_order
);
3308 create_default_process(s
);
3311 static void cleanup_processes(GDBState
*s
)
3313 g_free(s
->processes
);
3315 s
->processes
= NULL
;
3318 int gdbserver_start(const char *device
)
3320 trace_gdbstub_op_start(device
);
3323 char gdbstub_device_name
[128];
3324 Chardev
*chr
= NULL
;
3328 error_report("gdbstub: meaningless to attach gdb to a "
3329 "machine without any CPU.");
3335 if (strcmp(device
, "none") != 0) {
3336 if (strstart(device
, "tcp:", NULL
)) {
3337 /* enforce required TCP attributes */
3338 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
3339 "%s,nowait,nodelay,server", device
);
3340 device
= gdbstub_device_name
;
3343 else if (strcmp(device
, "stdio") == 0) {
3344 struct sigaction act
;
3346 memset(&act
, 0, sizeof(act
));
3347 act
.sa_handler
= gdb_sigterm_handler
;
3348 sigaction(SIGINT
, &act
, NULL
);
3352 * FIXME: it's a bit weird to allow using a mux chardev here
3353 * and implicitly setup a monitor. We may want to break this.
3355 chr
= qemu_chr_new_noreplay("gdb", device
, true, NULL
);
3360 s
= gdbserver_state
;
3362 s
= g_malloc0(sizeof(GDBState
));
3363 gdbserver_state
= s
;
3365 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
3367 /* Initialize a monitor terminal for gdb */
3368 mon_chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_GDB
,
3369 NULL
, NULL
, &error_abort
);
3370 monitor_init_hmp(mon_chr
, false);
3372 qemu_chr_fe_deinit(&s
->chr
, true);
3373 mon_chr
= s
->mon_chr
;
3374 cleanup_processes(s
);
3375 memset(s
, 0, sizeof(GDBState
));
3376 s
->mon_chr
= mon_chr
;
3379 create_processes(s
);
3382 qemu_chr_fe_init(&s
->chr
, chr
, &error_abort
);
3383 qemu_chr_fe_set_handlers(&s
->chr
, gdb_chr_can_receive
, gdb_chr_receive
,
3384 gdb_chr_event
, NULL
, s
, NULL
, true);
3386 s
->state
= chr
? RS_IDLE
: RS_INACTIVE
;
3387 s
->mon_chr
= mon_chr
;
3388 s
->current_syscall_cb
= NULL
;
3393 void gdbserver_cleanup(void)
3395 if (gdbserver_state
) {
3396 put_packet(gdbserver_state
, "W00");
3400 static void register_types(void)
3402 type_register_static(&char_gdb_type_info
);
3405 type_init(register_types
);