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 /* Old gdb always expects FP registers. Newer (xml-aware) gdb only
528 expects whatever the target description contains. Due to a
529 historical mishap the FP registers appear in between core integer
530 regs and PC, MSR, CR, and so forth. We hack round this by giving the
531 FP regs zero size when talking to a newer gdb. */
532 #if defined (TARGET_PPC64)
533 #define GDB_CORE_XML "power64-core.xml"
535 #define GDB_CORE_XML "power-core.xml"
538 static int cpu_gdb_read_register(CPUPPCState
*env
, uint8_t *mem_buf
, int n
)
542 GET_REGL(env
->gpr
[n
]);
548 stfq_p(mem_buf
, env
->fpr
[n
-32]);
560 for (i
= 0; i
< 8; i
++) {
561 cr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
576 GET_REG32(env
->fpscr
);
583 static int cpu_gdb_write_register(CPUPPCState
*env
, uint8_t *mem_buf
, int n
)
587 env
->gpr
[n
] = ldtul_p(mem_buf
);
588 return sizeof(target_ulong
);
594 env
->fpr
[n
-32] = ldfq_p(mem_buf
);
599 env
->nip
= ldtul_p(mem_buf
);
600 return sizeof(target_ulong
);
602 ppc_store_msr(env
, ldtul_p(mem_buf
));
603 return sizeof(target_ulong
);
606 uint32_t cr
= ldl_p(mem_buf
);
608 for (i
= 0; i
< 8; i
++) {
609 env
->crf
[i
] = (cr
>> (32 - ((i
+ 1) * 4))) & 0xF;
614 env
->lr
= ldtul_p(mem_buf
);
615 return sizeof(target_ulong
);
617 env
->ctr
= ldtul_p(mem_buf
);
618 return sizeof(target_ulong
);
620 env
->xer
= ldtul_p(mem_buf
);
621 return sizeof(target_ulong
);
627 store_fpscr(env
, ldtul_p(mem_buf
), 0xffffffff);
628 return sizeof(target_ulong
);
634 #elif defined (TARGET_SPARC)
637 #define GET_REGA(val) GET_REG32(val)
639 #define GET_REGA(val) GET_REGL(val)
642 static int cpu_gdb_read_register(CPUSPARCState
*env
, uint8_t *mem_buf
, int n
)
646 GET_REGA(env
->gregs
[n
]);
649 /* register window */
650 GET_REGA(env
->regwptr
[n
- 8]);
652 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
656 GET_REG32(env
->fpr
[(n
- 32) / 2].l
.lower
);
658 GET_REG32(env
->fpr
[(n
- 32) / 2].l
.upper
);
661 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
666 GET_REGA(cpu_get_psr(env
));
678 GET_REGA(0); /* csr */
686 GET_REG32(env
->fpr
[(n
- 32) / 2].l
.lower
);
688 GET_REG32(env
->fpr
[(n
- 32) / 2].l
.upper
);
692 /* f32-f62 (double width, even numbers only) */
693 GET_REG64(env
->fpr
[(n
- 32) / 2].ll
);
701 GET_REGL((cpu_get_ccr(env
) << 32) |
702 ((env
->asi
& 0xff) << 24) |
703 ((env
->pstate
& 0xfff) << 8) |
716 static int cpu_gdb_write_register(CPUSPARCState
*env
, uint8_t *mem_buf
, int n
)
718 #if defined(TARGET_ABI32)
721 tmp
= ldl_p(mem_buf
);
725 tmp
= ldtul_p(mem_buf
);
732 /* register window */
733 env
->regwptr
[n
- 8] = tmp
;
735 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
740 env
->fpr
[(n
- 32) / 2].l
.lower
= tmp
;
742 env
->fpr
[(n
- 32) / 2].l
.upper
= tmp
;
745 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
751 cpu_put_psr(env
, tmp
);
776 tmp
= ldl_p(mem_buf
);
778 env
->fpr
[(n
- 32) / 2].l
.lower
= tmp
;
780 env
->fpr
[(n
- 32) / 2].l
.upper
= tmp
;
784 /* f32-f62 (double width, even numbers only) */
785 env
->fpr
[(n
- 32) / 2].ll
= tmp
;
795 cpu_put_ccr(env
, tmp
>> 32);
796 env
->asi
= (tmp
>> 24) & 0xff;
797 env
->pstate
= (tmp
>> 8) & 0xfff;
798 cpu_put_cwp64(env
, tmp
& 0xff);
816 #elif defined (TARGET_ARM)
818 /* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect
819 whatever the target description contains. Due to a historical mishap
820 the FPA registers appear in between core integer regs and the CPSR.
821 We hack round this by giving the FPA regs zero size when talking to a
823 #define GDB_CORE_XML "arm-core.xml"
825 static int cpu_gdb_read_register(CPUARMState
*env
, uint8_t *mem_buf
, int n
)
828 /* Core integer register. */
829 GET_REG32(env
->regs
[n
]);
836 memset(mem_buf
, 0, 12);
841 /* FPA status register. */
848 GET_REG32(cpsr_read(env
));
850 /* Unknown register. */
854 static int cpu_gdb_write_register(CPUARMState
*env
, uint8_t *mem_buf
, int n
)
858 tmp
= ldl_p(mem_buf
);
860 /* Mask out low bit of PC to workaround gdb bugs. This will probably
861 cause problems if we ever implement the Jazelle DBX extensions. */
867 /* Core integer register. */
871 if (n
< 24) { /* 16-23 */
872 /* FPA registers (ignored). */
880 /* FPA status register (ignored). */
887 cpsr_write(env
, tmp
, 0xffffffff);
890 /* Unknown register. */
894 #elif defined (TARGET_M68K)
896 #define GDB_CORE_XML "cf-core.xml"
898 static int cpu_gdb_read_register(CPUM68KState
*env
, uint8_t *mem_buf
, int n
)
902 GET_REG32(env
->dregs
[n
]);
905 GET_REG32(env
->aregs
[n
- 8]);
914 /* FP registers not included here because they vary between
915 ColdFire and m68k. Use XML bits for these. */
919 static int cpu_gdb_write_register(CPUM68KState
*env
, uint8_t *mem_buf
, int n
)
923 tmp
= ldl_p(mem_buf
);
930 env
->aregs
[n
- 8] = tmp
;
945 #elif defined (TARGET_MIPS)
947 static int cpu_gdb_read_register(CPUMIPSState
*env
, uint8_t *mem_buf
, int n
)
950 GET_REGL(env
->active_tc
.gpr
[n
]);
952 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
953 if (n
>= 38 && n
< 70) {
954 if (env
->CP0_Status
& (1 << CP0St_FR
)) {
955 GET_REGL(env
->active_fpu
.fpr
[n
- 38].d
);
957 GET_REGL(env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
]);
962 GET_REGL((int32_t)env
->active_fpu
.fcr31
);
964 GET_REGL((int32_t)env
->active_fpu
.fcr0
);
969 GET_REGL((int32_t)env
->CP0_Status
);
971 GET_REGL(env
->active_tc
.LO
[0]);
973 GET_REGL(env
->active_tc
.HI
[0]);
975 GET_REGL(env
->CP0_BadVAddr
);
977 GET_REGL((int32_t)env
->CP0_Cause
);
979 GET_REGL(env
->active_tc
.PC
| !!(env
->hflags
& MIPS_HFLAG_M16
));
981 GET_REGL(0); /* fp */
983 GET_REGL((int32_t)env
->CP0_PRid
);
985 if (n
>= 73 && n
<= 88) {
986 /* 16 embedded regs. */
993 /* convert MIPS rounding mode in FCR31 to IEEE library */
994 static unsigned int ieee_rm
[] = {
995 float_round_nearest_even
,
1000 #define RESTORE_ROUNDING_MODE \
1001 set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], \
1002 &env->active_fpu.fp_status)
1004 static int cpu_gdb_write_register(CPUMIPSState
*env
, uint8_t *mem_buf
, int n
)
1008 tmp
= ldtul_p(mem_buf
);
1011 env
->active_tc
.gpr
[n
] = tmp
;
1012 return sizeof(target_ulong
);
1014 if (env
->CP0_Config1
& (1 << CP0C1_FP
)
1015 && n
>= 38 && n
< 73) {
1017 if (env
->CP0_Status
& (1 << CP0St_FR
)) {
1018 env
->active_fpu
.fpr
[n
- 38].d
= tmp
;
1020 env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
] = tmp
;
1025 env
->active_fpu
.fcr31
= tmp
& 0xFF83FFFF;
1026 /* set rounding mode */
1027 RESTORE_ROUNDING_MODE
;
1030 env
->active_fpu
.fcr0
= tmp
;
1033 return sizeof(target_ulong
);
1037 env
->CP0_Status
= tmp
;
1040 env
->active_tc
.LO
[0] = tmp
;
1043 env
->active_tc
.HI
[0] = tmp
;
1046 env
->CP0_BadVAddr
= tmp
;
1049 env
->CP0_Cause
= tmp
;
1052 env
->active_tc
.PC
= tmp
& ~(target_ulong
)1;
1054 env
->hflags
|= MIPS_HFLAG_M16
;
1056 env
->hflags
&= ~(MIPS_HFLAG_M16
);
1059 case 72: /* fp, ignored */
1065 /* Other registers are readonly. Ignore writes. */
1069 return sizeof(target_ulong
);
1071 #elif defined(TARGET_OPENRISC)
1073 static int cpu_gdb_read_register(CPUOpenRISCState
*env
, uint8_t *mem_buf
, int n
)
1076 GET_REG32(env
->gpr
[n
]);
1080 GET_REG32(env
->ppc
);
1083 GET_REG32(env
->npc
);
1095 static int cpu_gdb_write_register(CPUOpenRISCState
*env
,
1096 uint8_t *mem_buf
, int n
)
1098 OpenRISCCPU
*cpu
= openrisc_env_get_cpu(env
);
1099 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
1102 if (n
> cc
->gdb_num_core_regs
) {
1106 tmp
= ldl_p(mem_buf
);
1130 #elif defined (TARGET_SH4)
1132 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
1133 /* FIXME: We should use XML for this. */
1135 static int cpu_gdb_read_register(CPUSH4State
*env
, uint8_t *mem_buf
, int n
)
1139 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1140 GET_REGL(env
->gregs
[n
+ 16]);
1142 GET_REGL(env
->gregs
[n
]);
1145 GET_REGL(env
->gregs
[n
]);
1155 GET_REGL(env
->mach
);
1157 GET_REGL(env
->macl
);
1161 GET_REGL(env
->fpul
);
1163 GET_REGL(env
->fpscr
);
1165 if (env
->fpscr
& FPSCR_FR
) {
1166 stfl_p(mem_buf
, env
->fregs
[n
- 9]);
1168 stfl_p(mem_buf
, env
->fregs
[n
- 25]);
1176 GET_REGL(env
->gregs
[n
- 43]);
1178 GET_REGL(env
->gregs
[n
- (51 - 16)]);
1184 static int cpu_gdb_write_register(CPUSH4State
*env
, uint8_t *mem_buf
, int n
)
1188 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1189 env
->gregs
[n
+ 16] = ldl_p(mem_buf
);
1191 env
->gregs
[n
] = ldl_p(mem_buf
);
1195 env
->gregs
[n
] = ldl_p(mem_buf
);
1198 env
->pc
= ldl_p(mem_buf
);
1201 env
->pr
= ldl_p(mem_buf
);
1204 env
->gbr
= ldl_p(mem_buf
);
1207 env
->vbr
= ldl_p(mem_buf
);
1210 env
->mach
= ldl_p(mem_buf
);
1213 env
->macl
= ldl_p(mem_buf
);
1216 env
->sr
= ldl_p(mem_buf
);
1219 env
->fpul
= ldl_p(mem_buf
);
1222 env
->fpscr
= ldl_p(mem_buf
);
1225 if (env
->fpscr
& FPSCR_FR
) {
1226 env
->fregs
[n
- 9] = ldfl_p(mem_buf
);
1228 env
->fregs
[n
- 25] = ldfl_p(mem_buf
);
1232 env
->ssr
= ldl_p(mem_buf
);
1235 env
->spc
= ldl_p(mem_buf
);
1238 env
->gregs
[n
- 43] = ldl_p(mem_buf
);
1241 env
->gregs
[n
- (51 - 16)] = ldl_p(mem_buf
);
1249 #elif defined (TARGET_MICROBLAZE)
1251 static int cpu_gdb_read_register(CPUMBState
*env
, uint8_t *mem_buf
, int n
)
1254 GET_REG32(env
->regs
[n
]);
1256 GET_REG32(env
->sregs
[n
- 32]);
1261 static int cpu_gdb_write_register(CPUMBState
*env
, uint8_t *mem_buf
, int n
)
1263 MicroBlazeCPU
*cpu
= mb_env_get_cpu(env
);
1264 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
1267 if (n
> cc
->gdb_num_core_regs
) {
1271 tmp
= ldl_p(mem_buf
);
1276 env
->sregs
[n
- 32] = tmp
;
1280 #elif defined (TARGET_CRIS)
1283 read_register_crisv10(CPUCRISState
*env
, uint8_t *mem_buf
, int n
)
1286 GET_REG32(env
->regs
[n
]);
1296 GET_REG8(env
->pregs
[n
- 16]);
1298 GET_REG8(env
->pregs
[n
- 16]);
1301 GET_REG16(env
->pregs
[n
- 16]);
1304 GET_REG32(env
->pregs
[n
- 16]);
1312 static int cpu_gdb_read_register(CPUCRISState
*env
, uint8_t *mem_buf
, int n
)
1316 if (env
->pregs
[PR_VR
] < 32) {
1317 return read_register_crisv10(env
, mem_buf
, n
);
1320 srs
= env
->pregs
[PR_SRS
];
1322 GET_REG32(env
->regs
[n
]);
1325 if (n
>= 21 && n
< 32) {
1326 GET_REG32(env
->pregs
[n
- 16]);
1328 if (n
>= 33 && n
< 49) {
1329 GET_REG32(env
->sregs
[srs
][n
- 33]);
1333 GET_REG8(env
->pregs
[0]);
1335 GET_REG8(env
->pregs
[1]);
1337 GET_REG32(env
->pregs
[2]);
1341 GET_REG16(env
->pregs
[4]);
1349 static int cpu_gdb_write_register(CPUCRISState
*env
, uint8_t *mem_buf
, int n
)
1357 tmp
= ldl_p(mem_buf
);
1363 if (n
>= 21 && n
< 32) {
1364 env
->pregs
[n
- 16] = tmp
;
1367 /* FIXME: Should support function regs be writable? */
1374 env
->pregs
[PR_PID
] = tmp
;
1387 #elif defined (TARGET_ALPHA)
1389 static int cpu_gdb_read_register(CPUAlphaState
*env
, uint8_t *mem_buf
, int n
)
1399 d
.d
= env
->fir
[n
- 32];
1403 val
= cpu_alpha_load_fpcr(env
);
1413 /* 31 really is the zero register; 65 is unassigned in the
1414 gdb protocol, but is still required to occupy 8 bytes. */
1423 static int cpu_gdb_write_register(CPUAlphaState
*env
, uint8_t *mem_buf
, int n
)
1425 target_ulong tmp
= ldtul_p(mem_buf
);
1434 env
->fir
[n
- 32] = d
.d
;
1437 cpu_alpha_store_fpcr(env
, tmp
);
1447 /* 31 really is the zero register; 65 is unassigned in the
1448 gdb protocol, but is still required to occupy 8 bytes. */
1455 #elif defined (TARGET_S390X)
1457 static int cpu_gdb_read_register(CPUS390XState
*env
, uint8_t *mem_buf
, int n
)
1463 case S390_PSWM_REGNUM
:
1464 cc_op
= calc_cc(env
, env
->cc_op
, env
->cc_src
, env
->cc_dst
, env
->cc_vr
);
1465 val
= deposit64(env
->psw
.mask
, 44, 2, cc_op
);
1467 case S390_PSWA_REGNUM
:
1468 GET_REGL(env
->psw
.addr
);
1469 case S390_R0_REGNUM
... S390_R15_REGNUM
:
1470 GET_REGL(env
->regs
[n
-S390_R0_REGNUM
]);
1471 case S390_A0_REGNUM
... S390_A15_REGNUM
:
1472 GET_REG32(env
->aregs
[n
-S390_A0_REGNUM
]);
1473 case S390_FPC_REGNUM
:
1474 GET_REG32(env
->fpc
);
1475 case S390_F0_REGNUM
... S390_F15_REGNUM
:
1476 GET_REG64(env
->fregs
[n
-S390_F0_REGNUM
].ll
);
1482 static int cpu_gdb_write_register(CPUS390XState
*env
, uint8_t *mem_buf
, int n
)
1487 tmpl
= ldtul_p(mem_buf
);
1488 tmp32
= ldl_p(mem_buf
);
1491 case S390_PSWM_REGNUM
:
1492 env
->psw
.mask
= tmpl
;
1493 env
->cc_op
= extract64(tmpl
, 44, 2);
1495 case S390_PSWA_REGNUM
:
1496 env
->psw
.addr
= tmpl
;
1498 case S390_R0_REGNUM
... S390_R15_REGNUM
:
1499 env
->regs
[n
-S390_R0_REGNUM
] = tmpl
;
1501 case S390_A0_REGNUM
... S390_A15_REGNUM
:
1502 env
->aregs
[n
-S390_A0_REGNUM
] = tmp32
;
1505 case S390_FPC_REGNUM
:
1509 case S390_F0_REGNUM
... S390_F15_REGNUM
:
1510 env
->fregs
[n
-S390_F0_REGNUM
].ll
= tmpl
;
1517 #elif defined (TARGET_LM32)
1519 #include "hw/lm32/lm32_pic.h"
1521 static int cpu_gdb_read_register(CPULM32State
*env
, uint8_t *mem_buf
, int n
)
1524 GET_REG32(env
->regs
[n
]);
1529 /* FIXME: put in right exception ID */
1533 GET_REG32(env
->eba
);
1535 GET_REG32(env
->deba
);
1539 GET_REG32(lm32_pic_get_im(env
->pic_state
));
1541 GET_REG32(lm32_pic_get_ip(env
->pic_state
));
1547 static int cpu_gdb_write_register(CPULM32State
*env
, uint8_t *mem_buf
, int n
)
1549 LM32CPU
*cpu
= lm32_env_get_cpu(env
);
1550 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
1553 if (n
> cc
->gdb_num_core_regs
) {
1557 tmp
= ldl_p(mem_buf
);
1576 lm32_pic_set_im(env
->pic_state
, tmp
);
1579 lm32_pic_set_ip(env
->pic_state
, tmp
);
1585 #elif defined(TARGET_XTENSA)
1587 static int cpu_gdb_read_register(CPUXtensaState
*env
, uint8_t *mem_buf
, int n
)
1589 const XtensaGdbReg
*reg
= env
->config
->gdb_regmap
.reg
+ n
;
1591 if (n
< 0 || n
>= env
->config
->gdb_regmap
.num_regs
) {
1595 switch (reg
->type
) {
1600 xtensa_sync_phys_from_window(env
);
1601 GET_REG32(env
->phys_regs
[(reg
->targno
& 0xff) % env
->config
->nareg
]);
1604 GET_REG32(env
->sregs
[reg
->targno
& 0xff]);
1607 GET_REG32(env
->uregs
[reg
->targno
& 0xff]);
1610 GET_REG32(float32_val(env
->fregs
[reg
->targno
& 0x0f]));
1613 GET_REG32(env
->regs
[reg
->targno
& 0x0f]);
1616 qemu_log("%s from reg %d of unsupported type %d\n",
1617 __func__
, n
, reg
->type
);
1622 static int cpu_gdb_write_register(CPUXtensaState
*env
, uint8_t *mem_buf
, int n
)
1625 const XtensaGdbReg
*reg
= env
->config
->gdb_regmap
.reg
+ n
;
1627 if (n
< 0 || n
>= env
->config
->gdb_regmap
.num_regs
) {
1631 tmp
= ldl_p(mem_buf
);
1633 switch (reg
->type
) {
1639 env
->phys_regs
[(reg
->targno
& 0xff) % env
->config
->nareg
] = tmp
;
1640 xtensa_sync_window_from_phys(env
);
1644 env
->sregs
[reg
->targno
& 0xff] = tmp
;
1648 env
->uregs
[reg
->targno
& 0xff] = tmp
;
1652 env
->fregs
[reg
->targno
& 0x0f] = make_float32(tmp
);
1656 env
->regs
[reg
->targno
& 0x0f] = tmp
;
1660 qemu_log("%s to reg %d of unsupported type %d\n",
1661 __func__
, n
, reg
->type
);
1669 static int cpu_gdb_read_register(CPUArchState
*env
, uint8_t *mem_buf
, int n
)
1674 static int cpu_gdb_write_register(CPUArchState
*env
, uint8_t *mem_buf
, int n
)
1682 /* Encode data using the encoding for 'x' packets. */
1683 static int memtox(char *buf
, const char *mem
, int len
)
1691 case '#': case '$': case '*': case '}':
1703 static const char *get_feature_xml(const char *p
, const char **newp
)
1708 static char target_xml
[1024];
1711 while (p
[len
] && p
[len
] != ':')
1716 if (strncmp(p
, "target.xml", len
) == 0) {
1717 /* Generate the XML description for this CPU. */
1718 if (!target_xml
[0]) {
1719 GDBRegisterState
*r
;
1720 CPUState
*cpu
= first_cpu
;
1722 snprintf(target_xml
, sizeof(target_xml
),
1723 "<?xml version=\"1.0\"?>"
1724 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
1726 "<xi:include href=\"%s\"/>",
1729 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
1730 pstrcat(target_xml
, sizeof(target_xml
), "<xi:include href=\"");
1731 pstrcat(target_xml
, sizeof(target_xml
), r
->xml
);
1732 pstrcat(target_xml
, sizeof(target_xml
), "\"/>");
1734 pstrcat(target_xml
, sizeof(target_xml
), "</target>");
1738 for (i
= 0; ; i
++) {
1739 name
= xml_builtin
[i
][0];
1740 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
1743 return name
? xml_builtin
[i
][1] : NULL
;
1747 static int gdb_read_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
1749 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
1750 CPUArchState
*env
= cpu
->env_ptr
;
1751 GDBRegisterState
*r
;
1753 if (reg
< cc
->gdb_num_core_regs
) {
1754 return cpu_gdb_read_register(env
, mem_buf
, reg
);
1757 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
1758 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1759 return r
->get_reg(env
, mem_buf
, reg
- r
->base_reg
);
1765 static int gdb_write_register(CPUState
*cpu
, uint8_t *mem_buf
, int reg
)
1767 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
1768 CPUArchState
*env
= cpu
->env_ptr
;
1769 GDBRegisterState
*r
;
1771 if (reg
< cc
->gdb_num_core_regs
) {
1772 return cpu_gdb_write_register(env
, mem_buf
, reg
);
1775 for (r
= cpu
->gdb_regs
; r
; r
= r
->next
) {
1776 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1777 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
1783 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
1784 specifies the first register number and these registers are included in
1785 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
1786 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
1789 void gdb_register_coprocessor(CPUState
*cpu
,
1790 gdb_reg_cb get_reg
, gdb_reg_cb set_reg
,
1791 int num_regs
, const char *xml
, int g_pos
)
1793 GDBRegisterState
*s
;
1794 GDBRegisterState
**p
;
1798 /* Check for duplicates. */
1799 if (strcmp((*p
)->xml
, xml
) == 0)
1804 s
= g_new0(GDBRegisterState
, 1);
1805 s
->base_reg
= cpu
->gdb_num_regs
;
1806 s
->num_regs
= num_regs
;
1807 s
->get_reg
= get_reg
;
1808 s
->set_reg
= set_reg
;
1811 /* Add to end of list. */
1812 cpu
->gdb_num_regs
+= num_regs
;
1815 if (g_pos
!= s
->base_reg
) {
1816 fprintf(stderr
, "Error: Bad gdb register numbering for '%s'\n"
1817 "Expected %d got %d\n", xml
, g_pos
, s
->base_reg
);
1822 #ifndef CONFIG_USER_ONLY
1823 static const int xlat_gdb_type
[] = {
1824 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
1825 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
1826 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
1830 static int gdb_breakpoint_insert(target_ulong addr
, target_ulong len
, int type
)
1836 if (kvm_enabled()) {
1837 return kvm_insert_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1841 case GDB_BREAKPOINT_SW
:
1842 case GDB_BREAKPOINT_HW
:
1843 for (cpu
= first_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
1845 err
= cpu_breakpoint_insert(env
, addr
, BP_GDB
, NULL
);
1850 #ifndef CONFIG_USER_ONLY
1851 case GDB_WATCHPOINT_WRITE
:
1852 case GDB_WATCHPOINT_READ
:
1853 case GDB_WATCHPOINT_ACCESS
:
1854 for (cpu
= first_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
1856 err
= cpu_watchpoint_insert(env
, addr
, len
, xlat_gdb_type
[type
],
1868 static int gdb_breakpoint_remove(target_ulong addr
, target_ulong len
, int type
)
1874 if (kvm_enabled()) {
1875 return kvm_remove_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1879 case GDB_BREAKPOINT_SW
:
1880 case GDB_BREAKPOINT_HW
:
1881 for (cpu
= first_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
1883 err
= cpu_breakpoint_remove(env
, addr
, BP_GDB
);
1888 #ifndef CONFIG_USER_ONLY
1889 case GDB_WATCHPOINT_WRITE
:
1890 case GDB_WATCHPOINT_READ
:
1891 case GDB_WATCHPOINT_ACCESS
:
1892 for (cpu
= first_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
1894 err
= cpu_watchpoint_remove(env
, addr
, len
, xlat_gdb_type
[type
]);
1905 static void gdb_breakpoint_remove_all(void)
1910 if (kvm_enabled()) {
1911 kvm_remove_all_breakpoints(gdbserver_state
->c_cpu
);
1915 for (cpu
= first_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
1917 cpu_breakpoint_remove_all(env
, BP_GDB
);
1918 #ifndef CONFIG_USER_ONLY
1919 cpu_watchpoint_remove_all(env
, BP_GDB
);
1924 static void gdb_set_cpu_pc(GDBState
*s
, target_ulong pc
)
1926 CPUState
*cpu
= s
->c_cpu
;
1927 CPUClass
*cc
= CPU_GET_CLASS(cpu
);
1929 cpu_synchronize_state(cpu
);
1931 cc
->set_pc(cpu
, pc
);
1935 static CPUState
*find_cpu(uint32_t thread_id
)
1939 for (cpu
= first_cpu
; cpu
!= NULL
; cpu
= cpu
->next_cpu
) {
1940 if (cpu_index(cpu
) == thread_id
) {
1948 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
1953 int ch
, reg_size
, type
, res
;
1954 char buf
[MAX_PACKET_LENGTH
];
1955 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1957 target_ulong addr
, len
;
1960 printf("command='%s'\n", line_buf
);
1966 /* TODO: Make this return the correct value for user-mode. */
1967 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", GDB_SIGNAL_TRAP
,
1968 cpu_index(s
->c_cpu
));
1970 /* Remove all the breakpoints when this query is issued,
1971 * because gdb is doing and initial connect and the state
1972 * should be cleaned up.
1974 gdb_breakpoint_remove_all();
1978 addr
= strtoull(p
, (char **)&p
, 16);
1979 gdb_set_cpu_pc(s
, addr
);
1985 s
->signal
= gdb_signal_to_target (strtoul(p
, (char **)&p
, 16));
1986 if (s
->signal
== -1)
1991 if (strncmp(p
, "Cont", 4) == 0) {
1992 int res_signal
, res_thread
;
1996 put_packet(s
, "vCont;c;C;s;S");
2011 if (action
== 'C' || action
== 'S') {
2012 signal
= strtoul(p
, (char **)&p
, 16);
2013 } else if (action
!= 'c' && action
!= 's') {
2019 thread
= strtoull(p
+1, (char **)&p
, 16);
2021 action
= tolower(action
);
2022 if (res
== 0 || (res
== 'c' && action
== 's')) {
2024 res_signal
= signal
;
2025 res_thread
= thread
;
2029 if (res_thread
!= -1 && res_thread
!= 0) {
2030 cpu
= find_cpu(res_thread
);
2032 put_packet(s
, "E22");
2038 cpu_single_step(s
->c_cpu
, sstep_flags
);
2040 s
->signal
= res_signal
;
2046 goto unknown_command
;
2049 #ifdef CONFIG_USER_ONLY
2050 /* Kill the target */
2051 fprintf(stderr
, "\nQEMU: Terminated via GDBstub\n");
2056 gdb_breakpoint_remove_all();
2057 gdb_syscall_mode
= GDB_SYS_DISABLED
;
2059 put_packet(s
, "OK");
2063 addr
= strtoull(p
, (char **)&p
, 16);
2064 gdb_set_cpu_pc(s
, addr
);
2066 cpu_single_step(s
->c_cpu
, sstep_flags
);
2074 ret
= strtoull(p
, (char **)&p
, 16);
2077 err
= strtoull(p
, (char **)&p
, 16);
2084 if (s
->current_syscall_cb
) {
2085 s
->current_syscall_cb(s
->c_cpu
, ret
, err
);
2086 s
->current_syscall_cb
= NULL
;
2089 put_packet(s
, "T02");
2096 cpu_synchronize_state(s
->g_cpu
);
2098 for (addr
= 0; addr
< s
->g_cpu
->gdb_num_regs
; addr
++) {
2099 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
+ len
, addr
);
2102 memtohex(buf
, mem_buf
, len
);
2106 cpu_synchronize_state(s
->g_cpu
);
2107 registers
= mem_buf
;
2108 len
= strlen(p
) / 2;
2109 hextomem((uint8_t *)registers
, p
, len
);
2110 for (addr
= 0; addr
< s
->g_cpu
->gdb_num_regs
&& len
> 0; addr
++) {
2111 reg_size
= gdb_write_register(s
->g_cpu
, registers
, addr
);
2113 registers
+= reg_size
;
2115 put_packet(s
, "OK");
2118 addr
= strtoull(p
, (char **)&p
, 16);
2121 len
= strtoull(p
, NULL
, 16);
2122 if (target_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, false) != 0) {
2123 put_packet (s
, "E14");
2125 memtohex(buf
, mem_buf
, len
);
2130 addr
= strtoull(p
, (char **)&p
, 16);
2133 len
= strtoull(p
, (char **)&p
, 16);
2136 hextomem(mem_buf
, p
, len
);
2137 if (target_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
,
2139 put_packet(s
, "E14");
2141 put_packet(s
, "OK");
2145 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
2146 This works, but can be very slow. Anything new enough to
2147 understand XML also knows how to use this properly. */
2149 goto unknown_command
;
2150 addr
= strtoull(p
, (char **)&p
, 16);
2151 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
, addr
);
2153 memtohex(buf
, mem_buf
, reg_size
);
2156 put_packet(s
, "E14");
2161 goto unknown_command
;
2162 addr
= strtoull(p
, (char **)&p
, 16);
2165 reg_size
= strlen(p
) / 2;
2166 hextomem(mem_buf
, p
, reg_size
);
2167 gdb_write_register(s
->g_cpu
, mem_buf
, addr
);
2168 put_packet(s
, "OK");
2172 type
= strtoul(p
, (char **)&p
, 16);
2175 addr
= strtoull(p
, (char **)&p
, 16);
2178 len
= strtoull(p
, (char **)&p
, 16);
2180 res
= gdb_breakpoint_insert(addr
, len
, type
);
2182 res
= gdb_breakpoint_remove(addr
, len
, type
);
2184 put_packet(s
, "OK");
2185 else if (res
== -ENOSYS
)
2188 put_packet(s
, "E22");
2192 thread
= strtoull(p
, (char **)&p
, 16);
2193 if (thread
== -1 || thread
== 0) {
2194 put_packet(s
, "OK");
2197 cpu
= find_cpu(thread
);
2199 put_packet(s
, "E22");
2205 put_packet(s
, "OK");
2209 put_packet(s
, "OK");
2212 put_packet(s
, "E22");
2217 thread
= strtoull(p
, (char **)&p
, 16);
2218 cpu
= find_cpu(thread
);
2221 put_packet(s
, "OK");
2223 put_packet(s
, "E22");
2228 /* parse any 'q' packets here */
2229 if (!strcmp(p
,"qemu.sstepbits")) {
2230 /* Query Breakpoint bit definitions */
2231 snprintf(buf
, sizeof(buf
), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
2237 } else if (strncmp(p
,"qemu.sstep",10) == 0) {
2238 /* Display or change the sstep_flags */
2241 /* Display current setting */
2242 snprintf(buf
, sizeof(buf
), "0x%x", sstep_flags
);
2247 type
= strtoul(p
, (char **)&p
, 16);
2249 put_packet(s
, "OK");
2251 } else if (strcmp(p
,"C") == 0) {
2252 /* "Current thread" remains vague in the spec, so always return
2253 * the first CPU (gdb returns the first thread). */
2254 put_packet(s
, "QC1");
2256 } else if (strcmp(p
,"fThreadInfo") == 0) {
2257 s
->query_cpu
= first_cpu
;
2258 goto report_cpuinfo
;
2259 } else if (strcmp(p
,"sThreadInfo") == 0) {
2262 snprintf(buf
, sizeof(buf
), "m%x", cpu_index(s
->query_cpu
));
2264 s
->query_cpu
= s
->query_cpu
->next_cpu
;
2268 } else if (strncmp(p
,"ThreadExtraInfo,", 16) == 0) {
2269 thread
= strtoull(p
+16, (char **)&p
, 16);
2270 cpu
= find_cpu(thread
);
2272 cpu_synchronize_state(cpu
);
2273 len
= snprintf((char *)mem_buf
, sizeof(mem_buf
),
2274 "CPU#%d [%s]", cpu
->cpu_index
,
2275 cpu
->halted
? "halted " : "running");
2276 memtohex(buf
, mem_buf
, len
);
2281 #ifdef CONFIG_USER_ONLY
2282 else if (strncmp(p
, "Offsets", 7) == 0) {
2283 CPUArchState
*env
= s
->c_cpu
->env_ptr
;
2284 TaskState
*ts
= env
->opaque
;
2286 snprintf(buf
, sizeof(buf
),
2287 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
2288 ";Bss=" TARGET_ABI_FMT_lx
,
2289 ts
->info
->code_offset
,
2290 ts
->info
->data_offset
,
2291 ts
->info
->data_offset
);
2295 #else /* !CONFIG_USER_ONLY */
2296 else if (strncmp(p
, "Rcmd,", 5) == 0) {
2297 int len
= strlen(p
+ 5);
2299 if ((len
% 2) != 0) {
2300 put_packet(s
, "E01");
2303 hextomem(mem_buf
, p
+ 5, len
);
2306 qemu_chr_be_write(s
->mon_chr
, mem_buf
, len
);
2307 put_packet(s
, "OK");
2310 #endif /* !CONFIG_USER_ONLY */
2311 if (strncmp(p
, "Supported", 9) == 0) {
2312 snprintf(buf
, sizeof(buf
), "PacketSize=%x", MAX_PACKET_LENGTH
);
2314 pstrcat(buf
, sizeof(buf
), ";qXfer:features:read+");
2320 if (strncmp(p
, "Xfer:features:read:", 19) == 0) {
2322 target_ulong total_len
;
2326 xml
= get_feature_xml(p
, &p
);
2328 snprintf(buf
, sizeof(buf
), "E00");
2335 addr
= strtoul(p
, (char **)&p
, 16);
2338 len
= strtoul(p
, (char **)&p
, 16);
2340 total_len
= strlen(xml
);
2341 if (addr
> total_len
) {
2342 snprintf(buf
, sizeof(buf
), "E00");
2346 if (len
> (MAX_PACKET_LENGTH
- 5) / 2)
2347 len
= (MAX_PACKET_LENGTH
- 5) / 2;
2348 if (len
< total_len
- addr
) {
2350 len
= memtox(buf
+ 1, xml
+ addr
, len
);
2353 len
= memtox(buf
+ 1, xml
+ addr
, total_len
- addr
);
2355 put_packet_binary(s
, buf
, len
+ 1);
2359 /* Unrecognised 'q' command. */
2360 goto unknown_command
;
2364 /* put empty packet */
2372 void gdb_set_stop_cpu(CPUState
*cpu
)
2374 gdbserver_state
->c_cpu
= cpu
;
2375 gdbserver_state
->g_cpu
= cpu
;
2378 #ifndef CONFIG_USER_ONLY
2379 static void gdb_vm_state_change(void *opaque
, int running
, RunState state
)
2381 GDBState
*s
= gdbserver_state
;
2382 CPUArchState
*env
= s
->c_cpu
->env_ptr
;
2383 CPUState
*cpu
= s
->c_cpu
;
2388 if (running
|| s
->state
== RS_INACTIVE
) {
2391 /* Is there a GDB syscall waiting to be sent? */
2392 if (s
->current_syscall_cb
) {
2393 put_packet(s
, s
->syscall_buf
);
2397 case RUN_STATE_DEBUG
:
2398 if (env
->watchpoint_hit
) {
2399 switch (env
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
2410 snprintf(buf
, sizeof(buf
),
2411 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx
";",
2412 GDB_SIGNAL_TRAP
, cpu_index(cpu
), type
,
2413 env
->watchpoint_hit
->vaddr
);
2414 env
->watchpoint_hit
= NULL
;
2418 ret
= GDB_SIGNAL_TRAP
;
2420 case RUN_STATE_PAUSED
:
2421 ret
= GDB_SIGNAL_INT
;
2423 case RUN_STATE_SHUTDOWN
:
2424 ret
= GDB_SIGNAL_QUIT
;
2426 case RUN_STATE_IO_ERROR
:
2427 ret
= GDB_SIGNAL_IO
;
2429 case RUN_STATE_WATCHDOG
:
2430 ret
= GDB_SIGNAL_ALRM
;
2432 case RUN_STATE_INTERNAL_ERROR
:
2433 ret
= GDB_SIGNAL_ABRT
;
2435 case RUN_STATE_SAVE_VM
:
2436 case RUN_STATE_RESTORE_VM
:
2438 case RUN_STATE_FINISH_MIGRATE
:
2439 ret
= GDB_SIGNAL_XCPU
;
2442 ret
= GDB_SIGNAL_UNKNOWN
;
2445 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", ret
, cpu_index(cpu
));
2450 /* disable single step if it was enabled */
2451 cpu_single_step(cpu
, 0);
2455 /* Send a gdb syscall request.
2456 This accepts limited printf-style format specifiers, specifically:
2457 %x - target_ulong argument printed in hex.
2458 %lx - 64-bit argument printed in hex.
2459 %s - string pointer (target_ulong) and length (int) pair. */
2460 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
2469 s
= gdbserver_state
;
2472 s
->current_syscall_cb
= cb
;
2473 #ifndef CONFIG_USER_ONLY
2474 vm_stop(RUN_STATE_DEBUG
);
2478 p_end
= &s
->syscall_buf
[sizeof(s
->syscall_buf
)];
2485 addr
= va_arg(va
, target_ulong
);
2486 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
, addr
);
2489 if (*(fmt
++) != 'x')
2491 i64
= va_arg(va
, uint64_t);
2492 p
+= snprintf(p
, p_end
- p
, "%" PRIx64
, i64
);
2495 addr
= va_arg(va
, target_ulong
);
2496 p
+= snprintf(p
, p_end
- p
, TARGET_FMT_lx
"/%x",
2497 addr
, va_arg(va
, int));
2501 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
2511 #ifdef CONFIG_USER_ONLY
2512 put_packet(s
, s
->syscall_buf
);
2513 gdb_handlesig(s
->c_cpu
, 0);
2515 /* In this case wait to send the syscall packet until notification that
2516 the CPU has stopped. This must be done because if the packet is sent
2517 now the reply from the syscall request could be received while the CPU
2518 is still in the running state, which can cause packets to be dropped
2519 and state transition 'T' packets to be sent while the syscall is still
2525 static void gdb_read_byte(GDBState
*s
, int ch
)
2530 #ifndef CONFIG_USER_ONLY
2531 if (s
->last_packet_len
) {
2532 /* Waiting for a response to the last packet. If we see the start
2533 of a new command then abandon the previous response. */
2536 printf("Got NACK, retransmitting\n");
2538 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
2542 printf("Got ACK\n");
2544 printf("Got '%c' when expecting ACK/NACK\n", ch
);
2546 if (ch
== '+' || ch
== '$')
2547 s
->last_packet_len
= 0;
2551 if (runstate_is_running()) {
2552 /* when the CPU is running, we cannot do anything except stop
2553 it when receiving a char */
2554 vm_stop(RUN_STATE_PAUSED
);
2561 s
->line_buf_index
= 0;
2562 s
->state
= RS_GETLINE
;
2567 s
->state
= RS_CHKSUM1
;
2568 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2571 s
->line_buf
[s
->line_buf_index
++] = ch
;
2575 s
->line_buf
[s
->line_buf_index
] = '\0';
2576 s
->line_csum
= fromhex(ch
) << 4;
2577 s
->state
= RS_CHKSUM2
;
2580 s
->line_csum
|= fromhex(ch
);
2582 for(i
= 0; i
< s
->line_buf_index
; i
++) {
2583 csum
+= s
->line_buf
[i
];
2585 if (s
->line_csum
!= (csum
& 0xff)) {
2587 put_buffer(s
, &reply
, 1);
2591 put_buffer(s
, &reply
, 1);
2592 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
2601 /* Tell the remote gdb that the process has exited. */
2602 void gdb_exit(CPUArchState
*env
, int code
)
2607 s
= gdbserver_state
;
2611 #ifdef CONFIG_USER_ONLY
2612 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2617 snprintf(buf
, sizeof(buf
), "W%02x", (uint8_t)code
);
2620 #ifndef CONFIG_USER_ONLY
2622 qemu_chr_delete(s
->chr
);
2627 #ifdef CONFIG_USER_ONLY
2633 s
= gdbserver_state
;
2635 if (gdbserver_fd
< 0 || s
->fd
< 0)
2642 gdb_handlesig(CPUState
*cpu
, int sig
)
2644 CPUArchState
*env
= cpu
->env_ptr
;
2649 s
= gdbserver_state
;
2650 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2654 /* disable single step if it was enabled */
2655 cpu_single_step(cpu
, 0);
2659 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb(sig
));
2662 /* put_packet() might have detected that the peer terminated the
2670 s
->running_state
= 0;
2671 while (s
->running_state
== 0) {
2672 n
= read(s
->fd
, buf
, 256);
2676 for (i
= 0; i
< n
; i
++) {
2677 gdb_read_byte(s
, buf
[i
]);
2679 } else if (n
== 0 || errno
!= EAGAIN
) {
2680 /* XXX: Connection closed. Should probably wait for another
2681 connection before continuing. */
2690 /* Tell the remote gdb that the process has exited due to SIG. */
2691 void gdb_signalled(CPUArchState
*env
, int sig
)
2696 s
= gdbserver_state
;
2697 if (gdbserver_fd
< 0 || s
->fd
< 0) {
2701 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb(sig
));
2705 static void gdb_accept(void)
2708 struct sockaddr_in sockaddr
;
2713 len
= sizeof(sockaddr
);
2714 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
2715 if (fd
< 0 && errno
!= EINTR
) {
2718 } else if (fd
>= 0) {
2720 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
2726 /* set short latency */
2727 socket_set_nodelay(fd
);
2729 s
= g_malloc0(sizeof(GDBState
));
2730 s
->c_cpu
= first_cpu
;
2731 s
->g_cpu
= first_cpu
;
2735 gdbserver_state
= s
;
2737 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2740 static int gdbserver_open(int port
)
2742 struct sockaddr_in sockaddr
;
2745 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2751 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
2754 /* allow fast reuse */
2756 qemu_setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
2758 sockaddr
.sin_family
= AF_INET
;
2759 sockaddr
.sin_port
= htons(port
);
2760 sockaddr
.sin_addr
.s_addr
= 0;
2761 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
2767 ret
= listen(fd
, 0);
2776 int gdbserver_start(int port
)
2778 gdbserver_fd
= gdbserver_open(port
);
2779 if (gdbserver_fd
< 0)
2781 /* accept connections */
2786 /* Disable gdb stub for child processes. */
2787 void gdbserver_fork(CPUArchState
*env
)
2789 GDBState
*s
= gdbserver_state
;
2790 if (gdbserver_fd
< 0 || s
->fd
< 0)
2794 cpu_breakpoint_remove_all(env
, BP_GDB
);
2795 cpu_watchpoint_remove_all(env
, BP_GDB
);
2798 static int gdb_chr_can_receive(void *opaque
)
2800 /* We can handle an arbitrarily large amount of data.
2801 Pick the maximum packet size, which is as good as anything. */
2802 return MAX_PACKET_LENGTH
;
2805 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
2809 for (i
= 0; i
< size
; i
++) {
2810 gdb_read_byte(gdbserver_state
, buf
[i
]);
2814 static void gdb_chr_event(void *opaque
, int event
)
2817 case CHR_EVENT_OPENED
:
2818 vm_stop(RUN_STATE_PAUSED
);
2826 static void gdb_monitor_output(GDBState
*s
, const char *msg
, int len
)
2828 char buf
[MAX_PACKET_LENGTH
];
2831 if (len
> (MAX_PACKET_LENGTH
/2) - 1)
2832 len
= (MAX_PACKET_LENGTH
/2) - 1;
2833 memtohex(buf
+ 1, (uint8_t *)msg
, len
);
2837 static int gdb_monitor_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2839 const char *p
= (const char *)buf
;
2842 max_sz
= (sizeof(gdbserver_state
->last_packet
) - 2) / 2;
2844 if (len
<= max_sz
) {
2845 gdb_monitor_output(gdbserver_state
, p
, len
);
2848 gdb_monitor_output(gdbserver_state
, p
, max_sz
);
2856 static void gdb_sigterm_handler(int signal
)
2858 if (runstate_is_running()) {
2859 vm_stop(RUN_STATE_PAUSED
);
2864 int gdbserver_start(const char *device
)
2867 char gdbstub_device_name
[128];
2868 CharDriverState
*chr
= NULL
;
2869 CharDriverState
*mon_chr
;
2873 if (strcmp(device
, "none") != 0) {
2874 if (strstart(device
, "tcp:", NULL
)) {
2875 /* enforce required TCP attributes */
2876 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
2877 "%s,nowait,nodelay,server", device
);
2878 device
= gdbstub_device_name
;
2881 else if (strcmp(device
, "stdio") == 0) {
2882 struct sigaction act
;
2884 memset(&act
, 0, sizeof(act
));
2885 act
.sa_handler
= gdb_sigterm_handler
;
2886 sigaction(SIGINT
, &act
, NULL
);
2889 chr
= qemu_chr_new("gdb", device
, NULL
);
2893 qemu_chr_fe_claim_no_fail(chr
);
2894 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
2895 gdb_chr_event
, NULL
);
2898 s
= gdbserver_state
;
2900 s
= g_malloc0(sizeof(GDBState
));
2901 gdbserver_state
= s
;
2903 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
2905 /* Initialize a monitor terminal for gdb */
2906 mon_chr
= g_malloc0(sizeof(*mon_chr
));
2907 mon_chr
->chr_write
= gdb_monitor_write
;
2908 monitor_init(mon_chr
, 0);
2911 qemu_chr_delete(s
->chr
);
2912 mon_chr
= s
->mon_chr
;
2913 memset(s
, 0, sizeof(GDBState
));
2915 s
->c_cpu
= first_cpu
;
2916 s
->g_cpu
= first_cpu
;
2918 s
->state
= chr
? RS_IDLE
: RS_INACTIVE
;
2919 s
->mon_chr
= mon_chr
;
2920 s
->current_syscall_cb
= NULL
;