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 bool init
; /* have we been initialised? */
346 CPUState
*c_cpu
; /* current CPU for step/continue ops */
347 CPUState
*g_cpu
; /* current CPU for other ops */
348 CPUState
*query_cpu
; /* for q{f|s}ThreadInfo */
349 enum RSState state
; /* parsing state */
350 char line_buf
[MAX_PACKET_LENGTH
];
352 int line_sum
; /* running checksum */
353 int line_csum
; /* checksum at the end of the packet */
354 uint8_t last_packet
[MAX_PACKET_LENGTH
+ 4];
357 #ifdef CONFIG_USER_ONLY
365 GDBProcess
*processes
;
367 char syscall_buf
[256];
368 gdb_syscall_complete_cb current_syscall_cb
;
371 /* By default use no IRQs and no timers while single stepping so as to
372 * make single stepping like an ICE HW step.
374 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
376 static GDBState gdbserver_state
;
378 static void init_gdbserver_state(void)
380 g_assert(!gdbserver_state
.init
);
381 memset(&gdbserver_state
, 0, sizeof(GDBState
));
382 gdbserver_state
.init
= true;
385 #ifndef CONFIG_USER_ONLY
386 static void reset_gdbserver_state(void)
388 g_free(gdbserver_state
.processes
);
389 gdbserver_state
.processes
= NULL
;
390 gdbserver_state
.process_num
= 0;
396 #ifdef CONFIG_USER_ONLY
397 /* XXX: This is not thread safe. Do we care? */
398 static int gdbserver_fd
= -1;
400 static int get_char(void)
406 ret
= qemu_recv(gdbserver_state
.fd
, &ch
, 1, 0);
408 if (errno
== ECONNRESET
)
409 gdbserver_state
.fd
= -1;
412 } else if (ret
== 0) {
413 close(gdbserver_state
.fd
);
414 gdbserver_state
.fd
= -1;
430 /* Decide if either remote gdb syscalls or native file IO should be used. */
431 int use_gdb_syscalls(void)
433 SemihostingTarget target
= semihosting_get_target();
434 if (target
== SEMIHOSTING_TARGET_NATIVE
) {
435 /* -semihosting-config target=native */
437 } else if (target
== SEMIHOSTING_TARGET_GDB
) {
438 /* -semihosting-config target=gdb */
442 /* -semihosting-config target=auto */
443 /* On the first call check if gdb is connected and remember. */
444 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
445 gdb_syscall_mode
= gdbserver_state
.init
?
446 GDB_SYS_ENABLED
: GDB_SYS_DISABLED
;
448 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
451 /* Resume execution. */
452 static inline void gdb_continue(void)
455 #ifdef CONFIG_USER_ONLY
456 gdbserver_state
.running_state
= 1;
457 trace_gdbstub_op_continue();
459 if (!runstate_needs_reset()) {
460 trace_gdbstub_op_continue();
467 * Resume execution, per CPU actions. For user-mode emulation it's
468 * equivalent to gdb_continue.
470 static int gdb_continue_partial(char *newstates
)
474 #ifdef CONFIG_USER_ONLY
476 * This is not exactly accurate, but it's an improvement compared to the
477 * previous situation, where only one CPU would be single-stepped.
480 if (newstates
[cpu
->cpu_index
] == 's') {
481 trace_gdbstub_op_stepping(cpu
->cpu_index
);
482 cpu_single_step(cpu
, sstep_flags
);
485 gdbserver_state
.running_state
= 1;
489 if (!runstate_needs_reset()) {
490 if (vm_prepare_start()) {
495 switch (newstates
[cpu
->cpu_index
]) {
498 break; /* nothing to do here */
500 trace_gdbstub_op_stepping(cpu
->cpu_index
);
501 cpu_single_step(cpu
, sstep_flags
);
506 trace_gdbstub_op_continue_cpu(cpu
->cpu_index
);
517 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, true);
523 static void put_buffer(const uint8_t *buf
, int len
)
525 #ifdef CONFIG_USER_ONLY
529 ret
= send(gdbserver_state
.fd
, buf
, len
, 0);
539 /* XXX this blocks entire thread. Rewrite to use
540 * qemu_chr_fe_write and background I/O callbacks */
541 qemu_chr_fe_write_all(&gdbserver_state
.chr
, buf
, len
);
545 static inline int fromhex(int v
)
547 if (v
>= '0' && v
<= '9')
549 else if (v
>= 'A' && v
<= 'F')
551 else if (v
>= 'a' && v
<= 'f')
557 static inline int tohex(int v
)
565 /* writes 2*len+1 bytes in buf */
566 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
571 for(i
= 0; i
< len
; i
++) {
573 *q
++ = tohex(c
>> 4);
574 *q
++ = tohex(c
& 0xf);
579 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
583 for(i
= 0; i
< len
; i
++) {
584 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
589 static void hexdump(const char *buf
, int len
,
590 void (*trace_fn
)(size_t ofs
, char const *text
))
592 char line_buffer
[3 * 16 + 4 + 16 + 1];
595 for (i
= 0; i
< len
|| (i
& 0xF); ++i
) {
596 size_t byte_ofs
= i
& 15;
599 memset(line_buffer
, ' ', 3 * 16 + 4 + 16);
600 line_buffer
[3 * 16 + 4 + 16] = 0;
603 size_t col_group
= (i
>> 2) & 3;
604 size_t hex_col
= byte_ofs
* 3 + col_group
;
605 size_t txt_col
= 3 * 16 + 4 + byte_ofs
;
610 line_buffer
[hex_col
+ 0] = tohex((value
>> 4) & 0xF);
611 line_buffer
[hex_col
+ 1] = tohex((value
>> 0) & 0xF);
612 line_buffer
[txt_col
+ 0] = (value
>= ' ' && value
< 127)
618 trace_fn(i
& -16, line_buffer
);
622 /* return -1 if error, 0 if OK */
623 static int put_packet_binary(const char *buf
, int len
, bool dump
)
627 uint8_t *ps
= &gdbserver_state
.last_packet
[0];
629 if (dump
&& trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY
)) {
630 hexdump(buf
, len
, trace_gdbstub_io_binaryreply
);
639 for(i
= 0; i
< len
; i
++) {
643 *(p
++) = tohex((csum
>> 4) & 0xf);
644 *(p
++) = tohex((csum
) & 0xf);
646 gdbserver_state
.last_packet_len
= p
- ps
;
647 put_buffer(ps
, gdbserver_state
.last_packet_len
);
649 #ifdef CONFIG_USER_ONLY
662 /* return -1 if error, 0 if OK */
663 static int put_packet(const char *buf
)
665 trace_gdbstub_io_reply(buf
);
667 return put_packet_binary(buf
, strlen(buf
), false);
670 /* Encode data using the encoding for 'x' packets. */
671 static int memtox(char *buf
, const char *mem
, int len
)
679 case '#': case '$': case '*': case '}':
691 static uint32_t gdb_get_cpu_pid(CPUState
*cpu
)
693 /* TODO: In user mode, we should use the task state PID */
694 if (cpu
->cluster_index
== UNASSIGNED_CLUSTER_INDEX
) {
695 /* Return the default process' PID */
696 int index
= gdbserver_state
.process_num
- 1;
697 return gdbserver_state
.processes
[index
].pid
;
699 return cpu
->cluster_index
+ 1;
702 static GDBProcess
*gdb_get_process(uint32_t pid
)
707 /* 0 means any process, we take the first one */
708 return &gdbserver_state
.processes
[0];
711 for (i
= 0; i
< gdbserver_state
.process_num
; i
++) {
712 if (gdbserver_state
.processes
[i
].pid
== pid
) {
713 return &gdbserver_state
.processes
[i
];
720 static GDBProcess
*gdb_get_cpu_process(CPUState
*cpu
)
722 return gdb_get_process(gdb_get_cpu_pid(cpu
));
725 static CPUState
*find_cpu(uint32_t thread_id
)
730 if (cpu_gdb_index(cpu
) == thread_id
) {
738 static CPUState
*get_first_cpu_in_process(GDBProcess
*process
)
743 if (gdb_get_cpu_pid(cpu
) == process
->pid
) {
751 static CPUState
*gdb_next_cpu_in_process(CPUState
*cpu
)
753 uint32_t pid
= gdb_get_cpu_pid(cpu
);
757 if (gdb_get_cpu_pid(cpu
) == pid
) {
767 /* Return the cpu following @cpu, while ignoring unattached processes. */
768 static CPUState
*gdb_next_attached_cpu(CPUState
*cpu
)
773 if (gdb_get_cpu_process(cpu
)->attached
) {
783 /* Return the first attached cpu */
784 static CPUState
*gdb_first_attached_cpu(void)
786 CPUState
*cpu
= first_cpu
;
787 GDBProcess
*process
= gdb_get_cpu_process(cpu
);
789 if (!process
->attached
) {
790 return gdb_next_attached_cpu(cpu
);
796 static CPUState
*gdb_get_cpu(uint32_t pid
, uint32_t tid
)
802 /* 0 means any process/thread, we take the first attached one */
803 return gdb_first_attached_cpu();
804 } else if (pid
&& !tid
) {
805 /* any thread in a specific process */
806 process
= gdb_get_process(pid
);
808 if (process
== NULL
) {
812 if (!process
->attached
) {
816 return get_first_cpu_in_process(process
);
818 /* a specific thread */
825 process
= gdb_get_cpu_process(cpu
);
827 if (pid
&& process
->pid
!= pid
) {
831 if (!process
->attached
) {
839 static const char *get_feature_xml(const char *p
, const char **newp
,
845 CPUState
*cpu
= get_first_cpu_in_process(process
);
846 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
849 while (p
[len
] && p
[len
] != ':')
854 if (strncmp(p
, "target.xml", len
) == 0) {
855 char *buf
= process
->target_xml
;
856 const size_t buf_sz
= sizeof(process
->target_xml
);
858 /* Generate the XML description for this CPU. */
863 "<?xml version=\"1.0\"?>"
864 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
866 if (cc
->gdb_arch_name
) {
867 gchar
*arch
= cc
->gdb_arch_name(cpu
);
868 pstrcat(buf
, buf_sz
, "<architecture>");
869 pstrcat(buf
, buf_sz
, arch
);
870 pstrcat(buf
, buf_sz
, "</architecture>");
873 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
874 pstrcat(buf
, buf_sz
, cc
->gdb_core_xml_file
);
875 pstrcat(buf
, buf_sz
, "\"/>");
876 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
877 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
878 pstrcat(buf
, buf_sz
, r
->xml
);
879 pstrcat(buf
, buf_sz
, "\"/>");
881 pstrcat(buf
, buf_sz
, "</target>");
885 if (cc
->gdb_get_dynamic_xml
) {
886 char *xmlname
= g_strndup(p
, len
);
887 const char *xml
= cc
->gdb_get_dynamic_xml(cpu
, xmlname
);
895 name
= xml_builtin
[i
][0];
896 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
899 return name
? xml_builtin
[i
][1] : NULL
;
902 static int gdb_read_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_read_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
->get_reg(env
, mem_buf
, reg
- r
->base_reg
);
920 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
922 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
923 CPUArchState
*env
= cpu
->env_ptr
;
926 if (reg
< cc
->gdb_num_core_regs
) {
927 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
930 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
931 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
932 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
938 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
939 specifies the first register number and these registers are included in
940 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
941 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
944 void gdb_register_coprocessor(CPUState
*cpu
,
945 gdb_reg_cb get_reg
, gdb_reg_cb set_reg
,
946 int num_regs
, const char *xml
, int g_pos
)
949 GDBRegisterState
**p
;
953 /* Check for duplicates. */
954 if (strcmp((*p
)->xml
, xml
) == 0)
959 s
= g_new0(GDBRegisterState
, 1);
960 s
->base_reg
= cpu
->gdb_num_regs
;
961 s
->num_regs
= num_regs
;
962 s
->get_reg
= get_reg
;
963 s
->set_reg
= set_reg
;
966 /* Add to end of list. */
967 cpu
->gdb_num_regs
+= num_regs
;
970 if (g_pos
!= s
->base_reg
) {
971 error_report("Error: Bad gdb register numbering for '%s', "
972 "expected %d got %d", xml
, g_pos
, s
->base_reg
);
974 cpu
->gdb_num_g_regs
= cpu
->gdb_num_regs
;
979 #ifndef CONFIG_USER_ONLY
980 /* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
981 static inline int xlat_gdb_type(CPUState
*cpu
, int gdbtype
)
983 static const int xlat
[] = {
984 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
985 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
986 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
989 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
990 int cputype
= xlat
[gdbtype
];
992 if (cc
->gdb_stop_before_watchpoint
) {
993 cputype
|= BP_STOP_BEFORE_ACCESS
;
999 static int gdb_breakpoint_insert(int type
, target_ulong addr
, target_ulong len
)
1004 if (kvm_enabled()) {
1005 return kvm_insert_breakpoint(gdbserver_state
.c_cpu
, addr
, len
, type
);
1009 case GDB_BREAKPOINT_SW
:
1010 case GDB_BREAKPOINT_HW
:
1012 err
= cpu_breakpoint_insert(cpu
, addr
, BP_GDB
, NULL
);
1018 #ifndef CONFIG_USER_ONLY
1019 case GDB_WATCHPOINT_WRITE
:
1020 case GDB_WATCHPOINT_READ
:
1021 case GDB_WATCHPOINT_ACCESS
:
1023 err
= cpu_watchpoint_insert(cpu
, addr
, len
,
1024 xlat_gdb_type(cpu
, type
), NULL
);
1036 static int gdb_breakpoint_remove(int type
, target_ulong addr
, target_ulong len
)
1041 if (kvm_enabled()) {
1042 return kvm_remove_breakpoint(gdbserver_state
.c_cpu
, addr
, len
, type
);
1046 case GDB_BREAKPOINT_SW
:
1047 case GDB_BREAKPOINT_HW
:
1049 err
= cpu_breakpoint_remove(cpu
, addr
, BP_GDB
);
1055 #ifndef CONFIG_USER_ONLY
1056 case GDB_WATCHPOINT_WRITE
:
1057 case GDB_WATCHPOINT_READ
:
1058 case GDB_WATCHPOINT_ACCESS
:
1060 err
= cpu_watchpoint_remove(cpu
, addr
, len
,
1061 xlat_gdb_type(cpu
, type
));
1072 static inline void gdb_cpu_breakpoint_remove_all(CPUState
*cpu
)
1074 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
1075 #ifndef CONFIG_USER_ONLY
1076 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
1080 static void gdb_process_breakpoint_remove_all(GDBProcess
*p
)
1082 CPUState
*cpu
= get_first_cpu_in_process(p
);
1085 gdb_cpu_breakpoint_remove_all(cpu
);
1086 cpu
= gdb_next_cpu_in_process(cpu
);
1090 static void gdb_breakpoint_remove_all(void)
1094 if (kvm_enabled()) {
1095 kvm_remove_all_breakpoints(gdbserver_state
.c_cpu
);
1100 gdb_cpu_breakpoint_remove_all(cpu
);
1104 static void gdb_set_cpu_pc(target_ulong pc
)
1106 CPUState
*cpu
= gdbserver_state
.c_cpu
;
1108 cpu_synchronize_state(cpu
);
1109 cpu_set_pc(cpu
, pc
);
1112 static char *gdb_fmt_thread_id(CPUState
*cpu
, char *buf
, size_t buf_size
)
1114 if (gdbserver_state
.multiprocess
) {
1115 snprintf(buf
, buf_size
, "p%02x.%02x",
1116 gdb_get_cpu_pid(cpu
), cpu_gdb_index(cpu
));
1118 snprintf(buf
, buf_size
, "%02x", cpu_gdb_index(cpu
));
1124 typedef enum GDBThreadIdKind
{
1126 GDB_ALL_THREADS
, /* One process, all threads */
1131 static GDBThreadIdKind
read_thread_id(const char *buf
, const char **end_buf
,
1132 uint32_t *pid
, uint32_t *tid
)
1139 ret
= qemu_strtoul(buf
, &buf
, 16, &p
);
1142 return GDB_READ_THREAD_ERR
;
1151 ret
= qemu_strtoul(buf
, &buf
, 16, &t
);
1154 return GDB_READ_THREAD_ERR
;
1160 return GDB_ALL_PROCESSES
;
1168 return GDB_ALL_THREADS
;
1175 return GDB_ONE_THREAD
;
1179 * gdb_handle_vcont - Parses and handles a vCont packet.
1180 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1181 * a format error, 0 on success.
1183 static int gdb_handle_vcont(const char *p
)
1185 int res
, signal
= 0;
1190 GDBProcess
*process
;
1192 GDBThreadIdKind kind
;
1193 #ifdef CONFIG_USER_ONLY
1194 int max_cpus
= 1; /* global variable max_cpus exists only in system mode */
1197 max_cpus
= max_cpus
<= cpu
->cpu_index
? cpu
->cpu_index
+ 1 : max_cpus
;
1200 MachineState
*ms
= MACHINE(qdev_get_machine());
1201 unsigned int max_cpus
= ms
->smp
.max_cpus
;
1203 /* uninitialised CPUs stay 0 */
1204 newstates
= g_new0(char, max_cpus
);
1206 /* mark valid CPUs with 1 */
1208 newstates
[cpu
->cpu_index
] = 1;
1212 * res keeps track of what error we are returning, with -ENOTSUP meaning
1213 * that the command is unknown or unsupported, thus returning an empty
1214 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1215 * or incorrect parameters passed.
1225 if (cur_action
== 'C' || cur_action
== 'S') {
1226 cur_action
= qemu_tolower(cur_action
);
1227 res
= qemu_strtoul(p
+ 1, &p
, 16, &tmp
);
1231 signal
= gdb_signal_to_target(tmp
);
1232 } else if (cur_action
!= 'c' && cur_action
!= 's') {
1233 /* unknown/invalid/unsupported command */
1238 if (*p
== '\0' || *p
== ';') {
1240 * No thread specifier, action is on "all threads". The
1241 * specification is unclear regarding the process to act on. We
1242 * choose all processes.
1244 kind
= GDB_ALL_PROCESSES
;
1245 } else if (*p
++ == ':') {
1246 kind
= read_thread_id(p
, &p
, &pid
, &tid
);
1253 case GDB_READ_THREAD_ERR
:
1257 case GDB_ALL_PROCESSES
:
1258 cpu
= gdb_first_attached_cpu();
1260 if (newstates
[cpu
->cpu_index
] == 1) {
1261 newstates
[cpu
->cpu_index
] = cur_action
;
1264 cpu
= gdb_next_attached_cpu(cpu
);
1268 case GDB_ALL_THREADS
:
1269 process
= gdb_get_process(pid
);
1271 if (!process
->attached
) {
1276 cpu
= get_first_cpu_in_process(process
);
1278 if (newstates
[cpu
->cpu_index
] == 1) {
1279 newstates
[cpu
->cpu_index
] = cur_action
;
1282 cpu
= gdb_next_cpu_in_process(cpu
);
1286 case GDB_ONE_THREAD
:
1287 cpu
= gdb_get_cpu(pid
, tid
);
1289 /* invalid CPU/thread specified */
1295 /* only use if no previous match occourred */
1296 if (newstates
[cpu
->cpu_index
] == 1) {
1297 newstates
[cpu
->cpu_index
] = cur_action
;
1302 gdbserver_state
.signal
= signal
;
1303 gdb_continue_partial(newstates
);
1311 typedef union GdbCmdVariant
{
1314 unsigned long val_ul
;
1315 unsigned long long val_ull
;
1317 GDBThreadIdKind kind
;
1323 static const char *cmd_next_param(const char *param
, const char delimiter
)
1325 static const char all_delimiters
[] = ",;:=";
1326 char curr_delimiters
[2] = {0};
1327 const char *delimiters
;
1329 if (delimiter
== '?') {
1330 delimiters
= all_delimiters
;
1331 } else if (delimiter
== '0') {
1332 return strchr(param
, '\0');
1333 } else if (delimiter
== '.' && *param
) {
1336 curr_delimiters
[0] = delimiter
;
1337 delimiters
= curr_delimiters
;
1340 param
+= strcspn(param
, delimiters
);
1347 static int cmd_parse_params(const char *data
, const char *schema
,
1348 GdbCmdVariant
*params
, int *num_params
)
1351 const char *curr_schema
, *curr_data
;
1359 curr_schema
= schema
;
1362 while (curr_schema
[0] && curr_schema
[1] && *curr_data
) {
1363 switch (curr_schema
[0]) {
1365 if (qemu_strtoul(curr_data
, &curr_data
, 16,
1366 ¶ms
[curr_param
].val_ul
)) {
1370 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1373 if (qemu_strtou64(curr_data
, &curr_data
, 16,
1374 (uint64_t *)¶ms
[curr_param
].val_ull
)) {
1378 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1381 params
[curr_param
].data
= curr_data
;
1383 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1386 params
[curr_param
].opcode
= *(uint8_t *)curr_data
;
1388 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1391 params
[curr_param
].thread_id
.kind
=
1392 read_thread_id(curr_data
, &curr_data
,
1393 ¶ms
[curr_param
].thread_id
.pid
,
1394 ¶ms
[curr_param
].thread_id
.tid
);
1396 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1399 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1407 *num_params
= curr_param
;
1411 typedef struct GdbCmdContext
{
1412 GdbCmdVariant
*params
;
1414 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1415 char str_buf
[MAX_PACKET_LENGTH
+ 1];
1418 typedef void (*GdbCmdHandler
)(GdbCmdContext
*gdb_ctx
, void *user_ctx
);
1421 * cmd_startswith -> cmd is compared using startswith
1424 * schema definitions:
1425 * Each schema parameter entry consists of 2 chars,
1426 * the first char represents the parameter type handling
1427 * the second char represents the delimiter for the next parameter
1429 * Currently supported schema types:
1430 * 'l' -> unsigned long (stored in .val_ul)
1431 * 'L' -> unsigned long long (stored in .val_ull)
1432 * 's' -> string (stored in .data)
1433 * 'o' -> single char (stored in .opcode)
1434 * 't' -> thread id (stored in .thread_id)
1435 * '?' -> skip according to delimiter
1437 * Currently supported delimiters:
1438 * '?' -> Stop at any delimiter (",;:=\0")
1439 * '0' -> Stop at "\0"
1440 * '.' -> Skip 1 char unless reached "\0"
1441 * Any other value is treated as the delimiter value itself
1443 typedef struct GdbCmdParseEntry
{
1444 GdbCmdHandler handler
;
1446 bool cmd_startswith
;
1450 static inline int startswith(const char *string
, const char *pattern
)
1452 return !strncmp(string
, pattern
, strlen(pattern
));
1455 static int process_string_cmd(void *user_ctx
, const char *data
,
1456 const GdbCmdParseEntry
*cmds
, int num_cmds
)
1458 int i
, schema_len
, max_num_params
= 0;
1459 GdbCmdContext gdb_ctx
;
1465 for (i
= 0; i
< num_cmds
; i
++) {
1466 const GdbCmdParseEntry
*cmd
= &cmds
[i
];
1467 g_assert(cmd
->handler
&& cmd
->cmd
);
1469 if ((cmd
->cmd_startswith
&& !startswith(data
, cmd
->cmd
)) ||
1470 (!cmd
->cmd_startswith
&& strcmp(cmd
->cmd
, data
))) {
1475 schema_len
= strlen(cmd
->schema
);
1476 if (schema_len
% 2) {
1480 max_num_params
= schema_len
/ 2;
1484 (GdbCmdVariant
*)alloca(sizeof(*gdb_ctx
.params
) * max_num_params
);
1485 memset(gdb_ctx
.params
, 0, sizeof(*gdb_ctx
.params
) * max_num_params
);
1487 if (cmd_parse_params(&data
[strlen(cmd
->cmd
)], cmd
->schema
,
1488 gdb_ctx
.params
, &gdb_ctx
.num_params
)) {
1492 cmd
->handler(&gdb_ctx
, user_ctx
);
1499 static void run_cmd_parser(const char *data
, const GdbCmdParseEntry
*cmd
)
1505 /* In case there was an error during the command parsing we must
1506 * send a NULL packet to indicate the command is not supported */
1507 if (process_string_cmd(NULL
, data
, cmd
, 1)) {
1512 static void handle_detach(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1514 GDBProcess
*process
;
1517 if (gdbserver_state
.multiprocess
) {
1518 if (!gdb_ctx
->num_params
) {
1523 pid
= gdb_ctx
->params
[0].val_ul
;
1526 process
= gdb_get_process(pid
);
1527 gdb_process_breakpoint_remove_all(process
);
1528 process
->attached
= false;
1530 if (pid
== gdb_get_cpu_pid(gdbserver_state
.c_cpu
)) {
1531 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
1534 if (pid
== gdb_get_cpu_pid(gdbserver_state
.g_cpu
)) {
1535 gdbserver_state
.g_cpu
= gdb_first_attached_cpu();
1538 if (!gdbserver_state
.c_cpu
) {
1539 /* No more process attached */
1540 gdb_syscall_mode
= GDB_SYS_DISABLED
;
1546 static void handle_thread_alive(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1550 if (!gdb_ctx
->num_params
) {
1555 if (gdb_ctx
->params
[0].thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1560 cpu
= gdb_get_cpu(gdb_ctx
->params
[0].thread_id
.pid
,
1561 gdb_ctx
->params
[0].thread_id
.tid
);
1570 static void handle_continue(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1572 if (gdb_ctx
->num_params
) {
1573 gdb_set_cpu_pc(gdb_ctx
->params
[0].val_ull
);
1576 gdbserver_state
.signal
= 0;
1580 static void handle_cont_with_sig(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1582 unsigned long signal
= 0;
1585 * Note: C sig;[addr] is currently unsupported and we simply
1586 * omit the addr parameter
1588 if (gdb_ctx
->num_params
) {
1589 signal
= gdb_ctx
->params
[0].val_ul
;
1592 gdbserver_state
.signal
= gdb_signal_to_target(signal
);
1593 if (gdbserver_state
.signal
== -1) {
1594 gdbserver_state
.signal
= 0;
1599 static void handle_set_thread(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1603 if (gdb_ctx
->num_params
!= 2) {
1608 if (gdb_ctx
->params
[1].thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1613 if (gdb_ctx
->params
[1].thread_id
.kind
!= GDB_ONE_THREAD
) {
1618 cpu
= gdb_get_cpu(gdb_ctx
->params
[1].thread_id
.pid
,
1619 gdb_ctx
->params
[1].thread_id
.tid
);
1626 * Note: This command is deprecated and modern gdb's will be using the
1627 * vCont command instead.
1629 switch (gdb_ctx
->params
[0].opcode
) {
1631 gdbserver_state
.c_cpu
= cpu
;
1635 gdbserver_state
.g_cpu
= cpu
;
1644 static void handle_insert_bp(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1648 if (gdb_ctx
->num_params
!= 3) {
1653 res
= gdb_breakpoint_insert(gdb_ctx
->params
[0].val_ul
,
1654 gdb_ctx
->params
[1].val_ull
,
1655 gdb_ctx
->params
[2].val_ull
);
1659 } else if (res
== -ENOSYS
) {
1667 static void handle_remove_bp(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1671 if (gdb_ctx
->num_params
!= 3) {
1676 res
= gdb_breakpoint_remove(gdb_ctx
->params
[0].val_ul
,
1677 gdb_ctx
->params
[1].val_ull
,
1678 gdb_ctx
->params
[2].val_ull
);
1682 } else if (res
== -ENOSYS
) {
1691 * handle_set/get_reg
1693 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1694 * This works, but can be very slow. Anything new enough to understand
1695 * XML also knows how to use this properly. However to use this we
1696 * need to define a local XML file as well as be talking to a
1697 * reasonably modern gdb. Responding with an empty packet will cause
1698 * the remote gdb to fallback to older methods.
1701 static void handle_set_reg(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1710 if (gdb_ctx
->num_params
!= 2) {
1715 reg_size
= strlen(gdb_ctx
->params
[1].data
) / 2;
1716 hextomem(gdb_ctx
->mem_buf
, gdb_ctx
->params
[1].data
, reg_size
);
1717 gdb_write_register(gdbserver_state
.g_cpu
, gdb_ctx
->mem_buf
,
1718 gdb_ctx
->params
[0].val_ull
);
1722 static void handle_get_reg(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1731 if (!gdb_ctx
->num_params
) {
1736 reg_size
= gdb_read_register(gdbserver_state
.g_cpu
, gdb_ctx
->mem_buf
,
1737 gdb_ctx
->params
[0].val_ull
);
1743 memtohex(gdb_ctx
->str_buf
, gdb_ctx
->mem_buf
, reg_size
);
1744 put_packet(gdb_ctx
->str_buf
);
1747 static void handle_write_mem(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1749 if (gdb_ctx
->num_params
!= 3) {
1754 /* hextomem() reads 2*len bytes */
1755 if (gdb_ctx
->params
[1].val_ull
> strlen(gdb_ctx
->params
[2].data
) / 2) {
1760 hextomem(gdb_ctx
->mem_buf
, gdb_ctx
->params
[2].data
,
1761 gdb_ctx
->params
[1].val_ull
);
1762 if (target_memory_rw_debug(gdbserver_state
.g_cpu
, gdb_ctx
->params
[0].val_ull
,
1764 gdb_ctx
->params
[1].val_ull
, true)) {
1772 static void handle_read_mem(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1774 if (gdb_ctx
->num_params
!= 2) {
1779 /* memtohex() doubles the required space */
1780 if (gdb_ctx
->params
[1].val_ull
> MAX_PACKET_LENGTH
/ 2) {
1785 if (target_memory_rw_debug(gdbserver_state
.g_cpu
, gdb_ctx
->params
[0].val_ull
,
1787 gdb_ctx
->params
[1].val_ull
, false)) {
1792 memtohex(gdb_ctx
->str_buf
, gdb_ctx
->mem_buf
, gdb_ctx
->params
[1].val_ull
);
1793 put_packet(gdb_ctx
->str_buf
);
1796 static void handle_write_all_regs(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1798 target_ulong addr
, len
;
1802 if (!gdb_ctx
->num_params
) {
1806 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1807 registers
= gdb_ctx
->mem_buf
;
1808 len
= strlen(gdb_ctx
->params
[0].data
) / 2;
1809 hextomem(registers
, gdb_ctx
->params
[0].data
, len
);
1810 for (addr
= 0; addr
< gdbserver_state
.g_cpu
->gdb_num_g_regs
&& len
> 0;
1812 reg_size
= gdb_write_register(gdbserver_state
.g_cpu
, registers
, addr
);
1814 registers
+= reg_size
;
1819 static void handle_read_all_regs(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1821 target_ulong addr
, len
;
1823 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1825 for (addr
= 0; addr
< gdbserver_state
.g_cpu
->gdb_num_g_regs
; addr
++) {
1826 len
+= gdb_read_register(gdbserver_state
.g_cpu
, gdb_ctx
->mem_buf
+ len
,
1830 memtohex(gdb_ctx
->str_buf
, gdb_ctx
->mem_buf
, len
);
1831 put_packet(gdb_ctx
->str_buf
);
1834 static void handle_file_io(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1836 if (gdb_ctx
->num_params
>= 1 && gdbserver_state
.current_syscall_cb
) {
1837 target_ulong ret
, err
;
1839 ret
= (target_ulong
)gdb_ctx
->params
[0].val_ull
;
1840 if (gdb_ctx
->num_params
>= 2) {
1841 err
= (target_ulong
)gdb_ctx
->params
[1].val_ull
;
1845 gdbserver_state
.current_syscall_cb(gdbserver_state
.c_cpu
, ret
, err
);
1846 gdbserver_state
.current_syscall_cb
= NULL
;
1849 if (gdb_ctx
->num_params
>= 3 && gdb_ctx
->params
[2].opcode
== (uint8_t)'C') {
1857 static void handle_step(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1859 if (gdb_ctx
->num_params
) {
1860 gdb_set_cpu_pc((target_ulong
)gdb_ctx
->params
[0].val_ull
);
1863 cpu_single_step(gdbserver_state
.c_cpu
, sstep_flags
);
1867 static void handle_v_cont_query(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1869 put_packet("vCont;c;C;s;S");
1872 static void handle_v_cont(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1876 if (!gdb_ctx
->num_params
) {
1880 res
= gdb_handle_vcont(gdb_ctx
->params
[0].data
);
1881 if ((res
== -EINVAL
) || (res
== -ERANGE
)) {
1888 static void handle_v_attach(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1890 GDBProcess
*process
;
1894 pstrcpy(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "E22");
1895 if (!gdb_ctx
->num_params
) {
1899 process
= gdb_get_process(gdb_ctx
->params
[0].val_ul
);
1904 cpu
= get_first_cpu_in_process(process
);
1909 process
->attached
= true;
1910 gdbserver_state
.g_cpu
= cpu
;
1911 gdbserver_state
.c_cpu
= cpu
;
1913 gdb_fmt_thread_id(cpu
, thread_id
, sizeof(thread_id
));
1914 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "T%02xthread:%s;",
1915 GDB_SIGNAL_TRAP
, thread_id
);
1917 put_packet(gdb_ctx
->str_buf
);
1920 static void handle_v_kill(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1922 /* Kill the target */
1924 error_report("QEMU: Terminated via GDBstub");
1928 static GdbCmdParseEntry gdb_v_commands_table
[] = {
1929 /* Order is important if has same prefix */
1931 .handler
= handle_v_cont_query
,
1936 .handler
= handle_v_cont
,
1938 .cmd_startswith
= 1,
1942 .handler
= handle_v_attach
,
1944 .cmd_startswith
= 1,
1948 .handler
= handle_v_kill
,
1954 static void handle_v_commands(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1956 if (!gdb_ctx
->num_params
) {
1960 if (process_string_cmd(NULL
, gdb_ctx
->params
[0].data
,
1961 gdb_v_commands_table
,
1962 ARRAY_SIZE(gdb_v_commands_table
))) {
1967 static void handle_query_qemu_sstepbits(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1969 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
),
1970 "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", SSTEP_ENABLE
,
1971 SSTEP_NOIRQ
, SSTEP_NOTIMER
);
1972 put_packet(gdb_ctx
->str_buf
);
1975 static void handle_set_qemu_sstep(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1977 if (!gdb_ctx
->num_params
) {
1981 sstep_flags
= gdb_ctx
->params
[0].val_ul
;
1985 static void handle_query_qemu_sstep(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1987 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "0x%x", sstep_flags
);
1988 put_packet(gdb_ctx
->str_buf
);
1991 static void handle_query_curr_tid(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1994 GDBProcess
*process
;
1998 * "Current thread" remains vague in the spec, so always return
1999 * the first thread of the current process (gdb returns the
2002 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
2003 cpu
= get_first_cpu_in_process(process
);
2004 gdb_fmt_thread_id(cpu
, thread_id
, sizeof(thread_id
));
2005 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "QC%s", thread_id
);
2006 put_packet(gdb_ctx
->str_buf
);
2009 static void handle_query_threads(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2013 if (!gdbserver_state
.query_cpu
) {
2018 gdb_fmt_thread_id(gdbserver_state
.query_cpu
, thread_id
,
2020 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "m%s", thread_id
);
2021 put_packet(gdb_ctx
->str_buf
);
2022 gdbserver_state
.query_cpu
= gdb_next_attached_cpu(gdbserver_state
.query_cpu
);
2025 static void handle_query_first_threads(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2027 gdbserver_state
.query_cpu
= gdb_first_attached_cpu();
2028 handle_query_threads(gdb_ctx
, user_ctx
);
2031 static void handle_query_thread_extra(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2036 if (!gdb_ctx
->num_params
||
2037 gdb_ctx
->params
[0].thread_id
.kind
== GDB_READ_THREAD_ERR
) {
2042 cpu
= gdb_get_cpu(gdb_ctx
->params
[0].thread_id
.pid
,
2043 gdb_ctx
->params
[0].thread_id
.tid
);
2048 cpu_synchronize_state(cpu
);
2050 if (gdbserver_state
.multiprocess
&& (gdbserver_state
.process_num
> 1)) {
2051 /* Print the CPU model and name in multiprocess mode */
2052 ObjectClass
*oc
= object_get_class(OBJECT(cpu
));
2053 const char *cpu_model
= object_class_get_name(oc
);
2054 char *cpu_name
= object_get_canonical_path_component(OBJECT(cpu
));
2055 len
= snprintf((char *)gdb_ctx
->mem_buf
, sizeof(gdb_ctx
->str_buf
) / 2,
2056 "%s %s [%s]", cpu_model
, cpu_name
,
2057 cpu
->halted
? "halted " : "running");
2060 /* memtohex() doubles the required space */
2061 len
= snprintf((char *)gdb_ctx
->mem_buf
, sizeof(gdb_ctx
->str_buf
) / 2,
2062 "CPU#%d [%s]", cpu
->cpu_index
,
2063 cpu
->halted
? "halted " : "running");
2065 trace_gdbstub_op_extra_info((char *)gdb_ctx
->mem_buf
);
2066 memtohex(gdb_ctx
->str_buf
, gdb_ctx
->mem_buf
, len
);
2067 put_packet(gdb_ctx
->str_buf
);
2070 #ifdef CONFIG_USER_ONLY
2071 static void handle_query_offsets(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2075 ts
= gdbserver_state
.c_cpu
->opaque
;
2076 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
),
2077 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
2078 ";Bss=" TARGET_ABI_FMT_lx
,
2079 ts
->info
->code_offset
,
2080 ts
->info
->data_offset
,
2081 ts
->info
->data_offset
);
2082 put_packet(gdb_ctx
->str_buf
);
2085 static void handle_query_rcmd(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2089 if (!gdb_ctx
->num_params
) {
2094 len
= strlen(gdb_ctx
->params
[0].data
);
2101 hextomem(gdb_ctx
->mem_buf
, gdb_ctx
->params
[0].data
, len
);
2102 gdb_ctx
->mem_buf
[len
++] = 0;
2103 qemu_chr_be_write(gdbserver_state
.mon_chr
, gdb_ctx
->mem_buf
, len
);
2109 static void handle_query_supported(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2113 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "PacketSize=%x",
2115 cc
= CPU_GET_CLASS(first_cpu
);
2116 if (cc
->gdb_core_xml_file
) {
2117 pstrcat(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
),
2118 ";qXfer:features:read+");
2121 if (gdb_ctx
->num_params
&&
2122 strstr(gdb_ctx
->params
[0].data
, "multiprocess+")) {
2123 gdbserver_state
.multiprocess
= true;
2126 pstrcat(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), ";multiprocess+");
2127 put_packet(gdb_ctx
->str_buf
);
2130 static void handle_query_xfer_features(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2132 GDBProcess
*process
;
2134 unsigned long len
, total_len
, addr
;
2138 if (gdb_ctx
->num_params
< 3) {
2143 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
2144 cc
= CPU_GET_CLASS(gdbserver_state
.g_cpu
);
2145 if (!cc
->gdb_core_xml_file
) {
2151 p
= gdb_ctx
->params
[0].data
;
2152 xml
= get_feature_xml(p
, &p
, process
);
2158 addr
= gdb_ctx
->params
[1].val_ul
;
2159 len
= gdb_ctx
->params
[2].val_ul
;
2160 total_len
= strlen(xml
);
2161 if (addr
> total_len
) {
2166 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
2167 len
= (MAX_PACKET_LENGTH
- 5) / 2;
2170 if (len
< total_len
- addr
) {
2171 gdb_ctx
->str_buf
[0] = 'm';
2172 len
= memtox(gdb_ctx
->str_buf
+ 1, xml
+ addr
, len
);
2174 gdb_ctx
->str_buf
[0] = 'l';
2175 len
= memtox(gdb_ctx
->str_buf
+ 1, xml
+ addr
, total_len
- addr
);
2178 put_packet_binary(gdb_ctx
->str_buf
, len
+ 1, true);
2181 static void handle_query_attached(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2183 put_packet(GDB_ATTACHED
);
2186 static void handle_query_qemu_supported(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2188 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "sstepbits;sstep");
2189 #ifndef CONFIG_USER_ONLY
2190 pstrcat(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), ";PhyMemMode");
2192 put_packet(gdb_ctx
->str_buf
);
2195 #ifndef CONFIG_USER_ONLY
2196 static void handle_query_qemu_phy_mem_mode(GdbCmdContext
*gdb_ctx
,
2199 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "%d", phy_memory_mode
);
2200 put_packet(gdb_ctx
->str_buf
);
2203 static void handle_set_qemu_phy_mem_mode(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2205 if (!gdb_ctx
->num_params
) {
2210 if (!gdb_ctx
->params
[0].val_ul
) {
2211 phy_memory_mode
= 0;
2213 phy_memory_mode
= 1;
2219 static GdbCmdParseEntry gdb_gen_query_set_common_table
[] = {
2220 /* Order is important if has same prefix */
2222 .handler
= handle_query_qemu_sstepbits
,
2223 .cmd
= "qemu.sstepbits",
2226 .handler
= handle_query_qemu_sstep
,
2227 .cmd
= "qemu.sstep",
2230 .handler
= handle_set_qemu_sstep
,
2231 .cmd
= "qemu.sstep=",
2232 .cmd_startswith
= 1,
2237 static GdbCmdParseEntry gdb_gen_query_table
[] = {
2239 .handler
= handle_query_curr_tid
,
2243 .handler
= handle_query_threads
,
2244 .cmd
= "sThreadInfo",
2247 .handler
= handle_query_first_threads
,
2248 .cmd
= "fThreadInfo",
2251 .handler
= handle_query_thread_extra
,
2252 .cmd
= "ThreadExtraInfo,",
2253 .cmd_startswith
= 1,
2256 #ifdef CONFIG_USER_ONLY
2258 .handler
= handle_query_offsets
,
2263 .handler
= handle_query_rcmd
,
2265 .cmd_startswith
= 1,
2270 .handler
= handle_query_supported
,
2271 .cmd
= "Supported:",
2272 .cmd_startswith
= 1,
2276 .handler
= handle_query_supported
,
2281 .handler
= handle_query_xfer_features
,
2282 .cmd
= "Xfer:features:read:",
2283 .cmd_startswith
= 1,
2287 .handler
= handle_query_attached
,
2292 .handler
= handle_query_attached
,
2296 .handler
= handle_query_qemu_supported
,
2297 .cmd
= "qemu.Supported",
2299 #ifndef CONFIG_USER_ONLY
2301 .handler
= handle_query_qemu_phy_mem_mode
,
2302 .cmd
= "qemu.PhyMemMode",
2307 static GdbCmdParseEntry gdb_gen_set_table
[] = {
2308 /* Order is important if has same prefix */
2310 .handler
= handle_set_qemu_sstep
,
2311 .cmd
= "qemu.sstep:",
2312 .cmd_startswith
= 1,
2315 #ifndef CONFIG_USER_ONLY
2317 .handler
= handle_set_qemu_phy_mem_mode
,
2318 .cmd
= "qemu.PhyMemMode:",
2319 .cmd_startswith
= 1,
2325 static void handle_gen_query(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2327 if (!gdb_ctx
->num_params
) {
2331 if (!process_string_cmd(NULL
, gdb_ctx
->params
[0].data
,
2332 gdb_gen_query_set_common_table
,
2333 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
2337 if (process_string_cmd(NULL
, gdb_ctx
->params
[0].data
,
2338 gdb_gen_query_table
,
2339 ARRAY_SIZE(gdb_gen_query_table
))) {
2344 static void handle_gen_set(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2346 if (!gdb_ctx
->num_params
) {
2350 if (!process_string_cmd(NULL
, gdb_ctx
->params
[0].data
,
2351 gdb_gen_query_set_common_table
,
2352 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
2356 if (process_string_cmd(NULL
, gdb_ctx
->params
[0].data
,
2358 ARRAY_SIZE(gdb_gen_set_table
))) {
2363 static void handle_target_halt(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2367 gdb_fmt_thread_id(gdbserver_state
.c_cpu
, thread_id
,
2369 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "T%02xthread:%s;",
2370 GDB_SIGNAL_TRAP
, thread_id
);
2371 put_packet(gdb_ctx
->str_buf
);
2373 * Remove all the breakpoints when this query is issued,
2374 * because gdb is doing an initial connect and the state
2375 * should be cleaned up.
2377 gdb_breakpoint_remove_all();
2380 static int gdb_handle_packet(const char *line_buf
)
2382 const GdbCmdParseEntry
*cmd_parser
= NULL
;
2384 trace_gdbstub_io_command(line_buf
);
2386 switch (line_buf
[0]) {
2392 static const GdbCmdParseEntry target_halted_cmd_desc
= {
2393 .handler
= handle_target_halt
,
2397 cmd_parser
= &target_halted_cmd_desc
;
2402 static const GdbCmdParseEntry continue_cmd_desc
= {
2403 .handler
= handle_continue
,
2405 .cmd_startswith
= 1,
2408 cmd_parser
= &continue_cmd_desc
;
2413 static const GdbCmdParseEntry cont_with_sig_cmd_desc
= {
2414 .handler
= handle_cont_with_sig
,
2416 .cmd_startswith
= 1,
2419 cmd_parser
= &cont_with_sig_cmd_desc
;
2424 static const GdbCmdParseEntry v_cmd_desc
= {
2425 .handler
= handle_v_commands
,
2427 .cmd_startswith
= 1,
2430 cmd_parser
= &v_cmd_desc
;
2434 /* Kill the target */
2435 error_report("QEMU: Terminated via GDBstub");
2439 static const GdbCmdParseEntry detach_cmd_desc
= {
2440 .handler
= handle_detach
,
2442 .cmd_startswith
= 1,
2445 cmd_parser
= &detach_cmd_desc
;
2450 static const GdbCmdParseEntry step_cmd_desc
= {
2451 .handler
= handle_step
,
2453 .cmd_startswith
= 1,
2456 cmd_parser
= &step_cmd_desc
;
2461 static const GdbCmdParseEntry file_io_cmd_desc
= {
2462 .handler
= handle_file_io
,
2464 .cmd_startswith
= 1,
2467 cmd_parser
= &file_io_cmd_desc
;
2472 static const GdbCmdParseEntry read_all_regs_cmd_desc
= {
2473 .handler
= handle_read_all_regs
,
2477 cmd_parser
= &read_all_regs_cmd_desc
;
2482 static const GdbCmdParseEntry write_all_regs_cmd_desc
= {
2483 .handler
= handle_write_all_regs
,
2485 .cmd_startswith
= 1,
2488 cmd_parser
= &write_all_regs_cmd_desc
;
2493 static const GdbCmdParseEntry read_mem_cmd_desc
= {
2494 .handler
= handle_read_mem
,
2496 .cmd_startswith
= 1,
2499 cmd_parser
= &read_mem_cmd_desc
;
2504 static const GdbCmdParseEntry write_mem_cmd_desc
= {
2505 .handler
= handle_write_mem
,
2507 .cmd_startswith
= 1,
2510 cmd_parser
= &write_mem_cmd_desc
;
2515 static const GdbCmdParseEntry get_reg_cmd_desc
= {
2516 .handler
= handle_get_reg
,
2518 .cmd_startswith
= 1,
2521 cmd_parser
= &get_reg_cmd_desc
;
2526 static const GdbCmdParseEntry set_reg_cmd_desc
= {
2527 .handler
= handle_set_reg
,
2529 .cmd_startswith
= 1,
2532 cmd_parser
= &set_reg_cmd_desc
;
2537 static const GdbCmdParseEntry insert_bp_cmd_desc
= {
2538 .handler
= handle_insert_bp
,
2540 .cmd_startswith
= 1,
2543 cmd_parser
= &insert_bp_cmd_desc
;
2548 static const GdbCmdParseEntry remove_bp_cmd_desc
= {
2549 .handler
= handle_remove_bp
,
2551 .cmd_startswith
= 1,
2554 cmd_parser
= &remove_bp_cmd_desc
;
2559 static const GdbCmdParseEntry set_thread_cmd_desc
= {
2560 .handler
= handle_set_thread
,
2562 .cmd_startswith
= 1,
2565 cmd_parser
= &set_thread_cmd_desc
;
2570 static const GdbCmdParseEntry thread_alive_cmd_desc
= {
2571 .handler
= handle_thread_alive
,
2573 .cmd_startswith
= 1,
2576 cmd_parser
= &thread_alive_cmd_desc
;
2581 static const GdbCmdParseEntry gen_query_cmd_desc
= {
2582 .handler
= handle_gen_query
,
2584 .cmd_startswith
= 1,
2587 cmd_parser
= &gen_query_cmd_desc
;
2592 static const GdbCmdParseEntry gen_set_cmd_desc
= {
2593 .handler
= handle_gen_set
,
2595 .cmd_startswith
= 1,
2598 cmd_parser
= &gen_set_cmd_desc
;
2602 /* put empty packet */
2608 run_cmd_parser(line_buf
, cmd_parser
);
2614 void gdb_set_stop_cpu(CPUState
*cpu
)
2616 GDBProcess
*p
= gdb_get_cpu_process(cpu
);
2620 * Having a stop CPU corresponding to a process that is not attached
2621 * confuses GDB. So we ignore the request.
2626 gdbserver_state
.c_cpu
= cpu
;
2627 gdbserver_state
.g_cpu
= cpu
;
2630 #ifndef CONFIG_USER_ONLY
2631 static void gdb_vm_state_change(void *opaque
, int running
, RunState state
)
2633 CPUState
*cpu
= gdbserver_state
.c_cpu
;
2639 if (running
|| gdbserver_state
.state
== RS_INACTIVE
) {
2642 /* Is there a GDB syscall waiting to be sent? */
2643 if (gdbserver_state
.current_syscall_cb
) {
2644 put_packet(gdbserver_state
.syscall_buf
);
2649 /* No process attached */
2653 gdb_fmt_thread_id(cpu
, thread_id
, sizeof(thread_id
));
2656 case RUN_STATE_DEBUG
:
2657 if (cpu
->watchpoint_hit
) {
2658 switch (cpu
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
2669 trace_gdbstub_hit_watchpoint(type
, cpu_gdb_index(cpu
),
2670 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
2671 snprintf(buf
, sizeof(buf
),
2672 "T%02xthread:%s;%swatch:" TARGET_FMT_lx
";",
2673 GDB_SIGNAL_TRAP
, thread_id
, type
,
2674 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
2675 cpu
->watchpoint_hit
= NULL
;
2678 trace_gdbstub_hit_break();
2681 ret
= GDB_SIGNAL_TRAP
;
2683 case RUN_STATE_PAUSED
:
2684 trace_gdbstub_hit_paused();
2685 ret
= GDB_SIGNAL_INT
;
2687 case RUN_STATE_SHUTDOWN
:
2688 trace_gdbstub_hit_shutdown();
2689 ret
= GDB_SIGNAL_QUIT
;
2691 case RUN_STATE_IO_ERROR
:
2692 trace_gdbstub_hit_io_error();
2693 ret
= GDB_SIGNAL_IO
;
2695 case RUN_STATE_WATCHDOG
:
2696 trace_gdbstub_hit_watchdog();
2697 ret
= GDB_SIGNAL_ALRM
;
2699 case RUN_STATE_INTERNAL_ERROR
:
2700 trace_gdbstub_hit_internal_error();
2701 ret
= GDB_SIGNAL_ABRT
;
2703 case RUN_STATE_SAVE_VM
:
2704 case RUN_STATE_RESTORE_VM
:
2706 case RUN_STATE_FINISH_MIGRATE
:
2707 ret
= GDB_SIGNAL_XCPU
;
2710 trace_gdbstub_hit_unknown(state
);
2711 ret
= GDB_SIGNAL_UNKNOWN
;
2714 gdb_set_stop_cpu(cpu
);
2715 snprintf(buf
, sizeof(buf
), "T%02xthread:%s;", ret
, thread_id
);
2720 /* disable single step if it was enabled */
2721 cpu_single_step(cpu
, 0);
2725 /* Send a gdb syscall request.
2726 This accepts limited printf-style format specifiers, specifically:
2727 %x - target_ulong argument printed in hex.
2728 %lx - 64-bit argument printed in hex.
2729 %s - string pointer (target_ulong) and length (int) pair. */
2730 void gdb_do_syscallv(gdb_syscall_complete_cb cb
, const char *fmt
, va_list va
)
2737 if (!gdbserver_state
.init
) {
2741 gdbserver_state
.current_syscall_cb
= cb
;
2742 #ifndef CONFIG_USER_ONLY
2743 vm_stop(RUN_STATE_DEBUG
);
2745 p
= &gdbserver_state
.syscall_buf
[0];
2746 p_end
= &gdbserver_state
.syscall_buf
[sizeof(gdbserver_state
.syscall_buf
)];
2753 addr
= va_arg(va
, target_ulong
);
2754 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
, addr
);
2757 if (*(fmt
++) != 'x')
2759 i64
= va_arg(va
, uint64_t);
2760 p
+= snprintf(p
, p_end
- p
, "%" PRIx64
, i64
);
2763 addr
= va_arg(va
, target_ulong
);
2764 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
"/%x",
2765 addr
, va_arg(va
, int));
2769 error_report("gdbstub: Bad syscall format string '%s'",
2778 #ifdef CONFIG_USER_ONLY
2779 put_packet(gdbserver_state
.syscall_buf
);
2780 /* Return control to gdb for it to process the syscall request.
2781 * Since the protocol requires that gdb hands control back to us
2782 * using a "here are the results" F packet, we don't need to check
2783 * gdb_handlesig's return value (which is the signal to deliver if
2784 * execution was resumed via a continue packet).
2786 gdb_handlesig(gdbserver_state
.c_cpu
, 0);
2788 /* In this case wait to send the syscall packet until notification that
2789 the CPU has stopped. This must be done because if the packet is sent
2790 now the reply from the syscall request could be received while the CPU
2791 is still in the running state, which can cause packets to be dropped
2792 and state transition 'T' packets to be sent while the syscall is still
2794 qemu_cpu_kick(gdbserver_state
.c_cpu
);
2798 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
2803 gdb_do_syscallv(cb
, fmt
, va
);
2807 static void gdb_read_byte(uint8_t ch
)
2811 #ifndef CONFIG_USER_ONLY
2812 if (gdbserver_state
.last_packet_len
) {
2813 /* Waiting for a response to the last packet. If we see the start
2814 of a new command then abandon the previous response. */
2816 trace_gdbstub_err_got_nack();
2817 put_buffer((uint8_t *)gdbserver_state
.last_packet
, gdbserver_state
.last_packet_len
);
2818 } else if (ch
== '+') {
2819 trace_gdbstub_io_got_ack();
2821 trace_gdbstub_io_got_unexpected(ch
);
2824 if (ch
== '+' || ch
== '$')
2825 gdbserver_state
.last_packet_len
= 0;
2829 if (runstate_is_running()) {
2830 /* when the CPU is running, we cannot do anything except stop
2831 it when receiving a char */
2832 vm_stop(RUN_STATE_PAUSED
);
2836 switch(gdbserver_state
.state
) {
2839 /* start of command packet */
2840 gdbserver_state
.line_buf_index
= 0;
2841 gdbserver_state
.line_sum
= 0;
2842 gdbserver_state
.state
= RS_GETLINE
;
2844 trace_gdbstub_err_garbage(ch
);
2849 /* start escape sequence */
2850 gdbserver_state
.state
= RS_GETLINE_ESC
;
2851 gdbserver_state
.line_sum
+= ch
;
2852 } else if (ch
== '*') {
2853 /* start run length encoding sequence */
2854 gdbserver_state
.state
= RS_GETLINE_RLE
;
2855 gdbserver_state
.line_sum
+= ch
;
2856 } else if (ch
== '#') {
2857 /* end of command, start of checksum*/
2858 gdbserver_state
.state
= RS_CHKSUM1
;
2859 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2860 trace_gdbstub_err_overrun();
2861 gdbserver_state
.state
= RS_IDLE
;
2863 /* unescaped command character */
2864 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
;
2865 gdbserver_state
.line_sum
+= ch
;
2868 case RS_GETLINE_ESC
:
2870 /* unexpected end of command in escape sequence */
2871 gdbserver_state
.state
= RS_CHKSUM1
;
2872 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2873 /* command buffer overrun */
2874 trace_gdbstub_err_overrun();
2875 gdbserver_state
.state
= RS_IDLE
;
2877 /* parse escaped character and leave escape state */
2878 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
^ 0x20;
2879 gdbserver_state
.line_sum
+= ch
;
2880 gdbserver_state
.state
= RS_GETLINE
;
2883 case RS_GETLINE_RLE
:
2885 * Run-length encoding is explained in "Debugging with GDB /
2886 * Appendix E GDB Remote Serial Protocol / Overview".
2888 if (ch
< ' ' || ch
== '#' || ch
== '$' || ch
> 126) {
2889 /* invalid RLE count encoding */
2890 trace_gdbstub_err_invalid_repeat(ch
);
2891 gdbserver_state
.state
= RS_GETLINE
;
2893 /* decode repeat length */
2894 int repeat
= ch
- ' ' + 3;
2895 if (gdbserver_state
.line_buf_index
+ repeat
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2896 /* that many repeats would overrun the command buffer */
2897 trace_gdbstub_err_overrun();
2898 gdbserver_state
.state
= RS_IDLE
;
2899 } else if (gdbserver_state
.line_buf_index
< 1) {
2900 /* got a repeat but we have nothing to repeat */
2901 trace_gdbstub_err_invalid_rle();
2902 gdbserver_state
.state
= RS_GETLINE
;
2904 /* repeat the last character */
2905 memset(gdbserver_state
.line_buf
+ gdbserver_state
.line_buf_index
,
2906 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
- 1], repeat
);
2907 gdbserver_state
.line_buf_index
+= repeat
;
2908 gdbserver_state
.line_sum
+= ch
;
2909 gdbserver_state
.state
= RS_GETLINE
;
2914 /* get high hex digit of checksum */
2915 if (!isxdigit(ch
)) {
2916 trace_gdbstub_err_checksum_invalid(ch
);
2917 gdbserver_state
.state
= RS_GETLINE
;
2920 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
] = '\0';
2921 gdbserver_state
.line_csum
= fromhex(ch
) << 4;
2922 gdbserver_state
.state
= RS_CHKSUM2
;
2925 /* get low hex digit of checksum */
2926 if (!isxdigit(ch
)) {
2927 trace_gdbstub_err_checksum_invalid(ch
);
2928 gdbserver_state
.state
= RS_GETLINE
;
2931 gdbserver_state
.line_csum
|= fromhex(ch
);
2933 if (gdbserver_state
.line_csum
!= (gdbserver_state
.line_sum
& 0xff)) {
2934 trace_gdbstub_err_checksum_incorrect(gdbserver_state
.line_sum
, gdbserver_state
.line_csum
);
2935 /* send NAK reply */
2937 put_buffer(&reply
, 1);
2938 gdbserver_state
.state
= RS_IDLE
;
2940 /* send ACK reply */
2942 put_buffer(&reply
, 1);
2943 gdbserver_state
.state
= gdb_handle_packet(gdbserver_state
.line_buf
);
2952 /* Tell the remote gdb that the process has exited. */
2953 void gdb_exit(CPUArchState
*env
, int code
)
2957 if (!gdbserver_state
.init
) {
2960 #ifdef CONFIG_USER_ONLY
2961 if (gdbserver_fd
< 0 || gdbserver_state
.fd
< 0) {
2966 trace_gdbstub_op_exiting((uint8_t)code
);
2968 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
2971 #ifndef CONFIG_USER_ONLY
2972 qemu_chr_fe_deinit(&gdbserver_state
.chr
, true);
2977 * Create the process that will contain all the "orphan" CPUs (that are not
2978 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2979 * be attachable and thus will be invisible to the user.
2981 static void create_default_process(GDBState
*s
)
2983 GDBProcess
*process
;
2986 if (gdbserver_state
.process_num
) {
2987 max_pid
= s
->processes
[s
->process_num
- 1].pid
;
2990 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2991 process
= &s
->processes
[s
->process_num
- 1];
2993 /* We need an available PID slot for this process */
2994 assert(max_pid
< UINT32_MAX
);
2996 process
->pid
= max_pid
+ 1;
2997 process
->attached
= false;
2998 process
->target_xml
[0] = '\0';
3001 #ifdef CONFIG_USER_ONLY
3003 gdb_handlesig(CPUState
*cpu
, int sig
)
3008 if (gdbserver_fd
< 0 || gdbserver_state
.fd
< 0) {
3012 /* disable single step if it was enabled */
3013 cpu_single_step(cpu
, 0);
3017 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb(sig
));
3020 /* put_packet() might have detected that the peer terminated the
3022 if (gdbserver_state
.fd
< 0) {
3027 gdbserver_state
.state
= RS_IDLE
;
3028 gdbserver_state
.running_state
= 0;
3029 while (gdbserver_state
.running_state
== 0) {
3030 n
= read(gdbserver_state
.fd
, buf
, 256);
3034 for (i
= 0; i
< n
; i
++) {
3035 gdb_read_byte(buf
[i
]);
3038 /* XXX: Connection closed. Should probably wait for another
3039 connection before continuing. */
3041 close(gdbserver_state
.fd
);
3043 gdbserver_state
.fd
= -1;
3047 sig
= gdbserver_state
.signal
;
3048 gdbserver_state
.signal
= 0;
3052 /* Tell the remote gdb that the process has exited due to SIG. */
3053 void gdb_signalled(CPUArchState
*env
, int sig
)
3057 if (gdbserver_fd
< 0 || gdbserver_state
.fd
< 0) {
3061 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb(sig
));
3065 static bool gdb_accept(void)
3067 struct sockaddr_in sockaddr
;
3072 len
= sizeof(sockaddr
);
3073 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
3074 if (fd
< 0 && errno
!= EINTR
) {
3077 } else if (fd
>= 0) {
3078 qemu_set_cloexec(fd
);
3083 /* set short latency */
3084 if (socket_set_nodelay(fd
)) {
3085 perror("setsockopt");
3090 init_gdbserver_state();
3091 create_default_process(&gdbserver_state
);
3092 gdbserver_state
.processes
[0].attached
= true;
3093 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
3094 gdbserver_state
.g_cpu
= gdbserver_state
.c_cpu
;
3095 gdbserver_state
.fd
= fd
;
3096 gdb_has_xml
= false;
3100 static int gdbserver_open(int port
)
3102 struct sockaddr_in sockaddr
;
3105 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3110 qemu_set_cloexec(fd
);
3112 socket_set_fast_reuse(fd
);
3114 sockaddr
.sin_family
= AF_INET
;
3115 sockaddr
.sin_port
= htons(port
);
3116 sockaddr
.sin_addr
.s_addr
= 0;
3117 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
3123 ret
= listen(fd
, 1);
3132 int gdbserver_start(int port
)
3134 gdbserver_fd
= gdbserver_open(port
);
3135 if (gdbserver_fd
< 0)
3137 /* accept connections */
3138 if (!gdb_accept()) {
3139 close(gdbserver_fd
);
3146 /* Disable gdb stub for child processes. */
3147 void gdbserver_fork(CPUState
*cpu
)
3149 if (gdbserver_fd
< 0 || gdbserver_state
.fd
< 0) {
3152 close(gdbserver_state
.fd
);
3153 gdbserver_state
.fd
= -1;
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(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();
3187 s
->g_cpu
= s
->c_cpu
;
3189 vm_stop(RUN_STATE_PAUSED
);
3190 gdb_has_xml
= false;
3197 static void gdb_monitor_output(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(p
, len
);
3219 gdb_monitor_output(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
);
3303 if (gdbserver_state
.processes
) {
3305 qsort(gdbserver_state
.processes
, gdbserver_state
.process_num
, sizeof(gdbserver_state
.processes
[0]), pid_order
);
3308 create_default_process(s
);
3311 int gdbserver_start(const char *device
)
3313 trace_gdbstub_op_start(device
);
3315 char gdbstub_device_name
[128];
3316 Chardev
*chr
= NULL
;
3320 error_report("gdbstub: meaningless to attach gdb to a "
3321 "machine without any CPU.");
3327 if (strcmp(device
, "none") != 0) {
3328 if (strstart(device
, "tcp:", NULL
)) {
3329 /* enforce required TCP attributes */
3330 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
3331 "%s,nowait,nodelay,server", device
);
3332 device
= gdbstub_device_name
;
3335 else if (strcmp(device
, "stdio") == 0) {
3336 struct sigaction act
;
3338 memset(&act
, 0, sizeof(act
));
3339 act
.sa_handler
= gdb_sigterm_handler
;
3340 sigaction(SIGINT
, &act
, NULL
);
3344 * FIXME: it's a bit weird to allow using a mux chardev here
3345 * and implicitly setup a monitor. We may want to break this.
3347 chr
= qemu_chr_new_noreplay("gdb", device
, true, NULL
);
3352 if (!gdbserver_state
.init
) {
3353 init_gdbserver_state();
3355 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
3357 /* Initialize a monitor terminal for gdb */
3358 mon_chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_GDB
,
3359 NULL
, NULL
, &error_abort
);
3360 monitor_init_hmp(mon_chr
, false, &error_abort
);
3362 qemu_chr_fe_deinit(&gdbserver_state
.chr
, true);
3363 mon_chr
= gdbserver_state
.mon_chr
;
3364 reset_gdbserver_state();
3367 create_processes(&gdbserver_state
);
3370 qemu_chr_fe_init(&gdbserver_state
.chr
, chr
, &error_abort
);
3371 qemu_chr_fe_set_handlers(&gdbserver_state
.chr
, gdb_chr_can_receive
,
3372 gdb_chr_receive
, gdb_chr_event
,
3373 NULL
, &gdbserver_state
, NULL
, true);
3375 gdbserver_state
.state
= chr
? RS_IDLE
: RS_INACTIVE
;
3376 gdbserver_state
.mon_chr
= mon_chr
;
3377 gdbserver_state
.current_syscall_cb
= NULL
;
3382 void gdbserver_cleanup(void)
3384 if (gdbserver_state
.init
) {
3389 static void register_types(void)
3391 type_register_static(&char_gdb_type_info
);
3394 type_init(register_types
);