blob: 24297bad0e0b737c7c7e7db0f77cc09d522cd6ca [file] [log] [blame]
bellardb4608c02003-06-27 17:34:32 +00001/*
2 * gdb server stub
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard34751872005-07-02 14:31:34 +00004 * Copyright (c) 2003-2005 Fabrice Bellard
bellardb4608c02003-06-27 17:34:32 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
aurel32fad6cb12009-01-04 22:05:52 +000018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
bellardb4608c02003-06-27 17:34:32 +000019 */
pbrook978efd62006-06-17 18:30:42 +000020#include "config.h"
pbrook56aebc82008-10-11 17:55:29 +000021#include "qemu-common.h"
bellard1fddef42005-04-17 19:16:13 +000022#ifdef CONFIG_USER_ONLY
23#include <stdlib.h>
24#include <stdio.h>
25#include <stdarg.h>
26#include <string.h>
27#include <errno.h>
28#include <unistd.h>
pbrook978efd62006-06-17 18:30:42 +000029#include <fcntl.h>
bellard1fddef42005-04-17 19:16:13 +000030
31#include "qemu.h"
32#else
aliguori8a34a0f2009-03-05 23:01:55 +000033#include "monitor.h"
pbrook87ecb682007-11-17 17:14:51 +000034#include "qemu-char.h"
35#include "sysemu.h"
36#include "gdbstub.h"
bellard1fddef42005-04-17 19:16:13 +000037#endif
bellard67b915a2004-03-31 23:37:16 +000038
pbrook56aebc82008-10-11 17:55:29 +000039#define MAX_PACKET_LENGTH 4096
40
bellard8f447cc2006-06-14 15:21:14 +000041#include "qemu_socket.h"
aliguorie22a25c2009-03-12 20:12:48 +000042#include "kvm.h"
aurel32ca587a82008-12-18 22:44:13 +000043
44
45enum {
46 GDB_SIGNAL_0 = 0,
47 GDB_SIGNAL_INT = 2,
48 GDB_SIGNAL_TRAP = 5,
49 GDB_SIGNAL_UNKNOWN = 143
50};
51
52#ifdef CONFIG_USER_ONLY
53
54/* Map target signal numbers to GDB protocol signal numbers and vice
55 * versa. For user emulation's currently supported systems, we can
56 * assume most signals are defined.
57 */
58
59static int gdb_signal_table[] = {
60 0,
61 TARGET_SIGHUP,
62 TARGET_SIGINT,
63 TARGET_SIGQUIT,
64 TARGET_SIGILL,
65 TARGET_SIGTRAP,
66 TARGET_SIGABRT,
67 -1, /* SIGEMT */
68 TARGET_SIGFPE,
69 TARGET_SIGKILL,
70 TARGET_SIGBUS,
71 TARGET_SIGSEGV,
72 TARGET_SIGSYS,
73 TARGET_SIGPIPE,
74 TARGET_SIGALRM,
75 TARGET_SIGTERM,
76 TARGET_SIGURG,
77 TARGET_SIGSTOP,
78 TARGET_SIGTSTP,
79 TARGET_SIGCONT,
80 TARGET_SIGCHLD,
81 TARGET_SIGTTIN,
82 TARGET_SIGTTOU,
83 TARGET_SIGIO,
84 TARGET_SIGXCPU,
85 TARGET_SIGXFSZ,
86 TARGET_SIGVTALRM,
87 TARGET_SIGPROF,
88 TARGET_SIGWINCH,
89 -1, /* SIGLOST */
90 TARGET_SIGUSR1,
91 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +000092#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +000093 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +000094#else
95 -1,
96#endif
aurel32ca587a82008-12-18 22:44:13 +000097 -1, /* SIGPOLL */
98 -1,
99 -1,
100 -1,
101 -1,
102 -1,
103 -1,
104 -1,
105 -1,
106 -1,
107 -1,
108 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000109#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000110 __SIGRTMIN + 1,
111 __SIGRTMIN + 2,
112 __SIGRTMIN + 3,
113 __SIGRTMIN + 4,
114 __SIGRTMIN + 5,
115 __SIGRTMIN + 6,
116 __SIGRTMIN + 7,
117 __SIGRTMIN + 8,
118 __SIGRTMIN + 9,
119 __SIGRTMIN + 10,
120 __SIGRTMIN + 11,
121 __SIGRTMIN + 12,
122 __SIGRTMIN + 13,
123 __SIGRTMIN + 14,
124 __SIGRTMIN + 15,
125 __SIGRTMIN + 16,
126 __SIGRTMIN + 17,
127 __SIGRTMIN + 18,
128 __SIGRTMIN + 19,
129 __SIGRTMIN + 20,
130 __SIGRTMIN + 21,
131 __SIGRTMIN + 22,
132 __SIGRTMIN + 23,
133 __SIGRTMIN + 24,
134 __SIGRTMIN + 25,
135 __SIGRTMIN + 26,
136 __SIGRTMIN + 27,
137 __SIGRTMIN + 28,
138 __SIGRTMIN + 29,
139 __SIGRTMIN + 30,
140 __SIGRTMIN + 31,
141 -1, /* SIGCANCEL */
142 __SIGRTMIN,
143 __SIGRTMIN + 32,
144 __SIGRTMIN + 33,
145 __SIGRTMIN + 34,
146 __SIGRTMIN + 35,
147 __SIGRTMIN + 36,
148 __SIGRTMIN + 37,
149 __SIGRTMIN + 38,
150 __SIGRTMIN + 39,
151 __SIGRTMIN + 40,
152 __SIGRTMIN + 41,
153 __SIGRTMIN + 42,
154 __SIGRTMIN + 43,
155 __SIGRTMIN + 44,
156 __SIGRTMIN + 45,
157 __SIGRTMIN + 46,
158 __SIGRTMIN + 47,
159 __SIGRTMIN + 48,
160 __SIGRTMIN + 49,
161 __SIGRTMIN + 50,
162 __SIGRTMIN + 51,
163 __SIGRTMIN + 52,
164 __SIGRTMIN + 53,
165 __SIGRTMIN + 54,
166 __SIGRTMIN + 55,
167 __SIGRTMIN + 56,
168 __SIGRTMIN + 57,
169 __SIGRTMIN + 58,
170 __SIGRTMIN + 59,
171 __SIGRTMIN + 60,
172 __SIGRTMIN + 61,
173 __SIGRTMIN + 62,
174 __SIGRTMIN + 63,
175 __SIGRTMIN + 64,
176 __SIGRTMIN + 65,
177 __SIGRTMIN + 66,
178 __SIGRTMIN + 67,
179 __SIGRTMIN + 68,
180 __SIGRTMIN + 69,
181 __SIGRTMIN + 70,
182 __SIGRTMIN + 71,
183 __SIGRTMIN + 72,
184 __SIGRTMIN + 73,
185 __SIGRTMIN + 74,
186 __SIGRTMIN + 75,
187 __SIGRTMIN + 76,
188 __SIGRTMIN + 77,
189 __SIGRTMIN + 78,
190 __SIGRTMIN + 79,
191 __SIGRTMIN + 80,
192 __SIGRTMIN + 81,
193 __SIGRTMIN + 82,
194 __SIGRTMIN + 83,
195 __SIGRTMIN + 84,
196 __SIGRTMIN + 85,
197 __SIGRTMIN + 86,
198 __SIGRTMIN + 87,
199 __SIGRTMIN + 88,
200 __SIGRTMIN + 89,
201 __SIGRTMIN + 90,
202 __SIGRTMIN + 91,
203 __SIGRTMIN + 92,
204 __SIGRTMIN + 93,
205 __SIGRTMIN + 94,
206 __SIGRTMIN + 95,
207 -1, /* SIGINFO */
208 -1, /* UNKNOWN */
209 -1, /* DEFAULT */
210 -1,
211 -1,
212 -1,
213 -1,
214 -1,
215 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000216#endif
aurel32ca587a82008-12-18 22:44:13 +0000217};
bellard8f447cc2006-06-14 15:21:14 +0000218#else
aurel32ca587a82008-12-18 22:44:13 +0000219/* In system mode we only need SIGINT and SIGTRAP; other signals
220 are not yet supported. */
221
222enum {
223 TARGET_SIGINT = 2,
224 TARGET_SIGTRAP = 5
225};
226
227static int gdb_signal_table[] = {
228 -1,
229 -1,
230 TARGET_SIGINT,
231 -1,
232 -1,
233 TARGET_SIGTRAP
234};
bellard8f447cc2006-06-14 15:21:14 +0000235#endif
bellardb4608c02003-06-27 17:34:32 +0000236
aurel32ca587a82008-12-18 22:44:13 +0000237#ifdef CONFIG_USER_ONLY
238static int target_signal_to_gdb (int sig)
239{
240 int i;
241 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
242 if (gdb_signal_table[i] == sig)
243 return i;
244 return GDB_SIGNAL_UNKNOWN;
245}
246#endif
247
248static int gdb_signal_to_target (int sig)
249{
250 if (sig < ARRAY_SIZE (gdb_signal_table))
251 return gdb_signal_table[sig];
252 else
253 return -1;
254}
255
bellard4abe6152003-07-26 18:01:58 +0000256//#define DEBUG_GDB
bellardb4608c02003-06-27 17:34:32 +0000257
pbrook56aebc82008-10-11 17:55:29 +0000258typedef struct GDBRegisterState {
259 int base_reg;
260 int num_regs;
261 gdb_reg_cb get_reg;
262 gdb_reg_cb set_reg;
263 const char *xml;
264 struct GDBRegisterState *next;
265} GDBRegisterState;
266
bellard858693c2004-03-31 18:52:07 +0000267enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000268 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000269 RS_IDLE,
270 RS_GETLINE,
271 RS_CHKSUM1,
272 RS_CHKSUM2,
pbrooka2d1eba2007-01-28 03:10:55 +0000273 RS_SYSCALL,
bellard858693c2004-03-31 18:52:07 +0000274};
bellard858693c2004-03-31 18:52:07 +0000275typedef struct GDBState {
aliguori880a7572008-11-18 20:30:24 +0000276 CPUState *c_cpu; /* current CPU for step/continue ops */
277 CPUState *g_cpu; /* current CPU for other ops */
278 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000279 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000280 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000281 int line_buf_index;
282 int line_csum;
pbrook56aebc82008-10-11 17:55:29 +0000283 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000284 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000285 int signal;
bellard41625032005-04-24 10:07:11 +0000286#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000287 int fd;
bellard41625032005-04-24 10:07:11 +0000288 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000289#else
290 CharDriverState *chr;
aliguori8a34a0f2009-03-05 23:01:55 +0000291 CharDriverState *mon_chr;
bellard41625032005-04-24 10:07:11 +0000292#endif
bellard858693c2004-03-31 18:52:07 +0000293} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000294
edgar_igl60897d32008-05-09 08:25:14 +0000295/* By default use no IRQs and no timers while single stepping so as to
296 * make single stepping like an ICE HW step.
297 */
298static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
299
aliguori880a7572008-11-18 20:30:24 +0000300static GDBState *gdbserver_state;
301
pbrook56aebc82008-10-11 17:55:29 +0000302/* This is an ugly hack to cope with both new and old gdb.
303 If gdb sends qXfer:features:read then assume we're talking to a newish
304 gdb that understands target descriptions. */
305static int gdb_has_xml;
306
bellard1fddef42005-04-17 19:16:13 +0000307#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000308/* XXX: This is not thread safe. Do we care? */
309static int gdbserver_fd = -1;
310
bellard858693c2004-03-31 18:52:07 +0000311static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000312{
313 uint8_t ch;
314 int ret;
315
316 for(;;) {
bellard8f447cc2006-06-14 15:21:14 +0000317 ret = recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000318 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000319 if (errno == ECONNRESET)
320 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000321 if (errno != EINTR && errno != EAGAIN)
322 return -1;
323 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000324 close(s->fd);
325 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000326 return -1;
327 } else {
328 break;
329 }
330 }
331 return ch;
332}
pbrook4046d912007-01-28 01:53:16 +0000333#endif
bellardb4608c02003-06-27 17:34:32 +0000334
pbrooka2d1eba2007-01-28 03:10:55 +0000335static gdb_syscall_complete_cb gdb_current_syscall_cb;
336
blueswir1654efcf2009-04-18 07:29:59 +0000337static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000338 GDB_SYS_UNKNOWN,
339 GDB_SYS_ENABLED,
340 GDB_SYS_DISABLED,
341} gdb_syscall_mode;
342
343/* If gdb is connected when the first semihosting syscall occurs then use
344 remote gdb syscalls. Otherwise use native file IO. */
345int use_gdb_syscalls(void)
346{
347 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000348 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
349 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000350 }
351 return gdb_syscall_mode == GDB_SYS_ENABLED;
352}
353
edgar_iglba70a622008-03-14 06:10:42 +0000354/* Resume execution. */
355static inline void gdb_continue(GDBState *s)
356{
357#ifdef CONFIG_USER_ONLY
358 s->running_state = 1;
359#else
360 vm_start();
361#endif
362}
363
bellard858693c2004-03-31 18:52:07 +0000364static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000365{
pbrook4046d912007-01-28 01:53:16 +0000366#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000367 int ret;
368
369 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000370 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000371 if (ret < 0) {
372 if (errno != EINTR && errno != EAGAIN)
373 return;
374 } else {
375 buf += ret;
376 len -= ret;
377 }
378 }
pbrook4046d912007-01-28 01:53:16 +0000379#else
380 qemu_chr_write(s->chr, buf, len);
381#endif
bellardb4608c02003-06-27 17:34:32 +0000382}
383
384static inline int fromhex(int v)
385{
386 if (v >= '0' && v <= '9')
387 return v - '0';
388 else if (v >= 'A' && v <= 'F')
389 return v - 'A' + 10;
390 else if (v >= 'a' && v <= 'f')
391 return v - 'a' + 10;
392 else
393 return 0;
394}
395
396static inline int tohex(int v)
397{
398 if (v < 10)
399 return v + '0';
400 else
401 return v - 10 + 'a';
402}
403
404static void memtohex(char *buf, const uint8_t *mem, int len)
405{
406 int i, c;
407 char *q;
408 q = buf;
409 for(i = 0; i < len; i++) {
410 c = mem[i];
411 *q++ = tohex(c >> 4);
412 *q++ = tohex(c & 0xf);
413 }
414 *q = '\0';
415}
416
417static void hextomem(uint8_t *mem, const char *buf, int len)
418{
419 int i;
420
421 for(i = 0; i < len; i++) {
422 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
423 buf += 2;
424 }
425}
426
bellardb4608c02003-06-27 17:34:32 +0000427/* return -1 if error, 0 if OK */
pbrook56aebc82008-10-11 17:55:29 +0000428static int put_packet_binary(GDBState *s, const char *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000429{
pbrook56aebc82008-10-11 17:55:29 +0000430 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000431 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000432
bellardb4608c02003-06-27 17:34:32 +0000433 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000434 p = s->last_packet;
435 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000436 memcpy(p, buf, len);
437 p += len;
bellardb4608c02003-06-27 17:34:32 +0000438 csum = 0;
439 for(i = 0; i < len; i++) {
440 csum += buf[i];
441 }
pbrook4046d912007-01-28 01:53:16 +0000442 *(p++) = '#';
443 *(p++) = tohex((csum >> 4) & 0xf);
444 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000445
pbrook4046d912007-01-28 01:53:16 +0000446 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000447 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000448
pbrook4046d912007-01-28 01:53:16 +0000449#ifdef CONFIG_USER_ONLY
450 i = get_char(s);
451 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000452 return -1;
pbrook4046d912007-01-28 01:53:16 +0000453 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000454 break;
pbrook4046d912007-01-28 01:53:16 +0000455#else
456 break;
457#endif
bellardb4608c02003-06-27 17:34:32 +0000458 }
459 return 0;
460}
461
pbrook56aebc82008-10-11 17:55:29 +0000462/* return -1 if error, 0 if OK */
463static int put_packet(GDBState *s, const char *buf)
464{
465#ifdef DEBUG_GDB
466 printf("reply='%s'\n", buf);
467#endif
468
469 return put_packet_binary(s, buf, strlen(buf));
470}
471
472/* The GDB remote protocol transfers values in target byte order. This means
473 we can use the raw memory access routines to access the value buffer.
474 Conveniently, these also handle the case where the buffer is mis-aligned.
475 */
476#define GET_REG8(val) do { \
477 stb_p(mem_buf, val); \
478 return 1; \
479 } while(0)
480#define GET_REG16(val) do { \
481 stw_p(mem_buf, val); \
482 return 2; \
483 } while(0)
484#define GET_REG32(val) do { \
485 stl_p(mem_buf, val); \
486 return 4; \
487 } while(0)
488#define GET_REG64(val) do { \
489 stq_p(mem_buf, val); \
490 return 8; \
491 } while(0)
492
493#if TARGET_LONG_BITS == 64
494#define GET_REGL(val) GET_REG64(val)
495#define ldtul_p(addr) ldq_p(addr)
496#else
497#define GET_REGL(val) GET_REG32(val)
498#define ldtul_p(addr) ldl_p(addr)
499#endif
500
edgar_iglfde3fd62008-05-09 08:50:01 +0000501#if defined(TARGET_I386)
balrog5ad265e2007-10-31 00:21:35 +0000502
503#ifdef TARGET_X86_64
pbrook56aebc82008-10-11 17:55:29 +0000504static const int gpr_map[16] = {
bellard79808572008-05-09 14:40:22 +0000505 R_EAX, R_EBX, R_ECX, R_EDX, R_ESI, R_EDI, R_EBP, R_ESP,
pbrook56aebc82008-10-11 17:55:29 +0000506 8, 9, 10, 11, 12, 13, 14, 15
bellard79808572008-05-09 14:40:22 +0000507};
bellard79808572008-05-09 14:40:22 +0000508#else
pbrook56aebc82008-10-11 17:55:29 +0000509static const int gpr_map[8] = {0, 1, 2, 3, 4, 5, 6, 7};
bellard79808572008-05-09 14:40:22 +0000510#endif
pbrook56aebc82008-10-11 17:55:29 +0000511
512#define NUM_CORE_REGS (CPU_NB_REGS * 2 + 25)
513
Jan Kiszkab1631e72009-06-27 09:53:51 +0200514#define IDX_IP_REG CPU_NB_REGS
515#define IDX_FLAGS_REG (IDX_IP_REG + 1)
516#define IDX_SEG_REGS (IDX_FLAGS_REG + 1)
517#define IDX_FP_REGS (IDX_SEG_REGS + 6)
518#define IDX_XMM_REGS (IDX_FP_REGS + 16)
519#define IDX_MXCSR_REG (IDX_XMM_REGS + CPU_NB_REGS)
520
pbrook56aebc82008-10-11 17:55:29 +0000521static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
522{
523 if (n < CPU_NB_REGS) {
524 GET_REGL(env->regs[gpr_map[n]]);
Jan Kiszkab1631e72009-06-27 09:53:51 +0200525 } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
pbrook56aebc82008-10-11 17:55:29 +0000526#ifdef USE_X86LDOUBLE
Jan Kiszkab1631e72009-06-27 09:53:51 +0200527 /* FIXME: byteswap float values - after fixing fpregs layout. */
528 memcpy(mem_buf, &env->fpregs[n - IDX_FP_REGS], 10);
pbrook56aebc82008-10-11 17:55:29 +0000529#else
530 memset(mem_buf, 0, 10);
531#endif
532 return 10;
Jan Kiszkab1631e72009-06-27 09:53:51 +0200533 } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
534 n -= IDX_XMM_REGS;
535 stq_p(mem_buf, env->xmm_regs[n].XMM_Q(0));
536 stq_p(mem_buf + 8, env->xmm_regs[n].XMM_Q(1));
537 return 16;
pbrook56aebc82008-10-11 17:55:29 +0000538 } else {
pbrook56aebc82008-10-11 17:55:29 +0000539 switch (n) {
Jan Kiszkab1631e72009-06-27 09:53:51 +0200540 case IDX_IP_REG: GET_REGL(env->eip);
541 case IDX_FLAGS_REG: GET_REG32(env->eflags);
542
543 case IDX_SEG_REGS: GET_REG32(env->segs[R_CS].selector);
544 case IDX_SEG_REGS + 1: GET_REG32(env->segs[R_SS].selector);
545 case IDX_SEG_REGS + 2: GET_REG32(env->segs[R_DS].selector);
546 case IDX_SEG_REGS + 3: GET_REG32(env->segs[R_ES].selector);
547 case IDX_SEG_REGS + 4: GET_REG32(env->segs[R_FS].selector);
548 case IDX_SEG_REGS + 5: GET_REG32(env->segs[R_GS].selector);
549
550 case IDX_FP_REGS + 8: GET_REG32(env->fpuc);
551 case IDX_FP_REGS + 9: GET_REG32((env->fpus & ~0x3800) |
552 (env->fpstt & 0x7) << 11);
553 case IDX_FP_REGS + 10: GET_REG32(0); /* ftag */
554 case IDX_FP_REGS + 11: GET_REG32(0); /* fiseg */
555 case IDX_FP_REGS + 12: GET_REG32(0); /* fioff */
556 case IDX_FP_REGS + 13: GET_REG32(0); /* foseg */
557 case IDX_FP_REGS + 14: GET_REG32(0); /* fooff */
558 case IDX_FP_REGS + 15: GET_REG32(0); /* fop */
559
560 case IDX_MXCSR_REG: GET_REG32(env->mxcsr);
pbrook56aebc82008-10-11 17:55:29 +0000561 }
bellard79808572008-05-09 14:40:22 +0000562 }
pbrook56aebc82008-10-11 17:55:29 +0000563 return 0;
bellard79808572008-05-09 14:40:22 +0000564}
565
Jan Kiszka84273172009-06-27 09:53:51 +0200566static int cpu_x86_gdb_load_seg(CPUState *env, int sreg, uint8_t *mem_buf)
567{
568 uint16_t selector = ldl_p(mem_buf);
569
570 if (selector != env->segs[sreg].selector) {
571#if defined(CONFIG_USER_ONLY)
572 cpu_x86_load_seg(env, sreg, selector);
573#else
574 unsigned int limit, flags;
575 target_ulong base;
576
577 if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
578 base = selector << 4;
579 limit = 0xffff;
580 flags = 0;
581 } else {
582 if (!cpu_x86_get_descr_debug(env, selector, &base, &limit, &flags))
583 return 4;
584 }
585 cpu_x86_load_seg_cache(env, sreg, selector, base, limit, flags);
586#endif
587 }
588 return 4;
589}
590
Jan Kiszkab1631e72009-06-27 09:53:51 +0200591static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellard79808572008-05-09 14:40:22 +0000592{
pbrook56aebc82008-10-11 17:55:29 +0000593 uint32_t tmp;
594
Jan Kiszkab1631e72009-06-27 09:53:51 +0200595 if (n < CPU_NB_REGS) {
596 env->regs[gpr_map[n]] = ldtul_p(mem_buf);
pbrook56aebc82008-10-11 17:55:29 +0000597 return sizeof(target_ulong);
Jan Kiszkab1631e72009-06-27 09:53:51 +0200598 } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
pbrook56aebc82008-10-11 17:55:29 +0000599#ifdef USE_X86LDOUBLE
Jan Kiszkab1631e72009-06-27 09:53:51 +0200600 /* FIXME: byteswap float values - after fixing fpregs layout. */
601 memcpy(&env->fpregs[n - IDX_FP_REGS], mem_buf, 10);
pbrook56aebc82008-10-11 17:55:29 +0000602#endif
603 return 10;
Jan Kiszkab1631e72009-06-27 09:53:51 +0200604 } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
605 n -= IDX_XMM_REGS;
606 env->xmm_regs[n].XMM_Q(0) = ldq_p(mem_buf);
607 env->xmm_regs[n].XMM_Q(1) = ldq_p(mem_buf + 8);
608 return 16;
pbrook56aebc82008-10-11 17:55:29 +0000609 } else {
Jan Kiszkab1631e72009-06-27 09:53:51 +0200610 switch (n) {
611 case IDX_IP_REG:
612 env->eip = ldtul_p(mem_buf);
613 return sizeof(target_ulong);
614 case IDX_FLAGS_REG:
615 env->eflags = ldl_p(mem_buf);
616 return 4;
617
Jan Kiszka84273172009-06-27 09:53:51 +0200618 case IDX_SEG_REGS: return cpu_x86_gdb_load_seg(env, R_CS, mem_buf);
619 case IDX_SEG_REGS + 1: return cpu_x86_gdb_load_seg(env, R_SS, mem_buf);
620 case IDX_SEG_REGS + 2: return cpu_x86_gdb_load_seg(env, R_DS, mem_buf);
621 case IDX_SEG_REGS + 3: return cpu_x86_gdb_load_seg(env, R_ES, mem_buf);
622 case IDX_SEG_REGS + 4: return cpu_x86_gdb_load_seg(env, R_FS, mem_buf);
623 case IDX_SEG_REGS + 5: return cpu_x86_gdb_load_seg(env, R_GS, mem_buf);
Jan Kiszkab1631e72009-06-27 09:53:51 +0200624
625 case IDX_FP_REGS + 8:
626 env->fpuc = ldl_p(mem_buf);
627 return 4;
628 case IDX_FP_REGS + 9:
629 tmp = ldl_p(mem_buf);
630 env->fpstt = (tmp >> 11) & 7;
631 env->fpus = tmp & ~0x3800;
632 return 4;
633 case IDX_FP_REGS + 10: /* ftag */ return 4;
634 case IDX_FP_REGS + 11: /* fiseg */ return 4;
635 case IDX_FP_REGS + 12: /* fioff */ return 4;
636 case IDX_FP_REGS + 13: /* foseg */ return 4;
637 case IDX_FP_REGS + 14: /* fooff */ return 4;
638 case IDX_FP_REGS + 15: /* fop */ return 4;
639
640 case IDX_MXCSR_REG:
641 env->mxcsr = ldl_p(mem_buf);
642 return 4;
bellard79808572008-05-09 14:40:22 +0000643 }
bellard79808572008-05-09 14:40:22 +0000644 }
pbrook56aebc82008-10-11 17:55:29 +0000645 /* Unrecognised register. */
646 return 0;
bellard6da41ea2004-01-04 15:48:38 +0000647}
648
bellard9e62fd72004-01-05 22:49:06 +0000649#elif defined (TARGET_PPC)
pbrook56aebc82008-10-11 17:55:29 +0000650
aurel32e571cb42009-01-24 15:07:42 +0000651/* Old gdb always expects FP registers. Newer (xml-aware) gdb only
652 expects whatever the target description contains. Due to a
653 historical mishap the FP registers appear in between core integer
654 regs and PC, MSR, CR, and so forth. We hack round this by giving the
655 FP regs zero size when talking to a newer gdb. */
pbrook56aebc82008-10-11 17:55:29 +0000656#define NUM_CORE_REGS 71
aurel32e571cb42009-01-24 15:07:42 +0000657#if defined (TARGET_PPC64)
658#define GDB_CORE_XML "power64-core.xml"
659#else
660#define GDB_CORE_XML "power-core.xml"
661#endif
pbrook56aebc82008-10-11 17:55:29 +0000662
663static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
bellard9e62fd72004-01-05 22:49:06 +0000664{
pbrook56aebc82008-10-11 17:55:29 +0000665 if (n < 32) {
666 /* gprs */
667 GET_REGL(env->gpr[n]);
668 } else if (n < 64) {
669 /* fprs */
aurel32e571cb42009-01-24 15:07:42 +0000670 if (gdb_has_xml)
671 return 0;
aurel328d4acf92008-11-30 16:23:18 +0000672 stfq_p(mem_buf, env->fpr[n-32]);
pbrook56aebc82008-10-11 17:55:29 +0000673 return 8;
674 } else {
675 switch (n) {
676 case 64: GET_REGL(env->nip);
677 case 65: GET_REGL(env->msr);
678 case 66:
679 {
680 uint32_t cr = 0;
681 int i;
682 for (i = 0; i < 8; i++)
683 cr |= env->crf[i] << (32 - ((i + 1) * 4));
684 GET_REG32(cr);
685 }
686 case 67: GET_REGL(env->lr);
687 case 68: GET_REGL(env->ctr);
aurel323d7b4172008-10-21 11:28:46 +0000688 case 69: GET_REGL(env->xer);
aurel32e571cb42009-01-24 15:07:42 +0000689 case 70:
690 {
691 if (gdb_has_xml)
692 return 0;
693 GET_REG32(0); /* fpscr */
694 }
pbrook56aebc82008-10-11 17:55:29 +0000695 }
bellard9e62fd72004-01-05 22:49:06 +0000696 }
pbrook56aebc82008-10-11 17:55:29 +0000697 return 0;
bellard9e62fd72004-01-05 22:49:06 +0000698}
699
pbrook56aebc82008-10-11 17:55:29 +0000700static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellard9e62fd72004-01-05 22:49:06 +0000701{
pbrook56aebc82008-10-11 17:55:29 +0000702 if (n < 32) {
703 /* gprs */
704 env->gpr[n] = ldtul_p(mem_buf);
705 return sizeof(target_ulong);
706 } else if (n < 64) {
707 /* fprs */
aurel32e571cb42009-01-24 15:07:42 +0000708 if (gdb_has_xml)
709 return 0;
aurel328d4acf92008-11-30 16:23:18 +0000710 env->fpr[n-32] = ldfq_p(mem_buf);
pbrook56aebc82008-10-11 17:55:29 +0000711 return 8;
712 } else {
713 switch (n) {
714 case 64:
715 env->nip = ldtul_p(mem_buf);
716 return sizeof(target_ulong);
717 case 65:
718 ppc_store_msr(env, ldtul_p(mem_buf));
719 return sizeof(target_ulong);
720 case 66:
721 {
722 uint32_t cr = ldl_p(mem_buf);
723 int i;
724 for (i = 0; i < 8; i++)
725 env->crf[i] = (cr >> (32 - ((i + 1) * 4))) & 0xF;
726 return 4;
727 }
728 case 67:
729 env->lr = ldtul_p(mem_buf);
730 return sizeof(target_ulong);
731 case 68:
732 env->ctr = ldtul_p(mem_buf);
733 return sizeof(target_ulong);
734 case 69:
aurel323d7b4172008-10-21 11:28:46 +0000735 env->xer = ldtul_p(mem_buf);
736 return sizeof(target_ulong);
pbrook56aebc82008-10-11 17:55:29 +0000737 case 70:
738 /* fpscr */
aurel32e571cb42009-01-24 15:07:42 +0000739 if (gdb_has_xml)
740 return 0;
pbrook56aebc82008-10-11 17:55:29 +0000741 return 4;
742 }
bellard9e62fd72004-01-05 22:49:06 +0000743 }
pbrook56aebc82008-10-11 17:55:29 +0000744 return 0;
bellarde95c8d52004-09-30 22:22:08 +0000745}
pbrook56aebc82008-10-11 17:55:29 +0000746
bellarde95c8d52004-09-30 22:22:08 +0000747#elif defined (TARGET_SPARC)
bellarde95c8d52004-09-30 22:22:08 +0000748
pbrook56aebc82008-10-11 17:55:29 +0000749#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
750#define NUM_CORE_REGS 86
751#else
blueswir15a377912009-01-13 16:28:01 +0000752#define NUM_CORE_REGS 72
pbrook56aebc82008-10-11 17:55:29 +0000753#endif
754
755#ifdef TARGET_ABI32
756#define GET_REGA(val) GET_REG32(val)
757#else
758#define GET_REGA(val) GET_REGL(val)
759#endif
760
761static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
762{
763 if (n < 8) {
764 /* g0..g7 */
765 GET_REGA(env->gregs[n]);
bellarde95c8d52004-09-30 22:22:08 +0000766 }
pbrook56aebc82008-10-11 17:55:29 +0000767 if (n < 32) {
768 /* register window */
769 GET_REGA(env->regwptr[n - 8]);
bellarde95c8d52004-09-30 22:22:08 +0000770 }
pbrook56aebc82008-10-11 17:55:29 +0000771#if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
772 if (n < 64) {
773 /* fprs */
774 GET_REG32(*((uint32_t *)&env->fpr[n - 32]));
bellarde95c8d52004-09-30 22:22:08 +0000775 }
776 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
pbrook56aebc82008-10-11 17:55:29 +0000777 switch (n) {
778 case 64: GET_REGA(env->y);
779 case 65: GET_REGA(GET_PSR(env));
780 case 66: GET_REGA(env->wim);
781 case 67: GET_REGA(env->tbr);
782 case 68: GET_REGA(env->pc);
783 case 69: GET_REGA(env->npc);
784 case 70: GET_REGA(env->fsr);
785 case 71: GET_REGA(0); /* csr */
blueswir15a377912009-01-13 16:28:01 +0000786 default: GET_REGA(0);
bellard34751872005-07-02 14:31:34 +0000787 }
bellard34751872005-07-02 14:31:34 +0000788#else
pbrook56aebc82008-10-11 17:55:29 +0000789 if (n < 64) {
790 /* f0-f31 */
791 GET_REG32(*((uint32_t *)&env->fpr[n - 32]));
bellard34751872005-07-02 14:31:34 +0000792 }
pbrook56aebc82008-10-11 17:55:29 +0000793 if (n < 80) {
794 /* f32-f62 (double width, even numbers only) */
795 uint64_t val;
796
797 val = (uint64_t)*((uint32_t *)&env->fpr[(n - 64) * 2 + 32]) << 32;
798 val |= *((uint32_t *)&env->fpr[(n - 64) * 2 + 33]);
799 GET_REG64(val);
800 }
801 switch (n) {
802 case 80: GET_REGL(env->pc);
803 case 81: GET_REGL(env->npc);
804 case 82: GET_REGL(((uint64_t)GET_CCR(env) << 32) |
blueswir117d996e2007-07-07 20:53:22 +0000805 ((env->asi & 0xff) << 24) |
806 ((env->pstate & 0xfff) << 8) |
807 GET_CWP64(env));
pbrook56aebc82008-10-11 17:55:29 +0000808 case 83: GET_REGL(env->fsr);
809 case 84: GET_REGL(env->fprs);
810 case 85: GET_REGL(env->y);
811 }
bellard34751872005-07-02 14:31:34 +0000812#endif
pbrook56aebc82008-10-11 17:55:29 +0000813 return 0;
bellarde95c8d52004-09-30 22:22:08 +0000814}
815
pbrook56aebc82008-10-11 17:55:29 +0000816static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellarde95c8d52004-09-30 22:22:08 +0000817{
pbrook56aebc82008-10-11 17:55:29 +0000818#if defined(TARGET_ABI32)
819 abi_ulong tmp;
820
821 tmp = ldl_p(mem_buf);
blueswir196d19122008-06-07 08:03:05 +0000822#else
pbrook56aebc82008-10-11 17:55:29 +0000823 target_ulong tmp;
824
825 tmp = ldtul_p(mem_buf);
blueswir196d19122008-06-07 08:03:05 +0000826#endif
bellarde95c8d52004-09-30 22:22:08 +0000827
pbrook56aebc82008-10-11 17:55:29 +0000828 if (n < 8) {
829 /* g0..g7 */
830 env->gregs[n] = tmp;
831 } else if (n < 32) {
832 /* register window */
833 env->regwptr[n - 8] = tmp;
bellarde95c8d52004-09-30 22:22:08 +0000834 }
pbrook56aebc82008-10-11 17:55:29 +0000835#if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
836 else if (n < 64) {
837 /* fprs */
838 *((uint32_t *)&env->fpr[n - 32]) = tmp;
839 } else {
840 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
841 switch (n) {
842 case 64: env->y = tmp; break;
843 case 65: PUT_PSR(env, tmp); break;
844 case 66: env->wim = tmp; break;
845 case 67: env->tbr = tmp; break;
846 case 68: env->pc = tmp; break;
847 case 69: env->npc = tmp; break;
848 case 70: env->fsr = tmp; break;
849 default: return 0;
850 }
bellarde95c8d52004-09-30 22:22:08 +0000851 }
pbrook56aebc82008-10-11 17:55:29 +0000852 return 4;
bellard34751872005-07-02 14:31:34 +0000853#else
pbrook56aebc82008-10-11 17:55:29 +0000854 else if (n < 64) {
855 /* f0-f31 */
pbrook56aebc82008-10-11 17:55:29 +0000856 env->fpr[n] = ldfl_p(mem_buf);
857 return 4;
858 } else if (n < 80) {
859 /* f32-f62 (double width, even numbers only) */
860 *((uint32_t *)&env->fpr[(n - 64) * 2 + 32]) = tmp >> 32;
861 *((uint32_t *)&env->fpr[(n - 64) * 2 + 33]) = tmp;
862 } else {
863 switch (n) {
864 case 80: env->pc = tmp; break;
865 case 81: env->npc = tmp; break;
866 case 82:
867 PUT_CCR(env, tmp >> 32);
868 env->asi = (tmp >> 24) & 0xff;
869 env->pstate = (tmp >> 8) & 0xfff;
870 PUT_CWP64(env, tmp & 0xff);
871 break;
872 case 83: env->fsr = tmp; break;
873 case 84: env->fprs = tmp; break;
874 case 85: env->y = tmp; break;
875 default: return 0;
876 }
bellard34751872005-07-02 14:31:34 +0000877 }
pbrook56aebc82008-10-11 17:55:29 +0000878 return 8;
bellard34751872005-07-02 14:31:34 +0000879#endif
bellard9e62fd72004-01-05 22:49:06 +0000880}
bellard1fddef42005-04-17 19:16:13 +0000881#elif defined (TARGET_ARM)
pbrook56aebc82008-10-11 17:55:29 +0000882
883/* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect
884 whatever the target description contains. Due to a historical mishap
885 the FPA registers appear in between core integer regs and the CPSR.
886 We hack round this by giving the FPA regs zero size when talking to a
887 newer gdb. */
888#define NUM_CORE_REGS 26
889#define GDB_CORE_XML "arm-core.xml"
890
891static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
bellard1fddef42005-04-17 19:16:13 +0000892{
pbrook56aebc82008-10-11 17:55:29 +0000893 if (n < 16) {
894 /* Core integer register. */
895 GET_REG32(env->regs[n]);
896 }
897 if (n < 24) {
898 /* FPA registers. */
899 if (gdb_has_xml)
900 return 0;
901 memset(mem_buf, 0, 12);
902 return 12;
903 }
904 switch (n) {
905 case 24:
906 /* FPA status register. */
907 if (gdb_has_xml)
908 return 0;
909 GET_REG32(0);
910 case 25:
911 /* CPSR */
912 GET_REG32(cpsr_read(env));
913 }
914 /* Unknown register. */
915 return 0;
bellard1fddef42005-04-17 19:16:13 +0000916}
917
pbrook56aebc82008-10-11 17:55:29 +0000918static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellard1fddef42005-04-17 19:16:13 +0000919{
pbrook56aebc82008-10-11 17:55:29 +0000920 uint32_t tmp;
bellard1fddef42005-04-17 19:16:13 +0000921
pbrook56aebc82008-10-11 17:55:29 +0000922 tmp = ldl_p(mem_buf);
923
924 /* Mask out low bit of PC to workaround gdb bugs. This will probably
925 cause problems if we ever implement the Jazelle DBX extensions. */
926 if (n == 15)
927 tmp &= ~1;
928
929 if (n < 16) {
930 /* Core integer register. */
931 env->regs[n] = tmp;
932 return 4;
933 }
934 if (n < 24) { /* 16-23 */
935 /* FPA registers (ignored). */
936 if (gdb_has_xml)
937 return 0;
938 return 12;
939 }
940 switch (n) {
941 case 24:
942 /* FPA status register (ignored). */
943 if (gdb_has_xml)
944 return 0;
945 return 4;
946 case 25:
947 /* CPSR */
948 cpsr_write (env, tmp, 0xffffffff);
949 return 4;
950 }
951 /* Unknown register. */
952 return 0;
bellard1fddef42005-04-17 19:16:13 +0000953}
pbrook56aebc82008-10-11 17:55:29 +0000954
pbrooke6e59062006-10-22 00:18:54 +0000955#elif defined (TARGET_M68K)
pbrook56aebc82008-10-11 17:55:29 +0000956
957#define NUM_CORE_REGS 18
958
959#define GDB_CORE_XML "cf-core.xml"
960
961static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
pbrooke6e59062006-10-22 00:18:54 +0000962{
pbrook56aebc82008-10-11 17:55:29 +0000963 if (n < 8) {
964 /* D0-D7 */
965 GET_REG32(env->dregs[n]);
966 } else if (n < 16) {
967 /* A0-A7 */
968 GET_REG32(env->aregs[n - 8]);
969 } else {
970 switch (n) {
971 case 16: GET_REG32(env->sr);
972 case 17: GET_REG32(env->pc);
973 }
pbrooke6e59062006-10-22 00:18:54 +0000974 }
pbrook56aebc82008-10-11 17:55:29 +0000975 /* FP registers not included here because they vary between
976 ColdFire and m68k. Use XML bits for these. */
977 return 0;
pbrooke6e59062006-10-22 00:18:54 +0000978}
979
pbrook56aebc82008-10-11 17:55:29 +0000980static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
pbrooke6e59062006-10-22 00:18:54 +0000981{
pbrook56aebc82008-10-11 17:55:29 +0000982 uint32_t tmp;
pbrooke6e59062006-10-22 00:18:54 +0000983
pbrook56aebc82008-10-11 17:55:29 +0000984 tmp = ldl_p(mem_buf);
985
986 if (n < 8) {
987 /* D0-D7 */
988 env->dregs[n] = tmp;
989 } else if (n < 8) {
990 /* A0-A7 */
991 env->aregs[n - 8] = tmp;
992 } else {
993 switch (n) {
994 case 16: env->sr = tmp; break;
995 case 17: env->pc = tmp; break;
996 default: return 0;
997 }
pbrooke6e59062006-10-22 00:18:54 +0000998 }
pbrook56aebc82008-10-11 17:55:29 +0000999 return 4;
pbrooke6e59062006-10-22 00:18:54 +00001000}
bellard6f970bd2005-12-05 19:55:19 +00001001#elif defined (TARGET_MIPS)
pbrook56aebc82008-10-11 17:55:29 +00001002
1003#define NUM_CORE_REGS 73
1004
1005static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
bellard6f970bd2005-12-05 19:55:19 +00001006{
pbrook56aebc82008-10-11 17:55:29 +00001007 if (n < 32) {
1008 GET_REGL(env->active_tc.gpr[n]);
1009 }
1010 if (env->CP0_Config1 & (1 << CP0C1_FP)) {
1011 if (n >= 38 && n < 70) {
ths7ac256b2007-10-25 21:30:37 +00001012 if (env->CP0_Status & (1 << CP0St_FR))
pbrook56aebc82008-10-11 17:55:29 +00001013 GET_REGL(env->active_fpu.fpr[n - 38].d);
ths7ac256b2007-10-25 21:30:37 +00001014 else
pbrook56aebc82008-10-11 17:55:29 +00001015 GET_REGL(env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX]);
1016 }
1017 switch (n) {
1018 case 70: GET_REGL((int32_t)env->active_fpu.fcr31);
1019 case 71: GET_REGL((int32_t)env->active_fpu.fcr0);
1020 }
1021 }
1022 switch (n) {
1023 case 32: GET_REGL((int32_t)env->CP0_Status);
1024 case 33: GET_REGL(env->active_tc.LO[0]);
1025 case 34: GET_REGL(env->active_tc.HI[0]);
1026 case 35: GET_REGL(env->CP0_BadVAddr);
1027 case 36: GET_REGL((int32_t)env->CP0_Cause);
1028 case 37: GET_REGL(env->active_tc.PC);
1029 case 72: GET_REGL(0); /* fp */
1030 case 89: GET_REGL((int32_t)env->CP0_PRid);
1031 }
1032 if (n >= 73 && n <= 88) {
1033 /* 16 embedded regs. */
1034 GET_REGL(0);
1035 }
ths36d23952007-02-28 22:37:42 +00001036
pbrook56aebc82008-10-11 17:55:29 +00001037 return 0;
bellard6f970bd2005-12-05 19:55:19 +00001038}
1039
ths8e33c082006-12-11 19:22:27 +00001040/* convert MIPS rounding mode in FCR31 to IEEE library */
1041static unsigned int ieee_rm[] =
1042 {
1043 float_round_nearest_even,
1044 float_round_to_zero,
1045 float_round_up,
1046 float_round_down
1047 };
1048#define RESTORE_ROUNDING_MODE \
thsf01be152008-09-18 11:57:27 +00001049 set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
ths8e33c082006-12-11 19:22:27 +00001050
pbrook56aebc82008-10-11 17:55:29 +00001051static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellard6f970bd2005-12-05 19:55:19 +00001052{
pbrook56aebc82008-10-11 17:55:29 +00001053 target_ulong tmp;
bellard6f970bd2005-12-05 19:55:19 +00001054
pbrook56aebc82008-10-11 17:55:29 +00001055 tmp = ldtul_p(mem_buf);
bellard6f970bd2005-12-05 19:55:19 +00001056
pbrook56aebc82008-10-11 17:55:29 +00001057 if (n < 32) {
1058 env->active_tc.gpr[n] = tmp;
1059 return sizeof(target_ulong);
1060 }
1061 if (env->CP0_Config1 & (1 << CP0C1_FP)
1062 && n >= 38 && n < 73) {
1063 if (n < 70) {
ths7ac256b2007-10-25 21:30:37 +00001064 if (env->CP0_Status & (1 << CP0St_FR))
pbrook56aebc82008-10-11 17:55:29 +00001065 env->active_fpu.fpr[n - 38].d = tmp;
ths7ac256b2007-10-25 21:30:37 +00001066 else
pbrook56aebc82008-10-11 17:55:29 +00001067 env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX] = tmp;
1068 }
1069 switch (n) {
1070 case 70:
1071 env->active_fpu.fcr31 = tmp & 0xFF83FFFF;
1072 /* set rounding mode */
1073 RESTORE_ROUNDING_MODE;
ths8e33c082006-12-11 19:22:27 +00001074#ifndef CONFIG_SOFTFLOAT
pbrook56aebc82008-10-11 17:55:29 +00001075 /* no floating point exception for native float */
1076 SET_FP_ENABLE(env->active_fpu.fcr31, 0);
ths8e33c082006-12-11 19:22:27 +00001077#endif
pbrook56aebc82008-10-11 17:55:29 +00001078 break;
1079 case 71: env->active_fpu.fcr0 = tmp; break;
1080 }
1081 return sizeof(target_ulong);
1082 }
1083 switch (n) {
1084 case 32: env->CP0_Status = tmp; break;
1085 case 33: env->active_tc.LO[0] = tmp; break;
1086 case 34: env->active_tc.HI[0] = tmp; break;
1087 case 35: env->CP0_BadVAddr = tmp; break;
1088 case 36: env->CP0_Cause = tmp; break;
1089 case 37: env->active_tc.PC = tmp; break;
1090 case 72: /* fp, ignored */ break;
1091 default:
1092 if (n > 89)
1093 return 0;
1094 /* Other registers are readonly. Ignore writes. */
1095 break;
1096 }
1097
1098 return sizeof(target_ulong);
bellard6f970bd2005-12-05 19:55:19 +00001099}
bellardfdf9b3e2006-04-27 21:07:38 +00001100#elif defined (TARGET_SH4)
ths6ef99fc2007-05-13 16:36:24 +00001101
1102/* Hint: Use "set architecture sh4" in GDB to see fpu registers */
pbrook56aebc82008-10-11 17:55:29 +00001103/* FIXME: We should use XML for this. */
ths6ef99fc2007-05-13 16:36:24 +00001104
pbrook56aebc82008-10-11 17:55:29 +00001105#define NUM_CORE_REGS 59
1106
1107static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
bellardfdf9b3e2006-04-27 21:07:38 +00001108{
pbrook56aebc82008-10-11 17:55:29 +00001109 if (n < 8) {
1110 if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
1111 GET_REGL(env->gregs[n + 16]);
1112 } else {
1113 GET_REGL(env->gregs[n]);
1114 }
1115 } else if (n < 16) {
1116 GET_REGL(env->gregs[n - 8]);
1117 } else if (n >= 25 && n < 41) {
1118 GET_REGL(env->fregs[(n - 25) + ((env->fpscr & FPSCR_FR) ? 16 : 0)]);
1119 } else if (n >= 43 && n < 51) {
1120 GET_REGL(env->gregs[n - 43]);
1121 } else if (n >= 51 && n < 59) {
1122 GET_REGL(env->gregs[n - (51 - 16)]);
1123 }
1124 switch (n) {
1125 case 16: GET_REGL(env->pc);
1126 case 17: GET_REGL(env->pr);
1127 case 18: GET_REGL(env->gbr);
1128 case 19: GET_REGL(env->vbr);
1129 case 20: GET_REGL(env->mach);
1130 case 21: GET_REGL(env->macl);
1131 case 22: GET_REGL(env->sr);
1132 case 23: GET_REGL(env->fpul);
1133 case 24: GET_REGL(env->fpscr);
1134 case 41: GET_REGL(env->ssr);
1135 case 42: GET_REGL(env->spc);
1136 }
bellardfdf9b3e2006-04-27 21:07:38 +00001137
pbrook56aebc82008-10-11 17:55:29 +00001138 return 0;
bellardfdf9b3e2006-04-27 21:07:38 +00001139}
1140
pbrook56aebc82008-10-11 17:55:29 +00001141static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellardfdf9b3e2006-04-27 21:07:38 +00001142{
pbrook56aebc82008-10-11 17:55:29 +00001143 uint32_t tmp;
bellardfdf9b3e2006-04-27 21:07:38 +00001144
pbrook56aebc82008-10-11 17:55:29 +00001145 tmp = ldl_p(mem_buf);
1146
1147 if (n < 8) {
1148 if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
1149 env->gregs[n + 16] = tmp;
1150 } else {
1151 env->gregs[n] = tmp;
1152 }
1153 return 4;
1154 } else if (n < 16) {
1155 env->gregs[n - 8] = tmp;
1156 return 4;
1157 } else if (n >= 25 && n < 41) {
1158 env->fregs[(n - 25) + ((env->fpscr & FPSCR_FR) ? 16 : 0)] = tmp;
1159 } else if (n >= 43 && n < 51) {
1160 env->gregs[n - 43] = tmp;
1161 return 4;
1162 } else if (n >= 51 && n < 59) {
1163 env->gregs[n - (51 - 16)] = tmp;
1164 return 4;
1165 }
1166 switch (n) {
1167 case 16: env->pc = tmp;
1168 case 17: env->pr = tmp;
1169 case 18: env->gbr = tmp;
1170 case 19: env->vbr = tmp;
1171 case 20: env->mach = tmp;
1172 case 21: env->macl = tmp;
1173 case 22: env->sr = tmp;
1174 case 23: env->fpul = tmp;
1175 case 24: env->fpscr = tmp;
1176 case 41: env->ssr = tmp;
1177 case 42: env->spc = tmp;
1178 default: return 0;
1179 }
1180
1181 return 4;
bellardfdf9b3e2006-04-27 21:07:38 +00001182}
Edgar E. Iglesiasd74d6a92009-05-20 20:16:31 +02001183#elif defined (TARGET_MICROBLAZE)
1184
1185#define NUM_CORE_REGS (32 + 5)
1186
1187static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
1188{
1189 if (n < 32) {
1190 GET_REG32(env->regs[n]);
1191 } else {
1192 GET_REG32(env->sregs[n - 32]);
1193 }
1194 return 0;
1195}
1196
1197static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
1198{
1199 uint32_t tmp;
1200
1201 if (n > NUM_CORE_REGS)
1202 return 0;
1203
1204 tmp = ldl_p(mem_buf);
1205
1206 if (n < 32) {
1207 env->regs[n] = tmp;
1208 } else {
1209 env->sregs[n - 32] = tmp;
1210 }
1211 return 4;
1212}
thsf1ccf902007-10-08 13:16:14 +00001213#elif defined (TARGET_CRIS)
1214
pbrook56aebc82008-10-11 17:55:29 +00001215#define NUM_CORE_REGS 49
1216
1217static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
thsf1ccf902007-10-08 13:16:14 +00001218{
pbrook56aebc82008-10-11 17:55:29 +00001219 uint8_t srs;
1220
1221 srs = env->pregs[PR_SRS];
1222 if (n < 16) {
1223 GET_REG32(env->regs[n]);
1224 }
1225
1226 if (n >= 21 && n < 32) {
1227 GET_REG32(env->pregs[n - 16]);
1228 }
1229 if (n >= 33 && n < 49) {
1230 GET_REG32(env->sregs[srs][n - 33]);
1231 }
1232 switch (n) {
1233 case 16: GET_REG8(env->pregs[0]);
1234 case 17: GET_REG8(env->pregs[1]);
1235 case 18: GET_REG32(env->pregs[2]);
1236 case 19: GET_REG8(srs);
1237 case 20: GET_REG16(env->pregs[4]);
1238 case 32: GET_REG32(env->pc);
1239 }
1240
1241 return 0;
thsf1ccf902007-10-08 13:16:14 +00001242}
1243
pbrook56aebc82008-10-11 17:55:29 +00001244static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
thsf1ccf902007-10-08 13:16:14 +00001245{
pbrook56aebc82008-10-11 17:55:29 +00001246 uint32_t tmp;
thsf1ccf902007-10-08 13:16:14 +00001247
pbrook56aebc82008-10-11 17:55:29 +00001248 if (n > 49)
1249 return 0;
thsf1ccf902007-10-08 13:16:14 +00001250
pbrook56aebc82008-10-11 17:55:29 +00001251 tmp = ldl_p(mem_buf);
thsf1ccf902007-10-08 13:16:14 +00001252
pbrook56aebc82008-10-11 17:55:29 +00001253 if (n < 16) {
1254 env->regs[n] = tmp;
1255 }
thsf1ccf902007-10-08 13:16:14 +00001256
edgar_igld7b69672008-10-11 19:32:21 +00001257 if (n >= 21 && n < 32) {
1258 env->pregs[n - 16] = tmp;
1259 }
1260
1261 /* FIXME: Should support function regs be writable? */
pbrook56aebc82008-10-11 17:55:29 +00001262 switch (n) {
1263 case 16: return 1;
1264 case 17: return 1;
edgar_igld7b69672008-10-11 19:32:21 +00001265 case 18: env->pregs[PR_PID] = tmp; break;
pbrook56aebc82008-10-11 17:55:29 +00001266 case 19: return 1;
1267 case 20: return 2;
1268 case 32: env->pc = tmp; break;
1269 }
thsf1ccf902007-10-08 13:16:14 +00001270
pbrook56aebc82008-10-11 17:55:29 +00001271 return 4;
thsf1ccf902007-10-08 13:16:14 +00001272}
aurel3219bf5172008-12-07 23:26:32 +00001273#elif defined (TARGET_ALPHA)
1274
1275#define NUM_CORE_REGS 65
1276
1277static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
1278{
1279 if (n < 31) {
1280 GET_REGL(env->ir[n]);
1281 }
1282 else if (n == 31) {
1283 GET_REGL(0);
1284 }
1285 else if (n<63) {
1286 uint64_t val;
1287
1288 val=*((uint64_t *)&env->fir[n-32]);
1289 GET_REGL(val);
1290 }
1291 else if (n==63) {
1292 GET_REGL(env->fpcr);
1293 }
1294 else if (n==64) {
1295 GET_REGL(env->pc);
1296 }
1297 else {
1298 GET_REGL(0);
1299 }
1300
1301 return 0;
1302}
1303
1304static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
1305{
1306 target_ulong tmp;
1307 tmp = ldtul_p(mem_buf);
1308
1309 if (n < 31) {
1310 env->ir[n] = tmp;
1311 }
1312
1313 if (n > 31 && n < 63) {
1314 env->fir[n - 32] = ldfl_p(mem_buf);
1315 }
1316
1317 if (n == 64 ) {
1318 env->pc=tmp;
1319 }
1320
1321 return 8;
1322}
bellard1fddef42005-04-17 19:16:13 +00001323#else
pbrook56aebc82008-10-11 17:55:29 +00001324
1325#define NUM_CORE_REGS 0
1326
1327static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
bellard6da41ea2004-01-04 15:48:38 +00001328{
1329 return 0;
1330}
1331
pbrook56aebc82008-10-11 17:55:29 +00001332static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellard6da41ea2004-01-04 15:48:38 +00001333{
pbrook56aebc82008-10-11 17:55:29 +00001334 return 0;
bellard6da41ea2004-01-04 15:48:38 +00001335}
1336
1337#endif
bellardb4608c02003-06-27 17:34:32 +00001338
pbrook56aebc82008-10-11 17:55:29 +00001339static int num_g_regs = NUM_CORE_REGS;
1340
1341#ifdef GDB_CORE_XML
1342/* Encode data using the encoding for 'x' packets. */
1343static int memtox(char *buf, const char *mem, int len)
1344{
1345 char *p = buf;
1346 char c;
1347
1348 while (len--) {
1349 c = *(mem++);
1350 switch (c) {
1351 case '#': case '$': case '*': case '}':
1352 *(p++) = '}';
1353 *(p++) = c ^ 0x20;
1354 break;
1355 default:
1356 *(p++) = c;
1357 break;
1358 }
1359 }
1360 return p - buf;
1361}
1362
aurel323faf7782008-12-07 23:26:17 +00001363static const char *get_feature_xml(const char *p, const char **newp)
pbrook56aebc82008-10-11 17:55:29 +00001364{
1365 extern const char *const xml_builtin[][2];
1366 size_t len;
1367 int i;
1368 const char *name;
1369 static char target_xml[1024];
1370
1371 len = 0;
1372 while (p[len] && p[len] != ':')
1373 len++;
1374 *newp = p + len;
1375
1376 name = NULL;
1377 if (strncmp(p, "target.xml", len) == 0) {
1378 /* Generate the XML description for this CPU. */
1379 if (!target_xml[0]) {
1380 GDBRegisterState *r;
1381
blueswir15b3715b2008-10-25 11:18:12 +00001382 snprintf(target_xml, sizeof(target_xml),
1383 "<?xml version=\"1.0\"?>"
1384 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
1385 "<target>"
1386 "<xi:include href=\"%s\"/>",
1387 GDB_CORE_XML);
pbrook56aebc82008-10-11 17:55:29 +00001388
aliguori880a7572008-11-18 20:30:24 +00001389 for (r = first_cpu->gdb_regs; r; r = r->next) {
blueswir12dc766d2009-04-13 16:06:19 +00001390 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
1391 pstrcat(target_xml, sizeof(target_xml), r->xml);
1392 pstrcat(target_xml, sizeof(target_xml), "\"/>");
pbrook56aebc82008-10-11 17:55:29 +00001393 }
blueswir12dc766d2009-04-13 16:06:19 +00001394 pstrcat(target_xml, sizeof(target_xml), "</target>");
pbrook56aebc82008-10-11 17:55:29 +00001395 }
1396 return target_xml;
1397 }
1398 for (i = 0; ; i++) {
1399 name = xml_builtin[i][0];
1400 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
1401 break;
1402 }
1403 return name ? xml_builtin[i][1] : NULL;
1404}
1405#endif
1406
1407static int gdb_read_register(CPUState *env, uint8_t *mem_buf, int reg)
1408{
1409 GDBRegisterState *r;
1410
1411 if (reg < NUM_CORE_REGS)
1412 return cpu_gdb_read_register(env, mem_buf, reg);
1413
1414 for (r = env->gdb_regs; r; r = r->next) {
1415 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
1416 return r->get_reg(env, mem_buf, reg - r->base_reg);
1417 }
1418 }
1419 return 0;
1420}
1421
1422static int gdb_write_register(CPUState *env, uint8_t *mem_buf, int reg)
1423{
1424 GDBRegisterState *r;
1425
1426 if (reg < NUM_CORE_REGS)
1427 return cpu_gdb_write_register(env, mem_buf, reg);
1428
1429 for (r = env->gdb_regs; r; r = r->next) {
1430 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
1431 return r->set_reg(env, mem_buf, reg - r->base_reg);
1432 }
1433 }
1434 return 0;
1435}
1436
1437/* Register a supplemental set of CPU registers. If g_pos is nonzero it
1438 specifies the first register number and these registers are included in
1439 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
1440 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
1441 */
1442
1443void gdb_register_coprocessor(CPUState * env,
1444 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
1445 int num_regs, const char *xml, int g_pos)
1446{
1447 GDBRegisterState *s;
1448 GDBRegisterState **p;
1449 static int last_reg = NUM_CORE_REGS;
1450
1451 s = (GDBRegisterState *)qemu_mallocz(sizeof(GDBRegisterState));
1452 s->base_reg = last_reg;
1453 s->num_regs = num_regs;
1454 s->get_reg = get_reg;
1455 s->set_reg = set_reg;
1456 s->xml = xml;
1457 p = &env->gdb_regs;
1458 while (*p) {
1459 /* Check for duplicates. */
1460 if (strcmp((*p)->xml, xml) == 0)
1461 return;
1462 p = &(*p)->next;
1463 }
1464 /* Add to end of list. */
1465 last_reg += num_regs;
1466 *p = s;
1467 if (g_pos) {
1468 if (g_pos != s->base_reg) {
1469 fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
1470 "Expected %d got %d\n", xml, g_pos, s->base_reg);
1471 } else {
1472 num_g_regs = last_reg;
1473 }
1474 }
1475}
1476
aliguoria1d1bb32008-11-18 20:07:32 +00001477#ifndef CONFIG_USER_ONLY
1478static const int xlat_gdb_type[] = {
1479 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
1480 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
1481 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
1482};
1483#endif
1484
aliguori880a7572008-11-18 20:30:24 +00001485static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +00001486{
aliguori880a7572008-11-18 20:30:24 +00001487 CPUState *env;
1488 int err = 0;
1489
aliguorie22a25c2009-03-12 20:12:48 +00001490 if (kvm_enabled())
1491 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
1492
aliguoria1d1bb32008-11-18 20:07:32 +00001493 switch (type) {
1494 case GDB_BREAKPOINT_SW:
1495 case GDB_BREAKPOINT_HW:
aliguori880a7572008-11-18 20:30:24 +00001496 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1497 err = cpu_breakpoint_insert(env, addr, BP_GDB, NULL);
1498 if (err)
1499 break;
1500 }
1501 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001502#ifndef CONFIG_USER_ONLY
1503 case GDB_WATCHPOINT_WRITE:
1504 case GDB_WATCHPOINT_READ:
1505 case GDB_WATCHPOINT_ACCESS:
aliguori880a7572008-11-18 20:30:24 +00001506 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1507 err = cpu_watchpoint_insert(env, addr, len, xlat_gdb_type[type],
1508 NULL);
1509 if (err)
1510 break;
1511 }
1512 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001513#endif
1514 default:
1515 return -ENOSYS;
1516 }
1517}
1518
aliguori880a7572008-11-18 20:30:24 +00001519static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +00001520{
aliguori880a7572008-11-18 20:30:24 +00001521 CPUState *env;
1522 int err = 0;
1523
aliguorie22a25c2009-03-12 20:12:48 +00001524 if (kvm_enabled())
1525 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
1526
aliguoria1d1bb32008-11-18 20:07:32 +00001527 switch (type) {
1528 case GDB_BREAKPOINT_SW:
1529 case GDB_BREAKPOINT_HW:
aliguori880a7572008-11-18 20:30:24 +00001530 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1531 err = cpu_breakpoint_remove(env, addr, BP_GDB);
1532 if (err)
1533 break;
1534 }
1535 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001536#ifndef CONFIG_USER_ONLY
1537 case GDB_WATCHPOINT_WRITE:
1538 case GDB_WATCHPOINT_READ:
1539 case GDB_WATCHPOINT_ACCESS:
aliguori880a7572008-11-18 20:30:24 +00001540 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1541 err = cpu_watchpoint_remove(env, addr, len, xlat_gdb_type[type]);
1542 if (err)
1543 break;
1544 }
1545 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001546#endif
1547 default:
1548 return -ENOSYS;
1549 }
1550}
1551
aliguori880a7572008-11-18 20:30:24 +00001552static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +00001553{
aliguori880a7572008-11-18 20:30:24 +00001554 CPUState *env;
1555
aliguorie22a25c2009-03-12 20:12:48 +00001556 if (kvm_enabled()) {
1557 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
1558 return;
1559 }
1560
aliguori880a7572008-11-18 20:30:24 +00001561 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1562 cpu_breakpoint_remove_all(env, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +00001563#ifndef CONFIG_USER_ONLY
aliguori880a7572008-11-18 20:30:24 +00001564 cpu_watchpoint_remove_all(env, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +00001565#endif
aliguori880a7572008-11-18 20:30:24 +00001566 }
aliguoria1d1bb32008-11-18 20:07:32 +00001567}
1568
aurel32fab9d282009-04-08 21:29:37 +00001569static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
1570{
1571#if defined(TARGET_I386)
1572 s->c_cpu->eip = pc;
1573 cpu_synchronize_state(s->c_cpu, 1);
1574#elif defined (TARGET_PPC)
1575 s->c_cpu->nip = pc;
1576#elif defined (TARGET_SPARC)
1577 s->c_cpu->pc = pc;
1578 s->c_cpu->npc = pc + 4;
1579#elif defined (TARGET_ARM)
1580 s->c_cpu->regs[15] = pc;
1581#elif defined (TARGET_SH4)
1582 s->c_cpu->pc = pc;
1583#elif defined (TARGET_MIPS)
1584 s->c_cpu->active_tc.PC = pc;
Edgar E. Iglesiasd74d6a92009-05-20 20:16:31 +02001585#elif defined (TARGET_MICROBLAZE)
1586 s->c_cpu->sregs[SR_PC] = pc;
aurel32fab9d282009-04-08 21:29:37 +00001587#elif defined (TARGET_CRIS)
1588 s->c_cpu->pc = pc;
1589#elif defined (TARGET_ALPHA)
1590 s->c_cpu->pc = pc;
1591#endif
1592}
1593
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001594static inline int gdb_id(CPUState *env)
1595{
1596#if defined(CONFIG_USER_ONLY) && defined(USE_NPTL)
1597 return env->host_tid;
1598#else
1599 return env->cpu_index + 1;
1600#endif
1601}
1602
1603static CPUState *find_cpu(uint32_t thread_id)
1604{
1605 CPUState *env;
1606
1607 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1608 if (gdb_id(env) == thread_id) {
1609 return env;
1610 }
1611 }
1612
1613 return NULL;
1614}
1615
aliguori880a7572008-11-18 20:30:24 +00001616static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +00001617{
aliguori880a7572008-11-18 20:30:24 +00001618 CPUState *env;
bellardb4608c02003-06-27 17:34:32 +00001619 const char *p;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001620 uint32_t thread;
1621 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +00001622 char buf[MAX_PACKET_LENGTH];
1623 uint8_t mem_buf[MAX_PACKET_LENGTH];
1624 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +00001625 target_ulong addr, len;
ths3b46e622007-09-17 08:09:54 +00001626
bellard858693c2004-03-31 18:52:07 +00001627#ifdef DEBUG_GDB
1628 printf("command='%s'\n", line_buf);
bellard4c3a88a2003-07-26 12:06:08 +00001629#endif
bellard858693c2004-03-31 18:52:07 +00001630 p = line_buf;
1631 ch = *p++;
1632 switch(ch) {
1633 case '?':
bellard1fddef42005-04-17 19:16:13 +00001634 /* TODO: Make this return the correct value for user-mode. */
aurel32ca587a82008-12-18 22:44:13 +00001635 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001636 gdb_id(s->c_cpu));
bellard858693c2004-03-31 18:52:07 +00001637 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +00001638 /* Remove all the breakpoints when this query is issued,
1639 * because gdb is doing and initial connect and the state
1640 * should be cleaned up.
1641 */
aliguori880a7572008-11-18 20:30:24 +00001642 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +00001643 break;
1644 case 'c':
1645 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +00001646 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001647 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001648 }
aurel32ca587a82008-12-18 22:44:13 +00001649 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +00001650 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +00001651 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +00001652 case 'C':
aurel32ca587a82008-12-18 22:44:13 +00001653 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
1654 if (s->signal == -1)
1655 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +00001656 gdb_continue(s);
1657 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001658 case 'v':
1659 if (strncmp(p, "Cont", 4) == 0) {
1660 int res_signal, res_thread;
1661
1662 p += 4;
1663 if (*p == '?') {
1664 put_packet(s, "vCont;c;C;s;S");
1665 break;
1666 }
1667 res = 0;
1668 res_signal = 0;
1669 res_thread = 0;
1670 while (*p) {
1671 int action, signal;
1672
1673 if (*p++ != ';') {
1674 res = 0;
1675 break;
1676 }
1677 action = *p++;
1678 signal = 0;
1679 if (action == 'C' || action == 'S') {
1680 signal = strtoul(p, (char **)&p, 16);
1681 } else if (action != 'c' && action != 's') {
1682 res = 0;
1683 break;
1684 }
1685 thread = 0;
1686 if (*p == ':') {
1687 thread = strtoull(p+1, (char **)&p, 16);
1688 }
1689 action = tolower(action);
1690 if (res == 0 || (res == 'c' && action == 's')) {
1691 res = action;
1692 res_signal = signal;
1693 res_thread = thread;
1694 }
1695 }
1696 if (res) {
1697 if (res_thread != -1 && res_thread != 0) {
1698 env = find_cpu(res_thread);
1699 if (env == NULL) {
1700 put_packet(s, "E22");
1701 break;
1702 }
1703 s->c_cpu = env;
1704 }
1705 if (res == 's') {
1706 cpu_single_step(s->c_cpu, sstep_flags);
1707 }
1708 s->signal = res_signal;
1709 gdb_continue(s);
1710 return RS_IDLE;
1711 }
1712 break;
1713 } else {
1714 goto unknown_command;
1715 }
edgar_igl7d03f822008-05-17 18:58:29 +00001716 case 'k':
1717 /* Kill the target */
1718 fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
1719 exit(0);
1720 case 'D':
1721 /* Detach packet */
aliguori880a7572008-11-18 20:30:24 +00001722 gdb_breakpoint_remove_all();
edgar_igl7d03f822008-05-17 18:58:29 +00001723 gdb_continue(s);
1724 put_packet(s, "OK");
1725 break;
bellard858693c2004-03-31 18:52:07 +00001726 case 's':
1727 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +00001728 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001729 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001730 }
aliguori880a7572008-11-18 20:30:24 +00001731 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +00001732 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +00001733 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +00001734 case 'F':
1735 {
1736 target_ulong ret;
1737 target_ulong err;
1738
1739 ret = strtoull(p, (char **)&p, 16);
1740 if (*p == ',') {
1741 p++;
1742 err = strtoull(p, (char **)&p, 16);
1743 } else {
1744 err = 0;
1745 }
1746 if (*p == ',')
1747 p++;
1748 type = *p;
1749 if (gdb_current_syscall_cb)
aliguori880a7572008-11-18 20:30:24 +00001750 gdb_current_syscall_cb(s->c_cpu, ret, err);
pbrooka2d1eba2007-01-28 03:10:55 +00001751 if (type == 'C') {
1752 put_packet(s, "T02");
1753 } else {
edgar_iglba70a622008-03-14 06:10:42 +00001754 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +00001755 }
1756 }
1757 break;
bellard858693c2004-03-31 18:52:07 +00001758 case 'g':
aliguorie22a25c2009-03-12 20:12:48 +00001759 cpu_synchronize_state(s->g_cpu, 0);
pbrook56aebc82008-10-11 17:55:29 +00001760 len = 0;
1761 for (addr = 0; addr < num_g_regs; addr++) {
aliguori880a7572008-11-18 20:30:24 +00001762 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +00001763 len += reg_size;
1764 }
1765 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +00001766 put_packet(s, buf);
1767 break;
1768 case 'G':
pbrook56aebc82008-10-11 17:55:29 +00001769 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +00001770 len = strlen(p) / 2;
1771 hextomem((uint8_t *)registers, p, len);
pbrook56aebc82008-10-11 17:55:29 +00001772 for (addr = 0; addr < num_g_regs && len > 0; addr++) {
aliguori880a7572008-11-18 20:30:24 +00001773 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001774 len -= reg_size;
1775 registers += reg_size;
1776 }
aliguorie22a25c2009-03-12 20:12:48 +00001777 cpu_synchronize_state(s->g_cpu, 1);
bellard858693c2004-03-31 18:52:07 +00001778 put_packet(s, "OK");
1779 break;
1780 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001781 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001782 if (*p == ',')
1783 p++;
bellard9d9754a2006-06-25 15:32:37 +00001784 len = strtoull(p, NULL, 16);
aliguori880a7572008-11-18 20:30:24 +00001785 if (cpu_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 0) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001786 put_packet (s, "E14");
1787 } else {
1788 memtohex(buf, mem_buf, len);
1789 put_packet(s, buf);
1790 }
bellard858693c2004-03-31 18:52:07 +00001791 break;
1792 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001793 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001794 if (*p == ',')
1795 p++;
bellard9d9754a2006-06-25 15:32:37 +00001796 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001797 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001798 p++;
1799 hextomem(mem_buf, p, len);
aliguori880a7572008-11-18 20:30:24 +00001800 if (cpu_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 1) != 0)
bellard905f20b2005-04-26 21:09:55 +00001801 put_packet(s, "E14");
bellard858693c2004-03-31 18:52:07 +00001802 else
1803 put_packet(s, "OK");
1804 break;
pbrook56aebc82008-10-11 17:55:29 +00001805 case 'p':
1806 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1807 This works, but can be very slow. Anything new enough to
1808 understand XML also knows how to use this properly. */
1809 if (!gdb_has_xml)
1810 goto unknown_command;
1811 addr = strtoull(p, (char **)&p, 16);
aliguori880a7572008-11-18 20:30:24 +00001812 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001813 if (reg_size) {
1814 memtohex(buf, mem_buf, reg_size);
1815 put_packet(s, buf);
1816 } else {
1817 put_packet(s, "E14");
1818 }
1819 break;
1820 case 'P':
1821 if (!gdb_has_xml)
1822 goto unknown_command;
1823 addr = strtoull(p, (char **)&p, 16);
1824 if (*p == '=')
1825 p++;
1826 reg_size = strlen(p) / 2;
1827 hextomem(mem_buf, p, reg_size);
aliguori880a7572008-11-18 20:30:24 +00001828 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001829 put_packet(s, "OK");
1830 break;
bellard858693c2004-03-31 18:52:07 +00001831 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001832 case 'z':
1833 type = strtoul(p, (char **)&p, 16);
1834 if (*p == ',')
1835 p++;
bellard9d9754a2006-06-25 15:32:37 +00001836 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001837 if (*p == ',')
1838 p++;
bellard9d9754a2006-06-25 15:32:37 +00001839 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001840 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001841 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001842 else
aliguori880a7572008-11-18 20:30:24 +00001843 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001844 if (res >= 0)
1845 put_packet(s, "OK");
1846 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001847 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001848 else
1849 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001850 break;
aliguori880a7572008-11-18 20:30:24 +00001851 case 'H':
1852 type = *p++;
1853 thread = strtoull(p, (char **)&p, 16);
1854 if (thread == -1 || thread == 0) {
1855 put_packet(s, "OK");
1856 break;
1857 }
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001858 env = find_cpu(thread);
aliguori880a7572008-11-18 20:30:24 +00001859 if (env == NULL) {
1860 put_packet(s, "E22");
1861 break;
1862 }
1863 switch (type) {
1864 case 'c':
1865 s->c_cpu = env;
1866 put_packet(s, "OK");
1867 break;
1868 case 'g':
1869 s->g_cpu = env;
1870 put_packet(s, "OK");
1871 break;
1872 default:
1873 put_packet(s, "E22");
1874 break;
1875 }
1876 break;
1877 case 'T':
1878 thread = strtoull(p, (char **)&p, 16);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001879 env = find_cpu(thread);
1880
1881 if (env != NULL) {
1882 put_packet(s, "OK");
1883 } else {
aliguori880a7572008-11-18 20:30:24 +00001884 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001885 }
aliguori880a7572008-11-18 20:30:24 +00001886 break;
pbrook978efd62006-06-17 18:30:42 +00001887 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001888 case 'Q':
1889 /* parse any 'q' packets here */
1890 if (!strcmp(p,"qemu.sstepbits")) {
1891 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001892 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1893 SSTEP_ENABLE,
1894 SSTEP_NOIRQ,
1895 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001896 put_packet(s, buf);
1897 break;
1898 } else if (strncmp(p,"qemu.sstep",10) == 0) {
1899 /* Display or change the sstep_flags */
1900 p += 10;
1901 if (*p != '=') {
1902 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001903 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001904 put_packet(s, buf);
1905 break;
1906 }
1907 p++;
1908 type = strtoul(p, (char **)&p, 16);
1909 sstep_flags = type;
1910 put_packet(s, "OK");
1911 break;
aliguori880a7572008-11-18 20:30:24 +00001912 } else if (strcmp(p,"C") == 0) {
1913 /* "Current thread" remains vague in the spec, so always return
1914 * the first CPU (gdb returns the first thread). */
1915 put_packet(s, "QC1");
1916 break;
1917 } else if (strcmp(p,"fThreadInfo") == 0) {
1918 s->query_cpu = first_cpu;
1919 goto report_cpuinfo;
1920 } else if (strcmp(p,"sThreadInfo") == 0) {
1921 report_cpuinfo:
1922 if (s->query_cpu) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001923 snprintf(buf, sizeof(buf), "m%x", gdb_id(s->query_cpu));
aliguori880a7572008-11-18 20:30:24 +00001924 put_packet(s, buf);
1925 s->query_cpu = s->query_cpu->next_cpu;
1926 } else
1927 put_packet(s, "l");
1928 break;
1929 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1930 thread = strtoull(p+16, (char **)&p, 16);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001931 env = find_cpu(thread);
1932 if (env != NULL) {
1933 cpu_synchronize_state(env, 0);
1934 len = snprintf((char *)mem_buf, sizeof(mem_buf),
1935 "CPU#%d [%s]", env->cpu_index,
1936 env->halted ? "halted " : "running");
1937 memtohex(buf, mem_buf, len);
1938 put_packet(s, buf);
1939 }
aliguori880a7572008-11-18 20:30:24 +00001940 break;
edgar_igl60897d32008-05-09 08:25:14 +00001941 }
blueswir10b8a9882009-03-07 10:51:36 +00001942#ifdef CONFIG_USER_ONLY
edgar_igl60897d32008-05-09 08:25:14 +00001943 else if (strncmp(p, "Offsets", 7) == 0) {
aliguori880a7572008-11-18 20:30:24 +00001944 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001945
blueswir1363a37d2008-08-21 17:58:08 +00001946 snprintf(buf, sizeof(buf),
1947 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1948 ";Bss=" TARGET_ABI_FMT_lx,
1949 ts->info->code_offset,
1950 ts->info->data_offset,
1951 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001952 put_packet(s, buf);
1953 break;
1954 }
blueswir10b8a9882009-03-07 10:51:36 +00001955#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001956 else if (strncmp(p, "Rcmd,", 5) == 0) {
1957 int len = strlen(p + 5);
1958
1959 if ((len % 2) != 0) {
1960 put_packet(s, "E01");
1961 break;
1962 }
1963 hextomem(mem_buf, p + 5, len);
1964 len = len / 2;
1965 mem_buf[len++] = 0;
1966 qemu_chr_read(s->mon_chr, mem_buf, len);
1967 put_packet(s, "OK");
1968 break;
1969 }
blueswir10b8a9882009-03-07 10:51:36 +00001970#endif /* !CONFIG_USER_ONLY */
pbrook56aebc82008-10-11 17:55:29 +00001971 if (strncmp(p, "Supported", 9) == 0) {
blueswir15b3715b2008-10-25 11:18:12 +00001972 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
pbrook56aebc82008-10-11 17:55:29 +00001973#ifdef GDB_CORE_XML
blueswir12dc766d2009-04-13 16:06:19 +00001974 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
pbrook56aebc82008-10-11 17:55:29 +00001975#endif
1976 put_packet(s, buf);
1977 break;
1978 }
1979#ifdef GDB_CORE_XML
1980 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1981 const char *xml;
1982 target_ulong total_len;
1983
1984 gdb_has_xml = 1;
1985 p += 19;
aliguori880a7572008-11-18 20:30:24 +00001986 xml = get_feature_xml(p, &p);
pbrook56aebc82008-10-11 17:55:29 +00001987 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001988 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001989 put_packet(s, buf);
1990 break;
1991 }
1992
1993 if (*p == ':')
1994 p++;
1995 addr = strtoul(p, (char **)&p, 16);
1996 if (*p == ',')
1997 p++;
1998 len = strtoul(p, (char **)&p, 16);
1999
2000 total_len = strlen(xml);
2001 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00002002 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00002003 put_packet(s, buf);
2004 break;
2005 }
2006 if (len > (MAX_PACKET_LENGTH - 5) / 2)
2007 len = (MAX_PACKET_LENGTH - 5) / 2;
2008 if (len < total_len - addr) {
2009 buf[0] = 'm';
2010 len = memtox(buf + 1, xml + addr, len);
2011 } else {
2012 buf[0] = 'l';
2013 len = memtox(buf + 1, xml + addr, total_len - addr);
2014 }
2015 put_packet_binary(s, buf, len + 1);
2016 break;
2017 }
2018#endif
2019 /* Unrecognised 'q' command. */
2020 goto unknown_command;
2021
bellard858693c2004-03-31 18:52:07 +00002022 default:
pbrook56aebc82008-10-11 17:55:29 +00002023 unknown_command:
bellard858693c2004-03-31 18:52:07 +00002024 /* put empty packet */
2025 buf[0] = '\0';
2026 put_packet(s, buf);
2027 break;
2028 }
2029 return RS_IDLE;
2030}
2031
aliguori880a7572008-11-18 20:30:24 +00002032void gdb_set_stop_cpu(CPUState *env)
2033{
2034 gdbserver_state->c_cpu = env;
2035 gdbserver_state->g_cpu = env;
2036}
2037
bellard1fddef42005-04-17 19:16:13 +00002038#ifndef CONFIG_USER_ONLY
aliguori9781e042009-01-22 17:15:29 +00002039static void gdb_vm_state_change(void *opaque, int running, int reason)
bellard858693c2004-03-31 18:52:07 +00002040{
aliguori880a7572008-11-18 20:30:24 +00002041 GDBState *s = gdbserver_state;
2042 CPUState *env = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00002043 char buf[256];
aliguorid6fc1b32008-11-18 19:55:44 +00002044 const char *type;
bellard858693c2004-03-31 18:52:07 +00002045 int ret;
2046
aliguori9781e042009-01-22 17:15:29 +00002047 if (running || (reason != EXCP_DEBUG && reason != EXCP_INTERRUPT) ||
aliguori36556b22009-03-28 18:05:53 +00002048 s->state == RS_INACTIVE || s->state == RS_SYSCALL)
pbrooka2d1eba2007-01-28 03:10:55 +00002049 return;
2050
bellard858693c2004-03-31 18:52:07 +00002051 /* disable single step if it was enable */
aliguori880a7572008-11-18 20:30:24 +00002052 cpu_single_step(env, 0);
bellard858693c2004-03-31 18:52:07 +00002053
bellarde80cfcf2004-12-19 23:18:01 +00002054 if (reason == EXCP_DEBUG) {
aliguori880a7572008-11-18 20:30:24 +00002055 if (env->watchpoint_hit) {
2056 switch (env->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00002057 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00002058 type = "r";
2059 break;
aliguoria1d1bb32008-11-18 20:07:32 +00002060 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00002061 type = "a";
2062 break;
2063 default:
2064 type = "";
2065 break;
2066 }
aliguori880a7572008-11-18 20:30:24 +00002067 snprintf(buf, sizeof(buf),
2068 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
Nathan Froyd1e9fa732009-06-03 11:33:08 -07002069 GDB_SIGNAL_TRAP, gdb_id(env), type,
aliguori880a7572008-11-18 20:30:24 +00002070 env->watchpoint_hit->vaddr);
pbrook6658ffb2007-03-16 23:58:11 +00002071 put_packet(s, buf);
aliguori880a7572008-11-18 20:30:24 +00002072 env->watchpoint_hit = NULL;
pbrook6658ffb2007-03-16 23:58:11 +00002073 return;
2074 }
aliguori880a7572008-11-18 20:30:24 +00002075 tb_flush(env);
aurel32ca587a82008-12-18 22:44:13 +00002076 ret = GDB_SIGNAL_TRAP;
bellardbbeb7b52006-04-23 18:42:15 +00002077 } else {
aliguori9781e042009-01-22 17:15:29 +00002078 ret = GDB_SIGNAL_INT;
bellardbbeb7b52006-04-23 18:42:15 +00002079 }
Nathan Froyd1e9fa732009-06-03 11:33:08 -07002080 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, gdb_id(env));
bellard858693c2004-03-31 18:52:07 +00002081 put_packet(s, buf);
2082}
bellard1fddef42005-04-17 19:16:13 +00002083#endif
bellard858693c2004-03-31 18:52:07 +00002084
pbrooka2d1eba2007-01-28 03:10:55 +00002085/* Send a gdb syscall request.
2086 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00002087 %x - target_ulong argument printed in hex.
2088 %lx - 64-bit argument printed in hex.
2089 %s - string pointer (target_ulong) and length (int) pair. */
blueswir17ccfb2e2008-09-14 06:45:34 +00002090void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
pbrooka2d1eba2007-01-28 03:10:55 +00002091{
2092 va_list va;
2093 char buf[256];
2094 char *p;
2095 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00002096 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00002097 GDBState *s;
2098
aliguori880a7572008-11-18 20:30:24 +00002099 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00002100 if (!s)
2101 return;
2102 gdb_current_syscall_cb = cb;
2103 s->state = RS_SYSCALL;
2104#ifndef CONFIG_USER_ONLY
2105 vm_stop(EXCP_DEBUG);
2106#endif
2107 s->state = RS_IDLE;
2108 va_start(va, fmt);
2109 p = buf;
2110 *(p++) = 'F';
2111 while (*fmt) {
2112 if (*fmt == '%') {
2113 fmt++;
2114 switch (*fmt++) {
2115 case 'x':
2116 addr = va_arg(va, target_ulong);
blueswir1363a37d2008-08-21 17:58:08 +00002117 p += snprintf(p, &buf[sizeof(buf)] - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00002118 break;
pbrooka87295e2007-05-26 15:09:38 +00002119 case 'l':
2120 if (*(fmt++) != 'x')
2121 goto bad_format;
2122 i64 = va_arg(va, uint64_t);
blueswir1363a37d2008-08-21 17:58:08 +00002123 p += snprintf(p, &buf[sizeof(buf)] - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00002124 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002125 case 's':
2126 addr = va_arg(va, target_ulong);
blueswir1363a37d2008-08-21 17:58:08 +00002127 p += snprintf(p, &buf[sizeof(buf)] - p, TARGET_FMT_lx "/%x",
2128 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00002129 break;
2130 default:
pbrooka87295e2007-05-26 15:09:38 +00002131 bad_format:
pbrooka2d1eba2007-01-28 03:10:55 +00002132 fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
2133 fmt - 1);
2134 break;
2135 }
2136 } else {
2137 *(p++) = *(fmt++);
2138 }
2139 }
pbrook8a93e022007-08-06 13:19:15 +00002140 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00002141 va_end(va);
2142 put_packet(s, buf);
2143#ifdef CONFIG_USER_ONLY
aliguori880a7572008-11-18 20:30:24 +00002144 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00002145#else
aurel323098dba2009-03-07 21:28:24 +00002146 cpu_exit(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00002147#endif
2148}
2149
bellard6a00d602005-11-21 23:25:50 +00002150static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00002151{
2152 int i, csum;
ths60fe76f2007-12-16 03:02:09 +00002153 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00002154
bellard1fddef42005-04-17 19:16:13 +00002155#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00002156 if (s->last_packet_len) {
2157 /* Waiting for a response to the last packet. If we see the start
2158 of a new command then abandon the previous response. */
2159 if (ch == '-') {
2160#ifdef DEBUG_GDB
2161 printf("Got NACK, retransmitting\n");
2162#endif
thsffe8ab82007-12-16 03:16:05 +00002163 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
pbrook4046d912007-01-28 01:53:16 +00002164 }
2165#ifdef DEBUG_GDB
2166 else if (ch == '+')
2167 printf("Got ACK\n");
2168 else
2169 printf("Got '%c' when expecting ACK/NACK\n", ch);
2170#endif
2171 if (ch == '+' || ch == '$')
2172 s->last_packet_len = 0;
2173 if (ch != '$')
2174 return;
2175 }
bellard858693c2004-03-31 18:52:07 +00002176 if (vm_running) {
2177 /* when the CPU is running, we cannot do anything except stop
2178 it when receiving a char */
2179 vm_stop(EXCP_INTERRUPT);
ths5fafdf22007-09-16 21:08:06 +00002180 } else
bellard1fddef42005-04-17 19:16:13 +00002181#endif
bellard41625032005-04-24 10:07:11 +00002182 {
bellard858693c2004-03-31 18:52:07 +00002183 switch(s->state) {
2184 case RS_IDLE:
2185 if (ch == '$') {
2186 s->line_buf_index = 0;
2187 s->state = RS_GETLINE;
bellard4c3a88a2003-07-26 12:06:08 +00002188 }
2189 break;
bellard858693c2004-03-31 18:52:07 +00002190 case RS_GETLINE:
2191 if (ch == '#') {
2192 s->state = RS_CHKSUM1;
2193 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2194 s->state = RS_IDLE;
2195 } else {
2196 s->line_buf[s->line_buf_index++] = ch;
2197 }
2198 break;
2199 case RS_CHKSUM1:
2200 s->line_buf[s->line_buf_index] = '\0';
2201 s->line_csum = fromhex(ch) << 4;
2202 s->state = RS_CHKSUM2;
2203 break;
2204 case RS_CHKSUM2:
2205 s->line_csum |= fromhex(ch);
2206 csum = 0;
2207 for(i = 0; i < s->line_buf_index; i++) {
2208 csum += s->line_buf[i];
2209 }
2210 if (s->line_csum != (csum & 0xff)) {
ths60fe76f2007-12-16 03:02:09 +00002211 reply = '-';
2212 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00002213 s->state = RS_IDLE;
2214 } else {
ths60fe76f2007-12-16 03:02:09 +00002215 reply = '+';
2216 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00002217 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00002218 }
bellardb4608c02003-06-27 17:34:32 +00002219 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002220 default:
2221 abort();
bellardb4608c02003-06-27 17:34:32 +00002222 }
2223 }
bellard858693c2004-03-31 18:52:07 +00002224}
2225
bellard1fddef42005-04-17 19:16:13 +00002226#ifdef CONFIG_USER_ONLY
2227int
aurel32ca587a82008-12-18 22:44:13 +00002228gdb_queuesig (void)
2229{
2230 GDBState *s;
2231
2232 s = gdbserver_state;
2233
2234 if (gdbserver_fd < 0 || s->fd < 0)
2235 return 0;
2236 else
2237 return 1;
2238}
2239
2240int
bellard1fddef42005-04-17 19:16:13 +00002241gdb_handlesig (CPUState *env, int sig)
2242{
2243 GDBState *s;
2244 char buf[256];
2245 int n;
2246
aliguori880a7572008-11-18 20:30:24 +00002247 s = gdbserver_state;
edgar_igl1f487ee2008-05-17 22:20:53 +00002248 if (gdbserver_fd < 0 || s->fd < 0)
2249 return sig;
bellard1fddef42005-04-17 19:16:13 +00002250
2251 /* disable single step if it was enabled */
2252 cpu_single_step(env, 0);
2253 tb_flush(env);
2254
2255 if (sig != 0)
2256 {
aurel32ca587a82008-12-18 22:44:13 +00002257 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb (sig));
bellard1fddef42005-04-17 19:16:13 +00002258 put_packet(s, buf);
2259 }
edgar_igl1f487ee2008-05-17 22:20:53 +00002260 /* put_packet() might have detected that the peer terminated the
2261 connection. */
2262 if (s->fd < 0)
2263 return sig;
bellard1fddef42005-04-17 19:16:13 +00002264
bellard1fddef42005-04-17 19:16:13 +00002265 sig = 0;
2266 s->state = RS_IDLE;
bellard41625032005-04-24 10:07:11 +00002267 s->running_state = 0;
2268 while (s->running_state == 0) {
bellard1fddef42005-04-17 19:16:13 +00002269 n = read (s->fd, buf, 256);
2270 if (n > 0)
2271 {
2272 int i;
2273
2274 for (i = 0; i < n; i++)
bellard6a00d602005-11-21 23:25:50 +00002275 gdb_read_byte (s, buf[i]);
bellard1fddef42005-04-17 19:16:13 +00002276 }
2277 else if (n == 0 || errno != EAGAIN)
2278 {
2279 /* XXX: Connection closed. Should probably wait for annother
2280 connection before continuing. */
2281 return sig;
2282 }
bellard41625032005-04-24 10:07:11 +00002283 }
edgar_igl1f487ee2008-05-17 22:20:53 +00002284 sig = s->signal;
2285 s->signal = 0;
bellard1fddef42005-04-17 19:16:13 +00002286 return sig;
2287}
bellarde9009672005-04-26 20:42:36 +00002288
2289/* Tell the remote gdb that the process has exited. */
2290void gdb_exit(CPUState *env, int code)
2291{
2292 GDBState *s;
2293 char buf[4];
2294
aliguori880a7572008-11-18 20:30:24 +00002295 s = gdbserver_state;
edgar_igl1f487ee2008-05-17 22:20:53 +00002296 if (gdbserver_fd < 0 || s->fd < 0)
2297 return;
bellarde9009672005-04-26 20:42:36 +00002298
2299 snprintf(buf, sizeof(buf), "W%02x", code);
2300 put_packet(s, buf);
2301}
2302
aurel32ca587a82008-12-18 22:44:13 +00002303/* Tell the remote gdb that the process has exited due to SIG. */
2304void gdb_signalled(CPUState *env, int sig)
2305{
2306 GDBState *s;
2307 char buf[4];
2308
2309 s = gdbserver_state;
2310 if (gdbserver_fd < 0 || s->fd < 0)
2311 return;
2312
2313 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb (sig));
2314 put_packet(s, buf);
2315}
bellard1fddef42005-04-17 19:16:13 +00002316
aliguori880a7572008-11-18 20:30:24 +00002317static void gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00002318{
2319 GDBState *s;
2320 struct sockaddr_in sockaddr;
2321 socklen_t len;
2322 int val, fd;
2323
2324 for(;;) {
2325 len = sizeof(sockaddr);
2326 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
2327 if (fd < 0 && errno != EINTR) {
2328 perror("accept");
2329 return;
2330 } else if (fd >= 0) {
2331 break;
2332 }
2333 }
2334
2335 /* set short latency */
2336 val = 1;
bellard8f447cc2006-06-14 15:21:14 +00002337 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
ths3b46e622007-09-17 08:09:54 +00002338
aliguori880a7572008-11-18 20:30:24 +00002339 s = qemu_mallocz(sizeof(GDBState));
aliguori880a7572008-11-18 20:30:24 +00002340 s->c_cpu = first_cpu;
2341 s->g_cpu = first_cpu;
bellard858693c2004-03-31 18:52:07 +00002342 s->fd = fd;
pbrook56aebc82008-10-11 17:55:29 +00002343 gdb_has_xml = 0;
bellard858693c2004-03-31 18:52:07 +00002344
aliguori880a7572008-11-18 20:30:24 +00002345 gdbserver_state = s;
pbrooka2d1eba2007-01-28 03:10:55 +00002346
bellard858693c2004-03-31 18:52:07 +00002347 fcntl(fd, F_SETFL, O_NONBLOCK);
bellard858693c2004-03-31 18:52:07 +00002348}
2349
2350static int gdbserver_open(int port)
2351{
2352 struct sockaddr_in sockaddr;
2353 int fd, val, ret;
2354
2355 fd = socket(PF_INET, SOCK_STREAM, 0);
2356 if (fd < 0) {
2357 perror("socket");
2358 return -1;
2359 }
2360
2361 /* allow fast reuse */
2362 val = 1;
bellard8f447cc2006-06-14 15:21:14 +00002363 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
bellard858693c2004-03-31 18:52:07 +00002364
2365 sockaddr.sin_family = AF_INET;
2366 sockaddr.sin_port = htons(port);
2367 sockaddr.sin_addr.s_addr = 0;
2368 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
2369 if (ret < 0) {
2370 perror("bind");
2371 return -1;
2372 }
2373 ret = listen(fd, 0);
2374 if (ret < 0) {
2375 perror("listen");
2376 return -1;
2377 }
bellard858693c2004-03-31 18:52:07 +00002378 return fd;
2379}
2380
2381int gdbserver_start(int port)
2382{
2383 gdbserver_fd = gdbserver_open(port);
2384 if (gdbserver_fd < 0)
2385 return -1;
2386 /* accept connections */
aliguori880a7572008-11-18 20:30:24 +00002387 gdb_accept();
bellardb4608c02003-06-27 17:34:32 +00002388 return 0;
2389}
aurel322b1319c2008-12-18 22:44:04 +00002390
2391/* Disable gdb stub for child processes. */
2392void gdbserver_fork(CPUState *env)
2393{
2394 GDBState *s = gdbserver_state;
edgar_igl9f6164d2009-01-07 10:22:28 +00002395 if (gdbserver_fd < 0 || s->fd < 0)
aurel322b1319c2008-12-18 22:44:04 +00002396 return;
2397 close(s->fd);
2398 s->fd = -1;
2399 cpu_breakpoint_remove_all(env, BP_GDB);
2400 cpu_watchpoint_remove_all(env, BP_GDB);
2401}
pbrook4046d912007-01-28 01:53:16 +00002402#else
thsaa1f17c2007-07-11 22:48:58 +00002403static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00002404{
pbrook56aebc82008-10-11 17:55:29 +00002405 /* We can handle an arbitrarily large amount of data.
2406 Pick the maximum packet size, which is as good as anything. */
2407 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00002408}
2409
thsaa1f17c2007-07-11 22:48:58 +00002410static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00002411{
pbrook4046d912007-01-28 01:53:16 +00002412 int i;
2413
2414 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00002415 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00002416 }
2417}
2418
2419static void gdb_chr_event(void *opaque, int event)
2420{
2421 switch (event) {
2422 case CHR_EVENT_RESET:
2423 vm_stop(EXCP_INTERRUPT);
pbrook56aebc82008-10-11 17:55:29 +00002424 gdb_has_xml = 0;
pbrook4046d912007-01-28 01:53:16 +00002425 break;
2426 default:
2427 break;
2428 }
2429}
2430
aliguori8a34a0f2009-03-05 23:01:55 +00002431static void gdb_monitor_output(GDBState *s, const char *msg, int len)
2432{
2433 char buf[MAX_PACKET_LENGTH];
2434
2435 buf[0] = 'O';
2436 if (len > (MAX_PACKET_LENGTH/2) - 1)
2437 len = (MAX_PACKET_LENGTH/2) - 1;
2438 memtohex(buf + 1, (uint8_t *)msg, len);
2439 put_packet(s, buf);
2440}
2441
2442static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
2443{
2444 const char *p = (const char *)buf;
2445 int max_sz;
2446
2447 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
2448 for (;;) {
2449 if (len <= max_sz) {
2450 gdb_monitor_output(gdbserver_state, p, len);
2451 break;
2452 }
2453 gdb_monitor_output(gdbserver_state, p, max_sz);
2454 p += max_sz;
2455 len -= max_sz;
2456 }
2457 return len;
2458}
2459
aliguori59030a82009-04-05 18:43:41 +00002460#ifndef _WIN32
2461static void gdb_sigterm_handler(int signal)
2462{
2463 if (vm_running)
2464 vm_stop(EXCP_INTERRUPT);
2465}
2466#endif
2467
2468int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00002469{
2470 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00002471 char gdbstub_device_name[128];
aliguori36556b22009-03-28 18:05:53 +00002472 CharDriverState *chr = NULL;
2473 CharDriverState *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00002474
aliguori59030a82009-04-05 18:43:41 +00002475 if (!device)
2476 return -1;
2477 if (strcmp(device, "none") != 0) {
2478 if (strstart(device, "tcp:", NULL)) {
2479 /* enforce required TCP attributes */
2480 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
2481 "%s,nowait,nodelay,server", device);
2482 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00002483 }
aliguori59030a82009-04-05 18:43:41 +00002484#ifndef _WIN32
2485 else if (strcmp(device, "stdio") == 0) {
2486 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00002487
aliguori59030a82009-04-05 18:43:41 +00002488 memset(&act, 0, sizeof(act));
2489 act.sa_handler = gdb_sigterm_handler;
2490 sigaction(SIGINT, &act, NULL);
2491 }
2492#endif
2493 chr = qemu_chr_open("gdb", device, NULL);
aliguori36556b22009-03-28 18:05:53 +00002494 if (!chr)
2495 return -1;
2496
2497 qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
2498 gdb_chr_event, NULL);
pbrookcfc34752007-02-22 01:48:01 +00002499 }
2500
aliguori36556b22009-03-28 18:05:53 +00002501 s = gdbserver_state;
2502 if (!s) {
2503 s = qemu_mallocz(sizeof(GDBState));
2504 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00002505
aliguori36556b22009-03-28 18:05:53 +00002506 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
2507
2508 /* Initialize a monitor terminal for gdb */
2509 mon_chr = qemu_mallocz(sizeof(*mon_chr));
2510 mon_chr->chr_write = gdb_monitor_write;
2511 monitor_init(mon_chr, 0);
2512 } else {
2513 if (s->chr)
2514 qemu_chr_close(s->chr);
2515 mon_chr = s->mon_chr;
2516 memset(s, 0, sizeof(GDBState));
2517 }
aliguori880a7572008-11-18 20:30:24 +00002518 s->c_cpu = first_cpu;
2519 s->g_cpu = first_cpu;
pbrook4046d912007-01-28 01:53:16 +00002520 s->chr = chr;
aliguori36556b22009-03-28 18:05:53 +00002521 s->state = chr ? RS_IDLE : RS_INACTIVE;
2522 s->mon_chr = mon_chr;
aliguori8a34a0f2009-03-05 23:01:55 +00002523
pbrook4046d912007-01-28 01:53:16 +00002524 return 0;
2525}
2526#endif