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/cutils.h"
23 #ifdef CONFIG_USER_ONLY
26 #include "monitor/monitor.h"
27 #include "sysemu/char.h"
28 #include "sysemu/sysemu.h"
29 #include "exec/gdbstub.h"
32 #define MAX_PACKET_LENGTH 4096
35 #include "qemu/sockets.h"
36 #include "sysemu/kvm.h"
37 #include "exec/semihost.h"
39 #ifdef CONFIG_USER_ONLY
40 #define GDB_ATTACHED "0"
42 #define GDB_ATTACHED "1"
45 static inline int target_memory_rw_debug(CPUState
*cpu
, target_ulong addr
,
46 uint8_t *buf
, int len
, bool is_write
)
48 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
50 if (cc
->memory_rw_debug
) {
51 return cc
->memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
53 return cpu_memory_rw_debug(cpu
, addr
, buf
, len
, is_write
);
65 GDB_SIGNAL_UNKNOWN
= 143
68 #ifdef CONFIG_USER_ONLY
70 /* Map target signal numbers to GDB protocol signal numbers and vice
71 * versa. For user emulation's currently supported systems, we can
72 * assume most signals are defined.
75 static int gdb_signal_table
[] = {
235 /* In system mode we only need SIGINT and SIGTRAP; other signals
236 are not yet supported. */
243 static int gdb_signal_table
[] = {
253 #ifdef CONFIG_USER_ONLY
254 static int target_signal_to_gdb (int sig
)
257 for (i
= 0; i
< ARRAY_SIZE (gdb_signal_table
); i
++)
258 if (gdb_signal_table
[i
] == sig
)
260 return GDB_SIGNAL_UNKNOWN
;
264 static int gdb_signal_to_target (int sig
)
266 if (sig
< ARRAY_SIZE (gdb_signal_table
))
267 return gdb_signal_table
[sig
];
274 typedef struct GDBRegisterState
{
280 struct GDBRegisterState
*next
;
290 typedef struct GDBState
{
291 CPUState
*c_cpu
; /* current CPU for step/continue ops */
292 CPUState
*g_cpu
; /* current CPU for other ops */
293 CPUState
*query_cpu
; /* for q{f|s}ThreadInfo */
294 enum RSState state
; /* parsing state */
295 char line_buf
[MAX_PACKET_LENGTH
];
298 uint8_t last_packet
[MAX_PACKET_LENGTH
+ 4];
301 #ifdef CONFIG_USER_ONLY
305 CharDriverState
*chr
;
306 CharDriverState
*mon_chr
;
308 char syscall_buf
[256];
309 gdb_syscall_complete_cb current_syscall_cb
;
312 /* By default use no IRQs and no timers while single stepping so as to
313 * make single stepping like an ICE HW step.
315 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
317 static GDBState
*gdbserver_state
;
321 #ifdef CONFIG_USER_ONLY
322 /* XXX: This is not thread safe. Do we care? */
323 static int gdbserver_fd
= -1;
325 static int get_char(GDBState
*s
)
331 ret
= qemu_recv(s
->fd
, &ch
, 1, 0);
333 if (errno
== ECONNRESET
)
335 if (errno
!= EINTR
&& errno
!= EAGAIN
)
337 } else if (ret
== 0) {
355 /* Decide if either remote gdb syscalls or native file IO should be used. */
356 int use_gdb_syscalls(void)
358 SemihostingTarget target
= semihosting_get_target();
359 if (target
== SEMIHOSTING_TARGET_NATIVE
) {
360 /* -semihosting-config target=native */
362 } else if (target
== SEMIHOSTING_TARGET_GDB
) {
363 /* -semihosting-config target=gdb */
367 /* -semihosting-config target=auto */
368 /* On the first call check if gdb is connected and remember. */
369 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
370 gdb_syscall_mode
= (gdbserver_state
? GDB_SYS_ENABLED
373 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
376 /* Resume execution. */
377 static inline void gdb_continue(GDBState
*s
)
379 #ifdef CONFIG_USER_ONLY
380 s
->running_state
= 1;
382 if (!runstate_needs_reset()) {
388 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
390 #ifdef CONFIG_USER_ONLY
394 ret
= send(s
->fd
, buf
, len
, 0);
396 if (errno
!= EINTR
&& errno
!= EAGAIN
)
404 qemu_chr_fe_write(s
->chr
, buf
, len
);
408 static inline int fromhex(int v
)
410 if (v
>= '0' && v
<= '9')
412 else if (v
>= 'A' && v
<= 'F')
414 else if (v
>= 'a' && v
<= 'f')
420 static inline int tohex(int v
)
428 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
433 for(i
= 0; i
< len
; i
++) {
435 *q
++ = tohex(c
>> 4);
436 *q
++ = tohex(c
& 0xf);
441 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
445 for(i
= 0; i
< len
; i
++) {
446 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
451 /* return -1 if error, 0 if OK */
452 static int put_packet_binary(GDBState
*s
, const char *buf
, int len
)
463 for(i
= 0; i
< len
; i
++) {
467 *(p
++) = tohex((csum
>> 4) & 0xf);
468 *(p
++) = tohex((csum
) & 0xf);
470 s
->last_packet_len
= p
- s
->last_packet
;
471 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
473 #ifdef CONFIG_USER_ONLY
486 /* return -1 if error, 0 if OK */
487 static int put_packet(GDBState
*s
, const char *buf
)
490 printf("reply='%s'\n", buf
);
493 return put_packet_binary(s
, buf
, strlen(buf
));
496 /* Encode data using the encoding for 'x' packets. */
497 static int memtox(char *buf
, const char *mem
, int len
)
505 case '#': case '$': case '*': case '}':
517 static const char *get_feature_xml(const char *p
, const char **newp
,
523 static char target_xml
[1024];
526 while (p
[len
] && p
[len
] != ':')
531 if (strncmp(p
, "target.xml", len
) == 0) {
532 /* Generate the XML description for this CPU. */
533 if (!target_xml
[0]) {
535 CPUState
*cpu
= first_cpu
;
537 pstrcat(target_xml
, sizeof(target_xml
),
538 "<?xml version=\"1.0\"?>"
539 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
541 if (cc
->gdb_arch_name
) {
542 gchar
*arch
= cc
->gdb_arch_name(cpu
);
543 pstrcat(target_xml
, sizeof(target_xml
), "<architecture>");
544 pstrcat(target_xml
, sizeof(target_xml
), arch
);
545 pstrcat(target_xml
, sizeof(target_xml
), "</architecture>");
548 pstrcat(target_xml
, sizeof(target_xml
), "<xi:include href=\"");
549 pstrcat(target_xml
, sizeof(target_xml
), cc
->gdb_core_xml_file
);
550 pstrcat(target_xml
, sizeof(target_xml
), "\"/>");
551 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
552 pstrcat(target_xml
, sizeof(target_xml
), "<xi:include href=\"");
553 pstrcat(target_xml
, sizeof(target_xml
), r
->xml
);
554 pstrcat(target_xml
, sizeof(target_xml
), "\"/>");
556 pstrcat(target_xml
, sizeof(target_xml
), "</target>");
561 name
= xml_builtin
[i
][0];
562 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
565 return name
? xml_builtin
[i
][1] : NULL
;
568 static int gdb_read_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
570 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
571 CPUArchState
*env
= cpu
->env_ptr
;
574 if (reg
< cc
->gdb_num_core_regs
) {
575 return cc
->gdb_read_register(cpu
, mem_buf
, reg
);
578 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
579 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
580 return r
->get_reg(env
, mem_buf
, reg
- r
->base_reg
);
586 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
588 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
589 CPUArchState
*env
= cpu
->env_ptr
;
592 if (reg
< cc
->gdb_num_core_regs
) {
593 return cc
->gdb_write_register(cpu
, mem_buf
, reg
);
596 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
597 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
598 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
604 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
605 specifies the first register number and these registers are included in
606 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
607 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
610 void gdb_register_coprocessor(CPUState
*cpu
,
611 gdb_reg_cb get_reg
, gdb_reg_cb set_reg
,
612 int num_regs
, const char *xml
, int g_pos
)
615 GDBRegisterState
**p
;
619 /* Check for duplicates. */
620 if (strcmp((*p
)->xml
, xml
) == 0)
625 s
= g_new0(GDBRegisterState
, 1);
626 s
->base_reg
= cpu
->gdb_num_regs
;
627 s
->num_regs
= num_regs
;
628 s
->get_reg
= get_reg
;
629 s
->set_reg
= set_reg
;
632 /* Add to end of list. */
633 cpu
->gdb_num_regs
+= num_regs
;
636 if (g_pos
!= s
->base_reg
) {
637 fprintf(stderr
, "Error: Bad gdb register numbering for '%s'\n"
638 "Expected %d got %d\n", xml
, g_pos
, s
->base_reg
);
640 cpu
->gdb_num_g_regs
= cpu
->gdb_num_regs
;
645 #ifndef CONFIG_USER_ONLY
646 /* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
647 static inline int xlat_gdb_type(CPUState
*cpu
, int gdbtype
)
649 static const int xlat
[] = {
650 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
651 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
652 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
655 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
656 int cputype
= xlat
[gdbtype
];
658 if (cc
->gdb_stop_before_watchpoint
) {
659 cputype
|= BP_STOP_BEFORE_ACCESS
;
665 static int gdb_breakpoint_insert(target_ulong addr
, target_ulong len
, int type
)
671 return kvm_insert_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
675 case GDB_BREAKPOINT_SW
:
676 case GDB_BREAKPOINT_HW
:
678 err
= cpu_breakpoint_insert(cpu
, addr
, BP_GDB
, NULL
);
684 #ifndef CONFIG_USER_ONLY
685 case GDB_WATCHPOINT_WRITE
:
686 case GDB_WATCHPOINT_READ
:
687 case GDB_WATCHPOINT_ACCESS
:
689 err
= cpu_watchpoint_insert(cpu
, addr
, len
,
690 xlat_gdb_type(cpu
, type
), NULL
);
702 static int gdb_breakpoint_remove(target_ulong addr
, target_ulong len
, int type
)
708 return kvm_remove_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
712 case GDB_BREAKPOINT_SW
:
713 case GDB_BREAKPOINT_HW
:
715 err
= cpu_breakpoint_remove(cpu
, addr
, BP_GDB
);
721 #ifndef CONFIG_USER_ONLY
722 case GDB_WATCHPOINT_WRITE
:
723 case GDB_WATCHPOINT_READ
:
724 case GDB_WATCHPOINT_ACCESS
:
726 err
= cpu_watchpoint_remove(cpu
, addr
, len
,
727 xlat_gdb_type(cpu
, type
));
738 static void gdb_breakpoint_remove_all(void)
743 kvm_remove_all_breakpoints(gdbserver_state
->c_cpu
);
748 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
749 #ifndef CONFIG_USER_ONLY
750 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
755 static void gdb_set_cpu_pc(GDBState
*s
, target_ulong pc
)
757 CPUState
*cpu
= s
->c_cpu
;
759 cpu_synchronize_state(cpu
);
763 static CPUState
*find_cpu(uint32_t thread_id
)
768 if (cpu_index(cpu
) == thread_id
) {
776 static int is_query_packet(const char *p
, const char *query
, char separator
)
778 unsigned int query_len
= strlen(query
);
780 return strncmp(p
, query
, query_len
) == 0 &&
781 (p
[query_len
] == '\0' || p
[query_len
] == separator
);
784 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
790 int ch
, reg_size
, type
, res
;
791 char buf
[MAX_PACKET_LENGTH
];
792 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
794 target_ulong addr
, len
;
797 printf("command='%s'\n", line_buf
);
803 /* TODO: Make this return the correct value for user-mode. */
804 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", GDB_SIGNAL_TRAP
,
805 cpu_index(s
->c_cpu
));
807 /* Remove all the breakpoints when this query is issued,
808 * because gdb is doing and initial connect and the state
809 * should be cleaned up.
811 gdb_breakpoint_remove_all();
815 addr
= strtoull(p
, (char **)&p
, 16);
816 gdb_set_cpu_pc(s
, addr
);
822 s
->signal
= gdb_signal_to_target (strtoul(p
, (char **)&p
, 16));
828 if (strncmp(p
, "Cont", 4) == 0) {
829 int res_signal
, res_thread
;
833 put_packet(s
, "vCont;c;C;s;S");
848 if (action
== 'C' || action
== 'S') {
849 signal
= gdb_signal_to_target(strtoul(p
, (char **)&p
, 16));
853 } else if (action
!= 'c' && action
!= 's') {
859 thread
= strtoull(p
+1, (char **)&p
, 16);
861 action
= tolower(action
);
862 if (res
== 0 || (res
== 'c' && action
== 's')) {
869 if (res_thread
!= -1 && res_thread
!= 0) {
870 cpu
= find_cpu(res_thread
);
872 put_packet(s
, "E22");
878 cpu_single_step(s
->c_cpu
, sstep_flags
);
880 s
->signal
= res_signal
;
886 goto unknown_command
;
889 /* Kill the target */
890 fprintf(stderr
, "\nQEMU: Terminated via GDBstub\n");
894 gdb_breakpoint_remove_all();
895 gdb_syscall_mode
= GDB_SYS_DISABLED
;
901 addr
= strtoull(p
, (char **)&p
, 16);
902 gdb_set_cpu_pc(s
, addr
);
904 cpu_single_step(s
->c_cpu
, sstep_flags
);
912 ret
= strtoull(p
, (char **)&p
, 16);
915 err
= strtoull(p
, (char **)&p
, 16);
922 if (s
->current_syscall_cb
) {
923 s
->current_syscall_cb(s
->c_cpu
, ret
, err
);
924 s
->current_syscall_cb
= NULL
;
927 put_packet(s
, "T02");
934 cpu_synchronize_state(s
->g_cpu
);
936 for (addr
= 0; addr
< s
->g_cpu
->gdb_num_g_regs
; addr
++) {
937 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
+ len
, addr
);
940 memtohex(buf
, mem_buf
, len
);
944 cpu_synchronize_state(s
->g_cpu
);
947 hextomem((uint8_t *)registers
, p
, len
);
948 for (addr
= 0; addr
< s
->g_cpu
->gdb_num_g_regs
&& len
> 0; addr
++) {
949 reg_size
= gdb_write_register(s
->g_cpu
, registers
, addr
);
951 registers
+= reg_size
;
956 addr
= strtoull(p
, (char **)&p
, 16);
959 len
= strtoull(p
, NULL
, 16);
961 /* memtohex() doubles the required space */
962 if (len
> MAX_PACKET_LENGTH
/ 2) {
963 put_packet (s
, "E22");
967 if (target_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, false) != 0) {
968 put_packet (s
, "E14");
970 memtohex(buf
, mem_buf
, len
);
975 addr
= strtoull(p
, (char **)&p
, 16);
978 len
= strtoull(p
, (char **)&p
, 16);
982 /* hextomem() reads 2*len bytes */
983 if (len
> strlen(p
) / 2) {
984 put_packet (s
, "E22");
987 hextomem(mem_buf
, p
, len
);
988 if (target_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
,
990 put_packet(s
, "E14");
996 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
997 This works, but can be very slow. Anything new enough to
998 understand XML also knows how to use this properly. */
1000 goto unknown_command
;
1001 addr
= strtoull(p
, (char **)&p
, 16);
1002 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
, addr
);
1004 memtohex(buf
, mem_buf
, reg_size
);
1007 put_packet(s
, "E14");
1012 goto unknown_command
;
1013 addr
= strtoull(p
, (char **)&p
, 16);
1016 reg_size
= strlen(p
) / 2;
1017 hextomem(mem_buf
, p
, reg_size
);
1018 gdb_write_register(s
->g_cpu
, mem_buf
, addr
);
1019 put_packet(s
, "OK");
1023 type
= strtoul(p
, (char **)&p
, 16);
1026 addr
= strtoull(p
, (char **)&p
, 16);
1029 len
= strtoull(p
, (char **)&p
, 16);
1031 res
= gdb_breakpoint_insert(addr
, len
, type
);
1033 res
= gdb_breakpoint_remove(addr
, len
, type
);
1035 put_packet(s
, "OK");
1036 else if (res
== -ENOSYS
)
1039 put_packet(s
, "E22");
1043 thread
= strtoull(p
, (char **)&p
, 16);
1044 if (thread
== -1 || thread
== 0) {
1045 put_packet(s
, "OK");
1048 cpu
= find_cpu(thread
);
1050 put_packet(s
, "E22");
1056 put_packet(s
, "OK");
1060 put_packet(s
, "OK");
1063 put_packet(s
, "E22");
1068 thread
= strtoull(p
, (char **)&p
, 16);
1069 cpu
= find_cpu(thread
);
1072 put_packet(s
, "OK");
1074 put_packet(s
, "E22");
1079 /* parse any 'q' packets here */
1080 if (!strcmp(p
,"qemu.sstepbits")) {
1081 /* Query Breakpoint bit definitions */
1082 snprintf(buf
, sizeof(buf
), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1088 } else if (is_query_packet(p
, "qemu.sstep", '=')) {
1089 /* Display or change the sstep_flags */
1092 /* Display current setting */
1093 snprintf(buf
, sizeof(buf
), "0x%x", sstep_flags
);
1098 type
= strtoul(p
, (char **)&p
, 16);
1100 put_packet(s
, "OK");
1102 } else if (strcmp(p
,"C") == 0) {
1103 /* "Current thread" remains vague in the spec, so always return
1104 * the first CPU (gdb returns the first thread). */
1105 put_packet(s
, "QC1");
1107 } else if (strcmp(p
,"fThreadInfo") == 0) {
1108 s
->query_cpu
= first_cpu
;
1109 goto report_cpuinfo
;
1110 } else if (strcmp(p
,"sThreadInfo") == 0) {
1113 snprintf(buf
, sizeof(buf
), "m%x", cpu_index(s
->query_cpu
));
1115 s
->query_cpu
= CPU_NEXT(s
->query_cpu
);
1119 } else if (strncmp(p
,"ThreadExtraInfo,", 16) == 0) {
1120 thread
= strtoull(p
+16, (char **)&p
, 16);
1121 cpu
= find_cpu(thread
);
1123 cpu_synchronize_state(cpu
);
1124 /* memtohex() doubles the required space */
1125 len
= snprintf((char *)mem_buf
, sizeof(buf
) / 2,
1126 "CPU#%d [%s]", cpu
->cpu_index
,
1127 cpu
->halted
? "halted " : "running");
1128 memtohex(buf
, mem_buf
, len
);
1133 #ifdef CONFIG_USER_ONLY
1134 else if (strcmp(p
, "Offsets") == 0) {
1135 TaskState
*ts
= s
->c_cpu
->opaque
;
1137 snprintf(buf
, sizeof(buf
),
1138 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1139 ";Bss=" TARGET_ABI_FMT_lx
,
1140 ts
->info
->code_offset
,
1141 ts
->info
->data_offset
,
1142 ts
->info
->data_offset
);
1146 #else /* !CONFIG_USER_ONLY */
1147 else if (strncmp(p
, "Rcmd,", 5) == 0) {
1148 int len
= strlen(p
+ 5);
1150 if ((len
% 2) != 0) {
1151 put_packet(s
, "E01");
1155 hextomem(mem_buf
, p
+ 5, len
);
1157 qemu_chr_be_write(s
->mon_chr
, mem_buf
, len
);
1158 put_packet(s
, "OK");
1161 #endif /* !CONFIG_USER_ONLY */
1162 if (is_query_packet(p
, "Supported", ':')) {
1163 snprintf(buf
, sizeof(buf
), "PacketSize=%x", MAX_PACKET_LENGTH
);
1164 cc
= CPU_GET_CLASS(first_cpu
);
1165 if (cc
->gdb_core_xml_file
!= NULL
) {
1166 pstrcat(buf
, sizeof(buf
), ";qXfer:features:read+");
1171 if (strncmp(p
, "Xfer:features:read:", 19) == 0) {
1173 target_ulong total_len
;
1175 cc
= CPU_GET_CLASS(first_cpu
);
1176 if (cc
->gdb_core_xml_file
== NULL
) {
1177 goto unknown_command
;
1182 xml
= get_feature_xml(p
, &p
, cc
);
1184 snprintf(buf
, sizeof(buf
), "E00");
1191 addr
= strtoul(p
, (char **)&p
, 16);
1194 len
= strtoul(p
, (char **)&p
, 16);
1196 total_len
= strlen(xml
);
1197 if (addr
> total_len
) {
1198 snprintf(buf
, sizeof(buf
), "E00");
1202 if (len
> (MAX_PACKET_LENGTH
- 5) / 2)
1203 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1204 if (len
< total_len
- addr
) {
1206 len
= memtox(buf
+ 1, xml
+ addr
, len
);
1209 len
= memtox(buf
+ 1, xml
+ addr
, total_len
- addr
);
1211 put_packet_binary(s
, buf
, len
+ 1);
1214 if (is_query_packet(p
, "Attached", ':')) {
1215 put_packet(s
, GDB_ATTACHED
);
1218 /* Unrecognised 'q' command. */
1219 goto unknown_command
;
1223 /* put empty packet */
1231 void gdb_set_stop_cpu(CPUState
*cpu
)
1233 gdbserver_state
->c_cpu
= cpu
;
1234 gdbserver_state
->g_cpu
= cpu
;
1237 #ifndef CONFIG_USER_ONLY
1238 static void gdb_vm_state_change(void *opaque
, int running
, RunState state
)
1240 GDBState
*s
= gdbserver_state
;
1241 CPUState
*cpu
= s
->c_cpu
;
1246 if (running
|| s
->state
== RS_INACTIVE
) {
1249 /* Is there a GDB syscall waiting to be sent? */
1250 if (s
->current_syscall_cb
) {
1251 put_packet(s
, s
->syscall_buf
);
1255 case RUN_STATE_DEBUG
:
1256 if (cpu
->watchpoint_hit
) {
1257 switch (cpu
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
1268 snprintf(buf
, sizeof(buf
),
1269 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx
";",
1270 GDB_SIGNAL_TRAP
, cpu_index(cpu
), type
,
1271 (target_ulong
)cpu
->watchpoint_hit
->vaddr
);
1272 cpu
->watchpoint_hit
= NULL
;
1276 ret
= GDB_SIGNAL_TRAP
;
1278 case RUN_STATE_PAUSED
:
1279 ret
= GDB_SIGNAL_INT
;
1281 case RUN_STATE_SHUTDOWN
:
1282 ret
= GDB_SIGNAL_QUIT
;
1284 case RUN_STATE_IO_ERROR
:
1285 ret
= GDB_SIGNAL_IO
;
1287 case RUN_STATE_WATCHDOG
:
1288 ret
= GDB_SIGNAL_ALRM
;
1290 case RUN_STATE_INTERNAL_ERROR
:
1291 ret
= GDB_SIGNAL_ABRT
;
1293 case RUN_STATE_SAVE_VM
:
1294 case RUN_STATE_RESTORE_VM
:
1296 case RUN_STATE_FINISH_MIGRATE
:
1297 ret
= GDB_SIGNAL_XCPU
;
1300 ret
= GDB_SIGNAL_UNKNOWN
;
1303 gdb_set_stop_cpu(cpu
);
1304 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", ret
, cpu_index(cpu
));
1309 /* disable single step if it was enabled */
1310 cpu_single_step(cpu
, 0);
1314 /* Send a gdb syscall request.
1315 This accepts limited printf-style format specifiers, specifically:
1316 %x - target_ulong argument printed in hex.
1317 %lx - 64-bit argument printed in hex.
1318 %s - string pointer (target_ulong) and length (int) pair. */
1319 void gdb_do_syscallv(gdb_syscall_complete_cb cb
, const char *fmt
, va_list va
)
1327 s
= gdbserver_state
;
1330 s
->current_syscall_cb
= cb
;
1331 #ifndef CONFIG_USER_ONLY
1332 vm_stop(RUN_STATE_DEBUG
);
1335 p_end
= &s
->syscall_buf
[sizeof(s
->syscall_buf
)];
1342 addr
= va_arg(va
, target_ulong
);
1343 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
, addr
);
1346 if (*(fmt
++) != 'x')
1348 i64
= va_arg(va
, uint64_t);
1349 p
+= snprintf(p
, p_end
- p
, "%" PRIx64
, i64
);
1352 addr
= va_arg(va
, target_ulong
);
1353 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
"/%x",
1354 addr
, va_arg(va
, int));
1358 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
1367 #ifdef CONFIG_USER_ONLY
1368 put_packet(s
, s
->syscall_buf
);
1369 gdb_handlesig(s
->c_cpu
, 0);
1371 /* In this case wait to send the syscall packet until notification that
1372 the CPU has stopped. This must be done because if the packet is sent
1373 now the reply from the syscall request could be received while the CPU
1374 is still in the running state, which can cause packets to be dropped
1375 and state transition 'T' packets to be sent while the syscall is still
1377 qemu_cpu_kick(s
->c_cpu
);
1381 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
1386 gdb_do_syscallv(cb
, fmt
, va
);
1390 static void gdb_read_byte(GDBState
*s
, int ch
)
1395 #ifndef CONFIG_USER_ONLY
1396 if (s
->last_packet_len
) {
1397 /* Waiting for a response to the last packet. If we see the start
1398 of a new command then abandon the previous response. */
1401 printf("Got NACK, retransmitting\n");
1403 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
1407 printf("Got ACK\n");
1409 printf("Got '%c' when expecting ACK/NACK\n", ch
);
1411 if (ch
== '+' || ch
== '$')
1412 s
->last_packet_len
= 0;
1416 if (runstate_is_running()) {
1417 /* when the CPU is running, we cannot do anything except stop
1418 it when receiving a char */
1419 vm_stop(RUN_STATE_PAUSED
);
1426 s
->line_buf_index
= 0;
1427 s
->state
= RS_GETLINE
;
1432 s
->state
= RS_CHKSUM1
;
1433 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
1436 s
->line_buf
[s
->line_buf_index
++] = ch
;
1440 s
->line_buf
[s
->line_buf_index
] = '\0';
1441 s
->line_csum
= fromhex(ch
) << 4;
1442 s
->state
= RS_CHKSUM2
;
1445 s
->line_csum
|= fromhex(ch
);
1447 for(i
= 0; i
< s
->line_buf_index
; i
++) {
1448 csum
+= s
->line_buf
[i
];
1450 if (s
->line_csum
!= (csum
& 0xff)) {
1452 put_buffer(s
, &reply
, 1);
1456 put_buffer(s
, &reply
, 1);
1457 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
1466 /* Tell the remote gdb that the process has exited. */
1467 void gdb_exit(CPUArchState
*env
, int code
)
1472 s
= gdbserver_state
;
1476 #ifdef CONFIG_USER_ONLY
1477 if (gdbserver_fd
< 0 || s
->fd
< 0) {
1486 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
1489 #ifndef CONFIG_USER_ONLY
1490 qemu_chr_delete(s
->chr
);
1494 #ifdef CONFIG_USER_ONLY
1500 s
= gdbserver_state
;
1502 if (gdbserver_fd
< 0 || s
->fd
< 0)
1509 gdb_handlesig(CPUState
*cpu
, int sig
)
1515 s
= gdbserver_state
;
1516 if (gdbserver_fd
< 0 || s
->fd
< 0) {
1520 /* disable single step if it was enabled */
1521 cpu_single_step(cpu
, 0);
1525 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb(sig
));
1528 /* put_packet() might have detected that the peer terminated the
1536 s
->running_state
= 0;
1537 while (s
->running_state
== 0) {
1538 n
= read(s
->fd
, buf
, 256);
1542 for (i
= 0; i
< n
; i
++) {
1543 gdb_read_byte(s
, buf
[i
]);
1545 } else if (n
== 0 || errno
!= EAGAIN
) {
1546 /* XXX: Connection closed. Should probably wait for another
1547 connection before continuing. */
1556 /* Tell the remote gdb that the process has exited due to SIG. */
1557 void gdb_signalled(CPUArchState
*env
, int sig
)
1562 s
= gdbserver_state
;
1563 if (gdbserver_fd
< 0 || s
->fd
< 0) {
1567 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb(sig
));
1571 static void gdb_accept(void)
1574 struct sockaddr_in sockaddr
;
1579 len
= sizeof(sockaddr
);
1580 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
1581 if (fd
< 0 && errno
!= EINTR
) {
1584 } else if (fd
>= 0) {
1586 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
1592 /* set short latency */
1593 socket_set_nodelay(fd
);
1595 s
= g_malloc0(sizeof(GDBState
));
1596 s
->c_cpu
= first_cpu
;
1597 s
->g_cpu
= first_cpu
;
1599 gdb_has_xml
= false;
1601 gdbserver_state
= s
;
1603 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1606 static int gdbserver_open(int port
)
1608 struct sockaddr_in sockaddr
;
1611 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1617 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
1620 socket_set_fast_reuse(fd
);
1622 sockaddr
.sin_family
= AF_INET
;
1623 sockaddr
.sin_port
= htons(port
);
1624 sockaddr
.sin_addr
.s_addr
= 0;
1625 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
1631 ret
= listen(fd
, 0);
1640 int gdbserver_start(int port
)
1642 gdbserver_fd
= gdbserver_open(port
);
1643 if (gdbserver_fd
< 0)
1645 /* accept connections */
1650 /* Disable gdb stub for child processes. */
1651 void gdbserver_fork(CPUState
*cpu
)
1653 GDBState
*s
= gdbserver_state
;
1655 if (gdbserver_fd
< 0 || s
->fd
< 0) {
1660 cpu_breakpoint_remove_all(cpu
, BP_GDB
);
1661 cpu_watchpoint_remove_all(cpu
, BP_GDB
);
1664 static int gdb_chr_can_receive(void *opaque
)
1666 /* We can handle an arbitrarily large amount of data.
1667 Pick the maximum packet size, which is as good as anything. */
1668 return MAX_PACKET_LENGTH
;
1671 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
1675 for (i
= 0; i
< size
; i
++) {
1676 gdb_read_byte(gdbserver_state
, buf
[i
]);
1680 static void gdb_chr_event(void *opaque
, int event
)
1683 case CHR_EVENT_OPENED
:
1684 vm_stop(RUN_STATE_PAUSED
);
1685 gdb_has_xml
= false;
1692 static void gdb_monitor_output(GDBState
*s
, const char *msg
, int len
)
1694 char buf
[MAX_PACKET_LENGTH
];
1697 if (len
> (MAX_PACKET_LENGTH
/2) - 1)
1698 len
= (MAX_PACKET_LENGTH
/2) - 1;
1699 memtohex(buf
+ 1, (uint8_t *)msg
, len
);
1703 static int gdb_monitor_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1705 const char *p
= (const char *)buf
;
1708 max_sz
= (sizeof(gdbserver_state
->last_packet
) - 2) / 2;
1710 if (len
<= max_sz
) {
1711 gdb_monitor_output(gdbserver_state
, p
, len
);
1714 gdb_monitor_output(gdbserver_state
, p
, max_sz
);
1722 static void gdb_sigterm_handler(int signal
)
1724 if (runstate_is_running()) {
1725 vm_stop(RUN_STATE_PAUSED
);
1730 int gdbserver_start(const char *device
)
1733 char gdbstub_device_name
[128];
1734 CharDriverState
*chr
= NULL
;
1735 CharDriverState
*mon_chr
;
1736 ChardevCommon common
= { 0 };
1740 if (strcmp(device
, "none") != 0) {
1741 if (strstart(device
, "tcp:", NULL
)) {
1742 /* enforce required TCP attributes */
1743 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
1744 "%s,nowait,nodelay,server", device
);
1745 device
= gdbstub_device_name
;
1748 else if (strcmp(device
, "stdio") == 0) {
1749 struct sigaction act
;
1751 memset(&act
, 0, sizeof(act
));
1752 act
.sa_handler
= gdb_sigterm_handler
;
1753 sigaction(SIGINT
, &act
, NULL
);
1756 chr
= qemu_chr_new_noreplay("gdb", device
, NULL
);
1760 qemu_chr_fe_claim_no_fail(chr
);
1761 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
1762 gdb_chr_event
, NULL
);
1765 s
= gdbserver_state
;
1767 s
= g_malloc0(sizeof(GDBState
));
1768 gdbserver_state
= s
;
1770 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
1772 /* Initialize a monitor terminal for gdb */
1773 mon_chr
= qemu_chr_alloc(&common
, &error_abort
);
1774 mon_chr
->chr_write
= gdb_monitor_write
;
1775 monitor_init(mon_chr
, 0);
1778 qemu_chr_delete(s
->chr
);
1779 mon_chr
= s
->mon_chr
;
1780 memset(s
, 0, sizeof(GDBState
));
1782 s
->c_cpu
= first_cpu
;
1783 s
->g_cpu
= first_cpu
;
1785 s
->state
= chr
? RS_IDLE
: RS_INACTIVE
;
1786 s
->mon_chr
= mon_chr
;
1787 s
->current_syscall_cb
= NULL
;