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];
69 #ifdef CONFIG_USER_ONLY
77 #ifdef CONFIG_USER_ONLY
78 /* XXX: This is not thread safe. Do we care? */
79 static int gdbserver_fd
= -1;
81 /* XXX: remove this hack. */
82 static GDBState gdbserver_state
;
84 static int get_char(GDBState
*s
)
90 ret
= recv(s
->fd
, &ch
, 1, 0);
92 if (errno
!= EINTR
&& errno
!= EAGAIN
)
94 } else if (ret
== 0) {
104 /* GDB stub state for use by semihosting syscalls. */
105 static GDBState
*gdb_syscall_state
;
106 static gdb_syscall_complete_cb gdb_current_syscall_cb
;
114 /* If gdb is connected when the first semihosting syscall occurs then use
115 remote gdb syscalls. Otherwise use native file IO. */
116 int use_gdb_syscalls(void)
118 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
119 gdb_syscall_mode
= (gdb_syscall_state
? GDB_SYS_ENABLED
122 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
125 /* Resume execution. */
126 static inline void gdb_continue(GDBState
*s
)
128 #ifdef CONFIG_USER_ONLY
129 s
->running_state
= 1;
135 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
137 #ifdef CONFIG_USER_ONLY
141 ret
= send(s
->fd
, buf
, len
, 0);
143 if (errno
!= EINTR
&& errno
!= EAGAIN
)
151 qemu_chr_write(s
->chr
, buf
, len
);
155 static inline int fromhex(int v
)
157 if (v
>= '0' && v
<= '9')
159 else if (v
>= 'A' && v
<= 'F')
161 else if (v
>= 'a' && v
<= 'f')
167 static inline int tohex(int v
)
175 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
180 for(i
= 0; i
< len
; i
++) {
182 *q
++ = tohex(c
>> 4);
183 *q
++ = tohex(c
& 0xf);
188 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
192 for(i
= 0; i
< len
; i
++) {
193 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
198 /* return -1 if error, 0 if OK */
199 static int put_packet(GDBState
*s
, char *buf
)
205 printf("reply='%s'\n", buf
);
215 for(i
= 0; i
< len
; i
++) {
219 *(p
++) = tohex((csum
>> 4) & 0xf);
220 *(p
++) = tohex((csum
) & 0xf);
222 s
->last_packet_len
= p
- s
->last_packet
;
223 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
225 #ifdef CONFIG_USER_ONLY
238 #if defined(TARGET_I386)
240 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
243 uint32_t *registers
= (uint32_t *)mem_buf
;
246 /* This corresponds with amd64_register_info[] in gdb/amd64-tdep.c */
247 uint64_t *registers64
= (uint64_t *)mem_buf
;
249 if (env
->hflags
& HF_CS64_MASK
) {
250 registers64
[0] = tswap64(env
->regs
[R_EAX
]);
251 registers64
[1] = tswap64(env
->regs
[R_EBX
]);
252 registers64
[2] = tswap64(env
->regs
[R_ECX
]);
253 registers64
[3] = tswap64(env
->regs
[R_EDX
]);
254 registers64
[4] = tswap64(env
->regs
[R_ESI
]);
255 registers64
[5] = tswap64(env
->regs
[R_EDI
]);
256 registers64
[6] = tswap64(env
->regs
[R_EBP
]);
257 registers64
[7] = tswap64(env
->regs
[R_ESP
]);
258 for(i
= 8; i
< 16; i
++) {
259 registers64
[i
] = tswap64(env
->regs
[i
]);
261 registers64
[16] = tswap64(env
->eip
);
263 registers
= (uint32_t *)®isters64
[17];
264 registers
[0] = tswap32(env
->eflags
);
265 registers
[1] = tswap32(env
->segs
[R_CS
].selector
);
266 registers
[2] = tswap32(env
->segs
[R_SS
].selector
);
267 registers
[3] = tswap32(env
->segs
[R_DS
].selector
);
268 registers
[4] = tswap32(env
->segs
[R_ES
].selector
);
269 registers
[5] = tswap32(env
->segs
[R_FS
].selector
);
270 registers
[6] = tswap32(env
->segs
[R_GS
].selector
);
271 /* XXX: convert floats */
272 for(i
= 0; i
< 8; i
++) {
273 memcpy(mem_buf
+ 16 * 8 + 7 * 4 + i
* 10, &env
->fpregs
[i
], 10);
275 registers
[27] = tswap32(env
->fpuc
); /* fctrl */
276 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
277 registers
[28] = tswap32(fpus
); /* fstat */
278 registers
[29] = 0; /* ftag */
279 registers
[30] = 0; /* fiseg */
280 registers
[31] = 0; /* fioff */
281 registers
[32] = 0; /* foseg */
282 registers
[33] = 0; /* fooff */
283 registers
[34] = 0; /* fop */
284 for(i
= 0; i
< 16; i
++) {
285 memcpy(mem_buf
+ 16 * 8 + 35 * 4 + i
* 16, &env
->xmm_regs
[i
], 16);
287 registers
[99] = tswap32(env
->mxcsr
);
289 return 8 * 17 + 4 * 7 + 10 * 8 + 4 * 8 + 16 * 16 + 4;
293 for(i
= 0; i
< 8; i
++) {
294 registers
[i
] = env
->regs
[i
];
296 registers
[8] = env
->eip
;
297 registers
[9] = env
->eflags
;
298 registers
[10] = env
->segs
[R_CS
].selector
;
299 registers
[11] = env
->segs
[R_SS
].selector
;
300 registers
[12] = env
->segs
[R_DS
].selector
;
301 registers
[13] = env
->segs
[R_ES
].selector
;
302 registers
[14] = env
->segs
[R_FS
].selector
;
303 registers
[15] = env
->segs
[R_GS
].selector
;
304 /* XXX: convert floats */
305 for(i
= 0; i
< 8; i
++) {
306 memcpy(mem_buf
+ 16 * 4 + i
* 10, &env
->fpregs
[i
], 10);
308 registers
[36] = env
->fpuc
;
309 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
310 registers
[37] = fpus
;
311 registers
[38] = 0; /* XXX: convert tags */
312 registers
[39] = 0; /* fiseg */
313 registers
[40] = 0; /* fioff */
314 registers
[41] = 0; /* foseg */
315 registers
[42] = 0; /* fooff */
316 registers
[43] = 0; /* fop */
318 for(i
= 0; i
< 16; i
++)
319 tswapls(®isters
[i
]);
320 for(i
= 36; i
< 44; i
++)
321 tswapls(®isters
[i
]);
325 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
327 uint32_t *registers
= (uint32_t *)mem_buf
;
330 for(i
= 0; i
< 8; i
++) {
331 env
->regs
[i
] = tswapl(registers
[i
]);
333 env
->eip
= tswapl(registers
[8]);
334 env
->eflags
= tswapl(registers
[9]);
335 #if defined(CONFIG_USER_ONLY)
336 #define LOAD_SEG(index, sreg)\
337 if (tswapl(registers[index]) != env->segs[sreg].selector)\
338 cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
348 #elif defined (TARGET_PPC)
349 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
351 uint32_t *registers
= (uint32_t *)mem_buf
, tmp
;
355 for(i
= 0; i
< 32; i
++) {
356 registers
[i
] = tswapl(env
->gpr
[i
]);
359 for (i
= 0; i
< 32; i
++) {
360 registers
[(i
* 2) + 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
361 registers
[(i
* 2) + 33] = tswapl(*((uint32_t *)&env
->fpr
[i
] + 1));
363 /* nip, msr, ccr, lnk, ctr, xer, mq */
364 registers
[96] = tswapl(env
->nip
);
365 registers
[97] = tswapl(env
->msr
);
367 for (i
= 0; i
< 8; i
++)
368 tmp
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
369 registers
[98] = tswapl(tmp
);
370 registers
[99] = tswapl(env
->lr
);
371 registers
[100] = tswapl(env
->ctr
);
372 registers
[101] = tswapl(ppc_load_xer(env
));
378 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
380 uint32_t *registers
= (uint32_t *)mem_buf
;
384 for (i
= 0; i
< 32; i
++) {
385 env
->gpr
[i
] = tswapl(registers
[i
]);
388 for (i
= 0; i
< 32; i
++) {
389 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[(i
* 2) + 32]);
390 *((uint32_t *)&env
->fpr
[i
] + 1) = tswapl(registers
[(i
* 2) + 33]);
392 /* nip, msr, ccr, lnk, ctr, xer, mq */
393 env
->nip
= tswapl(registers
[96]);
394 ppc_store_msr(env
, tswapl(registers
[97]));
395 registers
[98] = tswapl(registers
[98]);
396 for (i
= 0; i
< 8; i
++)
397 env
->crf
[i
] = (registers
[98] >> (32 - ((i
+ 1) * 4))) & 0xF;
398 env
->lr
= tswapl(registers
[99]);
399 env
->ctr
= tswapl(registers
[100]);
400 ppc_store_xer(env
, tswapl(registers
[101]));
402 #elif defined (TARGET_SPARC)
403 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
405 target_ulong
*registers
= (target_ulong
*)mem_buf
;
409 for(i
= 0; i
< 8; i
++) {
410 registers
[i
] = tswapl(env
->gregs
[i
]);
412 /* fill in register window */
413 for(i
= 0; i
< 24; i
++) {
414 registers
[i
+ 8] = tswapl(env
->regwptr
[i
]);
416 #ifndef TARGET_SPARC64
418 for (i
= 0; i
< 32; i
++) {
419 registers
[i
+ 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
421 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
422 registers
[64] = tswapl(env
->y
);
427 registers
[65] = tswapl(tmp
);
429 registers
[66] = tswapl(env
->wim
);
430 registers
[67] = tswapl(env
->tbr
);
431 registers
[68] = tswapl(env
->pc
);
432 registers
[69] = tswapl(env
->npc
);
433 registers
[70] = tswapl(env
->fsr
);
434 registers
[71] = 0; /* csr */
436 return 73 * sizeof(target_ulong
);
439 for (i
= 0; i
< 64; i
+= 2) {
442 tmp
= ((uint64_t)*(uint32_t *)&env
->fpr
[i
]) << 32;
443 tmp
|= *(uint32_t *)&env
->fpr
[i
+ 1];
444 registers
[i
/ 2 + 32] = tswap64(tmp
);
446 registers
[64] = tswapl(env
->pc
);
447 registers
[65] = tswapl(env
->npc
);
448 registers
[66] = tswapl(((uint64_t)GET_CCR(env
) << 32) |
449 ((env
->asi
& 0xff) << 24) |
450 ((env
->pstate
& 0xfff) << 8) |
452 registers
[67] = tswapl(env
->fsr
);
453 registers
[68] = tswapl(env
->fprs
);
454 registers
[69] = tswapl(env
->y
);
455 return 70 * sizeof(target_ulong
);
459 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
461 target_ulong
*registers
= (target_ulong
*)mem_buf
;
465 for(i
= 0; i
< 7; i
++) {
466 env
->gregs
[i
] = tswapl(registers
[i
]);
468 /* fill in register window */
469 for(i
= 0; i
< 24; i
++) {
470 env
->regwptr
[i
] = tswapl(registers
[i
+ 8]);
472 #ifndef TARGET_SPARC64
474 for (i
= 0; i
< 32; i
++) {
475 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[i
+ 32]);
477 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
478 env
->y
= tswapl(registers
[64]);
479 PUT_PSR(env
, tswapl(registers
[65]));
480 env
->wim
= tswapl(registers
[66]);
481 env
->tbr
= tswapl(registers
[67]);
482 env
->pc
= tswapl(registers
[68]);
483 env
->npc
= tswapl(registers
[69]);
484 env
->fsr
= tswapl(registers
[70]);
486 for (i
= 0; i
< 64; i
+= 2) {
489 tmp
= tswap64(registers
[i
/ 2 + 32]);
490 *((uint32_t *)&env
->fpr
[i
]) = tmp
>> 32;
491 *((uint32_t *)&env
->fpr
[i
+ 1]) = tmp
& 0xffffffff;
493 env
->pc
= tswapl(registers
[64]);
494 env
->npc
= tswapl(registers
[65]);
496 uint64_t tmp
= tswapl(registers
[66]);
498 PUT_CCR(env
, tmp
>> 32);
499 env
->asi
= (tmp
>> 24) & 0xff;
500 env
->pstate
= (tmp
>> 8) & 0xfff;
501 PUT_CWP64(env
, tmp
& 0xff);
503 env
->fsr
= tswapl(registers
[67]);
504 env
->fprs
= tswapl(registers
[68]);
505 env
->y
= tswapl(registers
[69]);
508 #elif defined (TARGET_ARM)
509 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
515 /* 16 core integer registers (4 bytes each). */
516 for (i
= 0; i
< 16; i
++)
518 *(uint32_t *)ptr
= tswapl(env
->regs
[i
]);
521 /* 8 FPA registers (12 bytes each), FPS (4 bytes).
522 Not yet implemented. */
523 memset (ptr
, 0, 8 * 12 + 4);
525 /* CPSR (4 bytes). */
526 *(uint32_t *)ptr
= tswapl (cpsr_read(env
));
529 return ptr
- mem_buf
;
532 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
538 /* Core integer registers. */
539 for (i
= 0; i
< 16; i
++)
541 env
->regs
[i
] = tswapl(*(uint32_t *)ptr
);
544 /* Ignore FPA regs and scr. */
546 cpsr_write (env
, tswapl(*(uint32_t *)ptr
), 0xffffffff);
548 #elif defined (TARGET_M68K)
549 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
557 for (i
= 0; i
< 8; i
++) {
558 *(uint32_t *)ptr
= tswapl(env
->dregs
[i
]);
562 for (i
= 0; i
< 8; i
++) {
563 *(uint32_t *)ptr
= tswapl(env
->aregs
[i
]);
566 *(uint32_t *)ptr
= tswapl(env
->sr
);
568 *(uint32_t *)ptr
= tswapl(env
->pc
);
570 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
571 ColdFire has 8-bit double precision registers. */
572 for (i
= 0; i
< 8; i
++) {
574 *(uint32_t *)ptr
= tswap32(u
.l
.upper
);
575 *(uint32_t *)ptr
= tswap32(u
.l
.lower
);
577 /* FP control regs (not implemented). */
578 memset (ptr
, 0, 3 * 4);
581 return ptr
- mem_buf
;
584 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
592 for (i
= 0; i
< 8; i
++) {
593 env
->dregs
[i
] = tswapl(*(uint32_t *)ptr
);
597 for (i
= 0; i
< 8; i
++) {
598 env
->aregs
[i
] = tswapl(*(uint32_t *)ptr
);
601 env
->sr
= tswapl(*(uint32_t *)ptr
);
603 env
->pc
= tswapl(*(uint32_t *)ptr
);
605 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
606 ColdFire has 8-bit double precision registers. */
607 for (i
= 0; i
< 8; i
++) {
608 u
.l
.upper
= tswap32(*(uint32_t *)ptr
);
609 u
.l
.lower
= tswap32(*(uint32_t *)ptr
);
612 /* FP control regs (not implemented). */
615 #elif defined (TARGET_MIPS)
616 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
622 for (i
= 0; i
< 32; i
++)
624 *(target_ulong
*)ptr
= tswapl(env
->gpr
[env
->current_tc
][i
]);
625 ptr
+= sizeof(target_ulong
);
628 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_Status
);
629 ptr
+= sizeof(target_ulong
);
631 *(target_ulong
*)ptr
= tswapl(env
->LO
[env
->current_tc
][0]);
632 ptr
+= sizeof(target_ulong
);
634 *(target_ulong
*)ptr
= tswapl(env
->HI
[env
->current_tc
][0]);
635 ptr
+= sizeof(target_ulong
);
637 *(target_ulong
*)ptr
= tswapl(env
->CP0_BadVAddr
);
638 ptr
+= sizeof(target_ulong
);
640 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_Cause
);
641 ptr
+= sizeof(target_ulong
);
643 *(target_ulong
*)ptr
= tswapl(env
->PC
[env
->current_tc
]);
644 ptr
+= sizeof(target_ulong
);
646 if (env
->CP0_Config1
& (1 << CP0C1_FP
))
648 for (i
= 0; i
< 32; i
++)
650 if (env
->CP0_Status
& (1 << CP0St_FR
))
651 *(target_ulong
*)ptr
= tswapl(env
->fpu
->fpr
[i
].d
);
653 *(target_ulong
*)ptr
= tswap32(env
->fpu
->fpr
[i
].w
[FP_ENDIAN_IDX
]);
654 ptr
+= sizeof(target_ulong
);
657 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->fpu
->fcr31
);
658 ptr
+= sizeof(target_ulong
);
660 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->fpu
->fcr0
);
661 ptr
+= sizeof(target_ulong
);
664 /* "fp", pseudo frame pointer. Not yet implemented in gdb. */
665 *(target_ulong
*)ptr
= 0;
666 ptr
+= sizeof(target_ulong
);
668 /* Registers for embedded use, we just pad them. */
669 for (i
= 0; i
< 16; i
++)
671 *(target_ulong
*)ptr
= 0;
672 ptr
+= sizeof(target_ulong
);
676 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_PRid
);
677 ptr
+= sizeof(target_ulong
);
679 return ptr
- mem_buf
;
682 /* convert MIPS rounding mode in FCR31 to IEEE library */
683 static unsigned int ieee_rm
[] =
685 float_round_nearest_even
,
690 #define RESTORE_ROUNDING_MODE \
691 set_float_rounding_mode(ieee_rm[env->fpu->fcr31 & 3], &env->fpu->fp_status)
693 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
699 for (i
= 0; i
< 32; i
++)
701 env
->gpr
[env
->current_tc
][i
] = tswapl(*(target_ulong
*)ptr
);
702 ptr
+= sizeof(target_ulong
);
705 env
->CP0_Status
= tswapl(*(target_ulong
*)ptr
);
706 ptr
+= sizeof(target_ulong
);
708 env
->LO
[env
->current_tc
][0] = tswapl(*(target_ulong
*)ptr
);
709 ptr
+= sizeof(target_ulong
);
711 env
->HI
[env
->current_tc
][0] = tswapl(*(target_ulong
*)ptr
);
712 ptr
+= sizeof(target_ulong
);
714 env
->CP0_BadVAddr
= tswapl(*(target_ulong
*)ptr
);
715 ptr
+= sizeof(target_ulong
);
717 env
->CP0_Cause
= tswapl(*(target_ulong
*)ptr
);
718 ptr
+= sizeof(target_ulong
);
720 env
->PC
[env
->current_tc
] = tswapl(*(target_ulong
*)ptr
);
721 ptr
+= sizeof(target_ulong
);
723 if (env
->CP0_Config1
& (1 << CP0C1_FP
))
725 for (i
= 0; i
< 32; i
++)
727 if (env
->CP0_Status
& (1 << CP0St_FR
))
728 env
->fpu
->fpr
[i
].d
= tswapl(*(target_ulong
*)ptr
);
730 env
->fpu
->fpr
[i
].w
[FP_ENDIAN_IDX
] = tswapl(*(target_ulong
*)ptr
);
731 ptr
+= sizeof(target_ulong
);
734 env
->fpu
->fcr31
= tswapl(*(target_ulong
*)ptr
) & 0xFF83FFFF;
735 ptr
+= sizeof(target_ulong
);
737 /* The remaining registers are assumed to be read-only. */
739 /* set rounding mode */
740 RESTORE_ROUNDING_MODE
;
742 #ifndef CONFIG_SOFTFLOAT
743 /* no floating point exception for native float */
744 SET_FP_ENABLE(env
->fcr31
, 0);
748 #elif defined (TARGET_SH4)
750 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
752 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
754 uint32_t *ptr
= (uint32_t *)mem_buf
;
757 #define SAVE(x) *ptr++=tswapl(x)
758 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
759 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
+ 16]);
761 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
]);
763 for (i
= 8; i
< 16; i
++) SAVE(env
->gregs
[i
]);
773 for (i
= 0; i
< 16; i
++)
774 SAVE(env
->fregs
[i
+ ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
777 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
]);
778 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
+ 16]);
779 return ((uint8_t *)ptr
- mem_buf
);
782 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
784 uint32_t *ptr
= (uint32_t *)mem_buf
;
787 #define LOAD(x) (x)=*ptr++;
788 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
789 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
+ 16]);
791 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
]);
793 for (i
= 8; i
< 16; i
++) LOAD(env
->gregs
[i
]);
803 for (i
= 0; i
< 16; i
++)
804 LOAD(env
->fregs
[i
+ ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
807 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
]);
808 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
+ 16]);
810 #elif defined (TARGET_CRIS)
812 static int cris_save_32 (unsigned char *d
, uint32_t value
)
815 *d
++ = (value
>>= 8);
816 *d
++ = (value
>>= 8);
817 *d
++ = (value
>>= 8);
820 static int cris_save_16 (unsigned char *d
, uint32_t value
)
823 *d
++ = (value
>>= 8);
826 static int cris_save_8 (unsigned char *d
, uint32_t value
)
832 /* FIXME: this will bug on archs not supporting unaligned word accesses. */
833 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
835 uint8_t *ptr
= mem_buf
;
839 for (i
= 0; i
< 16; i
++)
840 ptr
+= cris_save_32 (ptr
, env
->regs
[i
]);
842 srs
= env
->pregs
[PR_SRS
];
844 ptr
+= cris_save_8 (ptr
, env
->pregs
[0]);
845 ptr
+= cris_save_8 (ptr
, env
->pregs
[1]);
846 ptr
+= cris_save_32 (ptr
, env
->pregs
[2]);
847 ptr
+= cris_save_8 (ptr
, srs
);
848 ptr
+= cris_save_16 (ptr
, env
->pregs
[4]);
850 for (i
= 5; i
< 16; i
++)
851 ptr
+= cris_save_32 (ptr
, env
->pregs
[i
]);
853 ptr
+= cris_save_32 (ptr
, env
->pc
);
855 for (i
= 0; i
< 16; i
++)
856 ptr
+= cris_save_32 (ptr
, env
->sregs
[srs
][i
]);
858 return ((uint8_t *)ptr
- mem_buf
);
861 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
863 uint32_t *ptr
= (uint32_t *)mem_buf
;
866 #define LOAD(x) (x)=*ptr++;
867 for (i
= 0; i
< 16; i
++) LOAD(env
->regs
[i
]);
871 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
876 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
882 static int gdb_handle_packet(GDBState
*s
, CPUState
*env
, const char *line_buf
)
885 int ch
, reg_size
, type
;
887 uint8_t mem_buf
[4096];
889 target_ulong addr
, len
;
892 printf("command='%s'\n", line_buf
);
898 /* TODO: Make this return the correct value for user-mode. */
899 snprintf(buf
, sizeof(buf
), "S%02x", SIGTRAP
);
904 addr
= strtoull(p
, (char **)&p
, 16);
905 #if defined(TARGET_I386)
908 kvm_load_registers(env
);
909 #elif defined (TARGET_PPC)
911 #elif defined (TARGET_SPARC)
914 #elif defined (TARGET_ARM)
915 env
->regs
[15] = addr
;
916 #elif defined (TARGET_SH4)
918 #elif defined (TARGET_MIPS)
919 env
->PC
[env
->current_tc
] = addr
;
920 #elif defined (TARGET_CRIS)
928 addr
= strtoull(p
, (char **)&p
, 16);
929 #if defined(TARGET_I386)
932 kvm_load_registers(env
);
933 #elif defined (TARGET_PPC)
935 #elif defined (TARGET_SPARC)
938 #elif defined (TARGET_ARM)
939 env
->regs
[15] = addr
;
940 #elif defined (TARGET_SH4)
942 #elif defined (TARGET_MIPS)
943 env
->PC
[env
->current_tc
] = addr
;
944 #elif defined (TARGET_CRIS)
948 cpu_single_step(env
, 1);
956 ret
= strtoull(p
, (char **)&p
, 16);
959 err
= strtoull(p
, (char **)&p
, 16);
966 if (gdb_current_syscall_cb
)
967 gdb_current_syscall_cb(s
->env
, ret
, err
);
969 put_packet(s
, "T02");
977 kvm_save_registers(env
);
978 reg_size
= cpu_gdb_read_registers(env
, mem_buf
);
979 memtohex(buf
, mem_buf
, reg_size
);
983 registers
= (void *)mem_buf
;
985 hextomem((uint8_t *)registers
, p
, len
);
986 cpu_gdb_write_registers(env
, mem_buf
, len
);
988 kvm_load_registers(env
);
992 addr
= strtoull(p
, (char **)&p
, 16);
995 len
= strtoull(p
, NULL
, 16);
996 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 0) != 0) {
997 put_packet (s
, "E14");
999 memtohex(buf
, mem_buf
, len
);
1004 addr
= strtoull(p
, (char **)&p
, 16);
1007 len
= strtoull(p
, (char **)&p
, 16);
1010 hextomem(mem_buf
, p
, len
);
1011 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 1) != 0)
1012 put_packet(s
, "E14");
1014 put_packet(s
, "OK");
1017 type
= strtoul(p
, (char **)&p
, 16);
1020 addr
= strtoull(p
, (char **)&p
, 16);
1023 len
= strtoull(p
, (char **)&p
, 16);
1024 if (type
== 0 || type
== 1) {
1025 if (cpu_breakpoint_insert(env
, addr
) < 0)
1026 goto breakpoint_error
;
1027 put_packet(s
, "OK");
1028 #ifndef CONFIG_USER_ONLY
1029 } else if (type
== 2) {
1030 if (cpu_watchpoint_insert(env
, addr
) < 0)
1031 goto breakpoint_error
;
1032 put_packet(s
, "OK");
1036 put_packet(s
, "E22");
1040 type
= strtoul(p
, (char **)&p
, 16);
1043 addr
= strtoull(p
, (char **)&p
, 16);
1046 len
= strtoull(p
, (char **)&p
, 16);
1047 if (type
== 0 || type
== 1) {
1048 cpu_breakpoint_remove(env
, addr
);
1049 put_packet(s
, "OK");
1050 #ifndef CONFIG_USER_ONLY
1051 } else if (type
== 2) {
1052 cpu_watchpoint_remove(env
, addr
);
1053 put_packet(s
, "OK");
1056 goto breakpoint_error
;
1059 #ifdef CONFIG_LINUX_USER
1061 if (strncmp(p
, "Offsets", 7) == 0) {
1062 TaskState
*ts
= env
->opaque
;
1065 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1066 ";Bss=" TARGET_ABI_FMT_lx
,
1067 ts
->info
->code_offset
,
1068 ts
->info
->data_offset
,
1069 ts
->info
->data_offset
);
1077 /* put empty packet */
1085 extern void tb_flush(CPUState
*env
);
1087 #ifndef CONFIG_USER_ONLY
1088 static void gdb_vm_stopped(void *opaque
, int reason
)
1090 GDBState
*s
= opaque
;
1094 if (s
->state
== RS_SYSCALL
)
1097 /* disable single step if it was enable */
1098 cpu_single_step(s
->env
, 0);
1100 if (reason
== EXCP_DEBUG
) {
1101 if (s
->env
->watchpoint_hit
) {
1102 snprintf(buf
, sizeof(buf
), "T%02xwatch:" TARGET_FMT_lx
";",
1104 s
->env
->watchpoint
[s
->env
->watchpoint_hit
- 1].vaddr
);
1106 s
->env
->watchpoint_hit
= 0;
1111 } else if (reason
== EXCP_INTERRUPT
) {
1116 snprintf(buf
, sizeof(buf
), "S%02x", ret
);
1121 /* Send a gdb syscall request.
1122 This accepts limited printf-style format specifiers, specifically:
1123 %x - target_ulong argument printed in hex.
1124 %lx - 64-bit argument printed in hex.
1125 %s - string pointer (target_ulong) and length (int) pair. */
1126 void gdb_do_syscall(gdb_syscall_complete_cb cb
, char *fmt
, ...)
1135 s
= gdb_syscall_state
;
1138 gdb_current_syscall_cb
= cb
;
1139 s
->state
= RS_SYSCALL
;
1140 #ifndef CONFIG_USER_ONLY
1141 vm_stop(EXCP_DEBUG
);
1152 addr
= va_arg(va
, target_ulong
);
1153 p
+= sprintf(p
, TARGET_FMT_lx
, addr
);
1156 if (*(fmt
++) != 'x')
1158 i64
= va_arg(va
, uint64_t);
1159 p
+= sprintf(p
, "%" PRIx64
, i64
);
1162 addr
= va_arg(va
, target_ulong
);
1163 p
+= sprintf(p
, TARGET_FMT_lx
"/%x", addr
, va_arg(va
, int));
1167 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
1178 #ifdef CONFIG_USER_ONLY
1179 gdb_handlesig(s
->env
, 0);
1181 cpu_interrupt(s
->env
, CPU_INTERRUPT_EXIT
);
1185 static void gdb_read_byte(GDBState
*s
, int ch
)
1187 CPUState
*env
= s
->env
;
1191 #ifndef CONFIG_USER_ONLY
1192 if (s
->last_packet_len
) {
1193 /* Waiting for a response to the last packet. If we see the start
1194 of a new command then abandon the previous response. */
1197 printf("Got NACK, retransmitting\n");
1199 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
1203 printf("Got ACK\n");
1205 printf("Got '%c' when expecting ACK/NACK\n", ch
);
1207 if (ch
== '+' || ch
== '$')
1208 s
->last_packet_len
= 0;
1213 /* when the CPU is running, we cannot do anything except stop
1214 it when receiving a char */
1215 vm_stop(EXCP_INTERRUPT
);
1222 s
->line_buf_index
= 0;
1223 s
->state
= RS_GETLINE
;
1228 s
->state
= RS_CHKSUM1
;
1229 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
1232 s
->line_buf
[s
->line_buf_index
++] = ch
;
1236 s
->line_buf
[s
->line_buf_index
] = '\0';
1237 s
->line_csum
= fromhex(ch
) << 4;
1238 s
->state
= RS_CHKSUM2
;
1241 s
->line_csum
|= fromhex(ch
);
1243 for(i
= 0; i
< s
->line_buf_index
; i
++) {
1244 csum
+= s
->line_buf
[i
];
1246 if (s
->line_csum
!= (csum
& 0xff)) {
1248 put_buffer(s
, &reply
, 1);
1252 put_buffer(s
, &reply
, 1);
1253 s
->state
= gdb_handle_packet(s
, env
, s
->line_buf
);
1262 #ifdef CONFIG_USER_ONLY
1264 gdb_handlesig (CPUState
*env
, int sig
)
1270 if (gdbserver_fd
< 0)
1273 s
= &gdbserver_state
;
1275 /* disable single step if it was enabled */
1276 cpu_single_step(env
, 0);
1281 snprintf(buf
, sizeof(buf
), "S%02x", sig
);
1287 s
->running_state
= 0;
1288 while (s
->running_state
== 0) {
1289 n
= read (s
->fd
, buf
, 256);
1294 for (i
= 0; i
< n
; i
++)
1295 gdb_read_byte (s
, buf
[i
]);
1297 else if (n
== 0 || errno
!= EAGAIN
)
1299 /* XXX: Connection closed. Should probably wait for annother
1300 connection before continuing. */
1307 /* Tell the remote gdb that the process has exited. */
1308 void gdb_exit(CPUState
*env
, int code
)
1313 if (gdbserver_fd
< 0)
1316 s
= &gdbserver_state
;
1318 snprintf(buf
, sizeof(buf
), "W%02x", code
);
1323 static void gdb_accept(void *opaque
)
1326 struct sockaddr_in sockaddr
;
1331 len
= sizeof(sockaddr
);
1332 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
1333 if (fd
< 0 && errno
!= EINTR
) {
1336 } else if (fd
>= 0) {
1341 /* set short latency */
1343 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1345 s
= &gdbserver_state
;
1346 memset (s
, 0, sizeof (GDBState
));
1347 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1350 gdb_syscall_state
= s
;
1352 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1355 static int gdbserver_open(int port
)
1357 struct sockaddr_in sockaddr
;
1360 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1366 /* allow fast reuse */
1368 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
1370 sockaddr
.sin_family
= AF_INET
;
1371 sockaddr
.sin_port
= htons(port
);
1372 sockaddr
.sin_addr
.s_addr
= 0;
1373 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
1378 ret
= listen(fd
, 0);
1386 int gdbserver_start(int port
)
1388 gdbserver_fd
= gdbserver_open(port
);
1389 if (gdbserver_fd
< 0)
1391 /* accept connections */
1396 static int gdb_chr_can_receive(void *opaque
)
1401 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
1403 GDBState
*s
= opaque
;
1406 for (i
= 0; i
< size
; i
++) {
1407 gdb_read_byte(s
, buf
[i
]);
1411 static void gdb_chr_event(void *opaque
, int event
)
1414 case CHR_EVENT_RESET
:
1415 vm_stop(EXCP_INTERRUPT
);
1416 gdb_syscall_state
= opaque
;
1423 int gdbserver_start(const char *port
)
1426 char gdbstub_port_name
[128];
1429 CharDriverState
*chr
;
1431 if (!port
|| !*port
)
1434 port_num
= strtol(port
, &p
, 10);
1436 /* A numeric value is interpreted as a port number. */
1437 snprintf(gdbstub_port_name
, sizeof(gdbstub_port_name
),
1438 "tcp::%d,nowait,nodelay,server", port_num
);
1439 port
= gdbstub_port_name
;
1442 chr
= qemu_chr_open(port
);
1446 s
= qemu_mallocz(sizeof(GDBState
));
1450 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1452 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
1454 qemu_add_vm_stop_handler(gdb_vm_stopped
, s
);