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 "hw/semihosting/semihost.h"
52 #include "exec/exec-all.h"
54 #ifdef CONFIG_USER_ONLY
55 #define GDB_ATTACHED "0"
57 #define GDB_ATTACHED "1"
60 #ifndef CONFIG_USER_ONLY
61 static int phy_memory_mode
;
64 static inline int target_memory_rw_debug(CPUState
*cpu
, target_ulong addr
,
65 uint8_t *buf
, int len
, bool is_write
)
69 #ifndef CONFIG_USER_ONLY
70 if (phy_memory_mode
) {
72 cpu_physical_memory_write(addr
, buf
, len
);
74 cpu_physical_memory_read(addr
, buf
, len
);
80 cc
= CPU_GET_CLASS(cpu
);
81 if (cc
->memory_rw_debug
) {
82 return cc
->memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
84 return cpu_memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
87 /* Return the GDB index for a given vCPU state.
89 * For user mode this is simply the thread id. In system mode GDB
90 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
92 static inline int cpu_gdb_index(CPUState
*cpu
)
94 #if defined(CONFIG_USER_ONLY)
95 TaskState
*ts
= (TaskState
*) cpu
->opaque
;
98 return cpu
->cpu_index
+ 1;
108 GDB_SIGNAL_ALRM
= 14,
110 GDB_SIGNAL_XCPU
= 24,
111 GDB_SIGNAL_UNKNOWN
= 143
114 #ifdef CONFIG_USER_ONLY
116 /* Map target signal numbers to GDB protocol signal numbers and vice
117 * versa. For user emulation's currently supported systems, we can
118 * assume most signals are defined.
121 static int gdb_signal_table
[] = {
281 /* In system mode we only need SIGINT and SIGTRAP; other signals
282 are not yet supported. */
289 static int gdb_signal_table
[] = {
299 #ifdef CONFIG_USER_ONLY
300 static int target_signal_to_gdb (int sig
)
303 for (i
= 0; i
< ARRAY_SIZE (gdb_signal_table
); i
++)
304 if (gdb_signal_table
[i
] == sig
)
306 return GDB_SIGNAL_UNKNOWN
;
310 static int gdb_signal_to_target (int sig
)
312 if (sig
< ARRAY_SIZE (gdb_signal_table
))
313 return gdb_signal_table
[sig
];
318 typedef struct GDBRegisterState
{
324 struct GDBRegisterState
*next
;
327 typedef struct GDBProcess
{
331 char target_xml
[1024];
343 typedef struct GDBState
{
344 CPUState
*c_cpu
; /* current CPU for step/continue ops */
345 CPUState
*g_cpu
; /* current CPU for other ops */
346 CPUState
*query_cpu
; /* for q{f|s}ThreadInfo */
347 enum RSState state
; /* parsing state */
348 char line_buf
[MAX_PACKET_LENGTH
];
350 int line_sum
; /* running checksum */
351 int line_csum
; /* checksum at the end of the packet */
352 uint8_t last_packet
[MAX_PACKET_LENGTH
+ 4];
355 #ifdef CONFIG_USER_ONLY
363 GDBProcess
*processes
;
365 char syscall_buf
[256];
366 gdb_syscall_complete_cb current_syscall_cb
;
369 /* By default use no IRQs and no timers while single stepping so as to
370 * make single stepping like an ICE HW step.
372 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
374 static GDBState
*gdbserver_state
;
378 #ifdef CONFIG_USER_ONLY
379 /* XXX: This is not thread safe. Do we care? */
380 static int gdbserver_fd
= -1;
382 static int get_char(GDBState
*s
)
388 ret
= qemu_recv(s
->fd
, &ch
, 1, 0);
390 if (errno
== ECONNRESET
)
394 } else if (ret
== 0) {
412 /* Decide if either remote gdb syscalls or native file IO should be used. */
413 int use_gdb_syscalls(void)
415 SemihostingTarget target
= semihosting_get_target();
416 if (target
== SEMIHOSTING_TARGET_NATIVE
) {
417 /* -semihosting-config target=native */
419 } else if (target
== SEMIHOSTING_TARGET_GDB
) {
420 /* -semihosting-config target=gdb */
424 /* -semihosting-config target=auto */
425 /* On the first call check if gdb is connected and remember. */
426 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
427 gdb_syscall_mode
= (gdbserver_state
? GDB_SYS_ENABLED
430 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
433 /* Resume execution. */
434 static inline void gdb_continue(GDBState
*s
)
437 #ifdef CONFIG_USER_ONLY
438 s
->running_state
= 1;
439 trace_gdbstub_op_continue();
441 if (!runstate_needs_reset()) {
442 trace_gdbstub_op_continue();
449 * Resume execution, per CPU actions. For user-mode emulation it's
450 * equivalent to gdb_continue.
452 static int gdb_continue_partial(GDBState
*s
, char *newstates
)
456 #ifdef CONFIG_USER_ONLY
458 * This is not exactly accurate, but it's an improvement compared to the
459 * previous situation, where only one CPU would be single-stepped.
462 if (newstates
[cpu
->cpu_index
] == 's') {
463 trace_gdbstub_op_stepping(cpu
->cpu_index
);
464 cpu_single_step(cpu
, sstep_flags
);
467 s
->running_state
= 1;
471 if (!runstate_needs_reset()) {
472 if (vm_prepare_start()) {
477 switch (newstates
[cpu
->cpu_index
]) {
480 break; /* nothing to do here */
482 trace_gdbstub_op_stepping(cpu
->cpu_index
);
483 cpu_single_step(cpu
, sstep_flags
);
488 trace_gdbstub_op_continue_cpu(cpu
->cpu_index
);
499 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, true);
505 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
507 #ifdef CONFIG_USER_ONLY
511 ret
= send(s
->fd
, buf
, len
, 0);
521 /* XXX this blocks entire thread. Rewrite to use
522 * qemu_chr_fe_write and background I/O callbacks */
523 qemu_chr_fe_write_all(&s
->chr
, buf
, len
);
527 static inline int fromhex(int v
)
529 if (v
>= '0' && v
<= '9')
531 else if (v
>= 'A' && v
<= 'F')
533 else if (v
>= 'a' && v
<= 'f')
539 static inline int tohex(int v
)
547 /* writes 2*len+1 bytes in buf */
548 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
553 for(i
= 0; i
< len
; i
++) {
555 *q
++ = tohex(c
>> 4);
556 *q
++ = tohex(c
& 0xf);
561 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
565 for(i
= 0; i
< len
; i
++) {
566 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
571 static void hexdump(const char *buf
, int len
,
572 void (*trace_fn
)(size_t ofs
, char const *text
))
574 char line_buffer
[3 * 16 + 4 + 16 + 1];
577 for (i
= 0; i
< len
|| (i
& 0xF); ++i
) {
578 size_t byte_ofs
= i
& 15;
581 memset(line_buffer
, ' ', 3 * 16 + 4 + 16);
582 line_buffer
[3 * 16 + 4 + 16] = 0;
585 size_t col_group
= (i
>> 2) & 3;
586 size_t hex_col
= byte_ofs
* 3 + col_group
;
587 size_t txt_col
= 3 * 16 + 4 + byte_ofs
;
592 line_buffer
[hex_col
+ 0] = tohex((value
>> 4) & 0xF);
593 line_buffer
[hex_col
+ 1] = tohex((value
>> 0) & 0xF);
594 line_buffer
[txt_col
+ 0] = (value
>= ' ' && value
< 127)
600 trace_fn(i
& -16, line_buffer
);
604 /* return -1 if error, 0 if OK */
605 static int put_packet_binary(GDBState
*s
, const char *buf
, int len
, bool dump
)
610 if (dump
&& trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY
)) {
611 hexdump(buf
, len
, trace_gdbstub_io_binaryreply
);
620 for(i
= 0; i
< len
; i
++) {
624 *(p
++) = tohex((csum
>> 4) & 0xf);
625 *(p
++) = tohex((csum
) & 0xf);
627 s
->last_packet_len
= p
- s
->last_packet
;
628 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
630 #ifdef CONFIG_USER_ONLY
643 /* return -1 if error, 0 if OK */
644 static int put_packet(GDBState
*s
, const char *buf
)
646 trace_gdbstub_io_reply(buf
);
648 return put_packet_binary(s
, buf
, strlen(buf
), false);
651 /* Encode data using the encoding for 'x' packets. */
652 static int memtox(char *buf
, const char *mem
, int len
)
660 case '#': case '$': case '*': case '}':
672 static uint32_t gdb_get_cpu_pid(const GDBState
*s
, CPUState
*cpu
)
674 /* TODO: In user mode, we should use the task state PID */
675 if (cpu
->cluster_index
== UNASSIGNED_CLUSTER_INDEX
) {
676 /* Return the default process' PID */
677 return s
->processes
[s
->process_num
- 1].pid
;
679 return cpu
->cluster_index
+ 1;
682 static GDBProcess
*gdb_get_process(const GDBState
*s
, uint32_t pid
)
687 /* 0 means any process, we take the first one */
688 return &s
->processes
[0];
691 for (i
= 0; i
< s
->process_num
; i
++) {
692 if (s
->processes
[i
].pid
== pid
) {
693 return &s
->processes
[i
];
700 static GDBProcess
*gdb_get_cpu_process(const GDBState
*s
, CPUState
*cpu
)
702 return gdb_get_process(s
, gdb_get_cpu_pid(s
, cpu
));
705 static CPUState
*find_cpu(uint32_t thread_id
)
710 if (cpu_gdb_index(cpu
) == thread_id
) {
718 static CPUState
*get_first_cpu_in_process(const GDBState
*s
,
724 if (gdb_get_cpu_pid(s
, cpu
) == process
->pid
) {
732 static CPUState
*gdb_next_cpu_in_process(const GDBState
*s
, CPUState
*cpu
)
734 uint32_t pid
= gdb_get_cpu_pid(s
, cpu
);
738 if (gdb_get_cpu_pid(s
, cpu
) == pid
) {
748 /* Return the cpu following @cpu, while ignoring unattached processes. */
749 static CPUState
*gdb_next_attached_cpu(const GDBState
*s
, CPUState
*cpu
)
754 if (gdb_get_cpu_process(s
, cpu
)->attached
) {
764 /* Return the first attached cpu */
765 static CPUState
*gdb_first_attached_cpu(const GDBState
*s
)
767 CPUState
*cpu
= first_cpu
;
768 GDBProcess
*process
= gdb_get_cpu_process(s
, cpu
);
770 if (!process
->attached
) {
771 return gdb_next_attached_cpu(s
, cpu
);
777 static CPUState
*gdb_get_cpu(const GDBState
*s
, uint32_t pid
, uint32_t tid
)
783 /* 0 means any process/thread, we take the first attached one */
784 return gdb_first_attached_cpu(s
);
785 } else if (pid
&& !tid
) {
786 /* any thread in a specific process */
787 process
= gdb_get_process(s
, pid
);
789 if (process
== NULL
) {
793 if (!process
->attached
) {
797 return get_first_cpu_in_process(s
, process
);
799 /* a specific thread */
806 process
= gdb_get_cpu_process(s
, cpu
);
808 if (pid
&& process
->pid
!= pid
) {
812 if (!process
->attached
) {
820 static const char *get_feature_xml(const GDBState
*s
, const char *p
,
821 const char **newp
, GDBProcess
*process
)
826 CPUState
*cpu
= get_first_cpu_in_process(s
, process
);
827 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
830 while (p
[len
] && p
[len
] != ':')
835 if (strncmp(p
, "target.xml", len
) == 0) {
836 char *buf
= process
->target_xml
;
837 const size_t buf_sz
= sizeof(process
->target_xml
);
839 /* Generate the XML description for this CPU. */
844 "<?xml version=\"1.0\"?>"
845 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
847 if (cc
->gdb_arch_name
) {
848 gchar
*arch
= cc
->gdb_arch_name(cpu
);
849 pstrcat(buf
, buf_sz
, "<architecture>");
850 pstrcat(buf
, buf_sz
, arch
);
851 pstrcat(buf
, buf_sz
, "</architecture>");
854 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
855 pstrcat(buf
, buf_sz
, cc
->gdb_core_xml_file
);
856 pstrcat(buf
, buf_sz
, "\"/>");
857 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
858 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
859 pstrcat(buf
, buf_sz
, r
->xml
);
860 pstrcat(buf
, buf_sz
, "\"/>");
862 pstrcat(buf
, buf_sz
, "</target>");
866 if (cc
->gdb_get_dynamic_xml
) {
867 char *xmlname
= g_strndup(p
, len
);
868 const char *xml
= cc
->gdb_get_dynamic_xml(cpu
, xmlname
);
876 name
= xml_builtin
[i
][0];
877 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
880 return name
? xml_builtin
[i
][1] : NULL
;
883 static int gdb_read_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
885 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
886 CPUArchState
*env
= cpu
->env_ptr
;
889 if (reg
< cc
->gdb_num_core_regs
) {
890 return cc
->gdb_read_register(cpu
, mem_buf
, reg
);
893 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
894 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
895 return r
->get_reg(env
, mem_buf
, reg
- r
->base_reg
);
901 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
903 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
904 CPUArchState
*env
= cpu
->env_ptr
;
907 if (reg
< cc
->gdb_num_core_regs
) {
908 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
911 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
912 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
913 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
919 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
920 specifies the first register number and these registers are included in
921 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
922 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
925 void gdb_register_coprocessor(CPUState
*cpu
,
926 gdb_reg_cb get_reg
, gdb_reg_cb set_reg
,
927 int num_regs
, const char *xml
, int g_pos
)
930 GDBRegisterState
**p
;
934 /* Check for duplicates. */
935 if (strcmp((*p
)->xml
, xml
) == 0)
940 s
= g_new0(GDBRegisterState
, 1);
941 s
->base_reg
= cpu
->gdb_num_regs
;
942 s
->num_regs
= num_regs
;
943 s
->get_reg
= get_reg
;
944 s
->set_reg
= set_reg
;
947 /* Add to end of list. */
948 cpu
->gdb_num_regs
+= num_regs
;
951 if (g_pos
!= s
->base_reg
) {
952 error_report("Error: Bad gdb register numbering for '%s', "
953 "expected %d got %d", xml
, g_pos
, s
->base_reg
);
955 cpu
->gdb_num_g_regs
= cpu
->gdb_num_regs
;
960 #ifndef CONFIG_USER_ONLY
961 /* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
962 static inline int xlat_gdb_type(CPUState
*cpu
, int gdbtype
)
964 static const int xlat
[] = {
965 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
966 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
967 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
970 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
971 int cputype
= xlat
[gdbtype
];
973 if (cc
->gdb_stop_before_watchpoint
) {
974 cputype
|= BP_STOP_BEFORE_ACCESS
;
980 static int gdb_breakpoint_insert(int type
, target_ulong addr
, target_ulong len
)
986 return kvm_insert_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
990 case GDB_BREAKPOINT_SW
:
991 case GDB_BREAKPOINT_HW
:
993 err
= cpu_breakpoint_insert(cpu
, addr
, BP_GDB
, NULL
);
999 #ifndef CONFIG_USER_ONLY
1000 case GDB_WATCHPOINT_WRITE
:
1001 case GDB_WATCHPOINT_READ
:
1002 case GDB_WATCHPOINT_ACCESS
:
1004 err
= cpu_watchpoint_insert(cpu
, addr
, len
,
1005 xlat_gdb_type(cpu
, type
), NULL
);
1017 static int gdb_breakpoint_remove(int type
, target_ulong addr
, target_ulong len
)
1022 if (kvm_enabled()) {
1023 return kvm_remove_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1027 case GDB_BREAKPOINT_SW
:
1028 case GDB_BREAKPOINT_HW
:
1030 err
= cpu_breakpoint_remove(cpu
, addr
, BP_GDB
);
1036 #ifndef CONFIG_USER_ONLY
1037 case GDB_WATCHPOINT_WRITE
:
1038 case GDB_WATCHPOINT_READ
:
1039 case GDB_WATCHPOINT_ACCESS
:
1041 err
= cpu_watchpoint_remove(cpu
, addr
, len
,
1042 xlat_gdb_type(cpu
, type
));
1053 static inline void gdb_cpu_breakpoint_remove_all(CPUState
*cpu
)
1055 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
1056 #ifndef CONFIG_USER_ONLY
1057 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
1061 static void gdb_process_breakpoint_remove_all(const GDBState
*s
, GDBProcess
*p
)
1063 CPUState
*cpu
= get_first_cpu_in_process(s
, p
);
1066 gdb_cpu_breakpoint_remove_all(cpu
);
1067 cpu
= gdb_next_cpu_in_process(s
, cpu
);
1071 static void gdb_breakpoint_remove_all(void)
1075 if (kvm_enabled()) {
1076 kvm_remove_all_breakpoints(gdbserver_state
->c_cpu
);
1081 gdb_cpu_breakpoint_remove_all(cpu
);
1085 static void gdb_set_cpu_pc(GDBState
*s
, target_ulong pc
)
1087 CPUState
*cpu
= s
->c_cpu
;
1089 cpu_synchronize_state(cpu
);
1090 cpu_set_pc(cpu
, pc
);
1093 static char *gdb_fmt_thread_id(const GDBState
*s
, CPUState
*cpu
,
1094 char *buf
, size_t buf_size
)
1096 if (s
->multiprocess
) {
1097 snprintf(buf
, buf_size
, "p%02x.%02x",
1098 gdb_get_cpu_pid(s
, cpu
), cpu_gdb_index(cpu
));
1100 snprintf(buf
, buf_size
, "%02x", cpu_gdb_index(cpu
));
1106 typedef enum GDBThreadIdKind
{
1108 GDB_ALL_THREADS
, /* One process, all threads */
1113 static GDBThreadIdKind
read_thread_id(const char *buf
, const char **end_buf
,
1114 uint32_t *pid
, uint32_t *tid
)
1121 ret
= qemu_strtoul(buf
, &buf
, 16, &p
);
1124 return GDB_READ_THREAD_ERR
;
1133 ret
= qemu_strtoul(buf
, &buf
, 16, &t
);
1136 return GDB_READ_THREAD_ERR
;
1142 return GDB_ALL_PROCESSES
;
1150 return GDB_ALL_THREADS
;
1157 return GDB_ONE_THREAD
;
1161 * gdb_handle_vcont - Parses and handles a vCont packet.
1162 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1163 * a format error, 0 on success.
1165 static int gdb_handle_vcont(GDBState
*s
, const char *p
)
1167 int res
, signal
= 0;
1172 GDBProcess
*process
;
1174 GDBThreadIdKind kind
;
1175 #ifdef CONFIG_USER_ONLY
1176 int max_cpus
= 1; /* global variable max_cpus exists only in system mode */
1179 max_cpus
= max_cpus
<= cpu
->cpu_index
? cpu
->cpu_index
+ 1 : max_cpus
;
1182 MachineState
*ms
= MACHINE(qdev_get_machine());
1183 unsigned int max_cpus
= ms
->smp
.max_cpus
;
1185 /* uninitialised CPUs stay 0 */
1186 newstates
= g_new0(char, max_cpus
);
1188 /* mark valid CPUs with 1 */
1190 newstates
[cpu
->cpu_index
] = 1;
1194 * res keeps track of what error we are returning, with -ENOTSUP meaning
1195 * that the command is unknown or unsupported, thus returning an empty
1196 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1197 * or incorrect parameters passed.
1207 if (cur_action
== 'C' || cur_action
== 'S') {
1208 cur_action
= qemu_tolower(cur_action
);
1209 res
= qemu_strtoul(p
+ 1, &p
, 16, &tmp
);
1213 signal
= gdb_signal_to_target(tmp
);
1214 } else if (cur_action
!= 'c' && cur_action
!= 's') {
1215 /* unknown/invalid/unsupported command */
1220 if (*p
== '\0' || *p
== ';') {
1222 * No thread specifier, action is on "all threads". The
1223 * specification is unclear regarding the process to act on. We
1224 * choose all processes.
1226 kind
= GDB_ALL_PROCESSES
;
1227 } else if (*p
++ == ':') {
1228 kind
= read_thread_id(p
, &p
, &pid
, &tid
);
1235 case GDB_READ_THREAD_ERR
:
1239 case GDB_ALL_PROCESSES
:
1240 cpu
= gdb_first_attached_cpu(s
);
1242 if (newstates
[cpu
->cpu_index
] == 1) {
1243 newstates
[cpu
->cpu_index
] = cur_action
;
1246 cpu
= gdb_next_attached_cpu(s
, cpu
);
1250 case GDB_ALL_THREADS
:
1251 process
= gdb_get_process(s
, pid
);
1253 if (!process
->attached
) {
1258 cpu
= get_first_cpu_in_process(s
, process
);
1260 if (newstates
[cpu
->cpu_index
] == 1) {
1261 newstates
[cpu
->cpu_index
] = cur_action
;
1264 cpu
= gdb_next_cpu_in_process(s
, cpu
);
1268 case GDB_ONE_THREAD
:
1269 cpu
= gdb_get_cpu(s
, pid
, tid
);
1271 /* invalid CPU/thread specified */
1277 /* only use if no previous match occourred */
1278 if (newstates
[cpu
->cpu_index
] == 1) {
1279 newstates
[cpu
->cpu_index
] = cur_action
;
1285 gdb_continue_partial(s
, newstates
);
1293 typedef union GdbCmdVariant
{
1296 unsigned long val_ul
;
1297 unsigned long long val_ull
;
1299 GDBThreadIdKind kind
;
1305 static const char *cmd_next_param(const char *param
, const char delimiter
)
1307 static const char all_delimiters
[] = ",;:=";
1308 char curr_delimiters
[2] = {0};
1309 const char *delimiters
;
1311 if (delimiter
== '?') {
1312 delimiters
= all_delimiters
;
1313 } else if (delimiter
== '0') {
1314 return strchr(param
, '\0');
1315 } else if (delimiter
== '.' && *param
) {
1318 curr_delimiters
[0] = delimiter
;
1319 delimiters
= curr_delimiters
;
1322 param
+= strcspn(param
, delimiters
);
1329 static int cmd_parse_params(const char *data
, const char *schema
,
1330 GdbCmdVariant
*params
, int *num_params
)
1333 const char *curr_schema
, *curr_data
;
1341 curr_schema
= schema
;
1344 while (curr_schema
[0] && curr_schema
[1] && *curr_data
) {
1345 switch (curr_schema
[0]) {
1347 if (qemu_strtoul(curr_data
, &curr_data
, 16,
1348 ¶ms
[curr_param
].val_ul
)) {
1352 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1355 if (qemu_strtou64(curr_data
, &curr_data
, 16,
1356 (uint64_t *)¶ms
[curr_param
].val_ull
)) {
1360 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1363 params
[curr_param
].data
= curr_data
;
1365 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1368 params
[curr_param
].opcode
= *(uint8_t *)curr_data
;
1370 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1373 params
[curr_param
].thread_id
.kind
=
1374 read_thread_id(curr_data
, &curr_data
,
1375 ¶ms
[curr_param
].thread_id
.pid
,
1376 ¶ms
[curr_param
].thread_id
.tid
);
1378 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1381 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1389 *num_params
= curr_param
;
1393 typedef struct GdbCmdContext
{
1395 GdbCmdVariant
*params
;
1397 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1398 char str_buf
[MAX_PACKET_LENGTH
+ 1];
1401 typedef void (*GdbCmdHandler
)(GdbCmdContext
*gdb_ctx
, void *user_ctx
);
1404 * cmd_startswith -> cmd is compared using startswith
1407 * schema definitions:
1408 * Each schema parameter entry consists of 2 chars,
1409 * the first char represents the parameter type handling
1410 * the second char represents the delimiter for the next parameter
1412 * Currently supported schema types:
1413 * 'l' -> unsigned long (stored in .val_ul)
1414 * 'L' -> unsigned long long (stored in .val_ull)
1415 * 's' -> string (stored in .data)
1416 * 'o' -> single char (stored in .opcode)
1417 * 't' -> thread id (stored in .thread_id)
1418 * '?' -> skip according to delimiter
1420 * Currently supported delimiters:
1421 * '?' -> Stop at any delimiter (",;:=\0")
1422 * '0' -> Stop at "\0"
1423 * '.' -> Skip 1 char unless reached "\0"
1424 * Any other value is treated as the delimiter value itself
1426 typedef struct GdbCmdParseEntry
{
1427 GdbCmdHandler handler
;
1429 bool cmd_startswith
;
1433 static inline int startswith(const char *string
, const char *pattern
)
1435 return !strncmp(string
, pattern
, strlen(pattern
));
1438 static int process_string_cmd(GDBState
*s
, void *user_ctx
, const char *data
,
1439 const GdbCmdParseEntry
*cmds
, int num_cmds
)
1441 int i
, schema_len
, max_num_params
= 0;
1442 GdbCmdContext gdb_ctx
;
1448 for (i
= 0; i
< num_cmds
; i
++) {
1449 const GdbCmdParseEntry
*cmd
= &cmds
[i
];
1450 g_assert(cmd
->handler
&& cmd
->cmd
);
1452 if ((cmd
->cmd_startswith
&& !startswith(data
, cmd
->cmd
)) ||
1453 (!cmd
->cmd_startswith
&& strcmp(cmd
->cmd
, data
))) {
1458 schema_len
= strlen(cmd
->schema
);
1459 if (schema_len
% 2) {
1463 max_num_params
= schema_len
/ 2;
1467 (GdbCmdVariant
*)alloca(sizeof(*gdb_ctx
.params
) * max_num_params
);
1468 memset(gdb_ctx
.params
, 0, sizeof(*gdb_ctx
.params
) * max_num_params
);
1470 if (cmd_parse_params(&data
[strlen(cmd
->cmd
)], cmd
->schema
,
1471 gdb_ctx
.params
, &gdb_ctx
.num_params
)) {
1476 cmd
->handler(&gdb_ctx
, user_ctx
);
1483 static void run_cmd_parser(GDBState
*s
, const char *data
,
1484 const GdbCmdParseEntry
*cmd
)
1490 /* In case there was an error during the command parsing we must
1491 * send a NULL packet to indicate the command is not supported */
1492 if (process_string_cmd(s
, NULL
, data
, cmd
, 1)) {
1497 static void handle_detach(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1499 GDBProcess
*process
;
1500 GDBState
*s
= gdb_ctx
->s
;
1503 if (s
->multiprocess
) {
1504 if (!gdb_ctx
->num_params
) {
1505 put_packet(s
, "E22");
1509 pid
= gdb_ctx
->params
[0].val_ul
;
1512 process
= gdb_get_process(s
, pid
);
1513 gdb_process_breakpoint_remove_all(s
, process
);
1514 process
->attached
= false;
1516 if (pid
== gdb_get_cpu_pid(s
, s
->c_cpu
)) {
1517 s
->c_cpu
= gdb_first_attached_cpu(s
);
1520 if (pid
== gdb_get_cpu_pid(s
, s
->g_cpu
)) {
1521 s
->g_cpu
= gdb_first_attached_cpu(s
);
1525 /* No more process attached */
1526 gdb_syscall_mode
= GDB_SYS_DISABLED
;
1529 put_packet(s
, "OK");
1532 static void handle_thread_alive(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1536 if (!gdb_ctx
->num_params
) {
1537 put_packet(gdb_ctx
->s
, "E22");
1541 if (gdb_ctx
->params
[0].thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1542 put_packet(gdb_ctx
->s
, "E22");
1546 cpu
= gdb_get_cpu(gdb_ctx
->s
, gdb_ctx
->params
[0].thread_id
.pid
,
1547 gdb_ctx
->params
[0].thread_id
.tid
);
1549 put_packet(gdb_ctx
->s
, "E22");
1553 put_packet(gdb_ctx
->s
, "OK");
1556 static void handle_continue(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1558 if (gdb_ctx
->num_params
) {
1559 gdb_set_cpu_pc(gdb_ctx
->s
, gdb_ctx
->params
[0].val_ull
);
1562 gdb_ctx
->s
->signal
= 0;
1563 gdb_continue(gdb_ctx
->s
);
1566 static void handle_cont_with_sig(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1568 unsigned long signal
= 0;
1571 * Note: C sig;[addr] is currently unsupported and we simply
1572 * omit the addr parameter
1574 if (gdb_ctx
->num_params
) {
1575 signal
= gdb_ctx
->params
[0].val_ul
;
1578 gdb_ctx
->s
->signal
= gdb_signal_to_target(signal
);
1579 if (gdb_ctx
->s
->signal
== -1) {
1580 gdb_ctx
->s
->signal
= 0;
1582 gdb_continue(gdb_ctx
->s
);
1585 static void handle_set_thread(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1589 if (gdb_ctx
->num_params
!= 2) {
1590 put_packet(gdb_ctx
->s
, "E22");
1594 if (gdb_ctx
->params
[1].thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1595 put_packet(gdb_ctx
->s
, "E22");
1599 if (gdb_ctx
->params
[1].thread_id
.kind
!= GDB_ONE_THREAD
) {
1600 put_packet(gdb_ctx
->s
, "OK");
1604 cpu
= gdb_get_cpu(gdb_ctx
->s
, gdb_ctx
->params
[1].thread_id
.pid
,
1605 gdb_ctx
->params
[1].thread_id
.tid
);
1607 put_packet(gdb_ctx
->s
, "E22");
1612 * Note: This command is deprecated and modern gdb's will be using the
1613 * vCont command instead.
1615 switch (gdb_ctx
->params
[0].opcode
) {
1617 gdb_ctx
->s
->c_cpu
= cpu
;
1618 put_packet(gdb_ctx
->s
, "OK");
1621 gdb_ctx
->s
->g_cpu
= cpu
;
1622 put_packet(gdb_ctx
->s
, "OK");
1625 put_packet(gdb_ctx
->s
, "E22");
1630 static void handle_insert_bp(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1634 if (gdb_ctx
->num_params
!= 3) {
1635 put_packet(gdb_ctx
->s
, "E22");
1639 res
= gdb_breakpoint_insert(gdb_ctx
->params
[0].val_ul
,
1640 gdb_ctx
->params
[1].val_ull
,
1641 gdb_ctx
->params
[2].val_ull
);
1643 put_packet(gdb_ctx
->s
, "OK");
1645 } else if (res
== -ENOSYS
) {
1646 put_packet(gdb_ctx
->s
, "");
1650 put_packet(gdb_ctx
->s
, "E22");
1653 static void handle_remove_bp(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1657 if (gdb_ctx
->num_params
!= 3) {
1658 put_packet(gdb_ctx
->s
, "E22");
1662 res
= gdb_breakpoint_remove(gdb_ctx
->params
[0].val_ul
,
1663 gdb_ctx
->params
[1].val_ull
,
1664 gdb_ctx
->params
[2].val_ull
);
1666 put_packet(gdb_ctx
->s
, "OK");
1668 } else if (res
== -ENOSYS
) {
1669 put_packet(gdb_ctx
->s
, "");
1673 put_packet(gdb_ctx
->s
, "E22");
1676 static void handle_set_reg(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1681 put_packet(gdb_ctx
->s
, "E00");
1685 if (gdb_ctx
->num_params
!= 2) {
1686 put_packet(gdb_ctx
->s
, "E22");
1690 reg_size
= strlen(gdb_ctx
->params
[1].data
) / 2;
1691 hextomem(gdb_ctx
->mem_buf
, gdb_ctx
->params
[1].data
, reg_size
);
1692 gdb_write_register(gdb_ctx
->s
->g_cpu
, gdb_ctx
->mem_buf
,
1693 gdb_ctx
->params
[0].val_ull
);
1694 put_packet(gdb_ctx
->s
, "OK");
1697 static void handle_get_reg(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1702 * Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1703 * This works, but can be very slow. Anything new enough to
1704 * understand XML also knows how to use this properly.
1707 put_packet(gdb_ctx
->s
, "");
1711 if (!gdb_ctx
->num_params
) {
1712 put_packet(gdb_ctx
->s
, "E14");
1716 reg_size
= gdb_read_register(gdb_ctx
->s
->g_cpu
, gdb_ctx
->mem_buf
,
1717 gdb_ctx
->params
[0].val_ull
);
1719 put_packet(gdb_ctx
->s
, "E14");
1723 memtohex(gdb_ctx
->str_buf
, gdb_ctx
->mem_buf
, reg_size
);
1724 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1727 static void handle_write_mem(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1729 if (gdb_ctx
->num_params
!= 3) {
1730 put_packet(gdb_ctx
->s
, "E22");
1734 /* hextomem() reads 2*len bytes */
1735 if (gdb_ctx
->params
[1].val_ull
> strlen(gdb_ctx
->params
[2].data
) / 2) {
1736 put_packet(gdb_ctx
->s
, "E22");
1740 hextomem(gdb_ctx
->mem_buf
, gdb_ctx
->params
[2].data
,
1741 gdb_ctx
->params
[1].val_ull
);
1742 if (target_memory_rw_debug(gdb_ctx
->s
->g_cpu
, gdb_ctx
->params
[0].val_ull
,
1744 gdb_ctx
->params
[1].val_ull
, true)) {
1745 put_packet(gdb_ctx
->s
, "E14");
1749 put_packet(gdb_ctx
->s
, "OK");
1752 static void handle_read_mem(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1754 if (gdb_ctx
->num_params
!= 2) {
1755 put_packet(gdb_ctx
->s
, "E22");
1759 /* memtohex() doubles the required space */
1760 if (gdb_ctx
->params
[1].val_ull
> MAX_PACKET_LENGTH
/ 2) {
1761 put_packet(gdb_ctx
->s
, "E22");
1765 if (target_memory_rw_debug(gdb_ctx
->s
->g_cpu
, gdb_ctx
->params
[0].val_ull
,
1767 gdb_ctx
->params
[1].val_ull
, false)) {
1768 put_packet(gdb_ctx
->s
, "E14");
1772 memtohex(gdb_ctx
->str_buf
, gdb_ctx
->mem_buf
, gdb_ctx
->params
[1].val_ull
);
1773 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1776 static void handle_write_all_regs(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1778 target_ulong addr
, len
;
1782 if (!gdb_ctx
->num_params
) {
1786 cpu_synchronize_state(gdb_ctx
->s
->g_cpu
);
1787 registers
= gdb_ctx
->mem_buf
;
1788 len
= strlen(gdb_ctx
->params
[0].data
) / 2;
1789 hextomem(registers
, gdb_ctx
->params
[0].data
, len
);
1790 for (addr
= 0; addr
< gdb_ctx
->s
->g_cpu
->gdb_num_g_regs
&& len
> 0;
1792 reg_size
= gdb_write_register(gdb_ctx
->s
->g_cpu
, registers
, addr
);
1794 registers
+= reg_size
;
1796 put_packet(gdb_ctx
->s
, "OK");
1799 static void handle_read_all_regs(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1801 target_ulong addr
, len
;
1803 cpu_synchronize_state(gdb_ctx
->s
->g_cpu
);
1805 for (addr
= 0; addr
< gdb_ctx
->s
->g_cpu
->gdb_num_g_regs
; addr
++) {
1806 len
+= gdb_read_register(gdb_ctx
->s
->g_cpu
, gdb_ctx
->mem_buf
+ len
,
1810 memtohex(gdb_ctx
->str_buf
, gdb_ctx
->mem_buf
, len
);
1811 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1814 static void handle_file_io(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1816 if (gdb_ctx
->num_params
>= 2 && gdb_ctx
->s
->current_syscall_cb
) {
1817 target_ulong ret
, err
;
1819 ret
= (target_ulong
)gdb_ctx
->params
[0].val_ull
;
1820 err
= (target_ulong
)gdb_ctx
->params
[1].val_ull
;
1821 gdb_ctx
->s
->current_syscall_cb(gdb_ctx
->s
->c_cpu
, ret
, err
);
1822 gdb_ctx
->s
->current_syscall_cb
= NULL
;
1825 if (gdb_ctx
->num_params
>= 3 && gdb_ctx
->params
[2].opcode
== (uint8_t)'C') {
1826 put_packet(gdb_ctx
->s
, "T02");
1830 gdb_continue(gdb_ctx
->s
);
1833 static void handle_step(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1835 if (gdb_ctx
->num_params
) {
1836 gdb_set_cpu_pc(gdb_ctx
->s
, (target_ulong
)gdb_ctx
->params
[0].val_ull
);
1839 cpu_single_step(gdb_ctx
->s
->c_cpu
, sstep_flags
);
1840 gdb_continue(gdb_ctx
->s
);
1843 static void handle_v_cont_query(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1845 put_packet(gdb_ctx
->s
, "vCont;c;C;s;S");
1848 static void handle_v_cont(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1852 if (!gdb_ctx
->num_params
) {
1856 res
= gdb_handle_vcont(gdb_ctx
->s
, gdb_ctx
->params
[0].data
);
1857 if ((res
== -EINVAL
) || (res
== -ERANGE
)) {
1858 put_packet(gdb_ctx
->s
, "E22");
1860 put_packet(gdb_ctx
->s
, "");
1864 static void handle_v_attach(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1866 GDBProcess
*process
;
1870 pstrcpy(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "E22");
1871 if (!gdb_ctx
->num_params
) {
1875 process
= gdb_get_process(gdb_ctx
->s
, gdb_ctx
->params
[0].val_ul
);
1880 cpu
= get_first_cpu_in_process(gdb_ctx
->s
, process
);
1885 process
->attached
= true;
1886 gdb_ctx
->s
->g_cpu
= cpu
;
1887 gdb_ctx
->s
->c_cpu
= cpu
;
1889 gdb_fmt_thread_id(gdb_ctx
->s
, cpu
, thread_id
, sizeof(thread_id
));
1890 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "T%02xthread:%s;",
1891 GDB_SIGNAL_TRAP
, thread_id
);
1893 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1896 static void handle_v_kill(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1898 /* Kill the target */
1899 put_packet(gdb_ctx
->s
, "OK");
1900 error_report("QEMU: Terminated via GDBstub");
1904 static GdbCmdParseEntry gdb_v_commands_table
[] = {
1905 /* Order is important if has same prefix */
1907 .handler
= handle_v_cont_query
,
1912 .handler
= handle_v_cont
,
1914 .cmd_startswith
= 1,
1918 .handler
= handle_v_attach
,
1920 .cmd_startswith
= 1,
1924 .handler
= handle_v_kill
,
1930 static void handle_v_commands(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1932 if (!gdb_ctx
->num_params
) {
1936 if (process_string_cmd(gdb_ctx
->s
, NULL
, gdb_ctx
->params
[0].data
,
1937 gdb_v_commands_table
,
1938 ARRAY_SIZE(gdb_v_commands_table
))) {
1939 put_packet(gdb_ctx
->s
, "");
1943 static void handle_query_qemu_sstepbits(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1945 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
),
1946 "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", SSTEP_ENABLE
,
1947 SSTEP_NOIRQ
, SSTEP_NOTIMER
);
1948 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1951 static void handle_set_qemu_sstep(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1953 if (!gdb_ctx
->num_params
) {
1957 sstep_flags
= gdb_ctx
->params
[0].val_ul
;
1958 put_packet(gdb_ctx
->s
, "OK");
1961 static void handle_query_qemu_sstep(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1963 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "0x%x", sstep_flags
);
1964 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1967 static void handle_query_curr_tid(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1970 GDBProcess
*process
;
1974 * "Current thread" remains vague in the spec, so always return
1975 * the first thread of the current process (gdb returns the
1978 process
= gdb_get_cpu_process(gdb_ctx
->s
, gdb_ctx
->s
->g_cpu
);
1979 cpu
= get_first_cpu_in_process(gdb_ctx
->s
, process
);
1980 gdb_fmt_thread_id(gdb_ctx
->s
, cpu
, thread_id
, sizeof(thread_id
));
1981 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "QC%s", thread_id
);
1982 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1985 static void handle_query_threads(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1989 if (!gdb_ctx
->s
->query_cpu
) {
1990 put_packet(gdb_ctx
->s
, "l");
1994 gdb_fmt_thread_id(gdb_ctx
->s
, gdb_ctx
->s
->query_cpu
, thread_id
,
1996 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "m%s", thread_id
);
1997 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1998 gdb_ctx
->s
->query_cpu
=
1999 gdb_next_attached_cpu(gdb_ctx
->s
, gdb_ctx
->s
->query_cpu
);
2002 static void handle_query_first_threads(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2004 gdb_ctx
->s
->query_cpu
= gdb_first_attached_cpu(gdb_ctx
->s
);
2005 handle_query_threads(gdb_ctx
, user_ctx
);
2008 static void handle_query_thread_extra(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2013 if (!gdb_ctx
->num_params
||
2014 gdb_ctx
->params
[0].thread_id
.kind
== GDB_READ_THREAD_ERR
) {
2015 put_packet(gdb_ctx
->s
, "E22");
2019 cpu
= gdb_get_cpu(gdb_ctx
->s
, gdb_ctx
->params
[0].thread_id
.pid
,
2020 gdb_ctx
->params
[0].thread_id
.tid
);
2025 cpu_synchronize_state(cpu
);
2027 if (gdb_ctx
->s
->multiprocess
&& (gdb_ctx
->s
->process_num
> 1)) {
2028 /* Print the CPU model and name in multiprocess mode */
2029 ObjectClass
*oc
= object_get_class(OBJECT(cpu
));
2030 const char *cpu_model
= object_class_get_name(oc
);
2031 char *cpu_name
= object_get_canonical_path_component(OBJECT(cpu
));
2032 len
= snprintf((char *)gdb_ctx
->mem_buf
, sizeof(gdb_ctx
->str_buf
) / 2,
2033 "%s %s [%s]", cpu_model
, cpu_name
,
2034 cpu
->halted
? "halted " : "running");
2037 /* memtohex() doubles the required space */
2038 len
= snprintf((char *)gdb_ctx
->mem_buf
, sizeof(gdb_ctx
->str_buf
) / 2,
2039 "CPU#%d [%s]", cpu
->cpu_index
,
2040 cpu
->halted
? "halted " : "running");
2042 trace_gdbstub_op_extra_info((char *)gdb_ctx
->mem_buf
);
2043 memtohex(gdb_ctx
->str_buf
, gdb_ctx
->mem_buf
, len
);
2044 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
2047 #ifdef CONFIG_USER_ONLY
2048 static void handle_query_offsets(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2052 ts
= gdb_ctx
->s
->c_cpu
->opaque
;
2053 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
),
2054 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
2055 ";Bss=" TARGET_ABI_FMT_lx
,
2056 ts
->info
->code_offset
,
2057 ts
->info
->data_offset
,
2058 ts
->info
->data_offset
);
2059 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
2062 static void handle_query_rcmd(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2066 if (!gdb_ctx
->num_params
) {
2067 put_packet(gdb_ctx
->s
, "E22");
2071 len
= strlen(gdb_ctx
->params
[0].data
);
2073 put_packet(gdb_ctx
->s
, "E01");
2078 hextomem(gdb_ctx
->mem_buf
, gdb_ctx
->params
[0].data
, len
);
2079 gdb_ctx
->mem_buf
[len
++] = 0;
2080 qemu_chr_be_write(gdb_ctx
->s
->mon_chr
, gdb_ctx
->mem_buf
, len
);
2081 put_packet(gdb_ctx
->s
, "OK");
2086 static void handle_query_supported(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2090 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "PacketSize=%x",
2092 cc
= CPU_GET_CLASS(first_cpu
);
2093 if (cc
->gdb_core_xml_file
) {
2094 pstrcat(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
),
2095 ";qXfer:features:read+");
2098 if (gdb_ctx
->num_params
&&
2099 strstr(gdb_ctx
->params
[0].data
, "multiprocess+")) {
2100 gdb_ctx
->s
->multiprocess
= true;
2103 pstrcat(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), ";multiprocess+");
2104 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
2107 static void handle_query_xfer_features(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2109 GDBProcess
*process
;
2111 unsigned long len
, total_len
, addr
;
2115 if (gdb_ctx
->num_params
< 3) {
2116 put_packet(gdb_ctx
->s
, "E22");
2120 process
= gdb_get_cpu_process(gdb_ctx
->s
, gdb_ctx
->s
->g_cpu
);
2121 cc
= CPU_GET_CLASS(gdb_ctx
->s
->g_cpu
);
2122 if (!cc
->gdb_core_xml_file
) {
2123 put_packet(gdb_ctx
->s
, "");
2128 p
= gdb_ctx
->params
[0].data
;
2129 xml
= get_feature_xml(gdb_ctx
->s
, p
, &p
, process
);
2131 put_packet(gdb_ctx
->s
, "E00");
2135 addr
= gdb_ctx
->params
[1].val_ul
;
2136 len
= gdb_ctx
->params
[2].val_ul
;
2137 total_len
= strlen(xml
);
2138 if (addr
> total_len
) {
2139 put_packet(gdb_ctx
->s
, "E00");
2143 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
2144 len
= (MAX_PACKET_LENGTH
- 5) / 2;
2147 if (len
< total_len
- addr
) {
2148 gdb_ctx
->str_buf
[0] = 'm';
2149 len
= memtox(gdb_ctx
->str_buf
+ 1, xml
+ addr
, len
);
2151 gdb_ctx
->str_buf
[0] = 'l';
2152 len
= memtox(gdb_ctx
->str_buf
+ 1, xml
+ addr
, total_len
- addr
);
2155 put_packet_binary(gdb_ctx
->s
, gdb_ctx
->str_buf
, len
+ 1, true);
2158 static void handle_query_attached(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2160 put_packet(gdb_ctx
->s
, GDB_ATTACHED
);
2163 static void handle_query_qemu_supported(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2165 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "sstepbits;sstep");
2166 #ifndef CONFIG_USER_ONLY
2167 pstrcat(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), ";PhyMemMode");
2169 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
2172 #ifndef CONFIG_USER_ONLY
2173 static void handle_query_qemu_phy_mem_mode(GdbCmdContext
*gdb_ctx
,
2176 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "%d", phy_memory_mode
);
2177 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
2180 static void handle_set_qemu_phy_mem_mode(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2182 if (!gdb_ctx
->num_params
) {
2183 put_packet(gdb_ctx
->s
, "E22");
2187 if (!gdb_ctx
->params
[0].val_ul
) {
2188 phy_memory_mode
= 0;
2190 phy_memory_mode
= 1;
2192 put_packet(gdb_ctx
->s
, "OK");
2196 static GdbCmdParseEntry gdb_gen_query_set_common_table
[] = {
2197 /* Order is important if has same prefix */
2199 .handler
= handle_query_qemu_sstepbits
,
2200 .cmd
= "qemu.sstepbits",
2203 .handler
= handle_query_qemu_sstep
,
2204 .cmd
= "qemu.sstep",
2207 .handler
= handle_set_qemu_sstep
,
2208 .cmd
= "qemu.sstep=",
2209 .cmd_startswith
= 1,
2214 static GdbCmdParseEntry gdb_gen_query_table
[] = {
2216 .handler
= handle_query_curr_tid
,
2220 .handler
= handle_query_threads
,
2221 .cmd
= "sThreadInfo",
2224 .handler
= handle_query_first_threads
,
2225 .cmd
= "fThreadInfo",
2228 .handler
= handle_query_thread_extra
,
2229 .cmd
= "ThreadExtraInfo,",
2230 .cmd_startswith
= 1,
2233 #ifdef CONFIG_USER_ONLY
2235 .handler
= handle_query_offsets
,
2240 .handler
= handle_query_rcmd
,
2242 .cmd_startswith
= 1,
2247 .handler
= handle_query_supported
,
2248 .cmd
= "Supported:",
2249 .cmd_startswith
= 1,
2253 .handler
= handle_query_supported
,
2258 .handler
= handle_query_xfer_features
,
2259 .cmd
= "Xfer:features:read:",
2260 .cmd_startswith
= 1,
2264 .handler
= handle_query_attached
,
2269 .handler
= handle_query_attached
,
2273 .handler
= handle_query_qemu_supported
,
2274 .cmd
= "qemu.Supported",
2276 #ifndef CONFIG_USER_ONLY
2278 .handler
= handle_query_qemu_phy_mem_mode
,
2279 .cmd
= "qemu.PhyMemMode",
2284 static GdbCmdParseEntry gdb_gen_set_table
[] = {
2285 /* Order is important if has same prefix */
2287 .handler
= handle_set_qemu_sstep
,
2288 .cmd
= "qemu.sstep:",
2289 .cmd_startswith
= 1,
2292 #ifndef CONFIG_USER_ONLY
2294 .handler
= handle_set_qemu_phy_mem_mode
,
2295 .cmd
= "qemu.PhyMemMode:",
2296 .cmd_startswith
= 1,
2302 static void handle_gen_query(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2304 if (!gdb_ctx
->num_params
) {
2308 if (!process_string_cmd(gdb_ctx
->s
, NULL
, gdb_ctx
->params
[0].data
,
2309 gdb_gen_query_set_common_table
,
2310 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
2314 if (process_string_cmd(gdb_ctx
->s
, NULL
, gdb_ctx
->params
[0].data
,
2315 gdb_gen_query_table
,
2316 ARRAY_SIZE(gdb_gen_query_table
))) {
2317 put_packet(gdb_ctx
->s
, "");
2321 static void handle_gen_set(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2323 if (!gdb_ctx
->num_params
) {
2327 if (!process_string_cmd(gdb_ctx
->s
, NULL
, gdb_ctx
->params
[0].data
,
2328 gdb_gen_query_set_common_table
,
2329 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
2333 if (process_string_cmd(gdb_ctx
->s
, NULL
, gdb_ctx
->params
[0].data
,
2335 ARRAY_SIZE(gdb_gen_set_table
))) {
2336 put_packet(gdb_ctx
->s
, "");
2340 static void handle_target_halt(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2344 gdb_fmt_thread_id(gdb_ctx
->s
, gdb_ctx
->s
->c_cpu
, thread_id
,
2346 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "T%02xthread:%s;",
2347 GDB_SIGNAL_TRAP
, thread_id
);
2348 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
2350 * Remove all the breakpoints when this query is issued,
2351 * because gdb is doing an initial connect and the state
2352 * should be cleaned up.
2354 gdb_breakpoint_remove_all();
2357 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
2359 const GdbCmdParseEntry
*cmd_parser
= NULL
;
2361 trace_gdbstub_io_command(line_buf
);
2363 switch (line_buf
[0]) {
2365 put_packet(s
, "OK");
2369 static const GdbCmdParseEntry target_halted_cmd_desc
= {
2370 .handler
= handle_target_halt
,
2374 cmd_parser
= &target_halted_cmd_desc
;
2379 static const GdbCmdParseEntry continue_cmd_desc
= {
2380 .handler
= handle_continue
,
2382 .cmd_startswith
= 1,
2385 cmd_parser
= &continue_cmd_desc
;
2390 static const GdbCmdParseEntry cont_with_sig_cmd_desc
= {
2391 .handler
= handle_cont_with_sig
,
2393 .cmd_startswith
= 1,
2396 cmd_parser
= &cont_with_sig_cmd_desc
;
2401 static const GdbCmdParseEntry v_cmd_desc
= {
2402 .handler
= handle_v_commands
,
2404 .cmd_startswith
= 1,
2407 cmd_parser
= &v_cmd_desc
;
2411 /* Kill the target */
2412 error_report("QEMU: Terminated via GDBstub");
2416 static const GdbCmdParseEntry detach_cmd_desc
= {
2417 .handler
= handle_detach
,
2419 .cmd_startswith
= 1,
2422 cmd_parser
= &detach_cmd_desc
;
2427 static const GdbCmdParseEntry step_cmd_desc
= {
2428 .handler
= handle_step
,
2430 .cmd_startswith
= 1,
2433 cmd_parser
= &step_cmd_desc
;
2438 static const GdbCmdParseEntry file_io_cmd_desc
= {
2439 .handler
= handle_file_io
,
2441 .cmd_startswith
= 1,
2444 cmd_parser
= &file_io_cmd_desc
;
2449 static const GdbCmdParseEntry read_all_regs_cmd_desc
= {
2450 .handler
= handle_read_all_regs
,
2454 cmd_parser
= &read_all_regs_cmd_desc
;
2459 static const GdbCmdParseEntry write_all_regs_cmd_desc
= {
2460 .handler
= handle_write_all_regs
,
2462 .cmd_startswith
= 1,
2465 cmd_parser
= &write_all_regs_cmd_desc
;
2470 static const GdbCmdParseEntry read_mem_cmd_desc
= {
2471 .handler
= handle_read_mem
,
2473 .cmd_startswith
= 1,
2476 cmd_parser
= &read_mem_cmd_desc
;
2481 static const GdbCmdParseEntry write_mem_cmd_desc
= {
2482 .handler
= handle_write_mem
,
2484 .cmd_startswith
= 1,
2487 cmd_parser
= &write_mem_cmd_desc
;
2492 static const GdbCmdParseEntry get_reg_cmd_desc
= {
2493 .handler
= handle_get_reg
,
2495 .cmd_startswith
= 1,
2498 cmd_parser
= &get_reg_cmd_desc
;
2503 static const GdbCmdParseEntry set_reg_cmd_desc
= {
2504 .handler
= handle_set_reg
,
2506 .cmd_startswith
= 1,
2509 cmd_parser
= &set_reg_cmd_desc
;
2514 static const GdbCmdParseEntry insert_bp_cmd_desc
= {
2515 .handler
= handle_insert_bp
,
2517 .cmd_startswith
= 1,
2520 cmd_parser
= &insert_bp_cmd_desc
;
2525 static const GdbCmdParseEntry remove_bp_cmd_desc
= {
2526 .handler
= handle_remove_bp
,
2528 .cmd_startswith
= 1,
2531 cmd_parser
= &remove_bp_cmd_desc
;
2536 static const GdbCmdParseEntry set_thread_cmd_desc
= {
2537 .handler
= handle_set_thread
,
2539 .cmd_startswith
= 1,
2542 cmd_parser
= &set_thread_cmd_desc
;
2547 static const GdbCmdParseEntry thread_alive_cmd_desc
= {
2548 .handler
= handle_thread_alive
,
2550 .cmd_startswith
= 1,
2553 cmd_parser
= &thread_alive_cmd_desc
;
2558 static const GdbCmdParseEntry gen_query_cmd_desc
= {
2559 .handler
= handle_gen_query
,
2561 .cmd_startswith
= 1,
2564 cmd_parser
= &gen_query_cmd_desc
;
2569 static const GdbCmdParseEntry gen_set_cmd_desc
= {
2570 .handler
= handle_gen_set
,
2572 .cmd_startswith
= 1,
2575 cmd_parser
= &gen_set_cmd_desc
;
2579 /* put empty packet */
2584 run_cmd_parser(s
, line_buf
, cmd_parser
);
2589 void gdb_set_stop_cpu(CPUState
*cpu
)
2591 GDBProcess
*p
= gdb_get_cpu_process(gdbserver_state
, cpu
);
2595 * Having a stop CPU corresponding to a process that is not attached
2596 * confuses GDB. So we ignore the request.
2601 gdbserver_state
->c_cpu
= cpu
;
2602 gdbserver_state
->g_cpu
= cpu
;
2605 #ifndef CONFIG_USER_ONLY
2606 static void gdb_vm_state_change(void *opaque
, int running
, RunState state
)
2608 GDBState
*s
= gdbserver_state
;
2609 CPUState
*cpu
= s
->c_cpu
;
2615 if (running
|| s
->state
== RS_INACTIVE
) {
2618 /* Is there a GDB syscall waiting to be sent? */
2619 if (s
->current_syscall_cb
) {
2620 put_packet(s
, s
->syscall_buf
);
2625 /* No process attached */
2629 gdb_fmt_thread_id(s
, cpu
, thread_id
, sizeof(thread_id
));
2632 case RUN_STATE_DEBUG
:
2633 if (cpu
->watchpoint_hit
) {
2634 switch (cpu
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
2645 trace_gdbstub_hit_watchpoint(type
, cpu_gdb_index(cpu
),
2646 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
2647 snprintf(buf
, sizeof(buf
),
2648 "T%02xthread:%s;%swatch:" TARGET_FMT_lx
";",
2649 GDB_SIGNAL_TRAP
, thread_id
, type
,
2650 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
2651 cpu
->watchpoint_hit
= NULL
;
2654 trace_gdbstub_hit_break();
2657 ret
= GDB_SIGNAL_TRAP
;
2659 case RUN_STATE_PAUSED
:
2660 trace_gdbstub_hit_paused();
2661 ret
= GDB_SIGNAL_INT
;
2663 case RUN_STATE_SHUTDOWN
:
2664 trace_gdbstub_hit_shutdown();
2665 ret
= GDB_SIGNAL_QUIT
;
2667 case RUN_STATE_IO_ERROR
:
2668 trace_gdbstub_hit_io_error();
2669 ret
= GDB_SIGNAL_IO
;
2671 case RUN_STATE_WATCHDOG
:
2672 trace_gdbstub_hit_watchdog();
2673 ret
= GDB_SIGNAL_ALRM
;
2675 case RUN_STATE_INTERNAL_ERROR
:
2676 trace_gdbstub_hit_internal_error();
2677 ret
= GDB_SIGNAL_ABRT
;
2679 case RUN_STATE_SAVE_VM
:
2680 case RUN_STATE_RESTORE_VM
:
2682 case RUN_STATE_FINISH_MIGRATE
:
2683 ret
= GDB_SIGNAL_XCPU
;
2686 trace_gdbstub_hit_unknown(state
);
2687 ret
= GDB_SIGNAL_UNKNOWN
;
2690 gdb_set_stop_cpu(cpu
);
2691 snprintf(buf
, sizeof(buf
), "T%02xthread:%s;", ret
, thread_id
);
2696 /* disable single step if it was enabled */
2697 cpu_single_step(cpu
, 0);
2701 /* Send a gdb syscall request.
2702 This accepts limited printf-style format specifiers, specifically:
2703 %x - target_ulong argument printed in hex.
2704 %lx - 64-bit argument printed in hex.
2705 %s - string pointer (target_ulong) and length (int) pair. */
2706 void gdb_do_syscallv(gdb_syscall_complete_cb cb
, const char *fmt
, va_list va
)
2714 s
= gdbserver_state
;
2717 s
->current_syscall_cb
= cb
;
2718 #ifndef CONFIG_USER_ONLY
2719 vm_stop(RUN_STATE_DEBUG
);
2722 p_end
= &s
->syscall_buf
[sizeof(s
->syscall_buf
)];
2729 addr
= va_arg(va
, target_ulong
);
2730 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
, addr
);
2733 if (*(fmt
++) != 'x')
2735 i64
= va_arg(va
, uint64_t);
2736 p
+= snprintf(p
, p_end
- p
, "%" PRIx64
, i64
);
2739 addr
= va_arg(va
, target_ulong
);
2740 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
"/%x",
2741 addr
, va_arg(va
, int));
2745 error_report("gdbstub: Bad syscall format string '%s'",
2754 #ifdef CONFIG_USER_ONLY
2755 put_packet(s
, s
->syscall_buf
);
2756 /* Return control to gdb for it to process the syscall request.
2757 * Since the protocol requires that gdb hands control back to us
2758 * using a "here are the results" F packet, we don't need to check
2759 * gdb_handlesig's return value (which is the signal to deliver if
2760 * execution was resumed via a continue packet).
2762 gdb_handlesig(s
->c_cpu
, 0);
2764 /* In this case wait to send the syscall packet until notification that
2765 the CPU has stopped. This must be done because if the packet is sent
2766 now the reply from the syscall request could be received while the CPU
2767 is still in the running state, which can cause packets to be dropped
2768 and state transition 'T' packets to be sent while the syscall is still
2770 qemu_cpu_kick(s
->c_cpu
);
2774 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
2779 gdb_do_syscallv(cb
, fmt
, va
);
2783 static void gdb_read_byte(GDBState
*s
, uint8_t ch
)
2787 #ifndef CONFIG_USER_ONLY
2788 if (s
->last_packet_len
) {
2789 /* Waiting for a response to the last packet. If we see the start
2790 of a new command then abandon the previous response. */
2792 trace_gdbstub_err_got_nack();
2793 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
2794 } else if (ch
== '+') {
2795 trace_gdbstub_io_got_ack();
2797 trace_gdbstub_io_got_unexpected(ch
);
2800 if (ch
== '+' || ch
== '$')
2801 s
->last_packet_len
= 0;
2805 if (runstate_is_running()) {
2806 /* when the CPU is running, we cannot do anything except stop
2807 it when receiving a char */
2808 vm_stop(RUN_STATE_PAUSED
);
2815 /* start of command packet */
2816 s
->line_buf_index
= 0;
2818 s
->state
= RS_GETLINE
;
2820 trace_gdbstub_err_garbage(ch
);
2825 /* start escape sequence */
2826 s
->state
= RS_GETLINE_ESC
;
2828 } else if (ch
== '*') {
2829 /* start run length encoding sequence */
2830 s
->state
= RS_GETLINE_RLE
;
2832 } else if (ch
== '#') {
2833 /* end of command, start of checksum*/
2834 s
->state
= RS_CHKSUM1
;
2835 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2836 trace_gdbstub_err_overrun();
2839 /* unescaped command character */
2840 s
->line_buf
[s
->line_buf_index
++] = ch
;
2844 case RS_GETLINE_ESC
:
2846 /* unexpected end of command in escape sequence */
2847 s
->state
= RS_CHKSUM1
;
2848 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2849 /* command buffer overrun */
2850 trace_gdbstub_err_overrun();
2853 /* parse escaped character and leave escape state */
2854 s
->line_buf
[s
->line_buf_index
++] = ch
^ 0x20;
2856 s
->state
= RS_GETLINE
;
2859 case RS_GETLINE_RLE
:
2861 * Run-length encoding is explained in "Debugging with GDB /
2862 * Appendix E GDB Remote Serial Protocol / Overview".
2864 if (ch
< ' ' || ch
== '#' || ch
== '$' || ch
> 126) {
2865 /* invalid RLE count encoding */
2866 trace_gdbstub_err_invalid_repeat(ch
);
2867 s
->state
= RS_GETLINE
;
2869 /* decode repeat length */
2870 int repeat
= ch
- ' ' + 3;
2871 if (s
->line_buf_index
+ repeat
>= sizeof(s
->line_buf
) - 1) {
2872 /* that many repeats would overrun the command buffer */
2873 trace_gdbstub_err_overrun();
2875 } else if (s
->line_buf_index
< 1) {
2876 /* got a repeat but we have nothing to repeat */
2877 trace_gdbstub_err_invalid_rle();
2878 s
->state
= RS_GETLINE
;
2880 /* repeat the last character */
2881 memset(s
->line_buf
+ s
->line_buf_index
,
2882 s
->line_buf
[s
->line_buf_index
- 1], repeat
);
2883 s
->line_buf_index
+= repeat
;
2885 s
->state
= RS_GETLINE
;
2890 /* get high hex digit of checksum */
2891 if (!isxdigit(ch
)) {
2892 trace_gdbstub_err_checksum_invalid(ch
);
2893 s
->state
= RS_GETLINE
;
2896 s
->line_buf
[s
->line_buf_index
] = '\0';
2897 s
->line_csum
= fromhex(ch
) << 4;
2898 s
->state
= RS_CHKSUM2
;
2901 /* get low hex digit of checksum */
2902 if (!isxdigit(ch
)) {
2903 trace_gdbstub_err_checksum_invalid(ch
);
2904 s
->state
= RS_GETLINE
;
2907 s
->line_csum
|= fromhex(ch
);
2909 if (s
->line_csum
!= (s
->line_sum
& 0xff)) {
2910 trace_gdbstub_err_checksum_incorrect(s
->line_sum
, s
->line_csum
);
2911 /* send NAK reply */
2913 put_buffer(s
, &reply
, 1);
2916 /* send ACK reply */
2918 put_buffer(s
, &reply
, 1);
2919 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
2928 /* Tell the remote gdb that the process has exited. */
2929 void gdb_exit(CPUArchState
*env
, int code
)
2934 s
= gdbserver_state
;
2938 #ifdef CONFIG_USER_ONLY
2939 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2944 trace_gdbstub_op_exiting((uint8_t)code
);
2946 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
2949 #ifndef CONFIG_USER_ONLY
2950 qemu_chr_fe_deinit(&s
->chr
, true);
2955 * Create the process that will contain all the "orphan" CPUs (that are not
2956 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2957 * be attachable and thus will be invisible to the user.
2959 static void create_default_process(GDBState
*s
)
2961 GDBProcess
*process
;
2964 if (s
->process_num
) {
2965 max_pid
= s
->processes
[s
->process_num
- 1].pid
;
2968 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2969 process
= &s
->processes
[s
->process_num
- 1];
2971 /* We need an available PID slot for this process */
2972 assert(max_pid
< UINT32_MAX
);
2974 process
->pid
= max_pid
+ 1;
2975 process
->attached
= false;
2976 process
->target_xml
[0] = '\0';
2979 #ifdef CONFIG_USER_ONLY
2981 gdb_handlesig(CPUState
*cpu
, int sig
)
2987 s
= gdbserver_state
;
2988 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2992 /* disable single step if it was enabled */
2993 cpu_single_step(cpu
, 0);
2997 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb(sig
));
3000 /* put_packet() might have detected that the peer terminated the
3008 s
->running_state
= 0;
3009 while (s
->running_state
== 0) {
3010 n
= read(s
->fd
, buf
, 256);
3014 for (i
= 0; i
< n
; i
++) {
3015 gdb_read_byte(s
, buf
[i
]);
3018 /* XXX: Connection closed. Should probably wait for another
3019 connection before continuing. */
3032 /* Tell the remote gdb that the process has exited due to SIG. */
3033 void gdb_signalled(CPUArchState
*env
, int sig
)
3038 s
= gdbserver_state
;
3039 if (gdbserver_fd
< 0 || s
->fd
< 0) {
3043 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb(sig
));
3047 static bool gdb_accept(void)
3050 struct sockaddr_in sockaddr
;
3055 len
= sizeof(sockaddr
);
3056 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
3057 if (fd
< 0 && errno
!= EINTR
) {
3060 } else if (fd
>= 0) {
3061 qemu_set_cloexec(fd
);
3066 /* set short latency */
3067 if (socket_set_nodelay(fd
)) {
3068 perror("setsockopt");
3073 s
= g_malloc0(sizeof(GDBState
));
3074 create_default_process(s
);
3075 s
->processes
[0].attached
= true;
3076 s
->c_cpu
= gdb_first_attached_cpu(s
);
3077 s
->g_cpu
= s
->c_cpu
;
3079 gdb_has_xml
= false;
3081 gdbserver_state
= s
;
3085 static int gdbserver_open(int port
)
3087 struct sockaddr_in sockaddr
;
3090 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3095 qemu_set_cloexec(fd
);
3097 socket_set_fast_reuse(fd
);
3099 sockaddr
.sin_family
= AF_INET
;
3100 sockaddr
.sin_port
= htons(port
);
3101 sockaddr
.sin_addr
.s_addr
= 0;
3102 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
3108 ret
= listen(fd
, 1);
3117 int gdbserver_start(int port
)
3119 gdbserver_fd
= gdbserver_open(port
);
3120 if (gdbserver_fd
< 0)
3122 /* accept connections */
3123 if (!gdb_accept()) {
3124 close(gdbserver_fd
);
3131 /* Disable gdb stub for child processes. */
3132 void gdbserver_fork(CPUState
*cpu
)
3134 GDBState
*s
= gdbserver_state
;
3136 if (gdbserver_fd
< 0 || s
->fd
< 0) {
3141 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
3142 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
3145 static int gdb_chr_can_receive(void *opaque
)
3147 /* We can handle an arbitrarily large amount of data.
3148 Pick the maximum packet size, which is as good as anything. */
3149 return MAX_PACKET_LENGTH
;
3152 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
3156 for (i
= 0; i
< size
; i
++) {
3157 gdb_read_byte(gdbserver_state
, buf
[i
]);
3161 static void gdb_chr_event(void *opaque
, int event
)
3164 GDBState
*s
= (GDBState
*) opaque
;
3167 case CHR_EVENT_OPENED
:
3168 /* Start with first process attached, others detached */
3169 for (i
= 0; i
< s
->process_num
; i
++) {
3170 s
->processes
[i
].attached
= !i
;
3173 s
->c_cpu
= gdb_first_attached_cpu(s
);
3174 s
->g_cpu
= s
->c_cpu
;
3176 vm_stop(RUN_STATE_PAUSED
);
3177 gdb_has_xml
= false;
3184 static void gdb_monitor_output(GDBState
*s
, const char *msg
, int len
)
3186 char buf
[MAX_PACKET_LENGTH
];
3189 if (len
> (MAX_PACKET_LENGTH
/2) - 1)
3190 len
= (MAX_PACKET_LENGTH
/2) - 1;
3191 memtohex(buf
+ 1, (uint8_t *)msg
, len
);
3195 static int gdb_monitor_write(Chardev
*chr
, const uint8_t *buf
, int len
)
3197 const char *p
= (const char *)buf
;
3200 max_sz
= (sizeof(gdbserver_state
->last_packet
) - 2) / 2;
3202 if (len
<= max_sz
) {
3203 gdb_monitor_output(gdbserver_state
, p
, len
);
3206 gdb_monitor_output(gdbserver_state
, p
, max_sz
);
3214 static void gdb_sigterm_handler(int signal
)
3216 if (runstate_is_running()) {
3217 vm_stop(RUN_STATE_PAUSED
);
3222 static void gdb_monitor_open(Chardev
*chr
, ChardevBackend
*backend
,
3223 bool *be_opened
, Error
**errp
)
3228 static void char_gdb_class_init(ObjectClass
*oc
, void *data
)
3230 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
3232 cc
->internal
= true;
3233 cc
->open
= gdb_monitor_open
;
3234 cc
->chr_write
= gdb_monitor_write
;
3237 #define TYPE_CHARDEV_GDB "chardev-gdb"
3239 static const TypeInfo char_gdb_type_info
= {
3240 .name
= TYPE_CHARDEV_GDB
,
3241 .parent
= TYPE_CHARDEV
,
3242 .class_init
= char_gdb_class_init
,
3245 static int find_cpu_clusters(Object
*child
, void *opaque
)
3247 if (object_dynamic_cast(child
, TYPE_CPU_CLUSTER
)) {
3248 GDBState
*s
= (GDBState
*) opaque
;
3249 CPUClusterState
*cluster
= CPU_CLUSTER(child
);
3250 GDBProcess
*process
;
3252 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
3254 process
= &s
->processes
[s
->process_num
- 1];
3257 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
3258 * runtime, we enforce here that the machine does not use a cluster ID
3259 * that would lead to PID 0.
3261 assert(cluster
->cluster_id
!= UINT32_MAX
);
3262 process
->pid
= cluster
->cluster_id
+ 1;
3263 process
->attached
= false;
3264 process
->target_xml
[0] = '\0';
3269 return object_child_foreach(child
, find_cpu_clusters
, opaque
);
3272 static int pid_order(const void *a
, const void *b
)
3274 GDBProcess
*pa
= (GDBProcess
*) a
;
3275 GDBProcess
*pb
= (GDBProcess
*) b
;
3277 if (pa
->pid
< pb
->pid
) {
3279 } else if (pa
->pid
> pb
->pid
) {
3286 static void create_processes(GDBState
*s
)
3288 object_child_foreach(object_get_root(), find_cpu_clusters
, s
);
3292 qsort(s
->processes
, s
->process_num
, sizeof(s
->processes
[0]), pid_order
);
3295 create_default_process(s
);
3298 static void cleanup_processes(GDBState
*s
)
3300 g_free(s
->processes
);
3302 s
->processes
= NULL
;
3305 int gdbserver_start(const char *device
)
3307 trace_gdbstub_op_start(device
);
3310 char gdbstub_device_name
[128];
3311 Chardev
*chr
= NULL
;
3315 error_report("gdbstub: meaningless to attach gdb to a "
3316 "machine without any CPU.");
3322 if (strcmp(device
, "none") != 0) {
3323 if (strstart(device
, "tcp:", NULL
)) {
3324 /* enforce required TCP attributes */
3325 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
3326 "%s,nowait,nodelay,server", device
);
3327 device
= gdbstub_device_name
;
3330 else if (strcmp(device
, "stdio") == 0) {
3331 struct sigaction act
;
3333 memset(&act
, 0, sizeof(act
));
3334 act
.sa_handler
= gdb_sigterm_handler
;
3335 sigaction(SIGINT
, &act
, NULL
);
3339 * FIXME: it's a bit weird to allow using a mux chardev here
3340 * and implicitly setup a monitor. We may want to break this.
3342 chr
= qemu_chr_new_noreplay("gdb", device
, true, NULL
);
3347 s
= gdbserver_state
;
3349 s
= g_malloc0(sizeof(GDBState
));
3350 gdbserver_state
= s
;
3352 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
3354 /* Initialize a monitor terminal for gdb */
3355 mon_chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_GDB
,
3356 NULL
, NULL
, &error_abort
);
3357 monitor_init_hmp(mon_chr
, false);
3359 qemu_chr_fe_deinit(&s
->chr
, true);
3360 mon_chr
= s
->mon_chr
;
3361 cleanup_processes(s
);
3362 memset(s
, 0, sizeof(GDBState
));
3363 s
->mon_chr
= mon_chr
;
3366 create_processes(s
);
3369 qemu_chr_fe_init(&s
->chr
, chr
, &error_abort
);
3370 qemu_chr_fe_set_handlers(&s
->chr
, gdb_chr_can_receive
, gdb_chr_receive
,
3371 gdb_chr_event
, NULL
, s
, NULL
, true);
3373 s
->state
= chr
? RS_IDLE
: RS_INACTIVE
;
3374 s
->mon_chr
= mon_chr
;
3375 s
->current_syscall_cb
= NULL
;
3380 void gdbserver_cleanup(void)
3382 if (gdbserver_state
) {
3383 put_packet(gdbserver_state
, "W00");
3387 static void register_types(void)
3389 type_register_static(&char_gdb_type_info
);
3392 type_init(register_types
);