Statistics
| Branch: | Revision:

root / gdbstub.c @ eabfc239

History | View | Annotate | Download (53.5 kB)

1
/*
2
 * gdb server stub
3
 *
4
 * Copyright (c) 2003-2005 Fabrice Bellard
5
 *
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.
10
 *
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.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18
 */
19
#include "config.h"
20
#include "qemu-common.h"
21
#ifdef CONFIG_USER_ONLY
22
#include <stdlib.h>
23
#include <stdio.h>
24
#include <stdarg.h>
25
#include <string.h>
26
#include <errno.h>
27
#include <unistd.h>
28
#include <fcntl.h>
29

    
30
#include "qemu.h"
31
#else
32
#include "monitor/monitor.h"
33
#include "sysemu/char.h"
34
#include "sysemu/sysemu.h"
35
#include "exec/gdbstub.h"
36
#endif
37

    
38
#define MAX_PACKET_LENGTH 4096
39

    
40
#include "cpu.h"
41
#include "qemu/sockets.h"
42
#include "sysemu/kvm.h"
43
#include "qemu/bitops.h"
44

    
45
static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
46
                                         uint8_t *buf, int len, bool is_write)
47
{
48
    CPUClass *cc = CPU_GET_CLASS(cpu);
49

    
50
    if (cc->memory_rw_debug) {
51
        return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
52
    }
53
    return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
54
}
55

    
56
enum {
57
    GDB_SIGNAL_0 = 0,
58
    GDB_SIGNAL_INT = 2,
59
    GDB_SIGNAL_QUIT = 3,
60
    GDB_SIGNAL_TRAP = 5,
61
    GDB_SIGNAL_ABRT = 6,
62
    GDB_SIGNAL_ALRM = 14,
63
    GDB_SIGNAL_IO = 23,
64
    GDB_SIGNAL_XCPU = 24,
65
    GDB_SIGNAL_UNKNOWN = 143
66
};
67

    
68
#ifdef CONFIG_USER_ONLY
69

    
70
/* Map target signal numbers to GDB protocol signal numbers and vice
71
 * versa.  For user emulation's currently supported systems, we can
72
 * assume most signals are defined.
73
 */
74

    
75
static int gdb_signal_table[] = {
76
    0,
77
    TARGET_SIGHUP,
78
    TARGET_SIGINT,
79
    TARGET_SIGQUIT,
80
    TARGET_SIGILL,
81
    TARGET_SIGTRAP,
82
    TARGET_SIGABRT,
83
    -1, /* SIGEMT */
84
    TARGET_SIGFPE,
85
    TARGET_SIGKILL,
86
    TARGET_SIGBUS,
87
    TARGET_SIGSEGV,
88
    TARGET_SIGSYS,
89
    TARGET_SIGPIPE,
90
    TARGET_SIGALRM,
91
    TARGET_SIGTERM,
92
    TARGET_SIGURG,
93
    TARGET_SIGSTOP,
94
    TARGET_SIGTSTP,
95
    TARGET_SIGCONT,
96
    TARGET_SIGCHLD,
97
    TARGET_SIGTTIN,
98
    TARGET_SIGTTOU,
99
    TARGET_SIGIO,
100
    TARGET_SIGXCPU,
101
    TARGET_SIGXFSZ,
102
    TARGET_SIGVTALRM,
103
    TARGET_SIGPROF,
104
    TARGET_SIGWINCH,
105
    -1, /* SIGLOST */
106
    TARGET_SIGUSR1,
107
    TARGET_SIGUSR2,
108
#ifdef TARGET_SIGPWR
109
    TARGET_SIGPWR,
110
#else
111
    -1,
112
#endif
113
    -1, /* SIGPOLL */
114
    -1,
115
    -1,
116
    -1,
117
    -1,
118
    -1,
119
    -1,
120
    -1,
121
    -1,
122
    -1,
123
    -1,
124
    -1,
125
#ifdef __SIGRTMIN
126
    __SIGRTMIN + 1,
127
    __SIGRTMIN + 2,
128
    __SIGRTMIN + 3,
129
    __SIGRTMIN + 4,
130
    __SIGRTMIN + 5,
131
    __SIGRTMIN + 6,
132
    __SIGRTMIN + 7,
133
    __SIGRTMIN + 8,
134
    __SIGRTMIN + 9,
135
    __SIGRTMIN + 10,
136
    __SIGRTMIN + 11,
137
    __SIGRTMIN + 12,
138
    __SIGRTMIN + 13,
139
    __SIGRTMIN + 14,
140
    __SIGRTMIN + 15,
141
    __SIGRTMIN + 16,
142
    __SIGRTMIN + 17,
143
    __SIGRTMIN + 18,
144
    __SIGRTMIN + 19,
145
    __SIGRTMIN + 20,
146
    __SIGRTMIN + 21,
147
    __SIGRTMIN + 22,
148
    __SIGRTMIN + 23,
149
    __SIGRTMIN + 24,
150
    __SIGRTMIN + 25,
151
    __SIGRTMIN + 26,
152
    __SIGRTMIN + 27,
153
    __SIGRTMIN + 28,
154
    __SIGRTMIN + 29,
155
    __SIGRTMIN + 30,
156
    __SIGRTMIN + 31,
157
    -1, /* SIGCANCEL */
158
    __SIGRTMIN,
159
    __SIGRTMIN + 32,
160
    __SIGRTMIN + 33,
161
    __SIGRTMIN + 34,
162
    __SIGRTMIN + 35,
163
    __SIGRTMIN + 36,
164
    __SIGRTMIN + 37,
165
    __SIGRTMIN + 38,
166
    __SIGRTMIN + 39,
167
    __SIGRTMIN + 40,
168
    __SIGRTMIN + 41,
169
    __SIGRTMIN + 42,
170
    __SIGRTMIN + 43,
171
    __SIGRTMIN + 44,
172
    __SIGRTMIN + 45,
173
    __SIGRTMIN + 46,
174
    __SIGRTMIN + 47,
175
    __SIGRTMIN + 48,
176
    __SIGRTMIN + 49,
177
    __SIGRTMIN + 50,
178
    __SIGRTMIN + 51,
179
    __SIGRTMIN + 52,
180
    __SIGRTMIN + 53,
181
    __SIGRTMIN + 54,
182
    __SIGRTMIN + 55,
183
    __SIGRTMIN + 56,
184
    __SIGRTMIN + 57,
185
    __SIGRTMIN + 58,
186
    __SIGRTMIN + 59,
187
    __SIGRTMIN + 60,
188
    __SIGRTMIN + 61,
189
    __SIGRTMIN + 62,
190
    __SIGRTMIN + 63,
191
    __SIGRTMIN + 64,
192
    __SIGRTMIN + 65,
193
    __SIGRTMIN + 66,
194
    __SIGRTMIN + 67,
195
    __SIGRTMIN + 68,
196
    __SIGRTMIN + 69,
197
    __SIGRTMIN + 70,
198
    __SIGRTMIN + 71,
199
    __SIGRTMIN + 72,
200
    __SIGRTMIN + 73,
201
    __SIGRTMIN + 74,
202
    __SIGRTMIN + 75,
203
    __SIGRTMIN + 76,
204
    __SIGRTMIN + 77,
205
    __SIGRTMIN + 78,
206
    __SIGRTMIN + 79,
207
    __SIGRTMIN + 80,
208
    __SIGRTMIN + 81,
209
    __SIGRTMIN + 82,
210
    __SIGRTMIN + 83,
211
    __SIGRTMIN + 84,
212
    __SIGRTMIN + 85,
213
    __SIGRTMIN + 86,
214
    __SIGRTMIN + 87,
215
    __SIGRTMIN + 88,
216
    __SIGRTMIN + 89,
217
    __SIGRTMIN + 90,
218
    __SIGRTMIN + 91,
219
    __SIGRTMIN + 92,
220
    __SIGRTMIN + 93,
221
    __SIGRTMIN + 94,
222
    __SIGRTMIN + 95,
223
    -1, /* SIGINFO */
224
    -1, /* UNKNOWN */
225
    -1, /* DEFAULT */
226
    -1,
227
    -1,
228
    -1,
229
    -1,
230
    -1,
231
    -1
232
#endif
233
};
234
#else
235
/* In system mode we only need SIGINT and SIGTRAP; other signals
236
   are not yet supported.  */
