Blame view

gdbstub.c 14.6 KB
bellard authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * gdb server stub
 * 
 * Copyright (c) 2003 Fabrice Bellard
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
20
21
#include "vl.h"
bellard authored
22
23
24
25
26
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <signal.h>
bellard authored
27
//#define DEBUG_GDB
bellard authored
28
29
30
31
32
33
34
enum RSState {
    RS_IDLE,
    RS_GETLINE,
    RS_CHKSUM1,
    RS_CHKSUM2,
};
bellard authored
35
36
static int gdbserver_fd;
bellard authored
37
38
39
40
41
42
43
44
typedef struct GDBState {
    enum RSState state;
    int fd;
    char line_buf[4096];
    int line_buf_index;
    int line_csum;
} GDBState;
bellard authored
45
46
static int get_char(GDBState *s)
bellard authored
47
48
49
50
51
{
    uint8_t ch;
    int ret;

    for(;;) {
52
        ret = read(s->fd, &ch, 1);
bellard authored
53
54
55
56
57
58
59
60
61
62
63
64
        if (ret < 0) {
            if (errno != EINTR && errno != EAGAIN)
                return -1;
        } else if (ret == 0) {
            return -1;
        } else {
            break;
        }
    }
    return ch;
}
65
static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellard authored
66
67
68
69
{
    int ret;

    while (len > 0) {
70
        ret = write(s->fd, buf, len);
bellard authored
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
        if (ret < 0) {
            if (errno != EINTR && errno != EAGAIN)
                return;
        } else {
            buf += ret;
            len -= ret;
        }
    }
}

static inline int fromhex(int v)
{
    if (v >= '0' && v <= '9')
        return v - '0';
    else if (v >= 'A' && v <= 'F')
        return v - 'A' + 10;
    else if (v >= 'a' && v <= 'f')
        return v - 'a' + 10;
    else
        return 0;
}

static inline int tohex(int v)
{
    if (v < 10)
        return v + '0';
    else
        return v - 10 + 'a';
}

static void memtohex(char *buf, const uint8_t *mem, int len)
{
    int i, c;
    char *q;
    q = buf;
    for(i = 0; i < len; i++) {
        c = mem[i];
        *q++ = tohex(c >> 4);
        *q++ = tohex(c & 0xf);
    }
    *q = '\0';
}

static void hextomem(uint8_t *mem, const char *buf, int len)
{
    int i;

    for(i = 0; i < len; i++) {
        mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
        buf += 2;
    }
}

/* return -1 if error, 0 if OK */
125
static int put_packet(GDBState *s, char *buf)
bellard authored
126
127
128
129
130
131
132
133
134
135
{
    char buf1[3];
    int len, csum, ch, i;

#ifdef DEBUG_GDB
    printf("reply='%s'\n", buf);
#endif

    for(;;) {
        buf1[0] = '$';
136
        put_buffer(s, buf1, 1);
bellard authored
137
        len = strlen(buf);
138
        put_buffer(s, buf, len);
bellard authored
139
140
141
142
143
144
145
146
        csum = 0;
        for(i = 0; i < len; i++) {
            csum += buf[i];
        }
        buf1[0] = '#';
        buf1[1] = tohex((csum >> 4) & 0xf);
        buf1[2] = tohex((csum) & 0xf);
147
        put_buffer(s, buf1, 3);
bellard authored
148
149
        ch = get_char(s);
bellard authored
150
151
152
153
154
155
156
157
        if (ch < 0)
            return -1;
        if (ch == '+')
            break;
    }
    return 0;
}
bellard authored
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#if defined(TARGET_I386)

static void to_le32(uint8_t *p, int v)
{
    p[0] = v;
    p[1] = v >> 8;
    p[2] = v >> 16;
    p[3] = v >> 24;
}

