Statistics
| Branch: | Revision:

root / gdbstub.c @ c1b0b93b

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