237

    
238
enum {
239
    TARGET_SIGINT = 2,
240
    TARGET_SIGTRAP = 5
241
};
242

    
243
static int gdb_signal_table[] = {
244
    -1,
245
    -1,
246
    TARGET_SIGINT,
247
    -1,
248
    -1,
249
    TARGET_SIGTRAP
250
};
251
#endif
252

    
253
#ifdef CONFIG_USER_ONLY
254
static int target_signal_to_gdb (int sig)
255
{
256
    int i;
257
    for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
258
        if (gdb_signal_table[i] == sig)
259
            return i;
260
    return GDB_SIGNAL_UNKNOWN;
261
}
262
#endif
263

    
264
static int gdb_signal_to_target (int sig)
265
{
266
    if (sig < ARRAY_SIZE (gdb_signal_table))
267
        return gdb_signal_table[sig];
268
    else
269
        return -1;
270
}
271

    
272
//#define DEBUG_GDB
273

    
274
typedef struct GDBRegisterState {
275
    int base_reg;
276
    int num_regs;
277
    gdb_reg_cb get_reg;
278
    gdb_reg_cb set_reg;
279
    const char *xml;
280
    struct GDBRegisterState *next;
281
} GDBRegisterState;
282

    
283
enum RSState {
284
    RS_INACTIVE,
285
    RS_IDLE,
286
    RS_GETLINE,
287
    RS_CHKSUM1,
288
    RS_CHKSUM2,
289
};
290
typedef struct GDBState {
291
    CPUState *c_cpu; /* current CPU for step/continue ops */
292
    CPUState *g_cpu; /* current CPU for other ops */
293
    CPUState *query_cpu; /* for q{f|s}ThreadInfo */
294
    enum RSState state; /* parsing state */
295
    char line_buf[MAX_PACKET_LENGTH];
296
    int line_buf_index;
297
    int line_csum;
298
    uint8_t last_packet[MAX_PACKET_LENGTH + 4];
299
    int last_packet_len;
300
    int signal;
301
#ifdef CONFIG_USER_ONLY
302
    int fd;
303
    int running_state;
304
#else
305
    CharDriverState *chr;
306
    CharDriverState *mon_chr;
307
#endif
308
    char syscall_buf[256];
309
    gdb_syscall_complete_cb current_syscall_cb;
310
} GDBState;
311

    
312
/* By default use no IRQs and no timers while single stepping so as to
313
 * make single stepping like an ICE HW step.
314
 */
315
static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
316

    
317
static GDBState *gdbserver_state;
318

    
319
/* This is an ugly hack to cope with both new and old gdb.
320
   If gdb sends qXfer:features:read then assume we're talking to a newish
321
   gdb that understands target descriptions.  */
322
static int gdb_has_xml;
323

    
324
#ifdef CONFIG_USER_ONLY
325
/* XXX: This is not thread safe.  Do we care?  */
326
static int gdbserver_fd = -1;
327

    
328
static int get_char(GDBState *s)
329
{
330
    uint8_t ch;
331
    int ret;
332

    
333
    for(;;) {
334
        ret = qemu_recv(s->fd, &ch, 1, 0);
335
        if (ret < 0) {
336
            if (errno == ECONNRESET)
337
                s->fd = -1;
338
            if (errno != EINTR && errno != EAGAIN)
339
                return -1;
340
        } else if (ret == 0) {
341
            close(s->fd);
342
            s->fd = -1;
343
            return -1;
344
        } else {
345
            break;
346
        }
347
    }
348
    return ch;
349
}
350
#endif
351

    
352
static enum {
353
    GDB_SYS_UNKNOWN,
354
    GDB_SYS_ENABLED,
355
    GDB_SYS_DISABLED,
356
} gdb_syscall_mode;
357

    
358
/* If gdb is connected when the first semihosting syscall occurs then use
359
   remote gdb syscalls.  Otherwise use native file IO.  */
360
int use_gdb_syscalls(void)
361
{
362
    if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
363
        gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
364
                                            : GDB_SYS_DISABLED);
365
    }
366
    return gdb_syscall_mode == GDB_SYS_ENABLED;
367
}
368

    
369
/* Resume execution.  */
370
static inline void gdb_continue(GDBState *s)
371
{
372
#ifdef CONFIG_USER_ONLY
373
    s->running_state = 1;
374
#else
375
    if (runstate_check(RUN_STATE_GUEST_PANICKED)) {
376
        runstate_set(RUN_STATE_DEBUG);
377
    }
378
    if (!runstate_needs_reset()) {
379
        vm_start();
380
    }
381
#endif
382
}
383

    
384
static void put_buffer(GDBState *s, const uint8_t *buf, int len)
385
{
386
#ifdef CONFIG_USER_ONLY
387
    int ret;
388

    
389
    while (len > 0) {
390
        ret = send(s->fd, buf, len, 0);
391
        if (ret < 0) {
392
            if (errno != EINTR && errno != EAGAIN)
393
                return;
394
        } else {
395
            buf += ret;
396
            len -= ret;
397
        }
398
    }
399
#else
400
    qemu_chr_fe_write(s->chr, buf, len);
401
#endif
402
}
403

    
404
static inline int fromhex(int v)
405
{
406
    if (v >= '0' && v <= '9')
407
        return v - '0';
408
    else if (v >= 'A' && v <= 'F')
409
        return v - 'A' + 10;
410
    else if (v >= 'a' && v <= 'f')
411
        return v - 'a' + 10;
412
    else
413
        return 0;
414
}
415

    
416
static inline int tohex(int v)
417
{
418
    if (v < 10)
419
        return v + '0';
420
    else
421
        return v - 10 + 'a';
422
}
423

    
424
static void memtohex(char *buf, const uint8_t *mem, int len)
425
{
426
    int i, c;
427
    char *q;
428
    q = buf;
429
    for(i = 0; i < len; i++) {
430
        c = mem[i];
431
        *q++ = tohex(c >> 4);
432
        *q++ = tohex(c & 0xf);
433
    }
434
    *q = '\0';
435
}
436

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

    
441
    for(i = 0; i < len; i++) {
442
        mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
443
        buf += 2;
444
    }
445
}
446

    
447
/* return -1 if error, 0 if OK */
448
static int put_packet_binary(GDBState *s, const char *buf, int len)
449
{
450
    int csum, i;
451
    uint8_t *p;
452

    
453
    for(;;) {
454
        p = s->last_packet;
455
        *(p++) = '$';
456
        memcpy(p, buf, len);
457
        p += len;
458
        csum = 0;
459
        for(i = 0; i < len; i++) {
460
            csum += buf[i];
461
        }
462
        *(p++) = '#';
463
        *(p++) = tohex((csum >> 4) & 0xf);
464
        *(p++) = tohex((csum) & 0xf);
465

    
466
        s->last_packet_len = p - s->last_packet;
467
        put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
468

    
469
#ifdef CONFIG_USER_ONLY
470
        i = get_char(s);
471
        if (i < 0)
472
            return -1;
473
        if (i == '+')
474
            break;
475
#else
476
        break;
477
#endif
478
    }
479
    return 0;
480
}
481

    
482
/* return -1 if error, 0 if OK */
483
static int put_packet(GDBState *s, const char *buf)
484
{
485
#ifdef DEBUG_GDB
486
    printf("reply='%s'\n", buf);
487
#endif
488

    
489
    return put_packet_binary(s, buf, strlen(buf));
490
}
491

    
492
/* The GDB remote protocol transfers values in target byte order.  This means
493
   we can use the raw memory access routines to access the value buffer.
494
   Conveniently, these also handle the case where the buffer is mis-aligned.
495
 */
496
#define GET_REG8(val) do { \
497
    stb_p(mem_buf, val); \
498
    return 1; \
499
    } while(0)
500
#define GET_REG16(val) do { \
501
    stw_p(mem_buf, val); \
502
    return 2; \
503
    } while(0)
504
#define GET_REG32(val) do { \
505
    stl_p(mem_buf, val); \
506
    return 4; \
507
    } while(0)
508
#define GET_REG64(val) do { \
509
    stq_p(mem_buf, val); \
510
    return 8; \
511
    } while(0)
