Statistics
| Branch: | Revision:

root / gdbstub.c @ 9c22a623

History | View | Annotate | Download (60 kB)

1 b4608c04 bellard
/*
2 b4608c04 bellard
 * gdb server stub
3 5fafdf24 ths
 *
4 3475187d bellard
 * Copyright (c) 2003-2005 Fabrice Bellard
5 b4608c04 bellard
 *
6 b4608c04 bellard
 * This library is free software; you can redistribute it and/or
7 b4608c04 bellard
 * modify it under the terms of the GNU Lesser General Public
8 b4608c04 bellard
 * License as published by the Free Software Foundation; either
9 b4608c04 bellard
 * version 2 of the License, or (at your option) any later version.
10 b4608c04 bellard
 *
11 b4608c04 bellard
 * This library is distributed in the hope that it will be useful,
12 b4608c04 bellard
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 b4608c04 bellard
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 b4608c04 bellard
 * Lesser General Public License for more details.
15 b4608c04 bellard
 *
16 b4608c04 bellard
 * You should have received a copy of the GNU Lesser General Public
17 b4608c04 bellard
 * License along with this library; if not, write to the Free Software
18 fad6cb1a aurel32
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
19 b4608c04 bellard
 */
20 978efd6a pbrook
#include "config.h"
21 56aebc89 pbrook
#include "qemu-common.h"
22 1fddef4b bellard
#ifdef CONFIG_USER_ONLY
23 1fddef4b bellard
#include <stdlib.h>
24 1fddef4b bellard
#include <stdio.h>
25 1fddef4b bellard
#include <stdarg.h>
26 1fddef4b bellard
#include <string.h>
27 1fddef4b bellard
#include <errno.h>
28 1fddef4b bellard
#include <unistd.h>
29 978efd6a pbrook
#include <fcntl.h>
30 1fddef4b bellard
31 1fddef4b bellard
#include "qemu.h"
32 1fddef4b bellard
#else
33 8a34a0fb aliguori
#include "monitor.h"
34 87ecb68b pbrook
#include "qemu-char.h"
35 87ecb68b pbrook
#include "sysemu.h"
36 87ecb68b pbrook
#include "gdbstub.h"
37 1fddef4b bellard
#endif
38 67b915a5 bellard
39 56aebc89 pbrook
#define MAX_PACKET_LENGTH 4096
40 56aebc89 pbrook
41 8f447cc7 bellard
#include "qemu_socket.h"
42 e22a25c9 aliguori
#include "kvm.h"
43 ca587a8e aurel32
44 ca587a8e aurel32
45 ca587a8e aurel32
enum {
46 ca587a8e aurel32
    GDB_SIGNAL_0 = 0,
47 ca587a8e aurel32
    GDB_SIGNAL_INT = 2,
48 ca587a8e aurel32
    GDB_SIGNAL_TRAP = 5,
49 ca587a8e aurel32
    GDB_SIGNAL_UNKNOWN = 143
50 ca587a8e aurel32
};
51 ca587a8e aurel32
52 ca587a8e aurel32
#ifdef CONFIG_USER_ONLY
53 ca587a8e aurel32
54 ca587a8e aurel32
/* Map target signal numbers to GDB protocol signal numbers and vice
55 ca587a8e aurel32
 * versa.  For user emulation's currently supported systems, we can
56 ca587a8e aurel32
 * assume most signals are defined.
57 ca587a8e aurel32
 */
58 ca587a8e aurel32
59 ca587a8e aurel32
static int gdb_signal_table[] = {
60 ca587a8e aurel32
    0,
61 ca587a8e aurel32
    TARGET_SIGHUP,
62 ca587a8e aurel32
    TARGET_SIGINT,
63 ca587a8e aurel32
    TARGET_SIGQUIT,
64 ca587a8e aurel32
    TARGET_SIGILL,
65 ca587a8e aurel32
    TARGET_SIGTRAP,
66 ca587a8e aurel32
    TARGET_SIGABRT,
67 ca587a8e aurel32
    -1, /* SIGEMT */
68 ca587a8e aurel32
    TARGET_SIGFPE,
69 ca587a8e aurel32
    TARGET_SIGKILL,
70 ca587a8e aurel32
    TARGET_SIGBUS,
71 ca587a8e aurel32
    TARGET_SIGSEGV,
72 ca587a8e aurel32
    TARGET_SIGSYS,
73 ca587a8e aurel32
    TARGET_SIGPIPE,
74 ca587a8e aurel32
    TARGET_SIGALRM,
75 ca587a8e aurel32
    TARGET_SIGTERM,
76 ca587a8e aurel32
    TARGET_SIGURG,
77 ca587a8e aurel32
    TARGET_SIGSTOP,
78 ca587a8e aurel32
    TARGET_SIGTSTP,
79 ca587a8e aurel32
    TARGET_SIGCONT,
80 ca587a8e aurel32
    TARGET_SIGCHLD,
81 ca587a8e aurel32
    TARGET_SIGTTIN,
82 ca587a8e aurel32
    TARGET_SIGTTOU,
83 ca587a8e aurel32
    TARGET_SIGIO,
84 ca587a8e aurel32
    TARGET_SIGXCPU,
85 ca587a8e aurel32
    TARGET_SIGXFSZ,
86 ca587a8e aurel32
    TARGET_SIGVTALRM,
87 ca587a8e aurel32
    TARGET_SIGPROF,
88 ca587a8e aurel32
    TARGET_SIGWINCH,
89 ca587a8e aurel32
    -1, /* SIGLOST */
90 ca587a8e aurel32
    TARGET_SIGUSR1,
91 ca587a8e aurel32
    TARGET_SIGUSR2,
92 c72d5bf8 blueswir1
#ifdef TARGET_SIGPWR
93 ca587a8e aurel32
    TARGET_SIGPWR,
94 c72d5bf8 blueswir1
#else
95 c72d5bf8 blueswir1
    -1,
96 c72d5bf8 blueswir1
#endif
97 ca587a8e aurel32
    -1, /* SIGPOLL */
98 ca587a8e aurel32
    -1,
99 ca587a8e aurel32
    -1,
100 ca587a8e aurel32
    -1,
101 ca587a8e aurel32
    -1,
102 ca587a8e aurel32
    -1,
103 ca587a8e aurel32
    -1,
104 ca587a8e aurel32
    -1,
105 ca587a8e aurel32
    -1,
106 ca587a8e aurel32
    -1,
107 ca587a8e aurel32
    -1,
108 ca587a8e aurel32
    -1,
109 c72d5bf8 blueswir1
#ifdef __SIGRTMIN
110 ca587a8e aurel32
    __SIGRTMIN + 1,
111 ca587a8e aurel32
    __SIGRTMIN + 2,
112 ca587a8e aurel32
    __SIGRTMIN + 3,
113 ca587a8e aurel32
    __SIGRTMIN + 4,
114 ca587a8e aurel32
    __SIGRTMIN + 5,
115 ca587a8e aurel32
    __SIGRTMIN + 6,
116 ca587a8e aurel32
    __SIGRTMIN + 7,
117 ca587a8e aurel32
    __SIGRTMIN + 8,
118 ca587a8e aurel32
    __SIGRTMIN + 9,
119 ca587a8e aurel32
    __SIGRTMIN + 10,
120 ca587a8e aurel32
    __SIGRTMIN + 11,
121 ca587a8e aurel32
    __SIGRTMIN + 12,
122 ca587a8e aurel32
    __SIGRTMIN + 13,
123 ca587a8e aurel32
    __SIGRTMIN + 14,
124 ca587a8e aurel32
    __SIGRTMIN + 15,
125 ca587a8e aurel32
    __SIGRTMIN + 16,
126 ca587a8e aurel32
    __SIGRTMIN + 17,
127 ca587a8e aurel32
    __SIGRTMIN + 18,
128 ca587a8e aurel32
    __SIGRTMIN + 19,
129 ca587a8e aurel32
    __SIGRTMIN + 20,
130 ca587a8e aurel32
    __SIGRTMIN + 21,
131 ca587a8e aurel32
    __SIGRTMIN + 22,
132 ca587a8e aurel32
    __SIGRTMIN + 23,
133 ca587a8e aurel32
    __SIGRTMIN + 24,
134 ca587a8e aurel32
    __SIGRTMIN + 25,
135 ca587a8e aurel32
    __SIGRTMIN + 26,
136 ca587a8e aurel32
    __SIGRTMIN + 27,
137 ca587a8e aurel32
    __SIGRTMIN + 28,
138 ca587a8e aurel32
    __SIGRTMIN + 29,
139 ca587a8e aurel32
    __SIGRTMIN + 30,
140 ca587a8e aurel32
    __SIGRTMIN + 31,
141 ca587a8e aurel32
    -1, /* SIGCANCEL */
142 ca587a8e aurel32
    __SIGRTMIN,
143 ca587a8e aurel32
    __SIGRTMIN + 32,
144 ca587a8e aurel32
    __SIGRTMIN + 33,
145 ca587a8e aurel32
    __SIGRTMIN + 34,
146 ca587a8e aurel32
    __SIGRTMIN + 35,
147 ca587a8e aurel32
    __SIGRTMIN + 36,
148 ca587a8e aurel32
    __SIGRTMIN + 37,
149 ca587a8e aurel32
    __SIGRTMIN + 38,
150 ca587a8e aurel32
    __SIGRTMIN + 39,
151 ca587a8e aurel32
    __SIGRTMIN + 40,
152 ca587a8e aurel32
    __SIGRTMIN + 41,
153 ca587a8e aurel32
    __SIGRTMIN + 42,
154 ca587a8e aurel32
    __SIGRTMIN + 43,
155 ca587a8e aurel32
    __SIGRTMIN + 44,
156 ca587a8e aurel32
    __SIGRTMIN + 45,
157 ca587a8e aurel32
    __SIGRTMIN + 46,
158 ca587a8e aurel32
    __SIGRTMIN + 47,
159 ca587a8e aurel32
    __SIGRTMIN + 48,
160 ca587a8e aurel32
    __SIGRTMIN + 49,
161 ca587a8e aurel32
    __SIGRTMIN + 50,
162 ca587a8e aurel32
    __SIGRTMIN + 51,
163 ca587a8e aurel32
    __SIGRTMIN + 52,
164 ca587a8e aurel32
    __SIGRTMIN + 53,
165 ca587a8e aurel32
    __SIGRTMIN + 54,
166 ca587a8e aurel32
    __SIGRTMIN + 55,
167 ca587a8e aurel32
    __SIGRTMIN + 56,
168 ca587a8e aurel32
    __SIGRTMIN + 57,
169 ca587a8e aurel32
    __SIGRTMIN + 58,
170 ca587a8e aurel32
    __SIGRTMIN + 59,
171 ca587a8e aurel32
    __SIGRTMIN + 60,
172 ca587a8e aurel32
    __SIGRTMIN + 61,
173 ca587a8e aurel32
    __SIGRTMIN + 62,
174 ca587a8e aurel32
    __SIGRTMIN + 63,
175 ca587a8e aurel32
    __SIGRTMIN + 64,
176 ca587a8e aurel32
    __SIGRTMIN + 65,
177 ca587a8e aurel32
    __SIGRTMIN + 66,
178 ca587a8e aurel32
    __SIGRTMIN + 67,
179 ca587a8e aurel32
    __SIGRTMIN + 68,
180 ca587a8e aurel32
    __SIGRTMIN + 69,
181 ca587a8e aurel32
    __SIGRTMIN + 70,
182 ca587a8e aurel32
    __SIGRTMIN + 71,
183 ca587a8e aurel32
    __SIGRTMIN + 72,
184 ca587a8e aurel32
    __SIGRTMIN + 73,
185 ca587a8e aurel32
    __SIGRTMIN + 74,
186 ca587a8e aurel32
    __SIGRTMIN + 75,
187 ca587a8e aurel32
    __SIGRTMIN + 76,
188 ca587a8e aurel32
    __SIGRTMIN + 77,
189 ca587a8e aurel32
    __SIGRTMIN + 78,
190 ca587a8e aurel32
    __SIGRTMIN + 79,
191 ca587a8e aurel32
    __SIGRTMIN + 80,
192 ca587a8e aurel32
    __SIGRTMIN + 81,
193 ca587a8e aurel32
    __SIGRTMIN + 82,
194 ca587a8e aurel32
    __SIGRTMIN + 83,
195 ca587a8e aurel32
    __SIGRTMIN + 84,
196 ca587a8e aurel32
    __SIGRTMIN + 85,
197 ca587a8e aurel32
    __SIGRTMIN + 86,
198 ca587a8e aurel32
    __SIGRTMIN + 87,
199 ca587a8e aurel32
    __SIGRTMIN + 88,
200 ca587a8e aurel32
    __SIGRTMIN + 89,
201 ca587a8e aurel32
    __SIGRTMIN + 90,
202 ca587a8e aurel32
    __SIGRTMIN + 91,
203 ca587a8e aurel32
    __SIGRTMIN + 92,
204 ca587a8e aurel32
    __SIGRTMIN + 93,
205 ca587a8e aurel32
    __SIGRTMIN + 94,
206 ca587a8e aurel32
    __SIGRTMIN + 95,
207 ca587a8e aurel32
    -1, /* SIGINFO */
208 ca587a8e aurel32
    -1, /* UNKNOWN */
209 ca587a8e aurel32
    -1, /* DEFAULT */
210 ca587a8e aurel32
    -1,
211 ca587a8e aurel32
    -1,
212 ca587a8e aurel32
    -1,
213 ca587a8e aurel32
    -1,
214 ca587a8e aurel32
    -1,
215 ca587a8e aurel32
    -1
216 c72d5bf8 blueswir1
#endif
217 ca587a8e aurel32
};
218 8f447cc7 bellard
#else
219 ca587a8e aurel32
/* In system mode we only need SIGINT and SIGTRAP; other signals
220 ca587a8e aurel32
   are not yet supported.  */
221 ca587a8e aurel32
222 ca587a8e aurel32
enum {
223 ca587a8e aurel32
    TARGET_SIGINT = 2,
224 ca587a8e aurel32
    TARGET_SIGTRAP = 5
225 ca587a8e aurel32
};
226 ca587a8e aurel32
227 ca587a8e aurel32
static int gdb_signal_table[] = {
228 ca587a8e aurel32
    -1,
229 ca587a8e aurel32
    -1,
230 ca587a8e aurel32
    TARGET_SIGINT,
231 ca587a8e aurel32
    -1,
232 ca587a8e aurel32
    -1,
233 ca587a8e aurel32
    TARGET_SIGTRAP
234 ca587a8e aurel32
};
235 ca587a8e aurel32
#endif
236 ca587a8e aurel32
237 ca587a8e aurel32
#ifdef CONFIG_USER_ONLY
238 ca587a8e aurel32
static int target_signal_to_gdb (int sig)
239 ca587a8e aurel32
{
240 ca587a8e aurel32
    int i;
241 ca587a8e aurel32
    for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
242 ca587a8e aurel32
        if (gdb_signal_table[i] == sig)
243 ca587a8e aurel32
            return i;
244 ca587a8e aurel32
    return GDB_SIGNAL_UNKNOWN;
245 ca587a8e aurel32
}
246 8f447cc7 bellard
#endif
247 b4608c04 bellard
248 ca587a8e aurel32
static int gdb_signal_to_target (int sig)
249 ca587a8e aurel32
{
250 ca587a8e aurel32
    if (sig < ARRAY_SIZE (gdb_signal_table))
251 ca587a8e aurel32
        return gdb_signal_table[sig];
252 ca587a8e aurel32
    else
253 ca587a8e aurel32
        return -1;
254 ca587a8e aurel32
}
255 ca587a8e aurel32
256 4abe615b bellard
//#define DEBUG_GDB
257 b4608c04 bellard
258 56aebc89 pbrook
typedef struct GDBRegisterState {
259 56aebc89 pbrook
    int base_reg;
260 56aebc89 pbrook
    int num_regs;
261 56aebc89 pbrook
    gdb_reg_cb get_reg;
262 56aebc89 pbrook
    gdb_reg_cb set_reg;
263 56aebc89 pbrook
    const char *xml;
264 56aebc89 pbrook
    struct GDBRegisterState *next;
265 56aebc89 pbrook
} GDBRegisterState;
266 56aebc89 pbrook
267 858693c6 bellard
enum RSState {
268 36556b20 aliguori
    RS_INACTIVE,
269 858693c6 bellard
    RS_IDLE,
270 858693c6 bellard
    RS_GETLINE,
271 858693c6 bellard
    RS_CHKSUM1,
272 858693c6 bellard
    RS_CHKSUM2,
273 a2d1ebaf pbrook
    RS_SYSCALL,
274 858693c6 bellard
};
275 858693c6 bellard
typedef struct GDBState {
276 880a7578 aliguori
    CPUState *c_cpu; /* current CPU for step/continue ops */
277 880a7578 aliguori
    CPUState *g_cpu; /* current CPU for other ops */
278 880a7578 aliguori
    CPUState *query_cpu; /* for q{f|s}ThreadInfo */
279 41625033 bellard
    enum RSState state; /* parsing state */
280 56aebc89 pbrook
    char line_buf[MAX_PACKET_LENGTH];
281 858693c6 bellard
    int line_buf_index;
282 858693c6 bellard
    int line_csum;
283 56aebc89 pbrook
    uint8_t last_packet[MAX_PACKET_LENGTH + 4];
284 4046d913 pbrook
    int last_packet_len;
285 1f487ee9 edgar_igl
    int signal;
286 41625033 bellard
#ifdef CONFIG_USER_ONLY
287 4046d913 pbrook
    int fd;
288 41625033 bellard
    int running_state;
289 4046d913 pbrook
#else
290 4046d913 pbrook
    CharDriverState *chr;
291 8a34a0fb aliguori
    CharDriverState *mon_chr;
292 41625033 bellard
#endif
293 858693c6 bellard
} GDBState;
294 b4608c04 bellard
295 60897d36 edgar_igl
/* By default use no IRQs and no timers while single stepping so as to
296 60897d36 edgar_igl
 * make single stepping like an ICE HW step.
297 60897d36 edgar_igl
 */
