Statistics
| Branch: | Revision:

root / gdbstub.c @ 0497d2f4

History | View | Annotate | Download (59.4 kB)

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