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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
21 #include "qemu-common.h"
22 #ifdef CONFIG_USER_ONLY
34 #include "qemu-char.h"
40 #define MAX_PACKET_LENGTH 4096
42 #include "qemu_socket.h"
50 GDB_SIGNAL_UNKNOWN
= 143
53 #ifdef CONFIG_USER_ONLY
55 /* Map target signal numbers to GDB protocol signal numbers and vice
56 * versa. For user emulation's currently supported systems, we can
57 * assume most signals are defined.
60 static int gdb_signal_table
[] = {
220 /* In system mode we only need SIGINT and SIGTRAP; other signals
221 are not yet supported. */
228 static int gdb_signal_table
[] = {
238 #ifdef CONFIG_USER_ONLY
239 static int target_signal_to_gdb (int sig
)
242 for (i
= 0; i
< ARRAY_SIZE (gdb_signal_table
); i
++)
243 if (gdb_signal_table
[i
] == sig
)
245 return GDB_SIGNAL_UNKNOWN
;
249 static int gdb_signal_to_target (int sig
)
251 if (sig
< ARRAY_SIZE (gdb_signal_table
))
252 return gdb_signal_table
[sig
];
259 typedef struct GDBRegisterState
{
265 struct GDBRegisterState
*next
;
276 typedef struct GDBState
{
277 CPUState
*c_cpu
; /* current CPU for step/continue ops */
278 CPUState
*g_cpu
; /* current CPU for other ops */
279 CPUState
*query_cpu
; /* for q{f|s}ThreadInfo */
280 enum RSState state
; /* parsing state */
281 char line_buf
[MAX_PACKET_LENGTH
];
284 uint8_t last_packet
[MAX_PACKET_LENGTH
+ 4];
287 #ifdef CONFIG_USER_ONLY
291 CharDriverState
*chr
;
292 CharDriverState
*mon_chr
;
296 /* By default use no IRQs and no timers while single stepping so as to
297 * make single stepping like an ICE HW step.
299 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
301 static GDBState
*gdbserver_state
;
303 /* This is an ugly hack to cope with both new and old gdb.
304 If gdb sends qXfer:features:read then assume we're talking to a newish
305 gdb that understands target descriptions. */
306 static int gdb_has_xml
;
308 #ifdef CONFIG_USER_ONLY
309 /* XXX: This is not thread safe. Do we care? */
310 static int gdbserver_fd
= -1;
312 static int get_char(GDBState
*s
)
318 ret
= recv(s
->fd
, &ch
, 1, 0);
320 if (errno
== ECONNRESET
)
322 if (errno
!= EINTR
&& errno
!= EAGAIN
)
324 } else if (ret
== 0) {
336 static gdb_syscall_complete_cb gdb_current_syscall_cb
;
344 /* If gdb is connected when the first semihosting syscall occurs then use
345 remote gdb syscalls. Otherwise use native file IO. */
346 int use_gdb_syscalls(void)
348 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
349 gdb_syscall_mode
= (gdbserver_state
? GDB_SYS_ENABLED
352 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
355 /* Resume execution. */
356 static inline void gdb_continue(GDBState
*s
)
358 #ifdef CONFIG_USER_ONLY
359 s
->running_state
= 1;
365 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
367 #ifdef CONFIG_USER_ONLY
371 ret
= send(s
->fd
, buf
, len
, 0);
373 if (errno
!= EINTR
&& errno
!= EAGAIN
)
381 qemu_chr_write(s
->chr
, buf
, len
);
385 static inline int fromhex(int v
)
387 if (v
>= '0' && v
<= '9')
389 else if (v
>= 'A' && v
<= 'F')
391 else if (v
>= 'a' && v
<= 'f')
397 static inline int tohex(int v
)
405 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
410 for(i
= 0; i
< len
; i
++) {
412 *q
++ = tohex(c
>> 4);
413 *q
++ = tohex(c
& 0xf);
418 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
422 for(i
= 0; i
< len
; i
++) {
423 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
428 /* return -1 if error, 0 if OK */
429 static int put_packet_binary(GDBState
*s
, const char *buf
, int len
)
440 for(i
= 0; i
< len
; i
++) {
444 *(p
++) = tohex((csum
>> 4) & 0xf);
445 *(p
++) = tohex((csum
) & 0xf);
447 s
->last_packet_len
= p
- s
->last_packet
;
448 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
450 #ifdef CONFIG_USER_ONLY
463 /* return -1 if error, 0 if OK */
464 static int put_packet(GDBState
*s
, const char *buf
)
467 printf("reply='%s'\n", buf
);
470 return put_packet_binary(s
, buf
, strlen(buf
));
473 /* The GDB remote protocol transfers values in target byte order. This means
474 we can use the raw memory access routines to access the value buffer.
475 Conveniently, these also handle the case where the buffer is mis-aligned.
477 #define GET_REG8(val) do { \
478 stb_p(mem_buf, val); \
481 #define GET_REG16(val) do { \
482 stw_p(mem_buf, val); \
485 #define GET_REG32(val) do { \
486 stl_p(mem_buf, val); \
489 #define GET_REG64(val) do { \
490 stq_p(mem_buf, val); \
494 #if TARGET_LONG_BITS == 64
495 #define GET_REGL(val) GET_REG64(val)
496 #define ldtul_p(addr) ldq_p(addr)
498 #define GET_REGL(val) GET_REG32(val)
499 #define ldtul_p(addr) ldl_p(addr)
502 #if defined(TARGET_I386)
505 static const int gpr_map
[16] = {
506 R_EAX
, R_EBX
, R_ECX
, R_EDX
, R_ESI
, R_EDI
, R_EBP
, R_ESP
,
507 8, 9, 10, 11, 12, 13, 14, 15
510 static const int gpr_map
[8] = {0, 1, 2, 3, 4, 5, 6, 7};
513 #define NUM_CORE_REGS (CPU_NB_REGS * 2 + 25)
515 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
517 if (n
< CPU_NB_REGS
) {
518 GET_REGL(env
->regs
[gpr_map
[n
]]);
519 } else if (n
>= CPU_NB_REGS
+ 8 && n
< CPU_NB_REGS
+ 16) {
520 /* FIXME: byteswap float values. */
521 #ifdef USE_X86LDOUBLE
522 memcpy(mem_buf
, &env
->fpregs
[n
- (CPU_NB_REGS
+ 8)], 10);
524 memset(mem_buf
, 0, 10);
527 } else if (n
>= CPU_NB_REGS
+ 24) {
528 n
-= CPU_NB_REGS
+ 24;
529 if (n
< CPU_NB_REGS
) {
530 stq_p(mem_buf
, env
->xmm_regs
[n
].XMM_Q(0));
531 stq_p(mem_buf
+ 8, env
->xmm_regs
[n
].XMM_Q(1));
533 } else if (n
== CPU_NB_REGS
) {
534 GET_REG32(env
->mxcsr
);
539 case 0: GET_REGL(env
->eip
);
540 case 1: GET_REG32(env
->eflags
);
541 case 2: GET_REG32(env
->segs
[R_CS
].selector
);
542 case 3: GET_REG32(env
->segs
[R_SS
].selector
);
543 case 4: GET_REG32(env
->segs
[R_DS
].selector
);
544 case 5: GET_REG32(env
->segs
[R_ES
].selector
);
545 case 6: GET_REG32(env
->segs
[R_FS
].selector
);
546 case 7: GET_REG32(env
->segs
[R_GS
].selector
);
547 /* 8...15 x87 regs. */
548 case 16: GET_REG32(env
->fpuc
);
549 case 17: GET_REG32((env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11);
550 case 18: GET_REG32(0); /* ftag */
551 case 19: GET_REG32(0); /* fiseg */
552 case 20: GET_REG32(0); /* fioff */
553 case 21: GET_REG32(0); /* foseg */
554 case 22: GET_REG32(0); /* fooff */
555 case 23: GET_REG32(0); /* fop */
562 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int i
)
566 if (i
< CPU_NB_REGS
) {
567 env
->regs
[gpr_map
[i
]] = ldtul_p(mem_buf
);
568 return sizeof(target_ulong
);
569 } else if (i
>= CPU_NB_REGS
+ 8 && i
< CPU_NB_REGS
+ 16) {
570 i
-= CPU_NB_REGS
+ 8;
571 #ifdef USE_X86LDOUBLE
572 memcpy(&env
->fpregs
[i
], mem_buf
, 10);
575 } else if (i
>= CPU_NB_REGS
+ 24) {
576 i
-= CPU_NB_REGS
+ 24;
577 if (i
< CPU_NB_REGS
) {
578 env
->xmm_regs
[i
].XMM_Q(0) = ldq_p(mem_buf
);
579 env
->xmm_regs
[i
].XMM_Q(1) = ldq_p(mem_buf
+ 8);
581 } else if (i
== CPU_NB_REGS
) {
582 env
->mxcsr
= ldl_p(mem_buf
);
588 case 0: env
->eip
= ldtul_p(mem_buf
); return sizeof(target_ulong
);
589 case 1: env
->eflags
= ldl_p(mem_buf
); return 4;
590 #if defined(CONFIG_USER_ONLY)
591 #define LOAD_SEG(index, sreg)\
592 tmp = ldl_p(mem_buf);\
593 if (tmp != env->segs[sreg].selector)\
594 cpu_x86_load_seg(env, sreg, tmp);
596 /* FIXME: Honor segment registers. Needs to avoid raising an exception
597 when the selector is invalid. */
598 #define LOAD_SEG(index, sreg) do {} while(0)
600 case 2: LOAD_SEG(10, R_CS
); return 4;
601 case 3: LOAD_SEG(11, R_SS
); return 4;
602 case 4: LOAD_SEG(12, R_DS
); return 4;
603 case 5: LOAD_SEG(13, R_ES
); return 4;
604 case 6: LOAD_SEG(14, R_FS
); return 4;
605 case 7: LOAD_SEG(15, R_GS
); return 4;
606 /* 8...15 x87 regs. */
607 case 16: env
->fpuc
= ldl_p(mem_buf
); return 4;
609 tmp
= ldl_p(mem_buf
);
610 env
->fpstt
= (tmp
>> 11) & 7;
611 env
->fpus
= tmp
& ~0x3800;
613 case 18: /* ftag */ return 4;
614 case 19: /* fiseg */ return 4;
615 case 20: /* fioff */ return 4;
616 case 21: /* foseg */ return 4;
617 case 22: /* fooff */ return 4;
618 case 23: /* fop */ return 4;
622 /* Unrecognised register. */
626 #elif defined (TARGET_PPC)
628 /* Old gdb always expects FP registers. Newer (xml-aware) gdb only
629 expects whatever the target description contains. Due to a
630 historical mishap the FP registers appear in between core integer
631 regs and PC, MSR, CR, and so forth. We hack round this by giving the
632 FP regs zero size when talking to a newer gdb. */
633 #define NUM_CORE_REGS 71
634 #if defined (TARGET_PPC64)
635 #define GDB_CORE_XML "power64-core.xml"
637 #define GDB_CORE_XML "power-core.xml"
640 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
644 GET_REGL(env
->gpr
[n
]);
649 stfq_p(mem_buf
, env
->fpr
[n
-32]);
653 case 64: GET_REGL(env
->nip
);
654 case 65: GET_REGL(env
->msr
);
659 for (i
= 0; i
< 8; i
++)
660 cr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
663 case 67: GET_REGL(env
->lr
);
664 case 68: GET_REGL(env
->ctr
);
665 case 69: GET_REGL(env
->xer
);
670 GET_REG32(0); /* fpscr */
677 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
681 env
->gpr
[n
] = ldtul_p(mem_buf
);
682 return sizeof(target_ulong
);
687 env
->fpr
[n
-32] = ldfq_p(mem_buf
);
692 env
->nip
= ldtul_p(mem_buf
);
693 return sizeof(target_ulong
);
695 ppc_store_msr(env
, ldtul_p(mem_buf
));
696 return sizeof(target_ulong
);
699 uint32_t cr
= ldl_p(mem_buf
);
701 for (i
= 0; i
< 8; i
++)
702 env
->crf
[i
] = (cr
>> (32 - ((i
+ 1) * 4))) & 0xF;
706 env
->lr
= ldtul_p(mem_buf
);
707 return sizeof(target_ulong
);
709 env
->ctr
= ldtul_p(mem_buf
);
710 return sizeof(target_ulong
);
712 env
->xer
= ldtul_p(mem_buf
);
713 return sizeof(target_ulong
);
724 #elif defined (TARGET_SPARC)
726 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
727 #define NUM_CORE_REGS 86
729 #define NUM_CORE_REGS 72
733 #define GET_REGA(val) GET_REG32(val)
735 #define GET_REGA(val) GET_REGL(val)
738 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
742 GET_REGA(env
->gregs
[n
]);
745 /* register window */
746 GET_REGA(env
->regwptr
[n
- 8]);
748 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
751 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
753 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
755 case 64: GET_REGA(env
->y
);
756 case 65: GET_REGA(GET_PSR(env
));
757 case 66: GET_REGA(env
->wim
);
758 case 67: GET_REGA(env
->tbr
);
759 case 68: GET_REGA(env
->pc
);
760 case 69: GET_REGA(env
->npc
);
761 case 70: GET_REGA(env
->fsr
);
762 case 71: GET_REGA(0); /* csr */
763 default: GET_REGA(0);
768 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
771 /* f32-f62 (double width, even numbers only) */
774 val
= (uint64_t)*((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) << 32;
775 val
|= *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]);
779 case 80: GET_REGL(env
->pc
);
780 case 81: GET_REGL(env
->npc
);
781 case 82: GET_REGL(((uint64_t)GET_CCR(env
) << 32) |
782 ((env
->asi
& 0xff) << 24) |
783 ((env
->pstate
& 0xfff) << 8) |
785 case 83: GET_REGL(env
->fsr
);
786 case 84: GET_REGL(env
->fprs
);
787 case 85: GET_REGL(env
->y
);
793 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
795 #if defined(TARGET_ABI32)
798 tmp
= ldl_p(mem_buf
);
802 tmp
= ldtul_p(mem_buf
);
809 /* register window */
810 env
->regwptr
[n
- 8] = tmp
;
812 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
815 *((uint32_t *)&env
->fpr
[n
- 32]) = tmp
;
817 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
819 case 64: env
->y
= tmp
; break;
820 case 65: PUT_PSR(env
, tmp
); break;
821 case 66: env
->wim
= tmp
; break;
822 case 67: env
->tbr
= tmp
; break;
823 case 68: env
->pc
= tmp
; break;
824 case 69: env
->npc
= tmp
; break;
825 case 70: env
->fsr
= tmp
; break;
833 env
->fpr
[n
] = ldfl_p(mem_buf
);
836 /* f32-f62 (double width, even numbers only) */
837 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) = tmp
>> 32;
838 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]) = tmp
;
841 case 80: env
->pc
= tmp
; break;
842 case 81: env
->npc
= tmp
; break;
844 PUT_CCR(env
, tmp
>> 32);
845 env
->asi
= (tmp
>> 24) & 0xff;
846 env
->pstate
= (tmp
>> 8) & 0xfff;
847 PUT_CWP64(env
, tmp
& 0xff);
849 case 83: env
->fsr
= tmp
; break;
850 case 84: env
->fprs
= tmp
; break;
851 case 85: env
->y
= tmp
; break;
858 #elif defined (TARGET_ARM)
860 /* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect
861 whatever the target description contains. Due to a historical mishap
862 the FPA registers appear in between core integer regs and the CPSR.
863 We hack round this by giving the FPA regs zero size when talking to a
865 #define NUM_CORE_REGS 26
866 #define GDB_CORE_XML "arm-core.xml"
868 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
871 /* Core integer register. */
872 GET_REG32(env
->regs
[n
]);
878 memset(mem_buf
, 0, 12);
883 /* FPA status register. */
889 GET_REG32(cpsr_read(env
));
891 /* Unknown register. */
895 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
899 tmp
= ldl_p(mem_buf
);
901 /* Mask out low bit of PC to workaround gdb bugs. This will probably
902 cause problems if we ever implement the Jazelle DBX extensions. */
907 /* Core integer register. */
911 if (n
< 24) { /* 16-23 */
912 /* FPA registers (ignored). */
919 /* FPA status register (ignored). */
925 cpsr_write (env
, tmp
, 0xffffffff);
928 /* Unknown register. */
932 #elif defined (TARGET_M68K)
934 #define NUM_CORE_REGS 18
936 #define GDB_CORE_XML "cf-core.xml"
938 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
942 GET_REG32(env
->dregs
[n
]);
945 GET_REG32(env
->aregs
[n
- 8]);
948 case 16: GET_REG32(env
->sr
);
949 case 17: GET_REG32(env
->pc
);
952 /* FP registers not included here because they vary between
953 ColdFire and m68k. Use XML bits for these. */
957 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
961 tmp
= ldl_p(mem_buf
);
968 env
->aregs
[n
- 8] = tmp
;
971 case 16: env
->sr
= tmp
; break;
972 case 17: env
->pc
= tmp
; break;
978 #elif defined (TARGET_MIPS)
980 #define NUM_CORE_REGS 73
982 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
985 GET_REGL(env
->active_tc
.gpr
[n
]);
987 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
988 if (n
>= 38 && n
< 70) {
989 if (env
->CP0_Status
& (1 << CP0St_FR
))
990 GET_REGL(env
->active_fpu
.fpr
[n
- 38].d
);
992 GET_REGL(env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
]);
995 case 70: GET_REGL((int32_t)env
->active_fpu
.fcr31
);
996 case 71: GET_REGL((int32_t)env
->active_fpu
.fcr0
);
1000 case 32: GET_REGL((int32_t)env
->CP0_Status
);
1001 case 33: GET_REGL(env
->active_tc
.LO
[0]);
1002 case 34: GET_REGL(env
->active_tc
.HI
[0]);
1003 case 35: GET_REGL(env
->CP0_BadVAddr
);
1004 case 36: GET_REGL((int32_t)env
->CP0_Cause
);
1005 case 37: GET_REGL(env
->active_tc
.PC
);
1006 case 72: GET_REGL(0); /* fp */
1007 case 89: GET_REGL((int32_t)env
->CP0_PRid
);
1009 if (n
>= 73 && n
<= 88) {
1010 /* 16 embedded regs. */
1017 /* convert MIPS rounding mode in FCR31 to IEEE library */
1018 static unsigned int ieee_rm
[] =
1020 float_round_nearest_even
,
1021 float_round_to_zero
,
1025 #define RESTORE_ROUNDING_MODE \
1026 set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
1028 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1032 tmp
= ldtul_p(mem_buf
);
1035 env
->active_tc
.gpr
[n
] = tmp
;
1036 return sizeof(target_ulong
);
1038 if (env
->CP0_Config1
& (1 << CP0C1_FP
)
1039 && n
>= 38 && n
< 73) {
1041 if (env
->CP0_Status
& (1 << CP0St_FR
))
1042 env
->active_fpu
.fpr
[n
- 38].d
= tmp
;
1044 env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
] = tmp
;
1048 env
->active_fpu
.fcr31
= tmp
& 0xFF83FFFF;
1049 /* set rounding mode */
1050 RESTORE_ROUNDING_MODE
;
1051 #ifndef CONFIG_SOFTFLOAT
1052 /* no floating point exception for native float */
1053 SET_FP_ENABLE(env
->active_fpu
.fcr31
, 0);
1056 case 71: env
->active_fpu
.fcr0
= tmp
; break;
1058 return sizeof(target_ulong
);
1061 case 32: env
->CP0_Status
= tmp
; break;
1062 case 33: env
->active_tc
.LO
[0] = tmp
; break;
1063 case 34: env
->active_tc
.HI
[0] = tmp
; break;
1064 case 35: env
->CP0_BadVAddr
= tmp
; break;
1065 case 36: env
->CP0_Cause
= tmp
; break;
1066 case 37: env
->active_tc
.PC
= tmp
; break;
1067 case 72: /* fp, ignored */ break;
1071 /* Other registers are readonly. Ignore writes. */
1075 return sizeof(target_ulong
);
1077 #elif defined (TARGET_SH4)
1079 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
1080 /* FIXME: We should use XML for this. */
1082 #define NUM_CORE_REGS 59
1084 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1087 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1088 GET_REGL(env
->gregs
[n
+ 16]);
1090 GET_REGL(env
->gregs
[n
]);
1092 } else if (n
< 16) {
1093 GET_REGL(env
->gregs
[n
- 8]);
1094 } else if (n
>= 25 && n
< 41) {
1095 GET_REGL(env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
1096 } else if (n
>= 43 && n
< 51) {
1097 GET_REGL(env
->gregs
[n
- 43]);
1098 } else if (n
>= 51 && n
< 59) {
1099 GET_REGL(env
->gregs
[n
- (51 - 16)]);
1102 case 16: GET_REGL(env
->pc
);
1103 case 17: GET_REGL(env
->pr
);
1104 case 18: GET_REGL(env
->gbr
);
1105 case 19: GET_REGL(env
->vbr
);
1106 case 20: GET_REGL(env
->mach
);
1107 case 21: GET_REGL(env
->macl
);
1108 case 22: GET_REGL(env
->sr
);
1109 case 23: GET_REGL(env
->fpul
);
1110 case 24: GET_REGL(env
->fpscr
);
1111 case 41: GET_REGL(env
->ssr
);
1112 case 42: GET_REGL(env
->spc
);
1118 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1122 tmp
= ldl_p(mem_buf
);
1125 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1126 env
->gregs
[n
+ 16] = tmp
;
1128 env
->gregs
[n
] = tmp
;
1131 } else if (n
< 16) {
1132 env
->gregs
[n
- 8] = tmp
;
1134 } else if (n
>= 25 && n
< 41) {
1135 env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)] = tmp
;
1136 } else if (n
>= 43 && n
< 51) {
1137 env
->gregs
[n
- 43] = tmp
;
1139 } else if (n
>= 51 && n
< 59) {
1140 env
->gregs
[n
- (51 - 16)] = tmp
;
1144 case 16: env
->pc
= tmp
;
1145 case 17: env
->pr
= tmp
;
1146 case 18: env
->gbr
= tmp
;
1147 case 19: env
->vbr
= tmp
;
1148 case 20: env
->mach
= tmp
;
1149 case 21: env
->macl
= tmp
;
1150 case 22: env
->sr
= tmp
;
1151 case 23: env
->fpul
= tmp
;
1152 case 24: env
->fpscr
= tmp
;
1153 case 41: env
->ssr
= tmp
;
1154 case 42: env
->spc
= tmp
;
1160 #elif defined (TARGET_MICROBLAZE)
1162 #define NUM_CORE_REGS (32 + 5)
1164 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1167 GET_REG32(env
->regs
[n
]);
1169 GET_REG32(env
->sregs
[n
- 32]);
1174 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1178 if (n
> NUM_CORE_REGS
)
1181 tmp
= ldl_p(mem_buf
);
1186 env
->sregs
[n
- 32] = tmp
;
1190 #elif defined (TARGET_CRIS)
1192 #define NUM_CORE_REGS 49
1194 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1198 srs
= env
->pregs
[PR_SRS
];
1200 GET_REG32(env
->regs
[n
]);
1203 if (n
>= 21 && n
< 32) {
1204 GET_REG32(env
->pregs
[n
- 16]);
1206 if (n
>= 33 && n
< 49) {
1207 GET_REG32(env
->sregs
[srs
][n
- 33]);
1210 case 16: GET_REG8(env
->pregs
[0]);
1211 case 17: GET_REG8(env
->pregs
[1]);
1212 case 18: GET_REG32(env
->pregs
[2]);
1213 case 19: GET_REG8(srs
);
1214 case 20: GET_REG16(env
->pregs
[4]);
1215 case 32: GET_REG32(env
->pc
);
1221 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1228 tmp
= ldl_p(mem_buf
);
1234 if (n
>= 21 && n
< 32) {
1235 env
->pregs
[n
- 16] = tmp
;
1238 /* FIXME: Should support function regs be writable? */
1242 case 18: env
->pregs
[PR_PID
] = tmp
; break;
1245 case 32: env
->pc
= tmp
; break;
1250 #elif defined (TARGET_ALPHA)
1252 #define NUM_CORE_REGS 65
1254 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1257 GET_REGL(env
->ir
[n
]);
1265 val
=*((uint64_t *)&env
->fir
[n
-32]);
1269 GET_REGL(env
->fpcr
);
1281 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1284 tmp
= ldtul_p(mem_buf
);
1290 if (n
> 31 && n
< 63) {
1291 env
->fir
[n
- 32] = ldfl_p(mem_buf
);
1302 #define NUM_CORE_REGS 0
1304 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1309 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1316 static int num_g_regs
= NUM_CORE_REGS
;
1319 /* Encode data using the encoding for 'x' packets. */
1320 static int memtox(char *buf
, const char *mem
, int len
)
1328 case '#': case '$': case '*': case '}':
1340 static const char *get_feature_xml(const char *p
, const char **newp
)
1342 extern const char *const xml_builtin
[][2];
1346 static char target_xml
[1024];
1349 while (p
[len
] && p
[len
] != ':')
1354 if (strncmp(p
, "target.xml", len
) == 0) {
1355 /* Generate the XML description for this CPU. */
1356 if (!target_xml
[0]) {
1357 GDBRegisterState
*r
;
1359 snprintf(target_xml
, sizeof(target_xml
),
1360 "<?xml version=\"1.0\"?>"
1361 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
1363 "<xi:include href=\"%s\"/>",
1366 for (r
= first_cpu
->gdb_regs
; r
; r
= r
->next
) {
1367 pstrcat(target_xml
, sizeof(target_xml
), "<xi:include href=\"");
1368 pstrcat(target_xml
, sizeof(target_xml
), r
->xml
);
1369 pstrcat(target_xml
, sizeof(target_xml
), "\"/>");
1371 pstrcat(target_xml
, sizeof(target_xml
), "</target>");
1375 for (i
= 0; ; i
++) {
1376 name
= xml_builtin
[i
][0];
1377 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
1380 return name
? xml_builtin
[i
][1] : NULL
;
1384 static int gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int reg
)
1386 GDBRegisterState
*r
;
1388 if (reg
< NUM_CORE_REGS
)
1389 return cpu_gdb_read_register(env
, mem_buf
, reg
);
1391 for (r
= env
->gdb_regs
; r
; r
= r
->next
) {
1392 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1393 return r
->get_reg(env
, mem_buf
, reg
- r
->base_reg
);
1399 static int gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int reg
)
1401 GDBRegisterState
*r
;
1403 if (reg
< NUM_CORE_REGS
)
1404 return cpu_gdb_write_register(env
, mem_buf
, reg
);
1406 for (r
= env
->gdb_regs
; r
; r
= r
->next
) {
1407 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1408 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
1414 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
1415 specifies the first register number and these registers are included in
1416 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
1417 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
1420 void gdb_register_coprocessor(CPUState
* env
,
1421 gdb_reg_cb get_reg
, gdb_reg_cb set_reg
,
1422 int num_regs
, const char *xml
, int g_pos
)
1424 GDBRegisterState
*s
;
1425 GDBRegisterState
**p
;
1426 static int last_reg
= NUM_CORE_REGS
;
1428 s
= (GDBRegisterState
*)qemu_mallocz(sizeof(GDBRegisterState
));
1429 s
->base_reg
= last_reg
;
1430 s
->num_regs
= num_regs
;
1431 s
->get_reg
= get_reg
;
1432 s
->set_reg
= set_reg
;
1436 /* Check for duplicates. */
1437 if (strcmp((*p
)->xml
, xml
) == 0)
1441 /* Add to end of list. */
1442 last_reg
+= num_regs
;
1445 if (g_pos
!= s
->base_reg
) {
1446 fprintf(stderr
, "Error: Bad gdb register numbering for '%s'\n"
1447 "Expected %d got %d\n", xml
, g_pos
, s
->base_reg
);
1449 num_g_regs
= last_reg
;
1454 #ifndef CONFIG_USER_ONLY
1455 static const int xlat_gdb_type
[] = {
1456 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
1457 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
1458 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
1462 static int gdb_breakpoint_insert(target_ulong addr
, target_ulong len
, int type
)
1468 return kvm_insert_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1471 case GDB_BREAKPOINT_SW
:
1472 case GDB_BREAKPOINT_HW
:
1473 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1474 err
= cpu_breakpoint_insert(env
, addr
, BP_GDB
, NULL
);
1479 #ifndef CONFIG_USER_ONLY
1480 case GDB_WATCHPOINT_WRITE
:
1481 case GDB_WATCHPOINT_READ
:
1482 case GDB_WATCHPOINT_ACCESS
:
1483 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1484 err
= cpu_watchpoint_insert(env
, addr
, len
, xlat_gdb_type
[type
],
1496 static int gdb_breakpoint_remove(target_ulong addr
, target_ulong len
, int type
)
1502 return kvm_remove_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1505 case GDB_BREAKPOINT_SW
:
1506 case GDB_BREAKPOINT_HW
:
1507 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1508 err
= cpu_breakpoint_remove(env
, addr
, BP_GDB
);
1513 #ifndef CONFIG_USER_ONLY
1514 case GDB_WATCHPOINT_WRITE
:
1515 case GDB_WATCHPOINT_READ
:
1516 case GDB_WATCHPOINT_ACCESS
:
1517 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1518 err
= cpu_watchpoint_remove(env
, addr
, len
, xlat_gdb_type
[type
]);
1529 static void gdb_breakpoint_remove_all(void)
1533 if (kvm_enabled()) {
1534 kvm_remove_all_breakpoints(gdbserver_state
->c_cpu
);
1538 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1539 cpu_breakpoint_remove_all(env
, BP_GDB
);
1540 #ifndef CONFIG_USER_ONLY
1541 cpu_watchpoint_remove_all(env
, BP_GDB
);
1546 static void gdb_set_cpu_pc(GDBState
*s
, target_ulong pc
)
1548 #if defined(TARGET_I386)
1550 cpu_synchronize_state(s
->c_cpu
, 1);
1551 #elif defined (TARGET_PPC)
1553 #elif defined (TARGET_SPARC)
1555 s
->c_cpu
->npc
= pc
+ 4;
1556 #elif defined (TARGET_ARM)
1557 s
->c_cpu
->regs
[15] = pc
;
1558 #elif defined (TARGET_SH4)
1560 #elif defined (TARGET_MIPS)
1561 s
->c_cpu
->active_tc
.PC
= pc
;
1562 #elif defined (TARGET_MICROBLAZE)
1563 s
->c_cpu
->sregs
[SR_PC
] = pc
;
1564 #elif defined (TARGET_CRIS)
1566 #elif defined (TARGET_ALPHA)
1571 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
1575 int ch
, reg_size
, type
, res
, thread
;
1576 char buf
[MAX_PACKET_LENGTH
];
1577 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1579 target_ulong addr
, len
;
1582 printf("command='%s'\n", line_buf
);
1588 /* TODO: Make this return the correct value for user-mode. */
1589 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", GDB_SIGNAL_TRAP
,
1590 s
->c_cpu
->cpu_index
+1);
1592 /* Remove all the breakpoints when this query is issued,
1593 * because gdb is doing and initial connect and the state
1594 * should be cleaned up.
1596 gdb_breakpoint_remove_all();
1600 addr
= strtoull(p
, (char **)&p
, 16);
1601 gdb_set_cpu_pc(s
, addr
);
1607 s
->signal
= gdb_signal_to_target (strtoul(p
, (char **)&p
, 16));
1608 if (s
->signal
== -1)
1613 /* Kill the target */
1614 fprintf(stderr
, "\nQEMU: Terminated via GDBstub\n");
1618 gdb_breakpoint_remove_all();
1620 put_packet(s
, "OK");
1624 addr
= strtoull(p
, (char **)&p
, 16);
1625 gdb_set_cpu_pc(s
, addr
);
1627 cpu_single_step(s
->c_cpu
, sstep_flags
);
1635 ret
= strtoull(p
, (char **)&p
, 16);
1638 err
= strtoull(p
, (char **)&p
, 16);
1645 if (gdb_current_syscall_cb
)
1646 gdb_current_syscall_cb(s
->c_cpu
, ret
, err
);
1648 put_packet(s
, "T02");
1655 cpu_synchronize_state(s
->g_cpu
, 0);
1657 for (addr
= 0; addr
< num_g_regs
; addr
++) {
1658 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
+ len
, addr
);
1661 memtohex(buf
, mem_buf
, len
);
1665 registers
= mem_buf
;
1666 len
= strlen(p
) / 2;
1667 hextomem((uint8_t *)registers
, p
, len
);
1668 for (addr
= 0; addr
< num_g_regs
&& len
> 0; addr
++) {
1669 reg_size
= gdb_write_register(s
->g_cpu
, registers
, addr
);
1671 registers
+= reg_size
;
1673 cpu_synchronize_state(s
->g_cpu
, 1);
1674 put_packet(s
, "OK");
1677 addr
= strtoull(p
, (char **)&p
, 16);
1680 len
= strtoull(p
, NULL
, 16);
1681 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 0) != 0) {
1682 put_packet (s
, "E14");
1684 memtohex(buf
, mem_buf
, len
);
1689 addr
= strtoull(p
, (char **)&p
, 16);
1692 len
= strtoull(p
, (char **)&p
, 16);
1695 hextomem(mem_buf
, p
, len
);
1696 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 1) != 0)
1697 put_packet(s
, "E14");
1699 put_packet(s
, "OK");
1702 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1703 This works, but can be very slow. Anything new enough to
1704 understand XML also knows how to use this properly. */
1706 goto unknown_command
;
1707 addr
= strtoull(p
, (char **)&p
, 16);
1708 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
, addr
);
1710 memtohex(buf
, mem_buf
, reg_size
);
1713 put_packet(s
, "E14");
1718 goto unknown_command
;
1719 addr
= strtoull(p
, (char **)&p
, 16);
1722 reg_size
= strlen(p
) / 2;
1723 hextomem(mem_buf
, p
, reg_size
);
1724 gdb_write_register(s
->g_cpu
, mem_buf
, addr
);
1725 put_packet(s
, "OK");
1729 type
= strtoul(p
, (char **)&p
, 16);
1732 addr
= strtoull(p
, (char **)&p
, 16);
1735 len
= strtoull(p
, (char **)&p
, 16);
1737 res
= gdb_breakpoint_insert(addr
, len
, type
);
1739 res
= gdb_breakpoint_remove(addr
, len
, type
);
1741 put_packet(s
, "OK");
1742 else if (res
== -ENOSYS
)
1745 put_packet(s
, "E22");
1749 thread
= strtoull(p
, (char **)&p
, 16);
1750 if (thread
== -1 || thread
== 0) {
1751 put_packet(s
, "OK");
1754 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1755 if (env
->cpu_index
+ 1 == thread
)
1758 put_packet(s
, "E22");
1764 put_packet(s
, "OK");
1768 put_packet(s
, "OK");
1771 put_packet(s
, "E22");
1776 thread
= strtoull(p
, (char **)&p
, 16);
1777 #ifndef CONFIG_USER_ONLY
1778 if (thread
> 0 && thread
< smp_cpus
+ 1)
1782 put_packet(s
, "OK");
1784 put_packet(s
, "E22");
1788 /* parse any 'q' packets here */
1789 if (!strcmp(p
,"qemu.sstepbits")) {
1790 /* Query Breakpoint bit definitions */
1791 snprintf(buf
, sizeof(buf
), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1797 } else if (strncmp(p
,"qemu.sstep",10) == 0) {
1798 /* Display or change the sstep_flags */
1801 /* Display current setting */
1802 snprintf(buf
, sizeof(buf
), "0x%x", sstep_flags
);
1807 type
= strtoul(p
, (char **)&p
, 16);
1809 put_packet(s
, "OK");
1811 } else if (strcmp(p
,"C") == 0) {
1812 /* "Current thread" remains vague in the spec, so always return
1813 * the first CPU (gdb returns the first thread). */
1814 put_packet(s
, "QC1");
1816 } else if (strcmp(p
,"fThreadInfo") == 0) {
1817 s
->query_cpu
= first_cpu
;
1818 goto report_cpuinfo
;
1819 } else if (strcmp(p
,"sThreadInfo") == 0) {
1822 snprintf(buf
, sizeof(buf
), "m%x", s
->query_cpu
->cpu_index
+1);
1824 s
->query_cpu
= s
->query_cpu
->next_cpu
;
1828 } else if (strncmp(p
,"ThreadExtraInfo,", 16) == 0) {
1829 thread
= strtoull(p
+16, (char **)&p
, 16);
1830 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
)
1831 if (env
->cpu_index
+ 1 == thread
) {
1832 cpu_synchronize_state(env
, 0);
1833 len
= snprintf((char *)mem_buf
, sizeof(mem_buf
),
1834 "CPU#%d [%s]", env
->cpu_index
,
1835 env
->halted
? "halted " : "running");
1836 memtohex(buf
, mem_buf
, len
);
1842 #ifdef CONFIG_USER_ONLY
1843 else if (strncmp(p
, "Offsets", 7) == 0) {
1844 TaskState
*ts
= s
->c_cpu
->opaque
;
1846 snprintf(buf
, sizeof(buf
),
1847 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1848 ";Bss=" TARGET_ABI_FMT_lx
,
1849 ts
->info
->code_offset
,
1850 ts
->info
->data_offset
,
1851 ts
->info
->data_offset
);
1855 #else /* !CONFIG_USER_ONLY */
1856 else if (strncmp(p
, "Rcmd,", 5) == 0) {
1857 int len
= strlen(p
+ 5);
1859 if ((len
% 2) != 0) {
1860 put_packet(s
, "E01");
1863 hextomem(mem_buf
, p
+ 5, len
);
1866 qemu_chr_read(s
->mon_chr
, mem_buf
, len
);
1867 put_packet(s
, "OK");
1870 #endif /* !CONFIG_USER_ONLY */
1871 if (strncmp(p
, "Supported", 9) == 0) {
1872 snprintf(buf
, sizeof(buf
), "PacketSize=%x", MAX_PACKET_LENGTH
);
1874 pstrcat(buf
, sizeof(buf
), ";qXfer:features:read+");
1880 if (strncmp(p
, "Xfer:features:read:", 19) == 0) {
1882 target_ulong total_len
;
1886 xml
= get_feature_xml(p
, &p
);
1888 snprintf(buf
, sizeof(buf
), "E00");
1895 addr
= strtoul(p
, (char **)&p
, 16);
1898 len
= strtoul(p
, (char **)&p
, 16);
1900 total_len
= strlen(xml
);
1901 if (addr
> total_len
) {
1902 snprintf(buf
, sizeof(buf
), "E00");
1906 if (len
> (MAX_PACKET_LENGTH
- 5) / 2)
1907 len
= (MAX_PACKET_LENGTH
- 5) / 2;
1908 if (len
< total_len
- addr
) {
1910 len
= memtox(buf
+ 1, xml
+ addr
, len
);
1913 len
= memtox(buf
+ 1, xml
+ addr
, total_len
- addr
);
1915 put_packet_binary(s
, buf
, len
+ 1);
1919 /* Unrecognised 'q' command. */
1920 goto unknown_command
;
1924 /* put empty packet */
1932 void gdb_set_stop_cpu(CPUState
*env
)
1934 gdbserver_state
->c_cpu
= env
;
1935 gdbserver_state
->g_cpu
= env
;
1938 #ifndef CONFIG_USER_ONLY
1939 static void gdb_vm_state_change(void *opaque
, int running
, int reason
)
1941 GDBState
*s
= gdbserver_state
;
1942 CPUState
*env
= s
->c_cpu
;
1947 if (running
|| (reason
!= EXCP_DEBUG
&& reason
!= EXCP_INTERRUPT
) ||
1948 s
->state
== RS_INACTIVE
|| s
->state
== RS_SYSCALL
)
1951 /* disable single step if it was enable */
1952 cpu_single_step(env
, 0);
1954 if (reason
== EXCP_DEBUG
) {
1955 if (env
->watchpoint_hit
) {
1956 switch (env
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
1967 snprintf(buf
, sizeof(buf
),
1968 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx
";",
1969 GDB_SIGNAL_TRAP
, env
->cpu_index
+1, type
,
1970 env
->watchpoint_hit
->vaddr
);
1972 env
->watchpoint_hit
= NULL
;
1976 ret
= GDB_SIGNAL_TRAP
;
1978 ret
= GDB_SIGNAL_INT
;
1980 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", ret
, env
->cpu_index
+1);
1985 /* Send a gdb syscall request.
1986 This accepts limited printf-style format specifiers, specifically:
1987 %x - target_ulong argument printed in hex.
1988 %lx - 64-bit argument printed in hex.
1989 %s - string pointer (target_ulong) and length (int) pair. */
1990 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
1999 s
= gdbserver_state
;
2002 gdb_current_syscall_cb
= cb
;
2003 s
->state
= RS_SYSCALL
;
2004 #ifndef CONFIG_USER_ONLY
2005 vm_stop(EXCP_DEBUG
);
2016 addr
= va_arg(va
, target_ulong
);
2017 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
, addr
);
2020 if (*(fmt
++) != 'x')
2022 i64
= va_arg(va
, uint64_t);
2023 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, "%" PRIx64
, i64
);
2026 addr
= va_arg(va
, target_ulong
);
2027 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
"/%x",
2028 addr
, va_arg(va
, int));
2032 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
2043 #ifdef CONFIG_USER_ONLY
2044 gdb_handlesig(s
->c_cpu
, 0);
2050 static void gdb_read_byte(GDBState
*s
, int ch
)
2055 #ifndef CONFIG_USER_ONLY
2056 if (s
->last_packet_len
) {
2057 /* Waiting for a response to the last packet. If we see the start
2058 of a new command then abandon the previous response. */
2061 printf("Got NACK, retransmitting\n");
2063 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
2067 printf("Got ACK\n");
2069 printf("Got '%c' when expecting ACK/NACK\n", ch
);
2071 if (ch
== '+' || ch
== '$')
2072 s
->last_packet_len
= 0;
2077 /* when the CPU is running, we cannot do anything except stop
2078 it when receiving a char */
2079 vm_stop(EXCP_INTERRUPT
);
2086 s
->line_buf_index
= 0;
2087 s
->state
= RS_GETLINE
;
2092 s
->state
= RS_CHKSUM1
;
2093 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2096 s
->line_buf
[s
->line_buf_index
++] = ch
;
2100 s
->line_buf
[s
->line_buf_index
] = '\0';
2101 s
->line_csum
= fromhex(ch
) << 4;
2102 s
->state
= RS_CHKSUM2
;
2105 s
->line_csum
|= fromhex(ch
);
2107 for(i
= 0; i
< s
->line_buf_index
; i
++) {
2108 csum
+= s
->line_buf
[i
];
2110 if (s
->line_csum
!= (csum
& 0xff)) {
2112 put_buffer(s
, &reply
, 1);
2116 put_buffer(s
, &reply
, 1);
2117 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
2126 #ifdef CONFIG_USER_ONLY
2132 s
= gdbserver_state
;
2134 if (gdbserver_fd
< 0 || s
->fd
< 0)
2141 gdb_handlesig (CPUState
*env
, int sig
)
2147 s
= gdbserver_state
;
2148 if (gdbserver_fd
< 0 || s
->fd
< 0)
2151 /* disable single step if it was enabled */
2152 cpu_single_step(env
, 0);
2157 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb (sig
));
2160 /* put_packet() might have detected that the peer terminated the
2167 s
->running_state
= 0;
2168 while (s
->running_state
== 0) {
2169 n
= read (s
->fd
, buf
, 256);
2174 for (i
= 0; i
< n
; i
++)
2175 gdb_read_byte (s
, buf
[i
]);
2177 else if (n
== 0 || errno
!= EAGAIN
)
2179 /* XXX: Connection closed. Should probably wait for annother
2180 connection before continuing. */
2189 /* Tell the remote gdb that the process has exited. */
2190 void gdb_exit(CPUState
*env
, int code
)
2195 s
= gdbserver_state
;
2196 if (gdbserver_fd
< 0 || s
->fd
< 0)
2199 snprintf(buf
, sizeof(buf
), "W%02x", code
);
2203 /* Tell the remote gdb that the process has exited due to SIG. */
2204 void gdb_signalled(CPUState
*env
, int sig
)
2209 s
= gdbserver_state
;
2210 if (gdbserver_fd
< 0 || s
->fd
< 0)
2213 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb (sig
));
2217 static void gdb_accept(void)
2220 struct sockaddr_in sockaddr
;
2225 len
= sizeof(sockaddr
);
2226 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
2227 if (fd
< 0 && errno
!= EINTR
) {
2230 } else if (fd
>= 0) {
2235 /* set short latency */
2237 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2239 s
= qemu_mallocz(sizeof(GDBState
));
2240 s
->c_cpu
= first_cpu
;
2241 s
->g_cpu
= first_cpu
;
2245 gdbserver_state
= s
;
2247 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2250 static int gdbserver_open(int port
)
2252 struct sockaddr_in sockaddr
;
2255 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2261 /* allow fast reuse */
2263 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
2265 sockaddr
.sin_family
= AF_INET
;
2266 sockaddr
.sin_port
= htons(port
);
2267 sockaddr
.sin_addr
.s_addr
= 0;
2268 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
2273 ret
= listen(fd
, 0);
2281 int gdbserver_start(int port
)
2283 gdbserver_fd
= gdbserver_open(port
);
2284 if (gdbserver_fd
< 0)
2286 /* accept connections */
2291 /* Disable gdb stub for child processes. */
2292 void gdbserver_fork(CPUState
*env
)
2294 GDBState
*s
= gdbserver_state
;
2295 if (gdbserver_fd
< 0 || s
->fd
< 0)
2299 cpu_breakpoint_remove_all(env
, BP_GDB
);
2300 cpu_watchpoint_remove_all(env
, BP_GDB
);
2303 static int gdb_chr_can_receive(void *opaque
)
2305 /* We can handle an arbitrarily large amount of data.
2306 Pick the maximum packet size, which is as good as anything. */
2307 return MAX_PACKET_LENGTH
;
2310 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
2314 for (i
= 0; i
< size
; i
++) {
2315 gdb_read_byte(gdbserver_state
, buf
[i
]);
2319 static void gdb_chr_event(void *opaque
, int event
)
2322 case CHR_EVENT_RESET
:
2323 vm_stop(EXCP_INTERRUPT
);
2331 static void gdb_monitor_output(GDBState
*s
, const char *msg
, int len
)
2333 char buf
[MAX_PACKET_LENGTH
];
2336 if (len
> (MAX_PACKET_LENGTH
/2) - 1)
2337 len
= (MAX_PACKET_LENGTH
/2) - 1;
2338 memtohex(buf
+ 1, (uint8_t *)msg
, len
);
2342 static int gdb_monitor_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2344 const char *p
= (const char *)buf
;
2347 max_sz
= (sizeof(gdbserver_state
->last_packet
) - 2) / 2;
2349 if (len
<= max_sz
) {
2350 gdb_monitor_output(gdbserver_state
, p
, len
);
2353 gdb_monitor_output(gdbserver_state
, p
, max_sz
);
2361 static void gdb_sigterm_handler(int signal
)
2364 vm_stop(EXCP_INTERRUPT
);
2368 int gdbserver_start(const char *device
)
2371 char gdbstub_device_name
[128];
2372 CharDriverState
*chr
= NULL
;
2373 CharDriverState
*mon_chr
;
2377 if (strcmp(device
, "none") != 0) {
2378 if (strstart(device
, "tcp:", NULL
)) {
2379 /* enforce required TCP attributes */
2380 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
2381 "%s,nowait,nodelay,server", device
);
2382 device
= gdbstub_device_name
;
2385 else if (strcmp(device
, "stdio") == 0) {
2386 struct sigaction act
;
2388 memset(&act
, 0, sizeof(act
));
2389 act
.sa_handler
= gdb_sigterm_handler
;
2390 sigaction(SIGINT
, &act
, NULL
);
2393 chr
= qemu_chr_open("gdb", device
, NULL
);
2397 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
2398 gdb_chr_event
, NULL
);
2401 s
= gdbserver_state
;
2403 s
= qemu_mallocz(sizeof(GDBState
));
2404 gdbserver_state
= s
;
2406 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
2408 /* Initialize a monitor terminal for gdb */
2409 mon_chr
= qemu_mallocz(sizeof(*mon_chr
));
2410 mon_chr
->chr_write
= gdb_monitor_write
;
2411 monitor_init(mon_chr
, 0);
2414 qemu_chr_close(s
->chr
);
2415 mon_chr
= s
->mon_chr
;
2416 memset(s
, 0, sizeof(GDBState
));
2418 s
->c_cpu
= first_cpu
;
2419 s
->g_cpu
= first_cpu
;
2421 s
->state
= chr
? RS_IDLE
: RS_INACTIVE
;
2422 s
->mon_chr
= mon_chr
;