gdbstub: Fix cpu_gdb_{read,write}_register() Coding Style
[sdk/emulator/qemu.git] / gdbstub.c
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 #ifdef TARGET_X86_64
524 static const int gpr_map[16] = {
525     R_EAX, R_EBX, R_ECX, R_EDX, R_ESI, R_EDI, R_EBP, R_ESP,
526     8, 9, 10, 11, 12, 13, 14, 15
527 };
528 #else
529 #define gpr_map gpr_map32
530 #endif
531 static const int gpr_map32[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
532
533 #define NUM_CORE_REGS (CPU_NB_REGS * 2 + 25)
534
535 #define IDX_IP_REG      CPU_NB_REGS
536 #define IDX_FLAGS_REG   (IDX_IP_REG + 1)
537 #define IDX_SEG_REGS    (IDX_FLAGS_REG + 1)
538 #define IDX_FP_REGS     (IDX_SEG_REGS + 6)
539 #define IDX_XMM_REGS    (IDX_FP_REGS + 16)
540 #define IDX_MXCSR_REG   (IDX_XMM_REGS + CPU_NB_REGS)
541
542 static int cpu_gdb_read_register(CPUX86State *env, uint8_t *mem_buf, int n)
543 {
544     if (n < CPU_NB_REGS) {
545         if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
546             GET_REG64(env->regs[gpr_map[n]]);
547         } else if (n < CPU_NB_REGS32) {
548             GET_REG32(env->regs[gpr_map32[n]]);
549         }
550     } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
551 #ifdef USE_X86LDOUBLE
552         /* FIXME: byteswap float values - after fixing fpregs layout. */
553         memcpy(mem_buf, &env->fpregs[n - IDX_FP_REGS], 10);
554 #else
555         memset(mem_buf, 0, 10);
556 #endif
557         return 10;
558     } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
559         n -= IDX_XMM_REGS;
560         if (n < CPU_NB_REGS32 ||
561             (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK)) {
562             stq_p(mem_buf, env->xmm_regs[n].XMM_Q(0));
563             stq_p(mem_buf + 8, env->xmm_regs[n].XMM_Q(1));
564             return 16;
565         }
566     } else {
567         switch (n) {
568         case IDX_IP_REG:
569             if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
570                 GET_REG64(env->eip);
571             } else {
572                 GET_REG32(env->eip);
573             }
574         case IDX_FLAGS_REG:
575             GET_REG32(env->eflags);
576
577         case IDX_SEG_REGS:
578             GET_REG32(env->segs[R_CS].selector);
579         case IDX_SEG_REGS + 1:
580             GET_REG32(env->segs[R_SS].selector);
581         case IDX_SEG_REGS + 2:
582             GET_REG32(env->segs[R_DS].selector);
583         case IDX_SEG_REGS + 3:
584             GET_REG32(env->segs[R_ES].selector);
585         case IDX_SEG_REGS + 4:
586             GET_REG32(env->segs[R_FS].selector);
587         case IDX_SEG_REGS + 5:
588             GET_REG32(env->segs[R_GS].selector);
589
590         case IDX_FP_REGS + 8:
591             GET_REG32(env->fpuc);
592         case IDX_FP_REGS + 9:
593             GET_REG32((env->fpus & ~0x3800) |
594                       (env->fpstt & 0x7) << 11);
595         case IDX_FP_REGS + 10:
596             GET_REG32(0); /* ftag */
597         case IDX_FP_REGS + 11:
598             GET_REG32(0); /* fiseg */
599         case IDX_FP_REGS + 12:
600             GET_REG32(0); /* fioff */
601         case IDX_FP_REGS + 13:
602             GET_REG32(0); /* foseg */
603         case IDX_FP_REGS + 14:
604             GET_REG32(0); /* fooff */
605         case IDX_FP_REGS + 15:
606             GET_REG32(0); /* fop */
607
608         case IDX_MXCSR_REG:
609             GET_REG32(env->mxcsr);
610         }
611     }
612     return 0;
613 }
614
615 static int cpu_x86_gdb_load_seg(CPUX86State *env, int sreg, uint8_t *mem_buf)
616 {
617     uint16_t selector = ldl_p(mem_buf);
618
619     if (selector != env->segs[sreg].selector) {
620 #if defined(CONFIG_USER_ONLY)
621         cpu_x86_load_seg(env, sreg, selector);
622 #else
623         unsigned int limit, flags;
624         target_ulong base;
625
626         if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
627             base = selector << 4;
628             limit = 0xffff;
629             flags = 0;
630         } else {
631             if (!cpu_x86_get_descr_debug(env, selector, &base, &limit,
632                                          &flags)) {
633                 return 4;
634             }
635         }
636         cpu_x86_load_seg_cache(env, sreg, selector, base, limit, flags);
637 #endif
638     }
639     return 4;
640 }
641
642 static int cpu_gdb_write_register(CPUX86State *env, uint8_t *mem_buf, int n)
643 {
644     uint32_t tmp;
645
646     if (n < CPU_NB_REGS) {
647         if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
648             env->regs[gpr_map[n]] = ldtul_p(mem_buf);
649             return sizeof(target_ulong);
650         } else if (n < CPU_NB_REGS32) {
651             n = gpr_map32[n];
652             env->regs[n] &= ~0xffffffffUL;
653             env->regs[n] |= (uint32_t)ldl_p(mem_buf);
654             return 4;
655         }
656     } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
657 #ifdef USE_X86LDOUBLE
658         /* FIXME: byteswap float values - after fixing fpregs layout. */
659         memcpy(&env->fpregs[n - IDX_FP_REGS], mem_buf, 10);
660 #endif
661         return 10;
662     } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
663         n -= IDX_XMM_REGS;
664         if (n < CPU_NB_REGS32 ||
665             (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK)) {
666             env->xmm_regs[n].XMM_Q(0) = ldq_p(mem_buf);
667             env->xmm_regs[n].XMM_Q(1) = ldq_p(mem_buf + 8);
668             return 16;
669         }
670     } else {
671         switch (n) {
672         case IDX_IP_REG:
673             if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
674                 env->eip = ldq_p(mem_buf);
675                 return 8;
676             } else {
677                 env->eip &= ~0xffffffffUL;
678                 env->eip |= (uint32_t)ldl_p(mem_buf);
679                 return 4;
680             }
681         case IDX_FLAGS_REG:
682             env->eflags = ldl_p(mem_buf);
683             return 4;
684
685         case IDX_SEG_REGS:
686             return cpu_x86_gdb_load_seg(env, R_CS, mem_buf);
687         case IDX_SEG_REGS + 1:
688             return cpu_x86_gdb_load_seg(env, R_SS, mem_buf);
689         case IDX_SEG_REGS + 2:
690             return cpu_x86_gdb_load_seg(env, R_DS, mem_buf);
691         case IDX_SEG_REGS + 3:
692             return cpu_x86_gdb_load_seg(env, R_ES, mem_buf);
693         case IDX_SEG_REGS + 4:
694             return cpu_x86_gdb_load_seg(env, R_FS, mem_buf);
695         case IDX_SEG_REGS + 5:
696             return cpu_x86_gdb_load_seg(env, R_GS, mem_buf);
697
698         case IDX_FP_REGS + 8:
699             env->fpuc = ldl_p(mem_buf);
700             return 4;
701         case IDX_FP_REGS + 9:
702             tmp = ldl_p(mem_buf);
703             env->fpstt = (tmp >> 11) & 7;
704             env->fpus = tmp & ~0x3800;
705             return 4;
706         case IDX_FP_REGS + 10: /* ftag */
707             return 4;
708         case IDX_FP_REGS + 11: /* fiseg */
709             return 4;
710         case IDX_FP_REGS + 12: /* fioff */
711             return 4;
712         case IDX_FP_REGS + 13: /* foseg */
713             return 4;
714         case IDX_FP_REGS + 14: /* fooff */
715             return 4;
716         case IDX_FP_REGS + 15: /* fop */
717             return 4;
718
719         case IDX_MXCSR_REG:
720             env->mxcsr = ldl_p(mem_buf);
721             return 4;
722         }
723     }
724     /* Unrecognised register.  */
725     return 0;
726 }
727
728 #elif defined (TARGET_PPC)
729
730 /* Old gdb always expects FP registers.  Newer (xml-aware) gdb only
731    expects whatever the target description contains.  Due to a
732    historical mishap the FP registers appear in between core integer
733    regs and PC, MSR, CR, and so forth.  We hack round this by giving the
734    FP regs zero size when talking to a newer gdb.  */
735 #define NUM_CORE_REGS 71
736 #if defined (TARGET_PPC64)
737 #define GDB_CORE_XML "power64-core.xml"
738 #else
739 #define GDB_CORE_XML "power-core.xml"
740 #endif
741
742 static int cpu_gdb_read_register(CPUPPCState *env, uint8_t *mem_buf, int n)
743 {
744     if (n < 32) {
745         /* gprs */
746         GET_REGL(env->gpr[n]);
747     } else if (n < 64) {
748         /* fprs */
749         if (gdb_has_xml) {
750             return 0;
751         }
752         stfq_p(mem_buf, env->fpr[n-32]);
753         return 8;
754     } else {
755         switch (n) {
756         case 64:
757             GET_REGL(env->nip);
758         case 65:
759             GET_REGL(env->msr);
760         case 66:
761             {
762                 uint32_t cr = 0;
763                 int i;
764                 for (i = 0; i < 8; i++) {
765                     cr |= env->crf[i] << (32 - ((i + 1) * 4));
766                 }
767                 GET_REG32(cr);
768             }
769         case 67:
770             GET_REGL(env->lr);
771         case 68:
772             GET_REGL(env->ctr);
773         case 69:
774             GET_REGL(env->xer);
775         case 70:
776             {
777                 if (gdb_has_xml) {
778                     return 0;
779                 }
780                 GET_REG32(env->fpscr);
781             }
782         }
783     }
784     return 0;
785 }
786
787 static int cpu_gdb_write_register(CPUPPCState *env, uint8_t *mem_buf, int n)
788 {
789     if (n < 32) {
790         /* gprs */
791         env->gpr[n] = ldtul_p(mem_buf);
792         return sizeof(target_ulong);
793     } else if (n < 64) {
794         /* fprs */
795         if (gdb_has_xml) {
796             return 0;
797         }
798         env->fpr[n-32] = ldfq_p(mem_buf);
799         return 8;
800     } else {
801         switch (n) {
802         case 64:
803             env->nip = ldtul_p(mem_buf);
804             return sizeof(target_ulong);
805         case 65:
806             ppc_store_msr(env, ldtul_p(mem_buf));
807             return sizeof(target_ulong);
808         case 66:
809             {
810                 uint32_t cr = ldl_p(mem_buf);
811                 int i;
812                 for (i = 0; i < 8; i++) {
813                     env->crf[i] = (cr >> (32 - ((i + 1) * 4))) & 0xF;
814                 }
815                 return 4;
816             }
817         case 67:
818             env->lr = ldtul_p(mem_buf);
819             return sizeof(target_ulong);
820         case 68:
821             env->ctr = ldtul_p(mem_buf);
822             return sizeof(target_ulong);
823         case 69:
824             env->xer = ldtul_p(mem_buf);
825             return sizeof(target_ulong);
826         case 70:
827             /* fpscr */
828             if (gdb_has_xml) {
829                 return 0;
830             }
831             store_fpscr(env, ldtul_p(mem_buf), 0xffffffff);
832             return sizeof(target_ulong);
833         }
834     }
835     return 0;
836 }
837
838 #elif defined (TARGET_SPARC)
839
840 #if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
841 #define NUM_CORE_REGS 86
842 #else
843 #define NUM_CORE_REGS 72
844 #endif
845
846 #ifdef TARGET_ABI32
847 #define GET_REGA(val) GET_REG32(val)
848 #else
849 #define GET_REGA(val) GET_REGL(val)
850 #endif
851
852 static int cpu_gdb_read_register(CPUSPARCState *env, uint8_t *mem_buf, int n)
853 {
854     if (n < 8) {
855         /* g0..g7 */
856         GET_REGA(env->gregs[n]);
857     }
858     if (n < 32) {
859         /* register window */
860         GET_REGA(env->regwptr[n - 8]);
861     }
862 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
863     if (n < 64) {
864         /* fprs */
865         if (n & 1) {
866             GET_REG32(env->fpr[(n - 32) / 2].l.lower);
867         } else {
868             GET_REG32(env->fpr[(n - 32) / 2].l.upper);
869         }
870     }
871     /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
872     switch (n) {
873     case 64:
874         GET_REGA(env->y);
875     case 65:
876         GET_REGA(cpu_get_psr(env));
877     case 66:
878         GET_REGA(env->wim);
879     case 67:
880         GET_REGA(env->tbr);
881     case 68:
882         GET_REGA(env->pc);
883     case 69:
884         GET_REGA(env->npc);
885     case 70:
886         GET_REGA(env->fsr);
887     case 71:
888         GET_REGA(0); /* csr */
889     default:
890         GET_REGA(0);
891     }
892 #else
893     if (n < 64) {
894         /* f0-f31 */
895         if (n & 1) {
896             GET_REG32(env->fpr[(n - 32) / 2].l.lower);
897         } else {
898             GET_REG32(env->fpr[(n - 32) / 2].l.upper);
899         }
900     }
901     if (n < 80) {
902         /* f32-f62 (double width, even numbers only) */
903         GET_REG64(env->fpr[(n - 32) / 2].ll);
904     }
905     switch (n) {
906     case 80:
907         GET_REGL(env->pc);
908     case 81:
909         GET_REGL(env->npc);
910     case 82:
911         GET_REGL((cpu_get_ccr(env) << 32) |
912                  ((env->asi & 0xff) << 24) |
913                  ((env->pstate & 0xfff) << 8) |
914                  cpu_get_cwp64(env));
915     case 83:
916         GET_REGL(env->fsr);
917     case 84:
918         GET_REGL(env->fprs);
919     case 85:
920         GET_REGL(env->y);
921     }
922 #endif
923     return 0;
924 }
925
926 static int cpu_gdb_write_register(CPUSPARCState *env, uint8_t *mem_buf, int n)
927 {
928 #if defined(TARGET_ABI32)
929     abi_ulong tmp;
930
931     tmp = ldl_p(mem_buf);
932 #else
933     target_ulong tmp;
934
935     tmp = ldtul_p(mem_buf);
936 #endif
937
938     if (n < 8) {
939         /* g0..g7 */
940         env->gregs[n] = tmp;
941     } else if (n < 32) {
942         /* register window */
943         env->regwptr[n - 8] = tmp;
944     }
945 #if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
946     else if (n < 64) {
947         /* fprs */
948         /* f0-f31 */
949         if (n & 1) {
950             env->fpr[(n - 32) / 2].l.lower = tmp;
951         } else {
952             env->fpr[(n - 32) / 2].l.upper = tmp;
953         }
954     } else {
955         /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
956         switch (n) {
957         case 64:
958             env->y = tmp;
959             break;
960         case 65:
961             cpu_put_psr(env, tmp);
962             break;
963         case 66:
964             env->wim = tmp;
965             break;
966         case 67:
967             env->tbr = tmp;
968             break;
969         case 68:
970             env->pc = tmp;
971             break;
972         case 69:
973             env->npc = tmp;
974             break;
975         case 70:
976             env->fsr = tmp;
977             break;
978         default:
979             return 0;
980         }
981     }
982     return 4;
983 #else
984     else if (n < 64) {
985         /* f0-f31 */
986         tmp = ldl_p(mem_buf);
987         if (n & 1) {
988             env->fpr[(n - 32) / 2].l.lower = tmp;
989         } else {
990             env->fpr[(n - 32) / 2].l.upper = tmp;
991         }
992         return 4;
993     } else if (n < 80) {
994         /* f32-f62 (double width, even numbers only) */
995         env->fpr[(n - 32) / 2].ll = tmp;
996     } else {
997         switch (n) {
998         case 80:
999             env->pc = tmp;
1000             break;
1001         case 81:
1002             env->npc = tmp;
1003             break;
1004         case 82:
1005             cpu_put_ccr(env, tmp >> 32);
1006             env->asi = (tmp >> 24) & 0xff;
1007             env->pstate = (tmp >> 8) & 0xfff;
1008             cpu_put_cwp64(env, tmp & 0xff);
1009             break;
1010         case 83:
1011             env->fsr = tmp;
1012             break;
1013         case 84:
1014             env->fprs = tmp;
1015             break;
1016         case 85:
1017             env->y = tmp;
1018             break;
1019         default:
1020             return 0;
1021         }
1022     }
1023     return 8;
1024 #endif
1025 }
1026 #elif defined (TARGET_ARM)
1027
1028 /* Old gdb always expect FPA registers.  Newer (xml-aware) gdb only expect
1029    whatever the target description contains.  Due to a historical mishap
1030    the FPA registers appear in between core integer regs and the CPSR.
1031    We hack round this by giving the FPA regs zero size when talking to a
1032    newer gdb.  */
1033 #define NUM_CORE_REGS 26
1034 #define GDB_CORE_XML "arm-core.xml"
1035
1036 static int cpu_gdb_read_register(CPUARMState *env, uint8_t *mem_buf, int n)
1037 {
1038     if (n < 16) {
1039         /* Core integer register.  */
1040         GET_REG32(env->regs[n]);
1041     }
1042     if (n < 24) {
1043         /* FPA registers.  */
1044         if (gdb_has_xml) {
1045             return 0;
1046         }
1047         memset(mem_buf, 0, 12);
1048         return 12;
1049     }
1050     switch (n) {
1051     case 24:
1052         /* FPA status register.  */
1053         if (gdb_has_xml) {
1054             return 0;
1055         }
1056         GET_REG32(0);
1057     case 25:
1058         /* CPSR */
1059         GET_REG32(cpsr_read(env));
1060     }
1061     /* Unknown register.  */
1062     return 0;
1063 }
1064
1065 static int cpu_gdb_write_register(CPUARMState *env, uint8_t *mem_buf, int n)
1066 {
1067     uint32_t tmp;
1068
1069     tmp = ldl_p(mem_buf);
1070
1071     /* Mask out low bit of PC to workaround gdb bugs.  This will probably
1072        cause problems if we ever implement the Jazelle DBX extensions.  */
1073     if (n == 15) {
1074         tmp &= ~1;
1075     }
1076
1077     if (n < 16) {
1078         /* Core integer register.  */
1079         env->regs[n] = tmp;
1080         return 4;
1081     }
1082     if (n < 24) { /* 16-23 */
1083         /* FPA registers (ignored).  */
1084         if (gdb_has_xml) {
1085             return 0;
1086         }
1087         return 12;
1088     }
1089     switch (n) {
1090     case 24:
1091         /* FPA status register (ignored).  */
1092         if (gdb_has_xml) {
1093             return 0;
1094         }
1095         return 4;
1096     case 25:
1097         /* CPSR */
1098         cpsr_write(env, tmp, 0xffffffff);
1099         return 4;
1100     }
1101     /* Unknown register.  */
1102     return 0;
1103 }
1104
1105 #elif defined (TARGET_M68K)
1106
1107 #define NUM_CORE_REGS 18
1108
1109 #define GDB_CORE_XML "cf-core.xml"
1110
1111 static int cpu_gdb_read_register(CPUM68KState *env, uint8_t *mem_buf, int n)
1112 {
1113     if (n < 8) {
1114         /* D0-D7 */
1115         GET_REG32(env->dregs[n]);
1116     } else if (n < 16) {
1117         /* A0-A7 */
1118         GET_REG32(env->aregs[n - 8]);
1119     } else {
1120         switch (n) {
1121         case 16:
1122             GET_REG32(env->sr);
1123         case 17:
1124             GET_REG32(env->pc);
1125         }
1126     }
1127     /* FP registers not included here because they vary between
1128        ColdFire and m68k.  Use XML bits for these.  */
1129     return 0;
1130 }
1131
1132 static int cpu_gdb_write_register(CPUM68KState *env, uint8_t *mem_buf, int n)
1133 {
1134     uint32_t tmp;
1135
1136     tmp = ldl_p(mem_buf);
1137
1138     if (n < 8) {
1139         /* D0-D7 */
1140         env->dregs[n] = tmp;
1141     } else if (n < 16) {
1142         /* A0-A7 */
1143         env->aregs[n - 8] = tmp;
1144     } else {
1145         switch (n) {
1146         case 16:
1147             env->sr = tmp;
1148             break;
1149         case 17:
1150             env->pc = tmp;
1151             break;
1152         default:
1153             return 0;
1154         }
1155     }
1156     return 4;
1157 }
1158 #elif defined (TARGET_MIPS)
1159
1160 #define NUM_CORE_REGS 73
1161
1162 static int cpu_gdb_read_register(CPUMIPSState *env, uint8_t *mem_buf, int n)
1163 {
1164     if (n < 32) {
1165         GET_REGL(env->active_tc.gpr[n]);
1166     }
1167     if (env->CP0_Config1 & (1 << CP0C1_FP)) {
1168         if (n >= 38 && n < 70) {
1169             if (env->CP0_Status & (1 << CP0St_FR)) {
1170                 GET_REGL(env->active_fpu.fpr[n - 38].d);
1171             } else {
1172                 GET_REGL(env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX]);
1173             }
1174         }
1175         switch (n) {
1176         case 70:
1177             GET_REGL((int32_t)env->active_fpu.fcr31);
1178         case 71:
1179             GET_REGL((int32_t)env->active_fpu.fcr0);
1180         }
1181     }
1182     switch (n) {
1183     case 32:
1184         GET_REGL((int32_t)env->CP0_Status);
1185     case 33:
1186         GET_REGL(env->active_tc.LO[0]);
1187     case 34:
1188         GET_REGL(env->active_tc.HI[0]);
1189     case 35:
1190         GET_REGL(env->CP0_BadVAddr);
1191     case 36:
1192         GET_REGL((int32_t)env->CP0_Cause);
1193     case 37:
1194         GET_REGL(env->active_tc.PC | !!(env->hflags & MIPS_HFLAG_M16));
1195     case 72:
1196         GET_REGL(0); /* fp */
1197     case 89:
1198         GET_REGL((int32_t)env->CP0_PRid);
1199     }
1200     if (n >= 73 && n <= 88) {
1201         /* 16 embedded regs.  */
1202         GET_REGL(0);
1203     }
1204
1205     return 0;
1206 }
1207
1208 /* convert MIPS rounding mode in FCR31 to IEEE library */
1209 static unsigned int ieee_rm[] = {
1210     float_round_nearest_even,
1211     float_round_to_zero,
1212     float_round_up,
1213     float_round_down
1214 };
1215 #define RESTORE_ROUNDING_MODE \
1216     set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], \
1217                             &env->active_fpu.fp_status)
1218
1219 static int cpu_gdb_write_register(CPUMIPSState *env, uint8_t *mem_buf, int n)
1220 {
1221     target_ulong tmp;
1222
1223     tmp = ldtul_p(mem_buf);
1224
1225     if (n < 32) {
1226         env->active_tc.gpr[n] = tmp;
1227         return sizeof(target_ulong);
1228     }
1229     if (env->CP0_Config1 & (1 << CP0C1_FP)
1230             && n >= 38 && n < 73) {
1231         if (n < 70) {
1232             if (env->CP0_Status & (1 << CP0St_FR)) {
1233                 env->active_fpu.fpr[n - 38].d = tmp;
1234             } else {
1235                 env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX] = tmp;
1236             }
1237         }
1238         switch (n) {
1239         case 70:
1240             env->active_fpu.fcr31 = tmp & 0xFF83FFFF;
1241             /* set rounding mode */
1242             RESTORE_ROUNDING_MODE;
1243             break;
1244         case 71:
1245             env->active_fpu.fcr0 = tmp;
1246             break;
1247         }
1248         return sizeof(target_ulong);
1249     }
1250     switch (n) {
1251     case 32:
1252         env->CP0_Status = tmp;
1253         break;
1254     case 33:
1255         env->active_tc.LO[0] = tmp;
1256         break;
1257     case 34:
1258         env->active_tc.HI[0] = tmp;
1259         break;
1260     case 35:
1261         env->CP0_BadVAddr = tmp;
1262         break;
1263     case 36:
1264         env->CP0_Cause = tmp;
1265         break;
1266     case 37:
1267         env->active_tc.PC = tmp & ~(target_ulong)1;
1268         if (tmp & 1) {
1269             env->hflags |= MIPS_HFLAG_M16;
1270         } else {
1271             env->hflags &= ~(MIPS_HFLAG_M16);
1272         }
1273         break;
1274     case 72: /* fp, ignored */
1275         break;
1276     default:
1277         if (n > 89) {
1278             return 0;
1279         }
1280         /* Other registers are readonly.  Ignore writes.  */
1281         break;
1282     }
1283
1284     return sizeof(target_ulong);
1285 }
1286 #elif defined(TARGET_OPENRISC)
1287
1288 #define NUM_CORE_REGS (32 + 3)
1289
1290 static int cpu_gdb_read_register(CPUOpenRISCState *env, uint8_t *mem_buf, int n)
1291 {
1292     if (n < 32) {
1293         GET_REG32(env->gpr[n]);
1294     } else {
1295         switch (n) {
1296         case 32:    /* PPC */
1297             GET_REG32(env->ppc);
1298             break;
1299
1300         case 33:    /* NPC */
1301             GET_REG32(env->npc);
1302             break;
1303
1304         case 34:    /* SR */
1305             GET_REG32(env->sr);
1306             break;
1307
1308         default:
1309             break;
1310         }
1311     }
1312     return 0;
1313 }
1314
1315 static int cpu_gdb_write_register(CPUOpenRISCState *env,
1316                                   uint8_t *mem_buf, int n)
1317 {
1318     uint32_t tmp;
1319
1320     if (n > NUM_CORE_REGS) {
1321         return 0;
1322     }
1323
1324     tmp = ldl_p(mem_buf);
1325
1326     if (n < 32) {
1327         env->gpr[n] = tmp;
1328     } else {
1329         switch (n) {
1330         case 32: /* PPC */
1331             env->ppc = tmp;
1332             break;
1333
1334         case 33: /* NPC */
1335             env->npc = tmp;
1336             break;
1337
1338         case 34: /* SR */
1339             env->sr = tmp;
1340             break;
1341
1342         default:
1343             break;
1344         }
1345     }
1346     return 4;
1347 }
1348 #elif defined (TARGET_SH4)
1349
1350 /* Hint: Use "set architecture sh4" in GDB to see fpu registers */
1351 /* FIXME: We should use XML for this.  */
1352
1353 #define NUM_CORE_REGS 59
1354
1355 static int cpu_gdb_read_register(CPUSH4State *env, uint8_t *mem_buf, int n)
1356 {
1357     switch (n) {
1358     case 0 ... 7:
1359         if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
1360             GET_REGL(env->gregs[n + 16]);
1361         } else {
1362             GET_REGL(env->gregs[n]);
1363         }
1364     case 8 ... 15:
1365         GET_REGL(env->gregs[n]);
1366     case 16:
1367         GET_REGL(env->pc);
1368     case 17:
1369         GET_REGL(env->pr);
1370     case 18:
1371         GET_REGL(env->gbr);
1372     case 19:
1373         GET_REGL(env->vbr);
1374     case 20:
1375         GET_REGL(env->mach);
1376     case 21:
1377         GET_REGL(env->macl);
1378     case 22:
1379         GET_REGL(env->sr);
1380     case 23:
1381         GET_REGL(env->fpul);
1382     case 24:
1383         GET_REGL(env->fpscr);
1384     case 25 ... 40:
1385         if (env->fpscr & FPSCR_FR) {
1386             stfl_p(mem_buf, env->fregs[n - 9]);
1387         } else {
1388             stfl_p(mem_buf, env->fregs[n - 25]);
1389         }
1390         return 4;
1391     case 41:
1392         GET_REGL(env->ssr);
1393     case 42:
1394         GET_REGL(env->spc);
1395     case 43 ... 50:
1396         GET_REGL(env->gregs[n - 43]);
1397     case 51 ... 58:
1398         GET_REGL(env->gregs[n - (51 - 16)]);
1399     }
1400
1401     return 0;
1402 }
1403
1404 static int cpu_gdb_write_register(CPUSH4State *env, uint8_t *mem_buf, int n)
1405 {
1406     switch (n) {
1407     case 0 ... 7:
1408         if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
1409             env->gregs[n + 16] = ldl_p(mem_buf);
1410         } else {
1411             env->gregs[n] = ldl_p(mem_buf);
1412         }
1413         break;
1414     case 8 ... 15:
1415         env->gregs[n] = ldl_p(mem_buf);
1416         break;
1417     case 16:
1418         env->pc = ldl_p(mem_buf);
1419         break;
1420     case 17:
1421         env->pr = ldl_p(mem_buf);
1422         break;
1423     case 18:
1424         env->gbr = ldl_p(mem_buf);
1425         break;
1426     case 19:
1427         env->vbr = ldl_p(mem_buf);
1428         break;
1429     case 20:
1430         env->mach = ldl_p(mem_buf);
1431         break;
1432     case 21:
1433         env->macl = ldl_p(mem_buf);
1434         break;
1435     case 22:
1436         env->sr = ldl_p(mem_buf);
1437         break;
1438     case 23:
1439         env->fpul = ldl_p(mem_buf);
1440         break;
1441     case 24:
1442         env->fpscr = ldl_p(mem_buf);
1443         break;
1444     case 25 ... 40:
1445         if (env->fpscr & FPSCR_FR) {
1446             env->fregs[n - 9] = ldfl_p(mem_buf);
1447         } else {
1448             env->fregs[n - 25] = ldfl_p(mem_buf);
1449         }
1450         break;
1451     case 41:
1452         env->ssr = ldl_p(mem_buf);
1453         break;
1454     case 42:
1455         env->spc = ldl_p(mem_buf);
1456         break;
1457     case 43 ... 50:
1458         env->gregs[n - 43] = ldl_p(mem_buf);
1459         break;
1460     case 51 ... 58:
1461         env->gregs[n - (51 - 16)] = ldl_p(mem_buf);
1462         break;
1463     default:
1464         return 0;
1465     }
1466
1467     return 4;
1468 }
1469 #elif defined (TARGET_MICROBLAZE)
1470
1471 #define NUM_CORE_REGS (32 + 5)
1472
1473 static int cpu_gdb_read_register(CPUMBState *env, uint8_t *mem_buf, int n)
1474 {
1475     if (n < 32) {
1476         GET_REG32(env->regs[n]);
1477     } else {
1478         GET_REG32(env->sregs[n - 32]);
1479     }
1480     return 0;
1481 }
1482
1483 static int cpu_gdb_write_register(CPUMBState *env, uint8_t *mem_buf, int n)
1484 {
1485     uint32_t tmp;
1486
1487     if (n > NUM_CORE_REGS) {
1488         return 0;
1489     }
1490
1491     tmp = ldl_p(mem_buf);
1492
1493     if (n < 32) {
1494         env->regs[n] = tmp;
1495     } else {
1496         env->sregs[n - 32] = tmp;
1497     }
1498     return 4;
1499 }
1500 #elif defined (TARGET_CRIS)
1501
1502 #define NUM_CORE_REGS 49
1503
1504 static int
1505 read_register_crisv10(CPUCRISState *env, uint8_t *mem_buf, int n)
1506 {
1507     if (n < 15) {
1508         GET_REG32(env->regs[n]);
1509     }
1510
1511     if (n == 15) {
1512         GET_REG32(env->pc);
1513     }
1514
1515     if (n < 32) {
1516         switch (n) {
1517         case 16:
1518             GET_REG8(env->pregs[n - 16]);
1519             break;
1520         case 17:
1521             GET_REG8(env->pregs[n - 16]);
1522             break;
1523         case 20:
1524         case 21:
1525             GET_REG16(env->pregs[n - 16]);
1526             break;
1527         default:
1528             if (n >= 23) {
1529                 GET_REG32(env->pregs[n - 16]);
1530             }
1531             break;
1532         }
1533     }
1534     return 0;
1535 }
1536
1537 static int cpu_gdb_read_register(CPUCRISState *env, uint8_t *mem_buf, int n)
1538 {
1539     uint8_t srs;
1540
1541     if (env->pregs[PR_VR] < 32) {
1542         return read_register_crisv10(env, mem_buf, n);
1543     }
1544
1545     srs = env->pregs[PR_SRS];
1546     if (n < 16) {
1547         GET_REG32(env->regs[n]);
1548     }
1549
1550     if (n >= 21 && n < 32) {
1551         GET_REG32(env->pregs[n - 16]);
1552     }
1553     if (n >= 33 && n < 49) {
1554         GET_REG32(env->sregs[srs][n - 33]);
1555     }
1556     switch (n) {
1557     case 16:
1558         GET_REG8(env->pregs[0]);
1559     case 17:
1560         GET_REG8(env->pregs[1]);
1561     case 18:
1562         GET_REG32(env->pregs[2]);
1563     case 19:
1564         GET_REG8(srs);
1565     case 20:
1566         GET_REG16(env->pregs[4]);
1567     case 32:
1568         GET_REG32(env->pc);
1569     }
1570
1571     return 0;
1572 }
1573
1574 static int cpu_gdb_write_register(CPUCRISState *env, uint8_t *mem_buf, int n)
1575 {
1576     uint32_t tmp;
1577
1578     if (n > 49) {
1579         return 0;
1580     }
1581
1582     tmp = ldl_p(mem_buf);
1583
1584     if (n < 16) {
1585         env->regs[n] = tmp;
1586     }
1587
1588     if (n >= 21 && n < 32) {
1589         env->pregs[n - 16] = tmp;
1590     }
1591
1592     /* FIXME: Should support function regs be writable?  */
1593     switch (n) {
1594     case 16:
1595         return 1;
1596     case 17:
1597         return 1;
1598     case 18:
1599         env->pregs[PR_PID] = tmp;
1600         break;
1601     case 19:
1602         return 1;
1603     case 20:
1604         return 2;
1605     case 32:
1606         env->pc = tmp;
1607         break;
1608     }
1609
1610     return 4;
1611 }
1612 #elif defined (TARGET_ALPHA)
1613
1614 #define NUM_CORE_REGS 67
1615
1616 static int cpu_gdb_read_register(CPUAlphaState *env, uint8_t *mem_buf, int n)
1617 {
1618     uint64_t val;
1619     CPU_DoubleU d;
1620
1621     switch (n) {
1622     case 0 ... 30:
1623         val = env->ir[n];
1624         break;
1625     case 32 ... 62:
1626         d.d = env->fir[n - 32];
1627         val = d.ll;
1628         break;
1629     case 63:
1630         val = cpu_alpha_load_fpcr(env);
1631         break;
1632     case 64:
1633         val = env->pc;
1634         break;
1635     case 66:
1636         val = env->unique;
1637         break;
1638     case 31:
1639     case 65:
1640         /* 31 really is the zero register; 65 is unassigned in the
1641            gdb protocol, but is still required to occupy 8 bytes. */
1642         val = 0;
1643         break;
1644     default:
1645         return 0;
1646     }
1647     GET_REGL(val);
1648 }
1649
1650 static int cpu_gdb_write_register(CPUAlphaState *env, uint8_t *mem_buf, int n)
1651 {
1652     target_ulong tmp = ldtul_p(mem_buf);
1653     CPU_DoubleU d;
1654
1655     switch (n) {
1656     case 0 ... 30:
1657         env->ir[n] = tmp;
1658         break;
1659     case 32 ... 62:
1660         d.ll = tmp;
1661         env->fir[n - 32] = d.d;
1662         break;
1663     case 63:
1664         cpu_alpha_store_fpcr(env, tmp);
1665         break;
1666     case 64:
1667         env->pc = tmp;
1668         break;
1669     case 66:
1670         env->unique = tmp;
1671         break;
1672     case 31:
1673     case 65:
1674         /* 31 really is the zero register; 65 is unassigned in the
1675            gdb protocol, but is still required to occupy 8 bytes. */
1676         break;
1677     default:
1678         return 0;
1679     }
1680     return 8;
1681 }
1682 #elif defined (TARGET_S390X)
1683
1684 #define NUM_CORE_REGS  S390_NUM_REGS
1685
1686 static int cpu_gdb_read_register(CPUS390XState *env, uint8_t *mem_buf, int n)
1687 {
1688     uint64_t val;
1689     int cc_op;
1690
1691     switch (n) {
1692     case S390_PSWM_REGNUM:
1693         cc_op = calc_cc(env, env->cc_op, env->cc_src, env->cc_dst, env->cc_vr);
1694         val = deposit64(env->psw.mask, 44, 2, cc_op);
1695         GET_REGL(val);
1696         break;
1697     case S390_PSWA_REGNUM:
1698         GET_REGL(env->psw.addr);
1699         break;
1700     case S390_R0_REGNUM ... S390_R15_REGNUM:
1701         GET_REGL(env->regs[n-S390_R0_REGNUM]);
1702         break;
1703     case S390_A0_REGNUM ... S390_A15_REGNUM:
1704         GET_REG32(env->aregs[n-S390_A0_REGNUM]);
1705         break;
1706     case S390_FPC_REGNUM:
1707         GET_REG32(env->fpc);
1708         break;
1709     case S390_F0_REGNUM ... S390_F15_REGNUM:
1710         GET_REG64(env->fregs[n-S390_F0_REGNUM].ll);
1711         break;
1712     }
1713
1714     return 0;
1715 }
1716
1717 static int cpu_gdb_write_register(CPUS390XState *env, uint8_t *mem_buf, int n)
1718 {
1719     target_ulong tmpl;
1720     uint32_t tmp32;
1721     int r = 8;
1722     tmpl = ldtul_p(mem_buf);
1723     tmp32 = ldl_p(mem_buf);
1724
1725     switch (n) {
1726     case S390_PSWM_REGNUM:
1727         env->psw.mask = tmpl;
1728         env->cc_op = extract64(tmpl, 44, 2);
1729         break;
1730     case S390_PSWA_REGNUM:
1731         env->psw.addr = tmpl;
1732         break;
1733     case S390_R0_REGNUM ... S390_R15_REGNUM:
1734         env->regs[n-S390_R0_REGNUM] = tmpl;
1735         break;
1736     case S390_A0_REGNUM ... S390_A15_REGNUM:
1737         env->aregs[n-S390_A0_REGNUM] = tmp32;
1738         r = 4;
1739         break;
1740     case S390_FPC_REGNUM:
1741         env->fpc = tmp32;
1742         r = 4;
1743         break;
1744     case S390_F0_REGNUM ... S390_F15_REGNUM:
1745         env->fregs[n-S390_F0_REGNUM].ll = tmpl;
1746         break;
1747     default:
1748         return 0;
1749     }
1750     return r;
1751 }
1752 #elif defined (TARGET_LM32)
1753
1754 #include "hw/lm32/lm32_pic.h"
1755 #define NUM_CORE_REGS (32 + 7)
1756
1757 static int cpu_gdb_read_register(CPULM32State *env, uint8_t *mem_buf, int n)
1758 {
1759     if (n < 32) {
1760         GET_REG32(env->regs[n]);
1761     } else {
1762         switch (n) {
1763         case 32:
1764             GET_REG32(env->pc);
1765             break;
1766         /* FIXME: put in right exception ID */
1767         case 33:
1768             GET_REG32(0);
1769             break;
1770         case 34:
1771             GET_REG32(env->eba);
1772             break;
1773         case 35:
1774             GET_REG32(env->deba);
1775             break;
1776         case 36:
1777             GET_REG32(env->ie);
1778             break;
1779         case 37:
1780             GET_REG32(lm32_pic_get_im(env->pic_state));
1781             break;
1782         case 38:
1783             GET_REG32(lm32_pic_get_ip(env->pic_state));
1784             break;
1785         }
1786     }
1787     return 0;
1788 }
1789
1790 static int cpu_gdb_write_register(CPULM32State *env, uint8_t *mem_buf, int n)
1791 {
1792     uint32_t tmp;
1793
1794     if (n > NUM_CORE_REGS) {
1795         return 0;
1796     }
1797
1798     tmp = ldl_p(mem_buf);
1799
1800     if (n < 32) {
1801         env->regs[n] = tmp;
1802     } else {
1803         switch (n) {
1804         case 32:
1805             env->pc = tmp;
1806             break;
1807         case 34:
1808             env->eba = tmp;
1809             break;
1810         case 35:
1811             env->deba = tmp;
1812             break;
1813         case 36:
1814             env->ie = tmp;
1815             break;
1816         case 37:
1817             lm32_pic_set_im(env->pic_state, tmp);
1818             break;
1819         case 38:
1820             lm32_pic_set_ip(env->pic_state, tmp);
1821             break;
1822         }
1823     }
1824     return 4;
1825 }
1826 #elif defined(TARGET_XTENSA)
1827
1828 /* Use num_core_regs to see only non-privileged registers in an unmodified gdb.
1829  * Use num_regs to see all registers. gdb modification is required for that:
1830  * reset bit 0 in the 'flags' field of the registers definitions in the
1831  * gdb/xtensa-config.c inside gdb source tree or inside gdb overlay.
1832  */
1833 #define NUM_CORE_REGS (env->config->gdb_regmap.num_regs)
1834 #define num_g_regs NUM_CORE_REGS
1835
1836 static int cpu_gdb_read_register(CPUXtensaState *env, uint8_t *mem_buf, int n)
1837 {
1838     const XtensaGdbReg *reg = env->config->gdb_regmap.reg + n;
1839
1840     if (n < 0 || n >= env->config->gdb_regmap.num_regs) {
1841         return 0;
1842     }
1843
1844     switch (reg->type) {
1845     case 9: /*pc*/
1846         GET_REG32(env->pc);
1847         break;
1848
1849     case 1: /*ar*/
1850         xtensa_sync_phys_from_window(env);
1851         GET_REG32(env->phys_regs[(reg->targno & 0xff) % env->config->nareg]);
1852         break;
1853
1854     case 2: /*SR*/
1855         GET_REG32(env->sregs[reg->targno & 0xff]);
1856         break;
1857
1858     case 3: /*UR*/
1859         GET_REG32(env->uregs[reg->targno & 0xff]);
1860         break;
1861
1862     case 4: /*f*/
1863         GET_REG32(float32_val(env->fregs[reg->targno & 0x0f]));
1864         break;
1865
1866     case 8: /*a*/
1867         GET_REG32(env->regs[reg->targno & 0x0f]);
1868         break;
1869
1870     default:
1871         qemu_log("%s from reg %d of unsupported type %d\n",
1872                  __func__, n, reg->type);
1873         return 0;
1874     }
1875 }
1876
1877 static int cpu_gdb_write_register(CPUXtensaState *env, uint8_t *mem_buf, int n)
1878 {
1879     uint32_t tmp;
1880     const XtensaGdbReg *reg = env->config->gdb_regmap.reg + n;
1881
1882     if (n < 0 || n >= env->config->gdb_regmap.num_regs) {
1883         return 0;
1884     }
1885
1886     tmp = ldl_p(mem_buf);
1887
1888     switch (reg->type) {
1889     case 9: /*pc*/
1890         env->pc = tmp;
1891         break;
1892
1893     case 1: /*ar*/
1894         env->phys_regs[(reg->targno & 0xff) % env->config->nareg] = tmp;
1895         xtensa_sync_window_from_phys(env);
1896         break;
1897
1898     case 2: /*SR*/
1899         env->sregs[reg->targno & 0xff] = tmp;
1900         break;
1901
1902     case 3: /*UR*/
1903         env->uregs[reg->targno & 0xff] = tmp;
1904         break;
1905
1906     case 4: /*f*/
1907         env->fregs[reg->targno & 0x0f] = make_float32(tmp);
1908         break;
1909
1910     case 8: /*a*/
1911         env->regs[reg->targno & 0x0f] = tmp;
1912         break;
1913
1914     default:
1915         qemu_log("%s to reg %d of unsupported type %d\n",
1916                  __func__, n, reg->type);
1917         return 0;
1918     }
1919
1920     return 4;
1921 }
1922 #else
1923
1924 #define NUM_CORE_REGS 0
1925
1926 static int cpu_gdb_read_register(CPUArchState *env, uint8_t *mem_buf, int n)
1927 {
1928     return 0;
1929 }
1930
1931 static int cpu_gdb_write_register(CPUArchState *env, uint8_t *mem_buf, int n)
1932 {
1933     return 0;
1934 }
1935
1936 #endif
1937
1938 #if !defined(TARGET_XTENSA)
1939 static int num_g_regs = NUM_CORE_REGS;
1940 #endif
1941
1942 #ifdef GDB_CORE_XML
1943 /* Encode data using the encoding for 'x' packets.  */
1944 static int memtox(char *buf, const char *mem, int len)
1945 {
1946     char *p = buf;
1947     char c;
1948
1949     while (len--) {
1950         c = *(mem++);
1951         switch (c) {
1952         case '#': case '$': case '*': case '}':
1953             *(p++) = '}';
1954             *(p++) = c ^ 0x20;
1955             break;
1956         default:
1957             *(p++) = c;
1958             break;
1959         }
1960     }
1961     return p - buf;
1962 }
1963
1964 static const char *get_feature_xml(const char *p, const char **newp)
1965 {
1966     size_t len;
1967     int i;
1968     const char *name;
1969     static char target_xml[1024];
1970
1971     len = 0;
1972     while (p[len] && p[len] != ':')
1973         len++;
1974     *newp = p + len;
1975
1976     name = NULL;
1977     if (strncmp(p, "target.xml", len) == 0) {
1978         /* Generate the XML description for this CPU.  */
1979         if (!target_xml[0]) {
1980             GDBRegisterState *r;
1981             CPUState *cpu = first_cpu;
1982
1983             snprintf(target_xml, sizeof(target_xml),
1984                      "<?xml version=\"1.0\"?>"
1985                      "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
1986                      "<target>"
1987                      "<xi:include href=\"%s\"/>",
1988                      GDB_CORE_XML);
1989
1990             for (r = cpu->gdb_regs; r; r = r->next) {
1991                 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
1992                 pstrcat(target_xml, sizeof(target_xml), r->xml);
1993                 pstrcat(target_xml, sizeof(target_xml), "\"/>");
1994             }
1995             pstrcat(target_xml, sizeof(target_xml), "</target>");
1996         }
1997         return target_xml;
1998     }
1999     for (i = 0; ; i++) {
2000         name = xml_builtin[i][0];
2001         if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
2002             break;
2003     }
2004     return name ? xml_builtin[i][1] : NULL;
2005 }
2006 #endif
2007
2008 static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
2009 {
2010     CPUArchState *env = cpu->env_ptr;
2011     GDBRegisterState *r;
2012
2013     if (reg < NUM_CORE_REGS)
2014         return cpu_gdb_read_register(env, mem_buf, reg);
2015
2016     for (r = cpu->gdb_regs; r; r = r->next) {
2017         if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
2018             return r->get_reg(env, mem_buf, reg - r->base_reg);
2019         }
2020     }
2021     return 0;
2022 }
2023
2024 static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
2025 {
2026     CPUArchState *env = cpu->env_ptr;
2027     GDBRegisterState *r;
2028
2029     if (reg < NUM_CORE_REGS)
2030         return cpu_gdb_write_register(env, mem_buf, reg);
2031
2032     for (r = cpu->gdb_regs; r; r = r->next) {
2033         if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
2034             return r->set_reg(env, mem_buf, reg - r->base_reg);
2035         }
2036     }
2037     return 0;
2038 }
2039
2040 #if !defined(TARGET_XTENSA)
2041 /* Register a supplemental set of CPU registers.  If g_pos is nonzero it
2042    specifies the first register number and these registers are included in
2043    a standard "g" packet.  Direction is relative to gdb, i.e. get_reg is
2044    gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
2045  */
2046
2047 void gdb_register_coprocessor(CPUState *cpu,
2048                               gdb_reg_cb get_reg, gdb_reg_cb set_reg,
2049                               int num_regs, const char *xml, int g_pos)
2050 {
2051     GDBRegisterState *s;
2052     GDBRegisterState **p;
2053     static int last_reg = NUM_CORE_REGS;
2054
2055     p = &cpu->gdb_regs;
2056     while (*p) {
2057         /* Check for duplicates.  */
2058         if (strcmp((*p)->xml, xml) == 0)
2059             return;
2060         p = &(*p)->next;
2061     }
2062
2063     s = g_new0(GDBRegisterState, 1);
2064     s->base_reg = last_reg;
2065     s->num_regs = num_regs;
2066     s->get_reg = get_reg;
2067     s->set_reg = set_reg;
2068     s->xml = xml;
2069
2070     /* Add to end of list.  */
2071     last_reg += num_regs;
2072     *p = s;
2073     if (g_pos) {
2074         if (g_pos != s->base_reg) {
2075             fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
2076                     "Expected %d got %d\n", xml, g_pos, s->base_reg);
2077         } else {
2078             num_g_regs = last_reg;
2079         }
2080     }
2081 }
2082 #endif
2083
2084 #ifndef CONFIG_USER_ONLY
2085 static const int xlat_gdb_type[] = {
2086     [GDB_WATCHPOINT_WRITE]  = BP_GDB | BP_MEM_WRITE,
2087     [GDB_WATCHPOINT_READ]   = BP_GDB | BP_MEM_READ,
2088     [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
2089 };
2090 #endif
2091
2092 static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
2093 {
2094     CPUState *cpu;
2095     CPUArchState *env;
2096     int err = 0;
2097
2098     if (kvm_enabled()) {
2099         return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
2100     }
2101
2102     switch (type) {
2103     case GDB_BREAKPOINT_SW:
2104     case GDB_BREAKPOINT_HW:
2105         for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
2106             env = cpu->env_ptr;
2107             err = cpu_breakpoint_insert(env, addr, BP_GDB, NULL);
2108             if (err)
2109                 break;
2110         }
2111         return err;
2112 #ifndef CONFIG_USER_ONLY
2113     case GDB_WATCHPOINT_WRITE:
2114     case GDB_WATCHPOINT_READ:
2115     case GDB_WATCHPOINT_ACCESS:
2116         for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
2117             env = cpu->env_ptr;
2118             err = cpu_watchpoint_insert(env, addr, len, xlat_gdb_type[type],
2119                                         NULL);
2120             if (err)
2121                 break;
2122         }
2123         return err;
2124 #endif
2125     default:
2126         return -ENOSYS;
2127     }
2128 }
2129
2130 static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
2131 {
2132     CPUState *cpu;
2133     CPUArchState *env;
2134     int err = 0;
2135
2136     if (kvm_enabled()) {
2137         return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
2138     }
2139
2140     switch (type) {
2141     case GDB_BREAKPOINT_SW:
2142     case GDB_BREAKPOINT_HW:
2143         for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
2144             env = cpu->env_ptr;
2145             err = cpu_breakpoint_remove(env, addr, BP_GDB);
2146             if (err)
2147                 break;
2148         }
2149         return err;
2150 #ifndef CONFIG_USER_ONLY
2151     case GDB_WATCHPOINT_WRITE:
2152     case GDB_WATCHPOINT_READ:
2153     case GDB_WATCHPOINT_ACCESS:
2154         for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
2155             env = cpu->env_ptr;
2156             err = cpu_watchpoint_remove(env, addr, len, xlat_gdb_type[type]);
2157             if (err)
2158                 break;
2159         }
2160         return err;
2161 #endif
2162     default:
2163         return -ENOSYS;
2164     }
2165 }
2166
2167 static void gdb_breakpoint_remove_all(void)
2168 {
2169     CPUState *cpu;
2170     CPUArchState *env;
2171
2172     if (kvm_enabled()) {
2173         kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
2174         return;
2175     }
2176
2177     for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
2178         env = cpu->env_ptr;
2179         cpu_breakpoint_remove_all(env, BP_GDB);
2180 #ifndef CONFIG_USER_ONLY
2181         cpu_watchpoint_remove_all(env, BP_GDB);
2182 #endif
2183     }
2184 }
2185
2186 static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
2187 {
2188     CPUState *cpu = s->c_cpu;
2189     CPUClass *cc = CPU_GET_CLASS(cpu);
2190
2191     cpu_synchronize_state(cpu);
2192     if (cc->set_pc) {
2193         cc->set_pc(cpu, pc);
2194     }
2195 }
2196
2197 static CPUState *find_cpu(uint32_t thread_id)
2198 {
2199     CPUState *cpu;
2200
2201     for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
2202         if (cpu_index(cpu) == thread_id) {
2203             return cpu;
2204         }
2205     }
2206
2207     return NULL;
2208 }
2209
2210 static int gdb_handle_packet(GDBState *s, const char *line_buf)
2211 {
2212 #ifdef TARGET_XTENSA
2213     CPUArchState *env;
2214 #endif
2215     CPUState *cpu;
2216     const char *p;
2217     uint32_t thread;
2218     int ch, reg_size, type, res;
2219     char buf[MAX_PACKET_LENGTH];
2220     uint8_t mem_buf[MAX_PACKET_LENGTH];
2221     uint8_t *registers;
2222     target_ulong addr, len;
2223
2224 #ifdef DEBUG_GDB
2225     printf("command='%s'\n", line_buf);
2226 #endif
2227     p = line_buf;
2228     ch = *p++;
2229     switch(ch) {
2230     case '?':
2231         /* TODO: Make this return the correct value for user-mode.  */
2232         snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
2233                  cpu_index(s->c_cpu));
2234         put_packet(s, buf);
2235         /* Remove all the breakpoints when this query is issued,
2236          * because gdb is doing and initial connect and the state
2237          * should be cleaned up.
2238          */
2239         gdb_breakpoint_remove_all();
2240         break;
2241     case 'c':
2242         if (*p != '\0') {
2243             addr = strtoull(p, (char **)&p, 16);
2244             gdb_set_cpu_pc(s, addr);
2245         }
2246         s->signal = 0;
2247         gdb_continue(s);
2248         return RS_IDLE;
2249     case 'C':
2250         s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
2251         if (s->signal == -1)
2252             s->signal = 0;
2253         gdb_continue(s);
2254         return RS_IDLE;
2255     case 'v':
2256         if (strncmp(p, "Cont", 4) == 0) {
2257             int res_signal, res_thread;
2258
2259             p += 4;
2260             if (*p == '?') {
2261                 put_packet(s, "vCont;c;C;s;S");
2262                 break;
2263             }
2264             res = 0;
2265             res_signal = 0;
2266             res_thread = 0;
2267             while (*p) {
2268                 int action, signal;
2269
2270                 if (*p++ != ';') {
2271                     res = 0;
2272                     break;
2273                 }
2274                 action = *p++;
2275                 signal = 0;
2276                 if (action == 'C' || action == 'S') {
2277                     signal = strtoul(p, (char **)&p, 16);
2278                 } else if (action != 'c' && action != 's') {
2279                     res = 0;
2280                     break;
2281                 }
2282                 thread = 0;
2283                 if (*p == ':') {
2284                     thread = strtoull(p+1, (char **)&p, 16);
2285                 }
2286                 action = tolower(action);
2287                 if (res == 0 || (res == 'c' && action == 's')) {
2288                     res = action;
2289                     res_signal = signal;
2290                     res_thread = thread;
2291                 }
2292             }
2293             if (res) {
2294                 if (res_thread != -1 && res_thread != 0) {
2295                     cpu = find_cpu(res_thread);
2296                     if (cpu == NULL) {
2297                         put_packet(s, "E22");
2298                         break;
2299                     }
2300                     s->c_cpu = cpu;
2301                 }
2302                 if (res == 's') {
2303                     cpu_single_step(s->c_cpu, sstep_flags);
2304                 }
2305                 s->signal = res_signal;
2306                 gdb_continue(s);
2307                 return RS_IDLE;
2308             }
2309             break;
2310         } else {
2311             goto unknown_command;
2312         }
2313     case 'k':
2314 #ifdef CONFIG_USER_ONLY
2315         /* Kill the target */
2316         fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
2317         exit(0);
2318 #endif
2319     case 'D':
2320         /* Detach packet */
2321         gdb_breakpoint_remove_all();
2322         gdb_syscall_mode = GDB_SYS_DISABLED;
2323         gdb_continue(s);
2324         put_packet(s, "OK");
2325         break;
2326     case 's':
2327         if (*p != '\0') {
2328             addr = strtoull(p, (char **)&p, 16);
2329             gdb_set_cpu_pc(s, addr);
2330         }
2331         cpu_single_step(s->c_cpu, sstep_flags);
2332         gdb_continue(s);
2333         return RS_IDLE;
2334     case 'F':
2335         {
2336             target_ulong ret;
2337             target_ulong err;
2338
2339             ret = strtoull(p, (char **)&p, 16);
2340             if (*p == ',') {
2341                 p++;
2342                 err = strtoull(p, (char **)&p, 16);
2343             } else {
2344                 err = 0;
2345             }
2346             if (*p == ',')
2347                 p++;
2348             type = *p;
2349             if (s->current_syscall_cb) {
2350                 s->current_syscall_cb(s->c_cpu, ret, err);
2351                 s->current_syscall_cb = NULL;
2352             }
2353             if (type == 'C') {
2354                 put_packet(s, "T02");
2355             } else {
2356                 gdb_continue(s);
2357             }
2358         }
2359         break;
2360     case 'g':
2361         cpu_synchronize_state(s->g_cpu);
2362 #ifdef TARGET_XTENSA
2363         env = s->g_cpu->env_ptr;
2364 #endif
2365         len = 0;
2366         for (addr = 0; addr < num_g_regs; addr++) {
2367             reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
2368             len += reg_size;
2369         }
2370         memtohex(buf, mem_buf, len);
2371         put_packet(s, buf);
2372         break;
2373     case 'G':
2374         cpu_synchronize_state(s->g_cpu);
2375 #ifdef TARGET_XTENSA
2376         env = s->g_cpu->env_ptr;
2377 #endif
2378         registers = mem_buf;
2379         len = strlen(p) / 2;
2380         hextomem((uint8_t *)registers, p, len);
2381         for (addr = 0; addr < num_g_regs && len > 0; addr++) {
2382             reg_size = gdb_write_register(s->g_cpu, registers, addr);
2383             len -= reg_size;
2384             registers += reg_size;
2385         }
2386         put_packet(s, "OK");
2387         break;
2388     case 'm':
2389         addr = strtoull(p, (char **)&p, 16);
2390         if (*p == ',')
2391             p++;
2392         len = strtoull(p, NULL, 16);
2393         if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
2394             put_packet (s, "E14");
2395         } else {
2396             memtohex(buf, mem_buf, len);
2397             put_packet(s, buf);
2398         }
2399         break;
2400     case 'M':
2401         addr = strtoull(p, (char **)&p, 16);
2402         if (*p == ',')
2403             p++;
2404         len = strtoull(p, (char **)&p, 16);
2405         if (*p == ':')
2406             p++;
2407         hextomem(mem_buf, p, len);
2408         if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
2409                                    true) != 0) {
2410             put_packet(s, "E14");
2411         } else {
2412             put_packet(s, "OK");
2413         }
2414         break;
2415     case 'p':
2416         /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
2417            This works, but can be very slow.  Anything new enough to
2418            understand XML also knows how to use this properly.  */
2419         if (!gdb_has_xml)
2420             goto unknown_command;
2421         addr = strtoull(p, (char **)&p, 16);
2422         reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
2423         if (reg_size) {
2424             memtohex(buf, mem_buf, reg_size);
2425             put_packet(s, buf);
2426         } else {
2427             put_packet(s, "E14");
2428         }
2429         break;
2430     case 'P':
2431         if (!gdb_has_xml)
2432             goto unknown_command;
2433         addr = strtoull(p, (char **)&p, 16);
2434         if (*p == '=')
2435             p++;
2436         reg_size = strlen(p) / 2;
2437         hextomem(mem_buf, p, reg_size);
2438         gdb_write_register(s->g_cpu, mem_buf, addr);
2439         put_packet(s, "OK");
2440         break;
2441     case 'Z':
2442     case 'z':
2443         type = strtoul(p, (char **)&p, 16);
2444         if (*p == ',')
2445             p++;
2446         addr = strtoull(p, (char **)&p, 16);
2447         if (*p == ',')
2448             p++;
2449         len = strtoull(p, (char **)&p, 16);
2450         if (ch == 'Z')
2451             res = gdb_breakpoint_insert(addr, len, type);
2452         else
2453             res = gdb_breakpoint_remove(addr, len, type);
2454         if (res >= 0)
2455              put_packet(s, "OK");
2456         else if (res == -ENOSYS)
2457             put_packet(s, "");
2458         else
2459             put_packet(s, "E22");
2460         break;
2461     case 'H':
2462         type = *p++;
2463         thread = strtoull(p, (char **)&p, 16);
2464         if (thread == -1 || thread == 0) {
2465             put_packet(s, "OK");
2466             break;
2467         }
2468         cpu = find_cpu(thread);
2469         if (cpu == NULL) {
2470             put_packet(s, "E22");
2471             break;
2472         }
2473         switch (type) {
2474         case 'c':
2475             s->c_cpu = cpu;
2476             put_packet(s, "OK");
2477             break;
2478         case 'g':
2479             s->g_cpu = cpu;
2480             put_packet(s, "OK");
2481             break;
2482         default:
2483              put_packet(s, "E22");
2484              break;
2485         }
2486         break;
2487     case 'T':
2488         thread = strtoull(p, (char **)&p, 16);
2489         cpu = find_cpu(thread);
2490
2491         if (cpu != NULL) {
2492             put_packet(s, "OK");
2493         } else {
2494             put_packet(s, "E22");
2495         }
2496         break;
2497     case 'q':
2498     case 'Q':
2499         /* parse any 'q' packets here */
2500         if (!strcmp(p,"qemu.sstepbits")) {
2501             /* Query Breakpoint bit definitions */
2502             snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
2503                      SSTEP_ENABLE,
2504                      SSTEP_NOIRQ,
2505                      SSTEP_NOTIMER);
2506             put_packet(s, buf);
2507             break;
2508         } else if (strncmp(p,"qemu.sstep",10) == 0) {
2509             /* Display or change the sstep_flags */
2510             p += 10;
2511             if (*p != '=') {
2512                 /* Display current setting */
2513                 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
2514                 put_packet(s, buf);
2515                 break;
2516             }
2517             p++;
2518             type = strtoul(p, (char **)&p, 16);
2519             sstep_flags = type;
2520             put_packet(s, "OK");
2521             break;
2522         } else if (strcmp(p,"C") == 0) {
2523             /* "Current thread" remains vague in the spec, so always return
2524              *  the first CPU (gdb returns the first thread). */
2525             put_packet(s, "QC1");
2526             break;
2527         } else if (strcmp(p,"fThreadInfo") == 0) {
2528             s->query_cpu = first_cpu;
2529             goto report_cpuinfo;
2530         } else if (strcmp(p,"sThreadInfo") == 0) {
2531         report_cpuinfo:
2532             if (s->query_cpu) {
2533                 snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu));
2534                 put_packet(s, buf);
2535                 s->query_cpu = s->query_cpu->next_cpu;
2536             } else
2537                 put_packet(s, "l");
2538             break;
2539         } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
2540             thread = strtoull(p+16, (char **)&p, 16);
2541             cpu = find_cpu(thread);
2542             if (cpu != NULL) {
2543                 cpu_synchronize_state(cpu);
2544                 len = snprintf((char *)mem_buf, sizeof(mem_buf),
2545                                "CPU#%d [%s]", cpu->cpu_index,
2546                                cpu->halted ? "halted " : "running");
2547                 memtohex(buf, mem_buf, len);
2548                 put_packet(s, buf);
2549             }
2550             break;
2551         }
2552 #ifdef CONFIG_USER_ONLY
2553         else if (strncmp(p, "Offsets", 7) == 0) {
2554             CPUArchState *env = s->c_cpu->env_ptr;
2555             TaskState *ts = env->opaque;
2556
2557             snprintf(buf, sizeof(buf),
2558                      "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
2559                      ";Bss=" TARGET_ABI_FMT_lx,
2560                      ts->info->code_offset,
2561                      ts->info->data_offset,
2562                      ts->info->data_offset);
2563             put_packet(s, buf);
2564             break;
2565         }
2566 #else /* !CONFIG_USER_ONLY */
2567         else if (strncmp(p, "Rcmd,", 5) == 0) {
2568             int len = strlen(p + 5);
2569
2570             if ((len % 2) != 0) {
2571                 put_packet(s, "E01");
2572                 break;
2573             }
2574             hextomem(mem_buf, p + 5, len);
2575             len = len / 2;
2576             mem_buf[len++] = 0;
2577             qemu_chr_be_write(s->mon_chr, mem_buf, len);
2578             put_packet(s, "OK");
2579             break;
2580         }
2581 #endif /* !CONFIG_USER_ONLY */
2582         if (strncmp(p, "Supported", 9) == 0) {
2583             snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
2584 #ifdef GDB_CORE_XML
2585             pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
2586 #endif
2587             put_packet(s, buf);
2588             break;
2589         }
2590 #ifdef GDB_CORE_XML
2591         if (strncmp(p, "Xfer:features:read:", 19) == 0) {
2592             const char *xml;
2593             target_ulong total_len;
2594
2595             gdb_has_xml = 1;
2596             p += 19;
2597             xml = get_feature_xml(p, &p);
2598             if (!xml) {
2599                 snprintf(buf, sizeof(buf), "E00");
2600                 put_packet(s, buf);
2601                 break;
2602             }
2603
2604             if (*p == ':')
2605                 p++;
2606             addr = strtoul(p, (char **)&p, 16);
2607             if (*p == ',')
2608                 p++;
2609             len = strtoul(p, (char **)&p, 16);
2610
2611             total_len = strlen(xml);
2612             if (addr > total_len) {
2613                 snprintf(buf, sizeof(buf), "E00");
2614                 put_packet(s, buf);
2615                 break;
2616             }
2617             if (len > (MAX_PACKET_LENGTH - 5) / 2)
2618                 len = (MAX_PACKET_LENGTH - 5) / 2;
2619             if (len < total_len - addr) {
2620                 buf[0] = 'm';
2621                 len = memtox(buf + 1, xml + addr, len);
2622             } else {
2623                 buf[0] = 'l';
2624                 len = memtox(buf + 1, xml + addr, total_len - addr);
2625             }
2626             put_packet_binary(s, buf, len + 1);
2627             break;
2628         }
2629 #endif
2630         /* Unrecognised 'q' command.  */
2631         goto unknown_command;
2632
2633     default:
2634     unknown_command:
2635         /* put empty packet */
2636         buf[0] = '\0';
2637         put_packet(s, buf);
2638         break;
2639     }
2640     return RS_IDLE;
2641 }
2642
2643 void gdb_set_stop_cpu(CPUState *cpu)
2644 {
2645     gdbserver_state->c_cpu = cpu;
2646     gdbserver_state->g_cpu = cpu;
2647 }
2648
2649 #ifndef CONFIG_USER_ONLY
2650 static void gdb_vm_state_change(void *opaque, int running, RunState state)
2651 {
2652     GDBState *s = gdbserver_state;
2653     CPUArchState *env = s->c_cpu->env_ptr;
2654     CPUState *cpu = s->c_cpu;
2655     char buf[256];
2656     const char *type;
2657     int ret;
2658
2659     if (running || s->state == RS_INACTIVE) {
2660         return;
2661     }
2662     /* Is there a GDB syscall waiting to be sent?  */
2663     if (s->current_syscall_cb) {
2664         put_packet(s, s->syscall_buf);
2665         return;
2666     }
2667     switch (state) {
2668     case RUN_STATE_DEBUG:
2669         if (env->watchpoint_hit) {
2670             switch (env->watchpoint_hit->flags & BP_MEM_ACCESS) {
2671             case BP_MEM_READ:
2672                 type = "r";
2673                 break;
2674             case BP_MEM_ACCESS:
2675                 type = "a";
2676                 break;
2677             default:
2678                 type = "";
2679                 break;
2680             }
2681             snprintf(buf, sizeof(buf),
2682                      "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
2683                      GDB_SIGNAL_TRAP, cpu_index(cpu), type,
2684                      env->watchpoint_hit->vaddr);
2685             env->watchpoint_hit = NULL;
2686             goto send_packet;
2687         }
2688         tb_flush(env);
2689         ret = GDB_SIGNAL_TRAP;
2690         break;
2691     case RUN_STATE_PAUSED:
2692         ret = GDB_SIGNAL_INT;
2693         break;
2694     case RUN_STATE_SHUTDOWN:
2695         ret = GDB_SIGNAL_QUIT;
2696         break;
2697     case RUN_STATE_IO_ERROR:
2698         ret = GDB_SIGNAL_IO;
2699         break;
2700     case RUN_STATE_WATCHDOG:
2701         ret = GDB_SIGNAL_ALRM;
2702         break;
2703     case RUN_STATE_INTERNAL_ERROR:
2704         ret = GDB_SIGNAL_ABRT;
2705         break;
2706     case RUN_STATE_SAVE_VM:
2707     case RUN_STATE_RESTORE_VM:
2708         return;
2709     case RUN_STATE_FINISH_MIGRATE:
2710         ret = GDB_SIGNAL_XCPU;
2711         break;
2712     default:
2713         ret = GDB_SIGNAL_UNKNOWN;
2714         break;
2715     }
2716     snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(cpu));
2717
2718 send_packet:
2719     put_packet(s, buf);
2720
2721     /* disable single step if it was enabled */
2722     cpu_single_step(cpu, 0);
2723 }
2724 #endif
2725
2726 /* Send a gdb syscall request.
2727    This accepts limited printf-style format specifiers, specifically:
2728     %x  - target_ulong argument printed in hex.
2729     %lx - 64-bit argument printed in hex.
2730     %s  - string pointer (target_ulong) and length (int) pair.  */
2731 void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
2732 {
2733     va_list va;
2734     char *p;
2735     char *p_end;
2736     target_ulong addr;
2737     uint64_t i64;
2738     GDBState *s;
2739
2740     s = gdbserver_state;
2741     if (!s)
2742         return;
2743     s->current_syscall_cb = cb;
2744 #ifndef CONFIG_USER_ONLY
2745     vm_stop(RUN_STATE_DEBUG);
2746 #endif
2747     va_start(va, fmt);
2748     p = s->syscall_buf;
2749     p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
2750     *(p++) = 'F';
2751     while (*fmt) {
2752         if (*fmt == '%') {
2753             fmt++;
2754             switch (*fmt++) {
2755             case 'x':
2756                 addr = va_arg(va, target_ulong);
2757                 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
2758                 break;
2759             case 'l':
2760                 if (*(fmt++) != 'x')
2761                     goto bad_format;
2762                 i64 = va_arg(va, uint64_t);
2763                 p += snprintf(p, p_end - p, "%" PRIx64, i64);
2764                 break;
2765             case 's':
2766                 addr = va_arg(va, target_ulong);
2767                 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
2768                               addr, va_arg(va, int));
2769                 break;
2770             default:
2771             bad_format:
2772                 fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
2773                         fmt - 1);
2774                 break;
2775             }
2776         } else {
2777             *(p++) = *(fmt++);
2778         }
2779     }
2780     *p = 0;
2781     va_end(va);
2782 #ifdef CONFIG_USER_ONLY
2783     put_packet(s, s->syscall_buf);
2784     gdb_handlesig(s->c_cpu, 0);
2785 #else
2786     /* In this case wait to send the syscall packet until notification that
2787        the CPU has stopped.  This must be done because if the packet is sent
2788        now the reply from the syscall request could be received while the CPU
2789        is still in the running state, which can cause packets to be dropped
2790        and state transition 'T' packets to be sent while the syscall is still
2791        being processed.  */
2792     cpu_exit(s->c_cpu);
2793 #endif
2794 }
2795
2796 static void gdb_read_byte(GDBState *s, int ch)
2797 {
2798     int i, csum;
2799     uint8_t reply;
2800
2801 #ifndef CONFIG_USER_ONLY
2802     if (s->last_packet_len) {
2803         /* Waiting for a response to the last packet.  If we see the start
2804            of a new command then abandon the previous response.  */
2805         if (ch == '-') {
2806 #ifdef DEBUG_GDB
2807             printf("Got NACK, retransmitting\n");
2808 #endif
2809             put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
2810         }
2811 #ifdef DEBUG_GDB
2812         else if (ch == '+')
2813             printf("Got ACK\n");
2814         else
2815             printf("Got '%c' when expecting ACK/NACK\n", ch);
2816 #endif
2817         if (ch == '+' || ch == '$')
2818             s->last_packet_len = 0;
2819         if (ch != '$')
2820             return;
2821     }
2822     if (runstate_is_running()) {
2823         /* when the CPU is running, we cannot do anything except stop
2824            it when receiving a char */
2825         vm_stop(RUN_STATE_PAUSED);
2826     } else
2827 #endif
2828     {
2829         switch(s->state) {
2830         case RS_IDLE:
2831             if (ch == '$') {
2832                 s->line_buf_index = 0;
2833                 s->state = RS_GETLINE;
2834             }
2835             break;
2836         case RS_GETLINE:
2837             if (ch == '#') {
2838             s->state = RS_CHKSUM1;
2839             } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2840                 s->state = RS_IDLE;
2841             } else {
2842             s->line_buf[s->line_buf_index++] = ch;
2843             }
2844             break;
2845         case RS_CHKSUM1:
2846             s->line_buf[s->line_buf_index] = '\0';
2847             s->line_csum = fromhex(ch) << 4;
2848             s->state = RS_CHKSUM2;
2849             break;
2850         case RS_CHKSUM2:
2851             s->line_csum |= fromhex(ch);
2852             csum = 0;
2853             for(i = 0; i < s->line_buf_index; i++) {
2854                 csum += s->line_buf[i];
2855             }
2856             if (s->line_csum != (csum & 0xff)) {
2857                 reply = '-';
2858                 put_buffer(s, &reply, 1);
2859                 s->state = RS_IDLE;
2860             } else {
2861                 reply = '+';
2862                 put_buffer(s, &reply, 1);
2863                 s->state = gdb_handle_packet(s, s->line_buf);
2864             }
2865             break;
2866         default:
2867             abort();
2868         }
2869     }
2870 }
2871
2872 /* Tell the remote gdb that the process has exited.  */
2873 void gdb_exit(CPUArchState *env, int code)
2874 {
2875   GDBState *s;
2876   char buf[4];
2877
2878   s = gdbserver_state;
2879   if (!s) {
2880       return;
2881   }
2882 #ifdef CONFIG_USER_ONLY
2883   if (gdbserver_fd < 0 || s->fd < 0) {
2884       return;
2885   }
2886 #endif
2887
2888   snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
2889   put_packet(s, buf);
2890
2891 #ifndef CONFIG_USER_ONLY
2892   if (s->chr) {
2893       qemu_chr_delete(s->chr);
2894   }
2895 #endif
2896 }
2897
2898 #ifdef CONFIG_USER_ONLY
2899 int
2900 gdb_queuesig (void)
2901 {
2902     GDBState *s;
2903
2904     s = gdbserver_state;
2905
2906     if (gdbserver_fd < 0 || s->fd < 0)
2907         return 0;
2908     else
2909         return 1;
2910 }
2911
2912 int
2913 gdb_handlesig(CPUState *cpu, int sig)
2914 {
2915     CPUArchState *env = cpu->env_ptr;
2916     GDBState *s;
2917     char buf[256];
2918     int n;
2919
2920     s = gdbserver_state;
2921     if (gdbserver_fd < 0 || s->fd < 0) {
2922         return sig;
2923     }
2924
2925     /* disable single step if it was enabled */
2926     cpu_single_step(cpu, 0);
2927     tb_flush(env);
2928
2929     if (sig != 0) {
2930         snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
2931         put_packet(s, buf);
2932     }
2933     /* put_packet() might have detected that the peer terminated the
2934        connection.  */
2935     if (s->fd < 0) {
2936         return sig;
2937     }
2938
2939     sig = 0;
2940     s->state = RS_IDLE;
2941     s->running_state = 0;
2942     while (s->running_state == 0) {
2943         n = read(s->fd, buf, 256);
2944         if (n > 0) {
2945             int i;
2946
2947             for (i = 0; i < n; i++) {
2948                 gdb_read_byte(s, buf[i]);
2949             }
2950         } else if (n == 0 || errno != EAGAIN) {
2951             /* XXX: Connection closed.  Should probably wait for another
2952                connection before continuing.  */
2953             return sig;
2954         }
2955     }
2956     sig = s->signal;
2957     s->signal = 0;
2958     return sig;
2959 }
2960
2961 /* Tell the remote gdb that the process has exited due to SIG.  */
2962 void gdb_signalled(CPUArchState *env, int sig)
2963 {
2964     GDBState *s;
2965     char buf[4];
2966
2967     s = gdbserver_state;
2968     if (gdbserver_fd < 0 || s->fd < 0) {
2969         return;
2970     }
2971
2972     snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
2973     put_packet(s, buf);
2974 }
2975
2976 static void gdb_accept(void)
2977 {
2978     GDBState *s;
2979     struct sockaddr_in sockaddr;
2980     socklen_t len;
2981     int fd;
2982
2983     for(;;) {
2984         len = sizeof(sockaddr);
2985         fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
2986         if (fd < 0 && errno != EINTR) {
2987             perror("accept");
2988             return;
2989         } else if (fd >= 0) {
2990 #ifndef _WIN32
2991             fcntl(fd, F_SETFD, FD_CLOEXEC);
2992 #endif
2993             break;
2994         }
2995     }
2996
2997     /* set short latency */
2998     socket_set_nodelay(fd);
2999
3000     s = g_malloc0(sizeof(GDBState));
3001     s->c_cpu = first_cpu;
3002     s->g_cpu = first_cpu;
3003     s->fd = fd;
3004     gdb_has_xml = 0;
3005
3006     gdbserver_state = s;
3007
3008     fcntl(fd, F_SETFL, O_NONBLOCK);
3009 }
3010
3011 static int gdbserver_open(int port)
3012 {
3013     struct sockaddr_in sockaddr;
3014     int fd, val, ret;
3015
3016     fd = socket(PF_INET, SOCK_STREAM, 0);
3017     if (fd < 0) {
3018         perror("socket");
3019         return -1;
3020     }
3021 #ifndef _WIN32
3022     fcntl(fd, F_SETFD, FD_CLOEXEC);
3023 #endif
3024
3025     /* allow fast reuse */
3026     val = 1;
3027     qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
3028
3029     sockaddr.sin_family = AF_INET;
3030     sockaddr.sin_port = htons(port);
3031     sockaddr.sin_addr.s_addr = 0;
3032     ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
3033     if (ret < 0) {
3034         perror("bind");
3035         close(fd);
3036         return -1;
3037     }
3038     ret = listen(fd, 0);
3039     if (ret < 0) {
3040         perror("listen");
3041         close(fd);
3042         return -1;
3043     }
3044     return fd;
3045 }
3046
3047 int gdbserver_start(int port)
3048 {
3049     gdbserver_fd = gdbserver_open(port);
3050     if (gdbserver_fd < 0)
3051         return -1;
3052     /* accept connections */
3053     gdb_accept();
3054     return 0;
3055 }
3056
3057 /* Disable gdb stub for child processes.  */
3058 void gdbserver_fork(CPUArchState *env)
3059 {
3060     GDBState *s = gdbserver_state;
3061     if (gdbserver_fd < 0 || s->fd < 0)
3062       return;
3063     close(s->fd);
3064     s->fd = -1;
3065     cpu_breakpoint_remove_all(env, BP_GDB);
3066     cpu_watchpoint_remove_all(env, BP_GDB);
3067 }
3068 #else
3069 static int gdb_chr_can_receive(void *opaque)
3070 {
3071   /* We can handle an arbitrarily large amount of data.
3072    Pick the maximum packet size, which is as good as anything.  */
3073   return MAX_PACKET_LENGTH;
3074 }
3075
3076 static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
3077 {
3078     int i;
3079
3080     for (i = 0; i < size; i++) {
3081         gdb_read_byte(gdbserver_state, buf[i]);
3082     }
3083 }
3084
3085 static void gdb_chr_event(void *opaque, int event)
3086 {
3087     switch (event) {
3088     case CHR_EVENT_OPENED:
3089         vm_stop(RUN_STATE_PAUSED);
3090         gdb_has_xml = 0;
3091         break;
3092     default:
3093         break;
3094     }
3095 }
3096
3097 static void gdb_monitor_output(GDBState *s, const char *msg, int len)
3098 {
3099     char buf[MAX_PACKET_LENGTH];
3100
3101     buf[0] = 'O';
3102     if (len > (MAX_PACKET_LENGTH/2) - 1)
3103         len = (MAX_PACKET_LENGTH/2) - 1;
3104     memtohex(buf + 1, (uint8_t *)msg, len);
3105     put_packet(s, buf);
3106 }
3107
3108 static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
3109 {
3110     const char *p = (const char *)buf;
3111     int max_sz;
3112
3113     max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
3114     for (;;) {
3115         if (len <= max_sz) {
3116             gdb_monitor_output(gdbserver_state, p, len);
3117             break;
3118         }
3119         gdb_monitor_output(gdbserver_state, p, max_sz);
3120         p += max_sz;
3121         len -= max_sz;
3122     }
3123     return len;
3124 }
3125
3126 #ifndef _WIN32
3127 static void gdb_sigterm_handler(int signal)
3128 {
3129     if (runstate_is_running()) {
3130         vm_stop(RUN_STATE_PAUSED);
3131     }
3132 }
3133 #endif
3134
3135 int gdbserver_start(const char *device)
3136 {
3137     GDBState *s;
3138     char gdbstub_device_name[128];
3139     CharDriverState *chr = NULL;
3140     CharDriverState *mon_chr;
3141
3142     if (!device)
3143         return -1;
3144     if (strcmp(device, "none") != 0) {
3145         if (strstart(device, "tcp:", NULL)) {
3146             /* enforce required TCP attributes */
3147             snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
3148                      "%s,nowait,nodelay,server", device);
3149             device = gdbstub_device_name;
3150         }
3151 #ifndef _WIN32
3152         else if (strcmp(device, "stdio") == 0) {
3153             struct sigaction act;
3154
3155             memset(&act, 0, sizeof(act));
3156             act.sa_handler = gdb_sigterm_handler;
3157             sigaction(SIGINT, &act, NULL);
3158         }
3159 #endif
3160         chr = qemu_chr_new("gdb", device, NULL);
3161         if (!chr)
3162             return -1;
3163
3164         qemu_chr_fe_claim_no_fail(chr);
3165         qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
3166                               gdb_chr_event, NULL);
3167     }
3168
3169     s = gdbserver_state;
3170     if (!s) {
3171         s = g_malloc0(sizeof(GDBState));
3172         gdbserver_state = s;
3173
3174         qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
3175
3176         /* Initialize a monitor terminal for gdb */
3177         mon_chr = g_malloc0(sizeof(*mon_chr));
3178         mon_chr->chr_write = gdb_monitor_write;
3179         monitor_init(mon_chr, 0);
3180     } else {
3181         if (s->chr)
3182             qemu_chr_delete(s->chr);
3183         mon_chr = s->mon_chr;
3184         memset(s, 0, sizeof(GDBState));
3185     }
3186     s->c_cpu = first_cpu;
3187     s->g_cpu = first_cpu;
3188     s->chr = chr;
3189     s->state = chr ? RS_IDLE : RS_INACTIVE;
3190     s->mon_chr = mon_chr;
3191     s->current_syscall_cb = NULL;
3192
3193     return 0;
3194 }
3195 #endif