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
25 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <netinet/tcp.h>
40 static int gdbstub_open(int port
)
42 struct sockaddr_in sockaddr
;
46 fd
= socket(PF_INET
, SOCK_STREAM
, 0);
52 /* allow fast reuse */
54 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
56 sockaddr
.sin_family
= AF_INET
;
57 sockaddr
.sin_port
= htons(port
);
58 sockaddr
.sin_addr
.s_addr
= 0;
59 ret
= bind(fd
, (struct sockaddr
*)&sockaddr
, sizeof(sockaddr
));
70 /* now wait for one connection */
72 len
= sizeof(sockaddr
);
73 gdbstub_fd
= accept(fd
, (struct sockaddr
*)&sockaddr
, &len
);
74 if (gdbstub_fd
< 0 && errno
!= EINTR
) {
77 } else if (gdbstub_fd
>= 0) {
82 /* set short latency */
84 setsockopt(gdbstub_fd
, SOL_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
88 static int get_char(void)
94 ret
= read(gdbstub_fd
, &ch
, 1);
96 if (errno
!= EINTR
&& errno
!= EAGAIN
)
98 } else if (ret
== 0) {
107 static void put_buffer(const uint8_t *buf
, int len
)
112 ret
= write(gdbstub_fd
, buf
, len
);
114 if (errno
!= EINTR
&& errno
!= EAGAIN
)
123 static inline int fromhex(int v
)
125 if (v
>= '0' && v
<= '9')
127 else if (v
>= 'A' && v
<= 'F')
129 else if (v
>= 'a' && v
<= 'f')
135 static inline int tohex(int v
)
143 static void memtohex(char *buf
, const uint8_t *mem
, int len
)
148 for(i
= 0; i
< len
; i
++) {
150 *q
++ = tohex(c
>> 4);
151 *q
++ = tohex(c
& 0xf);
156 static void hextomem(uint8_t *mem
, const char *buf
, int len
)
160 for(i
= 0; i
< len
; i
++) {
161 mem
[i
] = (fromhex(buf
[0]) << 4) | fromhex(buf
[1]);
166 /* return -1 if error or EOF */
167 static int get_packet(char *buf
, int buf_size
)
169 int ch
, len
, csum
, csum1
;
188 if (len
> buf_size
- 1)
197 csum1
= fromhex(ch
) << 4;
201 csum1
|= fromhex(ch
);
202 if ((csum
& 0xff) != csum1
) {
204 put_buffer(reply
, 1);
207 put_buffer(reply
, 1);
212 printf("command='%s'\n", buf
);
217 /* return -1 if error, 0 if OK */
218 static int put_packet(char *buf
)
221 int len
, csum
, ch
, i
;
224 printf("reply='%s'\n", buf
);
231 put_buffer(buf
, len
);
233 for(i
= 0; i
< len
; i
++) {
237 buf1
[1] = tohex((csum
>> 4) & 0xf);
238 buf1
[2] = tohex((csum
) & 0xf);
251 static int memory_rw(uint8_t *buf
, uint32_t addr
, int len
, int is_write
)
257 page
= addr
& TARGET_PAGE_MASK
;
258 l
= (page
+ TARGET_PAGE_SIZE
) - addr
;
261 flags
= page_get_flags(page
);
262 if (!(flags
& PAGE_VALID
))
265 if (!(flags
& PAGE_WRITE
))
267 memcpy((uint8_t *)addr
, buf
, l
);
269 if (!(flags
& PAGE_READ
))
271 memcpy(buf
, (uint8_t *)addr
, l
);
280 /* port = 0 means default port */
281 int cpu_gdbstub(void *opaque
, int (*main_loop
)(void *opaque
), int port
)
285 int ret
, ch
, nb_regs
, i
, type
;
287 uint8_t mem_buf
[2000];
291 printf("Waiting gdb connection on port %d\n", port
);
292 if (gdbstub_open(port
) < 0)
294 printf("Connected\n");
296 ret
= get_packet(buf
, sizeof(buf
));
303 snprintf(buf
, sizeof(buf
), "S%02x", SIGTRAP
);
308 addr
= strtoul(p
, (char **)&p
, 16);
309 env
= cpu_gdbstub_get_env(opaque
);
310 #if defined(TARGET_I386)
314 ret
= main_loop(opaque
);
315 if (ret
== EXCP_DEBUG
)
319 snprintf(buf
, sizeof(buf
), "S%02x", ret
);
323 env
= cpu_gdbstub_get_env(opaque
);
325 addr
= strtoul(p
, (char **)&p
, 16);
326 #if defined(TARGET_I386)
330 cpu_single_step(env
, 1);
331 ret
= main_loop(opaque
);
332 cpu_single_step(env
, 0);
333 if (ret
== EXCP_DEBUG
)
337 snprintf(buf
, sizeof(buf
), "S%02x", ret
);
341 env
= cpu_gdbstub_get_env(opaque
);
342 registers
= (void *)mem_buf
;
343 #if defined(TARGET_I386)
344 for(i
= 0; i
< 8; i
++) {
345 registers
[i
] = tswapl(env
->regs
[i
]);
347 registers
[8] = env
->eip
;
348 registers
[9] = env
->eflags
;
349 registers
[10] = env
->segs
[R_CS
].selector
;
350 registers
[11] = env
->segs
[R_SS
].selector
;
351 registers
[12] = env
->segs
[R_DS
].selector
;
352 registers
[13] = env
->segs
[R_ES
].selector
;
353 registers
[14] = env
->segs
[R_FS
].selector
;
354 registers
[15] = env
->segs
[R_GS
].selector
;
357 memtohex(buf
, (const uint8_t *)registers
,
358 sizeof(registers
[0]) * nb_regs
);
362 env
= cpu_gdbstub_get_env(opaque
);
363 registers
= (void *)mem_buf
;
364 #if defined(TARGET_I386)
365 hextomem((uint8_t *)registers
, p
, 16 * 4);
366 for(i
= 0; i
< 8; i
++) {
367 env
->regs
[i
] = tswapl(registers
[i
]);
369 env
->eip
= registers
[8];
370 env
->eflags
= 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]));
386 addr
= strtoul(p
, (char **)&p
, 16);
389 len
= strtoul(p
, NULL
, 16);
390 if (memory_rw(mem_buf
, addr
, len
, 0) != 0)
391 memset(mem_buf
, 0, len
);
392 memtohex(buf
, mem_buf
, len
);
396 addr
= strtoul(p
, (char **)&p
, 16);
399 len
= strtoul(p
, (char **)&p
, 16);
402 hextomem(mem_buf
, p
, len
);
403 if (memory_rw(mem_buf
, addr
, len
, 1) != 0)
409 type
= strtoul(p
, (char **)&p
, 16);
412 addr
= strtoul(p
, (char **)&p
, 16);
415 len
= strtoul(p
, (char **)&p
, 16);
416 if (type
== 0 || type
== 1) {
417 env
= cpu_gdbstub_get_env(opaque
);
418 if (cpu_breakpoint_insert(env
, addr
) < 0)
419 goto breakpoint_error
;
427 type
= strtoul(p
, (char **)&p
, 16);
430 addr
= strtoul(p
, (char **)&p
, 16);
433 len
= strtoul(p
, (char **)&p
, 16);
434 if (type
== 0 || type
== 1) {
435 env
= cpu_gdbstub_get_env(opaque
);
436 cpu_breakpoint_remove(env
, addr
);
439 goto breakpoint_error
;
443 if (!strncmp(p
, "Tinit", 5)) {
446 } else if (!strncmp(p
, "TStart", 6)) {
447 /* start log (gdb 'tstart' command) */
448 cpu_set_log(CPU_LOG_ALL
);
450 } else if (!strncmp(p
, "TStop", 5)) {
451 /* stop log (gdb 'tstop' command) */
455 goto unknown_command
;
460 /* put empty packet */