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 #if defined(TARGET_I386)
253 static void to_le32(uint8_t *p, int v)
261 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
265 for(i = 0; i < 8; i++) {
266 to_le32(mem_buf + i * 4, env->regs[i]);
268 to_le32(mem_buf + 8 * 4, env->eip);
269 to_le32(mem_buf + 9 * 4, env->eflags);
270 to_le32(mem_buf + 10 * 4, env->segs[R_CS].selector);
271 to_le32(mem_buf + 11 * 4, env->segs[R_SS].selector);
272 to_le32(mem_buf + 12 * 4, env->segs[R_DS].selector);
273 to_le32(mem_buf + 13 * 4, env->segs[R_ES].selector);
274 to_le32(mem_buf + 14 * 4, env->segs[R_FS].selector);
275 to_le32(mem_buf + 15 * 4, env->segs[R_GS].selector);
276 /* XXX: convert floats */
277 for(i = 0; i < 8; i++) {
278 memcpy(mem_buf + 16 * 4 + i * 10, &env->fpregs[i], 10);
280 to_le32(mem_buf + 36 * 4, env->fpuc);
281 fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
282 to_le32(mem_buf + 37 * 4, fpus);
283 to_le32(mem_buf + 38 * 4, 0); /* XXX: convert tags */
284 to_le32(mem_buf + 39 * 4, 0); /* fiseg */
285 to_le32(mem_buf + 40 * 4, 0); /* fioff */
286 to_le32(mem_buf + 41 * 4, 0); /* foseg */
287 to_le32(mem_buf + 42 * 4, 0); /* fooff */
288 to_le32(mem_buf + 43 * 4, 0); /* fop */
292 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
294 uint32_t *registers = (uint32_t *)mem_buf;
297 for(i = 0; i < 8; i++) {
298 env->regs[i] = tswapl(registers[i]);
300 env->eip = registers[8];
301 env->eflags = registers[9];
302 #if defined(CONFIG_USER_ONLY)
303 #define LOAD_SEG(index, sreg)\
304 if (tswapl(registers[index]) != env->segs[sreg].selector)\
305 cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
315 #elif defined (TARGET_PPC)
316 static void to_le32(uint8_t *p, int v)
324 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
330 for(i = 0; i < 8; i++) {
331 to_le32(mem_buf + i * 4, env->gpr[i]);
334 for (i = 0; i < 32; i++) {
335 to_le32(mem_buf + (i * 2) + 32, *((uint32_t *)&env->fpr[i]));
336 to_le32(mem_buf + (i * 2) + 33, *((uint32_t *)&env->fpr[i] + 1));
338 /* nip, msr, ccr, lnk, ctr, xer, mq */
339 to_le32(mem_buf + 96, tswapl(env->nip));
340 to_le32(mem_buf + 97, tswapl(_load_msr()));
341 to_le32(mem_buf + 98, 0);
343 for (i = 0; i < 8; i++)
344 tmp |= env->crf[i] << (32 - (i * 4));
345 to_le32(mem_buf + 98, tmp);
346 to_le32(mem_buf + 99, tswapl(env->lr));
347 to_le32(mem_buf + 100, tswapl(env->ctr));
348 to_le32(mem_buf + 101, tswapl(_load_xer()));
349 to_le32(mem_buf + 102, 0);
354 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
356 uint32_t *registers = (uint32_t *)mem_buf;
360 for (i = 0; i < 32; i++) {
361 env->gpr[i] = tswapl(registers[i]);
364 for (i = 0; i < 32; i++) {
365 *((uint32_t *)&env->fpr[i]) = tswapl(registers[(i * 2) + 32]);
366 *((uint32_t *)&env->fpr[i] + 1) = tswapl(registers[(i * 2) + 33]);
368 /* nip, msr, ccr, lnk, ctr, xer, mq */
369 env->nip = tswapl(registers[96]);
370 _store_msr(tswapl(registers[97]));
371 registers[98] = tswapl(registers[98]);
372 for (i = 0; i < 8; i++)
373 env->crf[i] = (registers[98] >> (32 - (i * 4))) & 0xF;
374 env->lr = tswapl(registers[99]);
375 env->ctr = tswapl(registers[100]);
376 _store_xer(tswapl(registers[101]));
380 static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
385 static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
391 /* port = 0 means default port */
392 int cpu_gdbstub(void *opaque, int (*main_loop)(void *opaque), int port)
396 int ret, ch, reg_size, type;
398 uint8_t mem_buf[2000];
402 printf("Waiting gdb connection on port %d\n", port);
403 if (gdbstub_open(port) < 0)
405 printf("Connected\n");
407 ret = get_packet(buf, sizeof(buf));
414 snprintf(buf, sizeof(buf), "S%02x", SIGTRAP);
419 addr = strtoul(p, (char **)&p, 16);
420 env = cpu_gdbstub_get_env(opaque);
421 #if defined(TARGET_I386)
423 #elif defined (TARGET_PPC)
427 ret = main_loop(opaque);
428 if (ret == EXCP_DEBUG)
432 snprintf(buf, sizeof(buf), "S%02x", ret);
436 env = cpu_gdbstub_get_env(opaque);
438 addr = strtoul(p, (char **)&p, 16);
439 #if defined(TARGET_I386)
441 #elif defined (TARGET_PPC)
445 cpu_single_step(env, 1);
446 ret = main_loop(opaque);
447 cpu_single_step(env, 0);
448 if (ret == EXCP_DEBUG)
452 snprintf(buf, sizeof(buf), "S%02x", ret);
456 env = cpu_gdbstub_get_env(opaque);
457 reg_size = cpu_gdb_read_registers(env, mem_buf);
458 memtohex(buf, mem_buf, reg_size);
462 env = cpu_gdbstub_get_env(opaque);
463 registers = (void *)mem_buf;
465 hextomem((uint8_t *)registers, p, len);
466 cpu_gdb_write_registers(env, mem_buf, len);
470 env = cpu_gdbstub_get_env(opaque);
471 addr = strtoul(p, (char **)&p, 16);
474 len = strtoul(p, NULL, 16);
475 if (cpu_memory_rw_debug(env, addr, mem_buf, len, 0) != 0)
476 memset(mem_buf, 0, len);
477 memtohex(buf, mem_buf, len);
481 env = cpu_gdbstub_get_env(opaque);
482 addr = strtoul(p, (char **)&p, 16);
485 len = strtoul(p, (char **)&p, 16);
488 hextomem(mem_buf, p, len);
489 if (cpu_memory_rw_debug(env, addr, mem_buf, len, 1) != 0)
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 if (cpu_breakpoint_insert(env, addr) < 0)
505 goto breakpoint_error;
513 type = strtoul(p, (char **)&p, 16);
516 addr = strtoul(p, (char **)&p, 16);
519 len = strtoul(p, (char **)&p, 16);
520 if (type == 0 || type == 1) {
521 env = cpu_gdbstub_get_env(opaque);
522 cpu_breakpoint_remove(env, addr);
525 goto breakpoint_error;
529 if (!strncmp(p, "Tinit", 5)) {
532 } else if (!strncmp(p, "TStart", 6)) {
533 /* start log (gdb 'tstart' command) */
534 env = cpu_gdbstub_get_env(opaque);
536 cpu_set_log(CPU_LOG_ALL);
538 } else if (!strncmp(p, "TStop", 5)) {
539 /* stop log (gdb 'tstop' command) */
543 goto unknown_command;
548 /* put empty packet */