298 60897d36 edgar_igl
static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
299 60897d36 edgar_igl
300 880a7578 aliguori
static GDBState *gdbserver_state;
301 880a7578 aliguori
302 56aebc89 pbrook
/* This is an ugly hack to cope with both new and old gdb.
303 56aebc89 pbrook
   If gdb sends qXfer:features:read then assume we're talking to a newish
304 56aebc89 pbrook
   gdb that understands target descriptions.  */
305 56aebc89 pbrook
static int gdb_has_xml;
306 56aebc89 pbrook
307 1fddef4b bellard
#ifdef CONFIG_USER_ONLY
308 4046d913 pbrook
/* XXX: This is not thread safe.  Do we care?  */
309 4046d913 pbrook
static int gdbserver_fd = -1;
310 4046d913 pbrook
311 858693c6 bellard
static int get_char(GDBState *s)
312 b4608c04 bellard
{
313 b4608c04 bellard
    uint8_t ch;
314 b4608c04 bellard
    int ret;
315 b4608c04 bellard
316 b4608c04 bellard
    for(;;) {
317 8f447cc7 bellard
        ret = recv(s->fd, &ch, 1, 0);
318 b4608c04 bellard
        if (ret < 0) {
319 1f487ee9 edgar_igl
            if (errno == ECONNRESET)
320 1f487ee9 edgar_igl
                s->fd = -1;
321 b4608c04 bellard
            if (errno != EINTR && errno != EAGAIN)
322 b4608c04 bellard
                return -1;
323 b4608c04 bellard
        } else if (ret == 0) {
324 1f487ee9 edgar_igl
            close(s->fd);
325 1f487ee9 edgar_igl
            s->fd = -1;
326 b4608c04 bellard
            return -1;
327 b4608c04 bellard
        } else {
328 b4608c04 bellard
            break;
329 b4608c04 bellard
        }
330 b4608c04 bellard
    }
331 b4608c04 bellard
    return ch;
332 b4608c04 bellard
}
333 4046d913 pbrook
#endif
334 b4608c04 bellard
335 a2d1ebaf pbrook
static gdb_syscall_complete_cb gdb_current_syscall_cb;
336 a2d1ebaf pbrook
337 654efcf3 blueswir1
static enum {
338 a2d1ebaf pbrook
    GDB_SYS_UNKNOWN,
339 a2d1ebaf pbrook
    GDB_SYS_ENABLED,
340 a2d1ebaf pbrook
    GDB_SYS_DISABLED,
341 a2d1ebaf pbrook
} gdb_syscall_mode;
342 a2d1ebaf pbrook
343 a2d1ebaf pbrook
/* If gdb is connected when the first semihosting syscall occurs then use
344 a2d1ebaf pbrook
   remote gdb syscalls.  Otherwise use native file IO.  */
345 a2d1ebaf pbrook
int use_gdb_syscalls(void)
346 a2d1ebaf pbrook
{
347 a2d1ebaf pbrook
    if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
348 880a7578 aliguori
        gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
349 880a7578 aliguori
                                            : GDB_SYS_DISABLED);
350 a2d1ebaf pbrook
    }
351 a2d1ebaf pbrook
    return gdb_syscall_mode == GDB_SYS_ENABLED;
352 a2d1ebaf pbrook
}
353 a2d1ebaf pbrook
354 ba70a624 edgar_igl
/* Resume execution.  */
355 ba70a624 edgar_igl
static inline void gdb_continue(GDBState *s)
356 ba70a624 edgar_igl
{
357 ba70a624 edgar_igl
#ifdef CONFIG_USER_ONLY
358 ba70a624 edgar_igl
    s->running_state = 1;
359 ba70a624 edgar_igl
#else
360 ba70a624 edgar_igl
    vm_start();
361 ba70a624 edgar_igl
#endif
362 ba70a624 edgar_igl
}
363 ba70a624 edgar_igl
364 858693c6 bellard
static void put_buffer(GDBState *s, const uint8_t *buf, int len)
365 b4608c04 bellard
{
366 4046d913 pbrook
#ifdef CONFIG_USER_ONLY
367 b4608c04 bellard
    int ret;
368 b4608c04 bellard
369 b4608c04 bellard
    while (len > 0) {
370 8f447cc7 bellard
        ret = send(s->fd, buf, len, 0);
371 b4608c04 bellard
        if (ret < 0) {
372 b4608c04 bellard
            if (errno != EINTR && errno != EAGAIN)
373 b4608c04 bellard
                return;
374 b4608c04 bellard
        } else {
375 b4608c04 bellard
            buf += ret;
376 b4608c04 bellard
            len -= ret;
377 b4608c04 bellard
        }
378 b4608c04 bellard
    }
379 4046d913 pbrook
#else
380 4046d913 pbrook
    qemu_chr_write(s->chr, buf, len);
381 4046d913 pbrook
#endif
382 b4608c04 bellard
}
383 b4608c04 bellard
384 b4608c04 bellard
static inline int fromhex(int v)
385 b4608c04 bellard
{
386 b4608c04 bellard
    if (v >= '0' && v <= '9')
387 b4608c04 bellard
        return v - '0';
388 b4608c04 bellard
    else if (v >= 'A' && v <= 'F')
389 b4608c04 bellard
        return v - 'A' + 10;
390 b4608c04 bellard
    else if (v >= 'a' && v <= 'f')
391 b4608c04 bellard
        return v - 'a' + 10;
392 b4608c04 bellard
    else
393 b4608c04 bellard
        return 0;
394 b4608c04 bellard
}
395 b4608c04 bellard
396 b4608c04 bellard
static inline int tohex(int v)
397 b4608c04 bellard
{
398 b4608c04 bellard
    if (v < 10)
399 b4608c04 bellard
        return v + '0';
400 b4608c04 bellard
    else
401 b4608c04 bellard
        return v - 10 + 'a';
402 b4608c04 bellard
}
403 b4608c04 bellard
404 b4608c04 bellard
static void memtohex(char *buf, const uint8_t *mem, int len)
405 b4608c04 bellard
{
406 b4608c04 bellard
    int i, c;
407 b4608c04 bellard
    char *q;
408 b4608c04 bellard
    q = buf;
409 b4608c04 bellard
    for(i = 0; i < len; i++) {
410 b4608c04 bellard
        c = mem[i];
411 b4608c04 bellard
        *q++ = tohex(c >> 4);
412 b4608c04 bellard
        *q++ = tohex(c & 0xf);
413 b4608c04 bellard
    }
414 b4608c04 bellard
    *q = '\0';
415 b4608c04 bellard
}
416 b4608c04 bellard
417 b4608c04 bellard
static void hextomem(uint8_t *mem, const char *buf, int len)
418 b4608c04 bellard
{
419 b4608c04 bellard
    int i;
420 b4608c04 bellard
421 b4608c04 bellard
    for(i = 0; i < len; i++) {
422 b4608c04 bellard
        mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
423 b4608c04 bellard
        buf += 2;
424 b4608c04 bellard
    }
425 b4608c04 bellard
}
426 b4608c04 bellard
427 b4608c04 bellard
/* return -1 if error, 0 if OK */
428 56aebc89 pbrook
static int put_packet_binary(GDBState *s, const char *buf, int len)
429 b4608c04 bellard
{
430 56aebc89 pbrook
    int csum, i;
431 60fe76f3 ths
    uint8_t *p;
432 b4608c04 bellard
433 b4608c04 bellard
    for(;;) {
434 4046d913 pbrook
        p = s->last_packet;
435 4046d913 pbrook
        *(p++) = '$';
436 4046d913 pbrook
        memcpy(p, buf, len);
437 4046d913 pbrook
        p += len;
438 b4608c04 bellard
        csum = 0;
439 b4608c04 bellard
        for(i = 0; i < len; i++) {
440 b4608c04 bellard
            csum += buf[i];
441 b4608c04 bellard
        }
442 4046d913 pbrook
        *(p++) = '#';
443 4046d913 pbrook
        *(p++) = tohex((csum >> 4) & 0xf);
444 4046d913 pbrook
        *(p++) = tohex((csum) & 0xf);
445 b4608c04 bellard
446 4046d913 pbrook
        s->last_packet_len = p - s->last_packet;
447 ffe8ab83 ths
        put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
448 b4608c04 bellard
449 4046d913 pbrook
#ifdef CONFIG_USER_ONLY
450 4046d913 pbrook
        i = get_char(s);
451 4046d913 pbrook
        if (i < 0)
452 b4608c04 bellard
            return -1;
453 4046d913 pbrook
        if (i == '+')
454 b4608c04 bellard
            break;
455 4046d913 pbrook
#else
456 4046d913 pbrook
        break;
457 4046d913 pbrook
#endif
458 b4608c04 bellard
    }
459 b4608c04 bellard
    return 0;
460 b4608c04 bellard
}
461 b4608c04 bellard
462 56aebc89 pbrook
/* return -1 if error, 0 if OK */
463 56aebc89 pbrook
static int put_packet(GDBState *s, const char *buf)
464 56aebc89 pbrook
{
465 56aebc89 pbrook
#ifdef DEBUG_GDB
466 56aebc89 pbrook
    printf("reply='%s'\n", buf);
467 56aebc89 pbrook
#endif
468 79808573 bellard
469 56aebc89 pbrook
    return put_packet_binary(s, buf, strlen(buf));
470 56aebc89 pbrook
}
471 56aebc89 pbrook
472 56aebc89 pbrook
/* The GDB remote protocol transfers values in target byte order.  This means
473 56aebc89 pbrook
   we can use the raw memory access routines to access the value buffer.
474 56aebc89 pbrook
   Conveniently, these also handle the case where the buffer is mis-aligned.
475 56aebc89 pbrook
 */
476 56aebc89 pbrook
#define GET_REG8(val) do { \
477 56aebc89 pbrook
    stb_p(mem_buf, val); \
478 56aebc89 pbrook
    return 1; \
479 56aebc89 pbrook
    } while(0)
480 56aebc89 pbrook
#define GET_REG16(val) do { \
481 56aebc89 pbrook
    stw_p(mem_buf, val); \
482 56aebc89 pbrook
    return 2; \
483 56aebc89 pbrook
    } while(0)
484 56aebc89 pbrook
#define GET_REG32(val) do { \
485 56aebc89 pbrook
    stl_p(mem_buf, val); \
486 56aebc89 pbrook
    return 4; \
487 56aebc89 pbrook
    } while(0)
488 56aebc89 pbrook
#define GET_REG64(val) do { \
489 56aebc89 pbrook
    stq_p(mem_buf, val); \
490 56aebc89 pbrook
    return 8; \
491 56aebc89 pbrook
    } while(0)
492 56aebc89 pbrook
493 56aebc89 pbrook
#if TARGET_LONG_BITS == 64
494 56aebc89 pbrook
#define GET_REGL(val) GET_REG64(val)
495 56aebc89 pbrook
#define ldtul_p(addr) ldq_p(addr)
496 56aebc89 pbrook
#else
497 56aebc89 pbrook
#define GET_REGL(val) GET_REG32(val)
498 56aebc89 pbrook
#define ldtul_p(addr) ldl_p(addr)
499 79808573 bellard
#endif
500 79808573 bellard
501 56aebc89 pbrook
#if defined(TARGET_I386)
502 5ad265ee balrog
503 5ad265ee balrog
#ifdef TARGET_X86_64
504 56aebc89 pbrook
static const int gpr_map[16] = {
505 56aebc89 pbrook
    R_EAX, R_EBX, R_ECX, R_EDX, R_ESI, R_EDI, R_EBP, R_ESP,
506 56aebc89 pbrook
    8, 9, 10, 11, 12, 13, 14, 15
507 56aebc89 pbrook
};
508 79808573 bellard
#else
509 56aebc89 pbrook
static const int gpr_map[8] = {0, 1, 2, 3, 4, 5, 6, 7};
510 79808573 bellard
#endif
511 79808573 bellard
512 56aebc89 pbrook
#define NUM_CORE_REGS (CPU_NB_REGS * 2 + 25)
513 56aebc89 pbrook
514 56aebc89 pbrook
static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
515 79808573 bellard
{
516 56aebc89 pbrook
    if (n < CPU_NB_REGS) {
517 56aebc89 pbrook
        GET_REGL(env->regs[gpr_map[n]]);
518 56aebc89 pbrook
    } else if (n >= CPU_NB_REGS + 8 && n < CPU_NB_REGS + 16) {
519 56aebc89 pbrook
        /* FIXME: byteswap float values.  */
520 56aebc89 pbrook
#ifdef USE_X86LDOUBLE
521 56aebc89 pbrook
        memcpy(mem_buf, &env->fpregs[n - (CPU_NB_REGS + 8)], 10);
522 79808573 bellard
#else
523 56aebc89 pbrook
        memset(mem_buf, 0, 10);
524 79808573 bellard
#endif
525 56aebc89 pbrook
        return 10;
526 56aebc89 pbrook
    } else if (n >= CPU_NB_REGS + 24) {
527 56aebc89 pbrook
        n -= CPU_NB_REGS + 24;
528 56aebc89 pbrook
        if (n < CPU_NB_REGS) {
529 56aebc89 pbrook
            stq_p(mem_buf, env->xmm_regs[n].XMM_Q(0));
530 56aebc89 pbrook
            stq_p(mem_buf + 8, env->xmm_regs[n].XMM_Q(1));
531 56aebc89 pbrook
            return 16;
532 56aebc89 pbrook
        } else if (n == CPU_NB_REGS) {
533 56aebc89 pbrook
            GET_REG32(env->mxcsr);
534 56aebc89 pbrook
        } 
535 56aebc89 pbrook
    } else {
536 56aebc89 pbrook
        n -= CPU_NB_REGS;
537 56aebc89 pbrook
        switch (n) {
538 56aebc89 pbrook
        case 0: GET_REGL(env->eip);
539 56aebc89 pbrook
        case 1: GET_REG32(env->eflags);
540 56aebc89 pbrook
        case 2: GET_REG32(env->segs[R_CS].selector);
541 56aebc89 pbrook
        case 3: GET_REG32(env->segs[R_SS].selector);
542 56aebc89 pbrook
        case 4: GET_REG32(env->segs[R_DS].selector);
543 56aebc89 pbrook
        case 5: GET_REG32(env->segs[R_ES].selector);
544 56aebc89 pbrook
        case 6: GET_REG32(env->segs[R_FS].selector);
545 56aebc89 pbrook
        case 7: GET_REG32(env->segs[R_GS].selector);
546 56aebc89 pbrook
        /* 8...15 x87 regs.  */
547 56aebc89 pbrook
        case 16: GET_REG32(env->fpuc);
548 56aebc89 pbrook
        case 17: GET_REG32((env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11);
549 56aebc89 pbrook
        case 18: GET_REG32(0); /* ftag */
550 56aebc89 pbrook
        case 19: GET_REG32(0); /* fiseg */
551 56aebc89 pbrook
        case 20: GET_REG32(0); /* fioff */
552 56aebc89 pbrook
        case 21: GET_REG32(0); /* foseg */
553 56aebc89 pbrook
        case 22: GET_REG32(0); /* fooff */
554 56aebc89 pbrook
        case 23: GET_REG32(0); /* fop */
555 56aebc89 pbrook
        /* 24+ xmm regs.  */
556 56aebc89 pbrook
        }
557 79808573 bellard
    }
558 56aebc89 pbrook
    return 0;
559 6da41eaf bellard
}
560 6da41eaf bellard
561 56aebc89 pbrook
static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int i)
562 6da41eaf bellard
{
563 56aebc89 pbrook
    uint32_t tmp;
564 6da41eaf bellard
565 56aebc89 pbrook
    if (i < CPU_NB_REGS) {
566 56aebc89 pbrook
        env->regs[gpr_map[i]] = ldtul_p(mem_buf);
567 56aebc89 pbrook
        return sizeof(target_ulong);
568 56aebc89 pbrook
    } else if (i >= CPU_NB_REGS + 8 && i < CPU_NB_REGS + 16) {
569 56aebc89 pbrook
        i -= CPU_NB_REGS + 8;
570 56aebc89 pbrook
#ifdef USE_X86LDOUBLE
571 56aebc89 pbrook
        memcpy(&env->fpregs[i], mem_buf, 10);
572 79808573 bellard
#endif
573 56aebc89 pbrook
        return 10;
574 56aebc89 pbrook
    } else if (i >= CPU_NB_REGS + 24) {
575 56aebc89 pbrook
        i -= CPU_NB_REGS + 24;
576 56aebc89 pbrook
        if (i < CPU_NB_REGS) {
577 56aebc89 pbrook
            env->xmm_regs[i].XMM_Q(0) = ldq_p(mem_buf);
578 56aebc89 pbrook
            env->xmm_regs[i].XMM_Q(1) = ldq_p(mem_buf + 8);
579 56aebc89 pbrook
            return 16;
580 56aebc89 pbrook
        } else if (i == CPU_NB_REGS) {
581 56aebc89 pbrook
            env->mxcsr = ldl_p(mem_buf);
582 56aebc89 pbrook
            return 4;
583 79808573 bellard
        }
584 56aebc89 pbrook
    } else {
585 56aebc89 pbrook
        i -= CPU_NB_REGS;
586 56aebc89 pbrook
        switch (i) {
587 56aebc89 pbrook
        case 0: env->eip = ldtul_p(mem_buf); return sizeof(target_ulong);
588 56aebc89 pbrook
        case 1: env->eflags = ldl_p(mem_buf); return 4;
589 56aebc89 pbrook
#if defined(CONFIG_USER_ONLY)
590 56aebc89 pbrook
#define LOAD_SEG(index, sreg)\
591 56aebc89 pbrook
            tmp = ldl_p(mem_buf);\
592 56aebc89 pbrook
            if (tmp != env->segs[sreg].selector)\
593 56aebc89 pbrook
                cpu_x86_load_seg(env, sreg, tmp);
594 56aebc89 pbrook
#else
595 56aebc89 pbrook
/* FIXME: Honor segment registers.  Needs to avoid raising an exception
596 56aebc89 pbrook
   when the selector is invalid.  */
597 56aebc89 pbrook
#define LOAD_SEG(index, sreg) do {} while(0)
598 6da41eaf bellard
#endif
599 56aebc89 pbrook
        case 2: LOAD_SEG(10, R_CS); return 4;
600 56aebc89 pbrook
        case 3: LOAD_SEG(11, R_SS); return 4;
601 56aebc89 pbrook
        case 4: LOAD_SEG(12, R_DS); return 4;
602 56aebc89 pbrook
        case 5: LOAD_SEG(13, R_ES); return 4;
603 56aebc89 pbrook
        case 6: LOAD_SEG(14, R_FS); return 4;
604 56aebc89 pbrook
        case 7: LOAD_SEG(15, R_GS); return 4;
605 56aebc89 pbrook
        /* 8...15 x87 regs.  */
606 56aebc89 pbrook
        case 16: env->fpuc = ldl_p(mem_buf); return 4;
607 56aebc89 pbrook
        case 17:
608 56aebc89 pbrook
                 tmp = ldl_p(mem_buf);
609 56aebc89 pbrook
                 env->fpstt = (tmp >> 11) & 7;
610 56aebc89 pbrook
                 env->fpus = tmp & ~0x3800;
611 56aebc89 pbrook
                 return 4;
612 56aebc89 pbrook
        case 18: /* ftag */ return 4;
613 56aebc89 pbrook
        case 19: /* fiseg */ return 4;
614 56aebc89 pbrook
        case 20: /* fioff */ return 4;
615 56aebc89 pbrook
        case 21: /* foseg */ return 4;
616 56aebc89 pbrook
        case 22: /* fooff */ return 4;
617 56aebc89 pbrook
        case 23: /* fop */ return 4;
618 56aebc89 pbrook
        /* 24+ xmm regs.  */
619 79808573 bellard
        }
620 79808573 bellard
    }
621 56aebc89 pbrook
    /* Unrecognised register.  */
622 56aebc89 pbrook
    return 0;
623 6da41eaf bellard
}
624 6da41eaf bellard
625 9e62fd7f bellard
#elif defined (TARGET_PPC)
626 9e62fd7f bellard
627 e571cb47 aurel32
/* Old gdb always expects FP registers.  Newer (xml-aware) gdb only
628 e571cb47 aurel32
   expects whatever the target description contains.  Due to a
629 e571cb47 aurel32
   historical mishap the FP registers appear in between core integer
630 e571cb47 aurel32
   regs and PC, MSR, CR, and so forth.  We hack round this by giving the
631 e571cb47 aurel32
   FP regs zero size when talking to a newer gdb.  */