static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
{
    int i, fpus;

    for(i = 0; i < 8; i++) {
        to_le32(mem_buf + i * 4, env->regs[i]);
    }
    to_le32(mem_buf + 8 * 4, env->eip);
    to_le32(mem_buf + 9 * 4, env->eflags);
    to_le32(mem_buf + 10 * 4, env->segs[R_CS].selector);
    to_le32(mem_buf + 11 * 4, env->segs[R_SS].selector);
    to_le32(mem_buf + 12 * 4, env->segs[R_DS].selector);
    to_le32(mem_buf + 13 * 4, env->segs[R_ES].selector);
    to_le32(mem_buf + 14 * 4, env->segs[R_FS].selector);
    to_le32(mem_buf + 15 * 4, env->segs[R_GS].selector);
    /* XXX: convert floats */
    for(i = 0; i < 8; i++) {
        memcpy(mem_buf + 16 * 4 + i * 10, &env->fpregs[i], 10);
    }
    to_le32(mem_buf + 36 * 4, env->fpuc);
    fpus = (env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11;
    to_le32(mem_buf + 37 * 4, fpus);
    to_le32(mem_buf + 38 * 4, 0); /* XXX: convert tags */
    to_le32(mem_buf + 39 * 4, 0); /* fiseg */
    to_le32(mem_buf + 40 * 4, 0); /* fioff */
    to_le32(mem_buf + 41 * 4, 0); /* foseg */
    to_le32(mem_buf + 42 * 4, 0); /* fooff */
    to_le32(mem_buf + 43 * 4, 0); /* fop */
    return 44 * 4;
}

static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
{
    uint32_t *registers = (uint32_t *)mem_buf;
    int i;

    for(i = 0; i < 8; i++) {
        env->regs[i] = tswapl(registers[i]);
    }
    env->eip = registers[8];
    env->eflags = registers[9];
#if defined(CONFIG_USER_ONLY)
#define LOAD_SEG(index, sreg)\
            if (tswapl(registers[index]) != env->segs[sreg].selector)\
                cpu_x86_load_seg(env, sreg, tswapl(registers[index]));
            LOAD_SEG(10, R_CS);
            LOAD_SEG(11, R_SS);
            LOAD_SEG(12, R_DS);
            LOAD_SEG(13, R_ES);
            LOAD_SEG(14, R_FS);
            LOAD_SEG(15, R_GS);
#endif
}
bellard authored
222
#elif defined (TARGET_PPC)
223
static void to_le32(uint32_t *buf, uint32_t v)
bellard authored
224
{
225
    uint8_t *p = (uint8_t *)buf;
bellard authored
226
227
228
229
230
231
    p[3] = v;
    p[2] = v >> 8;
    p[1] = v >> 16;
    p[0] = v >> 24;
}
232
233
234
235
236
237
238
static uint32_t from_le32 (uint32_t *buf)
{
    uint8_t *p = (uint8_t *)buf;

    return p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
}
bellard authored
239
240
static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
{
241
    uint32_t *registers = (uint32_t *)mem_buf, tmp;
bellard authored
242
243
244
    int i;

    /* fill in gprs */
245
246
    for(i = 0; i < 32; i++) {
        to_le32(&registers[i], env->gpr[i]);
bellard authored
247
248
249
    }
    /* fill in fprs */
    for (i = 0; i < 32; i++) {
250
251
        to_le32(&registers[(i * 2) + 32], *((uint32_t *)&env->fpr[i]));
	to_le32(&registers[(i * 2) + 33], *((uint32_t *)&env->fpr[i] + 1));
bellard authored
252
253
    }
    /* nip, msr, ccr, lnk, ctr, xer, mq */
254
255
    to_le32(&registers[96], (uint32_t)env->nip/* - 4*/);
    to_le32(&registers[97], _load_msr(env));
bellard authored
256
257
    tmp = 0;
    for (i = 0; i < 8; i++)
258
259
260
261
262
263
264
265
        tmp |= env->crf[i] << (32 - ((i + 1) * 4));
    to_le32(&registers[98], tmp);
    to_le32(&registers[99], env->lr);
    to_le32(&registers[100], env->ctr);
    to_le32(&registers[101], _load_xer(env));
    to_le32(&registers[102], 0);

    return 103 * 4;
bellard authored
266
267
268
269
270
271
272
273
274
}

static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
{
    uint32_t *registers = (uint32_t *)mem_buf;
    int i;

    /* fill in gprs */
    for (i = 0; i < 32; i++) {
275
        env->gpr[i] = from_le32(&registers[i]);
bellard authored
276
277
278
    }
    /* fill in fprs */
    for (i = 0; i < 32; i++) {
279
280
        *((uint32_t *)&env->fpr[i]) = from_le32(&registers[(i * 2) + 32]);
	*((uint32_t *)&env->fpr[i] + 1) = from_le32(&registers[(i * 2) + 33]);
bellard authored
281
282
    }
    /* nip, msr, ccr, lnk, ctr, xer, mq */
283
284
285
    env->nip = from_le32(&registers[96]);
    _store_msr(env, from_le32(&registers[97]));
    registers[98] = from_le32(&registers[98]);
bellard authored
286
    for (i = 0; i < 8; i++)
287
288
289
290
        env->crf[i] = (registers[98] >> (32 - ((i + 1) * 4))) & 0xF;
    env->lr = from_le32(&registers[99]);
    env->ctr = from_le32(&registers[100]);
    _store_xer(env, from_le32(&registers[101]));
bellard authored
291
}
bellard authored
292
293
294
295
296
297
298
299
300
301
302
303
#else

static int cpu_gdb_read_registers(CPUState *env, uint8_t *mem_buf)
{
    return 0;
}

static void cpu_gdb_write_registers(CPUState *env, uint8_t *mem_buf, int size)
{
}

#endif
bellard authored
304
305

/* port = 0 means default port */
306
static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellard authored
307
{
308
    CPUState *env = cpu_single_env;
bellard authored
309
    const char *p;
310
    int ch, reg_size, type;
bellard authored
311
312
313
314
315
    char buf[4096];
    uint8_t mem_buf[2000];
    uint32_t *registers;
    uint32_t addr, len;
316
317
318
319
320
321
322
323
324
325
326
327
328
#ifdef DEBUG_GDB
    printf("command='%s'\n", line_buf);
#endif
    p = line_buf;
    ch = *p++;
    switch(ch) {
    case '?':
        snprintf(buf, sizeof(buf), "S%02x", SIGTRAP);
        put_packet(s, buf);
        break;
    case 'c':
        if (*p != '\0') {
            addr = strtoul(p, (char **)&p, 16);
bellard authored
329
#if defined(TARGET_I386)
330
            env->eip = addr;
bellard authored
331
#elif defined (TARGET_PPC)
332
            env->nip = addr;
bellard authored
333
#endif
334
335
336
337
338
339
        }
        vm_start();
        break;
    case 's':
        if (*p != '\0') {
            addr = strtoul(p, (char **)&p, 16);
340
#if defined(TARGET_I386)
341
            env->eip = addr;
bellard authored
342
#elif defined (TARGET_PPC)
343
            env->nip = addr;
344
#endif
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
        }
        cpu_single_step(env, 1);
        vm_start();
        break;
    case 'g':
        reg_size = cpu_gdb_read_registers(env, mem_buf);
        memtohex(buf, mem_buf, reg_size);
        put_packet(s, buf);
        break;
    case 'G':
        registers = (void *)mem_buf;
        len = strlen(p) / 2;
        hextomem((uint8_t *)registers, p, len);
        cpu_gdb_write_registers(env, mem_buf, len);
        put_packet(s, "OK");
        break;
    case 'm':
        addr = strtoul(p, (char **)&p, 16);
        if (*p == ',')
            p++;
        len = strtoul(p, NULL, 16);
        if (cpu_memory_rw_debug(env, addr, mem_buf, len, 0) != 0)
            memset(mem_buf, 0, len);
        memtohex(buf, mem_buf, len);
        put_packet(s, buf);
        break;
    case 'M':
        addr = strtoul(p, (char **)&p, 16);
        if (*p == ',')
            p++;
        len = strtoul(p, (char **)&p, 16);
        if (*p == ',')
            p++;
        hextomem(mem_buf, p, len);
        if (cpu_memory_rw_debug(env, addr, mem_buf, len, 1) != 0)
            put_packet(s, "ENN");
        else
            put_packet(s, "OK");
        break;
    case 'Z':
        type = strtoul(p, (char **)&p, 16);
        if (*p == ',')
            p++;
        addr = strtoul(p, (char **)&p, 16);
        if (*p == ',')
            p++;
        len = strtoul(p, (char **)&p, 16);
        if (type == 0 || type == 1) {
            if (cpu_breakpoint_insert(env, addr) < 0)
                goto breakpoint_error;
            put_packet(s, "OK");
        } else {
        breakpoint_error:
            put_packet(s, "ENN");
        }
        break;
    case 'z':
        type = strtoul(p, (char **)&p, 16);
        if (*p == ',')
            p++;
        addr = strtoul(p, (char **)&p, 16);
        if (*p == ',')
            p++;
        len = strtoul(p, (char **)&p, 16);
        if (type == 0 || type == 1) {
            cpu_breakpoint_remove(env, addr);
            put_packet(s, "OK");
        } else {
            goto breakpoint_error;
        }
        break;
    default:
        //        unknown_command:
        /* put empty packet */
        buf[0] = '\0';
        put_packet(s, buf);
        break;
    }
    return RS_IDLE;
}

static void gdb_vm_stopped(void *opaque, int reason)
{
    GDBState *s = opaque;
    char buf[256];
    int ret;

    /* disable single step if it was enable */
    cpu_single_step(cpu_single_env, 0);

    if (reason == EXCP_DEBUG)
        ret = SIGTRAP;
    else
        ret = 0;
    snprintf(buf, sizeof(buf), "S%02x", ret);
    put_packet(s, buf);
}

static void gdb_read_byte(GDBState *s, int ch)
{
    int i, csum;
    char reply[1];

    if (vm_running) {
        /* when the CPU is running, we cannot do anything except stop
           it when receiving a char */
        vm_stop(EXCP_INTERRUPT);
    } else {
        switch(s->state) {
        case RS_IDLE:
            if (ch == '$') {
                s->line_buf_index = 0;
                s->state = RS_GETLINE;
458
            }
bellard authored
459
            break;
460
461
462
463
464
        case RS_GETLINE:
            if (ch == '#') {
            s->state = RS_CHKSUM1;
            } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
                s->state = RS_IDLE;
bellard authored
465
            } else {
466
            s->line_buf[s->line_buf_index++] = ch;
bellard authored
467
468
            }
            break;
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
        case RS_CHKSUM1:
            s->line_buf[s->line_buf_index] = '\0';
            s->line_csum = fromhex(ch) << 4;
            s->state = RS_CHKSUM2;
            break;
        case RS_CHKSUM2:
            s->line_csum |= fromhex(ch);
            csum = 0;
            for(i = 0; i < s->line_buf_index; i++) {
                csum += s->line_buf[i];
            }
            if (s->line_csum != (csum & 0xff)) {
                reply[0] = '-';
                put_buffer(s, reply, 1);
                s->state = RS_IDLE;
bellard authored
484
            } else {
485
486
487
                reply[0] = '+';
                put_buffer(s, reply, 1);
                s->state = gdb_handle_packet(s, s->line_buf);
bellard authored
488
489
            }
            break;
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
        }
    }
}

