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+");
1738 if (strncmp(p
, "Xfer:features:read:", 19) == 0) {
1740 target_ulong total_len
;
1742 process
= gdb_get_cpu_process(s
, s
->g_cpu
);
1743 cc
= CPU_GET_CLASS(s
->g_cpu
);
1744 if (cc
->gdb_core_xml_file
== NULL
) {
1745 goto unknown_command
;
1750 xml
= get_feature_xml(s
, p
, &p
, process
);
1752 snprintf(buf
, sizeof(buf
), "E00");
1759 addr
= strtoul(p
, (char **)&p
, 16);
1762 len
= strtoul(p
, (char **)&p
, 16);
1764 total_len
= strlen(xml
);
1765 if (addr
> total_len
) {
1766 snprintf(buf
, sizeof(buf
), "E00");
1770 if (len
> (MAX_PACKET_LENGTH
- 5) / 2)
1771 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1772 if (len
< total_len
- addr
) {
1774 len
= memtox(buf
+ 1, xml
+ addr
, len
);
1777 len
= memtox(buf
+ 1, xml
+ addr
, total_len
- addr
);
1779 put_packet_binary(s
, buf
, len
+ 1, true);
1782 if (is_query_packet(p
, "Attached", ':')) {
1783 put_packet(s
, GDB_ATTACHED
);
1786 /* Unrecognised 'q' command. */
1787 goto unknown_command
;
1791 /* put empty packet */
1799 void gdb_set_stop_cpu(CPUState
*cpu
)
1801 GDBProcess
*p
= gdb_get_cpu_process(gdbserver_state
, cpu
);
1805 * Having a stop CPU corresponding to a process that is not attached
1806 * confuses GDB. So we ignore the request.
1811 gdbserver_state
->c_cpu
= cpu
;
1812 gdbserver_state
->g_cpu
= cpu
;
1815 #ifndef CONFIG_USER_ONLY
1816 static void gdb_vm_state_change(void *opaque
, int running
, RunState state
)
1818 GDBState
*s
= gdbserver_state
;
1819 CPUState
*cpu
= s
->c_cpu
;
1825 if (running
|| s
->state
== RS_INACTIVE
) {
1828 /* Is there a GDB syscall waiting to be sent? */
1829 if (s
->current_syscall_cb
) {
1830 put_packet(s
, s
->syscall_buf
);
1835 /* No process attached */
1839 gdb_fmt_thread_id(s
, cpu
, thread_id
, sizeof(thread_id
));
1842 case RUN_STATE_DEBUG
:
1843 if (cpu
->watchpoint_hit
) {
1844 switch (cpu
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
1855 trace_gdbstub_hit_watchpoint(type
, cpu_gdb_index(cpu
),
1856 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
1857 snprintf(buf
, sizeof(buf
),
1858 "T%02xthread:%s;%swatch:" TARGET_FMT_lx
";",
1859 GDB_SIGNAL_TRAP
, thread_id
, type
,
1860 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
1861 cpu
->watchpoint_hit
= NULL
;
1864 trace_gdbstub_hit_break();
1867 ret
= GDB_SIGNAL_TRAP
;
1869 case RUN_STATE_PAUSED
:
1870 trace_gdbstub_hit_paused();
1871 ret
= GDB_SIGNAL_INT
;
1873 case RUN_STATE_SHUTDOWN
:
1874 trace_gdbstub_hit_shutdown();
1875 ret
= GDB_SIGNAL_QUIT
;
1877 case RUN_STATE_IO_ERROR
:
1878 trace_gdbstub_hit_io_error();
1879 ret
= GDB_SIGNAL_IO
;
1881 case RUN_STATE_WATCHDOG
:
1882 trace_gdbstub_hit_watchdog();
1883 ret
= GDB_SIGNAL_ALRM
;
1885 case RUN_STATE_INTERNAL_ERROR
:
1886 trace_gdbstub_hit_internal_error();
1887 ret
= GDB_SIGNAL_ABRT
;
1889 case RUN_STATE_SAVE_VM
:
1890 case RUN_STATE_RESTORE_VM
:
1892 case RUN_STATE_FINISH_MIGRATE
:
1893 ret
= GDB_SIGNAL_XCPU
;
1896 trace_gdbstub_hit_unknown(state
);
1897 ret
= GDB_SIGNAL_UNKNOWN
;
1900 gdb_set_stop_cpu(cpu
);
1901 snprintf(buf
, sizeof(buf
), "T%02xthread:%s;", ret
, thread_id
);
1906 /* disable single step if it was enabled */
1907 cpu_single_step(cpu
, 0);
1911 /* Send a gdb syscall request.
1912 This accepts limited printf-style format specifiers, specifically:
1913 %x - target_ulong argument printed in hex.
1914 %lx - 64-bit argument printed in hex.
1915 %s - string pointer (target_ulong) and length (int) pair. */
1916 void gdb_do_syscallv(gdb_syscall_complete_cb cb
, const char *fmt
, va_list va
)
1924 s
= gdbserver_state
;
1927 s
->current_syscall_cb
= cb
;
1928 #ifndef CONFIG_USER_ONLY
1929 vm_stop(RUN_STATE_DEBUG
);
1932 p_end
= &s
->syscall_buf
[sizeof(s
->syscall_buf
)];
1939 addr
= va_arg(va
, target_ulong
);
1940 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
, addr
);
1943 if (*(fmt
++) != 'x')
1945 i64
= va_arg(va
, uint64_t);
1946 p
+= snprintf(p
, p_end
- p
, "%" PRIx64
, i64
);
1949 addr
= va_arg(va
, target_ulong
);
1950 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
"/%x",
1951 addr
, va_arg(va
, int));
1955 error_report("gdbstub: Bad syscall format string '%s'",
1964 #ifdef CONFIG_USER_ONLY
1965 put_packet(s
, s
->syscall_buf
);
1966 /* Return control to gdb for it to process the syscall request.
1967 * Since the protocol requires that gdb hands control back to us
1968 * using a "here are the results" F packet, we don't need to check
1969 * gdb_handlesig's return value (which is the signal to deliver if
1970 * execution was resumed via a continue packet).
1972 gdb_handlesig(s
->c_cpu
, 0);
1974 /* In this case wait to send the syscall packet until notification that
1975 the CPU has stopped. This must be done because if the packet is sent
1976 now the reply from the syscall request could be received while the CPU
1977 is still in the running state, which can cause packets to be dropped
1978 and state transition 'T' packets to be sent while the syscall is still
1980 qemu_cpu_kick(s
->c_cpu
);
1984 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
1989 gdb_do_syscallv(cb
, fmt
, va
);
1993 static void gdb_read_byte(GDBState
*s
, int ch
)
1997 #ifndef CONFIG_USER_ONLY
1998 if (s
->last_packet_len
) {
1999 /* Waiting for a response to the last packet. If we see the start
2000 of a new command then abandon the previous response. */
2002 trace_gdbstub_err_got_nack();
2003 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
2004 } else if (ch
== '+') {
2005 trace_gdbstub_io_got_ack();
2007 trace_gdbstub_io_got_unexpected((uint8_t)ch
);
2010 if (ch
== '+' || ch
== '$')
2011 s
->last_packet_len
= 0;
2015 if (runstate_is_running()) {
2016 /* when the CPU is running, we cannot do anything except stop
2017 it when receiving a char */
2018 vm_stop(RUN_STATE_PAUSED
);
2025 /* start of command packet */
2026 s
->line_buf_index
= 0;
2028 s
->state
= RS_GETLINE
;
2030 trace_gdbstub_err_garbage((uint8_t)ch
);
2035 /* start escape sequence */
2036 s
->state
= RS_GETLINE_ESC
;
2038 } else if (ch
== '*') {
2039 /* start run length encoding sequence */
2040 s
->state
= RS_GETLINE_RLE
;
2042 } else if (ch
== '#') {
2043 /* end of command, start of checksum*/
2044 s
->state
= RS_CHKSUM1
;
2045 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2046 trace_gdbstub_err_overrun();
2049 /* unescaped command character */
2050 s
->line_buf
[s
->line_buf_index
++] = ch
;
2054 case RS_GETLINE_ESC
:
2056 /* unexpected end of command in escape sequence */
2057 s
->state
= RS_CHKSUM1
;
2058 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2059 /* command buffer overrun */
2060 trace_gdbstub_err_overrun();
2063 /* parse escaped character and leave escape state */
2064 s
->line_buf
[s
->line_buf_index
++] = ch
^ 0x20;
2066 s
->state
= RS_GETLINE
;
2069 case RS_GETLINE_RLE
:
2071 /* invalid RLE count encoding */
2072 trace_gdbstub_err_invalid_repeat((uint8_t)ch
);
2073 s
->state
= RS_GETLINE
;
2075 /* decode repeat length */
2076 int repeat
= (unsigned char)ch
- ' ' + 3;
2077 if (s
->line_buf_index
+ repeat
>= sizeof(s
->line_buf
) - 1) {
2078 /* that many repeats would overrun the command buffer */
2079 trace_gdbstub_err_overrun();
2081 } else if (s
->line_buf_index
< 1) {
2082 /* got a repeat but we have nothing to repeat */
2083 trace_gdbstub_err_invalid_rle();
2084 s
->state
= RS_GETLINE
;
2086 /* repeat the last character */
2087 memset(s
->line_buf
+ s
->line_buf_index
,
2088 s
->line_buf
[s
->line_buf_index
- 1], repeat
);
2089 s
->line_buf_index
+= repeat
;
2091 s
->state
= RS_GETLINE
;
2096 /* get high hex digit of checksum */
2097 if (!isxdigit(ch
)) {
2098 trace_gdbstub_err_checksum_invalid((uint8_t)ch
);
2099 s
->state
= RS_GETLINE
;
2102 s
->line_buf
[s
->line_buf_index
] = '\0';
2103 s
->line_csum
= fromhex(ch
) << 4;
2104 s
->state
= RS_CHKSUM2
;
2107 /* get low hex digit of checksum */
2108 if (!isxdigit(ch
)) {
2109 trace_gdbstub_err_checksum_invalid((uint8_t)ch
);
2110 s
->state
= RS_GETLINE
;
2113 s
->line_csum
|= fromhex(ch
);
2115 if (s
->line_csum
!= (s
->line_sum
& 0xff)) {
2116 trace_gdbstub_err_checksum_incorrect(s
->line_sum
, s
->line_csum
);
2117 /* send NAK reply */
2119 put_buffer(s
, &reply
, 1);
2122 /* send ACK reply */
2124 put_buffer(s
, &reply
, 1);
2125 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
2134 /* Tell the remote gdb that the process has exited. */
2135 void gdb_exit(CPUArchState
*env
, int code
)
2140 s
= gdbserver_state
;
2144 #ifdef CONFIG_USER_ONLY
2145 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2150 trace_gdbstub_op_exiting((uint8_t)code
);
2152 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
2155 #ifndef CONFIG_USER_ONLY
2156 qemu_chr_fe_deinit(&s
->chr
, true);
2161 * Create the process that will contain all the "orphan" CPUs (that are not
2162 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2163 * be attachable and thus will be invisible to the user.
2165 static void create_default_process(GDBState
*s
)
2167 GDBProcess
*process
;
2170 if (s
->process_num
) {
2171 max_pid
= s
->processes
[s
->process_num
- 1].pid
;
2174 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2175 process
= &s
->processes
[s
->process_num
- 1];
2177 /* We need an available PID slot for this process */
2178 assert(max_pid
< UINT32_MAX
);
2180 process
->pid
= max_pid
+ 1;
2181 process
->attached
= false;
2182 process
->target_xml
[0] = '\0';
2185 #ifdef CONFIG_USER_ONLY
2187 gdb_handlesig(CPUState
*cpu
, int sig
)
2193 s
= gdbserver_state
;
2194 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2198 /* disable single step if it was enabled */
2199 cpu_single_step(cpu
, 0);
2203 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb(sig
));
2206 /* put_packet() might have detected that the peer terminated the
2214 s
->running_state
= 0;
2215 while (s
->running_state
== 0) {
2216 n
= read(s
->fd
, buf
, 256);
2220 for (i
= 0; i
< n
; i
++) {
2221 gdb_read_byte(s
, buf
[i
]);
2224 /* XXX: Connection closed. Should probably wait for another
2225 connection before continuing. */
2238 /* Tell the remote gdb that the process has exited due to SIG. */
2239 void gdb_signalled(CPUArchState
*env
, int sig
)
2244 s
= gdbserver_state
;
2245 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2249 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb(sig
));
2253 static bool gdb_accept(void)
2256 struct sockaddr_in sockaddr
;
2261 len
= sizeof(sockaddr
);
2262 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
2263 if (fd
< 0 && errno
!= EINTR
) {
2266 } else if (fd
>= 0) {
2267 qemu_set_cloexec(fd
);
2272 /* set short latency */
2273 if (socket_set_nodelay(fd
)) {
2274 perror("setsockopt");
2279 s
= g_malloc0(sizeof(GDBState
));
2280 create_default_process(s
);
2281 s
->processes
[0].attached
= true;
2282 s
->c_cpu
= gdb_first_attached_cpu(s
);
2283 s
->g_cpu
= s
->c_cpu
;
2285 gdb_has_xml
= false;
2287 gdbserver_state
= s
;
2291 static int gdbserver_open(int port
)
2293 struct sockaddr_in sockaddr
;
2296 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2301 qemu_set_cloexec(fd
);
2303 socket_set_fast_reuse(fd
);
2305 sockaddr
.sin_family
= AF_INET
;
2306 sockaddr
.sin_port
= htons(port
);
2307 sockaddr
.sin_addr
.s_addr
= 0;
2308 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
2314 ret
= listen(fd
, 1);
2323 int gdbserver_start(int port
)
2325 gdbserver_fd
= gdbserver_open(port
);
2326 if (gdbserver_fd
< 0)
2328 /* accept connections */
2329 if (!gdb_accept()) {
2330 close(gdbserver_fd
);
2337 /* Disable gdb stub for child processes. */
2338 void gdbserver_fork(CPUState
*cpu
)
2340 GDBState
*s
= gdbserver_state
;
2342 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2347 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
2348 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
2351 static int gdb_chr_can_receive(void *opaque
)
2353 /* We can handle an arbitrarily large amount of data.
2354 Pick the maximum packet size, which is as good as anything. */
2355 return MAX_PACKET_LENGTH
;
2358 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
2362 for (i
= 0; i
< size
; i
++) {
2363 gdb_read_byte(gdbserver_state
, buf
[i
]);
2367 static void gdb_chr_event(void *opaque
, int event
)
2370 GDBState
*s
= (GDBState
*) opaque
;
2373 case CHR_EVENT_OPENED
:
2374 /* Start with first process attached, others detached */
2375 for (i
= 0; i
< s
->process_num
; i
++) {
2376 s
->processes
[i
].attached
= !i
;
2379 s
->c_cpu
= gdb_first_attached_cpu(s
);
2380 s
->g_cpu
= s
->c_cpu
;
2382 vm_stop(RUN_STATE_PAUSED
);
2383 gdb_has_xml
= false;
2390 static void gdb_monitor_output(GDBState
*s
, const char *msg
, int len
)
2392 char buf
[MAX_PACKET_LENGTH
];
2395 if (len
> (MAX_PACKET_LENGTH
/2) - 1)
2396 len
= (MAX_PACKET_LENGTH
/2) - 1;
2397 memtohex(buf
+ 1, (uint8_t *)msg
, len
);
2401 static int gdb_monitor_write(Chardev
*chr
, const uint8_t *buf
, int len
)
2403 const char *p
= (const char *)buf
;
2406 max_sz
= (sizeof(gdbserver_state
->last_packet
) - 2) / 2;
2408 if (len
<= max_sz
) {
2409 gdb_monitor_output(gdbserver_state
, p
, len
);
2412 gdb_monitor_output(gdbserver_state
, p
, max_sz
);
2420 static void gdb_sigterm_handler(int signal
)
2422 if (runstate_is_running()) {
2423 vm_stop(RUN_STATE_PAUSED
);
2428 static void gdb_monitor_open(Chardev
*chr
, ChardevBackend
*backend
,
2429 bool *be_opened
, Error
**errp
)
2434 static void char_gdb_class_init(ObjectClass
*oc
, void *data
)
2436 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
2438 cc
->internal
= true;
2439 cc
->open
= gdb_monitor_open
;
2440 cc
->chr_write
= gdb_monitor_write
;
2443 #define TYPE_CHARDEV_GDB "chardev-gdb"
2445 static const TypeInfo char_gdb_type_info
= {
2446 .name
= TYPE_CHARDEV_GDB
,
2447 .parent
= TYPE_CHARDEV
,
2448 .class_init
= char_gdb_class_init
,
2451 static int find_cpu_clusters(Object
*child
, void *opaque
)
2453 if (object_dynamic_cast(child
, TYPE_CPU_CLUSTER
)) {
2454 GDBState
*s
= (GDBState
*) opaque
;
2455 CPUClusterState
*cluster
= CPU_CLUSTER(child
);
2456 GDBProcess
*process
;
2458 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2460 process
= &s
->processes
[s
->process_num
- 1];
2463 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
2464 * runtime, we enforce here that the machine does not use a cluster ID
2465 * that would lead to PID 0.
2467 assert(cluster
->cluster_id
!= UINT32_MAX
);
2468 process
->pid
= cluster
->cluster_id
+ 1;
2469 process
->attached
= false;
2470 process
->target_xml
[0] = '\0';
2475 return object_child_foreach(child
, find_cpu_clusters
, opaque
);
2478 static int pid_order(const void *a
, const void *b
)
2480 GDBProcess
*pa
= (GDBProcess
*) a
;
2481 GDBProcess
*pb
= (GDBProcess
*) b
;
2483 if (pa
->pid
< pb
->pid
) {
2485 } else if (pa
->pid
> pb
->pid
) {
2492 static void create_processes(GDBState
*s
)
2494 object_child_foreach(object_get_root(), find_cpu_clusters
, s
);
2498 qsort(s
->processes
, s
->process_num
, sizeof(s
->processes
[0]), pid_order
);
2501 create_default_process(s
);
2504 static void cleanup_processes(GDBState
*s
)
2506 g_free(s
->processes
);
2508 s
->processes
= NULL
;
2511 int gdbserver_start(const char *device
)
2513 trace_gdbstub_op_start(device
);
2516 char gdbstub_device_name
[128];
2517 Chardev
*chr
= NULL
;
2521 error_report("gdbstub: meaningless to attach gdb to a "
2522 "machine without any CPU.");
2528 if (strcmp(device
, "none") != 0) {
2529 if (strstart(device
, "tcp:", NULL
)) {
2530 /* enforce required TCP attributes */
2531 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
2532 "%s,nowait,nodelay,server", device
);
2533 device
= gdbstub_device_name
;
2536 else if (strcmp(device
, "stdio") == 0) {
2537 struct sigaction act
;
2539 memset(&act
, 0, sizeof(act
));
2540 act
.sa_handler
= gdb_sigterm_handler
;
2541 sigaction(SIGINT
, &act
, NULL
);
2545 * FIXME: it's a bit weird to allow using a mux chardev here
2546 * and implicitly setup a monitor. We may want to break this.
2548 chr
= qemu_chr_new_noreplay("gdb", device
, true);
2553 s
= gdbserver_state
;
2555 s
= g_malloc0(sizeof(GDBState
));
2556 gdbserver_state
= s
;
2558 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
2560 /* Initialize a monitor terminal for gdb */
2561 mon_chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_GDB
,
2562 NULL
, &error_abort
);
2563 monitor_init(mon_chr
, 0);
2565 qemu_chr_fe_deinit(&s
->chr
, true);
2566 mon_chr
= s
->mon_chr
;
2567 cleanup_processes(s
);
2568 memset(s
, 0, sizeof(GDBState
));
2569 s
->mon_chr
= mon_chr
;
2572 create_processes(s
);
2575 qemu_chr_fe_init(&s
->chr
, chr
, &error_abort
);
2576 qemu_chr_fe_set_handlers(&s
->chr
, gdb_chr_can_receive
, gdb_chr_receive
,
2577 gdb_chr_event
, NULL
, s
, NULL
, true);
2579 s
->state
= chr
? RS_IDLE
: RS_INACTIVE
;
2580 s
->mon_chr
= mon_chr
;
2581 s
->current_syscall_cb
= NULL
;
2586 void gdbserver_cleanup(void)
2588 if (gdbserver_state
) {
2589 put_packet(gdbserver_state
, "W00");
2593 static void register_types(void)
2595 type_register_static(&char_gdb_type_info
);
2598 type_init(register_types
);