632 56aebc89 pbrook
#define NUM_CORE_REGS 71
633 e571cb47 aurel32
#if defined (TARGET_PPC64)
634 e571cb47 aurel32
#define GDB_CORE_XML "power64-core.xml"
635 e571cb47 aurel32
#else
636 e571cb47 aurel32
#define GDB_CORE_XML "power-core.xml"
637 e571cb47 aurel32
#endif
638 9e62fd7f bellard
639 56aebc89 pbrook
static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
640 9e62fd7f bellard
{
641 56aebc89 pbrook
    if (n < 32) {
642 56aebc89 pbrook
        /* gprs */
643 56aebc89 pbrook
        GET_REGL(env->gpr[n]);
644 56aebc89 pbrook
    } else if (n < 64) {
645 56aebc89 pbrook
        /* fprs */
646 e571cb47 aurel32
        if (gdb_has_xml)
647 e571cb47 aurel32
            return 0;
648 8d4acf9b aurel32
        stfq_p(mem_buf, env->fpr[n-32]);
649 56aebc89 pbrook
        return 8;
650 56aebc89 pbrook
    } else {
651 56aebc89 pbrook
        switch (n) {
652 56aebc89 pbrook
        case 64: GET_REGL(env->nip);
653 56aebc89 pbrook
        case 65: GET_REGL(env->msr);
654 56aebc89 pbrook
        case 66:
655 56aebc89 pbrook
            {
656 56aebc89 pbrook
                uint32_t cr = 0;
657 56aebc89 pbrook
                int i;
658 56aebc89 pbrook
                for (i = 0; i < 8; i++)
659 56aebc89 pbrook
                    cr |= env->crf[i] << (32 - ((i + 1) * 4));
660 56aebc89 pbrook
                GET_REG32(cr);
661 56aebc89 pbrook
            }
662 56aebc89 pbrook
        case 67: GET_REGL(env->lr);
663 56aebc89 pbrook
        case 68: GET_REGL(env->ctr);
664 3d7b417e aurel32
        case 69: GET_REGL(env->xer);
665 e571cb47 aurel32
        case 70:
666 e571cb47 aurel32
            {
667 e571cb47 aurel32
                if (gdb_has_xml)
668 e571cb47 aurel32
                    return 0;
669 e571cb47 aurel32
                GET_REG32(0); /* fpscr */
670 e571cb47 aurel32
            }
671 56aebc89 pbrook
        }
672 56aebc89 pbrook
    }
673 56aebc89 pbrook
    return 0;
674 56aebc89 pbrook
}
675 9e62fd7f bellard
676 56aebc89 pbrook
static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
677 56aebc89 pbrook
{
678 56aebc89 pbrook
    if (n < 32) {
679 56aebc89 pbrook
        /* gprs */
680 56aebc89 pbrook
        env->gpr[n] = ldtul_p(mem_buf);
681 56aebc89 pbrook
        return sizeof(target_ulong);
682 56aebc89 pbrook
    } else if (n < 64) {
683 56aebc89 pbrook
        /* fprs */
684 e571cb47 aurel32
        if (gdb_has_xml)
685 e571cb47 aurel32
            return 0;
686 8d4acf9b aurel32
        env->fpr[n-32] = ldfq_p(mem_buf);
687 56aebc89 pbrook
        return 8;
688 56aebc89 pbrook
    } else {
689 56aebc89 pbrook
        switch (n) {
690 56aebc89 pbrook
        case 64:
691 56aebc89 pbrook
            env->nip = ldtul_p(mem_buf);
692 56aebc89 pbrook
            return sizeof(target_ulong);
693 56aebc89 pbrook
        case 65:
694 56aebc89 pbrook
            ppc_store_msr(env, ldtul_p(mem_buf));
695 56aebc89 pbrook
            return sizeof(target_ulong);
696 56aebc89 pbrook
        case 66:
697 56aebc89 pbrook
            {
698 56aebc89 pbrook
                uint32_t cr = ldl_p(mem_buf);
699 56aebc89 pbrook
                int i;
700 56aebc89 pbrook
                for (i = 0; i < 8; i++)
701 56aebc89 pbrook
                    env->crf[i] = (cr >> (32 - ((i + 1) * 4))) & 0xF;
702 56aebc89 pbrook
                return 4;
703 56aebc89 pbrook
            }
704 56aebc89 pbrook
        case 67:
705 56aebc89 pbrook
            env->lr = ldtul_p(mem_buf);
706 56aebc89 pbrook
            return sizeof(target_ulong);
707 56aebc89 pbrook
        case 68:
708 56aebc89 pbrook
            env->ctr = ldtul_p(mem_buf);
709 56aebc89 pbrook
            return sizeof(target_ulong);
710 56aebc89 pbrook
        case 69:
711 3d7b417e aurel32
            env->xer = ldtul_p(mem_buf);
712 3d7b417e aurel32
            return sizeof(target_ulong);
713 56aebc89 pbrook
        case 70:
714 56aebc89 pbrook
            /* fpscr */
715 e571cb47 aurel32
            if (gdb_has_xml)
716 e571cb47 aurel32
                return 0;
717 56aebc89 pbrook
            return 4;
718 56aebc89 pbrook
        }
719 56aebc89 pbrook
    }
720 56aebc89 pbrook
    return 0;
721 e95c8d51 bellard
}
722 56aebc89 pbrook
723 e95c8d51 bellard
#elif defined (TARGET_SPARC)
724 56aebc89 pbrook
725 56aebc89 pbrook
#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
726 56aebc89 pbrook
#define NUM_CORE_REGS 86
727 96d19126 blueswir1
#else
728 5a377912 blueswir1
#define NUM_CORE_REGS 72
729 96d19126 blueswir1
#endif
730 56aebc89 pbrook
731 96d19126 blueswir1
#ifdef TARGET_ABI32
732 56aebc89 pbrook
#define GET_REGA(val) GET_REG32(val)
733 96d19126 blueswir1
#else
734 56aebc89 pbrook
#define GET_REGA(val) GET_REGL(val)
735 96d19126 blueswir1
#endif
736 e95c8d51 bellard
737 56aebc89 pbrook
static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
738 56aebc89 pbrook
{
739 56aebc89 pbrook
    if (n < 8) {
740 56aebc89 pbrook
        /* g0..g7 */
741 56aebc89 pbrook
        GET_REGA(env->gregs[n]);
742 e95c8d51 bellard
    }
743 56aebc89 pbrook
    if (n < 32) {
744 56aebc89 pbrook
        /* register window */
745 56aebc89 pbrook
        GET_REGA(env->regwptr[n - 8]);
746 e95c8d51 bellard
    }
747 56aebc89 pbrook
#if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
748 56aebc89 pbrook
    if (n < 64) {
749 56aebc89 pbrook
        /* fprs */
750 56aebc89 pbrook
        GET_REG32(*((uint32_t *)&env->fpr[n - 32]));
751 e95c8d51 bellard
    }
752 e95c8d51 bellard
    /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
753 56aebc89 pbrook
    switch (n) {
754 56aebc89 pbrook
    case 64: GET_REGA(env->y);
755 56aebc89 pbrook
    case 65: GET_REGA(GET_PSR(env));
756 56aebc89 pbrook
    case 66: GET_REGA(env->wim);
757 56aebc89 pbrook
    case 67: GET_REGA(env->tbr);
758 56aebc89 pbrook
    case 68: GET_REGA(env->pc);
759 56aebc89 pbrook
    case 69: GET_REGA(env->npc);
760 56aebc89 pbrook
    case 70: GET_REGA(env->fsr);
761 56aebc89 pbrook
    case 71: GET_REGA(0); /* csr */
762 5a377912 blueswir1
    default: GET_REGA(0);
763 56aebc89 pbrook
    }
764 3475187d bellard
#else
765 56aebc89 pbrook
    if (n < 64) {
766 56aebc89 pbrook
        /* f0-f31 */
767 56aebc89 pbrook
        GET_REG32(*((uint32_t *)&env->fpr[n - 32]));
768 56aebc89 pbrook
    }
769 56aebc89 pbrook
    if (n < 80) {
770 56aebc89 pbrook
        /* f32-f62 (double width, even numbers only) */
771 56aebc89 pbrook
        uint64_t val;
772 9d9754a3 bellard
773 56aebc89 pbrook
        val = (uint64_t)*((uint32_t *)&env->fpr[(n - 64) * 2 + 32]) << 32;
774 56aebc89 pbrook
        val |= *((uint32_t *)&env->fpr[(n - 64) * 2 + 33]);
775 56aebc89 pbrook
        GET_REG64(val);
776 3475187d bellard
    }
777 56aebc89 pbrook
    switch (n) {
778 56aebc89 pbrook
    case 80: GET_REGL(env->pc);
779 56aebc89 pbrook
    case 81: GET_REGL(env->npc);
780 56aebc89 pbrook
    case 82: GET_REGL(((uint64_t)GET_CCR(env) << 32) |
781 17d996e1 blueswir1
                           ((env->asi & 0xff) << 24) |
782 17d996e1 blueswir1
                           ((env->pstate & 0xfff) << 8) |
783 17d996e1 blueswir1
                           GET_CWP64(env));
784 56aebc89 pbrook
    case 83: GET_REGL(env->fsr);
785 56aebc89 pbrook
    case 84: GET_REGL(env->fprs);
786 56aebc89 pbrook
    case 85: GET_REGL(env->y);
787 56aebc89 pbrook
    }
788 3475187d bellard
#endif
789 56aebc89 pbrook
    return 0;
790 e95c8d51 bellard
}
791 e95c8d51 bellard
792 56aebc89 pbrook
static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
793 e95c8d51 bellard
{
794 56aebc89 pbrook
#if defined(TARGET_ABI32)
795 56aebc89 pbrook
    abi_ulong tmp;
796 56aebc89 pbrook
797 56aebc89 pbrook
    tmp = ldl_p(mem_buf);
798 96d19126 blueswir1
#else
799 56aebc89 pbrook
    target_ulong tmp;
800 56aebc89 pbrook
801 56aebc89 pbrook
    tmp = ldtul_p(mem_buf);
802 96d19126 blueswir1
#endif
803 e95c8d51 bellard
804 56aebc89 pbrook
    if (n < 8) {
805 56aebc89 pbrook
        /* g0..g7 */
806 56aebc89 pbrook
        env->gregs[n] = tmp;
807 56aebc89 pbrook
    } else if (n < 32) {
808 56aebc89 pbrook
        /* register window */
809 56aebc89 pbrook
        env->regwptr[n - 8] = tmp;
810 e95c8d51 bellard
    }
811 56aebc89 pbrook
#if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
812 56aebc89 pbrook
    else if (n < 64) {
813 56aebc89 pbrook
        /* fprs */
814 56aebc89 pbrook
        *((uint32_t *)&env->fpr[n - 32]) = tmp;
815 56aebc89 pbrook
    } else {
816 56aebc89 pbrook
        /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
817 56aebc89 pbrook
        switch (n) {
818 56aebc89 pbrook
        case 64: env->y = tmp; break;
819 56aebc89 pbrook
        case 65: PUT_PSR(env, tmp); break;
820 56aebc89 pbrook
        case 66: env->wim = tmp; break;
821 56aebc89 pbrook
        case 67: env->tbr = tmp; break;
822 56aebc89 pbrook
        case 68: env->pc = tmp; break;
823 56aebc89 pbrook
        case 69: env->npc = tmp; break;
824 56aebc89 pbrook
        case 70: env->fsr = tmp; break;
825 56aebc89 pbrook
        default: return 0;
826 56aebc89 pbrook
        }
827 e95c8d51 bellard
    }
828 56aebc89 pbrook
    return 4;
829 3475187d bellard
#else
830 56aebc89 pbrook
    else if (n < 64) {
831 56aebc89 pbrook
        /* f0-f31 */
832 56aebc89 pbrook
        env->fpr[n] = ldfl_p(mem_buf);
833 56aebc89 pbrook
        return 4;
834 56aebc89 pbrook
    } else if (n < 80) {
835 56aebc89 pbrook
        /* f32-f62 (double width, even numbers only) */
836 56aebc89 pbrook
        *((uint32_t *)&env->fpr[(n - 64) * 2 + 32]) = tmp >> 32;
837 56aebc89 pbrook
        *((uint32_t *)&env->fpr[(n - 64) * 2 + 33]) = tmp;
838 56aebc89 pbrook
    } else {
839 56aebc89 pbrook
        switch (n) {
840 56aebc89 pbrook
        case 80: env->pc = tmp; break;
841 56aebc89 pbrook
        case 81: env->npc = tmp; break;
842 56aebc89 pbrook
        case 82:
843 56aebc89 pbrook
            PUT_CCR(env, tmp >> 32);
844 56aebc89 pbrook
            env->asi = (tmp >> 24) & 0xff;
845 56aebc89 pbrook
            env->pstate = (tmp >> 8) & 0xfff;
846 56aebc89 pbrook
            PUT_CWP64(env, tmp & 0xff);
847 56aebc89 pbrook
            break;
848 56aebc89 pbrook
        case 83: env->fsr = tmp; break;
849 56aebc89 pbrook
        case 84: env->fprs = tmp; break;
850 56aebc89 pbrook
        case 85: env->y = tmp; break;
851 56aebc89 pbrook
        default: return 0;
852 56aebc89 pbrook
        }
853 17d996e1 blueswir1
    }
854 56aebc89 pbrook
    return 8;
855 3475187d bellard
#endif
856 9e62fd7f bellard
}
857 1fddef4b bellard
#elif defined (TARGET_ARM)
858 6da41eaf bellard
859 56aebc89 pbrook
/* Old gdb always expect FPA registers.  Newer (xml-aware) gdb only expect
860 56aebc89 pbrook
   whatever the target description contains.  Due to a historical mishap
861 56aebc89 pbrook
   the FPA registers appear in between core integer regs and the CPSR.
862 56aebc89 pbrook
   We hack round this by giving the FPA regs zero size when talking to a
863 56aebc89 pbrook
   newer gdb.  */