512

    
513
#if TARGET_LONG_BITS == 64
514
#define GET_REGL(val) GET_REG64(val)
515
#define ldtul_p(addr) ldq_p(addr)
516
#else
517
#define GET_REGL(val) GET_REG32(val)
518
#define ldtul_p(addr) ldl_p(addr)
519
#endif
520

    
521
#if defined(TARGET_I386)
522

    
523
#include "target-i386/gdbstub.c"
524

    
525
#elif defined (TARGET_PPC)
526

    
527
#if defined (TARGET_PPC64)
528
#define GDB_CORE_XML "power64-core.xml"
529
#else
530
#define GDB_CORE_XML "power-core.xml"
531
#endif
532

    
533
#include "target-ppc/gdbstub.c"
534

    
535
#elif defined (TARGET_SPARC)
536

    
537
#include "target-sparc/gdbstub.c"
538

    
539
#elif defined (TARGET_ARM)
540

    
541
#define GDB_CORE_XML "arm-core.xml"
542

    
543
#include "target-arm/gdbstub.c"
544

    
545
#elif defined (TARGET_M68K)
546

    
547
#define GDB_CORE_XML "cf-core.xml"
548

    
549
#include "target-m68k/gdbstub.c"
550

    
551
#elif defined (TARGET_MIPS)
552

    
553
#include "target-mips/gdbstub.c"
554

    
555
#elif defined(TARGET_OPENRISC)
556

    
557
#include "target-openrisc/gdbstub.c"
558

    
559
#elif defined (TARGET_SH4)
560

    
561
#include "target-sh4/gdbstub.c"
562

    
563
#elif defined (TARGET_MICROBLAZE)
564

    
565
#include "target-microblaze/gdbstub.c"
566

    
567
#elif defined (TARGET_CRIS)
568

    
569
static int
570
read_register_crisv10(CPUCRISState *env, uint8_t *mem_buf, int n)
571
{
572
    if (n < 15) {
573
        GET_REG32(env->regs[n]);
574
    }
575

    
576
    if (n == 15) {
577
        GET_REG32(env->pc);
578
    }
579

    
580
    if (n < 32) {
581
        switch (n) {
582
        case 16:
583
            GET_REG8(env->pregs[n - 16]);
584
        case 17:
585
            GET_REG8(env->pregs[n - 16]);
586
        case 20:
587
        case 21:
588
            GET_REG16(env->pregs[n - 16]);
589
        default:
590
            if (n >= 23) {
591
                GET_REG32(env->pregs[n - 16]);
592
            }
593
            break;
594
        }
595
    }
596
    return 0;
597
}
598

    
599
static int cpu_gdb_read_register(CPUCRISState *env, uint8_t *mem_buf, int n)
600
{
601
    uint8_t srs;
602

    
603
    if (env->pregs[PR_VR] < 32) {
604
        return read_register_crisv10(env, mem_buf, n);
605
    }
606

    
607
    srs = env->pregs[PR_SRS];
608
    if (n < 16) {
609
        GET_REG32(env->regs[n]);
610
    }
611

    
612
    if (n >= 21 && n < 32) {
613
        GET_REG32(env->pregs[n - 16]);
614
    }
615
    if (n >= 33 && n < 49) {
616
        GET_REG32(env->sregs[srs][n - 33]);
617
    }
618
    switch (n) {
619
    case 16:
620
        GET_REG8(env->pregs[0]);
621
    case 17:
622
        GET_REG8(env->pregs[1]);
623
    case 18:
624
        GET_REG32(env->pregs[2]);
625
    case 19:
626
        GET_REG8(srs);
627
    case 20:
628
        GET_REG16(env->pregs[4]);
629
    case 32:
630
        GET_REG32(env->pc);
631
    }
632

    
633
    return 0;
634
}
635

    
636
static int cpu_gdb_write_register(CPUCRISState *env, uint8_t *mem_buf, int n)
637
{
638
    uint32_t tmp;
639

    
640
    if (n > 49) {
641
        return 0;
642
    }
643

    
644
    tmp = ldl_p(mem_buf);
645

    
646
    if (n < 16) {
647
        env->regs[n] = tmp;
648
    }
649

    
650
    if (n >= 21 && n < 32) {
651
        env->pregs[n - 16] = tmp;
652
    }
653

    
654
    /* FIXME: Should support function regs be writable?  */
655
    switch (n) {
656
    case 16:
657
        return 1;
658
    case 17:
659
        return 1;
660
    case 18:
661
        env->pregs[PR_PID] = tmp;
662
        break;
663
    case 19:
664
        return 1;
665
    case 20:
666
        return 2;
667
    case 32:
668
        env->pc = tmp;
669
        break;
670
    }
671

    
672
    return 4;
673
}
674
#elif defined (TARGET_ALPHA)
675

    
676
static int cpu_gdb_read_register(CPUAlphaState *env, uint8_t *mem_buf, int n)
677
{
678
    uint64_t val;
679
    CPU_DoubleU d;
680

    
681
    switch (n) {
682
    case 0 ... 30:
683
        val = env->ir[n];
684
        break;
685
    case 32 ... 62:
686
        d.d = env->fir[n - 32];
687
        val = d.ll;
688
        break;
689
    case 63:
690
        val = cpu_alpha_load_fpcr(env);
691
        break;
692
    case 64:
693
        val = env->pc;
694
        break;
695
    case 66:
696
        val = env->unique;
697
        break;
698
    case 31:
699
    case 65:
700
        /* 31 really is the zero register; 65 is unassigned in the
701
           gdb protocol, but is still required to occupy 8 bytes. */
702
        val = 0;
703
        break;
704
    default:
705
        return 0;
706
    }
707
    GET_REGL(val);
708
}
709

    
710
static int cpu_gdb_write_register(CPUAlphaState *env, uint8_t *mem_buf, int n)
711
{
712
    target_ulong tmp = ldtul_p(mem_buf);
713
    CPU_DoubleU d;
714

    
715
    switch (n) {
716
    case 0 ... 30:
717
        env->ir[n] = tmp;
718
        break;
719
    case 32 ... 62:
720
        d.ll = tmp;
721
        env->fir[n - 32] = d.d;
722
        break;
723
    case 63:
724
        cpu_alpha_store_fpcr(env, tmp);
725
        break;
726
    case 64:
727
        env->pc = tmp;
728
        break;
729
    case 66:
730
        env->unique = tmp;
731
        break;
732
    case 31:
733
    case 65:
734
        /* 31 really is the zero register; 65 is unassigned in the
735
           gdb protocol, but is still required to occupy 8 bytes. */
736
        break;
737
    default:
738
        return 0;
739
    }
740
    return 8;
741
}
742
#elif defined (TARGET_S390X)
743

    
744
static int cpu_gdb_read_register(CPUS390XState *env, uint8_t *mem_buf, int n)
745
{
746
    uint64_t val;
747
    int cc_op;
748

    
749
    switch (n) {
750
    case S390_PSWM_REGNUM:
751
        cc_op = calc_cc(env, env->cc_op, env->cc_src, env->cc_dst, env->cc_vr);
752
        val = deposit64(env->psw.mask, 44, 2, cc_op);
753
        GET_REGL(val);
754
    case S390_PSWA_REGNUM:
755
        GET_REGL(env->psw.addr);
756
    case S390_R0_REGNUM ... S390_R15_REGNUM:
757
        GET_REGL(env->regs[n-S390_R0_REGNUM]);
758
    case S390_A0_REGNUM ... S390_A15_REGNUM:
759
        GET_REG32(env->aregs[n-S390_A0_REGNUM]);
760
    case S390_FPC_REGNUM:
761
        GET_REG32(env->fpc);
762
    case S390_F0_REGNUM ... S390_F15_REGNUM:
763
        GET_REG64(env->fregs[n-S390_F0_REGNUM].ll);
764
    }
765

    
766
    return 0;
767
}
768

    
769
static int cpu_gdb_write_register(CPUS390XState *env, uint8_t *mem_buf, int n)
770
{
771
    target_ulong tmpl;
772
    uint32_t tmp32;
773
    int r = 8;
774
    tmpl = ldtul_p(mem_buf);
775
    tmp32 = ldl_p(mem_buf);
776

    
777
    switch (n) {
778
    case S390_PSWM_REGNUM:
779
        env->psw.mask = tmpl;
780
        env->cc_op = extract64(tmpl, 44, 2);
781
        break;
782
    case S390_PSWA_REGNUM:
783
        env->psw.addr = tmpl;
784
        break;
785
    case S390_R0_REGNUM ... S390_R15_REGNUM:
786
        env->regs[n-S390_R0_REGNUM] = tmpl;
787
        break;
788
    case S390_A0_REGNUM ... S390_A15_REGNUM:
789
        env->aregs[n-S390_A0_REGNUM] = tmp32;
790
        r = 4;
791
        break;
792
    case S390_FPC_REGNUM:
793
        env->fpc = tmp32;
794
        r = 4;
795
        break;
796
    case S390_F0_REGNUM ... S390_F15_REGNUM:
797
        env->fregs[n-S390_F0_REGNUM].ll = tmpl;
798
        break;
799
    default:
800
        return 0;
801
    }
802
    return r;
803
}
804
#elif defined (TARGET_LM32)
805

    
806
#include "hw/lm32/lm32_pic.h"
807

    
808
static int cpu_gdb_read_register(CPULM32State *env, uint8_t *mem_buf, int n)
809
{
810
    if (n < 32) {
811
        GET_REG32(env->regs[n]);
812
    } else {
813
        switch (n) {
814
        case 32:
815
            GET_REG32(env->pc);
816
        /* FIXME: put in right exception ID */
817
        case 33:
818
            GET_REG32(0);
819
        case 34:
820
            GET_REG32(env->eba);
821
        case 35:
822
            GET_REG32(env->deba);
823
        case 36:
824
            GET_REG32(env->ie);
825
        case 37:
826
            GET_REG32(lm32_pic_get_im(env->pic_state));
827
        case 38:
828
            GET_REG32(lm32_pic_get_ip(env->pic_state));
829
        }
830
    }
831
    return 0;
832
}
833

    
834
static int cpu_gdb_write_register(CPULM32State *env, uint8_t *mem_buf, int n)
835
{
836
    LM32CPU *cpu = lm32_env_get_cpu(env);
837
    CPUClass *cc = CPU_GET_CLASS(cpu);
838
    uint32_t tmp;
839

    
840
    if (n > cc->gdb_num_core_regs) {
841
        return 0;
842
    }
843

    
844
    tmp = ldl_p(mem_buf);
845

    
846
    if (n < 32) {
847
        env->regs[n] = tmp;
848
    } else {
849
        switch (n) {
850
        case 32:
851
            env->pc = tmp;
852
            break;
853
        case 34:
854
            env->eba = tmp;
855
            break;
856
        case 35:
857
            env->deba = tmp;
858
            break;
859
        case 36:
860
            env->ie = tmp;
861
            break;
862
        case 37:
863
            lm32_pic_set_im(env->pic_state, tmp);
864
            break;
865
        case 38:
866
            lm32_pic_set_ip(env->pic_state, tmp);
867
            break;
868
        }
869
    }
870
    return 4;
871
}
872
#elif defined(TARGET_XTENSA)
873

    
874
static int cpu_gdb_read_register(CPUXtensaState *env, uint8_t *mem_buf, int n)
875
{
876
    const XtensaGdbReg *reg = env->config->gdb_regmap.reg + n;
877

    
878
    if (n < 0 || n >= env->config->gdb_regmap.num_regs) {
879
        return 0;
880
    }
881

    
882
    switch (reg->type) {
883
    case 9: /*pc*/
884
        GET_REG32(env->pc);
885

    
886
    case 1: /*ar*/
887
        xtensa_sync_phys_from_window(env);
888
        GET_REG32(env->phys_regs[(reg->targno & 0xff) % env->config->nareg]);
889

    
890
    case 2: /*SR*/
891
        GET_REG32(env->sregs[reg->targno & 0xff]);
892

    
893
    case 3: /*UR*/
894
        GET_REG32(env->uregs[reg->targno & 0xff]);
895

    
896
    case 4: /*f*/
897
        GET_REG32(float32_val(env->fregs[reg->targno & 0x0f]));
898

    
899
    case 8: /*a*/
900
        GET_REG32(env->regs[reg->targno & 0x0f]);
901

    
902
    default:
903
        qemu_log("%s from reg %d of unsupported type %d\n",
904
                 __func__, n, reg->type);
905
        return 0;
906
    }
907
}
908

    
909
static int cpu_gdb_write_register(CPUXtensaState *env, uint8_t *mem_buf, int n)
910
{
911
    uint32_t tmp;
912
    const XtensaGdbReg *reg = env->config->gdb_regmap.reg + n;
913

    
914
    if (n < 0 || n >= env->config->gdb_regmap.num_regs) {
915
        return 0;
916
    }
917

    
918
    tmp = ldl_p(mem_buf);
919

    
920
    switch (reg->type) {
921
    case 9: /*pc*/
922
        env->pc = tmp;
923
        break;
924

    
925
    case 1: /*ar*/
926
        env->phys_regs[(reg->targno & 0xff) % env->config->nareg] = tmp;
927
        xtensa_sync_window_from_phys(env);
928
        break;
929

    
930
    case 2: /*SR*/
931
        env->sregs[reg->targno & 0xff] = tmp;
932
        break;
933

    
934
    case 3: /*UR*/
935
        env->uregs[reg->targno & 0xff] = tmp;
936
        break;
937

    
938
    case 4: /*f*/
939
        env->fregs[reg->targno & 0x0f] = make_float32(tmp);
940
        break;
941

    
942
    case 8: /*a*/
943
        env->regs[reg->targno & 0x0f] = tmp;
944
        break;
945

    
946
    default:
947
        qemu_log("%s to reg %d of unsupported type %d\n",
948
                 __func__, n, reg->type);
949
        return 0;
950
    }
951

    
952
    return 4;
953
}
954
#else
955

    
956
static int cpu_gdb_read_register(CPUArchState *env, uint8_t *mem_buf, int n)
957
{
958
    return 0;
959
}
960

    
961
static int cpu_gdb_write_register(CPUArchState *env, uint8_t *mem_buf, int n)
962
{
963
    return 0;
964
}
965

    
966
#endif
967

    
968
#ifdef GDB_CORE_XML
969
/* Encode data using the encoding for 'x' packets.  */
970
static int memtox(char *buf, const char *mem, int len)
971
{
972
    char *p = buf;
973
    char c;
974

    
975
    while (len--) {
976
        c = *(mem++);
977
        switch (c) {
978
        case '#': case '$': case '*': case '}':
979
            *(p++) = '}';
980
            *(p++) = c ^ 0x20;
981
            break;
982
        default:
983
            *(p++) = c;
984
            break;
985
        }
986
    }
987
    return p - buf;
988
}
989

    
990
static const char *get_feature_xml(const char *p, const char **newp)
991
{
992
    size_t len;
993
    int i;
994
    const char *name;
995
    static char target_xml[1024];
996

    
997
    len = 0;
998
    while (p[len] && p[len] != ':')
999
        len++;
1000
    *newp = p + len;
1001

    
1002
    name = NULL;
1003
    if (strncmp(p, "target.xml", len) == 0) {
1004
        /* Generate the XML description for this CPU.  */
1005
        if (!target_xml[0]) {
1006
            GDBRegisterState *r;
1007
            CPUState *cpu = first_cpu;
1008

    
1009
            snprintf(target_xml, sizeof(target_xml),
1010
                     "<?xml version=\"1.0\"?>"
1011
                     "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
1012
                     "<target>"
1013
                     "<xi:include href=\"%s\"/>",
1014
                     GDB_CORE_XML);
1015

    
1016
            for (r = cpu->gdb_regs; r; r = r->next) {
1017
                pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
1018
                pstrcat(target_xml, sizeof(target_xml), r->xml);
1019
                pstrcat(target_xml, sizeof(target_xml), "\"/>");
1020
            }
1021
            pstrcat(target_xml, sizeof(target_xml), "</target>");
1022
        }
1023
        return target_xml;
1024
    }
1025
    for (i = 0; ; i++) {
1026
        name = xml_builtin[i][0];
1027
        if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
1028
            break;
1029
    }
1030
    return name ? xml_builtin[i][1] : NULL;
1031
}
1032
#endif
1033

    
1034
static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
1035
{
1036
    CPUClass *cc = CPU_GET_CLASS(cpu);
1037
    CPUArchState *env = cpu->env_ptr;
1038
    GDBRegisterState *r;
1039

    
1040
    if (reg < cc->gdb_num_core_regs) {
1041
        return cpu_gdb_read_register(env, mem_buf, reg);
1042
    }
1043

    
1044
    for (r = cpu->gdb_regs; r; r = r->next) {
1045
        if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
1046
            return r->get_reg(env, mem_buf, reg - r->base_reg);
1047
        }
1048
    }
