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
36 #include "qemu_socket.h"
38 /* XXX: these constants may be independent of the host ones even for Unix */
58 typedef struct GDBState
{
59 CPUState
*env
; /* current CPU */
60 enum RSState state
; /* parsing state */
64 char last_packet
[4100];
66 #ifdef CONFIG_USER_ONLY
74 #ifdef CONFIG_USER_ONLY
75 /* XXX: This is not thread safe. Do we care? */
76 static int gdbserver_fd
= -1;
78 /* XXX: remove this hack. */
79 static GDBState gdbserver_state
;
81 static int get_char(GDBState
*s
)
87 ret
= recv(s
->fd
, &ch
, 1, 0);
89 if (errno
!= EINTR
&& errno
!= EAGAIN
)
91 } else if (ret
== 0) {
101 /* GDB stub state for use by semihosting syscalls. */
102 static GDBState
*gdb_syscall_state
;
103 static gdb_syscall_complete_cb gdb_current_syscall_cb
;
111 /* If gdb is connected when the first semihosting syscall occurs then use
112 remote gdb syscalls. Otherwise use native file IO. */
113 int use_gdb_syscalls(void)
115 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
116 gdb_syscall_mode
= (gdb_syscall_state
? GDB_SYS_ENABLED
119 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
122 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
124 #ifdef CONFIG_USER_ONLY
128 ret
= send(s
->fd
, buf
, len
, 0);
130 if (errno
!= EINTR
&& errno
!= EAGAIN
)
138 qemu_chr_write(s
->chr
, buf
, len
);
142 static inline int fromhex(int v
)
144 if (v
>= '0' && v
<= '9')
146 else if (v
>= 'A' && v
<= 'F')
148 else if (v
>= 'a' && v
<= 'f')
154 static inline int tohex(int v
)
162 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
167 for(i
= 0; i
< len
; i
++) {
169 *q
++ = tohex(c
>> 4);
170 *q
++ = tohex(c
& 0xf);
175 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
179 for(i
= 0; i
< len
; i
++) {
180 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
185 /* return -1 if error, 0 if OK */
186 static int put_packet(GDBState
*s
, char *buf
)
192 printf("reply='%s'\n", buf
);
202 for(i
= 0; i
< len
; i
++) {
206 *(p
++) = tohex((csum
>> 4) & 0xf);
207 *(p
++) = tohex((csum
) & 0xf);
209 s
->last_packet_len
= p
- s
->last_packet
;
210 put_buffer(s
, s
->last_packet
, s
->last_packet_len
);
212 #ifdef CONFIG_USER_ONLY
225 #if defined(TARGET_X86_64)
227 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
229 uint8_t *p
= mem_buf
;
232 #define PUTREG(x) do { \
233 target_ulong reg = tswapl(x); \
234 memcpy(p, ®, sizeof reg); \
237 #define PUTREG32(x) do { \
238 uint32_t reg = tswap32(x); \
239 memcpy(p, ®, sizeof reg); \
242 #define PUTREGF(x) do { \
243 memcpy(p, &(x), 10); \
247 PUTREG(env
->regs
[R_EAX
]);
248 PUTREG(env
->regs
[R_EBX
]);
249 PUTREG(env
->regs
[R_ECX
]);
250 PUTREG(env
->regs
[R_EDX
]);
251 PUTREG(env
->regs
[R_ESI
]);
252 PUTREG(env
->regs
[R_EDI
]);
253 PUTREG(env
->regs
[R_EBP
]);
254 PUTREG(env
->regs
[R_ESP
]);
255 PUTREG(env
->regs
[8]);
256 PUTREG(env
->regs
[9]);
257 PUTREG(env
->regs
[10]);
258 PUTREG(env
->regs
[11]);
259 PUTREG(env
->regs
[12]);
260 PUTREG(env
->regs
[13]);
261 PUTREG(env
->regs
[14]);
262 PUTREG(env
->regs
[15]);
265 PUTREG32(env
->eflags
);
266 PUTREG32(env
->segs
[R_CS
].selector
);
267 PUTREG32(env
->segs
[R_SS
].selector
);
268 PUTREG32(env
->segs
[R_DS
].selector
);
269 PUTREG32(env
->segs
[R_ES
].selector
);
270 PUTREG32(env
->segs
[R_FS
].selector
);
271 PUTREG32(env
->segs
[R_GS
].selector
);
272 /* XXX: convert floats */
273 for(i
= 0; i
< 8; i
++) {
274 PUTREGF(env
->fpregs
[i
]);
277 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
279 PUTREG32(0); /* XXX: convert tags */
280 PUTREG32(0); /* fiseg */
281 PUTREG32(0); /* fioff */
282 PUTREG32(0); /* foseg */
283 PUTREG32(0); /* fooff */
284 PUTREG32(0); /* fop */
293 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
295 uint8_t *p
= mem_buf
;
299 #define GETREG(x) do { \
301 memcpy(®, p, sizeof reg); \
305 #define GETREG32(x) do { \
307 memcpy(®, p, sizeof reg); \
311 #define GETREGF(x) do { \
312 memcpy(&(x), p, 10); \
316 GETREG(env
->regs
[R_EAX
]);
317 GETREG(env
->regs
[R_EBX
]);
318 GETREG(env
->regs
[R_ECX
]);
319 GETREG(env
->regs
[R_EDX
]);
320 GETREG(env
->regs
[R_ESI
]);
321 GETREG(env
->regs
[R_EDI
]);
322 GETREG(env
->regs
[R_EBP
]);
323 GETREG(env
->regs
[R_ESP
]);
324 GETREG(env
->regs
[8]);
325 GETREG(env
->regs
[9]);
326 GETREG(env
->regs
[10]);
327 GETREG(env
->regs
[11]);
328 GETREG(env
->regs
[12]);
329 GETREG(env
->regs
[13]);
330 GETREG(env
->regs
[14]);
331 GETREG(env
->regs
[15]);
334 GETREG32(env
->eflags
);
335 GETREG32(env
->segs
[R_CS
].selector
);
336 GETREG32(env
->segs
[R_SS
].selector
);
337 GETREG32(env
->segs
[R_DS
].selector
);
338 GETREG32(env
->segs
[R_ES
].selector
);
339 GETREG32(env
->segs
[R_FS
].selector
);
340 GETREG32(env
->segs
[R_GS
].selector
);
341 /* XXX: convert floats */
342 for(i
= 0; i
< 8; i
++) {
343 GETREGF(env
->fpregs
[i
]);
346 GETREG32(fpus
); /* XXX: convert fpus */
347 GETREG32(junk
); /* XXX: convert tags */
348 GETREG32(junk
); /* fiseg */
349 GETREG32(junk
); /* fioff */
350 GETREG32(junk
); /* foseg */
351 GETREG32(junk
); /* fooff */
352 GETREG32(junk
); /* fop */
359 #elif defined(TARGET_I386)
361 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
363 uint32_t *registers
= (uint32_t *)mem_buf
;
366 for(i
= 0; i
< 8; i
++) {
367 registers
[i
] = env
->regs
[i
];
369 registers
[8] = env
->eip
;
370 registers
[9] = env
->eflags
;
371 registers
[10] = env
->segs
[R_CS
].selector
;
372 registers
[11] = env
->segs
[R_SS
].selector
;
373 registers
[12] = env
->segs
[R_DS
].selector
;
374 registers
[13] = env
->segs
[R_ES
].selector
;
375 registers
[14] = env
->segs
[R_FS
].selector
;
376 registers
[15] = env
->segs
[R_GS
].selector
;
377 /* XXX: convert floats */
378 for(i
= 0; i
< 8; i
++) {
379 memcpy(mem_buf
+ 16 * 4 + i
* 10, &env
->fpregs
[i
], 10);
381 registers
[36] = env
->fpuc
;
382 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
383 registers
[37] = fpus
;
384 registers
[38] = 0; /* XXX: convert tags */
385 registers
[39] = 0; /* fiseg */
386 registers
[40] = 0; /* fioff */
387 registers
[41] = 0; /* foseg */
388 registers
[42] = 0; /* fooff */
389 registers
[43] = 0; /* fop */
391 for(i
= 0; i
< 16; i
++)
392 tswapls(®isters
[i
]);
393 for(i
= 36; i
< 44; i
++)
394 tswapls(®isters
[i
]);
398 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
400 uint32_t *registers
= (uint32_t *)mem_buf
;
403 for(i
= 0; i
< 8; i
++) {
404 env
->regs
[i
] = tswapl(registers
[i
]);
406 env
->eip
= tswapl(registers
[8]);
407 env
->eflags
= tswapl(registers
[9]);
408 #if defined(CONFIG_USER_ONLY)
409 #define LOAD_SEG(index, sreg)\
410 if (tswapl(registers[index]) != env->segs[sreg].selector)\
411 cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
421 #elif defined (TARGET_PPC)
422 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
424 uint32_t *registers
= (uint32_t *)mem_buf
, tmp
;
428 for(i
= 0; i
< 32; i
++) {
429 registers
[i
] = tswapl(env
->gpr
[i
]);
432 for (i
= 0; i
< 32; i
++) {
433 registers
[(i
* 2) + 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
434 registers
[(i
* 2) + 33] = tswapl(*((uint32_t *)&env
->fpr
[i
] + 1));
436 /* nip, msr, ccr, lnk, ctr, xer, mq */
437 registers
[96] = tswapl(env
->nip
);
438 registers
[97] = tswapl(do_load_msr(env
));
440 for (i
= 0; i
< 8; i
++)
441 tmp
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
442 registers
[98] = tswapl(tmp
);
443 registers
[99] = tswapl(env
->lr
);
444 registers
[100] = tswapl(env
->ctr
);
445 registers
[101] = tswapl(ppc_load_xer(env
));
451 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
453 uint32_t *registers
= (uint32_t *)mem_buf
;
457 for (i
= 0; i
< 32; i
++) {
458 env
->gpr
[i
] = tswapl(registers
[i
]);
461 for (i
= 0; i
< 32; i
++) {
462 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[(i
* 2) + 32]);
463 *((uint32_t *)&env
->fpr
[i
] + 1) = tswapl(registers
[(i
* 2) + 33]);
465 /* nip, msr, ccr, lnk, ctr, xer, mq */
466 env
->nip
= tswapl(registers
[96]);
467 do_store_msr(env
, tswapl(registers
[97]));
468 registers
[98] = tswapl(registers
[98]);
469 for (i
= 0; i
< 8; i
++)
470 env
->crf
[i
] = (registers
[98] >> (32 - ((i
+ 1) * 4))) & 0xF;
471 env
->lr
= tswapl(registers
[99]);
472 env
->ctr
= tswapl(registers
[100]);
473 ppc_store_xer(env
, tswapl(registers
[101]));
475 #elif defined (TARGET_SPARC)
476 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
478 target_ulong
*registers
= (target_ulong
*)mem_buf
;
482 for(i
= 0; i
< 8; i
++) {
483 registers
[i
] = tswapl(env
->gregs
[i
]);
485 /* fill in register window */
486 for(i
= 0; i
< 24; i
++) {
487 registers
[i
+ 8] = tswapl(env
->regwptr
[i
]);
489 #ifndef TARGET_SPARC64
491 for (i
= 0; i
< 32; i
++) {
492 registers
[i
+ 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
494 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
495 registers
[64] = tswapl(env
->y
);
500 registers
[65] = tswapl(tmp
);
502 registers
[66] = tswapl(env
->wim
);
503 registers
[67] = tswapl(env
->tbr
);
504 registers
[68] = tswapl(env
->pc
);
505 registers
[69] = tswapl(env
->npc
);
506 registers
[70] = tswapl(env
->fsr
);
507 registers
[71] = 0; /* csr */
509 return 73 * sizeof(target_ulong
);
512 for (i
= 0; i
< 64; i
+= 2) {
515 tmp
= ((uint64_t)*(uint32_t *)&env
->fpr
[i
]) << 32;
516 tmp
|= *(uint32_t *)&env
->fpr
[i
+ 1];
517 registers
[i
/ 2 + 32] = tswap64(tmp
);
519 registers
[64] = tswapl(env
->pc
);
520 registers
[65] = tswapl(env
->npc
);
521 registers
[66] = tswapl(((uint64_t)GET_CCR(env
) << 32) |
522 ((env
->asi
& 0xff) << 24) |
523 ((env
->pstate
& 0xfff) << 8) |
525 registers
[67] = tswapl(env
->fsr
);
526 registers
[68] = tswapl(env
->fprs
);
527 registers
[69] = tswapl(env
->y
);
528 return 70 * sizeof(target_ulong
);
532 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
534 target_ulong
*registers
= (target_ulong
*)mem_buf
;
538 for(i
= 0; i
< 7; i
++) {
539 env
->gregs
[i
] = tswapl(registers
[i
]);
541 /* fill in register window */
542 for(i
= 0; i
< 24; i
++) {
543 env
->regwptr
[i
] = tswapl(registers
[i
+ 8]);
545 #ifndef TARGET_SPARC64
547 for (i
= 0; i
< 32; i
++) {
548 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[i
+ 32]);
550 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
551 env
->y
= tswapl(registers
[64]);
552 PUT_PSR(env
, tswapl(registers
[65]));
553 env
->wim
= tswapl(registers
[66]);
554 env
->tbr
= tswapl(registers
[67]);
555 env
->pc
= tswapl(registers
[68]);
556 env
->npc
= tswapl(registers
[69]);
557 env
->fsr
= tswapl(registers
[70]);
559 for (i
= 0; i
< 64; i
+= 2) {
562 tmp
= tswap64(registers
[i
/ 2 + 32]);
563 *((uint32_t *)&env
->fpr
[i
]) = tmp
>> 32;
564 *((uint32_t *)&env
->fpr
[i
+ 1]) = tmp
& 0xffffffff;
566 env
->pc
= tswapl(registers
[64]);
567 env
->npc
= tswapl(registers
[65]);
569 uint64_t tmp
= tswapl(registers
[66]);
571 PUT_CCR(env
, tmp
>> 32);
572 env
->asi
= (tmp
>> 24) & 0xff;
573 env
->pstate
= (tmp
>> 8) & 0xfff;
574 PUT_CWP64(env
, tmp
& 0xff);
576 env
->fsr
= tswapl(registers
[67]);
577 env
->fprs
= tswapl(registers
[68]);
578 env
->y
= tswapl(registers
[69]);
581 #elif defined (TARGET_ARM)
582 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
588 /* 16 core integer registers (4 bytes each). */
589 for (i
= 0; i
< 16; i
++)
591 *(uint32_t *)ptr
= tswapl(env
->regs
[i
]);
594 /* 8 FPA registers (12 bytes each), FPS (4 bytes).
595 Not yet implemented. */
596 memset (ptr
, 0, 8 * 12 + 4);
598 /* CPSR (4 bytes). */
599 *(uint32_t *)ptr
= tswapl (cpsr_read(env
));
602 return ptr
- mem_buf
;
605 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
611 /* Core integer registers. */
612 for (i
= 0; i
< 16; i
++)
614 env
->regs
[i
] = tswapl(*(uint32_t *)ptr
);
617 /* Ignore FPA regs and scr. */
619 cpsr_write (env
, tswapl(*(uint32_t *)ptr
), 0xffffffff);
621 #elif defined (TARGET_M68K)
622 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
630 for (i
= 0; i
< 8; i
++) {
631 *(uint32_t *)ptr
= tswapl(env
->dregs
[i
]);
635 for (i
= 0; i
< 8; i
++) {
636 *(uint32_t *)ptr
= tswapl(env
->aregs
[i
]);
639 *(uint32_t *)ptr
= tswapl(env
->sr
);
641 *(uint32_t *)ptr
= tswapl(env
->pc
);
643 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
644 ColdFire has 8-bit double precision registers. */
645 for (i
= 0; i
< 8; i
++) {
647 *(uint32_t *)ptr
= tswap32(u
.l
.upper
);
648 *(uint32_t *)ptr
= tswap32(u
.l
.lower
);
650 /* FP control regs (not implemented). */
651 memset (ptr
, 0, 3 * 4);
654 return ptr
- mem_buf
;
657 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
665 for (i
= 0; i
< 8; i
++) {
666 env
->dregs
[i
] = tswapl(*(uint32_t *)ptr
);
670 for (i
= 0; i
< 8; i
++) {
671 env
->aregs
[i
] = tswapl(*(uint32_t *)ptr
);
674 env
->sr
= tswapl(*(uint32_t *)ptr
);
676 env
->pc
= tswapl(*(uint32_t *)ptr
);
678 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
679 ColdFire has 8-bit double precision registers. */
680 for (i
= 0; i
< 8; i
++) {
681 u
.l
.upper
= tswap32(*(uint32_t *)ptr
);
682 u
.l
.lower
= tswap32(*(uint32_t *)ptr
);
685 /* FP control regs (not implemented). */
688 #elif defined (TARGET_MIPS)
689 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
695 for (i
= 0; i
< 32; i
++)
697 *(target_ulong
*)ptr
= tswapl(env
->gpr
[i
][env
->current_tc
]);
698 ptr
+= sizeof(target_ulong
);
701 *(target_ulong
*)ptr
= tswapl(env
->CP0_Status
);
702 ptr
+= sizeof(target_ulong
);
704 *(target_ulong
*)ptr
= tswapl(env
->LO
[0][env
->current_tc
]);
705 ptr
+= sizeof(target_ulong
);
707 *(target_ulong
*)ptr
= tswapl(env
->HI
[0][env
->current_tc
]);
708 ptr
+= sizeof(target_ulong
);
710 *(target_ulong
*)ptr
= tswapl(env
->CP0_BadVAddr
);
711 ptr
+= sizeof(target_ulong
);
713 *(target_ulong
*)ptr
= tswapl(env
->CP0_Cause
);
714 ptr
+= sizeof(target_ulong
);
716 *(target_ulong
*)ptr
= tswapl(env
->PC
[env
->current_tc
]);
717 ptr
+= sizeof(target_ulong
);
719 if (env
->CP0_Config1
& (1 << CP0C1_FP
))
721 for (i
= 0; i
< 32; i
++)
723 *(target_ulong
*)ptr
= tswapl(env
->fpu
->fpr
[i
].fs
[FP_ENDIAN_IDX
]);
724 ptr
+= sizeof(target_ulong
);
727 *(target_ulong
*)ptr
= tswapl(env
->fpu
->fcr31
);
728 ptr
+= sizeof(target_ulong
);
730 *(target_ulong
*)ptr
= tswapl(env
->fpu
->fcr0
);
731 ptr
+= sizeof(target_ulong
);
734 /* 32 FP registers, fsr, fir, fp. Not yet implemented. */
735 /* what's 'fp' mean here? */
737 return ptr
- mem_buf
;
740 /* convert MIPS rounding mode in FCR31 to IEEE library */
741 static unsigned int ieee_rm
[] =
743 float_round_nearest_even
,
748 #define RESTORE_ROUNDING_MODE \
749 set_float_rounding_mode(ieee_rm[env->fpu->fcr31 & 3], &env->fpu->fp_status)
751 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
757 for (i
= 0; i
< 32; i
++)
759 env
->gpr
[i
][env
->current_tc
] = tswapl(*(target_ulong
*)ptr
);
760 ptr
+= sizeof(target_ulong
);
763 env
->CP0_Status
= tswapl(*(target_ulong
*)ptr
);
764 ptr
+= sizeof(target_ulong
);
766 env
->LO
[0][env
->current_tc
] = tswapl(*(target_ulong
*)ptr
);
767 ptr
+= sizeof(target_ulong
);
769 env
->HI
[0][env
->current_tc
] = tswapl(*(target_ulong
*)ptr
);
770 ptr
+= sizeof(target_ulong
);
772 env
->CP0_BadVAddr
= tswapl(*(target_ulong
*)ptr
);
773 ptr
+= sizeof(target_ulong
);
775 env
->CP0_Cause
= tswapl(*(target_ulong
*)ptr
);
776 ptr
+= sizeof(target_ulong
);
778 env
->PC
[env
->current_tc
] = tswapl(*(target_ulong
*)ptr
);
779 ptr
+= sizeof(target_ulong
);
781 if (env
->CP0_Config1
& (1 << CP0C1_FP
))
783 for (i
= 0; i
< 32; i
++)
785 env
->fpu
->fpr
[i
].fs
[FP_ENDIAN_IDX
] = tswapl(*(target_ulong
*)ptr
);
786 ptr
+= sizeof(target_ulong
);
789 env
->fpu
->fcr31
= tswapl(*(target_ulong
*)ptr
) & 0x0183FFFF;
790 ptr
+= sizeof(target_ulong
);
792 env
->fpu
->fcr0
= tswapl(*(target_ulong
*)ptr
);
793 ptr
+= sizeof(target_ulong
);
795 /* set rounding mode */
796 RESTORE_ROUNDING_MODE
;
798 #ifndef CONFIG_SOFTFLOAT
799 /* no floating point exception for native float */
800 SET_FP_ENABLE(env
->fcr31
, 0);
804 #elif defined (TARGET_SH4)
806 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
808 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
810 uint32_t *ptr
= (uint32_t *)mem_buf
;
813 #define SAVE(x) *ptr++=tswapl(x)
814 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
815 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
+ 16]);
817 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
]);
819 for (i
= 8; i
< 16; i
++) SAVE(env
->gregs
[i
]);
829 for (i
= 0; i
< 16; i
++)
830 SAVE(env
->fregs
[i
+ ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
833 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
]);
834 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
+ 16]);
835 return ((uint8_t *)ptr
- mem_buf
);
838 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
840 uint32_t *ptr
= (uint32_t *)mem_buf
;
843 #define LOAD(x) (x)=*ptr++;
844 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
845 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
+ 16]);
847 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
]);
849 for (i
= 8; i
< 16; i
++) LOAD(env
->gregs
[i
]);
859 for (i
= 0; i
< 16; i
++)
860 LOAD(env
->fregs
[i
+ ((env
->fpscr
& FPSCR_FR
) ? 16 : 0)]);
863 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
]);
864 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
+ 16]);
867 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
872 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
878 static int gdb_handle_packet(GDBState
*s
, CPUState
*env
, const char *line_buf
)
881 int ch
, reg_size
, type
;
883 uint8_t mem_buf
[2000];
885 target_ulong addr
, len
;
888 printf("command='%s'\n", line_buf
);
894 /* TODO: Make this return the correct value for user-mode. */
895 snprintf(buf
, sizeof(buf
), "S%02x", SIGTRAP
);
900 addr
= strtoull(p
, (char **)&p
, 16);
901 #if defined(TARGET_I386)
904 kvm_load_registers(env
);
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
;
919 #ifdef CONFIG_USER_ONLY
920 s
->running_state
= 1;
927 addr
= strtoull(p
, (char **)&p
, 16);
928 #if defined(TARGET_I386)
931 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
;
946 cpu_single_step(env
, 1);
947 #ifdef CONFIG_USER_ONLY
948 s
->running_state
= 1;
958 ret
= strtoull(p
, (char **)&p
, 16);
961 err
= strtoull(p
, (char **)&p
, 16);
968 if (gdb_current_syscall_cb
)
969 gdb_current_syscall_cb(s
->env
, ret
, err
);
971 put_packet(s
, "T02");
973 #ifdef CONFIG_USER_ONLY
974 s
->running_state
= 1;
983 kvm_save_registers(env
);
985 reg_size
= cpu_gdb_read_registers(env
, mem_buf
);
986 memtohex(buf
, mem_buf
, reg_size
);
990 registers
= (void *)mem_buf
;
992 hextomem((uint8_t *)registers
, p
, len
);
993 cpu_gdb_write_registers(env
, mem_buf
, len
);
995 kvm_load_registers(env
);
1000 addr
= strtoull(p
, (char **)&p
, 16);
1003 len
= strtoull(p
, NULL
, 16);
1004 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 0) != 0) {
1005 put_packet (s
, "E14");
1007 memtohex(buf
, mem_buf
, len
);
1012 addr
= strtoull(p
, (char **)&p
, 16);
1015 len
= strtoull(p
, (char **)&p
, 16);
1018 hextomem(mem_buf
, p
, len
);
1019 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 1) != 0)
1020 put_packet(s
, "E14");
1022 put_packet(s
, "OK");
1025 type
= strtoul(p
, (char **)&p
, 16);
1028 addr
= strtoull(p
, (char **)&p
, 16);
1031 len
= strtoull(p
, (char **)&p
, 16);
1032 if (type
== 0 || type
== 1) {
1033 if (cpu_breakpoint_insert(env
, addr
) < 0)
1034 goto breakpoint_error
;
1035 put_packet(s
, "OK");
1036 #ifndef CONFIG_USER_ONLY
1037 } else if (type
== 2) {
1038 if (cpu_watchpoint_insert(env
, addr
) < 0)
1039 goto breakpoint_error
;
1040 put_packet(s
, "OK");
1044 put_packet(s
, "E22");
1048 type
= strtoul(p
, (char **)&p
, 16);
1051 addr
= strtoull(p
, (char **)&p
, 16);
1054 len
= strtoull(p
, (char **)&p
, 16);
1055 if (type
== 0 || type
== 1) {
1056 cpu_breakpoint_remove(env
, addr
);
1057 put_packet(s
, "OK");
1058 #ifndef CONFIG_USER_ONLY
1059 } else if (type
== 2) {
1060 cpu_watchpoint_remove(env
, addr
);
1061 put_packet(s
, "OK");
1064 goto breakpoint_error
;
1067 #ifdef CONFIG_LINUX_USER
1069 if (strncmp(p
, "Offsets", 7) == 0) {
1070 TaskState
*ts
= env
->opaque
;
1073 "Text=" TARGET_FMT_lx
";Data=" TARGET_FMT_lx
";Bss=" TARGET_FMT_lx
,
1074 ts
->info
->code_offset
,
1075 ts
->info
->data_offset
,
1076 ts
->info
->data_offset
);
1084 /* put empty packet */
1092 extern void tb_flush(CPUState
*env
);
1094 #ifndef CONFIG_USER_ONLY
1095 static void gdb_vm_stopped(void *opaque
, int reason
)
1097 GDBState
*s
= opaque
;
1101 if (s
->state
== RS_SYSCALL
)
1104 /* disable single step if it was enable */
1105 cpu_single_step(s
->env
, 0);
1107 if (reason
== EXCP_DEBUG
) {
1108 if (s
->env
->watchpoint_hit
) {
1109 snprintf(buf
, sizeof(buf
), "T%02xwatch:" TARGET_FMT_lx
";",
1111 s
->env
->watchpoint
[s
->env
->watchpoint_hit
- 1].vaddr
);
1113 s
->env
->watchpoint_hit
= 0;
1118 } else if (reason
== EXCP_INTERRUPT
) {
1123 snprintf(buf
, sizeof(buf
), "S%02x", ret
);
1128 /* Send a gdb syscall request.
1129 This accepts limited printf-style format specifiers, specifically:
1130 %x - target_ulong argument printed in hex.
1131 %lx - 64-bit argument printed in hex.
1132 %s - string pointer (target_ulong) and length (int) pair. */
1133 void gdb_do_syscall(gdb_syscall_complete_cb cb
, char *fmt
, ...)
1142 s
= gdb_syscall_state
;
1145 gdb_current_syscall_cb
= cb
;
1146 s
->state
= RS_SYSCALL
;
1147 #ifndef CONFIG_USER_ONLY
1148 vm_stop(EXCP_DEBUG
);
1159 addr
= va_arg(va
, target_ulong
);
1160 p
+= sprintf(p
, TARGET_FMT_lx
, addr
);
1163 if (*(fmt
++) != 'x')
1165 i64
= va_arg(va
, uint64_t);
1166 p
+= sprintf(p
, "%" PRIx64
, i64
);
1169 addr
= va_arg(va
, target_ulong
);
1170 p
+= sprintf(p
, TARGET_FMT_lx
"/%x", addr
, va_arg(va
, int));
1174 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
1185 #ifdef CONFIG_USER_ONLY
1186 gdb_handlesig(s
->env
, 0);
1188 cpu_interrupt(s
->env
, CPU_INTERRUPT_EXIT
);
1192 static void gdb_read_byte(GDBState
*s
, int ch
)
1194 CPUState
*env
= s
->env
;
1198 #ifndef CONFIG_USER_ONLY
1199 if (s
->last_packet_len
) {
1200 /* Waiting for a response to the last packet. If we see the start
1201 of a new command then abandon the previous response. */
1204 printf("Got NACK, retransmitting\n");
1206 put_buffer(s
, s
->last_packet
, s
->last_packet_len
);
1210 printf("Got ACK\n");
1212 printf("Got '%c' when expecting ACK/NACK\n", ch
);
1214 if (ch
== '+' || ch
== '$')
1215 s
->last_packet_len
= 0;
1220 /* when the CPU is running, we cannot do anything except stop
1221 it when receiving a char */
1222 vm_stop(EXCP_INTERRUPT
);
1229 s
->line_buf_index
= 0;
1230 s
->state
= RS_GETLINE
;
1235 s
->state
= RS_CHKSUM1
;
1236 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
1239 s
->line_buf
[s
->line_buf_index
++] = ch
;
1243 s
->line_buf
[s
->line_buf_index
] = '\0';
1244 s
->line_csum
= fromhex(ch
) << 4;
1245 s
->state
= RS_CHKSUM2
;
1248 s
->line_csum
|= fromhex(ch
);
1250 for(i
= 0; i
< s
->line_buf_index
; i
++) {
1251 csum
+= s
->line_buf
[i
];
1253 if (s
->line_csum
!= (csum
& 0xff)) {
1255 put_buffer(s
, reply
, 1);
1259 put_buffer(s
, reply
, 1);
1260 s
->state
= gdb_handle_packet(s
, env
, s
->line_buf
);
1269 #ifdef CONFIG_USER_ONLY
1271 gdb_handlesig (CPUState
*env
, int sig
)
1277 if (gdbserver_fd
< 0)
1280 s
= &gdbserver_state
;
1282 /* disable single step if it was enabled */
1283 cpu_single_step(env
, 0);
1288 snprintf(buf
, sizeof(buf
), "S%02x", sig
);
1294 s
->running_state
= 0;
1295 while (s
->running_state
== 0) {
1296 n
= read (s
->fd
, buf
, 256);
1301 for (i
= 0; i
< n
; i
++)
1302 gdb_read_byte (s
, buf
[i
]);
1304 else if (n
== 0 || errno
!= EAGAIN
)
1306 /* XXX: Connection closed. Should probably wait for annother
1307 connection before continuing. */
1314 /* Tell the remote gdb that the process has exited. */
1315 void gdb_exit(CPUState
*env
, int code
)
1320 if (gdbserver_fd
< 0)
1323 s
= &gdbserver_state
;
1325 snprintf(buf
, sizeof(buf
), "W%02x", code
);
1330 static void gdb_accept(void *opaque
)
1333 struct sockaddr_in sockaddr
;
1338 len
= sizeof(sockaddr
);
1339 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
1340 if (fd
< 0 && errno
!= EINTR
) {
1343 } else if (fd
>= 0) {
1348 /* set short latency */
1350 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1352 s
= &gdbserver_state
;
1353 memset (s
, 0, sizeof (GDBState
));
1354 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1357 gdb_syscall_state
= s
;
1359 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1362 static int gdbserver_open(int port
)
1364 struct sockaddr_in sockaddr
;
1367 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1373 /* allow fast reuse */
1375 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
1377 sockaddr
.sin_family
= AF_INET
;
1378 sockaddr
.sin_port
= htons(port
);
1379 sockaddr
.sin_addr
.s_addr
= 0;
1380 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
1385 ret
= listen(fd
, 0);
1393 int gdbserver_start(int port
)
1395 gdbserver_fd
= gdbserver_open(port
);
1396 if (gdbserver_fd
< 0)
1398 /* accept connections */
1403 static int gdb_chr_can_receive(void *opaque
)
1408 static void gdb_chr_receive(void *opaque
, const uint8_t *buf
, int size
)
1410 GDBState
*s
= opaque
;
1413 for (i
= 0; i
< size
; i
++) {
1414 gdb_read_byte(s
, buf
[i
]);
1418 static void gdb_chr_event(void *opaque
, int event
)
1421 case CHR_EVENT_RESET
:
1422 vm_stop(EXCP_INTERRUPT
);
1423 gdb_syscall_state
= opaque
;
1430 int gdbserver_start(const char *port
)
1433 char gdbstub_port_name
[128];
1436 CharDriverState
*chr
;
1438 if (!port
|| !*port
)
1441 port_num
= strtol(port
, &p
, 10);
1443 /* A numeric value is interpreted as a port number. */
1444 snprintf(gdbstub_port_name
, sizeof(gdbstub_port_name
),
1445 "tcp::%d,nowait,nodelay,server", port_num
);
1446 port
= gdbstub_port_name
;
1449 chr
= qemu_chr_open(port
);
1453 s
= qemu_mallocz(sizeof(GDBState
));
1457 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1459 qemu_chr_add_handlers(chr
, gdb_chr_can_receive
, gdb_chr_receive
,
1461 qemu_add_vm_stop_handler(gdb_vm_stopped
, s
);