864 56aebc89 pbrook
#define NUM_CORE_REGS 26
865 56aebc89 pbrook
#define GDB_CORE_XML "arm-core.xml"
866 e6e5906b pbrook
867 56aebc89 pbrook
static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
868 e6e5906b pbrook
{
869 56aebc89 pbrook
    if (n < 16) {
870 56aebc89 pbrook
        /* Core integer register.  */
871 56aebc89 pbrook
        GET_REG32(env->regs[n]);
872 56aebc89 pbrook
    }
873 56aebc89 pbrook
    if (n < 24) {
874 56aebc89 pbrook
        /* FPA registers.  */
875 56aebc89 pbrook
        if (gdb_has_xml)
876 56aebc89 pbrook
            return 0;
877 56aebc89 pbrook
        memset(mem_buf, 0, 12);
878 56aebc89 pbrook
        return 12;
879 56aebc89 pbrook
    }
880 56aebc89 pbrook
    switch (n) {
881 56aebc89 pbrook
    case 24:
882 56aebc89 pbrook
        /* FPA status register.  */
883 56aebc89 pbrook
        if (gdb_has_xml)
884 56aebc89 pbrook
            return 0;
885 56aebc89 pbrook
        GET_REG32(0);
886 56aebc89 pbrook
    case 25:
887 56aebc89 pbrook
        /* CPSR */
888 56aebc89 pbrook
        GET_REG32(cpsr_read(env));
889 56aebc89 pbrook
    }
890 56aebc89 pbrook
    /* Unknown register.  */
891 56aebc89 pbrook
    return 0;
892 e6e5906b pbrook
}
893 6f970bd9 bellard
894 56aebc89 pbrook
static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
895 56aebc89 pbrook
{
896 56aebc89 pbrook
    uint32_t tmp;
897 6f970bd9 bellard
898 56aebc89 pbrook
    tmp = ldl_p(mem_buf);
899 6f970bd9 bellard
900 56aebc89 pbrook
    /* Mask out low bit of PC to workaround gdb bugs.  This will probably
901 56aebc89 pbrook
       cause problems if we ever implement the Jazelle DBX extensions.  */
902 56aebc89 pbrook
    if (n == 15)
903 56aebc89 pbrook
        tmp &= ~1;
904 6f970bd9 bellard
905 56aebc89 pbrook
    if (n < 16) {
906 56aebc89 pbrook
        /* Core integer register.  */
907 56aebc89 pbrook
        env->regs[n] = tmp;
908 56aebc89 pbrook
        return 4;
909 56aebc89 pbrook
    }
910 56aebc89 pbrook
    if (n < 24) { /* 16-23 */
911 56aebc89 pbrook
        /* FPA registers (ignored).  */
912 56aebc89 pbrook
        if (gdb_has_xml)
913 56aebc89 pbrook
            return 0;
914 56aebc89 pbrook
        return 12;
915 56aebc89 pbrook
    }
916 56aebc89 pbrook
    switch (n) {
917 56aebc89 pbrook
    case 24:
918 56aebc89 pbrook
        /* FPA status register (ignored).  */
919 56aebc89 pbrook
        if (gdb_has_xml)
920 56aebc89 pbrook
            return 0;
921 56aebc89 pbrook
        return 4;
922 56aebc89 pbrook
    case 25:
923 56aebc89 pbrook
        /* CPSR */
924 56aebc89 pbrook
        cpsr_write (env, tmp, 0xffffffff);
925 56aebc89 pbrook
        return 4;
926 56aebc89 pbrook
    }
927 56aebc89 pbrook
    /* Unknown register.  */
928 56aebc89 pbrook
    return 0;
929 56aebc89 pbrook
}
930 6f970bd9 bellard
931 56aebc89 pbrook
#elif defined (TARGET_M68K)
932 6f970bd9 bellard
933 56aebc89 pbrook
#define NUM_CORE_REGS 18
934 6f970bd9 bellard
935 56aebc89 pbrook
#define GDB_CORE_XML "cf-core.xml"
936 6f970bd9 bellard
937 56aebc89 pbrook
static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
938 56aebc89 pbrook
{
939 56aebc89 pbrook
    if (n < 8) {
940 56aebc89 pbrook
        /* D0-D7 */
941 56aebc89 pbrook
        GET_REG32(env->dregs[n]);
942 56aebc89 pbrook
    } else if (n < 16) {
943 56aebc89 pbrook
        /* A0-A7 */
944 56aebc89 pbrook
        GET_REG32(env->aregs[n - 8]);
945 56aebc89 pbrook
    } else {
946 56aebc89 pbrook
        switch (n) {
947 56aebc89 pbrook
        case 16: GET_REG32(env->sr);
948 56aebc89 pbrook
        case 17: GET_REG32(env->pc);
949 56aebc89 pbrook
        }
950 56aebc89 pbrook
    }
951 56aebc89 pbrook
    /* FP registers not included here because they vary between
952 56aebc89 pbrook
       ColdFire and m68k.  Use XML bits for these.  */
953 56aebc89 pbrook
    return 0;
954 56aebc89 pbrook
}
955 8e33c08c ths
956 56aebc89 pbrook
static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
957 56aebc89 pbrook
{
958 56aebc89 pbrook
    uint32_t tmp;
959 8e33c08c ths
960 56aebc89 pbrook
    tmp = ldl_p(mem_buf);
961 8e33c08c ths
962 56aebc89 pbrook
    if (n < 8) {
963 56aebc89 pbrook
        /* D0-D7 */
964 56aebc89 pbrook
        env->dregs[n] = tmp;
965 56aebc89 pbrook
    } else if (n < 8) {
966 56aebc89 pbrook
        /* A0-A7 */
967 56aebc89 pbrook
        env->aregs[n - 8] = tmp;
968 56aebc89 pbrook
    } else {
969 56aebc89 pbrook
        switch (n) {
970 56aebc89 pbrook
        case 16: env->sr = tmp; break;
971 56aebc89 pbrook
        case 17: env->pc = tmp; break;
972 56aebc89 pbrook
        default: return 0;
973 56aebc89 pbrook
        }
974 56aebc89 pbrook
    }
975 56aebc89 pbrook
    return 4;
976 56aebc89 pbrook
}
977 56aebc89 pbrook
#elif defined (TARGET_MIPS)
978 7ac256b8 ths
979 56aebc89 pbrook
#define NUM_CORE_REGS 73
980 7ac256b8 ths
981 56aebc89 pbrook
static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
982 56aebc89 pbrook
{
983 56aebc89 pbrook
    if (n < 32) {
984 56aebc89 pbrook
        GET_REGL(env->active_tc.gpr[n]);
985 56aebc89 pbrook
    }
986 56aebc89 pbrook
    if (env->CP0_Config1 & (1 << CP0C1_FP)) {
987 56aebc89 pbrook
        if (n >= 38 && n < 70) {
988 56aebc89 pbrook
            if (env->CP0_Status & (1 << CP0St_FR))
989 56aebc89 pbrook
                GET_REGL(env->active_fpu.fpr[n - 38].d);
990 56aebc89 pbrook
            else
991 56aebc89 pbrook
                GET_REGL(env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX]);
992 56aebc89 pbrook
        }
993 56aebc89 pbrook
        switch (n) {
994 56aebc89 pbrook
        case 70: GET_REGL((int32_t)env->active_fpu.fcr31);
995 56aebc89 pbrook
        case 71: GET_REGL((int32_t)env->active_fpu.fcr0);
996 56aebc89 pbrook
        }
997 56aebc89 pbrook
    }
998 56aebc89 pbrook
    switch (n) {
999 56aebc89 pbrook
    case 32: GET_REGL((int32_t)env->CP0_Status);
1000 56aebc89 pbrook
    case 33: GET_REGL(env->active_tc.LO[0]);
1001 56aebc89 pbrook
    case 34: GET_REGL(env->active_tc.HI[0]);
1002 56aebc89 pbrook
    case 35: GET_REGL(env->CP0_BadVAddr);
1003 56aebc89 pbrook
    case 36: GET_REGL((int32_t)env->CP0_Cause);
1004 56aebc89 pbrook
    case 37: GET_REGL(env->active_tc.PC);
1005 56aebc89 pbrook
    case 72: GET_REGL(0); /* fp */
1006 56aebc89 pbrook
    case 89: GET_REGL((int32_t)env->CP0_PRid);
1007 56aebc89 pbrook
    }
1008 56aebc89 pbrook
    if (n >= 73 && n <= 88) {
1009 56aebc89 pbrook
        /* 16 embedded regs.  */
1010 56aebc89 pbrook
        GET_REGL(0);
1011 56aebc89 pbrook
    }
1012 6f970bd9 bellard
1013 56aebc89 pbrook
    return 0;
1014 6f970bd9 bellard
}
1015 6f970bd9 bellard
1016 8e33c08c ths
/* convert MIPS rounding mode in FCR31 to IEEE library */
1017 8e33c08c ths
static unsigned int ieee_rm[] =
1018 8e33c08c ths
  {
1019 8e33c08c ths
    float_round_nearest_even,
1020 8e33c08c ths
    float_round_to_zero,
1021 8e33c08c ths
    float_round_up,
1022 8e33c08c ths
    float_round_down
1023 8e33c08c ths
  };
1024 8e33c08c ths
#define RESTORE_ROUNDING_MODE \
1025 f01be154 ths
    set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
1026 8e33c08c ths
1027 56aebc89 pbrook
static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
1028 6f970bd9 bellard
{
1029 56aebc89 pbrook
    target_ulong tmp;
1030 6f970bd9 bellard
1031 56aebc89 pbrook
    tmp = ldtul_p(mem_buf);
1032 6f970bd9 bellard
1033 56aebc89 pbrook
    if (n < 32) {
1034 56aebc89 pbrook
        env->active_tc.gpr[n] = tmp;
1035 56aebc89 pbrook
        return sizeof(target_ulong);
1036 56aebc89 pbrook
    }
1037 56aebc89 pbrook
    if (env->CP0_Config1 & (1 << CP0C1_FP)
1038 56aebc89 pbrook
            && n >= 38 && n < 73) {
1039 56aebc89 pbrook
        if (n < 70) {
1040 7ac256b8 ths
            if (env->CP0_Status & (1 << CP0St_FR))
1041 56aebc89 pbrook
              env->active_fpu.fpr[n - 38].d = tmp;
1042 7ac256b8 ths
            else
1043 56aebc89 pbrook
              env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX] = tmp;
1044 56aebc89 pbrook
        }
1045 56aebc89 pbrook
        switch (n) {
1046 56aebc89 pbrook
        case 70:
1047 56aebc89 pbrook
            env->active_fpu.fcr31 = tmp & 0xFF83FFFF;
1048 56aebc89 pbrook
            /* set rounding mode */
1049 56aebc89 pbrook
            RESTORE_ROUNDING_MODE;
1050 8e33c08c ths
#ifndef CONFIG_SOFTFLOAT
1051 56aebc89 pbrook
            /* no floating point exception for native float */
1052 56aebc89 pbrook
            SET_FP_ENABLE(env->active_fpu.fcr31, 0);
1053 8e33c08c ths
#endif
1054 56aebc89 pbrook
            break;
1055 56aebc89 pbrook
        case 71: env->active_fpu.fcr0 = tmp; break;
1056 56aebc89 pbrook
        }
1057 56aebc89 pbrook
        return sizeof(target_ulong);
1058 56aebc89 pbrook
    }
1059 56aebc89 pbrook
    switch (n) {
1060 56aebc89 pbrook
    case 32: env->CP0_Status = tmp; break;
1061 56aebc89 pbrook
    case 33: env->active_tc.LO[0] = tmp; break;
1062 56aebc89 pbrook
    case 34: env->active_tc.HI[0] = tmp; break;
1063 56aebc89 pbrook
    case 35: env->CP0_BadVAddr = tmp; break;
1064 56aebc89 pbrook
    case 36: env->CP0_Cause = tmp; break;
1065 56aebc89 pbrook
    case 37: env->active_tc.PC = tmp; break;
1066 56aebc89 pbrook
    case 72: /* fp, ignored */ break;
1067 56aebc89 pbrook
    default: 
1068 56aebc89 pbrook
        if (n > 89)
1069 56aebc89 pbrook
            return 0;
1070 56aebc89 pbrook
        /* Other registers are readonly.  Ignore writes.  */
1071 56aebc89 pbrook
        break;
1072 56aebc89 pbrook
    }
1073 56aebc89 pbrook
1074 56aebc89 pbrook
    return sizeof(target_ulong);
1075 6f970bd9 bellard
}
1076 fdf9b3e8 bellard
#elif defined (TARGET_SH4)
1077 6ef99fc5 ths
1078 6ef99fc5 ths
/* Hint: Use "set architecture sh4" in GDB to see fpu registers */
1079 56aebc89 pbrook
/* FIXME: We should use XML for this.  */
1080 56aebc89 pbrook
1081 56aebc89 pbrook
#define NUM_CORE_REGS 59
1082 6ef99fc5 ths
1083 56aebc89 pbrook
static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
1084 fdf9b3e8 bellard
{
1085 56aebc89 pbrook
    if (n < 8) {
1086 56aebc89 pbrook
        if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
1087 56aebc89 pbrook
            GET_REGL(env->gregs[n + 16]);
1088 56aebc89 pbrook
        } else {
1089 56aebc89 pbrook
            GET_REGL(env->gregs[n]);
1090 56aebc89 pbrook
        }
1091 56aebc89 pbrook
    } else if (n < 16) {
1092 56aebc89 pbrook
        GET_REGL(env->gregs[n - 8]);
1093 56aebc89 pbrook
    } else if (n >= 25 && n < 41) {
1094 56aebc89 pbrook
        GET_REGL(env->fregs[(n - 25) + ((env->fpscr & FPSCR_FR) ? 16 : 0)]);
1095 56aebc89 pbrook
    } else if (n >= 43 && n < 51) {
1096 56aebc89 pbrook
        GET_REGL(env->gregs[n - 43]);
1097 56aebc89 pbrook
    } else if (n >= 51 && n < 59) {
1098 56aebc89 pbrook
        GET_REGL(env->gregs[n - (51 - 16)]);
1099 56aebc89 pbrook
    }
1100 56aebc89 pbrook
    switch (n) {
1101 56aebc89 pbrook
    case 16: GET_REGL(env->pc);
1102 56aebc89 pbrook
    case 17: GET_REGL(env->pr);
1103 56aebc89 pbrook
    case 18: GET_REGL(env->gbr);
1104 56aebc89 pbrook
    case 19: GET_REGL(env->vbr);
1105 56aebc89 pbrook
    case 20: GET_REGL(env->mach);
1106 56aebc89 pbrook
    case 21: GET_REGL(env->macl);
1107 56aebc89 pbrook
    case 22: GET_REGL(env->sr);
1108 56aebc89 pbrook
    case 23: GET_REGL(env->fpul);
1109 56aebc89 pbrook
    case 24: GET_REGL(env->fpscr);
1110 56aebc89 pbrook
    case 41: GET_REGL(env->ssr);
1111 56aebc89 pbrook
    case 42: GET_REGL(env->spc);
1112 56aebc89 pbrook
    }
1113 56aebc89 pbrook
1114 56aebc89 pbrook
    return 0;
1115 fdf9b3e8 bellard
}
1116 fdf9b3e8 bellard
1117 56aebc89 pbrook
static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
1118 fdf9b3e8 bellard
{
1119 56aebc89 pbrook
    uint32_t tmp;
1120 56aebc89 pbrook
1121 56aebc89 pbrook
    tmp = ldl_p(mem_buf);
1122 56aebc89 pbrook
1123 56aebc89 pbrook
    if (n < 8) {
1124 56aebc89 pbrook
        if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
1125 56aebc89 pbrook
            env->gregs[n + 16] = tmp;
1126 56aebc89 pbrook
        } else {
1127 56aebc89 pbrook
            env->gregs[n] = tmp;
1128 56aebc89 pbrook
        }
1129 56aebc89 pbrook
        return 4;
1130 56aebc89 pbrook
    } else if (n < 16) {
1131 56aebc89 pbrook
        env->gregs[n - 8] = tmp;
1132 56aebc89 pbrook
        return 4;
1133 56aebc89 pbrook
    } else if (n >= 25 && n < 41) {
1134 56aebc89 pbrook
        env->fregs[(n - 25) + ((env->fpscr & FPSCR_FR) ? 16 : 0)] = tmp;
1135 56aebc89 pbrook
    } else if (n >= 43 && n < 51) {
1136 56aebc89 pbrook
        env->gregs[n - 43] = tmp;
1137 56aebc89 pbrook
        return 4;
1138 56aebc89 pbrook
    } else if (n >= 51 && n < 59) {
1139 56aebc89 pbrook
        env->gregs[n - (51 - 16)] = tmp;
1140 56aebc89 pbrook
        return 4;
1141 56aebc89 pbrook
    }
1142 56aebc89 pbrook
    switch (n) {
1143 56aebc89 pbrook
    case 16: env->pc = tmp;
1144 56aebc89 pbrook
    case 17: env->pr = tmp;
1145 56aebc89 pbrook
    case 18: env->gbr = tmp;
1146 56aebc89 pbrook
    case 19: env->vbr = tmp;
1147 56aebc89 pbrook
    case 20: env->mach = tmp;
1148 56aebc89 pbrook
    case 21: env->macl = tmp;
1149 56aebc89 pbrook
    case 22: env->sr = tmp;
1150 56aebc89 pbrook
    case 23: env->fpul = tmp;
1151 56aebc89 pbrook
    case 24: env->fpscr = tmp;
1152 56aebc89 pbrook
    case 41: env->ssr = tmp;
1153 56aebc89 pbrook
    case 42: env->spc = tmp;
1154 56aebc89 pbrook
    default: return 0;
1155 56aebc89 pbrook
    }
1156 56aebc89 pbrook
1157 56aebc89 pbrook
    return 4;
