Statistics
| Branch: | Revision:

root / gdbstub.c @ 8098ed41

History | View | Annotate | Download (52.8 kB)

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