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 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
127 #ifdef CONFIG_USER_ONLY
131 ret
= send(s
->fd
, buf
, len
, 0);
133 if (errno
!= EINTR
&& errno
!= EAGAIN
)
141 qemu_chr_write(s
->chr
, buf
, len
);
145 static inline int fromhex(int v
)
147 if (v
>= '0' && v
<= '9')
149 else if (v
>= 'A' && v
<= 'F')
151 else if (v
>= 'a' && v
<= 'f')
157 static inline int tohex(int v
)
165 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
170 for(i
= 0; i
< len
; i
++) {
172 *q
++ = tohex(c
>> 4);
173 *q
++ = tohex(c
& 0xf);
178 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
182 for(i
= 0; i
< len
; i
++) {
183 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
188 /* return -1 if error, 0 if OK */
189 static int put_packet(GDBState
*s
, char *buf
)
195 printf("reply='%s'\n", buf
);
205 for(i
= 0; i
< len
; i
++) {
209 *(p
++) = tohex((csum
>> 4) & 0xf);
210 *(p
++) = tohex((csum
) & 0xf);
212 s
->last_packet_len
= p
- s
->last_packet
;
213 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
215 #ifdef CONFIG_USER_ONLY
228 #if defined(TARGET_I386)
230 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
233 uint32_t *registers
= (uint32_t *)mem_buf
;
236 /* This corresponds with amd64_register_info[] in gdb/amd64-tdep.c */
237 uint64_t *registers64
= (uint64_t *)mem_buf
;
239 if (env
->hflags
& HF_CS64_MASK
) {
240 registers64
[0] = tswap64(env
->regs
[R_EAX
]);
241 registers64
[1] = tswap64(env
->regs
[R_EBX
]);
242 registers64
[2] = tswap64(env
->regs
[R_ECX
]);
243 registers64
[3] = tswap64(env
->regs
[R_EDX
]);
244 registers64
[4] = tswap64(env
->regs
[R_ESI
]);
245 registers64
[5] = tswap64(env
->regs
[R_EDI
]);
246 registers64
[6] = tswap64(env
->regs
[R_EBP
]);
247 registers64
[7] = tswap64(env
->regs
[R_ESP
]);
248 for(i
= 8; i
< 16; i
++) {
249 registers64
[i
] = tswap64(env
->regs
[i
]);
251 registers64
[16] = tswap64(env
->eip
);
253 registers
= (uint32_t *)®isters64
[17];
254 registers
[0] = tswap32(env
->eflags
);
255 registers
[1] = tswap32(env
->segs
[R_CS
].selector
);
256 registers
[2] = tswap32(env
->segs
[R_SS
].selector
);
257 registers
[3] = tswap32(env
->segs
[R_DS
].selector
);
258 registers
[4] = tswap32(env
->segs
[R_ES
].selector
);
259 registers
[5] = tswap32(env
->segs
[R_FS
].selector
);
260 registers
[6] = tswap32(env
->segs
[R_GS
].selector
);
261 /* XXX: convert floats */
262 for(i
= 0; i
< 8; i
++) {
263 memcpy(mem_buf
+ 16 * 8 + 7 * 4 + i
* 10, &env
->fpregs
[i
], 10);
265 registers
[27] = tswap32(env
->fpuc
); /* fctrl */
266 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
267 registers
[28] = tswap32(fpus
); /* fstat */
268 registers
[29] = 0; /* ftag */
269 registers
[30] = 0; /* fiseg */
270 registers
[31] = 0; /* fioff */
271 registers
[32] = 0; /* foseg */
272 registers
[33] = 0; /* fooff */
273 registers
[34] = 0; /* fop */
274 for(i
= 0; i
< 16; i
++) {
275 memcpy(mem_buf
+ 16 * 8 + 35 * 4 + i
* 16, &env
->xmm_regs
[i
], 16);
277 registers
[99] = tswap32(env
->mxcsr
);
279 return 8 * 17 + 4 * 7 + 10 * 8 + 4 * 8 + 16 * 16 + 4;
283 for(i
= 0; i
< 8; i
++) {
284 registers
[i
] = env
->regs
[i
];
286 registers
[8] = env
->eip
;
287 registers
[9] = env
->eflags
;
288 registers
[10] = env
->segs
[R_CS
].selector
;
289 registers
[11] = env
->segs
[R_SS
].selector
;
290 registers
[12] = env
->segs
[R_DS
].selector
;
291 registers
[13] = env
->segs
[R_ES
].selector
;
292 registers
[14] = env
->segs
[R_FS
].selector
;
293 registers
[15] = env
->segs
[R_GS
].selector
;
294 /* XXX: convert floats */
295 for(i
= 0; i
< 8; i
++) {
296 memcpy(mem_buf
+ 16 * 4 + i
* 10, &env
->fpregs
[i
], 10);
298 registers
[36] = env
->fpuc
;
299 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
300 registers
[37] = fpus
;
301 registers
[38] = 0; /* XXX: convert tags */
302 registers
[39] = 0; /* fiseg */
303 registers
[40] = 0; /* fioff */
304 registers
[41] = 0; /* foseg */
305 registers
[42] = 0; /* fooff */
306 registers
[43] = 0; /* fop */
308 for(i
= 0; i
< 16; i
++)
309 tswapls(®isters
[i
]);
310 for(i
= 36; i
< 44; i
++)
311 tswapls(®isters
[i
]);
315 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
317 uint32_t *registers
= (uint32_t *)mem_buf
;
320 for(i
= 0; i
< 8; i
++) {
321 env
->regs
[i
] = tswapl(registers
[i
]);
323 env
->eip
= tswapl(registers
[8]);
324 env
->eflags
= tswapl(registers
[9]);
325 #if defined(CONFIG_USER_ONLY)
326 #define LOAD_SEG(index, sreg)\
327 if (tswapl(registers[index]) != env->segs[sreg].selector)\
328 cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
338 #elif defined (TARGET_PPC)
339 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
341 uint32_t *registers
= (uint32_t *)mem_buf
, tmp
;
345 for(i
= 0; i
< 32; i
++) {
346 registers
[i
] = tswapl(env
->gpr
[i
]);
349 for (i
= 0; i
< 32; i
++) {
350 registers
[(i
* 2) + 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
351 registers
[(i
* 2) + 33] = tswapl(*((uint32_t *)&env
->fpr
[i
] + 1));
353 /* nip, msr, ccr, lnk, ctr, xer, mq */
354 registers
[96] = tswapl(env
->nip
);
355 registers
[97] = tswapl(env
->msr
);
357 for (i
= 0; i
< 8; i
++)
358 tmp
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
359 registers
[98] = tswapl(tmp
);
360 registers
[99] = tswapl(env
->lr
);
361 registers
[100] = tswapl(env
->ctr
);
362 registers
[101] = tswapl(ppc_load_xer(env
));
368 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
370 uint32_t *registers
= (uint32_t *)mem_buf
;
374 for (i
= 0; i
< 32; i
++) {
375 env
->gpr
[i
] = tswapl(registers
[i
]);
378 for (i
= 0; i
< 32; i
++) {
379 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[(i
* 2) + 32]);
380 *((uint32_t *)&env
->fpr
[i
] + 1) = tswapl(registers
[(i
* 2) + 33]);
382 /* nip, msr, ccr, lnk, ctr, xer, mq */
383 env
->nip
= tswapl(registers
[96]);
384 ppc_store_msr(env
, tswapl(registers
[97]));
385 registers
[98] = tswapl(registers
[98]);
386 for (i
= 0; i
< 8; i
++)
387 env
->crf
[i
] = (registers
[98] >> (32 - ((i
+ 1) * 4))) & 0xF;
388 env
->lr
= tswapl(registers
[99]);
389 env
->ctr
= tswapl(registers
[100]);
390 ppc_store_xer(env
, tswapl(registers
[101]));
392 #elif defined (TARGET_SPARC)
393 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
395 target_ulong
*registers
= (target_ulong
*)mem_buf
;
399 for(i
= 0; i
< 8; i
++) {
400 registers
[i
] = tswapl(env
->gregs
[i
]);
402 /* fill in register window */
403 for(i
= 0; i
< 24; i
++) {
404 registers
[i
+ 8] = tswapl(env
->regwptr
[i
]);
406 #ifndef TARGET_SPARC64
408 for (i
= 0; i
< 32; i
++) {
409 registers
[i
+ 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
411 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
412 registers
[64] = tswapl(env
->y
);
417 registers
[65] = tswapl(tmp
);
419 registers
[66] = tswapl(env
->wim
);
420 registers
[67] = tswapl(env
->tbr
);
421 registers
[68] = tswapl(env
->pc
);
422 registers
[69] = tswapl(env
->npc
);
423 registers
[70] = tswapl(env
->fsr
);
424 registers
[71] = 0; /* csr */
426 return 73 * sizeof(target_ulong
);
429 for (i
= 0; i
< 64; i
+= 2) {
432 tmp
= ((uint64_t)*(uint32_t *)&env
->fpr
[i
]) << 32;
433 tmp
|= *(uint32_t *)&env
->fpr
[i
+ 1];
434 registers
[i
/ 2 + 32] = tswap64(tmp
);
436 registers
[64] = tswapl(env
->pc
);
437 registers
[65] = tswapl(env
->npc
);
438 registers
[66] = tswapl(((uint64_t)GET_CCR(env
) << 32) |
439 ((env
->asi
& 0xff) << 24) |
440 ((env
->pstate
& 0xfff) << 8) |
442 registers
[67] = tswapl(env
->fsr
);
443 registers
[68] = tswapl(env
->fprs
);
444 registers
[69] = tswapl(env
->y
);
445 return 70 * sizeof(target_ulong
);
449 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
451 target_ulong
*registers
= (target_ulong
*)mem_buf
;
455 for(i
= 0; i
< 7; i
++) {
456 env
->gregs
[i
] = tswapl(registers
[i
]);
458 /* fill in register window */
459 for(i
= 0; i
< 24; i
++) {
460 env
->regwptr
[i
] = tswapl(registers
[i
+ 8]);
462 #ifndef TARGET_SPARC64
464 for (i
= 0; i
< 32; i
++) {
465 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[i
+ 32]);
467 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
468 env
->y
= tswapl(registers
[64]);
469 PUT_PSR(env
, tswapl(registers
[65]));
470 env
->wim
= tswapl(registers
[66]);
471 env
->tbr
= tswapl(registers
[67]);
472 env
->pc
= tswapl(registers
[68]);
473 env
->npc
= tswapl(registers
[69]);
474 env
->fsr
= tswapl(registers
[70]);
476 for (i
= 0; i
< 64; i
+= 2) {
479 tmp
= tswap64(registers
[i
/ 2 + 32]);
480 *((uint32_t *)&env
->fpr
[i
]) = tmp
>> 32;
481 *((uint32_t *)&env
->fpr
[i
+ 1]) = tmp
& 0xffffffff;
483 env
->pc
= tswapl(registers
[64]);
484 env
->npc
= tswapl(registers
[65]);
486 uint64_t tmp
= tswapl(registers
[66]);
488 PUT_CCR(env
, tmp
>> 32);
489 env
->asi
= (tmp
>> 24) & 0xff;
490 env
->pstate
= (tmp
>> 8) & 0xfff;
491 PUT_CWP64(env
, tmp
& 0xff);
493 env
->fsr
= tswapl(registers
[67]);
494 env
->fprs
= tswapl(registers
[68]);
495 env
->y
= tswapl(registers
[69]);
498 #elif defined (TARGET_ARM)
499 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
505 /* 16 core integer registers (4 bytes each). */
506 for (i
= 0; i
< 16; i
++)
508 *(uint32_t *)ptr
= tswapl(env
->regs
[i
]);
511 /* 8 FPA registers (12 bytes each), FPS (4 bytes).
512 Not yet implemented. */
513 memset (ptr
, 0, 8 * 12 + 4);
515 /* CPSR (4 bytes). */
516 *(uint32_t *)ptr
= tswapl (cpsr_read(env
));
519 return ptr
- mem_buf
;
522 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
528 /* Core integer registers. */
529 for (i
= 0; i
< 16; i
++)
531 env
->regs
[i
] = tswapl(*(uint32_t *)ptr
);
534 /* Ignore FPA regs and scr. */
536 cpsr_write (env
, tswapl(*(uint32_t *)ptr
), 0xffffffff);
538 #elif defined (TARGET_M68K)
539 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
547 for (i
= 0; i
< 8; i
++) {
548 *(uint32_t *)ptr
= tswapl(env
->dregs
[i
]);
552 for (i
= 0; i
< 8; i
++) {
553 *(uint32_t *)ptr
= tswapl(env
->aregs
[i
]);
556 *(uint32_t *)ptr
= tswapl(env
->sr
);
558 *(uint32_t *)ptr
= tswapl(env
->pc
);
560 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
561 ColdFire has 8-bit double precision registers. */
562 for (i
= 0; i
< 8; i
++) {
564 *(uint32_t *)ptr
= tswap32(u
.l
.upper
);
565 *(uint32_t *)ptr
= tswap32(u
.l
.lower
);
567 /* FP control regs (not implemented). */
568 memset (ptr
, 0, 3 * 4);
571 return ptr
- mem_buf
;
574 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
582 for (i
= 0; i
< 8; i
++) {
583 env
->dregs
[i
] = tswapl(*(uint32_t *)ptr
);
587 for (i
= 0; i
< 8; i
++) {
588 env
->aregs
[i
] = tswapl(*(uint32_t *)ptr
);
591 env
->sr
= tswapl(*(uint32_t *)ptr
);
593 env
->pc
= tswapl(*(uint32_t *)ptr
);
595 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
596 ColdFire has 8-bit double precision registers. */
597 for (i
= 0; i
< 8; i
++) {
598 u
.l
.upper
= tswap32(*(uint32_t *)ptr
);
599 u
.l
.lower
= tswap32(*(uint32_t *)ptr
);
602 /* FP control regs (not implemented). */
605 #elif defined (TARGET_MIPS)
606 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
612 for (i
= 0; i
< 32; i
++)
614 *(target_ulong
*)ptr
= tswapl(env
->gpr
[env
->current_tc
][i
]);
615 ptr
+= sizeof(target_ulong
);
618 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_Status
);
619 ptr
+= sizeof(target_ulong
);
621 *(target_ulong
*)ptr
= tswapl(env
->LO
[env
->current_tc
][0]);
622 ptr
+= sizeof(target_ulong
);
624 *(target_ulong
*)ptr
= tswapl(env
->HI
[env
->current_tc
][0]);
625 ptr
+= sizeof(target_ulong
);
627 *(target_ulong
*)ptr
= tswapl(env
->CP0_BadVAddr
);
628 ptr
+= sizeof(target_ulong
);
630 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_Cause
);
631 ptr
+= sizeof(target_ulong
);
633 *(target_ulong
*)ptr
= tswapl(env
->PC
[env
->current_tc
]);
634 ptr
+= sizeof(target_ulong
);
636 if (env
->CP0_Config1
& (1 << CP0C1_FP
))
638 for (i
= 0; i
< 32; i
++)
640 if (env
->CP0_Status
& (1 << CP0St_FR
))
641 *(target_ulong
*)ptr
= tswapl(env
->fpu
->fpr
[i
].d
);
643 *(target_ulong
*)ptr
= tswap32(env
->fpu
->fpr
[i
].w
[FP_ENDIAN_IDX
]);
644 ptr
+= sizeof(target_ulong
);
647 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->fpu
->fcr31
);
648 ptr
+= sizeof(target_ulong
);
650 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->fpu
->fcr0
);
651 ptr
+= sizeof(target_ulong
);
654 /* "fp", pseudo frame pointer. Not yet implemented in gdb. */
655 *(target_ulong
*)ptr
= 0;
656 ptr
+= sizeof(target_ulong
);
658 /* Registers for embedded use, we just pad them. */
659 for (i
= 0; i
< 16; i
++)
661 *(target_ulong
*)ptr
= 0;
662 ptr
+= sizeof(target_ulong
);
666 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_PRid
);
667 ptr
+= sizeof(target_ulong
);
669 return ptr
- mem_buf
;
672 /* convert MIPS rounding mode in FCR31 to IEEE library */
673 static unsigned int ieee_rm
[] =
675 float_round_nearest_even
,
680 #define RESTORE_ROUNDING_MODE \
681 set_float_rounding_mode(ieee_rm[env->fpu->fcr31 & 3], &env->fpu->fp_status)
683 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
689 for (i
= 0; i
< 32; i
++)
691 env
->gpr
[env
->current_tc
][i
] = tswapl(*(target_ulong
*)ptr
);
692 ptr
+= sizeof(target_ulong
);
695 env
->CP0_Status
= tswapl(*(target_ulong
*)ptr
);
696 ptr
+= sizeof(target_ulong
);
698 env
->LO
[env
->current_tc
][0] = tswapl(*(target_ulong
*)ptr
);
699 ptr
+= sizeof(target_ulong
);
701 env
->HI
[env
->current_tc
][0] = tswapl(*(target_ulong
*)ptr
);
702 ptr
+= sizeof(target_ulong
);
704 env
->CP0_BadVAddr
= tswapl(*(target_ulong
*)ptr
);
705 ptr
+= sizeof(target_ulong
);
707 env
->CP0_Cause
= tswapl(*(target_ulong
*)ptr
);
708 ptr
+= sizeof(target_ulong
);
710 env
->PC
[env
->current_tc
] = tswapl(*(target_ulong
*)ptr
);
711 ptr
+= sizeof(target_ulong
);
713 if (env
->CP0_Config1
& (1 << CP0C1_FP
))
715 for (i
= 0; i
< 32; i
++)
717 if (env
->CP0_Status
& (1 << CP0St_FR
))
718 env
->fpu
->fpr
[i
].d
= tswapl(*(target_ulong
*)ptr
);
720 env
->fpu
->fpr
[i
].w
[FP_ENDIAN_IDX
] = tswapl(*(target_ulong
*)ptr
);
721 ptr
+= sizeof(target_ulong
);
724 env
->fpu
->fcr31
= tswapl(*(target_ulong
*)ptr
) & 0xFF83FFFF;
725 ptr
+= sizeof(target_ulong
);
727 /* The remaining registers are assumed to be read-only. */
729 /* set rounding mode */
730 RESTORE_ROUNDING_MODE
;
732 #ifndef CONFIG_SOFTFLOAT
733 /* no floating point exception for native float */
734 SET_FP_ENABLE(env
->fcr31
, 0);
738 #elif defined (TARGET_SH4)
740 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
742 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
744 uint32_t *ptr
= (uint32_t *)mem_buf
;
747 #define SAVE(x) *ptr++=tswapl(x)
748 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
749 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
+ 16]);
751 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
]);
753 for (i
= 8; i
< 16; i
++) SAVE(env
->gregs
[i
]);
763 for (i
= 0; i
< 16; i
++)
764 SAVE(env
->fregs
[i
+ ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
767 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
]);
768 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
+ 16]);
769 return ((uint8_t *)ptr
- mem_buf
);
772 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
774 uint32_t *ptr
= (uint32_t *)mem_buf
;
777 #define LOAD(x) (x)=*ptr++;
778 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
779 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
+ 16]);
781 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
]);
783 for (i
= 8; i
< 16; i
++) LOAD(env
->gregs
[i
]);
793 for (i
= 0; i
< 16; i
++)
794 LOAD(env
->fregs
[i
+ ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
797 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
]);
798 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
+ 16]);
800 #elif defined (TARGET_CRIS)
802 static int cris_save_32 (unsigned char *d
, uint32_t value
)
805 *d
++ = (value
>>= 8);
806 *d
++ = (value
>>= 8);
807 *d
++ = (value
>>= 8);
810 static int cris_save_16 (unsigned char *d
, uint32_t value
)
813 *d
++ = (value
>>= 8);
816 static int cris_save_8 (unsigned char *d
, uint32_t value
)
822 /* FIXME: this will bug on archs not supporting unaligned word accesses. */
823 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
825 uint8_t *ptr
= mem_buf
;
829 for (i
= 0; i
< 16; i
++)
830 ptr
+= cris_save_32 (ptr
, env
->regs
[i
]);
832 srs
= env
->pregs
[PR_SRS
];
834 ptr
+= cris_save_8 (ptr
, env
->pregs
[0]);
835 ptr
+= cris_save_8 (ptr
, env
->pregs
[1]);
836 ptr
+= cris_save_32 (ptr
, env
->pregs
[2]);
837 ptr
+= cris_save_8 (ptr
, srs
);
838 ptr
+= cris_save_16 (ptr
, env
->pregs
[4]);
840 for (i
= 5; i
< 16; i
++)
841 ptr
+= cris_save_32 (ptr
, env
->pregs
[i
]);
843 ptr
+= cris_save_32 (ptr
, env
->pc
);
845 for (i
= 0; i
< 16; i
++)
846 ptr
+= cris_save_32 (ptr
, env
->sregs
[srs
][i
]);
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 for (i
= 0; i
< 16; i
++) LOAD(env
->regs
[i
]);
861 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
866 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
872 static int gdb_handle_packet(GDBState
*s
, CPUState
*env
, const char *line_buf
)
875 int ch
, reg_size
, type
;
877 uint8_t mem_buf
[4096];
879 target_ulong addr
, len
;
882 printf("command='%s'\n", line_buf
);
888 /* TODO: Make this return the correct value for user-mode. */
889 snprintf(buf
, sizeof(buf
), "S%02x", SIGTRAP
);
894 addr
= strtoull(p
, (char **)&p
, 16);
895 #if defined(TARGET_I386)
898 kvm_load_registers(env
);
899 #elif defined (TARGET_PPC)
901 #elif defined (TARGET_SPARC)
904 #elif defined (TARGET_ARM)
905 env
->regs
[15] = addr
;
906 #elif defined (TARGET_SH4)
908 #elif defined (TARGET_MIPS)
909 env
->PC
[env
->current_tc
] = addr
;
910 #elif defined (TARGET_CRIS)
914 #ifdef CONFIG_USER_ONLY
915 s
->running_state
= 1;
922 addr
= strtoull(p
, (char **)&p
, 16);
923 #if defined(TARGET_I386)
926 kvm_load_registers(env
);
927 #elif defined (TARGET_PPC)
929 #elif defined (TARGET_SPARC)
932 #elif defined (TARGET_ARM)
933 env
->regs
[15] = addr
;
934 #elif defined (TARGET_SH4)
936 #elif defined (TARGET_MIPS)
937 env
->PC
[env
->current_tc
] = addr
;
938 #elif defined (TARGET_CRIS)
942 cpu_single_step(env
, 1);
943 #ifdef CONFIG_USER_ONLY
944 s
->running_state
= 1;
954 ret
= strtoull(p
, (char **)&p
, 16);
957 err
= strtoull(p
, (char **)&p
, 16);
964 if (gdb_current_syscall_cb
)
965 gdb_current_syscall_cb(s
->env
, ret
, err
);
967 put_packet(s
, "T02");
969 #ifdef CONFIG_USER_ONLY
970 s
->running_state
= 1;
979 kvm_save_registers(env
);
980 reg_size
= cpu_gdb_read_registers(env
, mem_buf
);
981 memtohex(buf
, mem_buf
, reg_size
);
985 registers
= (void *)mem_buf
;
987 hextomem((uint8_t *)registers
, p
, len
);
988 cpu_gdb_write_registers(env
, mem_buf
, len
);
990 kvm_load_registers(env
);
994 addr
= strtoull(p
, (char **)&p
, 16);
997 len
= strtoull(p
, NULL
, 16);
998 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 0) != 0) {
999 put_packet (s
, "E14");
1001 memtohex(buf
, mem_buf
, len
);
1006 addr
= strtoull(p
, (char **)&p
, 16);
1009 len
= strtoull(p
, (char **)&p
, 16);
1012 hextomem(mem_buf
, p
, len
);
1013 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 1) != 0)
1014 put_packet(s
, "E14");
1016 put_packet(s
, "OK");
1019 type
= strtoul(p
, (char **)&p
, 16);
1022 addr
= strtoull(p
, (char **)&p
, 16);
1025 len
= strtoull(p
, (char **)&p
, 16);
1026 if (type
== 0 || type
== 1) {
1027 if (cpu_breakpoint_insert(env
, addr
) < 0)
1028 goto breakpoint_error
;
1029 put_packet(s
, "OK");
1030 #ifndef CONFIG_USER_ONLY
1031 } else if (type
== 2) {
1032 if (cpu_watchpoint_insert(env
, addr
) < 0)
1033 goto breakpoint_error
;
1034 put_packet(s
, "OK");
1038 put_packet(s
, "E22");
1042 type
= strtoul(p
, (char **)&p
, 16);
1045 addr
= strtoull(p
, (char **)&p
, 16);
1048 len
= strtoull(p
, (char **)&p
, 16);
1049 if (type
== 0 || type
== 1) {
1050 cpu_breakpoint_remove(env
, addr
);
1051 put_packet(s
, "OK");
1052 #ifndef CONFIG_USER_ONLY
1053 } else if (type
== 2) {
1054 cpu_watchpoint_remove(env
, addr
);
1055 put_packet(s
, "OK");
1058 goto breakpoint_error
;
1061 #ifdef CONFIG_LINUX_USER
1063 if (strncmp(p
, "Offsets", 7) == 0) {
1064 TaskState
*ts
= env
->opaque
;
1067 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1068 ";Bss=" TARGET_ABI_FMT_lx
,
1069 ts
->info
->code_offset
,
1070 ts
->info
->data_offset
,
1071 ts
->info
->data_offset
);
1079 /* put empty packet */
1087 extern void tb_flush(CPUState
*env
);
1089 #ifndef CONFIG_USER_ONLY
1090 static void gdb_vm_stopped(void *opaque
, int reason
)
1092 GDBState
*s
= opaque
;
1096 if (s
->state
== RS_SYSCALL
)
1099 /* disable single step if it was enable */
1100 cpu_single_step(s
->env
, 0);
1102 if (reason
== EXCP_DEBUG
) {
1103 if (s
->env
->watchpoint_hit
) {
1104 snprintf(buf
, sizeof(buf
), "T%02xwatch:" TARGET_FMT_lx
";",
1106 s
->env
->watchpoint
[s
->env
->watchpoint_hit
- 1].vaddr
);
1108 s
->env
->watchpoint_hit
= 0;
1113 } else if (reason
== EXCP_INTERRUPT
) {
1118 snprintf(buf
, sizeof(buf
), "S%02x", ret
);
1123 /* Send a gdb syscall request.
1124 This accepts limited printf-style format specifiers, specifically:
1125 %x - target_ulong argument printed in hex.
1126 %lx - 64-bit argument printed in hex.
1127 %s - string pointer (target_ulong) and length (int) pair. */
1128 void gdb_do_syscall(gdb_syscall_complete_cb cb
, char *fmt
, ...)
1137 s
= gdb_syscall_state
;
1140 gdb_current_syscall_cb
= cb
;
1141 s
->state
= RS_SYSCALL
;
1142 #ifndef CONFIG_USER_ONLY
1143 vm_stop(EXCP_DEBUG
);
1154 addr
= va_arg(va
, target_ulong
);
1155 p
+= sprintf(p
, TARGET_FMT_lx
, addr
);
1158 if (*(fmt
++) != 'x')
1160 i64
= va_arg(va
, uint64_t);
1161 p
+= sprintf(p
, "%" PRIx64
, i64
);
1164 addr
= va_arg(va
, target_ulong
);
1165 p
+= sprintf(p
, TARGET_FMT_lx
"/%x", addr
, va_arg(va
, int));
1169 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
1180 #ifdef CONFIG_USER_ONLY
1181 gdb_handlesig(s
->env
, 0);
1183 cpu_interrupt(s
->env
, CPU_INTERRUPT_EXIT
);
1187 static void gdb_read_byte(GDBState
*s
, int ch
)
1189 CPUState
*env
= s
->env
;
1193 #ifndef CONFIG_USER_ONLY
1194 if (s
->last_packet_len
) {
1195 /* Waiting for a response to the last packet. If we see the start
1196 of a new command then abandon the previous response. */
1199 printf("Got NACK, retransmitting\n");
1201 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
1205 printf("Got ACK\n");
1207 printf("Got '%c' when expecting ACK/NACK\n", ch
);
1209 if (ch
== '+' || ch
== '$')
1210 s
->last_packet_len
= 0;
1215 /* when the CPU is running, we cannot do anything except stop
1216 it when receiving a char */
1217 vm_stop(EXCP_INTERRUPT
);
1224 s
->line_buf_index
= 0;
1225 s
->state
= RS_GETLINE
;
1230 s
->state
= RS_CHKSUM1
;
1231 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
1234 s
->line_buf
[s
->line_buf_index
++] = ch
;
1238 s
->line_buf
[s
->line_buf_index
] = '\0';
1239 s
->line_csum
= fromhex(ch
) << 4;
1240 s
->state
= RS_CHKSUM2
;
1243 s
->line_csum
|= fromhex(ch
);
1245 for(i
= 0; i
< s
->line_buf_index
; i
++) {
1246 csum
+= s
->line_buf
[i
];
1248 if (s
->line_csum
!= (csum
& 0xff)) {
1250 put_buffer(s
, &reply
, 1);
1254 put_buffer(s
, &reply
, 1);
1255 s
->state
= gdb_handle_packet(s
, env
, s
->line_buf
);
1264 #ifdef CONFIG_USER_ONLY
1266 gdb_handlesig (CPUState
*env
, int sig
)
1272 if (gdbserver_fd
< 0)
1275 s
= &gdbserver_state
;
1277 /* disable single step if it was enabled */
1278 cpu_single_step(env
, 0);
1283 snprintf(buf
, sizeof(buf
), "S%02x", sig
);
1289 s
->running_state
= 0;
1290 while (s
->running_state
== 0) {
1291 n
= read (s
->fd
, buf
, 256);
1296 for (i
= 0; i
< n
; i
++)
1297 gdb_read_byte (s
, buf
[i
]);
1299 else if (n
== 0 || errno
!= EAGAIN
)
1301 /* XXX: Connection closed. Should probably wait for annother
1302 connection before continuing. */
1309 /* Tell the remote gdb that the process has exited. */
1310 void gdb_exit(CPUState
*env
, int code
)
1315 if (gdbserver_fd
< 0)
1318 s
= &gdbserver_state
;
1320 snprintf(buf
, sizeof(buf
), "W%02x", code
);
1325 static void gdb_accept(void *opaque
)
1328 struct sockaddr_in sockaddr
;
1333 len
= sizeof(sockaddr
);
1334 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
1335 if (fd
< 0 && errno
!= EINTR
) {
1338 } else if (fd
>= 0) {
1343 /* set short latency */
1345 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1347 s
= &gdbserver_state
;
1348 memset (s
, 0, sizeof (GDBState
));
1349 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1352 gdb_syscall_state
= s
;
1354 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1357 static int gdbserver_open(int port
)
1359 struct sockaddr_in sockaddr
;
1362 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1368 /* allow fast reuse */
1370 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
1372 sockaddr
.sin_family
= AF_INET
;
1373 sockaddr
.sin_port
= htons(port
);
1374 sockaddr
.sin_addr
.s_addr
= 0;
1375 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
1380 ret
= listen(fd
, 0);
1388 int gdbserver_start(int port
)
1390 gdbserver_fd
= gdbserver_open(port
);
1391 if (gdbserver_fd
< 0)
1393 /* accept connections */
1398 static int gdb_chr_can_receive(void *opaque
)
1403 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
1405 GDBState
*s
= opaque
;
1408 for (i
= 0; i
< size
; i
++) {
1409 gdb_read_byte(s
, buf
[i
]);
1413 static void gdb_chr_event(void *opaque
, int event
)
1416 case CHR_EVENT_RESET
:
1417 vm_stop(EXCP_INTERRUPT
);
1418 gdb_syscall_state
= opaque
;
1425 int gdbserver_start(const char *port
)
1428 char gdbstub_port_name
[128];
1431 CharDriverState
*chr
;
1433 if (!port
|| !*port
)
1436 port_num
= strtol(port
, &p
, 10);
1438 /* A numeric value is interpreted as a port number. */
1439 snprintf(gdbstub_port_name
, sizeof(gdbstub_port_name
),
1440 "tcp::%d,nowait,nodelay,server", port_num
);
1441 port
= gdbstub_port_name
;
1444 chr
= qemu_chr_open(port
);
1448 s
= qemu_mallocz(sizeof(GDBState
));
1452 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1454 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
1456 qemu_add_vm_stop_handler(gdb_vm_stopped
, s
);