blob: 518c939f452d4b318c73ca6a9b3474c8dc81b374 [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
337enum {
338 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
514static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
515{
516 if (n < CPU_NB_REGS) {
517 GET_REGL(env->regs[gpr_map[n]]);
518 } else if (n >= CPU_NB_REGS + 8 && n < CPU_NB_REGS + 16) {
519 /* FIXME: byteswap float values. */
520#ifdef USE_X86LDOUBLE
521 memcpy(mem_buf, &env->fpregs[n - (CPU_NB_REGS + 8)], 10);
522#else
523 memset(mem_buf, 0, 10);
524#endif
525 return 10;
526 } else if (n >= CPU_NB_REGS + 24) {
527 n -= CPU_NB_REGS + 24;
528 if (n < CPU_NB_REGS) {
529 stq_p(mem_buf, env->xmm_regs[n].XMM_Q(0));
530 stq_p(mem_buf + 8, env->xmm_regs[n].XMM_Q(1));
531 return 16;
532 } else if (n == CPU_NB_REGS) {
533 GET_REG32(env->mxcsr);
534 }
535 } else {
536 n -= CPU_NB_REGS;
537 switch (n) {
538 case 0: GET_REGL(env->eip);
539 case 1: GET_REG32(env->eflags);
540 case 2: GET_REG32(env->segs[R_CS].selector);
541 case 3: GET_REG32(env->segs[R_SS].selector);
542 case 4: GET_REG32(env->segs[R_DS].selector);
543 case 5: GET_REG32(env->segs[R_ES].selector);
544 case 6: GET_REG32(env->segs[R_FS].selector);
545 case 7: GET_REG32(env->segs[R_GS].selector);
546 /* 8...15 x87 regs. */
547 case 16: GET_REG32(env->fpuc);
548 case 17: GET_REG32((env->fpus & ~0x3800) | (env->fpstt & 0x7) << 11);
549 case 18: GET_REG32(0); /* ftag */
550 case 19: GET_REG32(0); /* fiseg */
551 case 20: GET_REG32(0); /* fioff */
552 case 21: GET_REG32(0); /* foseg */
553 case 22: GET_REG32(0); /* fooff */
554 case 23: GET_REG32(0); /* fop */
555 /* 24+ xmm regs. */
556 }
bellard79808572008-05-09 14:40:22 +0000557 }
pbrook56aebc82008-10-11 17:55:29 +0000558 return 0;
bellard79808572008-05-09 14:40:22 +0000559}
560
pbrook56aebc82008-10-11 17:55:29 +0000561static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int i)
bellard79808572008-05-09 14:40:22 +0000562{
pbrook56aebc82008-10-11 17:55:29 +0000563 uint32_t tmp;
564
565 if (i < CPU_NB_REGS) {
566 env->regs[gpr_map[i]] = ldtul_p(mem_buf);
567 return sizeof(target_ulong);
568 } else if (i >= CPU_NB_REGS + 8 && i < CPU_NB_REGS + 16) {
569 i -= CPU_NB_REGS + 8;
570#ifdef USE_X86LDOUBLE
571 memcpy(&env->fpregs[i], mem_buf, 10);
572#endif
573 return 10;
574 } else if (i >= CPU_NB_REGS + 24) {
575 i -= CPU_NB_REGS + 24;
576 if (i < CPU_NB_REGS) {
577 env->xmm_regs[i].XMM_Q(0) = ldq_p(mem_buf);
578 env->xmm_regs[i].XMM_Q(1) = ldq_p(mem_buf + 8);
579 return 16;
580 } else if (i == CPU_NB_REGS) {
581 env->mxcsr = ldl_p(mem_buf);
582 return 4;
583 }
584 } else {
585 i -= CPU_NB_REGS;
586 switch (i) {
587 case 0: env->eip = ldtul_p(mem_buf); return sizeof(target_ulong);
588 case 1: env->eflags = ldl_p(mem_buf); return 4;
bellard79808572008-05-09 14:40:22 +0000589#if defined(CONFIG_USER_ONLY)
pbrook56aebc82008-10-11 17:55:29 +0000590#define LOAD_SEG(index, sreg)\
591 tmp = ldl_p(mem_buf);\
592 if (tmp != env->segs[sreg].selector)\
593 cpu_x86_load_seg(env, sreg, tmp);
bellard79808572008-05-09 14:40:22 +0000594#else
pbrook56aebc82008-10-11 17:55:29 +0000595/* FIXME: Honor segment registers. Needs to avoid raising an exception
596 when the selector is invalid. */
597#define LOAD_SEG(index, sreg) do {} while(0)
bellard79808572008-05-09 14:40:22 +0000598#endif
pbrook56aebc82008-10-11 17:55:29 +0000599 case 2: LOAD_SEG(10, R_CS); return 4;
600 case 3: LOAD_SEG(11, R_SS); return 4;
601 case 4: LOAD_SEG(12, R_DS); return 4;
602 case 5: LOAD_SEG(13, R_ES); return 4;
603 case 6: LOAD_SEG(14, R_FS); return 4;
604 case 7: LOAD_SEG(15, R_GS); return 4;
605 /* 8...15 x87 regs. */
606 case 16: env->fpuc = ldl_p(mem_buf); return 4;
607 case 17:
608 tmp = ldl_p(mem_buf);
609 env->fpstt = (tmp >> 11) & 7;
610 env->fpus = tmp & ~0x3800;
611 return 4;
612 case 18: /* ftag */ return 4;
613 case 19: /* fiseg */ return 4;
614 case 20: /* fioff */ return 4;
615 case 21: /* foseg */ return 4;
616 case 22: /* fooff */ return 4;
617 case 23: /* fop */ return 4;
618 /* 24+ xmm regs. */
bellard79808572008-05-09 14:40:22 +0000619 }
bellard79808572008-05-09 14:40:22 +0000620 }
pbrook56aebc82008-10-11 17:55:29 +0000621 /* Unrecognised register. */
622 return 0;
bellard6da41ea2004-01-04 15:48:38 +0000623}
624
bellard9e62fd72004-01-05 22:49:06 +0000625#elif defined (TARGET_PPC)
pbrook56aebc82008-10-11 17:55:29 +0000626
aurel32e571cb42009-01-24 15:07:42 +0000627/* Old gdb always expects FP registers. Newer (xml-aware) gdb only
628 expects whatever the target description contains. Due to a
629 historical mishap the FP registers appear in between core integer
630 regs and PC, MSR, CR, and so forth. We hack round this by giving the
631 FP regs zero size when talking to a newer gdb. */
pbrook56aebc82008-10-11 17:55:29 +0000632#define NUM_CORE_REGS 71
aurel32e571cb42009-01-24 15:07:42 +0000633#if defined (TARGET_PPC64)
634#define GDB_CORE_XML "power64-core.xml"
635#else
636#define GDB_CORE_XML "power-core.xml"
637#endif
pbrook56aebc82008-10-11 17:55:29 +0000638
639static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
bellard9e62fd72004-01-05 22:49:06 +0000640{
pbrook56aebc82008-10-11 17:55:29 +0000641 if (n < 32) {
642 /* gprs */
643 GET_REGL(env->gpr[n]);
644 } else if (n < 64) {
645 /* fprs */
aurel32e571cb42009-01-24 15:07:42 +0000646 if (gdb_has_xml)
647 return 0;
aurel328d4acf92008-11-30 16:23:18 +0000648 stfq_p(mem_buf, env->fpr[n-32]);
pbrook56aebc82008-10-11 17:55:29 +0000649 return 8;
650 } else {
651 switch (n) {
652 case 64: GET_REGL(env->nip);
653 case 65: GET_REGL(env->msr);
654 case 66:
655 {
656 uint32_t cr = 0;
657 int i;
658 for (i = 0; i < 8; i++)
659 cr |= env->crf[i] << (32 - ((i + 1) * 4));
660 GET_REG32(cr);
661 }
662 case 67: GET_REGL(env->lr);
663 case 68: GET_REGL(env->ctr);
aurel323d7b4172008-10-21 11:28:46 +0000664 case 69: GET_REGL(env->xer);
aurel32e571cb42009-01-24 15:07:42 +0000665 case 70:
666 {
667 if (gdb_has_xml)
668 return 0;
669 GET_REG32(0); /* fpscr */
670 }
pbrook56aebc82008-10-11 17:55:29 +0000671 }
bellard9e62fd72004-01-05 22:49:06 +0000672 }
pbrook56aebc82008-10-11 17:55:29 +0000673 return 0;
bellard9e62fd72004-01-05 22:49:06 +0000674}
675
pbrook56aebc82008-10-11 17:55:29 +0000676static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellard9e62fd72004-01-05 22:49:06 +0000677{
pbrook56aebc82008-10-11 17:55:29 +0000678 if (n < 32) {
679 /* gprs */
680 env->gpr[n] = ldtul_p(mem_buf);
681 return sizeof(target_ulong);
682 } else if (n < 64) {
683 /* fprs */
aurel32e571cb42009-01-24 15:07:42 +0000684 if (gdb_has_xml)
685 return 0;
aurel328d4acf92008-11-30 16:23:18 +0000686 env->fpr[n-32] = ldfq_p(mem_buf);
pbrook56aebc82008-10-11 17:55:29 +0000687 return 8;
688 } else {
689 switch (n) {
690 case 64:
691 env->nip = ldtul_p(mem_buf);
692 return sizeof(target_ulong);
693 case 65:
694 ppc_store_msr(env, ldtul_p(mem_buf));
695 return sizeof(target_ulong);
696 case 66:
697 {
698 uint32_t cr = ldl_p(mem_buf);
699 int i;
700 for (i = 0; i < 8; i++)
701 env->crf[i] = (cr >> (32 - ((i + 1) * 4))) & 0xF;
702 return 4;
703 }
704 case 67:
705 env->lr = ldtul_p(mem_buf);
706 return sizeof(target_ulong);
707 case 68:
708 env->ctr = ldtul_p(mem_buf);
709 return sizeof(target_ulong);
710 case 69:
aurel323d7b4172008-10-21 11:28:46 +0000711 env->xer = ldtul_p(mem_buf);
712 return sizeof(target_ulong);
pbrook56aebc82008-10-11 17:55:29 +0000713 case 70:
714 /* fpscr */
aurel32e571cb42009-01-24 15:07:42 +0000715 if (gdb_has_xml)
716 return 0;
pbrook56aebc82008-10-11 17:55:29 +0000717 return 4;
718 }
bellard9e62fd72004-01-05 22:49:06 +0000719 }
pbrook56aebc82008-10-11 17:55:29 +0000720 return 0;
bellarde95c8d52004-09-30 22:22:08 +0000721}
pbrook56aebc82008-10-11 17:55:29 +0000722
bellarde95c8d52004-09-30 22:22:08 +0000723#elif defined (TARGET_SPARC)
bellarde95c8d52004-09-30 22:22:08 +0000724
pbrook56aebc82008-10-11 17:55:29 +0000725#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
726#define NUM_CORE_REGS 86
727#else
blueswir15a377912009-01-13 16:28:01 +0000728#define NUM_CORE_REGS 72
pbrook56aebc82008-10-11 17:55:29 +0000729#endif
730
731#ifdef TARGET_ABI32
732#define GET_REGA(val) GET_REG32(val)
733#else
734#define GET_REGA(val) GET_REGL(val)
735#endif
736
737static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
738{
739 if (n < 8) {
740 /* g0..g7 */
741 GET_REGA(env->gregs[n]);
bellarde95c8d52004-09-30 22:22:08 +0000742 }
pbrook56aebc82008-10-11 17:55:29 +0000743 if (n < 32) {
744 /* register window */
745 GET_REGA(env->regwptr[n - 8]);
bellarde95c8d52004-09-30 22:22:08 +0000746 }
pbrook56aebc82008-10-11 17:55:29 +0000747#if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
748 if (n < 64) {
749 /* fprs */
750 GET_REG32(*((uint32_t *)&env->fpr[n - 32]));
bellarde95c8d52004-09-30 22:22:08 +0000751 }
752 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
pbrook56aebc82008-10-11 17:55:29 +0000753 switch (n) {
754 case 64: GET_REGA(env->y);
755 case 65: GET_REGA(GET_PSR(env));
756 case 66: GET_REGA(env->wim);
757 case 67: GET_REGA(env->tbr);
758 case 68: GET_REGA(env->pc);
759 case 69: GET_REGA(env->npc);
760 case 70: GET_REGA(env->fsr);
761 case 71: GET_REGA(0); /* csr */
blueswir15a377912009-01-13 16:28:01 +0000762 default: GET_REGA(0);
bellard34751872005-07-02 14:31:34 +0000763 }
bellard34751872005-07-02 14:31:34 +0000764#else
pbrook56aebc82008-10-11 17:55:29 +0000765 if (n < 64) {
766 /* f0-f31 */
767 GET_REG32(*((uint32_t *)&env->fpr[n - 32]));
bellard34751872005-07-02 14:31:34 +0000768 }
pbrook56aebc82008-10-11 17:55:29 +0000769 if (n < 80) {
770 /* f32-f62 (double width, even numbers only) */
771 uint64_t val;
772
773 val = (uint64_t)*((uint32_t *)&env->fpr[(n - 64) * 2 + 32]) << 32;
774 val |= *((uint32_t *)&env->fpr[(n - 64) * 2 + 33]);
775 GET_REG64(val);
776 }
777 switch (n) {
778 case 80: GET_REGL(env->pc);
779 case 81: GET_REGL(env->npc);
780 case 82: GET_REGL(((uint64_t)GET_CCR(env) << 32) |
blueswir117d996e2007-07-07 20:53:22 +0000781 ((env->asi & 0xff) << 24) |
782 ((env->pstate & 0xfff) << 8) |
783 GET_CWP64(env));
pbrook56aebc82008-10-11 17:55:29 +0000784 case 83: GET_REGL(env->fsr);
785 case 84: GET_REGL(env->fprs);
786 case 85: GET_REGL(env->y);
787 }
bellard34751872005-07-02 14:31:34 +0000788#endif
pbrook56aebc82008-10-11 17:55:29 +0000789 return 0;
bellarde95c8d52004-09-30 22:22:08 +0000790}
791
pbrook56aebc82008-10-11 17:55:29 +0000792static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellarde95c8d52004-09-30 22:22:08 +0000793{
pbrook56aebc82008-10-11 17:55:29 +0000794#if defined(TARGET_ABI32)
795 abi_ulong tmp;
796
797 tmp = ldl_p(mem_buf);
blueswir196d19122008-06-07 08:03:05 +0000798#else
pbrook56aebc82008-10-11 17:55:29 +0000799 target_ulong tmp;
800
801 tmp = ldtul_p(mem_buf);
blueswir196d19122008-06-07 08:03:05 +0000802#endif
bellarde95c8d52004-09-30 22:22:08 +0000803
pbrook56aebc82008-10-11 17:55:29 +0000804 if (n < 8) {
805 /* g0..g7 */
806 env->gregs[n] = tmp;
807 } else if (n < 32) {
808 /* register window */
809 env->regwptr[n - 8] = tmp;
bellarde95c8d52004-09-30 22:22:08 +0000810 }
pbrook56aebc82008-10-11 17:55:29 +0000811#if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
812 else if (n < 64) {
813 /* fprs */
814 *((uint32_t *)&env->fpr[n - 32]) = tmp;
815 } else {
816 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
817 switch (n) {
818 case 64: env->y = tmp; break;
819 case 65: PUT_PSR(env, tmp); break;
820 case 66: env->wim = tmp; break;
821 case 67: env->tbr = tmp; break;
822 case 68: env->pc = tmp; break;
823 case 69: env->npc = tmp; break;
824 case 70: env->fsr = tmp; break;
825 default: return 0;
826 }
bellarde95c8d52004-09-30 22:22:08 +0000827 }
pbrook56aebc82008-10-11 17:55:29 +0000828 return 4;
bellard34751872005-07-02 14:31:34 +0000829#else
pbrook56aebc82008-10-11 17:55:29 +0000830 else if (n < 64) {
831 /* f0-f31 */
pbrook56aebc82008-10-11 17:55:29 +0000832 env->fpr[n] = ldfl_p(mem_buf);
833 return 4;
834 } else if (n < 80) {
835 /* f32-f62 (double width, even numbers only) */
836 *((uint32_t *)&env->fpr[(n - 64) * 2 + 32]) = tmp >> 32;
837 *((uint32_t *)&env->fpr[(n - 64) * 2 + 33]) = tmp;
838 } else {
839 switch (n) {
840 case 80: env->pc = tmp; break;
841 case 81: env->npc = tmp; break;
842 case 82:
843 PUT_CCR(env, tmp >> 32);
844 env->asi = (tmp >> 24) & 0xff;
845 env->pstate = (tmp >> 8) & 0xfff;
846 PUT_CWP64(env, tmp & 0xff);
847 break;
848 case 83: env->fsr = tmp; break;
849 case 84: env->fprs = tmp; break;
850 case 85: env->y = tmp; break;
851 default: return 0;
852 }
bellard34751872005-07-02 14:31:34 +0000853 }
pbrook56aebc82008-10-11 17:55:29 +0000854 return 8;
bellard34751872005-07-02 14:31:34 +0000855#endif
bellard9e62fd72004-01-05 22:49:06 +0000856}
bellard1fddef42005-04-17 19:16:13 +0000857#elif defined (TARGET_ARM)
pbrook56aebc82008-10-11 17:55:29 +0000858
859/* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect
860 whatever the target description contains. Due to a historical mishap
861 the FPA registers appear in between core integer regs and the CPSR.
862 We hack round this by giving the FPA regs zero size when talking to a
863 newer gdb. */
864#define NUM_CORE_REGS 26
865#define GDB_CORE_XML "arm-core.xml"
866
867static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
bellard1fddef42005-04-17 19:16:13 +0000868{
pbrook56aebc82008-10-11 17:55:29 +0000869 if (n < 16) {
870 /* Core integer register. */
871 GET_REG32(env->regs[n]);
872 }
873 if (n < 24) {
874 /* FPA registers. */
875 if (gdb_has_xml)
876 return 0;
877 memset(mem_buf, 0, 12);
878 return 12;
879 }
880 switch (n) {
881 case 24:
882 /* FPA status register. */
883 if (gdb_has_xml)
884 return 0;
885 GET_REG32(0);
886 case 25:
887 /* CPSR */
888 GET_REG32(cpsr_read(env));
889 }
890 /* Unknown register. */
891 return 0;
bellard1fddef42005-04-17 19:16:13 +0000892}
893
pbrook56aebc82008-10-11 17:55:29 +0000894static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellard1fddef42005-04-17 19:16:13 +0000895{
pbrook56aebc82008-10-11 17:55:29 +0000896 uint32_t tmp;
bellard1fddef42005-04-17 19:16:13 +0000897
pbrook56aebc82008-10-11 17:55:29 +0000898 tmp = ldl_p(mem_buf);
899
900 /* Mask out low bit of PC to workaround gdb bugs. This will probably
901 cause problems if we ever implement the Jazelle DBX extensions. */
902 if (n == 15)
903 tmp &= ~1;
904
905 if (n < 16) {
906 /* Core integer register. */
907 env->regs[n] = tmp;
908 return 4;
909 }
910 if (n < 24) { /* 16-23 */
911 /* FPA registers (ignored). */
912 if (gdb_has_xml)
913 return 0;
914 return 12;
915 }
916 switch (n) {
917 case 24:
918 /* FPA status register (ignored). */
919 if (gdb_has_xml)
920 return 0;
921 return 4;
922 case 25:
923 /* CPSR */
924 cpsr_write (env, tmp, 0xffffffff);
925 return 4;
926 }
927 /* Unknown register. */
928 return 0;
bellard1fddef42005-04-17 19:16:13 +0000929}
pbrook56aebc82008-10-11 17:55:29 +0000930
pbrooke6e59062006-10-22 00:18:54 +0000931#elif defined (TARGET_M68K)
pbrook56aebc82008-10-11 17:55:29 +0000932
933#define NUM_CORE_REGS 18
934
935#define GDB_CORE_XML "cf-core.xml"
936
937static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
pbrooke6e59062006-10-22 00:18:54 +0000938{
pbrook56aebc82008-10-11 17:55:29 +0000939 if (n < 8) {
940 /* D0-D7 */
941 GET_REG32(env->dregs[n]);
942 } else if (n < 16) {
943 /* A0-A7 */
944 GET_REG32(env->aregs[n - 8]);
945 } else {
946 switch (n) {
947 case 16: GET_REG32(env->sr);
948 case 17: GET_REG32(env->pc);
949 }
pbrooke6e59062006-10-22 00:18:54 +0000950 }
pbrook56aebc82008-10-11 17:55:29 +0000951 /* FP registers not included here because they vary between
952 ColdFire and m68k. Use XML bits for these. */
953 return 0;
pbrooke6e59062006-10-22 00:18:54 +0000954}
955
pbrook56aebc82008-10-11 17:55:29 +0000956static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
pbrooke6e59062006-10-22 00:18:54 +0000957{
pbrook56aebc82008-10-11 17:55:29 +0000958 uint32_t tmp;
pbrooke6e59062006-10-22 00:18:54 +0000959
pbrook56aebc82008-10-11 17:55:29 +0000960 tmp = ldl_p(mem_buf);
961
962 if (n < 8) {
963 /* D0-D7 */
964 env->dregs[n] = tmp;
965 } else if (n < 8) {
966 /* A0-A7 */
967 env->aregs[n - 8] = tmp;
968 } else {
969 switch (n) {
970 case 16: env->sr = tmp; break;
971 case 17: env->pc = tmp; break;
972 default: return 0;
973 }
pbrooke6e59062006-10-22 00:18:54 +0000974 }
pbrook56aebc82008-10-11 17:55:29 +0000975 return 4;
pbrooke6e59062006-10-22 00:18:54 +0000976}
bellard6f970bd2005-12-05 19:55:19 +0000977#elif defined (TARGET_MIPS)
pbrook56aebc82008-10-11 17:55:29 +0000978
979#define NUM_CORE_REGS 73
980
981static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
bellard6f970bd2005-12-05 19:55:19 +0000982{
pbrook56aebc82008-10-11 17:55:29 +0000983 if (n < 32) {
984 GET_REGL(env->active_tc.gpr[n]);
985 }
986 if (env->CP0_Config1 & (1 << CP0C1_FP)) {
987 if (n >= 38 && n < 70) {
ths7ac256b2007-10-25 21:30:37 +0000988 if (env->CP0_Status & (1 << CP0St_FR))
pbrook56aebc82008-10-11 17:55:29 +0000989 GET_REGL(env->active_fpu.fpr[n - 38].d);
ths7ac256b2007-10-25 21:30:37 +0000990 else
pbrook56aebc82008-10-11 17:55:29 +0000991 GET_REGL(env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX]);
992 }
993 switch (n) {
994 case 70: GET_REGL((int32_t)env->active_fpu.fcr31);
995 case 71: GET_REGL((int32_t)env->active_fpu.fcr0);
996 }
997 }
998 switch (n) {
999 case 32: GET_REGL((int32_t)env->CP0_Status);
1000 case 33: GET_REGL(env->active_tc.LO[0]);
1001 case 34: GET_REGL(env->active_tc.HI[0]);
1002 case 35: GET_REGL(env->CP0_BadVAddr);
1003 case 36: GET_REGL((int32_t)env->CP0_Cause);
1004 case 37: GET_REGL(env->active_tc.PC);
1005 case 72: GET_REGL(0); /* fp */
1006 case 89: GET_REGL((int32_t)env->CP0_PRid);
1007 }
1008 if (n >= 73 && n <= 88) {
1009 /* 16 embedded regs. */
1010 GET_REGL(0);
1011 }
ths36d23952007-02-28 22:37:42 +00001012
pbrook56aebc82008-10-11 17:55:29 +00001013 return 0;
bellard6f970bd2005-12-05 19:55:19 +00001014}
1015
ths8e33c082006-12-11 19:22:27 +00001016/* convert MIPS rounding mode in FCR31 to IEEE library */
1017static unsigned int ieee_rm[] =
1018 {
1019 float_round_nearest_even,
1020 float_round_to_zero,
1021 float_round_up,
1022 float_round_down
1023 };
1024#define RESTORE_ROUNDING_MODE \
thsf01be152008-09-18 11:57:27 +00001025 set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
ths8e33c082006-12-11 19:22:27 +00001026
pbrook56aebc82008-10-11 17:55:29 +00001027static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellard6f970bd2005-12-05 19:55:19 +00001028{
pbrook56aebc82008-10-11 17:55:29 +00001029 target_ulong tmp;
bellard6f970bd2005-12-05 19:55:19 +00001030
pbrook56aebc82008-10-11 17:55:29 +00001031 tmp = ldtul_p(mem_buf);
bellard6f970bd2005-12-05 19:55:19 +00001032
pbrook56aebc82008-10-11 17:55:29 +00001033 if (n < 32) {
1034 env->active_tc.gpr[n] = tmp;
1035 return sizeof(target_ulong);
1036 }
1037 if (env->CP0_Config1 & (1 << CP0C1_FP)
1038 && n >= 38 && n < 73) {
1039 if (n < 70) {
ths7ac256b2007-10-25 21:30:37 +00001040 if (env->CP0_Status & (1 << CP0St_FR))
pbrook56aebc82008-10-11 17:55:29 +00001041 env->active_fpu.fpr[n - 38].d = tmp;
ths7ac256b2007-10-25 21:30:37 +00001042 else
pbrook56aebc82008-10-11 17:55:29 +00001043 env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX] = tmp;
1044 }
1045 switch (n) {
1046 case 70:
1047 env->active_fpu.fcr31 = tmp & 0xFF83FFFF;
1048 /* set rounding mode */
1049 RESTORE_ROUNDING_MODE;
ths8e33c082006-12-11 19:22:27 +00001050#ifndef CONFIG_SOFTFLOAT
pbrook56aebc82008-10-11 17:55:29 +00001051 /* no floating point exception for native float */
1052 SET_FP_ENABLE(env->active_fpu.fcr31, 0);
ths8e33c082006-12-11 19:22:27 +00001053#endif
pbrook56aebc82008-10-11 17:55:29 +00001054 break;
1055 case 71: env->active_fpu.fcr0 = tmp; break;
1056 }
1057 return sizeof(target_ulong);
1058 }
1059 switch (n) {
1060 case 32: env->CP0_Status = tmp; break;
1061 case 33: env->active_tc.LO[0] = tmp; break;
1062 case 34: env->active_tc.HI[0] = tmp; break;
1063 case 35: env->CP0_BadVAddr = tmp; break;
1064 case 36: env->CP0_Cause = tmp; break;
1065 case 37: env->active_tc.PC = tmp; break;
1066 case 72: /* fp, ignored */ break;
1067 default:
1068 if (n > 89)
1069 return 0;
1070 /* Other registers are readonly. Ignore writes. */
1071 break;
1072 }
1073
1074 return sizeof(target_ulong);
bellard6f970bd2005-12-05 19:55:19 +00001075}
bellardfdf9b3e2006-04-27 21:07:38 +00001076#elif defined (TARGET_SH4)
ths6ef99fc2007-05-13 16:36:24 +00001077
1078/* Hint: Use "set architecture sh4" in GDB to see fpu registers */
pbrook56aebc82008-10-11 17:55:29 +00001079/* FIXME: We should use XML for this. */
ths6ef99fc2007-05-13 16:36:24 +00001080
pbrook56aebc82008-10-11 17:55:29 +00001081#define NUM_CORE_REGS 59
1082
1083static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
bellardfdf9b3e2006-04-27 21:07:38 +00001084{
pbrook56aebc82008-10-11 17:55:29 +00001085 if (n < 8) {
1086 if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
1087 GET_REGL(env->gregs[n + 16]);
1088 } else {
1089 GET_REGL(env->gregs[n]);
1090 }
1091 } else if (n < 16) {
1092 GET_REGL(env->gregs[n - 8]);
1093 } else if (n >= 25 && n < 41) {
1094 GET_REGL(env->fregs[(n - 25) + ((env->fpscr & FPSCR_FR) ? 16 : 0)]);
1095 } else if (n >= 43 && n < 51) {
1096 GET_REGL(env->gregs[n - 43]);
1097 } else if (n >= 51 && n < 59) {
1098 GET_REGL(env->gregs[n - (51 - 16)]);
1099 }
1100 switch (n) {
1101 case 16: GET_REGL(env->pc);
1102 case 17: GET_REGL(env->pr);
1103 case 18: GET_REGL(env->gbr);
1104 case 19: GET_REGL(env->vbr);
1105 case 20: GET_REGL(env->mach);
1106 case 21: GET_REGL(env->macl);
1107 case 22: GET_REGL(env->sr);
1108 case 23: GET_REGL(env->fpul);
1109 case 24: GET_REGL(env->fpscr);
1110 case 41: GET_REGL(env->ssr);
1111 case 42: GET_REGL(env->spc);
1112 }
bellardfdf9b3e2006-04-27 21:07:38 +00001113
pbrook56aebc82008-10-11 17:55:29 +00001114 return 0;
bellardfdf9b3e2006-04-27 21:07:38 +00001115}
1116
pbrook56aebc82008-10-11 17:55:29 +00001117static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellardfdf9b3e2006-04-27 21:07:38 +00001118{
pbrook56aebc82008-10-11 17:55:29 +00001119 uint32_t tmp;
bellardfdf9b3e2006-04-27 21:07:38 +00001120
pbrook56aebc82008-10-11 17:55:29 +00001121 tmp = ldl_p(mem_buf);
1122
1123 if (n < 8) {
1124 if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
1125 env->gregs[n + 16] = tmp;
1126 } else {
1127 env->gregs[n] = tmp;
1128 }
1129 return 4;
1130 } else if (n < 16) {
1131 env->gregs[n - 8] = tmp;
1132 return 4;
1133 } else if (n >= 25 && n < 41) {
1134 env->fregs[(n - 25) + ((env->fpscr & FPSCR_FR) ? 16 : 0)] = tmp;
1135 } else if (n >= 43 && n < 51) {
1136 env->gregs[n - 43] = tmp;
1137 return 4;
1138 } else if (n >= 51 && n < 59) {
1139 env->gregs[n - (51 - 16)] = tmp;
1140 return 4;
1141 }
1142 switch (n) {
1143 case 16: env->pc = tmp;
1144 case 17: env->pr = tmp;
1145 case 18: env->gbr = tmp;
1146 case 19: env->vbr = tmp;
1147 case 20: env->mach = tmp;
1148 case 21: env->macl = tmp;
1149 case 22: env->sr = tmp;
1150 case 23: env->fpul = tmp;
1151 case 24: env->fpscr = tmp;
1152 case 41: env->ssr = tmp;
1153 case 42: env->spc = tmp;
1154 default: return 0;
1155 }
1156
1157 return 4;
bellardfdf9b3e2006-04-27 21:07:38 +00001158}
thsf1ccf902007-10-08 13:16:14 +00001159#elif defined (TARGET_CRIS)
1160
pbrook56aebc82008-10-11 17:55:29 +00001161#define NUM_CORE_REGS 49
1162
1163static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
thsf1ccf902007-10-08 13:16:14 +00001164{
pbrook56aebc82008-10-11 17:55:29 +00001165 uint8_t srs;
1166
1167 srs = env->pregs[PR_SRS];
1168 if (n < 16) {
1169 GET_REG32(env->regs[n]);
1170 }
1171
1172 if (n >= 21 && n < 32) {
1173 GET_REG32(env->pregs[n - 16]);
1174 }
1175 if (n >= 33 && n < 49) {
1176 GET_REG32(env->sregs[srs][n - 33]);
1177 }
1178 switch (n) {
1179 case 16: GET_REG8(env->pregs[0]);
1180 case 17: GET_REG8(env->pregs[1]);
1181 case 18: GET_REG32(env->pregs[2]);
1182 case 19: GET_REG8(srs);
1183 case 20: GET_REG16(env->pregs[4]);
1184 case 32: GET_REG32(env->pc);
1185 }
1186
1187 return 0;
thsf1ccf902007-10-08 13:16:14 +00001188}
1189
pbrook56aebc82008-10-11 17:55:29 +00001190static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
thsf1ccf902007-10-08 13:16:14 +00001191{
pbrook56aebc82008-10-11 17:55:29 +00001192 uint32_t tmp;
thsf1ccf902007-10-08 13:16:14 +00001193
pbrook56aebc82008-10-11 17:55:29 +00001194 if (n > 49)
1195 return 0;
thsf1ccf902007-10-08 13:16:14 +00001196
pbrook56aebc82008-10-11 17:55:29 +00001197 tmp = ldl_p(mem_buf);
thsf1ccf902007-10-08 13:16:14 +00001198
pbrook56aebc82008-10-11 17:55:29 +00001199 if (n < 16) {
1200 env->regs[n] = tmp;
1201 }
thsf1ccf902007-10-08 13:16:14 +00001202
edgar_igld7b69672008-10-11 19:32:21 +00001203 if (n >= 21 && n < 32) {
1204 env->pregs[n - 16] = tmp;
1205 }
1206
1207 /* FIXME: Should support function regs be writable? */
pbrook56aebc82008-10-11 17:55:29 +00001208 switch (n) {
1209 case 16: return 1;
1210 case 17: return 1;
edgar_igld7b69672008-10-11 19:32:21 +00001211 case 18: env->pregs[PR_PID] = tmp; break;
pbrook56aebc82008-10-11 17:55:29 +00001212 case 19: return 1;
1213 case 20: return 2;
1214 case 32: env->pc = tmp; break;
1215 }
thsf1ccf902007-10-08 13:16:14 +00001216
pbrook56aebc82008-10-11 17:55:29 +00001217 return 4;
thsf1ccf902007-10-08 13:16:14 +00001218}
aurel3219bf5172008-12-07 23:26:32 +00001219#elif defined (TARGET_ALPHA)
1220
1221#define NUM_CORE_REGS 65
1222
1223static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
1224{
1225 if (n < 31) {
1226 GET_REGL(env->ir[n]);
1227 }
1228 else if (n == 31) {
1229 GET_REGL(0);
1230 }
1231 else if (n<63) {
1232 uint64_t val;
1233
1234 val=*((uint64_t *)&env->fir[n-32]);
1235 GET_REGL(val);
1236 }
1237 else if (n==63) {
1238 GET_REGL(env->fpcr);
1239 }
1240 else if (n==64) {
1241 GET_REGL(env->pc);
1242 }
1243 else {
1244 GET_REGL(0);
1245 }
1246
1247 return 0;
1248}
1249
1250static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
1251{
1252 target_ulong tmp;
1253 tmp = ldtul_p(mem_buf);
1254
1255 if (n < 31) {
1256 env->ir[n] = tmp;
1257 }
1258
1259 if (n > 31 && n < 63) {
1260 env->fir[n - 32] = ldfl_p(mem_buf);
1261 }
1262
1263 if (n == 64 ) {
1264 env->pc=tmp;
1265 }
1266
1267 return 8;
1268}
bellard1fddef42005-04-17 19:16:13 +00001269#else
pbrook56aebc82008-10-11 17:55:29 +00001270
1271#define NUM_CORE_REGS 0
1272
1273static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
bellard6da41ea2004-01-04 15:48:38 +00001274{
1275 return 0;
1276}
1277
pbrook56aebc82008-10-11 17:55:29 +00001278static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellard6da41ea2004-01-04 15:48:38 +00001279{
pbrook56aebc82008-10-11 17:55:29 +00001280 return 0;
bellard6da41ea2004-01-04 15:48:38 +00001281}
1282
1283#endif
bellardb4608c02003-06-27 17:34:32 +00001284
pbrook56aebc82008-10-11 17:55:29 +00001285static int num_g_regs = NUM_CORE_REGS;
1286
1287#ifdef GDB_CORE_XML
1288/* Encode data using the encoding for 'x' packets. */
1289static int memtox(char *buf, const char *mem, int len)
1290{
1291 char *p = buf;
1292 char c;
1293
1294 while (len--) {
1295 c = *(mem++);
1296 switch (c) {
1297 case '#': case '$': case '*': case '}':
1298 *(p++) = '}';
1299 *(p++) = c ^ 0x20;
1300 break;
1301 default:
1302 *(p++) = c;
1303 break;
1304 }
1305 }
1306 return p - buf;
1307}
1308
aurel323faf7782008-12-07 23:26:17 +00001309static const char *get_feature_xml(const char *p, const char **newp)
pbrook56aebc82008-10-11 17:55:29 +00001310{
1311 extern const char *const xml_builtin[][2];
1312 size_t len;
1313 int i;
1314 const char *name;
1315 static char target_xml[1024];
1316
1317 len = 0;
1318 while (p[len] && p[len] != ':')
1319 len++;
1320 *newp = p + len;
1321
1322 name = NULL;
1323 if (strncmp(p, "target.xml", len) == 0) {
1324 /* Generate the XML description for this CPU. */
1325 if (!target_xml[0]) {
1326 GDBRegisterState *r;
1327
blueswir15b3715b2008-10-25 11:18:12 +00001328 snprintf(target_xml, sizeof(target_xml),
1329 "<?xml version=\"1.0\"?>"
1330 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
1331 "<target>"
1332 "<xi:include href=\"%s\"/>",
1333 GDB_CORE_XML);
pbrook56aebc82008-10-11 17:55:29 +00001334
aliguori880a7572008-11-18 20:30:24 +00001335 for (r = first_cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +00001336 strcat(target_xml, "<xi:include href=\"");
1337 strcat(target_xml, r->xml);
1338 strcat(target_xml, "\"/>");
1339 }
1340 strcat(target_xml, "</target>");
1341 }
1342 return target_xml;
1343 }
1344 for (i = 0; ; i++) {
1345 name = xml_builtin[i][0];
1346 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
1347 break;
1348 }
1349 return name ? xml_builtin[i][1] : NULL;
1350}
1351#endif
1352
1353static int gdb_read_register(CPUState *env, uint8_t *mem_buf, int reg)
1354{
1355 GDBRegisterState *r;
1356
1357 if (reg < NUM_CORE_REGS)
1358 return cpu_gdb_read_register(env, mem_buf, reg);
1359
1360 for (r = env->gdb_regs; r; r = r->next) {
1361 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
1362 return r->get_reg(env, mem_buf, reg - r->base_reg);
1363 }
1364 }
1365 return 0;
1366}
1367
1368static int gdb_write_register(CPUState *env, uint8_t *mem_buf, int reg)
1369{
1370 GDBRegisterState *r;
1371
1372 if (reg < NUM_CORE_REGS)
1373 return cpu_gdb_write_register(env, mem_buf, reg);
1374
1375 for (r = env->gdb_regs; r; r = r->next) {
1376 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
1377 return r->set_reg(env, mem_buf, reg - r->base_reg);
1378 }
1379 }
1380 return 0;
1381}
1382
1383/* Register a supplemental set of CPU registers. If g_pos is nonzero it
1384 specifies the first register number and these registers are included in
1385 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
1386 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
1387 */
1388
1389void gdb_register_coprocessor(CPUState * env,
1390 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
1391 int num_regs, const char *xml, int g_pos)
1392{
1393 GDBRegisterState *s;
1394 GDBRegisterState **p;
1395 static int last_reg = NUM_CORE_REGS;
1396
1397 s = (GDBRegisterState *)qemu_mallocz(sizeof(GDBRegisterState));
1398 s->base_reg = last_reg;
1399 s->num_regs = num_regs;
1400 s->get_reg = get_reg;
1401 s->set_reg = set_reg;
1402 s->xml = xml;
1403 p = &env->gdb_regs;
1404 while (*p) {
1405 /* Check for duplicates. */
1406 if (strcmp((*p)->xml, xml) == 0)
1407 return;
1408 p = &(*p)->next;
1409 }
1410 /* Add to end of list. */
1411 last_reg += num_regs;
1412 *p = s;
1413 if (g_pos) {
1414 if (g_pos != s->base_reg) {
1415 fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
1416 "Expected %d got %d\n", xml, g_pos, s->base_reg);
1417 } else {
1418 num_g_regs = last_reg;
1419 }
1420 }
1421}
1422
aliguoria1d1bb32008-11-18 20:07:32 +00001423#ifndef CONFIG_USER_ONLY
1424static const int xlat_gdb_type[] = {
1425 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
1426 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
1427 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
1428};
1429#endif
1430
aliguori880a7572008-11-18 20:30:24 +00001431static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +00001432{
aliguori880a7572008-11-18 20:30:24 +00001433 CPUState *env;
1434 int err = 0;
1435
aliguorie22a25c2009-03-12 20:12:48 +00001436 if (kvm_enabled())
1437 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
1438
aliguoria1d1bb32008-11-18 20:07:32 +00001439 switch (type) {
1440 case GDB_BREAKPOINT_SW:
1441 case GDB_BREAKPOINT_HW:
aliguori880a7572008-11-18 20:30:24 +00001442 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1443 err = cpu_breakpoint_insert(env, addr, BP_GDB, NULL);
1444 if (err)
1445 break;
1446 }
1447 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001448#ifndef CONFIG_USER_ONLY
1449 case GDB_WATCHPOINT_WRITE:
1450 case GDB_WATCHPOINT_READ:
1451 case GDB_WATCHPOINT_ACCESS:
aliguori880a7572008-11-18 20:30:24 +00001452 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1453 err = cpu_watchpoint_insert(env, addr, len, xlat_gdb_type[type],
1454 NULL);
1455 if (err)
1456 break;
1457 }
1458 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001459#endif
1460 default:
1461 return -ENOSYS;
1462 }
1463}
1464
aliguori880a7572008-11-18 20:30:24 +00001465static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +00001466{
aliguori880a7572008-11-18 20:30:24 +00001467 CPUState *env;
1468 int err = 0;
1469
aliguorie22a25c2009-03-12 20:12:48 +00001470 if (kvm_enabled())
1471 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
1472
aliguoria1d1bb32008-11-18 20:07:32 +00001473 switch (type) {
1474 case GDB_BREAKPOINT_SW:
1475 case GDB_BREAKPOINT_HW:
aliguori880a7572008-11-18 20:30:24 +00001476 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1477 err = cpu_breakpoint_remove(env, addr, BP_GDB);
1478 if (err)
1479 break;
1480 }
1481 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001482#ifndef CONFIG_USER_ONLY
1483 case GDB_WATCHPOINT_WRITE:
1484 case GDB_WATCHPOINT_READ:
1485 case GDB_WATCHPOINT_ACCESS:
aliguori880a7572008-11-18 20:30:24 +00001486 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1487 err = cpu_watchpoint_remove(env, addr, len, xlat_gdb_type[type]);
1488 if (err)
1489 break;
1490 }
1491 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001492#endif
1493 default:
1494 return -ENOSYS;
1495 }
1496}
1497
aliguori880a7572008-11-18 20:30:24 +00001498static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +00001499{
aliguori880a7572008-11-18 20:30:24 +00001500 CPUState *env;
1501
aliguorie22a25c2009-03-12 20:12:48 +00001502 if (kvm_enabled()) {
1503 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
1504 return;
1505 }
1506
aliguori880a7572008-11-18 20:30:24 +00001507 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1508 cpu_breakpoint_remove_all(env, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +00001509#ifndef CONFIG_USER_ONLY
aliguori880a7572008-11-18 20:30:24 +00001510 cpu_watchpoint_remove_all(env, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +00001511#endif
aliguori880a7572008-11-18 20:30:24 +00001512 }
aliguoria1d1bb32008-11-18 20:07:32 +00001513}
1514
aliguori880a7572008-11-18 20:30:24 +00001515static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +00001516{
aliguori880a7572008-11-18 20:30:24 +00001517 CPUState *env;
bellardb4608c02003-06-27 17:34:32 +00001518 const char *p;
aliguori880a7572008-11-18 20:30:24 +00001519 int ch, reg_size, type, res, thread;
pbrook56aebc82008-10-11 17:55:29 +00001520 char buf[MAX_PACKET_LENGTH];
1521 uint8_t mem_buf[MAX_PACKET_LENGTH];
1522 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +00001523 target_ulong addr, len;
ths3b46e622007-09-17 08:09:54 +00001524
bellard858693c2004-03-31 18:52:07 +00001525#ifdef DEBUG_GDB
1526 printf("command='%s'\n", line_buf);
bellard4c3a88a2003-07-26 12:06:08 +00001527#endif
bellard858693c2004-03-31 18:52:07 +00001528 p = line_buf;
1529 ch = *p++;
1530 switch(ch) {
1531 case '?':
bellard1fddef42005-04-17 19:16:13 +00001532 /* TODO: Make this return the correct value for user-mode. */
aurel32ca587a82008-12-18 22:44:13 +00001533 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
aliguori880a7572008-11-18 20:30:24 +00001534 s->c_cpu->cpu_index+1);
bellard858693c2004-03-31 18:52:07 +00001535 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +00001536 /* Remove all the breakpoints when this query is issued,
1537 * because gdb is doing and initial connect and the state
1538 * should be cleaned up.
1539 */
aliguori880a7572008-11-18 20:30:24 +00001540 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +00001541 break;
1542 case 'c':
1543 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +00001544 addr = strtoull(p, (char **)&p, 16);
bellardc33a3462003-07-29 20:50:33 +00001545#if defined(TARGET_I386)
aliguori880a7572008-11-18 20:30:24 +00001546 s->c_cpu->eip = addr;
aliguorie22a25c2009-03-12 20:12:48 +00001547 cpu_synchronize_state(s->c_cpu, 1);
bellard5be1a8e2004-01-04 23:51:58 +00001548#elif defined (TARGET_PPC)
aliguori880a7572008-11-18 20:30:24 +00001549 s->c_cpu->nip = addr;
bellard8d5f07f2004-10-04 21:23:09 +00001550#elif defined (TARGET_SPARC)
aliguori880a7572008-11-18 20:30:24 +00001551 s->c_cpu->pc = addr;
1552 s->c_cpu->npc = addr + 4;
bellardb5ff1b32005-11-26 10:38:39 +00001553#elif defined (TARGET_ARM)
aliguori880a7572008-11-18 20:30:24 +00001554 s->c_cpu->regs[15] = addr;
bellardfdf9b3e2006-04-27 21:07:38 +00001555#elif defined (TARGET_SH4)
aliguori880a7572008-11-18 20:30:24 +00001556 s->c_cpu->pc = addr;
ths8fac5802007-07-12 10:05:07 +00001557#elif defined (TARGET_MIPS)
aliguori880a7572008-11-18 20:30:24 +00001558 s->c_cpu->active_tc.PC = addr;
thsf1ccf902007-10-08 13:16:14 +00001559#elif defined (TARGET_CRIS)
aliguori880a7572008-11-18 20:30:24 +00001560 s->c_cpu->pc = addr;
aurel3219bf5172008-12-07 23:26:32 +00001561#elif defined (TARGET_ALPHA)
1562 s->c_cpu->pc = addr;
bellardc33a3462003-07-29 20:50:33 +00001563#endif
bellard858693c2004-03-31 18:52:07 +00001564 }
aurel32ca587a82008-12-18 22:44:13 +00001565 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +00001566 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +00001567 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +00001568 case 'C':
aurel32ca587a82008-12-18 22:44:13 +00001569 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
1570 if (s->signal == -1)
1571 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +00001572 gdb_continue(s);
1573 return RS_IDLE;
edgar_igl7d03f822008-05-17 18:58:29 +00001574 case 'k':
1575 /* Kill the target */
1576 fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
1577 exit(0);
1578 case 'D':
1579 /* Detach packet */
aliguori880a7572008-11-18 20:30:24 +00001580 gdb_breakpoint_remove_all();
edgar_igl7d03f822008-05-17 18:58:29 +00001581 gdb_continue(s);
1582 put_packet(s, "OK");
1583 break;
bellard858693c2004-03-31 18:52:07 +00001584 case 's':
1585 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +00001586 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001587#if defined(TARGET_I386)
aliguori880a7572008-11-18 20:30:24 +00001588 s->c_cpu->eip = addr;
aliguorie22a25c2009-03-12 20:12:48 +00001589 cpu_synchronize_state(s->c_cpu, 1);
bellard858693c2004-03-31 18:52:07 +00001590#elif defined (TARGET_PPC)
aliguori880a7572008-11-18 20:30:24 +00001591 s->c_cpu->nip = addr;
bellard8d5f07f2004-10-04 21:23:09 +00001592#elif defined (TARGET_SPARC)
aliguori880a7572008-11-18 20:30:24 +00001593 s->c_cpu->pc = addr;
1594 s->c_cpu->npc = addr + 4;
bellardb5ff1b32005-11-26 10:38:39 +00001595#elif defined (TARGET_ARM)
aliguori880a7572008-11-18 20:30:24 +00001596 s->c_cpu->regs[15] = addr;
bellardfdf9b3e2006-04-27 21:07:38 +00001597#elif defined (TARGET_SH4)
aliguori880a7572008-11-18 20:30:24 +00001598 s->c_cpu->pc = addr;
ths8fac5802007-07-12 10:05:07 +00001599#elif defined (TARGET_MIPS)
aliguori880a7572008-11-18 20:30:24 +00001600 s->c_cpu->active_tc.PC = addr;
thsf1ccf902007-10-08 13:16:14 +00001601#elif defined (TARGET_CRIS)
aliguori880a7572008-11-18 20:30:24 +00001602 s->c_cpu->pc = addr;
aurel3219bf5172008-12-07 23:26:32 +00001603#elif defined (TARGET_ALPHA)
1604 s->c_cpu->pc = addr;
bellard858693c2004-03-31 18:52:07 +00001605#endif
1606 }
aliguori880a7572008-11-18 20:30:24 +00001607 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +00001608 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +00001609 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +00001610 case 'F':
1611 {
1612 target_ulong ret;
1613 target_ulong err;
1614
1615 ret = strtoull(p, (char **)&p, 16);
1616 if (*p == ',') {
1617 p++;
1618 err = strtoull(p, (char **)&p, 16);
1619 } else {
1620 err = 0;
1621 }
1622 if (*p == ',')
1623 p++;
1624 type = *p;
1625 if (gdb_current_syscall_cb)
aliguori880a7572008-11-18 20:30:24 +00001626 gdb_current_syscall_cb(s->c_cpu, ret, err);
pbrooka2d1eba2007-01-28 03:10:55 +00001627 if (type == 'C') {
1628 put_packet(s, "T02");
1629 } else {
edgar_iglba70a622008-03-14 06:10:42 +00001630 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +00001631 }
1632 }
1633 break;
bellard858693c2004-03-31 18:52:07 +00001634 case 'g':
aliguorie22a25c2009-03-12 20:12:48 +00001635 cpu_synchronize_state(s->g_cpu, 0);
pbrook56aebc82008-10-11 17:55:29 +00001636 len = 0;
1637 for (addr = 0; addr < num_g_regs; addr++) {
aliguori880a7572008-11-18 20:30:24 +00001638 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +00001639 len += reg_size;
1640 }
1641 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +00001642 put_packet(s, buf);
1643 break;
1644 case 'G':
pbrook56aebc82008-10-11 17:55:29 +00001645 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +00001646 len = strlen(p) / 2;
1647 hextomem((uint8_t *)registers, p, len);
pbrook56aebc82008-10-11 17:55:29 +00001648 for (addr = 0; addr < num_g_regs && len > 0; addr++) {
aliguori880a7572008-11-18 20:30:24 +00001649 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001650 len -= reg_size;
1651 registers += reg_size;
1652 }
aliguorie22a25c2009-03-12 20:12:48 +00001653 cpu_synchronize_state(s->g_cpu, 1);
bellard858693c2004-03-31 18:52:07 +00001654 put_packet(s, "OK");
1655 break;
1656 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001657 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001658 if (*p == ',')
1659 p++;
bellard9d9754a2006-06-25 15:32:37 +00001660 len = strtoull(p, NULL, 16);
aliguori880a7572008-11-18 20:30:24 +00001661 if (cpu_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 0) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001662 put_packet (s, "E14");
1663 } else {
1664 memtohex(buf, mem_buf, len);
1665 put_packet(s, buf);
1666 }
bellard858693c2004-03-31 18:52:07 +00001667 break;
1668 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001669 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001670 if (*p == ',')
1671 p++;
bellard9d9754a2006-06-25 15:32:37 +00001672 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001673 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001674 p++;
1675 hextomem(mem_buf, p, len);
aliguori880a7572008-11-18 20:30:24 +00001676 if (cpu_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 1) != 0)
bellard905f20b2005-04-26 21:09:55 +00001677 put_packet(s, "E14");
bellard858693c2004-03-31 18:52:07 +00001678 else
1679 put_packet(s, "OK");
1680 break;
pbrook56aebc82008-10-11 17:55:29 +00001681 case 'p':
1682 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1683 This works, but can be very slow. Anything new enough to
1684 understand XML also knows how to use this properly. */
1685 if (!gdb_has_xml)
1686 goto unknown_command;
1687 addr = strtoull(p, (char **)&p, 16);
aliguori880a7572008-11-18 20:30:24 +00001688 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001689 if (reg_size) {
1690 memtohex(buf, mem_buf, reg_size);
1691 put_packet(s, buf);
1692 } else {
1693 put_packet(s, "E14");
1694 }
1695 break;
1696 case 'P':
1697 if (!gdb_has_xml)
1698 goto unknown_command;
1699 addr = strtoull(p, (char **)&p, 16);
1700 if (*p == '=')
1701 p++;
1702 reg_size = strlen(p) / 2;
1703 hextomem(mem_buf, p, reg_size);
aliguori880a7572008-11-18 20:30:24 +00001704 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001705 put_packet(s, "OK");
1706 break;
bellard858693c2004-03-31 18:52:07 +00001707 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001708 case 'z':
1709 type = strtoul(p, (char **)&p, 16);
1710 if (*p == ',')
1711 p++;
bellard9d9754a2006-06-25 15:32:37 +00001712 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001713 if (*p == ',')
1714 p++;
bellard9d9754a2006-06-25 15:32:37 +00001715 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001716 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001717 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001718 else
aliguori880a7572008-11-18 20:30:24 +00001719 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001720 if (res >= 0)
1721 put_packet(s, "OK");
1722 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001723 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001724 else
1725 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001726 break;
aliguori880a7572008-11-18 20:30:24 +00001727 case 'H':
1728 type = *p++;
1729 thread = strtoull(p, (char **)&p, 16);
1730 if (thread == -1 || thread == 0) {
1731 put_packet(s, "OK");
1732 break;
1733 }
1734 for (env = first_cpu; env != NULL; env = env->next_cpu)
1735 if (env->cpu_index + 1 == thread)
1736 break;
1737 if (env == NULL) {
1738 put_packet(s, "E22");
1739 break;
1740 }
1741 switch (type) {
1742 case 'c':
1743 s->c_cpu = env;
1744 put_packet(s, "OK");
1745 break;
1746 case 'g':
1747 s->g_cpu = env;
1748 put_packet(s, "OK");
1749 break;
1750 default:
1751 put_packet(s, "E22");
1752 break;
1753 }
1754 break;
1755 case 'T':
1756 thread = strtoull(p, (char **)&p, 16);
1757#ifndef CONFIG_USER_ONLY
1758 if (thread > 0 && thread < smp_cpus + 1)
1759#else
1760 if (thread == 1)
1761#endif
1762 put_packet(s, "OK");
1763 else
1764 put_packet(s, "E22");
1765 break;
pbrook978efd62006-06-17 18:30:42 +00001766 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001767 case 'Q':
1768 /* parse any 'q' packets here */
1769 if (!strcmp(p,"qemu.sstepbits")) {
1770 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001771 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1772 SSTEP_ENABLE,
1773 SSTEP_NOIRQ,
1774 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001775 put_packet(s, buf);
1776 break;
1777 } else if (strncmp(p,"qemu.sstep",10) == 0) {
1778 /* Display or change the sstep_flags */
1779 p += 10;
1780 if (*p != '=') {
1781 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001782 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001783 put_packet(s, buf);
1784 break;
1785 }
1786 p++;
1787 type = strtoul(p, (char **)&p, 16);
1788 sstep_flags = type;
1789 put_packet(s, "OK");
1790 break;
aliguori880a7572008-11-18 20:30:24 +00001791 } else if (strcmp(p,"C") == 0) {
1792 /* "Current thread" remains vague in the spec, so always return
1793 * the first CPU (gdb returns the first thread). */
1794 put_packet(s, "QC1");
1795 break;
1796 } else if (strcmp(p,"fThreadInfo") == 0) {
1797 s->query_cpu = first_cpu;
1798 goto report_cpuinfo;
1799 } else if (strcmp(p,"sThreadInfo") == 0) {
1800 report_cpuinfo:
1801 if (s->query_cpu) {
1802 snprintf(buf, sizeof(buf), "m%x", s->query_cpu->cpu_index+1);
1803 put_packet(s, buf);
1804 s->query_cpu = s->query_cpu->next_cpu;
1805 } else
1806 put_packet(s, "l");
1807 break;
1808 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1809 thread = strtoull(p+16, (char **)&p, 16);
1810 for (env = first_cpu; env != NULL; env = env->next_cpu)
1811 if (env->cpu_index + 1 == thread) {
aliguorie22a25c2009-03-12 20:12:48 +00001812 cpu_synchronize_state(env, 0);
aliguori880a7572008-11-18 20:30:24 +00001813 len = snprintf((char *)mem_buf, sizeof(mem_buf),
1814 "CPU#%d [%s]", env->cpu_index,
1815 env->halted ? "halted " : "running");
1816 memtohex(buf, mem_buf, len);
1817 put_packet(s, buf);
1818 break;
1819 }
1820 break;
edgar_igl60897d32008-05-09 08:25:14 +00001821 }
blueswir10b8a9882009-03-07 10:51:36 +00001822#ifdef CONFIG_USER_ONLY
edgar_igl60897d32008-05-09 08:25:14 +00001823 else if (strncmp(p, "Offsets", 7) == 0) {
aliguori880a7572008-11-18 20:30:24 +00001824 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001825
blueswir1363a37d2008-08-21 17:58:08 +00001826 snprintf(buf, sizeof(buf),
1827 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1828 ";Bss=" TARGET_ABI_FMT_lx,
1829 ts->info->code_offset,
1830 ts->info->data_offset,
1831 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001832 put_packet(s, buf);
1833 break;
1834 }
blueswir10b8a9882009-03-07 10:51:36 +00001835#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001836 else if (strncmp(p, "Rcmd,", 5) == 0) {
1837 int len = strlen(p + 5);
1838
1839 if ((len % 2) != 0) {
1840 put_packet(s, "E01");
1841 break;
1842 }
1843 hextomem(mem_buf, p + 5, len);
1844 len = len / 2;
1845 mem_buf[len++] = 0;
1846 qemu_chr_read(s->mon_chr, mem_buf, len);
1847 put_packet(s, "OK");
1848 break;
1849 }
blueswir10b8a9882009-03-07 10:51:36 +00001850#endif /* !CONFIG_USER_ONLY */
pbrook56aebc82008-10-11 17:55:29 +00001851 if (strncmp(p, "Supported", 9) == 0) {
blueswir15b3715b2008-10-25 11:18:12 +00001852 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
pbrook56aebc82008-10-11 17:55:29 +00001853#ifdef GDB_CORE_XML
1854 strcat(buf, ";qXfer:features:read+");
1855#endif
1856 put_packet(s, buf);
1857 break;
1858 }
1859#ifdef GDB_CORE_XML
1860 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1861 const char *xml;
1862 target_ulong total_len;
1863
1864 gdb_has_xml = 1;
1865 p += 19;
aliguori880a7572008-11-18 20:30:24 +00001866 xml = get_feature_xml(p, &p);
pbrook56aebc82008-10-11 17:55:29 +00001867 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001868 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001869 put_packet(s, buf);
1870 break;
1871 }
1872
1873 if (*p == ':')
1874 p++;
1875 addr = strtoul(p, (char **)&p, 16);
1876 if (*p == ',')
1877 p++;
1878 len = strtoul(p, (char **)&p, 16);
1879
1880 total_len = strlen(xml);
1881 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001882 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001883 put_packet(s, buf);
1884 break;
1885 }
1886 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1887 len = (MAX_PACKET_LENGTH - 5) / 2;
1888 if (len < total_len - addr) {
1889 buf[0] = 'm';
1890 len = memtox(buf + 1, xml + addr, len);
1891 } else {
1892 buf[0] = 'l';
1893 len = memtox(buf + 1, xml + addr, total_len - addr);
1894 }
1895 put_packet_binary(s, buf, len + 1);
1896 break;
1897 }
1898#endif
1899 /* Unrecognised 'q' command. */
1900 goto unknown_command;
1901
bellard858693c2004-03-31 18:52:07 +00001902 default:
pbrook56aebc82008-10-11 17:55:29 +00001903 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001904 /* put empty packet */
1905 buf[0] = '\0';
1906 put_packet(s, buf);
1907 break;
1908 }
1909 return RS_IDLE;
1910}
1911
aliguori880a7572008-11-18 20:30:24 +00001912void gdb_set_stop_cpu(CPUState *env)
1913{
1914 gdbserver_state->c_cpu = env;
1915 gdbserver_state->g_cpu = env;
1916}
1917
bellard1fddef42005-04-17 19:16:13 +00001918#ifndef CONFIG_USER_ONLY
aliguori9781e042009-01-22 17:15:29 +00001919static void gdb_vm_state_change(void *opaque, int running, int reason)
bellard858693c2004-03-31 18:52:07 +00001920{
aliguori880a7572008-11-18 20:30:24 +00001921 GDBState *s = gdbserver_state;
1922 CPUState *env = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001923 char buf[256];
aliguorid6fc1b32008-11-18 19:55:44 +00001924 const char *type;
bellard858693c2004-03-31 18:52:07 +00001925 int ret;
1926
aliguori9781e042009-01-22 17:15:29 +00001927 if (running || (reason != EXCP_DEBUG && reason != EXCP_INTERRUPT) ||
aliguori36556b22009-03-28 18:05:53 +00001928 s->state == RS_INACTIVE || s->state == RS_SYSCALL)
pbrooka2d1eba2007-01-28 03:10:55 +00001929 return;
1930
bellard858693c2004-03-31 18:52:07 +00001931 /* disable single step if it was enable */
aliguori880a7572008-11-18 20:30:24 +00001932 cpu_single_step(env, 0);
bellard858693c2004-03-31 18:52:07 +00001933
bellarde80cfcf2004-12-19 23:18:01 +00001934 if (reason == EXCP_DEBUG) {
aliguori880a7572008-11-18 20:30:24 +00001935 if (env->watchpoint_hit) {
1936 switch (env->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001937 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001938 type = "r";
1939 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001940 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001941 type = "a";
1942 break;
1943 default:
1944 type = "";
1945 break;
1946 }
aliguori880a7572008-11-18 20:30:24 +00001947 snprintf(buf, sizeof(buf),
1948 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
aurel32ca587a82008-12-18 22:44:13 +00001949 GDB_SIGNAL_TRAP, env->cpu_index+1, type,
aliguori880a7572008-11-18 20:30:24 +00001950 env->watchpoint_hit->vaddr);
pbrook6658ffb2007-03-16 23:58:11 +00001951 put_packet(s, buf);
aliguori880a7572008-11-18 20:30:24 +00001952 env->watchpoint_hit = NULL;
pbrook6658ffb2007-03-16 23:58:11 +00001953 return;
1954 }
aliguori880a7572008-11-18 20:30:24 +00001955 tb_flush(env);
aurel32ca587a82008-12-18 22:44:13 +00001956 ret = GDB_SIGNAL_TRAP;
bellardbbeb7b52006-04-23 18:42:15 +00001957 } else {
aliguori9781e042009-01-22 17:15:29 +00001958 ret = GDB_SIGNAL_INT;
bellardbbeb7b52006-04-23 18:42:15 +00001959 }
aliguori880a7572008-11-18 20:30:24 +00001960 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, env->cpu_index+1);
bellard858693c2004-03-31 18:52:07 +00001961 put_packet(s, buf);
1962}
bellard1fddef42005-04-17 19:16:13 +00001963#endif
bellard858693c2004-03-31 18:52:07 +00001964
pbrooka2d1eba2007-01-28 03:10:55 +00001965/* Send a gdb syscall request.
1966 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001967 %x - target_ulong argument printed in hex.
1968 %lx - 64-bit argument printed in hex.
1969 %s - string pointer (target_ulong) and length (int) pair. */
blueswir17ccfb2e2008-09-14 06:45:34 +00001970void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
pbrooka2d1eba2007-01-28 03:10:55 +00001971{
1972 va_list va;
1973 char buf[256];
1974 char *p;
1975 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001976 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001977 GDBState *s;
1978
aliguori880a7572008-11-18 20:30:24 +00001979 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001980 if (!s)
1981 return;
1982 gdb_current_syscall_cb = cb;
1983 s->state = RS_SYSCALL;
1984#ifndef CONFIG_USER_ONLY
1985 vm_stop(EXCP_DEBUG);
1986#endif
1987 s->state = RS_IDLE;
1988 va_start(va, fmt);
1989 p = buf;
1990 *(p++) = 'F';
1991 while (*fmt) {
1992 if (*fmt == '%') {
1993 fmt++;
1994 switch (*fmt++) {
1995 case 'x':
1996 addr = va_arg(va, target_ulong);
blueswir1363a37d2008-08-21 17:58:08 +00001997 p += snprintf(p, &buf[sizeof(buf)] - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001998 break;
pbrooka87295e2007-05-26 15:09:38 +00001999 case 'l':
2000 if (*(fmt++) != 'x')
2001 goto bad_format;
2002 i64 = va_arg(va, uint64_t);
blueswir1363a37d2008-08-21 17:58:08 +00002003 p += snprintf(p, &buf[sizeof(buf)] - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00002004 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002005 case 's':
2006 addr = va_arg(va, target_ulong);
blueswir1363a37d2008-08-21 17:58:08 +00002007 p += snprintf(p, &buf[sizeof(buf)] - p, TARGET_FMT_lx "/%x",
2008 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00002009 break;
2010 default:
pbrooka87295e2007-05-26 15:09:38 +00002011 bad_format:
pbrooka2d1eba2007-01-28 03:10:55 +00002012 fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
2013 fmt - 1);
2014 break;
2015 }
2016 } else {
2017 *(p++) = *(fmt++);
2018 }
2019 }
pbrook8a93e022007-08-06 13:19:15 +00002020 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00002021 va_end(va);
2022 put_packet(s, buf);
2023#ifdef CONFIG_USER_ONLY
aliguori880a7572008-11-18 20:30:24 +00002024 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00002025#else
aurel323098dba2009-03-07 21:28:24 +00002026 cpu_exit(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00002027#endif
2028}
2029
bellard6a00d602005-11-21 23:25:50 +00002030static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00002031{
2032 int i, csum;
ths60fe76f2007-12-16 03:02:09 +00002033 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00002034
bellard1fddef42005-04-17 19:16:13 +00002035#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00002036 if (s->last_packet_len) {
2037 /* Waiting for a response to the last packet. If we see the start
2038 of a new command then abandon the previous response. */
2039 if (ch == '-') {
2040#ifdef DEBUG_GDB
2041 printf("Got NACK, retransmitting\n");
2042#endif
thsffe8ab82007-12-16 03:16:05 +00002043 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
pbrook4046d912007-01-28 01:53:16 +00002044 }
2045#ifdef DEBUG_GDB
2046 else if (ch == '+')
2047 printf("Got ACK\n");
2048 else
2049 printf("Got '%c' when expecting ACK/NACK\n", ch);
2050#endif
2051 if (ch == '+' || ch == '$')
2052 s->last_packet_len = 0;
2053 if (ch != '$')
2054 return;
2055 }
bellard858693c2004-03-31 18:52:07 +00002056 if (vm_running) {
2057 /* when the CPU is running, we cannot do anything except stop
2058 it when receiving a char */
2059 vm_stop(EXCP_INTERRUPT);
ths5fafdf22007-09-16 21:08:06 +00002060 } else
bellard1fddef42005-04-17 19:16:13 +00002061#endif
bellard41625032005-04-24 10:07:11 +00002062 {
bellard858693c2004-03-31 18:52:07 +00002063 switch(s->state) {
2064 case RS_IDLE:
2065 if (ch == '$') {
2066 s->line_buf_index = 0;
2067 s->state = RS_GETLINE;
bellard4c3a88a2003-07-26 12:06:08 +00002068 }
2069 break;
bellard858693c2004-03-31 18:52:07 +00002070 case RS_GETLINE:
2071 if (ch == '#') {
2072 s->state = RS_CHKSUM1;
2073 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2074 s->state = RS_IDLE;
2075 } else {
2076 s->line_buf[s->line_buf_index++] = ch;
2077 }
2078 break;
2079 case RS_CHKSUM1:
2080 s->line_buf[s->line_buf_index] = '\0';
2081 s->line_csum = fromhex(ch) << 4;
2082 s->state = RS_CHKSUM2;
2083 break;
2084 case RS_CHKSUM2:
2085 s->line_csum |= fromhex(ch);
2086 csum = 0;
2087 for(i = 0; i < s->line_buf_index; i++) {
2088 csum += s->line_buf[i];
2089 }
2090 if (s->line_csum != (csum & 0xff)) {
ths60fe76f2007-12-16 03:02:09 +00002091 reply = '-';
2092 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00002093 s->state = RS_IDLE;
2094 } else {
ths60fe76f2007-12-16 03:02:09 +00002095 reply = '+';
2096 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00002097 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00002098 }
bellardb4608c02003-06-27 17:34:32 +00002099 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002100 default:
2101 abort();
bellardb4608c02003-06-27 17:34:32 +00002102 }
2103 }
bellard858693c2004-03-31 18:52:07 +00002104}
2105
bellard1fddef42005-04-17 19:16:13 +00002106#ifdef CONFIG_USER_ONLY
2107int
aurel32ca587a82008-12-18 22:44:13 +00002108gdb_queuesig (void)
2109{
2110 GDBState *s;
2111
2112 s = gdbserver_state;
2113
2114 if (gdbserver_fd < 0 || s->fd < 0)
2115 return 0;
2116 else
2117 return 1;
2118}
2119
2120int
bellard1fddef42005-04-17 19:16:13 +00002121gdb_handlesig (CPUState *env, int sig)
2122{
2123 GDBState *s;
2124 char buf[256];
2125 int n;
2126
aliguori880a7572008-11-18 20:30:24 +00002127 s = gdbserver_state;
edgar_igl1f487ee2008-05-17 22:20:53 +00002128 if (gdbserver_fd < 0 || s->fd < 0)
2129 return sig;
bellard1fddef42005-04-17 19:16:13 +00002130
2131 /* disable single step if it was enabled */
2132 cpu_single_step(env, 0);
2133 tb_flush(env);
2134
2135 if (sig != 0)
2136 {
aurel32ca587a82008-12-18 22:44:13 +00002137 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb (sig));
bellard1fddef42005-04-17 19:16:13 +00002138 put_packet(s, buf);
2139 }
edgar_igl1f487ee2008-05-17 22:20:53 +00002140 /* put_packet() might have detected that the peer terminated the
2141 connection. */
2142 if (s->fd < 0)
2143 return sig;
bellard1fddef42005-04-17 19:16:13 +00002144
bellard1fddef42005-04-17 19:16:13 +00002145 sig = 0;
2146 s->state = RS_IDLE;
bellard41625032005-04-24 10:07:11 +00002147 s->running_state = 0;
2148 while (s->running_state == 0) {
bellard1fddef42005-04-17 19:16:13 +00002149 n = read (s->fd, buf, 256);
2150 if (n > 0)
2151 {
2152 int i;
2153
2154 for (i = 0; i < n; i++)
bellard6a00d602005-11-21 23:25:50 +00002155 gdb_read_byte (s, buf[i]);
bellard1fddef42005-04-17 19:16:13 +00002156 }
2157 else if (n == 0 || errno != EAGAIN)
2158 {
2159 /* XXX: Connection closed. Should probably wait for annother
2160 connection before continuing. */
2161 return sig;
2162 }
bellard41625032005-04-24 10:07:11 +00002163 }
edgar_igl1f487ee2008-05-17 22:20:53 +00002164 sig = s->signal;
2165 s->signal = 0;
bellard1fddef42005-04-17 19:16:13 +00002166 return sig;
2167}
bellarde9009672005-04-26 20:42:36 +00002168
2169/* Tell the remote gdb that the process has exited. */
2170void gdb_exit(CPUState *env, int code)
2171{
2172 GDBState *s;
2173 char buf[4];
2174
aliguori880a7572008-11-18 20:30:24 +00002175 s = gdbserver_state;
edgar_igl1f487ee2008-05-17 22:20:53 +00002176 if (gdbserver_fd < 0 || s->fd < 0)
2177 return;
bellarde9009672005-04-26 20:42:36 +00002178
2179 snprintf(buf, sizeof(buf), "W%02x", code);
2180 put_packet(s, buf);
2181}
2182
aurel32ca587a82008-12-18 22:44:13 +00002183/* Tell the remote gdb that the process has exited due to SIG. */
2184void gdb_signalled(CPUState *env, int sig)
2185{
2186 GDBState *s;
2187 char buf[4];
2188
2189 s = gdbserver_state;
2190 if (gdbserver_fd < 0 || s->fd < 0)
2191 return;
2192
2193 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb (sig));
2194 put_packet(s, buf);
2195}
bellard1fddef42005-04-17 19:16:13 +00002196
aliguori880a7572008-11-18 20:30:24 +00002197static void gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00002198{
2199 GDBState *s;
2200 struct sockaddr_in sockaddr;
2201 socklen_t len;
2202 int val, fd;
2203
2204 for(;;) {
2205 len = sizeof(sockaddr);
2206 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
2207 if (fd < 0 && errno != EINTR) {
2208 perror("accept");
2209 return;
2210 } else if (fd >= 0) {
2211 break;
2212 }
2213 }
2214
2215 /* set short latency */
2216 val = 1;
bellard8f447cc2006-06-14 15:21:14 +00002217 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
ths3b46e622007-09-17 08:09:54 +00002218
aliguori880a7572008-11-18 20:30:24 +00002219 s = qemu_mallocz(sizeof(GDBState));
aliguori880a7572008-11-18 20:30:24 +00002220 s->c_cpu = first_cpu;
2221 s->g_cpu = first_cpu;
bellard858693c2004-03-31 18:52:07 +00002222 s->fd = fd;
pbrook56aebc82008-10-11 17:55:29 +00002223 gdb_has_xml = 0;
bellard858693c2004-03-31 18:52:07 +00002224
aliguori880a7572008-11-18 20:30:24 +00002225 gdbserver_state = s;
pbrooka2d1eba2007-01-28 03:10:55 +00002226
bellard858693c2004-03-31 18:52:07 +00002227 fcntl(fd, F_SETFL, O_NONBLOCK);
bellard858693c2004-03-31 18:52:07 +00002228}
2229
2230static int gdbserver_open(int port)
2231{
2232 struct sockaddr_in sockaddr;
2233 int fd, val, ret;
2234
2235 fd = socket(PF_INET, SOCK_STREAM, 0);
2236 if (fd < 0) {
2237 perror("socket");
2238 return -1;
2239 }
2240
2241 /* allow fast reuse */
2242 val = 1;
bellard8f447cc2006-06-14 15:21:14 +00002243 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
bellard858693c2004-03-31 18:52:07 +00002244
2245 sockaddr.sin_family = AF_INET;
2246 sockaddr.sin_port = htons(port);
2247 sockaddr.sin_addr.s_addr = 0;
2248 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
2249 if (ret < 0) {
2250 perror("bind");
2251 return -1;
2252 }
2253 ret = listen(fd, 0);
2254 if (ret < 0) {
2255 perror("listen");
2256 return -1;
2257 }
bellard858693c2004-03-31 18:52:07 +00002258 return fd;
2259}
2260
2261int gdbserver_start(int port)
2262{
2263 gdbserver_fd = gdbserver_open(port);
2264 if (gdbserver_fd < 0)
2265 return -1;
2266 /* accept connections */
aliguori880a7572008-11-18 20:30:24 +00002267 gdb_accept();
bellardb4608c02003-06-27 17:34:32 +00002268 return 0;
2269}
aurel322b1319c2008-12-18 22:44:04 +00002270
2271/* Disable gdb stub for child processes. */
2272void gdbserver_fork(CPUState *env)
2273{
2274 GDBState *s = gdbserver_state;
edgar_igl9f6164d2009-01-07 10:22:28 +00002275 if (gdbserver_fd < 0 || s->fd < 0)
aurel322b1319c2008-12-18 22:44:04 +00002276 return;
2277 close(s->fd);
2278 s->fd = -1;
2279 cpu_breakpoint_remove_all(env, BP_GDB);
2280 cpu_watchpoint_remove_all(env, BP_GDB);
2281}
pbrook4046d912007-01-28 01:53:16 +00002282#else
thsaa1f17c2007-07-11 22:48:58 +00002283static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00002284{
pbrook56aebc82008-10-11 17:55:29 +00002285 /* We can handle an arbitrarily large amount of data.
2286 Pick the maximum packet size, which is as good as anything. */
2287 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00002288}
2289
thsaa1f17c2007-07-11 22:48:58 +00002290static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00002291{
pbrook4046d912007-01-28 01:53:16 +00002292 int i;
2293
2294 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00002295 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00002296 }
2297}
2298
2299static void gdb_chr_event(void *opaque, int event)
2300{
2301 switch (event) {
2302 case CHR_EVENT_RESET:
2303 vm_stop(EXCP_INTERRUPT);
pbrook56aebc82008-10-11 17:55:29 +00002304 gdb_has_xml = 0;
pbrook4046d912007-01-28 01:53:16 +00002305 break;
2306 default:
2307 break;
2308 }
2309}
2310
aliguori8a34a0f2009-03-05 23:01:55 +00002311static void gdb_monitor_output(GDBState *s, const char *msg, int len)
2312{
2313 char buf[MAX_PACKET_LENGTH];
2314
2315 buf[0] = 'O';
2316 if (len > (MAX_PACKET_LENGTH/2) - 1)
2317 len = (MAX_PACKET_LENGTH/2) - 1;
2318 memtohex(buf + 1, (uint8_t *)msg, len);
2319 put_packet(s, buf);
2320}
2321
2322static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
2323{
2324 const char *p = (const char *)buf;
2325 int max_sz;
2326
2327 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
2328 for (;;) {
2329 if (len <= max_sz) {
2330 gdb_monitor_output(gdbserver_state, p, len);
2331 break;
2332 }
2333 gdb_monitor_output(gdbserver_state, p, max_sz);
2334 p += max_sz;
2335 len -= max_sz;
2336 }
2337 return len;
2338}
2339
pbrookcfc34752007-02-22 01:48:01 +00002340int gdbserver_start(const char *port)
pbrook4046d912007-01-28 01:53:16 +00002341{
2342 GDBState *s;
pbrookcfc34752007-02-22 01:48:01 +00002343 char gdbstub_port_name[128];
2344 int port_num;
2345 char *p;
aliguori36556b22009-03-28 18:05:53 +00002346 CharDriverState *chr = NULL;
2347 CharDriverState *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00002348
pbrookcfc34752007-02-22 01:48:01 +00002349 if (!port || !*port)
2350 return -1;
aliguori36556b22009-03-28 18:05:53 +00002351 if (strcmp(port, "none") != 0) {
2352 port_num = strtol(port, &p, 10);
2353 if (*p == 0) {
2354 /* A numeric value is interpreted as a port number. */
2355 snprintf(gdbstub_port_name, sizeof(gdbstub_port_name),
2356 "tcp::%d,nowait,nodelay,server", port_num);
2357 port = gdbstub_port_name;
2358 }
pbrookcfc34752007-02-22 01:48:01 +00002359
aliguori36556b22009-03-28 18:05:53 +00002360 chr = qemu_chr_open("gdb", port, NULL);
2361 if (!chr)
2362 return -1;
2363
2364 qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
2365 gdb_chr_event, NULL);
pbrookcfc34752007-02-22 01:48:01 +00002366 }
2367
aliguori36556b22009-03-28 18:05:53 +00002368 s = gdbserver_state;
2369 if (!s) {
2370 s = qemu_mallocz(sizeof(GDBState));
2371 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00002372
aliguori36556b22009-03-28 18:05:53 +00002373 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
2374
2375 /* Initialize a monitor terminal for gdb */
2376 mon_chr = qemu_mallocz(sizeof(*mon_chr));
2377 mon_chr->chr_write = gdb_monitor_write;
2378 monitor_init(mon_chr, 0);
2379 } else {
2380 if (s->chr)
2381 qemu_chr_close(s->chr);
2382 mon_chr = s->mon_chr;
2383 memset(s, 0, sizeof(GDBState));
2384 }
aliguori880a7572008-11-18 20:30:24 +00002385 s->c_cpu = first_cpu;
2386 s->g_cpu = first_cpu;
pbrook4046d912007-01-28 01:53:16 +00002387 s->chr = chr;
aliguori36556b22009-03-28 18:05:53 +00002388 s->state = chr ? RS_IDLE : RS_INACTIVE;
2389 s->mon_chr = mon_chr;
aliguori8a34a0f2009-03-05 23:01:55 +00002390
pbrook4046d912007-01-28 01:53:16 +00002391 return 0;
2392}
2393#endif