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
35 #include "qemu_socket.h"
37 /* XXX: these constants may be independent of the host ones even for Unix */
57 typedef struct GDBState
{
58 CPUState
*env
; /* current CPU */
59 enum RSState state
; /* parsing state */
63 char last_packet
[4100];
65 #ifdef CONFIG_USER_ONLY
73 #ifdef CONFIG_USER_ONLY
74 /* XXX: This is not thread safe. Do we care? */
75 static int gdbserver_fd
= -1;
77 /* XXX: remove this hack. */
78 static GDBState gdbserver_state
;
80 static int get_char(GDBState
*s
)
86 ret
= recv(s
->fd
, &ch
, 1, 0);
88 if (errno
!= EINTR
&& errno
!= EAGAIN
)
90 } else if (ret
== 0) {
100 /* GDB stub state for use by semihosting syscalls. */
101 static GDBState
*gdb_syscall_state
;
102 static gdb_syscall_complete_cb gdb_current_syscall_cb
;
110 /* If gdb is connected when the first semihosting syscall occurs then use
111 remote gdb syscalls. Otherwise use native file IO. */
112 int use_gdb_syscalls(void)
114 if (gdb_syscall_mode
== GDB_SYS_UNKNOWN
) {
115 gdb_syscall_mode
= (gdb_syscall_state
? GDB_SYS_ENABLED
118 return gdb_syscall_mode
== GDB_SYS_ENABLED
;
121 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
123 #ifdef CONFIG_USER_ONLY
127 ret
= send(s
->fd
, buf
, len
, 0);
129 if (errno
!= EINTR
&& errno
!= EAGAIN
)
137 qemu_chr_write(s
->chr
, buf
, len
);
141 static inline int fromhex(int v
)
143 if (v
>= '0' && v
<= '9')
145 else if (v
>= 'A' && v
<= 'F')
147 else if (v
>= 'a' && v
<= 'f')
153 static inline int tohex(int v
)
161 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
166 for(i
= 0; i
< len
; i
++) {
168 *q
++ = tohex(c
>> 4);
169 *q
++ = tohex(c
& 0xf);
174 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
178 for(i
= 0; i
< len
; i
++) {
179 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
184 /* return -1 if error, 0 if OK */
185 static int put_packet(GDBState
*s
, char *buf
)
191 printf("reply='%s'\n", buf
);
201 for(i
= 0; i
< len
; i
++) {
205 *(p
++) = tohex((csum
>> 4) & 0xf);
206 *(p
++) = tohex((csum
) & 0xf);
208 s
->last_packet_len
= p
- s
->last_packet
;
209 put_buffer(s
, s
->last_packet
, s
->last_packet_len
);
211 #ifdef CONFIG_USER_ONLY
224 #if defined(TARGET_X86_64)
226 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
228 uint8_t *p
= mem_buf
;
231 #define PUTREG(x) do { \
232 target_ulong reg = tswapl(x); \
233 memcpy(p, ®, sizeof reg); \
236 #define PUTREG32(x) do { \
237 uint32_t reg = tswap32(x); \
238 memcpy(p, ®, sizeof reg); \
241 #define PUTREGF(x) do { \
242 memcpy(p, &(x), 10); \
246 PUTREG(env
->regs
[R_EAX
]);
247 PUTREG(env
->regs
[R_EBX
]);
248 PUTREG(env
->regs
[R_ECX
]);
249 PUTREG(env
->regs
[R_EDX
]);
250 PUTREG(env
->regs
[R_ESI
]);
251 PUTREG(env
->regs
[R_EDI
]);
252 PUTREG(env
->regs
[R_EBP
]);
253 PUTREG(env
->regs
[R_ESP
]);
254 PUTREG(env
->regs
[8]);
255 PUTREG(env
->regs
[9]);
256 PUTREG(env
->regs
[10]);
257 PUTREG(env
->regs
[11]);
258 PUTREG(env
->regs
[12]);
259 PUTREG(env
->regs
[13]);
260 PUTREG(env
->regs
[14]);
261 PUTREG(env
->regs
[15]);
264 PUTREG32(env
->eflags
);
265 PUTREG32(env
->segs
[R_CS
].selector
);
266 PUTREG32(env
->segs
[R_SS
].selector
);
267 PUTREG32(env
->segs
[R_DS
].selector
);
268 PUTREG32(env
->segs
[R_ES
].selector
);
269 PUTREG32(env
->segs
[R_FS
].selector
);
270 PUTREG32(env
->segs
[R_GS
].selector
);
271 /* XXX: convert floats */
272 for(i
= 0; i
< 8; i
++) {
273 PUTREGF(env
->fpregs
[i
]);
276 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
278 PUTREG32(0); /* XXX: convert tags */
279 PUTREG32(0); /* fiseg */
280 PUTREG32(0); /* fioff */
281 PUTREG32(0); /* foseg */
282 PUTREG32(0); /* fooff */
283 PUTREG32(0); /* fop */
292 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
294 uint8_t *p
= mem_buf
;
298 #define GETREG(x) do { \
300 memcpy(®, p, sizeof reg); \
304 #define GETREG32(x) do { \
306 memcpy(®, p, sizeof reg); \
310 #define GETREGF(x) do { \
311 memcpy(&(x), p, 10); \
315 GETREG(env
->regs
[R_EAX
]);
316 GETREG(env
->regs
[R_EBX
]);
317 GETREG(env
->regs
[R_ECX
]);
318 GETREG(env
->regs
[R_EDX
]);
319 GETREG(env
->regs
[R_ESI
]);
320 GETREG(env
->regs
[R_EDI
]);
321 GETREG(env
->regs
[R_EBP
]);
322 GETREG(env
->regs
[R_ESP
]);
323 GETREG(env
->regs
[8]);
324 GETREG(env
->regs
[9]);
325 GETREG(env
->regs
[10]);
326 GETREG(env
->regs
[11]);
327 GETREG(env
->regs
[12]);
328 GETREG(env
->regs
[13]);
329 GETREG(env
->regs
[14]);
330 GETREG(env
->regs
[15]);
333 GETREG32(env
->eflags
);
334 GETREG32(env
->segs
[R_CS
].selector
);
335 GETREG32(env
->segs
[R_SS
].selector
);
336 GETREG32(env
->segs
[R_DS
].selector
);
337 GETREG32(env
->segs
[R_ES
].selector
);
338 GETREG32(env
->segs
[R_FS
].selector
);
339 GETREG32(env
->segs
[R_GS
].selector
);
340 /* XXX: convert floats */
341 for(i
= 0; i
< 8; i
++) {
342 GETREGF(env
->fpregs
[i
]);
345 GETREG32(fpus
); /* XXX: convert fpus */
346 GETREG32(junk
); /* XXX: convert tags */
347 GETREG32(junk
); /* fiseg */
348 GETREG32(junk
); /* fioff */
349 GETREG32(junk
); /* foseg */
350 GETREG32(junk
); /* fooff */
351 GETREG32(junk
); /* fop */
358 #elif defined(TARGET_I386)
360 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
362 uint32_t *registers
= (uint32_t *)mem_buf
;
365 for(i
= 0; i
< 8; i
++) {
366 registers
[i
] = env
->regs
[i
];
368 registers
[8] = env
->eip
;
369 registers
[9] = env
->eflags
;
370 registers
[10] = env
->segs
[R_CS
].selector
;
371 registers
[11] = env
->segs
[R_SS
].selector
;
372 registers
[12] = env
->segs
[R_DS
].selector
;
373 registers
[13] = env
->segs
[R_ES
].selector
;
374 registers
[14] = env
->segs
[R_FS
].selector
;
375 registers
[15] = env
->segs
[R_GS
].selector
;
376 /* XXX: convert floats */
377 for(i
= 0; i
< 8; i
++) {
378 memcpy(mem_buf
+ 16 * 4 + i
* 10, &env
->fpregs
[i
], 10);
380 registers
[36] = env
->fpuc
;
381 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
382 registers
[37] = fpus
;
383 registers
[38] = 0; /* XXX: convert tags */
384 registers
[39] = 0; /* fiseg */
385 registers
[40] = 0; /* fioff */
386 registers
[41] = 0; /* foseg */
387 registers
[42] = 0; /* fooff */
388 registers
[43] = 0; /* fop */
390 for(i
= 0; i
< 16; i
++)
391 tswapls(®isters
[i
]);
392 for(i
= 36; i
< 44; i
++)
393 tswapls(®isters
[i
]);
397 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
399 uint32_t *registers
= (uint32_t *)mem_buf
;
402 for(i
= 0; i
< 8; i
++) {
403 env
->regs
[i
] = tswapl(registers
[i
]);
405 env
->eip
= tswapl(registers
[8]);
406 env
->eflags
= tswapl(registers
[9]);
407 #if defined(CONFIG_USER_ONLY)
408 #define LOAD_SEG(index, sreg)\
409 if (tswapl(registers[index]) != env->segs[sreg].selector)\
410 cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
420 #elif defined (TARGET_PPC)
421 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
423 uint32_t *registers
= (uint32_t *)mem_buf
, tmp
;
427 for(i
= 0; i
< 32; i
++) {
428 registers
[i
] = tswapl(env
->gpr
[i
]);
431 for (i
= 0; i
< 32; i
++) {
432 registers
[(i
* 2) + 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
433 registers
[(i
* 2) + 33] = tswapl(*((uint32_t *)&env
->fpr
[i
] + 1));
435 /* nip, msr, ccr, lnk, ctr, xer, mq */
436 registers
[96] = tswapl(env
->nip
);
437 registers
[97] = tswapl(do_load_msr(env
));
439 for (i
= 0; i
< 8; i
++)
440 tmp
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
441 registers
[98] = tswapl(tmp
);
442 registers
[99] = tswapl(env
->lr
);
443 registers
[100] = tswapl(env
->ctr
);
444 registers
[101] = tswapl(do_load_xer(env
));
450 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
452 uint32_t *registers
= (uint32_t *)mem_buf
;
456 for (i
= 0; i
< 32; i
++) {
457 env
->gpr
[i
] = tswapl(registers
[i
]);
460 for (i
= 0; i
< 32; i
++) {
461 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[(i
* 2) + 32]);
462 *((uint32_t *)&env
->fpr
[i
] + 1) = tswapl(registers
[(i
* 2) + 33]);
464 /* nip, msr, ccr, lnk, ctr, xer, mq */
465 env
->nip
= tswapl(registers
[96]);
466 do_store_msr(env
, tswapl(registers
[97]));
467 registers
[98] = tswapl(registers
[98]);
468 for (i
= 0; i
< 8; i
++)
469 env
->crf
[i
] = (registers
[98] >> (32 - ((i
+ 1) * 4))) & 0xF;
470 env
->lr
= tswapl(registers
[99]);
471 env
->ctr
= tswapl(registers
[100]);
472 do_store_xer(env
, tswapl(registers
[101]));
474 #elif defined (TARGET_SPARC)
475 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
477 target_ulong
*registers
= (target_ulong
*)mem_buf
;
481 for(i
= 0; i
< 8; i
++) {
482 registers
[i
] = tswapl(env
->gregs
[i
]);
484 /* fill in register window */
485 for(i
= 0; i
< 24; i
++) {
486 registers
[i
+ 8] = tswapl(env
->regwptr
[i
]);
488 #ifndef TARGET_SPARC64
490 for (i
= 0; i
< 32; i
++) {
491 registers
[i
+ 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
493 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
494 registers
[64] = tswapl(env
->y
);
499 registers
[65] = tswapl(tmp
);
501 registers
[66] = tswapl(env
->wim
);
502 registers
[67] = tswapl(env
->tbr
);
503 registers
[68] = tswapl(env
->pc
);
504 registers
[69] = tswapl(env
->npc
);
505 registers
[70] = tswapl(env
->fsr
);
506 registers
[71] = 0; /* csr */
508 return 73 * sizeof(target_ulong
);
511 for (i
= 0; i
< 64; i
+= 2) {
514 tmp
= (uint64_t)tswap32(*((uint32_t *)&env
->fpr
[i
])) << 32;
515 tmp
|= tswap32(*((uint32_t *)&env
->fpr
[i
+ 1]));
516 registers
[i
/2 + 32] = tmp
;
518 registers
[64] = tswapl(env
->pc
);
519 registers
[65] = tswapl(env
->npc
);
520 registers
[66] = tswapl(env
->tstate
[env
->tl
]);
521 registers
[67] = tswapl(env
->fsr
);
522 registers
[68] = tswapl(env
->fprs
);
523 registers
[69] = tswapl(env
->y
);
524 return 70 * sizeof(target_ulong
);
528 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
530 target_ulong
*registers
= (target_ulong
*)mem_buf
;
534 for(i
= 0; i
< 7; i
++) {
535 env
->gregs
[i
] = tswapl(registers
[i
]);
537 /* fill in register window */
538 for(i
= 0; i
< 24; i
++) {
539 env
->regwptr
[i
] = tswapl(registers
[i
+ 8]);
541 #ifndef TARGET_SPARC64
543 for (i
= 0; i
< 32; i
++) {
544 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[i
+ 32]);
546 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
547 env
->y
= tswapl(registers
[64]);
548 PUT_PSR(env
, tswapl(registers
[65]));
549 env
->wim
= tswapl(registers
[66]);
550 env
->tbr
= tswapl(registers
[67]);
551 env
->pc
= tswapl(registers
[68]);
552 env
->npc
= tswapl(registers
[69]);
553 env
->fsr
= tswapl(registers
[70]);
555 for (i
= 0; i
< 64; i
+= 2) {
556 *((uint32_t *)&env
->fpr
[i
]) = tswap32(registers
[i
/2 + 32] >> 32);
557 *((uint32_t *)&env
->fpr
[i
+ 1]) = tswap32(registers
[i
/2 + 32] & 0xffffffff);
559 env
->pc
= tswapl(registers
[64]);
560 env
->npc
= tswapl(registers
[65]);
561 env
->tstate
[env
->tl
] = tswapl(registers
[66]);
562 env
->fsr
= tswapl(registers
[67]);
563 env
->fprs
= tswapl(registers
[68]);
564 env
->y
= tswapl(registers
[69]);
567 #elif defined (TARGET_ARM)
568 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
574 /* 16 core integer registers (4 bytes each). */
575 for (i
= 0; i
< 16; i
++)
577 *(uint32_t *)ptr
= tswapl(env
->regs
[i
]);
580 /* 8 FPA registers (12 bytes each), FPS (4 bytes).
581 Not yet implemented. */
582 memset (ptr
, 0, 8 * 12 + 4);
584 /* CPSR (4 bytes). */
585 *(uint32_t *)ptr
= tswapl (cpsr_read(env
));
588 return ptr
- mem_buf
;
591 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
597 /* Core integer registers. */
598 for (i
= 0; i
< 16; i
++)
600 env
->regs
[i
] = tswapl(*(uint32_t *)ptr
);
603 /* Ignore FPA regs and scr. */
605 cpsr_write (env
, tswapl(*(uint32_t *)ptr
), 0xffffffff);
607 #elif defined (TARGET_M68K)
608 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
616 for (i
= 0; i
< 8; i
++) {
617 *(uint32_t *)ptr
= tswapl(env
->dregs
[i
]);
621 for (i
= 0; i
< 8; i
++) {
622 *(uint32_t *)ptr
= tswapl(env
->aregs
[i
]);
625 *(uint32_t *)ptr
= tswapl(env
->sr
);
627 *(uint32_t *)ptr
= tswapl(env
->pc
);
629 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
630 ColdFire has 8-bit double precision registers. */
631 for (i
= 0; i
< 8; i
++) {
633 *(uint32_t *)ptr
= tswap32(u
.l
.upper
);
634 *(uint32_t *)ptr
= tswap32(u
.l
.lower
);
636 /* FP control regs (not implemented). */
637 memset (ptr
, 0, 3 * 4);
640 return ptr
- mem_buf
;
643 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
651 for (i
= 0; i
< 8; i
++) {
652 env
->dregs
[i
] = tswapl(*(uint32_t *)ptr
);
656 for (i
= 0; i
< 8; i
++) {
657 env
->aregs
[i
] = tswapl(*(uint32_t *)ptr
);
660 env
->sr
= tswapl(*(uint32_t *)ptr
);
662 env
->pc
= tswapl(*(uint32_t *)ptr
);
664 /* F0-F7. The 68881/68040 have 12-bit extended precision registers.
665 ColdFire has 8-bit double precision registers. */
666 for (i
= 0; i
< 8; i
++) {
667 u
.l
.upper
= tswap32(*(uint32_t *)ptr
);
668 u
.l
.lower
= tswap32(*(uint32_t *)ptr
);
671 /* FP control regs (not implemented). */
674 #elif defined (TARGET_MIPS)
675 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
681 for (i
= 0; i
< 32; i
++)
683 *(uint32_t *)ptr
= tswapl(env
->gpr
[i
]);
687 *(uint32_t *)ptr
= tswapl(env
->CP0_Status
);
690 *(uint32_t *)ptr
= tswapl(env
->LO
);
693 *(uint32_t *)ptr
= tswapl(env
->HI
);
696 *(uint32_t *)ptr
= tswapl(env
->CP0_BadVAddr
);
699 *(uint32_t *)ptr
= tswapl(env
->CP0_Cause
);
702 *(uint32_t *)ptr
= tswapl(env
->PC
);
706 for (i
= 0; i
< 32; i
++)
708 *(uint32_t *)ptr
= tswapl(FPR_W (env
, i
));
712 *(uint32_t *)ptr
= tswapl(env
->fcr31
);
715 *(uint32_t *)ptr
= tswapl(env
->fcr0
);
719 /* 32 FP registers, fsr, fir, fp. Not yet implemented. */
720 /* what's 'fp' mean here? */
722 return ptr
- mem_buf
;
725 /* convert MIPS rounding mode in FCR31 to IEEE library */
726 static unsigned int ieee_rm
[] =
728 float_round_nearest_even
,
733 #define RESTORE_ROUNDING_MODE \
734 set_float_rounding_mode(ieee_rm[env->fcr31 & 3], &env->fp_status)
736 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
742 for (i
= 0; i
< 32; i
++)
744 env
->gpr
[i
] = tswapl(*(uint32_t *)ptr
);
748 env
->CP0_Status
= tswapl(*(uint32_t *)ptr
);
751 env
->LO
= tswapl(*(uint32_t *)ptr
);
754 env
->HI
= tswapl(*(uint32_t *)ptr
);
757 env
->CP0_BadVAddr
= tswapl(*(uint32_t *)ptr
);
760 env
->CP0_Cause
= tswapl(*(uint32_t *)ptr
);
763 env
->PC
= tswapl(*(uint32_t *)ptr
);
767 for (i
= 0; i
< 32; i
++)
769 FPR_W (env
, i
) = tswapl(*(uint32_t *)ptr
);
773 env
->fcr31
= tswapl(*(uint32_t *)ptr
) & 0x0183FFFF;
776 env
->fcr0
= tswapl(*(uint32_t *)ptr
);
779 /* set rounding mode */
780 RESTORE_ROUNDING_MODE
;
782 #ifndef CONFIG_SOFTFLOAT
783 /* no floating point exception for native float */
784 SET_FP_ENABLE(env
->fcr31
, 0);
788 #elif defined (TARGET_SH4)
789 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
791 uint32_t *ptr
= (uint32_t *)mem_buf
;
794 #define SAVE(x) *ptr++=tswapl(x)
795 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
796 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
+ 16]);
798 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
]);
800 for (i
= 8; i
< 16; i
++) SAVE(env
->gregs
[i
]);
808 SAVE (0); /* TICKS */
809 SAVE (0); /* STALLS */
810 SAVE (0); /* CYCLES */
811 SAVE (0); /* INSTS */
814 return ((uint8_t *)ptr
- mem_buf
);
817 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
819 uint32_t *ptr
= (uint32_t *)mem_buf
;
822 #define LOAD(x) (x)=*ptr++;
823 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
824 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
+ 16]);
826 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
]);
828 for (i
= 8; i
< 16; i
++) LOAD(env
->gregs
[i
]);
838 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
843 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
849 static int gdb_handle_packet(GDBState
*s
, CPUState
*env
, const char *line_buf
)
852 int ch
, reg_size
, type
;
854 uint8_t mem_buf
[2000];
856 target_ulong addr
, len
;
859 printf("command='%s'\n", line_buf
);
865 /* TODO: Make this return the correct value for user-mode. */
866 snprintf(buf
, sizeof(buf
), "S%02x", SIGTRAP
);
871 addr
= strtoull(p
, (char **)&p
, 16);
872 #if defined(TARGET_I386)
874 #elif defined (TARGET_PPC)
876 #elif defined (TARGET_SPARC)
879 #elif defined (TARGET_ARM)
880 env
->regs
[15] = addr
;
881 #elif defined (TARGET_SH4)
885 #ifdef CONFIG_USER_ONLY
886 s
->running_state
= 1;
893 addr
= strtoul(p
, (char **)&p
, 16);
894 #if defined(TARGET_I386)
896 #elif defined (TARGET_PPC)
898 #elif defined (TARGET_SPARC)
901 #elif defined (TARGET_ARM)
902 env
->regs
[15] = addr
;
903 #elif defined (TARGET_SH4)
907 cpu_single_step(env
, 1);
908 #ifdef CONFIG_USER_ONLY
909 s
->running_state
= 1;
919 ret
= strtoull(p
, (char **)&p
, 16);
922 err
= strtoull(p
, (char **)&p
, 16);
929 if (gdb_current_syscall_cb
)
930 gdb_current_syscall_cb(s
->env
, ret
, err
);
932 put_packet(s
, "T02");
934 #ifdef CONFIG_USER_ONLY
935 s
->running_state
= 1;
943 reg_size
= cpu_gdb_read_registers(env
, mem_buf
);
944 memtohex(buf
, mem_buf
, reg_size
);
948 registers
= (void *)mem_buf
;
950 hextomem((uint8_t *)registers
, p
, len
);
951 cpu_gdb_write_registers(env
, mem_buf
, len
);
955 addr
= strtoull(p
, (char **)&p
, 16);
958 len
= strtoull(p
, NULL
, 16);
959 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 0) != 0) {
960 put_packet (s
, "E14");
962 memtohex(buf
, mem_buf
, len
);
967 addr
= strtoull(p
, (char **)&p
, 16);
970 len
= strtoull(p
, (char **)&p
, 16);
973 hextomem(mem_buf
, p
, len
);
974 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 1) != 0)
975 put_packet(s
, "E14");
980 type
= strtoul(p
, (char **)&p
, 16);
983 addr
= strtoull(p
, (char **)&p
, 16);
986 len
= strtoull(p
, (char **)&p
, 16);
987 if (type
== 0 || type
== 1) {
988 if (cpu_breakpoint_insert(env
, addr
) < 0)
989 goto breakpoint_error
;
993 put_packet(s
, "E22");
997 type
= strtoul(p
, (char **)&p
, 16);
1000 addr
= strtoull(p
, (char **)&p
, 16);
1003 len
= strtoull(p
, (char **)&p
, 16);
1004 if (type
== 0 || type
== 1) {
1005 cpu_breakpoint_remove(env
, addr
);
1006 put_packet(s
, "OK");
1008 goto breakpoint_error
;
1011 #ifdef CONFIG_LINUX_USER
1013 if (strncmp(p
, "Offsets", 7) == 0) {
1014 TaskState
*ts
= env
->opaque
;
1016 sprintf(buf
, "Text=%x;Data=%x;Bss=%x", ts
->info
->code_offset
,
1017 ts
->info
->data_offset
, ts
->info
->data_offset
);
1025 /* put empty packet */
1033 extern void tb_flush(CPUState
*env
);
1035 #ifndef CONFIG_USER_ONLY
1036 static void gdb_vm_stopped(void *opaque
, int reason
)
1038 GDBState
*s
= opaque
;
1042 if (s
->state
== RS_SYSCALL
)
1045 /* disable single step if it was enable */
1046 cpu_single_step(s
->env
, 0);
1048 if (reason
== EXCP_DEBUG
) {
1051 } else if (reason
== EXCP_INTERRUPT
) {
1056 snprintf(buf
, sizeof(buf
), "S%02x", ret
);
1061 /* Send a gdb syscall request.
1062 This accepts limited printf-style format specifiers, specifically:
1063 %x - target_ulong argument printed in hex.
1064 %s - string pointer (target_ulong) and length (int) pair. */
1065 void gdb_do_syscall(gdb_syscall_complete_cb cb
, char *fmt
, ...)
1073 s
= gdb_syscall_state
;
1076 gdb_current_syscall_cb
= cb
;
1077 s
->state
= RS_SYSCALL
;
1078 #ifndef CONFIG_USER_ONLY
1079 vm_stop(EXCP_DEBUG
);
1090 addr
= va_arg(va
, target_ulong
);
1091 p
+= sprintf(p
, TARGET_FMT_lx
, addr
);
1094 addr
= va_arg(va
, target_ulong
);
1095 p
+= sprintf(p
, TARGET_FMT_lx
"/%x", addr
, va_arg(va
, int));
1098 fprintf(stderr
, "gdbstub: Bad syscall format string '%s'\n",
1108 #ifdef CONFIG_USER_ONLY
1109 gdb_handlesig(s
->env
, 0);
1111 cpu_interrupt(s
->env
, CPU_INTERRUPT_EXIT
);
1115 static void gdb_read_byte(GDBState
*s
, int ch
)
1117 CPUState
*env
= s
->env
;
1121 #ifndef CONFIG_USER_ONLY
1122 if (s
->last_packet_len
) {
1123 /* Waiting for a response to the last packet. If we see the start
1124 of a new command then abandon the previous response. */
1127 printf("Got NACK, retransmitting\n");
1129 put_buffer(s
, s
->last_packet
, s
->last_packet_len
);
1133 printf("Got ACK\n");
1135 printf("Got '%c' when expecting ACK/NACK\n", ch
);
1137 if (ch
== '+' || ch
== '$')
1138 s
->last_packet_len
= 0;
1143 /* when the CPU is running, we cannot do anything except stop
1144 it when receiving a char */
1145 vm_stop(EXCP_INTERRUPT
);
1152 s
->line_buf_index
= 0;
1153 s
->state
= RS_GETLINE
;
1158 s
->state
= RS_CHKSUM1
;
1159 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
1162 s
->line_buf
[s
->line_buf_index
++] = ch
;
1166 s
->line_buf
[s
->line_buf_index
] = '\0';
1167 s
->line_csum
= fromhex(ch
) << 4;
1168 s
->state
= RS_CHKSUM2
;
1171 s
->line_csum
|= fromhex(ch
);
1173 for(i
= 0; i
< s
->line_buf_index
; i
++) {
1174 csum
+= s
->line_buf
[i
];
1176 if (s
->line_csum
!= (csum
& 0xff)) {
1178 put_buffer(s
, reply
, 1);
1182 put_buffer(s
, reply
, 1);
1183 s
->state
= gdb_handle_packet(s
, env
, s
->line_buf
);
1192 #ifdef CONFIG_USER_ONLY
1194 gdb_handlesig (CPUState
*env
, int sig
)
1200 if (gdbserver_fd
< 0)
1203 s
= &gdbserver_state
;
1205 /* disable single step if it was enabled */
1206 cpu_single_step(env
, 0);
1211 snprintf(buf
, sizeof(buf
), "S%02x", sig
);
1217 s
->running_state
= 0;
1218 while (s
->running_state
== 0) {
1219 n
= read (s
->fd
, buf
, 256);
1224 for (i
= 0; i
< n
; i
++)
1225 gdb_read_byte (s
, buf
[i
]);
1227 else if (n
== 0 || errno
!= EAGAIN
)
1229 /* XXX: Connection closed. Should probably wait for annother
1230 connection before continuing. */
1237 /* Tell the remote gdb that the process has exited. */
1238 void gdb_exit(CPUState
*env
, int code
)
1243 if (gdbserver_fd
< 0)
1246 s
= &gdbserver_state
;
1248 snprintf(buf
, sizeof(buf
), "W%02x", code
);
1253 static void gdb_accept(void *opaque
)
1256 struct sockaddr_in sockaddr
;
1261 len
= sizeof(sockaddr
);
1262 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
1263 if (fd
< 0 && errno
!= EINTR
) {
1266 } else if (fd
>= 0) {
1271 /* set short latency */
1273 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1275 s
= &gdbserver_state
;
1276 memset (s
, 0, sizeof (GDBState
));
1277 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1280 gdb_syscall_state
= s
;
1282 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1285 static int gdbserver_open(int port
)
1287 struct sockaddr_in sockaddr
;
1290 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1296 /* allow fast reuse */
1298 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
1300 sockaddr
.sin_family
= AF_INET
;
1301 sockaddr
.sin_port
= htons(port
);
1302 sockaddr
.sin_addr
.s_addr
= 0;
1303 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
1308 ret
= listen(fd
, 0);
1316 int gdbserver_start(int port
)
1318 gdbserver_fd
= gdbserver_open(port
);
1319 if (gdbserver_fd
< 0)
1321 /* accept connections */
1326 static int gdb_chr_can_recieve(void *opaque
)
1331 static void gdb_chr_recieve(void *opaque
, const uint8_t *buf
, int size
)
1333 GDBState
*s
= opaque
;
1336 for (i
= 0; i
< size
; i
++) {
1337 gdb_read_byte(s
, buf
[i
]);
1341 static void gdb_chr_event(void *opaque
, int event
)
1344 case CHR_EVENT_RESET
:
1345 vm_stop(EXCP_INTERRUPT
);
1346 gdb_syscall_state
= opaque
;
1353 int gdbserver_start(CharDriverState
*chr
)
1360 s
= qemu_mallocz(sizeof(GDBState
));
1364 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1366 qemu_chr_add_handlers(chr
, gdb_chr_can_recieve
, gdb_chr_recieve
,
1368 qemu_add_vm_stop_handler(gdb_vm_stopped
, s
);
1372 int gdbserver_start_port(int port
)
1374 CharDriverState
*chr
;
1375 char gdbstub_port_name
[128];
1377 snprintf(gdbstub_port_name
, sizeof(gdbstub_port_name
),
1378 "tcp::%d,nowait,nodelay,server", port
);
1379 chr
= qemu_chr_open(gdbstub_port_name
);
1382 return gdbserver_start(chr
);