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>
45 static int gdbstub_open(int port)
47 struct sockaddr_in sockaddr;
51 fd = socket(PF_INET, SOCK_STREAM, 0);
57 /* allow fast reuse */
59 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
61 sockaddr.sin_family = AF_INET;
62 sockaddr.sin_port = htons(port);
63 sockaddr.sin_addr.s_addr = 0;
64 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
75 /* now wait for one connection */
77 len = sizeof(sockaddr);
78 gdbstub_fd = accept(fd, (struct sockaddr *)&sockaddr, &len);
79 if (gdbstub_fd < 0 && errno != EINTR) {
82 } else if (gdbstub_fd >= 0) {
87 /* set short latency */
89 setsockopt(gdbstub_fd, SOL_TCP, TCP_NODELAY, &val, sizeof(val));
93 static int get_char(void)
99 ret = read(gdbstub_fd, &ch, 1);
101 if (errno != EINTR && errno != EAGAIN)
103 } else if (ret == 0) {
112 static void put_buffer(const uint8_t *buf, int len)
117 ret = write(gdbstub_fd, buf, len);
119 if (errno != EINTR && errno != EAGAIN)
128 static inline int fromhex(int v)
130 if (v >= '0' && v <= '9')
132 else if (v >= 'A' && v <= 'F')
134 else if (v >= 'a' && v <= 'f')
140 static inline int tohex(int v)
148 static void memtohex(char *buf, const uint8_t *mem, int len)
153 for(i = 0; i < len; i++) {
155 *q++ = tohex(c >> 4);
156 *q++ = tohex(c & 0xf);
161 static void hextomem(uint8_t *mem, const char *buf, int len)
165 for(i = 0; i < len; i++) {
166 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
171 /* return -1 if error or EOF */
172 static int get_packet(char *buf, int buf_size)
174 int ch, len, csum, csum1;
193 if (len > buf_size - 1)
202 csum1 = fromhex(ch) << 4;
206 csum1 |= fromhex(ch);
207 if ((csum & 0xff) != csum1) {
209 put_buffer(reply, 1);
212 put_buffer(reply, 1);
217 printf("command='%s'\n", buf);
222 /* return -1 if error, 0 if OK */
223 static int put_packet(char *buf)
226 int len, csum, ch, i;
229 printf("reply='%s'\n", buf);
236 put_buffer(buf, len);
238 for(i = 0; i < len; i++) {
242 buf1[1] = tohex((csum >> 4) & 0xf);
243 buf1[2] = tohex((csum) & 0xf);
256 static int memory_rw(uint8_t *buf, uint32_t addr, int len, int is_write)
262 page = addr & TARGET_PAGE_MASK;
263 l = (page + TARGET_PAGE_SIZE) - addr;
266 flags = page_get_flags(page);
267 if (!(flags & PAGE_VALID))
270 if (!(flags & PAGE_WRITE))
272 memcpy((uint8_t *)addr, buf, l);
274 if (!(flags & PAGE_READ))
276 memcpy(buf, (uint8_t *)addr, l);
285 /* port = 0 means default port */
286 int cpu_gdbstub(void *opaque, void (*main_loop)(void *opaque), int port)
290 int ret, ch, nb_regs, i;
292 uint8_t mem_buf[2000];
296 printf("Waiting gdb connection on port %d\n", port);
297 if (gdbstub_open(port) < 0)
299 printf("Connected\n");
301 ret = get_packet(buf, sizeof(buf));
308 snprintf(buf, sizeof(buf), "S%02x", SIGTRAP);
313 snprintf(buf, sizeof(buf), "S%02x", 0);
317 env = cpu_gdbstub_get_env(opaque);
318 registers = (void *)mem_buf;
319 #if defined(TARGET_I386)
320 for(i = 0; i < 8; i++) {
321 registers[i] = tswapl(env->regs[i]);
323 registers[8] = env->eip;
324 registers[9] = env->eflags;
325 registers[10] = env->segs[R_CS].selector;
326 registers[11] = env->segs[R_SS].selector;
327 registers[12] = env->segs[R_DS].selector;
328 registers[13] = env->segs[R_ES].selector;
329 registers[14] = env->segs[R_FS].selector;
330 registers[15] = env->segs[R_GS].selector;
333 memtohex(buf, (const uint8_t *)registers,
334 sizeof(registers[0]) * nb_regs);
338 env = cpu_gdbstub_get_env(opaque);
339 registers = (void *)mem_buf;
340 #if defined(TARGET_I386)
341 hextomem((uint8_t *)registers, p, 16 * 4);
342 for(i = 0; i < 8; i++) {
343 env->regs[i] = tswapl(registers[i]);
345 env->eip = registers[8];
346 env->eflags = registers[9];
347 #define LOAD_SEG(index, sreg)\
348 if (tswapl(registers[index]) != env->segs[sreg].selector)\
349 cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
360 addr = strtoul(p, (char **)&p, 16);
363 len = strtoul(p, NULL, 16);
364 if (memory_rw(mem_buf, addr, len, 0) != 0)
365 memset(mem_buf, 0, len);
366 memtohex(buf, mem_buf, len);
370 addr = strtoul(p, (char **)&p, 16);
373 len = strtoul(p, (char **)&p, 16);
376 hextomem(mem_buf, p, len);
377 if (memory_rw(mem_buf, addr, len, 1) != 0)
383 /* put empty packet */