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
20 #ifdef CONFIG_USER_ONLY
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netinet/tcp.h>
46 /* XXX: This is not thread safe. Do we care? */
47 static int gdbserver_fd
= -1;
49 typedef struct GDBState
{
50 CPUState
*env
; /* current CPU */
51 enum RSState state
; /* parsing state */
56 #ifdef CONFIG_USER_ONLY
61 #ifdef CONFIG_USER_ONLY
62 /* XXX: remove this hack. */
63 static GDBState gdbserver_state
;
66 static int get_char(GDBState
*s
)
72 ret
= read(s
->fd
, &ch
, 1);
74 if (errno
!= EINTR
&& errno
!= EAGAIN
)
76 } else if (ret
== 0) {
85 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
90 ret
= write(s
->fd
, buf
, len
);
92 if (errno
!= EINTR
&& errno
!= EAGAIN
)
101 static inline int fromhex(int v
)
103 if (v
>= '0' && v
<= '9')
105 else if (v
>= 'A' && v
<= 'F')
107 else if (v
>= 'a' && v
<= 'f')
113 static inline int tohex(int v
)
121 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
126 for(i
= 0; i
< len
; i
++) {
128 *q
++ = tohex(c
>> 4);
129 *q
++ = tohex(c
& 0xf);
134 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
138 for(i
= 0; i
< len
; i
++) {
139 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
144 /* return -1 if error, 0 if OK */
145 static int put_packet(GDBState
*s
, char *buf
)
148 int len
, csum
, ch
, i
;
151 printf("reply='%s'\n", buf
);
156 put_buffer(s
, buf1
, 1);
158 put_buffer(s
, buf
, len
);
160 for(i
= 0; i
< len
; i
++) {
164 buf1
[1] = tohex((csum
>> 4) & 0xf);
165 buf1
[2] = tohex((csum
) & 0xf);
167 put_buffer(s
, buf1
, 3);
178 #if defined(TARGET_I386)
180 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
182 uint32_t *registers
= (uint32_t *)mem_buf
;
185 for(i
= 0; i
< 8; i
++) {
186 registers
[i
] = env
->regs
[i
];
188 registers
[8] = env
->eip
;
189 registers
[9] = env
->eflags
;
190 registers
[10] = env
->segs
[R_CS
].selector
;
191 registers
[11] = env
->segs
[R_SS
].selector
;
192 registers
[12] = env
->segs
[R_DS
].selector
;
193 registers
[13] = env
->segs
[R_ES
].selector
;
194 registers
[14] = env
->segs
[R_FS
].selector
;
195 registers
[15] = env
->segs
[R_GS
].selector
;
196 /* XXX: convert floats */
197 for(i
= 0; i
< 8; i
++) {
198 memcpy(mem_buf
+ 16 * 4 + i
* 10, &env
->fpregs
[i
], 10);
200 registers
[36] = env
->fpuc
;
201 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
202 registers
[37] = fpus
;
203 registers
[38] = 0; /* XXX: convert tags */
204 registers
[39] = 0; /* fiseg */
205 registers
[40] = 0; /* fioff */
206 registers
[41] = 0; /* foseg */
207 registers
[42] = 0; /* fooff */
208 registers
[43] = 0; /* fop */
210 for(i
= 0; i
< 16; i
++)
211 tswapls(®isters
[i
]);
212 for(i
= 36; i
< 44; i
++)
213 tswapls(®isters
[i
]);
217 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
219 uint32_t *registers
= (uint32_t *)mem_buf
;
222 for(i
= 0; i
< 8; i
++) {
223 env
->regs
[i
] = tswapl(registers
[i
]);
225 env
->eip
= tswapl(registers
[8]);
226 env
->eflags
= tswapl(registers
[9]);
227 #if defined(CONFIG_USER_ONLY)
228 #define LOAD_SEG(index, sreg)\
229 if (tswapl(registers[index]) != env->segs[sreg].selector)\
230 cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
240 #elif defined (TARGET_PPC)
241 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
243 uint32_t *registers
= (uint32_t *)mem_buf
, tmp
;
247 for(i
= 0; i
< 32; i
++) {
248 registers
[i
] = tswapl(env
->gpr
[i
]);
251 for (i
= 0; i
< 32; i
++) {
252 registers
[(i
* 2) + 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
253 registers
[(i
* 2) + 33] = tswapl(*((uint32_t *)&env
->fpr
[i
] + 1));
255 /* nip, msr, ccr, lnk, ctr, xer, mq */
256 registers
[96] = tswapl(env
->nip
);
257 registers
[97] = tswapl(do_load_msr(env
));
259 for (i
= 0; i
< 8; i
++)
260 tmp
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
261 registers
[98] = tswapl(tmp
);
262 registers
[99] = tswapl(env
->lr
);
263 registers
[100] = tswapl(env
->ctr
);
264 registers
[101] = tswapl(do_load_xer(env
));
270 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
272 uint32_t *registers
= (uint32_t *)mem_buf
;
276 for (i
= 0; i
< 32; i
++) {
277 env
->gpr
[i
] = tswapl(registers
[i
]);
280 for (i
= 0; i
< 32; i
++) {
281 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[(i
* 2) + 32]);
282 *((uint32_t *)&env
->fpr
[i
] + 1) = tswapl(registers
[(i
* 2) + 33]);
284 /* nip, msr, ccr, lnk, ctr, xer, mq */
285 env
->nip
= tswapl(registers
[96]);
286 do_store_msr(env
, tswapl(registers
[97]));
287 registers
[98] = tswapl(registers
[98]);
288 for (i
= 0; i
< 8; i
++)
289 env
->crf
[i
] = (registers
[98] >> (32 - ((i
+ 1) * 4))) & 0xF;
290 env
->lr
= tswapl(registers
[99]);
291 env
->ctr
= tswapl(registers
[100]);
292 do_store_xer(env
, tswapl(registers
[101]));
294 #elif defined (TARGET_SPARC)
295 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
297 target_ulong
*registers
= (target_ulong
*)mem_buf
;
301 for(i
= 0; i
< 8; i
++) {
302 registers
[i
] = tswapl(env
->gregs
[i
]);
304 /* fill in register window */
305 for(i
= 0; i
< 24; i
++) {
306 registers
[i
+ 8] = tswapl(env
->regwptr
[i
]);
309 for (i
= 0; i
< 32; i
++) {
310 registers
[i
+ 32] = tswapl(*((uint32_t *)&env
->fpr
[i
]));
312 #ifndef TARGET_SPARC64
313 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
314 registers
[64] = tswapl(env
->y
);
319 registers
[65] = tswapl(tmp
);
321 registers
[66] = tswapl(env
->wim
);
322 registers
[67] = tswapl(env
->tbr
);
323 registers
[68] = tswapl(env
->pc
);
324 registers
[69] = tswapl(env
->npc
);
325 registers
[70] = tswapl(env
->fsr
);
326 registers
[71] = 0; /* csr */
328 return 73 * sizeof(target_ulong
);
330 for (i
= 0; i
< 32; i
+= 2) {
331 registers
[i
/2 + 64] = tswapl(*((uint64_t *)&env
->fpr
[i
]));
333 registers
[81] = tswapl(env
->pc
);
334 registers
[82] = tswapl(env
->npc
);
335 registers
[83] = tswapl(env
->tstate
[env
->tl
]);
336 registers
[84] = tswapl(env
->fsr
);
337 registers
[85] = tswapl(env
->fprs
);
338 registers
[86] = tswapl(env
->y
);
339 return 87 * sizeof(target_ulong
);
343 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
345 target_ulong
*registers
= (target_ulong
*)mem_buf
;
349 for(i
= 0; i
< 7; i
++) {
350 env
->gregs
[i
] = tswapl(registers
[i
]);
352 /* fill in register window */
353 for(i
= 0; i
< 24; i
++) {
354 env
->regwptr
[i
] = tswapl(registers
[i
+ 8]);
357 for (i
= 0; i
< 32; i
++) {
358 *((uint32_t *)&env
->fpr
[i
]) = tswapl(registers
[i
+ 32]);
360 #ifndef TARGET_SPARC64
361 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
362 env
->y
= tswapl(registers
[64]);
363 PUT_PSR(env
, tswapl(registers
[65]));
364 env
->wim
= tswapl(registers
[66]);
365 env
->tbr
= tswapl(registers
[67]);
366 env
->pc
= tswapl(registers
[68]);
367 env
->npc
= tswapl(registers
[69]);
368 env
->fsr
= tswapl(registers
[70]);
370 for (i
= 0; i
< 32; i
+= 2) {
372 tmp
= tswapl(registers
[i
/2 + 64]) << 32;
373 tmp
|= tswapl(registers
[i
/2 + 64 + 1]);
374 *((uint64_t *)&env
->fpr
[i
]) = tmp
;
376 env
->pc
= tswapl(registers
[81]);
377 env
->npc
= tswapl(registers
[82]);
378 env
->tstate
[env
->tl
] = tswapl(registers
[83]);
379 env
->fsr
= tswapl(registers
[84]);
380 env
->fprs
= tswapl(registers
[85]);
381 env
->y
= tswapl(registers
[86]);
384 #elif defined (TARGET_ARM)
385 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
391 /* 16 core integer registers (4 bytes each). */
392 for (i
= 0; i
< 16; i
++)
394 *(uint32_t *)ptr
= tswapl(env
->regs
[i
]);
397 /* 8 FPA registers (12 bytes each), FPS (4 bytes).
398 Not yet implemented. */
399 memset (ptr
, 0, 8 * 12 + 4);
401 /* CPSR (4 bytes). */
402 *(uint32_t *)ptr
= tswapl (cpsr_read(env
));
405 return ptr
- mem_buf
;
408 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
414 /* Core integer registers. */
415 for (i
= 0; i
< 16; i
++)
417 env
->regs
[i
] = tswapl(*(uint32_t *)ptr
);
420 /* Ignore FPA regs and scr. */
422 cpsr_write (env
, tswapl(*(uint32_t *)ptr
), 0xffffffff);
424 #elif defined (TARGET_MIPS)
425 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
431 for (i
= 0; i
< 32; i
++)
433 *(uint32_t *)ptr
= tswapl(env
->gpr
[i
]);
437 *(uint32_t *)ptr
= tswapl(env
->CP0_Status
);
440 *(uint32_t *)ptr
= tswapl(env
->LO
);
443 *(uint32_t *)ptr
= tswapl(env
->HI
);
446 *(uint32_t *)ptr
= tswapl(env
->CP0_BadVAddr
);
449 *(uint32_t *)ptr
= tswapl(env
->CP0_Cause
);
452 *(uint32_t *)ptr
= tswapl(env
->PC
);
455 /* 32 FP registers, fsr, fir, fp. Not yet implemented. */
457 return ptr
- mem_buf
;
460 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
466 for (i
= 0; i
< 32; i
++)
468 env
->gpr
[i
] = tswapl(*(uint32_t *)ptr
);
472 env
->CP0_Status
= tswapl(*(uint32_t *)ptr
);
475 env
->LO
= tswapl(*(uint32_t *)ptr
);
478 env
->HI
= tswapl(*(uint32_t *)ptr
);
481 env
->CP0_BadVAddr
= tswapl(*(uint32_t *)ptr
);
484 env
->CP0_Cause
= tswapl(*(uint32_t *)ptr
);
487 env
->PC
= tswapl(*(uint32_t *)ptr
);
491 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
496 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
502 static int gdb_handle_packet(GDBState
*s
, CPUState
*env
, const char *line_buf
)
505 int ch
, reg_size
, type
;
507 uint8_t mem_buf
[2000];
512 printf("command='%s'\n", line_buf
);
518 /* TODO: Make this return the correct value for user-mode. */
519 snprintf(buf
, sizeof(buf
), "S%02x", SIGTRAP
);
524 addr
= strtoul(p
, (char **)&p
, 16);
525 #if defined(TARGET_I386)
527 #elif defined (TARGET_PPC)
529 #elif defined (TARGET_SPARC)
532 #elif defined (TARGET_ARM)
533 env
->regs
[15] = addr
;
536 #ifdef CONFIG_USER_ONLY
537 s
->running_state
= 1;
544 addr
= strtoul(p
, (char **)&p
, 16);
545 #if defined(TARGET_I386)
547 #elif defined (TARGET_PPC)
549 #elif defined (TARGET_SPARC)
552 #elif defined (TARGET_ARM)
553 env
->regs
[15] = addr
;
556 cpu_single_step(env
, 1);
557 #ifdef CONFIG_USER_ONLY
558 s
->running_state
= 1;
564 reg_size
= cpu_gdb_read_registers(env
, mem_buf
);
565 memtohex(buf
, mem_buf
, reg_size
);
569 registers
= (void *)mem_buf
;
571 hextomem((uint8_t *)registers
, p
, len
);
572 cpu_gdb_write_registers(env
, mem_buf
, len
);
576 addr
= strtoul(p
, (char **)&p
, 16);
579 len
= strtoul(p
, NULL
, 16);
580 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 0) != 0) {
581 put_packet (s
, "E14");
583 memtohex(buf
, mem_buf
, len
);
588 addr
= strtoul(p
, (char **)&p
, 16);
591 len
= strtoul(p
, (char **)&p
, 16);
594 hextomem(mem_buf
, p
, len
);
595 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 1) != 0)
596 put_packet(s
, "E14");
601 type
= strtoul(p
, (char **)&p
, 16);
604 addr
= strtoul(p
, (char **)&p
, 16);
607 len
= strtoul(p
, (char **)&p
, 16);
608 if (type
== 0 || type
== 1) {
609 if (cpu_breakpoint_insert(env
, addr
) < 0)
610 goto breakpoint_error
;
614 put_packet(s
, "E22");
618 type
= strtoul(p
, (char **)&p
, 16);
621 addr
= strtoul(p
, (char **)&p
, 16);
624 len
= strtoul(p
, (char **)&p
, 16);
625 if (type
== 0 || type
== 1) {
626 cpu_breakpoint_remove(env
, addr
);
629 goto breakpoint_error
;
634 /* put empty packet */
642 extern void tb_flush(CPUState
*env
);
644 #ifndef CONFIG_USER_ONLY
645 static void gdb_vm_stopped(void *opaque
, int reason
)
647 GDBState
*s
= opaque
;
651 /* disable single step if it was enable */
652 cpu_single_step(s
->env
, 0);
654 if (reason
== EXCP_DEBUG
) {
660 snprintf(buf
, sizeof(buf
), "S%02x", ret
);
665 static void gdb_read_byte(GDBState
*s
, int ch
)
667 CPUState
*env
= s
->env
;
671 #ifndef CONFIG_USER_ONLY
673 /* when the CPU is running, we cannot do anything except stop
674 it when receiving a char */
675 vm_stop(EXCP_INTERRUPT
);
682 s
->line_buf_index
= 0;
683 s
->state
= RS_GETLINE
;
688 s
->state
= RS_CHKSUM1
;
689 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
692 s
->line_buf
[s
->line_buf_index
++] = ch
;
696 s
->line_buf
[s
->line_buf_index
] = '\0';
697 s
->line_csum
= fromhex(ch
) << 4;
698 s
->state
= RS_CHKSUM2
;
701 s
->line_csum
|= fromhex(ch
);
703 for(i
= 0; i
< s
->line_buf_index
; i
++) {
704 csum
+= s
->line_buf
[i
];
706 if (s
->line_csum
!= (csum
& 0xff)) {
708 put_buffer(s
, reply
, 1);
712 put_buffer(s
, reply
, 1);
713 s
->state
= gdb_handle_packet(s
, env
, s
->line_buf
);
720 #ifdef CONFIG_USER_ONLY
722 gdb_handlesig (CPUState
*env
, int sig
)
728 if (gdbserver_fd
< 0)
731 s
= &gdbserver_state
;
733 /* disable single step if it was enabled */
734 cpu_single_step(env
, 0);
739 snprintf(buf
, sizeof(buf
), "S%02x", sig
);
745 s
->running_state
= 0;
746 while (s
->running_state
== 0) {
747 n
= read (s
->fd
, buf
, 256);
752 for (i
= 0; i
< n
; i
++)
753 gdb_read_byte (s
, buf
[i
]);
755 else if (n
== 0 || errno
!= EAGAIN
)
757 /* XXX: Connection closed. Should probably wait for annother
758 connection before continuing. */
765 /* Tell the remote gdb that the process has exited. */
766 void gdb_exit(CPUState
*env
, int code
)
771 if (gdbserver_fd
< 0)
774 s
= &gdbserver_state
;
776 snprintf(buf
, sizeof(buf
), "W%02x", code
);
781 static void gdb_read(void *opaque
)
783 GDBState
*s
= opaque
;
787 size
= read(s
->fd
, buf
, sizeof(buf
));
791 /* end of connection */
792 qemu_del_vm_stop_handler(gdb_vm_stopped
, s
);
793 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
797 for(i
= 0; i
< size
; i
++)
798 gdb_read_byte(s
, buf
[i
]);
804 static void gdb_accept(void *opaque
)
807 struct sockaddr_in sockaddr
;
812 len
= sizeof(sockaddr
);
813 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
814 if (fd
< 0 && errno
!= EINTR
) {
817 } else if (fd
>= 0) {
822 /* set short latency */
824 setsockopt(fd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
826 #ifdef CONFIG_USER_ONLY
827 s
= &gdbserver_state
;
828 memset (s
, 0, sizeof (GDBState
));
830 s
= qemu_mallocz(sizeof(GDBState
));
836 s
->env
= first_cpu
; /* XXX: allow to change CPU */
839 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
841 #ifndef CONFIG_USER_ONLY
843 vm_stop(EXCP_INTERRUPT
);
845 /* start handling I/O */
846 qemu_set_fd_handler(s
->fd
, gdb_read
, NULL
, s
);
847 /* when the VM is stopped, the following callback is called */
848 qemu_add_vm_stop_handler(gdb_vm_stopped
, s
);
852 static int gdbserver_open(int port
)
854 struct sockaddr_in sockaddr
;
857 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
863 /* allow fast reuse */
865 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
867 sockaddr
.sin_family
= AF_INET
;
868 sockaddr
.sin_port
= htons(port
);
869 sockaddr
.sin_addr
.s_addr
= 0;
870 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
880 #ifndef CONFIG_USER_ONLY
881 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
886 int gdbserver_start(int port
)
888 gdbserver_fd
= gdbserver_open(port
);
889 if (gdbserver_fd
< 0)
891 /* accept connections */
892 #ifdef CONFIG_USER_ONLY
895 qemu_set_fd_handler(gdbserver_fd
, gdb_accept
, NULL
, NULL
);