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 /* Resume execution. */
125 static inline void gdb_continue(GDBState
*s
)
127 #ifdef CONFIG_USER_ONLY
128 s
->running_state
= 1;
134 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
136 #ifdef CONFIG_USER_ONLY
140 ret
= send(s
->fd
, buf
, len
, 0);
142 if (errno
!= EINTR
&& errno
!= EAGAIN
)
150 qemu_chr_write(s
->chr
, buf
, len
);
154 static inline int fromhex(int v
)
156 if (v
>= '0' && v
<= '9')
158 else if (v
>= 'A' && v
<= 'F')
160 else if (v
>= 'a' && v
<= 'f')
166 static inline int tohex(int v
)
174 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
179 for(i
= 0; i
< len
; i
++) {
181 *q
++ = tohex(c
>> 4);
182 *q
++ = tohex(c
& 0xf);
187 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
191 for(i
= 0; i
< len
; i
++) {
192 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
197 /* return -1 if error, 0 if OK */
198 static int put_packet(GDBState
*s
, char *buf
)
204 printf("reply='%s'\n", buf
);
214 for(i
= 0; i
< len
; i
++) {
218 *(p
++) = tohex((csum
>> 4) & 0xf);
219 *(p
++) = tohex((csum
) & 0xf);
221 s
->last_packet_len
= p
- s
->last_packet
;
222 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
224 #ifdef CONFIG_USER_ONLY
237 #if defined(TARGET_I386)
239 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
242 uint32_t *registers
= (uint32_t *)mem_buf
;
245 /* This corresponds with amd64_register_info[] in gdb/amd64-tdep.c */
246 uint64_t *registers64
= (uint64_t *)mem_buf
;
248 if (env
->hflags
& HF_CS64_MASK
) {
249 registers64
[0] = tswap64(env
->regs
[R_EAX
]);
250 registers64
[1] = tswap64(env
->regs
[R_EBX
]);
251 registers64
[2] = tswap64(env
->regs
[R_ECX
]);
252 registers64
[3] = tswap64(env
->regs
[R_EDX
]);
253 registers64
[4] = tswap64(env
->regs
[R_ESI
]);
254 registers64
[5] = tswap64(env
->regs
[R_EDI
]);
255 registers64
[6] = tswap64(env
->regs
[R_EBP
]);
256 registers64
[7] = tswap64(env
->regs
[R_ESP
]);
257 for(i
= 8; i
< 16; i
++) {
258 registers64
[i
] = tswap64(env
->regs
[i
]);
260 registers64
[16] = tswap64(env
->eip
);
262 registers
= (uint32_t *)®isters64
[17];
263 registers
[0] = tswap32(env
->eflags
);
264 registers
[1] = tswap32(env
->segs
[R_CS
].selector
);
265 registers
[2] = tswap32(env
->segs
[R_SS
].selector
);
266 registers
[3] = tswap32(env
->segs
[R_DS
].selector
);
267 registers
[4] = tswap32(env
->segs
[R_ES
].selector
);
268 registers
[5] = tswap32(env
->segs
[R_FS
].selector
);
269 registers
[6] = tswap32(env
->segs
[R_GS
].selector
);
270 /* XXX: convert floats */
271 for(i
= 0; i
< 8; i
++) {
272 memcpy(mem_buf
+ 16 * 8 + 7 * 4 + i
* 10, &env
->fpregs
[i
], 10);
274 registers
[27] = tswap32(env
->fpuc
); /* fctrl */
275 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
276 registers
[28] = tswap32(fpus
); /* fstat */
277 registers
[29] = 0; /* ftag */
278 registers
[30] = 0; /* fiseg */
279 registers
[31] = 0; /* fioff */
280 registers
[32] = 0; /* foseg */
281 registers
[33] = 0; /* fooff */
282 registers
[34] = 0; /* fop */
283 for(i
= 0; i
< 16; i
++) {
284 memcpy(mem_buf
+ 16 * 8 + 35 * 4 + i
* 16, &env
->xmm_regs
[i
], 16);
286 registers
[99] = tswap32(env
->mxcsr
);
288 return 8 * 17 + 4 * 7 + 10 * 8 + 4 * 8 + 16 * 16 + 4;
292 for(i
= 0; i
< 8; i
++) {
293 registers
[i
] = env
->regs
[i
];
295 registers
[8] = env
->eip
;
296 registers
[9] = env
->eflags
;
297 registers
[10] = env
->segs
[R_CS
].selector
;
298 registers
[11] = env
->segs
[R_SS
].selector
;
299 registers
[12] = env
->segs
[R_DS
].selector
;
300 registers
[13] = env
->segs
[R_ES
].selector
;
301 registers
[14] = env
->segs
[R_FS
].selector
;
302 registers
[15] = env
->segs
[R_GS
].selector
;
303 /* XXX: convert floats */
304 for(i
= 0; i
< 8; i
++) {
305 memcpy(mem_buf
+ 16 * 4 + i
* 10, &env
->fpregs
[i
], 10);
307 registers
[36] = env
->fpuc
;
308 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
309 registers
[37] = fpus
;
310 registers
[38] = 0; /* XXX: convert tags */
311 registers
[39] = 0; /* fiseg */
312 registers
[40] = 0; /* fioff */
313 registers
[41] = 0; /* foseg */
314 registers
[42] = 0; /* fooff */
315 registers
[43] = 0; /* fop */
317 for(i
= 0; i
< 16; i
++)
318 tswapls(®isters
[i
]);
319 for(i
= 36; i
< 44; i
++)
320 tswapls(®isters
[i
]);
324 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
326 uint32_t *registers
= (uint32_t *)mem_buf
;
329 for(i
= 0; i
< 8; i
++) {
330 env
->regs
[i
] = tswapl(registers
[i
]);
332 env
->eip
= tswapl(registers
[8]);
333 env
->eflags
= tswapl(registers
[9]);
334 #if defined(CONFIG_USER_ONLY)
335 #define LOAD_SEG(index, sreg)\
336 if (tswapl(registers[index]) != env->segs[sreg].selector)\
337 cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
347 #elif defined (TARGET_PPC)
348 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
350 uint32_t *registers
= (uint32_t *)mem_buf
, tmp
;
354 for(i
= 0; i
< 32; i
++) {
355 registers
[i
] = tswapl(env
->gpr
[i
]);
358 for (i
= 0; i
< 32; i
++) {
359 registers
[(i
* 2) + 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
360 registers
[(i
* 2) + 33] = tswapl(*((uint32_t *)&env
->fpr
[i
] + 1));
362 /* nip, msr, ccr, lnk, ctr, xer, mq */
363 registers
[96] = tswapl(env
->nip
);
364 registers
[97] = tswapl(env
->msr
);
366 for (i
= 0; i
< 8; i
++)
367 tmp
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
368 registers
[98] = tswapl(tmp
);
369 registers
[99] = tswapl(env
->lr
);
370 registers
[100] = tswapl(env
->ctr
);
371 registers
[101] = tswapl(ppc_load_xer(env
));
377 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
379 uint32_t *registers
= (uint32_t *)mem_buf
;
383 for (i
= 0; i
< 32; i
++) {
384 env
->gpr
[i
] = tswapl(registers
[i
]);
387 for (i
= 0; i
< 32; i
++) {
388 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[(i
* 2) + 32]);
389 *((uint32_t *)&env
->fpr
[i
] + 1) = tswapl(registers
[(i
* 2) + 33]);
391 /* nip, msr, ccr, lnk, ctr, xer, mq */
392 env
->nip
= tswapl(registers
[96]);
393 ppc_store_msr(env
, tswapl(registers
[97]));
394 registers
[98] = tswapl(registers
[98]);
395 for (i
= 0; i
< 8; i
++)
396 env
->crf
[i
] = (registers
[98] >> (32 - ((i
+ 1) * 4))) & 0xF;
397 env
->lr
= tswapl(registers
[99]);
398 env
->ctr
= tswapl(registers
[100]);
399 ppc_store_xer(env
, tswapl(registers
[101]));
401 #elif defined (TARGET_SPARC)
402 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
404 target_ulong
*registers
= (target_ulong
*)mem_buf
;
408 for(i
= 0; i
< 8; i
++) {
409 registers
[i
] = tswapl(env
->gregs
[i
]);
411 /* fill in register window */
412 for(i
= 0; i
< 24; i
++) {
413 registers
[i
+ 8] = tswapl(env
->regwptr
[i
]);
415 #ifndef TARGET_SPARC64
417 for (i
= 0; i
< 32; i
++) {
418 registers
[i
+ 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
420 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
421 registers
[64] = tswapl(env
->y
);
426 registers
[65] = tswapl(tmp
);
428 registers
[66] = tswapl(env
->wim
);
429 registers
[67] = tswapl(env
->tbr
);
430 registers
[68] = tswapl(env
->pc
);
431 registers
[69] = tswapl(env
->npc
);
432 registers
[70] = tswapl(env
->fsr
);
433 registers
[71] = 0; /* csr */
435 return 73 * sizeof(target_ulong
);
438 for (i
= 0; i
< 64; i
+= 2) {
441 tmp
= ((uint64_t)*(uint32_t *)&env
->fpr
[i
]) << 32;
442 tmp
|= *(uint32_t *)&env
->fpr
[i
+ 1];
443 registers
[i
/ 2 + 32] = tswap64(tmp
);
445 registers
[64] = tswapl(env
->pc
);
446 registers
[65] = tswapl(env
->npc
);
447 registers
[66] = tswapl(((uint64_t)GET_CCR(env
) << 32) |
448 ((env
->asi
& 0xff) << 24) |
449 ((env
->pstate
& 0xfff) << 8) |
451 registers
[67] = tswapl(env
->fsr
);
452 registers
[68] = tswapl(env
->fprs
);
453 registers
[69] = tswapl(env
->y
);
454 return 70 * sizeof(target_ulong
);
458 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
460 target_ulong
*registers
= (target_ulong
*)mem_buf
;
464 for(i
= 0; i
< 7; i
++) {
465 env
->gregs
[i
] = tswapl(registers
[i
]);
467 /* fill in register window */
468 for(i
= 0; i
< 24; i
++) {
469 env
->regwptr
[i
] = tswapl(registers
[i
+ 8]);
471 #ifndef TARGET_SPARC64
473 for (i
= 0; i
< 32; i
++) {
474 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[i
+ 32]);
476 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
477 env
->y
= tswapl(registers
[64]);
478 PUT_PSR(env
, tswapl(registers
[65]));
479 env
->wim
= tswapl(registers
[66]);
480 env
->tbr
= tswapl(registers
[67]);
481 env
->pc
= tswapl(registers
[68]);
482 env
->npc
= tswapl(registers
[69]);
483 env
->fsr
= tswapl(registers
[70]);
485 for (i
= 0; i
< 64; i
+= 2) {
488 tmp
= tswap64(registers
[i
/ 2 + 32]);
489 *((uint32_t *)&env
->fpr
[i
]) = tmp
>> 32;
490 *((uint32_t *)&env
->fpr
[i
+ 1]) = tmp
& 0xffffffff;
492 env
->pc
= tswapl(registers
[64]);
493 env
->npc
= tswapl(registers
[65]);
495 uint64_t tmp
= tswapl(registers
[66]);
497 PUT_CCR(env
, tmp
>> 32);
498 env
->asi
= (tmp
>> 24) & 0xff;
499 env
->pstate
= (tmp
>> 8) & 0xfff;
500 PUT_CWP64(env
, tmp
& 0xff);
502 env
->fsr
= tswapl(registers
[67]);
503 env
->fprs
= tswapl(registers
[68]);
504 env
->y
= tswapl(registers
[69]);
507 #elif defined (TARGET_ARM)
508 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
514 /* 16 core integer registers (4 bytes each). */
515 for (i
= 0; i
< 16; i
++)
517 *(uint32_t *)ptr
= tswapl(env
->regs
[i
]);
520 /* 8 FPA registers (12 bytes each), FPS (4 bytes).
521 Not yet implemented. */
522 memset (ptr
, 0, 8 * 12 + 4);
524 /* CPSR (4 bytes). */
525 *(uint32_t *)ptr
= tswapl (cpsr_read(env
));
528 return ptr
- mem_buf
;
531 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
537 /* Core integer registers. */
538 for (i
= 0; i
< 16; i
++)
540 env
->regs
[i
] = tswapl(*(uint32_t *)ptr
);
543 /* Ignore FPA regs and scr. */
545 cpsr_write (env
, tswapl(*(uint32_t *)ptr
), 0xffffffff);
547 #elif defined (TARGET_M68K)
548 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
556 for (i
= 0; i
< 8; i
++) {
557 *(uint32_t *)ptr
= tswapl(env
->dregs
[i
]);
561 for (i
= 0; i
< 8; i
++) {
562 *(uint32_t *)ptr
= tswapl(env
->aregs
[i
]);
565 *(uint32_t *)ptr
= tswapl(env
->sr
);
567 *(uint32_t *)ptr
= tswapl(env
->pc
);
569 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
570 ColdFire has 8-bit double precision registers. */
571 for (i
= 0; i
< 8; i
++) {
573 *(uint32_t *)ptr
= tswap32(u
.l
.upper
);
574 *(uint32_t *)ptr
= tswap32(u
.l
.lower
);
576 /* FP control regs (not implemented). */
577 memset (ptr
, 0, 3 * 4);
580 return ptr
- mem_buf
;
583 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
591 for (i
= 0; i
< 8; i
++) {
592 env
->dregs
[i
] = tswapl(*(uint32_t *)ptr
);
596 for (i
= 0; i
< 8; i
++) {
597 env
->aregs
[i
] = tswapl(*(uint32_t *)ptr
);
600 env
->sr
= tswapl(*(uint32_t *)ptr
);
602 env
->pc
= tswapl(*(uint32_t *)ptr
);
604 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
605 ColdFire has 8-bit double precision registers. */
606 for (i
= 0; i
< 8; i
++) {
607 u
.l
.upper
= tswap32(*(uint32_t *)ptr
);
608 u
.l
.lower
= tswap32(*(uint32_t *)ptr
);
611 /* FP control regs (not implemented). */
614 #elif defined (TARGET_MIPS)
615 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
621 for (i
= 0; i
< 32; i
++)
623 *(target_ulong
*)ptr
= tswapl(env
->gpr
[env
->current_tc
][i
]);
624 ptr
+= sizeof(target_ulong
);
627 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_Status
);
628 ptr
+= sizeof(target_ulong
);
630 *(target_ulong
*)ptr
= tswapl(env
->LO
[env
->current_tc
][0]);
631 ptr
+= sizeof(target_ulong
);
633 *(target_ulong
*)ptr
= tswapl(env
->HI
[env
->current_tc
][0]);
634 ptr
+= sizeof(target_ulong
);
636 *(target_ulong
*)ptr
= tswapl(env
->CP0_BadVAddr
);
637 ptr
+= sizeof(target_ulong
);
639 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_Cause
);
640 ptr
+= sizeof(target_ulong
);
642 *(target_ulong
*)ptr
= tswapl(env
->PC
[env
->current_tc
]);
643 ptr
+= sizeof(target_ulong
);
645 if (env
->CP0_Config1
& (1 << CP0C1_FP
))
647 for (i
= 0; i
< 32; i
++)
649 if (env
->CP0_Status
& (1 << CP0St_FR
))
650 *(target_ulong
*)ptr
= tswapl(env
->fpu
->fpr
[i
].d
);
652 *(target_ulong
*)ptr
= tswap32(env
->fpu
->fpr
[i
].w
[FP_ENDIAN_IDX
]);
653 ptr
+= sizeof(target_ulong
);
656 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->fpu
->fcr31
);
657 ptr
+= sizeof(target_ulong
);
659 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->fpu
->fcr0
);
660 ptr
+= sizeof(target_ulong
);
663 /* "fp", pseudo frame pointer. Not yet implemented in gdb. */
664 *(target_ulong
*)ptr
= 0;
665 ptr
+= sizeof(target_ulong
);
667 /* Registers for embedded use, we just pad them. */
668 for (i
= 0; i
< 16; i
++)
670 *(target_ulong
*)ptr
= 0;
671 ptr
+= sizeof(target_ulong
);
675 *(target_ulong
*)ptr
= (int32_t)tswap32(env
->CP0_PRid
);
676 ptr
+= sizeof(target_ulong
);
678 return ptr
- mem_buf
;
681 /* convert MIPS rounding mode in FCR31 to IEEE library */
682 static unsigned int ieee_rm
[] =
684 float_round_nearest_even
,
689 #define RESTORE_ROUNDING_MODE \
690 set_float_rounding_mode(ieee_rm[env->fpu->fcr31 & 3], &env->fpu->fp_status)
692 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
698 for (i
= 0; i
< 32; i
++)
700 env
->gpr
[env
->current_tc
][i
] = tswapl(*(target_ulong
*)ptr
);
701 ptr
+= sizeof(target_ulong
);
704 env
->CP0_Status
= tswapl(*(target_ulong
*)ptr
);
705 ptr
+= sizeof(target_ulong
);
707 env
->LO
[env
->current_tc
][0] = tswapl(*(target_ulong
*)ptr
);
708 ptr
+= sizeof(target_ulong
);
710 env
->HI
[env
->current_tc
][0] = tswapl(*(target_ulong
*)ptr
);
711 ptr
+= sizeof(target_ulong
);
713 env
->CP0_BadVAddr
= tswapl(*(target_ulong
*)ptr
);
714 ptr
+= sizeof(target_ulong
);
716 env
->CP0_Cause
= tswapl(*(target_ulong
*)ptr
);
717 ptr
+= sizeof(target_ulong
);
719 env
->PC
[env
->current_tc
] = tswapl(*(target_ulong
*)ptr
);
720 ptr
+= sizeof(target_ulong
);
722 if (env
->CP0_Config1
& (1 << CP0C1_FP
))
724 for (i
= 0; i
< 32; i
++)
726 if (env
->CP0_Status
& (1 << CP0St_FR
))
727 env
->fpu
->fpr
[i
].d
= tswapl(*(target_ulong
*)ptr
);
729 env
->fpu
->fpr
[i
].w
[FP_ENDIAN_IDX
] = tswapl(*(target_ulong
*)ptr
);
730 ptr
+= sizeof(target_ulong
);
733 env
->fpu
->fcr31
= tswapl(*(target_ulong
*)ptr
) & 0xFF83FFFF;
734 ptr
+= sizeof(target_ulong
);
736 /* The remaining registers are assumed to be read-only. */
738 /* set rounding mode */
739 RESTORE_ROUNDING_MODE
;
741 #ifndef CONFIG_SOFTFLOAT
742 /* no floating point exception for native float */
743 SET_FP_ENABLE(env
->fcr31
, 0);
747 #elif defined (TARGET_SH4)
749 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
751 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
753 uint32_t *ptr
= (uint32_t *)mem_buf
;
756 #define SAVE(x) *ptr++=tswapl(x)
757 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
758 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
+ 16]);
760 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
]);
762 for (i
= 8; i
< 16; i
++) SAVE(env
->gregs
[i
]);
772 for (i
= 0; i
< 16; i
++)
773 SAVE(env
->fregs
[i
+ ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
776 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
]);
777 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
+ 16]);
778 return ((uint8_t *)ptr
- mem_buf
);
781 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
783 uint32_t *ptr
= (uint32_t *)mem_buf
;
786 #define LOAD(x) (x)=*ptr++;
787 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
788 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
+ 16]);
790 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
]);
792 for (i
= 8; i
< 16; i
++) LOAD(env
->gregs
[i
]);
802 for (i
= 0; i
< 16; i
++)
803 LOAD(env
->fregs
[i
+ ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
806 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
]);
807 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
+ 16]);
809 #elif defined (TARGET_CRIS)
811 static int cris_save_32 (unsigned char *d
, uint32_t value
)
814 *d
++ = (value
>>= 8);
815 *d
++ = (value
>>= 8);
816 *d
++ = (value
>>= 8);
819 static int cris_save_16 (unsigned char *d
, uint32_t value
)
822 *d
++ = (value
>>= 8);
825 static int cris_save_8 (unsigned char *d
, uint32_t value
)
831 /* FIXME: this will bug on archs not supporting unaligned word accesses. */
832 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
834 uint8_t *ptr
= mem_buf
;
838 for (i
= 0; i
< 16; i
++)
839 ptr
+= cris_save_32 (ptr
, env
->regs
[i
]);
841 srs
= env
->pregs
[PR_SRS
];
843 ptr
+= cris_save_8 (ptr
, env
->pregs
[0]);
844 ptr
+= cris_save_8 (ptr
, env
->pregs
[1]);
845 ptr
+= cris_save_32 (ptr
, env
->pregs
[2]);
846 ptr
+= cris_save_8 (ptr
, srs
);
847 ptr
+= cris_save_16 (ptr
, env
->pregs
[4]);
849 for (i
= 5; i
< 16; i
++)
850 ptr
+= cris_save_32 (ptr
, env
->pregs
[i
]);
852 ptr
+= cris_save_32 (ptr
, env
->pc
);
854 for (i
= 0; i
< 16; i
++)
855 ptr
+= cris_save_32 (ptr
, env
->sregs
[srs
][i
]);
857 return ((uint8_t *)ptr
- mem_buf
);
860 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
862 uint32_t *ptr
= (uint32_t *)mem_buf
;
865 #define LOAD(x) (x)=*ptr++;
866 for (i
= 0; i
< 16; i
++) LOAD(env
->regs
[i
]);
870 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
875 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
881 static int gdb_handle_packet(GDBState
*s
, CPUState
*env
, const char *line_buf
)
884 int ch
, reg_size
, type
;
886 uint8_t mem_buf
[4096];
888 target_ulong addr
, len
;
891 printf("command='%s'\n", line_buf
);
897 /* TODO: Make this return the correct value for user-mode. */
898 snprintf(buf
, sizeof(buf
), "S%02x", SIGTRAP
);
903 addr
= strtoull(p
, (char **)&p
, 16);
904 #if defined(TARGET_I386)
906 #elif defined (TARGET_PPC)
908 #elif defined (TARGET_SPARC)
911 #elif defined (TARGET_ARM)
912 env
->regs
[15] = addr
;
913 #elif defined (TARGET_SH4)
915 #elif defined (TARGET_MIPS)
916 env
->PC
[env
->current_tc
] = addr
;
917 #elif defined (TARGET_CRIS)
925 addr
= strtoull(p
, (char **)&p
, 16);
926 #if defined(TARGET_I386)
928 #elif defined (TARGET_PPC)
930 #elif defined (TARGET_SPARC)
933 #elif defined (TARGET_ARM)
934 env
->regs
[15] = addr
;
935 #elif defined (TARGET_SH4)
937 #elif defined (TARGET_MIPS)
938 env
->PC
[env
->current_tc
] = addr
;
939 #elif defined (TARGET_CRIS)
943 cpu_single_step(env
, 1);
951 ret
= strtoull(p
, (char **)&p
, 16);
954 err
= strtoull(p
, (char **)&p
, 16);
961 if (gdb_current_syscall_cb
)
962 gdb_current_syscall_cb(s
->env
, ret
, err
);
964 put_packet(s
, "T02");
971 reg_size
= cpu_gdb_read_registers(env
, mem_buf
);
972 memtohex(buf
, mem_buf
, reg_size
);
976 registers
= (void *)mem_buf
;
978 hextomem((uint8_t *)registers
, p
, len
);
979 cpu_gdb_write_registers(env
, mem_buf
, len
);
983 addr
= strtoull(p
, (char **)&p
, 16);
986 len
= strtoull(p
, NULL
, 16);
987 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 0) != 0) {
988 put_packet (s
, "E14");
990 memtohex(buf
, mem_buf
, len
);
995 addr
= strtoull(p
, (char **)&p
, 16);
998 len
= strtoull(p
, (char **)&p
, 16);
1001 hextomem(mem_buf
, p
, len
);
1002 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 1) != 0)
1003 put_packet(s
, "E14");
1005 put_packet(s
, "OK");
1008 type
= strtoul(p
, (char **)&p
, 16);
1011 addr
= strtoull(p
, (char **)&p
, 16);
1014 len
= strtoull(p
, (char **)&p
, 16);
1015 if (type
== 0 || type
== 1) {
1016 if (cpu_breakpoint_insert(env
, addr
) < 0)
1017 goto breakpoint_error
;
1018 put_packet(s
, "OK");
1019 #ifndef CONFIG_USER_ONLY
1020 } else if (type
== 2) {
1021 if (cpu_watchpoint_insert(env
, addr
) < 0)
1022 goto breakpoint_error
;
1023 put_packet(s
, "OK");
1027 put_packet(s
, "E22");
1031 type
= strtoul(p
, (char **)&p
, 16);
1034 addr
= strtoull(p
, (char **)&p
, 16);
1037 len
= strtoull(p
, (char **)&p
, 16);
1038 if (type
== 0 || type
== 1) {
1039 cpu_breakpoint_remove(env
, addr
);
1040 put_packet(s
, "OK");
1041 #ifndef CONFIG_USER_ONLY
1042 } else if (type
== 2) {
1043 cpu_watchpoint_remove(env
, addr
);
1044 put_packet(s
, "OK");
1047 goto breakpoint_error
;
1050 #ifdef CONFIG_LINUX_USER
1052 if (strncmp(p
, "Offsets", 7) == 0) {
1053 TaskState
*ts
= env
->opaque
;
1056 "Text=" TARGET_ABI_FMT_lx
";Data=" TARGET_ABI_FMT_lx
1057 ";Bss=" TARGET_ABI_FMT_lx
,
1058 ts
->info
->code_offset
,
1059 ts
->info
->data_offset
,
1060 ts
->info
->data_offset
);
1068 /* put empty packet */
1076 extern void tb_flush(CPUState
*env
);
1078 #ifndef CONFIG_USER_ONLY
1079 static void gdb_vm_stopped(void *opaque
, int reason
)
1081 GDBState
*s
= opaque
;
1085 if (s
->state
== RS_SYSCALL
)
1088 /* disable single step if it was enable */
1089 cpu_single_step(s
->env
, 0);
1091 if (reason
== EXCP_DEBUG
) {
1092 if (s
->env
->watchpoint_hit
) {
1093 snprintf(buf
, sizeof(buf
), "T%02xwatch:" TARGET_FMT_lx
";",
1095 s
->env
->watchpoint
[s
->env
->watchpoint_hit
- 1].vaddr
);
1097 s
->env
->watchpoint_hit
= 0;
1102 } else if (reason
== EXCP_INTERRUPT
) {
1107 snprintf(buf
, sizeof(buf
), "S%02x", ret
);
1112 /* Send a gdb syscall request.
1113 This accepts limited printf-style format specifiers, specifically:
1114 %x - target_ulong argument printed in hex.
1115 %lx - 64-bit argument printed in hex.
1116 %s - string pointer (target_ulong) and length (int) pair. */
1117 void gdb_do_syscall(gdb_syscall_complete_cb cb
, char *fmt
, ...)
1126 s
= gdb_syscall_state
;
1129 gdb_current_syscall_cb
= cb
;
1130 s
->state
= RS_SYSCALL
;
1131 #ifndef CONFIG_USER_ONLY
1132 vm_stop(EXCP_DEBUG
);
1143 addr
= va_arg(va
, target_ulong
);
1144 p
+= sprintf(p
, TARGET_FMT_lx
, addr
);
1147 if (*(fmt
++) != 'x')
1149 i64
= va_arg(va
, uint64_t);
1150 p
+= sprintf(p
, "%" PRIx64
, i64
);
1153 addr
= va_arg(va
, target_ulong
);
1154 p
+= sprintf(p
, TARGET_FMT_lx
"/%x", addr
, va_arg(va
, int));
1158 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
1169 #ifdef CONFIG_USER_ONLY
1170 gdb_handlesig(s
->env
, 0);
1172 cpu_interrupt(s
->env
, CPU_INTERRUPT_EXIT
);
1176 static void gdb_read_byte(GDBState
*s
, int ch
)
1178 CPUState
*env
= s
->env
;
1182 #ifndef CONFIG_USER_ONLY
1183 if (s
->last_packet_len
) {
1184 /* Waiting for a response to the last packet. If we see the start
1185 of a new command then abandon the previous response. */
1188 printf("Got NACK, retransmitting\n");
1190 put_buffer(s
, (uint8_t *)s
->last_packet
, s
->last_packet_len
);
1194 printf("Got ACK\n");
1196 printf("Got '%c' when expecting ACK/NACK\n", ch
);
1198 if (ch
== '+' || ch
== '$')
1199 s
->last_packet_len
= 0;
1204 /* when the CPU is running, we cannot do anything except stop
1205 it when receiving a char */
1206 vm_stop(EXCP_INTERRUPT
);
1213 s
->line_buf_index
= 0;
1214 s
->state
= RS_GETLINE
;
1219 s
->state
= RS_CHKSUM1
;
1220 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
1223 s
->line_buf
[s
->line_buf_index
++] = ch
;
1227 s
->line_buf
[s
->line_buf_index
] = '\0';
1228 s
->line_csum
= fromhex(ch
) << 4;
1229 s
->state
= RS_CHKSUM2
;
1232 s
->line_csum
|= fromhex(ch
);
1234 for(i
= 0; i
< s
->line_buf_index
; i
++) {
1235 csum
+= s
->line_buf
[i
];
1237 if (s
->line_csum
!= (csum
& 0xff)) {
1239 put_buffer(s
, &reply
, 1);
1243 put_buffer(s
, &reply
, 1);
1244 s
->state
= gdb_handle_packet(s
, env
, s
->line_buf
);
1253 #ifdef CONFIG_USER_ONLY
1255 gdb_handlesig (CPUState
*env
, int sig
)
1261 if (gdbserver_fd
< 0)
1264 s
= &gdbserver_state
;
1266 /* disable single step if it was enabled */
1267 cpu_single_step(env
, 0);
1272 snprintf(buf
, sizeof(buf
), "S%02x", sig
);
1278 s
->running_state
= 0;
1279 while (s
->running_state
== 0) {
1280 n
= read (s
->fd
, buf
, 256);
1285 for (i
= 0; i
< n
; i
++)
1286 gdb_read_byte (s
, buf
[i
]);
1288 else if (n
== 0 || errno
!= EAGAIN
)
1290 /* XXX: Connection closed. Should probably wait for annother
1291 connection before continuing. */
1298 /* Tell the remote gdb that the process has exited. */
1299 void gdb_exit(CPUState
*env
, int code
)
1304 if (gdbserver_fd
< 0)
1307 s
= &gdbserver_state
;
1309 snprintf(buf
, sizeof(buf
), "W%02x", code
);
1314 static void gdb_accept(void *opaque
)
1317 struct sockaddr_in sockaddr
;
1322 len
= sizeof(sockaddr
);
1323 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
1324 if (fd
< 0 && errno
!= EINTR
) {
1327 } else if (fd
>= 0) {
1332 /* set short latency */
1334 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1336 s
= &gdbserver_state
;
1337 memset (s
, 0, sizeof (GDBState
));
1338 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1341 gdb_syscall_state
= s
;
1343 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1346 static int gdbserver_open(int port
)
1348 struct sockaddr_in sockaddr
;
1351 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1357 /* allow fast reuse */
1359 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
1361 sockaddr
.sin_family
= AF_INET
;
1362 sockaddr
.sin_port
= htons(port
);
1363 sockaddr
.sin_addr
.s_addr
= 0;
1364 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
1369 ret
= listen(fd
, 0);
1377 int gdbserver_start(int port
)
1379 gdbserver_fd
= gdbserver_open(port
);
1380 if (gdbserver_fd
< 0)
1382 /* accept connections */
1387 static int gdb_chr_can_receive(void *opaque
)
1392 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
1394 GDBState
*s
= opaque
;
1397 for (i
= 0; i
< size
; i
++) {
1398 gdb_read_byte(s
, buf
[i
]);
1402 static void gdb_chr_event(void *opaque
, int event
)
1405 case CHR_EVENT_RESET
:
1406 vm_stop(EXCP_INTERRUPT
);
1407 gdb_syscall_state
= opaque
;
1414 int gdbserver_start(const char *port
)
1417 char gdbstub_port_name
[128];
1420 CharDriverState
*chr
;
1422 if (!port
|| !*port
)
1425 port_num
= strtol(port
, &p
, 10);
1427 /* A numeric value is interpreted as a port number. */
1428 snprintf(gdbstub_port_name
, sizeof(gdbstub_port_name
),
1429 "tcp::%d,nowait,nodelay,server", port_num
);
1430 port
= gdbstub_port_name
;
1433 chr
= qemu_chr_open(port
);
1437 s
= qemu_mallocz(sizeof(GDBState
));
1441 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1443 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
1445 qemu_add_vm_stop_handler(gdb_vm_stopped
, s
);