4 * Copyright (c) 2003-2005 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "qemu-common.h"
22 #include "qapi/error.h"
23 #include "qemu/error-report.h"
24 #include "qemu/ctype.h"
25 #include "qemu/cutils.h"
26 #include "qemu/module.h"
27 #include "trace-root.h"
28 #ifdef CONFIG_USER_ONLY
31 #include "monitor/monitor.h"
32 #include "chardev/char.h"
33 #include "chardev/char-fe.h"
34 #include "sysemu/sysemu.h"
35 #include "exec/gdbstub.h"
36 #include "hw/cpu/cluster.h"
39 #define MAX_PACKET_LENGTH 4096
41 #include "qemu/sockets.h"
42 #include "sysemu/hw_accel.h"
43 #include "sysemu/kvm.h"
44 #include "hw/semihosting/semihost.h"
45 #include "exec/exec-all.h"
47 #ifdef CONFIG_USER_ONLY
48 #define GDB_ATTACHED "0"
50 #define GDB_ATTACHED "1"
53 static inline int target_memory_rw_debug(CPUState
*cpu
, target_ulong addr
,
54 uint8_t *buf
, int len
, bool is_write
)
56 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
58 if (cc
->memory_rw_debug
) {
59 return cc
->memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
61 return cpu_memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
64 /* Return the GDB index for a given vCPU state.
66 * For user mode this is simply the thread id. In system mode GDB
67 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
69 static inline int cpu_gdb_index(CPUState
*cpu
)
71 #if defined(CONFIG_USER_ONLY)
72 TaskState
*ts
= (TaskState
*) cpu
->opaque
;
75 return cpu
->cpu_index
+ 1;
88 GDB_SIGNAL_UNKNOWN
= 143
91 #ifdef CONFIG_USER_ONLY
93 /* Map target signal numbers to GDB protocol signal numbers and vice
94 * versa. For user emulation's currently supported systems, we can
95 * assume most signals are defined.
98 static int gdb_signal_table
[] = {
258 /* In system mode we only need SIGINT and SIGTRAP; other signals
259 are not yet supported. */
266 static int gdb_signal_table
[] = {
276 #ifdef CONFIG_USER_ONLY
277 static int target_signal_to_gdb (int sig
)
280 for (i
= 0; i
< ARRAY_SIZE (gdb_signal_table
); i
++)
281 if (gdb_signal_table
[i
] == sig
)
283 return GDB_SIGNAL_UNKNOWN
;
287 static int gdb_signal_to_target (int sig
)
289 if (sig
< ARRAY_SIZE (gdb_signal_table
))
290 return gdb_signal_table
[sig
];
295 typedef struct GDBRegisterState
{
301 struct GDBRegisterState
*next
;
304 typedef struct GDBProcess
{
308 char target_xml
[1024];
320 typedef struct GDBState
{
321 CPUState
*c_cpu
; /* current CPU for step/continue ops */
322 CPUState
*g_cpu
; /* current CPU for other ops */
323 CPUState
*query_cpu
; /* for q{f|s}ThreadInfo */
324 enum RSState state
; /* parsing state */
325 char line_buf
[MAX_PACKET_LENGTH
];
327 int line_sum
; /* running checksum */
328 int line_csum
; /* checksum at the end of the packet */
329 uint8_t last_packet
[MAX_PACKET_LENGTH
+ 4];
332 #ifdef CONFIG_USER_ONLY
340 GDBProcess
*processes
;
342 char syscall_buf
[256];
343 gdb_syscall_complete_cb current_syscall_cb
;
346 /* By default use no IRQs and no timers while single stepping so as to
347 * make single stepping like an ICE HW step.
349 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
351 static GDBState
*gdbserver_state
;
355 #ifdef CONFIG_USER_ONLY
356 /* XXX: This is not thread safe. Do we care? */
357 static int gdbserver_fd
= -1;
359 static int get_char(GDBState
*s
)
365 ret
= qemu_recv(s
->fd
, &ch
, 1, 0);
367 if (errno
== ECONNRESET
)
371 } else if (ret
== 0) {
389 /* Decide if either remote gdb syscalls or native file IO should be used. */
390 int use_gdb_syscalls(void)
392 SemihostingTarget target
= semihosting_get_target();
393 if (target
== SEMIHOSTING_TARGET_NATIVE
) {
394 /* -semihosting-config target=native */
396 } else if (target
== SEMIHOSTING_TARGET_GDB
) {
397 /* -semihosting-config target=gdb */
401 /* -semihosting-config target=auto */
402 /* On the first call check if gdb is connected and remember. */
403 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
404 gdb_syscall_mode
= (gdbserver_state
? GDB_SYS_ENABLED
407 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
410 /* Resume execution. */
411 static inline void gdb_continue(GDBState
*s
)
414 #ifdef CONFIG_USER_ONLY
415 s
->running_state
= 1;
416 trace_gdbstub_op_continue();
418 if (!runstate_needs_reset()) {
419 trace_gdbstub_op_continue();
426 * Resume execution, per CPU actions. For user-mode emulation it's
427 * equivalent to gdb_continue.
429 static int gdb_continue_partial(GDBState
*s
, char *newstates
)
433 #ifdef CONFIG_USER_ONLY
435 * This is not exactly accurate, but it's an improvement compared to the
436 * previous situation, where only one CPU would be single-stepped.
439 if (newstates
[cpu
->cpu_index
] == 's') {
440 trace_gdbstub_op_stepping(cpu
->cpu_index
);
441 cpu_single_step(cpu
, sstep_flags
);
444 s
->running_state
= 1;
448 if (!runstate_needs_reset()) {
449 if (vm_prepare_start()) {
454 switch (newstates
[cpu
->cpu_index
]) {
457 break; /* nothing to do here */
459 trace_gdbstub_op_stepping(cpu
->cpu_index
);
460 cpu_single_step(cpu
, sstep_flags
);
465 trace_gdbstub_op_continue_cpu(cpu
->cpu_index
);
476 qemu_clock_enable(QEMU_CLOCK_VIRTUAL
, true);
482 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
484 #ifdef CONFIG_USER_ONLY
488 ret
= send(s
->fd
, buf
, len
, 0);
498 /* XXX this blocks entire thread. Rewrite to use
499 * qemu_chr_fe_write and background I/O callbacks */
500 qemu_chr_fe_write_all(&s
->chr
, buf
, len
);
504 static inline int fromhex(int v
)
506 if (v
>= '0' && v
<= '9')
508 else if (v
>= 'A' && v
<= 'F')
510 else if (v
>= 'a' && v
<= 'f')
516 static inline int tohex(int v
)
524 /* writes 2*len+1 bytes in buf */
525 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
530 for(i
= 0; i
< len
; i
++) {
532 *q
++ = tohex(c
>> 4);
533 *q
++ = tohex(c
& 0xf);
538 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
542 for(i
= 0; i
< len
; i
++) {
543 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
548 static void hexdump(const char *buf
, int len
,
549 void (*trace_fn
)(size_t ofs
, char const *text
))
551 char line_buffer
[3 * 16 + 4 + 16 + 1];
554 for (i
= 0; i
< len
|| (i
& 0xF); ++i
) {
555 size_t byte_ofs
= i
& 15;
558 memset(line_buffer
, ' ', 3 * 16 + 4 + 16);
559 line_buffer
[3 * 16 + 4 + 16] = 0;
562 size_t col_group
= (i
>> 2) & 3;
563 size_t hex_col
= byte_ofs
* 3 + col_group
;
564 size_t txt_col
= 3 * 16 + 4 + byte_ofs
;
569 line_buffer
[hex_col
+ 0] = tohex((value
>> 4) & 0xF);
570 line_buffer
[hex_col
+ 1] = tohex((value
>> 0) & 0xF);
571 line_buffer
[txt_col
+ 0] = (value
>= ' ' && value
< 127)
577 trace_fn(i
& -16, line_buffer
);
581 /* return -1 if error, 0 if OK */
582 static int put_packet_binary(GDBState
*s
, const char *buf
, int len
, bool dump
)
587 if (dump
&& trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY
)) {
588 hexdump(buf
, len
, trace_gdbstub_io_binaryreply
);
597 for(i
= 0; i
< len
; i
++) {
601 *(p
++) = tohex((csum
>> 4) & 0xf);
602 *(p
++) = tohex((csum
) & 0xf);
604 s
->last_packet_len
= p
- s
->last_packet
;
605 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
607 #ifdef CONFIG_USER_ONLY
620 /* return -1 if error, 0 if OK */
621 static int put_packet(GDBState
*s
, const char *buf
)
623 trace_gdbstub_io_reply(buf
);
625 return put_packet_binary(s
, buf
, strlen(buf
), false);
628 /* Encode data using the encoding for 'x' packets. */
629 static int memtox(char *buf
, const char *mem
, int len
)
637 case '#': case '$': case '*': case '}':
649 static uint32_t gdb_get_cpu_pid(const GDBState
*s
, CPUState
*cpu
)
651 /* TODO: In user mode, we should use the task state PID */
652 if (cpu
->cluster_index
== UNASSIGNED_CLUSTER_INDEX
) {
653 /* Return the default process' PID */
654 return s
->processes
[s
->process_num
- 1].pid
;
656 return cpu
->cluster_index
+ 1;
659 static GDBProcess
*gdb_get_process(const GDBState
*s
, uint32_t pid
)
664 /* 0 means any process, we take the first one */
665 return &s
->processes
[0];
668 for (i
= 0; i
< s
->process_num
; i
++) {
669 if (s
->processes
[i
].pid
== pid
) {
670 return &s
->processes
[i
];
677 static GDBProcess
*gdb_get_cpu_process(const GDBState
*s
, CPUState
*cpu
)
679 return gdb_get_process(s
, gdb_get_cpu_pid(s
, cpu
));
682 static CPUState
*find_cpu(uint32_t thread_id
)
687 if (cpu_gdb_index(cpu
) == thread_id
) {
695 static CPUState
*get_first_cpu_in_process(const GDBState
*s
,
701 if (gdb_get_cpu_pid(s
, cpu
) == process
->pid
) {
709 static CPUState
*gdb_next_cpu_in_process(const GDBState
*s
, CPUState
*cpu
)
711 uint32_t pid
= gdb_get_cpu_pid(s
, cpu
);
715 if (gdb_get_cpu_pid(s
, cpu
) == pid
) {
725 /* Return the cpu following @cpu, while ignoring unattached processes. */
726 static CPUState
*gdb_next_attached_cpu(const GDBState
*s
, CPUState
*cpu
)
731 if (gdb_get_cpu_process(s
, cpu
)->attached
) {
741 /* Return the first attached cpu */
742 static CPUState
*gdb_first_attached_cpu(const GDBState
*s
)
744 CPUState
*cpu
= first_cpu
;
745 GDBProcess
*process
= gdb_get_cpu_process(s
, cpu
);
747 if (!process
->attached
) {
748 return gdb_next_attached_cpu(s
, cpu
);
754 static CPUState
*gdb_get_cpu(const GDBState
*s
, uint32_t pid
, uint32_t tid
)
760 /* 0 means any process/thread, we take the first attached one */
761 return gdb_first_attached_cpu(s
);
762 } else if (pid
&& !tid
) {
763 /* any thread in a specific process */
764 process
= gdb_get_process(s
, pid
);
766 if (process
== NULL
) {
770 if (!process
->attached
) {
774 return get_first_cpu_in_process(s
, process
);
776 /* a specific thread */
783 process
= gdb_get_cpu_process(s
, cpu
);
785 if (pid
&& process
->pid
!= pid
) {
789 if (!process
->attached
) {
797 static const char *get_feature_xml(const GDBState
*s
, const char *p
,
798 const char **newp
, GDBProcess
*process
)
803 CPUState
*cpu
= get_first_cpu_in_process(s
, process
);
804 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
807 while (p
[len
] && p
[len
] != ':')
812 if (strncmp(p
, "target.xml", len
) == 0) {
813 char *buf
= process
->target_xml
;
814 const size_t buf_sz
= sizeof(process
->target_xml
);
816 /* Generate the XML description for this CPU. */
821 "<?xml version=\"1.0\"?>"
822 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
824 if (cc
->gdb_arch_name
) {
825 gchar
*arch
= cc
->gdb_arch_name(cpu
);
826 pstrcat(buf
, buf_sz
, "<architecture>");
827 pstrcat(buf
, buf_sz
, arch
);
828 pstrcat(buf
, buf_sz
, "</architecture>");
831 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
832 pstrcat(buf
, buf_sz
, cc
->gdb_core_xml_file
);
833 pstrcat(buf
, buf_sz
, "\"/>");
834 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
835 pstrcat(buf
, buf_sz
, "<xi:include href=\"");
836 pstrcat(buf
, buf_sz
, r
->xml
);
837 pstrcat(buf
, buf_sz
, "\"/>");
839 pstrcat(buf
, buf_sz
, "</target>");
843 if (cc
->gdb_get_dynamic_xml
) {
844 char *xmlname
= g_strndup(p
, len
);
845 const char *xml
= cc
->gdb_get_dynamic_xml(cpu
, xmlname
);
853 name
= xml_builtin
[i
][0];
854 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
857 return name
? xml_builtin
[i
][1] : NULL
;
860 static int gdb_read_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
862 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
863 CPUArchState
*env
= cpu
->env_ptr
;
866 if (reg
< cc
->gdb_num_core_regs
) {
867 return cc
->gdb_read_register(cpu
, mem_buf
, reg
);
870 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
871 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
872 return r
->get_reg(env
, mem_buf
, reg
- r
->base_reg
);
878 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
880 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
881 CPUArchState
*env
= cpu
->env_ptr
;
884 if (reg
< cc
->gdb_num_core_regs
) {
885 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
888 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
889 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
890 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
896 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
897 specifies the first register number and these registers are included in
898 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
899 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
902 void gdb_register_coprocessor(CPUState
*cpu
,
903 gdb_reg_cb get_reg
, gdb_reg_cb set_reg
,
904 int num_regs
, const char *xml
, int g_pos
)
907 GDBRegisterState
**p
;
911 /* Check for duplicates. */
912 if (strcmp((*p
)->xml
, xml
) == 0)
917 s
= g_new0(GDBRegisterState
, 1);
918 s
->base_reg
= cpu
->gdb_num_regs
;
919 s
->num_regs
= num_regs
;
920 s
->get_reg
= get_reg
;
921 s
->set_reg
= set_reg
;
924 /* Add to end of list. */
925 cpu
->gdb_num_regs
+= num_regs
;
928 if (g_pos
!= s
->base_reg
) {
929 error_report("Error: Bad gdb register numbering for '%s', "
930 "expected %d got %d", xml
, g_pos
, s
->base_reg
);
932 cpu
->gdb_num_g_regs
= cpu
->gdb_num_regs
;
937 #ifndef CONFIG_USER_ONLY
938 /* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
939 static inline int xlat_gdb_type(CPUState
*cpu
, int gdbtype
)
941 static const int xlat
[] = {
942 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
943 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
944 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
947 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
948 int cputype
= xlat
[gdbtype
];
950 if (cc
->gdb_stop_before_watchpoint
) {
951 cputype
|= BP_STOP_BEFORE_ACCESS
;
957 static int gdb_breakpoint_insert(int type
, target_ulong addr
, target_ulong len
)
963 return kvm_insert_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
967 case GDB_BREAKPOINT_SW
:
968 case GDB_BREAKPOINT_HW
:
970 err
= cpu_breakpoint_insert(cpu
, addr
, BP_GDB
, NULL
);
976 #ifndef CONFIG_USER_ONLY
977 case GDB_WATCHPOINT_WRITE
:
978 case GDB_WATCHPOINT_READ
:
979 case GDB_WATCHPOINT_ACCESS
:
981 err
= cpu_watchpoint_insert(cpu
, addr
, len
,
982 xlat_gdb_type(cpu
, type
), NULL
);
994 static int gdb_breakpoint_remove(int type
, target_ulong addr
, target_ulong len
)
1000 return kvm_remove_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1004 case GDB_BREAKPOINT_SW
:
1005 case GDB_BREAKPOINT_HW
:
1007 err
= cpu_breakpoint_remove(cpu
, addr
, BP_GDB
);
1013 #ifndef CONFIG_USER_ONLY
1014 case GDB_WATCHPOINT_WRITE
:
1015 case GDB_WATCHPOINT_READ
:
1016 case GDB_WATCHPOINT_ACCESS
:
1018 err
= cpu_watchpoint_remove(cpu
, addr
, len
,
1019 xlat_gdb_type(cpu
, type
));
1030 static inline void gdb_cpu_breakpoint_remove_all(CPUState
*cpu
)
1032 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
1033 #ifndef CONFIG_USER_ONLY
1034 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
1038 static void gdb_process_breakpoint_remove_all(const GDBState
*s
, GDBProcess
*p
)
1040 CPUState
*cpu
= get_first_cpu_in_process(s
, p
);
1043 gdb_cpu_breakpoint_remove_all(cpu
);
1044 cpu
= gdb_next_cpu_in_process(s
, cpu
);
1048 static void gdb_breakpoint_remove_all(void)
1052 if (kvm_enabled()) {
1053 kvm_remove_all_breakpoints(gdbserver_state
->c_cpu
);
1058 gdb_cpu_breakpoint_remove_all(cpu
);
1062 static void gdb_set_cpu_pc(GDBState
*s
, target_ulong pc
)
1064 CPUState
*cpu
= s
->c_cpu
;
1066 cpu_synchronize_state(cpu
);
1067 cpu_set_pc(cpu
, pc
);
1070 static char *gdb_fmt_thread_id(const GDBState
*s
, CPUState
*cpu
,
1071 char *buf
, size_t buf_size
)
1073 if (s
->multiprocess
) {
1074 snprintf(buf
, buf_size
, "p%02x.%02x",
1075 gdb_get_cpu_pid(s
, cpu
), cpu_gdb_index(cpu
));
1077 snprintf(buf
, buf_size
, "%02x", cpu_gdb_index(cpu
));
1083 typedef enum GDBThreadIdKind
{
1085 GDB_ALL_THREADS
, /* One process, all threads */
1090 static GDBThreadIdKind
read_thread_id(const char *buf
, const char **end_buf
,
1091 uint32_t *pid
, uint32_t *tid
)
1098 ret
= qemu_strtoul(buf
, &buf
, 16, &p
);
1101 return GDB_READ_THREAD_ERR
;
1110 ret
= qemu_strtoul(buf
, &buf
, 16, &t
);
1113 return GDB_READ_THREAD_ERR
;
1119 return GDB_ALL_PROCESSES
;
1127 return GDB_ALL_THREADS
;
1134 return GDB_ONE_THREAD
;
1137 static int is_query_packet(const char *p
, const char *query
, char separator
)
1139 unsigned int query_len
= strlen(query
);
1141 return strncmp(p
, query
, query_len
) == 0 &&
1142 (p
[query_len
] == '\0' || p
[query_len
] == separator
);
1146 * gdb_handle_vcont - Parses and handles a vCont packet.
1147 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1148 * a format error, 0 on success.
1150 static int gdb_handle_vcont(GDBState
*s
, const char *p
)
1152 int res
, signal
= 0;
1157 GDBProcess
*process
;
1159 GDBThreadIdKind kind
;
1160 #ifdef CONFIG_USER_ONLY
1161 int max_cpus
= 1; /* global variable max_cpus exists only in system mode */
1164 max_cpus
= max_cpus
<= cpu
->cpu_index
? cpu
->cpu_index
+ 1 : max_cpus
;
1167 /* uninitialised CPUs stay 0 */
1168 newstates
= g_new0(char, max_cpus
);
1170 /* mark valid CPUs with 1 */
1172 newstates
[cpu
->cpu_index
] = 1;
1176 * res keeps track of what error we are returning, with -ENOTSUP meaning
1177 * that the command is unknown or unsupported, thus returning an empty
1178 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1179 * or incorrect parameters passed.
1189 if (cur_action
== 'C' || cur_action
== 'S') {
1190 cur_action
= qemu_tolower(cur_action
);
1191 res
= qemu_strtoul(p
+ 1, &p
, 16, &tmp
);
1195 signal
= gdb_signal_to_target(tmp
);
1196 } else if (cur_action
!= 'c' && cur_action
!= 's') {
1197 /* unknown/invalid/unsupported command */
1202 if (*p
== '\0' || *p
== ';') {
1204 * No thread specifier, action is on "all threads". The
1205 * specification is unclear regarding the process to act on. We
1206 * choose all processes.
1208 kind
= GDB_ALL_PROCESSES
;
1209 } else if (*p
++ == ':') {
1210 kind
= read_thread_id(p
, &p
, &pid
, &tid
);
1217 case GDB_READ_THREAD_ERR
:
1221 case GDB_ALL_PROCESSES
:
1222 cpu
= gdb_first_attached_cpu(s
);
1224 if (newstates
[cpu
->cpu_index
] == 1) {
1225 newstates
[cpu
->cpu_index
] = cur_action
;
1228 cpu
= gdb_next_attached_cpu(s
, cpu
);
1232 case GDB_ALL_THREADS
:
1233 process
= gdb_get_process(s
, pid
);
1235 if (!process
->attached
) {
1240 cpu
= get_first_cpu_in_process(s
, process
);
1242 if (newstates
[cpu
->cpu_index
] == 1) {
1243 newstates
[cpu
->cpu_index
] = cur_action
;
1246 cpu
= gdb_next_cpu_in_process(s
, cpu
);
1250 case GDB_ONE_THREAD
:
1251 cpu
= gdb_get_cpu(s
, pid
, tid
);
1253 /* invalid CPU/thread specified */
1259 /* only use if no previous match occourred */
1260 if (newstates
[cpu
->cpu_index
] == 1) {
1261 newstates
[cpu
->cpu_index
] = cur_action
;
1267 gdb_continue_partial(s
, newstates
);
1275 typedef union GdbCmdVariant
{
1278 unsigned long val_ul
;
1279 unsigned long long val_ull
;
1281 GDBThreadIdKind kind
;
1287 static const char *cmd_next_param(const char *param
, const char delimiter
)
1289 static const char all_delimiters
[] = ",;:=";
1290 char curr_delimiters
[2] = {0};
1291 const char *delimiters
;
1293 if (delimiter
== '?') {
1294 delimiters
= all_delimiters
;
1295 } else if (delimiter
== '0') {
1296 return strchr(param
, '\0');
1297 } else if (delimiter
== '.' && *param
) {
1300 curr_delimiters
[0] = delimiter
;
1301 delimiters
= curr_delimiters
;
1304 param
+= strcspn(param
, delimiters
);
1311 static int cmd_parse_params(const char *data
, const char *schema
,
1312 GdbCmdVariant
*params
, int *num_params
)
1315 const char *curr_schema
, *curr_data
;
1323 curr_schema
= schema
;
1326 while (curr_schema
[0] && curr_schema
[1] && *curr_data
) {
1327 switch (curr_schema
[0]) {
1329 if (qemu_strtoul(curr_data
, &curr_data
, 16,
1330 ¶ms
[curr_param
].val_ul
)) {
1334 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1337 if (qemu_strtou64(curr_data
, &curr_data
, 16,
1338 (uint64_t *)¶ms
[curr_param
].val_ull
)) {
1342 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1345 params
[curr_param
].data
= curr_data
;
1347 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1350 params
[curr_param
].opcode
= *(uint8_t *)curr_data
;
1352 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1355 params
[curr_param
].thread_id
.kind
=
1356 read_thread_id(curr_data
, &curr_data
,
1357 ¶ms
[curr_param
].thread_id
.pid
,
1358 ¶ms
[curr_param
].thread_id
.tid
);
1360 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1363 curr_data
= cmd_next_param(curr_data
, curr_schema
[1]);
1371 *num_params
= curr_param
;
1375 typedef struct GdbCmdContext
{
1377 GdbCmdVariant
*params
;
1379 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1380 char str_buf
[MAX_PACKET_LENGTH
+ 1];
1383 typedef void (*GdbCmdHandler
)(GdbCmdContext
*gdb_ctx
, void *user_ctx
);
1386 * cmd_startswith -> cmd is compared using startswith
1389 * schema definitions:
1390 * Each schema parameter entry consists of 2 chars,
1391 * the first char represents the parameter type handling
1392 * the second char represents the delimiter for the next parameter
1394 * Currently supported schema types:
1395 * 'l' -> unsigned long (stored in .val_ul)
1396 * 'L' -> unsigned long long (stored in .val_ull)
1397 * 's' -> string (stored in .data)
1398 * 'o' -> single char (stored in .opcode)
1399 * 't' -> thread id (stored in .thread_id)
1400 * '?' -> skip according to delimiter
1402 * Currently supported delimiters:
1403 * '?' -> Stop at any delimiter (",;:=\0")
1404 * '0' -> Stop at "\0"
1405 * '.' -> Skip 1 char unless reached "\0"
1406 * Any other value is treated as the delimiter value itself
1408 typedef struct GdbCmdParseEntry
{
1409 GdbCmdHandler handler
;
1411 bool cmd_startswith
;
1415 static inline int startswith(const char *string
, const char *pattern
)
1417 return !strncmp(string
, pattern
, strlen(pattern
));
1420 static int process_string_cmd(GDBState
*s
, void *user_ctx
, const char *data
,
1421 const GdbCmdParseEntry
*cmds
, int num_cmds
)
1423 int i
, schema_len
, max_num_params
= 0;
1424 GdbCmdContext gdb_ctx
;
1430 for (i
= 0; i
< num_cmds
; i
++) {
1431 const GdbCmdParseEntry
*cmd
= &cmds
[i
];
1432 g_assert(cmd
->handler
&& cmd
->cmd
);
1434 if ((cmd
->cmd_startswith
&& !startswith(data
, cmd
->cmd
)) ||
1435 (!cmd
->cmd_startswith
&& strcmp(cmd
->cmd
, data
))) {
1440 schema_len
= strlen(cmd
->schema
);
1441 if (schema_len
% 2) {
1445 max_num_params
= schema_len
/ 2;
1449 (GdbCmdVariant
*)alloca(sizeof(*gdb_ctx
.params
) * max_num_params
);
1450 memset(gdb_ctx
.params
, 0, sizeof(*gdb_ctx
.params
) * max_num_params
);
1452 if (cmd_parse_params(&data
[strlen(cmd
->cmd
)], cmd
->schema
,
1453 gdb_ctx
.params
, &gdb_ctx
.num_params
)) {
1458 cmd
->handler(&gdb_ctx
, user_ctx
);
1465 static void run_cmd_parser(GDBState
*s
, const char *data
,
1466 const GdbCmdParseEntry
*cmd
)
1472 /* In case there was an error during the command parsing we must
1473 * send a NULL packet to indicate the command is not supported */
1474 if (process_string_cmd(s
, NULL
, data
, cmd
, 1)) {
1479 static void handle_detach(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1481 GDBProcess
*process
;
1482 GDBState
*s
= gdb_ctx
->s
;
1485 if (s
->multiprocess
) {
1486 if (!gdb_ctx
->num_params
) {
1487 put_packet(s
, "E22");
1491 pid
= gdb_ctx
->params
[0].val_ul
;
1494 process
= gdb_get_process(s
, pid
);
1495 gdb_process_breakpoint_remove_all(s
, process
);
1496 process
->attached
= false;
1498 if (pid
== gdb_get_cpu_pid(s
, s
->c_cpu
)) {
1499 s
->c_cpu
= gdb_first_attached_cpu(s
);
1502 if (pid
== gdb_get_cpu_pid(s
, s
->g_cpu
)) {
1503 s
->g_cpu
= gdb_first_attached_cpu(s
);
1507 /* No more process attached */
1508 gdb_syscall_mode
= GDB_SYS_DISABLED
;
1511 put_packet(s
, "OK");
1514 static void handle_thread_alive(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1518 if (!gdb_ctx
->num_params
) {
1519 put_packet(gdb_ctx
->s
, "E22");
1523 if (gdb_ctx
->params
[0].thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1524 put_packet(gdb_ctx
->s
, "E22");
1528 cpu
= gdb_get_cpu(gdb_ctx
->s
, gdb_ctx
->params
[0].thread_id
.pid
,
1529 gdb_ctx
->params
[0].thread_id
.tid
);
1531 put_packet(gdb_ctx
->s
, "E22");
1535 put_packet(gdb_ctx
->s
, "OK");
1538 static void handle_continue(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1540 if (gdb_ctx
->num_params
) {
1541 gdb_set_cpu_pc(gdb_ctx
->s
, gdb_ctx
->params
[0].val_ull
);
1544 gdb_ctx
->s
->signal
= 0;
1545 gdb_continue(gdb_ctx
->s
);
1548 static void handle_cont_with_sig(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1550 unsigned long signal
= 0;
1553 * Note: C sig;[addr] is currently unsupported and we simply
1554 * omit the addr parameter
1556 if (gdb_ctx
->num_params
) {
1557 signal
= gdb_ctx
->params
[0].val_ul
;
1560 gdb_ctx
->s
->signal
= gdb_signal_to_target(signal
);
1561 if (gdb_ctx
->s
->signal
== -1) {
1562 gdb_ctx
->s
->signal
= 0;
1564 gdb_continue(gdb_ctx
->s
);
1567 static void handle_set_thread(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1571 if (gdb_ctx
->num_params
!= 2) {
1572 put_packet(gdb_ctx
->s
, "E22");
1576 if (gdb_ctx
->params
[1].thread_id
.kind
== GDB_READ_THREAD_ERR
) {
1577 put_packet(gdb_ctx
->s
, "E22");
1581 if (gdb_ctx
->params
[1].thread_id
.kind
!= GDB_ONE_THREAD
) {
1582 put_packet(gdb_ctx
->s
, "OK");
1586 cpu
= gdb_get_cpu(gdb_ctx
->s
, gdb_ctx
->params
[1].thread_id
.pid
,
1587 gdb_ctx
->params
[1].thread_id
.tid
);
1589 put_packet(gdb_ctx
->s
, "E22");
1594 * Note: This command is deprecated and modern gdb's will be using the
1595 * vCont command instead.
1597 switch (gdb_ctx
->params
[0].opcode
) {
1599 gdb_ctx
->s
->c_cpu
= cpu
;
1600 put_packet(gdb_ctx
->s
, "OK");
1603 gdb_ctx
->s
->g_cpu
= cpu
;
1604 put_packet(gdb_ctx
->s
, "OK");
1607 put_packet(gdb_ctx
->s
, "E22");
1612 static void handle_insert_bp(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1616 if (gdb_ctx
->num_params
!= 3) {
1617 put_packet(gdb_ctx
->s
, "E22");
1621 res
= gdb_breakpoint_insert(gdb_ctx
->params
[0].val_ul
,
1622 gdb_ctx
->params
[1].val_ull
,
1623 gdb_ctx
->params
[2].val_ull
);
1625 put_packet(gdb_ctx
->s
, "OK");
1627 } else if (res
== -ENOSYS
) {
1628 put_packet(gdb_ctx
->s
, "");
1632 put_packet(gdb_ctx
->s
, "E22");
1635 static void handle_remove_bp(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1639 if (gdb_ctx
->num_params
!= 3) {
1640 put_packet(gdb_ctx
->s
, "E22");
1644 res
= gdb_breakpoint_remove(gdb_ctx
->params
[0].val_ul
,
1645 gdb_ctx
->params
[1].val_ull
,
1646 gdb_ctx
->params
[2].val_ull
);
1648 put_packet(gdb_ctx
->s
, "OK");
1650 } else if (res
== -ENOSYS
) {
1651 put_packet(gdb_ctx
->s
, "");
1655 put_packet(gdb_ctx
->s
, "E22");
1658 static void handle_set_reg(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1663 put_packet(gdb_ctx
->s
, "E00");
1667 if (gdb_ctx
->num_params
!= 2) {
1668 put_packet(gdb_ctx
->s
, "E22");
1672 reg_size
= strlen(gdb_ctx
->params
[1].data
) / 2;
1673 hextomem(gdb_ctx
->mem_buf
, gdb_ctx
->params
[1].data
, reg_size
);
1674 gdb_write_register(gdb_ctx
->s
->g_cpu
, gdb_ctx
->mem_buf
,
1675 gdb_ctx
->params
[0].val_ull
);
1676 put_packet(gdb_ctx
->s
, "OK");
1679 static void handle_get_reg(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1684 * Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1685 * This works, but can be very slow. Anything new enough to
1686 * understand XML also knows how to use this properly.
1689 put_packet(gdb_ctx
->s
, "");
1693 if (!gdb_ctx
->num_params
) {
1694 put_packet(gdb_ctx
->s
, "E14");
1698 reg_size
= gdb_read_register(gdb_ctx
->s
->g_cpu
, gdb_ctx
->mem_buf
,
1699 gdb_ctx
->params
[0].val_ull
);
1701 put_packet(gdb_ctx
->s
, "E14");
1705 memtohex(gdb_ctx
->str_buf
, gdb_ctx
->mem_buf
, reg_size
);
1706 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1709 static void handle_write_mem(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1711 if (gdb_ctx
->num_params
!= 3) {
1712 put_packet(gdb_ctx
->s
, "E22");
1716 /* hextomem() reads 2*len bytes */
1717 if (gdb_ctx
->params
[1].val_ull
> strlen(gdb_ctx
->params
[2].data
) / 2) {
1718 put_packet(gdb_ctx
->s
, "E22");
1722 hextomem(gdb_ctx
->mem_buf
, gdb_ctx
->params
[2].data
,
1723 gdb_ctx
->params
[1].val_ull
);
1724 if (target_memory_rw_debug(gdb_ctx
->s
->g_cpu
, gdb_ctx
->params
[0].val_ull
,
1726 gdb_ctx
->params
[1].val_ull
, true)) {
1727 put_packet(gdb_ctx
->s
, "E14");
1731 put_packet(gdb_ctx
->s
, "OK");
1734 static void handle_read_mem(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1736 if (gdb_ctx
->num_params
!= 2) {
1737 put_packet(gdb_ctx
->s
, "E22");
1741 /* memtohex() doubles the required space */
1742 if (gdb_ctx
->params
[1].val_ull
> MAX_PACKET_LENGTH
/ 2) {
1743 put_packet(gdb_ctx
->s
, "E22");
1747 if (target_memory_rw_debug(gdb_ctx
->s
->g_cpu
, gdb_ctx
->params
[0].val_ull
,
1749 gdb_ctx
->params
[1].val_ull
, false)) {
1750 put_packet(gdb_ctx
->s
, "E14");
1754 memtohex(gdb_ctx
->str_buf
, gdb_ctx
->mem_buf
, gdb_ctx
->params
[1].val_ull
);
1755 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1758 static void handle_write_all_regs(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1760 target_ulong addr
, len
;
1764 if (!gdb_ctx
->num_params
) {
1768 cpu_synchronize_state(gdb_ctx
->s
->g_cpu
);
1769 registers
= gdb_ctx
->mem_buf
;
1770 len
= strlen(gdb_ctx
->params
[0].data
) / 2;
1771 hextomem(registers
, gdb_ctx
->params
[0].data
, len
);
1772 for (addr
= 0; addr
< gdb_ctx
->s
->g_cpu
->gdb_num_g_regs
&& len
> 0;
1774 reg_size
= gdb_write_register(gdb_ctx
->s
->g_cpu
, registers
, addr
);
1776 registers
+= reg_size
;
1778 put_packet(gdb_ctx
->s
, "OK");
1781 static void handle_read_all_regs(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1783 target_ulong addr
, len
;
1785 cpu_synchronize_state(gdb_ctx
->s
->g_cpu
);
1787 for (addr
= 0; addr
< gdb_ctx
->s
->g_cpu
->gdb_num_g_regs
; addr
++) {
1788 len
+= gdb_read_register(gdb_ctx
->s
->g_cpu
, gdb_ctx
->mem_buf
+ len
,
1792 memtohex(gdb_ctx
->str_buf
, gdb_ctx
->mem_buf
, len
);
1793 put_packet(gdb_ctx
->s
, gdb_ctx
->str_buf
);
1796 static void handle_file_io(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1798 if (gdb_ctx
->num_params
>= 2 && gdb_ctx
->s
->current_syscall_cb
) {
1799 target_ulong ret
, err
;
1801 ret
= (target_ulong
)gdb_ctx
->params
[0].val_ull
;
1802 err
= (target_ulong
)gdb_ctx
->params
[1].val_ull
;
1803 gdb_ctx
->s
->current_syscall_cb(gdb_ctx
->s
->c_cpu
, ret
, err
);
1804 gdb_ctx
->s
->current_syscall_cb
= NULL
;
1807 if (gdb_ctx
->num_params
>= 3 && gdb_ctx
->params
[2].opcode
== (uint8_t)'C') {
1808 put_packet(gdb_ctx
->s
, "T02");
1812 gdb_continue(gdb_ctx
->s
);
1815 static void handle_step(GdbCmdContext
*gdb_ctx
, void *user_ctx
)
1817 if (gdb_ctx
->num_params
) {
1818 gdb_set_cpu_pc(gdb_ctx
->s
, (target_ulong
)gdb_ctx
->params
[0].val_ull
);
1821 cpu_single_step(gdb_ctx
->s
->c_cpu
, sstep_flags
);
1822 gdb_continue(gdb_ctx
->s
);
1825 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
1828 GDBProcess
*process
;
1833 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1834 char buf
[sizeof(mem_buf
) + 1 /* trailing NUL */];
1836 target_ulong addr
, len
;
1837 const GdbCmdParseEntry
*cmd_parser
= NULL
;
1839 trace_gdbstub_io_command(line_buf
);
1845 put_packet(s
, "OK");
1848 /* TODO: Make this return the correct value for user-mode. */
1849 snprintf(buf
, sizeof(buf
), "T%02xthread:%s;", GDB_SIGNAL_TRAP
,
1850 gdb_fmt_thread_id(s
, s
->c_cpu
, thread_id
, sizeof(thread_id
)));
1852 /* Remove all the breakpoints when this query is issued,
1853 * because gdb is doing and initial connect and the state
1854 * should be cleaned up.
1856 gdb_breakpoint_remove_all();
1860 static const GdbCmdParseEntry continue_cmd_desc
= {
1861 .handler
= handle_continue
,
1863 .cmd_startswith
= 1,
1866 cmd_parser
= &continue_cmd_desc
;
1871 static const GdbCmdParseEntry cont_with_sig_cmd_desc
= {
1872 .handler
= handle_cont_with_sig
,
1874 .cmd_startswith
= 1,
1877 cmd_parser
= &cont_with_sig_cmd_desc
;
1881 if (strncmp(p
, "Cont", 4) == 0) {
1884 put_packet(s
, "vCont;c;C;s;S");
1888 res
= gdb_handle_vcont(s
, p
);
1891 if ((res
== -EINVAL
) || (res
== -ERANGE
)) {
1892 put_packet(s
, "E22");
1895 goto unknown_command
;
1898 } else if (strncmp(p
, "Attach;", 7) == 0) {
1903 if (qemu_strtoul(p
, &p
, 16, &pid
)) {
1904 put_packet(s
, "E22");
1908 process
= gdb_get_process(s
, pid
);
1910 if (process
== NULL
) {
1911 put_packet(s
, "E22");
1915 cpu
= get_first_cpu_in_process(s
, process
);
1918 /* Refuse to attach an empty process */
1919 put_packet(s
, "E22");
1923 process
->attached
= true;
1928 snprintf(buf
, sizeof(buf
), "T%02xthread:%s;", GDB_SIGNAL_TRAP
,
1929 gdb_fmt_thread_id(s
, cpu
, thread_id
, sizeof(thread_id
)));
1933 } else if (strncmp(p
, "Kill;", 5) == 0) {
1934 /* Kill the target */
1935 put_packet(s
, "OK");
1936 error_report("QEMU: Terminated via GDBstub");
1939 goto unknown_command
;
1942 /* Kill the target */
1943 error_report("QEMU: Terminated via GDBstub");
1947 static const GdbCmdParseEntry detach_cmd_desc
= {
1948 .handler
= handle_detach
,
1950 .cmd_startswith
= 1,
1953 cmd_parser
= &detach_cmd_desc
;
1958 static const GdbCmdParseEntry step_cmd_desc
= {
1959 .handler
= handle_step
,
1961 .cmd_startswith
= 1,
1964 cmd_parser
= &step_cmd_desc
;
1969 static const GdbCmdParseEntry file_io_cmd_desc
= {
1970 .handler
= handle_file_io
,
1972 .cmd_startswith
= 1,
1975 cmd_parser
= &file_io_cmd_desc
;
1980 static const GdbCmdParseEntry read_all_regs_cmd_desc
= {
1981 .handler
= handle_read_all_regs
,
1985 cmd_parser
= &read_all_regs_cmd_desc
;
1990 static const GdbCmdParseEntry write_all_regs_cmd_desc
= {
1991 .handler
= handle_write_all_regs
,
1993 .cmd_startswith
= 1,
1996 cmd_parser
= &write_all_regs_cmd_desc
;
2001 static const GdbCmdParseEntry read_mem_cmd_desc
= {
2002 .handler
= handle_read_mem
,
2004 .cmd_startswith
= 1,
2007 cmd_parser
= &read_mem_cmd_desc
;
2012 static const GdbCmdParseEntry write_mem_cmd_desc
= {
2013 .handler
= handle_write_mem
,
2015 .cmd_startswith
= 1,
2018 cmd_parser
= &write_mem_cmd_desc
;
2023 static const GdbCmdParseEntry get_reg_cmd_desc
= {
2024 .handler
= handle_get_reg
,
2026 .cmd_startswith
= 1,
2029 cmd_parser
= &get_reg_cmd_desc
;
2034 static const GdbCmdParseEntry set_reg_cmd_desc
= {
2035 .handler
= handle_set_reg
,
2037 .cmd_startswith
= 1,
2040 cmd_parser
= &set_reg_cmd_desc
;
2045 static const GdbCmdParseEntry insert_bp_cmd_desc
= {
2046 .handler
= handle_insert_bp
,
2048 .cmd_startswith
= 1,
2051 cmd_parser
= &insert_bp_cmd_desc
;
2056 static const GdbCmdParseEntry remove_bp_cmd_desc
= {
2057 .handler
= handle_remove_bp
,
2059 .cmd_startswith
= 1,
2062 cmd_parser
= &remove_bp_cmd_desc
;
2067 static const GdbCmdParseEntry set_thread_cmd_desc
= {
2068 .handler
= handle_set_thread
,
2070 .cmd_startswith
= 1,
2073 cmd_parser
= &set_thread_cmd_desc
;
2078 static const GdbCmdParseEntry thread_alive_cmd_desc
= {
2079 .handler
= handle_thread_alive
,
2081 .cmd_startswith
= 1,
2084 cmd_parser
= &thread_alive_cmd_desc
;
2089 /* parse any 'q' packets here */
2090 if (!strcmp(p
,"qemu.sstepbits")) {
2091 /* Query Breakpoint bit definitions */
2092 snprintf(buf
, sizeof(buf
), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
2098 } else if (is_query_packet(p
, "qemu.sstep", '=')) {
2099 /* Display or change the sstep_flags */
2102 /* Display current setting */
2103 snprintf(buf
, sizeof(buf
), "0x%x", sstep_flags
);
2108 type
= strtoul(p
, (char **)&p
, 16);
2110 put_packet(s
, "OK");
2112 } else if (strcmp(p
,"C") == 0) {
2114 * "Current thread" remains vague in the spec, so always return
2115 * the first thread of the current process (gdb returns the
2118 cpu
= get_first_cpu_in_process(s
, gdb_get_cpu_process(s
, s
->g_cpu
));
2119 snprintf(buf
, sizeof(buf
), "QC%s",
2120 gdb_fmt_thread_id(s
, cpu
, thread_id
, sizeof(thread_id
)));
2123 } else if (strcmp(p
,"fThreadInfo") == 0) {
2124 s
->query_cpu
= gdb_first_attached_cpu(s
);
2125 goto report_cpuinfo
;
2126 } else if (strcmp(p
,"sThreadInfo") == 0) {
2129 snprintf(buf
, sizeof(buf
), "m%s",
2130 gdb_fmt_thread_id(s
, s
->query_cpu
,
2131 thread_id
, sizeof(thread_id
)));
2133 s
->query_cpu
= gdb_next_attached_cpu(s
, s
->query_cpu
);
2137 } else if (strncmp(p
,"ThreadExtraInfo,", 16) == 0) {
2138 if (read_thread_id(p
+ 16, &p
, &pid
, &tid
) == GDB_READ_THREAD_ERR
) {
2139 put_packet(s
, "E22");
2142 cpu
= gdb_get_cpu(s
, pid
, tid
);
2144 cpu_synchronize_state(cpu
);
2146 if (s
->multiprocess
&& (s
->process_num
> 1)) {
2147 /* Print the CPU model and name in multiprocess mode */
2148 ObjectClass
*oc
= object_get_class(OBJECT(cpu
));
2149 const char *cpu_model
= object_class_get_name(oc
);
2151 object_get_canonical_path_component(OBJECT(cpu
));
2152 len
= snprintf((char *)mem_buf
, sizeof(buf
) / 2,
2153 "%s %s [%s]", cpu_model
, cpu_name
,
2154 cpu
->halted
? "halted " : "running");
2157 /* memtohex() doubles the required space */
2158 len
= snprintf((char *)mem_buf
, sizeof(buf
) / 2,
2159 "CPU#%d [%s]", cpu
->cpu_index
,
2160 cpu
->halted
? "halted " : "running");
2162 trace_gdbstub_op_extra_info((char *)mem_buf
);
2163 memtohex(buf
, mem_buf
, len
);
2168 #ifdef CONFIG_USER_ONLY
2169 else if (strcmp(p
, "Offsets") == 0) {
2170 TaskState
*ts
= s
->c_cpu
->opaque
;
2172 snprintf(buf
, sizeof(buf
),
2173 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
2174 ";Bss=" TARGET_ABI_FMT_lx
,
2175 ts
->info
->code_offset
,
2176 ts
->info
->data_offset
,
2177 ts
->info
->data_offset
);
2181 #else /* !CONFIG_USER_ONLY */
2182 else if (strncmp(p
, "Rcmd,", 5) == 0) {
2183 int len
= strlen(p
+ 5);
2185 if ((len
% 2) != 0) {
2186 put_packet(s
, "E01");
2190 hextomem(mem_buf
, p
+ 5, len
);
2192 qemu_chr_be_write(s
->mon_chr
, mem_buf
, len
);
2193 put_packet(s
, "OK");
2196 #endif /* !CONFIG_USER_ONLY */
2197 if (is_query_packet(p
, "Supported", ':')) {
2198 snprintf(buf
, sizeof(buf
), "PacketSize=%x", MAX_PACKET_LENGTH
);
2199 cc
= CPU_GET_CLASS(first_cpu
);
2200 if (cc
->gdb_core_xml_file
!= NULL
) {
2201 pstrcat(buf
, sizeof(buf
), ";qXfer:features:read+");
2204 if (strstr(p
, "multiprocess+")) {
2205 s
->multiprocess
= true;
2207 pstrcat(buf
, sizeof(buf
), ";multiprocess+");
2212 if (strncmp(p
, "Xfer:features:read:", 19) == 0) {
2214 target_ulong total_len
;
2216 process
= gdb_get_cpu_process(s
, s
->g_cpu
);
2217 cc
= CPU_GET_CLASS(s
->g_cpu
);
2218 if (cc
->gdb_core_xml_file
== NULL
) {
2219 goto unknown_command
;
2224 xml
= get_feature_xml(s
, p
, &p
, process
);
2226 snprintf(buf
, sizeof(buf
), "E00");
2233 addr
= strtoul(p
, (char **)&p
, 16);
2236 len
= strtoul(p
, (char **)&p
, 16);
2238 total_len
= strlen(xml
);
2239 if (addr
> total_len
) {
2240 snprintf(buf
, sizeof(buf
), "E00");
2244 if (len
> (MAX_PACKET_LENGTH
- 5) / 2)
2245 len
= (MAX_PACKET_LENGTH
- 5) / 2;
2246 if (len
< total_len
- addr
) {
2248 len
= memtox(buf
+ 1, xml
+ addr
, len
);
2251 len
= memtox(buf
+ 1, xml
+ addr
, total_len
- addr
);
2253 put_packet_binary(s
, buf
, len
+ 1, true);
2256 if (is_query_packet(p
, "Attached", ':')) {
2257 put_packet(s
, GDB_ATTACHED
);
2260 /* Unrecognised 'q' command. */
2261 goto unknown_command
;
2265 /* put empty packet */
2271 run_cmd_parser(s
, line_buf
, cmd_parser
);
2276 void gdb_set_stop_cpu(CPUState
*cpu
)
2278 GDBProcess
*p
= gdb_get_cpu_process(gdbserver_state
, cpu
);
2282 * Having a stop CPU corresponding to a process that is not attached
2283 * confuses GDB. So we ignore the request.
2288 gdbserver_state
->c_cpu
= cpu
;
2289 gdbserver_state
->g_cpu
= cpu
;
2292 #ifndef CONFIG_USER_ONLY
2293 static void gdb_vm_state_change(void *opaque
, int running
, RunState state
)
2295 GDBState
*s
= gdbserver_state
;
2296 CPUState
*cpu
= s
->c_cpu
;
2302 if (running
|| s
->state
== RS_INACTIVE
) {
2305 /* Is there a GDB syscall waiting to be sent? */
2306 if (s
->current_syscall_cb
) {
2307 put_packet(s
, s
->syscall_buf
);
2312 /* No process attached */
2316 gdb_fmt_thread_id(s
, cpu
, thread_id
, sizeof(thread_id
));
2319 case RUN_STATE_DEBUG
:
2320 if (cpu
->watchpoint_hit
) {
2321 switch (cpu
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
2332 trace_gdbstub_hit_watchpoint(type
, cpu_gdb_index(cpu
),
2333 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
2334 snprintf(buf
, sizeof(buf
),
2335 "T%02xthread:%s;%swatch:" TARGET_FMT_lx
";",
2336 GDB_SIGNAL_TRAP
, thread_id
, type
,
2337 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
2338 cpu
->watchpoint_hit
= NULL
;
2341 trace_gdbstub_hit_break();
2344 ret
= GDB_SIGNAL_TRAP
;
2346 case RUN_STATE_PAUSED
:
2347 trace_gdbstub_hit_paused();
2348 ret
= GDB_SIGNAL_INT
;
2350 case RUN_STATE_SHUTDOWN
:
2351 trace_gdbstub_hit_shutdown();
2352 ret
= GDB_SIGNAL_QUIT
;
2354 case RUN_STATE_IO_ERROR
:
2355 trace_gdbstub_hit_io_error();
2356 ret
= GDB_SIGNAL_IO
;
2358 case RUN_STATE_WATCHDOG
:
2359 trace_gdbstub_hit_watchdog();
2360 ret
= GDB_SIGNAL_ALRM
;
2362 case RUN_STATE_INTERNAL_ERROR
:
2363 trace_gdbstub_hit_internal_error();
2364 ret
= GDB_SIGNAL_ABRT
;
2366 case RUN_STATE_SAVE_VM
:
2367 case RUN_STATE_RESTORE_VM
:
2369 case RUN_STATE_FINISH_MIGRATE
:
2370 ret
= GDB_SIGNAL_XCPU
;
2373 trace_gdbstub_hit_unknown(state
);
2374 ret
= GDB_SIGNAL_UNKNOWN
;
2377 gdb_set_stop_cpu(cpu
);
2378 snprintf(buf
, sizeof(buf
), "T%02xthread:%s;", ret
, thread_id
);
2383 /* disable single step if it was enabled */
2384 cpu_single_step(cpu
, 0);
2388 /* Send a gdb syscall request.
2389 This accepts limited printf-style format specifiers, specifically:
2390 %x - target_ulong argument printed in hex.
2391 %lx - 64-bit argument printed in hex.
2392 %s - string pointer (target_ulong) and length (int) pair. */
2393 void gdb_do_syscallv(gdb_syscall_complete_cb cb
, const char *fmt
, va_list va
)
2401 s
= gdbserver_state
;
2404 s
->current_syscall_cb
= cb
;
2405 #ifndef CONFIG_USER_ONLY
2406 vm_stop(RUN_STATE_DEBUG
);
2409 p_end
= &s
->syscall_buf
[sizeof(s
->syscall_buf
)];
2416 addr
= va_arg(va
, target_ulong
);
2417 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
, addr
);
2420 if (*(fmt
++) != 'x')
2422 i64
= va_arg(va
, uint64_t);
2423 p
+= snprintf(p
, p_end
- p
, "%" PRIx64
, i64
);
2426 addr
= va_arg(va
, target_ulong
);
2427 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
"/%x",
2428 addr
, va_arg(va
, int));
2432 error_report("gdbstub: Bad syscall format string '%s'",
2441 #ifdef CONFIG_USER_ONLY
2442 put_packet(s
, s
->syscall_buf
);
2443 /* Return control to gdb for it to process the syscall request.
2444 * Since the protocol requires that gdb hands control back to us
2445 * using a "here are the results" F packet, we don't need to check
2446 * gdb_handlesig's return value (which is the signal to deliver if
2447 * execution was resumed via a continue packet).
2449 gdb_handlesig(s
->c_cpu
, 0);
2451 /* In this case wait to send the syscall packet until notification that
2452 the CPU has stopped. This must be done because if the packet is sent
2453 now the reply from the syscall request could be received while the CPU
2454 is still in the running state, which can cause packets to be dropped
2455 and state transition 'T' packets to be sent while the syscall is still
2457 qemu_cpu_kick(s
->c_cpu
);
2461 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
2466 gdb_do_syscallv(cb
, fmt
, va
);
2470 static void gdb_read_byte(GDBState
*s
, uint8_t ch
)
2474 #ifndef CONFIG_USER_ONLY
2475 if (s
->last_packet_len
) {
2476 /* Waiting for a response to the last packet. If we see the start
2477 of a new command then abandon the previous response. */
2479 trace_gdbstub_err_got_nack();
2480 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
2481 } else if (ch
== '+') {
2482 trace_gdbstub_io_got_ack();
2484 trace_gdbstub_io_got_unexpected(ch
);
2487 if (ch
== '+' || ch
== '$')
2488 s
->last_packet_len
= 0;
2492 if (runstate_is_running()) {
2493 /* when the CPU is running, we cannot do anything except stop
2494 it when receiving a char */
2495 vm_stop(RUN_STATE_PAUSED
);
2502 /* start of command packet */
2503 s
->line_buf_index
= 0;
2505 s
->state
= RS_GETLINE
;
2507 trace_gdbstub_err_garbage(ch
);
2512 /* start escape sequence */
2513 s
->state
= RS_GETLINE_ESC
;
2515 } else if (ch
== '*') {
2516 /* start run length encoding sequence */
2517 s
->state
= RS_GETLINE_RLE
;
2519 } else if (ch
== '#') {
2520 /* end of command, start of checksum*/
2521 s
->state
= RS_CHKSUM1
;
2522 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2523 trace_gdbstub_err_overrun();
2526 /* unescaped command character */
2527 s
->line_buf
[s
->line_buf_index
++] = ch
;
2531 case RS_GETLINE_ESC
:
2533 /* unexpected end of command in escape sequence */
2534 s
->state
= RS_CHKSUM1
;
2535 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2536 /* command buffer overrun */
2537 trace_gdbstub_err_overrun();
2540 /* parse escaped character and leave escape state */
2541 s
->line_buf
[s
->line_buf_index
++] = ch
^ 0x20;
2543 s
->state
= RS_GETLINE
;
2546 case RS_GETLINE_RLE
:
2548 * Run-length encoding is explained in "Debugging with GDB /
2549 * Appendix E GDB Remote Serial Protocol / Overview".
2551 if (ch
< ' ' || ch
== '#' || ch
== '$' || ch
> 126) {
2552 /* invalid RLE count encoding */
2553 trace_gdbstub_err_invalid_repeat(ch
);
2554 s
->state
= RS_GETLINE
;
2556 /* decode repeat length */
2557 int repeat
= ch
- ' ' + 3;
2558 if (s
->line_buf_index
+ repeat
>= sizeof(s
->line_buf
) - 1) {
2559 /* that many repeats would overrun the command buffer */
2560 trace_gdbstub_err_overrun();
2562 } else if (s
->line_buf_index
< 1) {
2563 /* got a repeat but we have nothing to repeat */
2564 trace_gdbstub_err_invalid_rle();
2565 s
->state
= RS_GETLINE
;
2567 /* repeat the last character */
2568 memset(s
->line_buf
+ s
->line_buf_index
,
2569 s
->line_buf
[s
->line_buf_index
- 1], repeat
);
2570 s
->line_buf_index
+= repeat
;
2572 s
->state
= RS_GETLINE
;
2577 /* get high hex digit of checksum */
2578 if (!isxdigit(ch
)) {
2579 trace_gdbstub_err_checksum_invalid(ch
);
2580 s
->state
= RS_GETLINE
;
2583 s
->line_buf
[s
->line_buf_index
] = '\0';
2584 s
->line_csum
= fromhex(ch
) << 4;
2585 s
->state
= RS_CHKSUM2
;
2588 /* get low hex digit of checksum */
2589 if (!isxdigit(ch
)) {
2590 trace_gdbstub_err_checksum_invalid(ch
);
2591 s
->state
= RS_GETLINE
;
2594 s
->line_csum
|= fromhex(ch
);
2596 if (s
->line_csum
!= (s
->line_sum
& 0xff)) {
2597 trace_gdbstub_err_checksum_incorrect(s
->line_sum
, s
->line_csum
);
2598 /* send NAK reply */
2600 put_buffer(s
, &reply
, 1);
2603 /* send ACK reply */
2605 put_buffer(s
, &reply
, 1);
2606 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
2615 /* Tell the remote gdb that the process has exited. */
2616 void gdb_exit(CPUArchState
*env
, int code
)
2621 s
= gdbserver_state
;
2625 #ifdef CONFIG_USER_ONLY
2626 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2631 trace_gdbstub_op_exiting((uint8_t)code
);
2633 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
2636 #ifndef CONFIG_USER_ONLY
2637 qemu_chr_fe_deinit(&s
->chr
, true);
2642 * Create the process that will contain all the "orphan" CPUs (that are not
2643 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2644 * be attachable and thus will be invisible to the user.
2646 static void create_default_process(GDBState
*s
)
2648 GDBProcess
*process
;
2651 if (s
->process_num
) {
2652 max_pid
= s
->processes
[s
->process_num
- 1].pid
;
2655 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2656 process
= &s
->processes
[s
->process_num
- 1];
2658 /* We need an available PID slot for this process */
2659 assert(max_pid
< UINT32_MAX
);
2661 process
->pid
= max_pid
+ 1;
2662 process
->attached
= false;
2663 process
->target_xml
[0] = '\0';
2666 #ifdef CONFIG_USER_ONLY
2668 gdb_handlesig(CPUState
*cpu
, int sig
)
2674 s
= gdbserver_state
;
2675 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2679 /* disable single step if it was enabled */
2680 cpu_single_step(cpu
, 0);
2684 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb(sig
));
2687 /* put_packet() might have detected that the peer terminated the
2695 s
->running_state
= 0;
2696 while (s
->running_state
== 0) {
2697 n
= read(s
->fd
, buf
, 256);
2701 for (i
= 0; i
< n
; i
++) {
2702 gdb_read_byte(s
, buf
[i
]);
2705 /* XXX: Connection closed. Should probably wait for another
2706 connection before continuing. */
2719 /* Tell the remote gdb that the process has exited due to SIG. */
2720 void gdb_signalled(CPUArchState
*env
, int sig
)
2725 s
= gdbserver_state
;
2726 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2730 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb(sig
));
2734 static bool gdb_accept(void)
2737 struct sockaddr_in sockaddr
;
2742 len
= sizeof(sockaddr
);
2743 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
2744 if (fd
< 0 && errno
!= EINTR
) {
2747 } else if (fd
>= 0) {
2748 qemu_set_cloexec(fd
);
2753 /* set short latency */
2754 if (socket_set_nodelay(fd
)) {
2755 perror("setsockopt");
2760 s
= g_malloc0(sizeof(GDBState
));
2761 create_default_process(s
);
2762 s
->processes
[0].attached
= true;
2763 s
->c_cpu
= gdb_first_attached_cpu(s
);
2764 s
->g_cpu
= s
->c_cpu
;
2766 gdb_has_xml
= false;
2768 gdbserver_state
= s
;
2772 static int gdbserver_open(int port
)
2774 struct sockaddr_in sockaddr
;
2777 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2782 qemu_set_cloexec(fd
);
2784 socket_set_fast_reuse(fd
);
2786 sockaddr
.sin_family
= AF_INET
;
2787 sockaddr
.sin_port
= htons(port
);
2788 sockaddr
.sin_addr
.s_addr
= 0;
2789 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
2795 ret
= listen(fd
, 1);
2804 int gdbserver_start(int port
)
2806 gdbserver_fd
= gdbserver_open(port
);
2807 if (gdbserver_fd
< 0)
2809 /* accept connections */
2810 if (!gdb_accept()) {
2811 close(gdbserver_fd
);
2818 /* Disable gdb stub for child processes. */
2819 void gdbserver_fork(CPUState
*cpu
)
2821 GDBState
*s
= gdbserver_state
;
2823 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2828 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
2829 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
2832 static int gdb_chr_can_receive(void *opaque
)
2834 /* We can handle an arbitrarily large amount of data.
2835 Pick the maximum packet size, which is as good as anything. */
2836 return MAX_PACKET_LENGTH
;
2839 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
2843 for (i
= 0; i
< size
; i
++) {
2844 gdb_read_byte(gdbserver_state
, buf
[i
]);
2848 static void gdb_chr_event(void *opaque
, int event
)
2851 GDBState
*s
= (GDBState
*) opaque
;
2854 case CHR_EVENT_OPENED
:
2855 /* Start with first process attached, others detached */
2856 for (i
= 0; i
< s
->process_num
; i
++) {
2857 s
->processes
[i
].attached
= !i
;
2860 s
->c_cpu
= gdb_first_attached_cpu(s
);
2861 s
->g_cpu
= s
->c_cpu
;
2863 vm_stop(RUN_STATE_PAUSED
);
2864 gdb_has_xml
= false;
2871 static void gdb_monitor_output(GDBState
*s
, const char *msg
, int len
)
2873 char buf
[MAX_PACKET_LENGTH
];
2876 if (len
> (MAX_PACKET_LENGTH
/2) - 1)
2877 len
= (MAX_PACKET_LENGTH
/2) - 1;
2878 memtohex(buf
+ 1, (uint8_t *)msg
, len
);
2882 static int gdb_monitor_write(Chardev
*chr
, const uint8_t *buf
, int len
)
2884 const char *p
= (const char *)buf
;
2887 max_sz
= (sizeof(gdbserver_state
->last_packet
) - 2) / 2;
2889 if (len
<= max_sz
) {
2890 gdb_monitor_output(gdbserver_state
, p
, len
);
2893 gdb_monitor_output(gdbserver_state
, p
, max_sz
);
2901 static void gdb_sigterm_handler(int signal
)
2903 if (runstate_is_running()) {
2904 vm_stop(RUN_STATE_PAUSED
);
2909 static void gdb_monitor_open(Chardev
*chr
, ChardevBackend
*backend
,
2910 bool *be_opened
, Error
**errp
)
2915 static void char_gdb_class_init(ObjectClass
*oc
, void *data
)
2917 ChardevClass
*cc
= CHARDEV_CLASS(oc
);
2919 cc
->internal
= true;
2920 cc
->open
= gdb_monitor_open
;
2921 cc
->chr_write
= gdb_monitor_write
;
2924 #define TYPE_CHARDEV_GDB "chardev-gdb"
2926 static const TypeInfo char_gdb_type_info
= {
2927 .name
= TYPE_CHARDEV_GDB
,
2928 .parent
= TYPE_CHARDEV
,
2929 .class_init
= char_gdb_class_init
,
2932 static int find_cpu_clusters(Object
*child
, void *opaque
)
2934 if (object_dynamic_cast(child
, TYPE_CPU_CLUSTER
)) {
2935 GDBState
*s
= (GDBState
*) opaque
;
2936 CPUClusterState
*cluster
= CPU_CLUSTER(child
);
2937 GDBProcess
*process
;
2939 s
->processes
= g_renew(GDBProcess
, s
->processes
, ++s
->process_num
);
2941 process
= &s
->processes
[s
->process_num
- 1];
2944 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
2945 * runtime, we enforce here that the machine does not use a cluster ID
2946 * that would lead to PID 0.
2948 assert(cluster
->cluster_id
!= UINT32_MAX
);
2949 process
->pid
= cluster
->cluster_id
+ 1;
2950 process
->attached
= false;
2951 process
->target_xml
[0] = '\0';
2956 return object_child_foreach(child
, find_cpu_clusters
, opaque
);
2959 static int pid_order(const void *a
, const void *b
)
2961 GDBProcess
*pa
= (GDBProcess
*) a
;
2962 GDBProcess
*pb
= (GDBProcess
*) b
;
2964 if (pa
->pid
< pb
->pid
) {
2966 } else if (pa
->pid
> pb
->pid
) {
2973 static void create_processes(GDBState
*s
)
2975 object_child_foreach(object_get_root(), find_cpu_clusters
, s
);
2979 qsort(s
->processes
, s
->process_num
, sizeof(s
->processes
[0]), pid_order
);
2982 create_default_process(s
);
2985 static void cleanup_processes(GDBState
*s
)
2987 g_free(s
->processes
);
2989 s
->processes
= NULL
;
2992 int gdbserver_start(const char *device
)
2994 trace_gdbstub_op_start(device
);
2997 char gdbstub_device_name
[128];
2998 Chardev
*chr
= NULL
;
3002 error_report("gdbstub: meaningless to attach gdb to a "
3003 "machine without any CPU.");
3009 if (strcmp(device
, "none") != 0) {
3010 if (strstart(device
, "tcp:", NULL
)) {
3011 /* enforce required TCP attributes */
3012 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
3013 "%s,nowait,nodelay,server", device
);
3014 device
= gdbstub_device_name
;
3017 else if (strcmp(device
, "stdio") == 0) {
3018 struct sigaction act
;
3020 memset(&act
, 0, sizeof(act
));
3021 act
.sa_handler
= gdb_sigterm_handler
;
3022 sigaction(SIGINT
, &act
, NULL
);
3026 * FIXME: it's a bit weird to allow using a mux chardev here
3027 * and implicitly setup a monitor. We may want to break this.
3029 chr
= qemu_chr_new_noreplay("gdb", device
, true, NULL
);
3034 s
= gdbserver_state
;
3036 s
= g_malloc0(sizeof(GDBState
));
3037 gdbserver_state
= s
;
3039 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
3041 /* Initialize a monitor terminal for gdb */
3042 mon_chr
= qemu_chardev_new(NULL
, TYPE_CHARDEV_GDB
,
3043 NULL
, NULL
, &error_abort
);
3044 monitor_init(mon_chr
, 0);
3046 qemu_chr_fe_deinit(&s
->chr
, true);
3047 mon_chr
= s
->mon_chr
;
3048 cleanup_processes(s
);
3049 memset(s
, 0, sizeof(GDBState
));
3050 s
->mon_chr
= mon_chr
;
3053 create_processes(s
);
3056 qemu_chr_fe_init(&s
->chr
, chr
, &error_abort
);
3057 qemu_chr_fe_set_handlers(&s
->chr
, gdb_chr_can_receive
, gdb_chr_receive
,
3058 gdb_chr_event
, NULL
, s
, NULL
, true);
3060 s
->state
= chr
? RS_IDLE
: RS_INACTIVE
;
3061 s
->mon_chr
= mon_chr
;
3062 s
->current_syscall_cb
= NULL
;
3067 void gdbserver_cleanup(void)
3069 if (gdbserver_state
) {
3070 put_packet(gdbserver_state
, "W00");
3074 static void register_types(void)
3076 type_register_static(&char_gdb_type_info
);
3079 type_init(register_types
);