4 * This implements a subset of the remote protocol as described in:
6 * https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
8 * Copyright (c) 2003-2005 Fabrice Bellard
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 * SPDX-License-Identifier: LGPL-2.0+
26 #include "qemu/osdep.h"
27 #include "qemu-common.h"
28 #include "qapi/error.h"
29 #include "qemu/error-report.h"
30 #include "qemu/ctype.h"
31 #include "qemu/cutils.h"
32 #include "qemu/module.h"
33 #include "trace-root.h"
34 #ifdef CONFIG_USER_ONLY
37 #include "monitor/monitor.h"
38 #include "chardev/char.h"
39 #include "chardev/char-fe.h"
40 #include "sysemu/sysemu.h"
41 #include "exec/gdbstub.h"
42 #include "hw/cpu/cluster.h"
43 #include "hw/boards.h"
46 #define MAX_PACKET_LENGTH 4096
48 #include "qemu/sockets.h"
49 #include "sysemu/hw_accel.h"
50 #include "sysemu/kvm.h"
51 #include "sysemu/runstate.h"
52 #include "hw/semihosting/semihost.h"
53 #include "exec/exec-all.h"
55 #ifdef CONFIG_USER_ONLY
56 #define GDB_ATTACHED "0"
58 #define GDB_ATTACHED "1"
61 #ifndef CONFIG_USER_ONLY
62 static int phy_memory_mode
;
65 static inline int target_memory_rw_debug(CPUState
*cpu
, target_ulong addr
,
66 uint8_t *buf
, int len
, bool is_write
)
70 #ifndef CONFIG_USER_ONLY
71 if (phy_memory_mode
) {
73 cpu_physical_memory_write(addr
, buf
, len
);
75 cpu_physical_memory_read(addr
, buf
, len
);
81 cc
= CPU_GET_CLASS(cpu
);
82 if (cc
->memory_rw_debug
) {
83 return cc
->memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
85 return cpu_memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
88 /* Return the GDB index for a given vCPU state.
90 * For user mode this is simply the thread id. In system mode GDB
91 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
93 static inline int cpu_gdb_index(CPUState
*cpu
)
95 #if defined(CONFIG_USER_ONLY)
96 TaskState
*ts
= (TaskState
*) cpu
->opaque
;
99 return cpu
->cpu_index
+ 1;
109 GDB_SIGNAL_ALRM
= 14,
111 GDB_SIGNAL_XCPU
= 24,
112 GDB_SIGNAL_UNKNOWN
= 143
115 #ifdef CONFIG_USER_ONLY
117 /* Map target signal numbers to GDB protocol signal numbers and vice
118 * versa. For user emulation's currently supported systems, we can
119 * assume most signals are defined.
122 static int gdb_signal_table
[] = {
282 /* In system mode we only need SIGINT and SIGTRAP; other signals
283 are not yet supported. */
290 static int gdb_signal_table
[] = {
300 #ifdef CONFIG_USER_ONLY
301 static int target_signal_to_gdb (int sig
)
304 for (i
= 0; i
< ARRAY_SIZE (gdb_signal_table
); i
++)
305 if (gdb_signal_table
[i
] == sig
)
307 return GDB_SIGNAL_UNKNOWN
;
311 static int gdb_signal_to_target (int sig
)
313 if (sig
< ARRAY_SIZE (gdb_signal_table
))
314 return gdb_signal_table
[sig
];
319 typedef struct GDBRegisterState
{
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
364 GDBProcess
*processes
;
366 char syscall_buf
[256];
367 gdb_syscall_complete_cb current_syscall_cb
;
372 /* By default use no IRQs and no timers while single stepping so as to
373 * make single stepping like an ICE HW step.
375 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
377 static GDBState gdbserver_state
;
379 static void init_gdbserver_state(void)
381 g_assert(!gdbserver_state
.init
);
382 memset(&gdbserver_state
, 0, sizeof(GDBState
));
383 gdbserver_state
.init
= true;
384 gdbserver_state
.str_buf
= g_string_new(NULL
);
385 gdbserver_state
.mem_buf
= g_byte_array_sized_new(MAX_PACKET_LENGTH
);
386 gdbserver_state
.last_packet
= g_byte_array_sized_new(MAX_PACKET_LENGTH
+ 4);
389 #ifndef CONFIG_USER_ONLY
390 static void reset_gdbserver_state(void)
392 g_free(gdbserver_state
.processes
);
393 gdbserver_state
.processes
= NULL
;
394 gdbserver_state
.process_num
= 0;
400 #ifdef CONFIG_USER_ONLY
401 /* XXX: This is not thread safe. Do we care? */
402 static int gdbserver_fd
= -1;
404 static int get_char(void)
410 ret
= qemu_recv(gdbserver_state
.fd
, &ch
, 1, 0);
412 if (errno
== ECONNRESET
)
413 gdbserver_state
.fd
= -1;
416 } else if (ret
== 0) {
417 close(gdbserver_state
.fd
);
418 gdbserver_state
.fd
= -1;
434 /* Decide if either remote gdb syscalls or native file IO should be used. */
435 int use_gdb_syscalls(void)
437 SemihostingTarget target
= semihosting_get_target();
438 if (target
== SEMIHOSTING_TARGET_NATIVE
) {
439 /* -semihosting-config target=native */
441 } else if (target
== SEMIHOSTING_TARGET_GDB
) {
442 /* -semihosting-config target=gdb */
446 /* -semihosting-config target=auto */
447 /* On the first call check if gdb is connected and remember. */
448 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
449 gdb_syscall_mode
= gdbserver_state
.init
?
450 GDB_SYS_ENABLED
: GDB_SYS_DISABLED
;
452 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
455 /* Resume execution. */
456 static inline void gdb_continue(void)
459 #ifdef CONFIG_USER_ONLY
460 gdbserver_state
.running_state
= 1;
461 trace_gdbstub_op_continue();
463 if (!runstate_needs_reset()) {
464 trace_gdbstub_op_continue();
471 * Resume execution, per CPU actions. For user-mode emulation it's
472 * equivalent to gdb_continue.
474 static int gdb_continue_partial(char *newstates
)
478 #ifdef CONFIG_USER_ONLY
480 * This is not exactly accurate, but it's an improvement compared to the
481 * previous situation, where only one CPU would be single-stepped.
484 if (newstates
[cpu
->cpu_index
] == 's') {
485 trace_gdbstub_op_stepping(cpu
->cpu_index
);
486 cpu_single_step(cpu
, sstep_flags
);
489 gdbserver_state
.running_state
= 1;
493 if (!runstate_needs_reset()) {
494 if (vm_prepare_start()) {
499 switch (newstates
[cpu
->cpu_index
]) {
502 break; /* nothing to do here */
504 trace_gdbstub_op_stepping(cpu
->cpu_index
);
505 cpu_single_step(cpu
, sstep_flags
);
510 trace_gdbstub_op_continue_cpu(cpu
->cpu_index
);
521 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, true);
527 static void put_buffer(const uint8_t *buf
, int len
)
529 #ifdef CONFIG_USER_ONLY
533 ret
= send(gdbserver_state
.fd
, buf
, len
, 0);
543 /* XXX this blocks entire thread. Rewrite to use
544 * qemu_chr_fe_write and background I/O callbacks */
545 qemu_chr_fe_write_all(&gdbserver_state
.chr
, buf
, len
);
549 static inline int fromhex(int v
)
551 if (v
>= '0' && v
<= '9')
553 else if (v
>= 'A' && v
<= 'F')
555 else if (v
>= 'a' && v
<= 'f')
561 static inline int tohex(int v
)
569 /* writes 2*len+1 bytes in buf */
570 static void memtohex(GString
*buf
, const uint8_t *mem
, int len
)
573 for(i
= 0; i
< len
; i
++) {
575 g_string_append_c(buf
, tohex(c
>> 4));
576 g_string_append_c(buf
, tohex(c
& 0xf));
578 g_string_append_c(buf
, '\0');
581 static void hextomem(GByteArray
*mem
, const char *buf
, int len
)
585 for(i
= 0; i
< len
; i
++) {
586 guint8 byte
= fromhex(buf
[0]) << 4 | fromhex(buf
[1]);
587 g_byte_array_append(mem
, &byte
, 1);
592 static void hexdump(const char *buf
, int len
,
593 void (*trace_fn
)(size_t ofs
, char const *text
))
595 char line_buffer
[3 * 16 + 4 + 16 + 1];
598 for (i
= 0; i
< len
|| (i
& 0xF); ++i
) {
599 size_t byte_ofs
= i
& 15;
602 memset(line_buffer
, ' ', 3 * 16 + 4 + 16);
603 line_buffer
[3 * 16 + 4 + 16] = 0;
606 size_t col_group
= (i
>> 2) & 3;
607 size_t hex_col
= byte_ofs
* 3 + col_group
;
608 size_t txt_col
= 3 * 16 + 4 + byte_ofs
;
613 line_buffer
[hex_col
+ 0] = tohex((value
>> 4) & 0xF);
614 line_buffer
[hex_col
+ 1] = tohex((value
>> 0) & 0xF);
615 line_buffer
[txt_col
+ 0] = (value
>= ' ' && value
< 127)
621 trace_fn(i
& -16, line_buffer
);
625 /* return -1 if error, 0 if OK */
626 static int put_packet_binary(const char *buf
, int len
, bool dump
)
631 if (dump
&& trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY
)) {
632 hexdump(buf
, len
, trace_gdbstub_io_binaryreply
);
636 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
637 g_byte_array_append(gdbserver_state
.last_packet
,
638 (const uint8_t *) "$", 1);
639 g_byte_array_append(gdbserver_state
.last_packet
,
640 (const uint8_t *) buf
, len
);
642 for(i
= 0; i
< len
; i
++) {
646 footer
[1] = tohex((csum
>> 4) & 0xf);
647 footer
[2] = tohex((csum
) & 0xf);
648 g_byte_array_append(gdbserver_state
.last_packet
, footer
, 3);
650 put_buffer(gdbserver_state
.last_packet
->data
,
651 gdbserver_state
.last_packet
->len
);
653 #ifdef CONFIG_USER_ONLY
666 /* return -1 if error, 0 if OK */
667 static int put_packet(const char *buf
)
669 trace_gdbstub_io_reply(buf
);
671 return put_packet_binary(buf
, strlen(buf
), false);
674 static void put_strbuf(void)
676 put_packet(gdbserver_state
.str_buf
->str
);
679 /* Encode data using the encoding for 'x' packets. */
680 static void memtox(GString
*buf
, const char *mem
, int len
)
687 case '#': case '$': case '*': case '}':
688 g_string_append_c(buf
, '}');
689 g_string_append_c(buf
, c
^ 0x20);
692 g_string_append_c(buf
, c
);
698 static uint32_t gdb_get_cpu_pid(CPUState
*cpu
)
700 /* TODO: In user mode, we should use the task state PID */
701 if (cpu
->cluster_index
== UNASSIGNED_CLUSTER_INDEX
) {
702 /* Return the default process' PID */
703 int index
= gdbserver_state
.process_num
- 1;
704 return gdbserver_state
.processes
[index
].pid
;
706 return cpu
->cluster_index
+ 1;
709 static GDBProcess
*gdb_get_process(uint32_t pid
)
714 /* 0 means any process, we take the first one */
715 return &gdbserver_state
.processes
[0];
718 for (i
= 0; i
< gdbserver_state
.process_num
; i
++) {
719 if (gdbserver_state
.processes
[i
].pid
== pid
) {
720 return &gdbserver_state
.processes
[i
];
727 static GDBProcess
*gdb_get_cpu_process(CPUState
*cpu
)
729 return gdb_get_process(gdb_get_cpu_pid(cpu
));
732 static CPUState
*find_cpu(uint32_t thread_id
)
737 if (cpu_gdb_index(cpu
) == thread_id
) {
745 static CPUState
*get_first_cpu_in_process(GDBProcess
*process
)
750 if (gdb_get_cpu_pid(cpu
) == process
->pid
) {
758 static CPUState
*gdb_next_cpu_in_process(CPUState
*cpu
)
760 uint32_t pid
= gdb_get_cpu_pid(cpu
);
764 if (gdb_get_cpu_pid(cpu
) == pid
) {
774 /* Return the cpu following @cpu, while ignoring unattached processes. */
775 static CPUState
*gdb_next_attached_cpu(CPUState
*cpu
)
780 if (gdb_get_cpu_process(cpu
)->attached
) {
790 /* Return the first attached cpu */
791 static CPUState
*gdb_first_attached_cpu(void)
793 CPUState
*cpu
= first_cpu
;
794 GDBProcess
*process
= gdb_get_cpu_process(cpu
);
796 if (!process
->attached
) {
797 return gdb_next_attached_cpu(cpu
);
803 static CPUState
*gdb_get_cpu(uint32_t pid
, uint32_t tid
)
809 /* 0 means any process/thread, we take the first attached one */
810 return gdb_first_attached_cpu();
811 } else if (pid
&& !tid
) {
812 /* any thread in a specific process */
813 process
= gdb_get_process(pid
);
815 if (process
== NULL
) {
819 if (!process
->attached
) {
823 return get_first_cpu_in_process(process
);
825 /* a specific thread */
832 process
= gdb_get_cpu_process(cpu
);
834 if (pid
&& process
->pid
!= pid
) {
838 if (!process
->attached
) {
846 static const char *get_feature_xml(const char *p
, const char **newp
,
852 CPUState
*cpu
= get_first_cpu_in_process(process
);
853 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
856 while (p
[len
] && p
[len
] != ':')
861 if (strncmp(p
, "target.xml", len
) == 0) {
862 char *buf
= process
->target_xml
;
863 const size_t buf_sz
= sizeof(process
->target_xml
);
865 /* Generate the XML description for this CPU. */
870 "<?xml version=\"1.0\"?>"
871 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
873 if (cc
->gdb_arch_name
) {
874 gchar
*arch
= cc
->gdb_arch_name(cpu
);
875 pstrcat(buf
, buf_sz
, "<architecture>");
876 pstrcat(buf
, buf_sz
, arch
);
877 pstrcat(buf
, buf_sz
, "</architecture>");
880 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
881 pstrcat(buf
, buf_sz
, cc
->gdb_core_xml_file
);
882 pstrcat(buf
, buf_sz
, "\"/>");
883 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
884 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
885 pstrcat(buf
, buf_sz
, r
->xml
);
886 pstrcat(buf
, buf_sz
, "\"/>");
888 pstrcat(buf
, buf_sz
, "</target>");
892 if (cc
->gdb_get_dynamic_xml
) {
893 char *xmlname
= g_strndup(p
, len
);
894 const char *xml
= cc
->gdb_get_dynamic_xml(cpu
, xmlname
);
902 name
= xml_builtin
[i
][0];
903 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
906 return name
? xml_builtin
[i
][1] : NULL
;
909 static int gdb_read_register(CPUState
*cpu
, GByteArray
*buf
, int reg
)
911 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
912 CPUArchState
*env
= cpu
->env_ptr
;
915 if (reg
< cc
->gdb_num_core_regs
) {
916 return cc
->gdb_read_register(cpu
, buf
, reg
);
919 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
920 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
921 return r
->get_reg(env
, buf
, reg
- r
->base_reg
);
927 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
929 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
930 CPUArchState
*env
= cpu
->env_ptr
;
933 if (reg
< cc
->gdb_num_core_regs
) {
934 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
937 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
938 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
939 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
945 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
946 specifies the first register number and these registers are included in
947 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
948 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
951 void gdb_register_coprocessor(CPUState
*cpu
,
952 gdb_get_reg_cb get_reg
, gdb_set_reg_cb set_reg
,
953 int num_regs
, const char *xml
, int g_pos
)
956 GDBRegisterState
**p
;
960 /* Check for duplicates. */
961 if (strcmp((*p
)->xml
, xml
) == 0)
966 s
= g_new0(GDBRegisterState
, 1);
967 s
->base_reg
= cpu
->gdb_num_regs
;
968 s
->num_regs
= num_regs
;
969 s
->get_reg
= get_reg
;
970 s
->set_reg
= set_reg
;
973 /* Add to end of list. */
974 cpu
->gdb_num_regs
+= num_regs
;
977 if (g_pos
!= s
->base_reg
) {
978 error_report("Error: Bad gdb register numbering for '%s', "
979 "expected %d got %d", xml
, g_pos
, s
->base_reg
);
981 cpu
->gdb_num_g_regs
= cpu
->gdb_num_regs
;
986 #ifndef CONFIG_USER_ONLY
987 /* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
988 static inline int xlat_gdb_type(CPUState
*cpu
, int gdbtype
)
990 static const int xlat
[] = {
991 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
992 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
993 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
996 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
997 int cputype
= xlat
[gdbtype
];
999 if (cc
->gdb_stop_before_watchpoint
) {
1000 cputype
|= BP_STOP_BEFORE_ACCESS
;
1006 static int gdb_breakpoint_insert(int type
, target_ulong addr
, target_ulong len
)
1011 if (kvm_enabled()) {
1012 return kvm_insert_breakpoint(gdbserver_state
.c_cpu
, addr
, len
, type
);
1016 case GDB_BREAKPOINT_SW
:
1017 case GDB_BREAKPOINT_HW
:
1019 err
= cpu_breakpoint_insert(cpu
, addr
, BP_GDB
, NULL
);
1025 #ifndef CONFIG_USER_ONLY
1026 case GDB_WATCHPOINT_WRITE
:
1027 case GDB_WATCHPOINT_READ
:
1028 case GDB_WATCHPOINT_ACCESS
:
1030 err
= cpu_watchpoint_insert(cpu
, addr
, len
,
1031 xlat_gdb_type(cpu
, type
), NULL
);
1043 static int gdb_breakpoint_remove(int type
, target_ulong addr
, target_ulong len
)
1048 if (kvm_enabled()) {
1049 return kvm_remove_breakpoint(gdbserver_state
.c_cpu
, addr
, len
, type
);
1053 case GDB_BREAKPOINT_SW
:
1054 case GDB_BREAKPOINT_HW
:
1056 err
= cpu_breakpoint_remove(cpu
, addr
, BP_GDB
);
1062 #ifndef CONFIG_USER_ONLY
1063 case GDB_WATCHPOINT_WRITE
:
1064 case GDB_WATCHPOINT_READ
:
1065 case GDB_WATCHPOINT_ACCESS
:
1067 err
= cpu_watchpoint_remove(cpu
, addr
, len
,
1068 xlat_gdb_type(cpu
, type
));
1079 static inline void gdb_cpu_breakpoint_remove_all(CPUState
*cpu
)
1081 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
1082 #ifndef CONFIG_USER_ONLY
1083 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
1087 static void gdb_process_breakpoint_remove_all(GDBProcess
*p
)
1089 CPUState
*cpu
= get_first_cpu_in_process(p
);
1092 gdb_cpu_breakpoint_remove_all(cpu
);
1093 cpu
= gdb_next_cpu_in_process(cpu
);
1097 static void gdb_breakpoint_remove_all(void)
1101 if (kvm_enabled()) {
1102 kvm_remove_all_breakpoints(gdbserver_state
.c_cpu
);
1107 gdb_cpu_breakpoint_remove_all(cpu
);
1111 static void gdb_set_cpu_pc(target_ulong pc
)
1113 CPUState
*cpu
= gdbserver_state
.c_cpu
;
1115 cpu_synchronize_state(cpu
);
1116 cpu_set_pc(cpu
, pc
);
1119 static void gdb_append_thread_id(CPUState
*cpu
, GString
*buf
)
1121 if (gdbserver_state
.multiprocess
) {
1122 g_string_append_printf(buf
, "p%02x.%02x",
1123 gdb_get_cpu_pid(cpu
), cpu_gdb_index(cpu
));
1125 g_string_append_printf(buf
, "%02x", cpu_gdb_index(cpu
));
1129 typedef enum GDBThreadIdKind
{
1131 GDB_ALL_THREADS
, /* One process, all threads */
1136 static GDBThreadIdKind
read_thread_id(const char *buf
, const char **end_buf
,
1137 uint32_t *pid
, uint32_t *tid
)
1144 ret
= qemu_strtoul(buf
, &buf
, 16, &p
);
1147 return GDB_READ_THREAD_ERR
;
1156 ret
= qemu_strtoul(buf
, &buf
, 16, &t
);
1159 return GDB_READ_THREAD_ERR
;
1165 return GDB_ALL_PROCESSES
;
1173 return GDB_ALL_THREADS
;
1180 return GDB_ONE_THREAD
;
1184 * gdb_handle_vcont - Parses and handles a vCont packet.
1185 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1186 * a format error, 0 on success.
1188 static int gdb_handle_vcont(const char *p
)
1190 int res
, signal
= 0;
1195 GDBProcess
*process
;
1197 GDBThreadIdKind kind
;
1198 #ifdef CONFIG_USER_ONLY
1199 int max_cpus
= 1; /* global variable max_cpus exists only in system mode */
1202 max_cpus
= max_cpus
<= cpu
->cpu_index
? cpu
->cpu_index
+ 1 : max_cpus
;
1205 MachineState
*ms
= MACHINE(qdev_get_machine());
1206 unsigned int max_cpus
= ms
->smp
.max_cpus
;
1208 /* uninitialised CPUs stay 0 */
1209 newstates
= g_new0(char, max_cpus
);
1211 /* mark valid CPUs with 1 */
1213 newstates
[cpu
->cpu_index
] = 1;
1217 * res keeps track of what error we are returning, with -ENOTSUP meaning
1218 * that the command is unknown or unsupported, thus returning an empty
1219 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1220 * or incorrect parameters passed.
1230 if (cur_action
== 'C' || cur_action
== 'S') {
1231 cur_action
= qemu_tolower(cur_action
);
1232 res
= qemu_strtoul(p
+ 1, &p
, 16, &tmp
);
1236 signal
= gdb_signal_to_target(tmp
);
1237 } else if (cur_action
!= 'c' && cur_action
!= 's') {
1238 /* unknown/invalid/unsupported command */
1243 if (*p
== '\0' || *p
== ';') {
1245 * No thread specifier, action is on "all threads". The
1246 * specification is unclear regarding the process to act on. We
1247 * choose all processes.
1249 kind
= GDB_ALL_PROCESSES
;
1250 } else if (*p
++ == ':') {
1251 kind
= read_thread_id(p
, &p
, &pid
, &tid
);
1258 case GDB_READ_THREAD_ERR
:
1262 case GDB_ALL_PROCESSES
:
1263 cpu
= gdb_first_attached_cpu();
1265 if (newstates
[cpu
->cpu_index
] == 1) {
1266 newstates
[cpu
->cpu_index
] = cur_action
;
1269 cpu
= gdb_next_attached_cpu(cpu
);
1273 case GDB_ALL_THREADS
:
1274 process
= gdb_get_process(pid
);
1276 if (!process
->attached
) {
1281 cpu
= get_first_cpu_in_process(process
);
1283 if (newstates
[cpu
->cpu_index
] == 1) {
1284 newstates
[cpu
->cpu_index
] = cur_action
;
1287 cpu
= gdb_next_cpu_in_process(cpu
);
1291 case GDB_ONE_THREAD
:
1292 cpu
= gdb_get_cpu(pid
, tid
);
1294 /* invalid CPU/thread specified */
1300 /* only use if no previous match occourred */
1301 if (newstates
[cpu
->cpu_index
] == 1) {
1302 newstates
[cpu
->cpu_index
] = cur_action
;
1307 gdbserver_state
.signal
= signal
;
1308 gdb_continue_partial(newstates
);
1316 typedef union GdbCmdVariant
{
1319 unsigned long val_ul
;
1320 unsigned long long val_ull
;
1322 GDBThreadIdKind kind
;
1328 static const char *cmd_next_param(const char *param
, const char delimiter
)
1330 static const char all_delimiters
[] = ",;:=";
1331 char curr_delimiters
[2] = {0};
1332 const char *delimiters
;
1334 if (delimiter
== '?') {
1335 delimiters
= all_delimiters
;
1336 } else if (delimiter
== '0') {
1337 return strchr(param
, '\0');
1338 } else if (delimiter
== '.' && *param
) {
1341 curr_delimiters
[0] = delimiter
;
1342 delimiters
= curr_delimiters
;
1345 param
+= strcspn(param
, delimiters
);
1352 static int cmd_parse_params(const char *data
, const char *schema
,
1353 GdbCmdVariant
*params
, int *num_params
)
1356 const char *curr_schema
, *curr_data
;
1364 curr_schema
= schema
;
1367 while (curr_schema
[0] && curr_schema
[1] && *curr_data
) {
1368 switch (curr_schema
[0]) {
1370 if (qemu_strtoul(curr_data
, &curr_data
, 16,
1371 ¶ms
[curr_param
].val_ul
)) {
1375 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1378 if (qemu_strtou64(curr_data
, &curr_data
, 16,
1379 (uint64_t *)¶ms
[curr_param
].val_ull
)) {
1383 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1386 params
[curr_param
].data
= curr_data
;
1388 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1391 params
[curr_param
].opcode
= *(uint8_t *)curr_data
;
1393 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1396 params
[curr_param
].thread_id
.kind
=
1397 read_thread_id(curr_data
, &curr_data
,
1398 ¶ms
[curr_param
].thread_id
.pid
,
1399 ¶ms
[curr_param
].thread_id
.tid
);
1401 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1404 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1412 *num_params
= curr_param
;
1416 typedef struct GdbCmdContext
{
1417 GdbCmdVariant
*params
;
1421 typedef void (*GdbCmdHandler
)(GdbCmdContext
*gdb_ctx
, void *user_ctx
);
1424 * cmd_startswith -> cmd is compared using startswith
1427 * schema definitions:
1428 * Each schema parameter entry consists of 2 chars,
1429 * the first char represents the parameter type handling
1430 * the second char represents the delimiter for the next parameter
1432 * Currently supported schema types:
1433 * 'l' -> unsigned long (stored in .val_ul)
1434 * 'L' -> unsigned long long (stored in .val_ull)
1435 * 's' -> string (stored in .data)
1436 * 'o' -> single char (stored in .opcode)
1437 * 't' -> thread id (stored in .thread_id)
1438 * '?' -> skip according to delimiter
1440 * Currently supported delimiters:
1441 * '?' -> Stop at any delimiter (",;:=\0")
1442 * '0' -> Stop at "\0"
1443 * '.' -> Skip 1 char unless reached "\0"
1444 * Any other value is treated as the delimiter value itself
1446 typedef struct GdbCmdParseEntry
{
1447 GdbCmdHandler handler
;
1449 bool cmd_startswith
;
1453 static inline int startswith(const char *string
, const char *pattern
)
1455 return !strncmp(string
, pattern
, strlen(pattern
));
1458 static int process_string_cmd(void *user_ctx
, const char *data
,
1459 const GdbCmdParseEntry
*cmds
, int num_cmds
)
1461 int i
, schema_len
, max_num_params
= 0;
1462 GdbCmdContext gdb_ctx
;
1468 for (i
= 0; i
< num_cmds
; i
++) {
1469 const GdbCmdParseEntry
*cmd
= &cmds
[i
];
1470 g_assert(cmd
->handler
&& cmd
->cmd
);
1472 if ((cmd
->cmd_startswith
&& !startswith(data
, cmd
->cmd
)) ||
1473 (!cmd
->cmd_startswith
&& strcmp(cmd
->cmd
, data
))) {
1478 schema_len
= strlen(cmd
->schema
);
1479 if (schema_len
% 2) {
1483 max_num_params
= schema_len
/ 2;
1487 (GdbCmdVariant
*)alloca(sizeof(*gdb_ctx
.params
) * max_num_params
);
1488 memset(gdb_ctx
.params
, 0, sizeof(*gdb_ctx
.params
) * max_num_params
);
1490 if (cmd_parse_params(&data
[strlen(cmd
->cmd
)], cmd
->schema
,
1491 gdb_ctx
.params
, &gdb_ctx
.num_params
)) {
1495 cmd
->handler(&gdb_ctx
, user_ctx
);
1502 static void run_cmd_parser(const char *data
, const GdbCmdParseEntry
*cmd
)
1508 g_string_set_size(gdbserver_state
.str_buf
, 0);
1509 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
1511 /* In case there was an error during the command parsing we must
1512 * send a NULL packet to indicate the command is not supported */
1513 if (process_string_cmd(NULL
, data
, cmd
, 1)) {
1518 static void handle_detach(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1520 GDBProcess
*process
;
1523 if (gdbserver_state
.multiprocess
) {
1524 if (!gdb_ctx
->num_params
) {
1529 pid
= gdb_ctx
->params
[0].val_ul
;
1532 process
= gdb_get_process(pid
);
1533 gdb_process_breakpoint_remove_all(process
);
1534 process
->attached
= false;
1536 if (pid
== gdb_get_cpu_pid(gdbserver_state
.c_cpu
)) {
1537 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
1540 if (pid
== gdb_get_cpu_pid(gdbserver_state
.g_cpu
)) {
1541 gdbserver_state
.g_cpu
= gdb_first_attached_cpu();
1544 if (!gdbserver_state
.c_cpu
) {
1545 /* No more process attached */
1546 gdb_syscall_mode
= GDB_SYS_DISABLED
;
1552 static void handle_thread_alive(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1556 if (!gdb_ctx
->num_params
) {
1561 if (gdb_ctx
->params
[0].thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1566 cpu
= gdb_get_cpu(gdb_ctx
->params
[0].thread_id
.pid
,
1567 gdb_ctx
->params
[0].thread_id
.tid
);
1576 static void handle_continue(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1578 if (gdb_ctx
->num_params
) {
1579 gdb_set_cpu_pc(gdb_ctx
->params
[0].val_ull
);
1582 gdbserver_state
.signal
= 0;
1586 static void handle_cont_with_sig(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1588 unsigned long signal
= 0;
1591 * Note: C sig;[addr] is currently unsupported and we simply
1592 * omit the addr parameter
1594 if (gdb_ctx
->num_params
) {
1595 signal
= gdb_ctx
->params
[0].val_ul
;
1598 gdbserver_state
.signal
= gdb_signal_to_target(signal
);
1599 if (gdbserver_state
.signal
== -1) {
1600 gdbserver_state
.signal
= 0;
1605 static void handle_set_thread(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1609 if (gdb_ctx
->num_params
!= 2) {
1614 if (gdb_ctx
->params
[1].thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1619 if (gdb_ctx
->params
[1].thread_id
.kind
!= GDB_ONE_THREAD
) {
1624 cpu
= gdb_get_cpu(gdb_ctx
->params
[1].thread_id
.pid
,
1625 gdb_ctx
->params
[1].thread_id
.tid
);
1632 * Note: This command is deprecated and modern gdb's will be using the
1633 * vCont command instead.
1635 switch (gdb_ctx
->params
[0].opcode
) {
1637 gdbserver_state
.c_cpu
= cpu
;
1641 gdbserver_state
.g_cpu
= cpu
;
1650 static void handle_insert_bp(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1654 if (gdb_ctx
->num_params
!= 3) {
1659 res
= gdb_breakpoint_insert(gdb_ctx
->params
[0].val_ul
,
1660 gdb_ctx
->params
[1].val_ull
,
1661 gdb_ctx
->params
[2].val_ull
);
1665 } else if (res
== -ENOSYS
) {
1673 static void handle_remove_bp(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1677 if (gdb_ctx
->num_params
!= 3) {
1682 res
= gdb_breakpoint_remove(gdb_ctx
->params
[0].val_ul
,
1683 gdb_ctx
->params
[1].val_ull
,
1684 gdb_ctx
->params
[2].val_ull
);
1688 } else if (res
== -ENOSYS
) {
1697 * handle_set/get_reg
1699 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1700 * This works, but can be very slow. Anything new enough to understand
1701 * XML also knows how to use this properly. However to use this we
1702 * need to define a local XML file as well as be talking to a
1703 * reasonably modern gdb. Responding with an empty packet will cause
1704 * the remote gdb to fallback to older methods.
1707 static void handle_set_reg(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1716 if (gdb_ctx
->num_params
!= 2) {
1721 reg_size
= strlen(gdb_ctx
->params
[1].data
) / 2;
1722 hextomem(gdbserver_state
.mem_buf
, gdb_ctx
->params
[1].data
, reg_size
);
1723 gdb_write_register(gdbserver_state
.g_cpu
, gdbserver_state
.mem_buf
->data
,
1724 gdb_ctx
->params
[0].val_ull
);
1728 static void handle_get_reg(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1737 if (!gdb_ctx
->num_params
) {
1742 reg_size
= gdb_read_register(gdbserver_state
.g_cpu
,
1743 gdbserver_state
.mem_buf
,
1744 gdb_ctx
->params
[0].val_ull
);
1749 g_byte_array_set_size(gdbserver_state
.mem_buf
, reg_size
);
1752 memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
, reg_size
);
1756 static void handle_write_mem(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1758 if (gdb_ctx
->num_params
!= 3) {
1763 /* hextomem() reads 2*len bytes */
1764 if (gdb_ctx
->params
[1].val_ull
> strlen(gdb_ctx
->params
[2].data
) / 2) {
1769 hextomem(gdbserver_state
.mem_buf
, gdb_ctx
->params
[2].data
,
1770 gdb_ctx
->params
[1].val_ull
);
1771 if (target_memory_rw_debug(gdbserver_state
.g_cpu
, gdb_ctx
->params
[0].val_ull
,
1772 gdbserver_state
.mem_buf
->data
,
1773 gdbserver_state
.mem_buf
->len
, true)) {
1781 static void handle_read_mem(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1783 if (gdb_ctx
->num_params
!= 2) {
1788 /* memtohex() doubles the required space */
1789 if (gdb_ctx
->params
[1].val_ull
> MAX_PACKET_LENGTH
/ 2) {
1794 g_byte_array_set_size(gdbserver_state
.mem_buf
, gdb_ctx
->params
[1].val_ull
);
1796 if (target_memory_rw_debug(gdbserver_state
.g_cpu
, gdb_ctx
->params
[0].val_ull
,
1797 gdbserver_state
.mem_buf
->data
,
1798 gdbserver_state
.mem_buf
->len
, false)) {
1803 memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
,
1804 gdbserver_state
.mem_buf
->len
);
1808 static void handle_write_all_regs(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1810 target_ulong addr
, len
;
1814 if (!gdb_ctx
->num_params
) {
1818 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1819 len
= strlen(gdb_ctx
->params
[0].data
) / 2;
1820 hextomem(gdbserver_state
.mem_buf
, gdb_ctx
->params
[0].data
, len
);
1821 registers
= gdbserver_state
.mem_buf
->data
;
1822 for (addr
= 0; addr
< gdbserver_state
.g_cpu
->gdb_num_g_regs
&& len
> 0;
1824 reg_size
= gdb_write_register(gdbserver_state
.g_cpu
, registers
, addr
);
1826 registers
+= reg_size
;
1831 static void handle_read_all_regs(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1833 target_ulong addr
, len
;
1835 cpu_synchronize_state(gdbserver_state
.g_cpu
);
1836 g_byte_array_set_size(gdbserver_state
.mem_buf
, 0);
1838 for (addr
= 0; addr
< gdbserver_state
.g_cpu
->gdb_num_g_regs
; addr
++) {
1839 len
+= gdb_read_register(gdbserver_state
.g_cpu
,
1840 gdbserver_state
.mem_buf
,
1843 g_assert(len
== gdbserver_state
.mem_buf
->len
);
1845 memtohex(gdbserver_state
.str_buf
, gdbserver_state
.mem_buf
->data
, len
);
1849 static void handle_file_io(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1851 if (gdb_ctx
->num_params
>= 1 && gdbserver_state
.current_syscall_cb
) {
1852 target_ulong ret
, err
;
1854 ret
= (target_ulong
)gdb_ctx
->params
[0].val_ull
;
1855 if (gdb_ctx
->num_params
>= 2) {
1856 err
= (target_ulong
)gdb_ctx
->params
[1].val_ull
;
1860 gdbserver_state
.current_syscall_cb(gdbserver_state
.c_cpu
, ret
, err
);
1861 gdbserver_state
.current_syscall_cb
= NULL
;
1864 if (gdb_ctx
->num_params
>= 3 && gdb_ctx
->params
[2].opcode
== (uint8_t)'C') {
1872 static void handle_step(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1874 if (gdb_ctx
->num_params
) {
1875 gdb_set_cpu_pc((target_ulong
)gdb_ctx
->params
[0].val_ull
);
1878 cpu_single_step(gdbserver_state
.c_cpu
, sstep_flags
);
1882 static void handle_v_cont_query(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1884 put_packet("vCont;c;C;s;S");
1887 static void handle_v_cont(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1891 if (!gdb_ctx
->num_params
) {
1895 res
= gdb_handle_vcont(gdb_ctx
->params
[0].data
);
1896 if ((res
== -EINVAL
) || (res
== -ERANGE
)) {
1903 static void handle_v_attach(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1905 GDBProcess
*process
;
1908 g_string_assign(gdbserver_state
.str_buf
, "E22");
1909 if (!gdb_ctx
->num_params
) {
1913 process
= gdb_get_process(gdb_ctx
->params
[0].val_ul
);
1918 cpu
= get_first_cpu_in_process(process
);
1923 process
->attached
= true;
1924 gdbserver_state
.g_cpu
= cpu
;
1925 gdbserver_state
.c_cpu
= cpu
;
1927 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
1928 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
1929 g_string_append_c(gdbserver_state
.str_buf
, ';');
1934 static void handle_v_kill(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1936 /* Kill the target */
1938 error_report("QEMU: Terminated via GDBstub");
1942 static GdbCmdParseEntry gdb_v_commands_table
[] = {
1943 /* Order is important if has same prefix */
1945 .handler
= handle_v_cont_query
,
1950 .handler
= handle_v_cont
,
1952 .cmd_startswith
= 1,
1956 .handler
= handle_v_attach
,
1958 .cmd_startswith
= 1,
1962 .handler
= handle_v_kill
,
1968 static void handle_v_commands(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1970 if (!gdb_ctx
->num_params
) {
1974 if (process_string_cmd(NULL
, gdb_ctx
->params
[0].data
,
1975 gdb_v_commands_table
,
1976 ARRAY_SIZE(gdb_v_commands_table
))) {
1981 static void handle_query_qemu_sstepbits(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1983 g_string_printf(gdbserver_state
.str_buf
, "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1984 SSTEP_ENABLE
, SSTEP_NOIRQ
, SSTEP_NOTIMER
);
1988 static void handle_set_qemu_sstep(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1990 if (!gdb_ctx
->num_params
) {
1994 sstep_flags
= gdb_ctx
->params
[0].val_ul
;
1998 static void handle_query_qemu_sstep(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2000 g_string_printf(gdbserver_state
.str_buf
, "0x%x", sstep_flags
);
2004 static void handle_query_curr_tid(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2007 GDBProcess
*process
;
2010 * "Current thread" remains vague in the spec, so always return
2011 * the first thread of the current process (gdb returns the
2014 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
2015 cpu
= get_first_cpu_in_process(process
);
2016 g_string_assign(gdbserver_state
.str_buf
, "QC");
2017 gdb_append_thread_id(cpu
, gdbserver_state
.str_buf
);
2021 static void handle_query_threads(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2023 if (!gdbserver_state
.query_cpu
) {
2028 g_string_assign(gdbserver_state
.str_buf
, "m");
2029 gdb_append_thread_id(gdbserver_state
.query_cpu
, gdbserver_state
.str_buf
);
2031 gdbserver_state
.query_cpu
= gdb_next_attached_cpu(gdbserver_state
.query_cpu
);
2034 static void handle_query_first_threads(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2036 gdbserver_state
.query_cpu
= gdb_first_attached_cpu();
2037 handle_query_threads(gdb_ctx
, user_ctx
);
2040 static void handle_query_thread_extra(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2042 g_autoptr(GString
) rs
= g_string_new(NULL
);
2045 if (!gdb_ctx
->num_params
||
2046 gdb_ctx
->params
[0].thread_id
.kind
== GDB_READ_THREAD_ERR
) {
2051 cpu
= gdb_get_cpu(gdb_ctx
->params
[0].thread_id
.pid
,
2052 gdb_ctx
->params
[0].thread_id
.tid
);
2057 cpu_synchronize_state(cpu
);
2059 if (gdbserver_state
.multiprocess
&& (gdbserver_state
.process_num
> 1)) {
2060 /* Print the CPU model and name in multiprocess mode */
2061 ObjectClass
*oc
= object_get_class(OBJECT(cpu
));
2062 const char *cpu_model
= object_class_get_name(oc
);
2063 g_autofree
char *cpu_name
=
2064 object_get_canonical_path_component(OBJECT(cpu
));
2065 g_string_printf(rs
, "%s %s [%s]", cpu_model
, cpu_name
,
2066 cpu
->halted
? "halted " : "running");
2068 g_string_printf(rs
, "CPU#%d [%s]", cpu
->cpu_index
,
2069 cpu
->halted
? "halted " : "running");
2071 trace_gdbstub_op_extra_info(rs
->str
);
2072 memtohex(gdbserver_state
.str_buf
, (uint8_t *)rs
->str
, rs
->len
);
2076 #ifdef CONFIG_USER_ONLY
2077 static void handle_query_offsets(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2081 ts
= gdbserver_state
.c_cpu
->opaque
;
2082 g_string_printf(gdbserver_state
.str_buf
,
2083 "Text=" TARGET_ABI_FMT_lx
2084 ";Data=" TARGET_ABI_FMT_lx
2085 ";Bss=" TARGET_ABI_FMT_lx
,
2086 ts
->info
->code_offset
,
2087 ts
->info
->data_offset
,
2088 ts
->info
->data_offset
);
2092 static void handle_query_rcmd(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2094 const guint8 zero
= 0;
2097 if (!gdb_ctx
->num_params
) {
2102 len
= strlen(gdb_ctx
->params
[0].data
);
2108 g_assert(gdbserver_state
.mem_buf
->len
== 0);
2110 hextomem(gdbserver_state
.mem_buf
, gdb_ctx
->params
[0].data
, len
);
2111 g_byte_array_append(gdbserver_state
.mem_buf
, &zero
, 1);
2112 qemu_chr_be_write(gdbserver_state
.mon_chr
, gdbserver_state
.mem_buf
->data
,
2113 gdbserver_state
.mem_buf
->len
);
2118 static void handle_query_supported(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2122 g_string_printf(gdbserver_state
.str_buf
, "PacketSize=%x", MAX_PACKET_LENGTH
);
2123 cc
= CPU_GET_CLASS(first_cpu
);
2124 if (cc
->gdb_core_xml_file
) {
2125 g_string_append(gdbserver_state
.str_buf
, ";qXfer:features:read+");
2128 if (gdb_ctx
->num_params
&&
2129 strstr(gdb_ctx
->params
[0].data
, "multiprocess+")) {
2130 gdbserver_state
.multiprocess
= true;
2133 g_string_append(gdbserver_state
.str_buf
, ";vContSupported+;multiprocess+");
2137 static void handle_query_xfer_features(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2139 GDBProcess
*process
;
2141 unsigned long len
, total_len
, addr
;
2145 if (gdb_ctx
->num_params
< 3) {
2150 process
= gdb_get_cpu_process(gdbserver_state
.g_cpu
);
2151 cc
= CPU_GET_CLASS(gdbserver_state
.g_cpu
);
2152 if (!cc
->gdb_core_xml_file
) {
2158 p
= gdb_ctx
->params
[0].data
;
2159 xml
= get_feature_xml(p
, &p
, process
);
2165 addr
= gdb_ctx
->params
[1].val_ul
;
2166 len
= gdb_ctx
->params
[2].val_ul
;
2167 total_len
= strlen(xml
);
2168 if (addr
> total_len
) {
2173 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
2174 len
= (MAX_PACKET_LENGTH
- 5) / 2;
2177 if (len
< total_len
- addr
) {
2178 g_string_assign(gdbserver_state
.str_buf
, "m");
2179 memtox(gdbserver_state
.str_buf
, xml
+ addr
, len
);
2181 g_string_assign(gdbserver_state
.str_buf
, "l");
2182 memtox(gdbserver_state
.str_buf
, xml
+ addr
, total_len
- addr
);
2185 put_packet_binary(gdbserver_state
.str_buf
->str
,
2186 gdbserver_state
.str_buf
->len
, true);
2189 static void handle_query_attached(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2191 put_packet(GDB_ATTACHED
);
2194 static void handle_query_qemu_supported(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2196 g_string_printf(gdbserver_state
.str_buf
, "sstepbits;sstep");
2197 #ifndef CONFIG_USER_ONLY
2198 g_string_append(gdbserver_state
.str_buf
, ";PhyMemMode");
2203 #ifndef CONFIG_USER_ONLY
2204 static void handle_query_qemu_phy_mem_mode(GdbCmdContext
*gdb_ctx
,
2207 g_string_printf(gdbserver_state
.str_buf
, "%d", phy_memory_mode
);
2211 static void handle_set_qemu_phy_mem_mode(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2213 if (!gdb_ctx
->num_params
) {
2218 if (!gdb_ctx
->params
[0].val_ul
) {
2219 phy_memory_mode
= 0;
2221 phy_memory_mode
= 1;
2227 static GdbCmdParseEntry gdb_gen_query_set_common_table
[] = {
2228 /* Order is important if has same prefix */
2230 .handler
= handle_query_qemu_sstepbits
,
2231 .cmd
= "qemu.sstepbits",
2234 .handler
= handle_query_qemu_sstep
,
2235 .cmd
= "qemu.sstep",
2238 .handler
= handle_set_qemu_sstep
,
2239 .cmd
= "qemu.sstep=",
2240 .cmd_startswith
= 1,
2245 static GdbCmdParseEntry gdb_gen_query_table
[] = {
2247 .handler
= handle_query_curr_tid
,
2251 .handler
= handle_query_threads
,
2252 .cmd
= "sThreadInfo",
2255 .handler
= handle_query_first_threads
,
2256 .cmd
= "fThreadInfo",
2259 .handler
= handle_query_thread_extra
,
2260 .cmd
= "ThreadExtraInfo,",
2261 .cmd_startswith
= 1,
2264 #ifdef CONFIG_USER_ONLY
2266 .handler
= handle_query_offsets
,
2271 .handler
= handle_query_rcmd
,
2273 .cmd_startswith
= 1,
2278 .handler
= handle_query_supported
,
2279 .cmd
= "Supported:",
2280 .cmd_startswith
= 1,
2284 .handler
= handle_query_supported
,
2289 .handler
= handle_query_xfer_features
,
2290 .cmd
= "Xfer:features:read:",
2291 .cmd_startswith
= 1,
2295 .handler
= handle_query_attached
,
2300 .handler
= handle_query_attached
,
2304 .handler
= handle_query_qemu_supported
,
2305 .cmd
= "qemu.Supported",
2307 #ifndef CONFIG_USER_ONLY
2309 .handler
= handle_query_qemu_phy_mem_mode
,
2310 .cmd
= "qemu.PhyMemMode",
2315 static GdbCmdParseEntry gdb_gen_set_table
[] = {
2316 /* Order is important if has same prefix */
2318 .handler
= handle_set_qemu_sstep
,
2319 .cmd
= "qemu.sstep:",
2320 .cmd_startswith
= 1,
2323 #ifndef CONFIG_USER_ONLY
2325 .handler
= handle_set_qemu_phy_mem_mode
,
2326 .cmd
= "qemu.PhyMemMode:",
2327 .cmd_startswith
= 1,
2333 static void handle_gen_query(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2335 if (!gdb_ctx
->num_params
) {
2339 if (!process_string_cmd(NULL
, gdb_ctx
->params
[0].data
,
2340 gdb_gen_query_set_common_table
,
2341 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
2345 if (process_string_cmd(NULL
, gdb_ctx
->params
[0].data
,
2346 gdb_gen_query_table
,
2347 ARRAY_SIZE(gdb_gen_query_table
))) {
2352 static void handle_gen_set(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2354 if (!gdb_ctx
->num_params
) {
2358 if (!process_string_cmd(NULL
, gdb_ctx
->params
[0].data
,
2359 gdb_gen_query_set_common_table
,
2360 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
2364 if (process_string_cmd(NULL
, gdb_ctx
->params
[0].data
,
2366 ARRAY_SIZE(gdb_gen_set_table
))) {
2371 static void handle_target_halt(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2373 g_string_printf(gdbserver_state
.str_buf
, "T%02xthread:", GDB_SIGNAL_TRAP
);
2374 gdb_append_thread_id(gdbserver_state
.c_cpu
, gdbserver_state
.str_buf
);
2375 g_string_append_c(gdbserver_state
.str_buf
, ';');
2378 * Remove all the breakpoints when this query is issued,
2379 * because gdb is doing an initial connect and the state
2380 * should be cleaned up.
2382 gdb_breakpoint_remove_all();
2385 static int gdb_handle_packet(const char *line_buf
)
2387 const GdbCmdParseEntry
*cmd_parser
= NULL
;
2389 trace_gdbstub_io_command(line_buf
);
2391 switch (line_buf
[0]) {
2397 static const GdbCmdParseEntry target_halted_cmd_desc
= {
2398 .handler
= handle_target_halt
,
2402 cmd_parser
= &target_halted_cmd_desc
;
2407 static const GdbCmdParseEntry continue_cmd_desc
= {
2408 .handler
= handle_continue
,
2410 .cmd_startswith
= 1,
2413 cmd_parser
= &continue_cmd_desc
;
2418 static const GdbCmdParseEntry cont_with_sig_cmd_desc
= {
2419 .handler
= handle_cont_with_sig
,
2421 .cmd_startswith
= 1,
2424 cmd_parser
= &cont_with_sig_cmd_desc
;
2429 static const GdbCmdParseEntry v_cmd_desc
= {
2430 .handler
= handle_v_commands
,
2432 .cmd_startswith
= 1,
2435 cmd_parser
= &v_cmd_desc
;
2439 /* Kill the target */
2440 error_report("QEMU: Terminated via GDBstub");
2444 static const GdbCmdParseEntry detach_cmd_desc
= {
2445 .handler
= handle_detach
,
2447 .cmd_startswith
= 1,
2450 cmd_parser
= &detach_cmd_desc
;
2455 static const GdbCmdParseEntry step_cmd_desc
= {
2456 .handler
= handle_step
,
2458 .cmd_startswith
= 1,
2461 cmd_parser
= &step_cmd_desc
;
2466 static const GdbCmdParseEntry file_io_cmd_desc
= {
2467 .handler
= handle_file_io
,
2469 .cmd_startswith
= 1,
2472 cmd_parser
= &file_io_cmd_desc
;
2477 static const GdbCmdParseEntry read_all_regs_cmd_desc
= {
2478 .handler
= handle_read_all_regs
,
2482 cmd_parser
= &read_all_regs_cmd_desc
;
2487 static const GdbCmdParseEntry write_all_regs_cmd_desc
= {
2488 .handler
= handle_write_all_regs
,
2490 .cmd_startswith
= 1,
2493 cmd_parser
= &write_all_regs_cmd_desc
;
2498 static const GdbCmdParseEntry read_mem_cmd_desc
= {
2499 .handler
= handle_read_mem
,
2501 .cmd_startswith
= 1,
2504 cmd_parser
= &read_mem_cmd_desc
;
2509 static const GdbCmdParseEntry write_mem_cmd_desc
= {
2510 .handler
= handle_write_mem
,
2512 .cmd_startswith
= 1,
2515 cmd_parser
= &write_mem_cmd_desc
;
2520 static const GdbCmdParseEntry get_reg_cmd_desc
= {
2521 .handler
= handle_get_reg
,
2523 .cmd_startswith
= 1,
2526 cmd_parser
= &get_reg_cmd_desc
;
2531 static const GdbCmdParseEntry set_reg_cmd_desc
= {
2532 .handler
= handle_set_reg
,
2534 .cmd_startswith
= 1,
2537 cmd_parser
= &set_reg_cmd_desc
;
2542 static const GdbCmdParseEntry insert_bp_cmd_desc
= {
2543 .handler
= handle_insert_bp
,
2545 .cmd_startswith
= 1,
2548 cmd_parser
= &insert_bp_cmd_desc
;
2553 static const GdbCmdParseEntry remove_bp_cmd_desc
= {
2554 .handler
= handle_remove_bp
,
2556 .cmd_startswith
= 1,
2559 cmd_parser
= &remove_bp_cmd_desc
;
2564 static const GdbCmdParseEntry set_thread_cmd_desc
= {
2565 .handler
= handle_set_thread
,
2567 .cmd_startswith
= 1,
2570 cmd_parser
= &set_thread_cmd_desc
;
2575 static const GdbCmdParseEntry thread_alive_cmd_desc
= {
2576 .handler
= handle_thread_alive
,
2578 .cmd_startswith
= 1,
2581 cmd_parser
= &thread_alive_cmd_desc
;
2586 static const GdbCmdParseEntry gen_query_cmd_desc
= {
2587 .handler
= handle_gen_query
,
2589 .cmd_startswith
= 1,
2592 cmd_parser
= &gen_query_cmd_desc
;
2597 static const GdbCmdParseEntry gen_set_cmd_desc
= {
2598 .handler
= handle_gen_set
,
2600 .cmd_startswith
= 1,
2603 cmd_parser
= &gen_set_cmd_desc
;
2607 /* put empty packet */
2613 run_cmd_parser(line_buf
, cmd_parser
);
2619 void gdb_set_stop_cpu(CPUState
*cpu
)
2621 GDBProcess
*p
= gdb_get_cpu_process(cpu
);
2625 * Having a stop CPU corresponding to a process that is not attached
2626 * confuses GDB. So we ignore the request.
2631 gdbserver_state
.c_cpu
= cpu
;
2632 gdbserver_state
.g_cpu
= cpu
;
2635 #ifndef CONFIG_USER_ONLY
2636 static void gdb_vm_state_change(void *opaque
, int running
, RunState state
)
2638 CPUState
*cpu
= gdbserver_state
.c_cpu
;
2639 g_autoptr(GString
) buf
= g_string_new(NULL
);
2640 g_autoptr(GString
) tid
= g_string_new(NULL
);
2644 if (running
|| gdbserver_state
.state
== RS_INACTIVE
) {
2647 /* Is there a GDB syscall waiting to be sent? */
2648 if (gdbserver_state
.current_syscall_cb
) {
2649 put_packet(gdbserver_state
.syscall_buf
);
2654 /* No process attached */
2658 gdb_append_thread_id(cpu
, tid
);
2661 case RUN_STATE_DEBUG
:
2662 if (cpu
->watchpoint_hit
) {
2663 switch (cpu
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
2674 trace_gdbstub_hit_watchpoint(type
, cpu_gdb_index(cpu
),
2675 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
2676 g_string_printf(buf
, "T%02xthread:%s;%swatch:" TARGET_FMT_lx
";",
2677 GDB_SIGNAL_TRAP
, tid
->str
, type
,
2678 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
2679 cpu
->watchpoint_hit
= NULL
;
2682 trace_gdbstub_hit_break();
2685 ret
= GDB_SIGNAL_TRAP
;
2687 case RUN_STATE_PAUSED
:
2688 trace_gdbstub_hit_paused();
2689 ret
= GDB_SIGNAL_INT
;
2691 case RUN_STATE_SHUTDOWN
:
2692 trace_gdbstub_hit_shutdown();
2693 ret
= GDB_SIGNAL_QUIT
;
2695 case RUN_STATE_IO_ERROR
:
2696 trace_gdbstub_hit_io_error();
2697 ret
= GDB_SIGNAL_IO
;
2699 case RUN_STATE_WATCHDOG
:
2700 trace_gdbstub_hit_watchdog();
2701 ret
= GDB_SIGNAL_ALRM
;
2703 case RUN_STATE_INTERNAL_ERROR
:
2704 trace_gdbstub_hit_internal_error();
2705 ret
= GDB_SIGNAL_ABRT
;
2707 case RUN_STATE_SAVE_VM
:
2708 case RUN_STATE_RESTORE_VM
:
2710 case RUN_STATE_FINISH_MIGRATE
:
2711 ret
= GDB_SIGNAL_XCPU
;
2714 trace_gdbstub_hit_unknown(state
);
2715 ret
= GDB_SIGNAL_UNKNOWN
;
2718 gdb_set_stop_cpu(cpu
);
2719 g_string_printf(buf
, "T%02xthread:%s;", ret
, tid
->str
);
2722 put_packet(buf
->str
);
2724 /* disable single step if it was enabled */
2725 cpu_single_step(cpu
, 0);
2729 /* Send a gdb syscall request.
2730 This accepts limited printf-style format specifiers, specifically:
2731 %x - target_ulong argument printed in hex.
2732 %lx - 64-bit argument printed in hex.
2733 %s - string pointer (target_ulong) and length (int) pair. */
2734 void gdb_do_syscallv(gdb_syscall_complete_cb cb
, const char *fmt
, va_list va
)
2741 if (!gdbserver_state
.init
) {
2745 gdbserver_state
.current_syscall_cb
= cb
;
2746 #ifndef CONFIG_USER_ONLY
2747 vm_stop(RUN_STATE_DEBUG
);
2749 p
= &gdbserver_state
.syscall_buf
[0];
2750 p_end
= &gdbserver_state
.syscall_buf
[sizeof(gdbserver_state
.syscall_buf
)];
2757 addr
= va_arg(va
, target_ulong
);
2758 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
, addr
);
2761 if (*(fmt
++) != 'x')
2763 i64
= va_arg(va
, uint64_t);
2764 p
+= snprintf(p
, p_end
- p
, "%" PRIx64
, i64
);
2767 addr
= va_arg(va
, target_ulong
);
2768 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
"/%x",
2769 addr
, va_arg(va
, int));
2773 error_report("gdbstub: Bad syscall format string '%s'",
2782 #ifdef CONFIG_USER_ONLY
2783 put_packet(gdbserver_state
.syscall_buf
);
2784 /* Return control to gdb for it to process the syscall request.
2785 * Since the protocol requires that gdb hands control back to us
2786 * using a "here are the results" F packet, we don't need to check
2787 * gdb_handlesig's return value (which is the signal to deliver if
2788 * execution was resumed via a continue packet).
2790 gdb_handlesig(gdbserver_state
.c_cpu
, 0);
2792 /* In this case wait to send the syscall packet until notification that
2793 the CPU has stopped. This must be done because if the packet is sent
2794 now the reply from the syscall request could be received while the CPU
2795 is still in the running state, which can cause packets to be dropped
2796 and state transition 'T' packets to be sent while the syscall is still
2798 qemu_cpu_kick(gdbserver_state
.c_cpu
);
2802 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
2807 gdb_do_syscallv(cb
, fmt
, va
);
2811 static void gdb_read_byte(uint8_t ch
)
2815 #ifndef CONFIG_USER_ONLY
2816 if (gdbserver_state
.last_packet
->len
) {
2817 /* Waiting for a response to the last packet. If we see the start
2818 of a new command then abandon the previous response. */
2820 trace_gdbstub_err_got_nack();
2821 put_buffer(gdbserver_state
.last_packet
->data
,
2822 gdbserver_state
.last_packet
->len
);
2823 } else if (ch
== '+') {
2824 trace_gdbstub_io_got_ack();
2826 trace_gdbstub_io_got_unexpected(ch
);
2829 if (ch
== '+' || ch
== '$') {
2830 g_byte_array_set_size(gdbserver_state
.last_packet
, 0);
2835 if (runstate_is_running()) {
2836 /* when the CPU is running, we cannot do anything except stop
2837 it when receiving a char */
2838 vm_stop(RUN_STATE_PAUSED
);
2842 switch(gdbserver_state
.state
) {
2845 /* start of command packet */
2846 gdbserver_state
.line_buf_index
= 0;
2847 gdbserver_state
.line_sum
= 0;
2848 gdbserver_state
.state
= RS_GETLINE
;
2850 trace_gdbstub_err_garbage(ch
);
2855 /* start escape sequence */
2856 gdbserver_state
.state
= RS_GETLINE_ESC
;
2857 gdbserver_state
.line_sum
+= ch
;
2858 } else if (ch
== '*') {
2859 /* start run length encoding sequence */
2860 gdbserver_state
.state
= RS_GETLINE_RLE
;
2861 gdbserver_state
.line_sum
+= ch
;
2862 } else if (ch
== '#') {
2863 /* end of command, start of checksum*/
2864 gdbserver_state
.state
= RS_CHKSUM1
;
2865 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2866 trace_gdbstub_err_overrun();
2867 gdbserver_state
.state
= RS_IDLE
;
2869 /* unescaped command character */
2870 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
;
2871 gdbserver_state
.line_sum
+= ch
;
2874 case RS_GETLINE_ESC
:
2876 /* unexpected end of command in escape sequence */
2877 gdbserver_state
.state
= RS_CHKSUM1
;
2878 } else if (gdbserver_state
.line_buf_index
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2879 /* command buffer overrun */
2880 trace_gdbstub_err_overrun();
2881 gdbserver_state
.state
= RS_IDLE
;
2883 /* parse escaped character and leave escape state */
2884 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
++] = ch
^ 0x20;
2885 gdbserver_state
.line_sum
+= ch
;
2886 gdbserver_state
.state
= RS_GETLINE
;
2889 case RS_GETLINE_RLE
:
2891 * Run-length encoding is explained in "Debugging with GDB /
2892 * Appendix E GDB Remote Serial Protocol / Overview".
2894 if (ch
< ' ' || ch
== '#' || ch
== '$' || ch
> 126) {
2895 /* invalid RLE count encoding */
2896 trace_gdbstub_err_invalid_repeat(ch
);
2897 gdbserver_state
.state
= RS_GETLINE
;
2899 /* decode repeat length */
2900 int repeat
= ch
- ' ' + 3;
2901 if (gdbserver_state
.line_buf_index
+ repeat
>= sizeof(gdbserver_state
.line_buf
) - 1) {
2902 /* that many repeats would overrun the command buffer */
2903 trace_gdbstub_err_overrun();
2904 gdbserver_state
.state
= RS_IDLE
;
2905 } else if (gdbserver_state
.line_buf_index
< 1) {
2906 /* got a repeat but we have nothing to repeat */
2907 trace_gdbstub_err_invalid_rle();
2908 gdbserver_state
.state
= RS_GETLINE
;
2910 /* repeat the last character */
2911 memset(gdbserver_state
.line_buf
+ gdbserver_state
.line_buf_index
,
2912 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
- 1], repeat
);
2913 gdbserver_state
.line_buf_index
+= repeat
;
2914 gdbserver_state
.line_sum
+= ch
;
2915 gdbserver_state
.state
= RS_GETLINE
;
2920 /* get high hex digit of checksum */
2921 if (!isxdigit(ch
)) {
2922 trace_gdbstub_err_checksum_invalid(ch
);
2923 gdbserver_state
.state
= RS_GETLINE
;
2926 gdbserver_state
.line_buf
[gdbserver_state
.line_buf_index
] = '\0';
2927 gdbserver_state
.line_csum
= fromhex(ch
) << 4;
2928 gdbserver_state
.state
= RS_CHKSUM2
;
2931 /* get low hex digit of checksum */
2932 if (!isxdigit(ch
)) {
2933 trace_gdbstub_err_checksum_invalid(ch
);
2934 gdbserver_state
.state
= RS_GETLINE
;
2937 gdbserver_state
.line_csum
|= fromhex(ch
);
2939 if (gdbserver_state
.line_csum
!= (gdbserver_state
.line_sum
& 0xff)) {
2940 trace_gdbstub_err_checksum_incorrect(gdbserver_state
.line_sum
, gdbserver_state
.line_csum
);
2941 /* send NAK reply */
2943 put_buffer(&reply
, 1);
2944 gdbserver_state
.state
= RS_IDLE
;
2946 /* send ACK reply */
2948 put_buffer(&reply
, 1);
2949 gdbserver_state
.state
= gdb_handle_packet(gdbserver_state
.line_buf
);
2958 /* Tell the remote gdb that the process has exited. */
2959 void gdb_exit(CPUArchState
*env
, int code
)
2963 if (!gdbserver_state
.init
) {
2966 #ifdef CONFIG_USER_ONLY
2967 if (gdbserver_fd
< 0 || gdbserver_state
.fd
< 0) {
2972 trace_gdbstub_op_exiting((uint8_t)code
);
2974 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
2977 #ifndef CONFIG_USER_ONLY
2978 qemu_chr_fe_deinit(&gdbserver_state
.chr
, true);
2983 * Create the process that will contain all the "orphan" CPUs (that are not
2984 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2985 * be attachable and thus will be invisible to the user.
2987 static void create_default_process(GDBState
*s
)
2989 GDBProcess
*process
;
2992 if (gdbserver_state
.process_num
) {
2993 max_pid
= s
->processes
[s
->process_num
- 1].pid
;
2996 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2997 process
= &s
->processes
[s
->process_num
- 1];
2999 /* We need an available PID slot for this process */
3000 assert(max_pid
< UINT32_MAX
);
3002 process
->pid
= max_pid
+ 1;
3003 process
->attached
= false;
3004 process
->target_xml
[0] = '\0';
3007 #ifdef CONFIG_USER_ONLY
3009 gdb_handlesig(CPUState
*cpu
, int sig
)
3014 if (gdbserver_fd
< 0 || gdbserver_state
.fd
< 0) {
3018 /* disable single step if it was enabled */
3019 cpu_single_step(cpu
, 0);
3023 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb(sig
));
3026 /* put_packet() might have detected that the peer terminated the
3028 if (gdbserver_state
.fd
< 0) {
3033 gdbserver_state
.state
= RS_IDLE
;
3034 gdbserver_state
.running_state
= 0;
3035 while (gdbserver_state
.running_state
== 0) {
3036 n
= read(gdbserver_state
.fd
, buf
, 256);
3040 for (i
= 0; i
< n
; i
++) {
3041 gdb_read_byte(buf
[i
]);
3044 /* XXX: Connection closed. Should probably wait for another
3045 connection before continuing. */
3047 close(gdbserver_state
.fd
);
3049 gdbserver_state
.fd
= -1;
3053 sig
= gdbserver_state
.signal
;
3054 gdbserver_state
.signal
= 0;
3058 /* Tell the remote gdb that the process has exited due to SIG. */
3059 void gdb_signalled(CPUArchState
*env
, int sig
)
3063 if (gdbserver_fd
< 0 || gdbserver_state
.fd
< 0) {
3067 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb(sig
));
3071 static bool gdb_accept(void)
3073 struct sockaddr_in sockaddr
;
3078 len
= sizeof(sockaddr
);
3079 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
3080 if (fd
< 0 && errno
!= EINTR
) {
3083 } else if (fd
>= 0) {
3084 qemu_set_cloexec(fd
);
3089 /* set short latency */
3090 if (socket_set_nodelay(fd
)) {
3091 perror("setsockopt");
3096 init_gdbserver_state();
3097 create_default_process(&gdbserver_state
);
3098 gdbserver_state
.processes
[0].attached
= true;
3099 gdbserver_state
.c_cpu
= gdb_first_attached_cpu();
3100 gdbserver_state
.g_cpu
= gdbserver_state
.c_cpu
;
3101 gdbserver_state
.fd
= fd
;
3102 gdb_has_xml
= false;
3106 static int gdbserver_open(int port
)
3108 struct sockaddr_in sockaddr
;
3111 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3116 qemu_set_cloexec(fd
);
3118 socket_set_fast_reuse(fd
);
3120 sockaddr
.sin_family
= AF_INET
;
3121 sockaddr
.sin_port
= htons(port
);
3122 sockaddr
.sin_addr
.s_addr
= 0;
3123 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
3129 ret
= listen(fd
, 1);
3138 int gdbserver_start(int port
)
3140 gdbserver_fd
= gdbserver_open(port
);
3141 if (gdbserver_fd
< 0)
3143 /* accept connections */
3144 if (!gdb_accept()) {
3145 close(gdbserver_fd
);
3152 /* Disable gdb stub for child processes. */
3153 void gdbserver_fork(CPUState
*cpu
)
3155 if (gdbserver_fd
< 0 || gdbserver_state
.fd
< 0) {
3158 close(gdbserver_state
.fd
);
3159 gdbserver_state
.fd
= -1;
3160 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
3161 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
3164 static int gdb_chr_can_receive(void *opaque
)
3166 /* We can handle an arbitrarily large amount of data.
3167 Pick the maximum packet size, which is as good as anything. */
3168 return MAX_PACKET_LENGTH
;
3171 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
3175 for (i
= 0; i
< size
; i
++) {
3176 gdb_read_byte(buf
[i
]);
3180 static void gdb_chr_event(void *opaque
, QEMUChrEvent event
)
3183 GDBState
*s
= (GDBState
*) opaque
;
3186 case CHR_EVENT_OPENED
:
3187 /* Start with first process attached, others detached */
3188 for (i
= 0; i
< s
->process_num
; i
++) {
3189 s
->processes
[i
].attached
= !i
;
3192 s
->c_cpu
= gdb_first_attached_cpu();
3193 s
->g_cpu
= s
->c_cpu
;
3195 vm_stop(RUN_STATE_PAUSED
);
3196 gdb_has_xml
= false;
3203 static int gdb_monitor_write(Chardev
*chr
, const uint8_t *buf
, int len
)
3205 g_autoptr(GString
) hex_buf
= g_string_new("O");
3206 memtohex(hex_buf
, buf
, len
);
3207 put_packet(hex_buf
->str
);
3212 static void gdb_sigterm_handler(int signal
)
3214 if (runstate_is_running()) {
3215 vm_stop(RUN_STATE_PAUSED
);
3220 static void gdb_monitor_open(Chardev
*chr
, ChardevBackend
*backend
,
3221 bool *be_opened
, Error
**errp
)
3226 static void char_gdb_class_init(ObjectClass
*oc
, void *data
)
3228 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
3230 cc
->internal
= true;
3231 cc
->open
= gdb_monitor_open
;
3232 cc
->chr_write
= gdb_monitor_write
;
3235 #define TYPE_CHARDEV_GDB "chardev-gdb"
3237 static const TypeInfo char_gdb_type_info
= {
3238 .name
= TYPE_CHARDEV_GDB
,
3239 .parent
= TYPE_CHARDEV
,
3240 .class_init
= char_gdb_class_init
,
3243 static int find_cpu_clusters(Object
*child
, void *opaque
)
3245 if (object_dynamic_cast(child
, TYPE_CPU_CLUSTER
)) {
3246 GDBState
*s
= (GDBState
*) opaque
;
3247 CPUClusterState
*cluster
= CPU_CLUSTER(child
);
3248 GDBProcess
*process
;
3250 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
3252 process
= &s
->processes
[s
->process_num
- 1];
3255 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
3256 * runtime, we enforce here that the machine does not use a cluster ID
3257 * that would lead to PID 0.
3259 assert(cluster
->cluster_id
!= UINT32_MAX
);
3260 process
->pid
= cluster
->cluster_id
+ 1;
3261 process
->attached
= false;
3262 process
->target_xml
[0] = '\0';
3267 return object_child_foreach(child
, find_cpu_clusters
, opaque
);
3270 static int pid_order(const void *a
, const void *b
)
3272 GDBProcess
*pa
= (GDBProcess
*) a
;
3273 GDBProcess
*pb
= (GDBProcess
*) b
;
3275 if (pa
->pid
< pb
->pid
) {
3277 } else if (pa
->pid
> pb
->pid
) {
3284 static void create_processes(GDBState
*s
)
3286 object_child_foreach(object_get_root(), find_cpu_clusters
, s
);
3288 if (gdbserver_state
.processes
) {
3290 qsort(gdbserver_state
.processes
, gdbserver_state
.process_num
, sizeof(gdbserver_state
.processes
[0]), pid_order
);
3293 create_default_process(s
);
3296 int gdbserver_start(const char *device
)
3298 trace_gdbstub_op_start(device
);
3300 char gdbstub_device_name
[128];
3301 Chardev
*chr
= NULL
;
3305 error_report("gdbstub: meaningless to attach gdb to a "
3306 "machine without any CPU.");
3312 if (strcmp(device
, "none") != 0) {
3313 if (strstart(device
, "tcp:", NULL
)) {
3314 /* enforce required TCP attributes */
3315 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
3316 "%s,nowait,nodelay,server", device
);
3317 device
= gdbstub_device_name
;
3320 else if (strcmp(device
, "stdio") == 0) {
3321 struct sigaction act
;
3323 memset(&act
, 0, sizeof(act
));
3324 act
.sa_handler
= gdb_sigterm_handler
;
3325 sigaction(SIGINT
, &act
, NULL
);
3329 * FIXME: it's a bit weird to allow using a mux chardev here
3330 * and implicitly setup a monitor. We may want to break this.
3332 chr
= qemu_chr_new_noreplay("gdb", device
, true, NULL
);
3337 if (!gdbserver_state
.init
) {
3338 init_gdbserver_state();
3340 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
3342 /* Initialize a monitor terminal for gdb */
3343 mon_chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_GDB
,
3344 NULL
, NULL
, &error_abort
);
3345 monitor_init_hmp(mon_chr
, false, &error_abort
);
3347 qemu_chr_fe_deinit(&gdbserver_state
.chr
, true);
3348 mon_chr
= gdbserver_state
.mon_chr
;
3349 reset_gdbserver_state();
3352 create_processes(&gdbserver_state
);
3355 qemu_chr_fe_init(&gdbserver_state
.chr
, chr
, &error_abort
);
3356 qemu_chr_fe_set_handlers(&gdbserver_state
.chr
, gdb_chr_can_receive
,
3357 gdb_chr_receive
, gdb_chr_event
,
3358 NULL
, &gdbserver_state
, NULL
, true);
3360 gdbserver_state
.state
= chr
? RS_IDLE
: RS_INACTIVE
;
3361 gdbserver_state
.mon_chr
= mon_chr
;
3362 gdbserver_state
.current_syscall_cb
= NULL
;
3367 void gdbserver_cleanup(void)
3369 if (gdbserver_state
.init
) {
3374 static void register_types(void)
3376 type_register_static(&char_gdb_type_info
);
3379 type_init(register_types
);