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 static CPUState
*gdb_get_cpu(const GDBState
*s
, uint32_t pid
, uint32_t tid
)
765 /* 0 means any thread, we take the first one */
775 process
= gdb_get_cpu_process(s
, cpu
);
777 if (process
->pid
!= pid
) {
781 if (!process
->attached
) {
788 /* Return the cpu following @cpu, while ignoring unattached processes. */
789 static CPUState
*gdb_next_attached_cpu(const GDBState
*s
, CPUState
*cpu
)
794 if (gdb_get_cpu_process(s
, cpu
)->attached
) {
804 /* Return the first attached cpu */
805 static CPUState
*gdb_first_attached_cpu(const GDBState
*s
)
807 CPUState
*cpu
= first_cpu
;
808 GDBProcess
*process
= gdb_get_cpu_process(s
, cpu
);
810 if (!process
->attached
) {
811 return gdb_next_attached_cpu(s
, cpu
);
817 static const char *get_feature_xml(const GDBState
*s
, const char *p
,
818 const char **newp
, GDBProcess
*process
)
823 CPUState
*cpu
= get_first_cpu_in_process(s
, process
);
824 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
827 while (p
[len
] && p
[len
] != ':')
832 if (strncmp(p
, "target.xml", len
) == 0) {
833 char *buf
= process
->target_xml
;
834 const size_t buf_sz
= sizeof(process
->target_xml
);
836 /* Generate the XML description for this CPU. */
841 "<?xml version=\"1.0\"?>"
842 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
844 if (cc
->gdb_arch_name
) {
845 gchar
*arch
= cc
->gdb_arch_name(cpu
);
846 pstrcat(buf
, buf_sz
, "<architecture>");
847 pstrcat(buf
, buf_sz
, arch
);
848 pstrcat(buf
, buf_sz
, "</architecture>");
851 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
852 pstrcat(buf
, buf_sz
, cc
->gdb_core_xml_file
);
853 pstrcat(buf
, buf_sz
, "\"/>");
854 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
855 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
856 pstrcat(buf
, buf_sz
, r
->xml
);
857 pstrcat(buf
, buf_sz
, "\"/>");
859 pstrcat(buf
, buf_sz
, "</target>");
863 if (cc
->gdb_get_dynamic_xml
) {
864 char *xmlname
= g_strndup(p
, len
);
865 const char *xml
= cc
->gdb_get_dynamic_xml(cpu
, xmlname
);
873 name
= xml_builtin
[i
][0];
874 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
877 return name
? xml_builtin
[i
][1] : NULL
;
880 static int gdb_read_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
882 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
883 CPUArchState
*env
= cpu
->env_ptr
;
886 if (reg
< cc
->gdb_num_core_regs
) {
887 return cc
->gdb_read_register(cpu
, mem_buf
, reg
);
890 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
891 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
892 return r
->get_reg(env
, mem_buf
, reg
- r
->base_reg
);
898 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
900 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
901 CPUArchState
*env
= cpu
->env_ptr
;
904 if (reg
< cc
->gdb_num_core_regs
) {
905 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
908 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
909 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
910 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
916 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
917 specifies the first register number and these registers are included in
918 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
919 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
922 void gdb_register_coprocessor(CPUState
*cpu
,
923 gdb_reg_cb get_reg
, gdb_reg_cb set_reg
,
924 int num_regs
, const char *xml
, int g_pos
)
927 GDBRegisterState
**p
;
931 /* Check for duplicates. */
932 if (strcmp((*p
)->xml
, xml
) == 0)
937 s
= g_new0(GDBRegisterState
, 1);
938 s
->base_reg
= cpu
->gdb_num_regs
;
939 s
->num_regs
= num_regs
;
940 s
->get_reg
= get_reg
;
941 s
->set_reg
= set_reg
;
944 /* Add to end of list. */
945 cpu
->gdb_num_regs
+= num_regs
;
948 if (g_pos
!= s
->base_reg
) {
949 error_report("Error: Bad gdb register numbering for '%s', "
950 "expected %d got %d", xml
, g_pos
, s
->base_reg
);
952 cpu
->gdb_num_g_regs
= cpu
->gdb_num_regs
;
957 #ifndef CONFIG_USER_ONLY
958 /* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
959 static inline int xlat_gdb_type(CPUState
*cpu
, int gdbtype
)
961 static const int xlat
[] = {
962 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
963 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
964 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
967 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
968 int cputype
= xlat
[gdbtype
];
970 if (cc
->gdb_stop_before_watchpoint
) {
971 cputype
|= BP_STOP_BEFORE_ACCESS
;
977 static int gdb_breakpoint_insert(target_ulong addr
, target_ulong len
, int type
)
983 return kvm_insert_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
987 case GDB_BREAKPOINT_SW
:
988 case GDB_BREAKPOINT_HW
:
990 err
= cpu_breakpoint_insert(cpu
, addr
, BP_GDB
, NULL
);
996 #ifndef CONFIG_USER_ONLY
997 case GDB_WATCHPOINT_WRITE
:
998 case GDB_WATCHPOINT_READ
:
999 case GDB_WATCHPOINT_ACCESS
:
1001 err
= cpu_watchpoint_insert(cpu
, addr
, len
,
1002 xlat_gdb_type(cpu
, type
), NULL
);
1014 static int gdb_breakpoint_remove(target_ulong addr
, target_ulong len
, int type
)
1019 if (kvm_enabled()) {
1020 return kvm_remove_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1024 case GDB_BREAKPOINT_SW
:
1025 case GDB_BREAKPOINT_HW
:
1027 err
= cpu_breakpoint_remove(cpu
, addr
, BP_GDB
);
1033 #ifndef CONFIG_USER_ONLY
1034 case GDB_WATCHPOINT_WRITE
:
1035 case GDB_WATCHPOINT_READ
:
1036 case GDB_WATCHPOINT_ACCESS
:
1038 err
= cpu_watchpoint_remove(cpu
, addr
, len
,
1039 xlat_gdb_type(cpu
, type
));
1050 static inline void gdb_cpu_breakpoint_remove_all(CPUState
*cpu
)
1052 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
1053 #ifndef CONFIG_USER_ONLY
1054 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
1058 static void gdb_process_breakpoint_remove_all(const GDBState
*s
, GDBProcess
*p
)
1060 CPUState
*cpu
= get_first_cpu_in_process(s
, p
);
1063 gdb_cpu_breakpoint_remove_all(cpu
);
1064 cpu
= gdb_next_cpu_in_process(s
, cpu
);
1068 static void gdb_breakpoint_remove_all(void)
1072 if (kvm_enabled()) {
1073 kvm_remove_all_breakpoints(gdbserver_state
->c_cpu
);
1078 gdb_cpu_breakpoint_remove_all(cpu
);
1082 static void gdb_set_cpu_pc(GDBState
*s
, target_ulong pc
)
1084 CPUState
*cpu
= s
->c_cpu
;
1086 cpu_synchronize_state(cpu
);
1087 cpu_set_pc(cpu
, pc
);
1090 static char *gdb_fmt_thread_id(const GDBState
*s
, CPUState
*cpu
,
1091 char *buf
, size_t buf_size
)
1093 if (s
->multiprocess
) {
1094 snprintf(buf
, buf_size
, "p%02x.%02x",
1095 gdb_get_cpu_pid(s
, cpu
), cpu_gdb_index(cpu
));
1097 snprintf(buf
, buf_size
, "%02x", cpu_gdb_index(cpu
));
1103 typedef enum GDBThreadIdKind
{
1105 GDB_ALL_THREADS
, /* One process, all threads */
1110 static GDBThreadIdKind
read_thread_id(const char *buf
, const char **end_buf
,
1111 uint32_t *pid
, uint32_t *tid
)
1118 ret
= qemu_strtoul(buf
, &buf
, 16, &p
);
1121 return GDB_READ_THREAD_ERR
;
1130 ret
= qemu_strtoul(buf
, &buf
, 16, &t
);
1133 return GDB_READ_THREAD_ERR
;
1139 return GDB_ALL_PROCESSES
;
1147 return GDB_ALL_THREADS
;
1154 return GDB_ONE_THREAD
;
1157 static int is_query_packet(const char *p
, const char *query
, char separator
)
1159 unsigned int query_len
= strlen(query
);
1161 return strncmp(p
, query
, query_len
) == 0 &&
1162 (p
[query_len
] == '\0' || p
[query_len
] == separator
);
1166 * gdb_handle_vcont - Parses and handles a vCont packet.
1167 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1168 * a format error, 0 on success.
1170 static int gdb_handle_vcont(GDBState
*s
, const char *p
)
1172 int res
, signal
= 0;
1177 GDBProcess
*process
;
1179 #ifdef CONFIG_USER_ONLY
1180 int max_cpus
= 1; /* global variable max_cpus exists only in system mode */
1183 max_cpus
= max_cpus
<= cpu
->cpu_index
? cpu
->cpu_index
+ 1 : max_cpus
;
1186 /* uninitialised CPUs stay 0 */
1187 newstates
= g_new0(char, max_cpus
);
1189 /* mark valid CPUs with 1 */
1191 newstates
[cpu
->cpu_index
] = 1;
1195 * res keeps track of what error we are returning, with -ENOTSUP meaning
1196 * that the command is unknown or unsupported, thus returning an empty
1197 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1198 * or incorrect parameters passed.
1208 if (cur_action
== 'C' || cur_action
== 'S') {
1209 cur_action
= qemu_tolower(cur_action
);
1210 res
= qemu_strtoul(p
+ 1, &p
, 16, &tmp
);
1214 signal
= gdb_signal_to_target(tmp
);
1215 } else if (cur_action
!= 'c' && cur_action
!= 's') {
1216 /* unknown/invalid/unsupported command */
1226 switch (read_thread_id(p
, &p
, &pid
, &tid
)) {
1227 case GDB_READ_THREAD_ERR
:
1231 case GDB_ALL_PROCESSES
:
1232 cpu
= gdb_first_attached_cpu(s
);
1234 if (newstates
[cpu
->cpu_index
] == 1) {
1235 newstates
[cpu
->cpu_index
] = cur_action
;
1238 cpu
= gdb_next_attached_cpu(s
, cpu
);
1242 case GDB_ALL_THREADS
:
1243 process
= gdb_get_process(s
, pid
);
1245 if (!process
->attached
) {
1250 cpu
= get_first_cpu_in_process(s
, process
);
1252 if (newstates
[cpu
->cpu_index
] == 1) {
1253 newstates
[cpu
->cpu_index
] = cur_action
;
1256 cpu
= gdb_next_cpu_in_process(s
, cpu
);
1260 case GDB_ONE_THREAD
:
1261 cpu
= gdb_get_cpu(s
, pid
, tid
);
1263 /* invalid CPU/thread specified */
1269 /* only use if no previous match occourred */
1270 if (newstates
[cpu
->cpu_index
] == 1) {
1271 newstates
[cpu
->cpu_index
] = cur_action
;
1277 gdb_continue_partial(s
, newstates
);
1285 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
1288 GDBProcess
*process
;
1292 int ch
, reg_size
, type
, res
;
1293 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1294 char buf
[sizeof(mem_buf
) + 1 /* trailing NUL */];
1297 target_ulong addr
, len
;
1298 GDBThreadIdKind thread_kind
;
1300 trace_gdbstub_io_command(line_buf
);
1306 put_packet(s
, "OK");
1309 /* TODO: Make this return the correct value for user-mode. */
1310 snprintf(buf
, sizeof(buf
), "T%02xthread:%s;", GDB_SIGNAL_TRAP
,
1311 gdb_fmt_thread_id(s
, s
->c_cpu
, thread_id
, sizeof(thread_id
)));
1313 /* Remove all the breakpoints when this query is issued,
1314 * because gdb is doing and initial connect and the state
1315 * should be cleaned up.
1317 gdb_breakpoint_remove_all();
1321 addr
= strtoull(p
, (char **)&p
, 16);
1322 gdb_set_cpu_pc(s
, addr
);
1328 s
->signal
= gdb_signal_to_target (strtoul(p
, (char **)&p
, 16));
1329 if (s
->signal
== -1)
1334 if (strncmp(p
, "Cont", 4) == 0) {
1337 put_packet(s
, "vCont;c;C;s;S");
1341 res
= gdb_handle_vcont(s
, p
);
1344 if ((res
== -EINVAL
) || (res
== -ERANGE
)) {
1345 put_packet(s
, "E22");
1348 goto unknown_command
;
1351 } else if (strncmp(p
, "Attach;", 7) == 0) {
1356 if (qemu_strtoul(p
, &p
, 16, &pid
)) {
1357 put_packet(s
, "E22");
1361 process
= gdb_get_process(s
, pid
);
1363 if (process
== NULL
) {
1364 put_packet(s
, "E22");
1368 cpu
= get_first_cpu_in_process(s
, process
);
1371 /* Refuse to attach an empty process */
1372 put_packet(s
, "E22");
1376 process
->attached
= true;
1381 snprintf(buf
, sizeof(buf
), "T%02xthread:%s;", GDB_SIGNAL_TRAP
,
1382 gdb_fmt_thread_id(s
, cpu
, thread_id
, sizeof(thread_id
)));
1387 goto unknown_command
;
1390 /* Kill the target */
1391 error_report("QEMU: Terminated via GDBstub");
1397 if (s
->multiprocess
) {
1400 put_packet(s
, "E22");
1404 if (qemu_strtoul(p
+ 1, &p
, 16, &lpid
)) {
1405 put_packet(s
, "E22");
1412 process
= gdb_get_process(s
, pid
);
1413 gdb_process_breakpoint_remove_all(s
, process
);
1414 process
->attached
= false;
1416 if (pid
== gdb_get_cpu_pid(s
, s
->c_cpu
)) {
1417 s
->c_cpu
= gdb_first_attached_cpu(s
);
1420 if (pid
== gdb_get_cpu_pid(s
, s
->g_cpu
)) {
1421 s
->g_cpu
= gdb_first_attached_cpu(s
);
1424 if (s
->c_cpu
== NULL
) {
1425 /* No more process attached */
1426 gdb_syscall_mode
= GDB_SYS_DISABLED
;
1429 put_packet(s
, "OK");
1433 addr
= strtoull(p
, (char **)&p
, 16);
1434 gdb_set_cpu_pc(s
, addr
);
1436 cpu_single_step(s
->c_cpu
, sstep_flags
);
1444 ret
= strtoull(p
, (char **)&p
, 16);
1447 err
= strtoull(p
, (char **)&p
, 16);
1454 if (s
->current_syscall_cb
) {
1455 s
->current_syscall_cb(s
->c_cpu
, ret
, err
);
1456 s
->current_syscall_cb
= NULL
;
1459 put_packet(s
, "T02");
1466 cpu_synchronize_state(s
->g_cpu
);
1468 for (addr
= 0; addr
< s
->g_cpu
->gdb_num_g_regs
; addr
++) {
1469 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
+ len
, addr
);
1472 memtohex(buf
, mem_buf
, len
);
1476 cpu_synchronize_state(s
->g_cpu
);
1477 registers
= mem_buf
;
1478 len
= strlen(p
) / 2;
1479 hextomem((uint8_t *)registers
, p
, len
);
1480 for (addr
= 0; addr
< s
->g_cpu
->gdb_num_g_regs
&& len
> 0; addr
++) {
1481 reg_size
= gdb_write_register(s
->g_cpu
, registers
, addr
);
1483 registers
+= reg_size
;
1485 put_packet(s
, "OK");
1488 addr
= strtoull(p
, (char **)&p
, 16);
1491 len
= strtoull(p
, NULL
, 16);
1493 /* memtohex() doubles the required space */
1494 if (len
> MAX_PACKET_LENGTH
/ 2) {
1495 put_packet (s
, "E22");
1499 if (target_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, false) != 0) {
1500 put_packet (s
, "E14");
1502 memtohex(buf
, mem_buf
, len
);
1507 addr
= strtoull(p
, (char **)&p
, 16);
1510 len
= strtoull(p
, (char **)&p
, 16);
1514 /* hextomem() reads 2*len bytes */
1515 if (len
> strlen(p
) / 2) {
1516 put_packet (s
, "E22");
1519 hextomem(mem_buf
, p
, len
);
1520 if (target_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
,
1522 put_packet(s
, "E14");
1524 put_packet(s
, "OK");
1528 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1529 This works, but can be very slow. Anything new enough to
1530 understand XML also knows how to use this properly. */
1532 goto unknown_command
;
1533 addr
= strtoull(p
, (char **)&p
, 16);
1534 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
, addr
);
1536 memtohex(buf
, mem_buf
, reg_size
);
1539 put_packet(s
, "E14");
1544 goto unknown_command
;
1545 addr
= strtoull(p
, (char **)&p
, 16);
1548 reg_size
= strlen(p
) / 2;
1549 hextomem(mem_buf
, p
, reg_size
);
1550 gdb_write_register(s
->g_cpu
, mem_buf
, addr
);
1551 put_packet(s
, "OK");
1555 type
= strtoul(p
, (char **)&p
, 16);
1558 addr
= strtoull(p
, (char **)&p
, 16);
1561 len
= strtoull(p
, (char **)&p
, 16);
1563 res
= gdb_breakpoint_insert(addr
, len
, type
);
1565 res
= gdb_breakpoint_remove(addr
, len
, type
);
1567 put_packet(s
, "OK");
1568 else if (res
== -ENOSYS
)
1571 put_packet(s
, "E22");
1576 thread_kind
= read_thread_id(p
, &p
, &pid
, &tid
);
1577 if (thread_kind
== GDB_READ_THREAD_ERR
) {
1578 put_packet(s
, "E22");
1582 if (thread_kind
!= GDB_ONE_THREAD
) {
1583 put_packet(s
, "OK");
1586 cpu
= gdb_get_cpu(s
, pid
, tid
);
1588 put_packet(s
, "E22");
1594 put_packet(s
, "OK");
1598 put_packet(s
, "OK");
1601 put_packet(s
, "E22");
1606 thread_kind
= read_thread_id(p
, &p
, &pid
, &tid
);
1607 if (thread_kind
== GDB_READ_THREAD_ERR
) {
1608 put_packet(s
, "E22");
1611 cpu
= gdb_get_cpu(s
, pid
, tid
);
1614 put_packet(s
, "OK");
1616 put_packet(s
, "E22");
1621 /* parse any 'q' packets here */
1622 if (!strcmp(p
,"qemu.sstepbits")) {
1623 /* Query Breakpoint bit definitions */
1624 snprintf(buf
, sizeof(buf
), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1630 } else if (is_query_packet(p
, "qemu.sstep", '=')) {
1631 /* Display or change the sstep_flags */
1634 /* Display current setting */
1635 snprintf(buf
, sizeof(buf
), "0x%x", sstep_flags
);
1640 type
= strtoul(p
, (char **)&p
, 16);
1642 put_packet(s
, "OK");
1644 } else if (strcmp(p
,"C") == 0) {
1646 * "Current thread" remains vague in the spec, so always return
1647 * the first thread of the current process (gdb returns the
1650 cpu
= get_first_cpu_in_process(s
, gdb_get_cpu_process(s
, s
->g_cpu
));
1651 snprintf(buf
, sizeof(buf
), "QC%s",
1652 gdb_fmt_thread_id(s
, cpu
, thread_id
, sizeof(thread_id
)));
1655 } else if (strcmp(p
,"fThreadInfo") == 0) {
1656 s
->query_cpu
= gdb_first_attached_cpu(s
);
1657 goto report_cpuinfo
;
1658 } else if (strcmp(p
,"sThreadInfo") == 0) {
1661 snprintf(buf
, sizeof(buf
), "m%s",
1662 gdb_fmt_thread_id(s
, s
->query_cpu
,
1663 thread_id
, sizeof(thread_id
)));
1665 s
->query_cpu
= gdb_next_attached_cpu(s
, s
->query_cpu
);
1669 } else if (strncmp(p
,"ThreadExtraInfo,", 16) == 0) {
1670 if (read_thread_id(p
+ 16, &p
, &pid
, &tid
) == GDB_READ_THREAD_ERR
) {
1671 put_packet(s
, "E22");
1674 cpu
= gdb_get_cpu(s
, pid
, tid
);
1676 cpu_synchronize_state(cpu
);
1678 if (s
->multiprocess
&& (s
->process_num
> 1)) {
1679 /* Print the CPU model and name in multiprocess mode */
1680 ObjectClass
*oc
= object_get_class(OBJECT(cpu
));
1681 const char *cpu_model
= object_class_get_name(oc
);
1683 object_get_canonical_path_component(OBJECT(cpu
));
1684 len
= snprintf((char *)mem_buf
, sizeof(buf
) / 2,
1685 "%s %s [%s]", cpu_model
, cpu_name
,
1686 cpu
->halted
? "halted " : "running");
1689 /* memtohex() doubles the required space */
1690 len
= snprintf((char *)mem_buf
, sizeof(buf
) / 2,
1691 "CPU#%d [%s]", cpu
->cpu_index
,
1692 cpu
->halted
? "halted " : "running");
1694 trace_gdbstub_op_extra_info((char *)mem_buf
);
1695 memtohex(buf
, mem_buf
, len
);
1700 #ifdef CONFIG_USER_ONLY
1701 else if (strcmp(p
, "Offsets") == 0) {
1702 TaskState
*ts
= s
->c_cpu
->opaque
;
1704 snprintf(buf
, sizeof(buf
),
1705 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1706 ";Bss=" TARGET_ABI_FMT_lx
,
1707 ts
->info
->code_offset
,
1708 ts
->info
->data_offset
,
1709 ts
->info
->data_offset
);
1713 #else /* !CONFIG_USER_ONLY */
1714 else if (strncmp(p
, "Rcmd,", 5) == 0) {
1715 int len
= strlen(p
+ 5);
1717 if ((len
% 2) != 0) {
1718 put_packet(s
, "E01");
1722 hextomem(mem_buf
, p
+ 5, len
);
1724 qemu_chr_be_write(s
->mon_chr
, mem_buf
, len
);
1725 put_packet(s
, "OK");
1728 #endif /* !CONFIG_USER_ONLY */
1729 if (is_query_packet(p
, "Supported", ':')) {
1730 snprintf(buf
, sizeof(buf
), "PacketSize=%x", MAX_PACKET_LENGTH
);
1731 cc
= CPU_GET_CLASS(first_cpu
);
1732 if (cc
->gdb_core_xml_file
!= NULL
) {
1733 pstrcat(buf
, sizeof(buf
), ";qXfer:features:read+");
1736 if (strstr(p
, "multiprocess+")) {
1737 s
->multiprocess
= true;
1739 pstrcat(buf
, sizeof(buf
), ";multiprocess+");
1744 if (strncmp(p
, "Xfer:features:read:", 19) == 0) {
1746 target_ulong total_len
;
1748 process
= gdb_get_cpu_process(s
, s
->g_cpu
);
1749 cc
= CPU_GET_CLASS(s
->g_cpu
);
1750 if (cc
->gdb_core_xml_file
== NULL
) {
1751 goto unknown_command
;
1756 xml
= get_feature_xml(s
, p
, &p
, process
);
1758 snprintf(buf
, sizeof(buf
), "E00");
1765 addr
= strtoul(p
, (char **)&p
, 16);
1768 len
= strtoul(p
, (char **)&p
, 16);
1770 total_len
= strlen(xml
);
1771 if (addr
> total_len
) {
1772 snprintf(buf
, sizeof(buf
), "E00");
1776 if (len
> (MAX_PACKET_LENGTH
- 5) / 2)
1777 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1778 if (len
< total_len
- addr
) {
1780 len
= memtox(buf
+ 1, xml
+ addr
, len
);
1783 len
= memtox(buf
+ 1, xml
+ addr
, total_len
- addr
);
1785 put_packet_binary(s
, buf
, len
+ 1, true);
1788 if (is_query_packet(p
, "Attached", ':')) {
1789 put_packet(s
, GDB_ATTACHED
);
1792 /* Unrecognised 'q' command. */
1793 goto unknown_command
;
1797 /* put empty packet */
1805 void gdb_set_stop_cpu(CPUState
*cpu
)
1807 GDBProcess
*p
= gdb_get_cpu_process(gdbserver_state
, cpu
);
1811 * Having a stop CPU corresponding to a process that is not attached
1812 * confuses GDB. So we ignore the request.
1817 gdbserver_state
->c_cpu
= cpu
;
1818 gdbserver_state
->g_cpu
= cpu
;
1821 #ifndef CONFIG_USER_ONLY
1822 static void gdb_vm_state_change(void *opaque
, int running
, RunState state
)
1824 GDBState
*s
= gdbserver_state
;
1825 CPUState
*cpu
= s
->c_cpu
;
1831 if (running
|| s
->state
== RS_INACTIVE
) {
1834 /* Is there a GDB syscall waiting to be sent? */
1835 if (s
->current_syscall_cb
) {
1836 put_packet(s
, s
->syscall_buf
);
1841 /* No process attached */
1845 gdb_fmt_thread_id(s
, cpu
, thread_id
, sizeof(thread_id
));
1848 case RUN_STATE_DEBUG
:
1849 if (cpu
->watchpoint_hit
) {
1850 switch (cpu
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
1861 trace_gdbstub_hit_watchpoint(type
, cpu_gdb_index(cpu
),
1862 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
1863 snprintf(buf
, sizeof(buf
),
1864 "T%02xthread:%s;%swatch:" TARGET_FMT_lx
";",
1865 GDB_SIGNAL_TRAP
, thread_id
, type
,
1866 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
1867 cpu
->watchpoint_hit
= NULL
;
1870 trace_gdbstub_hit_break();
1873 ret
= GDB_SIGNAL_TRAP
;
1875 case RUN_STATE_PAUSED
:
1876 trace_gdbstub_hit_paused();
1877 ret
= GDB_SIGNAL_INT
;
1879 case RUN_STATE_SHUTDOWN
:
1880 trace_gdbstub_hit_shutdown();
1881 ret
= GDB_SIGNAL_QUIT
;
1883 case RUN_STATE_IO_ERROR
:
1884 trace_gdbstub_hit_io_error();
1885 ret
= GDB_SIGNAL_IO
;
1887 case RUN_STATE_WATCHDOG
:
1888 trace_gdbstub_hit_watchdog();
1889 ret
= GDB_SIGNAL_ALRM
;
1891 case RUN_STATE_INTERNAL_ERROR
:
1892 trace_gdbstub_hit_internal_error();
1893 ret
= GDB_SIGNAL_ABRT
;
1895 case RUN_STATE_SAVE_VM
:
1896 case RUN_STATE_RESTORE_VM
:
1898 case RUN_STATE_FINISH_MIGRATE
:
1899 ret
= GDB_SIGNAL_XCPU
;
1902 trace_gdbstub_hit_unknown(state
);
1903 ret
= GDB_SIGNAL_UNKNOWN
;
1906 gdb_set_stop_cpu(cpu
);
1907 snprintf(buf
, sizeof(buf
), "T%02xthread:%s;", ret
, thread_id
);
1912 /* disable single step if it was enabled */
1913 cpu_single_step(cpu
, 0);
1917 /* Send a gdb syscall request.
1918 This accepts limited printf-style format specifiers, specifically:
1919 %x - target_ulong argument printed in hex.
1920 %lx - 64-bit argument printed in hex.
1921 %s - string pointer (target_ulong) and length (int) pair. */
1922 void gdb_do_syscallv(gdb_syscall_complete_cb cb
, const char *fmt
, va_list va
)
1930 s
= gdbserver_state
;
1933 s
->current_syscall_cb
= cb
;
1934 #ifndef CONFIG_USER_ONLY
1935 vm_stop(RUN_STATE_DEBUG
);
1938 p_end
= &s
->syscall_buf
[sizeof(s
->syscall_buf
)];
1945 addr
= va_arg(va
, target_ulong
);
1946 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
, addr
);
1949 if (*(fmt
++) != 'x')
1951 i64
= va_arg(va
, uint64_t);
1952 p
+= snprintf(p
, p_end
- p
, "%" PRIx64
, i64
);
1955 addr
= va_arg(va
, target_ulong
);
1956 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
"/%x",
1957 addr
, va_arg(va
, int));
1961 error_report("gdbstub: Bad syscall format string '%s'",
1970 #ifdef CONFIG_USER_ONLY
1971 put_packet(s
, s
->syscall_buf
);
1972 /* Return control to gdb for it to process the syscall request.
1973 * Since the protocol requires that gdb hands control back to us
1974 * using a "here are the results" F packet, we don't need to check
1975 * gdb_handlesig's return value (which is the signal to deliver if
1976 * execution was resumed via a continue packet).
1978 gdb_handlesig(s
->c_cpu
, 0);
1980 /* In this case wait to send the syscall packet until notification that
1981 the CPU has stopped. This must be done because if the packet is sent
1982 now the reply from the syscall request could be received while the CPU
1983 is still in the running state, which can cause packets to be dropped
1984 and state transition 'T' packets to be sent while the syscall is still
1986 qemu_cpu_kick(s
->c_cpu
);
1990 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
1995 gdb_do_syscallv(cb
, fmt
, va
);
1999 static void gdb_read_byte(GDBState
*s
, int ch
)
2003 #ifndef CONFIG_USER_ONLY
2004 if (s
->last_packet_len
) {
2005 /* Waiting for a response to the last packet. If we see the start
2006 of a new command then abandon the previous response. */
2008 trace_gdbstub_err_got_nack();
2009 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
2010 } else if (ch
== '+') {
2011 trace_gdbstub_io_got_ack();
2013 trace_gdbstub_io_got_unexpected((uint8_t)ch
);
2016 if (ch
== '+' || ch
== '$')
2017 s
->last_packet_len
= 0;
2021 if (runstate_is_running()) {
2022 /* when the CPU is running, we cannot do anything except stop
2023 it when receiving a char */
2024 vm_stop(RUN_STATE_PAUSED
);
2031 /* start of command packet */
2032 s
->line_buf_index
= 0;
2034 s
->state
= RS_GETLINE
;
2036 trace_gdbstub_err_garbage((uint8_t)ch
);
2041 /* start escape sequence */
2042 s
->state
= RS_GETLINE_ESC
;
2044 } else if (ch
== '*') {
2045 /* start run length encoding sequence */
2046 s
->state
= RS_GETLINE_RLE
;
2048 } else if (ch
== '#') {
2049 /* end of command, start of checksum*/
2050 s
->state
= RS_CHKSUM1
;
2051 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2052 trace_gdbstub_err_overrun();
2055 /* unescaped command character */
2056 s
->line_buf
[s
->line_buf_index
++] = ch
;
2060 case RS_GETLINE_ESC
:
2062 /* unexpected end of command in escape sequence */
2063 s
->state
= RS_CHKSUM1
;
2064 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2065 /* command buffer overrun */
2066 trace_gdbstub_err_overrun();
2069 /* parse escaped character and leave escape state */
2070 s
->line_buf
[s
->line_buf_index
++] = ch
^ 0x20;
2072 s
->state
= RS_GETLINE
;
2075 case RS_GETLINE_RLE
:
2077 /* invalid RLE count encoding */
2078 trace_gdbstub_err_invalid_repeat((uint8_t)ch
);
2079 s
->state
= RS_GETLINE
;
2081 /* decode repeat length */
2082 int repeat
= (unsigned char)ch
- ' ' + 3;
2083 if (s
->line_buf_index
+ repeat
>= sizeof(s
->line_buf
) - 1) {
2084 /* that many repeats would overrun the command buffer */
2085 trace_gdbstub_err_overrun();
2087 } else if (s
->line_buf_index
< 1) {
2088 /* got a repeat but we have nothing to repeat */
2089 trace_gdbstub_err_invalid_rle();
2090 s
->state
= RS_GETLINE
;
2092 /* repeat the last character */
2093 memset(s
->line_buf
+ s
->line_buf_index
,
2094 s
->line_buf
[s
->line_buf_index
- 1], repeat
);
2095 s
->line_buf_index
+= repeat
;
2097 s
->state
= RS_GETLINE
;
2102 /* get high hex digit of checksum */
2103 if (!isxdigit(ch
)) {
2104 trace_gdbstub_err_checksum_invalid((uint8_t)ch
);
2105 s
->state
= RS_GETLINE
;
2108 s
->line_buf
[s
->line_buf_index
] = '\0';
2109 s
->line_csum
= fromhex(ch
) << 4;
2110 s
->state
= RS_CHKSUM2
;
2113 /* get low hex digit of checksum */
2114 if (!isxdigit(ch
)) {
2115 trace_gdbstub_err_checksum_invalid((uint8_t)ch
);
2116 s
->state
= RS_GETLINE
;
2119 s
->line_csum
|= fromhex(ch
);
2121 if (s
->line_csum
!= (s
->line_sum
& 0xff)) {
2122 trace_gdbstub_err_checksum_incorrect(s
->line_sum
, s
->line_csum
);
2123 /* send NAK reply */
2125 put_buffer(s
, &reply
, 1);
2128 /* send ACK reply */
2130 put_buffer(s
, &reply
, 1);
2131 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
2140 /* Tell the remote gdb that the process has exited. */
2141 void gdb_exit(CPUArchState
*env
, int code
)
2146 s
= gdbserver_state
;
2150 #ifdef CONFIG_USER_ONLY
2151 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2156 trace_gdbstub_op_exiting((uint8_t)code
);
2158 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
2161 #ifndef CONFIG_USER_ONLY
2162 qemu_chr_fe_deinit(&s
->chr
, true);
2167 * Create the process that will contain all the "orphan" CPUs (that are not
2168 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2169 * be attachable and thus will be invisible to the user.
2171 static void create_default_process(GDBState
*s
)
2173 GDBProcess
*process
;
2176 if (s
->process_num
) {
2177 max_pid
= s
->processes
[s
->process_num
- 1].pid
;
2180 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2181 process
= &s
->processes
[s
->process_num
- 1];
2183 /* We need an available PID slot for this process */
2184 assert(max_pid
< UINT32_MAX
);
2186 process
->pid
= max_pid
+ 1;
2187 process
->attached
= false;
2188 process
->target_xml
[0] = '\0';
2191 #ifdef CONFIG_USER_ONLY
2193 gdb_handlesig(CPUState
*cpu
, int sig
)
2199 s
= gdbserver_state
;
2200 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2204 /* disable single step if it was enabled */
2205 cpu_single_step(cpu
, 0);
2209 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb(sig
));
2212 /* put_packet() might have detected that the peer terminated the
2220 s
->running_state
= 0;
2221 while (s
->running_state
== 0) {
2222 n
= read(s
->fd
, buf
, 256);
2226 for (i
= 0; i
< n
; i
++) {
2227 gdb_read_byte(s
, buf
[i
]);
2230 /* XXX: Connection closed. Should probably wait for another
2231 connection before continuing. */
2244 /* Tell the remote gdb that the process has exited due to SIG. */
2245 void gdb_signalled(CPUArchState
*env
, int sig
)
2250 s
= gdbserver_state
;
2251 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2255 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb(sig
));
2259 static bool gdb_accept(void)
2262 struct sockaddr_in sockaddr
;
2267 len
= sizeof(sockaddr
);
2268 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
2269 if (fd
< 0 && errno
!= EINTR
) {
2272 } else if (fd
>= 0) {
2273 qemu_set_cloexec(fd
);
2278 /* set short latency */
2279 if (socket_set_nodelay(fd
)) {
2280 perror("setsockopt");
2285 s
= g_malloc0(sizeof(GDBState
));
2286 create_default_process(s
);
2287 s
->processes
[0].attached
= true;
2288 s
->c_cpu
= gdb_first_attached_cpu(s
);
2289 s
->g_cpu
= s
->c_cpu
;
2291 gdb_has_xml
= false;
2293 gdbserver_state
= s
;
2297 static int gdbserver_open(int port
)
2299 struct sockaddr_in sockaddr
;
2302 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2307 qemu_set_cloexec(fd
);
2309 socket_set_fast_reuse(fd
);
2311 sockaddr
.sin_family
= AF_INET
;
2312 sockaddr
.sin_port
= htons(port
);
2313 sockaddr
.sin_addr
.s_addr
= 0;
2314 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
2320 ret
= listen(fd
, 1);
2329 int gdbserver_start(int port
)
2331 gdbserver_fd
= gdbserver_open(port
);
2332 if (gdbserver_fd
< 0)
2334 /* accept connections */
2335 if (!gdb_accept()) {
2336 close(gdbserver_fd
);
2343 /* Disable gdb stub for child processes. */
2344 void gdbserver_fork(CPUState
*cpu
)
2346 GDBState
*s
= gdbserver_state
;
2348 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2353 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
2354 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
2357 static int gdb_chr_can_receive(void *opaque
)
2359 /* We can handle an arbitrarily large amount of data.
2360 Pick the maximum packet size, which is as good as anything. */
2361 return MAX_PACKET_LENGTH
;
2364 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
2368 for (i
= 0; i
< size
; i
++) {
2369 gdb_read_byte(gdbserver_state
, buf
[i
]);
2373 static void gdb_chr_event(void *opaque
, int event
)
2376 GDBState
*s
= (GDBState
*) opaque
;
2379 case CHR_EVENT_OPENED
:
2380 /* Start with first process attached, others detached */
2381 for (i
= 0; i
< s
->process_num
; i
++) {
2382 s
->processes
[i
].attached
= !i
;
2385 s
->c_cpu
= gdb_first_attached_cpu(s
);
2386 s
->g_cpu
= s
->c_cpu
;
2388 vm_stop(RUN_STATE_PAUSED
);
2389 gdb_has_xml
= false;
2396 static void gdb_monitor_output(GDBState
*s
, const char *msg
, int len
)
2398 char buf
[MAX_PACKET_LENGTH
];
2401 if (len
> (MAX_PACKET_LENGTH
/2) - 1)
2402 len
= (MAX_PACKET_LENGTH
/2) - 1;
2403 memtohex(buf
+ 1, (uint8_t *)msg
, len
);
2407 static int gdb_monitor_write(Chardev
*chr
, const uint8_t *buf
, int len
)
2409 const char *p
= (const char *)buf
;
2412 max_sz
= (sizeof(gdbserver_state
->last_packet
) - 2) / 2;
2414 if (len
<= max_sz
) {
2415 gdb_monitor_output(gdbserver_state
, p
, len
);
2418 gdb_monitor_output(gdbserver_state
, p
, max_sz
);
2426 static void gdb_sigterm_handler(int signal
)
2428 if (runstate_is_running()) {
2429 vm_stop(RUN_STATE_PAUSED
);
2434 static void gdb_monitor_open(Chardev
*chr
, ChardevBackend
*backend
,
2435 bool *be_opened
, Error
**errp
)
2440 static void char_gdb_class_init(ObjectClass
*oc
, void *data
)
2442 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
2444 cc
->internal
= true;
2445 cc
->open
= gdb_monitor_open
;
2446 cc
->chr_write
= gdb_monitor_write
;
2449 #define TYPE_CHARDEV_GDB "chardev-gdb"
2451 static const TypeInfo char_gdb_type_info
= {
2452 .name
= TYPE_CHARDEV_GDB
,
2453 .parent
= TYPE_CHARDEV
,
2454 .class_init
= char_gdb_class_init
,
2457 static int find_cpu_clusters(Object
*child
, void *opaque
)
2459 if (object_dynamic_cast(child
, TYPE_CPU_CLUSTER
)) {
2460 GDBState
*s
= (GDBState
*) opaque
;
2461 CPUClusterState
*cluster
= CPU_CLUSTER(child
);
2462 GDBProcess
*process
;
2464 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2466 process
= &s
->processes
[s
->process_num
- 1];
2469 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
2470 * runtime, we enforce here that the machine does not use a cluster ID
2471 * that would lead to PID 0.
2473 assert(cluster
->cluster_id
!= UINT32_MAX
);
2474 process
->pid
= cluster
->cluster_id
+ 1;
2475 process
->attached
= false;
2476 process
->target_xml
[0] = '\0';
2481 return object_child_foreach(child
, find_cpu_clusters
, opaque
);
2484 static int pid_order(const void *a
, const void *b
)
2486 GDBProcess
*pa
= (GDBProcess
*) a
;
2487 GDBProcess
*pb
= (GDBProcess
*) b
;
2489 if (pa
->pid
< pb
->pid
) {
2491 } else if (pa
->pid
> pb
->pid
) {
2498 static void create_processes(GDBState
*s
)
2500 object_child_foreach(object_get_root(), find_cpu_clusters
, s
);
2504 qsort(s
->processes
, s
->process_num
, sizeof(s
->processes
[0]), pid_order
);
2507 create_default_process(s
);
2510 static void cleanup_processes(GDBState
*s
)
2512 g_free(s
->processes
);
2514 s
->processes
= NULL
;
2517 int gdbserver_start(const char *device
)
2519 trace_gdbstub_op_start(device
);
2522 char gdbstub_device_name
[128];
2523 Chardev
*chr
= NULL
;
2527 error_report("gdbstub: meaningless to attach gdb to a "
2528 "machine without any CPU.");
2534 if (strcmp(device
, "none") != 0) {
2535 if (strstart(device
, "tcp:", NULL
)) {
2536 /* enforce required TCP attributes */
2537 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
2538 "%s,nowait,nodelay,server", device
);
2539 device
= gdbstub_device_name
;
2542 else if (strcmp(device
, "stdio") == 0) {
2543 struct sigaction act
;
2545 memset(&act
, 0, sizeof(act
));
2546 act
.sa_handler
= gdb_sigterm_handler
;
2547 sigaction(SIGINT
, &act
, NULL
);
2551 * FIXME: it's a bit weird to allow using a mux chardev here
2552 * and implicitly setup a monitor. We may want to break this.
2554 chr
= qemu_chr_new_noreplay("gdb", device
, true);
2559 s
= gdbserver_state
;
2561 s
= g_malloc0(sizeof(GDBState
));
2562 gdbserver_state
= s
;
2564 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
2566 /* Initialize a monitor terminal for gdb */
2567 mon_chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_GDB
,
2568 NULL
, &error_abort
);
2569 monitor_init(mon_chr
, 0);
2571 qemu_chr_fe_deinit(&s
->chr
, true);
2572 mon_chr
= s
->mon_chr
;
2573 cleanup_processes(s
);
2574 memset(s
, 0, sizeof(GDBState
));
2575 s
->mon_chr
= mon_chr
;
2578 create_processes(s
);
2581 qemu_chr_fe_init(&s
->chr
, chr
, &error_abort
);
2582 qemu_chr_fe_set_handlers(&s
->chr
, gdb_chr_can_receive
, gdb_chr_receive
,
2583 gdb_chr_event
, NULL
, s
, NULL
, true);
2585 s
->state
= chr
? RS_IDLE
: RS_INACTIVE
;
2586 s
->mon_chr
= mon_chr
;
2587 s
->current_syscall_cb
= NULL
;
2592 void gdbserver_cleanup(void)
2594 if (gdbserver_state
) {
2595 put_packet(gdbserver_state
, "W00");
2599 static void register_types(void)
2601 type_register_static(&char_gdb_type_info
);
2604 type_init(register_types
);