1049
    return 0;
1050
}
1051

    
1052
static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
1053
{
1054
    CPUClass *cc = CPU_GET_CLASS(cpu);
1055
    CPUArchState *env = cpu->env_ptr;
1056
    GDBRegisterState *r;
1057

    
1058
    if (reg < cc->gdb_num_core_regs) {
1059
        return cpu_gdb_write_register(env, mem_buf, reg);
1060
    }
1061

    
1062
    for (r = cpu->gdb_regs; r; r = r->next) {
1063
        if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
1064
            return r->set_reg(env, mem_buf, reg - r->base_reg);
1065
        }
1066
    }
1067
    return 0;
1068
}
1069

    
1070
/* Register a supplemental set of CPU registers.  If g_pos is nonzero it
1071
   specifies the first register number and these registers are included in
1072
   a standard "g" packet.  Direction is relative to gdb, i.e. get_reg is
1073
   gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
1074
 */
1075

    
1076
void gdb_register_coprocessor(CPUState *cpu,
1077
                              gdb_reg_cb get_reg, gdb_reg_cb set_reg,
1078
                              int num_regs, const char *xml, int g_pos)
1079
{
1080
    GDBRegisterState *s;
1081
    GDBRegisterState **p;
1082

    
1083
    p = &cpu->gdb_regs;
1084
    while (*p) {
1085
        /* Check for duplicates.  */
1086
        if (strcmp((*p)->xml, xml) == 0)
1087
            return;
1088
        p = &(*p)->next;
1089
    }
1090

    
1091
    s = g_new0(GDBRegisterState, 1);
1092
    s->base_reg = cpu->gdb_num_regs;
1093
    s->num_regs = num_regs;
1094
    s->get_reg = get_reg;
1095
    s->set_reg = set_reg;
1096
    s->xml = xml;
1097

    
1098
    /* Add to end of list.  */
1099
    cpu->gdb_num_regs += num_regs;
1100
    *p = s;
1101
    if (g_pos) {
1102
        if (g_pos != s->base_reg) {
1103
            fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
1104
                    "Expected %d got %d\n", xml, g_pos, s->base_reg);
1105
        }
1106
    }
1107
}
1108

    
1109
#ifndef CONFIG_USER_ONLY
1110
static const int xlat_gdb_type[] = {
1111
    [GDB_WATCHPOINT_WRITE]  = BP_GDB | BP_MEM_WRITE,
1112
    [GDB_WATCHPOINT_READ]   = BP_GDB | BP_MEM_READ,
1113
    [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
1114
};
1115
#endif
1116

    
1117
static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
1118
{
1119
    CPUState *cpu;
1120
    CPUArchState *env;
1121
    int err = 0;
1122

    
1123
    if (kvm_enabled()) {
1124
        return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
1125
    }
1126

    
1127
    switch (type) {
1128
    case GDB_BREAKPOINT_SW:
1129
    case GDB_BREAKPOINT_HW:
1130
        for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
1131
            env = cpu->env_ptr;
1132
            err = cpu_breakpoint_insert(env, addr, BP_GDB, NULL);
1133
            if (err)
1134
                break;
1135
        }
1136
        return err;
1137
#ifndef CONFIG_USER_ONLY
1138
    case GDB_WATCHPOINT_WRITE:
1139
    case GDB_WATCHPOINT_READ:
1140
    case GDB_WATCHPOINT_ACCESS:
1141
        for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
1142
            env = cpu->env_ptr;
1143
            err = cpu_watchpoint_insert(env, addr, len, xlat_gdb_type[type],
1144
                                        NULL);
1145
            if (err)
1146
                break;
1147
        }
1148
        return err;
1149
#endif
1150
    default:
1151
        return -ENOSYS;
1152
    }
