4 * Copyright (c) 2003-2005 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
22 #include "qapi/error.h"
23 #include "qemu/error-report.h"
24 #include "qemu/ctype.h"
25 #include "qemu/cutils.h"
26 #include "qemu/module.h"
27 #include "trace-root.h"
28 #ifdef CONFIG_USER_ONLY
31 #include "monitor/monitor.h"
32 #include "chardev/char.h"
33 #include "chardev/char-fe.h"
34 #include "sysemu/sysemu.h"
35 #include "exec/gdbstub.h"
36 #include "hw/cpu/cluster.h"
39 #define MAX_PACKET_LENGTH 4096
41 #include "qemu/sockets.h"
42 #include "sysemu/hw_accel.h"
43 #include "sysemu/kvm.h"
44 #include "hw/semihosting/semihost.h"
45 #include "exec/exec-all.h"
47 #ifdef CONFIG_USER_ONLY
48 #define GDB_ATTACHED "0"
50 #define GDB_ATTACHED "1"
53 static inline int target_memory_rw_debug(CPUState
*cpu
, target_ulong addr
,
54 uint8_t *buf
, int len
, bool is_write
)
56 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
58 if (cc
->memory_rw_debug
) {
59 return cc
->memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
61 return cpu_memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
64 /* Return the GDB index for a given vCPU state.
66 * For user mode this is simply the thread id. In system mode GDB
67 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
69 static inline int cpu_gdb_index(CPUState
*cpu
)
71 #if defined(CONFIG_USER_ONLY)
72 TaskState
*ts
= (TaskState
*) cpu
->opaque
;
75 return cpu
->cpu_index
+ 1;
88 GDB_SIGNAL_UNKNOWN
= 143
91 #ifdef CONFIG_USER_ONLY
93 /* Map target signal numbers to GDB protocol signal numbers and vice
94 * versa. For user emulation's currently supported systems, we can
95 * assume most signals are defined.
98 static int gdb_signal_table
[] = {
258 /* In system mode we only need SIGINT and SIGTRAP; other signals
259 are not yet supported. */
266 static int gdb_signal_table
[] = {
276 #ifdef CONFIG_USER_ONLY
277 static int target_signal_to_gdb (int sig
)
280 for (i
= 0; i
< ARRAY_SIZE (gdb_signal_table
); i
++)
281 if (gdb_signal_table
[i
] == sig
)
283 return GDB_SIGNAL_UNKNOWN
;
287 static int gdb_signal_to_target (int sig
)
289 if (sig
< ARRAY_SIZE (gdb_signal_table
))
290 return gdb_signal_table
[sig
];
295 typedef struct GDBRegisterState
{
301 struct GDBRegisterState
*next
;
304 typedef struct GDBProcess
{
308 char target_xml
[1024];
320 typedef struct GDBState
{
321 CPUState
*c_cpu
; /* current CPU for step/continue ops */
322 CPUState
*g_cpu
; /* current CPU for other ops */
323 CPUState
*query_cpu
; /* for q{f|s}ThreadInfo */
324 enum RSState state
; /* parsing state */
325 char line_buf
[MAX_PACKET_LENGTH
];
327 int line_sum
; /* running checksum */
328 int line_csum
; /* checksum at the end of the packet */
329 uint8_t last_packet
[MAX_PACKET_LENGTH
+ 4];
332 #ifdef CONFIG_USER_ONLY
340 GDBProcess
*processes
;
342 char syscall_buf
[256];
343 gdb_syscall_complete_cb current_syscall_cb
;
346 /* By default use no IRQs and no timers while single stepping so as to
347 * make single stepping like an ICE HW step.
349 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
351 static GDBState
*gdbserver_state
;
355 #ifdef CONFIG_USER_ONLY
356 /* XXX: This is not thread safe. Do we care? */
357 static int gdbserver_fd
= -1;
359 static int get_char(GDBState
*s
)
365 ret
= qemu_recv(s
->fd
, &ch
, 1, 0);
367 if (errno
== ECONNRESET
)
371 } else if (ret
== 0) {
389 /* Decide if either remote gdb syscalls or native file IO should be used. */
390 int use_gdb_syscalls(void)
392 SemihostingTarget target
= semihosting_get_target();
393 if (target
== SEMIHOSTING_TARGET_NATIVE
) {
394 /* -semihosting-config target=native */
396 } else if (target
== SEMIHOSTING_TARGET_GDB
) {
397 /* -semihosting-config target=gdb */
401 /* -semihosting-config target=auto */
402 /* On the first call check if gdb is connected and remember. */
403 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
404 gdb_syscall_mode
= (gdbserver_state
? GDB_SYS_ENABLED
407 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
410 /* Resume execution. */
411 static inline void gdb_continue(GDBState
*s
)
414 #ifdef CONFIG_USER_ONLY
415 s
->running_state
= 1;
416 trace_gdbstub_op_continue();
418 if (!runstate_needs_reset()) {
419 trace_gdbstub_op_continue();
426 * Resume execution, per CPU actions. For user-mode emulation it's
427 * equivalent to gdb_continue.
429 static int gdb_continue_partial(GDBState
*s
, char *newstates
)
433 #ifdef CONFIG_USER_ONLY
435 * This is not exactly accurate, but it's an improvement compared to the
436 * previous situation, where only one CPU would be single-stepped.
439 if (newstates
[cpu
->cpu_index
] == 's') {
440 trace_gdbstub_op_stepping(cpu
->cpu_index
);
441 cpu_single_step(cpu
, sstep_flags
);
444 s
->running_state
= 1;
448 if (!runstate_needs_reset()) {
449 if (vm_prepare_start()) {
454 switch (newstates
[cpu
->cpu_index
]) {
457 break; /* nothing to do here */
459 trace_gdbstub_op_stepping(cpu
->cpu_index
);
460 cpu_single_step(cpu
, sstep_flags
);
465 trace_gdbstub_op_continue_cpu(cpu
->cpu_index
);
476 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, true);
482 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
484 #ifdef CONFIG_USER_ONLY
488 ret
= send(s
->fd
, buf
, len
, 0);
498 /* XXX this blocks entire thread. Rewrite to use
499 * qemu_chr_fe_write and background I/O callbacks */
500 qemu_chr_fe_write_all(&s
->chr
, buf
, len
);
504 static inline int fromhex(int v
)
506 if (v
>= '0' && v
<= '9')
508 else if (v
>= 'A' && v
<= 'F')
510 else if (v
>= 'a' && v
<= 'f')
516 static inline int tohex(int v
)
524 /* writes 2*len+1 bytes in buf */
525 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
530 for(i
= 0; i
< len
; i
++) {
532 *q
++ = tohex(c
>> 4);
533 *q
++ = tohex(c
& 0xf);
538 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
542 for(i
= 0; i
< len
; i
++) {
543 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
548 static void hexdump(const char *buf
, int len
,
549 void (*trace_fn
)(size_t ofs
, char const *text
))
551 char line_buffer
[3 * 16 + 4 + 16 + 1];
554 for (i
= 0; i
< len
|| (i
& 0xF); ++i
) {
555 size_t byte_ofs
= i
& 15;
558 memset(line_buffer
, ' ', 3 * 16 + 4 + 16);
559 line_buffer
[3 * 16 + 4 + 16] = 0;
562 size_t col_group
= (i
>> 2) & 3;
563 size_t hex_col
= byte_ofs
* 3 + col_group
;
564 size_t txt_col
= 3 * 16 + 4 + byte_ofs
;
569 line_buffer
[hex_col
+ 0] = tohex((value
>> 4) & 0xF);
570 line_buffer
[hex_col
+ 1] = tohex((value
>> 0) & 0xF);
571 line_buffer
[txt_col
+ 0] = (value
>= ' ' && value
< 127)
577 trace_fn(i
& -16, line_buffer
);
581 /* return -1 if error, 0 if OK */
582 static int put_packet_binary(GDBState
*s
, const char *buf
, int len
, bool dump
)
587 if (dump
&& trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY
)) {
588 hexdump(buf
, len
, trace_gdbstub_io_binaryreply
);
597 for(i
= 0; i
< len
; i
++) {
601 *(p
++) = tohex((csum
>> 4) & 0xf);
602 *(p
++) = tohex((csum
) & 0xf);
604 s
->last_packet_len
= p
- s
->last_packet
;
605 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
607 #ifdef CONFIG_USER_ONLY
620 /* return -1 if error, 0 if OK */
621 static int put_packet(GDBState
*s
, const char *buf
)
623 trace_gdbstub_io_reply(buf
);
625 return put_packet_binary(s
, buf
, strlen(buf
), false);
628 /* Encode data using the encoding for 'x' packets. */
629 static int memtox(char *buf
, const char *mem
, int len
)
637 case '#': case '$': case '*': case '}':
649 static uint32_t gdb_get_cpu_pid(const GDBState
*s
, CPUState
*cpu
)
651 /* TODO: In user mode, we should use the task state PID */
652 if (cpu
->cluster_index
== UNASSIGNED_CLUSTER_INDEX
) {
653 /* Return the default process' PID */
654 return s
->processes
[s
->process_num
- 1].pid
;
656 return cpu
->cluster_index
+ 1;
659 static GDBProcess
*gdb_get_process(const GDBState
*s
, uint32_t pid
)
664 /* 0 means any process, we take the first one */
665 return &s
->processes
[0];
668 for (i
= 0; i
< s
->process_num
; i
++) {
669 if (s
->processes
[i
].pid
== pid
) {
670 return &s
->processes
[i
];
677 static GDBProcess
*gdb_get_cpu_process(const GDBState
*s
, CPUState
*cpu
)
679 return gdb_get_process(s
, gdb_get_cpu_pid(s
, cpu
));
682 static CPUState
*find_cpu(uint32_t thread_id
)
687 if (cpu_gdb_index(cpu
) == thread_id
) {
695 static CPUState
*get_first_cpu_in_process(const GDBState
*s
,
701 if (gdb_get_cpu_pid(s
, cpu
) == process
->pid
) {
709 static CPUState
*gdb_next_cpu_in_process(const GDBState
*s
, CPUState
*cpu
)
711 uint32_t pid
= gdb_get_cpu_pid(s
, cpu
);
715 if (gdb_get_cpu_pid(s
, cpu
) == pid
) {
725 /* Return the cpu following @cpu, while ignoring unattached processes. */
726 static CPUState
*gdb_next_attached_cpu(const GDBState
*s
, CPUState
*cpu
)
731 if (gdb_get_cpu_process(s
, cpu
)->attached
) {
741 /* Return the first attached cpu */
742 static CPUState
*gdb_first_attached_cpu(const GDBState
*s
)
744 CPUState
*cpu
= first_cpu
;
745 GDBProcess
*process
= gdb_get_cpu_process(s
, cpu
);
747 if (!process
->attached
) {
748 return gdb_next_attached_cpu(s
, cpu
);
754 static CPUState
*gdb_get_cpu(const GDBState
*s
, uint32_t pid
, uint32_t tid
)
760 /* 0 means any process/thread, we take the first attached one */
761 return gdb_first_attached_cpu(s
);
762 } else if (pid
&& !tid
) {
763 /* any thread in a specific process */
764 process
= gdb_get_process(s
, pid
);
766 if (process
== NULL
) {
770 if (!process
->attached
) {
774 return get_first_cpu_in_process(s
, process
);
776 /* a specific thread */
783 process
= gdb_get_cpu_process(s
, cpu
);
785 if (pid
&& process
->pid
!= pid
) {
789 if (!process
->attached
) {
797 static const char *get_feature_xml(const GDBState
*s
, const char *p
,
798 const char **newp
, GDBProcess
*process
)
803 CPUState
*cpu
= get_first_cpu_in_process(s
, process
);
804 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
807 while (p
[len
] && p
[len
] != ':')
812 if (strncmp(p
, "target.xml", len
) == 0) {
813 char *buf
= process
->target_xml
;
814 const size_t buf_sz
= sizeof(process
->target_xml
);
816 /* Generate the XML description for this CPU. */
821 "<?xml version=\"1.0\"?>"
822 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
824 if (cc
->gdb_arch_name
) {
825 gchar
*arch
= cc
->gdb_arch_name(cpu
);
826 pstrcat(buf
, buf_sz
, "<architecture>");
827 pstrcat(buf
, buf_sz
, arch
);
828 pstrcat(buf
, buf_sz
, "</architecture>");
831 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
832 pstrcat(buf
, buf_sz
, cc
->gdb_core_xml_file
);
833 pstrcat(buf
, buf_sz
, "\"/>");
834 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
835 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
836 pstrcat(buf
, buf_sz
, r
->xml
);
837 pstrcat(buf
, buf_sz
, "\"/>");
839 pstrcat(buf
, buf_sz
, "</target>");
843 if (cc
->gdb_get_dynamic_xml
) {
844 char *xmlname
= g_strndup(p
, len
);
845 const char *xml
= cc
->gdb_get_dynamic_xml(cpu
, xmlname
);
853 name
= xml_builtin
[i
][0];
854 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
857 return name
? xml_builtin
[i
][1] : NULL
;
860 static int gdb_read_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
862 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
863 CPUArchState
*env
= cpu
->env_ptr
;
866 if (reg
< cc
->gdb_num_core_regs
) {
867 return cc
->gdb_read_register(cpu
, mem_buf
, reg
);
870 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
871 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
872 return r
->get_reg(env
, mem_buf
, reg
- r
->base_reg
);
878 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
880 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
881 CPUArchState
*env
= cpu
->env_ptr
;
884 if (reg
< cc
->gdb_num_core_regs
) {
885 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
888 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
889 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
890 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
896 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
897 specifies the first register number and these registers are included in
898 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
899 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
902 void gdb_register_coprocessor(CPUState
*cpu
,
903 gdb_reg_cb get_reg
, gdb_reg_cb set_reg
,
904 int num_regs
, const char *xml
, int g_pos
)
907 GDBRegisterState
**p
;
911 /* Check for duplicates. */
912 if (strcmp((*p
)->xml
, xml
) == 0)
917 s
= g_new0(GDBRegisterState
, 1);
918 s
->base_reg
= cpu
->gdb_num_regs
;
919 s
->num_regs
= num_regs
;
920 s
->get_reg
= get_reg
;
921 s
->set_reg
= set_reg
;
924 /* Add to end of list. */
925 cpu
->gdb_num_regs
+= num_regs
;
928 if (g_pos
!= s
->base_reg
) {
929 error_report("Error: Bad gdb register numbering for '%s', "
930 "expected %d got %d", xml
, g_pos
, s
->base_reg
);
932 cpu
->gdb_num_g_regs
= cpu
->gdb_num_regs
;
937 #ifndef CONFIG_USER_ONLY
938 /* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
939 static inline int xlat_gdb_type(CPUState
*cpu
, int gdbtype
)
941 static const int xlat
[] = {
942 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
943 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
944 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
947 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
948 int cputype
= xlat
[gdbtype
];
950 if (cc
->gdb_stop_before_watchpoint
) {
951 cputype
|= BP_STOP_BEFORE_ACCESS
;
957 static int gdb_breakpoint_insert(int type
, target_ulong addr
, target_ulong len
)
963 return kvm_insert_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
967 case GDB_BREAKPOINT_SW
:
968 case GDB_BREAKPOINT_HW
:
970 err
= cpu_breakpoint_insert(cpu
, addr
, BP_GDB
, NULL
);
976 #ifndef CONFIG_USER_ONLY
977 case GDB_WATCHPOINT_WRITE
:
978 case GDB_WATCHPOINT_READ
:
979 case GDB_WATCHPOINT_ACCESS
:
981 err
= cpu_watchpoint_insert(cpu
, addr
, len
,
982 xlat_gdb_type(cpu
, type
), NULL
);
994 static int gdb_breakpoint_remove(int type
, target_ulong addr
, target_ulong len
)
1000 return kvm_remove_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1004 case GDB_BREAKPOINT_SW
:
1005 case GDB_BREAKPOINT_HW
:
1007 err
= cpu_breakpoint_remove(cpu
, addr
, BP_GDB
);
1013 #ifndef CONFIG_USER_ONLY
1014 case GDB_WATCHPOINT_WRITE
:
1015 case GDB_WATCHPOINT_READ
:
1016 case GDB_WATCHPOINT_ACCESS
:
1018 err
= cpu_watchpoint_remove(cpu
, addr
, len
,
1019 xlat_gdb_type(cpu
, type
));
1030 static inline void gdb_cpu_breakpoint_remove_all(CPUState
*cpu
)
1032 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
1033 #ifndef CONFIG_USER_ONLY
1034 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
1038 static void gdb_process_breakpoint_remove_all(const GDBState
*s
, GDBProcess
*p
)
1040 CPUState
*cpu
= get_first_cpu_in_process(s
, p
);
1043 gdb_cpu_breakpoint_remove_all(cpu
);
1044 cpu
= gdb_next_cpu_in_process(s
, cpu
);
1048 static void gdb_breakpoint_remove_all(void)
1052 if (kvm_enabled()) {
1053 kvm_remove_all_breakpoints(gdbserver_state
->c_cpu
);
1058 gdb_cpu_breakpoint_remove_all(cpu
);
1062 static void gdb_set_cpu_pc(GDBState
*s
, target_ulong pc
)
1064 CPUState
*cpu
= s
->c_cpu
;
1066 cpu_synchronize_state(cpu
);
1067 cpu_set_pc(cpu
, pc
);
1070 static char *gdb_fmt_thread_id(const GDBState
*s
, CPUState
*cpu
,
1071 char *buf
, size_t buf_size
)
1073 if (s
->multiprocess
) {
1074 snprintf(buf
, buf_size
, "p%02x.%02x",
1075 gdb_get_cpu_pid(s
, cpu
), cpu_gdb_index(cpu
));
1077 snprintf(buf
, buf_size
, "%02x", cpu_gdb_index(cpu
));
1083 typedef enum GDBThreadIdKind
{
1085 GDB_ALL_THREADS
, /* One process, all threads */
1090 static GDBThreadIdKind
read_thread_id(const char *buf
, const char **end_buf
,
1091 uint32_t *pid
, uint32_t *tid
)
1098 ret
= qemu_strtoul(buf
, &buf
, 16, &p
);
1101 return GDB_READ_THREAD_ERR
;
1110 ret
= qemu_strtoul(buf
, &buf
, 16, &t
);
1113 return GDB_READ_THREAD_ERR
;
1119 return GDB_ALL_PROCESSES
;
1127 return GDB_ALL_THREADS
;
1134 return GDB_ONE_THREAD
;
1138 * gdb_handle_vcont - Parses and handles a vCont packet.
1139 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1140 * a format error, 0 on success.
1142 static int gdb_handle_vcont(GDBState
*s
, const char *p
)
1144 int res
, signal
= 0;
1149 GDBProcess
*process
;
1151 GDBThreadIdKind kind
;
1152 #ifdef CONFIG_USER_ONLY
1153 int max_cpus
= 1; /* global variable max_cpus exists only in system mode */
1156 max_cpus
= max_cpus
<= cpu
->cpu_index
? cpu
->cpu_index
+ 1 : max_cpus
;
1159 /* uninitialised CPUs stay 0 */
1160 newstates
= g_new0(char, max_cpus
);
1162 /* mark valid CPUs with 1 */
1164 newstates
[cpu
->cpu_index
] = 1;
1168 * res keeps track of what error we are returning, with -ENOTSUP meaning
1169 * that the command is unknown or unsupported, thus returning an empty
1170 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1171 * or incorrect parameters passed.
1181 if (cur_action
== 'C' || cur_action
== 'S') {
1182 cur_action
= qemu_tolower(cur_action
);
1183 res
= qemu_strtoul(p
+ 1, &p
, 16, &tmp
);
1187 signal
= gdb_signal_to_target(tmp
);
1188 } else if (cur_action
!= 'c' && cur_action
!= 's') {
1189 /* unknown/invalid/unsupported command */
1194 if (*p
== '\0' || *p
== ';') {
1196 * No thread specifier, action is on "all threads". The
1197 * specification is unclear regarding the process to act on. We
1198 * choose all processes.
1200 kind
= GDB_ALL_PROCESSES
;
1201 } else if (*p
++ == ':') {
1202 kind
= read_thread_id(p
, &p
, &pid
, &tid
);
1209 case GDB_READ_THREAD_ERR
:
1213 case GDB_ALL_PROCESSES
:
1214 cpu
= gdb_first_attached_cpu(s
);
1216 if (newstates
[cpu
->cpu_index
] == 1) {
1217 newstates
[cpu
->cpu_index
] = cur_action
;
1220 cpu
= gdb_next_attached_cpu(s
, cpu
);
1224 case GDB_ALL_THREADS
:
1225 process
= gdb_get_process(s
, pid
);
1227 if (!process
->attached
) {
1232 cpu
= get_first_cpu_in_process(s
, process
);
1234 if (newstates
[cpu
->cpu_index
] == 1) {
1235 newstates
[cpu
->cpu_index
] = cur_action
;
1238 cpu
= gdb_next_cpu_in_process(s
, cpu
);
1242 case GDB_ONE_THREAD
:
1243 cpu
= gdb_get_cpu(s
, pid
, tid
);
1245 /* invalid CPU/thread specified */
1251 /* only use if no previous match occourred */
1252 if (newstates
[cpu
->cpu_index
] == 1) {
1253 newstates
[cpu
->cpu_index
] = cur_action
;
1259 gdb_continue_partial(s
, newstates
);
1267 typedef union GdbCmdVariant
{
1270 unsigned long val_ul
;
1271 unsigned long long val_ull
;
1273 GDBThreadIdKind kind
;
1279 static const char *cmd_next_param(const char *param
, const char delimiter
)
1281 static const char all_delimiters
[] = ",;:=";
1282 char curr_delimiters
[2] = {0};
1283 const char *delimiters
;
1285 if (delimiter
== '?') {
1286 delimiters
= all_delimiters
;
1287 } else if (delimiter
== '0') {
1288 return strchr(param
, '\0');
1289 } else if (delimiter
== '.' && *param
) {
1292 curr_delimiters
[0] = delimiter
;
1293 delimiters
= curr_delimiters
;
1296 param
+= strcspn(param
, delimiters
);
1303 static int cmd_parse_params(const char *data
, const char *schema
,
1304 GdbCmdVariant
*params
, int *num_params
)
1307 const char *curr_schema
, *curr_data
;
1315 curr_schema
= schema
;
1318 while (curr_schema
[0] && curr_schema
[1] && *curr_data
) {
1319 switch (curr_schema
[0]) {
1321 if (qemu_strtoul(curr_data
, &curr_data
, 16,
1322 ¶ms
[curr_param
].val_ul
)) {
1326 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1329 if (qemu_strtou64(curr_data
, &curr_data
, 16,
1330 (uint64_t *)¶ms
[curr_param
].val_ull
)) {
1334 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1337 params
[curr_param
].data
= curr_data
;
1339 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1342 params
[curr_param
].opcode
= *(uint8_t *)curr_data
;
1344 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1347 params
[curr_param
].thread_id
.kind
=
1348 read_thread_id(curr_data
, &curr_data
,
1349 ¶ms
[curr_param
].thread_id
.pid
,
1350 ¶ms
[curr_param
].thread_id
.tid
);
1352 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1355 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1363 *num_params
= curr_param
;
1367 typedef struct GdbCmdContext
{
1369 GdbCmdVariant
*params
;
1371 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1372 char str_buf
[MAX_PACKET_LENGTH
+ 1];
1375 typedef void (*GdbCmdHandler
)(GdbCmdContext
*gdb_ctx
, void *user_ctx
);
1378 * cmd_startswith -> cmd is compared using startswith
1381 * schema definitions:
1382 * Each schema parameter entry consists of 2 chars,
1383 * the first char represents the parameter type handling
1384 * the second char represents the delimiter for the next parameter
1386 * Currently supported schema types:
1387 * 'l' -> unsigned long (stored in .val_ul)
1388 * 'L' -> unsigned long long (stored in .val_ull)
1389 * 's' -> string (stored in .data)
1390 * 'o' -> single char (stored in .opcode)
1391 * 't' -> thread id (stored in .thread_id)
1392 * '?' -> skip according to delimiter
1394 * Currently supported delimiters:
1395 * '?' -> Stop at any delimiter (",;:=\0")
1396 * '0' -> Stop at "\0"
1397 * '.' -> Skip 1 char unless reached "\0"
1398 * Any other value is treated as the delimiter value itself
1400 typedef struct GdbCmdParseEntry
{
1401 GdbCmdHandler handler
;
1403 bool cmd_startswith
;
1407 static inline int startswith(const char *string
, const char *pattern
)
1409 return !strncmp(string
, pattern
, strlen(pattern
));
1412 static int process_string_cmd(GDBState
*s
, void *user_ctx
, const char *data
,
1413 const GdbCmdParseEntry
*cmds
, int num_cmds
)
1415 int i
, schema_len
, max_num_params
= 0;
1416 GdbCmdContext gdb_ctx
;
1422 for (i
= 0; i
< num_cmds
; i
++) {
1423 const GdbCmdParseEntry
*cmd
= &cmds
[i
];
1424 g_assert(cmd
->handler
&& cmd
->cmd
);
1426 if ((cmd
->cmd_startswith
&& !startswith(data
, cmd
->cmd
)) ||
1427 (!cmd
->cmd_startswith
&& strcmp(cmd
->cmd
, data
))) {
1432 schema_len
= strlen(cmd
->schema
);
1433 if (schema_len
% 2) {
1437 max_num_params
= schema_len
/ 2;
1441 (GdbCmdVariant
*)alloca(sizeof(*gdb_ctx
.params
) * max_num_params
);
1442 memset(gdb_ctx
.params
, 0, sizeof(*gdb_ctx
.params
) * max_num_params
);
1444 if (cmd_parse_params(&data
[strlen(cmd
->cmd
)], cmd
->schema
,
1445 gdb_ctx
.params
, &gdb_ctx
.num_params
)) {
1450 cmd
->handler(&gdb_ctx
, user_ctx
);
1457 static void run_cmd_parser(GDBState
*s
, const char *data
,
1458 const GdbCmdParseEntry
*cmd
)
1464 /* In case there was an error during the command parsing we must
1465 * send a NULL packet to indicate the command is not supported */
1466 if (process_string_cmd(s
, NULL
, data
, cmd
, 1)) {
1471 static void handle_detach(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1473 GDBProcess
*process
;
1474 GDBState
*s
= gdb_ctx
->s
;
1477 if (s
->multiprocess
) {
1478 if (!gdb_ctx
->num_params
) {
1479 put_packet(s
, "E22");
1483 pid
= gdb_ctx
->params
[0].val_ul
;
1486 process
= gdb_get_process(s
, pid
);
1487 gdb_process_breakpoint_remove_all(s
, process
);
1488 process
->attached
= false;
1490 if (pid
== gdb_get_cpu_pid(s
, s
->c_cpu
)) {
1491 s
->c_cpu
= gdb_first_attached_cpu(s
);
1494 if (pid
== gdb_get_cpu_pid(s
, s
->g_cpu
)) {
1495 s
->g_cpu
= gdb_first_attached_cpu(s
);
1499 /* No more process attached */
1500 gdb_syscall_mode
= GDB_SYS_DISABLED
;
1503 put_packet(s
, "OK");
1506 static void handle_thread_alive(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1510 if (!gdb_ctx
->num_params
) {
1511 put_packet(gdb_ctx
->s
, "E22");
1515 if (gdb_ctx
->params
[0].thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1516 put_packet(gdb_ctx
->s
, "E22");
1520 cpu
= gdb_get_cpu(gdb_ctx
->s
, gdb_ctx
->params
[0].thread_id
.pid
,
1521 gdb_ctx
->params
[0].thread_id
.tid
);
1523 put_packet(gdb_ctx
->s
, "E22");
1527 put_packet(gdb_ctx
->s
, "OK");
1530 static void handle_continue(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1532 if (gdb_ctx
->num_params
) {
1533 gdb_set_cpu_pc(gdb_ctx
->s
, gdb_ctx
->params
[0].val_ull
);
1536 gdb_ctx
->s
->signal
= 0;
1537 gdb_continue(gdb_ctx
->s
);
1540 static void handle_cont_with_sig(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1542 unsigned long signal
= 0;
1545 * Note: C sig;[addr] is currently unsupported and we simply
1546 * omit the addr parameter
1548 if (gdb_ctx
->num_params
) {
1549 signal
= gdb_ctx
->params
[0].val_ul
;
1552 gdb_ctx
->s
->signal
= gdb_signal_to_target(signal
);
1553 if (gdb_ctx
->s
->signal
== -1) {
1554 gdb_ctx
->s
->signal
= 0;
1556 gdb_continue(gdb_ctx
->s
);
1559 static void handle_set_thread(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1563 if (gdb_ctx
->num_params
!= 2) {
1564 put_packet(gdb_ctx
->s
, "E22");
1568 if (gdb_ctx
->params
[1].thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1569 put_packet(gdb_ctx
->s
, "E22");
1573 if (gdb_ctx
->params
[1].thread_id
.kind
!= GDB_ONE_THREAD
) {
1574 put_packet(gdb_ctx
->s
, "OK");
1578 cpu
= gdb_get_cpu(gdb_ctx
->s
, gdb_ctx
->params
[1].thread_id
.pid
,
1579 gdb_ctx
->params
[1].thread_id
.tid
);
1581 put_packet(gdb_ctx
->s
, "E22");
1586 * Note: This command is deprecated and modern gdb's will be using the
1587 * vCont command instead.
1589 switch (gdb_ctx
->params
[0].opcode
) {
1591 gdb_ctx
->s
->c_cpu
= cpu
;
1592 put_packet(gdb_ctx
->s
, "OK");
1595 gdb_ctx
->s
->g_cpu
= cpu
;
1596 put_packet(gdb_ctx
->s
, "OK");
1599 put_packet(gdb_ctx
->s
, "E22");
1604 static void handle_insert_bp(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1608 if (gdb_ctx
->num_params
!= 3) {
1609 put_packet(gdb_ctx
->s
, "E22");
1613 res
= gdb_breakpoint_insert(gdb_ctx
->params
[0].val_ul
,
1614 gdb_ctx
->params
[1].val_ull
,
1615 gdb_ctx
->params
[2].val_ull
);
1617 put_packet(gdb_ctx
->s
, "OK");
1619 } else if (res
== -ENOSYS
) {
1620 put_packet(gdb_ctx
->s
, "");
1624 put_packet(gdb_ctx
->s
, "E22");
1627 static void handle_remove_bp(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1631 if (gdb_ctx
->num_params
!= 3) {
1632 put_packet(gdb_ctx
->s
, "E22");
1636 res
= gdb_breakpoint_remove(gdb_ctx
->params
[0].val_ul
,
1637 gdb_ctx
->params
[1].val_ull
,
1638 gdb_ctx
->params
[2].val_ull
);
1640 put_packet(gdb_ctx
->s
, "OK");
1642 } else if (res
== -ENOSYS
) {
1643 put_packet(gdb_ctx
->s
, "");
1647 put_packet(gdb_ctx
->s
, "E22");
1650 static void handle_set_reg(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1655 put_packet(gdb_ctx
->s
, "E00");
1659 if (gdb_ctx
->num_params
!= 2) {
1660 put_packet(gdb_ctx
->s
, "E22");
1664 reg_size
= strlen(gdb_ctx
->params
[1].data
) / 2;
1665 hextomem(gdb_ctx
->mem_buf
, gdb_ctx
->params
[1].data
, reg_size
);
1666 gdb_write_register(gdb_ctx
->s
->g_cpu
, gdb_ctx
->mem_buf
,
1667 gdb_ctx
->params
[0].val_ull
);
1668 put_packet(gdb_ctx
->s
, "OK");
1671 static void handle_get_reg(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1676 * Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1677 * This works, but can be very slow. Anything new enough to
1678 * understand XML also knows how to use this properly.
1681 put_packet(gdb_ctx
->s
, "");
1685 if (!gdb_ctx
->num_params
) {
1686 put_packet(gdb_ctx
->s
, "E14");
1690 reg_size
= gdb_read_register(gdb_ctx
->s
->g_cpu
, gdb_ctx
->mem_buf
,
1691 gdb_ctx
->params
[0].val_ull
);
1693 put_packet(gdb_ctx
->s
, "E14");
1697 memtohex(gdb_ctx
->str_buf
, gdb_ctx
->mem_buf
, reg_size
);
1698 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1701 static void handle_write_mem(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1703 if (gdb_ctx
->num_params
!= 3) {
1704 put_packet(gdb_ctx
->s
, "E22");
1708 /* hextomem() reads 2*len bytes */
1709 if (gdb_ctx
->params
[1].val_ull
> strlen(gdb_ctx
->params
[2].data
) / 2) {
1710 put_packet(gdb_ctx
->s
, "E22");
1714 hextomem(gdb_ctx
->mem_buf
, gdb_ctx
->params
[2].data
,
1715 gdb_ctx
->params
[1].val_ull
);
1716 if (target_memory_rw_debug(gdb_ctx
->s
->g_cpu
, gdb_ctx
->params
[0].val_ull
,
1718 gdb_ctx
->params
[1].val_ull
, true)) {
1719 put_packet(gdb_ctx
->s
, "E14");
1723 put_packet(gdb_ctx
->s
, "OK");
1726 static void handle_read_mem(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1728 if (gdb_ctx
->num_params
!= 2) {
1729 put_packet(gdb_ctx
->s
, "E22");
1733 /* memtohex() doubles the required space */
1734 if (gdb_ctx
->params
[1].val_ull
> MAX_PACKET_LENGTH
/ 2) {
1735 put_packet(gdb_ctx
->s
, "E22");
1739 if (target_memory_rw_debug(gdb_ctx
->s
->g_cpu
, gdb_ctx
->params
[0].val_ull
,
1741 gdb_ctx
->params
[1].val_ull
, false)) {
1742 put_packet(gdb_ctx
->s
, "E14");
1746 memtohex(gdb_ctx
->str_buf
, gdb_ctx
->mem_buf
, gdb_ctx
->params
[1].val_ull
);
1747 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1750 static void handle_write_all_regs(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1752 target_ulong addr
, len
;
1756 if (!gdb_ctx
->num_params
) {
1760 cpu_synchronize_state(gdb_ctx
->s
->g_cpu
);
1761 registers
= gdb_ctx
->mem_buf
;
1762 len
= strlen(gdb_ctx
->params
[0].data
) / 2;
1763 hextomem(registers
, gdb_ctx
->params
[0].data
, len
);
1764 for (addr
= 0; addr
< gdb_ctx
->s
->g_cpu
->gdb_num_g_regs
&& len
> 0;
1766 reg_size
= gdb_write_register(gdb_ctx
->s
->g_cpu
, registers
, addr
);
1768 registers
+= reg_size
;
1770 put_packet(gdb_ctx
->s
, "OK");
1773 static void handle_read_all_regs(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1775 target_ulong addr
, len
;
1777 cpu_synchronize_state(gdb_ctx
->s
->g_cpu
);
1779 for (addr
= 0; addr
< gdb_ctx
->s
->g_cpu
->gdb_num_g_regs
; addr
++) {
1780 len
+= gdb_read_register(gdb_ctx
->s
->g_cpu
, gdb_ctx
->mem_buf
+ len
,
1784 memtohex(gdb_ctx
->str_buf
, gdb_ctx
->mem_buf
, len
);
1785 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1788 static void handle_file_io(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1790 if (gdb_ctx
->num_params
>= 2 && gdb_ctx
->s
->current_syscall_cb
) {
1791 target_ulong ret
, err
;
1793 ret
= (target_ulong
)gdb_ctx
->params
[0].val_ull
;
1794 err
= (target_ulong
)gdb_ctx
->params
[1].val_ull
;
1795 gdb_ctx
->s
->current_syscall_cb(gdb_ctx
->s
->c_cpu
, ret
, err
);
1796 gdb_ctx
->s
->current_syscall_cb
= NULL
;
1799 if (gdb_ctx
->num_params
>= 3 && gdb_ctx
->params
[2].opcode
== (uint8_t)'C') {
1800 put_packet(gdb_ctx
->s
, "T02");
1804 gdb_continue(gdb_ctx
->s
);
1807 static void handle_step(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1809 if (gdb_ctx
->num_params
) {
1810 gdb_set_cpu_pc(gdb_ctx
->s
, (target_ulong
)gdb_ctx
->params
[0].val_ull
);
1813 cpu_single_step(gdb_ctx
->s
->c_cpu
, sstep_flags
);
1814 gdb_continue(gdb_ctx
->s
);
1817 static void handle_v_cont_query(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1819 put_packet(gdb_ctx
->s
, "vCont;c;C;s;S");
1822 static void handle_v_cont(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1826 if (!gdb_ctx
->num_params
) {
1830 res
= gdb_handle_vcont(gdb_ctx
->s
, gdb_ctx
->params
[0].data
);
1831 if ((res
== -EINVAL
) || (res
== -ERANGE
)) {
1832 put_packet(gdb_ctx
->s
, "E22");
1834 put_packet(gdb_ctx
->s
, "");
1838 static void handle_v_attach(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1840 GDBProcess
*process
;
1844 pstrcpy(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "E22");
1845 if (!gdb_ctx
->num_params
) {
1849 process
= gdb_get_process(gdb_ctx
->s
, gdb_ctx
->params
[0].val_ul
);
1854 cpu
= get_first_cpu_in_process(gdb_ctx
->s
, process
);
1859 process
->attached
= true;
1860 gdb_ctx
->s
->g_cpu
= cpu
;
1861 gdb_ctx
->s
->c_cpu
= cpu
;
1863 gdb_fmt_thread_id(gdb_ctx
->s
, cpu
, thread_id
, sizeof(thread_id
));
1864 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "T%02xthread:%s;",
1865 GDB_SIGNAL_TRAP
, thread_id
);
1867 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1870 static void handle_v_kill(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1872 /* Kill the target */
1873 put_packet(gdb_ctx
->s
, "OK");
1874 error_report("QEMU: Terminated via GDBstub");
1878 static GdbCmdParseEntry gdb_v_commands_table
[] = {
1879 /* Order is important if has same prefix */
1881 .handler
= handle_v_cont_query
,
1886 .handler
= handle_v_cont
,
1888 .cmd_startswith
= 1,
1892 .handler
= handle_v_attach
,
1894 .cmd_startswith
= 1,
1898 .handler
= handle_v_kill
,
1904 static void handle_v_commands(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1906 if (!gdb_ctx
->num_params
) {
1910 if (process_string_cmd(gdb_ctx
->s
, NULL
, gdb_ctx
->params
[0].data
,
1911 gdb_v_commands_table
,
1912 ARRAY_SIZE(gdb_v_commands_table
))) {
1913 put_packet(gdb_ctx
->s
, "");
1917 static void handle_query_qemu_sstepbits(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1919 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
),
1920 "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", SSTEP_ENABLE
,
1921 SSTEP_NOIRQ
, SSTEP_NOTIMER
);
1922 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1925 static void handle_set_qemu_sstep(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1927 if (!gdb_ctx
->num_params
) {
1931 sstep_flags
= gdb_ctx
->params
[0].val_ul
;
1932 put_packet(gdb_ctx
->s
, "OK");
1935 static void handle_query_qemu_sstep(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1937 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "0x%x", sstep_flags
);
1938 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1941 static void handle_query_curr_tid(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1944 GDBProcess
*process
;
1948 * "Current thread" remains vague in the spec, so always return
1949 * the first thread of the current process (gdb returns the
1952 process
= gdb_get_cpu_process(gdb_ctx
->s
, gdb_ctx
->s
->g_cpu
);
1953 cpu
= get_first_cpu_in_process(gdb_ctx
->s
, process
);
1954 gdb_fmt_thread_id(gdb_ctx
->s
, cpu
, thread_id
, sizeof(thread_id
));
1955 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "QC%s", thread_id
);
1956 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1959 static void handle_query_threads(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1963 if (!gdb_ctx
->s
->query_cpu
) {
1964 put_packet(gdb_ctx
->s
, "l");
1968 gdb_fmt_thread_id(gdb_ctx
->s
, gdb_ctx
->s
->query_cpu
, thread_id
,
1970 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "m%s", thread_id
);
1971 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1972 gdb_ctx
->s
->query_cpu
=
1973 gdb_next_attached_cpu(gdb_ctx
->s
, gdb_ctx
->s
->query_cpu
);
1976 static void handle_query_first_threads(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1978 gdb_ctx
->s
->query_cpu
= gdb_first_attached_cpu(gdb_ctx
->s
);
1979 handle_query_threads(gdb_ctx
, user_ctx
);
1982 static void handle_query_thread_extra(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1987 if (!gdb_ctx
->num_params
||
1988 gdb_ctx
->params
[0].thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1989 put_packet(gdb_ctx
->s
, "E22");
1993 cpu
= gdb_get_cpu(gdb_ctx
->s
, gdb_ctx
->params
[0].thread_id
.pid
,
1994 gdb_ctx
->params
[0].thread_id
.tid
);
1999 cpu_synchronize_state(cpu
);
2001 if (gdb_ctx
->s
->multiprocess
&& (gdb_ctx
->s
->process_num
> 1)) {
2002 /* Print the CPU model and name in multiprocess mode */
2003 ObjectClass
*oc
= object_get_class(OBJECT(cpu
));
2004 const char *cpu_model
= object_class_get_name(oc
);
2005 char *cpu_name
= object_get_canonical_path_component(OBJECT(cpu
));
2006 len
= snprintf((char *)gdb_ctx
->mem_buf
, sizeof(gdb_ctx
->str_buf
) / 2,
2007 "%s %s [%s]", cpu_model
, cpu_name
,
2008 cpu
->halted
? "halted " : "running");
2011 /* memtohex() doubles the required space */
2012 len
= snprintf((char *)gdb_ctx
->mem_buf
, sizeof(gdb_ctx
->str_buf
) / 2,
2013 "CPU#%d [%s]", cpu
->cpu_index
,
2014 cpu
->halted
? "halted " : "running");
2016 trace_gdbstub_op_extra_info((char *)gdb_ctx
->mem_buf
);
2017 memtohex(gdb_ctx
->str_buf
, gdb_ctx
->mem_buf
, len
);
2018 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
2021 #ifdef CONFIG_USER_ONLY
2022 static void handle_query_offsets(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2026 ts
= gdb_ctx
->s
->c_cpu
->opaque
;
2027 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
),
2028 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
2029 ";Bss=" TARGET_ABI_FMT_lx
,
2030 ts
->info
->code_offset
,
2031 ts
->info
->data_offset
,
2032 ts
->info
->data_offset
);
2033 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
2036 static void handle_query_rcmd(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2040 if (!gdb_ctx
->num_params
) {
2041 put_packet(gdb_ctx
->s
, "E22");
2045 len
= strlen(gdb_ctx
->params
[0].data
);
2047 put_packet(gdb_ctx
->s
, "E01");
2052 hextomem(gdb_ctx
->mem_buf
, gdb_ctx
->params
[0].data
, len
);
2053 gdb_ctx
->mem_buf
[len
++] = 0;
2054 qemu_chr_be_write(gdb_ctx
->s
->mon_chr
, gdb_ctx
->mem_buf
, len
);
2055 put_packet(gdb_ctx
->s
, "OK");
2060 static void handle_query_supported(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2064 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "PacketSize=%x",
2066 cc
= CPU_GET_CLASS(first_cpu
);
2067 if (cc
->gdb_core_xml_file
) {
2068 pstrcat(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
),
2069 ";qXfer:features:read+");
2072 if (gdb_ctx
->num_params
&&
2073 strstr(gdb_ctx
->params
[0].data
, "multiprocess+")) {
2074 gdb_ctx
->s
->multiprocess
= true;
2077 pstrcat(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), ";multiprocess+");
2078 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
2081 static void handle_query_xfer_features(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2083 GDBProcess
*process
;
2085 unsigned long len
, total_len
, addr
;
2089 if (gdb_ctx
->num_params
< 3) {
2090 put_packet(gdb_ctx
->s
, "E22");
2094 process
= gdb_get_cpu_process(gdb_ctx
->s
, gdb_ctx
->s
->g_cpu
);
2095 cc
= CPU_GET_CLASS(gdb_ctx
->s
->g_cpu
);
2096 if (!cc
->gdb_core_xml_file
) {
2097 put_packet(gdb_ctx
->s
, "");
2102 p
= gdb_ctx
->params
[0].data
;
2103 xml
= get_feature_xml(gdb_ctx
->s
, p
, &p
, process
);
2105 put_packet(gdb_ctx
->s
, "E00");
2109 addr
= gdb_ctx
->params
[1].val_ul
;
2110 len
= gdb_ctx
->params
[2].val_ul
;
2111 total_len
= strlen(xml
);
2112 if (addr
> total_len
) {
2113 put_packet(gdb_ctx
->s
, "E00");
2117 if (len
> (MAX_PACKET_LENGTH
- 5) / 2) {
2118 len
= (MAX_PACKET_LENGTH
- 5) / 2;
2121 if (len
< total_len
- addr
) {
2122 gdb_ctx
->str_buf
[0] = 'm';
2123 len
= memtox(gdb_ctx
->str_buf
+ 1, xml
+ addr
, len
);
2125 gdb_ctx
->str_buf
[0] = 'l';
2126 len
= memtox(gdb_ctx
->str_buf
+ 1, xml
+ addr
, total_len
- addr
);
2129 put_packet_binary(gdb_ctx
->s
, gdb_ctx
->str_buf
, len
+ 1, true);
2132 static void handle_query_attached(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2134 put_packet(gdb_ctx
->s
, GDB_ATTACHED
);
2137 static void handle_query_qemu_supported(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2139 put_packet(gdb_ctx
->s
, "sstepbits;sstep");
2142 static GdbCmdParseEntry gdb_gen_query_set_common_table
[] = {
2143 /* Order is important if has same prefix */
2145 .handler
= handle_query_qemu_sstepbits
,
2146 .cmd
= "qemu.sstepbits",
2149 .handler
= handle_query_qemu_sstep
,
2150 .cmd
= "qemu.sstep",
2153 .handler
= handle_set_qemu_sstep
,
2154 .cmd
= "qemu.sstep=",
2155 .cmd_startswith
= 1,
2160 static GdbCmdParseEntry gdb_gen_query_table
[] = {
2162 .handler
= handle_query_curr_tid
,
2166 .handler
= handle_query_threads
,
2167 .cmd
= "sThreadInfo",
2170 .handler
= handle_query_first_threads
,
2171 .cmd
= "fThreadInfo",
2174 .handler
= handle_query_thread_extra
,
2175 .cmd
= "ThreadExtraInfo,",
2176 .cmd_startswith
= 1,
2179 #ifdef CONFIG_USER_ONLY
2181 .handler
= handle_query_offsets
,
2186 .handler
= handle_query_rcmd
,
2188 .cmd_startswith
= 1,
2193 .handler
= handle_query_supported
,
2194 .cmd
= "Supported:",
2195 .cmd_startswith
= 1,
2199 .handler
= handle_query_supported
,
2204 .handler
= handle_query_xfer_features
,
2205 .cmd
= "Xfer:features:read:",
2206 .cmd_startswith
= 1,
2210 .handler
= handle_query_attached
,
2215 .handler
= handle_query_attached
,
2219 .handler
= handle_query_qemu_supported
,
2220 .cmd
= "qemu.Supported",
2224 static GdbCmdParseEntry gdb_gen_set_table
[] = {
2225 /* Order is important if has same prefix */
2227 .handler
= handle_set_qemu_sstep
,
2228 .cmd
= "qemu.sstep:",
2229 .cmd_startswith
= 1,
2234 static void handle_gen_query(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2236 if (!gdb_ctx
->num_params
) {
2240 if (!process_string_cmd(gdb_ctx
->s
, NULL
, gdb_ctx
->params
[0].data
,
2241 gdb_gen_query_set_common_table
,
2242 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
2246 if (process_string_cmd(gdb_ctx
->s
, NULL
, gdb_ctx
->params
[0].data
,
2247 gdb_gen_query_table
,
2248 ARRAY_SIZE(gdb_gen_query_table
))) {
2249 put_packet(gdb_ctx
->s
, "");
2253 static void handle_gen_set(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2255 if (!gdb_ctx
->num_params
) {
2259 if (!process_string_cmd(gdb_ctx
->s
, NULL
, gdb_ctx
->params
[0].data
,
2260 gdb_gen_query_set_common_table
,
2261 ARRAY_SIZE(gdb_gen_query_set_common_table
))) {
2265 if (process_string_cmd(gdb_ctx
->s
, NULL
, gdb_ctx
->params
[0].data
,
2267 ARRAY_SIZE(gdb_gen_set_table
))) {
2268 put_packet(gdb_ctx
->s
, "");
2272 static void handle_target_halt(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
2276 gdb_fmt_thread_id(gdb_ctx
->s
, gdb_ctx
->s
->c_cpu
, thread_id
,
2278 snprintf(gdb_ctx
->str_buf
, sizeof(gdb_ctx
->str_buf
), "T%02xthread:%s;",
2279 GDB_SIGNAL_TRAP
, thread_id
);
2280 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
2282 * Remove all the breakpoints when this query is issued,
2283 * because gdb is doing an initial connect and the state
2284 * should be cleaned up.
2286 gdb_breakpoint_remove_all();
2289 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
2293 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
2294 char buf
[sizeof(mem_buf
) + 1 /* trailing NUL */];
2295 const GdbCmdParseEntry
*cmd_parser
= NULL
;
2297 trace_gdbstub_io_command(line_buf
);
2303 put_packet(s
, "OK");
2307 static const GdbCmdParseEntry target_halted_cmd_desc
= {
2308 .handler
= handle_target_halt
,
2312 cmd_parser
= &target_halted_cmd_desc
;
2317 static const GdbCmdParseEntry continue_cmd_desc
= {
2318 .handler
= handle_continue
,
2320 .cmd_startswith
= 1,
2323 cmd_parser
= &continue_cmd_desc
;
2328 static const GdbCmdParseEntry cont_with_sig_cmd_desc
= {
2329 .handler
= handle_cont_with_sig
,
2331 .cmd_startswith
= 1,
2334 cmd_parser
= &cont_with_sig_cmd_desc
;
2339 static const GdbCmdParseEntry v_cmd_desc
= {
2340 .handler
= handle_v_commands
,
2342 .cmd_startswith
= 1,
2345 cmd_parser
= &v_cmd_desc
;
2349 /* Kill the target */
2350 error_report("QEMU: Terminated via GDBstub");
2354 static const GdbCmdParseEntry detach_cmd_desc
= {
2355 .handler
= handle_detach
,
2357 .cmd_startswith
= 1,
2360 cmd_parser
= &detach_cmd_desc
;
2365 static const GdbCmdParseEntry step_cmd_desc
= {
2366 .handler
= handle_step
,
2368 .cmd_startswith
= 1,
2371 cmd_parser
= &step_cmd_desc
;
2376 static const GdbCmdParseEntry file_io_cmd_desc
= {
2377 .handler
= handle_file_io
,
2379 .cmd_startswith
= 1,
2382 cmd_parser
= &file_io_cmd_desc
;
2387 static const GdbCmdParseEntry read_all_regs_cmd_desc
= {
2388 .handler
= handle_read_all_regs
,
2392 cmd_parser
= &read_all_regs_cmd_desc
;
2397 static const GdbCmdParseEntry write_all_regs_cmd_desc
= {
2398 .handler
= handle_write_all_regs
,
2400 .cmd_startswith
= 1,
2403 cmd_parser
= &write_all_regs_cmd_desc
;
2408 static const GdbCmdParseEntry read_mem_cmd_desc
= {
2409 .handler
= handle_read_mem
,
2411 .cmd_startswith
= 1,
2414 cmd_parser
= &read_mem_cmd_desc
;
2419 static const GdbCmdParseEntry write_mem_cmd_desc
= {
2420 .handler
= handle_write_mem
,
2422 .cmd_startswith
= 1,
2425 cmd_parser
= &write_mem_cmd_desc
;
2430 static const GdbCmdParseEntry get_reg_cmd_desc
= {
2431 .handler
= handle_get_reg
,
2433 .cmd_startswith
= 1,
2436 cmd_parser
= &get_reg_cmd_desc
;
2441 static const GdbCmdParseEntry set_reg_cmd_desc
= {
2442 .handler
= handle_set_reg
,
2444 .cmd_startswith
= 1,
2447 cmd_parser
= &set_reg_cmd_desc
;
2452 static const GdbCmdParseEntry insert_bp_cmd_desc
= {
2453 .handler
= handle_insert_bp
,
2455 .cmd_startswith
= 1,
2458 cmd_parser
= &insert_bp_cmd_desc
;
2463 static const GdbCmdParseEntry remove_bp_cmd_desc
= {
2464 .handler
= handle_remove_bp
,
2466 .cmd_startswith
= 1,
2469 cmd_parser
= &remove_bp_cmd_desc
;
2474 static const GdbCmdParseEntry set_thread_cmd_desc
= {
2475 .handler
= handle_set_thread
,
2477 .cmd_startswith
= 1,
2480 cmd_parser
= &set_thread_cmd_desc
;
2485 static const GdbCmdParseEntry thread_alive_cmd_desc
= {
2486 .handler
= handle_thread_alive
,
2488 .cmd_startswith
= 1,
2491 cmd_parser
= &thread_alive_cmd_desc
;
2496 static const GdbCmdParseEntry gen_query_cmd_desc
= {
2497 .handler
= handle_gen_query
,
2499 .cmd_startswith
= 1,
2502 cmd_parser
= &gen_query_cmd_desc
;
2507 static const GdbCmdParseEntry gen_set_cmd_desc
= {
2508 .handler
= handle_gen_set
,
2510 .cmd_startswith
= 1,
2513 cmd_parser
= &gen_set_cmd_desc
;
2517 /* put empty packet */
2523 run_cmd_parser(s
, line_buf
, cmd_parser
);
2528 void gdb_set_stop_cpu(CPUState
*cpu
)
2530 GDBProcess
*p
= gdb_get_cpu_process(gdbserver_state
, cpu
);
2534 * Having a stop CPU corresponding to a process that is not attached
2535 * confuses GDB. So we ignore the request.
2540 gdbserver_state
->c_cpu
= cpu
;
2541 gdbserver_state
->g_cpu
= cpu
;
2544 #ifndef CONFIG_USER_ONLY
2545 static void gdb_vm_state_change(void *opaque
, int running
, RunState state
)
2547 GDBState
*s
= gdbserver_state
;
2548 CPUState
*cpu
= s
->c_cpu
;
2554 if (running
|| s
->state
== RS_INACTIVE
) {
2557 /* Is there a GDB syscall waiting to be sent? */
2558 if (s
->current_syscall_cb
) {
2559 put_packet(s
, s
->syscall_buf
);
2564 /* No process attached */
2568 gdb_fmt_thread_id(s
, cpu
, thread_id
, sizeof(thread_id
));
2571 case RUN_STATE_DEBUG
:
2572 if (cpu
->watchpoint_hit
) {
2573 switch (cpu
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
2584 trace_gdbstub_hit_watchpoint(type
, cpu_gdb_index(cpu
),
2585 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
2586 snprintf(buf
, sizeof(buf
),
2587 "T%02xthread:%s;%swatch:" TARGET_FMT_lx
";",
2588 GDB_SIGNAL_TRAP
, thread_id
, type
,
2589 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
2590 cpu
->watchpoint_hit
= NULL
;
2593 trace_gdbstub_hit_break();
2596 ret
= GDB_SIGNAL_TRAP
;
2598 case RUN_STATE_PAUSED
:
2599 trace_gdbstub_hit_paused();
2600 ret
= GDB_SIGNAL_INT
;
2602 case RUN_STATE_SHUTDOWN
:
2603 trace_gdbstub_hit_shutdown();
2604 ret
= GDB_SIGNAL_QUIT
;
2606 case RUN_STATE_IO_ERROR
:
2607 trace_gdbstub_hit_io_error();
2608 ret
= GDB_SIGNAL_IO
;
2610 case RUN_STATE_WATCHDOG
:
2611 trace_gdbstub_hit_watchdog();
2612 ret
= GDB_SIGNAL_ALRM
;
2614 case RUN_STATE_INTERNAL_ERROR
:
2615 trace_gdbstub_hit_internal_error();
2616 ret
= GDB_SIGNAL_ABRT
;
2618 case RUN_STATE_SAVE_VM
:
2619 case RUN_STATE_RESTORE_VM
:
2621 case RUN_STATE_FINISH_MIGRATE
:
2622 ret
= GDB_SIGNAL_XCPU
;
2625 trace_gdbstub_hit_unknown(state
);
2626 ret
= GDB_SIGNAL_UNKNOWN
;
2629 gdb_set_stop_cpu(cpu
);
2630 snprintf(buf
, sizeof(buf
), "T%02xthread:%s;", ret
, thread_id
);
2635 /* disable single step if it was enabled */
2636 cpu_single_step(cpu
, 0);
2640 /* Send a gdb syscall request.
2641 This accepts limited printf-style format specifiers, specifically:
2642 %x - target_ulong argument printed in hex.
2643 %lx - 64-bit argument printed in hex.
2644 %s - string pointer (target_ulong) and length (int) pair. */
2645 void gdb_do_syscallv(gdb_syscall_complete_cb cb
, const char *fmt
, va_list va
)
2653 s
= gdbserver_state
;
2656 s
->current_syscall_cb
= cb
;
2657 #ifndef CONFIG_USER_ONLY
2658 vm_stop(RUN_STATE_DEBUG
);
2661 p_end
= &s
->syscall_buf
[sizeof(s
->syscall_buf
)];
2668 addr
= va_arg(va
, target_ulong
);
2669 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
, addr
);
2672 if (*(fmt
++) != 'x')
2674 i64
= va_arg(va
, uint64_t);
2675 p
+= snprintf(p
, p_end
- p
, "%" PRIx64
, i64
);
2678 addr
= va_arg(va
, target_ulong
);
2679 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
"/%x",
2680 addr
, va_arg(va
, int));
2684 error_report("gdbstub: Bad syscall format string '%s'",
2693 #ifdef CONFIG_USER_ONLY
2694 put_packet(s
, s
->syscall_buf
);
2695 /* Return control to gdb for it to process the syscall request.
2696 * Since the protocol requires that gdb hands control back to us
2697 * using a "here are the results" F packet, we don't need to check
2698 * gdb_handlesig's return value (which is the signal to deliver if
2699 * execution was resumed via a continue packet).
2701 gdb_handlesig(s
->c_cpu
, 0);
2703 /* In this case wait to send the syscall packet until notification that
2704 the CPU has stopped. This must be done because if the packet is sent
2705 now the reply from the syscall request could be received while the CPU
2706 is still in the running state, which can cause packets to be dropped
2707 and state transition 'T' packets to be sent while the syscall is still
2709 qemu_cpu_kick(s
->c_cpu
);
2713 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
2718 gdb_do_syscallv(cb
, fmt
, va
);
2722 static void gdb_read_byte(GDBState
*s
, uint8_t ch
)
2726 #ifndef CONFIG_USER_ONLY
2727 if (s
->last_packet_len
) {
2728 /* Waiting for a response to the last packet. If we see the start
2729 of a new command then abandon the previous response. */
2731 trace_gdbstub_err_got_nack();
2732 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
2733 } else if (ch
== '+') {
2734 trace_gdbstub_io_got_ack();
2736 trace_gdbstub_io_got_unexpected(ch
);
2739 if (ch
== '+' || ch
== '$')
2740 s
->last_packet_len
= 0;
2744 if (runstate_is_running()) {
2745 /* when the CPU is running, we cannot do anything except stop
2746 it when receiving a char */
2747 vm_stop(RUN_STATE_PAUSED
);
2754 /* start of command packet */
2755 s
->line_buf_index
= 0;
2757 s
->state
= RS_GETLINE
;
2759 trace_gdbstub_err_garbage(ch
);
2764 /* start escape sequence */
2765 s
->state
= RS_GETLINE_ESC
;
2767 } else if (ch
== '*') {
2768 /* start run length encoding sequence */
2769 s
->state
= RS_GETLINE_RLE
;
2771 } else if (ch
== '#') {
2772 /* end of command, start of checksum*/
2773 s
->state
= RS_CHKSUM1
;
2774 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2775 trace_gdbstub_err_overrun();
2778 /* unescaped command character */
2779 s
->line_buf
[s
->line_buf_index
++] = ch
;
2783 case RS_GETLINE_ESC
:
2785 /* unexpected end of command in escape sequence */
2786 s
->state
= RS_CHKSUM1
;
2787 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2788 /* command buffer overrun */
2789 trace_gdbstub_err_overrun();
2792 /* parse escaped character and leave escape state */
2793 s
->line_buf
[s
->line_buf_index
++] = ch
^ 0x20;
2795 s
->state
= RS_GETLINE
;
2798 case RS_GETLINE_RLE
:
2800 * Run-length encoding is explained in "Debugging with GDB /
2801 * Appendix E GDB Remote Serial Protocol / Overview".
2803 if (ch
< ' ' || ch
== '#' || ch
== '$' || ch
> 126) {
2804 /* invalid RLE count encoding */
2805 trace_gdbstub_err_invalid_repeat(ch
);
2806 s
->state
= RS_GETLINE
;
2808 /* decode repeat length */
2809 int repeat
= ch
- ' ' + 3;
2810 if (s
->line_buf_index
+ repeat
>= sizeof(s
->line_buf
) - 1) {
2811 /* that many repeats would overrun the command buffer */
2812 trace_gdbstub_err_overrun();
2814 } else if (s
->line_buf_index
< 1) {
2815 /* got a repeat but we have nothing to repeat */
2816 trace_gdbstub_err_invalid_rle();
2817 s
->state
= RS_GETLINE
;
2819 /* repeat the last character */
2820 memset(s
->line_buf
+ s
->line_buf_index
,
2821 s
->line_buf
[s
->line_buf_index
- 1], repeat
);
2822 s
->line_buf_index
+= repeat
;
2824 s
->state
= RS_GETLINE
;
2829 /* get high hex digit of checksum */
2830 if (!isxdigit(ch
)) {
2831 trace_gdbstub_err_checksum_invalid(ch
);
2832 s
->state
= RS_GETLINE
;
2835 s
->line_buf
[s
->line_buf_index
] = '\0';
2836 s
->line_csum
= fromhex(ch
) << 4;
2837 s
->state
= RS_CHKSUM2
;
2840 /* get low hex digit of checksum */
2841 if (!isxdigit(ch
)) {
2842 trace_gdbstub_err_checksum_invalid(ch
);
2843 s
->state
= RS_GETLINE
;
2846 s
->line_csum
|= fromhex(ch
);
2848 if (s
->line_csum
!= (s
->line_sum
& 0xff)) {
2849 trace_gdbstub_err_checksum_incorrect(s
->line_sum
, s
->line_csum
);
2850 /* send NAK reply */
2852 put_buffer(s
, &reply
, 1);
2855 /* send ACK reply */
2857 put_buffer(s
, &reply
, 1);
2858 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
2867 /* Tell the remote gdb that the process has exited. */
2868 void gdb_exit(CPUArchState
*env
, int code
)
2873 s
= gdbserver_state
;
2877 #ifdef CONFIG_USER_ONLY
2878 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2883 trace_gdbstub_op_exiting((uint8_t)code
);
2885 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
2888 #ifndef CONFIG_USER_ONLY
2889 qemu_chr_fe_deinit(&s
->chr
, true);
2894 * Create the process that will contain all the "orphan" CPUs (that are not
2895 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2896 * be attachable and thus will be invisible to the user.
2898 static void create_default_process(GDBState
*s
)
2900 GDBProcess
*process
;
2903 if (s
->process_num
) {
2904 max_pid
= s
->processes
[s
->process_num
- 1].pid
;
2907 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2908 process
= &s
->processes
[s
->process_num
- 1];
2910 /* We need an available PID slot for this process */
2911 assert(max_pid
< UINT32_MAX
);
2913 process
->pid
= max_pid
+ 1;
2914 process
->attached
= false;
2915 process
->target_xml
[0] = '\0';
2918 #ifdef CONFIG_USER_ONLY
2920 gdb_handlesig(CPUState
*cpu
, int sig
)
2926 s
= gdbserver_state
;
2927 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2931 /* disable single step if it was enabled */
2932 cpu_single_step(cpu
, 0);
2936 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb(sig
));
2939 /* put_packet() might have detected that the peer terminated the
2947 s
->running_state
= 0;
2948 while (s
->running_state
== 0) {
2949 n
= read(s
->fd
, buf
, 256);
2953 for (i
= 0; i
< n
; i
++) {
2954 gdb_read_byte(s
, buf
[i
]);
2957 /* XXX: Connection closed. Should probably wait for another
2958 connection before continuing. */
2971 /* Tell the remote gdb that the process has exited due to SIG. */
2972 void gdb_signalled(CPUArchState
*env
, int sig
)
2977 s
= gdbserver_state
;
2978 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2982 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb(sig
));
2986 static bool gdb_accept(void)
2989 struct sockaddr_in sockaddr
;
2994 len
= sizeof(sockaddr
);
2995 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
2996 if (fd
< 0 && errno
!= EINTR
) {
2999 } else if (fd
>= 0) {
3000 qemu_set_cloexec(fd
);
3005 /* set short latency */
3006 if (socket_set_nodelay(fd
)) {
3007 perror("setsockopt");
3012 s
= g_malloc0(sizeof(GDBState
));
3013 create_default_process(s
);
3014 s
->processes
[0].attached
= true;
3015 s
->c_cpu
= gdb_first_attached_cpu(s
);
3016 s
->g_cpu
= s
->c_cpu
;
3018 gdb_has_xml
= false;
3020 gdbserver_state
= s
;
3024 static int gdbserver_open(int port
)
3026 struct sockaddr_in sockaddr
;
3029 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
3034 qemu_set_cloexec(fd
);
3036 socket_set_fast_reuse(fd
);
3038 sockaddr
.sin_family
= AF_INET
;
3039 sockaddr
.sin_port
= htons(port
);
3040 sockaddr
.sin_addr
.s_addr
= 0;
3041 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
3047 ret
= listen(fd
, 1);
3056 int gdbserver_start(int port
)
3058 gdbserver_fd
= gdbserver_open(port
);
3059 if (gdbserver_fd
< 0)
3061 /* accept connections */
3062 if (!gdb_accept()) {
3063 close(gdbserver_fd
);
3070 /* Disable gdb stub for child processes. */
3071 void gdbserver_fork(CPUState
*cpu
)
3073 GDBState
*s
= gdbserver_state
;
3075 if (gdbserver_fd
< 0 || s
->fd
< 0) {
3080 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
3081 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
3084 static int gdb_chr_can_receive(void *opaque
)
3086 /* We can handle an arbitrarily large amount of data.
3087 Pick the maximum packet size, which is as good as anything. */
3088 return MAX_PACKET_LENGTH
;
3091 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
3095 for (i
= 0; i
< size
; i
++) {
3096 gdb_read_byte(gdbserver_state
, buf
[i
]);
3100 static void gdb_chr_event(void *opaque
, int event
)
3103 GDBState
*s
= (GDBState
*) opaque
;
3106 case CHR_EVENT_OPENED
:
3107 /* Start with first process attached, others detached */
3108 for (i
= 0; i
< s
->process_num
; i
++) {
3109 s
->processes
[i
].attached
= !i
;
3112 s
->c_cpu
= gdb_first_attached_cpu(s
);
3113 s
->g_cpu
= s
->c_cpu
;
3115 vm_stop(RUN_STATE_PAUSED
);
3116 gdb_has_xml
= false;
3123 static void gdb_monitor_output(GDBState
*s
, const char *msg
, int len
)
3125 char buf
[MAX_PACKET_LENGTH
];
3128 if (len
> (MAX_PACKET_LENGTH
/2) - 1)
3129 len
= (MAX_PACKET_LENGTH
/2) - 1;
3130 memtohex(buf
+ 1, (uint8_t *)msg
, len
);
3134 static int gdb_monitor_write(Chardev
*chr
, const uint8_t *buf
, int len
)
3136 const char *p
= (const char *)buf
;
3139 max_sz
= (sizeof(gdbserver_state
->last_packet
) - 2) / 2;
3141 if (len
<= max_sz
) {
3142 gdb_monitor_output(gdbserver_state
, p
, len
);
3145 gdb_monitor_output(gdbserver_state
, p
, max_sz
);
3153 static void gdb_sigterm_handler(int signal
)
3155 if (runstate_is_running()) {
3156 vm_stop(RUN_STATE_PAUSED
);
3161 static void gdb_monitor_open(Chardev
*chr
, ChardevBackend
*backend
,
3162 bool *be_opened
, Error
**errp
)
3167 static void char_gdb_class_init(ObjectClass
*oc
, void *data
)
3169 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
3171 cc
->internal
= true;
3172 cc
->open
= gdb_monitor_open
;
3173 cc
->chr_write
= gdb_monitor_write
;
3176 #define TYPE_CHARDEV_GDB "chardev-gdb"
3178 static const TypeInfo char_gdb_type_info
= {
3179 .name
= TYPE_CHARDEV_GDB
,
3180 .parent
= TYPE_CHARDEV
,
3181 .class_init
= char_gdb_class_init
,
3184 static int find_cpu_clusters(Object
*child
, void *opaque
)
3186 if (object_dynamic_cast(child
, TYPE_CPU_CLUSTER
)) {
3187 GDBState
*s
= (GDBState
*) opaque
;
3188 CPUClusterState
*cluster
= CPU_CLUSTER(child
);
3189 GDBProcess
*process
;
3191 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
3193 process
= &s
->processes
[s
->process_num
- 1];
3196 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
3197 * runtime, we enforce here that the machine does not use a cluster ID
3198 * that would lead to PID 0.
3200 assert(cluster
->cluster_id
!= UINT32_MAX
);
3201 process
->pid
= cluster
->cluster_id
+ 1;
3202 process
->attached
= false;
3203 process
->target_xml
[0] = '\0';
3208 return object_child_foreach(child
, find_cpu_clusters
, opaque
);
3211 static int pid_order(const void *a
, const void *b
)
3213 GDBProcess
*pa
= (GDBProcess
*) a
;
3214 GDBProcess
*pb
= (GDBProcess
*) b
;
3216 if (pa
->pid
< pb
->pid
) {
3218 } else if (pa
->pid
> pb
->pid
) {
3225 static void create_processes(GDBState
*s
)
3227 object_child_foreach(object_get_root(), find_cpu_clusters
, s
);
3231 qsort(s
->processes
, s
->process_num
, sizeof(s
->processes
[0]), pid_order
);
3234 create_default_process(s
);
3237 static void cleanup_processes(GDBState
*s
)
3239 g_free(s
->processes
);
3241 s
->processes
= NULL
;
3244 int gdbserver_start(const char *device
)
3246 trace_gdbstub_op_start(device
);
3249 char gdbstub_device_name
[128];
3250 Chardev
*chr
= NULL
;
3254 error_report("gdbstub: meaningless to attach gdb to a "
3255 "machine without any CPU.");
3261 if (strcmp(device
, "none") != 0) {
3262 if (strstart(device
, "tcp:", NULL
)) {
3263 /* enforce required TCP attributes */
3264 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
3265 "%s,nowait,nodelay,server", device
);
3266 device
= gdbstub_device_name
;
3269 else if (strcmp(device
, "stdio") == 0) {
3270 struct sigaction act
;
3272 memset(&act
, 0, sizeof(act
));
3273 act
.sa_handler
= gdb_sigterm_handler
;
3274 sigaction(SIGINT
, &act
, NULL
);
3278 * FIXME: it's a bit weird to allow using a mux chardev here
3279 * and implicitly setup a monitor. We may want to break this.
3281 chr
= qemu_chr_new_noreplay("gdb", device
, true, NULL
);
3286 s
= gdbserver_state
;
3288 s
= g_malloc0(sizeof(GDBState
));
3289 gdbserver_state
= s
;
3291 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
3293 /* Initialize a monitor terminal for gdb */
3294 mon_chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_GDB
,
3295 NULL
, NULL
, &error_abort
);
3296 monitor_init(mon_chr
, 0);
3298 qemu_chr_fe_deinit(&s
->chr
, true);
3299 mon_chr
= s
->mon_chr
;
3300 cleanup_processes(s
);
3301 memset(s
, 0, sizeof(GDBState
));
3302 s
->mon_chr
= mon_chr
;
3305 create_processes(s
);
3308 qemu_chr_fe_init(&s
->chr
, chr
, &error_abort
);
3309 qemu_chr_fe_set_handlers(&s
->chr
, gdb_chr_can_receive
, gdb_chr_receive
,
3310 gdb_chr_event
, NULL
, s
, NULL
, true);
3312 s
->state
= chr
? RS_IDLE
: RS_INACTIVE
;
3313 s
->mon_chr
= mon_chr
;
3314 s
->current_syscall_cb
= NULL
;
3319 void gdbserver_cleanup(void)
3321 if (gdbserver_state
) {
3322 put_packet(gdbserver_state
, "W00");
3326 static void register_types(void)
3328 type_register_static(&char_gdb_type_info
);
3331 type_init(register_types
);