1158 fdf9b3e8 bellard
}
1159 f1ccf904 ths
#elif defined (TARGET_CRIS)
1160 f1ccf904 ths
1161 56aebc89 pbrook
#define NUM_CORE_REGS 49
1162 56aebc89 pbrook
1163 56aebc89 pbrook
static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
1164 f1ccf904 ths
{
1165 56aebc89 pbrook
    uint8_t srs;
1166 56aebc89 pbrook
1167 56aebc89 pbrook
    srs = env->pregs[PR_SRS];
1168 56aebc89 pbrook
    if (n < 16) {
1169 56aebc89 pbrook
        GET_REG32(env->regs[n]);
1170 56aebc89 pbrook
    }
1171 56aebc89 pbrook
1172 56aebc89 pbrook
    if (n >= 21 && n < 32) {
1173 56aebc89 pbrook
        GET_REG32(env->pregs[n - 16]);
1174 56aebc89 pbrook
    }
1175 56aebc89 pbrook
    if (n >= 33 && n < 49) {
1176 56aebc89 pbrook
        GET_REG32(env->sregs[srs][n - 33]);
1177 56aebc89 pbrook
    }
1178 56aebc89 pbrook
    switch (n) {
1179 56aebc89 pbrook
    case 16: GET_REG8(env->pregs[0]);
1180 56aebc89 pbrook
    case 17: GET_REG8(env->pregs[1]);
1181 56aebc89 pbrook
    case 18: GET_REG32(env->pregs[2]);
1182 56aebc89 pbrook
    case 19: GET_REG8(srs);
1183 56aebc89 pbrook
    case 20: GET_REG16(env->pregs[4]);
1184 56aebc89 pbrook
    case 32: GET_REG32(env->pc);
1185 56aebc89 pbrook
    }
1186 56aebc89 pbrook
1187 56aebc89 pbrook
    return 0;
1188 f1ccf904 ths
}
1189 56aebc89 pbrook
1190 56aebc89 pbrook
static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
1191 f1ccf904 ths
{
1192 56aebc89 pbrook
    uint32_t tmp;
1193 56aebc89 pbrook
1194 56aebc89 pbrook
    if (n > 49)
1195 56aebc89 pbrook
        return 0;
1196 56aebc89 pbrook
1197 56aebc89 pbrook
    tmp = ldl_p(mem_buf);
1198 56aebc89 pbrook
1199 56aebc89 pbrook
    if (n < 16) {
1200 56aebc89 pbrook
        env->regs[n] = tmp;
1201 56aebc89 pbrook
    }
1202 56aebc89 pbrook
1203 d7b6967a edgar_igl
    if (n >= 21 && n < 32) {
1204 d7b6967a edgar_igl
        env->pregs[n - 16] = tmp;
1205 d7b6967a edgar_igl
    }
1206 d7b6967a edgar_igl
1207 d7b6967a edgar_igl
    /* FIXME: Should support function regs be writable?  */
1208 56aebc89 pbrook
    switch (n) {
1209 56aebc89 pbrook
    case 16: return 1;
1210 56aebc89 pbrook
    case 17: return 1;
1211 d7b6967a edgar_igl
    case 18: env->pregs[PR_PID] = tmp; break;
1212 56aebc89 pbrook
    case 19: return 1;
1213 56aebc89 pbrook
    case 20: return 2;
1214 56aebc89 pbrook
    case 32: env->pc = tmp; break;
1215 56aebc89 pbrook
    }
1216 56aebc89 pbrook
1217 56aebc89 pbrook
    return 4;
1218 f1ccf904 ths
}
1219 19bf517b aurel32
#elif defined (TARGET_ALPHA)
1220 19bf517b aurel32
1221 19bf517b aurel32
#define NUM_CORE_REGS 65
1222 19bf517b aurel32
1223 19bf517b aurel32
static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
1224 19bf517b aurel32
{
1225 19bf517b aurel32
    if (n < 31) {
1226 19bf517b aurel32
       GET_REGL(env->ir[n]);
1227 19bf517b aurel32
    }
1228 19bf517b aurel32
    else if (n == 31) {
1229 19bf517b aurel32
       GET_REGL(0);
1230 19bf517b aurel32
    }
1231 19bf517b aurel32
    else if (n<63) {
1232 19bf517b aurel32
       uint64_t val;
1233 19bf517b aurel32
1234 19bf517b aurel32
       val=*((uint64_t *)&env->fir[n-32]);
1235 19bf517b aurel32
       GET_REGL(val);
1236 19bf517b aurel32
    }
1237 19bf517b aurel32
    else if (n==63) {
1238 19bf517b aurel32
       GET_REGL(env->fpcr);
1239 19bf517b aurel32
    }
1240 19bf517b aurel32
    else if (n==64) {
1241 19bf517b aurel32
       GET_REGL(env->pc);
1242 19bf517b aurel32
    }
1243 19bf517b aurel32
    else {
1244 19bf517b aurel32
       GET_REGL(0);
1245 19bf517b aurel32
    }
1246 19bf517b aurel32
1247 19bf517b aurel32
    return 0;
1248 19bf517b aurel32
}
1249 19bf517b aurel32
1250 19bf517b aurel32
static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
1251 19bf517b aurel32
{
1252 19bf517b aurel32
    target_ulong tmp;
1253 19bf517b aurel32
    tmp = ldtul_p(mem_buf);
1254 19bf517b aurel32
1255 19bf517b aurel32
    if (n < 31) {
1256 19bf517b aurel32
        env->ir[n] = tmp;
1257 19bf517b aurel32
    }
1258 19bf517b aurel32
1259 19bf517b aurel32
    if (n > 31 && n < 63) {
1260 19bf517b aurel32
        env->fir[n - 32] = ldfl_p(mem_buf);
1261 19bf517b aurel32
    }
1262 19bf517b aurel32
1263 19bf517b aurel32
    if (n == 64 ) {
1264 19bf517b aurel32
       env->pc=tmp;
1265 19bf517b aurel32
    }
1266 19bf517b aurel32
1267 19bf517b aurel32
    return 8;
1268 19bf517b aurel32
}
1269 56aebc89 pbrook
#else
1270 56aebc89 pbrook
1271 56aebc89 pbrook
#define NUM_CORE_REGS 0
1272 56aebc89 pbrook
1273 56aebc89 pbrook
static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
1274 f1ccf904 ths
{
1275 56aebc89 pbrook
    return 0;
1276 f1ccf904 ths
}
1277 f1ccf904 ths
1278 56aebc89 pbrook
static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
1279 f1ccf904 ths
{
1280 56aebc89 pbrook
    return 0;
1281 56aebc89 pbrook
}
1282 f1ccf904 ths
1283 56aebc89 pbrook
#endif
1284 f1ccf904 ths
1285 56aebc89 pbrook
static int num_g_regs = NUM_CORE_REGS;
1286 f1ccf904 ths
1287 56aebc89 pbrook
#ifdef GDB_CORE_XML
1288 56aebc89 pbrook
/* Encode data using the encoding for 'x' packets.  */
1289 56aebc89 pbrook
static int memtox(char *buf, const char *mem, int len)
1290 56aebc89 pbrook
{
1291 56aebc89 pbrook
    char *p = buf;
1292 56aebc89 pbrook
    char c;
1293 56aebc89 pbrook
1294 56aebc89 pbrook
    while (len--) {
1295 56aebc89 pbrook
        c = *(mem++);
1296 56aebc89 pbrook
        switch (c) {
1297 56aebc89 pbrook
        case '#': case '$': case '*': case '}':
1298 56aebc89 pbrook
            *(p++) = '}';
1299 56aebc89 pbrook
            *(p++) = c ^ 0x20;
1300 56aebc89 pbrook
            break;
1301 56aebc89 pbrook
        default:
1302 56aebc89 pbrook
            *(p++) = c;
1303 56aebc89 pbrook
            break;
1304 56aebc89 pbrook
        }
1305 56aebc89 pbrook
    }
1306 56aebc89 pbrook
    return p - buf;
1307 56aebc89 pbrook
}
1308 f1ccf904 ths
1309 3faf778e aurel32
static const char *get_feature_xml(const char *p, const char **newp)
1310 56aebc89 pbrook
{
1311 56aebc89 pbrook
    extern const char *const xml_builtin[][2];
1312 56aebc89 pbrook
    size_t len;
1313 56aebc89 pbrook
    int i;
1314 56aebc89 pbrook
    const char *name;
1315 56aebc89 pbrook
    static char target_xml[1024];
1316 56aebc89 pbrook
1317 56aebc89 pbrook
    len = 0;
1318 56aebc89 pbrook
    while (p[len] && p[len] != ':')
1319 56aebc89 pbrook
        len++;
1320 56aebc89 pbrook
    *newp = p + len;
1321 56aebc89 pbrook
1322 56aebc89 pbrook
    name = NULL;
1323 56aebc89 pbrook
    if (strncmp(p, "target.xml", len) == 0) {
1324 56aebc89 pbrook
        /* Generate the XML description for this CPU.  */
1325 56aebc89 pbrook
        if (!target_xml[0]) {
1326 56aebc89 pbrook
            GDBRegisterState *r;
1327 56aebc89 pbrook
1328 5b3715bf blueswir1
            snprintf(target_xml, sizeof(target_xml),
1329 5b3715bf blueswir1
                     "<?xml version=\"1.0\"?>"
1330 5b3715bf blueswir1
                     "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
1331 5b3715bf blueswir1
                     "<target>"
1332 5b3715bf blueswir1
                     "<xi:include href=\"%s\"/>",
1333 5b3715bf blueswir1
                     GDB_CORE_XML);
1334 56aebc89 pbrook
1335 880a7578 aliguori
            for (r = first_cpu->gdb_regs; r; r = r->next) {
1336 2dc766da blueswir1
                pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
1337 2dc766da blueswir1
                pstrcat(target_xml, sizeof(target_xml), r->xml);
1338 2dc766da blueswir1
                pstrcat(target_xml, sizeof(target_xml), "\"/>");
1339 56aebc89 pbrook
            }
1340 2dc766da blueswir1
            pstrcat(target_xml, sizeof(target_xml), "</target>");
1341 56aebc89 pbrook
        }
1342 56aebc89 pbrook
        return target_xml;
1343 56aebc89 pbrook
    }
1344 56aebc89 pbrook
    for (i = 0; ; i++) {
1345 56aebc89 pbrook
        name = xml_builtin[i][0];
1346 56aebc89 pbrook
        if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
1347 56aebc89 pbrook
            break;
1348 56aebc89 pbrook
    }
1349 56aebc89 pbrook
    return name ? xml_builtin[i][1] : NULL;
1350 56aebc89 pbrook
}
1351 56aebc89 pbrook
#endif
1352 f1ccf904 ths
1353 56aebc89 pbrook
static int gdb_read_register(CPUState *env, uint8_t *mem_buf, int reg)
1354 56aebc89 pbrook
{
1355 56aebc89 pbrook
    GDBRegisterState *r;
1356 f1ccf904 ths
1357 56aebc89 pbrook
    if (reg < NUM_CORE_REGS)
1358 56aebc89 pbrook
        return cpu_gdb_read_register(env, mem_buf, reg);
1359 f1ccf904 ths
1360 56aebc89 pbrook
    for (r = env->gdb_regs; r; r = r->next) {
1361 56aebc89 pbrook
        if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
1362 56aebc89 pbrook
            return r->get_reg(env, mem_buf, reg - r->base_reg);
1363 56aebc89 pbrook
        }
1364 56aebc89 pbrook
    }
1365 56aebc89 pbrook
    return 0;
1366 f1ccf904 ths
}
1367 f1ccf904 ths
1368 56aebc89 pbrook
static int gdb_write_register(CPUState *env, uint8_t *mem_buf, int reg)
1369 f1ccf904 ths
{
1370 56aebc89 pbrook
    GDBRegisterState *r;
1371 f1ccf904 ths
1372 56aebc89 pbrook
    if (reg < NUM_CORE_REGS)
1373 56aebc89 pbrook
        return cpu_gdb_write_register(env, mem_buf, reg);
1374 56aebc89 pbrook
1375 56aebc89 pbrook
    for (r = env->gdb_regs; r; r = r->next) {
1376 56aebc89 pbrook
        if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
1377 56aebc89 pbrook
            return r->set_reg(env, mem_buf, reg - r->base_reg);
1378 56aebc89 pbrook
        }
1379 56aebc89 pbrook
    }
1380 6da41eaf bellard
    return 0;
1381 6da41eaf bellard
}
1382 6da41eaf bellard
1383 56aebc89 pbrook
/* Register a supplemental set of CPU registers.  If g_pos is nonzero it
1384 56aebc89 pbrook
   specifies the first register number and these registers are included in
1385 56aebc89 pbrook
   a standard "g" packet.  Direction is relative to gdb, i.e. get_reg is
1386 56aebc89 pbrook
   gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
1387 56aebc89 pbrook
 */
1388 56aebc89 pbrook
1389 56aebc89 pbrook
void gdb_register_coprocessor(CPUState * env,
1390 56aebc89 pbrook
                             gdb_reg_cb get_reg, gdb_reg_cb set_reg,
1391 56aebc89 pbrook
                             int num_regs, const char *xml, int g_pos)
1392 6da41eaf bellard
{
1393 56aebc89 pbrook
    GDBRegisterState *s;
1394 56aebc89 pbrook
    GDBRegisterState **p;
1395 56aebc89 pbrook
    static int last_reg = NUM_CORE_REGS;
1396 56aebc89 pbrook
1397 56aebc89 pbrook
    s = (GDBRegisterState *)qemu_mallocz(sizeof(GDBRegisterState));
1398 56aebc89 pbrook
    s->base_reg = last_reg;
1399 56aebc89 pbrook
    s->num_regs = num_regs;
1400 56aebc89 pbrook
    s->get_reg = get_reg;
1401 56aebc89 pbrook
    s->set_reg = set_reg;
1402 56aebc89 pbrook
    s->xml = xml;
1403 56aebc89 pbrook
    p = &env->gdb_regs;
1404 56aebc89 pbrook
    while (*p) {
1405 56aebc89 pbrook
        /* Check for duplicates.  */
1406 56aebc89 pbrook
        if (strcmp((*p)->xml, xml) == 0)
1407 56aebc89 pbrook
            return;
1408 56aebc89 pbrook
        p = &(*p)->next;
1409 56aebc89 pbrook
    }
1410 56aebc89 pbrook
    /* Add to end of list.  */
1411 56aebc89 pbrook
    last_reg += num_regs;
1412 56aebc89 pbrook
    *p = s;
1413 56aebc89 pbrook
    if (g_pos) {
1414 56aebc89 pbrook
        if (g_pos != s->base_reg) {
1415 56aebc89 pbrook
            fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
1416 56aebc89 pbrook
                    "Expected %d got %d\n", xml, g_pos, s->base_reg);
1417 56aebc89 pbrook
        } else {
1418 56aebc89 pbrook
            num_g_regs = last_reg;
1419 56aebc89 pbrook
        }
1420 56aebc89 pbrook
    }
1421 6da41eaf bellard
}
1422 6da41eaf bellard
1423 a1d1bb31 aliguori
#ifndef CONFIG_USER_ONLY
1424 a1d1bb31 aliguori
static const int xlat_gdb_type[] = {
1425 a1d1bb31 aliguori
    [GDB_WATCHPOINT_WRITE]  = BP_GDB | BP_MEM_WRITE,
1426 a1d1bb31 aliguori
    [GDB_WATCHPOINT_READ]   = BP_GDB | BP_MEM_READ,
1427 a1d1bb31 aliguori
    [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
1428 a1d1bb31 aliguori
};
1429 a1d1bb31 aliguori
#endif
1430 a1d1bb31 aliguori
1431 880a7578 aliguori
static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
1432 a1d1bb31 aliguori
{
1433 880a7578 aliguori
    CPUState *env;
1434 880a7578 aliguori
    int err = 0;
1435 880a7578 aliguori
1436 e22a25c9 aliguori
    if (kvm_enabled())
1437 e22a25c9 aliguori
        return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
1438 e22a25c9 aliguori
1439 a1d1bb31 aliguori
    switch (type) {
1440 a1d1bb31 aliguori
    case GDB_BREAKPOINT_SW:
1441 a1d1bb31 aliguori
    case GDB_BREAKPOINT_HW:
1442 880a7578 aliguori
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
1443 880a7578 aliguori
            err = cpu_breakpoint_insert(env, addr, BP_GDB, NULL);
1444 880a7578 aliguori
            if (err)
1445 880a7578 aliguori
                break;
1446 880a7578 aliguori
        }
1447 880a7578 aliguori
        return err;
1448 a1d1bb31 aliguori
#ifndef CONFIG_USER_ONLY
1449 a1d1bb31 aliguori
    case GDB_WATCHPOINT_WRITE:
1450 a1d1bb31 aliguori
    case GDB_WATCHPOINT_READ:
1451 a1d1bb31 aliguori
    case GDB_WATCHPOINT_ACCESS:
1452 880a7578 aliguori
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
1453 880a7578 aliguori
            err = cpu_watchpoint_insert(env, addr, len, xlat_gdb_type[type],
1454 880a7578 aliguori
                                        NULL);
1455 880a7578 aliguori
            if (err)
1456 880a7578 aliguori
                break;
1457 880a7578 aliguori
        }
1458 880a7578 aliguori
        return err;
1459 a1d1bb31 aliguori
#endif
1460 a1d1bb31 aliguori
    default:
1461 a1d1bb31 aliguori
        return -ENOSYS;
1462 a1d1bb31 aliguori
    }
1463 a1d1bb31 aliguori
}
1464 a1d1bb31 aliguori
1465 880a7578 aliguori
static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
1466 a1d1bb31 aliguori
{
1467 880a7578 aliguori
    CPUState *env;
1468 880a7578 aliguori
    int err = 0;
1469 880a7578 aliguori
1470 e22a25c9 aliguori
    if (kvm_enabled())
1471 e22a25c9 aliguori
        return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
1472 e22a25c9 aliguori
1473 a1d1bb31 aliguori
    switch (type) {
1474 a1d1bb31 aliguori
    case GDB_BREAKPOINT_SW:
1475 a1d1bb31 aliguori
    case GDB_BREAKPOINT_HW:
1476 880a7578 aliguori
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
1477 880a7578 aliguori
            err = cpu_breakpoint_remove(env, addr, BP_GDB);
1478 880a7578 aliguori
            if (err)
1479 880a7578 aliguori
                break;
1480 880a7578 aliguori
        }
1481 880a7578 aliguori
        return err;
1482 a1d1bb31 aliguori
#ifndef CONFIG_USER_ONLY
1483 a1d1bb31 aliguori
    case GDB_WATCHPOINT_WRITE:
1484 a1d1bb31 aliguori
    case GDB_WATCHPOINT_READ:
1485 a1d1bb31 aliguori
    case GDB_WATCHPOINT_ACCESS:
1486 880a7578 aliguori
        for (env = first_cpu; env != NULL; env = env->next_cpu) {
1487 880a7578 aliguori
            err = cpu_watchpoint_remove(env, addr, len, xlat_gdb_type[type]);
1488 880a7578 aliguori
            if (err)
1489 880a7578 aliguori
                break;
1490 880a7578 aliguori
        }
1491 880a7578 aliguori
        return err;
1492 a1d1bb31 aliguori
#endif
1493 a1d1bb31 aliguori
    default:
1494 a1d1bb31 aliguori
        return -ENOSYS;
1495 a1d1bb31 aliguori
    }