1153
}
1154

    
1155
static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
1156
{
1157
    CPUState *cpu;
1158
    CPUArchState *env;
1159
    int err = 0;
1160

    
1161
    if (kvm_enabled()) {
1162
        return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
1163
    }
1164

    
1165
    switch (type) {
1166
    case GDB_BREAKPOINT_SW:
1167
    case GDB_BREAKPOINT_HW:
1168
        for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
1169
            env = cpu->env_ptr;
1170
            err = cpu_breakpoint_remove(env, addr, BP_GDB);
1171
            if (err)
1172
                break;
1173
        }
1174
        return err;
1175
#ifndef CONFIG_USER_ONLY
1176
    case GDB_WATCHPOINT_WRITE:
1177
    case GDB_WATCHPOINT_READ:
1178
    case GDB_WATCHPOINT_ACCESS:
1179
        for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
1180
            env = cpu->env_ptr;
1181
            err = cpu_watchpoint_remove(env, addr, len, xlat_gdb_type[type]);
1182
            if (err)
1183
                break;
1184
        }
1185
        return err;
1186
#endif
1187
    default:
1188
        return -ENOSYS;
1189
    }
1190
}
1191

    
1192
static void gdb_breakpoint_remove_all(void)
1193
{
1194
    CPUState *cpu;
1195
    CPUArchState *env;
1196

    
1197
    if (kvm_enabled()) {
1198
        kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
1199
        return;
1200
    }
1201

    
1202
    for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
1203
        env = cpu->env_ptr;
1204
        cpu_breakpoint_remove_all(env, BP_GDB);
1205
#ifndef CONFIG_USER_ONLY
1206
        cpu_watchpoint_remove_all(env, BP_GDB);
1207
#endif
1208
    }
1209
}
1210

    
1211
static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
1212
{
1213
    CPUState *cpu = s->c_cpu;
1214
    CPUClass *cc = CPU_GET_CLASS(cpu);
1215

    
1216
    cpu_synchronize_state(cpu);
1217
    if (cc->set_pc) {
1218
        cc->set_pc(cpu, pc);
1219
    }
1220
}
1221

    
1222
static CPUState *find_cpu(uint32_t thread_id)
1223
{
1224
    CPUState *cpu;
1225

    
1226
    for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
1227
        if (cpu_index(cpu) == thread_id) {
1228
            return cpu;
1229
        }
1230
    }
1231

    
1232
    return NULL;
1233
}
1234

    
1235
static int gdb_handle_packet(GDBState *s, const char *line_buf)
1236
{
1237
    CPUState *cpu;
1238
    const char *p;
1239
    uint32_t thread;
1240
    int ch, reg_size, type, res;
1241
    char buf[MAX_PACKET_LENGTH];
1242
    uint8_t mem_buf[MAX_PACKET_LENGTH];
1243
    uint8_t *registers;
1244
    target_ulong addr, len;
1245

    
1246
#ifdef DEBUG_GDB
1247
    printf("command='%s'\n", line_buf);
1248
#endif
1249
    p = line_buf;
1250
    ch = *p++;
1251
    switch(ch) {
1252
    case '?':
1253
        /* TODO: Make this return the correct value for user-mode.  */
1254
        snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
1255
                 cpu_index(s->c_cpu));
1256
        put_packet(s, buf);
1257
        /* Remove all the breakpoints when this query is issued,
1258
         * because gdb is doing and initial connect and the state
1259
         * should be cleaned up.
1260
         */
1261
        gdb_breakpoint_remove_all();
1262
        break;
1263
    case 'c':
1264
        if (*p != '\0') {
1265
            addr = strtoull(p, (char **)&p, 16);
1266
            gdb_set_cpu_pc(s, addr);
1267
        }
1268
        s->signal = 0;
1269
        gdb_continue(s);
1270
        return RS_IDLE;
1271
    case 'C':
1272
        s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
1273
        if (s->signal == -1)
1274
            s->signal = 0;
1275
        gdb_continue(s);
1276
        return RS_IDLE;
1277
    case 'v':
1278
        if (strncmp(p, "Cont", 4) == 0) {
1279
            int res_signal, res_thread;
1280

    
1281
            p += 4;
1282
            if (*p == '?') {
1283
                put_packet(s, "vCont;c;C;s;S");
1284
                break;
1285
            }
1286
            res = 0;
1287
            res_signal = 0;
1288
            res_thread = 0;
1289
            while (*p) {
1290
                int action, signal;
1291

    
1292
                if (*p++ != ';') {
1293
                    res = 0;
1294
                    break;
1295
                }
1296
                action = *p++;
1297
                signal = 0;
1298
                if (action == 'C' || action == 'S') {
1299
                    signal = strtoul(p, (char **)&p, 16);
1300
                } else if (action != 'c' && action != 's') {
1301
                    res = 0;
1302
                    break;
1303
                }
1304
                thread = 0;
1305
                if (*p == ':') {
1306
                    thread = strtoull(p+1, (char **)&p, 16);
1307
                }
1308
                action = tolower(action);
1309
                if (res == 0 || (res == 'c' && action == 's')) {
1310
                    res = action;
1311
                    res_signal = signal;
1312
                    res_thread = thread;
1313
                }
1314
            }
1315
            if (res) {
1316
                if (res_thread != -1 && res_thread != 0) {
1317
                    cpu = find_cpu(res_thread);
1318
                    if (cpu == NULL) {
1319
                        put_packet(s, "E22");
1320
                        break;
1321
                    }
1322
                    s->c_cpu = cpu;
1323
                }
1324
                if (res == 's') {
1325
                    cpu_single_step(s->c_cpu, sstep_flags);
1326
                }
1327
                s->signal = res_signal;
1328
                gdb_continue(s);
1329
                return RS_IDLE;
1330
            }
1331
            break;
1332
        } else {
1333
            goto unknown_command;
1334
        }
1335
    case 'k':
1336
#ifdef CONFIG_USER_ONLY
1337
        /* Kill the target */
1338
        fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
1339
        exit(0);
1340
#endif
1341
    case 'D':
1342
        /* Detach packet */
1343
        gdb_breakpoint_remove_all();
1344
        gdb_syscall_mode = GDB_SYS_DISABLED;
1345
        gdb_continue(s);
1346
        put_packet(s, "OK");
1347
        break;
1348
    case 's':
1349
        if (*p != '\0') {
1350
            addr = strtoull(p, (char **)&p, 16);
1351
            gdb_set_cpu_pc(s, addr);
1352
        }
1353
        cpu_single_step(s->c_cpu, sstep_flags);
1354
        gdb_continue(s);
1355
        return RS_IDLE;
1356
    case 'F':
1357
        {
1358
            target_ulong ret;
1359
            target_ulong err;
1360

    
1361
            ret = strtoull(p, (char **)&p, 16);
1362
            if (*p == ',') {
1363
                p++;
1364
                err = strtoull(p, (char **)&p, 16);
1365
            } else {
1366
                err = 0;
1367
            }
1368
            if (*p == ',')
1369
                p++;
1370
            type = *p;
1371
            if (s->current_syscall_cb) {
1372
                s->current_syscall_cb(s->c_cpu, ret, err);
1373
                s->current_syscall_cb = NULL;
1374
            }
1375
            if (type == 'C') {
1376
                put_packet(s, "T02");
1377
            } else {
1378
                gdb_continue(s);
1379
            }
1380
        }
1381
        break;
1382
    case 'g':
1383
        cpu_synchronize_state(s->g_cpu);
1384
        len = 0;
1385
        for (addr = 0; addr < s->g_cpu->gdb_num_regs; addr++) {
1386
            reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
1387
            len += reg_size;
1388
        }
1389
        memtohex(buf, mem_buf, len);
1390
        put_packet(s, buf);
1391
        break;
1392
    case 'G':
1393
        cpu_synchronize_state(s->g_cpu);
1394
        registers = mem_buf;
1395
        len = strlen(p) / 2;
1396
        hextomem((uint8_t *)registers, p, len);
1397
        for (addr = 0; addr < s->g_cpu->gdb_num_regs && len > 0; addr++) {
1398
            reg_size = gdb_write_register(s->g_cpu, registers, addr);
1399
            len -= reg_size;
1400
            registers += reg_size;
1401
        }
1402
        put_packet(s, "OK");
1403
        break;
1404
    case 'm':
1405
        addr = strtoull(p, (char **)&p, 16);
1406
        if (*p == ',')
1407
            p++;
1408
        len = strtoull(p, NULL, 16);
1409
        if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
1410
            put_packet (s, "E14");
1411
        } else {
1412
            memtohex(buf, mem_buf, len);
1413
            put_packet(s, buf);
1414
        }
