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"
39 #define MAX_PACKET_LENGTH 4096
41 #include "qemu_socket.h"
49 GDB_SIGNAL_UNKNOWN
= 143
52 #ifdef CONFIG_USER_ONLY
54 /* Map target signal numbers to GDB protocol signal numbers and vice
55 * versa. For user emulation's currently supported systems, we can
56 * assume most signals are defined.
59 static int gdb_signal_table
[] = {
219 /* In system mode we only need SIGINT and SIGTRAP; other signals
220 are not yet supported. */
227 static int gdb_signal_table
[] = {
237 #ifdef CONFIG_USER_ONLY
238 static int target_signal_to_gdb (int sig
)
241 for (i
= 0; i
< ARRAY_SIZE (gdb_signal_table
); i
++)
242 if (gdb_signal_table
[i
] == sig
)
244 return GDB_SIGNAL_UNKNOWN
;
248 static int gdb_signal_to_target (int sig
)
250 if (sig
< ARRAY_SIZE (gdb_signal_table
))
251 return gdb_signal_table
[sig
];
258 typedef struct GDBRegisterState
{
264 struct GDBRegisterState
*next
;
275 typedef struct GDBState
{
276 CPUState
*c_cpu
; /* current CPU for step/continue ops */
277 CPUState
*g_cpu
; /* current CPU for other ops */
278 CPUState
*query_cpu
; /* for q{f|s}ThreadInfo */
279 enum RSState state
; /* parsing state */
280 char line_buf
[MAX_PACKET_LENGTH
];
283 uint8_t last_packet
[MAX_PACKET_LENGTH
+ 4];
286 #ifdef CONFIG_USER_ONLY
290 CharDriverState
*chr
;
291 CharDriverState
*mon_chr
;
295 /* By default use no IRQs and no timers while single stepping so as to
296 * make single stepping like an ICE HW step.
298 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
300 static GDBState
*gdbserver_state
;
302 /* This is an ugly hack to cope with both new and old gdb.
303 If gdb sends qXfer:features:read then assume we're talking to a newish
304 gdb that understands target descriptions. */
305 static int gdb_has_xml
;
307 #ifdef CONFIG_USER_ONLY
308 /* XXX: This is not thread safe. Do we care? */
309 static int gdbserver_fd
= -1;
311 static int get_char(GDBState
*s
)
317 ret
= recv(s
->fd
, &ch
, 1, 0);
319 if (errno
== ECONNRESET
)
321 if (errno
!= EINTR
&& errno
!= EAGAIN
)
323 } else if (ret
== 0) {
335 static gdb_syscall_complete_cb gdb_current_syscall_cb
;
343 /* If gdb is connected when the first semihosting syscall occurs then use
344 remote gdb syscalls. Otherwise use native file IO. */
345 int use_gdb_syscalls(void)
347 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
348 gdb_syscall_mode
= (gdbserver_state
? GDB_SYS_ENABLED
351 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
354 /* Resume execution. */
355 static inline void gdb_continue(GDBState
*s
)
357 #ifdef CONFIG_USER_ONLY
358 s
->running_state
= 1;
364 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
366 #ifdef CONFIG_USER_ONLY
370 ret
= send(s
->fd
, buf
, len
, 0);
372 if (errno
!= EINTR
&& errno
!= EAGAIN
)
380 qemu_chr_write(s
->chr
, buf
, len
);
384 static inline int fromhex(int v
)
386 if (v
>= '0' && v
<= '9')
388 else if (v
>= 'A' && v
<= 'F')
390 else if (v
>= 'a' && v
<= 'f')
396 static inline int tohex(int v
)
404 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
409 for(i
= 0; i
< len
; i
++) {
411 *q
++ = tohex(c
>> 4);
412 *q
++ = tohex(c
& 0xf);
417 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
421 for(i
= 0; i
< len
; i
++) {
422 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
427 /* return -1 if error, 0 if OK */
428 static int put_packet_binary(GDBState
*s
, const char *buf
, int len
)
439 for(i
= 0; i
< len
; i
++) {
443 *(p
++) = tohex((csum
>> 4) & 0xf);
444 *(p
++) = tohex((csum
) & 0xf);
446 s
->last_packet_len
= p
- s
->last_packet
;
447 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
449 #ifdef CONFIG_USER_ONLY
462 /* return -1 if error, 0 if OK */
463 static int put_packet(GDBState
*s
, const char *buf
)
466 printf("reply='%s'\n", buf
);
469 return put_packet_binary(s
, buf
, strlen(buf
));
472 /* The GDB remote protocol transfers values in target byte order. This means
473 we can use the raw memory access routines to access the value buffer.
474 Conveniently, these also handle the case where the buffer is mis-aligned.
476 #define GET_REG8(val) do { \
477 stb_p(mem_buf, val); \
480 #define GET_REG16(val) do { \
481 stw_p(mem_buf, val); \
484 #define GET_REG32(val) do { \
485 stl_p(mem_buf, val); \
488 #define GET_REG64(val) do { \
489 stq_p(mem_buf, val); \
493 #if TARGET_LONG_BITS == 64
494 #define GET_REGL(val) GET_REG64(val)
495 #define ldtul_p(addr) ldq_p(addr)
497 #define GET_REGL(val) GET_REG32(val)
498 #define ldtul_p(addr) ldl_p(addr)
501 #if defined(TARGET_I386)
504 static const int gpr_map
[16] = {
505 R_EAX
, R_EBX
, R_ECX
, R_EDX
, R_ESI
, R_EDI
, R_EBP
, R_ESP
,
506 8, 9, 10, 11, 12, 13, 14, 15
509 static const int gpr_map
[8] = {0, 1, 2, 3, 4, 5, 6, 7};
512 #define NUM_CORE_REGS (CPU_NB_REGS * 2 + 25)
514 #define IDX_IP_REG CPU_NB_REGS
515 #define IDX_FLAGS_REG (IDX_IP_REG + 1)
516 #define IDX_SEG_REGS (IDX_FLAGS_REG + 1)
517 #define IDX_FP_REGS (IDX_SEG_REGS + 6)
518 #define IDX_XMM_REGS (IDX_FP_REGS + 16)
519 #define IDX_MXCSR_REG (IDX_XMM_REGS + CPU_NB_REGS)
521 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
523 if (n
< CPU_NB_REGS
) {
524 GET_REGL(env
->regs
[gpr_map
[n
]]);
525 } else if (n
>= IDX_FP_REGS
&& n
< IDX_FP_REGS
+ 8) {
526 #ifdef USE_X86LDOUBLE
527 /* FIXME: byteswap float values - after fixing fpregs layout. */
528 memcpy(mem_buf
, &env
->fpregs
[n
- IDX_FP_REGS
], 10);
530 memset(mem_buf
, 0, 10);
533 } else if (n
>= IDX_XMM_REGS
&& n
< IDX_XMM_REGS
+ CPU_NB_REGS
) {
535 stq_p(mem_buf
, env
->xmm_regs
[n
].XMM_Q(0));
536 stq_p(mem_buf
+ 8, env
->xmm_regs
[n
].XMM_Q(1));
540 case IDX_IP_REG
: GET_REGL(env
->eip
);
541 case IDX_FLAGS_REG
: GET_REG32(env
->eflags
);
543 case IDX_SEG_REGS
: GET_REG32(env
->segs
[R_CS
].selector
);
544 case IDX_SEG_REGS
+ 1: GET_REG32(env
->segs
[R_SS
].selector
);
545 case IDX_SEG_REGS
+ 2: GET_REG32(env
->segs
[R_DS
].selector
);
546 case IDX_SEG_REGS
+ 3: GET_REG32(env
->segs
[R_ES
].selector
);
547 case IDX_SEG_REGS
+ 4: GET_REG32(env
->segs
[R_FS
].selector
);
548 case IDX_SEG_REGS
+ 5: GET_REG32(env
->segs
[R_GS
].selector
);
550 case IDX_FP_REGS
+ 8: GET_REG32(env
->fpuc
);
551 case IDX_FP_REGS
+ 9: GET_REG32((env
->fpus
& ~0x3800) |
552 (env
->fpstt
& 0x7) << 11);
553 case IDX_FP_REGS
+ 10: GET_REG32(0); /* ftag */
554 case IDX_FP_REGS
+ 11: GET_REG32(0); /* fiseg */
555 case IDX_FP_REGS
+ 12: GET_REG32(0); /* fioff */
556 case IDX_FP_REGS
+ 13: GET_REG32(0); /* foseg */
557 case IDX_FP_REGS
+ 14: GET_REG32(0); /* fooff */
558 case IDX_FP_REGS
+ 15: GET_REG32(0); /* fop */
560 case IDX_MXCSR_REG
: GET_REG32(env
->mxcsr
);
566 static int cpu_x86_gdb_load_seg(CPUState
*env
, int sreg
, uint8_t *mem_buf
)
568 uint16_t selector
= ldl_p(mem_buf
);
570 if (selector
!= env
->segs
[sreg
].selector
) {
571 #if defined(CONFIG_USER_ONLY)
572 cpu_x86_load_seg(env
, sreg
, selector
);
574 unsigned int limit
, flags
;
577 if (!(env
->cr
[0] & CR0_PE_MASK
) || (env
->eflags
& VM_MASK
)) {
578 base
= selector
<< 4;
582 if (!cpu_x86_get_descr_debug(env
, selector
, &base
, &limit
, &flags
))
585 cpu_x86_load_seg_cache(env
, sreg
, selector
, base
, limit
, flags
);
591 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
595 if (n
< CPU_NB_REGS
) {
596 env
->regs
[gpr_map
[n
]] = ldtul_p(mem_buf
);
597 return sizeof(target_ulong
);
598 } else if (n
>= IDX_FP_REGS
&& n
< IDX_FP_REGS
+ 8) {
599 #ifdef USE_X86LDOUBLE
600 /* FIXME: byteswap float values - after fixing fpregs layout. */
601 memcpy(&env
->fpregs
[n
- IDX_FP_REGS
], mem_buf
, 10);
604 } else if (n
>= IDX_XMM_REGS
&& n
< IDX_XMM_REGS
+ CPU_NB_REGS
) {
606 env
->xmm_regs
[n
].XMM_Q(0) = ldq_p(mem_buf
);
607 env
->xmm_regs
[n
].XMM_Q(1) = ldq_p(mem_buf
+ 8);
612 env
->eip
= ldtul_p(mem_buf
);
613 return sizeof(target_ulong
);
615 env
->eflags
= ldl_p(mem_buf
);
618 case IDX_SEG_REGS
: return cpu_x86_gdb_load_seg(env
, R_CS
, mem_buf
);
619 case IDX_SEG_REGS
+ 1: return cpu_x86_gdb_load_seg(env
, R_SS
, mem_buf
);
620 case IDX_SEG_REGS
+ 2: return cpu_x86_gdb_load_seg(env
, R_DS
, mem_buf
);
621 case IDX_SEG_REGS
+ 3: return cpu_x86_gdb_load_seg(env
, R_ES
, mem_buf
);
622 case IDX_SEG_REGS
+ 4: return cpu_x86_gdb_load_seg(env
, R_FS
, mem_buf
);
623 case IDX_SEG_REGS
+ 5: return cpu_x86_gdb_load_seg(env
, R_GS
, mem_buf
);
625 case IDX_FP_REGS
+ 8:
626 env
->fpuc
= ldl_p(mem_buf
);
628 case IDX_FP_REGS
+ 9:
629 tmp
= ldl_p(mem_buf
);
630 env
->fpstt
= (tmp
>> 11) & 7;
631 env
->fpus
= tmp
& ~0x3800;
633 case IDX_FP_REGS
+ 10: /* ftag */ return 4;
634 case IDX_FP_REGS
+ 11: /* fiseg */ return 4;
635 case IDX_FP_REGS
+ 12: /* fioff */ return 4;
636 case IDX_FP_REGS
+ 13: /* foseg */ return 4;
637 case IDX_FP_REGS
+ 14: /* fooff */ return 4;
638 case IDX_FP_REGS
+ 15: /* fop */ return 4;
641 env
->mxcsr
= ldl_p(mem_buf
);
645 /* Unrecognised register. */
649 #elif defined (TARGET_PPC)
651 /* Old gdb always expects FP registers. Newer (xml-aware) gdb only
652 expects whatever the target description contains. Due to a
653 historical mishap the FP registers appear in between core integer
654 regs and PC, MSR, CR, and so forth. We hack round this by giving the
655 FP regs zero size when talking to a newer gdb. */
656 #define NUM_CORE_REGS 71
657 #if defined (TARGET_PPC64)
658 #define GDB_CORE_XML "power64-core.xml"
660 #define GDB_CORE_XML "power-core.xml"
663 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
667 GET_REGL(env
->gpr
[n
]);
672 stfq_p(mem_buf
, env
->fpr
[n
-32]);
676 case 64: GET_REGL(env
->nip
);
677 case 65: GET_REGL(env
->msr
);
682 for (i
= 0; i
< 8; i
++)
683 cr
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
686 case 67: GET_REGL(env
->lr
);
687 case 68: GET_REGL(env
->ctr
);
688 case 69: GET_REGL(env
->xer
);
693 GET_REG32(0); /* fpscr */
700 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
704 env
->gpr
[n
] = ldtul_p(mem_buf
);
705 return sizeof(target_ulong
);
710 env
->fpr
[n
-32] = ldfq_p(mem_buf
);
715 env
->nip
= ldtul_p(mem_buf
);
716 return sizeof(target_ulong
);
718 ppc_store_msr(env
, ldtul_p(mem_buf
));
719 return sizeof(target_ulong
);
722 uint32_t cr
= ldl_p(mem_buf
);
724 for (i
= 0; i
< 8; i
++)
725 env
->crf
[i
] = (cr
>> (32 - ((i
+ 1) * 4))) & 0xF;
729 env
->lr
= ldtul_p(mem_buf
);
730 return sizeof(target_ulong
);
732 env
->ctr
= ldtul_p(mem_buf
);
733 return sizeof(target_ulong
);
735 env
->xer
= ldtul_p(mem_buf
);
736 return sizeof(target_ulong
);
747 #elif defined (TARGET_SPARC)
749 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
750 #define NUM_CORE_REGS 86
752 #define NUM_CORE_REGS 72
756 #define GET_REGA(val) GET_REG32(val)
758 #define GET_REGA(val) GET_REGL(val)
761 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
765 GET_REGA(env
->gregs
[n
]);
768 /* register window */
769 GET_REGA(env
->regwptr
[n
- 8]);
771 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
774 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
776 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
778 case 64: GET_REGA(env
->y
);
779 case 65: GET_REGA(GET_PSR(env
));
780 case 66: GET_REGA(env
->wim
);
781 case 67: GET_REGA(env
->tbr
);
782 case 68: GET_REGA(env
->pc
);
783 case 69: GET_REGA(env
->npc
);
784 case 70: GET_REGA(env
->fsr
);
785 case 71: GET_REGA(0); /* csr */
786 default: GET_REGA(0);
791 GET_REG32(*((uint32_t *)&env
->fpr
[n
- 32]));
794 /* f32-f62 (double width, even numbers only) */
797 val
= (uint64_t)*((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) << 32;
798 val
|= *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]);
802 case 80: GET_REGL(env
->pc
);
803 case 81: GET_REGL(env
->npc
);
804 case 82: GET_REGL(((uint64_t)GET_CCR(env
) << 32) |
805 ((env
->asi
& 0xff) << 24) |
806 ((env
->pstate
& 0xfff) << 8) |
808 case 83: GET_REGL(env
->fsr
);
809 case 84: GET_REGL(env
->fprs
);
810 case 85: GET_REGL(env
->y
);
816 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
818 #if defined(TARGET_ABI32)
821 tmp
= ldl_p(mem_buf
);
825 tmp
= ldtul_p(mem_buf
);
832 /* register window */
833 env
->regwptr
[n
- 8] = tmp
;
835 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
838 *((uint32_t *)&env
->fpr
[n
- 32]) = tmp
;
840 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
842 case 64: env
->y
= tmp
; break;
843 case 65: PUT_PSR(env
, tmp
); break;
844 case 66: env
->wim
= tmp
; break;
845 case 67: env
->tbr
= tmp
; break;
846 case 68: env
->pc
= tmp
; break;
847 case 69: env
->npc
= tmp
; break;
848 case 70: env
->fsr
= tmp
; break;
856 env
->fpr
[n
] = ldfl_p(mem_buf
);
859 /* f32-f62 (double width, even numbers only) */
860 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 32]) = tmp
>> 32;
861 *((uint32_t *)&env
->fpr
[(n
- 64) * 2 + 33]) = tmp
;
864 case 80: env
->pc
= tmp
; break;
865 case 81: env
->npc
= tmp
; break;
867 PUT_CCR(env
, tmp
>> 32);
868 env
->asi
= (tmp
>> 24) & 0xff;
869 env
->pstate
= (tmp
>> 8) & 0xfff;
870 PUT_CWP64(env
, tmp
& 0xff);
872 case 83: env
->fsr
= tmp
; break;
873 case 84: env
->fprs
= tmp
; break;
874 case 85: env
->y
= tmp
; break;
881 #elif defined (TARGET_ARM)
883 /* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect
884 whatever the target description contains. Due to a historical mishap
885 the FPA registers appear in between core integer regs and the CPSR.
886 We hack round this by giving the FPA regs zero size when talking to a
888 #define NUM_CORE_REGS 26
889 #define GDB_CORE_XML "arm-core.xml"
891 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
894 /* Core integer register. */
895 GET_REG32(env
->regs
[n
]);
901 memset(mem_buf
, 0, 12);
906 /* FPA status register. */
912 GET_REG32(cpsr_read(env
));
914 /* Unknown register. */
918 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
922 tmp
= ldl_p(mem_buf
);
924 /* Mask out low bit of PC to workaround gdb bugs. This will probably
925 cause problems if we ever implement the Jazelle DBX extensions. */
930 /* Core integer register. */
934 if (n
< 24) { /* 16-23 */
935 /* FPA registers (ignored). */
942 /* FPA status register (ignored). */
948 cpsr_write (env
, tmp
, 0xffffffff);
951 /* Unknown register. */
955 #elif defined (TARGET_M68K)
957 #define NUM_CORE_REGS 18
959 #define GDB_CORE_XML "cf-core.xml"
961 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
965 GET_REG32(env
->dregs
[n
]);
968 GET_REG32(env
->aregs
[n
- 8]);
971 case 16: GET_REG32(env
->sr
);
972 case 17: GET_REG32(env
->pc
);
975 /* FP registers not included here because they vary between
976 ColdFire and m68k. Use XML bits for these. */
980 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
984 tmp
= ldl_p(mem_buf
);
991 env
->aregs
[n
- 8] = tmp
;
994 case 16: env
->sr
= tmp
; break;
995 case 17: env
->pc
= tmp
; break;
1001 #elif defined (TARGET_MIPS)
1003 #define NUM_CORE_REGS 73
1005 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1008 GET_REGL(env
->active_tc
.gpr
[n
]);
1010 if (env
->CP0_Config1
& (1 << CP0C1_FP
)) {
1011 if (n
>= 38 && n
< 70) {
1012 if (env
->CP0_Status
& (1 << CP0St_FR
))
1013 GET_REGL(env
->active_fpu
.fpr
[n
- 38].d
);
1015 GET_REGL(env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
]);
1018 case 70: GET_REGL((int32_t)env
->active_fpu
.fcr31
);
1019 case 71: GET_REGL((int32_t)env
->active_fpu
.fcr0
);
1023 case 32: GET_REGL((int32_t)env
->CP0_Status
);
1024 case 33: GET_REGL(env
->active_tc
.LO
[0]);
1025 case 34: GET_REGL(env
->active_tc
.HI
[0]);
1026 case 35: GET_REGL(env
->CP0_BadVAddr
);
1027 case 36: GET_REGL((int32_t)env
->CP0_Cause
);
1028 case 37: GET_REGL(env
->active_tc
.PC
);
1029 case 72: GET_REGL(0); /* fp */
1030 case 89: GET_REGL((int32_t)env
->CP0_PRid
);
1032 if (n
>= 73 && n
<= 88) {
1033 /* 16 embedded regs. */
1040 /* convert MIPS rounding mode in FCR31 to IEEE library */
1041 static unsigned int ieee_rm
[] =
1043 float_round_nearest_even
,
1044 float_round_to_zero
,
1048 #define RESTORE_ROUNDING_MODE \
1049 set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
1051 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1055 tmp
= ldtul_p(mem_buf
);
1058 env
->active_tc
.gpr
[n
] = tmp
;
1059 return sizeof(target_ulong
);
1061 if (env
->CP0_Config1
& (1 << CP0C1_FP
)
1062 && n
>= 38 && n
< 73) {
1064 if (env
->CP0_Status
& (1 << CP0St_FR
))
1065 env
->active_fpu
.fpr
[n
- 38].d
= tmp
;
1067 env
->active_fpu
.fpr
[n
- 38].w
[FP_ENDIAN_IDX
] = tmp
;
1071 env
->active_fpu
.fcr31
= tmp
& 0xFF83FFFF;
1072 /* set rounding mode */
1073 RESTORE_ROUNDING_MODE
;
1074 #ifndef CONFIG_SOFTFLOAT
1075 /* no floating point exception for native float */
1076 SET_FP_ENABLE(env
->active_fpu
.fcr31
, 0);
1079 case 71: env
->active_fpu
.fcr0
= tmp
; break;
1081 return sizeof(target_ulong
);
1084 case 32: env
->CP0_Status
= tmp
; break;
1085 case 33: env
->active_tc
.LO
[0] = tmp
; break;
1086 case 34: env
->active_tc
.HI
[0] = tmp
; break;
1087 case 35: env
->CP0_BadVAddr
= tmp
; break;
1088 case 36: env
->CP0_Cause
= tmp
; break;
1089 case 37: env
->active_tc
.PC
= tmp
; break;
1090 case 72: /* fp, ignored */ break;
1094 /* Other registers are readonly. Ignore writes. */
1098 return sizeof(target_ulong
);
1100 #elif defined (TARGET_SH4)
1102 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
1103 /* FIXME: We should use XML for this. */
1105 #define NUM_CORE_REGS 59
1107 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1110 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1111 GET_REGL(env
->gregs
[n
+ 16]);
1113 GET_REGL(env
->gregs
[n
]);
1115 } else if (n
< 16) {
1116 GET_REGL(env
->gregs
[n
- 8]);
1117 } else if (n
>= 25 && n
< 41) {
1118 GET_REGL(env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
1119 } else if (n
>= 43 && n
< 51) {
1120 GET_REGL(env
->gregs
[n
- 43]);
1121 } else if (n
>= 51 && n
< 59) {
1122 GET_REGL(env
->gregs
[n
- (51 - 16)]);
1125 case 16: GET_REGL(env
->pc
);
1126 case 17: GET_REGL(env
->pr
);
1127 case 18: GET_REGL(env
->gbr
);
1128 case 19: GET_REGL(env
->vbr
);
1129 case 20: GET_REGL(env
->mach
);
1130 case 21: GET_REGL(env
->macl
);
1131 case 22: GET_REGL(env
->sr
);
1132 case 23: GET_REGL(env
->fpul
);
1133 case 24: GET_REGL(env
->fpscr
);
1134 case 41: GET_REGL(env
->ssr
);
1135 case 42: GET_REGL(env
->spc
);
1141 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1145 tmp
= ldl_p(mem_buf
);
1148 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
1149 env
->gregs
[n
+ 16] = tmp
;
1151 env
->gregs
[n
] = tmp
;
1154 } else if (n
< 16) {
1155 env
->gregs
[n
- 8] = tmp
;
1157 } else if (n
>= 25 && n
< 41) {
1158 env
->fregs
[(n
- 25) + ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)] = tmp
;
1159 } else if (n
>= 43 && n
< 51) {
1160 env
->gregs
[n
- 43] = tmp
;
1162 } else if (n
>= 51 && n
< 59) {
1163 env
->gregs
[n
- (51 - 16)] = tmp
;
1167 case 16: env
->pc
= tmp
;
1168 case 17: env
->pr
= tmp
;
1169 case 18: env
->gbr
= tmp
;
1170 case 19: env
->vbr
= tmp
;
1171 case 20: env
->mach
= tmp
;
1172 case 21: env
->macl
= tmp
;
1173 case 22: env
->sr
= tmp
;
1174 case 23: env
->fpul
= tmp
;
1175 case 24: env
->fpscr
= tmp
;
1176 case 41: env
->ssr
= tmp
;
1177 case 42: env
->spc
= tmp
;
1183 #elif defined (TARGET_MICROBLAZE)
1185 #define NUM_CORE_REGS (32 + 5)
1187 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1190 GET_REG32(env
->regs
[n
]);
1192 GET_REG32(env
->sregs
[n
- 32]);
1197 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1201 if (n
> NUM_CORE_REGS
)
1204 tmp
= ldl_p(mem_buf
);
1209 env
->sregs
[n
- 32] = tmp
;
1213 #elif defined (TARGET_CRIS)
1215 #define NUM_CORE_REGS 49
1217 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1221 srs
= env
->pregs
[PR_SRS
];
1223 GET_REG32(env
->regs
[n
]);
1226 if (n
>= 21 && n
< 32) {
1227 GET_REG32(env
->pregs
[n
- 16]);
1229 if (n
>= 33 && n
< 49) {
1230 GET_REG32(env
->sregs
[srs
][n
- 33]);
1233 case 16: GET_REG8(env
->pregs
[0]);
1234 case 17: GET_REG8(env
->pregs
[1]);
1235 case 18: GET_REG32(env
->pregs
[2]);
1236 case 19: GET_REG8(srs
);
1237 case 20: GET_REG16(env
->pregs
[4]);
1238 case 32: GET_REG32(env
->pc
);
1244 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1251 tmp
= ldl_p(mem_buf
);
1257 if (n
>= 21 && n
< 32) {
1258 env
->pregs
[n
- 16] = tmp
;
1261 /* FIXME: Should support function regs be writable? */
1265 case 18: env
->pregs
[PR_PID
] = tmp
; break;
1268 case 32: env
->pc
= tmp
; break;
1273 #elif defined (TARGET_ALPHA)
1275 #define NUM_CORE_REGS 65
1277 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1280 GET_REGL(env
->ir
[n
]);
1288 val
=*((uint64_t *)&env
->fir
[n
-32]);
1292 GET_REGL(env
->fpcr
);
1304 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1307 tmp
= ldtul_p(mem_buf
);
1313 if (n
> 31 && n
< 63) {
1314 env
->fir
[n
- 32] = ldfl_p(mem_buf
);
1325 #define NUM_CORE_REGS 0
1327 static int cpu_gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1332 static int cpu_gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int n
)
1339 static int num_g_regs
= NUM_CORE_REGS
;
1342 /* Encode data using the encoding for 'x' packets. */
1343 static int memtox(char *buf
, const char *mem
, int len
)
1351 case '#': case '$': case '*': case '}':
1363 static const char *get_feature_xml(const char *p
, const char **newp
)
1365 extern const char *const xml_builtin
[][2];
1369 static char target_xml
[1024];
1372 while (p
[len
] && p
[len
] != ':')
1377 if (strncmp(p
, "target.xml", len
) == 0) {
1378 /* Generate the XML description for this CPU. */
1379 if (!target_xml
[0]) {
1380 GDBRegisterState
*r
;
1382 snprintf(target_xml
, sizeof(target_xml
),
1383 "<?xml version=\"1.0\"?>"
1384 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
1386 "<xi:include href=\"%s\"/>",
1389 for (r
= first_cpu
->gdb_regs
; r
; r
= r
->next
) {
1390 pstrcat(target_xml
, sizeof(target_xml
), "<xi:include href=\"");
1391 pstrcat(target_xml
, sizeof(target_xml
), r
->xml
);
1392 pstrcat(target_xml
, sizeof(target_xml
), "\"/>");
1394 pstrcat(target_xml
, sizeof(target_xml
), "</target>");
1398 for (i
= 0; ; i
++) {
1399 name
= xml_builtin
[i
][0];
1400 if (!name
|| (strncmp(name
, p
, len
) == 0 && strlen(name
) == len
))
1403 return name
? xml_builtin
[i
][1] : NULL
;
1407 static int gdb_read_register(CPUState
*env
, uint8_t *mem_buf
, int reg
)
1409 GDBRegisterState
*r
;
1411 if (reg
< NUM_CORE_REGS
)
1412 return cpu_gdb_read_register(env
, mem_buf
, reg
);
1414 for (r
= env
->gdb_regs
; r
; r
= r
->next
) {
1415 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1416 return r
->get_reg(env
, mem_buf
, reg
- r
->base_reg
);
1422 static int gdb_write_register(CPUState
*env
, uint8_t *mem_buf
, int reg
)
1424 GDBRegisterState
*r
;
1426 if (reg
< NUM_CORE_REGS
)
1427 return cpu_gdb_write_register(env
, mem_buf
, reg
);
1429 for (r
= env
->gdb_regs
; r
; r
= r
->next
) {
1430 if (r
->base_reg
<= reg
&& reg
< r
->base_reg
+ r
->num_regs
) {
1431 return r
->set_reg(env
, mem_buf
, reg
- r
->base_reg
);
1437 /* Register a supplemental set of CPU registers. If g_pos is nonzero it
1438 specifies the first register number and these registers are included in
1439 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
1440 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
1443 void gdb_register_coprocessor(CPUState
* env
,
1444 gdb_reg_cb get_reg
, gdb_reg_cb set_reg
,
1445 int num_regs
, const char *xml
, int g_pos
)
1447 GDBRegisterState
*s
;
1448 GDBRegisterState
**p
;
1449 static int last_reg
= NUM_CORE_REGS
;
1451 s
= (GDBRegisterState
*)qemu_mallocz(sizeof(GDBRegisterState
));
1452 s
->base_reg
= last_reg
;
1453 s
->num_regs
= num_regs
;
1454 s
->get_reg
= get_reg
;
1455 s
->set_reg
= set_reg
;
1459 /* Check for duplicates. */
1460 if (strcmp((*p
)->xml
, xml
) == 0)
1464 /* Add to end of list. */
1465 last_reg
+= num_regs
;
1468 if (g_pos
!= s
->base_reg
) {
1469 fprintf(stderr
, "Error: Bad gdb register numbering for '%s'\n"
1470 "Expected %d got %d\n", xml
, g_pos
, s
->base_reg
);
1472 num_g_regs
= last_reg
;
1477 #ifndef CONFIG_USER_ONLY
1478 static const int xlat_gdb_type
[] = {
1479 [GDB_WATCHPOINT_WRITE
] = BP_GDB
| BP_MEM_WRITE
,
1480 [GDB_WATCHPOINT_READ
] = BP_GDB
| BP_MEM_READ
,
1481 [GDB_WATCHPOINT_ACCESS
] = BP_GDB
| BP_MEM_ACCESS
,
1485 static int gdb_breakpoint_insert(target_ulong addr
, target_ulong len
, int type
)
1491 return kvm_insert_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1494 case GDB_BREAKPOINT_SW
:
1495 case GDB_BREAKPOINT_HW
:
1496 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1497 err
= cpu_breakpoint_insert(env
, addr
, BP_GDB
, NULL
);
1502 #ifndef CONFIG_USER_ONLY
1503 case GDB_WATCHPOINT_WRITE
:
1504 case GDB_WATCHPOINT_READ
:
1505 case GDB_WATCHPOINT_ACCESS
:
1506 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1507 err
= cpu_watchpoint_insert(env
, addr
, len
, xlat_gdb_type
[type
],
1519 static int gdb_breakpoint_remove(target_ulong addr
, target_ulong len
, int type
)
1525 return kvm_remove_breakpoint(gdbserver_state
->c_cpu
, addr
, len
, type
);
1528 case GDB_BREAKPOINT_SW
:
1529 case GDB_BREAKPOINT_HW
:
1530 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1531 err
= cpu_breakpoint_remove(env
, addr
, BP_GDB
);
1536 #ifndef CONFIG_USER_ONLY
1537 case GDB_WATCHPOINT_WRITE
:
1538 case GDB_WATCHPOINT_READ
:
1539 case GDB_WATCHPOINT_ACCESS
:
1540 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1541 err
= cpu_watchpoint_remove(env
, addr
, len
, xlat_gdb_type
[type
]);
1552 static void gdb_breakpoint_remove_all(void)
1556 if (kvm_enabled()) {
1557 kvm_remove_all_breakpoints(gdbserver_state
->c_cpu
);
1561 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1562 cpu_breakpoint_remove_all(env
, BP_GDB
);
1563 #ifndef CONFIG_USER_ONLY
1564 cpu_watchpoint_remove_all(env
, BP_GDB
);
1569 static void gdb_set_cpu_pc(GDBState
*s
, target_ulong pc
)
1571 #if defined(TARGET_I386)
1573 cpu_synchronize_state(s
->c_cpu
, 1);
1574 #elif defined (TARGET_PPC)
1576 #elif defined (TARGET_SPARC)
1578 s
->c_cpu
->npc
= pc
+ 4;
1579 #elif defined (TARGET_ARM)
1580 s
->c_cpu
->regs
[15] = pc
;
1581 #elif defined (TARGET_SH4)
1583 #elif defined (TARGET_MIPS)
1584 s
->c_cpu
->active_tc
.PC
= pc
;
1585 #elif defined (TARGET_MICROBLAZE)
1586 s
->c_cpu
->sregs
[SR_PC
] = pc
;
1587 #elif defined (TARGET_CRIS)
1589 #elif defined (TARGET_ALPHA)
1594 static inline int gdb_id(CPUState
*env
)
1596 #if defined(CONFIG_USER_ONLY) && defined(USE_NPTL)
1597 return env
->host_tid
;
1599 return env
->cpu_index
+ 1;
1603 static CPUState
*find_cpu(uint32_t thread_id
)
1607 for (env
= first_cpu
; env
!= NULL
; env
= env
->next_cpu
) {
1608 if (gdb_id(env
) == thread_id
) {
1616 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
1621 int ch
, reg_size
, type
, res
;
1622 char buf
[MAX_PACKET_LENGTH
];
1623 uint8_t mem_buf
[MAX_PACKET_LENGTH
];
1625 target_ulong addr
, len
;
1628 printf("command='%s'\n", line_buf
);
1634 /* TODO: Make this return the correct value for user-mode. */
1635 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", GDB_SIGNAL_TRAP
,
1638 /* Remove all the breakpoints when this query is issued,
1639 * because gdb is doing and initial connect and the state
1640 * should be cleaned up.
1642 gdb_breakpoint_remove_all();
1646 addr
= strtoull(p
, (char **)&p
, 16);
1647 gdb_set_cpu_pc(s
, addr
);
1653 s
->signal
= gdb_signal_to_target (strtoul(p
, (char **)&p
, 16));
1654 if (s
->signal
== -1)
1659 if (strncmp(p
, "Cont", 4) == 0) {
1660 int res_signal
, res_thread
;
1664 put_packet(s
, "vCont;c;C;s;S");
1679 if (action
== 'C' || action
== 'S') {
1680 signal
= strtoul(p
, (char **)&p
, 16);
1681 } else if (action
!= 'c' && action
!= 's') {
1687 thread
= strtoull(p
+1, (char **)&p
, 16);
1689 action
= tolower(action
);
1690 if (res
== 0 || (res
== 'c' && action
== 's')) {
1692 res_signal
= signal
;
1693 res_thread
= thread
;
1697 if (res_thread
!= -1 && res_thread
!= 0) {
1698 env
= find_cpu(res_thread
);
1700 put_packet(s
, "E22");
1706 cpu_single_step(s
->c_cpu
, sstep_flags
);
1708 s
->signal
= res_signal
;
1714 goto unknown_command
;
1717 /* Kill the target */
1718 fprintf(stderr
, "\nQEMU: Terminated via GDBstub\n");
1722 gdb_breakpoint_remove_all();
1724 put_packet(s
, "OK");
1728 addr
= strtoull(p
, (char **)&p
, 16);
1729 gdb_set_cpu_pc(s
, addr
);
1731 cpu_single_step(s
->c_cpu
, sstep_flags
);
1739 ret
= strtoull(p
, (char **)&p
, 16);
1742 err
= strtoull(p
, (char **)&p
, 16);
1749 if (gdb_current_syscall_cb
)
1750 gdb_current_syscall_cb(s
->c_cpu
, ret
, err
);
1752 put_packet(s
, "T02");
1759 cpu_synchronize_state(s
->g_cpu
, 0);
1761 for (addr
= 0; addr
< num_g_regs
; addr
++) {
1762 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
+ len
, addr
);
1765 memtohex(buf
, mem_buf
, len
);
1769 registers
= mem_buf
;
1770 len
= strlen(p
) / 2;
1771 hextomem((uint8_t *)registers
, p
, len
);
1772 for (addr
= 0; addr
< num_g_regs
&& len
> 0; addr
++) {
1773 reg_size
= gdb_write_register(s
->g_cpu
, registers
, addr
);
1775 registers
+= reg_size
;
1777 cpu_synchronize_state(s
->g_cpu
, 1);
1778 put_packet(s
, "OK");
1781 addr
= strtoull(p
, (char **)&p
, 16);
1784 len
= strtoull(p
, NULL
, 16);
1785 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 0) != 0) {
1786 put_packet (s
, "E14");
1788 memtohex(buf
, mem_buf
, len
);
1793 addr
= strtoull(p
, (char **)&p
, 16);
1796 len
= strtoull(p
, (char **)&p
, 16);
1799 hextomem(mem_buf
, p
, len
);
1800 if (cpu_memory_rw_debug(s
->g_cpu
, addr
, mem_buf
, len
, 1) != 0)
1801 put_packet(s
, "E14");
1803 put_packet(s
, "OK");
1806 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1807 This works, but can be very slow. Anything new enough to
1808 understand XML also knows how to use this properly. */
1810 goto unknown_command
;
1811 addr
= strtoull(p
, (char **)&p
, 16);
1812 reg_size
= gdb_read_register(s
->g_cpu
, mem_buf
, addr
);
1814 memtohex(buf
, mem_buf
, reg_size
);
1817 put_packet(s
, "E14");
1822 goto unknown_command
;
1823 addr
= strtoull(p
, (char **)&p
, 16);
1826 reg_size
= strlen(p
) / 2;
1827 hextomem(mem_buf
, p
, reg_size
);
1828 gdb_write_register(s
->g_cpu
, mem_buf
, addr
);
1829 put_packet(s
, "OK");
1833 type
= strtoul(p
, (char **)&p
, 16);
1836 addr
= strtoull(p
, (char **)&p
, 16);
1839 len
= strtoull(p
, (char **)&p
, 16);
1841 res
= gdb_breakpoint_insert(addr
, len
, type
);
1843 res
= gdb_breakpoint_remove(addr
, len
, type
);
1845 put_packet(s
, "OK");
1846 else if (res
== -ENOSYS
)
1849 put_packet(s
, "E22");
1853 thread
= strtoull(p
, (char **)&p
, 16);
1854 if (thread
== -1 || thread
== 0) {
1855 put_packet(s
, "OK");
1858 env
= find_cpu(thread
);
1860 put_packet(s
, "E22");
1866 put_packet(s
, "OK");
1870 put_packet(s
, "OK");
1873 put_packet(s
, "E22");
1878 thread
= strtoull(p
, (char **)&p
, 16);
1879 env
= find_cpu(thread
);
1882 put_packet(s
, "OK");
1884 put_packet(s
, "E22");
1889 /* parse any 'q' packets here */
1890 if (!strcmp(p
,"qemu.sstepbits")) {
1891 /* Query Breakpoint bit definitions */
1892 snprintf(buf
, sizeof(buf
), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1898 } else if (strncmp(p
,"qemu.sstep",10) == 0) {
1899 /* Display or change the sstep_flags */
1902 /* Display current setting */
1903 snprintf(buf
, sizeof(buf
), "0x%x", sstep_flags
);
1908 type
= strtoul(p
, (char **)&p
, 16);
1910 put_packet(s
, "OK");
1912 } else if (strcmp(p
,"C") == 0) {
1913 /* "Current thread" remains vague in the spec, so always return
1914 * the first CPU (gdb returns the first thread). */
1915 put_packet(s
, "QC1");
1917 } else if (strcmp(p
,"fThreadInfo") == 0) {
1918 s
->query_cpu
= first_cpu
;
1919 goto report_cpuinfo
;
1920 } else if (strcmp(p
,"sThreadInfo") == 0) {
1923 snprintf(buf
, sizeof(buf
), "m%x", gdb_id(s
->query_cpu
));
1925 s
->query_cpu
= s
->query_cpu
->next_cpu
;
1929 } else if (strncmp(p
,"ThreadExtraInfo,", 16) == 0) {
1930 thread
= strtoull(p
+16, (char **)&p
, 16);
1931 env
= find_cpu(thread
);
1933 cpu_synchronize_state(env
, 0);
1934 len
= snprintf((char *)mem_buf
, sizeof(mem_buf
),
1935 "CPU#%d [%s]", env
->cpu_index
,
1936 env
->halted
? "halted " : "running");
1937 memtohex(buf
, mem_buf
, len
);
1942 #ifdef CONFIG_USER_ONLY
1943 else if (strncmp(p
, "Offsets", 7) == 0) {
1944 TaskState
*ts
= s
->c_cpu
->opaque
;
1946 snprintf(buf
, sizeof(buf
),
1947 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1948 ";Bss=" TARGET_ABI_FMT_lx
,
1949 ts
->info
->code_offset
,
1950 ts
->info
->data_offset
,
1951 ts
->info
->data_offset
);
1955 #else /* !CONFIG_USER_ONLY */
1956 else if (strncmp(p
, "Rcmd,", 5) == 0) {
1957 int len
= strlen(p
+ 5);
1959 if ((len
% 2) != 0) {
1960 put_packet(s
, "E01");
1963 hextomem(mem_buf
, p
+ 5, len
);
1966 qemu_chr_read(s
->mon_chr
, mem_buf
, len
);
1967 put_packet(s
, "OK");
1970 #endif /* !CONFIG_USER_ONLY */
1971 if (strncmp(p
, "Supported", 9) == 0) {
1972 snprintf(buf
, sizeof(buf
), "PacketSize=%x", MAX_PACKET_LENGTH
);
1974 pstrcat(buf
, sizeof(buf
), ";qXfer:features:read+");
1980 if (strncmp(p
, "Xfer:features:read:", 19) == 0) {
1982 target_ulong total_len
;
1986 xml
= get_feature_xml(p
, &p
);
1988 snprintf(buf
, sizeof(buf
), "E00");
1995 addr
= strtoul(p
, (char **)&p
, 16);
1998 len
= strtoul(p
, (char **)&p
, 16);
2000 total_len
= strlen(xml
);
2001 if (addr
> total_len
) {
2002 snprintf(buf
, sizeof(buf
), "E00");
2006 if (len
> (MAX_PACKET_LENGTH
- 5) / 2)
2007 len
= (MAX_PACKET_LENGTH
- 5) / 2;
2008 if (len
< total_len
- addr
) {
2010 len
= memtox(buf
+ 1, xml
+ addr
, len
);
2013 len
= memtox(buf
+ 1, xml
+ addr
, total_len
- addr
);
2015 put_packet_binary(s
, buf
, len
+ 1);
2019 /* Unrecognised 'q' command. */
2020 goto unknown_command
;
2024 /* put empty packet */
2032 void gdb_set_stop_cpu(CPUState
*env
)
2034 gdbserver_state
->c_cpu
= env
;
2035 gdbserver_state
->g_cpu
= env
;
2038 #ifndef CONFIG_USER_ONLY
2039 static void gdb_vm_state_change(void *opaque
, int running
, int reason
)
2041 GDBState
*s
= gdbserver_state
;
2042 CPUState
*env
= s
->c_cpu
;
2047 if (running
|| (reason
!= EXCP_DEBUG
&& reason
!= EXCP_INTERRUPT
) ||
2048 s
->state
== RS_INACTIVE
|| s
->state
== RS_SYSCALL
)
2051 /* disable single step if it was enable */
2052 cpu_single_step(env
, 0);
2054 if (reason
== EXCP_DEBUG
) {
2055 if (env
->watchpoint_hit
) {
2056 switch (env
->watchpoint_hit
->flags
& BP_MEM_ACCESS
) {
2067 snprintf(buf
, sizeof(buf
),
2068 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx
";",
2069 GDB_SIGNAL_TRAP
, gdb_id(env
), type
,
2070 env
->watchpoint_hit
->vaddr
);
2072 env
->watchpoint_hit
= NULL
;
2076 ret
= GDB_SIGNAL_TRAP
;
2078 ret
= GDB_SIGNAL_INT
;
2080 snprintf(buf
, sizeof(buf
), "T%02xthread:%02x;", ret
, gdb_id(env
));
2085 /* Send a gdb syscall request.
2086 This accepts limited printf-style format specifiers, specifically:
2087 %x - target_ulong argument printed in hex.
2088 %lx - 64-bit argument printed in hex.
2089 %s - string pointer (target_ulong) and length (int) pair. */
2090 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
2099 s
= gdbserver_state
;
2102 gdb_current_syscall_cb
= cb
;
2103 s
->state
= RS_SYSCALL
;
2104 #ifndef CONFIG_USER_ONLY
2105 vm_stop(EXCP_DEBUG
);
2116 addr
= va_arg(va
, target_ulong
);
2117 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
, addr
);
2120 if (*(fmt
++) != 'x')
2122 i64
= va_arg(va
, uint64_t);
2123 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, "%" PRIx64
, i64
);
2126 addr
= va_arg(va
, target_ulong
);
2127 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
"/%x",
2128 addr
, va_arg(va
, int));
2132 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
2143 #ifdef CONFIG_USER_ONLY
2144 gdb_handlesig(s
->c_cpu
, 0);
2150 static void gdb_read_byte(GDBState
*s
, int ch
)
2155 #ifndef CONFIG_USER_ONLY
2156 if (s
->last_packet_len
) {
2157 /* Waiting for a response to the last packet. If we see the start
2158 of a new command then abandon the previous response. */
2161 printf("Got NACK, retransmitting\n");
2163 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
2167 printf("Got ACK\n");
2169 printf("Got '%c' when expecting ACK/NACK\n", ch
);
2171 if (ch
== '+' || ch
== '$')
2172 s
->last_packet_len
= 0;
2177 /* when the CPU is running, we cannot do anything except stop
2178 it when receiving a char */
2179 vm_stop(EXCP_INTERRUPT
);
2186 s
->line_buf_index
= 0;
2187 s
->state
= RS_GETLINE
;
2192 s
->state
= RS_CHKSUM1
;
2193 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
2196 s
->line_buf
[s
->line_buf_index
++] = ch
;
2200 s
->line_buf
[s
->line_buf_index
] = '\0';
2201 s
->line_csum
= fromhex(ch
) << 4;
2202 s
->state
= RS_CHKSUM2
;
2205 s
->line_csum
|= fromhex(ch
);
2207 for(i
= 0; i
< s
->line_buf_index
; i
++) {
2208 csum
+= s
->line_buf
[i
];
2210 if (s
->line_csum
!= (csum
& 0xff)) {
2212 put_buffer(s
, &reply
, 1);
2216 put_buffer(s
, &reply
, 1);
2217 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
2226 #ifdef CONFIG_USER_ONLY
2232 s
= gdbserver_state
;
2234 if (gdbserver_fd
< 0 || s
->fd
< 0)
2241 gdb_handlesig (CPUState
*env
, int sig
)
2247 s
= gdbserver_state
;
2248 if (gdbserver_fd
< 0 || s
->fd
< 0)
2251 /* disable single step if it was enabled */
2252 cpu_single_step(env
, 0);
2257 snprintf(buf
, sizeof(buf
), "S%02x", target_signal_to_gdb (sig
));
2260 /* put_packet() might have detected that the peer terminated the
2267 s
->running_state
= 0;
2268 while (s
->running_state
== 0) {
2269 n
= read (s
->fd
, buf
, 256);
2274 for (i
= 0; i
< n
; i
++)
2275 gdb_read_byte (s
, buf
[i
]);
2277 else if (n
== 0 || errno
!= EAGAIN
)
2279 /* XXX: Connection closed. Should probably wait for annother
2280 connection before continuing. */
2289 /* Tell the remote gdb that the process has exited. */
2290 void gdb_exit(CPUState
*env
, int code
)
2295 s
= gdbserver_state
;
2296 if (gdbserver_fd
< 0 || s
->fd
< 0)
2299 snprintf(buf
, sizeof(buf
), "W%02x", code
);
2303 /* Tell the remote gdb that the process has exited due to SIG. */
2304 void gdb_signalled(CPUState
*env
, int sig
)
2309 s
= gdbserver_state
;
2310 if (gdbserver_fd
< 0 || s
->fd
< 0)
2313 snprintf(buf
, sizeof(buf
), "X%02x", target_signal_to_gdb (sig
));
2317 static void gdb_accept(void)
2320 struct sockaddr_in sockaddr
;
2325 len
= sizeof(sockaddr
);
2326 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
2327 if (fd
< 0 && errno
!= EINTR
) {
2330 } else if (fd
>= 0) {
2335 /* set short latency */
2337 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
2339 s
= qemu_mallocz(sizeof(GDBState
));
2340 s
->c_cpu
= first_cpu
;
2341 s
->g_cpu
= first_cpu
;
2345 gdbserver_state
= s
;
2347 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
2350 static int gdbserver_open(int port
)
2352 struct sockaddr_in sockaddr
;
2355 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
2361 /* allow fast reuse */
2363 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
2365 sockaddr
.sin_family
= AF_INET
;
2366 sockaddr
.sin_port
= htons(port
);
2367 sockaddr
.sin_addr
.s_addr
= 0;
2368 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
2373 ret
= listen(fd
, 0);
2381 int gdbserver_start(int port
)
2383 gdbserver_fd
= gdbserver_open(port
);
2384 if (gdbserver_fd
< 0)
2386 /* accept connections */
2391 /* Disable gdb stub for child processes. */
2392 void gdbserver_fork(CPUState
*env
)
2394 GDBState
*s
= gdbserver_state
;
2395 if (gdbserver_fd
< 0 || s
->fd
< 0)
2399 cpu_breakpoint_remove_all(env
, BP_GDB
);
2400 cpu_watchpoint_remove_all(env
, BP_GDB
);
2403 static int gdb_chr_can_receive(void *opaque
)
2405 /* We can handle an arbitrarily large amount of data.
2406 Pick the maximum packet size, which is as good as anything. */
2407 return MAX_PACKET_LENGTH
;
2410 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
2414 for (i
= 0; i
< size
; i
++) {
2415 gdb_read_byte(gdbserver_state
, buf
[i
]);
2419 static void gdb_chr_event(void *opaque
, int event
)
2422 case CHR_EVENT_RESET
:
2423 vm_stop(EXCP_INTERRUPT
);
2431 static void gdb_monitor_output(GDBState
*s
, const char *msg
, int len
)
2433 char buf
[MAX_PACKET_LENGTH
];
2436 if (len
> (MAX_PACKET_LENGTH
/2) - 1)
2437 len
= (MAX_PACKET_LENGTH
/2) - 1;
2438 memtohex(buf
+ 1, (uint8_t *)msg
, len
);
2442 static int gdb_monitor_write(CharDriverState
*chr
, const uint8_t *buf
, int len
)
2444 const char *p
= (const char *)buf
;
2447 max_sz
= (sizeof(gdbserver_state
->last_packet
) - 2) / 2;
2449 if (len
<= max_sz
) {
2450 gdb_monitor_output(gdbserver_state
, p
, len
);
2453 gdb_monitor_output(gdbserver_state
, p
, max_sz
);
2461 static void gdb_sigterm_handler(int signal
)
2464 vm_stop(EXCP_INTERRUPT
);
2468 int gdbserver_start(const char *device
)
2471 char gdbstub_device_name
[128];
2472 CharDriverState
*chr
= NULL
;
2473 CharDriverState
*mon_chr
;
2477 if (strcmp(device
, "none") != 0) {
2478 if (strstart(device
, "tcp:", NULL
)) {
2479 /* enforce required TCP attributes */
2480 snprintf(gdbstub_device_name
, sizeof(gdbstub_device_name
),
2481 "%s,nowait,nodelay,server", device
);
2482 device
= gdbstub_device_name
;
2485 else if (strcmp(device
, "stdio") == 0) {
2486 struct sigaction act
;
2488 memset(&act
, 0, sizeof(act
));
2489 act
.sa_handler
= gdb_sigterm_handler
;
2490 sigaction(SIGINT
, &act
, NULL
);
2493 chr
= qemu_chr_open("gdb", device
, NULL
);
2497 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
2498 gdb_chr_event
, NULL
);
2501 s
= gdbserver_state
;
2503 s
= qemu_mallocz(sizeof(GDBState
));
2504 gdbserver_state
= s
;
2506 qemu_add_vm_change_state_handler(gdb_vm_state_change
, NULL
);
2508 /* Initialize a monitor terminal for gdb */
2509 mon_chr
= qemu_mallocz(sizeof(*mon_chr
));
2510 mon_chr
->chr_write
= gdb_monitor_write
;
2511 monitor_init(mon_chr
, 0);
2514 qemu_chr_close(s
->chr
);
2515 mon_chr
= s
->mon_chr
;
2516 memset(s
, 0, sizeof(GDBState
));
2518 s
->c_cpu
= first_cpu
;
2519 s
->g_cpu
= first_cpu
;
2521 s
->state
= chr
? RS_IDLE
: RS_INACTIVE
;
2522 s
->mon_chr
= mon_chr
;