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"
38 #include "qemu_socket.h"
40 /* XXX: these constants may be independent of the host ones even for Unix */
60 typedef struct GDBState
{
61 CPUState
*env
; /* current CPU */
62 enum RSState state
; /* parsing state */
66 uint8_t last_packet
[4100];
69 #ifdef CONFIG_USER_ONLY
77 /* By default use no IRQs and no timers while single stepping so as to
78 * make single stepping like an ICE HW step.
80 static int sstep_flags
= SSTEP_ENABLE
|SSTEP_NOIRQ
|SSTEP_NOTIMER
;
82 #ifdef CONFIG_USER_ONLY
83 /* XXX: This is not thread safe. Do we care? */
84 static int gdbserver_fd
= -1;
86 /* XXX: remove this hack. */
87 static GDBState gdbserver_state
;
89 static int get_char(GDBState
*s
)
95 ret
= recv(s
->fd
, &ch
, 1, 0);
97 if (errno
== ECONNRESET
)
99 if (errno
!= EINTR
&& errno
!= EAGAIN
)
101 } else if (ret
== 0) {
113 /* GDB stub state for use by semihosting syscalls. */
114 static GDBState
*gdb_syscall_state
;
115 static gdb_syscall_complete_cb gdb_current_syscall_cb
;
123 /* If gdb is connected when the first semihosting syscall occurs then use
124 remote gdb syscalls. Otherwise use native file IO. */
125 int use_gdb_syscalls(void)
127 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
128 gdb_syscall_mode
= (gdb_syscall_state
? GDB_SYS_ENABLED
131 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
134 /* Resume execution. */
135 static inline void gdb_continue(GDBState
*s
)
137 #ifdef CONFIG_USER_ONLY
138 s
->running_state
= 1;
144 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
146 #ifdef CONFIG_USER_ONLY
150 ret
= send(s
->fd
, buf
, len
, 0);
152 if (errno
!= EINTR
&& errno
!= EAGAIN
)
160 qemu_chr_write(s
->chr
, buf
, len
);
164 static inline int fromhex(int v
)
166 if (v
>= '0' && v
<= '9')
168 else if (v
>= 'A' && v
<= 'F')
170 else if (v
>= 'a' && v
<= 'f')
176 static inline int tohex(int v
)
184 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
189 for(i
= 0; i
< len
; i
++) {
191 *q
++ = tohex(c
>> 4);
192 *q
++ = tohex(c
& 0xf);
197 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
201 for(i
= 0; i
< len
; i
++) {
202 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
207 /* return -1 if error, 0 if OK */
208 static int put_packet(GDBState
*s
, const char *buf
)
214 printf("reply='%s'\n", buf
);
224 for(i
= 0; i
< len
; i
++) {
228 *(p
++) = tohex((csum
>> 4) & 0xf);
229 *(p
++) = tohex((csum
) & 0xf);
231 s
->last_packet_len
= p
- s
->last_packet
;
232 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
234 #ifdef CONFIG_USER_ONLY
247 #if defined(TARGET_I386)
250 static const uint8_t gdb_x86_64_regs
[16] = {
251 R_EAX
, R_EBX
, R_ECX
, R_EDX
, R_ESI
, R_EDI
, R_EBP
, R_ESP
,
252 8, 9, 10, 11, 12, 13, 14, 15,
256 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
258 int i
, fpus
, nb_regs
;
263 if (env
->hflags
& HF_CS64_MASK
) {
265 for(i
= 0; i
< 16; i
++) {
266 *(uint64_t *)p
= tswap64(env
->regs
[gdb_x86_64_regs
[i
]]);
269 *(uint64_t *)p
= tswap64(env
->eip
);
275 for(i
= 0; i
< 8; i
++) {
276 *(uint32_t *)p
= tswap32(env
->regs
[i
]);
279 *(uint32_t *)p
= tswap32(env
->eip
);
283 *(uint32_t *)p
= tswap32(env
->eflags
);
285 *(uint32_t *)p
= tswap32(env
->segs
[R_CS
].selector
);
287 *(uint32_t *)p
= tswap32(env
->segs
[R_SS
].selector
);
289 *(uint32_t *)p
= tswap32(env
->segs
[R_DS
].selector
);
291 *(uint32_t *)p
= tswap32(env
->segs
[R_ES
].selector
);
293 *(uint32_t *)p
= tswap32(env
->segs
[R_FS
].selector
);
295 *(uint32_t *)p
= tswap32(env
->segs
[R_GS
].selector
);
297 for(i
= 0; i
< 8; i
++) {
298 /* XXX: convert floats */
299 #ifdef USE_X86LDOUBLE
300 memcpy(p
, &env
->fpregs
[i
], 10);
306 *(uint32_t *)p
= tswap32(env
->fpuc
); /* fctrl */
308 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
309 *(uint32_t *)p
= tswap32(fpus
); /* fstat */
311 *(uint32_t *)p
= 0; /* ftag */
313 *(uint32_t *)p
= 0; /* fiseg */
315 *(uint32_t *)p
= 0; /* fioff */
317 *(uint32_t *)p
= 0; /* foseg */
319 *(uint32_t *)p
= 0; /* fooff */
321 *(uint32_t *)p
= 0; /* fop */
323 for(i
= 0; i
< nb_regs
; i
++) {
324 *(uint64_t *)p
= tswap64(env
->xmm_regs
[i
].XMM_Q(0));
326 *(uint64_t *)p
= tswap64(env
->xmm_regs
[i
].XMM_Q(1));
329 *(uint32_t *)p
= tswap32(env
->mxcsr
);
334 static inline void cpu_gdb_load_seg(CPUState
*env
, const uint8_t **pp
,
340 sel
= tswap32(*(uint32_t *)p
);
342 if (sel
!= env
->segs
[sreg
].selector
) {
343 #if defined(CONFIG_USER_ONLY)
344 cpu_x86_load_seg(env
, sreg
, sel
);
346 /* XXX: do it with a debug function which does not raise an
353 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
355 const uint8_t *p
= mem_buf
;
360 if (env
->hflags
& HF_CS64_MASK
) {
362 for(i
= 0; i
< 16; i
++) {
363 env
->regs
[gdb_x86_64_regs
[i
]] = tswap64(*(uint64_t *)p
);
366 env
->eip
= tswap64(*(uint64_t *)p
);
372 for(i
= 0; i
< 8; i
++) {
373 env
->regs
[i
] = tswap32(*(uint32_t *)p
);
376 env
->eip
= tswap32(*(uint32_t *)p
);
379 env
->eflags
= tswap32(*(uint32_t *)p
);
381 cpu_gdb_load_seg(env
, &p
, R_CS
);
382 cpu_gdb_load_seg(env
, &p
, R_SS
);
383 cpu_gdb_load_seg(env
, &p
, R_DS
);
384 cpu_gdb_load_seg(env
, &p
, R_ES
);
385 cpu_gdb_load_seg(env
, &p
, R_FS
);
386 cpu_gdb_load_seg(env
, &p
, R_GS
);
389 for(i
= 0; i
< 8; i
++) {
390 /* XXX: convert floats */
391 #ifdef USE_X86LDOUBLE
392 memcpy(&env
->fpregs
[i
], p
, 10);
396 env
->fpuc
= tswap32(*(uint32_t *)p
); /* fctrl */
398 fpus
= tswap32(*(uint32_t *)p
);
400 env
->fpstt
= (fpus
>> 11) & 7;
401 env
->fpus
= fpus
& ~0x3800;
404 if (size
>= ((p
- mem_buf
) + 16 * nb_regs
+ 4)) {
406 for(i
= 0; i
< nb_regs
; i
++) {
407 env
->xmm_regs
[i
].XMM_Q(0) = tswap64(*(uint64_t *)p
);
409 env
->xmm_regs
[i
].XMM_Q(1) = tswap64(*(uint64_t *)p
);
412 env
->mxcsr
= tswap32(*(uint32_t *)p
);
417 #elif defined (TARGET_PPC)
418 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
420 uint32_t *registers
= (uint32_t *)mem_buf
, tmp
;
424 for(i
= 0; i
< 32; i
++) {
425 registers
[i
] = tswapl(env
->gpr
[i
]);
428 for (i
= 0; i
< 32; i
++) {
429 registers
[(i
* 2) + 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
430 registers
[(i
* 2) + 33] = tswapl(*((uint32_t *)&env
->fpr
[i
] + 1));
432 /* nip, msr, ccr, lnk, ctr, xer, mq */
433 registers
[96] = tswapl(env
->nip
);
434 registers
[97] = tswapl(env
->msr
);
436 for (i
= 0; i
< 8; i
++)
437 tmp
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
438 registers
[98] = tswapl(tmp
);
439 registers
[99] = tswapl(env
->lr
);
440 registers
[100] = tswapl(env
->ctr
);
441 registers
[101] = tswapl(ppc_load_xer(env
));
447 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
449 uint32_t *registers
= (uint32_t *)mem_buf
;
453 for (i
= 0; i
< 32; i
++) {
454 env
->gpr
[i
] = tswapl(registers
[i
]);
457 for (i
= 0; i
< 32; i
++) {
458 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[(i
* 2) + 32]);
459 *((uint32_t *)&env
->fpr
[i
] + 1) = tswapl(registers
[(i
* 2) + 33]);
461 /* nip, msr, ccr, lnk, ctr, xer, mq */
462 env
->nip
= tswapl(registers
[96]);
463 ppc_store_msr(env
, tswapl(registers
[97]));
464 registers
[98] = tswapl(registers
[98]);
465 for (i
= 0; i
< 8; i
++)
466 env
->crf
[i
] = (registers
[98] >> (32 - ((i
+ 1) * 4))) & 0xF;
467 env
->lr
= tswapl(registers
[99]);
468 env
->ctr
= tswapl(registers
[100]);
469 ppc_store_xer(env
, tswapl(registers
[101]));
471 #elif defined (TARGET_SPARC)
473 #define tswap_abi(val) tswap32(val &0xffffffff)
475 #define tswap_abi(val) tswapl(val)
477 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
480 abi_ulong
*registers
= (abi_ulong
*)mem_buf
;
482 target_ulong
*registers
= (target_ulong
*)mem_buf
;
487 for(i
= 0; i
< 8; i
++) {
488 registers
[i
] = tswap_abi(env
->gregs
[i
]);
490 /* fill in register window */
491 for(i
= 0; i
< 24; i
++) {
492 registers
[i
+ 8] = tswap_abi(env
->regwptr
[i
]);
494 #if !defined(TARGET_SPARC64) || defined(TARGET_ABI32)
496 for (i
= 0; i
< 32; i
++) {
497 registers
[i
+ 32] = tswap_abi(*((uint32_t *)&env
->fpr
[i
]));
499 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
500 registers
[64] = tswap_abi(env
->y
);
505 registers
[65] = tswap32(tmp
);
507 registers
[66] = tswap_abi(env
->wim
);
508 registers
[67] = tswap_abi(env
->tbr
);
509 registers
[68] = tswap_abi(env
->pc
);
510 registers
[69] = tswap_abi(env
->npc
);
511 registers
[70] = tswap_abi(env
->fsr
);
512 registers
[71] = 0; /* csr */
514 return 73 * sizeof(uint32_t);
517 for (i
= 0; i
< 64; i
+= 2) {
520 tmp
= ((uint64_t)*(uint32_t *)&env
->fpr
[i
]) << 32;
521 tmp
|= *(uint32_t *)&env
->fpr
[i
+ 1];
522 registers
[i
/ 2 + 32] = tswap64(tmp
);
524 registers
[64] = tswapl(env
->pc
);
525 registers
[65] = tswapl(env
->npc
);
526 registers
[66] = tswapl(((uint64_t)GET_CCR(env
) << 32) |
527 ((env
->asi
& 0xff) << 24) |
528 ((env
->pstate
& 0xfff) << 8) |
530 registers
[67] = tswapl(env
->fsr
);
531 registers
[68] = tswapl(env
->fprs
);
532 registers
[69] = tswapl(env
->y
);
533 return 70 * sizeof(target_ulong
);
537 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
540 abi_ulong
*registers
= (abi_ulong
*)mem_buf
;
542 target_ulong
*registers
= (target_ulong
*)mem_buf
;
547 for(i
= 0; i
< 7; i
++) {
548 env
->gregs
[i
] = tswap_abi(registers
[i
]);
550 /* fill in register window */
551 for(i
= 0; i
< 24; i
++) {
552 env
->regwptr
[i
] = tswap_abi(registers
[i
+ 8]);
554 #if !defined(TARGET_SPARC64) || defined(TARGET_ABI32)
556 for (i
= 0; i
< 32; i
++) {
557 *((uint32_t *)&env
->fpr
[i
]) = tswap_abi(registers
[i
+ 32]);
559 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
560 env
->y
= tswap_abi(registers
[64]);
561 PUT_PSR(env
, tswap_abi(registers
[65]));
562 env
->wim
= tswap_abi(registers
[66]);
563 env
->tbr
= tswap_abi(registers
[67]);
564 env
->pc
= tswap_abi(registers
[68]);
565 env
->npc
= tswap_abi(registers
[69]);
566 env
->fsr
= tswap_abi(registers
[70]);
568 for (i
= 0; i
< 64; i
+= 2) {
571 tmp
= tswap64(registers
[i
/ 2 + 32]);
572 *((uint32_t *)&env
->fpr
[i
]) = tmp
>> 32;
573 *((uint32_t *)&env
->fpr
[i
+ 1]) = tmp
& 0xffffffff;
575 env
->pc
= tswapl(registers
[64]);
576 env
->npc
= tswapl(registers
[65]);
578 uint64_t tmp
= tswapl(registers
[66]);
580 PUT_CCR(env
, tmp
>> 32);
581 env
->asi
= (tmp
>> 24) & 0xff;
582 env
->pstate
= (tmp
>> 8) & 0xfff;
583 PUT_CWP64(env
, tmp
& 0xff);
585 env
->fsr
= tswapl(registers
[67]);
586 env
->fprs
= tswapl(registers
[68]);
587 env
->y
= tswapl(registers
[69]);
591 #elif defined (TARGET_ARM)
592 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
598 /* 16 core integer registers (4 bytes each). */
599 for (i
= 0; i
< 16; i
++)
601 *(uint32_t *)ptr
= tswapl(env
->regs
[i
]);
604 /* 8 FPA registers (12 bytes each), FPS (4 bytes).
605 Not yet implemented. */
606 memset (ptr
, 0, 8 * 12 + 4);
608 /* CPSR (4 bytes). */
609 *(uint32_t *)ptr
= tswapl (cpsr_read(env
));
612 return ptr
- mem_buf
;
615 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
621 /* Core integer registers. */
622 for (i
= 0; i
< 16; i
++)
624 env
->regs
[i
] = tswapl(*(uint32_t *)ptr
);
627 /* Ignore FPA regs and scr. */
629 cpsr_write (env
, tswapl(*(uint32_t *)ptr
), 0xffffffff);
631 #elif defined (TARGET_M68K)
632 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
640 for (i
= 0; i
< 8; i
++) {
641 *(uint32_t *)ptr
= tswapl(env
->dregs
[i
]);
645 for (i
= 0; i
< 8; i
++) {
646 *(uint32_t *)ptr
= tswapl(env
->aregs
[i
]);
649 *(uint32_t *)ptr
= tswapl(env
->sr
);
651 *(uint32_t *)ptr
= tswapl(env
->pc
);
653 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
654 ColdFire has 8-bit double precision registers. */
655 for (i
= 0; i
< 8; i
++) {
657 *(uint32_t *)ptr
= tswap32(u
.l
.upper
);
658 *(uint32_t *)ptr
= tswap32(u
.l
.lower
);
660 /* FP control regs (not implemented). */
661 memset (ptr
, 0, 3 * 4);
664 return ptr
- mem_buf
;
667 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
675 for (i
= 0; i
< 8; i
++) {
676 env
->dregs
[i
] = tswapl(*(uint32_t *)ptr
);
680 for (i
= 0; i
< 8; i
++) {
681 env
->aregs
[i
] = tswapl(*(uint32_t *)ptr
);
684 env
->sr
= tswapl(*(uint32_t *)ptr
);
686 env
->pc
= tswapl(*(uint32_t *)ptr
);
688 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
689 ColdFire has 8-bit double precision registers. */
690 for (i
= 0; i
< 8; i
++) {
691 u
.l
.upper
= tswap32(*(uint32_t *)ptr
);
692 u
.l
.lower
= tswap32(*(uint32_t *)ptr
);
695 /* FP control regs (not implemented). */
698 #elif defined (TARGET_MIPS)
699 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
705 for (i
= 0; i
< 32; i
++)
707 *(target_ulong
*)ptr
= tswapl(env
->active_tc
.gpr
[i
]);
708 ptr
+= sizeof(target_ulong
);
711 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_Status
);
712 ptr
+= sizeof(target_ulong
);
714 *(target_ulong
*)ptr
= tswapl(env
->active_tc
.LO
[0]);
715 ptr
+= sizeof(target_ulong
);
717 *(target_ulong
*)ptr
= tswapl(env
->active_tc
.HI
[0]);
718 ptr
+= sizeof(target_ulong
);
720 *(target_ulong
*)ptr
= tswapl(env
->CP0_BadVAddr
);
721 ptr
+= sizeof(target_ulong
);
723 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_Cause
);
724 ptr
+= sizeof(target_ulong
);
726 *(target_ulong
*)ptr
= tswapl(env
->active_tc
.PC
);
727 ptr
+= sizeof(target_ulong
);
729 if (env
->CP0_Config1
& (1 << CP0C1_FP
))
731 for (i
= 0; i
< 32; i
++)
733 if (env
->CP0_Status
& (1 << CP0St_FR
))
734 *(target_ulong
*)ptr
= tswapl(env
->active_fpu
.fpr
[i
].d
);
736 *(target_ulong
*)ptr
= tswap32(env
->active_fpu
.fpr
[i
].w
[FP_ENDIAN_IDX
]);
737 ptr
+= sizeof(target_ulong
);
740 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->active_fpu
.fcr31
);
741 ptr
+= sizeof(target_ulong
);
743 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->active_fpu
.fcr0
);
744 ptr
+= sizeof(target_ulong
);
747 /* "fp", pseudo frame pointer. Not yet implemented in gdb. */
748 *(target_ulong
*)ptr
= 0;
749 ptr
+= sizeof(target_ulong
);
751 /* Registers for embedded use, we just pad them. */
752 for (i
= 0; i
< 16; i
++)
754 *(target_ulong
*)ptr
= 0;
755 ptr
+= sizeof(target_ulong
);
759 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_PRid
);
760 ptr
+= sizeof(target_ulong
);
762 return ptr
- mem_buf
;
765 /* convert MIPS rounding mode in FCR31 to IEEE library */
766 static unsigned int ieee_rm
[] =
768 float_round_nearest_even
,
773 #define RESTORE_ROUNDING_MODE \
774 set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
776 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
782 for (i
= 0; i
< 32; i
++)
784 env
->active_tc
.gpr
[i
] = tswapl(*(target_ulong
*)ptr
);
785 ptr
+= sizeof(target_ulong
);
788 env
->CP0_Status
= tswapl(*(target_ulong
*)ptr
);
789 ptr
+= sizeof(target_ulong
);
791 env
->active_tc
.LO
[0] = tswapl(*(target_ulong
*)ptr
);
792 ptr
+= sizeof(target_ulong
);
794 env
->active_tc
.HI
[0] = tswapl(*(target_ulong
*)ptr
);
795 ptr
+= sizeof(target_ulong
);
797 env
->CP0_BadVAddr
= tswapl(*(target_ulong
*)ptr
);
798 ptr
+= sizeof(target_ulong
);
800 env
->CP0_Cause
= tswapl(*(target_ulong
*)ptr
);
801 ptr
+= sizeof(target_ulong
);
803 env
->active_tc
.PC
= tswapl(*(target_ulong
*)ptr
);
804 ptr
+= sizeof(target_ulong
);
806 if (env
->CP0_Config1
& (1 << CP0C1_FP
))
808 for (i
= 0; i
< 32; i
++)
810 if (env
->CP0_Status
& (1 << CP0St_FR
))
811 env
->active_fpu
.fpr
[i
].d
= tswapl(*(target_ulong
*)ptr
);
813 env
->active_fpu
.fpr
[i
].w
[FP_ENDIAN_IDX
] = tswapl(*(target_ulong
*)ptr
);
814 ptr
+= sizeof(target_ulong
);
817 env
->active_fpu
.fcr31
= tswapl(*(target_ulong
*)ptr
) & 0xFF83FFFF;
818 ptr
+= sizeof(target_ulong
);
820 /* The remaining registers are assumed to be read-only. */
822 /* set rounding mode */
823 RESTORE_ROUNDING_MODE
;
825 #ifndef CONFIG_SOFTFLOAT
826 /* no floating point exception for native float */
827 SET_FP_ENABLE(env
->fcr31
, 0);
831 #elif defined (TARGET_SH4)
833 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
835 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
837 uint32_t *ptr
= (uint32_t *)mem_buf
;
840 #define SAVE(x) *ptr++=tswapl(x)
841 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
842 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
+ 16]);
844 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
]);
846 for (i
= 8; i
< 16; i
++) SAVE(env
->gregs
[i
]);
856 for (i
= 0; i
< 16; i
++)
857 SAVE(env
->fregs
[i
+ ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
860 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
]);
861 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
+ 16]);
862 return ((uint8_t *)ptr
- mem_buf
);
865 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
867 uint32_t *ptr
= (uint32_t *)mem_buf
;
870 #define LOAD(x) (x)=*ptr++;
871 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
872 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
+ 16]);
874 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
]);
876 for (i
= 8; i
< 16; i
++) LOAD(env
->gregs
[i
]);
886 for (i
= 0; i
< 16; i
++)
887 LOAD(env
->fregs
[i
+ ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
890 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
]);
891 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
+ 16]);
893 #elif defined (TARGET_CRIS)
895 static int cris_save_32 (unsigned char *d
, uint32_t value
)
898 *d
++ = (value
>>= 8);
899 *d
++ = (value
>>= 8);
900 *d
++ = (value
>>= 8);
903 static int cris_save_16 (unsigned char *d
, uint32_t value
)
906 *d
++ = (value
>>= 8);
909 static int cris_save_8 (unsigned char *d
, uint32_t value
)
915 /* FIXME: this will bug on archs not supporting unaligned word accesses. */
916 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
918 uint8_t *ptr
= mem_buf
;
922 for (i
= 0; i
< 16; i
++)
923 ptr
+= cris_save_32 (ptr
, env
->regs
[i
]);
925 srs
= env
->pregs
[PR_SRS
];
927 ptr
+= cris_save_8 (ptr
, env
->pregs
[0]);
928 ptr
+= cris_save_8 (ptr
, env
->pregs
[1]);
929 ptr
+= cris_save_32 (ptr
, env
->pregs
[2]);
930 ptr
+= cris_save_8 (ptr
, srs
);
931 ptr
+= cris_save_16 (ptr
, env
->pregs
[4]);
933 for (i
= 5; i
< 16; i
++)
934 ptr
+= cris_save_32 (ptr
, env
->pregs
[i
]);
936 ptr
+= cris_save_32 (ptr
, env
->pc
);
938 for (i
= 0; i
< 16; i
++)
939 ptr
+= cris_save_32 (ptr
, env
->sregs
[srs
][i
]);
941 return ((uint8_t *)ptr
- mem_buf
);
944 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
946 uint32_t *ptr
= (uint32_t *)mem_buf
;
949 #define LOAD(x) (x)=*ptr++;
950 for (i
= 0; i
< 16; i
++) LOAD(env
->regs
[i
]);
954 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
959 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
965 static int gdb_handle_packet(GDBState
*s
, CPUState
*env
, const char *line_buf
)
968 int ch
, reg_size
, type
;
970 uint8_t mem_buf
[4096];
972 target_ulong addr
, len
;
975 printf("command='%s'\n", line_buf
);
981 /* TODO: Make this return the correct value for user-mode. */
982 snprintf(buf
, sizeof(buf
), "S%02x", SIGTRAP
);
984 /* Remove all the breakpoints when this query is issued,
985 * because gdb is doing and initial connect and the state
986 * should be cleaned up.
988 cpu_breakpoint_remove_all(env
);
989 cpu_watchpoint_remove_all(env
);
993 addr
= strtoull(p
, (char **)&p
, 16);
994 #if defined(TARGET_I386)
996 #elif defined (TARGET_PPC)
998 #elif defined (TARGET_SPARC)
1000 env
->npc
= addr
+ 4;
1001 #elif defined (TARGET_ARM)
1002 env
->regs
[15] = addr
;
1003 #elif defined (TARGET_SH4)
1005 #elif defined (TARGET_MIPS)
1006 env
->active_tc
.PC
= addr
;
1007 #elif defined (TARGET_CRIS)
1014 s
->signal
= strtoul(p
, (char **)&p
, 16);
1018 /* Kill the target */
1019 fprintf(stderr
, "\nQEMU: Terminated via GDBstub\n");
1023 cpu_breakpoint_remove_all(env
);
1024 cpu_watchpoint_remove_all(env
);
1026 put_packet(s
, "OK");
1030 addr
= strtoull(p
, (char **)&p
, 16);
1031 #if defined(TARGET_I386)
1033 #elif defined (TARGET_PPC)
1035 #elif defined (TARGET_SPARC)
1037 env
->npc
= addr
+ 4;
1038 #elif defined (TARGET_ARM)
1039 env
->regs
[15] = addr
;
1040 #elif defined (TARGET_SH4)
1042 #elif defined (TARGET_MIPS)
1043 env
->active_tc
.PC
= addr
;
1044 #elif defined (TARGET_CRIS)
1048 cpu_single_step(env
, sstep_flags
);
1056 ret
= strtoull(p
, (char **)&p
, 16);
1059 err
= strtoull(p
, (char **)&p
, 16);
1066 if (gdb_current_syscall_cb
)
1067 gdb_current_syscall_cb(s
->env
, ret
, err
);
1069 put_packet(s
, "T02");
1076 reg_size
= cpu_gdb_read_registers(env
, mem_buf
);
1077 memtohex(buf
, mem_buf
, reg_size
);
1081 registers
= (void *)mem_buf
;
1082 len
= strlen(p
) / 2;
1083 hextomem((uint8_t *)registers
, p
, len
);
1084 cpu_gdb_write_registers(env
, mem_buf
, len
);
1085 put_packet(s
, "OK");
1088 addr
= strtoull(p
, (char **)&p
, 16);
1091 len
= strtoull(p
, NULL
, 16);
1092 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 0) != 0) {
1093 put_packet (s
, "E14");
1095 memtohex(buf
, mem_buf
, len
);
1100 addr
= strtoull(p
, (char **)&p
, 16);
1103 len
= strtoull(p
, (char **)&p
, 16);
1106 hextomem(mem_buf
, p
, len
);
1107 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 1) != 0)
1108 put_packet(s
, "E14");
1110 put_packet(s
, "OK");
1113 type
= strtoul(p
, (char **)&p
, 16);
1116 addr
= strtoull(p
, (char **)&p
, 16);
1119 len
= strtoull(p
, (char **)&p
, 16);
1123 if (cpu_breakpoint_insert(env
, addr
) < 0)
1124 goto breakpoint_error
;
1125 put_packet(s
, "OK");
1127 #ifndef CONFIG_USER_ONLY
1130 goto insert_watchpoint
;
1133 goto insert_watchpoint
;
1135 type
= PAGE_READ
| PAGE_WRITE
;
1137 if (cpu_watchpoint_insert(env
, addr
, type
) < 0)
1138 goto breakpoint_error
;
1139 put_packet(s
, "OK");
1148 put_packet(s
, "E22");
1152 type
= strtoul(p
, (char **)&p
, 16);
1155 addr
= strtoull(p
, (char **)&p
, 16);
1158 len
= strtoull(p
, (char **)&p
, 16);
1159 if (type
== 0 || type
== 1) {
1160 cpu_breakpoint_remove(env
, addr
);
1161 put_packet(s
, "OK");
1162 #ifndef CONFIG_USER_ONLY
1163 } else if (type
>= 2 || type
<= 4) {
1164 cpu_watchpoint_remove(env
, addr
);
1165 put_packet(s
, "OK");
1173 /* parse any 'q' packets here */
1174 if (!strcmp(p
,"qemu.sstepbits")) {
1175 /* Query Breakpoint bit definitions */
1176 snprintf(buf
, sizeof(buf
), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1182 } else if (strncmp(p
,"qemu.sstep",10) == 0) {
1183 /* Display or change the sstep_flags */
1186 /* Display current setting */
1187 snprintf(buf
, sizeof(buf
), "0x%x", sstep_flags
);
1192 type
= strtoul(p
, (char **)&p
, 16);
1194 put_packet(s
, "OK");
1197 #ifdef CONFIG_LINUX_USER
1198 else if (strncmp(p
, "Offsets", 7) == 0) {
1199 TaskState
*ts
= env
->opaque
;
1201 snprintf(buf
, sizeof(buf
),
1202 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1203 ";Bss=" TARGET_ABI_FMT_lx
,
1204 ts
->info
->code_offset
,
1205 ts
->info
->data_offset
,
1206 ts
->info
->data_offset
);
1213 /* put empty packet */
1221 extern void tb_flush(CPUState
*env
);
1223 #ifndef CONFIG_USER_ONLY
1224 static void gdb_vm_stopped(void *opaque
, int reason
)
1226 GDBState
*s
= opaque
;
1230 if (s
->state
== RS_SYSCALL
)
1233 /* disable single step if it was enable */
1234 cpu_single_step(s
->env
, 0);
1236 if (reason
== EXCP_DEBUG
) {
1237 if (s
->env
->watchpoint_hit
) {
1238 snprintf(buf
, sizeof(buf
), "T%02xwatch:" TARGET_FMT_lx
";",
1240 s
->env
->watchpoint
[s
->env
->watchpoint_hit
- 1].vaddr
);
1242 s
->env
->watchpoint_hit
= 0;
1247 } else if (reason
== EXCP_INTERRUPT
) {
1252 snprintf(buf
, sizeof(buf
), "S%02x", ret
);
1257 /* Send a gdb syscall request.
1258 This accepts limited printf-style format specifiers, specifically:
1259 %x - target_ulong argument printed in hex.
1260 %lx - 64-bit argument printed in hex.
1261 %s - string pointer (target_ulong) and length (int) pair. */
1262 void gdb_do_syscall(gdb_syscall_complete_cb cb
, const char *fmt
, ...)
1271 s
= gdb_syscall_state
;
1274 gdb_current_syscall_cb
= cb
;
1275 s
->state
= RS_SYSCALL
;
1276 #ifndef CONFIG_USER_ONLY
1277 vm_stop(EXCP_DEBUG
);
1288 addr
= va_arg(va
, target_ulong
);
1289 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
, addr
);
1292 if (*(fmt
++) != 'x')
1294 i64
= va_arg(va
, uint64_t);
1295 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, "%" PRIx64
, i64
);
1298 addr
= va_arg(va
, target_ulong
);
1299 p
+= snprintf(p
, &buf
[sizeof(buf
)] - p
, TARGET_FMT_lx
"/%x",
1300 addr
, va_arg(va
, int));
1304 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
1315 #ifdef CONFIG_USER_ONLY
1316 gdb_handlesig(s
->env
, 0);
1318 cpu_interrupt(s
->env
, CPU_INTERRUPT_EXIT
);
1322 static void gdb_read_byte(GDBState
*s
, int ch
)
1324 CPUState
*env
= s
->env
;
1328 #ifndef CONFIG_USER_ONLY
1329 if (s
->last_packet_len
) {
1330 /* Waiting for a response to the last packet. If we see the start
1331 of a new command then abandon the previous response. */
1334 printf("Got NACK, retransmitting\n");
1336 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
1340 printf("Got ACK\n");
1342 printf("Got '%c' when expecting ACK/NACK\n", ch
);
1344 if (ch
== '+' || ch
== '$')
1345 s
->last_packet_len
= 0;
1350 /* when the CPU is running, we cannot do anything except stop
1351 it when receiving a char */
1352 vm_stop(EXCP_INTERRUPT
);
1359 s
->line_buf_index
= 0;
1360 s
->state
= RS_GETLINE
;
1365 s
->state
= RS_CHKSUM1
;
1366 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
1369 s
->line_buf
[s
->line_buf_index
++] = ch
;
1373 s
->line_buf
[s
->line_buf_index
] = '\0';
1374 s
->line_csum
= fromhex(ch
) << 4;
1375 s
->state
= RS_CHKSUM2
;
1378 s
->line_csum
|= fromhex(ch
);
1380 for(i
= 0; i
< s
->line_buf_index
; i
++) {
1381 csum
+= s
->line_buf
[i
];
1383 if (s
->line_csum
!= (csum
& 0xff)) {
1385 put_buffer(s
, &reply
, 1);
1389 put_buffer(s
, &reply
, 1);
1390 s
->state
= gdb_handle_packet(s
, env
, s
->line_buf
);
1399 #ifdef CONFIG_USER_ONLY
1401 gdb_handlesig (CPUState
*env
, int sig
)
1407 s
= &gdbserver_state
;
1408 if (gdbserver_fd
< 0 || s
->fd
< 0)
1411 /* disable single step if it was enabled */
1412 cpu_single_step(env
, 0);
1417 snprintf(buf
, sizeof(buf
), "S%02x", sig
);
1420 /* put_packet() might have detected that the peer terminated the
1427 s
->running_state
= 0;
1428 while (s
->running_state
== 0) {
1429 n
= read (s
->fd
, buf
, 256);
1434 for (i
= 0; i
< n
; i
++)
1435 gdb_read_byte (s
, buf
[i
]);
1437 else if (n
== 0 || errno
!= EAGAIN
)
1439 /* XXX: Connection closed. Should probably wait for annother
1440 connection before continuing. */
1449 /* Tell the remote gdb that the process has exited. */
1450 void gdb_exit(CPUState
*env
, int code
)
1455 s
= &gdbserver_state
;
1456 if (gdbserver_fd
< 0 || s
->fd
< 0)
1459 snprintf(buf
, sizeof(buf
), "W%02x", code
);
1464 static void gdb_accept(void *opaque
)
1467 struct sockaddr_in sockaddr
;
1472 len
= sizeof(sockaddr
);
1473 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
1474 if (fd
< 0 && errno
!= EINTR
) {
1477 } else if (fd
>= 0) {
1482 /* set short latency */
1484 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1486 s
= &gdbserver_state
;
1487 memset (s
, 0, sizeof (GDBState
));
1488 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1491 gdb_syscall_state
= s
;
1493 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1496 static int gdbserver_open(int port
)
1498 struct sockaddr_in sockaddr
;
1501 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1507 /* allow fast reuse */
1509 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
1511 sockaddr
.sin_family
= AF_INET
;
1512 sockaddr
.sin_port
= htons(port
);
1513 sockaddr
.sin_addr
.s_addr
= 0;
1514 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
1519 ret
= listen(fd
, 0);
1527 int gdbserver_start(int port
)
1529 gdbserver_fd
= gdbserver_open(port
);
1530 if (gdbserver_fd
< 0)
1532 /* accept connections */
1537 static int gdb_chr_can_receive(void *opaque
)
1542 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
1544 GDBState
*s
= opaque
;
1547 for (i
= 0; i
< size
; i
++) {
1548 gdb_read_byte(s
, buf
[i
]);
1552 static void gdb_chr_event(void *opaque
, int event
)
1555 case CHR_EVENT_RESET
:
1556 vm_stop(EXCP_INTERRUPT
);
1557 gdb_syscall_state
= opaque
;
1564 int gdbserver_start(const char *port
)
1567 char gdbstub_port_name
[128];
1570 CharDriverState
*chr
;
1572 if (!port
|| !*port
)
1575 port_num
= strtol(port
, &p
, 10);
1577 /* A numeric value is interpreted as a port number. */
1578 snprintf(gdbstub_port_name
, sizeof(gdbstub_port_name
),
1579 "tcp::%d,nowait,nodelay,server", port_num
);
1580 port
= gdbstub_port_name
;
1583 chr
= qemu_chr_open(port
);
1587 s
= qemu_mallocz(sizeof(GDBState
));
1591 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1593 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
1595 qemu_add_vm_stop_handler(gdb_vm_stopped
, s
);