Statistics
| Branch: | Revision:

root / gdbstub.c @ 142ab5bb

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