1496 a1d1bb31 aliguori
}
1497 a1d1bb31 aliguori
1498 880a7578 aliguori
static void gdb_breakpoint_remove_all(void)
1499 a1d1bb31 aliguori
{
1500 880a7578 aliguori
    CPUState *env;
1501 880a7578 aliguori
1502 e22a25c9 aliguori
    if (kvm_enabled()) {
1503 e22a25c9 aliguori
        kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
1504 e22a25c9 aliguori
        return;
1505 e22a25c9 aliguori
    }
1506 e22a25c9 aliguori
1507 880a7578 aliguori
    for (env = first_cpu; env != NULL; env = env->next_cpu) {
1508 880a7578 aliguori
        cpu_breakpoint_remove_all(env, BP_GDB);
1509 a1d1bb31 aliguori
#ifndef CONFIG_USER_ONLY
1510 880a7578 aliguori
        cpu_watchpoint_remove_all(env, BP_GDB);
1511 a1d1bb31 aliguori
#endif
1512 880a7578 aliguori
    }
1513 a1d1bb31 aliguori
}
1514 a1d1bb31 aliguori
1515 fab9d284 aurel32
static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
1516 fab9d284 aurel32
{
1517 fab9d284 aurel32
#if defined(TARGET_I386)
1518 fab9d284 aurel32
    s->c_cpu->eip = pc;
1519 fab9d284 aurel32
    cpu_synchronize_state(s->c_cpu, 1);
1520 fab9d284 aurel32
#elif defined (TARGET_PPC)
1521 fab9d284 aurel32
    s->c_cpu->nip = pc;
1522 fab9d284 aurel32
#elif defined (TARGET_SPARC)
1523 fab9d284 aurel32
    s->c_cpu->pc = pc;
1524 fab9d284 aurel32
    s->c_cpu->npc = pc + 4;
1525 fab9d284 aurel32
#elif defined (TARGET_ARM)
1526 fab9d284 aurel32
    s->c_cpu->regs[15] = pc;
1527 fab9d284 aurel32
#elif defined (TARGET_SH4)
1528 fab9d284 aurel32
    s->c_cpu->pc = pc;
1529 fab9d284 aurel32
#elif defined (TARGET_MIPS)
1530 fab9d284 aurel32
    s->c_cpu->active_tc.PC = pc;
1531 fab9d284 aurel32
#elif defined (TARGET_CRIS)
1532 fab9d284 aurel32
    s->c_cpu->pc = pc;
1533 fab9d284 aurel32
#elif defined (TARGET_ALPHA)
1534 fab9d284 aurel32
    s->c_cpu->pc = pc;
1535 fab9d284 aurel32
#endif
1536 fab9d284 aurel32
}
1537 fab9d284 aurel32
1538 880a7578 aliguori
static int gdb_handle_packet(GDBState *s, const char *line_buf)
1539 b4608c04 bellard
{
1540 880a7578 aliguori
    CPUState *env;
1541 b4608c04 bellard
    const char *p;
1542 880a7578 aliguori
    int ch, reg_size, type, res, thread;
1543 56aebc89 pbrook
    char buf[MAX_PACKET_LENGTH];
1544 56aebc89 pbrook
    uint8_t mem_buf[MAX_PACKET_LENGTH];
1545 56aebc89 pbrook
    uint8_t *registers;
1546 9d9754a3 bellard
    target_ulong addr, len;
1547 3b46e624 ths
1548 858693c6 bellard
#ifdef DEBUG_GDB
1549 858693c6 bellard
    printf("command='%s'\n", line_buf);
1550 858693c6 bellard
#endif
1551 858693c6 bellard
    p = line_buf;
1552 858693c6 bellard
    ch = *p++;
1553 858693c6 bellard
    switch(ch) {
1554 858693c6 bellard
    case '?':
1555 1fddef4b bellard
        /* TODO: Make this return the correct value for user-mode.  */
1556 ca587a8e aurel32
        snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
1557 880a7578 aliguori
                 s->c_cpu->cpu_index+1);
1558 858693c6 bellard
        put_packet(s, buf);
1559 7d03f82f edgar_igl
        /* Remove all the breakpoints when this query is issued,
1560 7d03f82f edgar_igl
         * because gdb is doing and initial connect and the state
1561 7d03f82f edgar_igl
         * should be cleaned up.
1562 7d03f82f edgar_igl
         */
1563 880a7578 aliguori
        gdb_breakpoint_remove_all();
1564 858693c6 bellard
        break;
1565 858693c6 bellard
    case 'c':
1566 858693c6 bellard
        if (*p != '\0') {
1567 9d9754a3 bellard
            addr = strtoull(p, (char **)&p, 16);
1568 fab9d284 aurel32
            gdb_set_cpu_pc(s, addr);
1569 858693c6 bellard
        }
1570 ca587a8e aurel32
        s->signal = 0;
1571 ba70a624 edgar_igl
        gdb_continue(s);
1572 41625033 bellard
        return RS_IDLE;
1573 1f487ee9 edgar_igl
    case 'C':
1574 ca587a8e aurel32
        s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
1575 ca587a8e aurel32
        if (s->signal == -1)
1576 ca587a8e aurel32
            s->signal = 0;
1577 1f487ee9 edgar_igl
        gdb_continue(s);
1578 1f487ee9 edgar_igl
        return RS_IDLE;
1579 7d03f82f edgar_igl
    case 'k':
1580 7d03f82f edgar_igl
        /* Kill the target */
1581 7d03f82f edgar_igl
        fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
1582 7d03f82f edgar_igl
        exit(0);
1583 7d03f82f edgar_igl
    case 'D':
1584 7d03f82f edgar_igl
        /* Detach packet */
1585 880a7578 aliguori
        gdb_breakpoint_remove_all();
1586 7d03f82f edgar_igl
        gdb_continue(s);
1587 7d03f82f edgar_igl
        put_packet(s, "OK");
1588 7d03f82f edgar_igl
        break;
1589 858693c6 bellard
    case 's':
1590 858693c6 bellard
        if (*p != '\0') {
1591 8fac5803 ths
            addr = strtoull(p, (char **)&p, 16);
1592 fab9d284 aurel32
            gdb_set_cpu_pc(s, addr);
1593 858693c6 bellard
        }
1594 880a7578 aliguori
        cpu_single_step(s->c_cpu, sstep_flags);
1595 ba70a624 edgar_igl
        gdb_continue(s);
1596 41625033 bellard
        return RS_IDLE;
1597 a2d1ebaf pbrook
    case 'F':
1598 a2d1ebaf pbrook
        {
1599 a2d1ebaf pbrook
            target_ulong ret;
1600 a2d1ebaf pbrook
            target_ulong err;
1601 a2d1ebaf pbrook
1602 a2d1ebaf pbrook
            ret = strtoull(p, (char **)&p, 16);
1603 a2d1ebaf pbrook
            if (*p == ',') {
1604 a2d1ebaf pbrook
                p++;
1605 a2d1ebaf pbrook
                err = strtoull(p, (char **)&p, 16);
1606 a2d1ebaf pbrook
            } else {
1607 a2d1ebaf pbrook
                err = 0;
1608 a2d1ebaf pbrook
            }
1609 a2d1ebaf pbrook
            if (*p == ',')
1610 a2d1ebaf pbrook
                p++;
1611 a2d1ebaf pbrook
            type = *p;
1612 a2d1ebaf pbrook
            if (gdb_current_syscall_cb)
1613 880a7578 aliguori
                gdb_current_syscall_cb(s->c_cpu, ret, err);
1614 a2d1ebaf pbrook
            if (type == 'C') {
1615 a2d1ebaf pbrook
                put_packet(s, "T02");
1616 a2d1ebaf pbrook
            } else {
1617 ba70a624 edgar_igl
                gdb_continue(s);
1618 a2d1ebaf pbrook
            }
1619 a2d1ebaf pbrook
        }
1620 a2d1ebaf pbrook
        break;
1621 858693c6 bellard
    case 'g':
1622 e22a25c9 aliguori
        cpu_synchronize_state(s->g_cpu, 0);
1623 56aebc89 pbrook
        len = 0;
1624 56aebc89 pbrook
        for (addr = 0; addr < num_g_regs; addr++) {
1625 880a7578 aliguori
            reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
1626 56aebc89 pbrook
            len += reg_size;
1627 56aebc89 pbrook
        }
1628 56aebc89 pbrook
        memtohex(buf, mem_buf, len);
1629 858693c6 bellard
        put_packet(s, buf);
1630 858693c6 bellard
        break;
1631 858693c6 bellard
    case 'G':
1632 56aebc89 pbrook
        registers = mem_buf;
1633 858693c6 bellard
        len = strlen(p) / 2;
1634 858693c6 bellard
        hextomem((uint8_t *)registers, p, len);
1635 56aebc89 pbrook
        for (addr = 0; addr < num_g_regs && len > 0; addr++) {
1636 880a7578 aliguori
            reg_size = gdb_write_register(s->g_cpu, registers, addr);
1637 56aebc89 pbrook
            len -= reg_size;
1638 56aebc89 pbrook
            registers += reg_size;
1639 56aebc89 pbrook
        }
1640 e22a25c9 aliguori
        cpu_synchronize_state(s->g_cpu, 1);
1641 858693c6 bellard
        put_packet(s, "OK");
1642 858693c6 bellard
        break;
1643 858693c6 bellard
    case 'm':
1644 9d9754a3 bellard
        addr = strtoull(p, (char **)&p, 16);
1645 858693c6 bellard
        if (*p == ',')
1646 858693c6 bellard
            p++;
1647 9d9754a3 bellard
        len = strtoull(p, NULL, 16);
1648 880a7578 aliguori
        if (cpu_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 0) != 0) {
1649 6f970bd9 bellard
            put_packet (s, "E14");
1650 6f970bd9 bellard
        } else {
1651 6f970bd9 bellard
            memtohex(buf, mem_buf, len);
1652 6f970bd9 bellard
            put_packet(s, buf);
1653 6f970bd9 bellard
        }
1654 858693c6 bellard
        break;
1655 858693c6 bellard
    case 'M':
1656 9d9754a3 bellard
        addr = strtoull(p, (char **)&p, 16);
1657 858693c6 bellard
        if (*p == ',')
1658 858693c6 bellard
            p++;
1659 9d9754a3 bellard
        len = strtoull(p, (char **)&p, 16);
1660 b328f873 bellard
        if (*p == ':')
1661 858693c6 bellard
            p++;
1662 858693c6 bellard
        hextomem(mem_buf, p, len);
1663 880a7578 aliguori
        if (cpu_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 1) != 0)
1664 905f20b1 bellard
            put_packet(s, "E14");
1665 858693c6 bellard
        else
1666 858693c6 bellard
            put_packet(s, "OK");
1667 858693c6 bellard
        break;
1668 56aebc89 pbrook
    case 'p':
1669 56aebc89 pbrook
        /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1670 56aebc89 pbrook
           This works, but can be very slow.  Anything new enough to
1671 56aebc89 pbrook
           understand XML also knows how to use this properly.  */
1672 56aebc89 pbrook
        if (!gdb_has_xml)
1673 56aebc89 pbrook
            goto unknown_command;
1674 56aebc89 pbrook
        addr = strtoull(p, (char **)&p, 16);
1675 880a7578 aliguori
        reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
1676 56aebc89 pbrook
        if (reg_size) {
1677 56aebc89 pbrook
            memtohex(buf, mem_buf, reg_size);
1678 56aebc89 pbrook
            put_packet(s, buf);
1679 56aebc89 pbrook
        } else {
1680 56aebc89 pbrook
            put_packet(s, "E14");
1681 56aebc89 pbrook
        }
1682 56aebc89 pbrook
        break;
1683 56aebc89 pbrook
    case 'P':
1684 56aebc89 pbrook
        if (!gdb_has_xml)
1685 56aebc89 pbrook
            goto unknown_command;
1686 56aebc89 pbrook
        addr = strtoull(p, (char **)&p, 16);
1687 56aebc89 pbrook
        if (*p == '=')
1688 56aebc89 pbrook
            p++;
1689 56aebc89 pbrook
        reg_size = strlen(p) / 2;
1690 56aebc89 pbrook
        hextomem(mem_buf, p, reg_size);
1691 880a7578 aliguori
        gdb_write_register(s->g_cpu, mem_buf, addr);
1692 56aebc89 pbrook
        put_packet(s, "OK");
1693 56aebc89 pbrook
        break;
1694 858693c6 bellard
    case 'Z':
1695 858693c6 bellard
    case 'z':
1696 858693c6 bellard
        type = strtoul(p, (char **)&p, 16);
1697 858693c6 bellard
        if (*p == ',')
1698 858693c6 bellard
            p++;
1699 9d9754a3 bellard
        addr = strtoull(p, (char **)&p, 16);
1700 858693c6 bellard
        if (*p == ',')
1701 858693c6 bellard
            p++;
1702 9d9754a3 bellard
        len = strtoull(p, (char **)&p, 16);
1703 a1d1bb31 aliguori
        if (ch == 'Z')
1704 880a7578 aliguori
            res = gdb_breakpoint_insert(addr, len, type);
1705 a1d1bb31 aliguori
        else
1706 880a7578 aliguori
            res = gdb_breakpoint_remove(addr, len, type);
1707 a1d1bb31 aliguori
        if (res >= 0)
1708 a1d1bb31 aliguori
             put_packet(s, "OK");
1709 a1d1bb31 aliguori
        else if (res == -ENOSYS)
1710 0f459d16 pbrook
            put_packet(s, "");
1711 a1d1bb31 aliguori
        else
1712 a1d1bb31 aliguori
            put_packet(s, "E22");
1713 858693c6 bellard
        break;
1714 880a7578 aliguori
    case 'H':
1715 880a7578 aliguori
        type = *p++;
1716 880a7578 aliguori
        thread = strtoull(p, (char **)&p, 16);
1717 880a7578 aliguori
        if (thread == -1 || thread == 0) {
1718 880a7578 aliguori
            put_packet(s, "OK");
1719 880a7578 aliguori
            break;
1720 880a7578 aliguori
        }
1721 880a7578 aliguori
        for (env = first_cpu; env != NULL; env = env->next_cpu)
1722 880a7578 aliguori
            if (env->cpu_index + 1 == thread)
1723 880a7578 aliguori
                break;
1724 880a7578 aliguori
        if (env == NULL) {
1725 880a7578 aliguori
            put_packet(s, "E22");
1726 880a7578 aliguori
            break;
1727 880a7578 aliguori
        }
1728 880a7578 aliguori
        switch (type) {
1729 880a7578 aliguori
        case 'c':
1730 880a7578 aliguori
            s->c_cpu = env;
1731 880a7578 aliguori
            put_packet(s, "OK");
1732 880a7578 aliguori
            break;
1733 880a7578 aliguori
        case 'g':
1734 880a7578 aliguori
            s->g_cpu = env;
1735 880a7578 aliguori
            put_packet(s, "OK");
1736 880a7578 aliguori
            break;
1737 880a7578 aliguori
        default:
1738 880a7578 aliguori
             put_packet(s, "E22");
1739 880a7578 aliguori
             break;
1740 880a7578 aliguori
        }
1741 880a7578 aliguori
        break;
1742 880a7578 aliguori
    case 'T':
1743 880a7578 aliguori
        thread = strtoull(p, (char **)&p, 16);
1744 880a7578 aliguori
#ifndef CONFIG_USER_ONLY
1745 880a7578 aliguori
        if (thread > 0 && thread < smp_cpus + 1)
1746 880a7578 aliguori
#else
1747 880a7578 aliguori
        if (thread == 1)
1748 880a7578 aliguori
#endif
1749 880a7578 aliguori
             put_packet(s, "OK");
1750 880a7578 aliguori
        else
1751 880a7578 aliguori
            put_packet(s, "E22");
1752 880a7578 aliguori
        break;
1753 978efd6a pbrook
    case 'q':
1754 60897d36 edgar_igl
    case 'Q':
1755 60897d36 edgar_igl
        /* parse any 'q' packets here */
1756 60897d36 edgar_igl
        if (!strcmp(p,"qemu.sstepbits")) {
1757 60897d36 edgar_igl
            /* Query Breakpoint bit definitions */
1758 363a37d5 blueswir1
            snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1759 363a37d5 blueswir1
                     SSTEP_ENABLE,
1760 363a37d5 blueswir1
                     SSTEP_NOIRQ,
1761 363a37d5 blueswir1
                     SSTEP_NOTIMER);
1762 60897d36 edgar_igl
            put_packet(s, buf);
1763 60897d36 edgar_igl
            break;
1764 60897d36 edgar_igl
        } else if (strncmp(p,"qemu.sstep",10) == 0) {
1765 60897d36 edgar_igl
            /* Display or change the sstep_flags */
1766 60897d36 edgar_igl
            p += 10;
1767 60897d36 edgar_igl
            if (*p != '=') {
1768 60897d36 edgar_igl
                /* Display current setting */
1769 363a37d5 blueswir1
                snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
1770 60897d36 edgar_igl
                put_packet(s, buf);
1771 60897d36 edgar_igl
                break;
1772 60897d36 edgar_igl
            }
1773 60897d36 edgar_igl
            p++;
1774 60897d36 edgar_igl
            type = strtoul(p, (char **)&p, 16);
1775 60897d36 edgar_igl
            sstep_flags = type;
1776 60897d36 edgar_igl
            put_packet(s, "OK");
1777 60897d36 edgar_igl
            break;
1778 880a7578 aliguori
        } else if (strcmp(p,"C") == 0) {
1779 880a7578 aliguori
            /* "Current thread" remains vague in the spec, so always return
1780 880a7578 aliguori
             *  the first CPU (gdb returns the first thread). */
1781 880a7578 aliguori
            put_packet(s, "QC1");
1782 880a7578 aliguori
            break;
1783 880a7578 aliguori
        } else if (strcmp(p,"fThreadInfo") == 0) {
1784 880a7578 aliguori
            s->query_cpu = first_cpu;
1785 880a7578 aliguori
            goto report_cpuinfo;
1786 880a7578 aliguori
        } else if (strcmp(p,"sThreadInfo") == 0) {
1787 880a7578 aliguori
        report_cpuinfo:
1788 880a7578 aliguori
            if (s->query_cpu) {
1789 880a7578 aliguori
                snprintf(buf, sizeof(buf), "m%x", s->query_cpu->cpu_index+1);
1790 880a7578 aliguori
                put_packet(s, buf);
1791 880a7578 aliguori
                s->query_cpu = s->query_cpu->next_cpu;
1792 880a7578 aliguori
            } else
1793 880a7578 aliguori
                put_packet(s, "l");
1794 880a7578 aliguori
            break;
1795 880a7578 aliguori
        } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1796 880a7578 aliguori
            thread = strtoull(p+16, (char **)&p, 16);
