Statistics
| Branch: | Revision:

root / gdbstub.c @ a412ac57

History | View | Annotate | Download (10.8 kB)

1 b4608c04 bellard
/*
2 b4608c04 bellard
 * gdb server stub
3 b4608c04 bellard
 * 
4 b4608c04 bellard
 * Copyright (c) 2003 Fabrice Bellard
5 b4608c04 bellard
 *
6 b4608c04 bellard
 * This library is free software; you can redistribute it and/or
7 b4608c04 bellard
 * modify it under the terms of the GNU Lesser General Public
8 b4608c04 bellard
 * License as published by the Free Software Foundation; either
9 b4608c04 bellard
 * version 2 of the License, or (at your option) any later version.
10 b4608c04 bellard
 *
11 b4608c04 bellard
 * This library is distributed in the hope that it will be useful,
12 b4608c04 bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 b4608c04 bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 b4608c04 bellard
 * Lesser General Public License for more details.
15 b4608c04 bellard
 *
16 b4608c04 bellard
 * You should have received a copy of the GNU Lesser General Public
17 b4608c04 bellard
 * License along with this library; if not, write to the Free Software
18 b4608c04 bellard
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 b4608c04 bellard
 */
20 b4608c04 bellard
#include <stdlib.h>
21 b4608c04 bellard
#include <stdio.h>
22 b4608c04 bellard
#include <string.h>
23 b4608c04 bellard
#include <unistd.h>
24 b4608c04 bellard
#include <errno.h>
25 b4608c04 bellard
#include <sys/socket.h>
26 b4608c04 bellard
#include <netinet/in.h>
27 b4608c04 bellard
#include <netinet/tcp.h>
28 b4608c04 bellard
#include <signal.h>
29 b4608c04 bellard
30 b4608c04 bellard
#include "config.h"
31 b4608c04 bellard
#ifdef TARGET_I386
32 b4608c04 bellard
#include "cpu-i386.h"
33 b4608c04 bellard
#endif
34 b4608c04 bellard
#ifdef TARGET_ARM
35 b4608c04 bellard
#include "cpu-arm.h"
36 b4608c04 bellard
#endif
37 b4608c04 bellard
#include "thunk.h"
38 b4608c04 bellard
#include "exec.h"
39 b4608c04 bellard
40 4c3a88a2 bellard
#define DEBUG_GDB
41 b4608c04 bellard
42 b4608c04 bellard
int gdbstub_fd = -1;
43 b4608c04 bellard
44 b4608c04 bellard
/* return 0 if OK */
45 b4608c04 bellard
static int gdbstub_open(int port)
46 b4608c04 bellard
{
47 b4608c04 bellard
    struct sockaddr_in sockaddr;
48 b4608c04 bellard
    socklen_t len;
49 b4608c04 bellard
    int fd, val, ret;
50 b4608c04 bellard
51 b4608c04 bellard
    fd = socket(PF_INET, SOCK_STREAM, 0);
52 b4608c04 bellard
    if (fd < 0) {
53 b4608c04 bellard
        perror("socket");
54 b4608c04 bellard
        return -1;
55 b4608c04 bellard
    }
56 b4608c04 bellard
57 b4608c04 bellard
    /* allow fast reuse */
58 b4608c04 bellard
    val = 1;
59 b4608c04 bellard
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
60 b4608c04 bellard
61 b4608c04 bellard
    sockaddr.sin_family = AF_INET;
62 b4608c04 bellard
    sockaddr.sin_port = htons(port);
63 b4608c04 bellard
    sockaddr.sin_addr.s_addr = 0;
64 b4608c04 bellard
    ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
65 b4608c04 bellard
    if (ret < 0) {
66 b4608c04 bellard
        perror("bind");
67 b4608c04 bellard
        return -1;
68 b4608c04 bellard
    }
69 b4608c04 bellard
    ret = listen(fd, 0);
70 b4608c04 bellard
    if (ret < 0) {
71 b4608c04 bellard
        perror("listen");
72 b4608c04 bellard
        return -1;
73 b4608c04 bellard
    }
74 b4608c04 bellard
    
75 b4608c04 bellard
    /* now wait for one connection */
76 b4608c04 bellard
    for(;;) {
77 b4608c04 bellard
        len = sizeof(sockaddr);
78 b4608c04 bellard
        gdbstub_fd = accept(fd, (struct sockaddr *)&sockaddr, &len);
79 b4608c04 bellard
        if (gdbstub_fd < 0 && errno != EINTR) {
80 b4608c04 bellard
            perror("accept");
81 b4608c04 bellard
            return -1;
82 b4608c04 bellard
        } else if (gdbstub_fd >= 0) {
83 b4608c04 bellard
            break;
84 b4608c04 bellard
        }
85 b4608c04 bellard
    }
86 b4608c04 bellard
    
87 b4608c04 bellard
    /* set short latency */
88 b4608c04 bellard
    val = 1;
89 b4608c04 bellard
    setsockopt(gdbstub_fd, SOL_TCP, TCP_NODELAY, &val, sizeof(val));
90 b4608c04 bellard
    return 0;
91 b4608c04 bellard
}
92 b4608c04 bellard
93 b4608c04 bellard
static int get_char(void)
94 b4608c04 bellard
{
95 b4608c04 bellard
    uint8_t ch;
96 b4608c04 bellard
    int ret;
97 b4608c04 bellard
98 b4608c04 bellard
    for(;;) {
99 b4608c04 bellard
        ret = read(gdbstub_fd, &ch, 1);
100 b4608c04 bellard
        if (ret < 0) {
101 b4608c04 bellard
            if (errno != EINTR && errno != EAGAIN)
102 b4608c04 bellard
                return -1;
103 b4608c04 bellard
        } else if (ret == 0) {
104 b4608c04 bellard
            return -1;
105 b4608c04 bellard
        } else {
106 b4608c04 bellard
            break;
107 b4608c04 bellard
        }
108 b4608c04 bellard
    }
109 b4608c04 bellard
    return ch;
110 b4608c04 bellard
}
111 b4608c04 bellard
112 b4608c04 bellard
static void put_buffer(const uint8_t *buf, int len)
113 b4608c04 bellard
{
114 b4608c04 bellard
    int ret;
115 b4608c04 bellard
116 b4608c04 bellard
    while (len > 0) {
117 b4608c04 bellard
        ret = write(gdbstub_fd, buf, len);
118 b4608c04 bellard
        if (ret < 0) {
119 b4608c04 bellard
            if (errno != EINTR && errno != EAGAIN)
120 b4608c04 bellard
                return;
121 b4608c04 bellard
        } else {
122 b4608c04 bellard
            buf += ret;
123 b4608c04 bellard
            len -= ret;
124 b4608c04 bellard
        }
125 b4608c04 bellard
    }
126 b4608c04 bellard
}
127 b4608c04 bellard
128 b4608c04 bellard
static inline int fromhex(int v)
129 b4608c04 bellard
{
130 b4608c04 bellard
    if (v >= '0' && v <= '9')
131 b4608c04 bellard
        return v - '0';
132 b4608c04 bellard
    else if (v >= 'A' && v <= 'F')
133 b4608c04 bellard
        return v - 'A' + 10;
134 b4608c04 bellard
    else if (v >= 'a' && v <= 'f')
135 b4608c04 bellard
        return v - 'a' + 10;
136 b4608c04 bellard
    else
137 b4608c04 bellard
        return 0;
138 b4608c04 bellard
}
139 b4608c04 bellard
140 b4608c04 bellard
static inline int tohex(int v)
141 b4608c04 bellard
{
142 b4608c04 bellard
    if (v < 10)
143 b4608c04 bellard
        return v + '0';
144 b4608c04 bellard
    else
145 b4608c04 bellard
        return v - 10 + 'a';
146 b4608c04 bellard
}
147 b4608c04 bellard
148 b4608c04 bellard
static void memtohex(char *buf, const uint8_t *mem, int len)
149 b4608c04 bellard
{
150 b4608c04 bellard
    int i, c;
151 b4608c04 bellard
    char *q;
152 b4608c04 bellard
    q = buf;
153 b4608c04 bellard
    for(i = 0; i < len; i++) {
154 b4608c04 bellard
        c = mem[i];
155 b4608c04 bellard
        *q++ = tohex(c >> 4);
156 b4608c04 bellard
        *q++ = tohex(c & 0xf);
157 b4608c04 bellard
    }
158 b4608c04 bellard
    *q = '\0';
159 b4608c04 bellard
}
160 b4608c04 bellard
161 b4608c04 bellard
static void hextomem(uint8_t *mem, const char *buf, int len)
162 b4608c04 bellard
{
163 b4608c04 bellard
    int i;
164 b4608c04 bellard
165 b4608c04 bellard
    for(i = 0; i < len; i++) {
166 b4608c04 bellard
        mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
167 b4608c04 bellard
        buf += 2;
168 b4608c04 bellard
    }
169 b4608c04 bellard
}
170 b4608c04 bellard
171 b4608c04 bellard
/* return -1 if error or EOF */
172 b4608c04 bellard
static int get_packet(char *buf, int buf_size)
173 b4608c04 bellard
{
174 b4608c04 bellard
    int ch, len, csum, csum1;
175 b4608c04 bellard
    char reply[1];
176 b4608c04 bellard
    
177 b4608c04 bellard
    for(;;) {
178 b4608c04 bellard
        for(;;) {
179 b4608c04 bellard
            ch = get_char();
180 b4608c04 bellard
            if (ch < 0)
181 b4608c04 bellard
                return -1;
182 b4608c04 bellard
            if (ch == '$')
183 b4608c04 bellard
                break;
184 b4608c04 bellard
        }
185 b4608c04 bellard
        len = 0;
186 b4608c04 bellard
        csum = 0;
187 b4608c04 bellard
        for(;;) {
188 b4608c04 bellard
            ch = get_char();
189 b4608c04 bellard
            if (ch < 0)
190 b4608c04 bellard
                return -1;
191 b4608c04 bellard
            if (ch == '#')
192 b4608c04 bellard
                break;
193 b4608c04 bellard
            if (len > buf_size - 1)
194 b4608c04 bellard
                return -1;
195 b4608c04 bellard
            buf[len++] = ch;
196 b4608c04 bellard
            csum += ch;
197 b4608c04 bellard
        }
198 b4608c04 bellard
        buf[len] = '\0';
199 b4608c04 bellard
        ch = get_char();
200 b4608c04 bellard
        if (ch < 0)
201 b4608c04 bellard
            return -1;
202 b4608c04 bellard
        csum1 = fromhex(ch) << 4;
203 b4608c04 bellard
        ch = get_char();
204 b4608c04 bellard
        if (ch < 0)
205 b4608c04 bellard
            return -1;
206 b4608c04 bellard
        csum1 |= fromhex(ch);
207 b4608c04 bellard
        if ((csum & 0xff) != csum1) {
208 b4608c04 bellard
            reply[0] = '-';
209 b4608c04 bellard
            put_buffer(reply, 1);
210 b4608c04 bellard
        } else {
211 b4608c04 bellard
            reply[0] = '+';
212 b4608c04 bellard
            put_buffer(reply, 1);
213 b4608c04 bellard
            break;
214 b4608c04 bellard
        }
215 b4608c04 bellard
    }
216 b4608c04 bellard
#ifdef DEBUG_GDB
217 b4608c04 bellard
    printf("command='%s'\n", buf);
218 b4608c04 bellard
#endif
219 b4608c04 bellard
    return len;
220 b4608c04 bellard
}
221 b4608c04 bellard
222 b4608c04 bellard
/* return -1 if error, 0 if OK */
223 b4608c04 bellard
static int put_packet(char *buf)
224 b4608c04 bellard
{
225 b4608c04 bellard
    char buf1[3];
226 b4608c04 bellard
    int len, csum, ch, i;
227 b4608c04 bellard
228 b4608c04 bellard
#ifdef DEBUG_GDB
229 b4608c04 bellard
    printf("reply='%s'\n", buf);
230 b4608c04 bellard
#endif
231 b4608c04 bellard
232 b4608c04 bellard
    for(;;) {
233 b4608c04 bellard
        buf1[0] = '$';
234 b4608c04 bellard
        put_buffer(buf1, 1);
235 b4608c04 bellard
        len = strlen(buf);
236 b4608c04 bellard
        put_buffer(buf, len);
237 b4608c04 bellard
        csum = 0;
238 b4608c04 bellard
        for(i = 0; i < len; i++) {
239 b4608c04 bellard
            csum += buf[i];
240 b4608c04 bellard
        }
241 b4608c04 bellard
        buf1[0] = '#';
242 b4608c04 bellard
        buf1[1] = tohex((csum >> 4) & 0xf);
243 b4608c04 bellard
        buf1[2] = tohex((csum) & 0xf);
244 b4608c04 bellard
245 b4608c04 bellard
        put_buffer(buf1, 3);
246 b4608c04 bellard
247 b4608c04 bellard
        ch = get_char();
248 b4608c04 bellard
        if (ch < 0)
249 b4608c04 bellard
            return -1;
250 b4608c04 bellard
        if (ch == '+')
251 b4608c04 bellard
            break;
252 b4608c04 bellard
    }
253 b4608c04 bellard
    return 0;
254 b4608c04 bellard
}
255 b4608c04 bellard
256 b4608c04 bellard
static int memory_rw(uint8_t *buf, uint32_t addr, int len, int is_write)
257 b4608c04 bellard
{
258 b4608c04 bellard
    int l, flags;
259 b4608c04 bellard
    uint32_t page;
260 b4608c04 bellard
261 b4608c04 bellard
    while (len > 0) {
262 b4608c04 bellard
        page = addr & TARGET_PAGE_MASK;
263 b4608c04 bellard
        l = (page + TARGET_PAGE_SIZE) - addr;
264 b4608c04 bellard
        if (l > len)
265 b4608c04 bellard
            l = len;
266 b4608c04 bellard
        flags = page_get_flags(page);
267 b4608c04 bellard
        if (!(flags & PAGE_VALID))
268 b4608c04 bellard
            return -1;
269 b4608c04 bellard
        if (is_write) {
270 b4608c04 bellard
            if (!(flags & PAGE_WRITE))
271 b4608c04 bellard
                return -1;
272 b4608c04 bellard
            memcpy((uint8_t *)addr, buf, l);
273 b4608c04 bellard
        } else {
274 b4608c04 bellard
            if (!(flags & PAGE_READ))
275 b4608c04 bellard
                return -1;
276 b4608c04 bellard
            memcpy(buf, (uint8_t *)addr, l);
277 b4608c04 bellard
        }
278 b4608c04 bellard
        len -= l;
279 b4608c04 bellard
        buf += l;
280 b4608c04 bellard
        addr += l;
281 b4608c04 bellard
    }
282 b4608c04 bellard
    return 0;
283 b4608c04 bellard
}
284 b4608c04 bellard
285 b4608c04 bellard
/* port = 0 means default port */
286 4c3a88a2 bellard
int cpu_gdbstub(void *opaque, int (*main_loop)(void *opaque), int port)
287 b4608c04 bellard
{
288 b4608c04 bellard
    CPUState *env;
289 b4608c04 bellard
    const char *p;
290 4c3a88a2 bellard
    int ret, ch, nb_regs, i, type;
291 b4608c04 bellard
    char buf[4096];
292 b4608c04 bellard
    uint8_t mem_buf[2000];
293 b4608c04 bellard
    uint32_t *registers;
294 b4608c04 bellard
    uint32_t addr, len;
295 b4608c04 bellard
    
296 b4608c04 bellard
    printf("Waiting gdb connection on port %d\n", port);
297 b4608c04 bellard
    if (gdbstub_open(port) < 0)
298 b4608c04 bellard
        return -1;
299 b4608c04 bellard
    printf("Connected\n");
300 b4608c04 bellard
    for(;;) {
301 b4608c04 bellard
        ret = get_packet(buf, sizeof(buf));
302 b4608c04 bellard
        if (ret < 0)
303 b4608c04 bellard
            break;
304 b4608c04 bellard
        p = buf;
305 b4608c04 bellard
        ch = *p++;
306 b4608c04 bellard
        switch(ch) {
307 b4608c04 bellard
        case '?':
308 b4608c04 bellard
            snprintf(buf, sizeof(buf), "S%02x", SIGTRAP);
309 b4608c04 bellard
            put_packet(buf);
310 b4608c04 bellard
            break;
311 b4608c04 bellard
        case 'c':
312 4c3a88a2 bellard
            if (*p != '\0') {
313 4c3a88a2 bellard
                addr = strtoul(p, (char **)&p, 16);
314 4c3a88a2 bellard
                env = cpu_gdbstub_get_env(opaque);
315 4c3a88a2 bellard
#if defined(TARGET_I386)
316 4c3a88a2 bellard
                env->eip = addr;
317 4c3a88a2 bellard
#endif
318 4c3a88a2 bellard
            }
319 4c3a88a2 bellard
            ret = main_loop(opaque);
320 4c3a88a2 bellard
            if (ret == EXCP_DEBUG)
321 4c3a88a2 bellard
                ret = SIGTRAP;
322 4c3a88a2 bellard
            else
323 4c3a88a2 bellard
                ret = 0;
324 4c3a88a2 bellard
            snprintf(buf, sizeof(buf), "S%02x", ret);
325 b4608c04 bellard
            put_packet(buf);
326 b4608c04 bellard
            break;
327 b4608c04 bellard
        case 'g':
328 b4608c04 bellard
            env = cpu_gdbstub_get_env(opaque);
329 b4608c04 bellard
            registers = (void *)mem_buf;
330 b4608c04 bellard
#if defined(TARGET_I386)
331 b4608c04 bellard
            for(i = 0; i < 8; i++) {
332 b4608c04 bellard
                registers[i] = tswapl(env->regs[i]);
333 b4608c04 bellard
            }
334 b4608c04 bellard
            registers[8] = env->eip;
335 b4608c04 bellard
            registers[9] = env->eflags;
336 b4608c04 bellard
            registers[10] = env->segs[R_CS].selector;
337 b4608c04 bellard
            registers[11] = env->segs[R_SS].selector;
338 b4608c04 bellard
            registers[12] = env->segs[R_DS].selector;
339 b4608c04 bellard
            registers[13] = env->segs[R_ES].selector;
340 b4608c04 bellard
            registers[14] = env->segs[R_FS].selector;
341 b4608c04 bellard
            registers[15] = env->segs[R_GS].selector;
342 b4608c04 bellard
            nb_regs = 16;
343 b4608c04 bellard
#endif
344 b4608c04 bellard
            memtohex(buf, (const uint8_t *)registers, 
345 b4608c04 bellard
                     sizeof(registers[0]) * nb_regs);
346 b4608c04 bellard
            put_packet(buf);
347 b4608c04 bellard
            break;
348 b4608c04 bellard
        case 'G':
349 b4608c04 bellard
            env = cpu_gdbstub_get_env(opaque);
350 b4608c04 bellard
            registers = (void *)mem_buf;
351 b4608c04 bellard
#if defined(TARGET_I386)
352 b4608c04 bellard
            hextomem((uint8_t *)registers, p, 16 * 4);
353 b4608c04 bellard
            for(i = 0; i < 8; i++) {
354 b4608c04 bellard
                env->regs[i] = tswapl(registers[i]);
355 b4608c04 bellard
            }
356 b4608c04 bellard
            env->eip = registers[8];
357 b4608c04 bellard
            env->eflags = registers[9];
358 b4608c04 bellard
#define LOAD_SEG(index, sreg)\
359 b4608c04 bellard
            if (tswapl(registers[index]) != env->segs[sreg].selector)\
360 b4608c04 bellard
                cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
361 b4608c04 bellard
            LOAD_SEG(10, R_CS);
362 b4608c04 bellard
            LOAD_SEG(11, R_SS);
363 b4608c04 bellard
            LOAD_SEG(12, R_DS);
364 b4608c04 bellard
            LOAD_SEG(13, R_ES);
365 b4608c04 bellard
            LOAD_SEG(14, R_FS);
366 b4608c04 bellard
            LOAD_SEG(15, R_GS);
367 b4608c04 bellard
#endif
368 b4608c04 bellard
            put_packet("OK");
369 b4608c04 bellard
            break;
370 b4608c04 bellard
        case 'm':
371 b4608c04 bellard
            addr = strtoul(p, (char **)&p, 16);
372 b4608c04 bellard
            if (*p == ',')
373 b4608c04 bellard
                p++;
374 b4608c04 bellard
            len = strtoul(p, NULL, 16);
375 b4608c04 bellard
            if (memory_rw(mem_buf, addr, len, 0) != 0)
376 b4608c04 bellard
                memset(mem_buf, 0, len);
377 b4608c04 bellard
            memtohex(buf, mem_buf, len);
378 b4608c04 bellard
            put_packet(buf);
379 b4608c04 bellard
            break;
380 b4608c04 bellard
        case 'M':
381 b4608c04 bellard
            addr = strtoul(p, (char **)&p, 16);
382 b4608c04 bellard
            if (*p == ',')
383 b4608c04 bellard
                p++;
384 b4608c04 bellard
            len = strtoul(p, (char **)&p, 16);
385 b4608c04 bellard
            if (*p == ',')
386 b4608c04 bellard
                p++;
387 b4608c04 bellard
            hextomem(mem_buf, p, len);
388 b4608c04 bellard
            if (memory_rw(mem_buf, addr, len, 1) != 0)
389 b4608c04 bellard
                put_packet("ENN");
390 b4608c04 bellard
            else
391 b4608c04 bellard
                put_packet("OK");
392 b4608c04 bellard
            break;
393 4c3a88a2 bellard
        case 'Z':
394 4c3a88a2 bellard
            type = strtoul(p, (char **)&p, 16);
395 4c3a88a2 bellard
            if (*p == ',')
396 4c3a88a2 bellard
                p++;
397 4c3a88a2 bellard
            addr = strtoul(p, (char **)&p, 16);
398 4c3a88a2 bellard
            if (*p == ',')
399 4c3a88a2 bellard
                p++;
400 4c3a88a2 bellard
            len = strtoul(p, (char **)&p, 16);
401 4c3a88a2 bellard
            if (type == 0 || type == 1) {
402 4c3a88a2 bellard
                env = cpu_gdbstub_get_env(opaque);
403 4c3a88a2 bellard
                if (cpu_breakpoint_insert(env, addr) < 0)
404 4c3a88a2 bellard
                    goto breakpoint_error;
405 4c3a88a2 bellard
                put_packet("OK");
406 4c3a88a2 bellard
            } else {
407 4c3a88a2 bellard
            breakpoint_error:
408 4c3a88a2 bellard
                put_packet("ENN");
409 4c3a88a2 bellard
            }
410 4c3a88a2 bellard
            break;
411 4c3a88a2 bellard
        case 'z':
412 4c3a88a2 bellard
            type = strtoul(p, (char **)&p, 16);
413 4c3a88a2 bellard
            if (*p == ',')
414 4c3a88a2 bellard
                p++;
415 4c3a88a2 bellard
            addr = strtoul(p, (char **)&p, 16);
416 4c3a88a2 bellard
            if (*p == ',')
417 4c3a88a2 bellard
                p++;
418 4c3a88a2 bellard
            len = strtoul(p, (char **)&p, 16);
419 4c3a88a2 bellard
            if (type == 0 || type == 1) {
420 4c3a88a2 bellard
                env = cpu_gdbstub_get_env(opaque);
421 4c3a88a2 bellard
                cpu_breakpoint_remove(env, addr);
422 4c3a88a2 bellard
                put_packet("OK");
423 4c3a88a2 bellard
            } else {
424 4c3a88a2 bellard
                goto breakpoint_error;
425 4c3a88a2 bellard
            }
426 4c3a88a2 bellard
            break;
427 b4608c04 bellard
        default:
428 b4608c04 bellard
            /* put empty packet */
429 b4608c04 bellard
            buf[0] = '\0';
430 b4608c04 bellard
            put_packet(buf);
431 b4608c04 bellard
            break;
432 b4608c04 bellard
        }
433 b4608c04 bellard
    }
434 b4608c04 bellard
    return 0;
435 b4608c04 bellard
}