1415
        break;
1416
    case 'M':
1417
        addr = strtoull(p, (char **)&p, 16);
1418
        if (*p == ',')
1419
            p++;
1420
        len = strtoull(p, (char **)&p, 16);
1421
        if (*p == ':')
1422
            p++;
1423
        hextomem(mem_buf, p, len);
1424
        if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
1425
                                   true) != 0) {
1426
            put_packet(s, "E14");
1427
        } else {
1428
            put_packet(s, "OK");
1429
        }
1430
        break;
1431
    case 'p':
1432
        /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1433
           This works, but can be very slow.  Anything new enough to
1434
           understand XML also knows how to use this properly.  */
1435
        if (!gdb_has_xml)
1436
            goto unknown_command;
1437
        addr = strtoull(p, (char **)&p, 16);
1438
        reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
1439
        if (reg_size) {
1440
            memtohex(buf, mem_buf, reg_size);
1441
            put_packet(s, buf);
1442
        } else {
1443
            put_packet(s, "E14");
1444
        }
1445
        break;
1446
    case 'P':
1447
        if (!gdb_has_xml)
1448
            goto unknown_command;
1449
        addr = strtoull(p, (char **)&p, 16);
1450
        if (*p == '=')
1451
            p++;
1452
        reg_size = strlen(p) / 2;
1453
        hextomem(mem_buf, p, reg_size);
1454
        gdb_write_register(s->g_cpu, mem_buf, addr);
1455
        put_packet(s, "OK");
1456
        break;
1457
    case 'Z':
1458
    case 'z':
1459
        type = strtoul(p, (char **)&p, 16);
1460
        if (*p == ',')
1461
            p++;
1462
        addr = strtoull(p, (char **)&p, 16);
1463
        if (*p == ',')
1464
            p++;
1465
        len = strtoull(p, (char **)&p, 16);
1466
        if (ch == 'Z')
1467
            res = gdb_breakpoint_insert(addr, len, type);
1468
        else
1469
            res = gdb_breakpoint_remove(addr, len, type);
1470
        if (res >= 0)
1471
             put_packet(s, "OK");
1472
        else if (res == -ENOSYS)
1473
            put_packet(s, "");
1474
        else
1475
            put_packet(s, "E22");
1476
        break;
1477
    case 'H':
1478
        type = *p++;
1479
        thread = strtoull(p, (char **)&p, 16);
1480
        if (thread == -1 || thread == 0) {
1481
            put_packet(s, "OK");
1482
            break;
1483
        }
1484
        cpu = find_cpu(thread);
1485
        if (cpu == NULL) {
1486
            put_packet(s, "E22");
1487
            break;
1488
        }
1489
        switch (type) {
1490
        case 'c':
1491
            s->c_cpu = cpu;
1492
            put_packet(s, "OK");
1493
            break;
1494
        case 'g':
1495
            s->g_cpu = cpu;
1496
            put_packet(s, "OK");
1497
            break;
1498
        default:
1499
             put_packet(s, "E22");
1500
             break;
1501
        }
1502
        break;
1503
    case 'T':
1504
        thread = strtoull(p, (char **)&p, 16);
1505
        cpu = find_cpu(thread);
1506

    
1507
        if (cpu != NULL) {
1508
            put_packet(s, "OK");
1509
        } else {
1510
            put_packet(s, "E22");
1511
        }
1512
        break;
1513
    case 'q':
1514
    case 'Q':
1515
        /* parse any 'q' packets here */
1516
        if (!strcmp(p,"qemu.sstepbits")) {
1517
            /* Query Breakpoint bit definitions */
1518
            snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1519
                     SSTEP_ENABLE,
1520
                     SSTEP_NOIRQ,
1521
                     SSTEP_NOTIMER);
1522
            put_packet(s, buf);
1523
            break;
1524
        } else if (strncmp(p,"qemu.sstep",10) == 0) {
1525
            /* Display or change the sstep_flags */
1526
            p += 10;
1527
            if (*p != '=') {
1528
                /* Display current setting */
1529
                snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
1530
                put_packet(s, buf);
1531
                break;
1532
            }
1533
            p++;
1534
            type = strtoul(p, (char **)&p, 16);
1535
            sstep_flags = type;
1536
            put_packet(s, "OK");
1537
            break;
1538
        } else if (strcmp(p,"C") == 0) {
1539
            /* "Current thread" remains vague in the spec, so always return
1540
             *  the first CPU (gdb returns the first thread). */
1541
            put_packet(s, "QC1");
1542
            break;
1543
        } else if (strcmp(p,"fThreadInfo") == 0) {
1544
            s->query_cpu = first_cpu;
1545
            goto report_cpuinfo;
1546
        } else if (strcmp(p,"sThreadInfo") == 0) {
1547
        report_cpuinfo:
1548
            if (s->query_cpu) {
1549
                snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu));
1550
                put_packet(s, buf);
1551
                s->query_cpu = s->query_cpu->next_cpu;
1552
            } else
1553
                put_packet(s, "l");
1554
            break;
1555
        } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1556
            thread = strtoull(p+16, (char **)&p, 16);
1557
            cpu = find_cpu(thread);
1558
            if (cpu != NULL) {
1559
                cpu_synchronize_state(cpu);
1560
                len = snprintf((char *)mem_buf, sizeof(mem_buf),
1561
                               "CPU#%d [%s]", cpu->cpu_index,
1562
                               cpu->halted ? "halted " : "running");
1563
                memtohex(buf, mem_buf, len);
1564
                put_packet(s, buf);
1565
            }
1566
            break;
1567
        }
1568
#ifdef CONFIG_USER_ONLY
1569
        else if (strncmp(p, "Offsets", 7) == 0) {
1570
            CPUArchState *env = s->c_cpu->env_ptr;
1571
            TaskState *ts = env->opaque;
1572

    
1573
            snprintf(buf, sizeof(buf),
1574
                     "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1575
                     ";Bss=" TARGET_ABI_FMT_lx,
1576
                     ts->info->code_offset,
1577
                     ts->info->data_offset,
1578
                     ts->info->data_offset);
1579
            put_packet(s, buf);
1580
            break;
1581
        }
1582
#else /* !CONFIG_USER_ONLY */
1583
        else if (strncmp(p, "Rcmd,", 5) == 0) {
1584
            int len = strlen(p + 5);
1585

    
1586
            if ((len % 2) != 0) {
1587
                put_packet(s, "E01");
1588
                break;
1589
            }
1590
            hextomem(mem_buf, p + 5, len);
1591
            len = len / 2;
1592
            mem_buf[len++] = 0;
1593
            qemu_chr_be_write(s->mon_chr, mem_buf, len);
1594
            put_packet(s, "OK");
1595
            break;
1596
        }
1597
#endif /* !CONFIG_USER_ONLY */
1598
        if (strncmp(p, "Supported", 9) == 0) {
1599
            snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
1600
#ifdef GDB_CORE_XML
1601
            pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1602
#endif
1603
            put_packet(s, buf);
1604
            break;
1605
        }
1606
#ifdef GDB_CORE_XML
1607
        if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1608
            const char *xml;
1609
            target_ulong total_len;
1610

    
1611
            gdb_has_xml = 1;
1612
            p += 19;
1613
            xml = get_feature_xml(p, &p);
1614
            if (!xml) {
1615
                snprintf(buf, sizeof(buf), "E00");
1616
                put_packet(s, buf);
1617
                break;
1618
            }
1619

    
1620
            if (*p == ':')
1621
                p++;
1622
            addr = strtoul(p, (char **)&p, 16);
1623
            if (*p == ',')
1624
                p++;
1625
            len = strtoul(p, (char **)&p, 16);
1626

    
1627
            total_len = strlen(xml);
1628
            if (addr > total_len) {
1629
                snprintf(buf, sizeof(buf), "E00");
1630
                put_packet(s, buf);
1631
                break;
1632
            }
1633
            if (len > (MAX_PACKET_LENGTH - 5) / 2)
1634
                len = (MAX_PACKET_LENGTH - 5) / 2;
1635
            if (len < total_len - addr) {
1636
                buf[0] = 'm';
1637
                len = memtox(buf + 1, xml + addr, len);
1638
            } else {
1639
                buf[0] = 'l';
1640
                len = memtox(buf + 1, xml + addr, total_len - addr);
1641
            }
1642
            put_packet_binary(s, buf, len + 1);
1643
            break;
1644
        }
1645
#endif
1646
        /* Unrecognised 'q' command.  */
1647
        goto unknown_command;
1648

    
1649
    default:
1650
    unknown_command:
1651
        /* put empty packet */
1652
        buf[0] = '\0';
1653
        put_packet(s, buf);
1654
        break;
1655
    }
1656
    return RS_IDLE;
