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/hwaddr.h"
52 #include "sysemu/replay.h"
54 #include "internals.h"
56 #ifdef CONFIG_USER_ONLY
57 #define GDB_ATTACHED "0"
59 #define GDB_ATTACHED "1"
62 #ifndef CONFIG_USER_ONLY
63 static int phy_memory_mode
;
66 static inline int target_memory_rw_debug(CPUState
*cpu
, target_ulong addr
,
67 uint8_t *buf
, int len
, bool is_write
)
71 #ifndef CONFIG_USER_ONLY
72 if (phy_memory_mode
) {
74 cpu_physical_memory_write(addr
, buf
, len
);
76 cpu_physical_memory_read(addr
, buf
, len
);
82 cc
= CPU_GET_CLASS(cpu
);
83 if (cc
->memory_rw_debug
) {
84 return cc
->memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
86 return cpu_memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
89 /* Return the GDB index for a given vCPU state.
91 * For user mode this is simply the thread id. In system mode GDB
92 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
94 static inline int cpu_gdb_index(CPUState
*cpu
)
96 #if defined(CONFIG_USER_ONLY)
97 TaskState
*ts
= (TaskState
*) cpu
->opaque
;
98 return ts
? ts
->ts_tid
: -1;
100 return cpu
->cpu_index
+ 1;
110 GDB_SIGNAL_ALRM
= 14,
112 GDB_SIGNAL_XCPU
= 24,
113 GDB_SIGNAL_UNKNOWN
= 143
116 #ifdef CONFIG_USER_ONLY
118 /* Map target signal numbers to GDB protocol signal numbers and vice
119 * versa. For user emulation's currently supported systems, we can
120 * assume most signals are defined.
123 static int gdb_signal_table
[] = {
283 /* In system mode we only need SIGINT and SIGTRAP; other signals
284 are not yet supported. */
291 static int gdb_signal_table
[] = {
301 #ifdef CONFIG_USER_ONLY
302 static int target_signal_to_gdb (int sig
)
305 for (i
= 0; i
< ARRAY_SIZE (gdb_signal_table
); i
++)
306 if (gdb_signal_table
[i
] == sig
)
308 return GDB_SIGNAL_UNKNOWN
;
312 static int gdb_signal_to_target (int sig
)
314 if (sig
< ARRAY_SIZE (gdb_signal_table
))
315 return gdb_signal_table
[sig
];
320 typedef struct GDBRegisterState
{
323 gdb_get_reg_cb get_reg
;
324 gdb_set_reg_cb set_reg
;
326 struct GDBRegisterState
*next
;
329 typedef struct GDBProcess
{
333 char target_xml
[1024];
345 typedef struct GDBState
{
346 bool init
; /* have we been initialised? */
347 CPUState
*c_cpu
; /* current CPU for step/continue ops */
348 CPUState
*g_cpu
; /* current CPU for other ops */
349 CPUState
*query_cpu
; /* for q{f|s}ThreadInfo */
350 enum RSState state
; /* parsing state */
351 char line_buf
[MAX_PACKET_LENGTH
];
353 int line_sum
; /* running checksum */
354 int line_csum
; /* checksum at the end of the packet */
355 GByteArray
*last_packet
;
357 #ifdef CONFIG_USER_ONLY
366 GDBProcess
*processes
;
368 char syscall_buf
[256];
369 gdb_syscall_complete_cb current_syscall_cb
;
373 int supported_sstep_flags
;
376 static GDBState gdbserver_state
;
378 static void init_gdbserver_state(void)
380 g_assert(!gdbserver_state
.init
);
381 memset(&gdbserver_state
, 0, sizeof(GDBState
));
382 gdbserver_state
.init
= true;
383 gdbserver_state
.str_buf
= g_string_new(NULL
);
384 gdbserver_state
.mem_buf
= g_byte_array_sized_new(MAX_PACKET_LENGTH
);
385 gdbserver_state
.last_packet
= g_byte_array_sized_new(MAX_PACKET_LENGTH
+ 4);
388 * What single-step modes are supported is accelerator dependent.
389 * By default try to use no IRQs and no timers while single
390 * stepping so as to make single stepping like a typical ICE HW step.
392 gdbserver_state
.supported_sstep_flags
= accel_supported_gdbstub_sstep_flags();
393 gdbserver_state
.sstep_flags
= SSTEP_ENABLE
| SSTEP_NOIRQ
| SSTEP_NOTIMER
;
394 gdbserver_state
.sstep_flags
&= gdbserver_state
.supported_sstep_flags
;
397 #ifndef CONFIG_USER_ONLY
398 static void reset_gdbserver_state(void)
400 g_free(gdbserver_state
.processes
);
401 gdbserver_state
.processes
= NULL
;
402 gdbserver_state
.process_num
= 0;
408 #ifdef CONFIG_USER_ONLY
410 static int get_char(void)
416 ret
= recv(gdbserver_state
.fd
, &ch
, 1, 0);
418 if (errno
== ECONNRESET
)
419 gdbserver_state
.fd
= -1;
422 } else if (ret
== 0) {
423 close(gdbserver_state
.fd
);
424 gdbserver_state
.fd
= -1;
435 * Return true if there is a GDB currently connected to the stub
436 * and attached to a CPU
438 static bool gdb_attached(void)
440 return gdbserver_state
.init
&& gdbserver_state
.c_cpu
;
449 /* Decide if either remote gdb syscalls or native file IO should be used. */
450 int use_gdb_syscalls(void)
452 SemihostingTarget target
= semihosting_get_target();
453 if (target
== SEMIHOSTING_TARGET_NATIVE
) {
454 /* -semihosting-config target=native */
456 } else if (target
== SEMIHOSTING_TARGET_GDB
) {
457 /* -semihosting-config target=gdb */
461 /* -semihosting-config target=auto */
462 /* On the first call check if gdb is connected and remember. */
463 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
464 gdb_syscall_mode
= gdb_attached() ? GDB_SYS_ENABLED
: GDB_SYS_DISABLED
;
466 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
469 static bool stub_can_reverse(void)
471 #ifdef CONFIG_USER_ONLY
474 return replay_mode
== REPLAY_MODE_PLAY
;
478 /* Resume execution. */
479 static inline void gdb_continue(void)
482 #ifdef CONFIG_USER_ONLY
483 gdbserver_state
.running_state
= 1;
484 trace_gdbstub_op_continue();
486 if (!runstate_needs_reset()) {
487 trace_gdbstub_op_continue();
494 * Resume execution, per CPU actions. For user-mode emulation it's
495 * equivalent to gdb_continue.
497 static int gdb_continue_partial(char *newstates
)
501 #ifdef CONFIG_USER_ONLY
503 * This is not exactly accurate, but it's an improvement compared to the
504 * previous situation, where only one CPU would be single-stepped.
507 if (newstates
[cpu
->cpu_index
] == 's') {
508 trace_gdbstub_op_stepping(cpu
->cpu_index
);
509 cpu_single_step(cpu
, gdbserver_state
.sstep_flags
);
512 gdbserver_state
.running_state
= 1;
516 if (!runstate_needs_reset()) {
517 bool step_requested
= false;
519 if (newstates
[cpu
->cpu_index
] == 's') {
520 step_requested
= true;
525 if (vm_prepare_start(step_requested
)) {
530 switch (newstates
[cpu
->cpu_index
]) {
533 break; /* nothing to do here */
535 trace_gdbstub_op_stepping(cpu
->cpu_index
);
536 cpu_single_step(cpu
, gdbserver_state
.sstep_flags
);
541 trace_gdbstub_op_continue_cpu(cpu
->cpu_index
);
552 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, true);
558 static void put_buffer(const uint8_t *buf
, int len
)
560 #ifdef CONFIG_USER_ONLY
564 ret
= send(gdbserver_state
.fd
, buf
, len
, 0);
574 /* XXX this blocks entire thread. Rewrite to use
575 * qemu_chr_fe_write and background I/O callbacks */
576 qemu_chr_fe_write_all(&gdbserver_state
.chr
, buf
, len
);
580 static inline int fromhex(int v
)
582 if (v
>= '0' && v
<= '9')
584 else if (v
>= 'A' && v
<= 'F')
586 else if (v
>= 'a' && v
<= 'f')
592 static inline int tohex(int v
)
600 /* writes 2*len+1 bytes in buf */
601 static void memtohex(GString
*buf
, const uint8_t *mem
, int len
)
604 for(i
= 0; i
< len
; i
++) {
606 g_string_append_c(buf
, tohex(c
>> 4));
607 g_string_append_c(buf
, tohex(c
& 0xf));
609 g_string_append_c(buf
, '\0');
612 static void hextomem(GByteArray
*mem
, const char *buf
, int len
)
616 for(i
= 0; i
< len
; i
++) {
617 guint8 byte
= fromhex(buf
[0]) << 4 | fromhex(buf
[1]);
618 g_byte_array_append(mem
, &byte
, 1);
623 static void hexdump(const char *buf
, int len
,
624 void (*trace_fn
)(size_t ofs
, char const *text
))
626 char line_buffer
[3 * 16 + 4 + 16 + 1];
629 for (i
= 0; i
< len
|| (i
& 0xF); ++i
) {
630 size_t byte_ofs
= i
& 15;
633 memset(line_buffer
, ' ', 3 * 16 + 4 + 16);
634 line_buffer
[3 * 16 + 4 + 16] = 0;
637 size_t col_group
= (i
>> 2) & 3;
638 size_t hex_col
= byte_ofs
* 3 + col_group
;
639 size_t txt_col
= 3 * 16 + 4 + byte_ofs
;
644 line_buffer
[hex_col
+ 0] = tohex((value
>> 4) & 0xF);
645 line_buffer
[hex_col
+ 1] = tohex((value
>> 0) & 0xF);
646 line_buffer
[txt_col
+ 0] = (value
>= ' ' && value
< 127)
652 trace_fn(i
& -16, line_buffer
);
656 /* return -1 if error, 0 if OK */
657 static int put_packet_binary(const char *buf
, int len
, bool dump
)
662 if (dump
&& trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY
)) {
663 hexdump(buf
, len
, trace_gdbstub_io_binaryreply
);
667 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
668 g_byte_array_append(gdbserver_state
.last_packet
,
669 (const uint8_t *) "$", 1);
670 g_byte_array_append(gdbserver_state
.last_packet
,
671 (const uint8_t *) buf
, len
);
673 for(i
= 0; i
< len
; i
++) {
677 footer
[1] = tohex((csum
>> 4) & 0xf);
678 footer
[2] = tohex((csum
) & 0xf);
679 g_byte_array_append(gdbserver_state
.last_packet
, footer
, 3);
681 put_buffer(gdbserver_state
.last_packet
->data
,
682 gdbserver_state
.last_packet
->len
);
684 #ifdef CONFIG_USER_ONLY
697 /* return -1 if error, 0 if OK */
698 static int put_packet(const char *buf
)
700 trace_gdbstub_io_reply(buf
);
702 return put_packet_binary(buf
, strlen(buf
), false);
705 static void put_strbuf(void)
707 put_packet(gdbserver_state
.str_buf
->str
);
710 /* Encode data using the encoding for 'x' packets. */
711 static void memtox(GString
*buf
, const char *mem
, int len
)
718 case '#': case '$': case '*': case '}':
719 g_string_append_c(buf
, '}');
720 g_string_append_c(buf
, c
^ 0x20);
723 g_string_append_c(buf
, c
);
729 static uint32_t gdb_get_cpu_pid(CPUState
*cpu
)
731 /* TODO: In user mode, we should use the task state PID */
732 if (cpu
->cluster_index
== UNASSIGNED_CLUSTER_INDEX
) {
733 /* Return the default process' PID */
734 int index
= gdbserver_state
.process_num
- 1;
735 return gdbserver_state
.processes
[index
].pid
;
737 return cpu
->cluster_index
+ 1;
740 static GDBProcess
*gdb_get_process(uint32_t pid
)
745 /* 0 means any process, we take the first one */
746 return &gdbserver_state
.processes
[0];
749 for (i
= 0; i
< gdbserver_state
.process_num
; i
++) {
750 if (gdbserver_state
.processes
[i
].pid
== pid
) {
751 return &gdbserver_state
.processes
[i
];
758 static GDBProcess
*gdb_get_cpu_process(CPUState
*cpu
)
760 return gdb_get_process(gdb_get_cpu_pid(cpu
));
763 static CPUState
*find_cpu(uint32_t thread_id
)
768 if (cpu_gdb_index(cpu
) == thread_id
) {
776 static CPUState
*get_first_cpu_in_process(GDBProcess
*process
)
781 if (gdb_get_cpu_pid(cpu
) == process
->pid
) {
789 static CPUState
*gdb_next_cpu_in_process(CPUState
*cpu
)
791 uint32_t pid
= gdb_get_cpu_pid(cpu
);
795 if (gdb_get_cpu_pid(cpu
) == pid
) {
805 /* Return the cpu following @cpu, while ignoring unattached processes. */
806 static CPUState
*gdb_next_attached_cpu(CPUState
*cpu
)
811 if (gdb_get_cpu_process(cpu
)->attached
) {
821 /* Return the first attached cpu */
822 static CPUState
*gdb_first_attached_cpu(void)
824 CPUState
*cpu
= first_cpu
;
825 GDBProcess
*process
= gdb_get_cpu_process(cpu
);
827 if (!process
->attached
) {
828 return gdb_next_attached_cpu(cpu
);
834 static CPUState
*gdb_get_cpu(uint32_t pid
, uint32_t tid
)
840 /* 0 means any process/thread, we take the first attached one */
841 return gdb_first_attached_cpu();
842 } else if (pid
&& !tid
) {
843 /* any thread in a specific process */
844 process
= gdb_get_process(pid
);
846 if (process
== NULL
) {
850 if (!process
->attached
) {
854 return get_first_cpu_in_process(process
);
856 /* a specific thread */
863 process
= gdb_get_cpu_process(cpu
);
865 if (pid
&& process
->pid
!= pid
) {
869 if (!process
->attached
) {
877 static const char *get_feature_xml(const char *p
, const char **newp
,
883 CPUState
*cpu
= get_first_cpu_in_process(process
);
884 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
887 while (p
[len
] && p
[len
] != ':')
892 if (strncmp(p
, "target.xml", len
) == 0) {
893 char *buf
= process
->target_xml
;
894 const size_t buf_sz
= sizeof(process
->target_xml
);
896 /* Generate the XML description for this CPU. */
901 "<?xml version=\"1.0\"?>"
902 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
904 if (cc
->gdb_arch_name
) {
905 gchar
*arch
= cc
->gdb_arch_name(cpu
);
906 pstrcat(buf
, buf_sz
, "<architecture>");
907 pstrcat(buf
, buf_sz
, arch
);
908 pstrcat(buf
, buf_sz
, "</architecture>");
911 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
912 pstrcat(buf
, buf_sz
, cc
->gdb_core_xml_file
);
913 pstrcat(buf
, buf_sz
, "\"/>");
914 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
915 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
916 pstrcat(buf
, buf_sz
, r
->xml
);
917 pstrcat(buf
, buf_sz
, "\"/>");
919 pstrcat(buf
, buf_sz
, "</target>");
923 if (cc
->gdb_get_dynamic_xml
) {
924 char *xmlname
= g_strndup(p
, len
);
925 const char *xml
= cc
->gdb_get_dynamic_xml(cpu
, xmlname
);
933 name
= xml_builtin
[i
][0];
934 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
937 return name
? xml_builtin
[i
][1] : NULL
;
940 static int gdb_read_register(CPUState
*cpu
, GByteArray
*buf
, int reg
)
942 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
943 CPUArchState
*env
= cpu
->env_ptr
;
946 if (reg
< cc
->gdb_num_core_regs
) {
947 return cc
->gdb_read_register(cpu
, buf
, reg
);
950 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
951 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
952 return r
->get_reg(env
, buf
, reg
- r
->base_reg
);
958 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
960 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
961 CPUArchState
*env
= cpu
->env_ptr
;
964 if (reg
< cc
->gdb_num_core_regs
) {
965 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
968 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
969 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
970 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
976 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
977 specifies the first register number and these registers are included in
978 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
979 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
982 void gdb_register_coprocessor(CPUState
*cpu
,
983 gdb_get_reg_cb get_reg
, gdb_set_reg_cb set_reg
,
984 int num_regs
, const char *xml
, int g_pos
)
987 GDBRegisterState
**p
;
991 /* Check for duplicates. */
992 if (strcmp((*p
)->xml
, xml
) == 0)
997 s
= g_new0(GDBRegisterState
, 1);
998 s
->base_reg
= cpu
->gdb_num_regs
;
999 s
->num_regs
= num_regs
;
1000 s
->get_reg
= get_reg
;
1001 s
->set_reg
= set_reg
;
1004 /* Add to end of list. */
1005 cpu
->gdb_num_regs
+= num_regs
;
1008 if (g_pos
!= s
->base_reg
) {
1009 error_report("Error: Bad gdb register numbering for '%s', "
1010 "expected %d got %d", xml
, g_pos
, s
->base_reg
);
1012 cpu
->gdb_num_g_regs
= cpu
->gdb_num_regs
;
1017 static void gdb_process_breakpoint_remove_all(GDBProcess
*p
)
1019 CPUState
*cpu
= get_first_cpu_in_process(p
);
1022 gdb_breakpoint_remove_all(cpu
);
1023 cpu
= gdb_next_cpu_in_process(cpu
);
1028 static void gdb_set_cpu_pc(target_ulong pc
)
1030 CPUState
*cpu
= gdbserver_state
.c_cpu
;
1032 cpu_synchronize_state(cpu
);
1033 cpu_set_pc(cpu
, pc
);
1036 static void gdb_append_thread_id(CPUState
*cpu
, GString
*buf
)
1038 if (gdbserver_state
.multiprocess
) {
1039 g_string_append_printf(buf
, "p%02x.%02x",
1040 gdb_get_cpu_pid(cpu
), cpu_gdb_index(cpu
));
1042 g_string_append_printf(buf
, "%02x", cpu_gdb_index(cpu
));
1046 typedef enum GDBThreadIdKind
{
1048 GDB_ALL_THREADS
, /* One process, all threads */
1053 static GDBThreadIdKind
read_thread_id(const char *buf
, const char **end_buf
,
1054 uint32_t *pid
, uint32_t *tid
)
1061 ret
= qemu_strtoul(buf
, &buf
, 16, &p
);
1064 return GDB_READ_THREAD_ERR
;
1073 ret
= qemu_strtoul(buf
, &buf
, 16, &t
);
1076 return GDB_READ_THREAD_ERR
;
1082 return GDB_ALL_PROCESSES
;
1090 return GDB_ALL_THREADS
;
1097 return GDB_ONE_THREAD
;
1101 * gdb_handle_vcont - Parses and handles a vCont packet.
1102 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1103 * a format error, 0 on success.
1105 static int gdb_handle_vcont(const char *p
)
1107 int res
, signal
= 0;
1112 GDBProcess
*process
;
1114 GDBThreadIdKind kind
;
1115 #ifdef CONFIG_USER_ONLY
1116 int max_cpus
= 1; /* global variable max_cpus exists only in system mode */
1119 max_cpus
= max_cpus
<= cpu
->cpu_index
? cpu
->cpu_index
+ 1 : max_cpus
;
1122 MachineState
*ms
= MACHINE(qdev_get_machine());
1123 unsigned int max_cpus
= ms
->smp
.max_cpus
;
1125 /* uninitialised CPUs stay 0 */
1126 newstates
= g_new0(char, max_cpus
);
1128 /* mark valid CPUs with 1 */
1130 newstates
[cpu
->cpu_index
] = 1;
1134 * res keeps track of what error we are returning, with -ENOTSUP meaning
1135 * that the command is unknown or unsupported, thus returning an empty
1136 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1137 * or incorrect parameters passed.
1147 if (cur_action
== 'C' || cur_action
== 'S') {
1148 cur_action
= qemu_tolower(cur_action
);
1149 res
= qemu_strtoul(p
, &p
, 16, &tmp
);
1153 signal
= gdb_signal_to_target(tmp
);
1154 } else if (cur_action
!= 'c' && cur_action
!= 's') {
1155 /* unknown/invalid/unsupported command */
1160 if (*p
== '\0' || *p
== ';') {
1162 * No thread specifier, action is on "all threads". The
1163 * specification is unclear regarding the process to act on. We
1164 * choose all processes.
1166 kind
= GDB_ALL_PROCESSES
;
1167 } else if (*p
++ == ':') {
1168 kind
= read_thread_id(p
, &p
, &pid
, &tid
);
1175 case GDB_READ_THREAD_ERR
:
1179 case GDB_ALL_PROCESSES
:
1180 cpu
= gdb_first_attached_cpu();
1182 if (newstates
[cpu
->cpu_index
] == 1) {
1183 newstates
[cpu
->cpu_index
] = cur_action
;
1186 cpu
= gdb_next_attached_cpu(cpu
);
1190 case GDB_ALL_THREADS
:
1191 process
= gdb_get_process(pid
);
1193 if (!process
->attached
) {
1198 cpu
= get_first_cpu_in_process(process
);
1200 if (newstates
[cpu
->cpu_index
] == 1) {
1201 newstates
[cpu
->cpu_index
] = cur_action
;
1204 cpu
= gdb_next_cpu_in_process(cpu
);
1208 case GDB_ONE_THREAD
:
1209 cpu
= gdb_get_cpu(pid
, tid
);
1211 /* invalid CPU/thread specified */
1217 /* only use if no previous match occourred */
1218 if (newstates
[cpu
->cpu_index
] == 1) {
1219 newstates
[cpu
->cpu_index
] = cur_action
;
1224 gdbserver_state
.signal
= signal
;
1225 gdb_continue_partial(newstates
);
1233 typedef union GdbCmdVariant
{
1236 unsigned long val_ul
;
1237 unsigned long long val_ull
;
1239 GDBThreadIdKind kind
;
1245 #define get_param(p, i) (&g_array_index(p, GdbCmdVariant, i))
1247 static const char *cmd_next_param(const char *param
, const char delimiter
)
1249 static const char all_delimiters
[] = ",;:=";
1250 char curr_delimiters
[2] = {0};
1251 const char *delimiters
;
1253 if (delimiter
== '?') {
1254 delimiters
= all_delimiters
;
1255 } else if (delimiter
== '0') {
1256 return strchr(param
, '\0');
1257 } else if (delimiter
== '.' && *param
) {
1260 curr_delimiters
[0] = delimiter
;
1261 delimiters
= curr_delimiters
;
1264 param
+= strcspn(param
, delimiters
);
1271 static int cmd_parse_params(const char *data
, const char *schema
,
1274 const char *curr_schema
, *curr_data
;
1277 g_assert(params
->len
== 0);
1279 curr_schema
= schema
;
1281 while (curr_schema
[0] && curr_schema
[1] && *curr_data
) {
1282 GdbCmdVariant this_param
;
1284 switch (curr_schema
[0]) {
1286 if (qemu_strtoul(curr_data
, &curr_data
, 16,
1287 &this_param
.val_ul
)) {
1290 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1291 g_array_append_val(params
, this_param
);
1294 if (qemu_strtou64(curr_data
, &curr_data
, 16,
1295 (uint64_t *)&this_param
.val_ull
)) {
1298 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1299 g_array_append_val(params
, this_param
);
1302 this_param
.data
= curr_data
;
1303 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1304 g_array_append_val(params
, this_param
);
1307 this_param
.opcode
= *(uint8_t *)curr_data
;
1308 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1309 g_array_append_val(params
, this_param
);
1312 this_param
.thread_id
.kind
=
1313 read_thread_id(curr_data
, &curr_data
,
1314 &this_param
.thread_id
.pid
,
1315 &this_param
.thread_id
.tid
);
1316 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1317 g_array_append_val(params
, this_param
);
1320 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1331 typedef void (*GdbCmdHandler
)(GArray
*params
, void *user_ctx
);
1334 * cmd_startswith -> cmd is compared using startswith
1337 * schema definitions:
1338 * Each schema parameter entry consists of 2 chars,
1339 * the first char represents the parameter type handling
1340 * the second char represents the delimiter for the next parameter
1342 * Currently supported schema types:
1343 * 'l' -> unsigned long (stored in .val_ul)
1344 * 'L' -> unsigned long long (stored in .val_ull)
1345 * 's' -> string (stored in .data)
1346 * 'o' -> single char (stored in .opcode)
1347 * 't' -> thread id (stored in .thread_id)
1348 * '?' -> skip according to delimiter
1350 * Currently supported delimiters:
1351 * '?' -> Stop at any delimiter (",;:=\0")
1352 * '0' -> Stop at "\0"
1353 * '.' -> Skip 1 char unless reached "\0"
1354 * Any other value is treated as the delimiter value itself
1356 typedef struct GdbCmdParseEntry
{
1357 GdbCmdHandler handler
;
1359 bool cmd_startswith
;
1363 static inline int startswith(const char *string
, const char *pattern
)
1365 return !strncmp(string
, pattern
, strlen(pattern
));
1368 static int process_string_cmd(void *user_ctx
, const char *data
,
1369 const GdbCmdParseEntry
*cmds
, int num_cmds
)
1372 g_autoptr(GArray
) params
= g_array_new(false, true, sizeof(GdbCmdVariant
));
1378 for (i
= 0; i
< num_cmds
; i
++) {
1379 const GdbCmdParseEntry
*cmd
= &cmds
[i
];
1380 g_assert(cmd
->handler
&& cmd
->cmd
);
1382 if ((cmd
->cmd_startswith
&& !startswith(data
, cmd
->cmd
)) ||
1383 (!cmd
->cmd_startswith
&& strcmp(cmd
->cmd
, data
))) {
1388 if (cmd_parse_params(&data
[strlen(cmd
->cmd
)],
1389 cmd
->schema
, params
)) {
1394 cmd
->handler(params
, user_ctx
);
1401 static void run_cmd_parser(const char *data
, const GdbCmdParseEntry
*cmd
)
1407 g_string_set_size(gdbserver_state
.str_buf
, 0);
1408 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
1410 /* In case there was an error during the command parsing we must
1411 * send a NULL packet to indicate the command is not supported */
1412 if (process_string_cmd(NULL
, data
, cmd
, 1)) {
1417 static void handle_detach(GArray
*params
, void *user_ctx
)
1419 GDBProcess
*process
;
1422 if (gdbserver_state
.multiprocess
) {
1428 pid
= get_param(params
, 0)->val_ul
;
1431 process
= gdb_get_process(pid
);
1432 gdb_process_breakpoint_remove_all(process
);
1433 process
->attached
= false;
1435 if (pid
== gdb_get_cpu_pid(gdbserver_state
.c_cpu
)) {
1436 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
1439 if (pid
== gdb_get_cpu_pid(gdbserver_state
.g_cpu
)) {
1440 gdbserver_state
.g_cpu
= gdb_first_attached_cpu();
1443 if (!gdbserver_state
.c_cpu
) {
1444 /* No more process attached */
1445 gdb_syscall_mode
= GDB_SYS_DISABLED
;
1451 static void handle_thread_alive(GArray
*params
, void *user_ctx
)
1460 if (get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1465 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
1466 get_param(params
, 0)->thread_id
.tid
);
1475 static void handle_continue(GArray
*params
, void *user_ctx
)
1478 gdb_set_cpu_pc(get_param(params
, 0)->val_ull
);
1481 gdbserver_state
.signal
= 0;
1485 static void handle_cont_with_sig(GArray
*params
, void *user_ctx
)
1487 unsigned long signal
= 0;
1490 * Note: C sig;[addr] is currently unsupported and we simply
1491 * omit the addr parameter
1494 signal
= get_param(params
, 0)->val_ul
;
1497 gdbserver_state
.signal
= gdb_signal_to_target(signal
);
1498 if (gdbserver_state
.signal
== -1) {
1499 gdbserver_state
.signal
= 0;
1504 static void handle_set_thread(GArray
*params
, void *user_ctx
)
1508 if (params
->len
!= 2) {
1513 if (get_param(params
, 1)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1518 if (get_param(params
, 1)->thread_id
.kind
!= GDB_ONE_THREAD
) {
1523 cpu
= gdb_get_cpu(get_param(params
, 1)->thread_id
.pid
,
1524 get_param(params
, 1)->thread_id
.tid
);
1531 * Note: This command is deprecated and modern gdb's will be using the
1532 * vCont command instead.
1534 switch (get_param(params
, 0)->opcode
) {
1536 gdbserver_state
.c_cpu
= cpu
;
1540 gdbserver_state
.g_cpu
= cpu
;
1549 static void handle_insert_bp(GArray
*params
, void *user_ctx
)
1553 if (params
->len
!= 3) {
1558 res
= gdb_breakpoint_insert(gdbserver_state
.c_cpu
,
1559 get_param(params
, 0)->val_ul
,
1560 get_param(params
, 1)->val_ull
,
1561 get_param(params
, 2)->val_ull
);
1565 } else if (res
== -ENOSYS
) {
1573 static void handle_remove_bp(GArray
*params
, void *user_ctx
)
1577 if (params
->len
!= 3) {
1582 res
= gdb_breakpoint_remove(gdbserver_state
.c_cpu
,
1583 get_param(params
, 0)->val_ul
,
1584 get_param(params
, 1)->val_ull
,
1585 get_param(params
, 2)->val_ull
);
1589 } else if (res
== -ENOSYS
) {
1598 * handle_set/get_reg
1600 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1601 * This works, but can be very slow. Anything new enough to understand
1602 * XML also knows how to use this properly. However to use this we
1603 * need to define a local XML file as well as be talking to a
1604 * reasonably modern gdb. Responding with an empty packet will cause
1605 * the remote gdb to fallback to older methods.
1608 static void handle_set_reg(GArray
*params
, void *user_ctx
)
1617 if (params
->len
!= 2) {
1622 reg_size
= strlen(get_param(params
, 1)->data
) / 2;
1623 hextomem(gdbserver_state
.mem_buf
, get_param(params
, 1)->data
, reg_size
);
1624 gdb_write_register(gdbserver_state
.g_cpu
, gdbserver_state
.mem_buf
->data
,
1625 get_param(params
, 0)->val_ull
);
1629 static void handle_get_reg(GArray
*params
, void *user_ctx
)
1643 reg_size
= gdb_read_register(gdbserver_state
.g_cpu
,
1644 gdbserver_state
.mem_buf
,
1645 get_param(params
, 0)->val_ull
);
1650 g_byte_array_set_size(gdbserver_state
.mem_buf
, reg_size
);
1653 memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
, reg_size
);
1657 static void handle_write_mem(GArray
*params
, void *user_ctx
)
1659 if (params
->len
!= 3) {
1664 /* hextomem() reads 2*len bytes */
1665 if (get_param(params
, 1)->val_ull
>
1666 strlen(get_param(params
, 2)->data
) / 2) {
1671 hextomem(gdbserver_state
.mem_buf
, get_param(params
, 2)->data
,
1672 get_param(params
, 1)->val_ull
);
1673 if (target_memory_rw_debug(gdbserver_state
.g_cpu
,
1674 get_param(params
, 0)->val_ull
,
1675 gdbserver_state
.mem_buf
->data
,
1676 gdbserver_state
.mem_buf
->len
, true)) {
1684 static void handle_read_mem(GArray
*params
, void *user_ctx
)
1686 if (params
->len
!= 2) {
1691 /* memtohex() doubles the required space */
1692 if (get_param(params
, 1)->val_ull
> MAX_PACKET_LENGTH
/ 2) {
1697 g_byte_array_set_size(gdbserver_state
.mem_buf
,
1698 get_param(params
, 1)->val_ull
);
1700 if (target_memory_rw_debug(gdbserver_state
.g_cpu
,
1701 get_param(params
, 0)->val_ull
,
1702 gdbserver_state
.mem_buf
->data
,
1703 gdbserver_state
.mem_buf
->len
, false)) {
1708 memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
,
1709 gdbserver_state
.mem_buf
->len
);
1713 static void handle_write_all_regs(GArray
*params
, void *user_ctx
)
1715 target_ulong addr
, len
;
1723 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1724 len
= strlen(get_param(params
, 0)->data
) / 2;
1725 hextomem(gdbserver_state
.mem_buf
, get_param(params
, 0)->data
, len
);
1726 registers
= gdbserver_state
.mem_buf
->data
;
1727 for (addr
= 0; addr
< gdbserver_state
.g_cpu
->gdb_num_g_regs
&& len
> 0;
1729 reg_size
= gdb_write_register(gdbserver_state
.g_cpu
, registers
, addr
);
1731 registers
+= reg_size
;
1736 static void handle_read_all_regs(GArray
*params
, void *user_ctx
)
1738 target_ulong addr
, len
;
1740 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1741 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
1743 for (addr
= 0; addr
< gdbserver_state
.g_cpu
->gdb_num_g_regs
; addr
++) {
1744 len
+= gdb_read_register(gdbserver_state
.g_cpu
,
1745 gdbserver_state
.mem_buf
,
1748 g_assert(len
== gdbserver_state
.mem_buf
->len
);
1750 memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
, len
);
1754 static void handle_file_io(GArray
*params
, void *user_ctx
)
1756 if (params
->len
>= 1 && gdbserver_state
.current_syscall_cb
) {
1760 ret
= get_param(params
, 0)->val_ull
;
1761 if (params
->len
>= 2) {
1762 err
= get_param(params
, 1)->val_ull
;
1767 /* Convert GDB error numbers back to host error numbers. */
1768 #define E(X) case GDB_E##X: err = E##X; break
1797 gdbserver_state
.current_syscall_cb(gdbserver_state
.c_cpu
, ret
, err
);
1798 gdbserver_state
.current_syscall_cb
= NULL
;
1801 if (params
->len
>= 3 && get_param(params
, 2)->opcode
== (uint8_t)'C') {
1809 static void handle_step(GArray
*params
, void *user_ctx
)
1812 gdb_set_cpu_pc((target_ulong
)get_param(params
, 0)->val_ull
);
1815 cpu_single_step(gdbserver_state
.c_cpu
, gdbserver_state
.sstep_flags
);
1819 static void handle_backward(GArray
*params
, void *user_ctx
)
1821 if (!stub_can_reverse()) {
1824 if (params
->len
== 1) {
1825 switch (get_param(params
, 0)->opcode
) {
1827 if (replay_reverse_step()) {
1834 if (replay_reverse_continue()) {
1843 /* Default invalid command */
1847 static void handle_v_cont_query(GArray
*params
, void *user_ctx
)
1849 put_packet("vCont;c;C;s;S");
1852 static void handle_v_cont(GArray
*params
, void *user_ctx
)
1860 res
= gdb_handle_vcont(get_param(params
, 0)->data
);
1861 if ((res
== -EINVAL
) || (res
== -ERANGE
)) {
1868 static void handle_v_attach(GArray
*params
, void *user_ctx
)
1870 GDBProcess
*process
;
1873 g_string_assign(gdbserver_state
.str_buf
, "E22");
1878 process
= gdb_get_process(get_param(params
, 0)->val_ul
);
1883 cpu
= get_first_cpu_in_process(process
);
1888 process
->attached
= true;
1889 gdbserver_state
.g_cpu
= cpu
;
1890 gdbserver_state
.c_cpu
= cpu
;
1892 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
1893 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
1894 g_string_append_c(gdbserver_state
.str_buf
, ';');
1899 static void handle_v_kill(GArray
*params
, void *user_ctx
)
1901 /* Kill the target */
1903 error_report("QEMU: Terminated via GDBstub");
1908 static const GdbCmdParseEntry gdb_v_commands_table
[] = {
1909 /* Order is important if has same prefix */
1911 .handler
= handle_v_cont_query
,
1916 .handler
= handle_v_cont
,
1918 .cmd_startswith
= 1,
1922 .handler
= handle_v_attach
,
1924 .cmd_startswith
= 1,
1928 .handler
= handle_v_kill
,
1934 static void handle_v_commands(GArray
*params
, void *user_ctx
)
1940 if (process_string_cmd(NULL
, get_param(params
, 0)->data
,
1941 gdb_v_commands_table
,
1942 ARRAY_SIZE(gdb_v_commands_table
))) {
1947 static void handle_query_qemu_sstepbits(GArray
*params
, void *user_ctx
)
1949 g_string_printf(gdbserver_state
.str_buf
, "ENABLE=%x", SSTEP_ENABLE
);
1951 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOIRQ
) {
1952 g_string_append_printf(gdbserver_state
.str_buf
, ",NOIRQ=%x",
1956 if (gdbserver_state
.supported_sstep_flags
& SSTEP_NOTIMER
) {
1957 g_string_append_printf(gdbserver_state
.str_buf
, ",NOTIMER=%x",
1964 static void handle_set_qemu_sstep(GArray
*params
, void *user_ctx
)
1966 int new_sstep_flags
;
1972 new_sstep_flags
= get_param(params
, 0)->val_ul
;
1974 if (new_sstep_flags
& ~gdbserver_state
.supported_sstep_flags
) {
1979 gdbserver_state
.sstep_flags
= new_sstep_flags
;
1983 static void handle_query_qemu_sstep(GArray
*params
, void *user_ctx
)
1985 g_string_printf(gdbserver_state
.str_buf
, "0x%x",
1986 gdbserver_state
.sstep_flags
);
1990 static void handle_query_curr_tid(GArray
*params
, void *user_ctx
)
1993 GDBProcess
*process
;
1996 * "Current thread" remains vague in the spec, so always return
1997 * the first thread of the current process (gdb returns the
2000 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
2001 cpu
= get_first_cpu_in_process(process
);
2002 g_string_assign(gdbserver_state
.str_buf
, "QC");
2003 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
2007 static void handle_query_threads(GArray
*params
, void *user_ctx
)
2009 if (!gdbserver_state
.query_cpu
) {
2014 g_string_assign(gdbserver_state
.str_buf
, "m");
2015 gdb_append_thread_id(gdbserver_state
.query_cpu
, gdbserver_state
.str_buf
);
2017 gdbserver_state
.query_cpu
= gdb_next_attached_cpu(gdbserver_state
.query_cpu
);
2020 static void handle_query_first_threads(GArray
*params
, void *user_ctx
)
2022 gdbserver_state
.query_cpu
= gdb_first_attached_cpu();
2023 handle_query_threads(params
, user_ctx
);
2026 static void handle_query_thread_extra(GArray
*params
, void *user_ctx
)
2028 g_autoptr(GString
) rs
= g_string_new(NULL
);
2032 get_param(params
, 0)->thread_id
.kind
== GDB_READ_THREAD_ERR
) {
2037 cpu
= gdb_get_cpu(get_param(params
, 0)->thread_id
.pid
,
2038 get_param(params
, 0)->thread_id
.tid
);
2043 cpu_synchronize_state(cpu
);
2045 if (gdbserver_state
.multiprocess
&& (gdbserver_state
.process_num
> 1)) {
2046 /* Print the CPU model and name in multiprocess mode */
2047 ObjectClass
*oc
= object_get_class(OBJECT(cpu
));
2048 const char *cpu_model
= object_class_get_name(oc
);
2049 const char *cpu_name
=
2050 object_get_canonical_path_component(OBJECT(cpu
));
2051 g_string_printf(rs
, "%s %s [%s]", cpu_model
, cpu_name
,
2052 cpu
->halted
? "halted " : "running");
2054 g_string_printf(rs
, "CPU#%d [%s]", cpu
->cpu_index
,
2055 cpu
->halted
? "halted " : "running");
2057 trace_gdbstub_op_extra_info(rs
->str
);
2058 memtohex(gdbserver_state
.str_buf
, (uint8_t *)rs
->str
, rs
->len
);
2062 #ifdef CONFIG_USER_ONLY
2063 static void handle_query_offsets(GArray
*params
, void *user_ctx
)
2067 ts
= gdbserver_state
.c_cpu
->opaque
;
2068 g_string_printf(gdbserver_state
.str_buf
,
2069 "Text=" TARGET_ABI_FMT_lx
2070 ";Data=" TARGET_ABI_FMT_lx
2071 ";Bss=" TARGET_ABI_FMT_lx
,
2072 ts
->info
->code_offset
,
2073 ts
->info
->data_offset
,
2074 ts
->info
->data_offset
);
2078 static void handle_query_rcmd(GArray
*params
, void *user_ctx
)
2080 const guint8 zero
= 0;
2088 len
= strlen(get_param(params
, 0)->data
);
2094 g_assert(gdbserver_state
.mem_buf
->len
== 0);
2096 hextomem(gdbserver_state
.mem_buf
, get_param(params
, 0)->data
, len
);
2097 g_byte_array_append(gdbserver_state
.mem_buf
, &zero
, 1);
2098 qemu_chr_be_write(gdbserver_state
.mon_chr
, gdbserver_state
.mem_buf
->data
,
2099 gdbserver_state
.mem_buf
->len
);
2104 static void handle_query_supported(GArray
*params
, void *user_ctx
)
2108 g_string_printf(gdbserver_state
.str_buf
, "PacketSize=%x", MAX_PACKET_LENGTH
);
2109 cc
= CPU_GET_CLASS(first_cpu
);
2110 if (cc
->gdb_core_xml_file
) {
2111 g_string_append(gdbserver_state
.str_buf
, ";qXfer:features:read+");
2114 if (stub_can_reverse()) {
2115 g_string_append(gdbserver_state
.str_buf
,
2116 ";ReverseStep+;ReverseContinue+");
2119 #ifdef CONFIG_USER_ONLY
2120 if (gdbserver_state
.c_cpu
->opaque
) {
2121 g_string_append(gdbserver_state
.str_buf
, ";qXfer:auxv:read+");
2126 strstr(get_param(params
, 0)->data
, "multiprocess+")) {
2127 gdbserver_state
.multiprocess
= true;
2130 g_string_append(gdbserver_state
.str_buf
, ";vContSupported+;multiprocess+");
2134 static void handle_query_xfer_features(GArray
*params
, void *user_ctx
)
2136 GDBProcess
*process
;
2138 unsigned long len
, total_len
, addr
;
2142 if (params
->len
< 3) {
2147 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
2148 cc
= CPU_GET_CLASS(gdbserver_state
.g_cpu
);
2149 if (!cc
->gdb_core_xml_file
) {
2155 p
= get_param(params
, 0)->data
;
2156 xml
= get_feature_xml(p
, &p
, process
);
2162 addr
= get_param(params
, 1)->val_ul
;
2163 len
= get_param(params
, 2)->val_ul
;
2164 total_len
= strlen(xml
);
2165 if (addr
> total_len
) {
2170 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
2171 len
= (MAX_PACKET_LENGTH
- 5) / 2;
2174 if (len
< total_len
- addr
) {
2175 g_string_assign(gdbserver_state
.str_buf
, "m");
2176 memtox(gdbserver_state
.str_buf
, xml
+ addr
, len
);
2178 g_string_assign(gdbserver_state
.str_buf
, "l");
2179 memtox(gdbserver_state
.str_buf
, xml
+ addr
, total_len
- addr
);
2182 put_packet_binary(gdbserver_state
.str_buf
->str
,
2183 gdbserver_state
.str_buf
->len
, true);
2186 #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX_USER)
2187 static void handle_query_xfer_auxv(GArray
*params
, void *user_ctx
)
2190 unsigned long offset
, len
, saved_auxv
, auxv_len
;
2192 if (params
->len
< 2) {
2197 offset
= get_param(params
, 0)->val_ul
;
2198 len
= get_param(params
, 1)->val_ul
;
2199 ts
= gdbserver_state
.c_cpu
->opaque
;
2200 saved_auxv
= ts
->info
->saved_auxv
;
2201 auxv_len
= ts
->info
->auxv_len
;
2203 if (offset
>= auxv_len
) {
2208 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
2209 len
= (MAX_PACKET_LENGTH
- 5) / 2;
2212 if (len
< auxv_len
- offset
) {
2213 g_string_assign(gdbserver_state
.str_buf
, "m");
2215 g_string_assign(gdbserver_state
.str_buf
, "l");
2216 len
= auxv_len
- offset
;
2219 g_byte_array_set_size(gdbserver_state
.mem_buf
, len
);
2220 if (target_memory_rw_debug(gdbserver_state
.g_cpu
, saved_auxv
+ offset
,
2221 gdbserver_state
.mem_buf
->data
, len
, false)) {
2226 memtox(gdbserver_state
.str_buf
,
2227 (const char *)gdbserver_state
.mem_buf
->data
, len
);
2228 put_packet_binary(gdbserver_state
.str_buf
->str
,
2229 gdbserver_state
.str_buf
->len
, true);
2233 static void handle_query_attached(GArray
*params
, void *user_ctx
)
2235 put_packet(GDB_ATTACHED
);
2238 static void handle_query_qemu_supported(GArray
*params
, void *user_ctx
)
2240 g_string_printf(gdbserver_state
.str_buf
, "sstepbits;sstep");
2241 #ifndef CONFIG_USER_ONLY
2242 g_string_append(gdbserver_state
.str_buf
, ";PhyMemMode");
2247 #ifndef CONFIG_USER_ONLY
2248 static void handle_query_qemu_phy_mem_mode(GArray
*params
,
2251 g_string_printf(gdbserver_state
.str_buf
, "%d", phy_memory_mode
);
2255 static void handle_set_qemu_phy_mem_mode(GArray
*params
, void *user_ctx
)
2262 if (!get_param(params
, 0)->val_ul
) {
2263 phy_memory_mode
= 0;
2265 phy_memory_mode
= 1;
2271 static const GdbCmdParseEntry gdb_gen_query_set_common_table
[] = {
2272 /* Order is important if has same prefix */
2274 .handler
= handle_query_qemu_sstepbits
,
2275 .cmd
= "qemu.sstepbits",
2278 .handler
= handle_query_qemu_sstep
,
2279 .cmd
= "qemu.sstep",
2282 .handler
= handle_set_qemu_sstep
,
2283 .cmd
= "qemu.sstep=",
2284 .cmd_startswith
= 1,
2289 static const GdbCmdParseEntry gdb_gen_query_table
[] = {
2291 .handler
= handle_query_curr_tid
,
2295 .handler
= handle_query_threads
,
2296 .cmd
= "sThreadInfo",
2299 .handler
= handle_query_first_threads
,
2300 .cmd
= "fThreadInfo",
2303 .handler
= handle_query_thread_extra
,
2304 .cmd
= "ThreadExtraInfo,",
2305 .cmd_startswith
= 1,
2308 #ifdef CONFIG_USER_ONLY
2310 .handler
= handle_query_offsets
,
2315 .handler
= handle_query_rcmd
,
2317 .cmd_startswith
= 1,
2322 .handler
= handle_query_supported
,
2323 .cmd
= "Supported:",
2324 .cmd_startswith
= 1,
2328 .handler
= handle_query_supported
,
2333 .handler
= handle_query_xfer_features
,
2334 .cmd
= "Xfer:features:read:",
2335 .cmd_startswith
= 1,
2338 #if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX_USER)
2340 .handler
= handle_query_xfer_auxv
,
2341 .cmd
= "Xfer:auxv:read::",
2342 .cmd_startswith
= 1,
2347 .handler
= handle_query_attached
,
2352 .handler
= handle_query_attached
,
2356 .handler
= handle_query_qemu_supported
,
2357 .cmd
= "qemu.Supported",
2359 #ifndef CONFIG_USER_ONLY
2361 .handler
= handle_query_qemu_phy_mem_mode
,
2362 .cmd
= "qemu.PhyMemMode",
2367 static const GdbCmdParseEntry gdb_gen_set_table
[] = {
2368 /* Order is important if has same prefix */
2370 .handler
= handle_set_qemu_sstep
,
2371 .cmd
= "qemu.sstep:",
2372 .cmd_startswith
= 1,
2375 #ifndef CONFIG_USER_ONLY
2377 .handler
= handle_set_qemu_phy_mem_mode
,
2378 .cmd
= "qemu.PhyMemMode:",
2379 .cmd_startswith
= 1,
2385 static void handle_gen_query(GArray
*params
, void *user_ctx
)
2391 if (!process_string_cmd(NULL
, get_param(params
, 0)->data
,
2392 gdb_gen_query_set_common_table
,
2393 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
2397 if (process_string_cmd(NULL
, get_param(params
, 0)->data
,
2398 gdb_gen_query_table
,
2399 ARRAY_SIZE(gdb_gen_query_table
))) {
2404 static void handle_gen_set(GArray
*params
, void *user_ctx
)
2410 if (!process_string_cmd(NULL
, get_param(params
, 0)->data
,
2411 gdb_gen_query_set_common_table
,
2412 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
2416 if (process_string_cmd(NULL
, get_param(params
, 0)->data
,
2418 ARRAY_SIZE(gdb_gen_set_table
))) {
2423 static void handle_target_halt(GArray
*params
, void *user_ctx
)
2425 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
2426 gdb_append_thread_id(gdbserver_state
.c_cpu
, gdbserver_state
.str_buf
);
2427 g_string_append_c(gdbserver_state
.str_buf
, ';');
2430 * Remove all the breakpoints when this query is issued,
2431 * because gdb is doing an initial connect and the state
2432 * should be cleaned up.
2434 gdb_breakpoint_remove_all(gdbserver_state
.c_cpu
);
2437 static int gdb_handle_packet(const char *line_buf
)
2439 const GdbCmdParseEntry
*cmd_parser
= NULL
;
2441 trace_gdbstub_io_command(line_buf
);
2443 switch (line_buf
[0]) {
2449 static const GdbCmdParseEntry target_halted_cmd_desc
= {
2450 .handler
= handle_target_halt
,
2454 cmd_parser
= &target_halted_cmd_desc
;
2459 static const GdbCmdParseEntry continue_cmd_desc
= {
2460 .handler
= handle_continue
,
2462 .cmd_startswith
= 1,
2465 cmd_parser
= &continue_cmd_desc
;
2470 static const GdbCmdParseEntry cont_with_sig_cmd_desc
= {
2471 .handler
= handle_cont_with_sig
,
2473 .cmd_startswith
= 1,
2476 cmd_parser
= &cont_with_sig_cmd_desc
;
2481 static const GdbCmdParseEntry v_cmd_desc
= {
2482 .handler
= handle_v_commands
,
2484 .cmd_startswith
= 1,
2487 cmd_parser
= &v_cmd_desc
;
2491 /* Kill the target */
2492 error_report("QEMU: Terminated via GDBstub");
2497 static const GdbCmdParseEntry detach_cmd_desc
= {
2498 .handler
= handle_detach
,
2500 .cmd_startswith
= 1,
2503 cmd_parser
= &detach_cmd_desc
;
2508 static const GdbCmdParseEntry step_cmd_desc
= {
2509 .handler
= handle_step
,
2511 .cmd_startswith
= 1,
2514 cmd_parser
= &step_cmd_desc
;
2519 static const GdbCmdParseEntry backward_cmd_desc
= {
2520 .handler
= handle_backward
,
2522 .cmd_startswith
= 1,
2525 cmd_parser
= &backward_cmd_desc
;
2530 static const GdbCmdParseEntry file_io_cmd_desc
= {
2531 .handler
= handle_file_io
,
2533 .cmd_startswith
= 1,
2536 cmd_parser
= &file_io_cmd_desc
;
2541 static const GdbCmdParseEntry read_all_regs_cmd_desc
= {
2542 .handler
= handle_read_all_regs
,
2546 cmd_parser
= &read_all_regs_cmd_desc
;
2551 static const GdbCmdParseEntry write_all_regs_cmd_desc
= {
2552 .handler
= handle_write_all_regs
,
2554 .cmd_startswith
= 1,
2557 cmd_parser
= &write_all_regs_cmd_desc
;
2562 static const GdbCmdParseEntry read_mem_cmd_desc
= {
2563 .handler
= handle_read_mem
,
2565 .cmd_startswith
= 1,
2568 cmd_parser
= &read_mem_cmd_desc
;
2573 static const GdbCmdParseEntry write_mem_cmd_desc
= {
2574 .handler
= handle_write_mem
,
2576 .cmd_startswith
= 1,
2579 cmd_parser
= &write_mem_cmd_desc
;
2584 static const GdbCmdParseEntry get_reg_cmd_desc
= {
2585 .handler
= handle_get_reg
,
2587 .cmd_startswith
= 1,
2590 cmd_parser
= &get_reg_cmd_desc
;
2595 static const GdbCmdParseEntry set_reg_cmd_desc
= {
2596 .handler
= handle_set_reg
,
2598 .cmd_startswith
= 1,
2601 cmd_parser
= &set_reg_cmd_desc
;
2606 static const GdbCmdParseEntry insert_bp_cmd_desc
= {
2607 .handler
= handle_insert_bp
,
2609 .cmd_startswith
= 1,
2612 cmd_parser
= &insert_bp_cmd_desc
;
2617 static const GdbCmdParseEntry remove_bp_cmd_desc
= {
2618 .handler
= handle_remove_bp
,
2620 .cmd_startswith
= 1,
2623 cmd_parser
= &remove_bp_cmd_desc
;
2628 static const GdbCmdParseEntry set_thread_cmd_desc
= {
2629 .handler
= handle_set_thread
,
2631 .cmd_startswith
= 1,
2634 cmd_parser
= &set_thread_cmd_desc
;
2639 static const GdbCmdParseEntry thread_alive_cmd_desc
= {
2640 .handler
= handle_thread_alive
,
2642 .cmd_startswith
= 1,
2645 cmd_parser
= &thread_alive_cmd_desc
;
2650 static const GdbCmdParseEntry gen_query_cmd_desc
= {
2651 .handler
= handle_gen_query
,
2653 .cmd_startswith
= 1,
2656 cmd_parser
= &gen_query_cmd_desc
;
2661 static const GdbCmdParseEntry gen_set_cmd_desc
= {
2662 .handler
= handle_gen_set
,
2664 .cmd_startswith
= 1,
2667 cmd_parser
= &gen_set_cmd_desc
;
2671 /* put empty packet */
2677 run_cmd_parser(line_buf
, cmd_parser
);
2683 void gdb_set_stop_cpu(CPUState
*cpu
)
2685 GDBProcess
*p
= gdb_get_cpu_process(cpu
);
2689 * Having a stop CPU corresponding to a process that is not attached
2690 * confuses GDB. So we ignore the request.
2695 gdbserver_state
.c_cpu
= cpu
;
2696 gdbserver_state
.g_cpu
= cpu
;
2699 #ifndef CONFIG_USER_ONLY
2700 static void gdb_vm_state_change(void *opaque
, bool running
, RunState state
)
2702 CPUState
*cpu
= gdbserver_state
.c_cpu
;
2703 g_autoptr(GString
) buf
= g_string_new(NULL
);
2704 g_autoptr(GString
) tid
= g_string_new(NULL
);
2708 if (running
|| gdbserver_state
.state
== RS_INACTIVE
) {
2711 /* Is there a GDB syscall waiting to be sent? */
2712 if (gdbserver_state
.current_syscall_cb
) {
2713 put_packet(gdbserver_state
.syscall_buf
);
2718 /* No process attached */
2722 gdb_append_thread_id(cpu
, tid
);
2725 case RUN_STATE_DEBUG
:
2726 if (cpu
->watchpoint_hit
) {
2727 switch (cpu
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
2738 trace_gdbstub_hit_watchpoint(type
, cpu_gdb_index(cpu
),
2739 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
2740 g_string_printf(buf
, "T%02xthread:%s;%swatch:" TARGET_FMT_lx
";",
2741 GDB_SIGNAL_TRAP
, tid
->str
, type
,
2742 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
2743 cpu
->watchpoint_hit
= NULL
;
2746 trace_gdbstub_hit_break();
2749 ret
= GDB_SIGNAL_TRAP
;
2751 case RUN_STATE_PAUSED
:
2752 trace_gdbstub_hit_paused();
2753 ret
= GDB_SIGNAL_INT
;
2755 case RUN_STATE_SHUTDOWN
:
2756 trace_gdbstub_hit_shutdown();
2757 ret
= GDB_SIGNAL_QUIT
;
2759 case RUN_STATE_IO_ERROR
:
2760 trace_gdbstub_hit_io_error();
2761 ret
= GDB_SIGNAL_IO
;
2763 case RUN_STATE_WATCHDOG
:
2764 trace_gdbstub_hit_watchdog();
2765 ret
= GDB_SIGNAL_ALRM
;
2767 case RUN_STATE_INTERNAL_ERROR
:
2768 trace_gdbstub_hit_internal_error();
2769 ret
= GDB_SIGNAL_ABRT
;
2771 case RUN_STATE_SAVE_VM
:
2772 case RUN_STATE_RESTORE_VM
:
2774 case RUN_STATE_FINISH_MIGRATE
:
2775 ret
= GDB_SIGNAL_XCPU
;
2778 trace_gdbstub_hit_unknown(state
);
2779 ret
= GDB_SIGNAL_UNKNOWN
;
2782 gdb_set_stop_cpu(cpu
);
2783 g_string_printf(buf
, "T%02xthread:%s;", ret
, tid
->str
);
2786 put_packet(buf
->str
);
2788 /* disable single step if it was enabled */
2789 cpu_single_step(cpu
, 0);
2793 /* Send a gdb syscall request.
2794 This accepts limited printf-style format specifiers, specifically:
2795 %x - target_ulong argument printed in hex.
2796 %lx - 64-bit argument printed in hex.
2797 %s - string pointer (target_ulong) and length (int) pair. */
2798 void gdb_do_syscallv(gdb_syscall_complete_cb cb
, const char *fmt
, va_list va
)
2805 if (!gdb_attached()) {
2809 gdbserver_state
.current_syscall_cb
= cb
;
2810 #ifndef CONFIG_USER_ONLY
2811 vm_stop(RUN_STATE_DEBUG
);
2813 p
= &gdbserver_state
.syscall_buf
[0];
2814 p_end
= &gdbserver_state
.syscall_buf
[sizeof(gdbserver_state
.syscall_buf
)];
2821 addr
= va_arg(va
, target_ulong
);
2822 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
, addr
);
2825 if (*(fmt
++) != 'x')
2827 i64
= va_arg(va
, uint64_t);
2828 p
+= snprintf(p
, p_end
- p
, "%" PRIx64
, i64
);
2831 addr
= va_arg(va
, target_ulong
);
2832 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
"/%x",
2833 addr
, va_arg(va
, int));
2837 error_report("gdbstub: Bad syscall format string '%s'",
2846 #ifdef CONFIG_USER_ONLY
2847 put_packet(gdbserver_state
.syscall_buf
);
2848 /* Return control to gdb for it to process the syscall request.
2849 * Since the protocol requires that gdb hands control back to us
2850 * using a "here are the results" F packet, we don't need to check
2851 * gdb_handlesig's return value (which is the signal to deliver if
2852 * execution was resumed via a continue packet).
2854 gdb_handlesig(gdbserver_state
.c_cpu
, 0);
2856 /* In this case wait to send the syscall packet until notification that
2857 the CPU has stopped. This must be done because if the packet is sent
2858 now the reply from the syscall request could be received while the CPU
2859 is still in the running state, which can cause packets to be dropped
2860 and state transition 'T' packets to be sent while the syscall is still
2862 qemu_cpu_kick(gdbserver_state
.c_cpu
);
2866 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
2871 gdb_do_syscallv(cb
, fmt
, va
);
2875 static void gdb_read_byte(uint8_t ch
)
2879 #ifndef CONFIG_USER_ONLY
2880 if (gdbserver_state
.last_packet
->len
) {
2881 /* Waiting for a response to the last packet. If we see the start
2882 of a new command then abandon the previous response. */
2884 trace_gdbstub_err_got_nack();
2885 put_buffer(gdbserver_state
.last_packet
->data
,
2886 gdbserver_state
.last_packet
->len
);
2887 } else if (ch
== '+') {
2888 trace_gdbstub_io_got_ack();
2890 trace_gdbstub_io_got_unexpected(ch
);
2893 if (ch
== '+' || ch
== '$') {
2894 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
2899 if (runstate_is_running()) {
2900 /* when the CPU is running, we cannot do anything except stop
2901 it when receiving a char */
2902 vm_stop(RUN_STATE_PAUSED
);
2906 switch(gdbserver_state
.state
) {
2909 /* start of command packet */
2910 gdbserver_state
.line_buf_index
= 0;
2911 gdbserver_state
.line_sum
= 0;
2912 gdbserver_state
.state
= RS_GETLINE
;
2914 trace_gdbstub_err_garbage(ch
);
2919 /* start escape sequence */
2920 gdbserver_state
.state
= RS_GETLINE_ESC
;
2921 gdbserver_state
.line_sum
+= ch
;
2922 } else if (ch
== '*') {
2923 /* start run length encoding sequence */
2924 gdbserver_state
.state
= RS_GETLINE_RLE
;
2925 gdbserver_state
.line_sum
+= ch
;
2926 } else if (ch
== '#') {
2927 /* end of command, start of checksum*/
2928 gdbserver_state
.state
= RS_CHKSUM1
;
2929 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2930 trace_gdbstub_err_overrun();
2931 gdbserver_state
.state
= RS_IDLE
;
2933 /* unescaped command character */
2934 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
;
2935 gdbserver_state
.line_sum
+= ch
;
2938 case RS_GETLINE_ESC
:
2940 /* unexpected end of command in escape sequence */
2941 gdbserver_state
.state
= RS_CHKSUM1
;
2942 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2943 /* command buffer overrun */
2944 trace_gdbstub_err_overrun();
2945 gdbserver_state
.state
= RS_IDLE
;
2947 /* parse escaped character and leave escape state */
2948 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
^ 0x20;
2949 gdbserver_state
.line_sum
+= ch
;
2950 gdbserver_state
.state
= RS_GETLINE
;
2953 case RS_GETLINE_RLE
:
2955 * Run-length encoding is explained in "Debugging with GDB /
2956 * Appendix E GDB Remote Serial Protocol / Overview".
2958 if (ch
< ' ' || ch
== '#' || ch
== '$' || ch
> 126) {
2959 /* invalid RLE count encoding */
2960 trace_gdbstub_err_invalid_repeat(ch
);
2961 gdbserver_state
.state
= RS_GETLINE
;
2963 /* decode repeat length */
2964 int repeat
= ch
- ' ' + 3;
2965 if (gdbserver_state
.line_buf_index
+ repeat
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2966 /* that many repeats would overrun the command buffer */
2967 trace_gdbstub_err_overrun();
2968 gdbserver_state
.state
= RS_IDLE
;
2969 } else if (gdbserver_state
.line_buf_index
< 1) {
2970 /* got a repeat but we have nothing to repeat */
2971 trace_gdbstub_err_invalid_rle();
2972 gdbserver_state
.state
= RS_GETLINE
;
2974 /* repeat the last character */
2975 memset(gdbserver_state
.line_buf
+ gdbserver_state
.line_buf_index
,
2976 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
- 1], repeat
);
2977 gdbserver_state
.line_buf_index
+= repeat
;
2978 gdbserver_state
.line_sum
+= ch
;
2979 gdbserver_state
.state
= RS_GETLINE
;
2984 /* get high hex digit of checksum */
2985 if (!isxdigit(ch
)) {
2986 trace_gdbstub_err_checksum_invalid(ch
);
2987 gdbserver_state
.state
= RS_GETLINE
;
2990 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
] = '\0';
2991 gdbserver_state
.line_csum
= fromhex(ch
) << 4;
2992 gdbserver_state
.state
= RS_CHKSUM2
;
2995 /* get low hex digit of checksum */
2996 if (!isxdigit(ch
)) {
2997 trace_gdbstub_err_checksum_invalid(ch
);
2998 gdbserver_state
.state
= RS_GETLINE
;
3001 gdbserver_state
.line_csum
|= fromhex(ch
);
3003 if (gdbserver_state
.line_csum
!= (gdbserver_state
.line_sum
& 0xff)) {
3004 trace_gdbstub_err_checksum_incorrect(gdbserver_state
.line_sum
, gdbserver_state
.line_csum
);
3005 /* send NAK reply */
3007 put_buffer(&reply
, 1);
3008 gdbserver_state
.state
= RS_IDLE
;
3010 /* send ACK reply */
3012 put_buffer(&reply
, 1);
3013 gdbserver_state
.state
= gdb_handle_packet(gdbserver_state
.line_buf
);
3022 /* Tell the remote gdb that the process has exited. */
3023 void gdb_exit(int code
)
3027 if (!gdbserver_state
.init
) {
3030 #ifdef CONFIG_USER_ONLY
3031 if (gdbserver_state
.socket_path
) {
3032 unlink(gdbserver_state
.socket_path
);
3034 if (gdbserver_state
.fd
< 0) {
3039 trace_gdbstub_op_exiting((uint8_t)code
);
3041 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
3044 #ifndef CONFIG_USER_ONLY
3045 qemu_chr_fe_deinit(&gdbserver_state
.chr
, true);
3050 * Create the process that will contain all the "orphan" CPUs (that are not
3051 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
3052 * be attachable and thus will be invisible to the user.
3054 static void create_default_process(GDBState
*s
)
3056 GDBProcess
*process
;
3059 if (gdbserver_state
.process_num
) {
3060 max_pid
= s
->processes
[s
->process_num
- 1].pid
;
3063 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
3064 process
= &s
->processes
[s
->process_num
- 1];
3066 /* We need an available PID slot for this process */
3067 assert(max_pid
< UINT32_MAX
);
3069 process
->pid
= max_pid
+ 1;
3070 process
->attached
= false;
3071 process
->target_xml
[0] = '\0';
3074 #ifdef CONFIG_USER_ONLY
3076 gdb_handlesig(CPUState
*cpu
, int sig
)
3081 if (!gdbserver_state
.init
|| gdbserver_state
.fd
< 0) {
3085 /* disable single step if it was enabled */
3086 cpu_single_step(cpu
, 0);
3090 gdb_set_stop_cpu(cpu
);
3091 g_string_printf(gdbserver_state
.str_buf
,
3092 "T%02xthread:", target_signal_to_gdb(sig
));
3093 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
3094 g_string_append_c(gdbserver_state
.str_buf
, ';');
3097 /* put_packet() might have detected that the peer terminated the
3099 if (gdbserver_state
.fd
< 0) {
3104 gdbserver_state
.state
= RS_IDLE
;
3105 gdbserver_state
.running_state
= 0;
3106 while (gdbserver_state
.running_state
== 0) {
3107 n
= read(gdbserver_state
.fd
, buf
, 256);
3111 for (i
= 0; i
< n
; i
++) {
3112 gdb_read_byte(buf
[i
]);
3115 /* XXX: Connection closed. Should probably wait for another
3116 connection before continuing. */
3118 close(gdbserver_state
.fd
);
3120 gdbserver_state
.fd
= -1;
3124 sig
= gdbserver_state
.signal
;
3125 gdbserver_state
.signal
= 0;
3129 /* Tell the remote gdb that the process has exited due to SIG. */
3130 void gdb_signalled(CPUArchState
*env
, int sig
)
3134 if (!gdbserver_state
.init
|| gdbserver_state
.fd
< 0) {
3138 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb(sig
));
3142 static void gdb_accept_init(int fd
)
3144 init_gdbserver_state();
3145 create_default_process(&gdbserver_state
);
3146 gdbserver_state
.processes
[0].attached
= true;
3147 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
3148 gdbserver_state
.g_cpu
= gdbserver_state
.c_cpu
;
3149 gdbserver_state
.fd
= fd
;
3150 gdb_has_xml
= false;
3153 static bool gdb_accept_socket(int gdb_fd
)
3158 fd
= accept(gdb_fd
, NULL
, NULL
);
3159 if (fd
< 0 && errno
!= EINTR
) {
3160 perror("accept socket");
3162 } else if (fd
>= 0) {
3163 qemu_set_cloexec(fd
);
3168 gdb_accept_init(fd
);
3172 static int gdbserver_open_socket(const char *path
)
3174 struct sockaddr_un sockaddr
= {};
3177 fd
= socket(AF_UNIX
, SOCK_STREAM
, 0);
3179 perror("create socket");
3183 sockaddr
.sun_family
= AF_UNIX
;
3184 pstrcpy(sockaddr
.sun_path
, sizeof(sockaddr
.sun_path
) - 1, path
);
3185 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
3187 perror("bind socket");
3191 ret
= listen(fd
, 1);
3193 perror("listen socket");
3201 static bool gdb_accept_tcp(int gdb_fd
)
3203 struct sockaddr_in sockaddr
= {};
3208 len
= sizeof(sockaddr
);
3209 fd
= accept(gdb_fd
, (struct sockaddr
*)&sockaddr
, &len
);
3210 if (fd
< 0 && errno
!= EINTR
) {
3213 } else if (fd
>= 0) {
3214 qemu_set_cloexec(fd
);
3219 /* set short latency */
3220 if (socket_set_nodelay(fd
)) {
3221 perror("setsockopt");
3226 gdb_accept_init(fd
);
3230 static int gdbserver_open_port(int port
)
3232 struct sockaddr_in sockaddr
;
3235 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3240 qemu_set_cloexec(fd
);
3242 socket_set_fast_reuse(fd
);
3244 sockaddr
.sin_family
= AF_INET
;
3245 sockaddr
.sin_port
= htons(port
);
3246 sockaddr
.sin_addr
.s_addr
= 0;
3247 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
3253 ret
= listen(fd
, 1);
3263 int gdbserver_start(const char *port_or_path
)
3265 int port
= g_ascii_strtoull(port_or_path
, NULL
, 10);
3269 gdb_fd
= gdbserver_open_port(port
);
3271 gdb_fd
= gdbserver_open_socket(port_or_path
);
3278 if (port
> 0 && gdb_accept_tcp(gdb_fd
)) {
3280 } else if (gdb_accept_socket(gdb_fd
)) {
3281 gdbserver_state
.socket_path
= g_strdup(port_or_path
);
3290 /* Disable gdb stub for child processes. */
3291 void gdbserver_fork(CPUState
*cpu
)
3293 if (!gdbserver_state
.init
|| gdbserver_state
.fd
< 0) {
3296 close(gdbserver_state
.fd
);
3297 gdbserver_state
.fd
= -1;
3298 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
3299 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
3302 static int gdb_chr_can_receive(void *opaque
)
3304 /* We can handle an arbitrarily large amount of data.
3305 Pick the maximum packet size, which is as good as anything. */
3306 return MAX_PACKET_LENGTH
;
3309 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
3313 for (i
= 0; i
< size
; i
++) {
3314 gdb_read_byte(buf
[i
]);
3318 static void gdb_chr_event(void *opaque
, QEMUChrEvent event
)
3321 GDBState
*s
= (GDBState
*) opaque
;
3324 case CHR_EVENT_OPENED
:
3325 /* Start with first process attached, others detached */
3326 for (i
= 0; i
< s
->process_num
; i
++) {
3327 s
->processes
[i
].attached
= !i
;
3330 s
->c_cpu
= gdb_first_attached_cpu();
3331 s
->g_cpu
= s
->c_cpu
;
3333 vm_stop(RUN_STATE_PAUSED
);
3334 replay_gdb_attached();
3335 gdb_has_xml
= false;
3342 static int gdb_monitor_write(Chardev
*chr
, const uint8_t *buf
, int len
)
3344 g_autoptr(GString
) hex_buf
= g_string_new("O");
3345 memtohex(hex_buf
, buf
, len
);
3346 put_packet(hex_buf
->str
);
3351 static void gdb_sigterm_handler(int signal
)
3353 if (runstate_is_running()) {
3354 vm_stop(RUN_STATE_PAUSED
);
3359 static void gdb_monitor_open(Chardev
*chr
, ChardevBackend
*backend
,
3360 bool *be_opened
, Error
**errp
)
3365 static void char_gdb_class_init(ObjectClass
*oc
, void *data
)
3367 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
3369 cc
->internal
= true;
3370 cc
->open
= gdb_monitor_open
;
3371 cc
->chr_write
= gdb_monitor_write
;
3374 #define TYPE_CHARDEV_GDB "chardev-gdb"
3376 static const TypeInfo char_gdb_type_info
= {
3377 .name
= TYPE_CHARDEV_GDB
,
3378 .parent
= TYPE_CHARDEV
,
3379 .class_init
= char_gdb_class_init
,
3382 static int find_cpu_clusters(Object
*child
, void *opaque
)
3384 if (object_dynamic_cast(child
, TYPE_CPU_CLUSTER
)) {
3385 GDBState
*s
= (GDBState
*) opaque
;
3386 CPUClusterState
*cluster
= CPU_CLUSTER(child
);
3387 GDBProcess
*process
;
3389 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
3391 process
= &s
->processes
[s
->process_num
- 1];
3394 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
3395 * runtime, we enforce here that the machine does not use a cluster ID
3396 * that would lead to PID 0.
3398 assert(cluster
->cluster_id
!= UINT32_MAX
);
3399 process
->pid
= cluster
->cluster_id
+ 1;
3400 process
->attached
= false;
3401 process
->target_xml
[0] = '\0';
3406 return object_child_foreach(child
, find_cpu_clusters
, opaque
);
3409 static int pid_order(const void *a
, const void *b
)
3411 GDBProcess
*pa
= (GDBProcess
*) a
;
3412 GDBProcess
*pb
= (GDBProcess
*) b
;
3414 if (pa
->pid
< pb
->pid
) {
3416 } else if (pa
->pid
> pb
->pid
) {
3423 static void create_processes(GDBState
*s
)
3425 object_child_foreach(object_get_root(), find_cpu_clusters
, s
);
3427 if (gdbserver_state
.processes
) {
3429 qsort(gdbserver_state
.processes
, gdbserver_state
.process_num
, sizeof(gdbserver_state
.processes
[0]), pid_order
);
3432 create_default_process(s
);
3435 int gdbserver_start(const char *device
)
3437 trace_gdbstub_op_start(device
);
3439 char gdbstub_device_name
[128];
3440 Chardev
*chr
= NULL
;
3444 error_report("gdbstub: meaningless to attach gdb to a "
3445 "machine without any CPU.");
3449 if (!gdb_supports_guest_debug()) {
3450 error_report("gdbstub: current accelerator doesn't support guest debugging");
3456 if (strcmp(device
, "none") != 0) {
3457 if (strstart(device
, "tcp:", NULL
)) {
3458 /* enforce required TCP attributes */
3459 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
3460 "%s,wait=off,nodelay=on,server=on", device
);
3461 device
= gdbstub_device_name
;
3464 else if (strcmp(device
, "stdio") == 0) {
3465 struct sigaction act
;
3467 memset(&act
, 0, sizeof(act
));
3468 act
.sa_handler
= gdb_sigterm_handler
;
3469 sigaction(SIGINT
, &act
, NULL
);
3473 * FIXME: it's a bit weird to allow using a mux chardev here
3474 * and implicitly setup a monitor. We may want to break this.
3476 chr
= qemu_chr_new_noreplay("gdb", device
, true, NULL
);
3481 if (!gdbserver_state
.init
) {
3482 init_gdbserver_state();
3484 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
3486 /* Initialize a monitor terminal for gdb */
3487 mon_chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_GDB
,
3488 NULL
, NULL
, &error_abort
);
3489 monitor_init_hmp(mon_chr
, false, &error_abort
);
3491 qemu_chr_fe_deinit(&gdbserver_state
.chr
, true);
3492 mon_chr
= gdbserver_state
.mon_chr
;
3493 reset_gdbserver_state();
3496 create_processes(&gdbserver_state
);
3499 qemu_chr_fe_init(&gdbserver_state
.chr
, chr
, &error_abort
);
3500 qemu_chr_fe_set_handlers(&gdbserver_state
.chr
, gdb_chr_can_receive
,
3501 gdb_chr_receive
, gdb_chr_event
,
3502 NULL
, &gdbserver_state
, NULL
, true);
3504 gdbserver_state
.state
= chr
? RS_IDLE
: RS_INACTIVE
;
3505 gdbserver_state
.mon_chr
= mon_chr
;
3506 gdbserver_state
.current_syscall_cb
= NULL
;
3511 static void register_types(void)
3513 type_register_static(&char_gdb_type_info
);
3516 type_init(register_types
);