1797 880a7578 aliguori
            for (env = first_cpu; env != NULL; env = env->next_cpu)
1798 880a7578 aliguori
                if (env->cpu_index + 1 == thread) {
1799 e22a25c9 aliguori
                    cpu_synchronize_state(env, 0);
1800 880a7578 aliguori
                    len = snprintf((char *)mem_buf, sizeof(mem_buf),
1801 880a7578 aliguori
                                   "CPU#%d [%s]", env->cpu_index,
1802 880a7578 aliguori
                                   env->halted ? "halted " : "running");
1803 880a7578 aliguori
                    memtohex(buf, mem_buf, len);
1804 880a7578 aliguori
                    put_packet(s, buf);
1805 880a7578 aliguori
                    break;
1806 880a7578 aliguori
                }
1807 880a7578 aliguori
            break;
1808 60897d36 edgar_igl
        }
1809 0b8a988c blueswir1
#ifdef CONFIG_USER_ONLY
1810 60897d36 edgar_igl
        else if (strncmp(p, "Offsets", 7) == 0) {
1811 880a7578 aliguori
            TaskState *ts = s->c_cpu->opaque;
1812 978efd6a pbrook
1813 363a37d5 blueswir1
            snprintf(buf, sizeof(buf),
1814 363a37d5 blueswir1
                     "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1815 363a37d5 blueswir1
                     ";Bss=" TARGET_ABI_FMT_lx,
1816 363a37d5 blueswir1
                     ts->info->code_offset,
1817 363a37d5 blueswir1
                     ts->info->data_offset,
1818 363a37d5 blueswir1
                     ts->info->data_offset);
1819 978efd6a pbrook
            put_packet(s, buf);
1820 978efd6a pbrook
            break;
1821 978efd6a pbrook
        }
1822 0b8a988c blueswir1
#else /* !CONFIG_USER_ONLY */
1823 8a34a0fb aliguori
        else if (strncmp(p, "Rcmd,", 5) == 0) {
1824 8a34a0fb aliguori
            int len = strlen(p + 5);
1825 8a34a0fb aliguori
1826 8a34a0fb aliguori
            if ((len % 2) != 0) {
1827 8a34a0fb aliguori
                put_packet(s, "E01");
1828 8a34a0fb aliguori
                break;
1829 8a34a0fb aliguori
            }
1830 8a34a0fb aliguori
            hextomem(mem_buf, p + 5, len);
1831 8a34a0fb aliguori
            len = len / 2;
1832 8a34a0fb aliguori
            mem_buf[len++] = 0;
1833 8a34a0fb aliguori
            qemu_chr_read(s->mon_chr, mem_buf, len);
1834 8a34a0fb aliguori
            put_packet(s, "OK");
1835 8a34a0fb aliguori
            break;
1836 8a34a0fb aliguori
        }
1837 0b8a988c blueswir1
#endif /* !CONFIG_USER_ONLY */
1838 56aebc89 pbrook
        if (strncmp(p, "Supported", 9) == 0) {
1839 5b3715bf blueswir1
            snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
1840 56aebc89 pbrook
#ifdef GDB_CORE_XML
1841 2dc766da blueswir1
            pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1842 56aebc89 pbrook
#endif
1843 56aebc89 pbrook
            put_packet(s, buf);
1844 56aebc89 pbrook
            break;
1845 56aebc89 pbrook
        }
1846 56aebc89 pbrook
#ifdef GDB_CORE_XML
1847 56aebc89 pbrook
        if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1848 56aebc89 pbrook
            const char *xml;
1849 56aebc89 pbrook
            target_ulong total_len;
1850 56aebc89 pbrook
1851 56aebc89 pbrook
            gdb_has_xml = 1;
1852 56aebc89 pbrook
            p += 19;
1853 880a7578 aliguori
            xml = get_feature_xml(p, &p);
1854 56aebc89 pbrook
            if (!xml) {
1855 5b3715bf blueswir1
                snprintf(buf, sizeof(buf), "E00");
1856 56aebc89 pbrook
                put_packet(s, buf);
1857 56aebc89 pbrook
                break;
1858 56aebc89 pbrook
            }
1859 56aebc89 pbrook
1860 56aebc89 pbrook
            if (*p == ':')
1861 56aebc89 pbrook
                p++;
1862 56aebc89 pbrook
            addr = strtoul(p, (char **)&p, 16);
1863 56aebc89 pbrook
            if (*p == ',')
1864 56aebc89 pbrook
                p++;
1865 56aebc89 pbrook
            len = strtoul(p, (char **)&p, 16);
1866 56aebc89 pbrook
1867 56aebc89 pbrook
            total_len = strlen(xml);
1868 56aebc89 pbrook
            if (addr > total_len) {
1869 5b3715bf blueswir1
                snprintf(buf, sizeof(buf), "E00");
1870 56aebc89 pbrook
                put_packet(s, buf);
1871 56aebc89 pbrook
                break;
1872 56aebc89 pbrook
            }
1873 56aebc89 pbrook
            if (len > (MAX_PACKET_LENGTH - 5) / 2)
1874 56aebc89 pbrook
                len = (MAX_PACKET_LENGTH - 5) / 2;
1875 56aebc89 pbrook
            if (len < total_len - addr) {
1876 56aebc89 pbrook
                buf[0] = 'm';
1877 56aebc89 pbrook
                len = memtox(buf + 1, xml + addr, len);
1878 56aebc89 pbrook
            } else {
1879 56aebc89 pbrook
                buf[0] = 'l';
1880 56aebc89 pbrook
                len = memtox(buf + 1, xml + addr, total_len - addr);
1881 56aebc89 pbrook
            }
1882 56aebc89 pbrook
            put_packet_binary(s, buf, len + 1);
1883 56aebc89 pbrook
            break;
1884 56aebc89 pbrook
        }
1885 56aebc89 pbrook
#endif
1886 56aebc89 pbrook
        /* Unrecognised 'q' command.  */
1887 56aebc89 pbrook
        goto unknown_command;
1888 56aebc89 pbrook
1889 858693c6 bellard
    default:
1890 56aebc89 pbrook
    unknown_command:
1891 858693c6 bellard
        /* put empty packet */
1892 858693c6 bellard
        buf[0] = '\0';
1893 858693c6 bellard
        put_packet(s, buf);
1894 858693c6 bellard
        break;
1895 858693c6 bellard
    }
1896 858693c6 bellard
    return RS_IDLE;
1897 858693c6 bellard
}
1898 858693c6 bellard
1899 880a7578 aliguori
void gdb_set_stop_cpu(CPUState *env)
1900 880a7578 aliguori
{
1901 880a7578 aliguori
    gdbserver_state->c_cpu = env;
1902 880a7578 aliguori
    gdbserver_state->g_cpu = env;
1903 880a7578 aliguori
}
1904 880a7578 aliguori
1905 1fddef4b bellard
#ifndef CONFIG_USER_ONLY
1906 9781e040 aliguori
static void gdb_vm_state_change(void *opaque, int running, int reason)
1907 858693c6 bellard
{
1908 880a7578 aliguori
    GDBState *s = gdbserver_state;
1909 880a7578 aliguori
    CPUState *env = s->c_cpu;
1910 858693c6 bellard
    char buf[256];
1911 d6fc1b39 aliguori
    const char *type;
1912 858693c6 bellard
    int ret;
1913 858693c6 bellard
1914 9781e040 aliguori
    if (running || (reason != EXCP_DEBUG && reason != EXCP_INTERRUPT) ||
1915 36556b20 aliguori
        s->state == RS_INACTIVE || s->state == RS_SYSCALL)
1916 a2d1ebaf pbrook
        return;
1917 a2d1ebaf pbrook
1918 858693c6 bellard
    /* disable single step if it was enable */
1919 880a7578 aliguori
    cpu_single_step(env, 0);
1920 858693c6 bellard
1921 e80cfcfc bellard
    if (reason == EXCP_DEBUG) {
1922 880a7578 aliguori
        if (env->watchpoint_hit) {
1923 880a7578 aliguori
            switch (env->watchpoint_hit->flags & BP_MEM_ACCESS) {
1924 a1d1bb31 aliguori
            case BP_MEM_READ:
1925 d6fc1b39 aliguori
                type = "r";
1926 d6fc1b39 aliguori
                break;
1927 a1d1bb31 aliguori
            case BP_MEM_ACCESS:
1928 d6fc1b39 aliguori
                type = "a";
1929 d6fc1b39 aliguori
                break;
1930 d6fc1b39 aliguori
            default:
1931 d6fc1b39 aliguori
                type = "";
1932 d6fc1b39 aliguori
                break;
1933 d6fc1b39 aliguori
            }
1934 880a7578 aliguori
            snprintf(buf, sizeof(buf),
1935 880a7578 aliguori
                     "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
1936 ca587a8e aurel32
                     GDB_SIGNAL_TRAP, env->cpu_index+1, type,
1937 880a7578 aliguori
                     env->watchpoint_hit->vaddr);
1938 6658ffb8 pbrook
            put_packet(s, buf);
1939 880a7578 aliguori
            env->watchpoint_hit = NULL;
1940 6658ffb8 pbrook
            return;
1941 6658ffb8 pbrook
        }
1942 880a7578 aliguori
        tb_flush(env);
1943 ca587a8e aurel32
        ret = GDB_SIGNAL_TRAP;
1944 bbeb7b5c bellard
    } else {
1945 9781e040 aliguori
        ret = GDB_SIGNAL_INT;
1946 bbeb7b5c bellard
    }
1947 880a7578 aliguori
    snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, env->cpu_index+1);
1948 858693c6 bellard
    put_packet(s, buf);
1949 858693c6 bellard
}
1950 1fddef4b bellard
#endif
1951 858693c6 bellard
1952 a2d1ebaf pbrook
/* Send a gdb syscall request.
1953 a2d1ebaf pbrook
   This accepts limited printf-style format specifiers, specifically:
1954 a87295e8 pbrook
    %x  - target_ulong argument printed in hex.
1955 a87295e8 pbrook
    %lx - 64-bit argument printed in hex.
1956 a87295e8 pbrook
    %s  - string pointer (target_ulong) and length (int) pair.  */
1957 7ccfb2eb blueswir1
void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
1958 a2d1ebaf pbrook
{
1959 a2d1ebaf pbrook
    va_list va;
1960 a2d1ebaf pbrook
    char buf[256];
1961 a2d1ebaf pbrook
    char *p;
1962 a2d1ebaf pbrook
    target_ulong addr;
1963 a87295e8 pbrook
    uint64_t i64;
1964 a2d1ebaf pbrook
    GDBState *s;
1965 a2d1ebaf pbrook
1966 880a7578 aliguori
    s = gdbserver_state;
1967 a2d1ebaf pbrook
    if (!s)
1968 a2d1ebaf pbrook
        return;
1969 a2d1ebaf pbrook
    gdb_current_syscall_cb = cb;
1970 a2d1ebaf pbrook
    s->state = RS_SYSCALL;
1971 a2d1ebaf pbrook
#ifndef CONFIG_USER_ONLY
1972 a2d1ebaf pbrook
    vm_stop(EXCP_DEBUG);
1973 a2d1ebaf pbrook
#endif
1974 a2d1ebaf pbrook
    s->state = RS_IDLE;
1975 a2d1ebaf pbrook
    va_start(va, fmt);
1976 a2d1ebaf pbrook
    p = buf;
1977 a2d1ebaf pbrook
    *(p++) = 'F';
1978 a2d1ebaf pbrook
    while (*fmt) {
1979 a2d1ebaf pbrook
        if (*fmt == '%') {
1980 a2d1ebaf pbrook
            fmt++;
1981 a2d1ebaf pbrook
            switch (*fmt++) {
1982 a2d1ebaf pbrook
            case 'x':
1983 a2d1ebaf pbrook
                addr = va_arg(va, target_ulong);
1984 363a37d5 blueswir1
                p += snprintf(p, &buf[sizeof(buf)] - p, TARGET_FMT_lx, addr);
1985 a2d1ebaf pbrook
                break;
1986 a87295e8 pbrook
            case 'l':
1987 a87295e8 pbrook
                if (*(fmt++) != 'x')
1988 a87295e8 pbrook
                    goto bad_format;
1989 a87295e8 pbrook
                i64 = va_arg(va, uint64_t);
1990 363a37d5 blueswir1
                p += snprintf(p, &buf[sizeof(buf)] - p, "%" PRIx64, i64);
1991 a87295e8 pbrook
                break;
1992 a2d1ebaf pbrook
            case 's':
1993 a2d1ebaf pbrook
                addr = va_arg(va, target_ulong);
1994 363a37d5 blueswir1
                p += snprintf(p, &buf[sizeof(buf)] - p, TARGET_FMT_lx "/%x",
1995 363a37d5 blueswir1
                              addr, va_arg(va, int));
1996 a2d1ebaf pbrook
                break;
1997 a2d1ebaf pbrook
            default:
1998 a87295e8 pbrook
            bad_format:
1999 a2d1ebaf pbrook
                fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
2000 a2d1ebaf pbrook
                        fmt - 1);
2001 a2d1ebaf pbrook
                break;
2002 a2d1ebaf pbrook
            }
2003 a2d1ebaf pbrook
        } else {
2004 a2d1ebaf pbrook
            *(p++) = *(fmt++);
2005 a2d1ebaf pbrook
        }
2006 a2d1ebaf pbrook
    }
2007 8a93e02a pbrook
    *p = 0;
2008 a2d1ebaf pbrook
    va_end(va);
2009 a2d1ebaf pbrook
    put_packet(s, buf);
2010 a2d1ebaf pbrook
#ifdef CONFIG_USER_ONLY
2011 880a7578 aliguori
    gdb_handlesig(s->c_cpu, 0);
2012 a2d1ebaf pbrook
#else
2013 3098dba0 aurel32
    cpu_exit(s->c_cpu);
2014 a2d1ebaf pbrook
#endif
2015 a2d1ebaf pbrook
}
2016 a2d1ebaf pbrook
2017 6a00d601 bellard
static void gdb_read_byte(GDBState *s, int ch)
2018 858693c6 bellard
{
2019 858693c6 bellard
    int i, csum;
2020 60fe76f3 ths
    uint8_t reply;
2021 858693c6 bellard
2022 1fddef4b bellard
#ifndef CONFIG_USER_ONLY
2023 4046d913 pbrook
    if (s->last_packet_len) {
2024 4046d913 pbrook
        /* Waiting for a response to the last packet.  If we see the start
2025 4046d913 pbrook
           of a new command then abandon the previous response.  */
2026 4046d913 pbrook
        if (ch == '-') {
2027 4046d913 pbrook
#ifdef DEBUG_GDB
2028 4046d913 pbrook
            printf("Got NACK, retransmitting\n");
2029 4046d913 pbrook
#endif
2030 ffe8ab83 ths
            put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
2031 4046d913 pbrook
        }
2032 4046d913 pbrook
#ifdef DEBUG_GDB
2033 4046d913 pbrook
        else if (ch == '+')
2034 4046d913 pbrook
            printf("Got ACK\n");
2035 4046d913 pbrook
        else
2036 4046d913 pbrook
            printf("Got '%c' when expecting ACK/NACK\n", ch);
2037 4046d913 pbrook
#endif
2038 4046d913 pbrook
        if (ch == '+' || ch == '$')
2039 4046d913 pbrook
            s->last_packet_len = 0;
2040 4046d913 pbrook
        if (ch != '$')
2041 4046d913 pbrook
            return;
2042 4046d913 pbrook
    }
2043 858693c6 bellard
    if (vm_running) {
2044 858693c6 bellard
        /* when the CPU is running, we cannot do anything except stop
2045 858693c6 bellard
           it when receiving a char */
2046 858693c6 bellard
        vm_stop(EXCP_INTERRUPT);
2047 5fafdf24 ths
    } else
2048 1fddef4b bellard
#endif
2049 41625033 bellard
    {
2050 858693c6 bellard
        switch(s->state) {
2051 858693c6 bellard
        case RS_IDLE:
2052 858693c6 bellard
            if (ch == '$') {
2053 858693c6 bellard
                s->line_buf_index = 0;
2054 858693c6 bellard
                s->state = RS_GETLINE;
2055 c33a346e bellard
            }
2056 b4608c04 bellard
            break;
2057 858693c6 bellard
        case RS_GETLINE:
2058 858693c6 bellard
            if (ch == '#') {
2059 858693c6 bellard
            s->state = RS_CHKSUM1;
2060 858693c6 bellard
            } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2061 858693c6 bellard
                s->state = RS_IDLE;
2062 4c3a88a2 bellard
            } else {
2063 858693c6 bellard
            s->line_buf[s->line_buf_index++] = ch;
2064 4c3a88a2 bellard
            }
2065 4c3a88a2 bellard
            break;
2066 858693c6 bellard
        case RS_CHKSUM1:
2067 858693c6 bellard
            s->line_buf[s->line_buf_index] = '\0';
2068 858693c6 bellard
            s->line_csum = fromhex(ch) << 4;
2069 858693c6 bellard
            s->state = RS_CHKSUM2;
2070 858693c6 bellard
            break;
2071 858693c6 bellard
        case RS_CHKSUM2:
2072 858693c6 bellard
            s->line_csum |= fromhex(ch);
2073 858693c6 bellard
            csum = 0;
2074 858693c6 bellard
            for(i = 0; i < s->line_buf_index; i++) {
2075 858693c6 bellard
                csum += s->line_buf[i];
2076 858693c6 bellard
            }
2077 858693c6 bellard
            if (s->line_csum != (csum & 0xff)) {
2078 60fe76f3 ths
                reply = '-';
2079 60fe76f3 ths
                put_buffer(s, &reply, 1);
2080 858693c6 bellard
                s->state = RS_IDLE;
2081 4c3a88a2 bellard
            } else {
2082 60fe76f3 ths
                reply = '+';
2083 60fe76f3 ths
                put_buffer(s, &reply, 1);
2084 880a7578 aliguori
                s->state = gdb_handle_packet(s, s->line_buf);
2085 4c3a88a2 bellard
            }
2086 4c3a88a2 bellard
            break;
2087 a2d1ebaf pbrook
        default:
2088 a2d1ebaf pbrook
            abort();
2089 858693c6 bellard
        }
2090 858693c6 bellard
    }
