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
33 #include "qemu-char.h"
38 #define MAX_PACKET_LENGTH 4096
40 #include "qemu_socket.h"
48 GDB_SIGNAL_UNKNOWN
= 143
51 #ifdef CONFIG_USER_ONLY
53 /* Map target signal numbers to GDB protocol signal numbers and vice
54 * versa. For user emulation's currently supported systems, we can
55 * assume most signals are defined.
58 static int gdb_signal_table
[] = {
218 /* In system mode we only need SIGINT and SIGTRAP; other signals
219 are not yet supported. */
226 static int gdb_signal_table
[] = {
236 #ifdef CONFIG_USER_ONLY
237 static int target_signal_to_gdb (int sig
)
240 for (i
= 0; i
< ARRAY_SIZE (gdb_signal_table
); i
++)
241 if (gdb_signal_table
[i
] == sig
)
243 return GDB_SIGNAL_UNKNOWN
;
247 static int gdb_signal_to_target (int sig
)
249 if (sig
< ARRAY_SIZE (gdb_signal_table
))
250 return gdb_signal_table
[sig
];
257 typedef struct GDBRegisterState
{
263 struct GDBRegisterState
*next
;
274 typedef struct GDBState
{
275 CPUState
*c_cpu
; /* current CPU for step/continue ops */
276 CPUState
*g_cpu
; /* current CPU for other ops */
277 CPUState
*query_cpu
; /* for q{f|s}ThreadInfo */
278 enum RSState state
; /* parsing state */
279 char line_buf
[MAX_PACKET_LENGTH
];
282 uint8_t last_packet
[MAX_PACKET_LENGTH
+ 4];
285 #ifdef CONFIG_USER_ONLY
289 CharDriverState
*chr
;
290 CharDriverState
*mon_chr
;
294 /* By default use no IRQs and no timers while single stepping so as to
295 * make single stepping like an ICE HW step.
297 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
299 static GDBState
*gdbserver_state
;
301 /* This is an ugly hack to cope with both new and old gdb.
302 If gdb sends qXfer:features:read then assume we're talking to a newish
303 gdb that understands target descriptions. */
304 static int gdb_has_xml
;
306 #ifdef CONFIG_USER_ONLY
307 /* XXX: This is not thread safe. Do we care? */
308 static int gdbserver_fd
= -1;
310 static int get_char(GDBState
*s
)
316 ret
= recv(s
->fd
, &ch
, 1, 0);
318 if (errno
== ECONNRESET
)
320 if (errno
!= EINTR
&& errno
!= EAGAIN
)
322 } else if (ret
== 0) {
334 static gdb_syscall_complete_cb gdb_current_syscall_cb
;
342 /* If gdb is connected when the first semihosting syscall occurs then use
343 remote gdb syscalls. Otherwise use native file IO. */
344 int use_gdb_syscalls(void)
346 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
347 gdb_syscall_mode
= (gdbserver_state
? GDB_SYS_ENABLED
350 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
353 /* Resume execution. */
354 static inline void gdb_continue(GDBState
*s
)
356 #ifdef CONFIG_USER_ONLY
357 s
->running_state
= 1;
363 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
365 #ifdef CONFIG_USER_ONLY
369 ret
= send(s
->fd
, buf
, len
, 0);
371 if (errno
!= EINTR
&& errno
!= EAGAIN
)
379 qemu_chr_write(s
->chr
, buf
, len
);
383 static inline int fromhex(int v
)
385 if (v
>= '0' && v
<= '9')
387 else if (v
>= 'A' && v
<= 'F')
389 else if (v
>= 'a' && v
<= 'f')
395 static inline int tohex(int v
)
403 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
408 for(i
= 0; i
< len
; i
++) {
410 *q
++ = tohex(c
>> 4);
411 *q
++ = tohex(c
& 0xf);
416 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
420 for(i
= 0; i
< len
; i
++) {
421 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
426 /* return -1 if error, 0 if OK */
427 static int put_packet_binary(GDBState
*s
, const char *buf
, int len
)
438 for(i
= 0; i
< len
; i
++) {
442 *(p
++) = tohex((csum
>> 4) & 0xf);
443 *(p
++) = tohex((csum
) & 0xf);
445 s
->last_packet_len
= p
- s
->last_packet
;
446 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
448 #ifdef CONFIG_USER_ONLY
461 /* return -1 if error, 0 if OK */
462 static int put_packet(GDBState
*s
, const char *buf
)
465 printf("reply='%s'\n", buf
);
468 return put_packet_binary(s
, buf
, strlen(buf
));
471 /* The GDB remote protocol transfers values in target byte order. This means
472 we can use the raw memory access routines to access the value buffer.
473 Conveniently, these also handle the case where the buffer is mis-aligned.
475 #define GET_REG8(val) do { \
476 stb_p(mem_buf, val); \
479 #define GET_REG16(val) do { \
480 stw_p(mem_buf, val); \
483 #define GET_REG32(val) do { \
484 stl_p(mem_buf, val); \
487 #define GET_REG64(val) do { \
488 stq_p(mem_buf, val); \
492 #if TARGET_LONG_BITS == 64
493 #define GET_REGL(val) GET_REG64(val)
494 #define ldtul_p(addr) ldq_p(addr)
496 #define GET_REGL(val) GET_REG32(val)
497 #define ldtul_p(addr) ldl_p(addr)
500 #if defined(TARGET_I386)
503 static const int gpr_map
[16] = {
504 R_EAX
, R_EBX
, R_ECX
, R_EDX
, R_ESI
, R_EDI
, R_EBP
, R_ESP
,
505 8, 9, 10, 11, 12, 13, 14, 15
508 static const int gpr_map
[8] = {0, 1, 2, 3, 4, 5, 6, 7};
511 #define NUM_CORE_REGS (CPU_NB_REGS * 2 + 25)
513 #define IDX_IP_REG CPU_NB_REGS
514 #define IDX_FLAGS_REG (IDX_IP_REG + 1)
515 #define IDX_SEG_REGS (IDX_FLAGS_REG + 1)
516 #define IDX_FP_REGS (IDX_SEG_REGS + 6)
517 #define IDX_XMM_REGS (IDX_FP_REGS + 16)
518 #define IDX_MXCSR_REG (IDX_XMM_REGS + CPU_NB_REGS)
520 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
522 if (n
< CPU_NB_REGS
) {
523 GET_REGL(env
->regs
[gpr_map
[n
]]);
524 } else if (n
>= IDX_FP_REGS
&& n
< IDX_FP_REGS
+ 8) {
525 #ifdef USE_X86LDOUBLE
526 /* FIXME: byteswap float values - after fixing fpregs layout. */
527 memcpy(mem_buf
, &env
->fpregs
[n
- IDX_FP_REGS
], 10);
529 memset(mem_buf
, 0, 10);
532 } else if (n
>= IDX_XMM_REGS
&& n
< IDX_XMM_REGS
+ CPU_NB_REGS
) {
534 stq_p(mem_buf
, env
->xmm_regs
[n
].XMM_Q(0));
535 stq_p(mem_buf
+ 8, env
->xmm_regs
[n
].XMM_Q(1));
539 case IDX_IP_REG
: GET_REGL(env
->eip
);
540 case IDX_FLAGS_REG
: GET_REG32(env
->eflags
);
542 case IDX_SEG_REGS
: GET_REG32(env
->segs
[R_CS
].selector
);
543 case IDX_SEG_REGS
+ 1: GET_REG32(env
->segs
[R_SS
].selector
);
544 case IDX_SEG_REGS
+ 2: GET_REG32(env
->segs
[R_DS
].selector
);
545 case IDX_SEG_REGS
+ 3: GET_REG32(env
->segs
[R_ES
].selector
);
546 case IDX_SEG_REGS
+ 4: GET_REG32(env
->segs
[R_FS
].selector
);
547 case IDX_SEG_REGS
+ 5: GET_REG32(env
->segs
[R_GS
].selector
);
549 case IDX_FP_REGS
+ 8: GET_REG32(env
->fpuc
);
550 case IDX_FP_REGS
+ 9: GET_REG32((env
->fpus
& ~0x3800) |
551 (env
->fpstt
& 0x7) << 11);
552 case IDX_FP_REGS
+ 10: GET_REG32(0); /* ftag */
553 case IDX_FP_REGS
+ 11: GET_REG32(0); /* fiseg */
554 case IDX_FP_REGS
+ 12: GET_REG32(0); /* fioff */
555 case IDX_FP_REGS
+ 13: GET_REG32(0); /* foseg */
556 case IDX_FP_REGS
+ 14: GET_REG32(0); /* fooff */
557 case IDX_FP_REGS
+ 15: GET_REG32(0); /* fop */
559 case IDX_MXCSR_REG
: GET_REG32(env
->mxcsr
);
565 static int cpu_x86_gdb_load_seg(CPUState
*env
, int sreg
, uint8_t *mem_buf
)
567 uint16_t selector
= ldl_p(mem_buf
);
569 if (selector
!= env
->segs
[sreg
].selector
) {
570 #if defined(CONFIG_USER_ONLY)
571 cpu_x86_load_seg(env
, sreg
, selector
);
573 unsigned int limit
, flags
;
576 if (!(env
->cr
[0] & CR0_PE_MASK
) || (env
->eflags
& VM_MASK
)) {
577 base
= selector
<< 4;
581 if (!cpu_x86_get_descr_debug(env
, selector
, &base
, &limit
, &flags
))
584 cpu_x86_load_seg_cache(env
, sreg
, selector
, base
, limit
, flags
);
590 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
594 if (n
< CPU_NB_REGS
) {
595 env
->regs
[gpr_map
[n
]] = ldtul_p(mem_buf
);
596 return sizeof(target_ulong
);
597 } else if (n
>= IDX_FP_REGS
&& n
< IDX_FP_REGS
+ 8) {
598 #ifdef USE_X86LDOUBLE
599 /* FIXME: byteswap float values - after fixing fpregs layout. */
600 memcpy(&env
->fpregs
[n
- IDX_FP_REGS
], mem_buf
, 10);
603 } else if (n
>= IDX_XMM_REGS
&& n
< IDX_XMM_REGS
+ CPU_NB_REGS
) {
605 env
->xmm_regs
[n
].XMM_Q(0) = ldq_p(mem_buf
);
606 env
->xmm_regs
[n
].XMM_Q(1) = ldq_p(mem_buf
+ 8);
611 env
->eip
= ldtul_p(mem_buf
);
612 return sizeof(target_ulong
);
614 env
->eflags
= ldl_p(mem_buf
);
617 case IDX_SEG_REGS
: return cpu_x86_gdb_load_seg(env
, R_CS
, mem_buf
);
618 case IDX_SEG_REGS
+ 1: return cpu_x86_gdb_load_seg(env
, R_SS
, mem_buf
);
619 case IDX_SEG_REGS
+ 2: return cpu_x86_gdb_load_seg(env
, R_DS
, mem_buf
);
620 case IDX_SEG_REGS
+ 3: return cpu_x86_gdb_load_seg(env
, R_ES
, mem_buf
);
621 case IDX_SEG_REGS
+ 4: return cpu_x86_gdb_load_seg(env
, R_FS
, mem_buf
);
622 case IDX_SEG_REGS
+ 5: return cpu_x86_gdb_load_seg(env
, R_GS
, mem_buf
);
624 case IDX_FP_REGS
+ 8:
625 env
->fpuc
= ldl_p(mem_buf
);
627 case IDX_FP_REGS
+ 9:
628 tmp
= ldl_p(mem_buf
);
629 env
->fpstt
= (tmp
>> 11) & 7;
630 env
->fpus
= tmp
& ~0x3800;
632 case IDX_FP_REGS
+ 10: /* ftag */ return 4;
633 case IDX_FP_REGS
+ 11: /* fiseg */ return 4;
634 case IDX_FP_REGS
+ 12: /* fioff */ return 4;
635 case IDX_FP_REGS
+ 13: /* foseg */ return 4;
636 case IDX_FP_REGS
+ 14: /* fooff */ return 4;
637 case IDX_FP_REGS
+ 15: /* fop */ return 4;
640 env
->mxcsr
= ldl_p(mem_buf
);
644 /* Unrecognised register. */
648 #elif defined (TARGET_PPC)
650 /* Old gdb always expects FP registers. Newer (xml-aware) gdb only
651 expects whatever the target description contains. Due to a
652 historical mishap the FP registers appear in between core integer
653 regs and PC, MSR, CR, and so forth. We hack round this by giving the
654 FP regs zero size when talking to a newer gdb. */
655 #define NUM_CORE_REGS 71
656 #if defined (TARGET_PPC64)
657 #define GDB_CORE_XML "power64-core.xml"
659 #define GDB_CORE_XML "power-core.xml"
662 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
666 GET_REGL(env
->gpr
[n
]);
671 stfq_p(mem_buf
, env
->fpr
[n
-32]);
675 case 64: GET_REGL(env
->nip
);
676 case 65: GET_REGL(env
->msr
);
681 for (i
= 0; i
< 8; i
++)
682 cr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
685 case 67: GET_REGL(env
->lr
);
686 case 68: GET_REGL(env
->ctr
);
687 case 69: GET_REGL(env
->xer
);
692 GET_REG32(0); /* fpscr */
699 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
703 env
->gpr
[n
] = ldtul_p(mem_buf
);
704 return sizeof(target_ulong
);
709 env
->fpr
[n
-32] = ldfq_p(mem_buf
);
714 env
->nip
= ldtul_p(mem_buf
);
715 return sizeof(target_ulong
);
717 ppc_store_msr(env
, ldtul_p(mem_buf
));
718 return sizeof(target_ulong
);
721 uint32_t cr
= ldl_p(mem_buf
);
723 for (i
= 0; i
< 8; i
++)
724 env
->crf
[i
] = (cr
>> (32 - ((i
+ 1) * 4))) & 0xF;
728 env
->lr
= ldtul_p(mem_buf
);
729 return sizeof(target_ulong
);
731 env
->ctr
= ldtul_p(mem_buf
);
732 return sizeof(target_ulong
);
734 env
->xer
= ldtul_p(mem_buf
);
735 return sizeof(target_ulong
);
746 #elif defined (TARGET_SPARC)
748 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
749 #define NUM_CORE_REGS 86
751 #define NUM_CORE_REGS 72
755 #define GET_REGA(val) GET_REG32(val)
757 #define GET_REGA(val) GET_REGL(val)
760 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
764 GET_REGA(env
->gregs
[n
]);
767 /* register window */
768 GET_REGA(env
->regwptr
[n
- 8]);
770 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
773 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
775 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
777 case 64: GET_REGA(env
->y
);
778 case 65: GET_REGA(GET_PSR(env
));
779 case 66: GET_REGA(env
->wim
);
780 case 67: GET_REGA(env
->tbr
);
781 case 68: GET_REGA(env
->pc
);
782 case 69: GET_REGA(env
->npc
);
783 case 70: GET_REGA(env
->fsr
);
784 case 71: GET_REGA(0); /* csr */
785 default: GET_REGA(0);
790 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
793 /* f32-f62 (double width, even numbers only) */
796 val
= (uint64_t)*((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) << 32;
797 val
|= *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]);
801 case 80: GET_REGL(env
->pc
);
802 case 81: GET_REGL(env
->npc
);
803 case 82: GET_REGL(((uint64_t)GET_CCR(env
) << 32) |
804 ((env
->asi
& 0xff) << 24) |
805 ((env
->pstate
& 0xfff) << 8) |
807 case 83: GET_REGL(env
->fsr
);
808 case 84: GET_REGL(env
->fprs
);
809 case 85: GET_REGL(env
->y
);
815 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
817 #if defined(TARGET_ABI32)
820 tmp
= ldl_p(mem_buf
);
824 tmp
= ldtul_p(mem_buf
);
831 /* register window */
832 env
->regwptr
[n
- 8] = tmp
;
834 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
837 *((uint32_t *)&env
->fpr
[n
- 32]) = tmp
;
839 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
841 case 64: env
->y
= tmp
; break;
842 case 65: PUT_PSR(env
, tmp
); break;
843 case 66: env
->wim
= tmp
; break;
844 case 67: env
->tbr
= tmp
; break;
845 case 68: env
->pc
= tmp
; break;
846 case 69: env
->npc
= tmp
; break;
847 case 70: env
->fsr
= tmp
; break;
855 env
->fpr
[n
] = ldfl_p(mem_buf
);
858 /* f32-f62 (double width, even numbers only) */
859 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) = tmp
>> 32;
860 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]) = tmp
;
863 case 80: env
->pc
= tmp
; break;
864 case 81: env
->npc
= tmp
; break;
866 PUT_CCR(env
, tmp
>> 32);
867 env
->asi
= (tmp
>> 24) & 0xff;
868 env
->pstate
= (tmp
>> 8) & 0xfff;
869 PUT_CWP64(env
, tmp
& 0xff);
871 case 83: env
->fsr
= tmp
; break;
872 case 84: env
->fprs
= tmp
; break;
873 case 85: env
->y
= tmp
; break;
880 #elif defined (TARGET_ARM)
882 /* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect
883 whatever the target description contains. Due to a historical mishap
884 the FPA registers appear in between core integer regs and the CPSR.
885 We hack round this by giving the FPA regs zero size when talking to a
887 #define NUM_CORE_REGS 26
888 #define GDB_CORE_XML "arm-core.xml"
890 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
893 /* Core integer register. */
894 GET_REG32(env
->regs
[n
]);
900 memset(mem_buf
, 0, 12);
905 /* FPA status register. */
911 GET_REG32(cpsr_read(env
));
913 /* Unknown register. */
917 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
921 tmp
= ldl_p(mem_buf
);
923 /* Mask out low bit of PC to workaround gdb bugs. This will probably
924 cause problems if we ever implement the Jazelle DBX extensions. */
929 /* Core integer register. */
933 if (n
< 24) { /* 16-23 */
934 /* FPA registers (ignored). */
941 /* FPA status register (ignored). */
947 cpsr_write (env
, tmp
, 0xffffffff);
950 /* Unknown register. */
954 #elif defined (TARGET_M68K)
956 #define NUM_CORE_REGS 18
958 #define GDB_CORE_XML "cf-core.xml"
960 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
964 GET_REG32(env
->dregs
[n
]);
967 GET_REG32(env
->aregs
[n
- 8]);
970 case 16: GET_REG32(env
->sr
);
971 case 17: GET_REG32(env
->pc
);
974 /* FP registers not included here because they vary between
975 ColdFire and m68k. Use XML bits for these. */
979 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
983 tmp
= ldl_p(mem_buf
);
990 env
->aregs
[n
- 8] = tmp
;
993 case 16: env
->sr
= tmp
; break;
994 case 17: env
->pc
= tmp
; break;
1000 #elif defined (TARGET_MIPS)
1002 #define NUM_CORE_REGS 73
1004 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1007 GET_REGL(env
->active_tc
.gpr
[n
]);
1009 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
1010 if (n
>= 38 && n
< 70) {
1011 if (env
->CP0_Status
& (1 << CP0St_FR
))
1012 GET_REGL(env
->active_fpu
.fpr
[n
- 38].d
);
1014 GET_REGL(env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
]);
1017 case 70: GET_REGL((int32_t)env
->active_fpu
.fcr31
);
1018 case 71: GET_REGL((int32_t)env
->active_fpu
.fcr0
);
1022 case 32: GET_REGL((int32_t)env
->CP0_Status
);
1023 case 33: GET_REGL(env
->active_tc
.LO
[0]);
1024 case 34: GET_REGL(env
->active_tc
.HI
[0]);
1025 case 35: GET_REGL(env
->CP0_BadVAddr
);
1026 case 36: GET_REGL((int32_t)env
->CP0_Cause
);
1027 case 37: GET_REGL(env
->active_tc
.PC
);
1028 case 72: GET_REGL(0); /* fp */
1029 case 89: GET_REGL((int32_t)env
->CP0_PRid
);
1031 if (n
>= 73 && n
<= 88) {
1032 /* 16 embedded regs. */
1039 /* convert MIPS rounding mode in FCR31 to IEEE library */
1040 static unsigned int ieee_rm
[] =
1042 float_round_nearest_even
,
1043 float_round_to_zero
,
1047 #define RESTORE_ROUNDING_MODE \
1048 set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
1050 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1054 tmp
= ldtul_p(mem_buf
);
1057 env
->active_tc
.gpr
[n
] = tmp
;
1058 return sizeof(target_ulong
);
1060 if (env
->CP0_Config1
& (1 << CP0C1_FP
)
1061 && n
>= 38 && n
< 73) {
1063 if (env
->CP0_Status
& (1 << CP0St_FR
))
1064 env
->active_fpu
.fpr
[n
- 38].d
= tmp
;
1066 env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
] = tmp
;
1070 env
->active_fpu
.fcr31
= tmp
& 0xFF83FFFF;
1071 /* set rounding mode */
1072 RESTORE_ROUNDING_MODE
;
1073 #ifndef CONFIG_SOFTFLOAT
1074 /* no floating point exception for native float */
1075 SET_FP_ENABLE(env
->active_fpu
.fcr31
, 0);
1078 case 71: env
->active_fpu
.fcr0
= tmp
; break;
1080 return sizeof(target_ulong
);
1083 case 32: env
->CP0_Status
= tmp
; break;
1084 case 33: env
->active_tc
.LO
[0] = tmp
; break;
1085 case 34: env
->active_tc
.HI
[0] = tmp
; break;
1086 case 35: env
->CP0_BadVAddr
= tmp
; break;
1087 case 36: env
->CP0_Cause
= tmp
; break;
1088 case 37: env
->active_tc
.PC
= tmp
; break;
1089 case 72: /* fp, ignored */ break;
1093 /* Other registers are readonly. Ignore writes. */
1097 return sizeof(target_ulong
);
1099 #elif defined (TARGET_SH4)
1101 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
1102 /* FIXME: We should use XML for this. */
1104 #define NUM_CORE_REGS 59
1106 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1109 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1110 GET_REGL(env
->gregs
[n
+ 16]);
1112 GET_REGL(env
->gregs
[n
]);
1114 } else if (n
< 16) {
1115 GET_REGL(env
->gregs
[n
- 8]);
1116 } else if (n
>= 25 && n
< 41) {
1117 GET_REGL(env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
1118 } else if (n
>= 43 && n
< 51) {
1119 GET_REGL(env
->gregs
[n
- 43]);
1120 } else if (n
>= 51 && n
< 59) {
1121 GET_REGL(env
->gregs
[n
- (51 - 16)]);
1124 case 16: GET_REGL(env
->pc
);
1125 case 17: GET_REGL(env
->pr
);
1126 case 18: GET_REGL(env
->gbr
);
1127 case 19: GET_REGL(env
->vbr
);
1128 case 20: GET_REGL(env
->mach
);
1129 case 21: GET_REGL(env
->macl
);
1130 case 22: GET_REGL(env
->sr
);
1131 case 23: GET_REGL(env
->fpul
);
1132 case 24: GET_REGL(env
->fpscr
);
1133 case 41: GET_REGL(env
->ssr
);
1134 case 42: GET_REGL(env
->spc
);
1140 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1144 tmp
= ldl_p(mem_buf
);
1147 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1148 env
->gregs
[n
+ 16] = tmp
;
1150 env
->gregs
[n
] = tmp
;
1153 } else if (n
< 16) {
1154 env
->gregs
[n
- 8] = tmp
;
1156 } else if (n
>= 25 && n
< 41) {
1157 env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)] = tmp
;
1158 } else if (n
>= 43 && n
< 51) {
1159 env
->gregs
[n
- 43] = tmp
;
1161 } else if (n
>= 51 && n
< 59) {
1162 env
->gregs
[n
- (51 - 16)] = tmp
;
1166 case 16: env
->pc
= tmp
;
1167 case 17: env
->pr
= tmp
;
1168 case 18: env
->gbr
= tmp
;
1169 case 19: env
->vbr
= tmp
;
1170 case 20: env
->mach
= tmp
;
1171 case 21: env
->macl
= tmp
;
1172 case 22: env
->sr
= tmp
;
1173 case 23: env
->fpul
= tmp
;
1174 case 24: env
->fpscr
= tmp
;
1175 case 41: env
->ssr
= tmp
;
1176 case 42: env
->spc
= tmp
;
1182 #elif defined (TARGET_MICROBLAZE)
1184 #define NUM_CORE_REGS (32 + 5)
1186 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1189 GET_REG32(env
->regs
[n
]);
1191 GET_REG32(env
->sregs
[n
- 32]);
1196 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1200 if (n
> NUM_CORE_REGS
)
1203 tmp
= ldl_p(mem_buf
);
1208 env
->sregs
[n
- 32] = tmp
;
1212 #elif defined (TARGET_CRIS)
1214 #define NUM_CORE_REGS 49
1216 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1220 srs
= env
->pregs
[PR_SRS
];
1222 GET_REG32(env
->regs
[n
]);
1225 if (n
>= 21 && n
< 32) {
1226 GET_REG32(env
->pregs
[n
- 16]);
1228 if (n
>= 33 && n
< 49) {
1229 GET_REG32(env
->sregs
[srs
][n
- 33]);
1232 case 16: GET_REG8(env
->pregs
[0]);
1233 case 17: GET_REG8(env
->pregs
[1]);
1234 case 18: GET_REG32(env
->pregs
[2]);
1235 case 19: GET_REG8(srs
);
1236 case 20: GET_REG16(env
->pregs
[4]);
1237 case 32: GET_REG32(env
->pc
);
1243 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1250 tmp
= ldl_p(mem_buf
);
1256 if (n
>= 21 && n
< 32) {
1257 env
->pregs
[n
- 16] = tmp
;
1260 /* FIXME: Should support function regs be writable? */
1264 case 18: env
->pregs
[PR_PID
] = tmp
; break;
1267 case 32: env
->pc
= tmp
; break;
1272 #elif defined (TARGET_ALPHA)
1274 #define NUM_CORE_REGS 65
1276 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1279 GET_REGL(env
->ir
[n
]);
1287 val
=*((uint64_t *)&env
->fir
[n
-32]);
1291 GET_REGL(env
->fpcr
);
1303 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1306 tmp
= ldtul_p(mem_buf
);
1312 if (n
> 31 && n
< 63) {
1313 env
->fir
[n
- 32] = ldfl_p(mem_buf
);
1324 #define NUM_CORE_REGS 0
1326 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1331 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1338 static int num_g_regs
= NUM_CORE_REGS
;
1341 /* Encode data using the encoding for 'x' packets. */
1342 static int memtox(char *buf
, const char *mem
, int len
)
1350 case '#': case '$': case '*': case '}':
1362 static const char *get_feature_xml(const char *p
, const char **newp
)
1364 extern const char *const xml_builtin
[][2];
1368 static char target_xml
[1024];
1371 while (p
[len
] && p
[len
] != ':')
1376 if (strncmp(p
, "target.xml", len
) == 0) {
1377 /* Generate the XML description for this CPU. */
1378 if (!target_xml
[0]) {
1379 GDBRegisterState
*r
;
1381 snprintf(target_xml
, sizeof(target_xml
),
1382 "<?xml version=\"1.0\"?>"
1383 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
1385 "<xi:include href=\"%s\"/>",
1388 for (r
= first_cpu
->gdb_regs
; r
; r
= r
->next
) {
1389 pstrcat(target_xml
, sizeof(target_xml
), "<xi:include href=\"");
1390 pstrcat(target_xml
, sizeof(target_xml
), r
->xml
);
1391 pstrcat(target_xml
, sizeof(target_xml
), "\"/>");
1393 pstrcat(target_xml
, sizeof(target_xml
), "</target>");
1397 for (i
= 0; ; i
++) {
1398 name
= xml_builtin
[i
][0];
1399 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
1402 return name
? xml_builtin
[i
][1] : NULL
;
1406 static int gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int reg
)
1408 GDBRegisterState
*r
;
1410 if (reg
< NUM_CORE_REGS
)
1411 return cpu_gdb_read_register(env
, mem_buf
, reg
);
1413 for (r
= env
->gdb_regs
; r
; r
= r
->next
) {
1414 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1415 return r
->get_reg(env
, mem_buf
, reg
- r
->base_reg
);
1421 static int gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int reg
)
1423 GDBRegisterState
*r
;
1425 if (reg
< NUM_CORE_REGS
)
1426 return cpu_gdb_write_register(env
, mem_buf
, reg
);
1428 for (r
= env
->gdb_regs
; r
; r
= r
->next
) {
1429 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1430 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
1436 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
1437 specifies the first register number and these registers are included in
1438 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
1439 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
1442 void gdb_register_coprocessor(CPUState
* env
,
1443 gdb_reg_cb get_reg
, gdb_reg_cb set_reg
,
1444 int num_regs
, const char *xml
, int g_pos
)
1446 GDBRegisterState
*s
;
1447 GDBRegisterState
**p
;
1448 static int last_reg
= NUM_CORE_REGS
;
1450 s
= (GDBRegisterState
*)qemu_mallocz(sizeof(GDBRegisterState
));
1451 s
->base_reg
= last_reg
;
1452 s
->num_regs
= num_regs
;
1453 s
->get_reg
= get_reg
;
1454 s
->set_reg
= set_reg
;
1458 /* Check for duplicates. */
1459 if (strcmp((*p
)->xml
, xml
) == 0)
1463 /* Add to end of list. */
1464 last_reg
+= num_regs
;
1467 if (g_pos
!= s
->base_reg
) {
1468 fprintf(stderr
, "Error: Bad gdb register numbering for '%s'\n"
1469 "Expected %d got %d\n", xml
, g_pos
, s
->base_reg
);
1471 num_g_regs
= last_reg
;
1476 #ifndef CONFIG_USER_ONLY
1477 static const int xlat_gdb_type
[] = {
1478 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
1479 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
1480 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
1484 static int gdb_breakpoint_insert(target_ulong addr
, target_ulong len
, int type
)
1490 return kvm_insert_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1493 case GDB_BREAKPOINT_SW
:
1494 case GDB_BREAKPOINT_HW
:
1495 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1496 err
= cpu_breakpoint_insert(env
, addr
, BP_GDB
, NULL
);
1501 #ifndef CONFIG_USER_ONLY
1502 case GDB_WATCHPOINT_WRITE
:
1503 case GDB_WATCHPOINT_READ
:
1504 case GDB_WATCHPOINT_ACCESS
:
1505 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1506 err
= cpu_watchpoint_insert(env
, addr
, len
, xlat_gdb_type
[type
],
1518 static int gdb_breakpoint_remove(target_ulong addr
, target_ulong len
, int type
)
1524 return kvm_remove_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1527 case GDB_BREAKPOINT_SW
:
1528 case GDB_BREAKPOINT_HW
:
1529 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1530 err
= cpu_breakpoint_remove(env
, addr
, BP_GDB
);
1535 #ifndef CONFIG_USER_ONLY
1536 case GDB_WATCHPOINT_WRITE
:
1537 case GDB_WATCHPOINT_READ
:
1538 case GDB_WATCHPOINT_ACCESS
:
1539 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1540 err
= cpu_watchpoint_remove(env
, addr
, len
, xlat_gdb_type
[type
]);
1551 static void gdb_breakpoint_remove_all(void)
1555 if (kvm_enabled()) {
1556 kvm_remove_all_breakpoints(gdbserver_state
->c_cpu
);
1560 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1561 cpu_breakpoint_remove_all(env
, BP_GDB
);
1562 #ifndef CONFIG_USER_ONLY
1563 cpu_watchpoint_remove_all(env
, BP_GDB
);
1568 static void gdb_set_cpu_pc(GDBState
*s
, target_ulong pc
)
1570 #if defined(TARGET_I386)
1572 cpu_synchronize_state(s
->c_cpu
, 1);
1573 #elif defined (TARGET_PPC)
1575 #elif defined (TARGET_SPARC)
1577 s
->c_cpu
->npc
= pc
+ 4;
1578 #elif defined (TARGET_ARM)
1579 s
->c_cpu
->regs
[15] = pc
;
1580 #elif defined (TARGET_SH4)
1582 #elif defined (TARGET_MIPS)
1583 s
->c_cpu
->active_tc
.PC
= pc
;
1584 #elif defined (TARGET_MICROBLAZE)
1585 s
->c_cpu
->sregs
[SR_PC
] = pc
;
1586 #elif defined (TARGET_CRIS)
1588 #elif defined (TARGET_ALPHA)
1593 static inline int gdb_id(CPUState
*env
)
1595 #if defined(CONFIG_USER_ONLY) && defined(CONFIG_USE_NPTL)
1596 return env
->host_tid
;
1598 return env
->cpu_index
+ 1;
1602 static CPUState
*find_cpu(uint32_t thread_id
)
1606 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1607 if (gdb_id(env
) == thread_id
) {
1615 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
1620 int ch
, reg_size
, type
, res
;
1621 char buf
[MAX_PACKET_LENGTH
];
1622 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1624 target_ulong addr
, len
;
1627 printf("command='%s'\n", line_buf
);
1633 /* TODO: Make this return the correct value for user-mode. */
1634 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", GDB_SIGNAL_TRAP
,
1637 /* Remove all the breakpoints when this query is issued,
1638 * because gdb is doing and initial connect and the state
1639 * should be cleaned up.
1641 gdb_breakpoint_remove_all();
1645 addr
= strtoull(p
, (char **)&p
, 16);
1646 gdb_set_cpu_pc(s
, addr
);
1652 s
->signal
= gdb_signal_to_target (strtoul(p
, (char **)&p
, 16));
1653 if (s
->signal
== -1)
1658 if (strncmp(p
, "Cont", 4) == 0) {
1659 int res_signal
, res_thread
;
1663 put_packet(s
, "vCont;c;C;s;S");
1678 if (action
== 'C' || action
== 'S') {
1679 signal
= strtoul(p
, (char **)&p
, 16);
1680 } else if (action
!= 'c' && action
!= 's') {
1686 thread
= strtoull(p
+1, (char **)&p
, 16);
1688 action
= tolower(action
);
1689 if (res
== 0 || (res
== 'c' && action
== 's')) {
1691 res_signal
= signal
;
1692 res_thread
= thread
;
1696 if (res_thread
!= -1 && res_thread
!= 0) {
1697 env
= find_cpu(res_thread
);
1699 put_packet(s
, "E22");
1705 cpu_single_step(s
->c_cpu
, sstep_flags
);
1707 s
->signal
= res_signal
;
1713 goto unknown_command
;
1716 /* Kill the target */
1717 fprintf(stderr
, "\nQEMU: Terminated via GDBstub\n");
1721 gdb_breakpoint_remove_all();
1723 put_packet(s
, "OK");
1727 addr
= strtoull(p
, (char **)&p
, 16);
1728 gdb_set_cpu_pc(s
, addr
);
1730 cpu_single_step(s
->c_cpu
, sstep_flags
);
1738 ret
= strtoull(p
, (char **)&p
, 16);
1741 err
= strtoull(p
, (char **)&p
, 16);
1748 if (gdb_current_syscall_cb
)
1749 gdb_current_syscall_cb(s
->c_cpu
, ret
, err
);
1751 put_packet(s
, "T02");
1758 cpu_synchronize_state(s
->g_cpu
, 0);
1760 for (addr
= 0; addr
< num_g_regs
; addr
++) {
1761 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
+ len
, addr
);
1764 memtohex(buf
, mem_buf
, len
);
1768 registers
= mem_buf
;
1769 len
= strlen(p
) / 2;
1770 hextomem((uint8_t *)registers
, p
, len
);
1771 for (addr
= 0; addr
< num_g_regs
&& len
> 0; addr
++) {
1772 reg_size
= gdb_write_register(s
->g_cpu
, registers
, addr
);
1774 registers
+= reg_size
;
1776 cpu_synchronize_state(s
->g_cpu
, 1);
1777 put_packet(s
, "OK");
1780 addr
= strtoull(p
, (char **)&p
, 16);
1783 len
= strtoull(p
, NULL
, 16);
1784 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 0) != 0) {
1785 put_packet (s
, "E14");
1787 memtohex(buf
, mem_buf
, len
);
1792 addr
= strtoull(p
, (char **)&p
, 16);
1795 len
= strtoull(p
, (char **)&p
, 16);
1798 hextomem(mem_buf
, p
, len
);
1799 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 1) != 0)
1800 put_packet(s
, "E14");
1802 put_packet(s
, "OK");
1805 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1806 This works, but can be very slow. Anything new enough to
1807 understand XML also knows how to use this properly. */
1809 goto unknown_command
;
1810 addr
= strtoull(p
, (char **)&p
, 16);
1811 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
, addr
);
1813 memtohex(buf
, mem_buf
, reg_size
);
1816 put_packet(s
, "E14");
1821 goto unknown_command
;
1822 addr
= strtoull(p
, (char **)&p
, 16);
1825 reg_size
= strlen(p
) / 2;
1826 hextomem(mem_buf
, p
, reg_size
);
1827 gdb_write_register(s
->g_cpu
, mem_buf
, addr
);
1828 put_packet(s
, "OK");
1832 type
= strtoul(p
, (char **)&p
, 16);
1835 addr
= strtoull(p
, (char **)&p
, 16);
1838 len
= strtoull(p
, (char **)&p
, 16);
1840 res
= gdb_breakpoint_insert(addr
, len
, type
);
1842 res
= gdb_breakpoint_remove(addr
, len
, type
);
1844 put_packet(s
, "OK");
1845 else if (res
== -ENOSYS
)
1848 put_packet(s
, "E22");
1852 thread
= strtoull(p
, (char **)&p
, 16);
1853 if (thread
== -1 || thread
== 0) {
1854 put_packet(s
, "OK");
1857 env
= find_cpu(thread
);
1859 put_packet(s
, "E22");
1865 put_packet(s
, "OK");
1869 put_packet(s
, "OK");
1872 put_packet(s
, "E22");
1877 thread
= strtoull(p
, (char **)&p
, 16);
1878 env
= find_cpu(thread
);
1881 put_packet(s
, "OK");
1883 put_packet(s
, "E22");
1888 /* parse any 'q' packets here */
1889 if (!strcmp(p
,"qemu.sstepbits")) {
1890 /* Query Breakpoint bit definitions */
1891 snprintf(buf
, sizeof(buf
), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1897 } else if (strncmp(p
,"qemu.sstep",10) == 0) {
1898 /* Display or change the sstep_flags */
1901 /* Display current setting */
1902 snprintf(buf
, sizeof(buf
), "0x%x", sstep_flags
);
1907 type
= strtoul(p
, (char **)&p
, 16);
1909 put_packet(s
, "OK");
1911 } else if (strcmp(p
,"C") == 0) {
1912 /* "Current thread" remains vague in the spec, so always return
1913 * the first CPU (gdb returns the first thread). */
1914 put_packet(s
, "QC1");
1916 } else if (strcmp(p
,"fThreadInfo") == 0) {
1917 s
->query_cpu
= first_cpu
;
1918 goto report_cpuinfo
;
1919 } else if (strcmp(p
,"sThreadInfo") == 0) {
1922 snprintf(buf
, sizeof(buf
), "m%x", gdb_id(s
->query_cpu
));
1924 s
->query_cpu
= s
->query_cpu
->next_cpu
;
1928 } else if (strncmp(p
,"ThreadExtraInfo,", 16) == 0) {
1929 thread
= strtoull(p
+16, (char **)&p
, 16);
1930 env
= find_cpu(thread
);
1932 cpu_synchronize_state(env
, 0);
1933 len
= snprintf((char *)mem_buf
, sizeof(mem_buf
),
1934 "CPU#%d [%s]", env
->cpu_index
,
1935 env
->halted
? "halted " : "running");
1936 memtohex(buf
, mem_buf
, len
);
1941 #ifdef CONFIG_USER_ONLY
1942 else if (strncmp(p
, "Offsets", 7) == 0) {
1943 TaskState
*ts
= s
->c_cpu
->opaque
;
1945 snprintf(buf
, sizeof(buf
),
1946 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1947 ";Bss=" TARGET_ABI_FMT_lx
,
1948 ts
->info
->code_offset
,
1949 ts
->info
->data_offset
,
1950 ts
->info
->data_offset
);
1954 #else /* !CONFIG_USER_ONLY */
1955 else if (strncmp(p
, "Rcmd,", 5) == 0) {
1956 int len
= strlen(p
+ 5);
1958 if ((len
% 2) != 0) {
1959 put_packet(s
, "E01");
1962 hextomem(mem_buf
, p
+ 5, len
);
1965 qemu_chr_read(s
->mon_chr
, mem_buf
, len
);
1966 put_packet(s
, "OK");
1969 #endif /* !CONFIG_USER_ONLY */
1970 if (strncmp(p
, "Supported", 9) == 0) {
1971 snprintf(buf
, sizeof(buf
), "PacketSize=%x", MAX_PACKET_LENGTH
);
1973 pstrcat(buf
, sizeof(buf
), ";qXfer:features:read+");
1979 if (strncmp(p
, "Xfer:features:read:", 19) == 0) {
1981 target_ulong total_len
;
1985 xml
= get_feature_xml(p
, &p
);
1987 snprintf(buf
, sizeof(buf
), "E00");
1994 addr
= strtoul(p
, (char **)&p
, 16);
1997 len
= strtoul(p
, (char **)&p
, 16);
1999 total_len
= strlen(xml
);
2000 if (addr
> total_len
) {
2001 snprintf(buf
, sizeof(buf
), "E00");
2005 if (len
> (MAX_PACKET_LENGTH
- 5) / 2)
2006 len
= (MAX_PACKET_LENGTH
- 5) / 2;
2007 if (len
< total_len
- addr
) {
2009 len
= memtox(buf
+ 1, xml
+ addr
, len
);
2012 len
= memtox(buf
+ 1, xml
+ addr
, total_len
- addr
);
2014 put_packet_binary(s
, buf
, len
+ 1);
2018 /* Unrecognised 'q' command. */
2019 goto unknown_command
;
2023 /* put empty packet */
2031 void gdb_set_stop_cpu(CPUState
*env
)
2033 gdbserver_state
->c_cpu
= env
;
2034 gdbserver_state
->g_cpu
= env
;
2037 #ifndef CONFIG_USER_ONLY
2038 static void gdb_vm_state_change(void *opaque
, int running
, int reason
)
2040 GDBState
*s
= gdbserver_state
;
2041 CPUState
*env
= s
->c_cpu
;
2046 if (running
|| (reason
!= EXCP_DEBUG
&& reason
!= EXCP_INTERRUPT
) ||
2047 s
->state
== RS_INACTIVE
|| s
->state
== RS_SYSCALL
)
2050 /* disable single step if it was enable */
2051 cpu_single_step(env
, 0);
2053 if (reason
== EXCP_DEBUG
) {
2054 if (env
->watchpoint_hit
) {
2055 switch (env
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
2066 snprintf(buf
, sizeof(buf
),
2067 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx
";",
2068 GDB_SIGNAL_TRAP
, gdb_id(env
), type
,
2069 env
->watchpoint_hit
->vaddr
);
2071 env
->watchpoint_hit
= NULL
;
2075 ret
= GDB_SIGNAL_TRAP
;
2077 ret
= GDB_SIGNAL_INT
;
2079 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", ret
, gdb_id(env
));
2084 /* Send a gdb syscall request.
2085 This accepts limited printf-style format specifiers, specifically:
2086 %x - target_ulong argument printed in hex.
2087 %lx - 64-bit argument printed in hex.
2088 %s - string pointer (target_ulong) and length (int) pair. */
2089 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
2098 s
= gdbserver_state
;
2101 gdb_current_syscall_cb
= cb
;
2102 s
->state
= RS_SYSCALL
;
2103 #ifndef CONFIG_USER_ONLY
2104 vm_stop(EXCP_DEBUG
);
2115 addr
= va_arg(va
, target_ulong
);
2116 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
, addr
);
2119 if (*(fmt
++) != 'x')
2121 i64
= va_arg(va
, uint64_t);
2122 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, "%" PRIx64
, i64
);
2125 addr
= va_arg(va
, target_ulong
);
2126 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
"/%x",
2127 addr
, va_arg(va
, int));
2131 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
2142 #ifdef CONFIG_USER_ONLY
2143 gdb_handlesig(s
->c_cpu
, 0);
2149 static void gdb_read_byte(GDBState
*s
, int ch
)
2154 #ifndef CONFIG_USER_ONLY
2155 if (s
->last_packet_len
) {
2156 /* Waiting for a response to the last packet. If we see the start
2157 of a new command then abandon the previous response. */
2160 printf("Got NACK, retransmitting\n");
2162 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
2166 printf("Got ACK\n");
2168 printf("Got '%c' when expecting ACK/NACK\n", ch
);
2170 if (ch
== '+' || ch
== '$')
2171 s
->last_packet_len
= 0;
2176 /* when the CPU is running, we cannot do anything except stop
2177 it when receiving a char */
2178 vm_stop(EXCP_INTERRUPT
);
2185 s
->line_buf_index
= 0;
2186 s
->state
= RS_GETLINE
;
2191 s
->state
= RS_CHKSUM1
;
2192 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2195 s
->line_buf
[s
->line_buf_index
++] = ch
;
2199 s
->line_buf
[s
->line_buf_index
] = '\0';
2200 s
->line_csum
= fromhex(ch
) << 4;
2201 s
->state
= RS_CHKSUM2
;
2204 s
->line_csum
|= fromhex(ch
);
2206 for(i
= 0; i
< s
->line_buf_index
; i
++) {
2207 csum
+= s
->line_buf
[i
];
2209 if (s
->line_csum
!= (csum
& 0xff)) {
2211 put_buffer(s
, &reply
, 1);
2215 put_buffer(s
, &reply
, 1);
2216 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
2225 #ifdef CONFIG_USER_ONLY
2231 s
= gdbserver_state
;
2233 if (gdbserver_fd
< 0 || s
->fd
< 0)
2240 gdb_handlesig (CPUState
*env
, int sig
)
2246 s
= gdbserver_state
;
2247 if (gdbserver_fd
< 0 || s
->fd
< 0)
2250 /* disable single step if it was enabled */
2251 cpu_single_step(env
, 0);
2256 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb (sig
));
2259 /* put_packet() might have detected that the peer terminated the
2266 s
->running_state
= 0;
2267 while (s
->running_state
== 0) {
2268 n
= read (s
->fd
, buf
, 256);
2273 for (i
= 0; i
< n
; i
++)
2274 gdb_read_byte (s
, buf
[i
]);
2276 else if (n
== 0 || errno
!= EAGAIN
)
2278 /* XXX: Connection closed. Should probably wait for annother
2279 connection before continuing. */
2288 /* Tell the remote gdb that the process has exited. */
2289 void gdb_exit(CPUState
*env
, int code
)
2294 s
= gdbserver_state
;
2295 if (gdbserver_fd
< 0 || s
->fd
< 0)
2298 snprintf(buf
, sizeof(buf
), "W%02x", code
);
2302 /* Tell the remote gdb that the process has exited due to SIG. */
2303 void gdb_signalled(CPUState
*env
, int sig
)
2308 s
= gdbserver_state
;
2309 if (gdbserver_fd
< 0 || s
->fd
< 0)
2312 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb (sig
));
2316 static void gdb_accept(void)
2319 struct sockaddr_in sockaddr
;
2324 len
= sizeof(sockaddr
);
2325 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
2326 if (fd
< 0 && errno
!= EINTR
) {
2329 } else if (fd
>= 0) {
2334 /* set short latency */
2336 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2338 s
= qemu_mallocz(sizeof(GDBState
));
2339 s
->c_cpu
= first_cpu
;
2340 s
->g_cpu
= first_cpu
;
2344 gdbserver_state
= s
;
2346 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2349 static int gdbserver_open(int port
)
2351 struct sockaddr_in sockaddr
;
2354 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2360 /* allow fast reuse */
2362 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
2364 sockaddr
.sin_family
= AF_INET
;
2365 sockaddr
.sin_port
= htons(port
);
2366 sockaddr
.sin_addr
.s_addr
= 0;
2367 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
2372 ret
= listen(fd
, 0);
2380 int gdbserver_start(int port
)
2382 gdbserver_fd
= gdbserver_open(port
);
2383 if (gdbserver_fd
< 0)
2385 /* accept connections */
2390 /* Disable gdb stub for child processes. */
2391 void gdbserver_fork(CPUState
*env
)
2393 GDBState
*s
= gdbserver_state
;
2394 if (gdbserver_fd
< 0 || s
->fd
< 0)
2398 cpu_breakpoint_remove_all(env
, BP_GDB
);
2399 cpu_watchpoint_remove_all(env
, BP_GDB
);
2402 static int gdb_chr_can_receive(void *opaque
)
2404 /* We can handle an arbitrarily large amount of data.
2405 Pick the maximum packet size, which is as good as anything. */
2406 return MAX_PACKET_LENGTH
;
2409 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
2413 for (i
= 0; i
< size
; i
++) {
2414 gdb_read_byte(gdbserver_state
, buf
[i
]);
2418 static void gdb_chr_event(void *opaque
, int event
)
2421 case CHR_EVENT_RESET
:
2422 vm_stop(EXCP_INTERRUPT
);
2430 static void gdb_monitor_output(GDBState
*s
, const char *msg
, int len
)
2432 char buf
[MAX_PACKET_LENGTH
];
2435 if (len
> (MAX_PACKET_LENGTH
/2) - 1)
2436 len
= (MAX_PACKET_LENGTH
/2) - 1;
2437 memtohex(buf
+ 1, (uint8_t *)msg
, len
);
2441 static int gdb_monitor_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2443 const char *p
= (const char *)buf
;
2446 max_sz
= (sizeof(gdbserver_state
->last_packet
) - 2) / 2;
2448 if (len
<= max_sz
) {
2449 gdb_monitor_output(gdbserver_state
, p
, len
);
2452 gdb_monitor_output(gdbserver_state
, p
, max_sz
);
2460 static void gdb_sigterm_handler(int signal
)
2463 vm_stop(EXCP_INTERRUPT
);
2467 int gdbserver_start(const char *device
)
2470 char gdbstub_device_name
[128];
2471 CharDriverState
*chr
= NULL
;
2472 CharDriverState
*mon_chr
;
2476 if (strcmp(device
, "none") != 0) {
2477 if (strstart(device
, "tcp:", NULL
)) {
2478 /* enforce required TCP attributes */
2479 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
2480 "%s,nowait,nodelay,server", device
);
2481 device
= gdbstub_device_name
;
2484 else if (strcmp(device
, "stdio") == 0) {
2485 struct sigaction act
;
2487 memset(&act
, 0, sizeof(act
));
2488 act
.sa_handler
= gdb_sigterm_handler
;
2489 sigaction(SIGINT
, &act
, NULL
);
2492 chr
= qemu_chr_open("gdb", device
, NULL
);
2496 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
2497 gdb_chr_event
, NULL
);
2500 s
= gdbserver_state
;
2502 s
= qemu_mallocz(sizeof(GDBState
));
2503 gdbserver_state
= s
;
2505 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
2507 /* Initialize a monitor terminal for gdb */
2508 mon_chr
= qemu_mallocz(sizeof(*mon_chr
));
2509 mon_chr
->chr_write
= gdb_monitor_write
;
2510 monitor_init(mon_chr
, 0);
2513 qemu_chr_close(s
->chr
);
2514 mon_chr
= s
->mon_chr
;
2515 memset(s
, 0, sizeof(GDBState
));
2517 s
->c_cpu
= first_cpu
;
2518 s
->g_cpu
= first_cpu
;
2520 s
->state
= chr
? RS_IDLE
: RS_INACTIVE
;
2521 s
->mon_chr
= mon_chr
;