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_I386)
190 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
192 uint32_t *registers
= (uint32_t *)mem_buf
;
195 for(i
= 0; i
< 8; i
++) {
196 registers
[i
] = env
->regs
[i
];
198 registers
[8] = env
->eip
;
199 registers
[9] = env
->eflags
;
200 registers
[10] = env
->segs
[R_CS
].selector
;
201 registers
[11] = env
->segs
[R_SS
].selector
;
202 registers
[12] = env
->segs
[R_DS
].selector
;
203 registers
[13] = env
->segs
[R_ES
].selector
;
204 registers
[14] = env
->segs
[R_FS
].selector
;
205 registers
[15] = env
->segs
[R_GS
].selector
;
206 /* XXX: convert floats */
207 for(i
= 0; i
< 8; i
++) {
208 memcpy(mem_buf
+ 16 * 4 + i
* 10, &env
->fpregs
[i
], 10);
210 registers
[36] = env
->fpuc
;
211 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
212 registers
[37] = fpus
;
213 registers
[38] = 0; /* XXX: convert tags */
214 registers
[39] = 0; /* fiseg */
215 registers
[40] = 0; /* fioff */
216 registers
[41] = 0; /* foseg */
217 registers
[42] = 0; /* fooff */
218 registers
[43] = 0; /* fop */
220 for(i
= 0; i
< 16; i
++)
221 tswapls(®isters
[i
]);
222 for(i
= 36; i
< 44; i
++)
223 tswapls(®isters
[i
]);
227 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
229 uint32_t *registers
= (uint32_t *)mem_buf
;
232 for(i
= 0; i
< 8; i
++) {
233 env
->regs
[i
] = tswapl(registers
[i
]);
235 env
->eip
= tswapl(registers
[8]);
236 env
->eflags
= tswapl(registers
[9]);
237 #if defined(CONFIG_USER_ONLY)
238 #define LOAD_SEG(index, sreg)\
239 if (tswapl(registers[index]) != env->segs[sreg].selector)\
240 cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
250 #elif defined (TARGET_PPC)
251 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
253 uint32_t *registers
= (uint32_t *)mem_buf
, tmp
;
257 for(i
= 0; i
< 32; i
++) {
258 registers
[i
] = tswapl(env
->gpr
[i
]);
261 for (i
= 0; i
< 32; i
++) {
262 registers
[(i
* 2) + 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
263 registers
[(i
* 2) + 33] = tswapl(*((uint32_t *)&env
->fpr
[i
] + 1));
265 /* nip, msr, ccr, lnk, ctr, xer, mq */
266 registers
[96] = tswapl(env
->nip
);
267 registers
[97] = tswapl(do_load_msr(env
));
269 for (i
= 0; i
< 8; i
++)
270 tmp
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
271 registers
[98] = tswapl(tmp
);
272 registers
[99] = tswapl(env
->lr
);
273 registers
[100] = tswapl(env
->ctr
);
274 registers
[101] = tswapl(do_load_xer(env
));
280 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
282 uint32_t *registers
= (uint32_t *)mem_buf
;
286 for (i
= 0; i
< 32; i
++) {
287 env
->gpr
[i
] = tswapl(registers
[i
]);
290 for (i
= 0; i
< 32; i
++) {
291 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[(i
* 2) + 32]);
292 *((uint32_t *)&env
->fpr
[i
] + 1) = tswapl(registers
[(i
* 2) + 33]);
294 /* nip, msr, ccr, lnk, ctr, xer, mq */
295 env
->nip
= tswapl(registers
[96]);
296 do_store_msr(env
, tswapl(registers
[97]));
297 registers
[98] = tswapl(registers
[98]);
298 for (i
= 0; i
< 8; i
++)
299 env
->crf
[i
] = (registers
[98] >> (32 - ((i
+ 1) * 4))) & 0xF;
300 env
->lr
= tswapl(registers
[99]);
301 env
->ctr
= tswapl(registers
[100]);
302 do_store_xer(env
, tswapl(registers
[101]));
304 #elif defined (TARGET_SPARC)
305 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
307 target_ulong
*registers
= (target_ulong
*)mem_buf
;
311 for(i
= 0; i
< 8; i
++) {
312 registers
[i
] = tswapl(env
->gregs
[i
]);
314 /* fill in register window */
315 for(i
= 0; i
< 24; i
++) {
316 registers
[i
+ 8] = tswapl(env
->regwptr
[i
]);
319 for (i
= 0; i
< 32; i
++) {
320 registers
[i
+ 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
322 #ifndef TARGET_SPARC64
323 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
324 registers
[64] = tswapl(env
->y
);
329 registers
[65] = tswapl(tmp
);
331 registers
[66] = tswapl(env
->wim
);
332 registers
[67] = tswapl(env
->tbr
);
333 registers
[68] = tswapl(env
->pc
);
334 registers
[69] = tswapl(env
->npc
);
335 registers
[70] = tswapl(env
->fsr
);
336 registers
[71] = 0; /* csr */
338 return 73 * sizeof(target_ulong
);
340 for (i
= 0; i
< 32; i
+= 2) {
341 registers
[i
/2 + 64] = tswapl(*((uint64_t *)&env
->fpr
[i
]));
343 registers
[81] = tswapl(env
->pc
);
344 registers
[82] = tswapl(env
->npc
);
345 registers
[83] = tswapl(env
->tstate
[env
->tl
]);
346 registers
[84] = tswapl(env
->fsr
);
347 registers
[85] = tswapl(env
->fprs
);
348 registers
[86] = tswapl(env
->y
);
349 return 87 * sizeof(target_ulong
);
353 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
355 target_ulong
*registers
= (target_ulong
*)mem_buf
;
359 for(i
= 0; i
< 7; i
++) {
360 env
->gregs
[i
] = tswapl(registers
[i
]);
362 /* fill in register window */
363 for(i
= 0; i
< 24; i
++) {
364 env
->regwptr
[i
] = tswapl(registers
[i
+ 8]);
367 for (i
= 0; i
< 32; i
++) {
368 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[i
+ 32]);
370 #ifndef TARGET_SPARC64
371 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
372 env
->y
= tswapl(registers
[64]);
373 PUT_PSR(env
, tswapl(registers
[65]));
374 env
->wim
= tswapl(registers
[66]);
375 env
->tbr
= tswapl(registers
[67]);
376 env
->pc
= tswapl(registers
[68]);
377 env
->npc
= tswapl(registers
[69]);
378 env
->fsr
= tswapl(registers
[70]);
380 for (i
= 0; i
< 32; i
+= 2) {
382 tmp
= tswapl(registers
[i
/2 + 64]) << 32;
383 tmp
|= tswapl(registers
[i
/2 + 64 + 1]);
384 *((uint64_t *)&env
->fpr
[i
]) = tmp
;
386 env
->pc
= tswapl(registers
[81]);
387 env
->npc
= tswapl(registers
[82]);
388 env
->tstate
[env
->tl
] = tswapl(registers
[83]);
389 env
->fsr
= tswapl(registers
[84]);
390 env
->fprs
= tswapl(registers
[85]);
391 env
->y
= tswapl(registers
[86]);
394 #elif defined (TARGET_ARM)
395 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
401 /* 16 core integer registers (4 bytes each). */
402 for (i
= 0; i
< 16; i
++)
404 *(uint32_t *)ptr
= tswapl(env
->regs
[i
]);
407 /* 8 FPA registers (12 bytes each), FPS (4 bytes).
408 Not yet implemented. */
409 memset (ptr
, 0, 8 * 12 + 4);
411 /* CPSR (4 bytes). */
412 *(uint32_t *)ptr
= tswapl (cpsr_read(env
));
415 return ptr
- mem_buf
;
418 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
424 /* Core integer registers. */
425 for (i
= 0; i
< 16; i
++)
427 env
->regs
[i
] = tswapl(*(uint32_t *)ptr
);
430 /* Ignore FPA regs and scr. */
432 cpsr_write (env
, tswapl(*(uint32_t *)ptr
), 0xffffffff);
434 #elif defined (TARGET_MIPS)
435 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
441 for (i
= 0; i
< 32; i
++)
443 *(uint32_t *)ptr
= tswapl(env
->gpr
[i
]);
447 *(uint32_t *)ptr
= tswapl(env
->CP0_Status
);
450 *(uint32_t *)ptr
= tswapl(env
->LO
);
453 *(uint32_t *)ptr
= tswapl(env
->HI
);
456 *(uint32_t *)ptr
= tswapl(env
->CP0_BadVAddr
);
459 *(uint32_t *)ptr
= tswapl(env
->CP0_Cause
);
462 *(uint32_t *)ptr
= tswapl(env
->PC
);
465 /* 32 FP registers, fsr, fir, fp. Not yet implemented. */
467 return ptr
- mem_buf
;
470 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
476 for (i
= 0; i
< 32; i
++)
478 env
->gpr
[i
] = tswapl(*(uint32_t *)ptr
);
482 env
->CP0_Status
= tswapl(*(uint32_t *)ptr
);
485 env
->LO
= tswapl(*(uint32_t *)ptr
);
488 env
->HI
= tswapl(*(uint32_t *)ptr
);
491 env
->CP0_BadVAddr
= tswapl(*(uint32_t *)ptr
);
494 env
->CP0_Cause
= tswapl(*(uint32_t *)ptr
);
497 env
->PC
= tswapl(*(uint32_t *)ptr
);
500 #elif defined (TARGET_SH4)
501 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
503 uint32_t *ptr
= (uint32_t *)mem_buf
;
506 #define SAVE(x) *ptr++=tswapl(x)
507 for (i
= 0; i
< 16; i
++) SAVE(env
->gregs
[i
]);
515 SAVE (0); /* TICKS */
516 SAVE (0); /* STALLS */
517 SAVE (0); /* CYCLES */
518 SAVE (0); /* INSTS */
521 return ((uint8_t *)ptr
- mem_buf
);
524 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
526 uint32_t *ptr
= (uint32_t *)mem_buf
;
529 #define LOAD(x) (x)=*ptr++;
530 for (i
= 0; i
< 16; i
++) LOAD(env
->gregs
[i
]);
540 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
545 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
551 static int gdb_handle_packet(GDBState
*s
, CPUState
*env
, const char *line_buf
)
554 int ch
, reg_size
, type
;
556 uint8_t mem_buf
[2000];
561 printf("command='%s'\n", line_buf
);
567 /* TODO: Make this return the correct value for user-mode. */
568 snprintf(buf
, sizeof(buf
), "S%02x", SIGTRAP
);
573 addr
= strtoul(p
, (char **)&p
, 16);
574 #if defined(TARGET_I386)
576 #elif defined (TARGET_PPC)
578 #elif defined (TARGET_SPARC)
581 #elif defined (TARGET_ARM)
582 env
->regs
[15] = addr
;
583 #elif defined (TARGET_SH4)
587 #ifdef CONFIG_USER_ONLY
588 s
->running_state
= 1;
595 addr
= strtoul(p
, (char **)&p
, 16);
596 #if defined(TARGET_I386)
598 #elif defined (TARGET_PPC)
600 #elif defined (TARGET_SPARC)
603 #elif defined (TARGET_ARM)
604 env
->regs
[15] = addr
;
605 #elif defined (TARGET_SH4)
609 cpu_single_step(env
, 1);
610 #ifdef CONFIG_USER_ONLY
611 s
->running_state
= 1;
617 reg_size
= cpu_gdb_read_registers(env
, mem_buf
);
618 memtohex(buf
, mem_buf
, reg_size
);
622 registers
= (void *)mem_buf
;
624 hextomem((uint8_t *)registers
, p
, len
);
625 cpu_gdb_write_registers(env
, mem_buf
, len
);
629 addr
= strtoul(p
, (char **)&p
, 16);
632 len
= strtoul(p
, NULL
, 16);
633 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 0) != 0) {
634 put_packet (s
, "E14");
636 memtohex(buf
, mem_buf
, len
);
641 addr
= strtoul(p
, (char **)&p
, 16);
644 len
= strtoul(p
, (char **)&p
, 16);
647 hextomem(mem_buf
, p
, len
);
648 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 1) != 0)
649 put_packet(s
, "E14");
654 type
= strtoul(p
, (char **)&p
, 16);
657 addr
= strtoul(p
, (char **)&p
, 16);
660 len
= strtoul(p
, (char **)&p
, 16);
661 if (type
== 0 || type
== 1) {
662 if (cpu_breakpoint_insert(env
, addr
) < 0)
663 goto breakpoint_error
;
667 put_packet(s
, "E22");
671 type
= strtoul(p
, (char **)&p
, 16);
674 addr
= strtoul(p
, (char **)&p
, 16);
677 len
= strtoul(p
, (char **)&p
, 16);
678 if (type
== 0 || type
== 1) {
679 cpu_breakpoint_remove(env
, addr
);
682 goto breakpoint_error
;
685 #ifdef CONFIG_USER_ONLY
687 if (strncmp(p
, "Offsets", 7) == 0) {
688 TaskState
*ts
= env
->opaque
;
690 sprintf(buf
, "Text=%x;Data=%x;Bss=%x", ts
->info
->code_offset
,
691 ts
->info
->data_offset
, ts
->info
->data_offset
);
699 /* put empty packet */
707 extern void tb_flush(CPUState
*env
);
709 #ifndef CONFIG_USER_ONLY
710 static void gdb_vm_stopped(void *opaque
, int reason
)
712 GDBState
*s
= opaque
;
716 /* disable single step if it was enable */
717 cpu_single_step(s
->env
, 0);
719 if (reason
== EXCP_DEBUG
) {
722 } else if (reason
== EXCP_INTERRUPT
) {
727 snprintf(buf
, sizeof(buf
), "S%02x", ret
);
732 static void gdb_read_byte(GDBState
*s
, int ch
)
734 CPUState
*env
= s
->env
;
738 #ifndef CONFIG_USER_ONLY
740 /* when the CPU is running, we cannot do anything except stop
741 it when receiving a char */
742 vm_stop(EXCP_INTERRUPT
);
749 s
->line_buf_index
= 0;
750 s
->state
= RS_GETLINE
;
755 s
->state
= RS_CHKSUM1
;
756 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
759 s
->line_buf
[s
->line_buf_index
++] = ch
;
763 s
->line_buf
[s
->line_buf_index
] = '\0';
764 s
->line_csum
= fromhex(ch
) << 4;
765 s
->state
= RS_CHKSUM2
;
768 s
->line_csum
|= fromhex(ch
);
770 for(i
= 0; i
< s
->line_buf_index
; i
++) {
771 csum
+= s
->line_buf
[i
];
773 if (s
->line_csum
!= (csum
& 0xff)) {
775 put_buffer(s
, reply
, 1);
779 put_buffer(s
, reply
, 1);
780 s
->state
= gdb_handle_packet(s
, env
, s
->line_buf
);
787 #ifdef CONFIG_USER_ONLY
789 gdb_handlesig (CPUState
*env
, int sig
)
795 if (gdbserver_fd
< 0)
798 s
= &gdbserver_state
;
800 /* disable single step if it was enabled */
801 cpu_single_step(env
, 0);
806 snprintf(buf
, sizeof(buf
), "S%02x", sig
);
812 s
->running_state
= 0;
813 while (s
->running_state
== 0) {
814 n
= read (s
->fd
, buf
, 256);
819 for (i
= 0; i
< n
; i
++)
820 gdb_read_byte (s
, buf
[i
]);
822 else if (n
== 0 || errno
!= EAGAIN
)
824 /* XXX: Connection closed. Should probably wait for annother
825 connection before continuing. */
832 /* Tell the remote gdb that the process has exited. */
833 void gdb_exit(CPUState
*env
, int code
)
838 if (gdbserver_fd
< 0)
841 s
= &gdbserver_state
;
843 snprintf(buf
, sizeof(buf
), "W%02x", code
);
848 static void gdb_read(void *opaque
)
850 GDBState
*s
= opaque
;
854 size
= recv(s
->fd
, buf
, sizeof(buf
), 0);
858 /* end of connection */
859 qemu_del_vm_stop_handler(gdb_vm_stopped
, s
);
860 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
864 for(i
= 0; i
< size
; i
++)
865 gdb_read_byte(s
, buf
[i
]);
871 static void gdb_accept(void *opaque
)
874 struct sockaddr_in sockaddr
;
879 len
= sizeof(sockaddr
);
880 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
881 if (fd
< 0 && errno
!= EINTR
) {
884 } else if (fd
>= 0) {
889 /* set short latency */
891 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, (char *)&val
, sizeof(val
));
893 #ifdef CONFIG_USER_ONLY
894 s
= &gdbserver_state
;
895 memset (s
, 0, sizeof (GDBState
));
897 s
= qemu_mallocz(sizeof(GDBState
));
903 s
->env
= first_cpu
; /* XXX: allow to change CPU */
906 #ifdef CONFIG_USER_ONLY
907 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
909 socket_set_nonblock(fd
);
912 vm_stop(EXCP_INTERRUPT
);
914 /* start handling I/O */
915 qemu_set_fd_handler(s
->fd
, gdb_read
, NULL
, s
);
916 /* when the VM is stopped, the following callback is called */
917 qemu_add_vm_stop_handler(gdb_vm_stopped
, s
);
921 static int gdbserver_open(int port
)
923 struct sockaddr_in sockaddr
;
926 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
932 /* allow fast reuse */
934 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, (char *)&val
, sizeof(val
));
936 sockaddr
.sin_family
= AF_INET
;
937 sockaddr
.sin_port
= htons(port
);
938 sockaddr
.sin_addr
.s_addr
= 0;
939 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
949 #ifndef CONFIG_USER_ONLY
950 socket_set_nonblock(fd
);
955 int gdbserver_start(int port
)
957 gdbserver_fd
= gdbserver_open(port
);
958 if (gdbserver_fd
< 0)
960 /* accept connections */
961 #ifdef CONFIG_USER_ONLY
964 qemu_set_fd_handler(gdbserver_fd
, gdb_accept
, NULL
, NULL
);