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(do_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 do_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)tswap32(*((uint32_t *)&env
->fpr
[i
])) << 32;
516 tmp
|= tswap32(*((uint32_t *)&env
->fpr
[i
+ 1]));
517 registers
[i
/2 + 32] = tmp
;
519 registers
[64] = tswapl(env
->pc
);
520 registers
[65] = tswapl(env
->npc
);
521 registers
[66] = tswapl(env
->tstate
[env
->tl
]);
522 registers
[67] = tswapl(env
->fsr
);
523 registers
[68] = tswapl(env
->fprs
);
524 registers
[69] = tswapl(env
->y
);
525 return 70 * sizeof(target_ulong
);
529 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
531 target_ulong
*registers
= (target_ulong
*)mem_buf
;
535 for(i
= 0; i
< 7; i
++) {
536 env
->gregs
[i
] = tswapl(registers
[i
]);
538 /* fill in register window */
539 for(i
= 0; i
< 24; i
++) {
540 env
->regwptr
[i
] = tswapl(registers
[i
+ 8]);
542 #ifndef TARGET_SPARC64
544 for (i
= 0; i
< 32; i
++) {
545 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[i
+ 32]);
547 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
548 env
->y
= tswapl(registers
[64]);
549 PUT_PSR(env
, tswapl(registers
[65]));
550 env
->wim
= tswapl(registers
[66]);
551 env
->tbr
= tswapl(registers
[67]);
552 env
->pc
= tswapl(registers
[68]);
553 env
->npc
= tswapl(registers
[69]);
554 env
->fsr
= tswapl(registers
[70]);
556 for (i
= 0; i
< 64; i
+= 2) {
557 *((uint32_t *)&env
->fpr
[i
]) = tswap32(registers
[i
/2 + 32] >> 32);
558 *((uint32_t *)&env
->fpr
[i
+ 1]) = tswap32(registers
[i
/2 + 32] & 0xffffffff);
560 env
->pc
= tswapl(registers
[64]);
561 env
->npc
= tswapl(registers
[65]);
562 env
->tstate
[env
->tl
] = tswapl(registers
[66]);
563 env
->fsr
= tswapl(registers
[67]);
564 env
->fprs
= tswapl(registers
[68]);
565 env
->y
= tswapl(registers
[69]);
568 #elif defined (TARGET_ARM)
569 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
575 /* 16 core integer registers (4 bytes each). */
576 for (i
= 0; i
< 16; i
++)
578 *(uint32_t *)ptr
= tswapl(env
->regs
[i
]);
581 /* 8 FPA registers (12 bytes each), FPS (4 bytes).
582 Not yet implemented. */
583 memset (ptr
, 0, 8 * 12 + 4);
585 /* CPSR (4 bytes). */
586 *(uint32_t *)ptr
= tswapl (cpsr_read(env
));
589 return ptr
- mem_buf
;
592 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
598 /* Core integer registers. */
599 for (i
= 0; i
< 16; i
++)
601 env
->regs
[i
] = tswapl(*(uint32_t *)ptr
);
604 /* Ignore FPA regs and scr. */
606 cpsr_write (env
, tswapl(*(uint32_t *)ptr
), 0xffffffff);
608 #elif defined (TARGET_M68K)
609 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
617 for (i
= 0; i
< 8; i
++) {
618 *(uint32_t *)ptr
= tswapl(env
->dregs
[i
]);
622 for (i
= 0; i
< 8; i
++) {
623 *(uint32_t *)ptr
= tswapl(env
->aregs
[i
]);
626 *(uint32_t *)ptr
= tswapl(env
->sr
);
628 *(uint32_t *)ptr
= tswapl(env
->pc
);
630 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
631 ColdFire has 8-bit double precision registers. */
632 for (i
= 0; i
< 8; i
++) {
634 *(uint32_t *)ptr
= tswap32(u
.l
.upper
);
635 *(uint32_t *)ptr
= tswap32(u
.l
.lower
);
637 /* FP control regs (not implemented). */
638 memset (ptr
, 0, 3 * 4);
641 return ptr
- mem_buf
;
644 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
652 for (i
= 0; i
< 8; i
++) {
653 env
->dregs
[i
] = tswapl(*(uint32_t *)ptr
);
657 for (i
= 0; i
< 8; i
++) {
658 env
->aregs
[i
] = tswapl(*(uint32_t *)ptr
);
661 env
->sr
= tswapl(*(uint32_t *)ptr
);
663 env
->pc
= tswapl(*(uint32_t *)ptr
);
665 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
666 ColdFire has 8-bit double precision registers. */
667 for (i
= 0; i
< 8; i
++) {
668 u
.l
.upper
= tswap32(*(uint32_t *)ptr
);
669 u
.l
.lower
= tswap32(*(uint32_t *)ptr
);
672 /* FP control regs (not implemented). */
675 #elif defined (TARGET_MIPS)
676 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
682 for (i
= 0; i
< 32; i
++)
684 *(uint32_t *)ptr
= tswapl(env
->gpr
[i
]);
688 *(uint32_t *)ptr
= tswapl(env
->CP0_Status
);
691 *(uint32_t *)ptr
= tswapl(env
->LO
);
694 *(uint32_t *)ptr
= tswapl(env
->HI
);
697 *(uint32_t *)ptr
= tswapl(env
->CP0_BadVAddr
);
700 *(uint32_t *)ptr
= tswapl(env
->CP0_Cause
);
703 *(uint32_t *)ptr
= tswapl(env
->PC
);
707 for (i
= 0; i
< 32; i
++)
709 *(uint32_t *)ptr
= tswapl(FPR_W (env
, i
));
713 *(uint32_t *)ptr
= tswapl(env
->fcr31
);
716 *(uint32_t *)ptr
= tswapl(env
->fcr0
);
720 /* 32 FP registers, fsr, fir, fp. Not yet implemented. */
721 /* what's 'fp' mean here? */
723 return ptr
- mem_buf
;
726 /* convert MIPS rounding mode in FCR31 to IEEE library */
727 static unsigned int ieee_rm
[] =
729 float_round_nearest_even
,
734 #define RESTORE_ROUNDING_MODE \
735 set_float_rounding_mode(ieee_rm[env->fcr31 & 3], &env->fp_status)
737 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
743 for (i
= 0; i
< 32; i
++)
745 env
->gpr
[i
] = tswapl(*(uint32_t *)ptr
);
749 env
->CP0_Status
= tswapl(*(uint32_t *)ptr
);
752 env
->LO
= tswapl(*(uint32_t *)ptr
);
755 env
->HI
= tswapl(*(uint32_t *)ptr
);
758 env
->CP0_BadVAddr
= tswapl(*(uint32_t *)ptr
);
761 env
->CP0_Cause
= tswapl(*(uint32_t *)ptr
);
764 env
->PC
= tswapl(*(uint32_t *)ptr
);
768 for (i
= 0; i
< 32; i
++)
770 FPR_W (env
, i
) = tswapl(*(uint32_t *)ptr
);
774 env
->fcr31
= tswapl(*(uint32_t *)ptr
) & 0x0183FFFF;
777 env
->fcr0
= tswapl(*(uint32_t *)ptr
);
780 /* set rounding mode */
781 RESTORE_ROUNDING_MODE
;
783 #ifndef CONFIG_SOFTFLOAT
784 /* no floating point exception for native float */
785 SET_FP_ENABLE(env
->fcr31
, 0);
789 #elif defined (TARGET_SH4)
790 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
792 uint32_t *ptr
= (uint32_t *)mem_buf
;
795 #define SAVE(x) *ptr++=tswapl(x)
796 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
797 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
+ 16]);
799 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
]);
801 for (i
= 8; i
< 16; i
++) SAVE(env
->gregs
[i
]);
809 SAVE (0); /* TICKS */
810 SAVE (0); /* STALLS */
811 SAVE (0); /* CYCLES */
812 SAVE (0); /* INSTS */
815 return ((uint8_t *)ptr
- mem_buf
);
818 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
820 uint32_t *ptr
= (uint32_t *)mem_buf
;
823 #define LOAD(x) (x)=*ptr++;
824 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
825 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
+ 16]);
827 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
]);
829 for (i
= 8; i
< 16; i
++) LOAD(env
->gregs
[i
]);
839 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
844 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
850 static int gdb_handle_packet(GDBState
*s
, CPUState
*env
, const char *line_buf
)
853 int ch
, reg_size
, type
;
855 uint8_t mem_buf
[2000];
857 target_ulong addr
, len
;
860 printf("command='%s'\n", line_buf
);
866 /* TODO: Make this return the correct value for user-mode. */
867 snprintf(buf
, sizeof(buf
), "S%02x", SIGTRAP
);
872 addr
= strtoull(p
, (char **)&p
, 16);
873 #if defined(TARGET_I386)
876 kvm_load_registers(env
);
878 #elif defined (TARGET_PPC)
880 #elif defined (TARGET_SPARC)
883 #elif defined (TARGET_ARM)
884 env
->regs
[15] = addr
;
885 #elif defined (TARGET_SH4)
889 #ifdef CONFIG_USER_ONLY
890 s
->running_state
= 1;
897 addr
= strtoul(p
, (char **)&p
, 16);
898 #if defined(TARGET_I386)
901 kvm_load_registers(env
);
903 #elif defined (TARGET_PPC)
905 #elif defined (TARGET_SPARC)
908 #elif defined (TARGET_ARM)
909 env
->regs
[15] = addr
;
910 #elif defined (TARGET_SH4)
914 cpu_single_step(env
, 1);
915 #ifdef CONFIG_USER_ONLY
916 s
->running_state
= 1;
926 ret
= strtoull(p
, (char **)&p
, 16);
929 err
= strtoull(p
, (char **)&p
, 16);
936 if (gdb_current_syscall_cb
)
937 gdb_current_syscall_cb(s
->env
, ret
, err
);
939 put_packet(s
, "T02");
941 #ifdef CONFIG_USER_ONLY
942 s
->running_state
= 1;
951 kvm_save_registers(env
);
953 reg_size
= cpu_gdb_read_registers(env
, mem_buf
);
954 memtohex(buf
, mem_buf
, reg_size
);
958 registers
= (void *)mem_buf
;
960 hextomem((uint8_t *)registers
, p
, len
);
961 cpu_gdb_write_registers(env
, mem_buf
, len
);
963 kvm_load_registers(env
);
968 addr
= strtoull(p
, (char **)&p
, 16);
971 len
= strtoull(p
, NULL
, 16);
972 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 0) != 0) {
973 put_packet (s
, "E14");
975 memtohex(buf
, mem_buf
, len
);
980 addr
= strtoull(p
, (char **)&p
, 16);
983 len
= strtoull(p
, (char **)&p
, 16);
986 hextomem(mem_buf
, p
, len
);
987 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 1) != 0)
988 put_packet(s
, "E14");
993 type
= strtoul(p
, (char **)&p
, 16);
996 addr
= strtoull(p
, (char **)&p
, 16);
999 len
= strtoull(p
, (char **)&p
, 16);
1000 if (type
== 0 || type
== 1) {
1001 if (cpu_breakpoint_insert(env
, addr
) < 0)
1002 goto breakpoint_error
;
1003 put_packet(s
, "OK");
1006 put_packet(s
, "E22");
1010 type
= strtoul(p
, (char **)&p
, 16);
1013 addr
= strtoull(p
, (char **)&p
, 16);
1016 len
= strtoull(p
, (char **)&p
, 16);
1017 if (type
== 0 || type
== 1) {
1018 cpu_breakpoint_remove(env
, addr
);
1019 put_packet(s
, "OK");
1021 goto breakpoint_error
;
1024 #ifdef CONFIG_LINUX_USER
1026 if (strncmp(p
, "Offsets", 7) == 0) {
1027 TaskState
*ts
= env
->opaque
;
1029 sprintf(buf
, "Text=%x;Data=%x;Bss=%x", ts
->info
->code_offset
,
1030 ts
->info
->data_offset
, ts
->info
->data_offset
);
1038 /* put empty packet */
1046 extern void tb_flush(CPUState
*env
);
1048 #ifndef CONFIG_USER_ONLY
1049 static void gdb_vm_stopped(void *opaque
, int reason
)
1051 GDBState
*s
= opaque
;
1055 if (s
->state
== RS_SYSCALL
)
1058 /* disable single step if it was enable */
1059 cpu_single_step(s
->env
, 0);
1061 if (reason
== EXCP_DEBUG
) {
1064 } else if (reason
== EXCP_INTERRUPT
) {
1069 snprintf(buf
, sizeof(buf
), "S%02x", ret
);
1074 /* Send a gdb syscall request.
1075 This accepts limited printf-style format specifiers, specifically:
1076 %x - target_ulong argument printed in hex.
1077 %s - string pointer (target_ulong) and length (int) pair. */
1078 void gdb_do_syscall(gdb_syscall_complete_cb cb
, char *fmt
, ...)
1086 s
= gdb_syscall_state
;
1089 gdb_current_syscall_cb
= cb
;
1090 s
->state
= RS_SYSCALL
;
1091 #ifndef CONFIG_USER_ONLY
1092 vm_stop(EXCP_DEBUG
);
1103 addr
= va_arg(va
, target_ulong
);
1104 p
+= sprintf(p
, TARGET_FMT_lx
, addr
);
1107 addr
= va_arg(va
, target_ulong
);
1108 p
+= sprintf(p
, TARGET_FMT_lx
"/%x", addr
, va_arg(va
, int));
1111 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
1121 #ifdef CONFIG_USER_ONLY
1122 gdb_handlesig(s
->env
, 0);
1124 cpu_interrupt(s
->env
, CPU_INTERRUPT_EXIT
);
1128 static void gdb_read_byte(GDBState
*s
, int ch
)
1130 CPUState
*env
= s
->env
;
1134 #ifndef CONFIG_USER_ONLY
1135 if (s
->last_packet_len
) {
1136 /* Waiting for a response to the last packet. If we see the start
1137 of a new command then abandon the previous response. */
1140 printf("Got NACK, retransmitting\n");
1142 put_buffer(s
, s
->last_packet
, s
->last_packet_len
);
1146 printf("Got ACK\n");
1148 printf("Got '%c' when expecting ACK/NACK\n", ch
);
1150 if (ch
== '+' || ch
== '$')
1151 s
->last_packet_len
= 0;
1156 /* when the CPU is running, we cannot do anything except stop
1157 it when receiving a char */
1158 vm_stop(EXCP_INTERRUPT
);
1165 s
->line_buf_index
= 0;
1166 s
->state
= RS_GETLINE
;
1171 s
->state
= RS_CHKSUM1
;
1172 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
1175 s
->line_buf
[s
->line_buf_index
++] = ch
;
1179 s
->line_buf
[s
->line_buf_index
] = '\0';
1180 s
->line_csum
= fromhex(ch
) << 4;
1181 s
->state
= RS_CHKSUM2
;
1184 s
->line_csum
|= fromhex(ch
);
1186 for(i
= 0; i
< s
->line_buf_index
; i
++) {
1187 csum
+= s
->line_buf
[i
];
1189 if (s
->line_csum
!= (csum
& 0xff)) {
1191 put_buffer(s
, reply
, 1);
1195 put_buffer(s
, reply
, 1);
1196 s
->state
= gdb_handle_packet(s
, env
, s
->line_buf
);
1205 #ifdef CONFIG_USER_ONLY
1207 gdb_handlesig (CPUState
*env
, int sig
)
1213 if (gdbserver_fd
< 0)
1216 s
= &gdbserver_state
;
1218 /* disable single step if it was enabled */
1219 cpu_single_step(env
, 0);
1224 snprintf(buf
, sizeof(buf
), "S%02x", sig
);
1230 s
->running_state
= 0;
1231 while (s
->running_state
== 0) {
1232 n
= read (s
->fd
, buf
, 256);
1237 for (i
= 0; i
< n
; i
++)
1238 gdb_read_byte (s
, buf
[i
]);
1240 else if (n
== 0 || errno
!= EAGAIN
)
1242 /* XXX: Connection closed. Should probably wait for annother
1243 connection before continuing. */
1250 /* Tell the remote gdb that the process has exited. */
1251 void gdb_exit(CPUState
*env
, int code
)
1256 if (gdbserver_fd
< 0)
1259 s
= &gdbserver_state
;
1261 snprintf(buf
, sizeof(buf
), "W%02x", code
);
1266 static void gdb_accept(void *opaque
)
1269 struct sockaddr_in sockaddr
;
1274 len
= sizeof(sockaddr
);
1275 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
1276 if (fd
< 0 && errno
!= EINTR
) {
1279 } else if (fd
>= 0) {
1284 /* set short latency */
1286 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1288 s
= &gdbserver_state
;
1289 memset (s
, 0, sizeof (GDBState
));
1290 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1293 gdb_syscall_state
= s
;
1295 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1298 static int gdbserver_open(int port
)
1300 struct sockaddr_in sockaddr
;
1303 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1309 /* allow fast reuse */
1311 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
1313 sockaddr
.sin_family
= AF_INET
;
1314 sockaddr
.sin_port
= htons(port
);
1315 sockaddr
.sin_addr
.s_addr
= 0;
1316 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
1321 ret
= listen(fd
, 0);
1329 int gdbserver_start(int port
)
1331 gdbserver_fd
= gdbserver_open(port
);
1332 if (gdbserver_fd
< 0)
1334 /* accept connections */
1339 static int gdb_chr_can_recieve(void *opaque
)
1344 static void gdb_chr_recieve(void *opaque
, const uint8_t *buf
, int size
)
1346 GDBState
*s
= opaque
;
1349 for (i
= 0; i
< size
; i
++) {
1350 gdb_read_byte(s
, buf
[i
]);
1354 static void gdb_chr_event(void *opaque
, int event
)
1357 case CHR_EVENT_RESET
:
1358 vm_stop(EXCP_INTERRUPT
);
1359 gdb_syscall_state
= opaque
;
1366 int gdbserver_start(CharDriverState
*chr
)
1373 s
= qemu_mallocz(sizeof(GDBState
));
1377 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1379 qemu_chr_add_handlers(chr
, gdb_chr_can_recieve
, gdb_chr_recieve
,
1381 qemu_add_vm_stop_handler(gdb_vm_stopped
, s
);
1385 int gdbserver_start_port(int port
)
1387 CharDriverState
*chr
;
1388 char gdbstub_port_name
[128];
1390 snprintf(gdbstub_port_name
, sizeof(gdbstub_port_name
),
1391 "tcp::%d,nowait,nodelay,server", port
);
1392 chr
= qemu_chr_open(gdbstub_port_name
);
1395 return gdbserver_start(chr
);