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 */
56 /* XXX: This is not thread safe. Do we care? */
57 static int gdbserver_fd
= -1;
59 typedef struct GDBState
{
60 CPUState
*env
; /* current CPU */
61 enum RSState state
; /* parsing state */
66 #ifdef CONFIG_USER_ONLY
71 #ifdef CONFIG_USER_ONLY
72 /* XXX: remove this hack. */
73 static GDBState gdbserver_state
;
76 static int get_char(GDBState
*s
)
82 ret
= recv(s
->fd
, &ch
, 1, 0);
84 if (errno
!= EINTR
&& errno
!= EAGAIN
)
86 } else if (ret
== 0) {
95 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
100 ret
= send(s
->fd
, buf
, len
, 0);
102 if (errno
!= EINTR
&& errno
!= EAGAIN
)
111 static inline int fromhex(int v
)
113 if (v
>= '0' && v
<= '9')
115 else if (v
>= 'A' && v
<= 'F')
117 else if (v
>= 'a' && v
<= 'f')
123 static inline int tohex(int v
)
131 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
136 for(i
= 0; i
< len
; i
++) {
138 *q
++ = tohex(c
>> 4);
139 *q
++ = tohex(c
& 0xf);
144 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
148 for(i
= 0; i
< len
; i
++) {
149 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
154 /* return -1 if error, 0 if OK */
155 static int put_packet(GDBState
*s
, char *buf
)
158 int len
, csum
, ch
, i
;
161 printf("reply='%s'\n", buf
);
166 put_buffer(s
, buf1
, 1);
168 put_buffer(s
, buf
, len
);
170 for(i
= 0; i
< len
; i
++) {
174 buf1
[1] = tohex((csum
>> 4) & 0xf);
175 buf1
[2] = tohex((csum
) & 0xf);
177 put_buffer(s
, buf1
, 3);
188 #if defined(TARGET_X86_64)
190 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
192 uint8_t *p
= mem_buf
;
195 #define PUTREG(x) do { \
196 target_ulong reg = tswapl(x); \
197 memcpy(p, ®, sizeof reg); \
200 #define PUTREG32(x) do { \
201 uint32_t reg = tswap32(x); \
202 memcpy(p, ®, sizeof reg); \
205 #define PUTREGF(x) do { \
206 memcpy(p, &(x), 10); \
210 PUTREG(env
->regs
[R_EAX
]);
211 PUTREG(env
->regs
[R_EBX
]);
212 PUTREG(env
->regs
[R_ECX
]);
213 PUTREG(env
->regs
[R_EDX
]);
214 PUTREG(env
->regs
[R_ESI
]);
215 PUTREG(env
->regs
[R_EDI
]);
216 PUTREG(env
->regs
[R_EBP
]);
217 PUTREG(env
->regs
[R_ESP
]);
218 PUTREG(env
->regs
[8]);
219 PUTREG(env
->regs
[9]);
220 PUTREG(env
->regs
[10]);
221 PUTREG(env
->regs
[11]);
222 PUTREG(env
->regs
[12]);
223 PUTREG(env
->regs
[13]);
224 PUTREG(env
->regs
[14]);
225 PUTREG(env
->regs
[15]);
228 PUTREG32(env
->eflags
);
229 PUTREG32(env
->segs
[R_CS
].selector
);
230 PUTREG32(env
->segs
[R_SS
].selector
);
231 PUTREG32(env
->segs
[R_DS
].selector
);
232 PUTREG32(env
->segs
[R_ES
].selector
);
233 PUTREG32(env
->segs
[R_FS
].selector
);
234 PUTREG32(env
->segs
[R_GS
].selector
);
235 /* XXX: convert floats */
236 for(i
= 0; i
< 8; i
++) {
237 PUTREGF(env
->fpregs
[i
]);
240 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
242 PUTREG32(0); /* XXX: convert tags */
243 PUTREG32(0); /* fiseg */
244 PUTREG32(0); /* fioff */
245 PUTREG32(0); /* foseg */
246 PUTREG32(0); /* fooff */
247 PUTREG32(0); /* fop */
256 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
258 uint8_t *p
= mem_buf
;
262 #define GETREG(x) do { \
264 memcpy(®, p, sizeof reg); \
268 #define GETREG32(x) do { \
270 memcpy(®, p, sizeof reg); \
274 #define GETREGF(x) do { \
275 memcpy(&(x), p, 10); \
279 GETREG(env
->regs
[R_EAX
]);
280 GETREG(env
->regs
[R_EBX
]);
281 GETREG(env
->regs
[R_ECX
]);
282 GETREG(env
->regs
[R_EDX
]);
283 GETREG(env
->regs
[R_ESI
]);
284 GETREG(env
->regs
[R_EDI
]);
285 GETREG(env
->regs
[R_EBP
]);
286 GETREG(env
->regs
[R_ESP
]);
287 GETREG(env
->regs
[8]);
288 GETREG(env
->regs
[9]);
289 GETREG(env
->regs
[10]);
290 GETREG(env
->regs
[11]);
291 GETREG(env
->regs
[12]);
292 GETREG(env
->regs
[13]);
293 GETREG(env
->regs
[14]);
294 GETREG(env
->regs
[15]);
297 GETREG32(env
->eflags
);
298 GETREG32(env
->segs
[R_CS
].selector
);
299 GETREG32(env
->segs
[R_SS
].selector
);
300 GETREG32(env
->segs
[R_DS
].selector
);
301 GETREG32(env
->segs
[R_ES
].selector
);
302 GETREG32(env
->segs
[R_FS
].selector
);
303 GETREG32(env
->segs
[R_GS
].selector
);
304 /* XXX: convert floats */
305 for(i
= 0; i
< 8; i
++) {
306 GETREGF(env
->fpregs
[i
]);
309 GETREG32(fpus
); /* XXX: convert fpus */
310 GETREG32(junk
); /* XXX: convert tags */
311 GETREG32(junk
); /* fiseg */
312 GETREG32(junk
); /* fioff */
313 GETREG32(junk
); /* foseg */
314 GETREG32(junk
); /* fooff */
315 GETREG32(junk
); /* fop */
322 #elif defined(TARGET_I386)
324 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
326 uint32_t *registers
= (uint32_t *)mem_buf
;
329 for(i
= 0; i
< 8; i
++) {
330 registers
[i
] = env
->regs
[i
];
332 registers
[8] = env
->eip
;
333 registers
[9] = env
->eflags
;
334 registers
[10] = env
->segs
[R_CS
].selector
;
335 registers
[11] = env
->segs
[R_SS
].selector
;
336 registers
[12] = env
->segs
[R_DS
].selector
;
337 registers
[13] = env
->segs
[R_ES
].selector
;
338 registers
[14] = env
->segs
[R_FS
].selector
;
339 registers
[15] = env
->segs
[R_GS
].selector
;
340 /* XXX: convert floats */
341 for(i
= 0; i
< 8; i
++) {
342 memcpy(mem_buf
+ 16 * 4 + i
* 10, &env
->fpregs
[i
], 10);
344 registers
[36] = env
->fpuc
;
345 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
346 registers
[37] = fpus
;
347 registers
[38] = 0; /* XXX: convert tags */
348 registers
[39] = 0; /* fiseg */
349 registers
[40] = 0; /* fioff */
350 registers
[41] = 0; /* foseg */
351 registers
[42] = 0; /* fooff */
352 registers
[43] = 0; /* fop */
354 for(i
= 0; i
< 16; i
++)
355 tswapls(®isters
[i
]);
356 for(i
= 36; i
< 44; i
++)
357 tswapls(®isters
[i
]);
361 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
363 uint32_t *registers
= (uint32_t *)mem_buf
;
366 for(i
= 0; i
< 8; i
++) {
367 env
->regs
[i
] = tswapl(registers
[i
]);
369 env
->eip
= tswapl(registers
[8]);
370 env
->eflags
= tswapl(registers
[9]);
371 #if defined(CONFIG_USER_ONLY)
372 #define LOAD_SEG(index, sreg)\
373 if (tswapl(registers[index]) != env->segs[sreg].selector)\
374 cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
384 #elif defined (TARGET_PPC)
385 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
387 uint32_t *registers
= (uint32_t *)mem_buf
, tmp
;
391 for(i
= 0; i
< 32; i
++) {
392 registers
[i
] = tswapl(env
->gpr
[i
]);
395 for (i
= 0; i
< 32; i
++) {
396 registers
[(i
* 2) + 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
397 registers
[(i
* 2) + 33] = tswapl(*((uint32_t *)&env
->fpr
[i
] + 1));
399 /* nip, msr, ccr, lnk, ctr, xer, mq */
400 registers
[96] = tswapl(env
->nip
);
401 registers
[97] = tswapl(do_load_msr(env
));
403 for (i
= 0; i
< 8; i
++)
404 tmp
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
405 registers
[98] = tswapl(tmp
);
406 registers
[99] = tswapl(env
->lr
);
407 registers
[100] = tswapl(env
->ctr
);
408 registers
[101] = tswapl(do_load_xer(env
));
414 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
416 uint32_t *registers
= (uint32_t *)mem_buf
;
420 for (i
= 0; i
< 32; i
++) {
421 env
->gpr
[i
] = tswapl(registers
[i
]);
424 for (i
= 0; i
< 32; i
++) {
425 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[(i
* 2) + 32]);
426 *((uint32_t *)&env
->fpr
[i
] + 1) = tswapl(registers
[(i
* 2) + 33]);
428 /* nip, msr, ccr, lnk, ctr, xer, mq */
429 env
->nip
= tswapl(registers
[96]);
430 do_store_msr(env
, tswapl(registers
[97]));
431 registers
[98] = tswapl(registers
[98]);
432 for (i
= 0; i
< 8; i
++)
433 env
->crf
[i
] = (registers
[98] >> (32 - ((i
+ 1) * 4))) & 0xF;
434 env
->lr
= tswapl(registers
[99]);
435 env
->ctr
= tswapl(registers
[100]);
436 do_store_xer(env
, tswapl(registers
[101]));
438 #elif defined (TARGET_SPARC)
439 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
441 target_ulong
*registers
= (target_ulong
*)mem_buf
;
445 for(i
= 0; i
< 8; i
++) {
446 registers
[i
] = tswapl(env
->gregs
[i
]);
448 /* fill in register window */
449 for(i
= 0; i
< 24; i
++) {
450 registers
[i
+ 8] = tswapl(env
->regwptr
[i
]);
452 #ifndef TARGET_SPARC64
454 for (i
= 0; i
< 32; i
++) {
455 registers
[i
+ 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
457 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
458 registers
[64] = tswapl(env
->y
);
463 registers
[65] = tswapl(tmp
);
465 registers
[66] = tswapl(env
->wim
);
466 registers
[67] = tswapl(env
->tbr
);
467 registers
[68] = tswapl(env
->pc
);
468 registers
[69] = tswapl(env
->npc
);
469 registers
[70] = tswapl(env
->fsr
);
470 registers
[71] = 0; /* csr */
472 return 73 * sizeof(target_ulong
);
475 for (i
= 0; i
< 64; i
+= 2) {
478 tmp
= (uint64_t)tswap32(*((uint32_t *)&env
->fpr
[i
])) << 32;
479 tmp
|= tswap32(*((uint32_t *)&env
->fpr
[i
+ 1]));
480 registers
[i
/2 + 32] = tmp
;
482 registers
[64] = tswapl(env
->pc
);
483 registers
[65] = tswapl(env
->npc
);
484 registers
[66] = tswapl(env
->tstate
[env
->tl
]);
485 registers
[67] = tswapl(env
->fsr
);
486 registers
[68] = tswapl(env
->fprs
);
487 registers
[69] = tswapl(env
->y
);
488 return 70 * sizeof(target_ulong
);
492 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
494 target_ulong
*registers
= (target_ulong
*)mem_buf
;
498 for(i
= 0; i
< 7; i
++) {
499 env
->gregs
[i
] = tswapl(registers
[i
]);
501 /* fill in register window */
502 for(i
= 0; i
< 24; i
++) {
503 env
->regwptr
[i
] = tswapl(registers
[i
+ 8]);
505 #ifndef TARGET_SPARC64
507 for (i
= 0; i
< 32; i
++) {
508 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[i
+ 32]);
510 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
511 env
->y
= tswapl(registers
[64]);
512 PUT_PSR(env
, tswapl(registers
[65]));
513 env
->wim
= tswapl(registers
[66]);
514 env
->tbr
= tswapl(registers
[67]);
515 env
->pc
= tswapl(registers
[68]);
516 env
->npc
= tswapl(registers
[69]);
517 env
->fsr
= tswapl(registers
[70]);
519 for (i
= 0; i
< 64; i
+= 2) {
520 *((uint32_t *)&env
->fpr
[i
]) = tswap32(registers
[i
/2 + 32] >> 32);
521 *((uint32_t *)&env
->fpr
[i
+ 1]) = tswap32(registers
[i
/2 + 32] & 0xffffffff);
523 env
->pc
= tswapl(registers
[64]);
524 env
->npc
= tswapl(registers
[65]);
525 env
->tstate
[env
->tl
] = tswapl(registers
[66]);
526 env
->fsr
= tswapl(registers
[67]);
527 env
->fprs
= tswapl(registers
[68]);
528 env
->y
= tswapl(registers
[69]);
531 #elif defined (TARGET_ARM)
532 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
538 /* 16 core integer registers (4 bytes each). */
539 for (i
= 0; i
< 16; i
++)
541 *(uint32_t *)ptr
= tswapl(env
->regs
[i
]);
544 /* 8 FPA registers (12 bytes each), FPS (4 bytes).
545 Not yet implemented. */
546 memset (ptr
, 0, 8 * 12 + 4);
548 /* CPSR (4 bytes). */
549 *(uint32_t *)ptr
= tswapl (cpsr_read(env
));
552 return ptr
- mem_buf
;
555 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
561 /* Core integer registers. */
562 for (i
= 0; i
< 16; i
++)
564 env
->regs
[i
] = tswapl(*(uint32_t *)ptr
);
567 /* Ignore FPA regs and scr. */
569 cpsr_write (env
, tswapl(*(uint32_t *)ptr
), 0xffffffff);
571 #elif defined (TARGET_MIPS)
572 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
578 for (i
= 0; i
< 32; i
++)
580 *(uint32_t *)ptr
= tswapl(env
->gpr
[i
]);
584 *(uint32_t *)ptr
= tswapl(env
->CP0_Status
);
587 *(uint32_t *)ptr
= tswapl(env
->LO
);
590 *(uint32_t *)ptr
= tswapl(env
->HI
);
593 *(uint32_t *)ptr
= tswapl(env
->CP0_BadVAddr
);
596 *(uint32_t *)ptr
= tswapl(env
->CP0_Cause
);
599 *(uint32_t *)ptr
= tswapl(env
->PC
);
602 /* 32 FP registers, fsr, fir, fp. Not yet implemented. */
604 return ptr
- mem_buf
;
607 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
613 for (i
= 0; i
< 32; i
++)
615 env
->gpr
[i
] = tswapl(*(uint32_t *)ptr
);
619 env
->CP0_Status
= tswapl(*(uint32_t *)ptr
);
622 env
->LO
= tswapl(*(uint32_t *)ptr
);
625 env
->HI
= tswapl(*(uint32_t *)ptr
);
628 env
->CP0_BadVAddr
= tswapl(*(uint32_t *)ptr
);
631 env
->CP0_Cause
= tswapl(*(uint32_t *)ptr
);
634 env
->PC
= tswapl(*(uint32_t *)ptr
);
637 #elif defined (TARGET_SH4)
638 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
640 uint32_t *ptr
= (uint32_t *)mem_buf
;
643 #define SAVE(x) *ptr++=tswapl(x)
644 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
645 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
+ 16]);
647 for (i
= 0; i
< 8; i
++) SAVE(env
->gregs
[i
]);
649 for (i
= 8; i
< 16; i
++) SAVE(env
->gregs
[i
]);
657 SAVE (0); /* TICKS */
658 SAVE (0); /* STALLS */
659 SAVE (0); /* CYCLES */
660 SAVE (0); /* INSTS */
663 return ((uint8_t *)ptr
- mem_buf
);
666 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
668 uint32_t *ptr
= (uint32_t *)mem_buf
;
671 #define LOAD(x) (x)=*ptr++;
672 if ((env
->sr
& (SR_MD
| SR_RB
)) == (SR_MD
| SR_RB
)) {
673 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
+ 16]);
675 for (i
= 0; i
< 8; i
++) LOAD(env
->gregs
[i
]);
677 for (i
= 8; i
< 16; i
++) LOAD(env
->gregs
[i
]);
687 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
692 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
698 static int gdb_handle_packet(GDBState
*s
, CPUState
*env
, const char *line_buf
)
701 int ch
, reg_size
, type
;
703 uint8_t mem_buf
[2000];
705 target_ulong addr
, len
;
708 printf("command='%s'\n", line_buf
);
714 /* TODO: Make this return the correct value for user-mode. */
715 snprintf(buf
, sizeof(buf
), "S%02x", SIGTRAP
);
720 addr
= strtoull(p
, (char **)&p
, 16);
721 #if defined(TARGET_I386)
723 #elif defined (TARGET_PPC)
725 #elif defined (TARGET_SPARC)
728 #elif defined (TARGET_ARM)
729 env
->regs
[15] = addr
;
730 #elif defined (TARGET_SH4)
734 #ifdef CONFIG_USER_ONLY
735 s
->running_state
= 1;
742 addr
= strtoul(p
, (char **)&p
, 16);
743 #if defined(TARGET_I386)
745 #elif defined (TARGET_PPC)
747 #elif defined (TARGET_SPARC)
750 #elif defined (TARGET_ARM)
751 env
->regs
[15] = addr
;
752 #elif defined (TARGET_SH4)
756 cpu_single_step(env
, 1);
757 #ifdef CONFIG_USER_ONLY
758 s
->running_state
= 1;
764 reg_size
= cpu_gdb_read_registers(env
, mem_buf
);
765 memtohex(buf
, mem_buf
, reg_size
);
769 registers
= (void *)mem_buf
;
771 hextomem((uint8_t *)registers
, p
, len
);
772 cpu_gdb_write_registers(env
, mem_buf
, len
);
776 addr
= strtoull(p
, (char **)&p
, 16);
779 len
= strtoull(p
, NULL
, 16);
780 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 0) != 0) {
781 put_packet (s
, "E14");
783 memtohex(buf
, mem_buf
, len
);
788 addr
= strtoull(p
, (char **)&p
, 16);
791 len
= strtoull(p
, (char **)&p
, 16);
794 hextomem(mem_buf
, p
, len
);
795 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 1) != 0)
796 put_packet(s
, "E14");
801 type
= strtoul(p
, (char **)&p
, 16);
804 addr
= strtoull(p
, (char **)&p
, 16);
807 len
= strtoull(p
, (char **)&p
, 16);
808 if (type
== 0 || type
== 1) {
809 if (cpu_breakpoint_insert(env
, addr
) < 0)
810 goto breakpoint_error
;
814 put_packet(s
, "E22");
818 type
= strtoul(p
, (char **)&p
, 16);
821 addr
= strtoull(p
, (char **)&p
, 16);
824 len
= strtoull(p
, (char **)&p
, 16);
825 if (type
== 0 || type
== 1) {
826 cpu_breakpoint_remove(env
, addr
);
829 goto breakpoint_error
;
832 #ifdef CONFIG_USER_ONLY
834 if (strncmp(p
, "Offsets", 7) == 0) {
835 TaskState
*ts
= env
->opaque
;
837 sprintf(buf
, "Text=%x;Data=%x;Bss=%x", ts
->info
->code_offset
,
838 ts
->info
->data_offset
, ts
->info
->data_offset
);
846 /* put empty packet */
854 extern void tb_flush(CPUState
*env
);
856 #ifndef CONFIG_USER_ONLY
857 static void gdb_vm_stopped(void *opaque
, int reason
)
859 GDBState
*s
= opaque
;
863 /* disable single step if it was enable */
864 cpu_single_step(s
->env
, 0);
866 if (reason
== EXCP_DEBUG
) {
869 } else if (reason
== EXCP_INTERRUPT
) {
874 snprintf(buf
, sizeof(buf
), "S%02x", ret
);
879 static void gdb_read_byte(GDBState
*s
, int ch
)
881 CPUState
*env
= s
->env
;
885 #ifndef CONFIG_USER_ONLY
887 /* when the CPU is running, we cannot do anything except stop
888 it when receiving a char */
889 vm_stop(EXCP_INTERRUPT
);
896 s
->line_buf_index
= 0;
897 s
->state
= RS_GETLINE
;
902 s
->state
= RS_CHKSUM1
;
903 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
906 s
->line_buf
[s
->line_buf_index
++] = ch
;
910 s
->line_buf
[s
->line_buf_index
] = '\0';
911 s
->line_csum
= fromhex(ch
) << 4;
912 s
->state
= RS_CHKSUM2
;
915 s
->line_csum
|= fromhex(ch
);
917 for(i
= 0; i
< s
->line_buf_index
; i
++) {
918 csum
+= s
->line_buf
[i
];
920 if (s
->line_csum
!= (csum
& 0xff)) {
922 put_buffer(s
, reply
, 1);
926 put_buffer(s
, reply
, 1);
927 s
->state
= gdb_handle_packet(s
, env
, s
->line_buf
);
934 #ifdef CONFIG_USER_ONLY
936 gdb_handlesig (CPUState
*env
, int sig
)
942 if (gdbserver_fd
< 0)
945 s
= &gdbserver_state
;
947 /* disable single step if it was enabled */
948 cpu_single_step(env
, 0);
953 snprintf(buf
, sizeof(buf
), "S%02x", sig
);
959 s
->running_state
= 0;
960 while (s
->running_state
== 0) {
961 n
= read (s
->fd
, buf
, 256);
966 for (i
= 0; i
< n
; i
++)
967 gdb_read_byte (s
, buf
[i
]);
969 else if (n
== 0 || errno
!= EAGAIN
)
971 /* XXX: Connection closed. Should probably wait for annother
972 connection before continuing. */
979 /* Tell the remote gdb that the process has exited. */
980 void gdb_exit(CPUState
*env
, int code
)
985 if (gdbserver_fd
< 0)
988 s
= &gdbserver_state
;
990 snprintf(buf
, sizeof(buf
), "W%02x", code
);
995 static void gdb_read(void *opaque
)
997 GDBState
*s
= opaque
;
1001 size
= recv(s
->fd
, buf
, sizeof(buf
), 0);
1005 /* end of connection */
1006 qemu_del_vm_stop_handler(gdb_vm_stopped
, s
);
1007 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
1011 for(i
= 0; i
< size
; i
++)
1012 gdb_read_byte(s
, buf
[i
]);
1018 static void gdb_accept(void *opaque
)
1021 struct sockaddr_in sockaddr
;
1026 len
= sizeof(sockaddr
);
1027 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
1028 if (fd
< 0 && errno
!= EINTR
) {
1031 } else if (fd
>= 0) {
1036 /* set short latency */
1038 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
1040 #ifdef CONFIG_USER_ONLY
1041 s
= &gdbserver_state
;
1042 memset (s
, 0, sizeof (GDBState
));
1044 s
= qemu_mallocz(sizeof(GDBState
));
1050 s
->env
= first_cpu
; /* XXX: allow to change CPU */
1053 #ifdef CONFIG_USER_ONLY
1054 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
1056 socket_set_nonblock(fd
);
1059 vm_stop(EXCP_INTERRUPT
);
1061 /* start handling I/O */
1062 qemu_set_fd_handler(s
->fd
, gdb_read
, NULL
, s
);
1063 /* when the VM is stopped, the following callback is called */
1064 qemu_add_vm_stop_handler(gdb_vm_stopped
, s
);
1068 static int gdbserver_open(int port
)
1070 struct sockaddr_in sockaddr
;
1073 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
1079 /* allow fast reuse */
1081 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
1083 sockaddr
.sin_family
= AF_INET
;
1084 sockaddr
.sin_port
= htons(port
);
1085 sockaddr
.sin_addr
.s_addr
= 0;
1086 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
1091 ret
= listen(fd
, 0);
1096 #ifndef CONFIG_USER_ONLY
1097 socket_set_nonblock(fd
);
1102 int gdbserver_start(int port
)
1104 gdbserver_fd
= gdbserver_open(port
);
1105 if (gdbserver_fd
< 0)
1107 /* accept connections */
1108 #ifdef CONFIG_USER_ONLY
1111 qemu_set_fd_handler(gdbserver_fd
, gdb_accept
, NULL
, NULL
);