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
, 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)
472 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
474 target_ulong
*registers
= (target_ulong
*)mem_buf
;
478 for(i
= 0; i
< 8; i
++) {
479 registers
[i
] = tswapl(env
->gregs
[i
]);
481 /* fill in register window */
482 for(i
= 0; i
< 24; i
++) {
483 registers
[i
+ 8] = tswapl(env
->regwptr
[i
]);
485 #ifndef TARGET_SPARC64
487 for (i
= 0; i
< 32; i
++) {
488 registers
[i
+ 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
490 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
491 registers
[64] = tswapl(env
->y
);
496 registers
[65] = tswapl(tmp
);
498 registers
[66] = tswapl(env
->wim
);
499 registers
[67] = tswapl(env
->tbr
);
500 registers
[68] = tswapl(env
->pc
);
501 registers
[69] = tswapl(env
->npc
);
502 registers
[70] = tswapl(env
->fsr
);
503 registers
[71] = 0; /* csr */
505 return 73 * sizeof(target_ulong
);
508 for (i
= 0; i
< 64; i
+= 2) {
511 tmp
= ((uint64_t)*(uint32_t *)&env
->fpr
[i
]) << 32;
512 tmp
|= *(uint32_t *)&env
->fpr
[i
+ 1];
513 registers
[i
/ 2 + 32] = tswap64(tmp
);
515 registers
[64] = tswapl(env
->pc
);
516 registers
[65] = tswapl(env
->npc
);
517 registers
[66] = tswapl(((uint64_t)GET_CCR(env
) << 32) |
518 ((env
->asi
& 0xff) << 24) |
519 ((env
->pstate
& 0xfff) << 8) |
521 registers
[67] = tswapl(env
->fsr
);
522 registers
[68] = tswapl(env
->fprs
);
523 registers
[69] = tswapl(env
->y
);
524 return 70 * sizeof(target_ulong
);
528 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
530 target_ulong
*registers
= (target_ulong
*)mem_buf
;
534 for(i
= 0; i
< 7; i
++) {
535 env
->gregs
[i
] = tswapl(registers
[i
]);
537 /* fill in register window */
538 for(i
= 0; i
< 24; i
++) {
539 env
->regwptr
[i
] = tswapl(registers
[i
+ 8]);
541 #ifndef TARGET_SPARC64
543 for (i
= 0; i
< 32; i
++) {
544 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[i
+ 32]);
546 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
547 env
->y
= tswapl(registers
[64]);
548 PUT_PSR(env
, tswapl(registers
[65]));
549 env
->wim
= tswapl(registers
[66]);
550 env
->tbr
= tswapl(registers
[67]);
551 env
->pc
= tswapl(registers
[68]);
552 env
->npc
= tswapl(registers
[69]);
553 env
->fsr
= tswapl(registers
[70]);
555 for (i
= 0; i
< 64; i
+= 2) {
558 tmp
= tswap64(registers
[i
/ 2 + 32]);
559 *((uint32_t *)&env
->fpr
[i
]) = tmp
>> 32;
560 *((uint32_t *)&env
->fpr
[i
+ 1]) = tmp
& 0xffffffff;
562 env
->pc
= tswapl(registers
[64]);
563 env
->npc
= tswapl(registers
[65]);
565 uint64_t tmp
= tswapl(registers
[66]);
567 PUT_CCR(env
, tmp
>> 32);
568 env
->asi
= (tmp
>> 24) & 0xff;
569 env
->pstate
= (tmp
>> 8) & 0xfff;
570 PUT_CWP64(env
, tmp
& 0xff);
572 env
->fsr
= tswapl(registers
[67]);
573 env
->fprs
= tswapl(registers
[68]);
574 env
->y
= tswapl(registers
[69]);
577 #elif defined (TARGET_ARM)
578 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
584 /* 16 core integer registers (4 bytes each). */
585 for (i
= 0; i
< 16; i
++)
587 *(uint32_t *)ptr
= tswapl(env
->regs
[i
]);
590 /* 8 FPA registers (12 bytes each), FPS (4 bytes).
591 Not yet implemented. */
592 memset (ptr
, 0, 8 * 12 + 4);
594 /* CPSR (4 bytes). */
595 *(uint32_t *)ptr
= tswapl (cpsr_read(env
));
598 return ptr
- mem_buf
;
601 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
607 /* Core integer registers. */
608 for (i
= 0; i
< 16; i
++)
610 env
->regs
[i
] = tswapl(*(uint32_t *)ptr
);
613 /* Ignore FPA regs and scr. */
615 cpsr_write (env
, tswapl(*(uint32_t *)ptr
), 0xffffffff);
617 #elif defined (TARGET_M68K)
618 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
626 for (i
= 0; i
< 8; i
++) {
627 *(uint32_t *)ptr
= tswapl(env
->dregs
[i
]);
631 for (i
= 0; i
< 8; i
++) {
632 *(uint32_t *)ptr
= tswapl(env
->aregs
[i
]);
635 *(uint32_t *)ptr
= tswapl(env
->sr
);
637 *(uint32_t *)ptr
= tswapl(env
->pc
);
639 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
640 ColdFire has 8-bit double precision registers. */
641 for (i
= 0; i
< 8; i
++) {
643 *(uint32_t *)ptr
= tswap32(u
.l
.upper
);
644 *(uint32_t *)ptr
= tswap32(u
.l
.lower
);
646 /* FP control regs (not implemented). */
647 memset (ptr
, 0, 3 * 4);
650 return ptr
- mem_buf
;
653 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
661 for (i
= 0; i
< 8; i
++) {
662 env
->dregs
[i
] = tswapl(*(uint32_t *)ptr
);
666 for (i
= 0; i
< 8; i
++) {
667 env
->aregs
[i
] = tswapl(*(uint32_t *)ptr
);
670 env
->sr
= tswapl(*(uint32_t *)ptr
);
672 env
->pc
= tswapl(*(uint32_t *)ptr
);
674 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
675 ColdFire has 8-bit double precision registers. */
676 for (i
= 0; i
< 8; i
++) {
677 u
.l
.upper
= tswap32(*(uint32_t *)ptr
);
678 u
.l
.lower
= tswap32(*(uint32_t *)ptr
);
681 /* FP control regs (not implemented). */
684 #elif defined (TARGET_MIPS)
685 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
691 for (i
= 0; i
< 32; i
++)
693 *(target_ulong
*)ptr
= tswapl(env
->gpr
[env
->current_tc
][i
]);
694 ptr
+= sizeof(target_ulong
);
697 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_Status
);
698 ptr
+= sizeof(target_ulong
);
700 *(target_ulong
*)ptr
= tswapl(env
->LO
[env
->current_tc
][0]);
701 ptr
+= sizeof(target_ulong
);
703 *(target_ulong
*)ptr
= tswapl(env
->HI
[env
->current_tc
][0]);
704 ptr
+= sizeof(target_ulong
);
706 *(target_ulong
*)ptr
= tswapl(env
->CP0_BadVAddr
);
707 ptr
+= sizeof(target_ulong
);
709 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_Cause
);
710 ptr
+= sizeof(target_ulong
);
712 *(target_ulong
*)ptr
= tswapl(env
->PC
[env
->current_tc
]);
713 ptr
+= sizeof(target_ulong
);
715 if (env
->CP0_Config1
& (1 << CP0C1_FP
))
717 for (i
= 0; i
< 32; i
++)
719 if (env
->CP0_Status
& (1 << CP0St_FR
))
720 *(target_ulong
*)ptr
= tswapl(env
->fpu
->fpr
[i
].d
);
722 *(target_ulong
*)ptr
= tswap32(env
->fpu
->fpr
[i
].w
[FP_ENDIAN_IDX
]);
723 ptr
+= sizeof(target_ulong
);
726 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->fpu
->fcr31
);
727 ptr
+= sizeof(target_ulong
);
729 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->fpu
->fcr0
);
730 ptr
+= sizeof(target_ulong
);
733 /* "fp", pseudo frame pointer. Not yet implemented in gdb. */
734 *(target_ulong
*)ptr
= 0;
735 ptr
+= sizeof(target_ulong
);
737 /* Registers for embedded use, we just pad them. */
738 for (i
= 0; i
< 16; i
++)
740 *(target_ulong
*)ptr
= 0;
741 ptr
+= sizeof(target_ulong
);
745 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_PRid
);
746 ptr
+= sizeof(target_ulong
);
748 return ptr
- mem_buf
;
751 /* convert MIPS rounding mode in FCR31 to IEEE library */
752 static unsigned int ieee_rm
[] =
754 float_round_nearest_even
,
759 #define RESTORE_ROUNDING_MODE \
760 set_float_rounding_mode(ieee_rm[env->fpu->fcr31 & 3], &env->fpu->fp_status)
762 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
768 for (i
= 0; i
< 32; i
++)
770 env
->gpr
[env
->current_tc
][i
] = tswapl(*(target_ulong
*)ptr
);
771 ptr
+= sizeof(target_ulong
);
774 env
->CP0_Status
= tswapl(*(target_ulong
*)ptr
);
775 ptr
+= sizeof(target_ulong
);
777 env
->LO
[env
->current_tc
][0] = tswapl(*(target_ulong
*)ptr
);
778 ptr
+= sizeof(target_ulong
);
780 env
->HI
[env
->current_tc
][0] = tswapl(*(target_ulong
*)ptr
);
781 ptr
+= sizeof(target_ulong
);
783 env
->CP0_BadVAddr
= tswapl(*(target_ulong
*)ptr
);
784 ptr
+= sizeof(target_ulong
);
786 env
->CP0_Cause
= tswapl(*(target_ulong
*)ptr
);
787 ptr
+= sizeof(target_ulong
);
789 env
->PC
[env
->current_tc
] = tswapl(*(target_ulong
*)ptr
);
790 ptr
+= sizeof(target_ulong
);
792 if (env
->CP0_Config1
& (1 << CP0C1_FP
))
794 for (i
= 0; i
< 32; i
++)
796 if (env
->CP0_Status
& (1 << CP0St_FR
))
797 env
->fpu
->fpr
[i
].d
= tswapl(*(target_ulong
*)ptr
);
799 env
->fpu
->fpr
[i
].w
[FP_ENDIAN_IDX
] = tswapl(*(target_ulong
*)ptr
);
800 ptr
+= sizeof(target_ulong
);
803 env
->fpu
->fcr31
= tswapl(*(target_ulong
*)ptr
) & 0xFF83FFFF;
804 ptr
+= sizeof(target_ulong
);
806 /* The remaining registers are assumed to be read-only. */
808 /* set rounding mode */
809 RESTORE_ROUNDING_MODE
;
811 #ifndef CONFIG_SOFTFLOAT
812 /* no floating point exception for native float */
813 SET_FP_ENABLE(env
->fcr31
, 0);
817 #elif defined (TARGET_SH4)
819 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
821 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
823 uint32_t *ptr
= (uint32_t *)mem_buf
;
826 #define SAVE(x) *ptr++=tswapl(x)
827 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
828 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
+ 16]);
830 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
]);
832 for (i
= 8; i
< 16; i
++) SAVE(env
->gregs
[i
]);
842 for (i
= 0; i
< 16; i
++)
843 SAVE(env
->fregs
[i
+ ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
846 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
]);
847 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
+ 16]);
848 return ((uint8_t *)ptr
- mem_buf
);
851 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
853 uint32_t *ptr
= (uint32_t *)mem_buf
;
856 #define LOAD(x) (x)=*ptr++;
857 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
858 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
+ 16]);
860 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
]);
862 for (i
= 8; i
< 16; i
++) LOAD(env
->gregs
[i
]);
872 for (i
= 0; i
< 16; i
++)
873 LOAD(env
->fregs
[i
+ ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
876 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
]);
877 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
+ 16]);
879 #elif defined (TARGET_CRIS)
881 static int cris_save_32 (unsigned char *d
, uint32_t value
)
884 *d
++ = (value
>>= 8);
885 *d
++ = (value
>>= 8);
886 *d
++ = (value
>>= 8);
889 static int cris_save_16 (unsigned char *d
, uint32_t value
)
892 *d
++ = (value
>>= 8);
895 static int cris_save_8 (unsigned char *d
, uint32_t value
)
901 /* FIXME: this will bug on archs not supporting unaligned word accesses. */
902 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
904 uint8_t *ptr
= mem_buf
;
908 for (i
= 0; i
< 16; i
++)
909 ptr
+= cris_save_32 (ptr
, env
->regs
[i
]);
911 srs
= env
->pregs
[PR_SRS
];
913 ptr
+= cris_save_8 (ptr
, env
->pregs
[0]);
914 ptr
+= cris_save_8 (ptr
, env
->pregs
[1]);
915 ptr
+= cris_save_32 (ptr
, env
->pregs
[2]);
916 ptr
+= cris_save_8 (ptr
, srs
);
917 ptr
+= cris_save_16 (ptr
, env
->pregs
[4]);
919 for (i
= 5; i
< 16; i
++)
920 ptr
+= cris_save_32 (ptr
, env
->pregs
[i
]);
922 ptr
+= cris_save_32 (ptr
, env
->pc
);
924 for (i
= 0; i
< 16; i
++)
925 ptr
+= cris_save_32 (ptr
, env
->sregs
[srs
][i
]);
927 return ((uint8_t *)ptr
- mem_buf
);
930 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
932 uint32_t *ptr
= (uint32_t *)mem_buf
;
935 #define LOAD(x) (x)=*ptr++;
936 for (i
= 0; i
< 16; i
++) LOAD(env
->regs
[i
]);
940 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
945 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
951 static int gdb_handle_packet(GDBState
*s
, CPUState
*env
, const char *line_buf
)
954 int ch
, reg_size
, type
;
956 uint8_t mem_buf
[4096];
958 target_ulong addr
, len
;
961 printf("command='%s'\n", line_buf
);
967 /* TODO: Make this return the correct value for user-mode. */
968 snprintf(buf
, sizeof(buf
), "S%02x", SIGTRAP
);
970 /* Remove all the breakpoints when this query is issued,
971 * because gdb is doing and initial connect and the state
972 * should be cleaned up.
974 cpu_breakpoint_remove_all(env
);
975 cpu_watchpoint_remove_all(env
);
979 addr
= strtoull(p
, (char **)&p
, 16);
980 #if defined(TARGET_I386)
982 #elif defined (TARGET_PPC)
984 #elif defined (TARGET_SPARC)
987 #elif defined (TARGET_ARM)
988 env
->regs
[15] = addr
;
989 #elif defined (TARGET_SH4)
991 #elif defined (TARGET_MIPS)
992 env
->PC
[env
->current_tc
] = addr
;
993 #elif defined (TARGET_CRIS)
1000 s
->signal
= strtoul(p
, (char **)&p
, 16);
1004 /* Kill the target */
1005 fprintf(stderr
, "\nQEMU: Terminated via GDBstub\n");
1009 cpu_breakpoint_remove_all(env
);
1010 cpu_watchpoint_remove_all(env
);
1012 put_packet(s
, "OK");
1016 addr
= strtoull(p
, (char **)&p
, 16);
1017 #if defined(TARGET_I386)
1019 #elif defined (TARGET_PPC)
1021 #elif defined (TARGET_SPARC)
1023 env
->npc
= addr
+ 4;
1024 #elif defined (TARGET_ARM)
1025 env
->regs
[15] = addr
;
1026 #elif defined (TARGET_SH4)
1028 #elif defined (TARGET_MIPS)
1029 env
->PC
[env
->current_tc
] = addr
;
1030 #elif defined (TARGET_CRIS)
1034 cpu_single_step(env
, sstep_flags
);
1042 ret
= strtoull(p
, (char **)&p
, 16);
1045 err
= strtoull(p
, (char **)&p
, 16);
1052 if (gdb_current_syscall_cb
)
1053 gdb_current_syscall_cb(s
->env
, ret
, err
);
1055 put_packet(s
, "T02");
1062 reg_size
= cpu_gdb_read_registers(env
, mem_buf
);
1063 memtohex(buf
, mem_buf
, reg_size
);
1067 registers
= (void *)mem_buf
;
1068 len
= strlen(p
) / 2;
1069 hextomem((uint8_t *)registers
, p
, len
);
1070 cpu_gdb_write_registers(env
, mem_buf
, len
);
1071 put_packet(s
, "OK");
1074 addr
= strtoull(p
, (char **)&p
, 16);
1077 len
= strtoull(p
, NULL
, 16);
1078 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 0) != 0) {
1079 put_packet (s
, "E14");
1081 memtohex(buf
, mem_buf
, len
);
1086 addr
= strtoull(p
, (char **)&p
, 16);
1089 len
= strtoull(p
, (char **)&p
, 16);
1092 hextomem(mem_buf
, p
, len
);
1093 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 1) != 0)
1094 put_packet(s
, "E14");
1096 put_packet(s
, "OK");
1099 type
= strtoul(p
, (char **)&p
, 16);
1102 addr
= strtoull(p
, (char **)&p
, 16);
1105 len
= strtoull(p
, (char **)&p
, 16);
1106 if (type
== 0 || type
== 1) {
1107 if (cpu_breakpoint_insert(env
, addr
) < 0)
1108 goto breakpoint_error
;
1109 put_packet(s
, "OK");
1110 #ifndef CONFIG_USER_ONLY
1111 } else if (type
== 2) {
1112 if (cpu_watchpoint_insert(env
, addr
) < 0)
1113 goto breakpoint_error
;
1114 put_packet(s
, "OK");
1118 put_packet(s
, "E22");
1122 type
= strtoul(p
, (char **)&p
, 16);
1125 addr
= strtoull(p
, (char **)&p
, 16);
1128 len
= strtoull(p
, (char **)&p
, 16);
1129 if (type
== 0 || type
== 1) {
1130 cpu_breakpoint_remove(env
, addr
);
1131 put_packet(s
, "OK");
1132 #ifndef CONFIG_USER_ONLY
1133 } else if (type
== 2) {
1134 cpu_watchpoint_remove(env
, addr
);
1135 put_packet(s
, "OK");
1138 goto breakpoint_error
;
1143 /* parse any 'q' packets here */
1144 if (!strcmp(p
,"qemu.sstepbits")) {
1145 /* Query Breakpoint bit definitions */
1146 sprintf(buf
,"ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1152 } else if (strncmp(p
,"qemu.sstep",10) == 0) {
1153 /* Display or change the sstep_flags */
1156 /* Display current setting */
1157 sprintf(buf
,"0x%x", sstep_flags
);
1162 type
= strtoul(p
, (char **)&p
, 16);
1164 put_packet(s
, "OK");
1167 #ifdef CONFIG_LINUX_USER
1168 else if (strncmp(p
, "Offsets", 7) == 0) {
1169 TaskState
*ts
= env
->opaque
;
1172 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1173 ";Bss=" TARGET_ABI_FMT_lx
,
1174 ts
->info
->code_offset
,
1175 ts
->info
->data_offset
,
1176 ts
->info
->data_offset
);
1183 /* put empty packet */
1191 extern void tb_flush(CPUState
*env
);
1193 #ifndef CONFIG_USER_ONLY
1194 static void gdb_vm_stopped(void *opaque
, int reason
)
1196 GDBState
*s
= opaque
;
1200 if (s
->state
== RS_SYSCALL
)
1203 /* disable single step if it was enable */
1204 cpu_single_step(s
->env
, 0);
1206 if (reason
== EXCP_DEBUG
) {
1207 if (s
->env
->watchpoint_hit
) {
1208 snprintf(buf
, sizeof(buf
), "T%02xwatch:" TARGET_FMT_lx
";",
1210 s
->env
->watchpoint
[s
->env
->watchpoint_hit
- 1].vaddr
);
1212 s
->env
->watchpoint_hit
= 0;
1217 } else if (reason
== EXCP_INTERRUPT
) {
1222 snprintf(buf
, sizeof(buf
), "S%02x", ret
);
1227 /* Send a gdb syscall request.
1228 This accepts limited printf-style format specifiers, specifically:
1229 %x - target_ulong argument printed in hex.
1230 %lx - 64-bit argument printed in hex.
1231 %s - string pointer (target_ulong) and length (int) pair. */
1232 void gdb_do_syscall(gdb_syscall_complete_cb cb
, char *fmt
, ...)
1241 s
= gdb_syscall_state
;
1244 gdb_current_syscall_cb
= cb
;
1245 s
->state
= RS_SYSCALL
;
1246 #ifndef CONFIG_USER_ONLY
1247 vm_stop(EXCP_DEBUG
);
1258 addr
= va_arg(va
, target_ulong
);
1259 p
+= sprintf(p
, TARGET_FMT_lx
, addr
);
1262 if (*(fmt
++) != 'x')
1264 i64
= va_arg(va
, uint64_t);
1265 p
+= sprintf(p
, "%" PRIx64
, i64
);
1268 addr
= va_arg(va
, target_ulong
);
1269 p
+= sprintf(p
, TARGET_FMT_lx
"/%x", addr
, va_arg(va
, int));
1273 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
1284 #ifdef CONFIG_USER_ONLY
1285 gdb_handlesig(s
->env
, 0);
1287 cpu_interrupt(s
->env
, CPU_INTERRUPT_EXIT
);
1291 static void gdb_read_byte(GDBState
*s
, int ch
)
1293 CPUState
*env
= s
->env
;
1297 #ifndef CONFIG_USER_ONLY
1298 if (s
->last_packet_len
) {
1299 /* Waiting for a response to the last packet. If we see the start
1300 of a new command then abandon the previous response. */
1303 printf("Got NACK, retransmitting\n");
1305 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
1309 printf("Got ACK\n");
1311 printf("Got '%c' when expecting ACK/NACK\n", ch
);
1313 if (ch
== '+' || ch
== '$')
1314 s
->last_packet_len
= 0;
1319 /* when the CPU is running, we cannot do anything except stop
1320 it when receiving a char */
1321 vm_stop(EXCP_INTERRUPT
);
1328 s
->line_buf_index
= 0;
1329 s
->state
= RS_GETLINE
;
1334 s
->state
= RS_CHKSUM1
;
1335 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
1338 s
->line_buf
[s
->line_buf_index
++] = ch
;
1342 s
->line_buf
[s
->line_buf_index
] = '\0';
1343 s
->line_csum
= fromhex(ch
) << 4;
1344 s
->state
= RS_CHKSUM2
;
1347 s
->line_csum
|= fromhex(ch
);
1349 for(i
= 0; i
< s
->line_buf_index
; i
++) {
1350 csum
+= s
->line_buf
[i
];
1352 if (s
->line_csum
!= (csum
& 0xff)) {
1354 put_buffer(s
, &reply
, 1);
1358 put_buffer(s
, &reply
, 1);
1359 s
->state
= gdb_handle_packet(s
, env
, s
->line_buf
);
1368 #ifdef CONFIG_USER_ONLY
1370 gdb_handlesig (CPUState
*env
, int sig
)
1376 s
= &gdbserver_state
;
1377 if (gdbserver_fd
< 0 || s
->fd
< 0)
1380 /* disable single step if it was enabled */
1381 cpu_single_step(env
, 0);
1386 snprintf(buf
, sizeof(buf
), "S%02x", sig
);
1389 /* put_packet() might have detected that the peer terminated the
1396 s
->running_state
= 0;
1397 while (s
->running_state
== 0) {
1398 n
= read (s
->fd
, buf
, 256);
1403 for (i
= 0; i
< n
; i
++)
1404 gdb_read_byte (s
, buf
[i
]);
1406 else if (n
== 0 || errno
!= EAGAIN
)
1408 /* XXX: Connection closed. Should probably wait for annother
1409 connection before continuing. */
1418 /* Tell the remote gdb that the process has exited. */
1419 void gdb_exit(CPUState
*env
, int code
)
1424 s
= &gdbserver_state
;
1425 if (gdbserver_fd
< 0 || s
->fd
< 0)
1428 snprintf(buf
, sizeof(buf
), "W%02x", code
);
1433 static void gdb_accept(void *opaque
)
1436 struct sockaddr_in sockaddr
;
1441 len
= sizeof(sockaddr
);
1442 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
1443 if (fd
< 0 && errno
!= EINTR
) {
1446 } else if (fd
>= 0) {
1451 /* set short latency */
1453 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1455 s
= &gdbserver_state
;
1456 memset (s
, 0, sizeof (GDBState
));
1457 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1460 gdb_syscall_state
= s
;
1462 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1465 static int gdbserver_open(int port
)
1467 struct sockaddr_in sockaddr
;
1470 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1476 /* allow fast reuse */
1478 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
1480 sockaddr
.sin_family
= AF_INET
;
1481 sockaddr
.sin_port
= htons(port
);
1482 sockaddr
.sin_addr
.s_addr
= 0;
1483 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
1488 ret
= listen(fd
, 0);
1496 int gdbserver_start(int port
)
1498 gdbserver_fd
= gdbserver_open(port
);
1499 if (gdbserver_fd
< 0)
1501 /* accept connections */
1506 static int gdb_chr_can_receive(void *opaque
)
1511 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
1513 GDBState
*s
= opaque
;
1516 for (i
= 0; i
< size
; i
++) {
1517 gdb_read_byte(s
, buf
[i
]);
1521 static void gdb_chr_event(void *opaque
, int event
)
1524 case CHR_EVENT_RESET
:
1525 vm_stop(EXCP_INTERRUPT
);
1526 gdb_syscall_state
= opaque
;
1533 int gdbserver_start(const char *port
)
1536 char gdbstub_port_name
[128];
1539 CharDriverState
*chr
;
1541 if (!port
|| !*port
)
1544 port_num
= strtol(port
, &p
, 10);
1546 /* A numeric value is interpreted as a port number. */
1547 snprintf(gdbstub_port_name
, sizeof(gdbstub_port_name
),
1548 "tcp::%d,nowait,nodelay,server", port_num
);
1549 port
= gdbstub_port_name
;
1552 chr
= qemu_chr_open(port
);
1556 s
= qemu_mallocz(sizeof(GDBState
));
1560 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1562 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
1564 qemu_add_vm_stop_handler(gdb_vm_stopped
, s
);