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 /* better than nothing for SOFTMMU : we use physical addresses */
252 #if !defined(CONFIG_USER_ONLY)
253 static int memory_rw(uint8_t *buf, uint32_t addr, int len, int is_write)
257 if (addr >= phys_ram_size ||
258 ((int64_t)addr + len > phys_ram_size))
260 ptr = phys_ram_base + addr;
262 memcpy(ptr, buf, len);
264 memcpy(buf, ptr, len);
268 static int memory_rw(uint8_t *buf, uint32_t addr, int len, int is_write)
274 page = addr & TARGET_PAGE_MASK;
275 l = (page + TARGET_PAGE_SIZE) - addr;
278 flags = page_get_flags(page);
279 if (!(flags & PAGE_VALID))
282 if (!(flags & PAGE_WRITE))
284 memcpy((uint8_t *)addr, buf, l);
286 if (!(flags & PAGE_READ))
288 memcpy(buf, (uint8_t *)addr, l);
298 #if defined(TARGET_I386)
300 static void to_le32(uint8_t *p, int v)
308 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
312 for(i = 0; i < 8; i++) {
313 to_le32(mem_buf + i * 4, env->regs[i]);
315 to_le32(mem_buf + 8 * 4, env->eip);
316 to_le32(mem_buf + 9 * 4, env->eflags);
317 to_le32(mem_buf + 10 * 4, env->segs[R_CS].selector);
318 to_le32(mem_buf + 11 * 4, env->segs[R_SS].selector);
319 to_le32(mem_buf + 12 * 4, env->segs[R_DS].selector);
320 to_le32(mem_buf + 13 * 4, env->segs[R_ES].selector);
321 to_le32(mem_buf + 14 * 4, env->segs[R_FS].selector);
322 to_le32(mem_buf + 15 * 4, env->segs[R_GS].selector);
323 /* XXX: convert floats */
324 for(i = 0; i < 8; i++) {
325 memcpy(mem_buf + 16 * 4 + i * 10, &env->fpregs[i], 10);
327 to_le32(mem_buf + 36 * 4, env->fpuc);
328 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
329 to_le32(mem_buf + 37 * 4, fpus);
330 to_le32(mem_buf + 38 * 4, 0); /* XXX: convert tags */
331 to_le32(mem_buf + 39 * 4, 0); /* fiseg */
332 to_le32(mem_buf + 40 * 4, 0); /* fioff */
333 to_le32(mem_buf + 41 * 4, 0); /* foseg */
334 to_le32(mem_buf + 42 * 4, 0); /* fooff */
335 to_le32(mem_buf + 43 * 4, 0); /* fop */
339 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
341 uint32_t *registers = (uint32_t *)mem_buf;
344 for(i = 0; i < 8; i++) {
345 env->regs[i] = tswapl(registers[i]);
347 env->eip = registers[8];
348 env->eflags = registers[9];
349 #if defined(CONFIG_USER_ONLY)
350 #define LOAD_SEG(index, sreg)\
351 if (tswapl(registers[index]) != env->segs[sreg].selector)\
352 cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
364 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
369 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
375 /* port = 0 means default port */
376 int cpu_gdbstub(void *opaque, int (*main_loop)(void *opaque), int port)
380 int ret, ch, reg_size, type;
382 uint8_t mem_buf[2000];
386 printf("Waiting gdb connection on port %d\n", port);
387 if (gdbstub_open(port) < 0)
389 printf("Connected\n");
391 ret = get_packet(buf, sizeof(buf));
398 snprintf(buf, sizeof(buf), "S%02x", SIGTRAP);
403 addr = strtoul(p, (char **)&p, 16);
404 env = cpu_gdbstub_get_env(opaque);
405 #if defined(TARGET_I386)
407 #elif defined (TARGET_PPC)
411 ret = main_loop(opaque);
412 if (ret == EXCP_DEBUG)
416 snprintf(buf, sizeof(buf), "S%02x", ret);
420 env = cpu_gdbstub_get_env(opaque);
422 addr = strtoul(p, (char **)&p, 16);
423 #if defined(TARGET_I386)
425 #elif defined (TARGET_PPC)
429 cpu_single_step(env, 1);
430 ret = main_loop(opaque);
431 cpu_single_step(env, 0);
432 if (ret == EXCP_DEBUG)
436 snprintf(buf, sizeof(buf), "S%02x", ret);
440 env = cpu_gdbstub_get_env(opaque);
441 reg_size = cpu_gdb_read_registers(env, mem_buf);
442 memtohex(buf, mem_buf, reg_size);
446 env = cpu_gdbstub_get_env(opaque);
447 registers = (void *)mem_buf;
449 hextomem((uint8_t *)registers, p, len);
450 cpu_gdb_write_registers(env, mem_buf, len);
454 addr = strtoul(p, (char **)&p, 16);
457 len = strtoul(p, NULL, 16);
458 if (memory_rw(mem_buf, addr, len, 0) != 0)
459 memset(mem_buf, 0, len);
460 memtohex(buf, mem_buf, len);
464 addr = strtoul(p, (char **)&p, 16);
467 len = strtoul(p, (char **)&p, 16);
470 hextomem(mem_buf, p, len);
471 if (memory_rw(mem_buf, addr, len, 1) != 0)
477 type = strtoul(p, (char **)&p, 16);
480 addr = strtoul(p, (char **)&p, 16);
483 len = strtoul(p, (char **)&p, 16);
484 if (type == 0 || type == 1) {
485 env = cpu_gdbstub_get_env(opaque);
486 if (cpu_breakpoint_insert(env, addr) < 0)
487 goto breakpoint_error;
495 type = strtoul(p, (char **)&p, 16);
498 addr = strtoul(p, (char **)&p, 16);
501 len = strtoul(p, (char **)&p, 16);
502 if (type == 0 || type == 1) {
503 env = cpu_gdbstub_get_env(opaque);
504 cpu_breakpoint_remove(env, addr);
507 goto breakpoint_error;
511 if (!strncmp(p, "Tinit", 5)) {
514 } else if (!strncmp(p, "TStart", 6)) {
515 /* start log (gdb 'tstart' command) */
516 env = cpu_gdbstub_get_env(opaque);
518 cpu_set_log(CPU_LOG_ALL);
520 } else if (!strncmp(p, "TStop", 5)) {
521 /* stop log (gdb 'tstop' command) */
525 goto unknown_command;
530 /* put empty packet */