static int gdb_can_read(void *opaque)
{
    return 256;
}

static void gdb_read(void *opaque, const uint8_t *buf, int size)
{
    GDBState *s = opaque;
    int i;
    if (size == 0) {
        /* end of connection */
        qemu_del_vm_stop_handler(gdb_vm_stopped, s);
        qemu_del_fd_read_handler(s->fd);
        qemu_free(s);
        vm_start();
    } else {
        for(i = 0; i < size; i++)
            gdb_read_byte(s, buf[i]);
    }
}

static void gdb_accept(void *opaque, const uint8_t *buf, int size)
{
    GDBState *s;
    struct sockaddr_in sockaddr;
    socklen_t len;
    int val, fd;

    for(;;) {
        len = sizeof(sockaddr);
        fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
        if (fd < 0 && errno != EINTR) {
            perror("accept");
            return;
        } else if (fd >= 0) {
bellard authored
529
530
531
            break;
        }
    }
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593

    /* set short latency */
    val = 1;
    setsockopt(fd, SOL_TCP, TCP_NODELAY, &val, sizeof(val));

    s = qemu_mallocz(sizeof(GDBState));
    if (!s) {
        close(fd);
        return;
    }
    s->fd = fd;

    fcntl(fd, F_SETFL, O_NONBLOCK);

    /* stop the VM */
    vm_stop(EXCP_INTERRUPT);

    /* start handling I/O */
    qemu_add_fd_read_handler(s->fd, gdb_can_read, gdb_read, s);
    /* when the VM is stopped, the following callback is called */
    qemu_add_vm_stop_handler(gdb_vm_stopped, s);
}

static int gdbserver_open(int port)
{
    struct sockaddr_in sockaddr;
    int fd, val, ret;

    fd = socket(PF_INET, SOCK_STREAM, 0);
    if (fd < 0) {
        perror("socket");
        return -1;
    }

    /* allow fast reuse */
    val = 1;
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));

    sockaddr.sin_family = AF_INET;
    sockaddr.sin_port = htons(port);
    sockaddr.sin_addr.s_addr = 0;
    ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
    if (ret < 0) {
        perror("bind");
        return -1;
    }
    ret = listen(fd, 0);
    if (ret < 0) {
        perror("listen");
        return -1;
    }
    fcntl(fd, F_SETFL, O_NONBLOCK);
    return fd;
}

int gdbserver_start(int port)
{
    gdbserver_fd = gdbserver_open(port);
    if (gdbserver_fd < 0)
        return -1;
    /* accept connections */
    qemu_add_fd_read_handler(gdbserver_fd, NULL, gdb_accept, NULL);
bellard authored
594
595
    return 0;
}