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 "qapi/error.h"
28 #include "qemu/error-report.h"
29 #include "qemu/ctype.h"
30 #include "qemu/cutils.h"
31 #include "qemu/module.h"
33 #include "exec/gdbstub.h"
34 #ifdef CONFIG_USER_ONLY
37 #include "monitor/monitor.h"
38 #include "chardev/char.h"
39 #include "chardev/char-fe.h"
40 #include "hw/cpu/cluster.h"
41 #include "hw/boards.h"
44 #define MAX_PACKET_LENGTH 4096
46 #include "qemu/sockets.h"
47 #include "sysemu/hw_accel.h"
48 #include "sysemu/runstate.h"
49 #include "semihosting/semihost.h"
50 #include "exec/exec-all.h"
51 #include "exec/replay-core.h"
53 #include "internals.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 * What single-step modes are supported is accelerator dependent.
388 * By default try to use no IRQs and no timers while single
389 * stepping so as to make single stepping like a typical ICE HW step.
391 gdbserver_state
.supported_sstep_flags
= accel_supported_gdbstub_sstep_flags();
392 gdbserver_state
.sstep_flags
= SSTEP_ENABLE
| SSTEP_NOIRQ
| SSTEP_NOTIMER
;
393 gdbserver_state
.sstep_flags
&= gdbserver_state
.supported_sstep_flags
;
396 #ifndef CONFIG_USER_ONLY
397 static void reset_gdbserver_state(void)
399 g_free(gdbserver_state
.processes
);
400 gdbserver_state
.processes
= NULL
;
401 gdbserver_state
.process_num
= 0;
407 #ifdef CONFIG_USER_ONLY
409 static int get_char(void)
415 ret
= recv(gdbserver_state
.fd
, &ch
, 1, 0);
417 if (errno
== ECONNRESET
)
418 gdbserver_state
.fd
= -1;
421 } else if (ret
== 0) {
422 close(gdbserver_state
.fd
);
423 gdbserver_state
.fd
= -1;
434 * Return true if there is a GDB currently connected to the stub
435 * and attached to a CPU
437 static bool gdb_attached(void)
439 return gdbserver_state
.init
&& gdbserver_state
.c_cpu
;
448 /* Decide if either remote gdb syscalls or native file IO should be used. */
449 int use_gdb_syscalls(void)
451 SemihostingTarget target
= semihosting_get_target();
452 if (target
== SEMIHOSTING_TARGET_NATIVE
) {
453 /* -semihosting-config target=native */
455 } else if (target
== SEMIHOSTING_TARGET_GDB
) {
456 /* -semihosting-config target=gdb */
460 /* -semihosting-config target=auto */
461 /* On the first call check if gdb is connected and remember. */
462 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
463 gdb_syscall_mode
= gdb_attached() ? GDB_SYS_ENABLED
: GDB_SYS_DISABLED
;
465 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
468 static bool stub_can_reverse(void)
470 #ifdef CONFIG_USER_ONLY
473 return replay_mode
== REPLAY_MODE_PLAY
;
477 /* Resume execution. */
478 static inline void gdb_continue(void)
481 #ifdef CONFIG_USER_ONLY
482 gdbserver_state
.running_state
= 1;
483 trace_gdbstub_op_continue();
485 if (!runstate_needs_reset()) {
486 trace_gdbstub_op_continue();
493 * Resume execution, per CPU actions. For user-mode emulation it's
494 * equivalent to gdb_continue.
496 static int gdb_continue_partial(char *newstates
)
500 #ifdef CONFIG_USER_ONLY
502 * This is not exactly accurate, but it's an improvement compared to the
503 * previous situation, where only one CPU would be single-stepped.
506 if (newstates
[cpu
->cpu_index
] == 's') {
507 trace_gdbstub_op_stepping(cpu
->cpu_index
);
508 cpu_single_step(cpu
, gdbserver_state
.sstep_flags
);
511 gdbserver_state
.running_state
= 1;
515 if (!runstate_needs_reset()) {
516 bool step_requested
= false;
518 if (newstates
[cpu
->cpu_index
] == 's') {
519 step_requested
= true;
524 if (vm_prepare_start(step_requested
)) {
529 switch (newstates
[cpu
->cpu_index
]) {
532 break; /* nothing to do here */
534 trace_gdbstub_op_stepping(cpu
->cpu_index
);
535 cpu_single_step(cpu
, gdbserver_state
.sstep_flags
);
540 trace_gdbstub_op_continue_cpu(cpu
->cpu_index
);
551 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, true);
557 static void put_buffer(const uint8_t *buf
, int len
)
559 #ifdef CONFIG_USER_ONLY
563 ret
= send(gdbserver_state
.fd
, buf
, len
, 0);
573 /* XXX this blocks entire thread. Rewrite to use
574 * qemu_chr_fe_write and background I/O callbacks */
575 qemu_chr_fe_write_all(&gdbserver_state
.chr
, buf
, len
);
579 static inline int fromhex(int v
)
581 if (v
>= '0' && v
<= '9')
583 else if (v
>= 'A' && v
<= 'F')
585 else if (v
>= 'a' && v
<= 'f')
591 static inline int tohex(int v
)
599 /* writes 2*len+1 bytes in buf */
600 static void memtohex(GString
*buf
, const uint8_t *mem
, int len
)
603 for(i
= 0; i
< len
; i
++) {
605 g_string_append_c(buf
, tohex(c
>> 4));
606 g_string_append_c(buf
, tohex(c
& 0xf));
608 g_string_append_c(buf
, '\0');
611 static void hextomem(GByteArray
*mem
, const char *buf
, int len
)
615 for(i
= 0; i
< len
; i
++) {
616 guint8 byte
= fromhex(buf
[0]) << 4 | fromhex(buf
[1]);
617 g_byte_array_append(mem
, &byte
, 1);
622 static void hexdump(const char *buf
, int len
,
623 void (*trace_fn
)(size_t ofs
, char const *text
))
625 char line_buffer
[3 * 16 + 4 + 16 + 1];
628 for (i
= 0; i
< len
|| (i
& 0xF); ++i
) {
629 size_t byte_ofs
= i
& 15;
632 memset(line_buffer
, ' ', 3 * 16 + 4 + 16);
633 line_buffer
[3 * 16 + 4 + 16] = 0;
636 size_t col_group
= (i
>> 2) & 3;
637 size_t hex_col
= byte_ofs
* 3 + col_group
;
638 size_t txt_col
= 3 * 16 + 4 + byte_ofs
;
643 line_buffer
[hex_col
+ 0] = tohex((value
>> 4) & 0xF);
644 line_buffer
[hex_col
+ 1] = tohex((value
>> 0) & 0xF);
645 line_buffer
[txt_col
+ 0] = (value
>= ' ' && value
< 127)
651 trace_fn(i
& -16, line_buffer
);
655 /* return -1 if error, 0 if OK */
656 static int put_packet_binary(const char *buf
, int len
, bool dump
)
661 if (dump
&& trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY
)) {
662 hexdump(buf
, len
, trace_gdbstub_io_binaryreply
);
666 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
667 g_byte_array_append(gdbserver_state
.last_packet
,
668 (const uint8_t *) "$", 1);
669 g_byte_array_append(gdbserver_state
.last_packet
,
670 (const uint8_t *) buf
, len
);
672 for(i
= 0; i
< len
; i
++) {
676 footer
[1] = tohex((csum
>> 4) & 0xf);
677 footer
[2] = tohex((csum
) & 0xf);
678 g_byte_array_append(gdbserver_state
.last_packet
, footer
, 3);
680 put_buffer(gdbserver_state
.last_packet
->data
,
681 gdbserver_state
.last_packet
->len
);
683 #ifdef CONFIG_USER_ONLY
696 /* return -1 if error, 0 if OK */
697 static int put_packet(const char *buf
)
699 trace_gdbstub_io_reply(buf
);
701 return put_packet_binary(buf
, strlen(buf
), false);
704 static void put_strbuf(void)
706 put_packet(gdbserver_state
.str_buf
->str
);
709 /* Encode data using the encoding for 'x' packets. */
710 static void memtox(GString
*buf
, const char *mem
, int len
)
717 case '#': case '$': case '*': case '}':
718 g_string_append_c(buf
, '}');
719 g_string_append_c(buf
, c
^ 0x20);
722 g_string_append_c(buf
, c
);
728 static uint32_t gdb_get_cpu_pid(CPUState
*cpu
)
730 /* TODO: In user mode, we should use the task state PID */
731 if (cpu
->cluster_index
== UNASSIGNED_CLUSTER_INDEX
) {
732 /* Return the default process' PID */
733 int index
= gdbserver_state
.process_num
- 1;
734 return gdbserver_state
.processes
[index
].pid
;
736 return cpu
->cluster_index
+ 1;
739 static GDBProcess
*gdb_get_process(uint32_t pid
)
744 /* 0 means any process, we take the first one */
745 return &gdbserver_state
.processes
[0];
748 for (i
= 0; i
< gdbserver_state
.process_num
; i
++) {
749 if (gdbserver_state
.processes
[i
].pid
== pid
) {
750 return &gdbserver_state
.processes
[i
];
757 static GDBProcess
*gdb_get_cpu_process(CPUState
*cpu
)
759 return gdb_get_process(gdb_get_cpu_pid(cpu
));
762 static CPUState
*find_cpu(uint32_t thread_id
)
767 if (cpu_gdb_index(cpu
) == thread_id
) {
775 static CPUState
*get_first_cpu_in_process(GDBProcess
*process
)
780 if (gdb_get_cpu_pid(cpu
) == process
->pid
) {
788 static CPUState
*gdb_next_cpu_in_process(CPUState
*cpu
)
790 uint32_t pid
= gdb_get_cpu_pid(cpu
);
794 if (gdb_get_cpu_pid(cpu
) == pid
) {
804 /* Return the cpu following @cpu, while ignoring unattached processes. */
805 static CPUState
*gdb_next_attached_cpu(CPUState
*cpu
)
810 if (gdb_get_cpu_process(cpu
)->attached
) {
820 /* Return the first attached cpu */
821 static CPUState
*gdb_first_attached_cpu(void)
823 CPUState
*cpu
= first_cpu
;
824 GDBProcess
*process
= gdb_get_cpu_process(cpu
);
826 if (!process
->attached
) {
827 return gdb_next_attached_cpu(cpu
);
833 static CPUState
*gdb_get_cpu(uint32_t pid
, uint32_t tid
)
839 /* 0 means any process/thread, we take the first attached one */
840 return gdb_first_attached_cpu();
841 } else if (pid
&& !tid
) {
842 /* any thread in a specific process */
843 process
= gdb_get_process(pid
);
845 if (process
== NULL
) {
849 if (!process
->attached
) {
853 return get_first_cpu_in_process(process
);
855 /* a specific thread */
862 process
= gdb_get_cpu_process(cpu
);
864 if (pid
&& process
->pid
!= pid
) {
868 if (!process
->attached
) {
876 static const char *get_feature_xml(const char *p
, const char **newp
,
882 CPUState
*cpu
= get_first_cpu_in_process(process
);
883 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
886 while (p
[len
] && p
[len
] != ':')
891 if (strncmp(p
, "target.xml", len
) == 0) {
892 char *buf
= process
->target_xml
;
893 const size_t buf_sz
= sizeof(process
->target_xml
);
895 /* Generate the XML description for this CPU. */
900 "<?xml version=\"1.0\"?>"
901 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
903 if (cc
->gdb_arch_name
) {
904 gchar
*arch
= cc
->gdb_arch_name(cpu
);
905 pstrcat(buf
, buf_sz
, "<architecture>");
906 pstrcat(buf
, buf_sz
, arch
);
907 pstrcat(buf
, buf_sz
, "</architecture>");
910 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
911 pstrcat(buf
, buf_sz
, cc
->gdb_core_xml_file
);
912 pstrcat(buf
, buf_sz
, "\"/>");
913 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
914 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
915 pstrcat(buf
, buf_sz
, r
->xml
);
916 pstrcat(buf
, buf_sz
, "\"/>");
918 pstrcat(buf
, buf_sz
, "</target>");
922 if (cc
->gdb_get_dynamic_xml
) {
923 char *xmlname
= g_strndup(p
, len
);
924 const char *xml
= cc
->gdb_get_dynamic_xml(cpu
, xmlname
);
932 name
= xml_builtin
[i
][0];
933 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
936 return name
? xml_builtin
[i
][1] : NULL
;
939 static int gdb_read_register(CPUState
*cpu
, GByteArray
*buf
, int reg
)
941 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
942 CPUArchState
*env
= cpu
->env_ptr
;
945 if (reg
< cc
->gdb_num_core_regs
) {
946 return cc
->gdb_read_register(cpu
, buf
, reg
);
949 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
950 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
951 return r
->get_reg(env
, buf
, reg
- r
->base_reg
);
957 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
959 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
960 CPUArchState
*env
= cpu
->env_ptr
;
963 if (reg
< cc
->gdb_num_core_regs
) {
964 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
967 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
968 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
969 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
975 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
976 specifies the first register number and these registers are included in
977 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
978 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
981 void gdb_register_coprocessor(CPUState
*cpu
,
982 gdb_get_reg_cb get_reg
, gdb_set_reg_cb set_reg
,
983 int num_regs
, const char *xml
, int g_pos
)
986 GDBRegisterState
**p
;
990 /* Check for duplicates. */
991 if (strcmp((*p
)->xml
, xml
) == 0)
996 s
= g_new0(GDBRegisterState
, 1);
997 s
->base_reg
= cpu
->gdb_num_regs
;
998 s
->num_regs
= num_regs
;
999 s
->get_reg
= get_reg
;
1000 s
->set_reg
= set_reg
;
1003 /* Add to end of list. */
1004 cpu
->gdb_num_regs
+= num_regs
;
1007 if (g_pos
!= s
->base_reg
) {
1008 error_report("Error: Bad gdb register numbering for '%s', "
1009 "expected %d got %d", xml
, g_pos
, s
->base_reg
);
1011 cpu
->gdb_num_g_regs
= cpu
->gdb_num_regs
;
1016 static void gdb_process_breakpoint_remove_all(GDBProcess
*p
)
1018 CPUState
*cpu
= get_first_cpu_in_process(p
);
1021 gdb_breakpoint_remove_all(cpu
);
1022 cpu
= gdb_next_cpu_in_process(cpu
);
1027 static void gdb_set_cpu_pc(target_ulong pc
)
1029 CPUState
*cpu
= gdbserver_state
.c_cpu
;
1031 cpu_synchronize_state(cpu
);
1032 cpu_set_pc(cpu
, pc
);
1035 static void gdb_append_thread_id(CPUState
*cpu
, GString
*buf
)
1037 if (gdbserver_state
.multiprocess
) {
1038 g_string_append_printf(buf
, "p%02x.%02x",
1039 gdb_get_cpu_pid(cpu
), cpu_gdb_index(cpu
));
1041 g_string_append_printf(buf
, "%02x", cpu_gdb_index(cpu
));
1045 typedef enum GDBThreadIdKind
{
1047 GDB_ALL_THREADS
, /* One process, all threads */
1052 static GDBThreadIdKind
read_thread_id(const char *buf
, const char **end_buf
,
1053 uint32_t *pid
, uint32_t *tid
)
1060 ret
= qemu_strtoul(buf
, &buf
, 16, &p
);
1063 return GDB_READ_THREAD_ERR
;
1072 ret
= qemu_strtoul(buf
, &buf
, 16, &t
);
1075 return GDB_READ_THREAD_ERR
;
1081 return GDB_ALL_PROCESSES
;
1089 return GDB_ALL_THREADS
;
1096 return GDB_ONE_THREAD
;
1100 * gdb_handle_vcont - Parses and handles a vCont packet.
1101 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1102 * a format error, 0 on success.
1104 static int gdb_handle_vcont(const char *p
)
1106 int res
, signal
= 0;
1111 GDBProcess
*process
;
1113 GDBThreadIdKind kind
;
1114 #ifdef CONFIG_USER_ONLY
1115 int max_cpus
= 1; /* global variable max_cpus exists only in system mode */
1118 max_cpus
= max_cpus
<= cpu
->cpu_index
? cpu
->cpu_index
+ 1 : max_cpus
;
1121 MachineState
*ms
= MACHINE(qdev_get_machine());
1122 unsigned int max_cpus
= ms
->smp
.max_cpus
;
1124 /* uninitialised CPUs stay 0 */
1125 newstates
= g_new0(char, max_cpus
);
1127 /* mark valid CPUs with 1 */
1129 newstates
[cpu
->cpu_index
] = 1;
1133 * res keeps track of what error we are returning, with -ENOTSUP meaning
1134 * that the command is unknown or unsupported, thus returning an empty
1135 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1136 * or incorrect parameters passed.
1146 if (cur_action
== 'C' || cur_action
== 'S') {
1147 cur_action
= qemu_tolower(cur_action
);
1148 res
= qemu_strtoul(p
, &p
, 16, &tmp
);
1152 signal
= gdb_signal_to_target(tmp
);
1153 } else if (cur_action
!= 'c' && cur_action
!= 's') {
1154 /* unknown/invalid/unsupported command */
1159 if (*p
== '\0' || *p
== ';') {
1161 * No thread specifier, action is on "all threads". The
1162 * specification is unclear regarding the process to act on. We
1163 * choose all processes.
1165 kind
= GDB_ALL_PROCESSES
;
1166 } else if (*p
++ == ':') {
1167 kind
= read_thread_id(p
, &p
, &pid
, &tid
);
1174 case GDB_READ_THREAD_ERR
:
1178 case GDB_ALL_PROCESSES
:
1179 cpu
= gdb_first_attached_cpu();
1181 if (newstates
[cpu
->cpu_index
] == 1) {
1182 newstates
[cpu
->cpu_index
] = cur_action
;
1185 cpu
= gdb_next_attached_cpu(cpu
);
1189 case GDB_ALL_THREADS
:
1190 process
= gdb_get_process(pid
);
1192 if (!process
->attached
) {
1197 cpu
= get_first_cpu_in_process(process
);
1199 if (newstates
[cpu
->cpu_index
] == 1) {
1200 newstates
[cpu
->cpu_index
] = cur_action
;
1203 cpu
= gdb_next_cpu_in_process(cpu
);
1207 case GDB_ONE_THREAD
:
1208 cpu
= gdb_get_cpu(pid
, tid
);
1210 /* invalid CPU/thread specified */
1216 /* only use if no previous match occourred */
1217 if (newstates
[cpu
->cpu_index
] == 1) {
1218 newstates
[cpu
->cpu_index
] = cur_action
;
1223 gdbserver_state
.signal
= signal
;
1224 gdb_continue_partial(newstates
);
1232 typedef union GdbCmdVariant
{
1235 unsigned long val_ul
;
1236 unsigned long long val_ull
;
1238 GDBThreadIdKind kind
;
1244 #define get_param(p, i) (&g_array_index(p, GdbCmdVariant, i))
1246 static const char *cmd_next_param(const char *param
, const char delimiter
)
1248 static const char all_delimiters
[] = ",;:=";
1249 char curr_delimiters
[2] = {0};
1250 const char *delimiters
;
1252 if (delimiter
== '?') {
1253 delimiters
= all_delimiters
;
1254 } else if (delimiter
== '0') {
1255 return strchr(param
, '\0');
1256 } else if (delimiter
== '.' && *param
) {
1259 curr_delimiters
[0] = delimiter
;
1260 delimiters
= curr_delimiters
;
1263 param
+= strcspn(param
, delimiters
);
1270 static int cmd_parse_params(const char *data
, const char *schema
,
1273 const char *curr_schema
, *curr_data
;
1276 g_assert(params
->len
== 0);
1278 curr_schema
= schema
;
1280 while (curr_schema
[0] && curr_schema
[1] && *curr_data
) {
1281 GdbCmdVariant this_param
;
1283 switch (curr_schema
[0]) {
1285 if (qemu_strtoul(curr_data
, &curr_data
, 16,
1286 &this_param
.val_ul
)) {
1289 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1290 g_array_append_val(params
, this_param
);
1293 if (qemu_strtou64(curr_data
, &curr_data
, 16,
1294 (uint64_t *)&this_param
.val_ull
)) {
1297 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1298 g_array_append_val(params
, this_param
);
1301 this_param
.data
= curr_data
;
1302 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1303 g_array_append_val(params
, this_param
);
1306 this_param
.opcode
= *(uint8_t *)curr_data
;
1307 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1308 g_array_append_val(params
, this_param
);
1311 this_param
.thread_id
.kind
=
1312 read_thread_id(curr_data
, &curr_data
,
1313 &this_param
.thread_id
.pid
,
1314 &this_param
.thread_id
.tid
);
1315 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1316 g_array_append_val(params
, this_param
);
1319 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1330 typedef void (*GdbCmdHandler
)(GArray
*params
, void *user_ctx
);
1333 * cmd_startswith -> cmd is compared using startswith
1336 * schema definitions:
1337 * Each schema parameter entry consists of 2 chars,
1338 * the first char represents the parameter type handling
1339 * the second char represents the delimiter for the next parameter
1341 * Currently supported schema types:
1342 * 'l' -> unsigned long (stored in .val_ul)
1343 * 'L' -> unsigned long long (stored in .val_ull)
1344 * 's' -> string (stored in .data)
1345 * 'o' -> single char (stored in .opcode)
1346 * 't' -> thread id (stored in .thread_id)
1347 * '?' -> skip according to delimiter
1349 * Currently supported delimiters:
1350 * '?' -> Stop at any delimiter (",;:=\0")
1351 * '0' -> Stop at "\0"
1352 * '.' -> Skip 1 char unless reached "\0"
1353 * Any other value is treated as the delimiter value itself
1355 typedef struct GdbCmdParseEntry
{
1356 GdbCmdHandler handler
;
1358 bool cmd_startswith
;
1362 static inline int startswith(const char *string
, const char *pattern
)
1364 return !strncmp(string
, pattern
, strlen(pattern
));
1367 static int process_string_cmd(void *user_ctx
, const char *data
,
1368 const GdbCmdParseEntry
*cmds
, int num_cmds
)
1371 g_autoptr(GArray
) params
= g_array_new(false, true, sizeof(GdbCmdVariant
));
1377 for (i
= 0; i
< num_cmds
; i
++) {
1378 const GdbCmdParseEntry
*cmd
= &cmds
[i
];
1379 g_assert(cmd
->handler
&& cmd
->cmd
);
1381 if ((cmd
->cmd_startswith
&& !startswith(data
, cmd
->cmd
)) ||
1382 (!cmd
->cmd_startswith
&& strcmp(cmd
->cmd
, data
))) {
1387 if (cmd_parse_params(&data
[strlen(cmd
->cmd
)],
1388 cmd
->schema
, params
)) {
1393 cmd
->handler(params
, user_ctx
);
1400 static void run_cmd_parser(const char *data
, const GdbCmdParseEntry
*cmd
)
1406 g_string_set_size(gdbserver_state
.str_buf
, 0);
1407 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
1409 /* In case there was an error during the command parsing we must
1410 * send a NULL packet to indicate the command is not supported */
1411 if (process_string_cmd(NULL
, data
, cmd
, 1)) {
1416 static void handle_detach(GArray
*params
, void *user_ctx
)
1418 GDBProcess
*process
;
1421 if (gdbserver_state
.multiprocess
) {
1427 pid
= get_param(params
, 0)->val_ul
;
1430 process
= gdb_get_process(pid
);
1431 gdb_process_breakpoint_remove_all(process
);
1432 process
->attached
= false;
1434 if (pid
== gdb_get_cpu_pid(gdbserver_state
.c_cpu
)) {
1435 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
1438 if (pid
== gdb_get_cpu_pid(gdbserver_state
.g_cpu
)) {
1439 gdbserver_state
.g_cpu
= gdb_first_attached_cpu();
1442 if (!gdbserver_state
.c_cpu
) {
1443 /* No more process attached */
1444 gdb_syscall_mode
= GDB_SYS_DISABLED
;
1450 static void handle_thread_alive(GArray
*params
, void *user_ctx
)
1459 if (get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1464 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
1465 get_param(params
, 0)->thread_id
.tid
);
1474 static void handle_continue(GArray
*params
, void *user_ctx
)
1477 gdb_set_cpu_pc(get_param(params
, 0)->val_ull
);
1480 gdbserver_state
.signal
= 0;
1484 static void handle_cont_with_sig(GArray
*params
, void *user_ctx
)
1486 unsigned long signal
= 0;
1489 * Note: C sig;[addr] is currently unsupported and we simply
1490 * omit the addr parameter
1493 signal
= get_param(params
, 0)->val_ul
;
1496 gdbserver_state
.signal
= gdb_signal_to_target(signal
);
1497 if (gdbserver_state
.signal
== -1) {
1498 gdbserver_state
.signal
= 0;
1503 static void handle_set_thread(GArray
*params
, void *user_ctx
)
1507 if (params
->len
!= 2) {
1512 if (get_param(params
, 1)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1517 if (get_param(params
, 1)->thread_id
.kind
!= GDB_ONE_THREAD
) {
1522 cpu
= gdb_get_cpu(get_param(params
, 1)->thread_id
.pid
,
1523 get_param(params
, 1)->thread_id
.tid
);
1530 * Note: This command is deprecated and modern gdb's will be using the
1531 * vCont command instead.
1533 switch (get_param(params
, 0)->opcode
) {
1535 gdbserver_state
.c_cpu
= cpu
;
1539 gdbserver_state
.g_cpu
= cpu
;
1548 static void handle_insert_bp(GArray
*params
, void *user_ctx
)
1552 if (params
->len
!= 3) {
1557 res
= gdb_breakpoint_insert(gdbserver_state
.c_cpu
,
1558 get_param(params
, 0)->val_ul
,
1559 get_param(params
, 1)->val_ull
,
1560 get_param(params
, 2)->val_ull
);
1564 } else if (res
== -ENOSYS
) {
1572 static void handle_remove_bp(GArray
*params
, void *user_ctx
)
1576 if (params
->len
!= 3) {
1581 res
= gdb_breakpoint_remove(gdbserver_state
.c_cpu
,
1582 get_param(params
, 0)->val_ul
,
1583 get_param(params
, 1)->val_ull
,
1584 get_param(params
, 2)->val_ull
);
1588 } else if (res
== -ENOSYS
) {
1597 * handle_set/get_reg
1599 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1600 * This works, but can be very slow. Anything new enough to understand
1601 * XML also knows how to use this properly. However to use this we
1602 * need to define a local XML file as well as be talking to a
1603 * reasonably modern gdb. Responding with an empty packet will cause
1604 * the remote gdb to fallback to older methods.
1607 static void handle_set_reg(GArray
*params
, void *user_ctx
)
1616 if (params
->len
!= 2) {
1621 reg_size
= strlen(get_param(params
, 1)->data
) / 2;
1622 hextomem(gdbserver_state
.mem_buf
, get_param(params
, 1)->data
, reg_size
);
1623 gdb_write_register(gdbserver_state
.g_cpu
, gdbserver_state
.mem_buf
->data
,
1624 get_param(params
, 0)->val_ull
);
1628 static void handle_get_reg(GArray
*params
, void *user_ctx
)
1642 reg_size
= gdb_read_register(gdbserver_state
.g_cpu
,
1643 gdbserver_state
.mem_buf
,
1644 get_param(params
, 0)->val_ull
);
1649 g_byte_array_set_size(gdbserver_state
.mem_buf
, reg_size
);
1652 memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
, reg_size
);
1656 static void handle_write_mem(GArray
*params
, void *user_ctx
)
1658 if (params
->len
!= 3) {
1663 /* hextomem() reads 2*len bytes */
1664 if (get_param(params
, 1)->val_ull
>
1665 strlen(get_param(params
, 2)->data
) / 2) {
1670 hextomem(gdbserver_state
.mem_buf
, get_param(params
, 2)->data
,
1671 get_param(params
, 1)->val_ull
);
1672 if (target_memory_rw_debug(gdbserver_state
.g_cpu
,
1673 get_param(params
, 0)->val_ull
,
1674 gdbserver_state
.mem_buf
->data
,
1675 gdbserver_state
.mem_buf
->len
, true)) {
1683 static void handle_read_mem(GArray
*params
, void *user_ctx
)
1685 if (params
->len
!= 2) {
1690 /* memtohex() doubles the required space */
1691 if (get_param(params
, 1)->val_ull
> MAX_PACKET_LENGTH
/ 2) {
1696 g_byte_array_set_size(gdbserver_state
.mem_buf
,
1697 get_param(params
, 1)->val_ull
);
1699 if (target_memory_rw_debug(gdbserver_state
.g_cpu
,
1700 get_param(params
, 0)->val_ull
,
1701 gdbserver_state
.mem_buf
->data
,
1702 gdbserver_state
.mem_buf
->len
, false)) {
1707 memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
,
1708 gdbserver_state
.mem_buf
->len
);
1712 static void handle_write_all_regs(GArray
*params
, void *user_ctx
)
1714 target_ulong addr
, len
;
1722 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1723 len
= strlen(get_param(params
, 0)->data
) / 2;
1724 hextomem(gdbserver_state
.mem_buf
, get_param(params
, 0)->data
, len
);
1725 registers
= gdbserver_state
.mem_buf
->data
;
1726 for (addr
= 0; addr
< gdbserver_state
.g_cpu
->gdb_num_g_regs
&& len
> 0;
1728 reg_size
= gdb_write_register(gdbserver_state
.g_cpu
, registers
, addr
);
1730 registers
+= reg_size
;
1735 static void handle_read_all_regs(GArray
*params
, void *user_ctx
)
1737 target_ulong addr
, len
;
1739 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1740 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
1742 for (addr
= 0; addr
< gdbserver_state
.g_cpu
->gdb_num_g_regs
; addr
++) {
1743 len
+= gdb_read_register(gdbserver_state
.g_cpu
,
1744 gdbserver_state
.mem_buf
,
1747 g_assert(len
== gdbserver_state
.mem_buf
->len
);
1749 memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
, len
);
1753 static void handle_file_io(GArray
*params
, void *user_ctx
)
1755 if (params
->len
>= 1 && gdbserver_state
.current_syscall_cb
) {
1759 ret
= get_param(params
, 0)->val_ull
;
1760 if (params
->len
>= 2) {
1761 err
= get_param(params
, 1)->val_ull
;
1766 /* Convert GDB error numbers back to host error numbers. */
1767 #define E(X) case GDB_E##X: err = E##X; break
1796 gdbserver_state
.current_syscall_cb(gdbserver_state
.c_cpu
, ret
, err
);
1797 gdbserver_state
.current_syscall_cb
= NULL
;
1800 if (params
->len
>= 3 && get_param(params
, 2)->opcode
== (uint8_t)'C') {
1808 static void handle_step(GArray
*params
, void *user_ctx
)
1811 gdb_set_cpu_pc((target_ulong
)get_param(params
, 0)->val_ull
);
1814 cpu_single_step(gdbserver_state
.c_cpu
, gdbserver_state
.sstep_flags
);
1818 static void handle_backward(GArray
*params
, void *user_ctx
)
1820 if (!stub_can_reverse()) {
1823 if (params
->len
== 1) {
1824 switch (get_param(params
, 0)->opcode
) {
1826 if (replay_reverse_step()) {
1833 if (replay_reverse_continue()) {
1842 /* Default invalid command */
1846 static void handle_v_cont_query(GArray
*params
, void *user_ctx
)
1848 put_packet("vCont;c;C;s;S");
1851 static void handle_v_cont(GArray
*params
, void *user_ctx
)
1859 res
= gdb_handle_vcont(get_param(params
, 0)->data
);
1860 if ((res
== -EINVAL
) || (res
== -ERANGE
)) {
1867 static void handle_v_attach(GArray
*params
, void *user_ctx
)
1869 GDBProcess
*process
;
1872 g_string_assign(gdbserver_state
.str_buf
, "E22");
1877 process
= gdb_get_process(get_param(params
, 0)->val_ul
);
1882 cpu
= get_first_cpu_in_process(process
);
1887 process
->attached
= true;
1888 gdbserver_state
.g_cpu
= cpu
;
1889 gdbserver_state
.c_cpu
= cpu
;
1891 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
1892 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
1893 g_string_append_c(gdbserver_state
.str_buf
, ';');
1898 static void handle_v_kill(GArray
*params
, void *user_ctx
)
1900 /* Kill the target */
1902 error_report("QEMU: Terminated via GDBstub");
1907 static const GdbCmdParseEntry gdb_v_commands_table
[] = {
1908 /* Order is important if has same prefix */
1910 .handler
= handle_v_cont_query
,
1915 .handler
= handle_v_cont
,
1917 .cmd_startswith
= 1,
1921 .handler
= handle_v_attach
,
1923 .cmd_startswith
= 1,
1927 .handler
= handle_v_kill
,
1933 static void handle_v_commands(GArray
*params
, void *user_ctx
)
1939 if (process_string_cmd(NULL
, get_param(params
, 0)->data
,
1940 gdb_v_commands_table
,
1941 ARRAY_SIZE(gdb_v_commands_table
))) {
1946 static void handle_query_qemu_sstepbits(GArray
*params
, void *user_ctx
)
1948 g_string_printf(gdbserver_state
.str_buf
, "ENABLE=%x", SSTEP_ENABLE
);
1950 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOIRQ
) {
1951 g_string_append_printf(gdbserver_state
.str_buf
, ",NOIRQ=%x",
1955 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOTIMER
) {
1956 g_string_append_printf(gdbserver_state
.str_buf
, ",NOTIMER=%x",
1963 static void handle_set_qemu_sstep(GArray
*params
, void *user_ctx
)
1965 int new_sstep_flags
;
1971 new_sstep_flags
= get_param(params
, 0)->val_ul
;
1973 if (new_sstep_flags
& ~gdbserver_state
.supported_sstep_flags
) {
1978 gdbserver_state
.sstep_flags
= new_sstep_flags
;
1982 static void handle_query_qemu_sstep(GArray
*params
, void *user_ctx
)
1984 g_string_printf(gdbserver_state
.str_buf
, "0x%x",
1985 gdbserver_state
.sstep_flags
);
1989 static void handle_query_curr_tid(GArray
*params
, void *user_ctx
)
1992 GDBProcess
*process
;
1995 * "Current thread" remains vague in the spec, so always return
1996 * the first thread of the current process (gdb returns the
1999 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
2000 cpu
= get_first_cpu_in_process(process
);
2001 g_string_assign(gdbserver_state
.str_buf
, "QC");
2002 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
2006 static void handle_query_threads(GArray
*params
, void *user_ctx
)
2008 if (!gdbserver_state
.query_cpu
) {
2013 g_string_assign(gdbserver_state
.str_buf
, "m");
2014 gdb_append_thread_id(gdbserver_state
.query_cpu
, gdbserver_state
.str_buf
);
2016 gdbserver_state
.query_cpu
= gdb_next_attached_cpu(gdbserver_state
.query_cpu
);
2019 static void handle_query_first_threads(GArray
*params
, void *user_ctx
)
2021 gdbserver_state
.query_cpu
= gdb_first_attached_cpu();
2022 handle_query_threads(params
, user_ctx
);
2025 static void handle_query_thread_extra(GArray
*params
, void *user_ctx
)
2027 g_autoptr(GString
) rs
= g_string_new(NULL
);
2031 get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
2036 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
2037 get_param(params
, 0)->thread_id
.tid
);
2042 cpu_synchronize_state(cpu
);
2044 if (gdbserver_state
.multiprocess
&& (gdbserver_state
.process_num
> 1)) {
2045 /* Print the CPU model and name in multiprocess mode */
2046 ObjectClass
*oc
= object_get_class(OBJECT(cpu
));
2047 const char *cpu_model
= object_class_get_name(oc
);
2048 const char *cpu_name
=
2049 object_get_canonical_path_component(OBJECT(cpu
));
2050 g_string_printf(rs
, "%s %s [%s]", cpu_model
, cpu_name
,
2051 cpu
->halted
? "halted " : "running");
2053 g_string_printf(rs
, "CPU#%d [%s]", cpu
->cpu_index
,
2054 cpu
->halted
? "halted " : "running");
2056 trace_gdbstub_op_extra_info(rs
->str
);
2057 memtohex(gdbserver_state
.str_buf
, (uint8_t *)rs
->str
, rs
->len
);
2061 #ifdef CONFIG_USER_ONLY
2062 static void handle_query_offsets(GArray
*params
, void *user_ctx
)
2066 ts
= gdbserver_state
.c_cpu
->opaque
;
2067 g_string_printf(gdbserver_state
.str_buf
,
2068 "Text=" TARGET_ABI_FMT_lx
2069 ";Data=" TARGET_ABI_FMT_lx
2070 ";Bss=" TARGET_ABI_FMT_lx
,
2071 ts
->info
->code_offset
,
2072 ts
->info
->data_offset
,
2073 ts
->info
->data_offset
);
2077 static void handle_query_rcmd(GArray
*params
, void *user_ctx
)
2079 const guint8 zero
= 0;
2087 len
= strlen(get_param(params
, 0)->data
);
2093 g_assert(gdbserver_state
.mem_buf
->len
== 0);
2095 hextomem(gdbserver_state
.mem_buf
, get_param(params
, 0)->data
, len
);
2096 g_byte_array_append(gdbserver_state
.mem_buf
, &zero
, 1);
2097 qemu_chr_be_write(gdbserver_state
.mon_chr
, gdbserver_state
.mem_buf
->data
,
2098 gdbserver_state
.mem_buf
->len
);
2103 static void handle_query_supported(GArray
*params
, void *user_ctx
)
2107 g_string_printf(gdbserver_state
.str_buf
, "PacketSize=%x", MAX_PACKET_LENGTH
);
2108 cc
= CPU_GET_CLASS(first_cpu
);
2109 if (cc
->gdb_core_xml_file
) {
2110 g_string_append(gdbserver_state
.str_buf
, ";qXfer:features:read+");
2113 if (stub_can_reverse()) {
2114 g_string_append(gdbserver_state
.str_buf
,
2115 ";ReverseStep+;ReverseContinue+");
2118 #ifdef CONFIG_USER_ONLY
2119 if (gdbserver_state
.c_cpu
->opaque
) {
2120 g_string_append(gdbserver_state
.str_buf
, ";qXfer:auxv:read+");
2125 strstr(get_param(params
, 0)->data
, "multiprocess+")) {
2126 gdbserver_state
.multiprocess
= true;
2129 g_string_append(gdbserver_state
.str_buf
, ";vContSupported+;multiprocess+");
2133 static void handle_query_xfer_features(GArray
*params
, void *user_ctx
)
2135 GDBProcess
*process
;
2137 unsigned long len
, total_len
, addr
;
2141 if (params
->len
< 3) {
2146 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
2147 cc
= CPU_GET_CLASS(gdbserver_state
.g_cpu
);
2148 if (!cc
->gdb_core_xml_file
) {
2154 p
= get_param(params
, 0)->data
;
2155 xml
= get_feature_xml(p
, &p
, process
);
2161 addr
= get_param(params
, 1)->val_ul
;
2162 len
= get_param(params
, 2)->val_ul
;
2163 total_len
= strlen(xml
);
2164 if (addr
> total_len
) {
2169 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
2170 len
= (MAX_PACKET_LENGTH
- 5) / 2;
2173 if (len
< total_len
- addr
) {
2174 g_string_assign(gdbserver_state
.str_buf
, "m");
2175 memtox(gdbserver_state
.str_buf
, xml
+ addr
, len
);
2177 g_string_assign(gdbserver_state
.str_buf
, "l");
2178 memtox(gdbserver_state
.str_buf
, xml
+ addr
, total_len
- addr
);
2181 put_packet_binary(gdbserver_state
.str_buf
->str
,
2182 gdbserver_state
.str_buf
->len
, true);
2185 #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX_USER)
2186 static void handle_query_xfer_auxv(GArray
*params
, void *user_ctx
)
2189 unsigned long offset
, len
, saved_auxv
, auxv_len
;
2191 if (params
->len
< 2) {
2196 offset
= get_param(params
, 0)->val_ul
;
2197 len
= get_param(params
, 1)->val_ul
;
2198 ts
= gdbserver_state
.c_cpu
->opaque
;
2199 saved_auxv
= ts
->info
->saved_auxv
;
2200 auxv_len
= ts
->info
->auxv_len
;
2202 if (offset
>= auxv_len
) {
2207 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
2208 len
= (MAX_PACKET_LENGTH
- 5) / 2;
2211 if (len
< auxv_len
- offset
) {
2212 g_string_assign(gdbserver_state
.str_buf
, "m");
2214 g_string_assign(gdbserver_state
.str_buf
, "l");
2215 len
= auxv_len
- offset
;
2218 g_byte_array_set_size(gdbserver_state
.mem_buf
, len
);
2219 if (target_memory_rw_debug(gdbserver_state
.g_cpu
, saved_auxv
+ offset
,
2220 gdbserver_state
.mem_buf
->data
, len
, false)) {
2225 memtox(gdbserver_state
.str_buf
,
2226 (const char *)gdbserver_state
.mem_buf
->data
, len
);
2227 put_packet_binary(gdbserver_state
.str_buf
->str
,
2228 gdbserver_state
.str_buf
->len
, true);
2232 static void handle_query_attached(GArray
*params
, void *user_ctx
)
2234 put_packet(GDB_ATTACHED
);
2237 static void handle_query_qemu_supported(GArray
*params
, void *user_ctx
)
2239 g_string_printf(gdbserver_state
.str_buf
, "sstepbits;sstep");
2240 #ifndef CONFIG_USER_ONLY
2241 g_string_append(gdbserver_state
.str_buf
, ";PhyMemMode");
2246 #ifndef CONFIG_USER_ONLY
2247 static void handle_query_qemu_phy_mem_mode(GArray
*params
,
2250 g_string_printf(gdbserver_state
.str_buf
, "%d", phy_memory_mode
);
2254 static void handle_set_qemu_phy_mem_mode(GArray
*params
, void *user_ctx
)
2261 if (!get_param(params
, 0)->val_ul
) {
2262 phy_memory_mode
= 0;
2264 phy_memory_mode
= 1;
2270 static const GdbCmdParseEntry gdb_gen_query_set_common_table
[] = {
2271 /* Order is important if has same prefix */
2273 .handler
= handle_query_qemu_sstepbits
,
2274 .cmd
= "qemu.sstepbits",
2277 .handler
= handle_query_qemu_sstep
,
2278 .cmd
= "qemu.sstep",
2281 .handler
= handle_set_qemu_sstep
,
2282 .cmd
= "qemu.sstep=",
2283 .cmd_startswith
= 1,
2288 static const GdbCmdParseEntry gdb_gen_query_table
[] = {
2290 .handler
= handle_query_curr_tid
,
2294 .handler
= handle_query_threads
,
2295 .cmd
= "sThreadInfo",
2298 .handler
= handle_query_first_threads
,
2299 .cmd
= "fThreadInfo",
2302 .handler
= handle_query_thread_extra
,
2303 .cmd
= "ThreadExtraInfo,",
2304 .cmd_startswith
= 1,
2307 #ifdef CONFIG_USER_ONLY
2309 .handler
= handle_query_offsets
,
2314 .handler
= handle_query_rcmd
,
2316 .cmd_startswith
= 1,
2321 .handler
= handle_query_supported
,
2322 .cmd
= "Supported:",
2323 .cmd_startswith
= 1,
2327 .handler
= handle_query_supported
,
2332 .handler
= handle_query_xfer_features
,
2333 .cmd
= "Xfer:features:read:",
2334 .cmd_startswith
= 1,
2337 #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX_USER)
2339 .handler
= handle_query_xfer_auxv
,
2340 .cmd
= "Xfer:auxv:read::",
2341 .cmd_startswith
= 1,
2346 .handler
= handle_query_attached
,
2351 .handler
= handle_query_attached
,
2355 .handler
= handle_query_qemu_supported
,
2356 .cmd
= "qemu.Supported",
2358 #ifndef CONFIG_USER_ONLY
2360 .handler
= handle_query_qemu_phy_mem_mode
,
2361 .cmd
= "qemu.PhyMemMode",
2366 static const GdbCmdParseEntry gdb_gen_set_table
[] = {
2367 /* Order is important if has same prefix */
2369 .handler
= handle_set_qemu_sstep
,
2370 .cmd
= "qemu.sstep:",
2371 .cmd_startswith
= 1,
2374 #ifndef CONFIG_USER_ONLY
2376 .handler
= handle_set_qemu_phy_mem_mode
,
2377 .cmd
= "qemu.PhyMemMode:",
2378 .cmd_startswith
= 1,
2384 static void handle_gen_query(GArray
*params
, void *user_ctx
)
2390 if (!process_string_cmd(NULL
, get_param(params
, 0)->data
,
2391 gdb_gen_query_set_common_table
,
2392 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
2396 if (process_string_cmd(NULL
, get_param(params
, 0)->data
,
2397 gdb_gen_query_table
,
2398 ARRAY_SIZE(gdb_gen_query_table
))) {
2403 static void handle_gen_set(GArray
*params
, void *user_ctx
)
2409 if (!process_string_cmd(NULL
, get_param(params
, 0)->data
,
2410 gdb_gen_query_set_common_table
,
2411 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
2415 if (process_string_cmd(NULL
, get_param(params
, 0)->data
,
2417 ARRAY_SIZE(gdb_gen_set_table
))) {
2422 static void handle_target_halt(GArray
*params
, void *user_ctx
)
2424 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
2425 gdb_append_thread_id(gdbserver_state
.c_cpu
, gdbserver_state
.str_buf
);
2426 g_string_append_c(gdbserver_state
.str_buf
, ';');
2429 * Remove all the breakpoints when this query is issued,
2430 * because gdb is doing an initial connect and the state
2431 * should be cleaned up.
2433 gdb_breakpoint_remove_all(gdbserver_state
.c_cpu
);
2436 static int gdb_handle_packet(const char *line_buf
)
2438 const GdbCmdParseEntry
*cmd_parser
= NULL
;
2440 trace_gdbstub_io_command(line_buf
);
2442 switch (line_buf
[0]) {
2448 static const GdbCmdParseEntry target_halted_cmd_desc
= {
2449 .handler
= handle_target_halt
,
2453 cmd_parser
= &target_halted_cmd_desc
;
2458 static const GdbCmdParseEntry continue_cmd_desc
= {
2459 .handler
= handle_continue
,
2461 .cmd_startswith
= 1,
2464 cmd_parser
= &continue_cmd_desc
;
2469 static const GdbCmdParseEntry cont_with_sig_cmd_desc
= {
2470 .handler
= handle_cont_with_sig
,
2472 .cmd_startswith
= 1,
2475 cmd_parser
= &cont_with_sig_cmd_desc
;
2480 static const GdbCmdParseEntry v_cmd_desc
= {
2481 .handler
= handle_v_commands
,
2483 .cmd_startswith
= 1,
2486 cmd_parser
= &v_cmd_desc
;
2490 /* Kill the target */
2491 error_report("QEMU: Terminated via GDBstub");
2496 static const GdbCmdParseEntry detach_cmd_desc
= {
2497 .handler
= handle_detach
,
2499 .cmd_startswith
= 1,
2502 cmd_parser
= &detach_cmd_desc
;
2507 static const GdbCmdParseEntry step_cmd_desc
= {
2508 .handler
= handle_step
,
2510 .cmd_startswith
= 1,
2513 cmd_parser
= &step_cmd_desc
;
2518 static const GdbCmdParseEntry backward_cmd_desc
= {
2519 .handler
= handle_backward
,
2521 .cmd_startswith
= 1,
2524 cmd_parser
= &backward_cmd_desc
;
2529 static const GdbCmdParseEntry file_io_cmd_desc
= {
2530 .handler
= handle_file_io
,
2532 .cmd_startswith
= 1,
2535 cmd_parser
= &file_io_cmd_desc
;
2540 static const GdbCmdParseEntry read_all_regs_cmd_desc
= {
2541 .handler
= handle_read_all_regs
,
2545 cmd_parser
= &read_all_regs_cmd_desc
;
2550 static const GdbCmdParseEntry write_all_regs_cmd_desc
= {
2551 .handler
= handle_write_all_regs
,
2553 .cmd_startswith
= 1,
2556 cmd_parser
= &write_all_regs_cmd_desc
;
2561 static const GdbCmdParseEntry read_mem_cmd_desc
= {
2562 .handler
= handle_read_mem
,
2564 .cmd_startswith
= 1,
2567 cmd_parser
= &read_mem_cmd_desc
;
2572 static const GdbCmdParseEntry write_mem_cmd_desc
= {
2573 .handler
= handle_write_mem
,
2575 .cmd_startswith
= 1,
2578 cmd_parser
= &write_mem_cmd_desc
;
2583 static const GdbCmdParseEntry get_reg_cmd_desc
= {
2584 .handler
= handle_get_reg
,
2586 .cmd_startswith
= 1,
2589 cmd_parser
= &get_reg_cmd_desc
;
2594 static const GdbCmdParseEntry set_reg_cmd_desc
= {
2595 .handler
= handle_set_reg
,
2597 .cmd_startswith
= 1,
2600 cmd_parser
= &set_reg_cmd_desc
;
2605 static const GdbCmdParseEntry insert_bp_cmd_desc
= {
2606 .handler
= handle_insert_bp
,
2608 .cmd_startswith
= 1,
2611 cmd_parser
= &insert_bp_cmd_desc
;
2616 static const GdbCmdParseEntry remove_bp_cmd_desc
= {
2617 .handler
= handle_remove_bp
,
2619 .cmd_startswith
= 1,
2622 cmd_parser
= &remove_bp_cmd_desc
;
2627 static const GdbCmdParseEntry set_thread_cmd_desc
= {
2628 .handler
= handle_set_thread
,
2630 .cmd_startswith
= 1,
2633 cmd_parser
= &set_thread_cmd_desc
;
2638 static const GdbCmdParseEntry thread_alive_cmd_desc
= {
2639 .handler
= handle_thread_alive
,
2641 .cmd_startswith
= 1,
2644 cmd_parser
= &thread_alive_cmd_desc
;
2649 static const GdbCmdParseEntry gen_query_cmd_desc
= {
2650 .handler
= handle_gen_query
,
2652 .cmd_startswith
= 1,
2655 cmd_parser
= &gen_query_cmd_desc
;
2660 static const GdbCmdParseEntry gen_set_cmd_desc
= {
2661 .handler
= handle_gen_set
,
2663 .cmd_startswith
= 1,
2666 cmd_parser
= &gen_set_cmd_desc
;
2670 /* put empty packet */
2676 run_cmd_parser(line_buf
, cmd_parser
);
2682 void gdb_set_stop_cpu(CPUState
*cpu
)
2684 GDBProcess
*p
= gdb_get_cpu_process(cpu
);
2688 * Having a stop CPU corresponding to a process that is not attached
2689 * confuses GDB. So we ignore the request.
2694 gdbserver_state
.c_cpu
= cpu
;
2695 gdbserver_state
.g_cpu
= cpu
;
2698 #ifndef CONFIG_USER_ONLY
2699 static void gdb_vm_state_change(void *opaque
, bool running
, RunState state
)
2701 CPUState
*cpu
= gdbserver_state
.c_cpu
;
2702 g_autoptr(GString
) buf
= g_string_new(NULL
);
2703 g_autoptr(GString
) tid
= g_string_new(NULL
);
2707 if (running
|| gdbserver_state
.state
== RS_INACTIVE
) {
2710 /* Is there a GDB syscall waiting to be sent? */
2711 if (gdbserver_state
.current_syscall_cb
) {
2712 put_packet(gdbserver_state
.syscall_buf
);
2717 /* No process attached */
2721 gdb_append_thread_id(cpu
, tid
);
2724 case RUN_STATE_DEBUG
:
2725 if (cpu
->watchpoint_hit
) {
2726 switch (cpu
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
2737 trace_gdbstub_hit_watchpoint(type
, cpu_gdb_index(cpu
),
2738 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
2739 g_string_printf(buf
, "T%02xthread:%s;%swatch:" TARGET_FMT_lx
";",
2740 GDB_SIGNAL_TRAP
, tid
->str
, type
,
2741 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
2742 cpu
->watchpoint_hit
= NULL
;
2745 trace_gdbstub_hit_break();
2748 ret
= GDB_SIGNAL_TRAP
;
2750 case RUN_STATE_PAUSED
:
2751 trace_gdbstub_hit_paused();
2752 ret
= GDB_SIGNAL_INT
;
2754 case RUN_STATE_SHUTDOWN
:
2755 trace_gdbstub_hit_shutdown();
2756 ret
= GDB_SIGNAL_QUIT
;
2758 case RUN_STATE_IO_ERROR
:
2759 trace_gdbstub_hit_io_error();
2760 ret
= GDB_SIGNAL_IO
;
2762 case RUN_STATE_WATCHDOG
:
2763 trace_gdbstub_hit_watchdog();
2764 ret
= GDB_SIGNAL_ALRM
;
2766 case RUN_STATE_INTERNAL_ERROR
:
2767 trace_gdbstub_hit_internal_error();
2768 ret
= GDB_SIGNAL_ABRT
;
2770 case RUN_STATE_SAVE_VM
:
2771 case RUN_STATE_RESTORE_VM
:
2773 case RUN_STATE_FINISH_MIGRATE
:
2774 ret
= GDB_SIGNAL_XCPU
;
2777 trace_gdbstub_hit_unknown(state
);
2778 ret
= GDB_SIGNAL_UNKNOWN
;
2781 gdb_set_stop_cpu(cpu
);
2782 g_string_printf(buf
, "T%02xthread:%s;", ret
, tid
->str
);
2785 put_packet(buf
->str
);
2787 /* disable single step if it was enabled */
2788 cpu_single_step(cpu
, 0);
2792 /* Send a gdb syscall request.
2793 This accepts limited printf-style format specifiers, specifically:
2794 %x - target_ulong argument printed in hex.
2795 %lx - 64-bit argument printed in hex.
2796 %s - string pointer (target_ulong) and length (int) pair. */
2797 void gdb_do_syscallv(gdb_syscall_complete_cb cb
, const char *fmt
, va_list va
)
2804 if (!gdb_attached()) {
2808 gdbserver_state
.current_syscall_cb
= cb
;
2809 #ifndef CONFIG_USER_ONLY
2810 vm_stop(RUN_STATE_DEBUG
);
2812 p
= &gdbserver_state
.syscall_buf
[0];
2813 p_end
= &gdbserver_state
.syscall_buf
[sizeof(gdbserver_state
.syscall_buf
)];
2820 addr
= va_arg(va
, target_ulong
);
2821 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
, addr
);
2824 if (*(fmt
++) != 'x')
2826 i64
= va_arg(va
, uint64_t);
2827 p
+= snprintf(p
, p_end
- p
, "%" PRIx64
, i64
);
2830 addr
= va_arg(va
, target_ulong
);
2831 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
"/%x",
2832 addr
, va_arg(va
, int));
2836 error_report("gdbstub: Bad syscall format string '%s'",
2845 #ifdef CONFIG_USER_ONLY
2846 put_packet(gdbserver_state
.syscall_buf
);
2847 /* Return control to gdb for it to process the syscall request.
2848 * Since the protocol requires that gdb hands control back to us
2849 * using a "here are the results" F packet, we don't need to check
2850 * gdb_handlesig's return value (which is the signal to deliver if
2851 * execution was resumed via a continue packet).
2853 gdb_handlesig(gdbserver_state
.c_cpu
, 0);
2855 /* In this case wait to send the syscall packet until notification that
2856 the CPU has stopped. This must be done because if the packet is sent
2857 now the reply from the syscall request could be received while the CPU
2858 is still in the running state, which can cause packets to be dropped
2859 and state transition 'T' packets to be sent while the syscall is still
2861 qemu_cpu_kick(gdbserver_state
.c_cpu
);
2865 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
2870 gdb_do_syscallv(cb
, fmt
, va
);
2874 static void gdb_read_byte(uint8_t ch
)
2878 #ifndef CONFIG_USER_ONLY
2879 if (gdbserver_state
.last_packet
->len
) {
2880 /* Waiting for a response to the last packet. If we see the start
2881 of a new command then abandon the previous response. */
2883 trace_gdbstub_err_got_nack();
2884 put_buffer(gdbserver_state
.last_packet
->data
,
2885 gdbserver_state
.last_packet
->len
);
2886 } else if (ch
== '+') {
2887 trace_gdbstub_io_got_ack();
2889 trace_gdbstub_io_got_unexpected(ch
);
2892 if (ch
== '+' || ch
== '$') {
2893 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
2898 if (runstate_is_running()) {
2899 /* when the CPU is running, we cannot do anything except stop
2900 it when receiving a char */
2901 vm_stop(RUN_STATE_PAUSED
);
2905 switch(gdbserver_state
.state
) {
2908 /* start of command packet */
2909 gdbserver_state
.line_buf_index
= 0;
2910 gdbserver_state
.line_sum
= 0;
2911 gdbserver_state
.state
= RS_GETLINE
;
2913 trace_gdbstub_err_garbage(ch
);
2918 /* start escape sequence */
2919 gdbserver_state
.state
= RS_GETLINE_ESC
;
2920 gdbserver_state
.line_sum
+= ch
;
2921 } else if (ch
== '*') {
2922 /* start run length encoding sequence */
2923 gdbserver_state
.state
= RS_GETLINE_RLE
;
2924 gdbserver_state
.line_sum
+= ch
;
2925 } else if (ch
== '#') {
2926 /* end of command, start of checksum*/
2927 gdbserver_state
.state
= RS_CHKSUM1
;
2928 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2929 trace_gdbstub_err_overrun();
2930 gdbserver_state
.state
= RS_IDLE
;
2932 /* unescaped command character */
2933 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
;
2934 gdbserver_state
.line_sum
+= ch
;
2937 case RS_GETLINE_ESC
:
2939 /* unexpected end of command in escape sequence */
2940 gdbserver_state
.state
= RS_CHKSUM1
;
2941 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2942 /* command buffer overrun */
2943 trace_gdbstub_err_overrun();
2944 gdbserver_state
.state
= RS_IDLE
;
2946 /* parse escaped character and leave escape state */
2947 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
^ 0x20;
2948 gdbserver_state
.line_sum
+= ch
;
2949 gdbserver_state
.state
= RS_GETLINE
;
2952 case RS_GETLINE_RLE
:
2954 * Run-length encoding is explained in "Debugging with GDB /
2955 * Appendix E GDB Remote Serial Protocol / Overview".
2957 if (ch
< ' ' || ch
== '#' || ch
== '$' || ch
> 126) {
2958 /* invalid RLE count encoding */
2959 trace_gdbstub_err_invalid_repeat(ch
);
2960 gdbserver_state
.state
= RS_GETLINE
;
2962 /* decode repeat length */
2963 int repeat
= ch
- ' ' + 3;
2964 if (gdbserver_state
.line_buf_index
+ repeat
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2965 /* that many repeats would overrun the command buffer */
2966 trace_gdbstub_err_overrun();
2967 gdbserver_state
.state
= RS_IDLE
;
2968 } else if (gdbserver_state
.line_buf_index
< 1) {
2969 /* got a repeat but we have nothing to repeat */
2970 trace_gdbstub_err_invalid_rle();
2971 gdbserver_state
.state
= RS_GETLINE
;
2973 /* repeat the last character */
2974 memset(gdbserver_state
.line_buf
+ gdbserver_state
.line_buf_index
,
2975 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
- 1], repeat
);
2976 gdbserver_state
.line_buf_index
+= repeat
;
2977 gdbserver_state
.line_sum
+= ch
;
2978 gdbserver_state
.state
= RS_GETLINE
;
2983 /* get high hex digit of checksum */
2984 if (!isxdigit(ch
)) {
2985 trace_gdbstub_err_checksum_invalid(ch
);
2986 gdbserver_state
.state
= RS_GETLINE
;
2989 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
] = '\0';
2990 gdbserver_state
.line_csum
= fromhex(ch
) << 4;
2991 gdbserver_state
.state
= RS_CHKSUM2
;
2994 /* get low hex digit of checksum */
2995 if (!isxdigit(ch
)) {
2996 trace_gdbstub_err_checksum_invalid(ch
);
2997 gdbserver_state
.state
= RS_GETLINE
;
3000 gdbserver_state
.line_csum
|= fromhex(ch
);
3002 if (gdbserver_state
.line_csum
!= (gdbserver_state
.line_sum
& 0xff)) {
3003 trace_gdbstub_err_checksum_incorrect(gdbserver_state
.line_sum
, gdbserver_state
.line_csum
);
3004 /* send NAK reply */
3006 put_buffer(&reply
, 1);
3007 gdbserver_state
.state
= RS_IDLE
;
3009 /* send ACK reply */
3011 put_buffer(&reply
, 1);
3012 gdbserver_state
.state
= gdb_handle_packet(gdbserver_state
.line_buf
);
3021 /* Tell the remote gdb that the process has exited. */
3022 void gdb_exit(int code
)
3026 if (!gdbserver_state
.init
) {
3029 #ifdef CONFIG_USER_ONLY
3030 if (gdbserver_state
.socket_path
) {
3031 unlink(gdbserver_state
.socket_path
);
3033 if (gdbserver_state
.fd
< 0) {
3038 trace_gdbstub_op_exiting((uint8_t)code
);
3040 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
3043 #ifndef CONFIG_USER_ONLY
3044 qemu_chr_fe_deinit(&gdbserver_state
.chr
, true);
3049 * Create the process that will contain all the "orphan" CPUs (that are not
3050 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
3051 * be attachable and thus will be invisible to the user.
3053 static void create_default_process(GDBState
*s
)
3055 GDBProcess
*process
;
3058 if (gdbserver_state
.process_num
) {
3059 max_pid
= s
->processes
[s
->process_num
- 1].pid
;
3062 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
3063 process
= &s
->processes
[s
->process_num
- 1];
3065 /* We need an available PID slot for this process */
3066 assert(max_pid
< UINT32_MAX
);
3068 process
->pid
= max_pid
+ 1;
3069 process
->attached
= false;
3070 process
->target_xml
[0] = '\0';
3073 #ifdef CONFIG_USER_ONLY
3075 gdb_handlesig(CPUState
*cpu
, int sig
)
3080 if (!gdbserver_state
.init
|| gdbserver_state
.fd
< 0) {
3084 /* disable single step if it was enabled */
3085 cpu_single_step(cpu
, 0);
3089 gdb_set_stop_cpu(cpu
);
3090 g_string_printf(gdbserver_state
.str_buf
,
3091 "T%02xthread:", target_signal_to_gdb(sig
));
3092 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
3093 g_string_append_c(gdbserver_state
.str_buf
, ';');
3096 /* put_packet() might have detected that the peer terminated the
3098 if (gdbserver_state
.fd
< 0) {
3103 gdbserver_state
.state
= RS_IDLE
;
3104 gdbserver_state
.running_state
= 0;
3105 while (gdbserver_state
.running_state
== 0) {
3106 n
= read(gdbserver_state
.fd
, buf
, 256);
3110 for (i
= 0; i
< n
; i
++) {
3111 gdb_read_byte(buf
[i
]);
3114 /* XXX: Connection closed. Should probably wait for another
3115 connection before continuing. */
3117 close(gdbserver_state
.fd
);
3119 gdbserver_state
.fd
= -1;
3123 sig
= gdbserver_state
.signal
;
3124 gdbserver_state
.signal
= 0;
3128 /* Tell the remote gdb that the process has exited due to SIG. */
3129 void gdb_signalled(CPUArchState
*env
, int sig
)
3133 if (!gdbserver_state
.init
|| gdbserver_state
.fd
< 0) {
3137 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb(sig
));
3141 static void gdb_accept_init(int fd
)
3143 init_gdbserver_state();
3144 create_default_process(&gdbserver_state
);
3145 gdbserver_state
.processes
[0].attached
= true;
3146 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
3147 gdbserver_state
.g_cpu
= gdbserver_state
.c_cpu
;
3148 gdbserver_state
.fd
= fd
;
3149 gdb_has_xml
= false;
3152 static bool gdb_accept_socket(int gdb_fd
)
3157 fd
= accept(gdb_fd
, NULL
, NULL
);
3158 if (fd
< 0 && errno
!= EINTR
) {
3159 perror("accept socket");
3161 } else if (fd
>= 0) {
3162 qemu_set_cloexec(fd
);
3167 gdb_accept_init(fd
);
3171 static int gdbserver_open_socket(const char *path
)
3173 struct sockaddr_un sockaddr
= {};
3176 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
3178 perror("create socket");
3182 sockaddr
.sun_family
= AF_UNIX
;
3183 pstrcpy(sockaddr
.sun_path
, sizeof(sockaddr
.sun_path
) - 1, path
);
3184 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
3186 perror("bind socket");
3190 ret
= listen(fd
, 1);
3192 perror("listen socket");
3200 static bool gdb_accept_tcp(int gdb_fd
)
3202 struct sockaddr_in sockaddr
= {};
3207 len
= sizeof(sockaddr
);
3208 fd
= accept(gdb_fd
, (struct sockaddr
*)&sockaddr
, &len
);
3209 if (fd
< 0 && errno
!= EINTR
) {
3212 } else if (fd
>= 0) {
3213 qemu_set_cloexec(fd
);
3218 /* set short latency */
3219 if (socket_set_nodelay(fd
)) {
3220 perror("setsockopt");
3225 gdb_accept_init(fd
);
3229 static int gdbserver_open_port(int port
)
3231 struct sockaddr_in sockaddr
;
3234 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3239 qemu_set_cloexec(fd
);
3241 socket_set_fast_reuse(fd
);
3243 sockaddr
.sin_family
= AF_INET
;
3244 sockaddr
.sin_port
= htons(port
);
3245 sockaddr
.sin_addr
.s_addr
= 0;
3246 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
3252 ret
= listen(fd
, 1);
3262 int gdbserver_start(const char *port_or_path
)
3264 int port
= g_ascii_strtoull(port_or_path
, NULL
, 10);
3268 gdb_fd
= gdbserver_open_port(port
);
3270 gdb_fd
= gdbserver_open_socket(port_or_path
);
3277 if (port
> 0 && gdb_accept_tcp(gdb_fd
)) {
3279 } else if (gdb_accept_socket(gdb_fd
)) {
3280 gdbserver_state
.socket_path
= g_strdup(port_or_path
);
3289 /* Disable gdb stub for child processes. */
3290 void gdbserver_fork(CPUState
*cpu
)
3292 if (!gdbserver_state
.init
|| gdbserver_state
.fd
< 0) {
3295 close(gdbserver_state
.fd
);
3296 gdbserver_state
.fd
= -1;
3297 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
3298 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
3301 static int gdb_chr_can_receive(void *opaque
)
3303 /* We can handle an arbitrarily large amount of data.
3304 Pick the maximum packet size, which is as good as anything. */
3305 return MAX_PACKET_LENGTH
;
3308 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
3312 for (i
= 0; i
< size
; i
++) {
3313 gdb_read_byte(buf
[i
]);
3317 static void gdb_chr_event(void *opaque
, QEMUChrEvent event
)
3320 GDBState
*s
= (GDBState
*) opaque
;
3323 case CHR_EVENT_OPENED
:
3324 /* Start with first process attached, others detached */
3325 for (i
= 0; i
< s
->process_num
; i
++) {
3326 s
->processes
[i
].attached
= !i
;
3329 s
->c_cpu
= gdb_first_attached_cpu();
3330 s
->g_cpu
= s
->c_cpu
;
3332 vm_stop(RUN_STATE_PAUSED
);
3333 replay_gdb_attached();
3334 gdb_has_xml
= false;
3341 static int gdb_monitor_write(Chardev
*chr
, const uint8_t *buf
, int len
)
3343 g_autoptr(GString
) hex_buf
= g_string_new("O");
3344 memtohex(hex_buf
, buf
, len
);
3345 put_packet(hex_buf
->str
);
3350 static void gdb_sigterm_handler(int signal
)
3352 if (runstate_is_running()) {
3353 vm_stop(RUN_STATE_PAUSED
);
3358 static void gdb_monitor_open(Chardev
*chr
, ChardevBackend
*backend
,
3359 bool *be_opened
, Error
**errp
)
3364 static void char_gdb_class_init(ObjectClass
*oc
, void *data
)
3366 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
3368 cc
->internal
= true;
3369 cc
->open
= gdb_monitor_open
;
3370 cc
->chr_write
= gdb_monitor_write
;
3373 #define TYPE_CHARDEV_GDB "chardev-gdb"
3375 static const TypeInfo char_gdb_type_info
= {
3376 .name
= TYPE_CHARDEV_GDB
,
3377 .parent
= TYPE_CHARDEV
,
3378 .class_init
= char_gdb_class_init
,
3381 static int find_cpu_clusters(Object
*child
, void *opaque
)
3383 if (object_dynamic_cast(child
, TYPE_CPU_CLUSTER
)) {
3384 GDBState
*s
= (GDBState
*) opaque
;
3385 CPUClusterState
*cluster
= CPU_CLUSTER(child
);
3386 GDBProcess
*process
;
3388 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
3390 process
= &s
->processes
[s
->process_num
- 1];
3393 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
3394 * runtime, we enforce here that the machine does not use a cluster ID
3395 * that would lead to PID 0.
3397 assert(cluster
->cluster_id
!= UINT32_MAX
);
3398 process
->pid
= cluster
->cluster_id
+ 1;
3399 process
->attached
= false;
3400 process
->target_xml
[0] = '\0';
3405 return object_child_foreach(child
, find_cpu_clusters
, opaque
);
3408 static int pid_order(const void *a
, const void *b
)
3410 GDBProcess
*pa
= (GDBProcess
*) a
;
3411 GDBProcess
*pb
= (GDBProcess
*) b
;
3413 if (pa
->pid
< pb
->pid
) {
3415 } else if (pa
->pid
> pb
->pid
) {
3422 static void create_processes(GDBState
*s
)
3424 object_child_foreach(object_get_root(), find_cpu_clusters
, s
);
3426 if (gdbserver_state
.processes
) {
3428 qsort(gdbserver_state
.processes
, gdbserver_state
.process_num
, sizeof(gdbserver_state
.processes
[0]), pid_order
);
3431 create_default_process(s
);
3434 int gdbserver_start(const char *device
)
3436 trace_gdbstub_op_start(device
);
3438 char gdbstub_device_name
[128];
3439 Chardev
*chr
= NULL
;
3443 error_report("gdbstub: meaningless to attach gdb to a "
3444 "machine without any CPU.");
3448 if (!gdb_supports_guest_debug()) {
3449 error_report("gdbstub: current accelerator doesn't support guest debugging");
3455 if (strcmp(device
, "none") != 0) {
3456 if (strstart(device
, "tcp:", NULL
)) {
3457 /* enforce required TCP attributes */
3458 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
3459 "%s,wait=off,nodelay=on,server=on", device
);
3460 device
= gdbstub_device_name
;
3463 else if (strcmp(device
, "stdio") == 0) {
3464 struct sigaction act
;
3466 memset(&act
, 0, sizeof(act
));
3467 act
.sa_handler
= gdb_sigterm_handler
;
3468 sigaction(SIGINT
, &act
, NULL
);
3472 * FIXME: it's a bit weird to allow using a mux chardev here
3473 * and implicitly setup a monitor. We may want to break this.
3475 chr
= qemu_chr_new_noreplay("gdb", device
, true, NULL
);
3480 if (!gdbserver_state
.init
) {
3481 init_gdbserver_state();
3483 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
3485 /* Initialize a monitor terminal for gdb */
3486 mon_chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_GDB
,
3487 NULL
, NULL
, &error_abort
);
3488 monitor_init_hmp(mon_chr
, false, &error_abort
);
3490 qemu_chr_fe_deinit(&gdbserver_state
.chr
, true);
3491 mon_chr
= gdbserver_state
.mon_chr
;
3492 reset_gdbserver_state();
3495 create_processes(&gdbserver_state
);
3498 qemu_chr_fe_init(&gdbserver_state
.chr
, chr
, &error_abort
);
3499 qemu_chr_fe_set_handlers(&gdbserver_state
.chr
, gdb_chr_can_receive
,
3500 gdb_chr_receive
, gdb_chr_event
,
3501 NULL
, &gdbserver_state
, NULL
, true);
3503 gdbserver_state
.state
= chr
? RS_IDLE
: RS_INACTIVE
;
3504 gdbserver_state
.mon_chr
= mon_chr
;
3505 gdbserver_state
.current_syscall_cb
= NULL
;
3510 static void register_types(void)
3512 type_register_static(&char_gdb_type_info
);
3515 type_init(register_types
);