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-common.h"
21 #ifdef CONFIG_USER_ONLY
32 #include "monitor/monitor.h"
33 #include "sysemu/char.h"
34 #include "sysemu/sysemu.h"
35 #include "exec/gdbstub.h"
38 #define MAX_PACKET_LENGTH 4096
41 #include "qemu/sockets.h"
42 #include "sysemu/kvm.h"
43 #include "qemu/bitops.h"
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
;
319 /* This is an ugly hack to cope with both new and old gdb.
320 If gdb sends qXfer:features:read then assume we're talking to a newish
321 gdb that understands target descriptions. */
322 static int gdb_has_xml
;
324 #ifdef CONFIG_USER_ONLY
325 /* XXX: This is not thread safe. Do we care? */
326 static int gdbserver_fd
= -1;
328 static int get_char(GDBState
*s
)
334 ret
= qemu_recv(s
->fd
, &ch
, 1, 0);
336 if (errno
== ECONNRESET
)
338 if (errno
!= EINTR
&& errno
!= EAGAIN
)
340 } else if (ret
== 0) {
358 /* If gdb is connected when the first semihosting syscall occurs then use
359 remote gdb syscalls. Otherwise use native file IO. */
360 int use_gdb_syscalls(void)
362 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
363 gdb_syscall_mode
= (gdbserver_state
? GDB_SYS_ENABLED
366 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
369 /* Resume execution. */
370 static inline void gdb_continue(GDBState
*s
)
372 #ifdef CONFIG_USER_ONLY
373 s
->running_state
= 1;
375 if (runstate_check(RUN_STATE_GUEST_PANICKED
)) {
376 runstate_set(RUN_STATE_DEBUG
);
378 if (!runstate_needs_reset()) {
384 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
386 #ifdef CONFIG_USER_ONLY
390 ret
= send(s
->fd
, buf
, len
, 0);
392 if (errno
!= EINTR
&& errno
!= EAGAIN
)
400 qemu_chr_fe_write(s
->chr
, buf
, len
);
404 static inline int fromhex(int v
)
406 if (v
>= '0' && v
<= '9')
408 else if (v
>= 'A' && v
<= 'F')
410 else if (v
>= 'a' && v
<= 'f')
416 static inline int tohex(int v
)
424 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
429 for(i
= 0; i
< len
; i
++) {
431 *q
++ = tohex(c
>> 4);
432 *q
++ = tohex(c
& 0xf);
437 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
441 for(i
= 0; i
< len
; i
++) {
442 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
447 /* return -1 if error, 0 if OK */
448 static int put_packet_binary(GDBState
*s
, const char *buf
, int len
)
459 for(i
= 0; i
< len
; i
++) {
463 *(p
++) = tohex((csum
>> 4) & 0xf);
464 *(p
++) = tohex((csum
) & 0xf);
466 s
->last_packet_len
= p
- s
->last_packet
;
467 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
469 #ifdef CONFIG_USER_ONLY
482 /* return -1 if error, 0 if OK */
483 static int put_packet(GDBState
*s
, const char *buf
)
486 printf("reply='%s'\n", buf
);
489 return put_packet_binary(s
, buf
, strlen(buf
));
492 /* The GDB remote protocol transfers values in target byte order. This means
493 we can use the raw memory access routines to access the value buffer.
494 Conveniently, these also handle the case where the buffer is mis-aligned.
496 #define GET_REG8(val) do { \
497 stb_p(mem_buf, val); \
500 #define GET_REG16(val) do { \
501 stw_p(mem_buf, val); \
504 #define GET_REG32(val) do { \
505 stl_p(mem_buf, val); \
508 #define GET_REG64(val) do { \
509 stq_p(mem_buf, val); \
513 #if TARGET_LONG_BITS == 64
514 #define GET_REGL(val) GET_REG64(val)
515 #define ldtul_p(addr) ldq_p(addr)
517 #define GET_REGL(val) GET_REG32(val)
518 #define ldtul_p(addr) ldl_p(addr)
521 #if defined(TARGET_I386)
523 #include "target-i386/gdbstub.c"
525 #elif defined (TARGET_PPC)
527 #if defined (TARGET_PPC64)
528 #define GDB_CORE_XML "power64-core.xml"
530 #define GDB_CORE_XML "power-core.xml"
533 #include "target-ppc/gdbstub.c"
535 #elif defined (TARGET_SPARC)
537 #include "target-sparc/gdbstub.c"
539 #elif defined (TARGET_ARM)
541 #define GDB_CORE_XML "arm-core.xml"
543 #include "target-arm/gdbstub.c"
545 #elif defined (TARGET_M68K)
547 #define GDB_CORE_XML "cf-core.xml"
549 #include "target-m68k/gdbstub.c"
551 #elif defined (TARGET_MIPS)
553 #include "target-mips/gdbstub.c"
555 #elif defined(TARGET_OPENRISC)
557 #include "target-openrisc/gdbstub.c"
559 #elif defined (TARGET_SH4)
561 #include "target-sh4/gdbstub.c"
563 #elif defined (TARGET_MICROBLAZE)
565 #include "target-microblaze/gdbstub.c"
567 #elif defined (TARGET_CRIS)
569 #include "target-cris/gdbstub.c"
571 #elif defined (TARGET_ALPHA)
573 #include "target-alpha/gdbstub.c"
575 #elif defined (TARGET_S390X)
577 static int cpu_gdb_read_register(CPUS390XState
*env
, uint8_t *mem_buf
, int n
)
583 case S390_PSWM_REGNUM
:
584 cc_op
= calc_cc(env
, env
->cc_op
, env
->cc_src
, env
->cc_dst
, env
->cc_vr
);
585 val
= deposit64(env
->psw
.mask
, 44, 2, cc_op
);
587 case S390_PSWA_REGNUM
:
588 GET_REGL(env
->psw
.addr
);
589 case S390_R0_REGNUM
... S390_R15_REGNUM
:
590 GET_REGL(env
->regs
[n
-S390_R0_REGNUM
]);
591 case S390_A0_REGNUM
... S390_A15_REGNUM
:
592 GET_REG32(env
->aregs
[n
-S390_A0_REGNUM
]);
593 case S390_FPC_REGNUM
:
595 case S390_F0_REGNUM
... S390_F15_REGNUM
:
596 GET_REG64(env
->fregs
[n
-S390_F0_REGNUM
].ll
);
602 static int cpu_gdb_write_register(CPUS390XState
*env
, uint8_t *mem_buf
, int n
)
607 tmpl
= ldtul_p(mem_buf
);
608 tmp32
= ldl_p(mem_buf
);
611 case S390_PSWM_REGNUM
:
612 env
->psw
.mask
= tmpl
;
613 env
->cc_op
= extract64(tmpl
, 44, 2);
615 case S390_PSWA_REGNUM
:
616 env
->psw
.addr
= tmpl
;
618 case S390_R0_REGNUM
... S390_R15_REGNUM
:
619 env
->regs
[n
-S390_R0_REGNUM
] = tmpl
;
621 case S390_A0_REGNUM
... S390_A15_REGNUM
:
622 env
->aregs
[n
-S390_A0_REGNUM
] = tmp32
;
625 case S390_FPC_REGNUM
:
629 case S390_F0_REGNUM
... S390_F15_REGNUM
:
630 env
->fregs
[n
-S390_F0_REGNUM
].ll
= tmpl
;
637 #elif defined (TARGET_LM32)
639 #include "hw/lm32/lm32_pic.h"
641 static int cpu_gdb_read_register(CPULM32State
*env
, uint8_t *mem_buf
, int n
)
644 GET_REG32(env
->regs
[n
]);
649 /* FIXME: put in right exception ID */
655 GET_REG32(env
->deba
);
659 GET_REG32(lm32_pic_get_im(env
->pic_state
));
661 GET_REG32(lm32_pic_get_ip(env
->pic_state
));
667 static int cpu_gdb_write_register(CPULM32State
*env
, uint8_t *mem_buf
, int n
)
669 LM32CPU
*cpu
= lm32_env_get_cpu(env
);
670 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
673 if (n
> cc
->gdb_num_core_regs
) {
677 tmp
= ldl_p(mem_buf
);
696 lm32_pic_set_im(env
->pic_state
, tmp
);
699 lm32_pic_set_ip(env
->pic_state
, tmp
);
705 #elif defined(TARGET_XTENSA)
707 static int cpu_gdb_read_register(CPUXtensaState
*env
, uint8_t *mem_buf
, int n
)
709 const XtensaGdbReg
*reg
= env
->config
->gdb_regmap
.reg
+ n
;
711 if (n
< 0 || n
>= env
->config
->gdb_regmap
.num_regs
) {
720 xtensa_sync_phys_from_window(env
);
721 GET_REG32(env
->phys_regs
[(reg
->targno
& 0xff) % env
->config
->nareg
]);
724 GET_REG32(env
->sregs
[reg
->targno
& 0xff]);
727 GET_REG32(env
->uregs
[reg
->targno
& 0xff]);
730 GET_REG32(float32_val(env
->fregs
[reg
->targno
& 0x0f]));
733 GET_REG32(env
->regs
[reg
->targno
& 0x0f]);
736 qemu_log("%s from reg %d of unsupported type %d\n",
737 __func__
, n
, reg
->type
);
742 static int cpu_gdb_write_register(CPUXtensaState
*env
, uint8_t *mem_buf
, int n
)
745 const XtensaGdbReg
*reg
= env
->config
->gdb_regmap
.reg
+ n
;
747 if (n
< 0 || n
>= env
->config
->gdb_regmap
.num_regs
) {
751 tmp
= ldl_p(mem_buf
);
759 env
->phys_regs
[(reg
->targno
& 0xff) % env
->config
->nareg
] = tmp
;
760 xtensa_sync_window_from_phys(env
);
764 env
->sregs
[reg
->targno
& 0xff] = tmp
;
768 env
->uregs
[reg
->targno
& 0xff] = tmp
;
772 env
->fregs
[reg
->targno
& 0x0f] = make_float32(tmp
);
776 env
->regs
[reg
->targno
& 0x0f] = tmp
;
780 qemu_log("%s to reg %d of unsupported type %d\n",
781 __func__
, n
, reg
->type
);
789 static int cpu_gdb_read_register(CPUArchState
*env
, uint8_t *mem_buf
, int n
)
794 static int cpu_gdb_write_register(CPUArchState
*env
, uint8_t *mem_buf
, int n
)
802 /* Encode data using the encoding for 'x' packets. */
803 static int memtox(char *buf
, const char *mem
, int len
)
811 case '#': case '$': case '*': case '}':
823 static const char *get_feature_xml(const char *p
, const char **newp
)
828 static char target_xml
[1024];
831 while (p
[len
] && p
[len
] != ':')
836 if (strncmp(p
, "target.xml", len
) == 0) {
837 /* Generate the XML description for this CPU. */
838 if (!target_xml
[0]) {
840 CPUState
*cpu
= first_cpu
;
842 snprintf(target_xml
, sizeof(target_xml
),
843 "<?xml version=\"1.0\"?>"
844 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
846 "<xi:include href=\"%s\"/>",
849 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
850 pstrcat(target_xml
, sizeof(target_xml
), "<xi:include href=\"");
851 pstrcat(target_xml
, sizeof(target_xml
), r
->xml
);
852 pstrcat(target_xml
, sizeof(target_xml
), "\"/>");
854 pstrcat(target_xml
, sizeof(target_xml
), "</target>");
859 name
= xml_builtin
[i
][0];
860 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
863 return name
? xml_builtin
[i
][1] : NULL
;
867 static int gdb_read_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
869 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
870 CPUArchState
*env
= cpu
->env_ptr
;
873 if (reg
< cc
->gdb_num_core_regs
) {
874 return cpu_gdb_read_register(env
, mem_buf
, reg
);
877 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
878 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
879 return r
->get_reg(env
, mem_buf
, reg
- r
->base_reg
);
885 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
887 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
888 CPUArchState
*env
= cpu
->env_ptr
;
891 if (reg
< cc
->gdb_num_core_regs
) {
892 return cpu_gdb_write_register(env
, mem_buf
, reg
);
895 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
896 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
897 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
903 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
904 specifies the first register number and these registers are included in
905 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
906 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
909 void gdb_register_coprocessor(CPUState
*cpu
,
910 gdb_reg_cb get_reg
, gdb_reg_cb set_reg
,
911 int num_regs
, const char *xml
, int g_pos
)
914 GDBRegisterState
**p
;
918 /* Check for duplicates. */
919 if (strcmp((*p
)->xml
, xml
) == 0)
924 s
= g_new0(GDBRegisterState
, 1);
925 s
->base_reg
= cpu
->gdb_num_regs
;
926 s
->num_regs
= num_regs
;
927 s
->get_reg
= get_reg
;
928 s
->set_reg
= set_reg
;
931 /* Add to end of list. */
932 cpu
->gdb_num_regs
+= num_regs
;
935 if (g_pos
!= s
->base_reg
) {
936 fprintf(stderr
, "Error: Bad gdb register numbering for '%s'\n"
937 "Expected %d got %d\n", xml
, g_pos
, s
->base_reg
);
942 #ifndef CONFIG_USER_ONLY
943 static const int xlat_gdb_type
[] = {
944 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
945 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
946 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
950 static int gdb_breakpoint_insert(target_ulong addr
, target_ulong len
, int type
)
957 return kvm_insert_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
961 case GDB_BREAKPOINT_SW
:
962 case GDB_BREAKPOINT_HW
:
963 for (cpu
= first_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
965 err
= cpu_breakpoint_insert(env
, addr
, BP_GDB
, NULL
);
970 #ifndef CONFIG_USER_ONLY
971 case GDB_WATCHPOINT_WRITE
:
972 case GDB_WATCHPOINT_READ
:
973 case GDB_WATCHPOINT_ACCESS
:
974 for (cpu
= first_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
976 err
= cpu_watchpoint_insert(env
, addr
, len
, xlat_gdb_type
[type
],
988 static int gdb_breakpoint_remove(target_ulong addr
, target_ulong len
, int type
)
995 return kvm_remove_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
999 case GDB_BREAKPOINT_SW
:
1000 case GDB_BREAKPOINT_HW
:
1001 for (cpu
= first_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
1003 err
= cpu_breakpoint_remove(env
, addr
, BP_GDB
);
1008 #ifndef CONFIG_USER_ONLY
1009 case GDB_WATCHPOINT_WRITE
:
1010 case GDB_WATCHPOINT_READ
:
1011 case GDB_WATCHPOINT_ACCESS
:
1012 for (cpu
= first_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
1014 err
= cpu_watchpoint_remove(env
, addr
, len
, xlat_gdb_type
[type
]);
1025 static void gdb_breakpoint_remove_all(void)
1030 if (kvm_enabled()) {
1031 kvm_remove_all_breakpoints(gdbserver_state
->c_cpu
);
1035 for (cpu
= first_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
1037 cpu_breakpoint_remove_all(env
, BP_GDB
);
1038 #ifndef CONFIG_USER_ONLY
1039 cpu_watchpoint_remove_all(env
, BP_GDB
);
1044 static void gdb_set_cpu_pc(GDBState
*s
, target_ulong pc
)
1046 CPUState
*cpu
= s
->c_cpu
;
1047 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
1049 cpu_synchronize_state(cpu
);
1051 cc
->set_pc(cpu
, pc
);
1055 static CPUState
*find_cpu(uint32_t thread_id
)
1059 for (cpu
= first_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
1060 if (cpu_index(cpu
) == thread_id
) {
1068 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
1073 int ch
, reg_size
, type
, res
;
1074 char buf
[MAX_PACKET_LENGTH
];
1075 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1077 target_ulong addr
, len
;
1080 printf("command='%s'\n", line_buf
);
1086 /* TODO: Make this return the correct value for user-mode. */
1087 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", GDB_SIGNAL_TRAP
,
1088 cpu_index(s
->c_cpu
));
1090 /* Remove all the breakpoints when this query is issued,
1091 * because gdb is doing and initial connect and the state
1092 * should be cleaned up.
1094 gdb_breakpoint_remove_all();
1098 addr
= strtoull(p
, (char **)&p
, 16);
1099 gdb_set_cpu_pc(s
, addr
);
1105 s
->signal
= gdb_signal_to_target (strtoul(p
, (char **)&p
, 16));
1106 if (s
->signal
== -1)
1111 if (strncmp(p
, "Cont", 4) == 0) {
1112 int res_signal
, res_thread
;
1116 put_packet(s
, "vCont;c;C;s;S");
1131 if (action
== 'C' || action
== 'S') {
1132 signal
= strtoul(p
, (char **)&p
, 16);
1133 } else if (action
!= 'c' && action
!= 's') {
1139 thread
= strtoull(p
+1, (char **)&p
, 16);
1141 action
= tolower(action
);
1142 if (res
== 0 || (res
== 'c' && action
== 's')) {
1144 res_signal
= signal
;
1145 res_thread
= thread
;
1149 if (res_thread
!= -1 && res_thread
!= 0) {
1150 cpu
= find_cpu(res_thread
);
1152 put_packet(s
, "E22");
1158 cpu_single_step(s
->c_cpu
, sstep_flags
);
1160 s
->signal
= res_signal
;
1166 goto unknown_command
;
1169 #ifdef CONFIG_USER_ONLY
1170 /* Kill the target */
1171 fprintf(stderr
, "\nQEMU: Terminated via GDBstub\n");
1176 gdb_breakpoint_remove_all();
1177 gdb_syscall_mode
= GDB_SYS_DISABLED
;
1179 put_packet(s
, "OK");
1183 addr
= strtoull(p
, (char **)&p
, 16);
1184 gdb_set_cpu_pc(s
, addr
);
1186 cpu_single_step(s
->c_cpu
, sstep_flags
);
1194 ret
= strtoull(p
, (char **)&p
, 16);
1197 err
= strtoull(p
, (char **)&p
, 16);
1204 if (s
->current_syscall_cb
) {
1205 s
->current_syscall_cb(s
->c_cpu
, ret
, err
);
1206 s
->current_syscall_cb
= NULL
;
1209 put_packet(s
, "T02");
1216 cpu_synchronize_state(s
->g_cpu
);
1218 for (addr
= 0; addr
< s
->g_cpu
->gdb_num_regs
; addr
++) {
1219 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
+ len
, addr
);
1222 memtohex(buf
, mem_buf
, len
);
1226 cpu_synchronize_state(s
->g_cpu
);
1227 registers
= mem_buf
;
1228 len
= strlen(p
) / 2;
1229 hextomem((uint8_t *)registers
, p
, len
);
1230 for (addr
= 0; addr
< s
->g_cpu
->gdb_num_regs
&& len
> 0; addr
++) {
1231 reg_size
= gdb_write_register(s
->g_cpu
, registers
, addr
);
1233 registers
+= reg_size
;
1235 put_packet(s
, "OK");
1238 addr
= strtoull(p
, (char **)&p
, 16);
1241 len
= strtoull(p
, NULL
, 16);
1242 if (target_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, false) != 0) {
1243 put_packet (s
, "E14");
1245 memtohex(buf
, mem_buf
, len
);
1250 addr
= strtoull(p
, (char **)&p
, 16);
1253 len
= strtoull(p
, (char **)&p
, 16);
1256 hextomem(mem_buf
, p
, len
);
1257 if (target_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
,
1259 put_packet(s
, "E14");
1261 put_packet(s
, "OK");
1265 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1266 This works, but can be very slow. Anything new enough to
1267 understand XML also knows how to use this properly. */
1269 goto unknown_command
;
1270 addr
= strtoull(p
, (char **)&p
, 16);
1271 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
, addr
);
1273 memtohex(buf
, mem_buf
, reg_size
);
1276 put_packet(s
, "E14");
1281 goto unknown_command
;
1282 addr
= strtoull(p
, (char **)&p
, 16);
1285 reg_size
= strlen(p
) / 2;
1286 hextomem(mem_buf
, p
, reg_size
);
1287 gdb_write_register(s
->g_cpu
, mem_buf
, addr
);
1288 put_packet(s
, "OK");
1292 type
= strtoul(p
, (char **)&p
, 16);
1295 addr
= strtoull(p
, (char **)&p
, 16);
1298 len
= strtoull(p
, (char **)&p
, 16);
1300 res
= gdb_breakpoint_insert(addr
, len
, type
);
1302 res
= gdb_breakpoint_remove(addr
, len
, type
);
1304 put_packet(s
, "OK");
1305 else if (res
== -ENOSYS
)
1308 put_packet(s
, "E22");
1312 thread
= strtoull(p
, (char **)&p
, 16);
1313 if (thread
== -1 || thread
== 0) {
1314 put_packet(s
, "OK");
1317 cpu
= find_cpu(thread
);
1319 put_packet(s
, "E22");
1325 put_packet(s
, "OK");
1329 put_packet(s
, "OK");
1332 put_packet(s
, "E22");
1337 thread
= strtoull(p
, (char **)&p
, 16);
1338 cpu
= find_cpu(thread
);
1341 put_packet(s
, "OK");
1343 put_packet(s
, "E22");
1348 /* parse any 'q' packets here */
1349 if (!strcmp(p
,"qemu.sstepbits")) {
1350 /* Query Breakpoint bit definitions */
1351 snprintf(buf
, sizeof(buf
), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1357 } else if (strncmp(p
,"qemu.sstep",10) == 0) {
1358 /* Display or change the sstep_flags */
1361 /* Display current setting */
1362 snprintf(buf
, sizeof(buf
), "0x%x", sstep_flags
);
1367 type
= strtoul(p
, (char **)&p
, 16);
1369 put_packet(s
, "OK");
1371 } else if (strcmp(p
,"C") == 0) {
1372 /* "Current thread" remains vague in the spec, so always return
1373 * the first CPU (gdb returns the first thread). */
1374 put_packet(s
, "QC1");
1376 } else if (strcmp(p
,"fThreadInfo") == 0) {
1377 s
->query_cpu
= first_cpu
;
1378 goto report_cpuinfo
;
1379 } else if (strcmp(p
,"sThreadInfo") == 0) {
1382 snprintf(buf
, sizeof(buf
), "m%x", cpu_index(s
->query_cpu
));
1384 s
->query_cpu
= s
->query_cpu
->next_cpu
;
1388 } else if (strncmp(p
,"ThreadExtraInfo,", 16) == 0) {
1389 thread
= strtoull(p
+16, (char **)&p
, 16);
1390 cpu
= find_cpu(thread
);
1392 cpu_synchronize_state(cpu
);
1393 len
= snprintf((char *)mem_buf
, sizeof(mem_buf
),
1394 "CPU#%d [%s]", cpu
->cpu_index
,
1395 cpu
->halted
? "halted " : "running");
1396 memtohex(buf
, mem_buf
, len
);
1401 #ifdef CONFIG_USER_ONLY
1402 else if (strncmp(p
, "Offsets", 7) == 0) {
1403 CPUArchState
*env
= s
->c_cpu
->env_ptr
;
1404 TaskState
*ts
= env
->opaque
;
1406 snprintf(buf
, sizeof(buf
),
1407 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1408 ";Bss=" TARGET_ABI_FMT_lx
,
1409 ts
->info
->code_offset
,
1410 ts
->info
->data_offset
,
1411 ts
->info
->data_offset
);
1415 #else /* !CONFIG_USER_ONLY */
1416 else if (strncmp(p
, "Rcmd,", 5) == 0) {
1417 int len
= strlen(p
+ 5);
1419 if ((len
% 2) != 0) {
1420 put_packet(s
, "E01");
1423 hextomem(mem_buf
, p
+ 5, len
);
1426 qemu_chr_be_write(s
->mon_chr
, mem_buf
, len
);
1427 put_packet(s
, "OK");
1430 #endif /* !CONFIG_USER_ONLY */
1431 if (strncmp(p
, "Supported", 9) == 0) {
1432 snprintf(buf
, sizeof(buf
), "PacketSize=%x", MAX_PACKET_LENGTH
);
1434 pstrcat(buf
, sizeof(buf
), ";qXfer:features:read+");
1440 if (strncmp(p
, "Xfer:features:read:", 19) == 0) {
1442 target_ulong total_len
;
1446 xml
= get_feature_xml(p
, &p
);
1448 snprintf(buf
, sizeof(buf
), "E00");
1455 addr
= strtoul(p
, (char **)&p
, 16);
1458 len
= strtoul(p
, (char **)&p
, 16);
1460 total_len
= strlen(xml
);
1461 if (addr
> total_len
) {
1462 snprintf(buf
, sizeof(buf
), "E00");
1466 if (len
> (MAX_PACKET_LENGTH
- 5) / 2)
1467 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1468 if (len
< total_len
- addr
) {
1470 len
= memtox(buf
+ 1, xml
+ addr
, len
);
1473 len
= memtox(buf
+ 1, xml
+ addr
, total_len
- addr
);
1475 put_packet_binary(s
, buf
, len
+ 1);
1479 /* Unrecognised 'q' command. */
1480 goto unknown_command
;
1484 /* put empty packet */
1492 void gdb_set_stop_cpu(CPUState
*cpu
)
1494 gdbserver_state
->c_cpu
= cpu
;
1495 gdbserver_state
->g_cpu
= cpu
;
1498 #ifndef CONFIG_USER_ONLY
1499 static void gdb_vm_state_change(void *opaque
, int running
, RunState state
)
1501 GDBState
*s
= gdbserver_state
;
1502 CPUArchState
*env
= s
->c_cpu
->env_ptr
;
1503 CPUState
*cpu
= s
->c_cpu
;
1508 if (running
|| s
->state
== RS_INACTIVE
) {
1511 /* Is there a GDB syscall waiting to be sent? */
1512 if (s
->current_syscall_cb
) {
1513 put_packet(s
, s
->syscall_buf
);
1517 case RUN_STATE_DEBUG
:
1518 if (env
->watchpoint_hit
) {
1519 switch (env
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
1530 snprintf(buf
, sizeof(buf
),
1531 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx
";",
1532 GDB_SIGNAL_TRAP
, cpu_index(cpu
), type
,
1533 env
->watchpoint_hit
->vaddr
);
1534 env
->watchpoint_hit
= NULL
;
1538 ret
= GDB_SIGNAL_TRAP
;
1540 case RUN_STATE_PAUSED
:
1541 ret
= GDB_SIGNAL_INT
;
1543 case RUN_STATE_SHUTDOWN
:
1544 ret
= GDB_SIGNAL_QUIT
;
1546 case RUN_STATE_IO_ERROR
:
1547 ret
= GDB_SIGNAL_IO
;
1549 case RUN_STATE_WATCHDOG
:
1550 ret
= GDB_SIGNAL_ALRM
;
1552 case RUN_STATE_INTERNAL_ERROR
:
1553 ret
= GDB_SIGNAL_ABRT
;
1555 case RUN_STATE_SAVE_VM
:
1556 case RUN_STATE_RESTORE_VM
:
1558 case RUN_STATE_FINISH_MIGRATE
:
1559 ret
= GDB_SIGNAL_XCPU
;
1562 ret
= GDB_SIGNAL_UNKNOWN
;
1565 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", ret
, cpu_index(cpu
));
1570 /* disable single step if it was enabled */
1571 cpu_single_step(cpu
, 0);
1575 /* Send a gdb syscall request.
1576 This accepts limited printf-style format specifiers, specifically:
1577 %x - target_ulong argument printed in hex.
1578 %lx - 64-bit argument printed in hex.
1579 %s - string pointer (target_ulong) and length (int) pair. */
1580 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
1589 s
= gdbserver_state
;
1592 s
->current_syscall_cb
= cb
;
1593 #ifndef CONFIG_USER_ONLY
1594 vm_stop(RUN_STATE_DEBUG
);
1598 p_end
= &s
->syscall_buf
[sizeof(s
->syscall_buf
)];
1605 addr
= va_arg(va
, target_ulong
);
1606 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
, addr
);
1609 if (*(fmt
++) != 'x')
1611 i64
= va_arg(va
, uint64_t);
1612 p
+= snprintf(p
, p_end
- p
, "%" PRIx64
, i64
);
1615 addr
= va_arg(va
, target_ulong
);
1616 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
"/%x",
1617 addr
, va_arg(va
, int));
1621 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
1631 #ifdef CONFIG_USER_ONLY
1632 put_packet(s
, s
->syscall_buf
);
1633 gdb_handlesig(s
->c_cpu
, 0);
1635 /* In this case wait to send the syscall packet until notification that
1636 the CPU has stopped. This must be done because if the packet is sent
1637 now the reply from the syscall request could be received while the CPU
1638 is still in the running state, which can cause packets to be dropped
1639 and state transition 'T' packets to be sent while the syscall is still
1645 static void gdb_read_byte(GDBState
*s
, int ch
)
1650 #ifndef CONFIG_USER_ONLY
1651 if (s
->last_packet_len
) {
1652 /* Waiting for a response to the last packet. If we see the start
1653 of a new command then abandon the previous response. */
1656 printf("Got NACK, retransmitting\n");
1658 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
1662 printf("Got ACK\n");
1664 printf("Got '%c' when expecting ACK/NACK\n", ch
);
1666 if (ch
== '+' || ch
== '$')
1667 s
->last_packet_len
= 0;
1671 if (runstate_is_running()) {
1672 /* when the CPU is running, we cannot do anything except stop
1673 it when receiving a char */
1674 vm_stop(RUN_STATE_PAUSED
);
1681 s
->line_buf_index
= 0;
1682 s
->state
= RS_GETLINE
;
1687 s
->state
= RS_CHKSUM1
;
1688 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
1691 s
->line_buf
[s
->line_buf_index
++] = ch
;
1695 s
->line_buf
[s
->line_buf_index
] = '\0';
1696 s
->line_csum
= fromhex(ch
) << 4;
1697 s
->state
= RS_CHKSUM2
;
1700 s
->line_csum
|= fromhex(ch
);
1702 for(i
= 0; i
< s
->line_buf_index
; i
++) {
1703 csum
+= s
->line_buf
[i
];
1705 if (s
->line_csum
!= (csum
& 0xff)) {
1707 put_buffer(s
, &reply
, 1);
1711 put_buffer(s
, &reply
, 1);
1712 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
1721 /* Tell the remote gdb that the process has exited. */
1722 void gdb_exit(CPUArchState
*env
, int code
)
1727 s
= gdbserver_state
;
1731 #ifdef CONFIG_USER_ONLY
1732 if (gdbserver_fd
< 0 || s
->fd
< 0) {
1737 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
1740 #ifndef CONFIG_USER_ONLY
1742 qemu_chr_delete(s
->chr
);
1747 #ifdef CONFIG_USER_ONLY
1753 s
= gdbserver_state
;
1755 if (gdbserver_fd
< 0 || s
->fd
< 0)
1762 gdb_handlesig(CPUState
*cpu
, int sig
)
1764 CPUArchState
*env
= cpu
->env_ptr
;
1769 s
= gdbserver_state
;
1770 if (gdbserver_fd
< 0 || s
->fd
< 0) {
1774 /* disable single step if it was enabled */
1775 cpu_single_step(cpu
, 0);
1779 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb(sig
));
1782 /* put_packet() might have detected that the peer terminated the
1790 s
->running_state
= 0;
1791 while (s
->running_state
== 0) {
1792 n
= read(s
->fd
, buf
, 256);
1796 for (i
= 0; i
< n
; i
++) {
1797 gdb_read_byte(s
, buf
[i
]);
1799 } else if (n
== 0 || errno
!= EAGAIN
) {
1800 /* XXX: Connection closed. Should probably wait for another
1801 connection before continuing. */
1810 /* Tell the remote gdb that the process has exited due to SIG. */
1811 void gdb_signalled(CPUArchState
*env
, int sig
)
1816 s
= gdbserver_state
;
1817 if (gdbserver_fd
< 0 || s
->fd
< 0) {
1821 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb(sig
));
1825 static void gdb_accept(void)
1828 struct sockaddr_in sockaddr
;
1833 len
= sizeof(sockaddr
);
1834 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
1835 if (fd
< 0 && errno
!= EINTR
) {
1838 } else if (fd
>= 0) {
1840 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
1846 /* set short latency */
1847 socket_set_nodelay(fd
);
1849 s
= g_malloc0(sizeof(GDBState
));
1850 s
->c_cpu
= first_cpu
;
1851 s
->g_cpu
= first_cpu
;
1855 gdbserver_state
= s
;
1857 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1860 static int gdbserver_open(int port
)
1862 struct sockaddr_in sockaddr
;
1865 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1871 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
1874 /* allow fast reuse */
1876 qemu_setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
1878 sockaddr
.sin_family
= AF_INET
;
1879 sockaddr
.sin_port
= htons(port
);
1880 sockaddr
.sin_addr
.s_addr
= 0;
1881 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
1887 ret
= listen(fd
, 0);
1896 int gdbserver_start(int port
)
1898 gdbserver_fd
= gdbserver_open(port
);
1899 if (gdbserver_fd
< 0)
1901 /* accept connections */
1906 /* Disable gdb stub for child processes. */
1907 void gdbserver_fork(CPUArchState
*env
)
1909 GDBState
*s
= gdbserver_state
;
1910 if (gdbserver_fd
< 0 || s
->fd
< 0)
1914 cpu_breakpoint_remove_all(env
, BP_GDB
);
1915 cpu_watchpoint_remove_all(env
, BP_GDB
);
1918 static int gdb_chr_can_receive(void *opaque
)
1920 /* We can handle an arbitrarily large amount of data.
1921 Pick the maximum packet size, which is as good as anything. */
1922 return MAX_PACKET_LENGTH
;
1925 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
1929 for (i
= 0; i
< size
; i
++) {
1930 gdb_read_byte(gdbserver_state
, buf
[i
]);
1934 static void gdb_chr_event(void *opaque
, int event
)
1937 case CHR_EVENT_OPENED
:
1938 vm_stop(RUN_STATE_PAUSED
);
1946 static void gdb_monitor_output(GDBState
*s
, const char *msg
, int len
)
1948 char buf
[MAX_PACKET_LENGTH
];
1951 if (len
> (MAX_PACKET_LENGTH
/2) - 1)
1952 len
= (MAX_PACKET_LENGTH
/2) - 1;
1953 memtohex(buf
+ 1, (uint8_t *)msg
, len
);
1957 static int gdb_monitor_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
1959 const char *p
= (const char *)buf
;
1962 max_sz
= (sizeof(gdbserver_state
->last_packet
) - 2) / 2;
1964 if (len
<= max_sz
) {
1965 gdb_monitor_output(gdbserver_state
, p
, len
);
1968 gdb_monitor_output(gdbserver_state
, p
, max_sz
);
1976 static void gdb_sigterm_handler(int signal
)
1978 if (runstate_is_running()) {
1979 vm_stop(RUN_STATE_PAUSED
);
1984 int gdbserver_start(const char *device
)
1987 char gdbstub_device_name
[128];
1988 CharDriverState
*chr
= NULL
;
1989 CharDriverState
*mon_chr
;
1993 if (strcmp(device
, "none") != 0) {
1994 if (strstart(device
, "tcp:", NULL
)) {
1995 /* enforce required TCP attributes */
1996 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
1997 "%s,nowait,nodelay,server", device
);
1998 device
= gdbstub_device_name
;
2001 else if (strcmp(device
, "stdio") == 0) {
2002 struct sigaction act
;
2004 memset(&act
, 0, sizeof(act
));
2005 act
.sa_handler
= gdb_sigterm_handler
;
2006 sigaction(SIGINT
, &act
, NULL
);
2009 chr
= qemu_chr_new("gdb", device
, NULL
);
2013 qemu_chr_fe_claim_no_fail(chr
);
2014 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
2015 gdb_chr_event
, NULL
);
2018 s
= gdbserver_state
;
2020 s
= g_malloc0(sizeof(GDBState
));
2021 gdbserver_state
= s
;
2023 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
2025 /* Initialize a monitor terminal for gdb */
2026 mon_chr
= g_malloc0(sizeof(*mon_chr
));
2027 mon_chr
->chr_write
= gdb_monitor_write
;
2028 monitor_init(mon_chr
, 0);
2031 qemu_chr_delete(s
->chr
);
2032 mon_chr
= s
->mon_chr
;
2033 memset(s
, 0, sizeof(GDBState
));
2035 s
->c_cpu
= first_cpu
;
2036 s
->g_cpu
= first_cpu
;
2038 s
->state
= chr
? RS_IDLE
: RS_INACTIVE
;
2039 s
->mon_chr
= mon_chr
;
2040 s
->current_syscall_cb
= NULL
;