1657
}
1658

    
1659
void gdb_set_stop_cpu(CPUState *cpu)
1660
{
1661
    gdbserver_state->c_cpu = cpu;
1662
    gdbserver_state->g_cpu = cpu;
1663
}
1664

    
1665
#ifndef CONFIG_USER_ONLY
1666
static void gdb_vm_state_change(void *opaque, int running, RunState state)
1667
{
1668
    GDBState *s = gdbserver_state;
1669
    CPUArchState *env = s->c_cpu->env_ptr;
1670
    CPUState *cpu = s->c_cpu;
1671
    char buf[256];
1672
    const char *type;
1673
    int ret;
1674

    
1675
    if (running || s->state == RS_INACTIVE) {
1676
        return;
1677
    }
1678
    /* Is there a GDB syscall waiting to be sent?  */
1679
    if (s->current_syscall_cb) {
1680
        put_packet(s, s->syscall_buf);
1681
        return;
1682
    }
1683
    switch (state) {
1684
    case RUN_STATE_DEBUG:
1685
        if (env->watchpoint_hit) {
1686
            switch (env->watchpoint_hit->flags & BP_MEM_ACCESS) {
1687
            case BP_MEM_READ:
1688
                type = "r";
1689
                break;
1690
            case BP_MEM_ACCESS:
1691
                type = "a";
1692
                break;
1693
            default:
1694
                type = "";
1695
                break;
1696
            }
1697
            snprintf(buf, sizeof(buf),
1698
                     "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
1699
                     GDB_SIGNAL_TRAP, cpu_index(cpu), type,
1700
                     env->watchpoint_hit->vaddr);
1701
            env->watchpoint_hit = NULL;
1702
            goto send_packet;
1703
        }
1704
        tb_flush(env);
1705
        ret = GDB_SIGNAL_TRAP;
1706
        break;
1707
    case RUN_STATE_PAUSED:
1708
        ret = GDB_SIGNAL_INT;
1709
        break;
1710
    case RUN_STATE_SHUTDOWN:
1711
        ret = GDB_SIGNAL_QUIT;
1712
        break;
1713
    case RUN_STATE_IO_ERROR:
1714
        ret = GDB_SIGNAL_IO;
1715
        break;
1716
    case RUN_STATE_WATCHDOG:
1717
        ret = GDB_SIGNAL_ALRM;
1718
        break;
1719
    case RUN_STATE_INTERNAL_ERROR:
1720
        ret = GDB_SIGNAL_ABRT;
1721
        break;
1722
    case RUN_STATE_SAVE_VM:
1723
    case RUN_STATE_RESTORE_VM:
1724
        return;
1725
    case RUN_STATE_FINISH_MIGRATE:
1726
        ret = GDB_SIGNAL_XCPU;
1727
        break;
1728
    default:
1729
        ret = GDB_SIGNAL_UNKNOWN;
1730
        break;
1731
    }
1732
    snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(cpu));
1733

    
1734
send_packet:
1735
    put_packet(s, buf);
1736

    
1737
    /* disable single step if it was enabled */
1738
    cpu_single_step(cpu, 0);
1739
}
1740
#endif
1741

    
1742
/* Send a gdb syscall request.
1743
   This accepts limited printf-style format specifiers, specifically:
1744
    %x  - target_ulong argument printed in hex.
1745
    %lx - 64-bit argument printed in hex.
1746
    %s  - string pointer (target_ulong) and length (int) pair.  */
1747
void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
1748
{
1749
    va_list va;
1750
    char *p;
1751
    char *p_end;
1752
    target_ulong addr;
1753
    uint64_t i64;
1754
    GDBState *s;
1755

    
1756
    s = gdbserver_state;
1757
    if (!s)
1758
        return;
1759
    s->current_syscall_cb = cb;
1760
#ifndef CONFIG_USER_ONLY
1761
    vm_stop(RUN_STATE_DEBUG);
1762
#endif
1763
    va_start(va, fmt);
1764
    p = s->syscall_buf;
1765
    p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
1766
    *(p++) = 'F';
1767
    while (*fmt) {
1768
        if (*fmt == '%') {
1769
            fmt++;
1770
            switch (*fmt++) {
1771
            case 'x':
1772
                addr = va_arg(va, target_ulong);
1773
                p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
1774
                break;
1775
            case 'l':
1776
                if (*(fmt++) != 'x')
1777
                    goto bad_format;
1778
                i64 = va_arg(va, uint64_t);
1779
                p += snprintf(p, p_end - p, "%" PRIx64, i64);
1780
                break;
1781
            case 's':
1782
                addr = va_arg(va, target_ulong);
1783
                p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
1784
                              addr, va_arg(va, int));
1785
                break;
1786
            default:
1787
            bad_format:
1788
                fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
1789
                        fmt - 1);
1790
                break;
1791
            }
1792
        } else {
1793
            *(p++) = *(fmt++);
1794
        }
1795
    }
1796
    *p = 0;
1797
    va_end(va);
1798
#ifdef CONFIG_USER_ONLY
1799
    put_packet(s, s->syscall_buf);
1800
    gdb_handlesig(s->c_cpu, 0);
1801
#else
1802
    /* In this case wait to send the syscall packet until notification that
1803
       the CPU has stopped.  This must be done because if the packet is sent
1804
       now the reply from the syscall request could be received while the CPU
1805
       is still in the running state, which can cause packets to be dropped
1806
       and state transition 'T' packets to be sent while the syscall is still
1807
       being processed.  */
1808
    cpu_exit(s->c_cpu);
1809
#endif
1810
}
1811

    
1812
static void gdb_read_byte(GDBState *s, int ch)
1813
{
1814
    int i, csum;
1815
    uint8_t reply;
1816

    
1817
#ifndef CONFIG_USER_ONLY
1818
    if (s->last_packet_len) {
1819
        /* Waiting for a response to the last packet.  If we see the start
1820
           of a new command then abandon the previous response.  */
1821
        if (ch == '-') {
1822
#ifdef DEBUG_GDB
1823
            printf("Got NACK, retransmitting\n");
1824
#endif
1825
            put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
1826
        }
1827
#ifdef DEBUG_GDB
1828
        else if (ch == '+')
1829
            printf("Got ACK\n");
1830
        else
1831
            printf("Got '%c' when expecting ACK/NACK\n", ch);
1832
#endif
1833
        if (ch == '+' || ch == '$')
1834
            s->last_packet_len = 0;
1835
        if (ch != '$')
1836
            return;
1837
    }
1838
    if (runstate_is_running()) {
1839
        /* when the CPU is running, we cannot do anything except stop
1840
           it when receiving a char */
1841
        vm_stop(RUN_STATE_PAUSED);
1842
    } else
1843
#endif
1844
    {
1845
        switch(s->state) {
1846
        case RS_IDLE:
1847
            if (ch == '$') {
1848
                s->line_buf_index = 0;
1849
                s->state = RS_GETLINE;
1850
            }
1851
            break;
1852
        case RS_GETLINE:
1853
            if (ch == '#') {
1854
            s->state = RS_CHKSUM1;
1855
            } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1856
                s->state = RS_IDLE;
1857
            } else {
1858
            s->line_buf[s->line_buf_index++] = ch;
1859
            }
1860
            break;
1861
        case RS_CHKSUM1:
1862
            s->line_buf[s->line_buf_index] = '\0';
1863
            s->line_csum = fromhex(ch) << 4;
1864
            s->state = RS_CHKSUM2;
1865
            break;
1866
        case RS_CHKSUM2:
1867
            s->line_csum |= fromhex(ch);
1868
            csum = 0;
1869
            for(i = 0; i < s->line_buf_index; i++) {
1870
                csum += s->line_buf[i];
1871
            }
1872
            if (s->line_csum != (csum & 0xff)) {
1873
                reply = '-';
1874
                put_buffer(s, &reply, 1);
1875
                s->state = RS_IDLE;
1876
            } else {
1877
                reply = '+';
1878
                put_buffer(s, &reply, 1);
1879
                s->state = gdb_handle_packet(s, s->line_buf);
1880
            }
1881
            break;
1882
        default:
1883
            abort();
1884
        }
1885
    }
