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/trace-root.h"
34 #include "exec/gdbstub.h"
35 #ifdef CONFIG_USER_ONLY
38 #include "monitor/monitor.h"
39 #include "chardev/char.h"
40 #include "chardev/char-fe.h"
41 #include "hw/cpu/cluster.h"
42 #include "hw/boards.h"
45 #define MAX_PACKET_LENGTH 4096
47 #include "qemu/sockets.h"
48 #include "sysemu/hw_accel.h"
49 #include "sysemu/kvm.h"
50 #include "sysemu/runstate.h"
51 #include "semihosting/semihost.h"
52 #include "exec/exec-all.h"
53 #include "sysemu/replay.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
;
97 return ts
? ts
->ts_tid
: -1;
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
{
322 gdb_get_reg_cb get_reg
;
323 gdb_set_reg_cb set_reg
;
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 GByteArray
*last_packet
;
356 #ifdef CONFIG_USER_ONLY
365 GDBProcess
*processes
;
367 char syscall_buf
[256];
368 gdb_syscall_complete_cb current_syscall_cb
;
372 int supported_sstep_flags
;
375 static GDBState gdbserver_state
;
377 static void init_gdbserver_state(void)
379 g_assert(!gdbserver_state
.init
);
380 memset(&gdbserver_state
, 0, sizeof(GDBState
));
381 gdbserver_state
.init
= true;
382 gdbserver_state
.str_buf
= g_string_new(NULL
);
383 gdbserver_state
.mem_buf
= g_byte_array_sized_new(MAX_PACKET_LENGTH
);
384 gdbserver_state
.last_packet
= g_byte_array_sized_new(MAX_PACKET_LENGTH
+ 4);
387 * In replay mode all events will come from the log and can't be
388 * suppressed otherwise we would break determinism. However as those
389 * events are tied to the number of executed instructions we won't see
390 * them occurring every time we single step.
392 if (replay_mode
!= REPLAY_MODE_NONE
) {
393 gdbserver_state
.supported_sstep_flags
= SSTEP_ENABLE
;
395 gdbserver_state
.supported_sstep_flags
=
396 SSTEP_ENABLE
| SSTEP_NOIRQ
| SSTEP_NOTIMER
;
400 * By default use no IRQs and no timers while single stepping so as to
401 * make single stepping like an ICE HW step.
403 gdbserver_state
.sstep_flags
= gdbserver_state
.supported_sstep_flags
;
407 #ifndef CONFIG_USER_ONLY
408 static void reset_gdbserver_state(void)
410 g_free(gdbserver_state
.processes
);
411 gdbserver_state
.processes
= NULL
;
412 gdbserver_state
.process_num
= 0;
418 #ifdef CONFIG_USER_ONLY
420 static int get_char(void)
426 ret
= qemu_recv(gdbserver_state
.fd
, &ch
, 1, 0);
428 if (errno
== ECONNRESET
)
429 gdbserver_state
.fd
= -1;
432 } else if (ret
== 0) {
433 close(gdbserver_state
.fd
);
434 gdbserver_state
.fd
= -1;
450 /* Decide if either remote gdb syscalls or native file IO should be used. */
451 int use_gdb_syscalls(void)
453 SemihostingTarget target
= semihosting_get_target();
454 if (target
== SEMIHOSTING_TARGET_NATIVE
) {
455 /* -semihosting-config target=native */
457 } else if (target
== SEMIHOSTING_TARGET_GDB
) {
458 /* -semihosting-config target=gdb */
462 /* -semihosting-config target=auto */
463 /* On the first call check if gdb is connected and remember. */
464 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
465 gdb_syscall_mode
= gdbserver_state
.init
?
466 GDB_SYS_ENABLED
: GDB_SYS_DISABLED
;
468 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
471 static bool stub_can_reverse(void)
473 #ifdef CONFIG_USER_ONLY
476 return replay_mode
== REPLAY_MODE_PLAY
;
480 /* Resume execution. */
481 static inline void gdb_continue(void)
484 #ifdef CONFIG_USER_ONLY
485 gdbserver_state
.running_state
= 1;
486 trace_gdbstub_op_continue();
488 if (!runstate_needs_reset()) {
489 trace_gdbstub_op_continue();
496 * Resume execution, per CPU actions. For user-mode emulation it's
497 * equivalent to gdb_continue.
499 static int gdb_continue_partial(char *newstates
)
503 #ifdef CONFIG_USER_ONLY
505 * This is not exactly accurate, but it's an improvement compared to the
506 * previous situation, where only one CPU would be single-stepped.
509 if (newstates
[cpu
->cpu_index
] == 's') {
510 trace_gdbstub_op_stepping(cpu
->cpu_index
);
511 cpu_single_step(cpu
, gdbserver_state
.sstep_flags
);
514 gdbserver_state
.running_state
= 1;
518 if (!runstate_needs_reset()) {
519 if (vm_prepare_start()) {
524 switch (newstates
[cpu
->cpu_index
]) {
527 break; /* nothing to do here */
529 trace_gdbstub_op_stepping(cpu
->cpu_index
);
530 cpu_single_step(cpu
, gdbserver_state
.sstep_flags
);
535 trace_gdbstub_op_continue_cpu(cpu
->cpu_index
);
546 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, true);
552 static void put_buffer(const uint8_t *buf
, int len
)
554 #ifdef CONFIG_USER_ONLY
558 ret
= send(gdbserver_state
.fd
, buf
, len
, 0);
568 /* XXX this blocks entire thread. Rewrite to use
569 * qemu_chr_fe_write and background I/O callbacks */
570 qemu_chr_fe_write_all(&gdbserver_state
.chr
, buf
, len
);
574 static inline int fromhex(int v
)
576 if (v
>= '0' && v
<= '9')
578 else if (v
>= 'A' && v
<= 'F')
580 else if (v
>= 'a' && v
<= 'f')
586 static inline int tohex(int v
)
594 /* writes 2*len+1 bytes in buf */
595 static void memtohex(GString
*buf
, const uint8_t *mem
, int len
)
598 for(i
= 0; i
< len
; i
++) {
600 g_string_append_c(buf
, tohex(c
>> 4));
601 g_string_append_c(buf
, tohex(c
& 0xf));
603 g_string_append_c(buf
, '\0');
606 static void hextomem(GByteArray
*mem
, const char *buf
, int len
)
610 for(i
= 0; i
< len
; i
++) {
611 guint8 byte
= fromhex(buf
[0]) << 4 | fromhex(buf
[1]);
612 g_byte_array_append(mem
, &byte
, 1);
617 static void hexdump(const char *buf
, int len
,
618 void (*trace_fn
)(size_t ofs
, char const *text
))
620 char line_buffer
[3 * 16 + 4 + 16 + 1];
623 for (i
= 0; i
< len
|| (i
& 0xF); ++i
) {
624 size_t byte_ofs
= i
& 15;
627 memset(line_buffer
, ' ', 3 * 16 + 4 + 16);
628 line_buffer
[3 * 16 + 4 + 16] = 0;
631 size_t col_group
= (i
>> 2) & 3;
632 size_t hex_col
= byte_ofs
* 3 + col_group
;
633 size_t txt_col
= 3 * 16 + 4 + byte_ofs
;
638 line_buffer
[hex_col
+ 0] = tohex((value
>> 4) & 0xF);
639 line_buffer
[hex_col
+ 1] = tohex((value
>> 0) & 0xF);
640 line_buffer
[txt_col
+ 0] = (value
>= ' ' && value
< 127)
646 trace_fn(i
& -16, line_buffer
);
650 /* return -1 if error, 0 if OK */
651 static int put_packet_binary(const char *buf
, int len
, bool dump
)
656 if (dump
&& trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY
)) {
657 hexdump(buf
, len
, trace_gdbstub_io_binaryreply
);
661 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
662 g_byte_array_append(gdbserver_state
.last_packet
,
663 (const uint8_t *) "$", 1);
664 g_byte_array_append(gdbserver_state
.last_packet
,
665 (const uint8_t *) buf
, len
);
667 for(i
= 0; i
< len
; i
++) {
671 footer
[1] = tohex((csum
>> 4) & 0xf);
672 footer
[2] = tohex((csum
) & 0xf);
673 g_byte_array_append(gdbserver_state
.last_packet
, footer
, 3);
675 put_buffer(gdbserver_state
.last_packet
->data
,
676 gdbserver_state
.last_packet
->len
);
678 #ifdef CONFIG_USER_ONLY
691 /* return -1 if error, 0 if OK */
692 static int put_packet(const char *buf
)
694 trace_gdbstub_io_reply(buf
);
696 return put_packet_binary(buf
, strlen(buf
), false);
699 static void put_strbuf(void)
701 put_packet(gdbserver_state
.str_buf
->str
);
704 /* Encode data using the encoding for 'x' packets. */
705 static void memtox(GString
*buf
, const char *mem
, int len
)
712 case '#': case '$': case '*': case '}':
713 g_string_append_c(buf
, '}');
714 g_string_append_c(buf
, c
^ 0x20);
717 g_string_append_c(buf
, c
);
723 static uint32_t gdb_get_cpu_pid(CPUState
*cpu
)
725 /* TODO: In user mode, we should use the task state PID */
726 if (cpu
->cluster_index
== UNASSIGNED_CLUSTER_INDEX
) {
727 /* Return the default process' PID */
728 int index
= gdbserver_state
.process_num
- 1;
729 return gdbserver_state
.processes
[index
].pid
;
731 return cpu
->cluster_index
+ 1;
734 static GDBProcess
*gdb_get_process(uint32_t pid
)
739 /* 0 means any process, we take the first one */
740 return &gdbserver_state
.processes
[0];
743 for (i
= 0; i
< gdbserver_state
.process_num
; i
++) {
744 if (gdbserver_state
.processes
[i
].pid
== pid
) {
745 return &gdbserver_state
.processes
[i
];
752 static GDBProcess
*gdb_get_cpu_process(CPUState
*cpu
)
754 return gdb_get_process(gdb_get_cpu_pid(cpu
));
757 static CPUState
*find_cpu(uint32_t thread_id
)
762 if (cpu_gdb_index(cpu
) == thread_id
) {
770 static CPUState
*get_first_cpu_in_process(GDBProcess
*process
)
775 if (gdb_get_cpu_pid(cpu
) == process
->pid
) {
783 static CPUState
*gdb_next_cpu_in_process(CPUState
*cpu
)
785 uint32_t pid
= gdb_get_cpu_pid(cpu
);
789 if (gdb_get_cpu_pid(cpu
) == pid
) {
799 /* Return the cpu following @cpu, while ignoring unattached processes. */
800 static CPUState
*gdb_next_attached_cpu(CPUState
*cpu
)
805 if (gdb_get_cpu_process(cpu
)->attached
) {
815 /* Return the first attached cpu */
816 static CPUState
*gdb_first_attached_cpu(void)
818 CPUState
*cpu
= first_cpu
;
819 GDBProcess
*process
= gdb_get_cpu_process(cpu
);
821 if (!process
->attached
) {
822 return gdb_next_attached_cpu(cpu
);
828 static CPUState
*gdb_get_cpu(uint32_t pid
, uint32_t tid
)
834 /* 0 means any process/thread, we take the first attached one */
835 return gdb_first_attached_cpu();
836 } else if (pid
&& !tid
) {
837 /* any thread in a specific process */
838 process
= gdb_get_process(pid
);
840 if (process
== NULL
) {
844 if (!process
->attached
) {
848 return get_first_cpu_in_process(process
);
850 /* a specific thread */
857 process
= gdb_get_cpu_process(cpu
);
859 if (pid
&& process
->pid
!= pid
) {
863 if (!process
->attached
) {
871 static const char *get_feature_xml(const char *p
, const char **newp
,
877 CPUState
*cpu
= get_first_cpu_in_process(process
);
878 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
881 while (p
[len
] && p
[len
] != ':')
886 if (strncmp(p
, "target.xml", len
) == 0) {
887 char *buf
= process
->target_xml
;
888 const size_t buf_sz
= sizeof(process
->target_xml
);
890 /* Generate the XML description for this CPU. */
895 "<?xml version=\"1.0\"?>"
896 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
898 if (cc
->gdb_arch_name
) {
899 gchar
*arch
= cc
->gdb_arch_name(cpu
);
900 pstrcat(buf
, buf_sz
, "<architecture>");
901 pstrcat(buf
, buf_sz
, arch
);
902 pstrcat(buf
, buf_sz
, "</architecture>");
905 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
906 pstrcat(buf
, buf_sz
, cc
->gdb_core_xml_file
);
907 pstrcat(buf
, buf_sz
, "\"/>");
908 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
909 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
910 pstrcat(buf
, buf_sz
, r
->xml
);
911 pstrcat(buf
, buf_sz
, "\"/>");
913 pstrcat(buf
, buf_sz
, "</target>");
917 if (cc
->gdb_get_dynamic_xml
) {
918 char *xmlname
= g_strndup(p
, len
);
919 const char *xml
= cc
->gdb_get_dynamic_xml(cpu
, xmlname
);
927 name
= xml_builtin
[i
][0];
928 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
931 return name
? xml_builtin
[i
][1] : NULL
;
934 static int gdb_read_register(CPUState
*cpu
, GByteArray
*buf
, int reg
)
936 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
937 CPUArchState
*env
= cpu
->env_ptr
;
940 if (reg
< cc
->gdb_num_core_regs
) {
941 return cc
->gdb_read_register(cpu
, buf
, reg
);
944 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
945 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
946 return r
->get_reg(env
, buf
, reg
- r
->base_reg
);
952 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
954 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
955 CPUArchState
*env
= cpu
->env_ptr
;
958 if (reg
< cc
->gdb_num_core_regs
) {
959 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
962 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
963 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
964 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
970 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
971 specifies the first register number and these registers are included in
972 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
973 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
976 void gdb_register_coprocessor(CPUState
*cpu
,
977 gdb_get_reg_cb get_reg
, gdb_set_reg_cb set_reg
,
978 int num_regs
, const char *xml
, int g_pos
)
981 GDBRegisterState
**p
;
985 /* Check for duplicates. */
986 if (strcmp((*p
)->xml
, xml
) == 0)
991 s
= g_new0(GDBRegisterState
, 1);
992 s
->base_reg
= cpu
->gdb_num_regs
;
993 s
->num_regs
= num_regs
;
994 s
->get_reg
= get_reg
;
995 s
->set_reg
= set_reg
;
998 /* Add to end of list. */
999 cpu
->gdb_num_regs
+= num_regs
;
1002 if (g_pos
!= s
->base_reg
) {
1003 error_report("Error: Bad gdb register numbering for '%s', "
1004 "expected %d got %d", xml
, g_pos
, s
->base_reg
);
1006 cpu
->gdb_num_g_regs
= cpu
->gdb_num_regs
;
1011 #ifndef CONFIG_USER_ONLY
1012 /* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
1013 static inline int xlat_gdb_type(CPUState
*cpu
, int gdbtype
)
1015 static const int xlat
[] = {
1016 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
1017 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
1018 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
1021 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
1022 int cputype
= xlat
[gdbtype
];
1024 if (cc
->gdb_stop_before_watchpoint
) {
1025 cputype
|= BP_STOP_BEFORE_ACCESS
;
1031 static int gdb_breakpoint_insert(int type
, target_ulong addr
, target_ulong len
)
1036 if (kvm_enabled()) {
1037 return kvm_insert_breakpoint(gdbserver_state
.c_cpu
, addr
, len
, type
);
1041 case GDB_BREAKPOINT_SW
:
1042 case GDB_BREAKPOINT_HW
:
1044 err
= cpu_breakpoint_insert(cpu
, addr
, BP_GDB
, NULL
);
1050 #ifndef CONFIG_USER_ONLY
1051 case GDB_WATCHPOINT_WRITE
:
1052 case GDB_WATCHPOINT_READ
:
1053 case GDB_WATCHPOINT_ACCESS
:
1055 err
= cpu_watchpoint_insert(cpu
, addr
, len
,
1056 xlat_gdb_type(cpu
, type
), NULL
);
1068 static int gdb_breakpoint_remove(int type
, target_ulong addr
, target_ulong len
)
1073 if (kvm_enabled()) {
1074 return kvm_remove_breakpoint(gdbserver_state
.c_cpu
, addr
, len
, type
);
1078 case GDB_BREAKPOINT_SW
:
1079 case GDB_BREAKPOINT_HW
:
1081 err
= cpu_breakpoint_remove(cpu
, addr
, BP_GDB
);
1087 #ifndef CONFIG_USER_ONLY
1088 case GDB_WATCHPOINT_WRITE
:
1089 case GDB_WATCHPOINT_READ
:
1090 case GDB_WATCHPOINT_ACCESS
:
1092 err
= cpu_watchpoint_remove(cpu
, addr
, len
,
1093 xlat_gdb_type(cpu
, type
));
1104 static inline void gdb_cpu_breakpoint_remove_all(CPUState
*cpu
)
1106 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
1107 #ifndef CONFIG_USER_ONLY
1108 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
1112 static void gdb_process_breakpoint_remove_all(GDBProcess
*p
)
1114 CPUState
*cpu
= get_first_cpu_in_process(p
);
1117 gdb_cpu_breakpoint_remove_all(cpu
);
1118 cpu
= gdb_next_cpu_in_process(cpu
);
1122 static void gdb_breakpoint_remove_all(void)
1126 if (kvm_enabled()) {
1127 kvm_remove_all_breakpoints(gdbserver_state
.c_cpu
);
1132 gdb_cpu_breakpoint_remove_all(cpu
);
1136 static void gdb_set_cpu_pc(target_ulong pc
)
1138 CPUState
*cpu
= gdbserver_state
.c_cpu
;
1140 cpu_synchronize_state(cpu
);
1141 cpu_set_pc(cpu
, pc
);
1144 static void gdb_append_thread_id(CPUState
*cpu
, GString
*buf
)
1146 if (gdbserver_state
.multiprocess
) {
1147 g_string_append_printf(buf
, "p%02x.%02x",
1148 gdb_get_cpu_pid(cpu
), cpu_gdb_index(cpu
));
1150 g_string_append_printf(buf
, "%02x", cpu_gdb_index(cpu
));
1154 typedef enum GDBThreadIdKind
{
1156 GDB_ALL_THREADS
, /* One process, all threads */
1161 static GDBThreadIdKind
read_thread_id(const char *buf
, const char **end_buf
,
1162 uint32_t *pid
, uint32_t *tid
)
1169 ret
= qemu_strtoul(buf
, &buf
, 16, &p
);
1172 return GDB_READ_THREAD_ERR
;
1181 ret
= qemu_strtoul(buf
, &buf
, 16, &t
);
1184 return GDB_READ_THREAD_ERR
;
1190 return GDB_ALL_PROCESSES
;
1198 return GDB_ALL_THREADS
;
1205 return GDB_ONE_THREAD
;
1209 * gdb_handle_vcont - Parses and handles a vCont packet.
1210 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1211 * a format error, 0 on success.
1213 static int gdb_handle_vcont(const char *p
)
1215 int res
, signal
= 0;
1220 GDBProcess
*process
;
1222 GDBThreadIdKind kind
;
1223 #ifdef CONFIG_USER_ONLY
1224 int max_cpus
= 1; /* global variable max_cpus exists only in system mode */
1227 max_cpus
= max_cpus
<= cpu
->cpu_index
? cpu
->cpu_index
+ 1 : max_cpus
;
1230 MachineState
*ms
= MACHINE(qdev_get_machine());
1231 unsigned int max_cpus
= ms
->smp
.max_cpus
;
1233 /* uninitialised CPUs stay 0 */
1234 newstates
= g_new0(char, max_cpus
);
1236 /* mark valid CPUs with 1 */
1238 newstates
[cpu
->cpu_index
] = 1;
1242 * res keeps track of what error we are returning, with -ENOTSUP meaning
1243 * that the command is unknown or unsupported, thus returning an empty
1244 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1245 * or incorrect parameters passed.
1255 if (cur_action
== 'C' || cur_action
== 'S') {
1256 cur_action
= qemu_tolower(cur_action
);
1257 res
= qemu_strtoul(p
, &p
, 16, &tmp
);
1261 signal
= gdb_signal_to_target(tmp
);
1262 } else if (cur_action
!= 'c' && cur_action
!= 's') {
1263 /* unknown/invalid/unsupported command */
1268 if (*p
== '\0' || *p
== ';') {
1270 * No thread specifier, action is on "all threads". The
1271 * specification is unclear regarding the process to act on. We
1272 * choose all processes.
1274 kind
= GDB_ALL_PROCESSES
;
1275 } else if (*p
++ == ':') {
1276 kind
= read_thread_id(p
, &p
, &pid
, &tid
);
1283 case GDB_READ_THREAD_ERR
:
1287 case GDB_ALL_PROCESSES
:
1288 cpu
= gdb_first_attached_cpu();
1290 if (newstates
[cpu
->cpu_index
] == 1) {
1291 newstates
[cpu
->cpu_index
] = cur_action
;
1294 cpu
= gdb_next_attached_cpu(cpu
);
1298 case GDB_ALL_THREADS
:
1299 process
= gdb_get_process(pid
);
1301 if (!process
->attached
) {
1306 cpu
= get_first_cpu_in_process(process
);
1308 if (newstates
[cpu
->cpu_index
] == 1) {
1309 newstates
[cpu
->cpu_index
] = cur_action
;
1312 cpu
= gdb_next_cpu_in_process(cpu
);
1316 case GDB_ONE_THREAD
:
1317 cpu
= gdb_get_cpu(pid
, tid
);
1319 /* invalid CPU/thread specified */
1325 /* only use if no previous match occourred */
1326 if (newstates
[cpu
->cpu_index
] == 1) {
1327 newstates
[cpu
->cpu_index
] = cur_action
;
1332 gdbserver_state
.signal
= signal
;
1333 gdb_continue_partial(newstates
);
1341 typedef union GdbCmdVariant
{
1344 unsigned long val_ul
;
1345 unsigned long long val_ull
;
1347 GDBThreadIdKind kind
;
1353 #define get_param(p, i) (&g_array_index(p, GdbCmdVariant, i))
1355 static const char *cmd_next_param(const char *param
, const char delimiter
)
1357 static const char all_delimiters
[] = ",;:=";
1358 char curr_delimiters
[2] = {0};
1359 const char *delimiters
;
1361 if (delimiter
== '?') {
1362 delimiters
= all_delimiters
;
1363 } else if (delimiter
== '0') {
1364 return strchr(param
, '\0');
1365 } else if (delimiter
== '.' && *param
) {
1368 curr_delimiters
[0] = delimiter
;
1369 delimiters
= curr_delimiters
;
1372 param
+= strcspn(param
, delimiters
);
1379 static int cmd_parse_params(const char *data
, const char *schema
,
1382 const char *curr_schema
, *curr_data
;
1385 g_assert(params
->len
== 0);
1387 curr_schema
= schema
;
1389 while (curr_schema
[0] && curr_schema
[1] && *curr_data
) {
1390 GdbCmdVariant this_param
;
1392 switch (curr_schema
[0]) {
1394 if (qemu_strtoul(curr_data
, &curr_data
, 16,
1395 &this_param
.val_ul
)) {
1398 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1399 g_array_append_val(params
, this_param
);
1402 if (qemu_strtou64(curr_data
, &curr_data
, 16,
1403 (uint64_t *)&this_param
.val_ull
)) {
1406 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1407 g_array_append_val(params
, this_param
);
1410 this_param
.data
= curr_data
;
1411 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1412 g_array_append_val(params
, this_param
);
1415 this_param
.opcode
= *(uint8_t *)curr_data
;
1416 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1417 g_array_append_val(params
, this_param
);
1420 this_param
.thread_id
.kind
=
1421 read_thread_id(curr_data
, &curr_data
,
1422 &this_param
.thread_id
.pid
,
1423 &this_param
.thread_id
.tid
);
1424 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1425 g_array_append_val(params
, this_param
);
1428 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1439 typedef void (*GdbCmdHandler
)(GArray
*params
, void *user_ctx
);
1442 * cmd_startswith -> cmd is compared using startswith
1445 * schema definitions:
1446 * Each schema parameter entry consists of 2 chars,
1447 * the first char represents the parameter type handling
1448 * the second char represents the delimiter for the next parameter
1450 * Currently supported schema types:
1451 * 'l' -> unsigned long (stored in .val_ul)
1452 * 'L' -> unsigned long long (stored in .val_ull)
1453 * 's' -> string (stored in .data)
1454 * 'o' -> single char (stored in .opcode)
1455 * 't' -> thread id (stored in .thread_id)
1456 * '?' -> skip according to delimiter
1458 * Currently supported delimiters:
1459 * '?' -> Stop at any delimiter (",;:=\0")
1460 * '0' -> Stop at "\0"
1461 * '.' -> Skip 1 char unless reached "\0"
1462 * Any other value is treated as the delimiter value itself
1464 typedef struct GdbCmdParseEntry
{
1465 GdbCmdHandler handler
;
1467 bool cmd_startswith
;
1471 static inline int startswith(const char *string
, const char *pattern
)
1473 return !strncmp(string
, pattern
, strlen(pattern
));
1476 static int process_string_cmd(void *user_ctx
, const char *data
,
1477 const GdbCmdParseEntry
*cmds
, int num_cmds
)
1480 g_autoptr(GArray
) params
= g_array_new(false, true, sizeof(GdbCmdVariant
));
1486 for (i
= 0; i
< num_cmds
; i
++) {
1487 const GdbCmdParseEntry
*cmd
= &cmds
[i
];
1488 g_assert(cmd
->handler
&& cmd
->cmd
);
1490 if ((cmd
->cmd_startswith
&& !startswith(data
, cmd
->cmd
)) ||
1491 (!cmd
->cmd_startswith
&& strcmp(cmd
->cmd
, data
))) {
1496 if (cmd_parse_params(&data
[strlen(cmd
->cmd
)],
1497 cmd
->schema
, params
)) {
1502 cmd
->handler(params
, user_ctx
);
1509 static void run_cmd_parser(const char *data
, const GdbCmdParseEntry
*cmd
)
1515 g_string_set_size(gdbserver_state
.str_buf
, 0);
1516 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
1518 /* In case there was an error during the command parsing we must
1519 * send a NULL packet to indicate the command is not supported */
1520 if (process_string_cmd(NULL
, data
, cmd
, 1)) {
1525 static void handle_detach(GArray
*params
, void *user_ctx
)
1527 GDBProcess
*process
;
1530 if (gdbserver_state
.multiprocess
) {
1536 pid
= get_param(params
, 0)->val_ul
;
1539 process
= gdb_get_process(pid
);
1540 gdb_process_breakpoint_remove_all(process
);
1541 process
->attached
= false;
1543 if (pid
== gdb_get_cpu_pid(gdbserver_state
.c_cpu
)) {
1544 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
1547 if (pid
== gdb_get_cpu_pid(gdbserver_state
.g_cpu
)) {
1548 gdbserver_state
.g_cpu
= gdb_first_attached_cpu();
1551 if (!gdbserver_state
.c_cpu
) {
1552 /* No more process attached */
1553 gdb_syscall_mode
= GDB_SYS_DISABLED
;
1559 static void handle_thread_alive(GArray
*params
, void *user_ctx
)
1568 if (get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1573 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
1574 get_param(params
, 0)->thread_id
.tid
);
1583 static void handle_continue(GArray
*params
, void *user_ctx
)
1586 gdb_set_cpu_pc(get_param(params
, 0)->val_ull
);
1589 gdbserver_state
.signal
= 0;
1593 static void handle_cont_with_sig(GArray
*params
, void *user_ctx
)
1595 unsigned long signal
= 0;
1598 * Note: C sig;[addr] is currently unsupported and we simply
1599 * omit the addr parameter
1602 signal
= get_param(params
, 0)->val_ul
;
1605 gdbserver_state
.signal
= gdb_signal_to_target(signal
);
1606 if (gdbserver_state
.signal
== -1) {
1607 gdbserver_state
.signal
= 0;
1612 static void handle_set_thread(GArray
*params
, void *user_ctx
)
1616 if (params
->len
!= 2) {
1621 if (get_param(params
, 1)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1626 if (get_param(params
, 1)->thread_id
.kind
!= GDB_ONE_THREAD
) {
1631 cpu
= gdb_get_cpu(get_param(params
, 1)->thread_id
.pid
,
1632 get_param(params
, 1)->thread_id
.tid
);
1639 * Note: This command is deprecated and modern gdb's will be using the
1640 * vCont command instead.
1642 switch (get_param(params
, 0)->opcode
) {
1644 gdbserver_state
.c_cpu
= cpu
;
1648 gdbserver_state
.g_cpu
= cpu
;
1657 static void handle_insert_bp(GArray
*params
, void *user_ctx
)
1661 if (params
->len
!= 3) {
1666 res
= gdb_breakpoint_insert(get_param(params
, 0)->val_ul
,
1667 get_param(params
, 1)->val_ull
,
1668 get_param(params
, 2)->val_ull
);
1672 } else if (res
== -ENOSYS
) {
1680 static void handle_remove_bp(GArray
*params
, void *user_ctx
)
1684 if (params
->len
!= 3) {
1689 res
= gdb_breakpoint_remove(get_param(params
, 0)->val_ul
,
1690 get_param(params
, 1)->val_ull
,
1691 get_param(params
, 2)->val_ull
);
1695 } else if (res
== -ENOSYS
) {
1704 * handle_set/get_reg
1706 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1707 * This works, but can be very slow. Anything new enough to understand
1708 * XML also knows how to use this properly. However to use this we
1709 * need to define a local XML file as well as be talking to a
1710 * reasonably modern gdb. Responding with an empty packet will cause
1711 * the remote gdb to fallback to older methods.
1714 static void handle_set_reg(GArray
*params
, void *user_ctx
)
1723 if (params
->len
!= 2) {
1728 reg_size
= strlen(get_param(params
, 1)->data
) / 2;
1729 hextomem(gdbserver_state
.mem_buf
, get_param(params
, 1)->data
, reg_size
);
1730 gdb_write_register(gdbserver_state
.g_cpu
, gdbserver_state
.mem_buf
->data
,
1731 get_param(params
, 0)->val_ull
);
1735 static void handle_get_reg(GArray
*params
, void *user_ctx
)
1749 reg_size
= gdb_read_register(gdbserver_state
.g_cpu
,
1750 gdbserver_state
.mem_buf
,
1751 get_param(params
, 0)->val_ull
);
1756 g_byte_array_set_size(gdbserver_state
.mem_buf
, reg_size
);
1759 memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
, reg_size
);
1763 static void handle_write_mem(GArray
*params
, void *user_ctx
)
1765 if (params
->len
!= 3) {
1770 /* hextomem() reads 2*len bytes */
1771 if (get_param(params
, 1)->val_ull
>
1772 strlen(get_param(params
, 2)->data
) / 2) {
1777 hextomem(gdbserver_state
.mem_buf
, get_param(params
, 2)->data
,
1778 get_param(params
, 1)->val_ull
);
1779 if (target_memory_rw_debug(gdbserver_state
.g_cpu
,
1780 get_param(params
, 0)->val_ull
,
1781 gdbserver_state
.mem_buf
->data
,
1782 gdbserver_state
.mem_buf
->len
, true)) {
1790 static void handle_read_mem(GArray
*params
, void *user_ctx
)
1792 if (params
->len
!= 2) {
1797 /* memtohex() doubles the required space */
1798 if (get_param(params
, 1)->val_ull
> MAX_PACKET_LENGTH
/ 2) {
1803 g_byte_array_set_size(gdbserver_state
.mem_buf
,
1804 get_param(params
, 1)->val_ull
);
1806 if (target_memory_rw_debug(gdbserver_state
.g_cpu
,
1807 get_param(params
, 0)->val_ull
,
1808 gdbserver_state
.mem_buf
->data
,
1809 gdbserver_state
.mem_buf
->len
, false)) {
1814 memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
,
1815 gdbserver_state
.mem_buf
->len
);
1819 static void handle_write_all_regs(GArray
*params
, void *user_ctx
)
1821 target_ulong addr
, len
;
1829 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1830 len
= strlen(get_param(params
, 0)->data
) / 2;
1831 hextomem(gdbserver_state
.mem_buf
, get_param(params
, 0)->data
, len
);
1832 registers
= gdbserver_state
.mem_buf
->data
;
1833 for (addr
= 0; addr
< gdbserver_state
.g_cpu
->gdb_num_g_regs
&& len
> 0;
1835 reg_size
= gdb_write_register(gdbserver_state
.g_cpu
, registers
, addr
);
1837 registers
+= reg_size
;
1842 static void handle_read_all_regs(GArray
*params
, void *user_ctx
)
1844 target_ulong addr
, len
;
1846 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1847 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
1849 for (addr
= 0; addr
< gdbserver_state
.g_cpu
->gdb_num_g_regs
; addr
++) {
1850 len
+= gdb_read_register(gdbserver_state
.g_cpu
,
1851 gdbserver_state
.mem_buf
,
1854 g_assert(len
== gdbserver_state
.mem_buf
->len
);
1856 memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
, len
);
1860 static void handle_file_io(GArray
*params
, void *user_ctx
)
1862 if (params
->len
>= 1 && gdbserver_state
.current_syscall_cb
) {
1863 target_ulong ret
, err
;
1865 ret
= (target_ulong
)get_param(params
, 0)->val_ull
;
1866 if (params
->len
>= 2) {
1867 err
= (target_ulong
)get_param(params
, 1)->val_ull
;
1871 gdbserver_state
.current_syscall_cb(gdbserver_state
.c_cpu
, ret
, err
);
1872 gdbserver_state
.current_syscall_cb
= NULL
;
1875 if (params
->len
>= 3 && get_param(params
, 2)->opcode
== (uint8_t)'C') {
1883 static void handle_step(GArray
*params
, void *user_ctx
)
1886 gdb_set_cpu_pc((target_ulong
)get_param(params
, 0)->val_ull
);
1889 cpu_single_step(gdbserver_state
.c_cpu
, gdbserver_state
.sstep_flags
);
1893 static void handle_backward(GArray
*params
, void *user_ctx
)
1895 if (!stub_can_reverse()) {
1898 if (params
->len
== 1) {
1899 switch (get_param(params
, 0)->opcode
) {
1901 if (replay_reverse_step()) {
1908 if (replay_reverse_continue()) {
1917 /* Default invalid command */
1921 static void handle_v_cont_query(GArray
*params
, void *user_ctx
)
1923 put_packet("vCont;c;C;s;S");
1926 static void handle_v_cont(GArray
*params
, void *user_ctx
)
1934 res
= gdb_handle_vcont(get_param(params
, 0)->data
);
1935 if ((res
== -EINVAL
) || (res
== -ERANGE
)) {
1942 static void handle_v_attach(GArray
*params
, void *user_ctx
)
1944 GDBProcess
*process
;
1947 g_string_assign(gdbserver_state
.str_buf
, "E22");
1952 process
= gdb_get_process(get_param(params
, 0)->val_ul
);
1957 cpu
= get_first_cpu_in_process(process
);
1962 process
->attached
= true;
1963 gdbserver_state
.g_cpu
= cpu
;
1964 gdbserver_state
.c_cpu
= cpu
;
1966 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
1967 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
1968 g_string_append_c(gdbserver_state
.str_buf
, ';');
1973 static void handle_v_kill(GArray
*params
, void *user_ctx
)
1975 /* Kill the target */
1977 error_report("QEMU: Terminated via GDBstub");
1982 static const GdbCmdParseEntry gdb_v_commands_table
[] = {
1983 /* Order is important if has same prefix */
1985 .handler
= handle_v_cont_query
,
1990 .handler
= handle_v_cont
,
1992 .cmd_startswith
= 1,
1996 .handler
= handle_v_attach
,
1998 .cmd_startswith
= 1,
2002 .handler
= handle_v_kill
,
2008 static void handle_v_commands(GArray
*params
, void *user_ctx
)
2014 if (process_string_cmd(NULL
, get_param(params
, 0)->data
,
2015 gdb_v_commands_table
,
2016 ARRAY_SIZE(gdb_v_commands_table
))) {
2021 static void handle_query_qemu_sstepbits(GArray
*params
, void *user_ctx
)
2023 g_string_printf(gdbserver_state
.str_buf
, "ENABLE=%x", SSTEP_ENABLE
);
2025 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOIRQ
) {
2026 g_string_append_printf(gdbserver_state
.str_buf
, ",NOIRQ=%x",
2030 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOTIMER
) {
2031 g_string_append_printf(gdbserver_state
.str_buf
, ",NOTIMER=%x",
2038 static void handle_set_qemu_sstep(GArray
*params
, void *user_ctx
)
2040 int new_sstep_flags
;
2046 new_sstep_flags
= get_param(params
, 0)->val_ul
;
2048 if (new_sstep_flags
& ~gdbserver_state
.supported_sstep_flags
) {
2053 gdbserver_state
.sstep_flags
= new_sstep_flags
;
2057 static void handle_query_qemu_sstep(GArray
*params
, void *user_ctx
)
2059 g_string_printf(gdbserver_state
.str_buf
, "0x%x",
2060 gdbserver_state
.sstep_flags
);
2064 static void handle_query_curr_tid(GArray
*params
, void *user_ctx
)
2067 GDBProcess
*process
;
2070 * "Current thread" remains vague in the spec, so always return
2071 * the first thread of the current process (gdb returns the
2074 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
2075 cpu
= get_first_cpu_in_process(process
);
2076 g_string_assign(gdbserver_state
.str_buf
, "QC");
2077 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
2081 static void handle_query_threads(GArray
*params
, void *user_ctx
)
2083 if (!gdbserver_state
.query_cpu
) {
2088 g_string_assign(gdbserver_state
.str_buf
, "m");
2089 gdb_append_thread_id(gdbserver_state
.query_cpu
, gdbserver_state
.str_buf
);
2091 gdbserver_state
.query_cpu
= gdb_next_attached_cpu(gdbserver_state
.query_cpu
);
2094 static void handle_query_first_threads(GArray
*params
, void *user_ctx
)
2096 gdbserver_state
.query_cpu
= gdb_first_attached_cpu();
2097 handle_query_threads(params
, user_ctx
);
2100 static void handle_query_thread_extra(GArray
*params
, void *user_ctx
)
2102 g_autoptr(GString
) rs
= g_string_new(NULL
);
2106 get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
2111 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
2112 get_param(params
, 0)->thread_id
.tid
);
2117 cpu_synchronize_state(cpu
);
2119 if (gdbserver_state
.multiprocess
&& (gdbserver_state
.process_num
> 1)) {
2120 /* Print the CPU model and name in multiprocess mode */
2121 ObjectClass
*oc
= object_get_class(OBJECT(cpu
));
2122 const char *cpu_model
= object_class_get_name(oc
);
2123 const char *cpu_name
=
2124 object_get_canonical_path_component(OBJECT(cpu
));
2125 g_string_printf(rs
, "%s %s [%s]", cpu_model
, cpu_name
,
2126 cpu
->halted
? "halted " : "running");
2128 g_string_printf(rs
, "CPU#%d [%s]", cpu
->cpu_index
,
2129 cpu
->halted
? "halted " : "running");
2131 trace_gdbstub_op_extra_info(rs
->str
);
2132 memtohex(gdbserver_state
.str_buf
, (uint8_t *)rs
->str
, rs
->len
);
2136 #ifdef CONFIG_USER_ONLY
2137 static void handle_query_offsets(GArray
*params
, void *user_ctx
)
2141 ts
= gdbserver_state
.c_cpu
->opaque
;
2142 g_string_printf(gdbserver_state
.str_buf
,
2143 "Text=" TARGET_ABI_FMT_lx
2144 ";Data=" TARGET_ABI_FMT_lx
2145 ";Bss=" TARGET_ABI_FMT_lx
,
2146 ts
->info
->code_offset
,
2147 ts
->info
->data_offset
,
2148 ts
->info
->data_offset
);
2152 static void handle_query_rcmd(GArray
*params
, void *user_ctx
)
2154 const guint8 zero
= 0;
2162 len
= strlen(get_param(params
, 0)->data
);
2168 g_assert(gdbserver_state
.mem_buf
->len
== 0);
2170 hextomem(gdbserver_state
.mem_buf
, get_param(params
, 0)->data
, len
);
2171 g_byte_array_append(gdbserver_state
.mem_buf
, &zero
, 1);
2172 qemu_chr_be_write(gdbserver_state
.mon_chr
, gdbserver_state
.mem_buf
->data
,
2173 gdbserver_state
.mem_buf
->len
);
2178 static void handle_query_supported(GArray
*params
, void *user_ctx
)
2182 g_string_printf(gdbserver_state
.str_buf
, "PacketSize=%x", MAX_PACKET_LENGTH
);
2183 cc
= CPU_GET_CLASS(first_cpu
);
2184 if (cc
->gdb_core_xml_file
) {
2185 g_string_append(gdbserver_state
.str_buf
, ";qXfer:features:read+");
2188 if (stub_can_reverse()) {
2189 g_string_append(gdbserver_state
.str_buf
,
2190 ";ReverseStep+;ReverseContinue+");
2193 #ifdef CONFIG_USER_ONLY
2194 if (gdbserver_state
.c_cpu
->opaque
) {
2195 g_string_append(gdbserver_state
.str_buf
, ";qXfer:auxv:read+");
2200 strstr(get_param(params
, 0)->data
, "multiprocess+")) {
2201 gdbserver_state
.multiprocess
= true;
2204 g_string_append(gdbserver_state
.str_buf
, ";vContSupported+;multiprocess+");
2208 static void handle_query_xfer_features(GArray
*params
, void *user_ctx
)
2210 GDBProcess
*process
;
2212 unsigned long len
, total_len
, addr
;
2216 if (params
->len
< 3) {
2221 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
2222 cc
= CPU_GET_CLASS(gdbserver_state
.g_cpu
);
2223 if (!cc
->gdb_core_xml_file
) {
2229 p
= get_param(params
, 0)->data
;
2230 xml
= get_feature_xml(p
, &p
, process
);
2236 addr
= get_param(params
, 1)->val_ul
;
2237 len
= get_param(params
, 2)->val_ul
;
2238 total_len
= strlen(xml
);
2239 if (addr
> total_len
) {
2244 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
2245 len
= (MAX_PACKET_LENGTH
- 5) / 2;
2248 if (len
< total_len
- addr
) {
2249 g_string_assign(gdbserver_state
.str_buf
, "m");
2250 memtox(gdbserver_state
.str_buf
, xml
+ addr
, len
);
2252 g_string_assign(gdbserver_state
.str_buf
, "l");
2253 memtox(gdbserver_state
.str_buf
, xml
+ addr
, total_len
- addr
);
2256 put_packet_binary(gdbserver_state
.str_buf
->str
,
2257 gdbserver_state
.str_buf
->len
, true);
2260 #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX_USER)
2261 static void handle_query_xfer_auxv(GArray
*params
, void *user_ctx
)
2264 unsigned long offset
, len
, saved_auxv
, auxv_len
;
2266 if (params
->len
< 2) {
2271 offset
= get_param(params
, 0)->val_ul
;
2272 len
= get_param(params
, 1)->val_ul
;
2273 ts
= gdbserver_state
.c_cpu
->opaque
;
2274 saved_auxv
= ts
->info
->saved_auxv
;
2275 auxv_len
= ts
->info
->auxv_len
;
2277 if (offset
>= auxv_len
) {
2282 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
2283 len
= (MAX_PACKET_LENGTH
- 5) / 2;
2286 if (len
< auxv_len
- offset
) {
2287 g_string_assign(gdbserver_state
.str_buf
, "m");
2289 g_string_assign(gdbserver_state
.str_buf
, "l");
2290 len
= auxv_len
- offset
;
2293 g_byte_array_set_size(gdbserver_state
.mem_buf
, len
);
2294 if (target_memory_rw_debug(gdbserver_state
.g_cpu
, saved_auxv
+ offset
,
2295 gdbserver_state
.mem_buf
->data
, len
, false)) {
2300 memtox(gdbserver_state
.str_buf
,
2301 (const char *)gdbserver_state
.mem_buf
->data
, len
);
2302 put_packet_binary(gdbserver_state
.str_buf
->str
,
2303 gdbserver_state
.str_buf
->len
, true);
2307 static void handle_query_attached(GArray
*params
, void *user_ctx
)
2309 put_packet(GDB_ATTACHED
);
2312 static void handle_query_qemu_supported(GArray
*params
, void *user_ctx
)
2314 g_string_printf(gdbserver_state
.str_buf
, "sstepbits;sstep");
2315 #ifndef CONFIG_USER_ONLY
2316 g_string_append(gdbserver_state
.str_buf
, ";PhyMemMode");
2321 #ifndef CONFIG_USER_ONLY
2322 static void handle_query_qemu_phy_mem_mode(GArray
*params
,
2325 g_string_printf(gdbserver_state
.str_buf
, "%d", phy_memory_mode
);
2329 static void handle_set_qemu_phy_mem_mode(GArray
*params
, void *user_ctx
)
2336 if (!get_param(params
, 0)->val_ul
) {
2337 phy_memory_mode
= 0;
2339 phy_memory_mode
= 1;
2345 static const GdbCmdParseEntry gdb_gen_query_set_common_table
[] = {
2346 /* Order is important if has same prefix */
2348 .handler
= handle_query_qemu_sstepbits
,
2349 .cmd
= "qemu.sstepbits",
2352 .handler
= handle_query_qemu_sstep
,
2353 .cmd
= "qemu.sstep",
2356 .handler
= handle_set_qemu_sstep
,
2357 .cmd
= "qemu.sstep=",
2358 .cmd_startswith
= 1,
2363 static const GdbCmdParseEntry gdb_gen_query_table
[] = {
2365 .handler
= handle_query_curr_tid
,
2369 .handler
= handle_query_threads
,
2370 .cmd
= "sThreadInfo",
2373 .handler
= handle_query_first_threads
,
2374 .cmd
= "fThreadInfo",
2377 .handler
= handle_query_thread_extra
,
2378 .cmd
= "ThreadExtraInfo,",
2379 .cmd_startswith
= 1,
2382 #ifdef CONFIG_USER_ONLY
2384 .handler
= handle_query_offsets
,
2389 .handler
= handle_query_rcmd
,
2391 .cmd_startswith
= 1,
2396 .handler
= handle_query_supported
,
2397 .cmd
= "Supported:",
2398 .cmd_startswith
= 1,
2402 .handler
= handle_query_supported
,
2407 .handler
= handle_query_xfer_features
,
2408 .cmd
= "Xfer:features:read:",
2409 .cmd_startswith
= 1,
2412 #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX_USER)
2414 .handler
= handle_query_xfer_auxv
,
2415 .cmd
= "Xfer:auxv:read::",
2416 .cmd_startswith
= 1,
2421 .handler
= handle_query_attached
,
2426 .handler
= handle_query_attached
,
2430 .handler
= handle_query_qemu_supported
,
2431 .cmd
= "qemu.Supported",
2433 #ifndef CONFIG_USER_ONLY
2435 .handler
= handle_query_qemu_phy_mem_mode
,
2436 .cmd
= "qemu.PhyMemMode",
2441 static const GdbCmdParseEntry gdb_gen_set_table
[] = {
2442 /* Order is important if has same prefix */
2444 .handler
= handle_set_qemu_sstep
,
2445 .cmd
= "qemu.sstep:",
2446 .cmd_startswith
= 1,
2449 #ifndef CONFIG_USER_ONLY
2451 .handler
= handle_set_qemu_phy_mem_mode
,
2452 .cmd
= "qemu.PhyMemMode:",
2453 .cmd_startswith
= 1,
2459 static void handle_gen_query(GArray
*params
, void *user_ctx
)
2465 if (!process_string_cmd(NULL
, get_param(params
, 0)->data
,
2466 gdb_gen_query_set_common_table
,
2467 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
2471 if (process_string_cmd(NULL
, get_param(params
, 0)->data
,
2472 gdb_gen_query_table
,
2473 ARRAY_SIZE(gdb_gen_query_table
))) {
2478 static void handle_gen_set(GArray
*params
, void *user_ctx
)
2484 if (!process_string_cmd(NULL
, get_param(params
, 0)->data
,
2485 gdb_gen_query_set_common_table
,
2486 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
2490 if (process_string_cmd(NULL
, get_param(params
, 0)->data
,
2492 ARRAY_SIZE(gdb_gen_set_table
))) {
2497 static void handle_target_halt(GArray
*params
, void *user_ctx
)
2499 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
2500 gdb_append_thread_id(gdbserver_state
.c_cpu
, gdbserver_state
.str_buf
);
2501 g_string_append_c(gdbserver_state
.str_buf
, ';');
2504 * Remove all the breakpoints when this query is issued,
2505 * because gdb is doing an initial connect and the state
2506 * should be cleaned up.
2508 gdb_breakpoint_remove_all();
2511 static int gdb_handle_packet(const char *line_buf
)
2513 const GdbCmdParseEntry
*cmd_parser
= NULL
;
2515 trace_gdbstub_io_command(line_buf
);
2517 switch (line_buf
[0]) {
2523 static const GdbCmdParseEntry target_halted_cmd_desc
= {
2524 .handler
= handle_target_halt
,
2528 cmd_parser
= &target_halted_cmd_desc
;
2533 static const GdbCmdParseEntry continue_cmd_desc
= {
2534 .handler
= handle_continue
,
2536 .cmd_startswith
= 1,
2539 cmd_parser
= &continue_cmd_desc
;
2544 static const GdbCmdParseEntry cont_with_sig_cmd_desc
= {
2545 .handler
= handle_cont_with_sig
,
2547 .cmd_startswith
= 1,
2550 cmd_parser
= &cont_with_sig_cmd_desc
;
2555 static const GdbCmdParseEntry v_cmd_desc
= {
2556 .handler
= handle_v_commands
,
2558 .cmd_startswith
= 1,
2561 cmd_parser
= &v_cmd_desc
;
2565 /* Kill the target */
2566 error_report("QEMU: Terminated via GDBstub");
2571 static const GdbCmdParseEntry detach_cmd_desc
= {
2572 .handler
= handle_detach
,
2574 .cmd_startswith
= 1,
2577 cmd_parser
= &detach_cmd_desc
;
2582 static const GdbCmdParseEntry step_cmd_desc
= {
2583 .handler
= handle_step
,
2585 .cmd_startswith
= 1,
2588 cmd_parser
= &step_cmd_desc
;
2593 static const GdbCmdParseEntry backward_cmd_desc
= {
2594 .handler
= handle_backward
,
2596 .cmd_startswith
= 1,
2599 cmd_parser
= &backward_cmd_desc
;
2604 static const GdbCmdParseEntry file_io_cmd_desc
= {
2605 .handler
= handle_file_io
,
2607 .cmd_startswith
= 1,
2610 cmd_parser
= &file_io_cmd_desc
;
2615 static const GdbCmdParseEntry read_all_regs_cmd_desc
= {
2616 .handler
= handle_read_all_regs
,
2620 cmd_parser
= &read_all_regs_cmd_desc
;
2625 static const GdbCmdParseEntry write_all_regs_cmd_desc
= {
2626 .handler
= handle_write_all_regs
,
2628 .cmd_startswith
= 1,
2631 cmd_parser
= &write_all_regs_cmd_desc
;
2636 static const GdbCmdParseEntry read_mem_cmd_desc
= {
2637 .handler
= handle_read_mem
,
2639 .cmd_startswith
= 1,
2642 cmd_parser
= &read_mem_cmd_desc
;
2647 static const GdbCmdParseEntry write_mem_cmd_desc
= {
2648 .handler
= handle_write_mem
,
2650 .cmd_startswith
= 1,
2653 cmd_parser
= &write_mem_cmd_desc
;
2658 static const GdbCmdParseEntry get_reg_cmd_desc
= {
2659 .handler
= handle_get_reg
,
2661 .cmd_startswith
= 1,
2664 cmd_parser
= &get_reg_cmd_desc
;
2669 static const GdbCmdParseEntry set_reg_cmd_desc
= {
2670 .handler
= handle_set_reg
,
2672 .cmd_startswith
= 1,
2675 cmd_parser
= &set_reg_cmd_desc
;
2680 static const GdbCmdParseEntry insert_bp_cmd_desc
= {
2681 .handler
= handle_insert_bp
,
2683 .cmd_startswith
= 1,
2686 cmd_parser
= &insert_bp_cmd_desc
;
2691 static const GdbCmdParseEntry remove_bp_cmd_desc
= {
2692 .handler
= handle_remove_bp
,
2694 .cmd_startswith
= 1,
2697 cmd_parser
= &remove_bp_cmd_desc
;
2702 static const GdbCmdParseEntry set_thread_cmd_desc
= {
2703 .handler
= handle_set_thread
,
2705 .cmd_startswith
= 1,
2708 cmd_parser
= &set_thread_cmd_desc
;
2713 static const GdbCmdParseEntry thread_alive_cmd_desc
= {
2714 .handler
= handle_thread_alive
,
2716 .cmd_startswith
= 1,
2719 cmd_parser
= &thread_alive_cmd_desc
;
2724 static const GdbCmdParseEntry gen_query_cmd_desc
= {
2725 .handler
= handle_gen_query
,
2727 .cmd_startswith
= 1,
2730 cmd_parser
= &gen_query_cmd_desc
;
2735 static const GdbCmdParseEntry gen_set_cmd_desc
= {
2736 .handler
= handle_gen_set
,
2738 .cmd_startswith
= 1,
2741 cmd_parser
= &gen_set_cmd_desc
;
2745 /* put empty packet */
2751 run_cmd_parser(line_buf
, cmd_parser
);
2757 void gdb_set_stop_cpu(CPUState
*cpu
)
2759 GDBProcess
*p
= gdb_get_cpu_process(cpu
);
2763 * Having a stop CPU corresponding to a process that is not attached
2764 * confuses GDB. So we ignore the request.
2769 gdbserver_state
.c_cpu
= cpu
;
2770 gdbserver_state
.g_cpu
= cpu
;
2773 #ifndef CONFIG_USER_ONLY
2774 static void gdb_vm_state_change(void *opaque
, bool running
, RunState state
)
2776 CPUState
*cpu
= gdbserver_state
.c_cpu
;
2777 g_autoptr(GString
) buf
= g_string_new(NULL
);
2778 g_autoptr(GString
) tid
= g_string_new(NULL
);
2782 if (running
|| gdbserver_state
.state
== RS_INACTIVE
) {
2785 /* Is there a GDB syscall waiting to be sent? */
2786 if (gdbserver_state
.current_syscall_cb
) {
2787 put_packet(gdbserver_state
.syscall_buf
);
2792 /* No process attached */
2796 gdb_append_thread_id(cpu
, tid
);
2799 case RUN_STATE_DEBUG
:
2800 if (cpu
->watchpoint_hit
) {
2801 switch (cpu
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
2812 trace_gdbstub_hit_watchpoint(type
, cpu_gdb_index(cpu
),
2813 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
2814 g_string_printf(buf
, "T%02xthread:%s;%swatch:" TARGET_FMT_lx
";",
2815 GDB_SIGNAL_TRAP
, tid
->str
, type
,
2816 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
2817 cpu
->watchpoint_hit
= NULL
;
2820 trace_gdbstub_hit_break();
2823 ret
= GDB_SIGNAL_TRAP
;
2825 case RUN_STATE_PAUSED
:
2826 trace_gdbstub_hit_paused();
2827 ret
= GDB_SIGNAL_INT
;
2829 case RUN_STATE_SHUTDOWN
:
2830 trace_gdbstub_hit_shutdown();
2831 ret
= GDB_SIGNAL_QUIT
;
2833 case RUN_STATE_IO_ERROR
:
2834 trace_gdbstub_hit_io_error();
2835 ret
= GDB_SIGNAL_IO
;
2837 case RUN_STATE_WATCHDOG
:
2838 trace_gdbstub_hit_watchdog();
2839 ret
= GDB_SIGNAL_ALRM
;
2841 case RUN_STATE_INTERNAL_ERROR
:
2842 trace_gdbstub_hit_internal_error();
2843 ret
= GDB_SIGNAL_ABRT
;
2845 case RUN_STATE_SAVE_VM
:
2846 case RUN_STATE_RESTORE_VM
:
2848 case RUN_STATE_FINISH_MIGRATE
:
2849 ret
= GDB_SIGNAL_XCPU
;
2852 trace_gdbstub_hit_unknown(state
);
2853 ret
= GDB_SIGNAL_UNKNOWN
;
2856 gdb_set_stop_cpu(cpu
);
2857 g_string_printf(buf
, "T%02xthread:%s;", ret
, tid
->str
);
2860 put_packet(buf
->str
);
2862 /* disable single step if it was enabled */
2863 cpu_single_step(cpu
, 0);
2867 /* Send a gdb syscall request.
2868 This accepts limited printf-style format specifiers, specifically:
2869 %x - target_ulong argument printed in hex.
2870 %lx - 64-bit argument printed in hex.
2871 %s - string pointer (target_ulong) and length (int) pair. */
2872 void gdb_do_syscallv(gdb_syscall_complete_cb cb
, const char *fmt
, va_list va
)
2879 if (!gdbserver_state
.init
) {
2883 gdbserver_state
.current_syscall_cb
= cb
;
2884 #ifndef CONFIG_USER_ONLY
2885 vm_stop(RUN_STATE_DEBUG
);
2887 p
= &gdbserver_state
.syscall_buf
[0];
2888 p_end
= &gdbserver_state
.syscall_buf
[sizeof(gdbserver_state
.syscall_buf
)];
2895 addr
= va_arg(va
, target_ulong
);
2896 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
, addr
);
2899 if (*(fmt
++) != 'x')
2901 i64
= va_arg(va
, uint64_t);
2902 p
+= snprintf(p
, p_end
- p
, "%" PRIx64
, i64
);
2905 addr
= va_arg(va
, target_ulong
);
2906 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
"/%x",
2907 addr
, va_arg(va
, int));
2911 error_report("gdbstub: Bad syscall format string '%s'",
2920 #ifdef CONFIG_USER_ONLY
2921 put_packet(gdbserver_state
.syscall_buf
);
2922 /* Return control to gdb for it to process the syscall request.
2923 * Since the protocol requires that gdb hands control back to us
2924 * using a "here are the results" F packet, we don't need to check
2925 * gdb_handlesig's return value (which is the signal to deliver if
2926 * execution was resumed via a continue packet).
2928 gdb_handlesig(gdbserver_state
.c_cpu
, 0);
2930 /* In this case wait to send the syscall packet until notification that
2931 the CPU has stopped. This must be done because if the packet is sent
2932 now the reply from the syscall request could be received while the CPU
2933 is still in the running state, which can cause packets to be dropped
2934 and state transition 'T' packets to be sent while the syscall is still
2936 qemu_cpu_kick(gdbserver_state
.c_cpu
);
2940 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
2945 gdb_do_syscallv(cb
, fmt
, va
);
2949 static void gdb_read_byte(uint8_t ch
)
2953 #ifndef CONFIG_USER_ONLY
2954 if (gdbserver_state
.last_packet
->len
) {
2955 /* Waiting for a response to the last packet. If we see the start
2956 of a new command then abandon the previous response. */
2958 trace_gdbstub_err_got_nack();
2959 put_buffer(gdbserver_state
.last_packet
->data
,
2960 gdbserver_state
.last_packet
->len
);
2961 } else if (ch
== '+') {
2962 trace_gdbstub_io_got_ack();
2964 trace_gdbstub_io_got_unexpected(ch
);
2967 if (ch
== '+' || ch
== '$') {
2968 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
2973 if (runstate_is_running()) {
2974 /* when the CPU is running, we cannot do anything except stop
2975 it when receiving a char */
2976 vm_stop(RUN_STATE_PAUSED
);
2980 switch(gdbserver_state
.state
) {
2983 /* start of command packet */
2984 gdbserver_state
.line_buf_index
= 0;
2985 gdbserver_state
.line_sum
= 0;
2986 gdbserver_state
.state
= RS_GETLINE
;
2988 trace_gdbstub_err_garbage(ch
);
2993 /* start escape sequence */
2994 gdbserver_state
.state
= RS_GETLINE_ESC
;
2995 gdbserver_state
.line_sum
+= ch
;
2996 } else if (ch
== '*') {
2997 /* start run length encoding sequence */
2998 gdbserver_state
.state
= RS_GETLINE_RLE
;
2999 gdbserver_state
.line_sum
+= ch
;
3000 } else if (ch
== '#') {
3001 /* end of command, start of checksum*/
3002 gdbserver_state
.state
= RS_CHKSUM1
;
3003 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
3004 trace_gdbstub_err_overrun();
3005 gdbserver_state
.state
= RS_IDLE
;
3007 /* unescaped command character */
3008 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
;
3009 gdbserver_state
.line_sum
+= ch
;
3012 case RS_GETLINE_ESC
:
3014 /* unexpected end of command in escape sequence */
3015 gdbserver_state
.state
= RS_CHKSUM1
;
3016 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
3017 /* command buffer overrun */
3018 trace_gdbstub_err_overrun();
3019 gdbserver_state
.state
= RS_IDLE
;
3021 /* parse escaped character and leave escape state */
3022 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
^ 0x20;
3023 gdbserver_state
.line_sum
+= ch
;
3024 gdbserver_state
.state
= RS_GETLINE
;
3027 case RS_GETLINE_RLE
:
3029 * Run-length encoding is explained in "Debugging with GDB /
3030 * Appendix E GDB Remote Serial Protocol / Overview".
3032 if (ch
< ' ' || ch
== '#' || ch
== '$' || ch
> 126) {
3033 /* invalid RLE count encoding */
3034 trace_gdbstub_err_invalid_repeat(ch
);
3035 gdbserver_state
.state
= RS_GETLINE
;
3037 /* decode repeat length */
3038 int repeat
= ch
- ' ' + 3;
3039 if (gdbserver_state
.line_buf_index
+ repeat
>= sizeof(gdbserver_state
.line_buf
) - 1) {
3040 /* that many repeats would overrun the command buffer */
3041 trace_gdbstub_err_overrun();
3042 gdbserver_state
.state
= RS_IDLE
;
3043 } else if (gdbserver_state
.line_buf_index
< 1) {
3044 /* got a repeat but we have nothing to repeat */
3045 trace_gdbstub_err_invalid_rle();
3046 gdbserver_state
.state
= RS_GETLINE
;
3048 /* repeat the last character */
3049 memset(gdbserver_state
.line_buf
+ gdbserver_state
.line_buf_index
,
3050 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
- 1], repeat
);
3051 gdbserver_state
.line_buf_index
+= repeat
;
3052 gdbserver_state
.line_sum
+= ch
;
3053 gdbserver_state
.state
= RS_GETLINE
;
3058 /* get high hex digit of checksum */
3059 if (!isxdigit(ch
)) {
3060 trace_gdbstub_err_checksum_invalid(ch
);
3061 gdbserver_state
.state
= RS_GETLINE
;
3064 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
] = '\0';
3065 gdbserver_state
.line_csum
= fromhex(ch
) << 4;
3066 gdbserver_state
.state
= RS_CHKSUM2
;
3069 /* get low hex digit of checksum */
3070 if (!isxdigit(ch
)) {
3071 trace_gdbstub_err_checksum_invalid(ch
);
3072 gdbserver_state
.state
= RS_GETLINE
;
3075 gdbserver_state
.line_csum
|= fromhex(ch
);
3077 if (gdbserver_state
.line_csum
!= (gdbserver_state
.line_sum
& 0xff)) {
3078 trace_gdbstub_err_checksum_incorrect(gdbserver_state
.line_sum
, gdbserver_state
.line_csum
);
3079 /* send NAK reply */
3081 put_buffer(&reply
, 1);
3082 gdbserver_state
.state
= RS_IDLE
;
3084 /* send ACK reply */
3086 put_buffer(&reply
, 1);
3087 gdbserver_state
.state
= gdb_handle_packet(gdbserver_state
.line_buf
);
3096 /* Tell the remote gdb that the process has exited. */
3097 void gdb_exit(int code
)
3101 if (!gdbserver_state
.init
) {
3104 #ifdef CONFIG_USER_ONLY
3105 if (gdbserver_state
.socket_path
) {
3106 unlink(gdbserver_state
.socket_path
);
3108 if (gdbserver_state
.fd
< 0) {
3113 trace_gdbstub_op_exiting((uint8_t)code
);
3115 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
3118 #ifndef CONFIG_USER_ONLY
3119 qemu_chr_fe_deinit(&gdbserver_state
.chr
, true);
3124 * Create the process that will contain all the "orphan" CPUs (that are not
3125 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
3126 * be attachable and thus will be invisible to the user.
3128 static void create_default_process(GDBState
*s
)
3130 GDBProcess
*process
;
3133 if (gdbserver_state
.process_num
) {
3134 max_pid
= s
->processes
[s
->process_num
- 1].pid
;
3137 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
3138 process
= &s
->processes
[s
->process_num
- 1];
3140 /* We need an available PID slot for this process */
3141 assert(max_pid
< UINT32_MAX
);
3143 process
->pid
= max_pid
+ 1;
3144 process
->attached
= false;
3145 process
->target_xml
[0] = '\0';
3148 #ifdef CONFIG_USER_ONLY
3150 gdb_handlesig(CPUState
*cpu
, int sig
)
3155 if (!gdbserver_state
.init
|| gdbserver_state
.fd
< 0) {
3159 /* disable single step if it was enabled */
3160 cpu_single_step(cpu
, 0);
3164 gdb_set_stop_cpu(cpu
);
3165 g_string_printf(gdbserver_state
.str_buf
,
3166 "T%02xthread:", target_signal_to_gdb(sig
));
3167 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
3168 g_string_append_c(gdbserver_state
.str_buf
, ';');
3171 /* put_packet() might have detected that the peer terminated the
3173 if (gdbserver_state
.fd
< 0) {
3178 gdbserver_state
.state
= RS_IDLE
;
3179 gdbserver_state
.running_state
= 0;
3180 while (gdbserver_state
.running_state
== 0) {
3181 n
= read(gdbserver_state
.fd
, buf
, 256);
3185 for (i
= 0; i
< n
; i
++) {
3186 gdb_read_byte(buf
[i
]);
3189 /* XXX: Connection closed. Should probably wait for another
3190 connection before continuing. */
3192 close(gdbserver_state
.fd
);
3194 gdbserver_state
.fd
= -1;
3198 sig
= gdbserver_state
.signal
;
3199 gdbserver_state
.signal
= 0;
3203 /* Tell the remote gdb that the process has exited due to SIG. */
3204 void gdb_signalled(CPUArchState
*env
, int sig
)
3208 if (!gdbserver_state
.init
|| gdbserver_state
.fd
< 0) {
3212 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb(sig
));
3216 static void gdb_accept_init(int fd
)
3218 init_gdbserver_state();
3219 create_default_process(&gdbserver_state
);
3220 gdbserver_state
.processes
[0].attached
= true;
3221 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
3222 gdbserver_state
.g_cpu
= gdbserver_state
.c_cpu
;
3223 gdbserver_state
.fd
= fd
;
3224 gdb_has_xml
= false;
3227 static bool gdb_accept_socket(int gdb_fd
)
3232 fd
= accept(gdb_fd
, NULL
, NULL
);
3233 if (fd
< 0 && errno
!= EINTR
) {
3234 perror("accept socket");
3236 } else if (fd
>= 0) {
3237 qemu_set_cloexec(fd
);
3242 gdb_accept_init(fd
);
3246 static int gdbserver_open_socket(const char *path
)
3248 struct sockaddr_un sockaddr
= {};
3251 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
3253 perror("create socket");
3257 sockaddr
.sun_family
= AF_UNIX
;
3258 pstrcpy(sockaddr
.sun_path
, sizeof(sockaddr
.sun_path
) - 1, path
);
3259 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
3261 perror("bind socket");
3265 ret
= listen(fd
, 1);
3267 perror("listen socket");
3275 static bool gdb_accept_tcp(int gdb_fd
)
3277 struct sockaddr_in sockaddr
= {};
3282 len
= sizeof(sockaddr
);
3283 fd
= accept(gdb_fd
, (struct sockaddr
*)&sockaddr
, &len
);
3284 if (fd
< 0 && errno
!= EINTR
) {
3287 } else if (fd
>= 0) {
3288 qemu_set_cloexec(fd
);
3293 /* set short latency */
3294 if (socket_set_nodelay(fd
)) {
3295 perror("setsockopt");
3300 gdb_accept_init(fd
);
3304 static int gdbserver_open_port(int port
)
3306 struct sockaddr_in sockaddr
;
3309 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3314 qemu_set_cloexec(fd
);
3316 socket_set_fast_reuse(fd
);
3318 sockaddr
.sin_family
= AF_INET
;
3319 sockaddr
.sin_port
= htons(port
);
3320 sockaddr
.sin_addr
.s_addr
= 0;
3321 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
3327 ret
= listen(fd
, 1);
3337 int gdbserver_start(const char *port_or_path
)
3339 int port
= g_ascii_strtoull(port_or_path
, NULL
, 10);
3343 gdb_fd
= gdbserver_open_port(port
);
3345 gdb_fd
= gdbserver_open_socket(port_or_path
);
3352 if (port
> 0 && gdb_accept_tcp(gdb_fd
)) {
3354 } else if (gdb_accept_socket(gdb_fd
)) {
3355 gdbserver_state
.socket_path
= g_strdup(port_or_path
);
3364 /* Disable gdb stub for child processes. */
3365 void gdbserver_fork(CPUState
*cpu
)
3367 if (!gdbserver_state
.init
|| gdbserver_state
.fd
< 0) {
3370 close(gdbserver_state
.fd
);
3371 gdbserver_state
.fd
= -1;
3372 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
3373 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
3376 static int gdb_chr_can_receive(void *opaque
)
3378 /* We can handle an arbitrarily large amount of data.
3379 Pick the maximum packet size, which is as good as anything. */
3380 return MAX_PACKET_LENGTH
;
3383 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
3387 for (i
= 0; i
< size
; i
++) {
3388 gdb_read_byte(buf
[i
]);
3392 static void gdb_chr_event(void *opaque
, QEMUChrEvent event
)
3395 GDBState
*s
= (GDBState
*) opaque
;
3398 case CHR_EVENT_OPENED
:
3399 /* Start with first process attached, others detached */
3400 for (i
= 0; i
< s
->process_num
; i
++) {
3401 s
->processes
[i
].attached
= !i
;
3404 s
->c_cpu
= gdb_first_attached_cpu();
3405 s
->g_cpu
= s
->c_cpu
;
3407 vm_stop(RUN_STATE_PAUSED
);
3408 replay_gdb_attached();
3409 gdb_has_xml
= false;
3416 static int gdb_monitor_write(Chardev
*chr
, const uint8_t *buf
, int len
)
3418 g_autoptr(GString
) hex_buf
= g_string_new("O");
3419 memtohex(hex_buf
, buf
, len
);
3420 put_packet(hex_buf
->str
);
3425 static void gdb_sigterm_handler(int signal
)
3427 if (runstate_is_running()) {
3428 vm_stop(RUN_STATE_PAUSED
);
3433 static void gdb_monitor_open(Chardev
*chr
, ChardevBackend
*backend
,
3434 bool *be_opened
, Error
**errp
)
3439 static void char_gdb_class_init(ObjectClass
*oc
, void *data
)
3441 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
3443 cc
->internal
= true;
3444 cc
->open
= gdb_monitor_open
;
3445 cc
->chr_write
= gdb_monitor_write
;
3448 #define TYPE_CHARDEV_GDB "chardev-gdb"
3450 static const TypeInfo char_gdb_type_info
= {
3451 .name
= TYPE_CHARDEV_GDB
,
3452 .parent
= TYPE_CHARDEV
,
3453 .class_init
= char_gdb_class_init
,
3456 static int find_cpu_clusters(Object
*child
, void *opaque
)
3458 if (object_dynamic_cast(child
, TYPE_CPU_CLUSTER
)) {
3459 GDBState
*s
= (GDBState
*) opaque
;
3460 CPUClusterState
*cluster
= CPU_CLUSTER(child
);
3461 GDBProcess
*process
;
3463 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
3465 process
= &s
->processes
[s
->process_num
- 1];
3468 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
3469 * runtime, we enforce here that the machine does not use a cluster ID
3470 * that would lead to PID 0.
3472 assert(cluster
->cluster_id
!= UINT32_MAX
);
3473 process
->pid
= cluster
->cluster_id
+ 1;
3474 process
->attached
= false;
3475 process
->target_xml
[0] = '\0';
3480 return object_child_foreach(child
, find_cpu_clusters
, opaque
);
3483 static int pid_order(const void *a
, const void *b
)
3485 GDBProcess
*pa
= (GDBProcess
*) a
;
3486 GDBProcess
*pb
= (GDBProcess
*) b
;
3488 if (pa
->pid
< pb
->pid
) {
3490 } else if (pa
->pid
> pb
->pid
) {
3497 static void create_processes(GDBState
*s
)
3499 object_child_foreach(object_get_root(), find_cpu_clusters
, s
);
3501 if (gdbserver_state
.processes
) {
3503 qsort(gdbserver_state
.processes
, gdbserver_state
.process_num
, sizeof(gdbserver_state
.processes
[0]), pid_order
);
3506 create_default_process(s
);
3509 int gdbserver_start(const char *device
)
3511 trace_gdbstub_op_start(device
);
3513 char gdbstub_device_name
[128];
3514 Chardev
*chr
= NULL
;
3518 error_report("gdbstub: meaningless to attach gdb to a "
3519 "machine without any CPU.");
3525 if (strcmp(device
, "none") != 0) {
3526 if (strstart(device
, "tcp:", NULL
)) {
3527 /* enforce required TCP attributes */
3528 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
3529 "%s,wait=off,nodelay=on,server=on", device
);
3530 device
= gdbstub_device_name
;
3533 else if (strcmp(device
, "stdio") == 0) {
3534 struct sigaction act
;
3536 memset(&act
, 0, sizeof(act
));
3537 act
.sa_handler
= gdb_sigterm_handler
;
3538 sigaction(SIGINT
, &act
, NULL
);
3542 * FIXME: it's a bit weird to allow using a mux chardev here
3543 * and implicitly setup a monitor. We may want to break this.
3545 chr
= qemu_chr_new_noreplay("gdb", device
, true, NULL
);
3550 if (!gdbserver_state
.init
) {
3551 init_gdbserver_state();
3553 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
3555 /* Initialize a monitor terminal for gdb */
3556 mon_chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_GDB
,
3557 NULL
, NULL
, &error_abort
);
3558 monitor_init_hmp(mon_chr
, false, &error_abort
);
3560 qemu_chr_fe_deinit(&gdbserver_state
.chr
, true);
3561 mon_chr
= gdbserver_state
.mon_chr
;
3562 reset_gdbserver_state();
3565 create_processes(&gdbserver_state
);
3568 qemu_chr_fe_init(&gdbserver_state
.chr
, chr
, &error_abort
);
3569 qemu_chr_fe_set_handlers(&gdbserver_state
.chr
, gdb_chr_can_receive
,
3570 gdb_chr_receive
, gdb_chr_event
,
3571 NULL
, &gdbserver_state
, NULL
, true);
3573 gdbserver_state
.state
= chr
? RS_IDLE
: RS_INACTIVE
;
3574 gdbserver_state
.mon_chr
= mon_chr
;
3575 gdbserver_state
.current_syscall_cb
= NULL
;
3580 static void register_types(void)
3582 type_register_static(&char_gdb_type_info
);
3585 type_init(register_types
);