4 * Copyright (c) 2003 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
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <netinet/tcp.h>
36 static int gdbserver_fd
;
38 typedef struct GDBState
{
46 static int get_char(GDBState
*s
)
52 ret
= read(s
->fd
, &ch
, 1);
54 if (errno
!= EINTR
&& errno
!= EAGAIN
)
56 } else if (ret
== 0) {
65 static void put_buffer(GDBState
*s
, const uint8_t *buf
, int len
)
70 ret
= write(s
->fd
, buf
, len
);
72 if (errno
!= EINTR
&& errno
!= EAGAIN
)
81 static inline int fromhex(int v
)
83 if (v
>= '0' && v
<= '9')
85 else if (v
>= 'A' && v
<= 'F')
87 else if (v
>= 'a' && v
<= 'f')
93 static inline int tohex(int v
)
101 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
106 for(i
= 0; i
< len
; i
++) {
108 *q
++ = tohex(c
>> 4);
109 *q
++ = tohex(c
& 0xf);
114 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
118 for(i
= 0; i
< len
; i
++) {
119 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
124 /* return -1 if error, 0 if OK */
125 static int put_packet(GDBState
*s
, char *buf
)
128 int len
, csum
, ch
, i
;
131 printf("reply='%s'\n", buf
);
136 put_buffer(s
, buf1
, 1);
138 put_buffer(s
, buf
, len
);
140 for(i
= 0; i
< len
; i
++) {
144 buf1
[1] = tohex((csum
>> 4) & 0xf);
145 buf1
[2] = tohex((csum
) & 0xf);
147 put_buffer(s
, buf1
, 3);
158 #if defined(TARGET_I386)
160 static void to_le32(uint8_t *p
, int v
)
168 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
172 for(i
= 0; i
< 8; i
++) {
173 to_le32(mem_buf
+ i
* 4, env
->regs
[i
]);
175 to_le32(mem_buf
+ 8 * 4, env
->eip
);
176 to_le32(mem_buf
+ 9 * 4, env
->eflags
);
177 to_le32(mem_buf
+ 10 * 4, env
->segs
[R_CS
].selector
);
178 to_le32(mem_buf
+ 11 * 4, env
->segs
[R_SS
].selector
);
179 to_le32(mem_buf
+ 12 * 4, env
->segs
[R_DS
].selector
);
180 to_le32(mem_buf
+ 13 * 4, env
->segs
[R_ES
].selector
);
181 to_le32(mem_buf
+ 14 * 4, env
->segs
[R_FS
].selector
);
182 to_le32(mem_buf
+ 15 * 4, env
->segs
[R_GS
].selector
);
183 /* XXX: convert floats */
184 for(i
= 0; i
< 8; i
++) {
185 memcpy(mem_buf
+ 16 * 4 + i
* 10, &env
->fpregs
[i
], 10);
187 to_le32(mem_buf
+ 36 * 4, env
->fpuc
);
188 fpus
= (env
->fpus
& ~0x3800) | (env
->fpstt
& 0x7) << 11;
189 to_le32(mem_buf
+ 37 * 4, fpus
);
190 to_le32(mem_buf
+ 38 * 4, 0); /* XXX: convert tags */
191 to_le32(mem_buf
+ 39 * 4, 0); /* fiseg */
192 to_le32(mem_buf
+ 40 * 4, 0); /* fioff */
193 to_le32(mem_buf
+ 41 * 4, 0); /* foseg */
194 to_le32(mem_buf
+ 42 * 4, 0); /* fooff */
195 to_le32(mem_buf
+ 43 * 4, 0); /* fop */
199 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
201 uint32_t *registers
= (uint32_t *)mem_buf
;
204 for(i
= 0; i
< 8; i
++) {
205 env
->regs
[i
] = tswapl(registers
[i
]);
207 env
->eip
= registers
[8];
208 env
->eflags
= registers
[9];
209 #if defined(CONFIG_USER_ONLY)
210 #define LOAD_SEG(index, sreg)\
211 if (tswapl(registers[index]) != env->segs[sreg].selector)\
212 cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
222 #elif defined (TARGET_PPC)
223 static void to_le32(uint32_t *buf
, uint32_t v
)
225 uint8_t *p
= (uint8_t *)buf
;
232 static uint32_t from_le32 (uint32_t *buf
)
234 uint8_t *p
= (uint8_t *)buf
;
236 return p
[0] | (p
[1] << 8) | (p
[2] << 16) | (p
[3] << 24);
239 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
241 uint32_t *registers
= (uint32_t *)mem_buf
, tmp
;
245 for(i
= 0; i
< 32; i
++) {
246 to_le32(®isters
[i
], env
->gpr
[i
]);
249 for (i
= 0; i
< 32; i
++) {
250 to_le32(®isters
[(i
* 2) + 32], *((uint32_t *)&env
->fpr
[i
]));
251 to_le32(®isters
[(i
* 2) + 33], *((uint32_t *)&env
->fpr
[i
] + 1));
253 /* nip, msr, ccr, lnk, ctr, xer, mq */
254 to_le32(®isters
[96], (uint32_t)env
->nip
/* - 4*/);
255 to_le32(®isters
[97], _load_msr(env
));
257 for (i
= 0; i
< 8; i
++)
258 tmp
|= env
->crf
[i
] << (32 - ((i
+ 1) * 4));
259 to_le32(®isters
[98], tmp
);
260 to_le32(®isters
[99], env
->lr
);
261 to_le32(®isters
[100], env
->ctr
);
262 to_le32(®isters
[101], _load_xer(env
));
263 to_le32(®isters
[102], 0);
268 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
270 uint32_t *registers
= (uint32_t *)mem_buf
;
274 for (i
= 0; i
< 32; i
++) {
275 env
->gpr
[i
] = from_le32(®isters
[i
]);
278 for (i
= 0; i
< 32; i
++) {
279 *((uint32_t *)&env
->fpr
[i
]) = from_le32(®isters
[(i
* 2) + 32]);
280 *((uint32_t *)&env
->fpr
[i
] + 1) = from_le32(®isters
[(i
* 2) + 33]);
282 /* nip, msr, ccr, lnk, ctr, xer, mq */
283 env
->nip
= from_le32(®isters
[96]);
284 _store_msr(env
, from_le32(®isters
[97]));
285 registers
[98] = from_le32(®isters
[98]);
286 for (i
= 0; i
< 8; i
++)
287 env
->crf
[i
] = (registers
[98] >> (32 - ((i
+ 1) * 4))) & 0xF;
288 env
->lr
= from_le32(®isters
[99]);
289 env
->ctr
= from_le32(®isters
[100]);
290 _store_xer(env
, from_le32(®isters
[101]));
294 static int cpu_gdb_read_registers(CPUState
*env
, uint8_t *mem_buf
)
299 static void cpu_gdb_write_registers(CPUState
*env
, uint8_t *mem_buf
, int size
)
305 /* port = 0 means default port */
306 static int gdb_handle_packet(GDBState
*s
, const char *line_buf
)
308 CPUState
*env
= cpu_single_env
;
310 int ch
, reg_size
, type
;
312 uint8_t mem_buf
[2000];
317 printf("command='%s'\n", line_buf
);
323 snprintf(buf
, sizeof(buf
), "S%02x", SIGTRAP
);
328 addr
= strtoul(p
, (char **)&p
, 16);
329 #if defined(TARGET_I386)
331 #elif defined (TARGET_PPC)
339 addr
= strtoul(p
, (char **)&p
, 16);
340 #if defined(TARGET_I386)
342 #elif defined (TARGET_PPC)
346 cpu_single_step(env
, 1);
350 reg_size
= cpu_gdb_read_registers(env
, mem_buf
);
351 memtohex(buf
, mem_buf
, reg_size
);
355 registers
= (void *)mem_buf
;
357 hextomem((uint8_t *)registers
, p
, len
);
358 cpu_gdb_write_registers(env
, mem_buf
, len
);
362 addr
= strtoul(p
, (char **)&p
, 16);
365 len
= strtoul(p
, NULL
, 16);
366 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 0) != 0)
367 memset(mem_buf
, 0, len
);
368 memtohex(buf
, mem_buf
, len
);
372 addr
= strtoul(p
, (char **)&p
, 16);
375 len
= strtoul(p
, (char **)&p
, 16);
378 hextomem(mem_buf
, p
, len
);
379 if (cpu_memory_rw_debug(env
, addr
, mem_buf
, len
, 1) != 0)
380 put_packet(s
, "ENN");
385 type
= strtoul(p
, (char **)&p
, 16);
388 addr
= strtoul(p
, (char **)&p
, 16);
391 len
= strtoul(p
, (char **)&p
, 16);
392 if (type
== 0 || type
== 1) {
393 if (cpu_breakpoint_insert(env
, addr
) < 0)
394 goto breakpoint_error
;
398 put_packet(s
, "ENN");
402 type
= strtoul(p
, (char **)&p
, 16);
405 addr
= strtoul(p
, (char **)&p
, 16);
408 len
= strtoul(p
, (char **)&p
, 16);
409 if (type
== 0 || type
== 1) {
410 cpu_breakpoint_remove(env
, addr
);
413 goto breakpoint_error
;
418 /* put empty packet */
426 static void gdb_vm_stopped(void *opaque
, int reason
)
428 GDBState
*s
= opaque
;
432 /* disable single step if it was enable */
433 cpu_single_step(cpu_single_env
, 0);
435 if (reason
== EXCP_DEBUG
)
439 snprintf(buf
, sizeof(buf
), "S%02x", ret
);
443 static void gdb_read_byte(GDBState
*s
, int ch
)
449 /* when the CPU is running, we cannot do anything except stop
450 it when receiving a char */
451 vm_stop(EXCP_INTERRUPT
);
456 s
->line_buf_index
= 0;
457 s
->state
= RS_GETLINE
;
462 s
->state
= RS_CHKSUM1
;
463 } else if (s
->line_buf_index
>= sizeof(s
->line_buf
) - 1) {
466 s
->line_buf
[s
->line_buf_index
++] = ch
;
470 s
->line_buf
[s
->line_buf_index
] = '\0';
471 s
->line_csum
= fromhex(ch
) << 4;
472 s
->state
= RS_CHKSUM2
;
475 s
->line_csum
|= fromhex(ch
);
477 for(i
= 0; i
< s
->line_buf_index
; i
++) {
478 csum
+= s
->line_buf
[i
];
480 if (s
->line_csum
!= (csum
& 0xff)) {
482 put_buffer(s
, reply
, 1);
486 put_buffer(s
, reply
, 1);
487 s
->state
= gdb_handle_packet(s
, s
->line_buf
);
494 static int gdb_can_read(void *opaque
)
499 static void gdb_read(void *opaque
, const uint8_t *buf
, int size
)
501 GDBState
*s
= opaque
;
504 /* end of connection */
505 qemu_del_vm_stop_handler(gdb_vm_stopped
, s
);
506 qemu_del_fd_read_handler(s
->fd
);
510 for(i
= 0; i
< size
; i
++)
511 gdb_read_byte(s
, buf
[i
]);
515 static void gdb_accept(void *opaque
, const uint8_t *buf
, int size
)
518 struct sockaddr_in sockaddr
;
523 len
= sizeof(sockaddr
);
524 fd
= accept(gdbserver_fd
, (struct sockaddr
*)&sockaddr
, &len
);
525 if (fd
< 0 && errno
!= EINTR
) {
528 } else if (fd
>= 0) {
533 /* set short latency */
535 setsockopt(fd
, SOL_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
537 s
= qemu_mallocz(sizeof(GDBState
));
544 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
547 vm_stop(EXCP_INTERRUPT
);
549 /* start handling I/O */
550 qemu_add_fd_read_handler(s
->fd
, gdb_can_read
, gdb_read
, s
);
551 /* when the VM is stopped, the following callback is called */
552 qemu_add_vm_stop_handler(gdb_vm_stopped
, s
);
555 static int gdbserver_open(int port
)
557 struct sockaddr_in sockaddr
;
560 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
566 /* allow fast reuse */
568 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
570 sockaddr
.sin_family
= AF_INET
;
571 sockaddr
.sin_port
= htons(port
);
572 sockaddr
.sin_addr
.s_addr
= 0;
573 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
583 fcntl(fd
, F_SETFL
, O_NONBLOCK
);
587 int gdbserver_start(int port
)
589 gdbserver_fd
= gdbserver_open(port
);
590 if (gdbserver_fd
< 0)
592 /* accept connections */
593 qemu_add_fd_read_handler(gdbserver_fd
, NULL
, gdb_accept
, NULL
);