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/>.
19 #include "qemu/osdep.h"
20 #include "qapi/error.h"
21 #include "qemu/error-report.h"
22 #include "qemu/cutils.h"
23 #include "trace-root.h"
24 #ifdef CONFIG_USER_ONLY
27 #include "monitor/monitor.h"
28 #include "chardev/char.h"
29 #include "chardev/char-fe.h"
30 #include "sysemu/sysemu.h"
31 #include "exec/gdbstub.h"
32 #include "hw/cpu/cluster.h"
35 #define MAX_PACKET_LENGTH 4096
37 #include "qemu/sockets.h"
38 #include "sysemu/hw_accel.h"
39 #include "sysemu/kvm.h"
40 #include "exec/semihost.h"
41 #include "exec/exec-all.h"
43 #ifdef CONFIG_USER_ONLY
44 #define GDB_ATTACHED "0"
46 #define GDB_ATTACHED "1"
49 static inline int target_memory_rw_debug(CPUState
*cpu
, target_ulong addr
,
50 uint8_t *buf
, int len
, bool is_write
)
52 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
54 if (cc
->memory_rw_debug
) {
55 return cc
->memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
57 return cpu_memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
60 /* Return the GDB index for a given vCPU state.
62 * For user mode this is simply the thread id. In system mode GDB
63 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
65 static inline int cpu_gdb_index(CPUState
*cpu
)
67 #if defined(CONFIG_USER_ONLY)
68 TaskState
*ts
= (TaskState
*) cpu
->opaque
;
71 return cpu
->cpu_index
+ 1;
84 GDB_SIGNAL_UNKNOWN
= 143
87 #ifdef CONFIG_USER_ONLY
89 /* Map target signal numbers to GDB protocol signal numbers and vice
90 * versa. For user emulation's currently supported systems, we can
91 * assume most signals are defined.
94 static int gdb_signal_table
[] = {
254 /* In system mode we only need SIGINT and SIGTRAP; other signals
255 are not yet supported. */
262 static int gdb_signal_table
[] = {
272 #ifdef CONFIG_USER_ONLY
273 static int target_signal_to_gdb (int sig
)
276 for (i
= 0; i
< ARRAY_SIZE (gdb_signal_table
); i
++)
277 if (gdb_signal_table
[i
] == sig
)
279 return GDB_SIGNAL_UNKNOWN
;
283 static int gdb_signal_to_target (int sig
)
285 if (sig
< ARRAY_SIZE (gdb_signal_table
))
286 return gdb_signal_table
[sig
];
291 typedef struct GDBRegisterState
{
297 struct GDBRegisterState
*next
;
300 typedef struct GDBProcess
{
304 char target_xml
[1024];
316 typedef struct GDBState
{
317 CPUState
*c_cpu
; /* current CPU for step/continue ops */
318 CPUState
*g_cpu
; /* current CPU for other ops */
319 CPUState
*query_cpu
; /* for q{f|s}ThreadInfo */
320 enum RSState state
; /* parsing state */
321 char line_buf
[MAX_PACKET_LENGTH
];
323 int line_sum
; /* running checksum */
324 int line_csum
; /* checksum at the end of the packet */
325 uint8_t last_packet
[MAX_PACKET_LENGTH
+ 4];
328 #ifdef CONFIG_USER_ONLY
336 GDBProcess
*processes
;
338 char syscall_buf
[256];
339 gdb_syscall_complete_cb current_syscall_cb
;
342 /* By default use no IRQs and no timers while single stepping so as to
343 * make single stepping like an ICE HW step.
345 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
347 static GDBState
*gdbserver_state
;
351 #ifdef CONFIG_USER_ONLY
352 /* XXX: This is not thread safe. Do we care? */
353 static int gdbserver_fd
= -1;
355 static int get_char(GDBState
*s
)
361 ret
= qemu_recv(s
->fd
, &ch
, 1, 0);
363 if (errno
== ECONNRESET
)
367 } else if (ret
== 0) {
385 /* Decide if either remote gdb syscalls or native file IO should be used. */
386 int use_gdb_syscalls(void)
388 SemihostingTarget target
= semihosting_get_target();
389 if (target
== SEMIHOSTING_TARGET_NATIVE
) {
390 /* -semihosting-config target=native */
392 } else if (target
== SEMIHOSTING_TARGET_GDB
) {
393 /* -semihosting-config target=gdb */
397 /* -semihosting-config target=auto */
398 /* On the first call check if gdb is connected and remember. */
399 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
400 gdb_syscall_mode
= (gdbserver_state
? GDB_SYS_ENABLED
403 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
406 /* Resume execution. */
407 static inline void gdb_continue(GDBState
*s
)
410 #ifdef CONFIG_USER_ONLY
411 s
->running_state
= 1;
412 trace_gdbstub_op_continue();
414 if (!runstate_needs_reset()) {
415 trace_gdbstub_op_continue();
422 * Resume execution, per CPU actions. For user-mode emulation it's
423 * equivalent to gdb_continue.
425 static int gdb_continue_partial(GDBState
*s
, char *newstates
)
429 #ifdef CONFIG_USER_ONLY
431 * This is not exactly accurate, but it's an improvement compared to the
432 * previous situation, where only one CPU would be single-stepped.
435 if (newstates
[cpu
->cpu_index
] == 's') {
436 trace_gdbstub_op_stepping(cpu
->cpu_index
);
437 cpu_single_step(cpu
, sstep_flags
);
440 s
->running_state
= 1;
444 if (!runstate_needs_reset()) {
445 if (vm_prepare_start()) {
450 switch (newstates
[cpu
->cpu_index
]) {
453 break; /* nothing to do here */
455 trace_gdbstub_op_stepping(cpu
->cpu_index
);
456 cpu_single_step(cpu
, sstep_flags
);
461 trace_gdbstub_op_continue_cpu(cpu
->cpu_index
);
472 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, true);
478 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
480 #ifdef CONFIG_USER_ONLY
484 ret
= send(s
->fd
, buf
, len
, 0);
494 /* XXX this blocks entire thread. Rewrite to use
495 * qemu_chr_fe_write and background I/O callbacks */
496 qemu_chr_fe_write_all(&s
->chr
, buf
, len
);
500 static inline int fromhex(int v
)
502 if (v
>= '0' && v
<= '9')
504 else if (v
>= 'A' && v
<= 'F')
506 else if (v
>= 'a' && v
<= 'f')
512 static inline int tohex(int v
)
520 /* writes 2*len+1 bytes in buf */
521 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
526 for(i
= 0; i
< len
; i
++) {
528 *q
++ = tohex(c
>> 4);
529 *q
++ = tohex(c
& 0xf);
534 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
538 for(i
= 0; i
< len
; i
++) {
539 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
544 static void hexdump(const char *buf
, int len
,
545 void (*trace_fn
)(size_t ofs
, char const *text
))
547 char line_buffer
[3 * 16 + 4 + 16 + 1];
550 for (i
= 0; i
< len
|| (i
& 0xF); ++i
) {
551 size_t byte_ofs
= i
& 15;
554 memset(line_buffer
, ' ', 3 * 16 + 4 + 16);
555 line_buffer
[3 * 16 + 4 + 16] = 0;
558 size_t col_group
= (i
>> 2) & 3;
559 size_t hex_col
= byte_ofs
* 3 + col_group
;
560 size_t txt_col
= 3 * 16 + 4 + byte_ofs
;
565 line_buffer
[hex_col
+ 0] = tohex((value
>> 4) & 0xF);
566 line_buffer
[hex_col
+ 1] = tohex((value
>> 0) & 0xF);
567 line_buffer
[txt_col
+ 0] = (value
>= ' ' && value
< 127)
573 trace_fn(i
& -16, line_buffer
);
577 /* return -1 if error, 0 if OK */
578 static int put_packet_binary(GDBState
*s
, const char *buf
, int len
, bool dump
)
583 if (dump
&& trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY
)) {
584 hexdump(buf
, len
, trace_gdbstub_io_binaryreply
);
593 for(i
= 0; i
< len
; i
++) {
597 *(p
++) = tohex((csum
>> 4) & 0xf);
598 *(p
++) = tohex((csum
) & 0xf);
600 s
->last_packet_len
= p
- s
->last_packet
;
601 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
603 #ifdef CONFIG_USER_ONLY
616 /* return -1 if error, 0 if OK */
617 static int put_packet(GDBState
*s
, const char *buf
)
619 trace_gdbstub_io_reply(buf
);
621 return put_packet_binary(s
, buf
, strlen(buf
), false);
624 /* Encode data using the encoding for 'x' packets. */
625 static int memtox(char *buf
, const char *mem
, int len
)
633 case '#': case '$': case '*': case '}':
645 static uint32_t gdb_get_cpu_pid(const GDBState
*s
, CPUState
*cpu
)
647 #ifndef CONFIG_USER_ONLY
648 gchar
*path
, *name
= NULL
;
650 CPUClusterState
*cluster
;
653 path
= object_get_canonical_path(OBJECT(cpu
));
656 /* Return the default process' PID */
657 ret
= s
->processes
[s
->process_num
- 1].pid
;
661 name
= object_get_canonical_path_component(OBJECT(cpu
));
662 assert(name
!= NULL
);
665 * Retrieve the CPU parent path by removing the last '/' and the CPU name
666 * from the CPU canonical path.
668 path
[strlen(path
) - strlen(name
) - 1] = '\0';
670 obj
= object_resolve_path_type(path
, TYPE_CPU_CLUSTER
, NULL
);
673 /* Return the default process' PID */
674 ret
= s
->processes
[s
->process_num
- 1].pid
;
678 cluster
= CPU_CLUSTER(obj
);
679 ret
= cluster
->cluster_id
+ 1;
688 /* TODO: In user mode, we should use the task state PID */
689 return s
->processes
[s
->process_num
- 1].pid
;
693 static GDBProcess
*gdb_get_process(const GDBState
*s
, uint32_t pid
)
698 /* 0 means any process, we take the first one */
699 return &s
->processes
[0];
702 for (i
= 0; i
< s
->process_num
; i
++) {
703 if (s
->processes
[i
].pid
== pid
) {
704 return &s
->processes
[i
];
711 static GDBProcess
*gdb_get_cpu_process(const GDBState
*s
, CPUState
*cpu
)
713 return gdb_get_process(s
, gdb_get_cpu_pid(s
, cpu
));
716 static CPUState
*find_cpu(uint32_t thread_id
)
721 if (cpu_gdb_index(cpu
) == thread_id
) {
729 static CPUState
*get_first_cpu_in_process(const GDBState
*s
,
735 if (gdb_get_cpu_pid(s
, cpu
) == process
->pid
) {
743 static CPUState
*gdb_next_cpu_in_process(const GDBState
*s
, CPUState
*cpu
)
745 uint32_t pid
= gdb_get_cpu_pid(s
, cpu
);
749 if (gdb_get_cpu_pid(s
, cpu
) == pid
) {
759 /* Return the cpu following @cpu, while ignoring unattached processes. */
760 static CPUState
*gdb_next_attached_cpu(const GDBState
*s
, CPUState
*cpu
)
765 if (gdb_get_cpu_process(s
, cpu
)->attached
) {
775 /* Return the first attached cpu */
776 static CPUState
*gdb_first_attached_cpu(const GDBState
*s
)
778 CPUState
*cpu
= first_cpu
;
779 GDBProcess
*process
= gdb_get_cpu_process(s
, cpu
);
781 if (!process
->attached
) {
782 return gdb_next_attached_cpu(s
, cpu
);
788 static CPUState
*gdb_get_cpu(const GDBState
*s
, uint32_t pid
, uint32_t tid
)
794 /* 0 means any process/thread, we take the first attached one */
795 return gdb_first_attached_cpu(s
);
796 } else if (pid
&& !tid
) {
797 /* any thread in a specific process */
798 process
= gdb_get_process(s
, pid
);
800 if (process
== NULL
) {
804 if (!process
->attached
) {
808 return get_first_cpu_in_process(s
, process
);
810 /* a specific thread */
817 process
= gdb_get_cpu_process(s
, cpu
);
819 if (pid
&& process
->pid
!= pid
) {
823 if (!process
->attached
) {
831 static const char *get_feature_xml(const GDBState
*s
, const char *p
,
832 const char **newp
, GDBProcess
*process
)
837 CPUState
*cpu
= get_first_cpu_in_process(s
, process
);
838 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
841 while (p
[len
] && p
[len
] != ':')
846 if (strncmp(p
, "target.xml", len
) == 0) {
847 char *buf
= process
->target_xml
;
848 const size_t buf_sz
= sizeof(process
->target_xml
);
850 /* Generate the XML description for this CPU. */
855 "<?xml version=\"1.0\"?>"
856 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
858 if (cc
->gdb_arch_name
) {
859 gchar
*arch
= cc
->gdb_arch_name(cpu
);
860 pstrcat(buf
, buf_sz
, "<architecture>");
861 pstrcat(buf
, buf_sz
, arch
);
862 pstrcat(buf
, buf_sz
, "</architecture>");
865 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
866 pstrcat(buf
, buf_sz
, cc
->gdb_core_xml_file
);
867 pstrcat(buf
, buf_sz
, "\"/>");
868 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
869 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
870 pstrcat(buf
, buf_sz
, r
->xml
);
871 pstrcat(buf
, buf_sz
, "\"/>");
873 pstrcat(buf
, buf_sz
, "</target>");
877 if (cc
->gdb_get_dynamic_xml
) {
878 char *xmlname
= g_strndup(p
, len
);
879 const char *xml
= cc
->gdb_get_dynamic_xml(cpu
, xmlname
);
887 name
= xml_builtin
[i
][0];
888 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
891 return name
? xml_builtin
[i
][1] : NULL
;
894 static int gdb_read_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
896 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
897 CPUArchState
*env
= cpu
->env_ptr
;
900 if (reg
< cc
->gdb_num_core_regs
) {
901 return cc
->gdb_read_register(cpu
, mem_buf
, reg
);
904 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
905 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
906 return r
->get_reg(env
, mem_buf
, reg
- r
->base_reg
);
912 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
914 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
915 CPUArchState
*env
= cpu
->env_ptr
;
918 if (reg
< cc
->gdb_num_core_regs
) {
919 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
922 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
923 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
924 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
930 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
931 specifies the first register number and these registers are included in
932 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
933 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
936 void gdb_register_coprocessor(CPUState
*cpu
,
937 gdb_reg_cb get_reg
, gdb_reg_cb set_reg
,
938 int num_regs
, const char *xml
, int g_pos
)
941 GDBRegisterState
**p
;
945 /* Check for duplicates. */
946 if (strcmp((*p
)->xml
, xml
) == 0)
951 s
= g_new0(GDBRegisterState
, 1);
952 s
->base_reg
= cpu
->gdb_num_regs
;
953 s
->num_regs
= num_regs
;
954 s
->get_reg
= get_reg
;
955 s
->set_reg
= set_reg
;
958 /* Add to end of list. */
959 cpu
->gdb_num_regs
+= num_regs
;
962 if (g_pos
!= s
->base_reg
) {
963 error_report("Error: Bad gdb register numbering for '%s', "
964 "expected %d got %d", xml
, g_pos
, s
->base_reg
);
966 cpu
->gdb_num_g_regs
= cpu
->gdb_num_regs
;
971 #ifndef CONFIG_USER_ONLY
972 /* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
973 static inline int xlat_gdb_type(CPUState
*cpu
, int gdbtype
)
975 static const int xlat
[] = {
976 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
977 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
978 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
981 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
982 int cputype
= xlat
[gdbtype
];
984 if (cc
->gdb_stop_before_watchpoint
) {
985 cputype
|= BP_STOP_BEFORE_ACCESS
;
991 static int gdb_breakpoint_insert(target_ulong addr
, target_ulong len
, int type
)
997 return kvm_insert_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1001 case GDB_BREAKPOINT_SW
:
1002 case GDB_BREAKPOINT_HW
:
1004 err
= cpu_breakpoint_insert(cpu
, addr
, BP_GDB
, NULL
);
1010 #ifndef CONFIG_USER_ONLY
1011 case GDB_WATCHPOINT_WRITE
:
1012 case GDB_WATCHPOINT_READ
:
1013 case GDB_WATCHPOINT_ACCESS
:
1015 err
= cpu_watchpoint_insert(cpu
, addr
, len
,
1016 xlat_gdb_type(cpu
, type
), NULL
);
1028 static int gdb_breakpoint_remove(target_ulong addr
, target_ulong len
, int type
)
1033 if (kvm_enabled()) {
1034 return kvm_remove_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1038 case GDB_BREAKPOINT_SW
:
1039 case GDB_BREAKPOINT_HW
:
1041 err
= cpu_breakpoint_remove(cpu
, addr
, BP_GDB
);
1047 #ifndef CONFIG_USER_ONLY
1048 case GDB_WATCHPOINT_WRITE
:
1049 case GDB_WATCHPOINT_READ
:
1050 case GDB_WATCHPOINT_ACCESS
:
1052 err
= cpu_watchpoint_remove(cpu
, addr
, len
,
1053 xlat_gdb_type(cpu
, type
));
1064 static inline void gdb_cpu_breakpoint_remove_all(CPUState
*cpu
)
1066 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
1067 #ifndef CONFIG_USER_ONLY
1068 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
1072 static void gdb_process_breakpoint_remove_all(const GDBState
*s
, GDBProcess
*p
)
1074 CPUState
*cpu
= get_first_cpu_in_process(s
, p
);
1077 gdb_cpu_breakpoint_remove_all(cpu
);
1078 cpu
= gdb_next_cpu_in_process(s
, cpu
);
1082 static void gdb_breakpoint_remove_all(void)
1086 if (kvm_enabled()) {
1087 kvm_remove_all_breakpoints(gdbserver_state
->c_cpu
);
1092 gdb_cpu_breakpoint_remove_all(cpu
);
1096 static void gdb_set_cpu_pc(GDBState
*s
, target_ulong pc
)
1098 CPUState
*cpu
= s
->c_cpu
;
1100 cpu_synchronize_state(cpu
);
1101 cpu_set_pc(cpu
, pc
);
1104 static char *gdb_fmt_thread_id(const GDBState
*s
, CPUState
*cpu
,
1105 char *buf
, size_t buf_size
)
1107 if (s
->multiprocess
) {
1108 snprintf(buf
, buf_size
, "p%02x.%02x",
1109 gdb_get_cpu_pid(s
, cpu
), cpu_gdb_index(cpu
));
1111 snprintf(buf
, buf_size
, "%02x", cpu_gdb_index(cpu
));
1117 typedef enum GDBThreadIdKind
{
1119 GDB_ALL_THREADS
, /* One process, all threads */
1124 static GDBThreadIdKind
read_thread_id(const char *buf
, const char **end_buf
,
1125 uint32_t *pid
, uint32_t *tid
)
1132 ret
= qemu_strtoul(buf
, &buf
, 16, &p
);
1135 return GDB_READ_THREAD_ERR
;
1144 ret
= qemu_strtoul(buf
, &buf
, 16, &t
);
1147 return GDB_READ_THREAD_ERR
;
1153 return GDB_ALL_PROCESSES
;
1161 return GDB_ALL_THREADS
;
1168 return GDB_ONE_THREAD
;
1171 static int is_query_packet(const char *p
, const char *query
, char separator
)
1173 unsigned int query_len
= strlen(query
);
1175 return strncmp(p
, query
, query_len
) == 0 &&
1176 (p
[query_len
] == '\0' || p
[query_len
] == separator
);
1180 * gdb_handle_vcont - Parses and handles a vCont packet.
1181 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1182 * a format error, 0 on success.
1184 static int gdb_handle_vcont(GDBState
*s
, const char *p
)
1186 int res
, signal
= 0;
1191 GDBProcess
*process
;
1193 #ifdef CONFIG_USER_ONLY
1194 int max_cpus
= 1; /* global variable max_cpus exists only in system mode */
1197 max_cpus
= max_cpus
<= cpu
->cpu_index
? cpu
->cpu_index
+ 1 : max_cpus
;
1200 /* uninitialised CPUs stay 0 */
1201 newstates
= g_new0(char, max_cpus
);
1203 /* mark valid CPUs with 1 */
1205 newstates
[cpu
->cpu_index
] = 1;
1209 * res keeps track of what error we are returning, with -ENOTSUP meaning
1210 * that the command is unknown or unsupported, thus returning an empty
1211 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1212 * or incorrect parameters passed.
1222 if (cur_action
== 'C' || cur_action
== 'S') {
1223 cur_action
= qemu_tolower(cur_action
);
1224 res
= qemu_strtoul(p
+ 1, &p
, 16, &tmp
);
1228 signal
= gdb_signal_to_target(tmp
);
1229 } else if (cur_action
!= 'c' && cur_action
!= 's') {
1230 /* unknown/invalid/unsupported command */
1240 switch (read_thread_id(p
, &p
, &pid
, &tid
)) {
1241 case GDB_READ_THREAD_ERR
:
1245 case GDB_ALL_PROCESSES
:
1246 cpu
= gdb_first_attached_cpu(s
);
1248 if (newstates
[cpu
->cpu_index
] == 1) {
1249 newstates
[cpu
->cpu_index
] = cur_action
;
1252 cpu
= gdb_next_attached_cpu(s
, cpu
);
1256 case GDB_ALL_THREADS
:
1257 process
= gdb_get_process(s
, pid
);
1259 if (!process
->attached
) {
1264 cpu
= get_first_cpu_in_process(s
, process
);
1266 if (newstates
[cpu
->cpu_index
] == 1) {
1267 newstates
[cpu
->cpu_index
] = cur_action
;
1270 cpu
= gdb_next_cpu_in_process(s
, cpu
);
1274 case GDB_ONE_THREAD
:
1275 cpu
= gdb_get_cpu(s
, pid
, tid
);
1277 /* invalid CPU/thread specified */
1283 /* only use if no previous match occourred */
1284 if (newstates
[cpu
->cpu_index
] == 1) {
1285 newstates
[cpu
->cpu_index
] = cur_action
;
1291 gdb_continue_partial(s
, newstates
);
1299 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
1302 GDBProcess
*process
;
1306 int ch
, reg_size
, type
, res
;
1307 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1308 char buf
[sizeof(mem_buf
) + 1 /* trailing NUL */];
1311 target_ulong addr
, len
;
1312 GDBThreadIdKind thread_kind
;
1314 trace_gdbstub_io_command(line_buf
);
1320 put_packet(s
, "OK");
1323 /* TODO: Make this return the correct value for user-mode. */
1324 snprintf(buf
, sizeof(buf
), "T%02xthread:%s;", GDB_SIGNAL_TRAP
,
1325 gdb_fmt_thread_id(s
, s
->c_cpu
, thread_id
, sizeof(thread_id
)));
1327 /* Remove all the breakpoints when this query is issued,
1328 * because gdb is doing and initial connect and the state
1329 * should be cleaned up.
1331 gdb_breakpoint_remove_all();
1335 addr
= strtoull(p
, (char **)&p
, 16);
1336 gdb_set_cpu_pc(s
, addr
);
1342 s
->signal
= gdb_signal_to_target (strtoul(p
, (char **)&p
, 16));
1343 if (s
->signal
== -1)
1348 if (strncmp(p
, "Cont", 4) == 0) {
1351 put_packet(s
, "vCont;c;C;s;S");
1355 res
= gdb_handle_vcont(s
, p
);
1358 if ((res
== -EINVAL
) || (res
== -ERANGE
)) {
1359 put_packet(s
, "E22");
1362 goto unknown_command
;
1365 } else if (strncmp(p
, "Attach;", 7) == 0) {
1370 if (qemu_strtoul(p
, &p
, 16, &pid
)) {
1371 put_packet(s
, "E22");
1375 process
= gdb_get_process(s
, pid
);
1377 if (process
== NULL
) {
1378 put_packet(s
, "E22");
1382 cpu
= get_first_cpu_in_process(s
, process
);
1385 /* Refuse to attach an empty process */
1386 put_packet(s
, "E22");
1390 process
->attached
= true;
1395 snprintf(buf
, sizeof(buf
), "T%02xthread:%s;", GDB_SIGNAL_TRAP
,
1396 gdb_fmt_thread_id(s
, cpu
, thread_id
, sizeof(thread_id
)));
1401 goto unknown_command
;
1404 /* Kill the target */
1405 error_report("QEMU: Terminated via GDBstub");
1411 if (s
->multiprocess
) {
1414 put_packet(s
, "E22");
1418 if (qemu_strtoul(p
+ 1, &p
, 16, &lpid
)) {
1419 put_packet(s
, "E22");
1426 process
= gdb_get_process(s
, pid
);
1427 gdb_process_breakpoint_remove_all(s
, process
);
1428 process
->attached
= false;
1430 if (pid
== gdb_get_cpu_pid(s
, s
->c_cpu
)) {
1431 s
->c_cpu
= gdb_first_attached_cpu(s
);
1434 if (pid
== gdb_get_cpu_pid(s
, s
->g_cpu
)) {
1435 s
->g_cpu
= gdb_first_attached_cpu(s
);
1438 if (s
->c_cpu
== NULL
) {
1439 /* No more process attached */
1440 gdb_syscall_mode
= GDB_SYS_DISABLED
;
1443 put_packet(s
, "OK");
1447 addr
= strtoull(p
, (char **)&p
, 16);
1448 gdb_set_cpu_pc(s
, addr
);
1450 cpu_single_step(s
->c_cpu
, sstep_flags
);
1458 ret
= strtoull(p
, (char **)&p
, 16);
1461 err
= strtoull(p
, (char **)&p
, 16);
1468 if (s
->current_syscall_cb
) {
1469 s
->current_syscall_cb(s
->c_cpu
, ret
, err
);
1470 s
->current_syscall_cb
= NULL
;
1473 put_packet(s
, "T02");
1480 cpu_synchronize_state(s
->g_cpu
);
1482 for (addr
= 0; addr
< s
->g_cpu
->gdb_num_g_regs
; addr
++) {
1483 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
+ len
, addr
);
1486 memtohex(buf
, mem_buf
, len
);
1490 cpu_synchronize_state(s
->g_cpu
);
1491 registers
= mem_buf
;
1492 len
= strlen(p
) / 2;
1493 hextomem((uint8_t *)registers
, p
, len
);
1494 for (addr
= 0; addr
< s
->g_cpu
->gdb_num_g_regs
&& len
> 0; addr
++) {
1495 reg_size
= gdb_write_register(s
->g_cpu
, registers
, addr
);
1497 registers
+= reg_size
;
1499 put_packet(s
, "OK");
1502 addr
= strtoull(p
, (char **)&p
, 16);
1505 len
= strtoull(p
, NULL
, 16);
1507 /* memtohex() doubles the required space */
1508 if (len
> MAX_PACKET_LENGTH
/ 2) {
1509 put_packet (s
, "E22");
1513 if (target_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, false) != 0) {
1514 put_packet (s
, "E14");
1516 memtohex(buf
, mem_buf
, len
);
1521 addr
= strtoull(p
, (char **)&p
, 16);
1524 len
= strtoull(p
, (char **)&p
, 16);
1528 /* hextomem() reads 2*len bytes */
1529 if (len
> strlen(p
) / 2) {
1530 put_packet (s
, "E22");
1533 hextomem(mem_buf
, p
, len
);
1534 if (target_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
,
1536 put_packet(s
, "E14");
1538 put_packet(s
, "OK");
1542 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1543 This works, but can be very slow. Anything new enough to
1544 understand XML also knows how to use this properly. */
1546 goto unknown_command
;
1547 addr
= strtoull(p
, (char **)&p
, 16);
1548 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
, addr
);
1550 memtohex(buf
, mem_buf
, reg_size
);
1553 put_packet(s
, "E14");
1558 goto unknown_command
;
1559 addr
= strtoull(p
, (char **)&p
, 16);
1562 reg_size
= strlen(p
) / 2;
1563 hextomem(mem_buf
, p
, reg_size
);
1564 gdb_write_register(s
->g_cpu
, mem_buf
, addr
);
1565 put_packet(s
, "OK");
1569 type
= strtoul(p
, (char **)&p
, 16);
1572 addr
= strtoull(p
, (char **)&p
, 16);
1575 len
= strtoull(p
, (char **)&p
, 16);
1577 res
= gdb_breakpoint_insert(addr
, len
, type
);
1579 res
= gdb_breakpoint_remove(addr
, len
, type
);
1581 put_packet(s
, "OK");
1582 else if (res
== -ENOSYS
)
1585 put_packet(s
, "E22");
1590 thread_kind
= read_thread_id(p
, &p
, &pid
, &tid
);
1591 if (thread_kind
== GDB_READ_THREAD_ERR
) {
1592 put_packet(s
, "E22");
1596 if (thread_kind
!= GDB_ONE_THREAD
) {
1597 put_packet(s
, "OK");
1600 cpu
= gdb_get_cpu(s
, pid
, tid
);
1602 put_packet(s
, "E22");
1608 put_packet(s
, "OK");
1612 put_packet(s
, "OK");
1615 put_packet(s
, "E22");
1620 thread_kind
= read_thread_id(p
, &p
, &pid
, &tid
);
1621 if (thread_kind
== GDB_READ_THREAD_ERR
) {
1622 put_packet(s
, "E22");
1625 cpu
= gdb_get_cpu(s
, pid
, tid
);
1628 put_packet(s
, "OK");
1630 put_packet(s
, "E22");
1635 /* parse any 'q' packets here */
1636 if (!strcmp(p
,"qemu.sstepbits")) {
1637 /* Query Breakpoint bit definitions */
1638 snprintf(buf
, sizeof(buf
), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1644 } else if (is_query_packet(p
, "qemu.sstep", '=')) {
1645 /* Display or change the sstep_flags */
1648 /* Display current setting */
1649 snprintf(buf
, sizeof(buf
), "0x%x", sstep_flags
);
1654 type
= strtoul(p
, (char **)&p
, 16);
1656 put_packet(s
, "OK");
1658 } else if (strcmp(p
,"C") == 0) {
1660 * "Current thread" remains vague in the spec, so always return
1661 * the first thread of the current process (gdb returns the
1664 cpu
= get_first_cpu_in_process(s
, gdb_get_cpu_process(s
, s
->g_cpu
));
1665 snprintf(buf
, sizeof(buf
), "QC%s",
1666 gdb_fmt_thread_id(s
, cpu
, thread_id
, sizeof(thread_id
)));
1669 } else if (strcmp(p
,"fThreadInfo") == 0) {
1670 s
->query_cpu
= gdb_first_attached_cpu(s
);
1671 goto report_cpuinfo
;
1672 } else if (strcmp(p
,"sThreadInfo") == 0) {
1675 snprintf(buf
, sizeof(buf
), "m%s",
1676 gdb_fmt_thread_id(s
, s
->query_cpu
,
1677 thread_id
, sizeof(thread_id
)));
1679 s
->query_cpu
= gdb_next_attached_cpu(s
, s
->query_cpu
);
1683 } else if (strncmp(p
,"ThreadExtraInfo,", 16) == 0) {
1684 if (read_thread_id(p
+ 16, &p
, &pid
, &tid
) == GDB_READ_THREAD_ERR
) {
1685 put_packet(s
, "E22");
1688 cpu
= gdb_get_cpu(s
, pid
, tid
);
1690 cpu_synchronize_state(cpu
);
1692 if (s
->multiprocess
&& (s
->process_num
> 1)) {
1693 /* Print the CPU model and name in multiprocess mode */
1694 ObjectClass
*oc
= object_get_class(OBJECT(cpu
));
1695 const char *cpu_model
= object_class_get_name(oc
);
1697 object_get_canonical_path_component(OBJECT(cpu
));
1698 len
= snprintf((char *)mem_buf
, sizeof(buf
) / 2,
1699 "%s %s [%s]", cpu_model
, cpu_name
,
1700 cpu
->halted
? "halted " : "running");
1703 /* memtohex() doubles the required space */
1704 len
= snprintf((char *)mem_buf
, sizeof(buf
) / 2,
1705 "CPU#%d [%s]", cpu
->cpu_index
,
1706 cpu
->halted
? "halted " : "running");
1708 trace_gdbstub_op_extra_info((char *)mem_buf
);
1709 memtohex(buf
, mem_buf
, len
);
1714 #ifdef CONFIG_USER_ONLY
1715 else if (strcmp(p
, "Offsets") == 0) {
1716 TaskState
*ts
= s
->c_cpu
->opaque
;
1718 snprintf(buf
, sizeof(buf
),
1719 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1720 ";Bss=" TARGET_ABI_FMT_lx
,
1721 ts
->info
->code_offset
,
1722 ts
->info
->data_offset
,
1723 ts
->info
->data_offset
);
1727 #else /* !CONFIG_USER_ONLY */
1728 else if (strncmp(p
, "Rcmd,", 5) == 0) {
1729 int len
= strlen(p
+ 5);
1731 if ((len
% 2) != 0) {
1732 put_packet(s
, "E01");
1736 hextomem(mem_buf
, p
+ 5, len
);
1738 qemu_chr_be_write(s
->mon_chr
, mem_buf
, len
);
1739 put_packet(s
, "OK");
1742 #endif /* !CONFIG_USER_ONLY */
1743 if (is_query_packet(p
, "Supported", ':')) {
1744 snprintf(buf
, sizeof(buf
), "PacketSize=%x", MAX_PACKET_LENGTH
);
1745 cc
= CPU_GET_CLASS(first_cpu
);
1746 if (cc
->gdb_core_xml_file
!= NULL
) {
1747 pstrcat(buf
, sizeof(buf
), ";qXfer:features:read+");
1750 if (strstr(p
, "multiprocess+")) {
1751 s
->multiprocess
= true;
1753 pstrcat(buf
, sizeof(buf
), ";multiprocess+");
1758 if (strncmp(p
, "Xfer:features:read:", 19) == 0) {
1760 target_ulong total_len
;
1762 process
= gdb_get_cpu_process(s
, s
->g_cpu
);
1763 cc
= CPU_GET_CLASS(s
->g_cpu
);
1764 if (cc
->gdb_core_xml_file
== NULL
) {
1765 goto unknown_command
;
1770 xml
= get_feature_xml(s
, p
, &p
, process
);
1772 snprintf(buf
, sizeof(buf
), "E00");
1779 addr
= strtoul(p
, (char **)&p
, 16);
1782 len
= strtoul(p
, (char **)&p
, 16);
1784 total_len
= strlen(xml
);
1785 if (addr
> total_len
) {
1786 snprintf(buf
, sizeof(buf
), "E00");
1790 if (len
> (MAX_PACKET_LENGTH
- 5) / 2)
1791 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1792 if (len
< total_len
- addr
) {
1794 len
= memtox(buf
+ 1, xml
+ addr
, len
);
1797 len
= memtox(buf
+ 1, xml
+ addr
, total_len
- addr
);
1799 put_packet_binary(s
, buf
, len
+ 1, true);
1802 if (is_query_packet(p
, "Attached", ':')) {
1803 put_packet(s
, GDB_ATTACHED
);
1806 /* Unrecognised 'q' command. */
1807 goto unknown_command
;
1811 /* put empty packet */
1819 void gdb_set_stop_cpu(CPUState
*cpu
)
1821 GDBProcess
*p
= gdb_get_cpu_process(gdbserver_state
, cpu
);
1825 * Having a stop CPU corresponding to a process that is not attached
1826 * confuses GDB. So we ignore the request.
1831 gdbserver_state
->c_cpu
= cpu
;
1832 gdbserver_state
->g_cpu
= cpu
;
1835 #ifndef CONFIG_USER_ONLY
1836 static void gdb_vm_state_change(void *opaque
, int running
, RunState state
)
1838 GDBState
*s
= gdbserver_state
;
1839 CPUState
*cpu
= s
->c_cpu
;
1845 if (running
|| s
->state
== RS_INACTIVE
) {
1848 /* Is there a GDB syscall waiting to be sent? */
1849 if (s
->current_syscall_cb
) {
1850 put_packet(s
, s
->syscall_buf
);
1855 /* No process attached */
1859 gdb_fmt_thread_id(s
, cpu
, thread_id
, sizeof(thread_id
));
1862 case RUN_STATE_DEBUG
:
1863 if (cpu
->watchpoint_hit
) {
1864 switch (cpu
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
1875 trace_gdbstub_hit_watchpoint(type
, cpu_gdb_index(cpu
),
1876 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
1877 snprintf(buf
, sizeof(buf
),
1878 "T%02xthread:%s;%swatch:" TARGET_FMT_lx
";",
1879 GDB_SIGNAL_TRAP
, thread_id
, type
,
1880 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
1881 cpu
->watchpoint_hit
= NULL
;
1884 trace_gdbstub_hit_break();
1887 ret
= GDB_SIGNAL_TRAP
;
1889 case RUN_STATE_PAUSED
:
1890 trace_gdbstub_hit_paused();
1891 ret
= GDB_SIGNAL_INT
;
1893 case RUN_STATE_SHUTDOWN
:
1894 trace_gdbstub_hit_shutdown();
1895 ret
= GDB_SIGNAL_QUIT
;
1897 case RUN_STATE_IO_ERROR
:
1898 trace_gdbstub_hit_io_error();
1899 ret
= GDB_SIGNAL_IO
;
1901 case RUN_STATE_WATCHDOG
:
1902 trace_gdbstub_hit_watchdog();
1903 ret
= GDB_SIGNAL_ALRM
;
1905 case RUN_STATE_INTERNAL_ERROR
:
1906 trace_gdbstub_hit_internal_error();
1907 ret
= GDB_SIGNAL_ABRT
;
1909 case RUN_STATE_SAVE_VM
:
1910 case RUN_STATE_RESTORE_VM
:
1912 case RUN_STATE_FINISH_MIGRATE
:
1913 ret
= GDB_SIGNAL_XCPU
;
1916 trace_gdbstub_hit_unknown(state
);
1917 ret
= GDB_SIGNAL_UNKNOWN
;
1920 gdb_set_stop_cpu(cpu
);
1921 snprintf(buf
, sizeof(buf
), "T%02xthread:%s;", ret
, thread_id
);
1926 /* disable single step if it was enabled */
1927 cpu_single_step(cpu
, 0);
1931 /* Send a gdb syscall request.
1932 This accepts limited printf-style format specifiers, specifically:
1933 %x - target_ulong argument printed in hex.
1934 %lx - 64-bit argument printed in hex.
1935 %s - string pointer (target_ulong) and length (int) pair. */
1936 void gdb_do_syscallv(gdb_syscall_complete_cb cb
, const char *fmt
, va_list va
)
1944 s
= gdbserver_state
;
1947 s
->current_syscall_cb
= cb
;
1948 #ifndef CONFIG_USER_ONLY
1949 vm_stop(RUN_STATE_DEBUG
);
1952 p_end
= &s
->syscall_buf
[sizeof(s
->syscall_buf
)];
1959 addr
= va_arg(va
, target_ulong
);
1960 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
, addr
);
1963 if (*(fmt
++) != 'x')
1965 i64
= va_arg(va
, uint64_t);
1966 p
+= snprintf(p
, p_end
- p
, "%" PRIx64
, i64
);
1969 addr
= va_arg(va
, target_ulong
);
1970 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
"/%x",
1971 addr
, va_arg(va
, int));
1975 error_report("gdbstub: Bad syscall format string '%s'",
1984 #ifdef CONFIG_USER_ONLY
1985 put_packet(s
, s
->syscall_buf
);
1986 /* Return control to gdb for it to process the syscall request.
1987 * Since the protocol requires that gdb hands control back to us
1988 * using a "here are the results" F packet, we don't need to check
1989 * gdb_handlesig's return value (which is the signal to deliver if
1990 * execution was resumed via a continue packet).
1992 gdb_handlesig(s
->c_cpu
, 0);
1994 /* In this case wait to send the syscall packet until notification that
1995 the CPU has stopped. This must be done because if the packet is sent
1996 now the reply from the syscall request could be received while the CPU
1997 is still in the running state, which can cause packets to be dropped
1998 and state transition 'T' packets to be sent while the syscall is still
2000 qemu_cpu_kick(s
->c_cpu
);
2004 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
2009 gdb_do_syscallv(cb
, fmt
, va
);
2013 static void gdb_read_byte(GDBState
*s
, int ch
)
2017 #ifndef CONFIG_USER_ONLY
2018 if (s
->last_packet_len
) {
2019 /* Waiting for a response to the last packet. If we see the start
2020 of a new command then abandon the previous response. */
2022 trace_gdbstub_err_got_nack();
2023 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
2024 } else if (ch
== '+') {
2025 trace_gdbstub_io_got_ack();
2027 trace_gdbstub_io_got_unexpected((uint8_t)ch
);
2030 if (ch
== '+' || ch
== '$')
2031 s
->last_packet_len
= 0;
2035 if (runstate_is_running()) {
2036 /* when the CPU is running, we cannot do anything except stop
2037 it when receiving a char */
2038 vm_stop(RUN_STATE_PAUSED
);
2045 /* start of command packet */
2046 s
->line_buf_index
= 0;
2048 s
->state
= RS_GETLINE
;
2050 trace_gdbstub_err_garbage((uint8_t)ch
);
2055 /* start escape sequence */
2056 s
->state
= RS_GETLINE_ESC
;
2058 } else if (ch
== '*') {
2059 /* start run length encoding sequence */
2060 s
->state
= RS_GETLINE_RLE
;
2062 } else if (ch
== '#') {
2063 /* end of command, start of checksum*/
2064 s
->state
= RS_CHKSUM1
;
2065 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2066 trace_gdbstub_err_overrun();
2069 /* unescaped command character */
2070 s
->line_buf
[s
->line_buf_index
++] = ch
;
2074 case RS_GETLINE_ESC
:
2076 /* unexpected end of command in escape sequence */
2077 s
->state
= RS_CHKSUM1
;
2078 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2079 /* command buffer overrun */
2080 trace_gdbstub_err_overrun();
2083 /* parse escaped character and leave escape state */
2084 s
->line_buf
[s
->line_buf_index
++] = ch
^ 0x20;
2086 s
->state
= RS_GETLINE
;
2089 case RS_GETLINE_RLE
:
2091 /* invalid RLE count encoding */
2092 trace_gdbstub_err_invalid_repeat((uint8_t)ch
);
2093 s
->state
= RS_GETLINE
;
2095 /* decode repeat length */
2096 int repeat
= (unsigned char)ch
- ' ' + 3;
2097 if (s
->line_buf_index
+ repeat
>= sizeof(s
->line_buf
) - 1) {
2098 /* that many repeats would overrun the command buffer */
2099 trace_gdbstub_err_overrun();
2101 } else if (s
->line_buf_index
< 1) {
2102 /* got a repeat but we have nothing to repeat */
2103 trace_gdbstub_err_invalid_rle();
2104 s
->state
= RS_GETLINE
;
2106 /* repeat the last character */
2107 memset(s
->line_buf
+ s
->line_buf_index
,
2108 s
->line_buf
[s
->line_buf_index
- 1], repeat
);
2109 s
->line_buf_index
+= repeat
;
2111 s
->state
= RS_GETLINE
;
2116 /* get high hex digit of checksum */
2117 if (!isxdigit(ch
)) {
2118 trace_gdbstub_err_checksum_invalid((uint8_t)ch
);
2119 s
->state
= RS_GETLINE
;
2122 s
->line_buf
[s
->line_buf_index
] = '\0';
2123 s
->line_csum
= fromhex(ch
) << 4;
2124 s
->state
= RS_CHKSUM2
;
2127 /* get low hex digit of checksum */
2128 if (!isxdigit(ch
)) {
2129 trace_gdbstub_err_checksum_invalid((uint8_t)ch
);
2130 s
->state
= RS_GETLINE
;
2133 s
->line_csum
|= fromhex(ch
);
2135 if (s
->line_csum
!= (s
->line_sum
& 0xff)) {
2136 trace_gdbstub_err_checksum_incorrect(s
->line_sum
, s
->line_csum
);
2137 /* send NAK reply */
2139 put_buffer(s
, &reply
, 1);
2142 /* send ACK reply */
2144 put_buffer(s
, &reply
, 1);
2145 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
2154 /* Tell the remote gdb that the process has exited. */
2155 void gdb_exit(CPUArchState
*env
, int code
)
2160 s
= gdbserver_state
;
2164 #ifdef CONFIG_USER_ONLY
2165 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2170 trace_gdbstub_op_exiting((uint8_t)code
);
2172 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
2175 #ifndef CONFIG_USER_ONLY
2176 qemu_chr_fe_deinit(&s
->chr
, true);
2181 * Create the process that will contain all the "orphan" CPUs (that are not
2182 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2183 * be attachable and thus will be invisible to the user.
2185 static void create_default_process(GDBState
*s
)
2187 GDBProcess
*process
;
2190 if (s
->process_num
) {
2191 max_pid
= s
->processes
[s
->process_num
- 1].pid
;
2194 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2195 process
= &s
->processes
[s
->process_num
- 1];
2197 /* We need an available PID slot for this process */
2198 assert(max_pid
< UINT32_MAX
);
2200 process
->pid
= max_pid
+ 1;
2201 process
->attached
= false;
2202 process
->target_xml
[0] = '\0';
2205 #ifdef CONFIG_USER_ONLY
2207 gdb_handlesig(CPUState
*cpu
, int sig
)
2213 s
= gdbserver_state
;
2214 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2218 /* disable single step if it was enabled */
2219 cpu_single_step(cpu
, 0);
2223 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb(sig
));
2226 /* put_packet() might have detected that the peer terminated the
2234 s
->running_state
= 0;
2235 while (s
->running_state
== 0) {
2236 n
= read(s
->fd
, buf
, 256);
2240 for (i
= 0; i
< n
; i
++) {
2241 gdb_read_byte(s
, buf
[i
]);
2244 /* XXX: Connection closed. Should probably wait for another
2245 connection before continuing. */
2258 /* Tell the remote gdb that the process has exited due to SIG. */
2259 void gdb_signalled(CPUArchState
*env
, int sig
)
2264 s
= gdbserver_state
;
2265 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2269 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb(sig
));
2273 static bool gdb_accept(void)
2276 struct sockaddr_in sockaddr
;
2281 len
= sizeof(sockaddr
);
2282 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
2283 if (fd
< 0 && errno
!= EINTR
) {
2286 } else if (fd
>= 0) {
2287 qemu_set_cloexec(fd
);
2292 /* set short latency */
2293 if (socket_set_nodelay(fd
)) {
2294 perror("setsockopt");
2299 s
= g_malloc0(sizeof(GDBState
));
2300 create_default_process(s
);
2301 s
->processes
[0].attached
= true;
2302 s
->c_cpu
= gdb_first_attached_cpu(s
);
2303 s
->g_cpu
= s
->c_cpu
;
2305 gdb_has_xml
= false;
2307 gdbserver_state
= s
;
2311 static int gdbserver_open(int port
)
2313 struct sockaddr_in sockaddr
;
2316 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2321 qemu_set_cloexec(fd
);
2323 socket_set_fast_reuse(fd
);
2325 sockaddr
.sin_family
= AF_INET
;
2326 sockaddr
.sin_port
= htons(port
);
2327 sockaddr
.sin_addr
.s_addr
= 0;
2328 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
2334 ret
= listen(fd
, 1);
2343 int gdbserver_start(int port
)
2345 gdbserver_fd
= gdbserver_open(port
);
2346 if (gdbserver_fd
< 0)
2348 /* accept connections */
2349 if (!gdb_accept()) {
2350 close(gdbserver_fd
);
2357 /* Disable gdb stub for child processes. */
2358 void gdbserver_fork(CPUState
*cpu
)
2360 GDBState
*s
= gdbserver_state
;
2362 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2367 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
2368 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
2371 static int gdb_chr_can_receive(void *opaque
)
2373 /* We can handle an arbitrarily large amount of data.
2374 Pick the maximum packet size, which is as good as anything. */
2375 return MAX_PACKET_LENGTH
;
2378 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
2382 for (i
= 0; i
< size
; i
++) {
2383 gdb_read_byte(gdbserver_state
, buf
[i
]);
2387 static void gdb_chr_event(void *opaque
, int event
)
2390 GDBState
*s
= (GDBState
*) opaque
;
2393 case CHR_EVENT_OPENED
:
2394 /* Start with first process attached, others detached */
2395 for (i
= 0; i
< s
->process_num
; i
++) {
2396 s
->processes
[i
].attached
= !i
;
2399 s
->c_cpu
= gdb_first_attached_cpu(s
);
2400 s
->g_cpu
= s
->c_cpu
;
2402 vm_stop(RUN_STATE_PAUSED
);
2403 gdb_has_xml
= false;
2410 static void gdb_monitor_output(GDBState
*s
, const char *msg
, int len
)
2412 char buf
[MAX_PACKET_LENGTH
];
2415 if (len
> (MAX_PACKET_LENGTH
/2) - 1)
2416 len
= (MAX_PACKET_LENGTH
/2) - 1;
2417 memtohex(buf
+ 1, (uint8_t *)msg
, len
);
2421 static int gdb_monitor_write(Chardev
*chr
, const uint8_t *buf
, int len
)
2423 const char *p
= (const char *)buf
;
2426 max_sz
= (sizeof(gdbserver_state
->last_packet
) - 2) / 2;
2428 if (len
<= max_sz
) {
2429 gdb_monitor_output(gdbserver_state
, p
, len
);
2432 gdb_monitor_output(gdbserver_state
, p
, max_sz
);
2440 static void gdb_sigterm_handler(int signal
)
2442 if (runstate_is_running()) {
2443 vm_stop(RUN_STATE_PAUSED
);
2448 static void gdb_monitor_open(Chardev
*chr
, ChardevBackend
*backend
,
2449 bool *be_opened
, Error
**errp
)
2454 static void char_gdb_class_init(ObjectClass
*oc
, void *data
)
2456 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
2458 cc
->internal
= true;
2459 cc
->open
= gdb_monitor_open
;
2460 cc
->chr_write
= gdb_monitor_write
;
2463 #define TYPE_CHARDEV_GDB "chardev-gdb"
2465 static const TypeInfo char_gdb_type_info
= {
2466 .name
= TYPE_CHARDEV_GDB
,
2467 .parent
= TYPE_CHARDEV
,
2468 .class_init
= char_gdb_class_init
,
2471 static int find_cpu_clusters(Object
*child
, void *opaque
)
2473 if (object_dynamic_cast(child
, TYPE_CPU_CLUSTER
)) {
2474 GDBState
*s
= (GDBState
*) opaque
;
2475 CPUClusterState
*cluster
= CPU_CLUSTER(child
);
2476 GDBProcess
*process
;
2478 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2480 process
= &s
->processes
[s
->process_num
- 1];
2483 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
2484 * runtime, we enforce here that the machine does not use a cluster ID
2485 * that would lead to PID 0.
2487 assert(cluster
->cluster_id
!= UINT32_MAX
);
2488 process
->pid
= cluster
->cluster_id
+ 1;
2489 process
->attached
= false;
2490 process
->target_xml
[0] = '\0';
2495 return object_child_foreach(child
, find_cpu_clusters
, opaque
);
2498 static int pid_order(const void *a
, const void *b
)
2500 GDBProcess
*pa
= (GDBProcess
*) a
;
2501 GDBProcess
*pb
= (GDBProcess
*) b
;
2503 if (pa
->pid
< pb
->pid
) {
2505 } else if (pa
->pid
> pb
->pid
) {
2512 static void create_processes(GDBState
*s
)
2514 object_child_foreach(object_get_root(), find_cpu_clusters
, s
);
2518 qsort(s
->processes
, s
->process_num
, sizeof(s
->processes
[0]), pid_order
);
2521 create_default_process(s
);
2524 static void cleanup_processes(GDBState
*s
)
2526 g_free(s
->processes
);
2528 s
->processes
= NULL
;
2531 int gdbserver_start(const char *device
)
2533 trace_gdbstub_op_start(device
);
2536 char gdbstub_device_name
[128];
2537 Chardev
*chr
= NULL
;
2541 error_report("gdbstub: meaningless to attach gdb to a "
2542 "machine without any CPU.");
2548 if (strcmp(device
, "none") != 0) {
2549 if (strstart(device
, "tcp:", NULL
)) {
2550 /* enforce required TCP attributes */
2551 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
2552 "%s,nowait,nodelay,server", device
);
2553 device
= gdbstub_device_name
;
2556 else if (strcmp(device
, "stdio") == 0) {
2557 struct sigaction act
;
2559 memset(&act
, 0, sizeof(act
));
2560 act
.sa_handler
= gdb_sigterm_handler
;
2561 sigaction(SIGINT
, &act
, NULL
);
2565 * FIXME: it's a bit weird to allow using a mux chardev here
2566 * and implicitly setup a monitor. We may want to break this.
2568 chr
= qemu_chr_new_noreplay("gdb", device
, true);
2573 s
= gdbserver_state
;
2575 s
= g_malloc0(sizeof(GDBState
));
2576 gdbserver_state
= s
;
2578 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
2580 /* Initialize a monitor terminal for gdb */
2581 mon_chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_GDB
,
2582 NULL
, &error_abort
);
2583 monitor_init(mon_chr
, 0);
2585 qemu_chr_fe_deinit(&s
->chr
, true);
2586 mon_chr
= s
->mon_chr
;
2587 cleanup_processes(s
);
2588 memset(s
, 0, sizeof(GDBState
));
2589 s
->mon_chr
= mon_chr
;
2592 create_processes(s
);
2595 qemu_chr_fe_init(&s
->chr
, chr
, &error_abort
);
2596 qemu_chr_fe_set_handlers(&s
->chr
, gdb_chr_can_receive
, gdb_chr_receive
,
2597 gdb_chr_event
, NULL
, s
, NULL
, true);
2599 s
->state
= chr
? RS_IDLE
: RS_INACTIVE
;
2600 s
->mon_chr
= mon_chr
;
2601 s
->current_syscall_cb
= NULL
;
2606 void gdbserver_cleanup(void)
2608 if (gdbserver_state
) {
2609 put_packet(gdbserver_state
, "W00");
2613 static void register_types(void)
2615 type_register_static(&char_gdb_type_info
);
2618 type_init(register_types
);