2091 858693c6 bellard
}
2092 858693c6 bellard
2093 1fddef4b bellard
#ifdef CONFIG_USER_ONLY
2094 1fddef4b bellard
int
2095 ca587a8e aurel32
gdb_queuesig (void)
2096 ca587a8e aurel32
{
2097 ca587a8e aurel32
    GDBState *s;
2098 ca587a8e aurel32
2099 ca587a8e aurel32
    s = gdbserver_state;
2100 ca587a8e aurel32
2101 ca587a8e aurel32
    if (gdbserver_fd < 0 || s->fd < 0)
2102 ca587a8e aurel32
        return 0;
2103 ca587a8e aurel32
    else
2104 ca587a8e aurel32
        return 1;
2105 ca587a8e aurel32
}
2106 ca587a8e aurel32
2107 ca587a8e aurel32
int
2108 1fddef4b bellard
gdb_handlesig (CPUState *env, int sig)
2109 1fddef4b bellard
{
2110 1fddef4b bellard
  GDBState *s;
2111 1fddef4b bellard
  char buf[256];
2112 1fddef4b bellard
  int n;
2113 1fddef4b bellard
2114 880a7578 aliguori
  s = gdbserver_state;
2115 1f487ee9 edgar_igl
  if (gdbserver_fd < 0 || s->fd < 0)
2116 1f487ee9 edgar_igl
    return sig;
2117 1fddef4b bellard
2118 1fddef4b bellard
  /* disable single step if it was enabled */
2119 1fddef4b bellard
  cpu_single_step(env, 0);
2120 1fddef4b bellard
  tb_flush(env);
2121 1fddef4b bellard
2122 1fddef4b bellard
  if (sig != 0)
2123 1fddef4b bellard
    {
2124 ca587a8e aurel32
      snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb (sig));
2125 1fddef4b bellard
      put_packet(s, buf);
2126 1fddef4b bellard
    }
2127 1f487ee9 edgar_igl
  /* put_packet() might have detected that the peer terminated the 
2128 1f487ee9 edgar_igl
     connection.  */
2129 1f487ee9 edgar_igl
  if (s->fd < 0)
2130 1f487ee9 edgar_igl
      return sig;
2131 1fddef4b bellard
2132 1fddef4b bellard
  sig = 0;
2133 1fddef4b bellard
  s->state = RS_IDLE;
2134 41625033 bellard
  s->running_state = 0;
2135 41625033 bellard
  while (s->running_state == 0) {
2136 1fddef4b bellard
      n = read (s->fd, buf, 256);
2137 1fddef4b bellard
      if (n > 0)
2138 1fddef4b bellard
        {
2139 1fddef4b bellard
          int i;
2140 1fddef4b bellard
2141 1fddef4b bellard
          for (i = 0; i < n; i++)
2142 6a00d601 bellard
            gdb_read_byte (s, buf[i]);
2143 1fddef4b bellard
        }
2144 1fddef4b bellard
      else if (n == 0 || errno != EAGAIN)
2145 1fddef4b bellard
        {
2146 1fddef4b bellard
          /* XXX: Connection closed.  Should probably wait for annother
2147 1fddef4b bellard
             connection before continuing.  */
2148 1fddef4b bellard
          return sig;
2149 1fddef4b bellard
        }
2150 41625033 bellard
  }
2151 1f487ee9 edgar_igl
  sig = s->signal;
2152 1f487ee9 edgar_igl
  s->signal = 0;
2153 1fddef4b bellard
  return sig;
2154 1fddef4b bellard
}
2155 e9009676 bellard
2156 e9009676 bellard
/* Tell the remote gdb that the process has exited.  */
2157 e9009676 bellard
void gdb_exit(CPUState *env, int code)
2158 e9009676 bellard
{
2159 e9009676 bellard
  GDBState *s;
2160 e9009676 bellard
  char buf[4];
2161 e9009676 bellard
2162 880a7578 aliguori
  s = gdbserver_state;
2163 1f487ee9 edgar_igl
  if (gdbserver_fd < 0 || s->fd < 0)
2164 1f487ee9 edgar_igl
    return;
2165 e9009676 bellard
2166 e9009676 bellard
  snprintf(buf, sizeof(buf), "W%02x", code);
2167 e9009676 bellard
  put_packet(s, buf);
2168 e9009676 bellard
}
2169 e9009676 bellard
2170 ca587a8e aurel32
/* Tell the remote gdb that the process has exited due to SIG.  */
2171 ca587a8e aurel32
void gdb_signalled(CPUState *env, int sig)
2172 ca587a8e aurel32
{
2173 ca587a8e aurel32
  GDBState *s;
2174 ca587a8e aurel32
  char buf[4];
2175 ca587a8e aurel32
2176 ca587a8e aurel32
  s = gdbserver_state;
2177 ca587a8e aurel32
  if (gdbserver_fd < 0 || s->fd < 0)
2178 ca587a8e aurel32
    return;
2179 ca587a8e aurel32
2180 ca587a8e aurel32
  snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb (sig));
2181 ca587a8e aurel32
  put_packet(s, buf);
2182 ca587a8e aurel32
}
2183 1fddef4b bellard
2184 880a7578 aliguori
static void gdb_accept(void)
2185 858693c6 bellard
{
2186 858693c6 bellard
    GDBState *s;
2187 858693c6 bellard
    struct sockaddr_in sockaddr;
2188 858693c6 bellard
    socklen_t len;
2189 858693c6 bellard
    int val, fd;
2190 858693c6 bellard
2191 858693c6 bellard
    for(;;) {
2192 858693c6 bellard
        len = sizeof(sockaddr);
2193 858693c6 bellard
        fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
2194 858693c6 bellard
        if (fd < 0 && errno != EINTR) {
2195 858693c6 bellard
            perror("accept");
2196 858693c6 bellard
            return;
2197 858693c6 bellard
        } else if (fd >= 0) {
2198 b4608c04 bellard
            break;
2199 b4608c04 bellard
        }
2200 b4608c04 bellard
    }
2201 858693c6 bellard
2202 858693c6 bellard
    /* set short latency */
2203 858693c6 bellard
    val = 1;
2204 8f447cc7 bellard
    setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
2205 3b46e624 ths
2206 880a7578 aliguori
    s = qemu_mallocz(sizeof(GDBState));
2207 880a7578 aliguori
    s->c_cpu = first_cpu;
2208 880a7578 aliguori
    s->g_cpu = first_cpu;
2209 858693c6 bellard
    s->fd = fd;
2210 56aebc89 pbrook
    gdb_has_xml = 0;
2211 858693c6 bellard
2212 880a7578 aliguori
    gdbserver_state = s;
2213 a2d1ebaf pbrook
2214 858693c6 bellard
    fcntl(fd, F_SETFL, O_NONBLOCK);
2215 858693c6 bellard
}
2216 858693c6 bellard
2217 858693c6 bellard
static int gdbserver_open(int port)
2218 858693c6 bellard
{
2219 858693c6 bellard
    struct sockaddr_in sockaddr;
2220 858693c6 bellard
    int fd, val, ret;
2221 858693c6 bellard
2222 858693c6 bellard
    fd = socket(PF_INET, SOCK_STREAM, 0);
2223 858693c6 bellard
    if (fd < 0) {
2224 858693c6 bellard
        perror("socket");
2225 858693c6 bellard
        return -1;
2226 858693c6 bellard
    }
2227 858693c6 bellard
2228 858693c6 bellard
    /* allow fast reuse */
2229 858693c6 bellard
    val = 1;
2230 8f447cc7 bellard
    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
2231 858693c6 bellard
2232 858693c6 bellard
    sockaddr.sin_family = AF_INET;
2233 858693c6 bellard
    sockaddr.sin_port = htons(port);
2234 858693c6 bellard
    sockaddr.sin_addr.s_addr = 0;
2235 858693c6 bellard
    ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
2236 858693c6 bellard
    if (ret < 0) {
2237 858693c6 bellard
        perror("bind");
2238 858693c6 bellard
        return -1;
2239 858693c6 bellard
    }
2240 858693c6 bellard
    ret = listen(fd, 0);
2241 858693c6 bellard
    if (ret < 0) {
2242 858693c6 bellard
        perror("listen");
2243 858693c6 bellard
        return -1;
2244 858693c6 bellard
    }
2245 858693c6 bellard
    return fd;
2246 858693c6 bellard
}
2247 858693c6 bellard
2248 858693c6 bellard
int gdbserver_start(int port)
2249 858693c6 bellard
{
2250 858693c6 bellard
    gdbserver_fd = gdbserver_open(port);
2251 858693c6 bellard
    if (gdbserver_fd < 0)
2252 858693c6 bellard
        return -1;
2253 858693c6 bellard
    /* accept connections */
2254 880a7578 aliguori
    gdb_accept();
2255 4046d913 pbrook
    return 0;
2256 4046d913 pbrook
}
2257 2b1319c8 aurel32
2258 2b1319c8 aurel32
/* Disable gdb stub for child processes.  */
2259 2b1319c8 aurel32
void gdbserver_fork(CPUState *env)
2260 2b1319c8 aurel32
{
2261 2b1319c8 aurel32
    GDBState *s = gdbserver_state;
2262 9f6164d6 edgar_igl
    if (gdbserver_fd < 0 || s->fd < 0)
2263 2b1319c8 aurel32
      return;
2264 2b1319c8 aurel32
    close(s->fd);
2265 2b1319c8 aurel32
    s->fd = -1;
2266 2b1319c8 aurel32
    cpu_breakpoint_remove_all(env, BP_GDB);
2267 2b1319c8 aurel32
    cpu_watchpoint_remove_all(env, BP_GDB);
2268 2b1319c8 aurel32
}
2269 1fddef4b bellard
#else
2270 aa1f17c1 ths
static int gdb_chr_can_receive(void *opaque)
2271 4046d913 pbrook
{
2272 56aebc89 pbrook
  /* We can handle an arbitrarily large amount of data.
2273 56aebc89 pbrook
   Pick the maximum packet size, which is as good as anything.  */
2274 56aebc89 pbrook
  return MAX_PACKET_LENGTH;
2275 4046d913 pbrook
}
2276 4046d913 pbrook
2277 aa1f17c1 ths
static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
2278 4046d913 pbrook
{
2279 4046d913 pbrook
    int i;
2280 4046d913 pbrook
2281 4046d913 pbrook
    for (i = 0; i < size; i++) {
2282 880a7578 aliguori
        gdb_read_byte(gdbserver_state, buf[i]);
2283 4046d913 pbrook
    }
2284 4046d913 pbrook
}
2285 4046d913 pbrook
2286 4046d913 pbrook
static void gdb_chr_event(void *opaque, int event)
2287 4046d913 pbrook
{
2288 4046d913 pbrook
    switch (event) {
2289 4046d913 pbrook
    case CHR_EVENT_RESET:
2290 4046d913 pbrook
        vm_stop(EXCP_INTERRUPT);
2291 56aebc89 pbrook
        gdb_has_xml = 0;
2292 4046d913 pbrook
        break;
2293 4046d913 pbrook
    default:
2294 4046d913 pbrook
        break;
2295 4046d913 pbrook
    }
2296 4046d913 pbrook
}
2297 4046d913 pbrook
2298 8a34a0fb aliguori
static void gdb_monitor_output(GDBState *s, const char *msg, int len)
2299 8a34a0fb aliguori
{
2300 8a34a0fb aliguori
    char buf[MAX_PACKET_LENGTH];
2301 8a34a0fb aliguori
2302 8a34a0fb aliguori
    buf[0] = 'O';
2303 8a34a0fb aliguori
    if (len > (MAX_PACKET_LENGTH/2) - 1)
2304 8a34a0fb aliguori
        len = (MAX_PACKET_LENGTH/2) - 1;
2305 8a34a0fb aliguori
    memtohex(buf + 1, (uint8_t *)msg, len);
2306 8a34a0fb aliguori
    put_packet(s, buf);
2307 8a34a0fb aliguori
}
2308 8a34a0fb aliguori
2309 8a34a0fb aliguori
static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
2310 8a34a0fb aliguori
{
2311 8a34a0fb aliguori
    const char *p = (const char *)buf;
2312 8a34a0fb aliguori
    int max_sz;
2313 8a34a0fb aliguori
2314 8a34a0fb aliguori
    max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
2315 8a34a0fb aliguori
    for (;;) {
2316 8a34a0fb aliguori
        if (len <= max_sz) {
2317 8a34a0fb aliguori
            gdb_monitor_output(gdbserver_state, p, len);
2318 8a34a0fb aliguori
            break;
2319 8a34a0fb aliguori
        }
2320 8a34a0fb aliguori
        gdb_monitor_output(gdbserver_state, p, max_sz);
2321 8a34a0fb aliguori
        p += max_sz;
2322 8a34a0fb aliguori
        len -= max_sz;
2323 8a34a0fb aliguori
    }
2324 8a34a0fb aliguori
    return len;
2325 8a34a0fb aliguori
}
2326 8a34a0fb aliguori
2327 59030a8c aliguori
#ifndef _WIN32
2328 59030a8c aliguori
static void gdb_sigterm_handler(int signal)
2329 59030a8c aliguori
{
2330 59030a8c aliguori
    if (vm_running)
2331 59030a8c aliguori
        vm_stop(EXCP_INTERRUPT);
2332 59030a8c aliguori
}
2333 59030a8c aliguori
#endif
2334 59030a8c aliguori
2335 59030a8c aliguori
int gdbserver_start(const char *device)
2336 4046d913 pbrook
{
2337 4046d913 pbrook
    GDBState *s;
2338 59030a8c aliguori
    char gdbstub_device_name[128];
2339 36556b20 aliguori
    CharDriverState *chr = NULL;
2340 36556b20 aliguori
    CharDriverState *mon_chr;
2341 cfc3475a pbrook
2342 59030a8c aliguori
    if (!device)
2343 59030a8c aliguori
        return -1;
2344 59030a8c aliguori
    if (strcmp(device, "none") != 0) {
2345 59030a8c aliguori
        if (strstart(device, "tcp:", NULL)) {
2346 59030a8c aliguori
            /* enforce required TCP attributes */
2347 59030a8c aliguori
            snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
2348 59030a8c aliguori
                     "%s,nowait,nodelay,server", device);
2349 59030a8c aliguori
            device = gdbstub_device_name;
2350 36556b20 aliguori
        }
2351 59030a8c aliguori
#ifndef _WIN32
2352 59030a8c aliguori
        else if (strcmp(device, "stdio") == 0) {
2353 59030a8c aliguori
            struct sigaction act;
2354 4046d913 pbrook
2355 59030a8c aliguori
            memset(&act, 0, sizeof(act));
2356 59030a8c aliguori
            act.sa_handler = gdb_sigterm_handler;
2357 59030a8c aliguori
            sigaction(SIGINT, &act, NULL);
2358 59030a8c aliguori
        }
2359 59030a8c aliguori
#endif
2360 59030a8c aliguori
        chr = qemu_chr_open("gdb", device, NULL);
2361 36556b20 aliguori
        if (!chr)
2362 36556b20 aliguori
            return -1;
2363 36556b20 aliguori
2364 36556b20 aliguori
        qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
2365 36556b20 aliguori
                              gdb_chr_event, NULL);
2366 cfc3475a pbrook
    }
2367 cfc3475a pbrook
2368 36556b20 aliguori
    s = gdbserver_state;
2369 36556b20 aliguori
    if (!s) {
2370 36556b20 aliguori
        s = qemu_mallocz(sizeof(GDBState));
2371 36556b20 aliguori
        gdbserver_state = s;
2372 4046d913 pbrook
2373 36556b20 aliguori
        qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
2374 36556b20 aliguori
2375 36556b20 aliguori
        /* Initialize a monitor terminal for gdb */
2376 36556b20 aliguori
        mon_chr = qemu_mallocz(sizeof(*mon_chr));
2377 36556b20 aliguori
        mon_chr->chr_write = gdb_monitor_write;
2378 36556b20 aliguori
        monitor_init(mon_chr, 0);
2379 36556b20 aliguori
    } else {
2380 36556b20 aliguori
        if (s->chr)
2381 36556b20 aliguori
            qemu_chr_close(s->chr);
2382 36556b20 aliguori
        mon_chr = s->mon_chr;
2383 36556b20 aliguori
        memset(s, 0, sizeof(GDBState));
2384 36556b20 aliguori
    }
2385 880a7578 aliguori
    s->c_cpu = first_cpu;
2386 880a7578 aliguori
    s->g_cpu = first_cpu;
2387 4046d913 pbrook
    s->chr = chr;
2388 36556b20 aliguori
    s->state = chr ? RS_IDLE : RS_INACTIVE;
2389 36556b20 aliguori
    s->mon_chr = mon_chr;
2390 8a34a0fb aliguori
2391 b4608c04 bellard
    return 0;
2392 b4608c04 bellard
}
2393 4046d913 pbrook
#endif