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];
68 #ifdef CONFIG_USER_ONLY
76 #ifdef CONFIG_USER_ONLY
77 /* XXX: This is not thread safe. Do we care? */
78 static int gdbserver_fd
= -1;
80 /* XXX: remove this hack. */
81 static GDBState gdbserver_state
;
83 static int get_char(GDBState
*s
)
89 ret
= recv(s
->fd
, &ch
, 1, 0);
91 if (errno
!= EINTR
&& errno
!= EAGAIN
)
93 } else if (ret
== 0) {
103 /* GDB stub state for use by semihosting syscalls. */
104 static GDBState
*gdb_syscall_state
;
105 static gdb_syscall_complete_cb gdb_current_syscall_cb
;
113 /* If gdb is connected when the first semihosting syscall occurs then use
114 remote gdb syscalls. Otherwise use native file IO. */
115 int use_gdb_syscalls(void)
117 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
118 gdb_syscall_mode
= (gdb_syscall_state
? GDB_SYS_ENABLED
121 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
124 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
126 #ifdef CONFIG_USER_ONLY
130 ret
= send(s
->fd
, buf
, len
, 0);
132 if (errno
!= EINTR
&& errno
!= EAGAIN
)
140 qemu_chr_write(s
->chr
, buf
, len
);
144 static inline int fromhex(int v
)
146 if (v
>= '0' && v
<= '9')
148 else if (v
>= 'A' && v
<= 'F')
150 else if (v
>= 'a' && v
<= 'f')
156 static inline int tohex(int v
)
164 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
169 for(i
= 0; i
< len
; i
++) {
171 *q
++ = tohex(c
>> 4);
172 *q
++ = tohex(c
& 0xf);
177 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
181 for(i
= 0; i
< len
; i
++) {
182 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
187 /* return -1 if error, 0 if OK */
188 static int put_packet(GDBState
*s
, char *buf
)
194 printf("reply='%s'\n", buf
);
204 for(i
= 0; i
< len
; i
++) {
208 *(p
++) = tohex((csum
>> 4) & 0xf);
209 *(p
++) = tohex((csum
) & 0xf);
211 s
->last_packet_len
= p
- s
->last_packet
;
212 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
214 #ifdef CONFIG_USER_ONLY
227 #if defined(TARGET_I386)
229 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
232 uint32_t *registers
= (uint32_t *)mem_buf
;
235 /* This corresponds with amd64_register_info[] in gdb/amd64-tdep.c */
236 uint64_t *registers64
= (uint64_t *)mem_buf
;
238 if (env
->hflags
& HF_CS64_MASK
) {
239 registers64
[0] = tswap64(env
->regs
[R_EAX
]);
240 registers64
[1] = tswap64(env
->regs
[R_EBX
]);
241 registers64
[2] = tswap64(env
->regs
[R_ECX
]);
242 registers64
[3] = tswap64(env
->regs
[R_EDX
]);
243 registers64
[4] = tswap64(env
->regs
[R_ESI
]);
244 registers64
[5] = tswap64(env
->regs
[R_EDI
]);
245 registers64
[6] = tswap64(env
->regs
[R_EBP
]);
246 registers64
[7] = tswap64(env
->regs
[R_ESP
]);
247 for(i
= 8; i
< 16; i
++) {
248 registers64
[i
] = tswap64(env
->regs
[i
]);
250 registers64
[16] = tswap64(env
->eip
);
252 registers
= (uint32_t *)®isters64
[17];
253 registers
[0] = tswap32(env
->eflags
);
254 registers
[1] = tswap32(env
->segs
[R_CS
].selector
);
255 registers
[2] = tswap32(env
->segs
[R_SS
].selector
);
256 registers
[3] = tswap32(env
->segs
[R_DS
].selector
);
257 registers
[4] = tswap32(env
->segs
[R_ES
].selector
);
258 registers
[5] = tswap32(env
->segs
[R_FS
].selector
);
259 registers
[6] = tswap32(env
->segs
[R_GS
].selector
);
260 /* XXX: convert floats */
261 for(i
= 0; i
< 8; i
++) {
262 memcpy(mem_buf
+ 16 * 8 + 7 * 4 + i
* 10, &env
->fpregs
[i
], 10);
264 registers
[27] = tswap32(env
->fpuc
); /* fctrl */
265 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
266 registers
[28] = tswap32(fpus
); /* fstat */
267 registers
[29] = 0; /* ftag */
268 registers
[30] = 0; /* fiseg */
269 registers
[31] = 0; /* fioff */
270 registers
[32] = 0; /* foseg */
271 registers
[33] = 0; /* fooff */
272 registers
[34] = 0; /* fop */
273 for(i
= 0; i
< 16; i
++) {
274 memcpy(mem_buf
+ 16 * 8 + 35 * 4 + i
* 16, &env
->xmm_regs
[i
], 16);
276 registers
[99] = tswap32(env
->mxcsr
);
278 return 8 * 17 + 4 * 7 + 10 * 8 + 4 * 8 + 16 * 16 + 4;
282 for(i
= 0; i
< 8; i
++) {
283 registers
[i
] = env
->regs
[i
];
285 registers
[8] = env
->eip
;
286 registers
[9] = env
->eflags
;
287 registers
[10] = env
->segs
[R_CS
].selector
;
288 registers
[11] = env
->segs
[R_SS
].selector
;
289 registers
[12] = env
->segs
[R_DS
].selector
;
290 registers
[13] = env
->segs
[R_ES
].selector
;
291 registers
[14] = env
->segs
[R_FS
].selector
;
292 registers
[15] = env
->segs
[R_GS
].selector
;
293 /* XXX: convert floats */
294 for(i
= 0; i
< 8; i
++) {
295 memcpy(mem_buf
+ 16 * 4 + i
* 10, &env
->fpregs
[i
], 10);
297 registers
[36] = env
->fpuc
;
298 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
299 registers
[37] = fpus
;
300 registers
[38] = 0; /* XXX: convert tags */
301 registers
[39] = 0; /* fiseg */
302 registers
[40] = 0; /* fioff */
303 registers
[41] = 0; /* foseg */
304 registers
[42] = 0; /* fooff */
305 registers
[43] = 0; /* fop */
307 for(i
= 0; i
< 16; i
++)
308 tswapls(®isters
[i
]);
309 for(i
= 36; i
< 44; i
++)
310 tswapls(®isters
[i
]);
314 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
316 uint32_t *registers
= (uint32_t *)mem_buf
;
319 for(i
= 0; i
< 8; i
++) {
320 env
->regs
[i
] = tswapl(registers
[i
]);
322 env
->eip
= tswapl(registers
[8]);
323 env
->eflags
= tswapl(registers
[9]);
324 #if defined(CONFIG_USER_ONLY)
325 #define LOAD_SEG(index, sreg)\
326 if (tswapl(registers[index]) != env->segs[sreg].selector)\
327 cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
337 #elif defined (TARGET_PPC)
338 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
340 uint32_t *registers
= (uint32_t *)mem_buf
, tmp
;
344 for(i
= 0; i
< 32; i
++) {
345 registers
[i
] = tswapl(env
->gpr
[i
]);
348 for (i
= 0; i
< 32; i
++) {
349 registers
[(i
* 2) + 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
350 registers
[(i
* 2) + 33] = tswapl(*((uint32_t *)&env
->fpr
[i
] + 1));
352 /* nip, msr, ccr, lnk, ctr, xer, mq */
353 registers
[96] = tswapl(env
->nip
);
354 registers
[97] = tswapl(env
->msr
);
356 for (i
= 0; i
< 8; i
++)
357 tmp
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
358 registers
[98] = tswapl(tmp
);
359 registers
[99] = tswapl(env
->lr
);
360 registers
[100] = tswapl(env
->ctr
);
361 registers
[101] = tswapl(ppc_load_xer(env
));
367 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
369 uint32_t *registers
= (uint32_t *)mem_buf
;
373 for (i
= 0; i
< 32; i
++) {
374 env
->gpr
[i
] = tswapl(registers
[i
]);
377 for (i
= 0; i
< 32; i
++) {
378 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[(i
* 2) + 32]);
379 *((uint32_t *)&env
->fpr
[i
] + 1) = tswapl(registers
[(i
* 2) + 33]);
381 /* nip, msr, ccr, lnk, ctr, xer, mq */
382 env
->nip
= tswapl(registers
[96]);
383 ppc_store_msr(env
, tswapl(registers
[97]));
384 registers
[98] = tswapl(registers
[98]);
385 for (i
= 0; i
< 8; i
++)
386 env
->crf
[i
] = (registers
[98] >> (32 - ((i
+ 1) * 4))) & 0xF;
387 env
->lr
= tswapl(registers
[99]);
388 env
->ctr
= tswapl(registers
[100]);
389 ppc_store_xer(env
, tswapl(registers
[101]));
391 #elif defined (TARGET_SPARC)
392 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
394 target_ulong
*registers
= (target_ulong
*)mem_buf
;
398 for(i
= 0; i
< 8; i
++) {
399 registers
[i
] = tswapl(env
->gregs
[i
]);
401 /* fill in register window */
402 for(i
= 0; i
< 24; i
++) {
403 registers
[i
+ 8] = tswapl(env
->regwptr
[i
]);
405 #ifndef TARGET_SPARC64
407 for (i
= 0; i
< 32; i
++) {
408 registers
[i
+ 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
410 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
411 registers
[64] = tswapl(env
->y
);
416 registers
[65] = tswapl(tmp
);
418 registers
[66] = tswapl(env
->wim
);
419 registers
[67] = tswapl(env
->tbr
);
420 registers
[68] = tswapl(env
->pc
);
421 registers
[69] = tswapl(env
->npc
);
422 registers
[70] = tswapl(env
->fsr
);
423 registers
[71] = 0; /* csr */
425 return 73 * sizeof(target_ulong
);
428 for (i
= 0; i
< 64; i
+= 2) {
431 tmp
= ((uint64_t)*(uint32_t *)&env
->fpr
[i
]) << 32;
432 tmp
|= *(uint32_t *)&env
->fpr
[i
+ 1];
433 registers
[i
/ 2 + 32] = tswap64(tmp
);
435 registers
[64] = tswapl(env
->pc
);
436 registers
[65] = tswapl(env
->npc
);
437 registers
[66] = tswapl(((uint64_t)GET_CCR(env
) << 32) |
438 ((env
->asi
& 0xff) << 24) |
439 ((env
->pstate
& 0xfff) << 8) |
441 registers
[67] = tswapl(env
->fsr
);
442 registers
[68] = tswapl(env
->fprs
);
443 registers
[69] = tswapl(env
->y
);
444 return 70 * sizeof(target_ulong
);
448 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
450 target_ulong
*registers
= (target_ulong
*)mem_buf
;
454 for(i
= 0; i
< 7; i
++) {
455 env
->gregs
[i
] = tswapl(registers
[i
]);
457 /* fill in register window */
458 for(i
= 0; i
< 24; i
++) {
459 env
->regwptr
[i
] = tswapl(registers
[i
+ 8]);
461 #ifndef TARGET_SPARC64
463 for (i
= 0; i
< 32; i
++) {
464 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[i
+ 32]);
466 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
467 env
->y
= tswapl(registers
[64]);
468 PUT_PSR(env
, tswapl(registers
[65]));
469 env
->wim
= tswapl(registers
[66]);
470 env
->tbr
= tswapl(registers
[67]);
471 env
->pc
= tswapl(registers
[68]);
472 env
->npc
= tswapl(registers
[69]);
473 env
->fsr
= tswapl(registers
[70]);
475 for (i
= 0; i
< 64; i
+= 2) {
478 tmp
= tswap64(registers
[i
/ 2 + 32]);
479 *((uint32_t *)&env
->fpr
[i
]) = tmp
>> 32;
480 *((uint32_t *)&env
->fpr
[i
+ 1]) = tmp
& 0xffffffff;
482 env
->pc
= tswapl(registers
[64]);
483 env
->npc
= tswapl(registers
[65]);
485 uint64_t tmp
= tswapl(registers
[66]);
487 PUT_CCR(env
, tmp
>> 32);
488 env
->asi
= (tmp
>> 24) & 0xff;
489 env
->pstate
= (tmp
>> 8) & 0xfff;
490 PUT_CWP64(env
, tmp
& 0xff);
492 env
->fsr
= tswapl(registers
[67]);
493 env
->fprs
= tswapl(registers
[68]);
494 env
->y
= tswapl(registers
[69]);
497 #elif defined (TARGET_ARM)
498 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
504 /* 16 core integer registers (4 bytes each). */
505 for (i
= 0; i
< 16; i
++)
507 *(uint32_t *)ptr
= tswapl(env
->regs
[i
]);
510 /* 8 FPA registers (12 bytes each), FPS (4 bytes).
511 Not yet implemented. */
512 memset (ptr
, 0, 8 * 12 + 4);
514 /* CPSR (4 bytes). */
515 *(uint32_t *)ptr
= tswapl (cpsr_read(env
));
518 return ptr
- mem_buf
;
521 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
527 /* Core integer registers. */
528 for (i
= 0; i
< 16; i
++)
530 env
->regs
[i
] = tswapl(*(uint32_t *)ptr
);
533 /* Ignore FPA regs and scr. */
535 cpsr_write (env
, tswapl(*(uint32_t *)ptr
), 0xffffffff);
537 #elif defined (TARGET_M68K)
538 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
546 for (i
= 0; i
< 8; i
++) {
547 *(uint32_t *)ptr
= tswapl(env
->dregs
[i
]);
551 for (i
= 0; i
< 8; i
++) {
552 *(uint32_t *)ptr
= tswapl(env
->aregs
[i
]);
555 *(uint32_t *)ptr
= tswapl(env
->sr
);
557 *(uint32_t *)ptr
= tswapl(env
->pc
);
559 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
560 ColdFire has 8-bit double precision registers. */
561 for (i
= 0; i
< 8; i
++) {
563 *(uint32_t *)ptr
= tswap32(u
.l
.upper
);
564 *(uint32_t *)ptr
= tswap32(u
.l
.lower
);
566 /* FP control regs (not implemented). */
567 memset (ptr
, 0, 3 * 4);
570 return ptr
- mem_buf
;
573 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
581 for (i
= 0; i
< 8; i
++) {
582 env
->dregs
[i
] = tswapl(*(uint32_t *)ptr
);
586 for (i
= 0; i
< 8; i
++) {
587 env
->aregs
[i
] = tswapl(*(uint32_t *)ptr
);
590 env
->sr
= tswapl(*(uint32_t *)ptr
);
592 env
->pc
= tswapl(*(uint32_t *)ptr
);
594 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
595 ColdFire has 8-bit double precision registers. */
596 for (i
= 0; i
< 8; i
++) {
597 u
.l
.upper
= tswap32(*(uint32_t *)ptr
);
598 u
.l
.lower
= tswap32(*(uint32_t *)ptr
);
601 /* FP control regs (not implemented). */
604 #elif defined (TARGET_MIPS)
605 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
611 for (i
= 0; i
< 32; i
++)
613 *(target_ulong
*)ptr
= tswapl(env
->gpr
[i
][env
->current_tc
]);
614 ptr
+= sizeof(target_ulong
);
617 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_Status
);
618 ptr
+= sizeof(target_ulong
);
620 *(target_ulong
*)ptr
= tswapl(env
->LO
[0][env
->current_tc
]);
621 ptr
+= sizeof(target_ulong
);
623 *(target_ulong
*)ptr
= tswapl(env
->HI
[0][env
->current_tc
]);
624 ptr
+= sizeof(target_ulong
);
626 *(target_ulong
*)ptr
= tswapl(env
->CP0_BadVAddr
);
627 ptr
+= sizeof(target_ulong
);
629 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_Cause
);
630 ptr
+= sizeof(target_ulong
);
632 *(target_ulong
*)ptr
= tswapl(env
->PC
[env
->current_tc
]);
633 ptr
+= sizeof(target_ulong
);
635 if (env
->CP0_Config1
& (1 << CP0C1_FP
))
637 for (i
= 0; i
< 32; i
++)
639 if (env
->CP0_Status
& (1 << CP0St_FR
))
640 *(target_ulong
*)ptr
= tswapl(env
->fpu
->fpr
[i
].d
);
642 *(target_ulong
*)ptr
= tswap32(env
->fpu
->fpr
[i
].w
[FP_ENDIAN_IDX
]);
643 ptr
+= sizeof(target_ulong
);
646 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->fpu
->fcr31
);
647 ptr
+= sizeof(target_ulong
);
649 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->fpu
->fcr0
);
650 ptr
+= sizeof(target_ulong
);
653 /* "fp", pseudo frame pointer. Not yet implemented in gdb. */
654 *(target_ulong
*)ptr
= 0;
655 ptr
+= sizeof(target_ulong
);
657 /* Registers for embedded use, we just pad them. */
658 for (i
= 0; i
< 16; i
++)
660 *(target_ulong
*)ptr
= 0;
661 ptr
+= sizeof(target_ulong
);
665 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_PRid
);
666 ptr
+= sizeof(target_ulong
);
668 return ptr
- mem_buf
;
671 /* convert MIPS rounding mode in FCR31 to IEEE library */
672 static unsigned int ieee_rm
[] =
674 float_round_nearest_even
,
679 #define RESTORE_ROUNDING_MODE \
680 set_float_rounding_mode(ieee_rm[env->fpu->fcr31 & 3], &env->fpu->fp_status)
682 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
688 for (i
= 0; i
< 32; i
++)
690 env
->gpr
[i
][env
->current_tc
] = tswapl(*(target_ulong
*)ptr
);
691 ptr
+= sizeof(target_ulong
);
694 env
->CP0_Status
= tswapl(*(target_ulong
*)ptr
);
695 ptr
+= sizeof(target_ulong
);
697 env
->LO
[0][env
->current_tc
] = tswapl(*(target_ulong
*)ptr
);
698 ptr
+= sizeof(target_ulong
);
700 env
->HI
[0][env
->current_tc
] = tswapl(*(target_ulong
*)ptr
);
701 ptr
+= sizeof(target_ulong
);
703 env
->CP0_BadVAddr
= tswapl(*(target_ulong
*)ptr
);
704 ptr
+= sizeof(target_ulong
);
706 env
->CP0_Cause
= tswapl(*(target_ulong
*)ptr
);
707 ptr
+= sizeof(target_ulong
);
709 env
->PC
[env
->current_tc
] = tswapl(*(target_ulong
*)ptr
);
710 ptr
+= sizeof(target_ulong
);
712 if (env
->CP0_Config1
& (1 << CP0C1_FP
))
714 for (i
= 0; i
< 32; i
++)
716 if (env
->CP0_Status
& (1 << CP0St_FR
))
717 env
->fpu
->fpr
[i
].d
= tswapl(*(target_ulong
*)ptr
);
719 env
->fpu
->fpr
[i
].w
[FP_ENDIAN_IDX
] = tswapl(*(target_ulong
*)ptr
);
720 ptr
+= sizeof(target_ulong
);
723 env
->fpu
->fcr31
= tswapl(*(target_ulong
*)ptr
) & 0xFF83FFFF;
724 ptr
+= sizeof(target_ulong
);
726 /* The remaining registers are assumed to be read-only. */
728 /* set rounding mode */
729 RESTORE_ROUNDING_MODE
;
731 #ifndef CONFIG_SOFTFLOAT
732 /* no floating point exception for native float */
733 SET_FP_ENABLE(env
->fcr31
, 0);
737 #elif defined (TARGET_SH4)
739 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
741 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
743 uint32_t *ptr
= (uint32_t *)mem_buf
;
746 #define SAVE(x) *ptr++=tswapl(x)
747 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
748 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
+ 16]);
750 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
]);
752 for (i
= 8; i
< 16; i
++) SAVE(env
->gregs
[i
]);
762 for (i
= 0; i
< 16; i
++)
763 SAVE(env
->fregs
[i
+ ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
766 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
]);
767 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
+ 16]);
768 return ((uint8_t *)ptr
- mem_buf
);
771 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
773 uint32_t *ptr
= (uint32_t *)mem_buf
;
776 #define LOAD(x) (x)=*ptr++;
777 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
778 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
+ 16]);
780 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
]);
782 for (i
= 8; i
< 16; i
++) LOAD(env
->gregs
[i
]);
792 for (i
= 0; i
< 16; i
++)
793 LOAD(env
->fregs
[i
+ ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
796 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
]);
797 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
+ 16]);
799 #elif defined (TARGET_CRIS)
801 static int cris_save_32 (unsigned char *d
, uint32_t value
)
804 *d
++ = (value
>>= 8);
805 *d
++ = (value
>>= 8);
806 *d
++ = (value
>>= 8);
809 static int cris_save_16 (unsigned char *d
, uint32_t value
)
812 *d
++ = (value
>>= 8);
815 static int cris_save_8 (unsigned char *d
, uint32_t value
)
821 /* FIXME: this will bug on archs not supporting unaligned word accesses. */
822 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
824 uint8_t *ptr
= mem_buf
;
828 for (i
= 0; i
< 16; i
++)
829 ptr
+= cris_save_32 (ptr
, env
->regs
[i
]);
831 srs
= env
->pregs
[SR_SRS
];
833 ptr
+= cris_save_8 (ptr
, env
->pregs
[0]);
834 ptr
+= cris_save_8 (ptr
, env
->pregs
[1]);
835 ptr
+= cris_save_32 (ptr
, env
->pregs
[2]);
836 ptr
+= cris_save_8 (ptr
, srs
);
837 ptr
+= cris_save_16 (ptr
, env
->pregs
[4]);
839 for (i
= 5; i
< 16; i
++)
840 ptr
+= cris_save_32 (ptr
, env
->pregs
[i
]);
842 ptr
+= cris_save_32 (ptr
, env
->pc
);
844 for (i
= 0; i
< 16; i
++)
845 ptr
+= cris_save_32 (ptr
, env
->sregs
[srs
][i
]);
847 return ((uint8_t *)ptr
- mem_buf
);
850 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
852 uint32_t *ptr
= (uint32_t *)mem_buf
;
855 #define LOAD(x) (x)=*ptr++;
856 for (i
= 0; i
< 16; i
++) LOAD(env
->regs
[i
]);
860 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
865 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
871 static int gdb_handle_packet(GDBState
*s
, CPUState
*env
, const char *line_buf
)
874 int ch
, reg_size
, type
;
876 uint8_t mem_buf
[4096];
878 target_ulong addr
, len
;
881 printf("command='%s'\n", line_buf
);
887 /* TODO: Make this return the correct value for user-mode. */
888 snprintf(buf
, sizeof(buf
), "S%02x", SIGTRAP
);
893 addr
= strtoull(p
, (char **)&p
, 16);
894 #if defined(TARGET_I386)
896 #elif defined (TARGET_PPC)
898 #elif defined (TARGET_SPARC)
901 #elif defined (TARGET_ARM)
902 env
->regs
[15] = addr
;
903 #elif defined (TARGET_SH4)
905 #elif defined (TARGET_MIPS)
906 env
->PC
[env
->current_tc
] = addr
;
907 #elif defined (TARGET_CRIS)
911 #ifdef CONFIG_USER_ONLY
912 s
->running_state
= 1;
919 addr
= strtoull(p
, (char **)&p
, 16);
920 #if defined(TARGET_I386)
922 #elif defined (TARGET_PPC)
924 #elif defined (TARGET_SPARC)
927 #elif defined (TARGET_ARM)
928 env
->regs
[15] = addr
;
929 #elif defined (TARGET_SH4)
931 #elif defined (TARGET_MIPS)
932 env
->PC
[env
->current_tc
] = addr
;
933 #elif defined (TARGET_CRIS)
937 cpu_single_step(env
, 1);
938 #ifdef CONFIG_USER_ONLY
939 s
->running_state
= 1;
949 ret
= strtoull(p
, (char **)&p
, 16);
952 err
= strtoull(p
, (char **)&p
, 16);
959 if (gdb_current_syscall_cb
)
960 gdb_current_syscall_cb(s
->env
, ret
, err
);
962 put_packet(s
, "T02");
964 #ifdef CONFIG_USER_ONLY
965 s
->running_state
= 1;
973 reg_size
= cpu_gdb_read_registers(env
, mem_buf
);
974 memtohex(buf
, mem_buf
, reg_size
);
978 registers
= (void *)mem_buf
;
980 hextomem((uint8_t *)registers
, p
, len
);
981 cpu_gdb_write_registers(env
, mem_buf
, len
);
985 addr
= strtoull(p
, (char **)&p
, 16);
988 len
= strtoull(p
, NULL
, 16);
989 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 0) != 0) {
990 put_packet (s
, "E14");
992 memtohex(buf
, mem_buf
, len
);
997 addr
= strtoull(p
, (char **)&p
, 16);
1000 len
= strtoull(p
, (char **)&p
, 16);
1003 hextomem(mem_buf
, p
, len
);
1004 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 1) != 0)
1005 put_packet(s
, "E14");
1007 put_packet(s
, "OK");
1010 type
= strtoul(p
, (char **)&p
, 16);
1013 addr
= strtoull(p
, (char **)&p
, 16);
1016 len
= strtoull(p
, (char **)&p
, 16);
1017 if (type
== 0 || type
== 1) {
1018 if (cpu_breakpoint_insert(env
, addr
) < 0)
1019 goto breakpoint_error
;
1020 put_packet(s
, "OK");
1021 #ifndef CONFIG_USER_ONLY
1022 } else if (type
== 2) {
1023 if (cpu_watchpoint_insert(env
, addr
) < 0)
1024 goto breakpoint_error
;
1025 put_packet(s
, "OK");
1029 put_packet(s
, "E22");
1033 type
= strtoul(p
, (char **)&p
, 16);
1036 addr
= strtoull(p
, (char **)&p
, 16);
1039 len
= strtoull(p
, (char **)&p
, 16);
1040 if (type
== 0 || type
== 1) {
1041 cpu_breakpoint_remove(env
, addr
);
1042 put_packet(s
, "OK");
1043 #ifndef CONFIG_USER_ONLY
1044 } else if (type
== 2) {
1045 cpu_watchpoint_remove(env
, addr
);
1046 put_packet(s
, "OK");
1049 goto breakpoint_error
;
1052 #ifdef CONFIG_LINUX_USER
1054 if (strncmp(p
, "Offsets", 7) == 0) {
1055 TaskState
*ts
= env
->opaque
;
1058 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1059 ";Bss=" TARGET_ABI_FMT_lx
,
1060 ts
->info
->code_offset
,
1061 ts
->info
->data_offset
,
1062 ts
->info
->data_offset
);
1070 /* put empty packet */
1078 extern void tb_flush(CPUState
*env
);
1080 #ifndef CONFIG_USER_ONLY
1081 static void gdb_vm_stopped(void *opaque
, int reason
)
1083 GDBState
*s
= opaque
;
1087 if (s
->state
== RS_SYSCALL
)
1090 /* disable single step if it was enable */
1091 cpu_single_step(s
->env
, 0);
1093 if (reason
== EXCP_DEBUG
) {
1094 if (s
->env
->watchpoint_hit
) {
1095 snprintf(buf
, sizeof(buf
), "T%02xwatch:" TARGET_FMT_lx
";",
1097 s
->env
->watchpoint
[s
->env
->watchpoint_hit
- 1].vaddr
);
1099 s
->env
->watchpoint_hit
= 0;
1104 } else if (reason
== EXCP_INTERRUPT
) {
1109 snprintf(buf
, sizeof(buf
), "S%02x", ret
);
1114 /* Send a gdb syscall request.
1115 This accepts limited printf-style format specifiers, specifically:
1116 %x - target_ulong argument printed in hex.
1117 %lx - 64-bit argument printed in hex.
1118 %s - string pointer (target_ulong) and length (int) pair. */
1119 void gdb_do_syscall(gdb_syscall_complete_cb cb
, char *fmt
, ...)
1128 s
= gdb_syscall_state
;
1131 gdb_current_syscall_cb
= cb
;
1132 s
->state
= RS_SYSCALL
;
1133 #ifndef CONFIG_USER_ONLY
1134 vm_stop(EXCP_DEBUG
);
1145 addr
= va_arg(va
, target_ulong
);
1146 p
+= sprintf(p
, TARGET_FMT_lx
, addr
);
1149 if (*(fmt
++) != 'x')
1151 i64
= va_arg(va
, uint64_t);
1152 p
+= sprintf(p
, "%" PRIx64
, i64
);
1155 addr
= va_arg(va
, target_ulong
);
1156 p
+= sprintf(p
, TARGET_FMT_lx
"/%x", addr
, va_arg(va
, int));
1160 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
1171 #ifdef CONFIG_USER_ONLY
1172 gdb_handlesig(s
->env
, 0);
1174 cpu_interrupt(s
->env
, CPU_INTERRUPT_EXIT
);
1178 static void gdb_read_byte(GDBState
*s
, int ch
)
1180 CPUState
*env
= s
->env
;
1184 #ifndef CONFIG_USER_ONLY
1185 if (s
->last_packet_len
) {
1186 /* Waiting for a response to the last packet. If we see the start
1187 of a new command then abandon the previous response. */
1190 printf("Got NACK, retransmitting\n");
1192 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
1196 printf("Got ACK\n");
1198 printf("Got '%c' when expecting ACK/NACK\n", ch
);
1200 if (ch
== '+' || ch
== '$')
1201 s
->last_packet_len
= 0;
1206 /* when the CPU is running, we cannot do anything except stop
1207 it when receiving a char */
1208 vm_stop(EXCP_INTERRUPT
);
1215 s
->line_buf_index
= 0;
1216 s
->state
= RS_GETLINE
;
1221 s
->state
= RS_CHKSUM1
;
1222 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
1225 s
->line_buf
[s
->line_buf_index
++] = ch
;
1229 s
->line_buf
[s
->line_buf_index
] = '\0';
1230 s
->line_csum
= fromhex(ch
) << 4;
1231 s
->state
= RS_CHKSUM2
;
1234 s
->line_csum
|= fromhex(ch
);
1236 for(i
= 0; i
< s
->line_buf_index
; i
++) {
1237 csum
+= s
->line_buf
[i
];
1239 if (s
->line_csum
!= (csum
& 0xff)) {
1241 put_buffer(s
, &reply
, 1);
1245 put_buffer(s
, &reply
, 1);
1246 s
->state
= gdb_handle_packet(s
, env
, s
->line_buf
);
1255 #ifdef CONFIG_USER_ONLY
1257 gdb_handlesig (CPUState
*env
, int sig
)
1263 if (gdbserver_fd
< 0)
1266 s
= &gdbserver_state
;
1268 /* disable single step if it was enabled */
1269 cpu_single_step(env
, 0);
1274 snprintf(buf
, sizeof(buf
), "S%02x", sig
);
1280 s
->running_state
= 0;
1281 while (s
->running_state
== 0) {
1282 n
= read (s
->fd
, buf
, 256);
1287 for (i
= 0; i
< n
; i
++)
1288 gdb_read_byte (s
, buf
[i
]);
1290 else if (n
== 0 || errno
!= EAGAIN
)
1292 /* XXX: Connection closed. Should probably wait for annother
1293 connection before continuing. */
1300 /* Tell the remote gdb that the process has exited. */
1301 void gdb_exit(CPUState
*env
, int code
)
1306 if (gdbserver_fd
< 0)
1309 s
= &gdbserver_state
;
1311 snprintf(buf
, sizeof(buf
), "W%02x", code
);
1316 static void gdb_accept(void *opaque
)
1319 struct sockaddr_in sockaddr
;
1324 len
= sizeof(sockaddr
);
1325 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
1326 if (fd
< 0 && errno
!= EINTR
) {
1329 } else if (fd
>= 0) {
1334 /* set short latency */
1336 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1338 s
= &gdbserver_state
;
1339 memset (s
, 0, sizeof (GDBState
));
1340 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1343 gdb_syscall_state
= s
;
1345 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1348 static int gdbserver_open(int port
)
1350 struct sockaddr_in sockaddr
;
1353 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1359 /* allow fast reuse */
1361 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
1363 sockaddr
.sin_family
= AF_INET
;
1364 sockaddr
.sin_port
= htons(port
);
1365 sockaddr
.sin_addr
.s_addr
= 0;
1366 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
1371 ret
= listen(fd
, 0);
1379 int gdbserver_start(int port
)
1381 gdbserver_fd
= gdbserver_open(port
);
1382 if (gdbserver_fd
< 0)
1384 /* accept connections */
1389 static int gdb_chr_can_receive(void *opaque
)
1394 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
1396 GDBState
*s
= opaque
;
1399 for (i
= 0; i
< size
; i
++) {
1400 gdb_read_byte(s
, buf
[i
]);
1404 static void gdb_chr_event(void *opaque
, int event
)
1407 case CHR_EVENT_RESET
:
1408 vm_stop(EXCP_INTERRUPT
);
1409 gdb_syscall_state
= opaque
;
1416 int gdbserver_start(const char *port
)
1419 char gdbstub_port_name
[128];
1422 CharDriverState
*chr
;
1424 if (!port
|| !*port
)
1427 port_num
= strtol(port
, &p
, 10);
1429 /* A numeric value is interpreted as a port number. */
1430 snprintf(gdbstub_port_name
, sizeof(gdbstub_port_name
),
1431 "tcp::%d,nowait,nodelay,server", port_num
);
1432 port
= gdbstub_port_name
;
1435 chr
= qemu_chr_open(port
);
1439 s
= qemu_mallocz(sizeof(GDBState
));
1443 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1445 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
1447 qemu_add_vm_stop_handler(gdb_vm_stopped
, s
);