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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifdef CONFIG_USER_ONLY
32 #include "qemu-common.h"
33 #include "qemu-char.h"
39 #include "qemu_socket.h"
41 /* XXX: these constants may be independent of the host ones even for Unix */
61 typedef struct GDBState
{
62 CPUState
*env
; /* current CPU */
63 enum RSState state
; /* parsing state */
67 uint8_t last_packet
[4100];
70 #ifdef CONFIG_USER_ONLY
78 /* By default use no IRQs and no timers while single stepping so as to
79 * make single stepping like an ICE HW step.
81 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
83 #ifdef CONFIG_USER_ONLY
84 /* XXX: This is not thread safe. Do we care? */
85 static int gdbserver_fd
= -1;
87 /* XXX: remove this hack. */
88 static GDBState gdbserver_state
;
90 static int get_char(GDBState
*s
)
96 ret
= recv(s
->fd
, &ch
, 1, 0);
98 if (errno
== ECONNRESET
)
100 if (errno
!= EINTR
&& errno
!= EAGAIN
)
102 } else if (ret
== 0) {
114 /* GDB stub state for use by semihosting syscalls. */
115 static GDBState
*gdb_syscall_state
;
116 static gdb_syscall_complete_cb gdb_current_syscall_cb
;
124 /* If gdb is connected when the first semihosting syscall occurs then use
125 remote gdb syscalls. Otherwise use native file IO. */
126 int use_gdb_syscalls(void)
128 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
129 gdb_syscall_mode
= (gdb_syscall_state
? GDB_SYS_ENABLED
132 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
135 /* Resume execution. */
136 static inline void gdb_continue(GDBState
*s
)
138 #ifdef CONFIG_USER_ONLY
139 s
->running_state
= 1;
145 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
147 #ifdef CONFIG_USER_ONLY
151 ret
= send(s
->fd
, buf
, len
, 0);
153 if (errno
!= EINTR
&& errno
!= EAGAIN
)
161 qemu_chr_write(s
->chr
, buf
, len
);
165 static inline int fromhex(int v
)
167 if (v
>= '0' && v
<= '9')
169 else if (v
>= 'A' && v
<= 'F')
171 else if (v
>= 'a' && v
<= 'f')
177 static inline int tohex(int v
)
185 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
190 for(i
= 0; i
< len
; i
++) {
192 *q
++ = tohex(c
>> 4);
193 *q
++ = tohex(c
& 0xf);
198 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
202 for(i
= 0; i
< len
; i
++) {
203 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
208 /* return -1 if error, 0 if OK */
209 static int put_packet(GDBState
*s
, char *buf
)
215 printf("reply='%s'\n", buf
);
225 for(i
= 0; i
< len
; i
++) {
229 *(p
++) = tohex((csum
>> 4) & 0xf);
230 *(p
++) = tohex((csum
) & 0xf);
232 s
->last_packet_len
= p
- s
->last_packet
;
233 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
235 #ifdef CONFIG_USER_ONLY
248 #if defined(TARGET_I386)
251 static const uint8_t gdb_x86_64_regs
[16] = {
252 R_EAX
, R_EBX
, R_ECX
, R_EDX
, R_ESI
, R_EDI
, R_EBP
, R_ESP
,
253 8, 9, 10, 11, 12, 13, 14, 15,
257 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
259 int i
, fpus
, nb_regs
;
264 if (env
->hflags
& HF_CS64_MASK
) {
266 for(i
= 0; i
< 16; i
++) {
267 *(uint64_t *)p
= tswap64(env
->regs
[gdb_x86_64_regs
[i
]]);
270 *(uint64_t *)p
= tswap64(env
->eip
);
276 for(i
= 0; i
< 8; i
++) {
277 *(uint32_t *)p
= tswap32(env
->regs
[i
]);
280 *(uint32_t *)p
= tswap32(env
->eip
);
284 *(uint32_t *)p
= tswap32(env
->eflags
);
286 *(uint32_t *)p
= tswap32(env
->segs
[R_CS
].selector
);
288 *(uint32_t *)p
= tswap32(env
->segs
[R_SS
].selector
);
290 *(uint32_t *)p
= tswap32(env
->segs
[R_DS
].selector
);
292 *(uint32_t *)p
= tswap32(env
->segs
[R_ES
].selector
);
294 *(uint32_t *)p
= tswap32(env
->segs
[R_FS
].selector
);
296 *(uint32_t *)p
= tswap32(env
->segs
[R_GS
].selector
);
298 for(i
= 0; i
< 8; i
++) {
299 /* XXX: convert floats */
300 #ifdef USE_X86LDOUBLE
301 memcpy(p
, &env
->fpregs
[i
], 10);
307 *(uint32_t *)p
= tswap32(env
->fpuc
); /* fctrl */
309 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
310 *(uint32_t *)p
= tswap32(fpus
); /* fstat */
312 *(uint32_t *)p
= 0; /* ftag */
314 *(uint32_t *)p
= 0; /* fiseg */
316 *(uint32_t *)p
= 0; /* fioff */
318 *(uint32_t *)p
= 0; /* foseg */
320 *(uint32_t *)p
= 0; /* fooff */
322 *(uint32_t *)p
= 0; /* fop */
324 for(i
= 0; i
< nb_regs
; i
++) {
325 *(uint64_t *)p
= tswap64(env
->xmm_regs
[i
].XMM_Q(0));
327 *(uint64_t *)p
= tswap64(env
->xmm_regs
[i
].XMM_Q(1));
330 *(uint32_t *)p
= tswap32(env
->mxcsr
);
335 static inline void cpu_gdb_load_seg(CPUState
*env
, const uint8_t **pp
,
341 sel
= tswap32(*(uint32_t *)p
);
343 if (sel
!= env
->segs
[sreg
].selector
) {
344 #if defined(CONFIG_USER_ONLY)
345 cpu_x86_load_seg(env
, sreg
, sel
);
347 /* XXX: do it with a debug function which does not raise an
354 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
356 const uint8_t *p
= mem_buf
;
361 if (env
->hflags
& HF_CS64_MASK
) {
363 for(i
= 0; i
< 16; i
++) {
364 env
->regs
[gdb_x86_64_regs
[i
]] = tswap64(*(uint64_t *)p
);
367 env
->eip
= tswap64(*(uint64_t *)p
);
373 for(i
= 0; i
< 8; i
++) {
374 env
->regs
[i
] = tswap32(*(uint32_t *)p
);
377 env
->eip
= tswap32(*(uint32_t *)p
);
380 env
->eflags
= tswap32(*(uint32_t *)p
);
382 cpu_gdb_load_seg(env
, &p
, R_CS
);
383 cpu_gdb_load_seg(env
, &p
, R_SS
);
384 cpu_gdb_load_seg(env
, &p
, R_DS
);
385 cpu_gdb_load_seg(env
, &p
, R_ES
);
386 cpu_gdb_load_seg(env
, &p
, R_FS
);
387 cpu_gdb_load_seg(env
, &p
, R_GS
);
390 for(i
= 0; i
< 8; i
++) {
391 /* XXX: convert floats */
392 #ifdef USE_X86LDOUBLE
393 memcpy(&env
->fpregs
[i
], p
, 10);
397 env
->fpuc
= tswap32(*(uint32_t *)p
); /* fctrl */
399 fpus
= tswap32(*(uint32_t *)p
);
401 env
->fpstt
= (fpus
>> 11) & 7;
402 env
->fpus
= fpus
& ~0x3800;
405 if (size
>= ((p
- mem_buf
) + 16 * nb_regs
+ 4)) {
407 for(i
= 0; i
< nb_regs
; i
++) {
408 env
->xmm_regs
[i
].XMM_Q(0) = tswap64(*(uint64_t *)p
);
410 env
->xmm_regs
[i
].XMM_Q(1) = tswap64(*(uint64_t *)p
);
413 env
->mxcsr
= tswap32(*(uint32_t *)p
);
418 #elif defined (TARGET_PPC)
419 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
421 uint32_t *registers
= (uint32_t *)mem_buf
, tmp
;
425 for(i
= 0; i
< 32; i
++) {
426 registers
[i
] = tswapl(env
->gpr
[i
]);
429 for (i
= 0; i
< 32; i
++) {
430 registers
[(i
* 2) + 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
431 registers
[(i
* 2) + 33] = tswapl(*((uint32_t *)&env
->fpr
[i
] + 1));
433 /* nip, msr, ccr, lnk, ctr, xer, mq */
434 registers
[96] = tswapl(env
->nip
);
435 registers
[97] = tswapl(env
->msr
);
437 for (i
= 0; i
< 8; i
++)
438 tmp
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
439 registers
[98] = tswapl(tmp
);
440 registers
[99] = tswapl(env
->lr
);
441 registers
[100] = tswapl(env
->ctr
);
442 registers
[101] = tswapl(ppc_load_xer(env
));
448 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
450 uint32_t *registers
= (uint32_t *)mem_buf
;
454 for (i
= 0; i
< 32; i
++) {
455 env
->gpr
[i
] = tswapl(registers
[i
]);
458 for (i
= 0; i
< 32; i
++) {
459 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[(i
* 2) + 32]);
460 *((uint32_t *)&env
->fpr
[i
] + 1) = tswapl(registers
[(i
* 2) + 33]);
462 /* nip, msr, ccr, lnk, ctr, xer, mq */
463 env
->nip
= tswapl(registers
[96]);
464 ppc_store_msr(env
, tswapl(registers
[97]));
465 registers
[98] = tswapl(registers
[98]);
466 for (i
= 0; i
< 8; i
++)
467 env
->crf
[i
] = (registers
[98] >> (32 - ((i
+ 1) * 4))) & 0xF;
468 env
->lr
= tswapl(registers
[99]);
469 env
->ctr
= tswapl(registers
[100]);
470 ppc_store_xer(env
, tswapl(registers
[101]));
472 #elif defined (TARGET_SPARC)
474 #define tswap_abi(val) tswap32(val &0xffffffff)
476 #define tswap_abi(val) tswapl(val)
478 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
481 abi_ulong
*registers
= (abi_ulong
*)mem_buf
;
483 target_ulong
*registers
= (target_ulong
*)mem_buf
;
488 for(i
= 0; i
< 8; i
++) {
489 registers
[i
] = tswap_abi(env
->gregs
[i
]);
491 /* fill in register window */
492 for(i
= 0; i
< 24; i
++) {
493 registers
[i
+ 8] = tswap_abi(env
->regwptr
[i
]);
495 #if !defined(TARGET_SPARC64) || defined(TARGET_ABI32)
497 for (i
= 0; i
< 32; i
++) {
498 registers
[i
+ 32] = tswap_abi(*((uint32_t *)&env
->fpr
[i
]));
500 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
501 registers
[64] = tswap_abi(env
->y
);
506 registers
[65] = tswap32(tmp
);
508 registers
[66] = tswap_abi(env
->wim
);
509 registers
[67] = tswap_abi(env
->tbr
);
510 registers
[68] = tswap_abi(env
->pc
);
511 registers
[69] = tswap_abi(env
->npc
);
512 registers
[70] = tswap_abi(env
->fsr
);
513 registers
[71] = 0; /* csr */
515 return 73 * sizeof(uint32_t);
518 for (i
= 0; i
< 64; i
+= 2) {
521 tmp
= ((uint64_t)*(uint32_t *)&env
->fpr
[i
]) << 32;
522 tmp
|= *(uint32_t *)&env
->fpr
[i
+ 1];
523 registers
[i
/ 2 + 32] = tswap64(tmp
);
525 registers
[64] = tswapl(env
->pc
);
526 registers
[65] = tswapl(env
->npc
);
527 registers
[66] = tswapl(((uint64_t)GET_CCR(env
) << 32) |
528 ((env
->asi
& 0xff) << 24) |
529 ((env
->pstate
& 0xfff) << 8) |
531 registers
[67] = tswapl(env
->fsr
);
532 registers
[68] = tswapl(env
->fprs
);
533 registers
[69] = tswapl(env
->y
);
534 return 70 * sizeof(target_ulong
);
538 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
541 abi_ulong
*registers
= (abi_ulong
*)mem_buf
;
543 target_ulong
*registers
= (target_ulong
*)mem_buf
;
548 for(i
= 0; i
< 7; i
++) {
549 env
->gregs
[i
] = tswap_abi(registers
[i
]);
551 /* fill in register window */
552 for(i
= 0; i
< 24; i
++) {
553 env
->regwptr
[i
] = tswap_abi(registers
[i
+ 8]);
555 #if !defined(TARGET_SPARC64) || defined(TARGET_ABI32)
557 for (i
= 0; i
< 32; i
++) {
558 *((uint32_t *)&env
->fpr
[i
]) = tswap_abi(registers
[i
+ 32]);
560 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
561 env
->y
= tswap_abi(registers
[64]);
562 PUT_PSR(env
, tswap_abi(registers
[65]));
563 env
->wim
= tswap_abi(registers
[66]);
564 env
->tbr
= tswap_abi(registers
[67]);
565 env
->pc
= tswap_abi(registers
[68]);
566 env
->npc
= tswap_abi(registers
[69]);
567 env
->fsr
= tswap_abi(registers
[70]);
569 for (i
= 0; i
< 64; i
+= 2) {
572 tmp
= tswap64(registers
[i
/ 2 + 32]);
573 *((uint32_t *)&env
->fpr
[i
]) = tmp
>> 32;
574 *((uint32_t *)&env
->fpr
[i
+ 1]) = tmp
& 0xffffffff;
576 env
->pc
= tswapl(registers
[64]);
577 env
->npc
= tswapl(registers
[65]);
579 uint64_t tmp
= tswapl(registers
[66]);
581 PUT_CCR(env
, tmp
>> 32);
582 env
->asi
= (tmp
>> 24) & 0xff;
583 env
->pstate
= (tmp
>> 8) & 0xfff;
584 PUT_CWP64(env
, tmp
& 0xff);
586 env
->fsr
= tswapl(registers
[67]);
587 env
->fprs
= tswapl(registers
[68]);
588 env
->y
= tswapl(registers
[69]);
592 #elif defined (TARGET_ARM)
593 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
599 /* 16 core integer registers (4 bytes each). */
600 for (i
= 0; i
< 16; i
++)
602 *(uint32_t *)ptr
= tswapl(env
->regs
[i
]);
605 /* 8 FPA registers (12 bytes each), FPS (4 bytes).
606 Not yet implemented. */
607 memset (ptr
, 0, 8 * 12 + 4);
609 /* CPSR (4 bytes). */
610 *(uint32_t *)ptr
= tswapl (cpsr_read(env
));
613 return ptr
- mem_buf
;
616 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
622 /* Core integer registers. */
623 for (i
= 0; i
< 16; i
++)
625 env
->regs
[i
] = tswapl(*(uint32_t *)ptr
);
628 /* Ignore FPA regs and scr. */
630 cpsr_write (env
, tswapl(*(uint32_t *)ptr
), 0xffffffff);
632 #elif defined (TARGET_M68K)
633 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
641 for (i
= 0; i
< 8; i
++) {
642 *(uint32_t *)ptr
= tswapl(env
->dregs
[i
]);
646 for (i
= 0; i
< 8; i
++) {
647 *(uint32_t *)ptr
= tswapl(env
->aregs
[i
]);
650 *(uint32_t *)ptr
= tswapl(env
->sr
);
652 *(uint32_t *)ptr
= tswapl(env
->pc
);
654 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
655 ColdFire has 8-bit double precision registers. */
656 for (i
= 0; i
< 8; i
++) {
658 *(uint32_t *)ptr
= tswap32(u
.l
.upper
);
659 *(uint32_t *)ptr
= tswap32(u
.l
.lower
);
661 /* FP control regs (not implemented). */
662 memset (ptr
, 0, 3 * 4);
665 return ptr
- mem_buf
;
668 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
676 for (i
= 0; i
< 8; i
++) {
677 env
->dregs
[i
] = tswapl(*(uint32_t *)ptr
);
681 for (i
= 0; i
< 8; i
++) {
682 env
->aregs
[i
] = tswapl(*(uint32_t *)ptr
);
685 env
->sr
= tswapl(*(uint32_t *)ptr
);
687 env
->pc
= tswapl(*(uint32_t *)ptr
);
689 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
690 ColdFire has 8-bit double precision registers. */
691 for (i
= 0; i
< 8; i
++) {
692 u
.l
.upper
= tswap32(*(uint32_t *)ptr
);
693 u
.l
.lower
= tswap32(*(uint32_t *)ptr
);
696 /* FP control regs (not implemented). */
699 #elif defined (TARGET_MIPS)
700 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
706 for (i
= 0; i
< 32; i
++)
708 *(target_ulong
*)ptr
= tswapl(env
->active_tc
.gpr
[i
]);
709 ptr
+= sizeof(target_ulong
);
712 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_Status
);
713 ptr
+= sizeof(target_ulong
);
715 *(target_ulong
*)ptr
= tswapl(env
->active_tc
.LO
[0]);
716 ptr
+= sizeof(target_ulong
);
718 *(target_ulong
*)ptr
= tswapl(env
->active_tc
.HI
[0]);
719 ptr
+= sizeof(target_ulong
);
721 *(target_ulong
*)ptr
= tswapl(env
->CP0_BadVAddr
);
722 ptr
+= sizeof(target_ulong
);
724 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_Cause
);
725 ptr
+= sizeof(target_ulong
);
727 *(target_ulong
*)ptr
= tswapl(env
->active_tc
.PC
);
728 ptr
+= sizeof(target_ulong
);
730 if (env
->CP0_Config1
& (1 << CP0C1_FP
))
732 for (i
= 0; i
< 32; i
++)
734 if (env
->CP0_Status
& (1 << CP0St_FR
))
735 *(target_ulong
*)ptr
= tswapl(env
->fpu
->fpr
[i
].d
);
737 *(target_ulong
*)ptr
= tswap32(env
->fpu
->fpr
[i
].w
[FP_ENDIAN_IDX
]);
738 ptr
+= sizeof(target_ulong
);
741 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->fpu
->fcr31
);
742 ptr
+= sizeof(target_ulong
);
744 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->fpu
->fcr0
);
745 ptr
+= sizeof(target_ulong
);
748 /* "fp", pseudo frame pointer. Not yet implemented in gdb. */
749 *(target_ulong
*)ptr
= 0;
750 ptr
+= sizeof(target_ulong
);
752 /* Registers for embedded use, we just pad them. */
753 for (i
= 0; i
< 16; i
++)
755 *(target_ulong
*)ptr
= 0;
756 ptr
+= sizeof(target_ulong
);
760 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_PRid
);
761 ptr
+= sizeof(target_ulong
);
763 return ptr
- mem_buf
;
766 /* convert MIPS rounding mode in FCR31 to IEEE library */
767 static unsigned int ieee_rm
[] =
769 float_round_nearest_even
,
774 #define RESTORE_ROUNDING_MODE \
775 set_float_rounding_mode(ieee_rm[env->fpu->fcr31 & 3], &env->fpu->fp_status)
777 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
783 for (i
= 0; i
< 32; i
++)
785 env
->active_tc
.gpr
[i
] = tswapl(*(target_ulong
*)ptr
);
786 ptr
+= sizeof(target_ulong
);
789 env
->CP0_Status
= tswapl(*(target_ulong
*)ptr
);
790 ptr
+= sizeof(target_ulong
);
792 env
->active_tc
.LO
[0] = tswapl(*(target_ulong
*)ptr
);
793 ptr
+= sizeof(target_ulong
);
795 env
->active_tc
.HI
[0] = tswapl(*(target_ulong
*)ptr
);
796 ptr
+= sizeof(target_ulong
);
798 env
->CP0_BadVAddr
= tswapl(*(target_ulong
*)ptr
);
799 ptr
+= sizeof(target_ulong
);
801 env
->CP0_Cause
= tswapl(*(target_ulong
*)ptr
);
802 ptr
+= sizeof(target_ulong
);
804 env
->active_tc
.PC
= tswapl(*(target_ulong
*)ptr
);
805 ptr
+= sizeof(target_ulong
);
807 if (env
->CP0_Config1
& (1 << CP0C1_FP
))
809 for (i
= 0; i
< 32; i
++)
811 if (env
->CP0_Status
& (1 << CP0St_FR
))
812 env
->fpu
->fpr
[i
].d
= tswapl(*(target_ulong
*)ptr
);
814 env
->fpu
->fpr
[i
].w
[FP_ENDIAN_IDX
] = tswapl(*(target_ulong
*)ptr
);
815 ptr
+= sizeof(target_ulong
);
818 env
->fpu
->fcr31
= tswapl(*(target_ulong
*)ptr
) & 0xFF83FFFF;
819 ptr
+= sizeof(target_ulong
);
821 /* The remaining registers are assumed to be read-only. */
823 /* set rounding mode */
824 RESTORE_ROUNDING_MODE
;
826 #ifndef CONFIG_SOFTFLOAT
827 /* no floating point exception for native float */
828 SET_FP_ENABLE(env
->fcr31
, 0);
832 #elif defined (TARGET_SH4)
834 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
836 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
838 uint32_t *ptr
= (uint32_t *)mem_buf
;
841 #define SAVE(x) *ptr++=tswapl(x)
842 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
843 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
+ 16]);
845 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
]);
847 for (i
= 8; i
< 16; i
++) SAVE(env
->gregs
[i
]);
857 for (i
= 0; i
< 16; i
++)
858 SAVE(env
->fregs
[i
+ ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
861 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
]);
862 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
+ 16]);
863 return ((uint8_t *)ptr
- mem_buf
);
866 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
868 uint32_t *ptr
= (uint32_t *)mem_buf
;
871 #define LOAD(x) (x)=*ptr++;
872 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
873 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
+ 16]);
875 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
]);
877 for (i
= 8; i
< 16; i
++) LOAD(env
->gregs
[i
]);
887 for (i
= 0; i
< 16; i
++)
888 LOAD(env
->fregs
[i
+ ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
891 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
]);
892 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
+ 16]);
894 #elif defined (TARGET_CRIS)
896 static int cris_save_32 (unsigned char *d
, uint32_t value
)
899 *d
++ = (value
>>= 8);
900 *d
++ = (value
>>= 8);
901 *d
++ = (value
>>= 8);
904 static int cris_save_16 (unsigned char *d
, uint32_t value
)
907 *d
++ = (value
>>= 8);
910 static int cris_save_8 (unsigned char *d
, uint32_t value
)
916 /* FIXME: this will bug on archs not supporting unaligned word accesses. */
917 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
919 uint8_t *ptr
= mem_buf
;
923 for (i
= 0; i
< 16; i
++)
924 ptr
+= cris_save_32 (ptr
, env
->regs
[i
]);
926 srs
= env
->pregs
[PR_SRS
];
928 ptr
+= cris_save_8 (ptr
, env
->pregs
[0]);
929 ptr
+= cris_save_8 (ptr
, env
->pregs
[1]);
930 ptr
+= cris_save_32 (ptr
, env
->pregs
[2]);
931 ptr
+= cris_save_8 (ptr
, srs
);
932 ptr
+= cris_save_16 (ptr
, env
->pregs
[4]);
934 for (i
= 5; i
< 16; i
++)
935 ptr
+= cris_save_32 (ptr
, env
->pregs
[i
]);
937 ptr
+= cris_save_32 (ptr
, env
->pc
);
939 for (i
= 0; i
< 16; i
++)
940 ptr
+= cris_save_32 (ptr
, env
->sregs
[srs
][i
]);
942 return ((uint8_t *)ptr
- mem_buf
);
945 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
947 uint32_t *ptr
= (uint32_t *)mem_buf
;
950 #define LOAD(x) (x)=*ptr++;
951 for (i
= 0; i
< 16; i
++) LOAD(env
->regs
[i
]);
955 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
960 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
966 static int gdb_handle_packet(GDBState
*s
, CPUState
*env
, const char *line_buf
)
969 int ch
, reg_size
, type
;
971 uint8_t mem_buf
[4096];
973 target_ulong addr
, len
;
976 printf("command='%s'\n", line_buf
);
982 /* TODO: Make this return the correct value for user-mode. */
983 snprintf(buf
, sizeof(buf
), "S%02x", SIGTRAP
);
985 /* Remove all the breakpoints when this query is issued,
986 * because gdb is doing and initial connect and the state
987 * should be cleaned up.
989 cpu_breakpoint_remove_all(env
);
990 cpu_watchpoint_remove_all(env
);
994 addr
= strtoull(p
, (char **)&p
, 16);
995 #if defined(TARGET_I386)
997 kvm_load_registers(env
);
998 #elif defined (TARGET_PPC)
1000 kvm_load_registers(env
);
1001 #elif defined (TARGET_SPARC)
1003 env
->npc
= addr
+ 4;
1004 #elif defined (TARGET_ARM)
1005 env
->regs
[15] = addr
;
1006 #elif defined (TARGET_SH4)
1008 #elif defined (TARGET_MIPS)
1009 env
->active_tc
.PC
= addr
;
1010 #elif defined (TARGET_CRIS)
1017 s
->signal
= strtoul(p
, (char **)&p
, 16);
1021 /* Kill the target */
1022 fprintf(stderr
, "\nQEMU: Terminated via GDBstub\n");
1026 cpu_breakpoint_remove_all(env
);
1027 cpu_watchpoint_remove_all(env
);
1029 put_packet(s
, "OK");
1033 addr
= strtoull(p
, (char **)&p
, 16);
1034 #if defined(TARGET_I386)
1036 kvm_load_registers(env
);
1037 #elif defined (TARGET_PPC)
1039 kvm_load_registers(env
);
1040 #elif defined (TARGET_SPARC)
1042 env
->npc
= addr
+ 4;
1043 #elif defined (TARGET_ARM)
1044 env
->regs
[15] = addr
;
1045 #elif defined (TARGET_SH4)
1047 #elif defined (TARGET_MIPS)
1048 env
->active_tc
.PC
= addr
;
1049 #elif defined (TARGET_CRIS)
1053 cpu_single_step(env
, sstep_flags
);
1061 ret
= strtoull(p
, (char **)&p
, 16);
1064 err
= strtoull(p
, (char **)&p
, 16);
1071 if (gdb_current_syscall_cb
)
1072 gdb_current_syscall_cb(s
->env
, ret
, err
);
1074 put_packet(s
, "T02");
1081 kvm_save_registers(env
);
1082 reg_size
= cpu_gdb_read_registers(env
, mem_buf
);
1083 memtohex(buf
, mem_buf
, reg_size
);
1087 registers
= (void *)mem_buf
;
1088 len
= strlen(p
) / 2;
1089 hextomem((uint8_t *)registers
, p
, len
);
1090 cpu_gdb_write_registers(env
, mem_buf
, len
);
1091 kvm_load_registers(env
);
1092 put_packet(s
, "OK");
1095 addr
= strtoull(p
, (char **)&p
, 16);
1098 len
= strtoull(p
, NULL
, 16);
1099 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 0) != 0) {
1100 put_packet (s
, "E14");
1102 memtohex(buf
, mem_buf
, len
);
1107 addr
= strtoull(p
, (char **)&p
, 16);
1110 len
= strtoull(p
, (char **)&p
, 16);
1113 hextomem(mem_buf
, p
, len
);
1114 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 1) != 0)
1115 put_packet(s
, "E14");
1117 put_packet(s
, "OK");
1120 type
= strtoul(p
, (char **)&p
, 16);
1123 addr
= strtoull(p
, (char **)&p
, 16);
1126 len
= strtoull(p
, (char **)&p
, 16);
1130 if (cpu_breakpoint_insert(env
, addr
) < 0)
1131 goto breakpoint_error
;
1132 put_packet(s
, "OK");
1134 #ifndef CONFIG_USER_ONLY
1137 goto insert_watchpoint
;
1140 goto insert_watchpoint
;
1142 type
= PAGE_READ
| PAGE_WRITE
;
1144 if (cpu_watchpoint_insert(env
, addr
, type
) < 0)
1145 goto breakpoint_error
;
1146 put_packet(s
, "OK");
1155 put_packet(s
, "E22");
1159 type
= strtoul(p
, (char **)&p
, 16);
1162 addr
= strtoull(p
, (char **)&p
, 16);
1165 len
= strtoull(p
, (char **)&p
, 16);
1166 if (type
== 0 || type
== 1) {
1167 cpu_breakpoint_remove(env
, addr
);
1168 put_packet(s
, "OK");
1169 #ifndef CONFIG_USER_ONLY
1170 } else if (type
>= 2 || type
<= 4) {
1171 cpu_watchpoint_remove(env
, addr
);
1172 put_packet(s
, "OK");
1180 /* parse any 'q' packets here */
1181 if (!strcmp(p
,"qemu.sstepbits")) {
1182 /* Query Breakpoint bit definitions */
1183 snprintf(buf
, sizeof(buf
), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1189 } else if (strncmp(p
,"qemu.sstep",10) == 0) {
1190 /* Display or change the sstep_flags */
1193 /* Display current setting */
1194 snprintf(buf
, sizeof(buf
), "0x%x", sstep_flags
);
1199 type
= strtoul(p
, (char **)&p
, 16);
1201 put_packet(s
, "OK");
1204 #ifdef CONFIG_LINUX_USER
1205 else if (strncmp(p
, "Offsets", 7) == 0) {
1206 TaskState
*ts
= env
->opaque
;
1208 snprintf(buf
, sizeof(buf
),
1209 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1210 ";Bss=" TARGET_ABI_FMT_lx
,
1211 ts
->info
->code_offset
,
1212 ts
->info
->data_offset
,
1213 ts
->info
->data_offset
);
1220 /* put empty packet */
1228 extern void tb_flush(CPUState
*env
);
1230 #ifndef CONFIG_USER_ONLY
1231 static void gdb_vm_stopped(void *opaque
, int reason
)
1233 GDBState
*s
= opaque
;
1237 if (s
->state
== RS_SYSCALL
)
1240 /* disable single step if it was enable */
1241 cpu_single_step(s
->env
, 0);
1243 if (reason
== EXCP_DEBUG
) {
1244 if (s
->env
->watchpoint_hit
) {
1245 snprintf(buf
, sizeof(buf
), "T%02xwatch:" TARGET_FMT_lx
";",
1247 s
->env
->watchpoint
[s
->env
->watchpoint_hit
- 1].vaddr
);
1249 s
->env
->watchpoint_hit
= 0;
1254 } else if (reason
== EXCP_INTERRUPT
) {
1259 snprintf(buf
, sizeof(buf
), "S%02x", ret
);
1264 /* Send a gdb syscall request.
1265 This accepts limited printf-style format specifiers, specifically:
1266 %x - target_ulong argument printed in hex.
1267 %lx - 64-bit argument printed in hex.
1268 %s - string pointer (target_ulong) and length (int) pair. */
1269 void gdb_do_syscall(gdb_syscall_complete_cb cb
, char *fmt
, ...)
1278 s
= gdb_syscall_state
;
1281 gdb_current_syscall_cb
= cb
;
1282 s
->state
= RS_SYSCALL
;
1283 #ifndef CONFIG_USER_ONLY
1284 vm_stop(EXCP_DEBUG
);
1295 addr
= va_arg(va
, target_ulong
);
1296 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
, addr
);
1299 if (*(fmt
++) != 'x')
1301 i64
= va_arg(va
, uint64_t);
1302 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, "%" PRIx64
, i64
);
1305 addr
= va_arg(va
, target_ulong
);
1306 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
"/%x",
1307 addr
, va_arg(va
, int));
1311 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
1322 #ifdef CONFIG_USER_ONLY
1323 gdb_handlesig(s
->env
, 0);
1325 cpu_interrupt(s
->env
, CPU_INTERRUPT_EXIT
);
1329 static void gdb_read_byte(GDBState
*s
, int ch
)
1331 CPUState
*env
= s
->env
;
1335 #ifndef CONFIG_USER_ONLY
1336 if (s
->last_packet_len
) {
1337 /* Waiting for a response to the last packet. If we see the start
1338 of a new command then abandon the previous response. */
1341 printf("Got NACK, retransmitting\n");
1343 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
1347 printf("Got ACK\n");
1349 printf("Got '%c' when expecting ACK/NACK\n", ch
);
1351 if (ch
== '+' || ch
== '$')
1352 s
->last_packet_len
= 0;
1357 /* when the CPU is running, we cannot do anything except stop
1358 it when receiving a char */
1359 vm_stop(EXCP_INTERRUPT
);
1366 s
->line_buf_index
= 0;
1367 s
->state
= RS_GETLINE
;
1372 s
->state
= RS_CHKSUM1
;
1373 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
1376 s
->line_buf
[s
->line_buf_index
++] = ch
;
1380 s
->line_buf
[s
->line_buf_index
] = '\0';
1381 s
->line_csum
= fromhex(ch
) << 4;
1382 s
->state
= RS_CHKSUM2
;
1385 s
->line_csum
|= fromhex(ch
);
1387 for(i
= 0; i
< s
->line_buf_index
; i
++) {
1388 csum
+= s
->line_buf
[i
];
1390 if (s
->line_csum
!= (csum
& 0xff)) {
1392 put_buffer(s
, &reply
, 1);
1396 put_buffer(s
, &reply
, 1);
1397 s
->state
= gdb_handle_packet(s
, env
, s
->line_buf
);
1406 #ifdef CONFIG_USER_ONLY
1408 gdb_handlesig (CPUState
*env
, int sig
)
1414 s
= &gdbserver_state
;
1415 if (gdbserver_fd
< 0 || s
->fd
< 0)
1418 /* disable single step if it was enabled */
1419 cpu_single_step(env
, 0);
1424 snprintf(buf
, sizeof(buf
), "S%02x", sig
);
1427 /* put_packet() might have detected that the peer terminated the
1434 s
->running_state
= 0;
1435 while (s
->running_state
== 0) {
1436 n
= read (s
->fd
, buf
, 256);
1441 for (i
= 0; i
< n
; i
++)
1442 gdb_read_byte (s
, buf
[i
]);
1444 else if (n
== 0 || errno
!= EAGAIN
)
1446 /* XXX: Connection closed. Should probably wait for annother
1447 connection before continuing. */
1456 /* Tell the remote gdb that the process has exited. */
1457 void gdb_exit(CPUState
*env
, int code
)
1462 s
= &gdbserver_state
;
1463 if (gdbserver_fd
< 0 || s
->fd
< 0)
1466 snprintf(buf
, sizeof(buf
), "W%02x", code
);
1471 static void gdb_accept(void *opaque
)
1474 struct sockaddr_in sockaddr
;
1479 len
= sizeof(sockaddr
);
1480 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
1481 if (fd
< 0 && errno
!= EINTR
) {
1484 } else if (fd
>= 0) {
1489 /* set short latency */
1491 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1493 s
= &gdbserver_state
;
1494 memset (s
, 0, sizeof (GDBState
));
1495 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1498 gdb_syscall_state
= s
;
1500 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1503 static int gdbserver_open(int port
)
1505 struct sockaddr_in sockaddr
;
1508 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1514 /* allow fast reuse */
1516 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
1518 sockaddr
.sin_family
= AF_INET
;
1519 sockaddr
.sin_port
= htons(port
);
1520 sockaddr
.sin_addr
.s_addr
= 0;
1521 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
1526 ret
= listen(fd
, 0);
1534 int gdbserver_start(int port
)
1536 gdbserver_fd
= gdbserver_open(port
);
1537 if (gdbserver_fd
< 0)
1539 /* accept connections */
1544 static int gdb_chr_can_receive(void *opaque
)
1549 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
1551 GDBState
*s
= opaque
;
1554 for (i
= 0; i
< size
; i
++) {
1555 gdb_read_byte(s
, buf
[i
]);
1559 static void gdb_chr_event(void *opaque
, int event
)
1562 case CHR_EVENT_RESET
:
1563 vm_stop(EXCP_INTERRUPT
);
1564 gdb_syscall_state
= opaque
;
1571 int gdbserver_start(const char *port
)
1574 char gdbstub_port_name
[128];
1577 CharDriverState
*chr
;
1579 if (!port
|| !*port
)
1582 port_num
= strtol(port
, &p
, 10);
1584 /* A numeric value is interpreted as a port number. */
1585 snprintf(gdbstub_port_name
, sizeof(gdbstub_port_name
),
1586 "tcp::%d,nowait,nodelay,server", port_num
);
1587 port
= gdbstub_port_name
;
1590 chr
= qemu_chr_open(port
);
1594 s
= qemu_mallocz(sizeof(GDBState
));
1598 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1600 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
1602 qemu_add_vm_stop_handler(gdb_vm_stopped
, s
);