1886
}
1887

    
1888
/* Tell the remote gdb that the process has exited.  */
1889
void gdb_exit(CPUArchState *env, int code)
1890
{
1891
  GDBState *s;
1892
  char buf[4];
1893

    
1894
  s = gdbserver_state;
1895
  if (!s) {
1896
      return;
1897
  }
1898
#ifdef CONFIG_USER_ONLY
1899
  if (gdbserver_fd < 0 || s->fd < 0) {
1900
      return;
1901
  }
1902
#endif
1903

    
1904
  snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
1905
  put_packet(s, buf);
1906

    
1907
#ifndef CONFIG_USER_ONLY
1908
  if (s->chr) {
1909
      qemu_chr_delete(s->chr);
1910
  }
1911
#endif
1912
}
1913

    
1914
#ifdef CONFIG_USER_ONLY
1915
int
1916
gdb_queuesig (void)
1917
{
1918
    GDBState *s;
1919

    
1920
    s = gdbserver_state;
1921

    
1922
    if (gdbserver_fd < 0 || s->fd < 0)
1923
        return 0;
1924
    else
1925
        return 1;
1926
}
1927

    
1928
int
1929
gdb_handlesig(CPUState *cpu, int sig)
1930
{
1931
    CPUArchState *env = cpu->env_ptr;
1932
    GDBState *s;
1933
    char buf[256];
1934
    int n;
1935

    
1936
    s = gdbserver_state;
1937
    if (gdbserver_fd < 0 || s->fd < 0) {
1938
        return sig;
1939
    }
1940

    
1941
    /* disable single step if it was enabled */
1942
    cpu_single_step(cpu, 0);
1943
    tb_flush(env);
1944

    
1945
    if (sig != 0) {
1946
        snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
1947
        put_packet(s, buf);
1948
    }
1949
    /* put_packet() might have detected that the peer terminated the
1950
       connection.  */
1951
    if (s->fd < 0) {
1952
        return sig;
1953
    }
1954

    
1955
    sig = 0;
1956
    s->state = RS_IDLE;
1957
    s->running_state = 0;
1958
    while (s->running_state == 0) {
1959
        n = read(s->fd, buf, 256);
1960
        if (n > 0) {
1961
            int i;
1962

    
1963
            for (i = 0; i < n; i++) {
1964
                gdb_read_byte(s, buf[i]);
1965
            }
1966
        } else if (n == 0 || errno != EAGAIN) {
1967
            /* XXX: Connection closed.  Should probably wait for another
1968
               connection before continuing.  */
1969
            return sig;
1970
        }
1971
    }
1972
    sig = s->signal;
1973
    s->signal = 0;
1974
    return sig;
1975
}
1976

    
1977
/* Tell the remote gdb that the process has exited due to SIG.  */
1978
void gdb_signalled(CPUArchState *env, int sig)
1979
{
1980
    GDBState *s;
1981
    char buf[4];
1982

    
1983
    s = gdbserver_state;
1984
    if (gdbserver_fd < 0 || s->fd < 0) {
1985
        return;
1986
    }
1987

    
1988
    snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
1989
    put_packet(s, buf);
1990
}
1991

    
1992
static void gdb_accept(void)
1993
{
1994
    GDBState *s;
1995
    struct sockaddr_in sockaddr;
1996
    socklen_t len;
1997
    int fd;
1998

    
1999
    for(;;) {
2000
        len = sizeof(sockaddr);
2001
        fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
2002
        if (fd < 0 && errno != EINTR) {
2003
            perror("accept");
2004
            return;
2005
        } else if (fd >= 0) {
2006
#ifndef _WIN32
2007
            fcntl(fd, F_SETFD, FD_CLOEXEC);
2008
#endif
2009
            break;
2010
        }
2011
    }
2012

    
2013
    /* set short latency */
2014
    socket_set_nodelay(fd);
2015

    
2016
    s = g_malloc0(sizeof(GDBState));
2017
    s->c_cpu = first_cpu;
2018
    s->g_cpu = first_cpu;
2019
    s->fd = fd;
2020
    gdb_has_xml = 0;
2021

    
2022
    gdbserver_state = s;
2023

    
2024
    fcntl(fd, F_SETFL, O_NONBLOCK);
2025
}
2026

    
2027
static int gdbserver_open(int port)
2028
{
2029
    struct sockaddr_in sockaddr;
2030
    int fd, val, ret;
2031

    
2032
    fd = socket(PF_INET, SOCK_STREAM, 0);
2033
    if (fd < 0) {
2034
        perror("socket");
2035
        return -1;
2036
    }
2037
#ifndef _WIN32
2038
    fcntl(fd, F_SETFD, FD_CLOEXEC);
2039
#endif
2040

    
2041
    /* allow fast reuse */
2042
    val = 1;
2043
    qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
2044

    
2045
    sockaddr.sin_family = AF_INET;
2046
    sockaddr.sin_port = htons(port);
2047
    sockaddr.sin_addr.s_addr = 0;
2048
    ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
2049
    if (ret < 0) {
2050
        perror("bind");
2051
        close(fd);
2052
        return -1;
2053
    }
2054
    ret = listen(fd, 0);
2055
    if (ret < 0) {
2056
        perror("listen");
2057
        close(fd);
2058
        return -1;
2059
    }
2060
    return fd;
2061
}
2062

    
2063
int gdbserver_start(int port)
2064
{
2065
    gdbserver_fd = gdbserver_open(port);
2066
    if (gdbserver_fd < 0)
2067
        return -1;
2068
    /* accept connections */
2069
    gdb_accept();
2070
    return 0;
2071
}
2072

    
2073
/* Disable gdb stub for child processes.  */
2074
void gdbserver_fork(CPUArchState *env)
2075
{
2076
    GDBState *s = gdbserver_state;
2077
    if (gdbserver_fd < 0 || s->fd < 0)
2078
      return;
2079
    close(s->fd);
2080
    s->fd = -1;
2081
    cpu_breakpoint_remove_all(env, BP_GDB);
2082
    cpu_watchpoint_remove_all(env, BP_GDB);
2083
}
2084
#else
2085
static int gdb_chr_can_receive(void *opaque)
2086
{
2087
  /* We can handle an arbitrarily large amount of data.
2088
   Pick the maximum packet size, which is as good as anything.  */
2089
  return MAX_PACKET_LENGTH;
2090
}
2091

    
2092
static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
2093
{
2094
    int i;
2095

    
2096
    for (i = 0; i < size; i++) {
2097
        gdb_read_byte(gdbserver_state, buf[i]);
2098
    }
2099
}
2100

    
2101
static void gdb_chr_event(void *opaque, int event)
2102
{
2103
    switch (event) {
2104
    case CHR_EVENT_OPENED:
2105
        vm_stop(RUN_STATE_PAUSED);
2106
        gdb_has_xml = 0;
2107
        break;
2108
    default:
2109
        break;
2110
    }
2111
}
2112

    
2113
static void gdb_monitor_output(GDBState *s, const char *msg, int len)
2114
{
2115
    char buf[MAX_PACKET_LENGTH];
2116

    
2117
    buf[0] = 'O';
2118
    if (len > (MAX_PACKET_LENGTH/2) - 1)
2119
        len = (MAX_PACKET_LENGTH/2) - 1;
2120
    memtohex(buf + 1, (uint8_t *)msg, len);
2121
    put_packet(s, buf);
2122
}
2123

    
2124
static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
2125
{
2126
    const char *p = (const char *)buf;
2127
    int max_sz;
2128

    
2129
    max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
2130
    for (;;) {
2131
        if (len <= max_sz) {
2132
            gdb_monitor_output(gdbserver_state, p, len);
2133
            break;
2134
        }
2135
        gdb_monitor_output(gdbserver_state, p, max_sz);
2136
        p += max_sz;
2137
        len -= max_sz;
2138
    }
2139
    return len;
2140
}
2141

    
2142
#ifndef _WIN32
2143
static void gdb_sigterm_handler(int signal)
2144
{
2145
    if (runstate_is_running()) {
2146
        vm_stop(RUN_STATE_PAUSED);
2147
    }
2148
}
2149
#endif
2150

    
2151
int gdbserver_start(const char *device)
2152
{
2153
    GDBState *s;
2154
    char gdbstub_device_name[128];
2155
    CharDriverState *chr = NULL;
2156
    CharDriverState *mon_chr;
2157

    
2158
    if (!device)
2159
        return -1;
2160
    if (strcmp(device, "none") != 0) {
2161
        if (strstart(device, "tcp:", NULL)) {
2162
            /* enforce required TCP attributes */
2163
            snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
2164
                     "%s,nowait,nodelay,server", device);
2165
            device = gdbstub_device_name;
2166
        }
2167
#ifndef _WIN32
2168
        else if (strcmp(device, "stdio") == 0) {
2169
            struct sigaction act;
2170

    
2171
            memset(&act, 0, sizeof(act));
2172
            act.sa_handler = gdb_sigterm_handler;
2173
            sigaction(SIGINT, &act, NULL);
2174
        }
2175
#endif
2176
        chr = qemu_chr_new("gdb", device, NULL);
2177
        if (!chr)
2178
            return -1;
2179

    
2180
        qemu_chr_fe_claim_no_fail(chr);
2181
        qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
2182
                              gdb_chr_event, NULL);
2183
    }
2184

    
2185
    s = gdbserver_state;
2186
    if (!s) {
2187
        s = g_malloc0(sizeof(GDBState));
2188
        gdbserver_state = s;
2189

    
2190
        qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
2191

    
2192
        /* Initialize a monitor terminal for gdb */
2193
        mon_chr = g_malloc0(sizeof(*mon_chr));
2194
        mon_chr->chr_write = gdb_monitor_write;
2195
        monitor_init(mon_chr, 0);
2196
    } else {
2197
        if (s->chr)
2198
            qemu_chr_delete(s->chr);
2199
        mon_chr = s->mon_chr;
2200
        memset(s, 0, sizeof(GDBState));
2201
    }
2202
    s->c_cpu = first_cpu;
2203
    s->g_cpu = first_cpu;
2204
    s->chr = chr;
2205
    s->state = chr ? RS_IDLE : RS_INACTIVE;
2206
    s->mon_chr = mon_chr;
2207
    s->current_syscall_cb = NULL;
2208

    
2209
    return 0;
2210
}
2211
#endif