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