blob: 055093f89ba39a04a0841eb3b932b84c659d265b [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
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
bellardb4608c02003-06-27 17:34:32 +000018 */
pbrook978efd62006-06-17 18:30:42 +000019#include "config.h"
pbrook56aebc82008-10-11 17:55:29 +000020#include "qemu-common.h"
bellard1fddef42005-04-17 19:16:13 +000021#ifdef CONFIG_USER_ONLY
22#include <stdlib.h>
23#include <stdio.h>
24#include <stdarg.h>
25#include <string.h>
26#include <errno.h>
27#include <unistd.h>
pbrook978efd62006-06-17 18:30:42 +000028#include <fcntl.h>
bellard1fddef42005-04-17 19:16:13 +000029
30#include "qemu.h"
31#else
aliguori8a34a0f2009-03-05 23:01:55 +000032#include "monitor.h"
pbrook87ecb682007-11-17 17:14:51 +000033#include "qemu-char.h"
34#include "sysemu.h"
35#include "gdbstub.h"
bellard1fddef42005-04-17 19:16:13 +000036#endif
bellard67b915a2004-03-31 23:37:16 +000037
pbrook56aebc82008-10-11 17:55:29 +000038#define MAX_PACKET_LENGTH 4096
39
bellard8f447cc2006-06-14 15:21:14 +000040#include "qemu_socket.h"
aliguorie22a25c2009-03-12 20:12:48 +000041#include "kvm.h"
aurel32ca587a82008-12-18 22:44:13 +000042
43
44enum {
45 GDB_SIGNAL_0 = 0,
46 GDB_SIGNAL_INT = 2,
47 GDB_SIGNAL_TRAP = 5,
48 GDB_SIGNAL_UNKNOWN = 143
49};
50
51#ifdef CONFIG_USER_ONLY
52
53/* Map target signal numbers to GDB protocol signal numbers and vice
54 * versa. For user emulation's currently supported systems, we can
55 * assume most signals are defined.
56 */
57
58static int gdb_signal_table[] = {
59 0,
60 TARGET_SIGHUP,
61 TARGET_SIGINT,
62 TARGET_SIGQUIT,
63 TARGET_SIGILL,
64 TARGET_SIGTRAP,
65 TARGET_SIGABRT,
66 -1, /* SIGEMT */
67 TARGET_SIGFPE,
68 TARGET_SIGKILL,
69 TARGET_SIGBUS,
70 TARGET_SIGSEGV,
71 TARGET_SIGSYS,
72 TARGET_SIGPIPE,
73 TARGET_SIGALRM,
74 TARGET_SIGTERM,
75 TARGET_SIGURG,
76 TARGET_SIGSTOP,
77 TARGET_SIGTSTP,
78 TARGET_SIGCONT,
79 TARGET_SIGCHLD,
80 TARGET_SIGTTIN,
81 TARGET_SIGTTOU,
82 TARGET_SIGIO,
83 TARGET_SIGXCPU,
84 TARGET_SIGXFSZ,
85 TARGET_SIGVTALRM,
86 TARGET_SIGPROF,
87 TARGET_SIGWINCH,
88 -1, /* SIGLOST */
89 TARGET_SIGUSR1,
90 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +000091#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +000092 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +000093#else
94 -1,
95#endif
aurel32ca587a82008-12-18 22:44:13 +000096 -1, /* SIGPOLL */
97 -1,
98 -1,
99 -1,
100 -1,
101 -1,
102 -1,
103 -1,
104 -1,
105 -1,
106 -1,
107 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000108#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000109 __SIGRTMIN + 1,
110 __SIGRTMIN + 2,
111 __SIGRTMIN + 3,
112 __SIGRTMIN + 4,
113 __SIGRTMIN + 5,
114 __SIGRTMIN + 6,
115 __SIGRTMIN + 7,
116 __SIGRTMIN + 8,
117 __SIGRTMIN + 9,
118 __SIGRTMIN + 10,
119 __SIGRTMIN + 11,
120 __SIGRTMIN + 12,
121 __SIGRTMIN + 13,
122 __SIGRTMIN + 14,
123 __SIGRTMIN + 15,
124 __SIGRTMIN + 16,
125 __SIGRTMIN + 17,
126 __SIGRTMIN + 18,
127 __SIGRTMIN + 19,
128 __SIGRTMIN + 20,
129 __SIGRTMIN + 21,
130 __SIGRTMIN + 22,
131 __SIGRTMIN + 23,
132 __SIGRTMIN + 24,
133 __SIGRTMIN + 25,
134 __SIGRTMIN + 26,
135 __SIGRTMIN + 27,
136 __SIGRTMIN + 28,
137 __SIGRTMIN + 29,
138 __SIGRTMIN + 30,
139 __SIGRTMIN + 31,
140 -1, /* SIGCANCEL */
141 __SIGRTMIN,
142 __SIGRTMIN + 32,
143 __SIGRTMIN + 33,
144 __SIGRTMIN + 34,
145 __SIGRTMIN + 35,
146 __SIGRTMIN + 36,
147 __SIGRTMIN + 37,
148 __SIGRTMIN + 38,
149 __SIGRTMIN + 39,
150 __SIGRTMIN + 40,
151 __SIGRTMIN + 41,
152 __SIGRTMIN + 42,
153 __SIGRTMIN + 43,
154 __SIGRTMIN + 44,
155 __SIGRTMIN + 45,
156 __SIGRTMIN + 46,
157 __SIGRTMIN + 47,
158 __SIGRTMIN + 48,
159 __SIGRTMIN + 49,
160 __SIGRTMIN + 50,
161 __SIGRTMIN + 51,
162 __SIGRTMIN + 52,
163 __SIGRTMIN + 53,
164 __SIGRTMIN + 54,
165 __SIGRTMIN + 55,
166 __SIGRTMIN + 56,
167 __SIGRTMIN + 57,
168 __SIGRTMIN + 58,
169 __SIGRTMIN + 59,
170 __SIGRTMIN + 60,
171 __SIGRTMIN + 61,
172 __SIGRTMIN + 62,
173 __SIGRTMIN + 63,
174 __SIGRTMIN + 64,
175 __SIGRTMIN + 65,
176 __SIGRTMIN + 66,
177 __SIGRTMIN + 67,
178 __SIGRTMIN + 68,
179 __SIGRTMIN + 69,
180 __SIGRTMIN + 70,
181 __SIGRTMIN + 71,
182 __SIGRTMIN + 72,
183 __SIGRTMIN + 73,
184 __SIGRTMIN + 74,
185 __SIGRTMIN + 75,
186 __SIGRTMIN + 76,
187 __SIGRTMIN + 77,
188 __SIGRTMIN + 78,
189 __SIGRTMIN + 79,
190 __SIGRTMIN + 80,
191 __SIGRTMIN + 81,
192 __SIGRTMIN + 82,
193 __SIGRTMIN + 83,
194 __SIGRTMIN + 84,
195 __SIGRTMIN + 85,
196 __SIGRTMIN + 86,
197 __SIGRTMIN + 87,
198 __SIGRTMIN + 88,
199 __SIGRTMIN + 89,
200 __SIGRTMIN + 90,
201 __SIGRTMIN + 91,
202 __SIGRTMIN + 92,
203 __SIGRTMIN + 93,
204 __SIGRTMIN + 94,
205 __SIGRTMIN + 95,
206 -1, /* SIGINFO */
207 -1, /* UNKNOWN */
208 -1, /* DEFAULT */
209 -1,
210 -1,
211 -1,
212 -1,
213 -1,
214 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000215#endif
aurel32ca587a82008-12-18 22:44:13 +0000216};
bellard8f447cc2006-06-14 15:21:14 +0000217#else
aurel32ca587a82008-12-18 22:44:13 +0000218/* In system mode we only need SIGINT and SIGTRAP; other signals
219 are not yet supported. */
220
221enum {
222 TARGET_SIGINT = 2,
223 TARGET_SIGTRAP = 5
224};
225
226static int gdb_signal_table[] = {
227 -1,
228 -1,
229 TARGET_SIGINT,
230 -1,
231 -1,
232 TARGET_SIGTRAP
233};
bellard8f447cc2006-06-14 15:21:14 +0000234#endif
bellardb4608c02003-06-27 17:34:32 +0000235
aurel32ca587a82008-12-18 22:44:13 +0000236#ifdef CONFIG_USER_ONLY
237static int target_signal_to_gdb (int sig)
238{
239 int i;
240 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
241 if (gdb_signal_table[i] == sig)
242 return i;
243 return GDB_SIGNAL_UNKNOWN;
244}
245#endif
246
247static int gdb_signal_to_target (int sig)
248{
249 if (sig < ARRAY_SIZE (gdb_signal_table))
250 return gdb_signal_table[sig];
251 else
252 return -1;
253}
254
bellard4abe6152003-07-26 18:01:58 +0000255//#define DEBUG_GDB
bellardb4608c02003-06-27 17:34:32 +0000256
pbrook56aebc82008-10-11 17:55:29 +0000257typedef struct GDBRegisterState {
258 int base_reg;
259 int num_regs;
260 gdb_reg_cb get_reg;
261 gdb_reg_cb set_reg;
262 const char *xml;
263 struct GDBRegisterState *next;
264} GDBRegisterState;
265
bellard858693c2004-03-31 18:52:07 +0000266enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000267 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000268 RS_IDLE,
269 RS_GETLINE,
270 RS_CHKSUM1,
271 RS_CHKSUM2,
pbrooka2d1eba2007-01-28 03:10:55 +0000272 RS_SYSCALL,
bellard858693c2004-03-31 18:52:07 +0000273};
bellard858693c2004-03-31 18:52:07 +0000274typedef struct GDBState {
aliguori880a7572008-11-18 20:30:24 +0000275 CPUState *c_cpu; /* current CPU for step/continue ops */
276 CPUState *g_cpu; /* current CPU for other ops */
277 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000278 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000279 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000280 int line_buf_index;
281 int line_csum;
pbrook56aebc82008-10-11 17:55:29 +0000282 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000283 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000284 int signal;
bellard41625032005-04-24 10:07:11 +0000285#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000286 int fd;
bellard41625032005-04-24 10:07:11 +0000287 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000288#else
289 CharDriverState *chr;
aliguori8a34a0f2009-03-05 23:01:55 +0000290 CharDriverState *mon_chr;
bellard41625032005-04-24 10:07:11 +0000291#endif
bellard858693c2004-03-31 18:52:07 +0000292} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000293
edgar_igl60897d32008-05-09 08:25:14 +0000294/* By default use no IRQs and no timers while single stepping so as to
295 * make single stepping like an ICE HW step.
296 */
297static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
298
aliguori880a7572008-11-18 20:30:24 +0000299static GDBState *gdbserver_state;
300
pbrook56aebc82008-10-11 17:55:29 +0000301/* This is an ugly hack to cope with both new and old gdb.
302 If gdb sends qXfer:features:read then assume we're talking to a newish
303 gdb that understands target descriptions. */
304static int gdb_has_xml;
305
bellard1fddef42005-04-17 19:16:13 +0000306#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000307/* XXX: This is not thread safe. Do we care? */
308static int gdbserver_fd = -1;
309
bellard858693c2004-03-31 18:52:07 +0000310static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000311{
312 uint8_t ch;
313 int ret;
314
315 for(;;) {
bellard8f447cc2006-06-14 15:21:14 +0000316 ret = recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000317 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000318 if (errno == ECONNRESET)
319 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000320 if (errno != EINTR && errno != EAGAIN)
321 return -1;
322 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000323 close(s->fd);
324 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000325 return -1;
326 } else {
327 break;
328 }
329 }
330 return ch;
331}
pbrook4046d912007-01-28 01:53:16 +0000332#endif
bellardb4608c02003-06-27 17:34:32 +0000333
pbrooka2d1eba2007-01-28 03:10:55 +0000334static gdb_syscall_complete_cb gdb_current_syscall_cb;
335
blueswir1654efcf2009-04-18 07:29:59 +0000336static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000337 GDB_SYS_UNKNOWN,
338 GDB_SYS_ENABLED,
339 GDB_SYS_DISABLED,
340} gdb_syscall_mode;
341
342/* If gdb is connected when the first semihosting syscall occurs then use
343 remote gdb syscalls. Otherwise use native file IO. */
344int use_gdb_syscalls(void)
345{
346 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000347 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
348 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000349 }
350 return gdb_syscall_mode == GDB_SYS_ENABLED;
351}
352
edgar_iglba70a622008-03-14 06:10:42 +0000353/* Resume execution. */
354static inline void gdb_continue(GDBState *s)
355{
356#ifdef CONFIG_USER_ONLY
357 s->running_state = 1;
358#else
359 vm_start();
360#endif
361}
362
bellard858693c2004-03-31 18:52:07 +0000363static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000364{
pbrook4046d912007-01-28 01:53:16 +0000365#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000366 int ret;
367
368 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000369 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000370 if (ret < 0) {
371 if (errno != EINTR && errno != EAGAIN)
372 return;
373 } else {
374 buf += ret;
375 len -= ret;
376 }
377 }
pbrook4046d912007-01-28 01:53:16 +0000378#else
379 qemu_chr_write(s->chr, buf, len);
380#endif
bellardb4608c02003-06-27 17:34:32 +0000381}
382
383static inline int fromhex(int v)
384{
385 if (v >= '0' && v <= '9')
386 return v - '0';
387 else if (v >= 'A' && v <= 'F')
388 return v - 'A' + 10;
389 else if (v >= 'a' && v <= 'f')
390 return v - 'a' + 10;
391 else
392 return 0;
393}
394
395static inline int tohex(int v)
396{
397 if (v < 10)
398 return v + '0';
399 else
400 return v - 10 + 'a';
401}
402
403static void memtohex(char *buf, const uint8_t *mem, int len)
404{
405 int i, c;
406 char *q;
407 q = buf;
408 for(i = 0; i < len; i++) {
409 c = mem[i];
410 *q++ = tohex(c >> 4);
411 *q++ = tohex(c & 0xf);
412 }
413 *q = '\0';
414}
415
416static void hextomem(uint8_t *mem, const char *buf, int len)
417{
418 int i;
419
420 for(i = 0; i < len; i++) {
421 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
422 buf += 2;
423 }
424}
425
bellardb4608c02003-06-27 17:34:32 +0000426/* return -1 if error, 0 if OK */
pbrook56aebc82008-10-11 17:55:29 +0000427static int put_packet_binary(GDBState *s, const char *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000428{
pbrook56aebc82008-10-11 17:55:29 +0000429 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000430 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000431
bellardb4608c02003-06-27 17:34:32 +0000432 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000433 p = s->last_packet;
434 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000435 memcpy(p, buf, len);
436 p += len;
bellardb4608c02003-06-27 17:34:32 +0000437 csum = 0;
438 for(i = 0; i < len; i++) {
439 csum += buf[i];
440 }
pbrook4046d912007-01-28 01:53:16 +0000441 *(p++) = '#';
442 *(p++) = tohex((csum >> 4) & 0xf);
443 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000444
pbrook4046d912007-01-28 01:53:16 +0000445 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000446 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000447
pbrook4046d912007-01-28 01:53:16 +0000448#ifdef CONFIG_USER_ONLY
449 i = get_char(s);
450 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000451 return -1;
pbrook4046d912007-01-28 01:53:16 +0000452 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000453 break;
pbrook4046d912007-01-28 01:53:16 +0000454#else
455 break;
456#endif
bellardb4608c02003-06-27 17:34:32 +0000457 }
458 return 0;
459}
460
pbrook56aebc82008-10-11 17:55:29 +0000461/* return -1 if error, 0 if OK */
462static int put_packet(GDBState *s, const char *buf)
463{
464#ifdef DEBUG_GDB
465 printf("reply='%s'\n", buf);
466#endif
467
468 return put_packet_binary(s, buf, strlen(buf));
469}
470
471/* The GDB remote protocol transfers values in target byte order. This means
472 we can use the raw memory access routines to access the value buffer.
473 Conveniently, these also handle the case where the buffer is mis-aligned.
474 */
475#define GET_REG8(val) do { \
476 stb_p(mem_buf, val); \
477 return 1; \
478 } while(0)
479#define GET_REG16(val) do { \
480 stw_p(mem_buf, val); \
481 return 2; \
482 } while(0)
483#define GET_REG32(val) do { \
484 stl_p(mem_buf, val); \
485 return 4; \
486 } while(0)
487#define GET_REG64(val) do { \
488 stq_p(mem_buf, val); \
489 return 8; \
490 } while(0)
491
492#if TARGET_LONG_BITS == 64
493#define GET_REGL(val) GET_REG64(val)
494#define ldtul_p(addr) ldq_p(addr)
495#else
496#define GET_REGL(val) GET_REG32(val)
497#define ldtul_p(addr) ldl_p(addr)
498#endif
499
edgar_iglfde3fd62008-05-09 08:50:01 +0000500#if defined(TARGET_I386)
balrog5ad265e2007-10-31 00:21:35 +0000501
502#ifdef TARGET_X86_64
pbrook56aebc82008-10-11 17:55:29 +0000503static const int gpr_map[16] = {
bellard79808572008-05-09 14:40:22 +0000504 R_EAX, R_EBX, R_ECX, R_EDX, R_ESI, R_EDI, R_EBP, R_ESP,
pbrook56aebc82008-10-11 17:55:29 +0000505 8, 9, 10, 11, 12, 13, 14, 15
bellard79808572008-05-09 14:40:22 +0000506};
bellard79808572008-05-09 14:40:22 +0000507#else
Jan Kiszka5f30fa12009-09-17 18:14:13 +0200508#define gpr_map gpr_map32
bellard79808572008-05-09 14:40:22 +0000509#endif
Jan Kiszka5f30fa12009-09-17 18:14:13 +0200510static const int gpr_map32[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
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) {
Jan Kiszka5f30fa12009-09-17 18:14:13 +0200524 if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
525 GET_REG64(env->regs[gpr_map[n]]);
526 } else if (n < CPU_NB_REGS32) {
527 GET_REG32(env->regs[gpr_map32[n]]);
528 }
Jan Kiszkab1631e72009-06-27 09:53:51 +0200529 } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
pbrook56aebc82008-10-11 17:55:29 +0000530#ifdef USE_X86LDOUBLE
Jan Kiszkab1631e72009-06-27 09:53:51 +0200531 /* FIXME: byteswap float values - after fixing fpregs layout. */
532 memcpy(mem_buf, &env->fpregs[n - IDX_FP_REGS], 10);
pbrook56aebc82008-10-11 17:55:29 +0000533#else
534 memset(mem_buf, 0, 10);
535#endif
536 return 10;
Jan Kiszkab1631e72009-06-27 09:53:51 +0200537 } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
538 n -= IDX_XMM_REGS;
Jan Kiszka5f30fa12009-09-17 18:14:13 +0200539 if (n < CPU_NB_REGS32 ||
540 (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK)) {
541 stq_p(mem_buf, env->xmm_regs[n].XMM_Q(0));
542 stq_p(mem_buf + 8, env->xmm_regs[n].XMM_Q(1));
543 return 16;
544 }
pbrook56aebc82008-10-11 17:55:29 +0000545 } else {
pbrook56aebc82008-10-11 17:55:29 +0000546 switch (n) {
Jan Kiszka5f30fa12009-09-17 18:14:13 +0200547 case IDX_IP_REG:
548 if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
549 GET_REG64(env->eip);
550 } else {
551 GET_REG32(env->eip);
552 }
Jan Kiszkab1631e72009-06-27 09:53:51 +0200553 case IDX_FLAGS_REG: GET_REG32(env->eflags);
554
555 case IDX_SEG_REGS: GET_REG32(env->segs[R_CS].selector);
556 case IDX_SEG_REGS + 1: GET_REG32(env->segs[R_SS].selector);
557 case IDX_SEG_REGS + 2: GET_REG32(env->segs[R_DS].selector);
558 case IDX_SEG_REGS + 3: GET_REG32(env->segs[R_ES].selector);
559 case IDX_SEG_REGS + 4: GET_REG32(env->segs[R_FS].selector);
560 case IDX_SEG_REGS + 5: GET_REG32(env->segs[R_GS].selector);
561
562 case IDX_FP_REGS + 8: GET_REG32(env->fpuc);
563 case IDX_FP_REGS + 9: GET_REG32((env->fpus & ~0x3800) |
564 (env->fpstt & 0x7) << 11);
565 case IDX_FP_REGS + 10: GET_REG32(0); /* ftag */
566 case IDX_FP_REGS + 11: GET_REG32(0); /* fiseg */
567 case IDX_FP_REGS + 12: GET_REG32(0); /* fioff */
568 case IDX_FP_REGS + 13: GET_REG32(0); /* foseg */
569 case IDX_FP_REGS + 14: GET_REG32(0); /* fooff */
570 case IDX_FP_REGS + 15: GET_REG32(0); /* fop */
571
572 case IDX_MXCSR_REG: GET_REG32(env->mxcsr);
pbrook56aebc82008-10-11 17:55:29 +0000573 }
bellard79808572008-05-09 14:40:22 +0000574 }
pbrook56aebc82008-10-11 17:55:29 +0000575 return 0;
bellard79808572008-05-09 14:40:22 +0000576}
577
Jan Kiszka84273172009-06-27 09:53:51 +0200578static int cpu_x86_gdb_load_seg(CPUState *env, int sreg, uint8_t *mem_buf)
579{
580 uint16_t selector = ldl_p(mem_buf);
581
582 if (selector != env->segs[sreg].selector) {
583#if defined(CONFIG_USER_ONLY)
584 cpu_x86_load_seg(env, sreg, selector);
585#else
586 unsigned int limit, flags;
587 target_ulong base;
588
589 if (!(env->cr[0] & CR0_PE_MASK) || (env->eflags & VM_MASK)) {
590 base = selector << 4;
591 limit = 0xffff;
592 flags = 0;
593 } else {
594 if (!cpu_x86_get_descr_debug(env, selector, &base, &limit, &flags))
595 return 4;
596 }
597 cpu_x86_load_seg_cache(env, sreg, selector, base, limit, flags);
598#endif
599 }
600 return 4;
601}
602
Jan Kiszkab1631e72009-06-27 09:53:51 +0200603static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellard79808572008-05-09 14:40:22 +0000604{
pbrook56aebc82008-10-11 17:55:29 +0000605 uint32_t tmp;
606
Jan Kiszkab1631e72009-06-27 09:53:51 +0200607 if (n < CPU_NB_REGS) {
Jan Kiszka5f30fa12009-09-17 18:14:13 +0200608 if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
609 env->regs[gpr_map[n]] = ldtul_p(mem_buf);
610 return sizeof(target_ulong);
611 } else if (n < CPU_NB_REGS32) {
612 n = gpr_map32[n];
613 env->regs[n] &= ~0xffffffffUL;
614 env->regs[n] |= (uint32_t)ldl_p(mem_buf);
615 return 4;
616 }
Jan Kiszkab1631e72009-06-27 09:53:51 +0200617 } else if (n >= IDX_FP_REGS && n < IDX_FP_REGS + 8) {
pbrook56aebc82008-10-11 17:55:29 +0000618#ifdef USE_X86LDOUBLE
Jan Kiszkab1631e72009-06-27 09:53:51 +0200619 /* FIXME: byteswap float values - after fixing fpregs layout. */
620 memcpy(&env->fpregs[n - IDX_FP_REGS], mem_buf, 10);
pbrook56aebc82008-10-11 17:55:29 +0000621#endif
622 return 10;
Jan Kiszkab1631e72009-06-27 09:53:51 +0200623 } else if (n >= IDX_XMM_REGS && n < IDX_XMM_REGS + CPU_NB_REGS) {
624 n -= IDX_XMM_REGS;
Jan Kiszka5f30fa12009-09-17 18:14:13 +0200625 if (n < CPU_NB_REGS32 ||
626 (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK)) {
627 env->xmm_regs[n].XMM_Q(0) = ldq_p(mem_buf);
628 env->xmm_regs[n].XMM_Q(1) = ldq_p(mem_buf + 8);
629 return 16;
630 }
pbrook56aebc82008-10-11 17:55:29 +0000631 } else {
Jan Kiszkab1631e72009-06-27 09:53:51 +0200632 switch (n) {
633 case IDX_IP_REG:
Jan Kiszka5f30fa12009-09-17 18:14:13 +0200634 if (TARGET_LONG_BITS == 64 && env->hflags & HF_CS64_MASK) {
635 env->eip = ldq_p(mem_buf);
636 return 8;
637 } else {
638 env->eip &= ~0xffffffffUL;
639 env->eip |= (uint32_t)ldl_p(mem_buf);
640 return 4;
641 }
Jan Kiszkab1631e72009-06-27 09:53:51 +0200642 case IDX_FLAGS_REG:
643 env->eflags = ldl_p(mem_buf);
644 return 4;
645
Jan Kiszka84273172009-06-27 09:53:51 +0200646 case IDX_SEG_REGS: return cpu_x86_gdb_load_seg(env, R_CS, mem_buf);
647 case IDX_SEG_REGS + 1: return cpu_x86_gdb_load_seg(env, R_SS, mem_buf);
648 case IDX_SEG_REGS + 2: return cpu_x86_gdb_load_seg(env, R_DS, mem_buf);
649 case IDX_SEG_REGS + 3: return cpu_x86_gdb_load_seg(env, R_ES, mem_buf);
650 case IDX_SEG_REGS + 4: return cpu_x86_gdb_load_seg(env, R_FS, mem_buf);
651 case IDX_SEG_REGS + 5: return cpu_x86_gdb_load_seg(env, R_GS, mem_buf);
Jan Kiszkab1631e72009-06-27 09:53:51 +0200652
653 case IDX_FP_REGS + 8:
654 env->fpuc = ldl_p(mem_buf);
655 return 4;
656 case IDX_FP_REGS + 9:
657 tmp = ldl_p(mem_buf);
658 env->fpstt = (tmp >> 11) & 7;
659 env->fpus = tmp & ~0x3800;
660 return 4;
661 case IDX_FP_REGS + 10: /* ftag */ return 4;
662 case IDX_FP_REGS + 11: /* fiseg */ return 4;
663 case IDX_FP_REGS + 12: /* fioff */ return 4;
664 case IDX_FP_REGS + 13: /* foseg */ return 4;
665 case IDX_FP_REGS + 14: /* fooff */ return 4;
666 case IDX_FP_REGS + 15: /* fop */ return 4;
667
668 case IDX_MXCSR_REG:
669 env->mxcsr = ldl_p(mem_buf);
670 return 4;
bellard79808572008-05-09 14:40:22 +0000671 }
bellard79808572008-05-09 14:40:22 +0000672 }
pbrook56aebc82008-10-11 17:55:29 +0000673 /* Unrecognised register. */
674 return 0;
bellard6da41ea2004-01-04 15:48:38 +0000675}
676
bellard9e62fd72004-01-05 22:49:06 +0000677#elif defined (TARGET_PPC)
pbrook56aebc82008-10-11 17:55:29 +0000678
aurel32e571cb42009-01-24 15:07:42 +0000679/* Old gdb always expects FP registers. Newer (xml-aware) gdb only
680 expects whatever the target description contains. Due to a
681 historical mishap the FP registers appear in between core integer
682 regs and PC, MSR, CR, and so forth. We hack round this by giving the
683 FP regs zero size when talking to a newer gdb. */
pbrook56aebc82008-10-11 17:55:29 +0000684#define NUM_CORE_REGS 71
aurel32e571cb42009-01-24 15:07:42 +0000685#if defined (TARGET_PPC64)
686#define GDB_CORE_XML "power64-core.xml"
687#else
688#define GDB_CORE_XML "power-core.xml"
689#endif
pbrook56aebc82008-10-11 17:55:29 +0000690
691static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
bellard9e62fd72004-01-05 22:49:06 +0000692{
pbrook56aebc82008-10-11 17:55:29 +0000693 if (n < 32) {
694 /* gprs */
695 GET_REGL(env->gpr[n]);
696 } else if (n < 64) {
697 /* fprs */
aurel32e571cb42009-01-24 15:07:42 +0000698 if (gdb_has_xml)
699 return 0;
aurel328d4acf92008-11-30 16:23:18 +0000700 stfq_p(mem_buf, env->fpr[n-32]);
pbrook56aebc82008-10-11 17:55:29 +0000701 return 8;
702 } else {
703 switch (n) {
704 case 64: GET_REGL(env->nip);
705 case 65: GET_REGL(env->msr);
706 case 66:
707 {
708 uint32_t cr = 0;
709 int i;
710 for (i = 0; i < 8; i++)
711 cr |= env->crf[i] << (32 - ((i + 1) * 4));
712 GET_REG32(cr);
713 }
714 case 67: GET_REGL(env->lr);
715 case 68: GET_REGL(env->ctr);
aurel323d7b4172008-10-21 11:28:46 +0000716 case 69: GET_REGL(env->xer);
aurel32e571cb42009-01-24 15:07:42 +0000717 case 70:
718 {
719 if (gdb_has_xml)
720 return 0;
721 GET_REG32(0); /* fpscr */
722 }
pbrook56aebc82008-10-11 17:55:29 +0000723 }
bellard9e62fd72004-01-05 22:49:06 +0000724 }
pbrook56aebc82008-10-11 17:55:29 +0000725 return 0;
bellard9e62fd72004-01-05 22:49:06 +0000726}
727
pbrook56aebc82008-10-11 17:55:29 +0000728static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellard9e62fd72004-01-05 22:49:06 +0000729{
pbrook56aebc82008-10-11 17:55:29 +0000730 if (n < 32) {
731 /* gprs */
732 env->gpr[n] = ldtul_p(mem_buf);
733 return sizeof(target_ulong);
734 } else if (n < 64) {
735 /* fprs */
aurel32e571cb42009-01-24 15:07:42 +0000736 if (gdb_has_xml)
737 return 0;
aurel328d4acf92008-11-30 16:23:18 +0000738 env->fpr[n-32] = ldfq_p(mem_buf);
pbrook56aebc82008-10-11 17:55:29 +0000739 return 8;
740 } else {
741 switch (n) {
742 case 64:
743 env->nip = ldtul_p(mem_buf);
744 return sizeof(target_ulong);
745 case 65:
746 ppc_store_msr(env, ldtul_p(mem_buf));
747 return sizeof(target_ulong);
748 case 66:
749 {
750 uint32_t cr = ldl_p(mem_buf);
751 int i;
752 for (i = 0; i < 8; i++)
753 env->crf[i] = (cr >> (32 - ((i + 1) * 4))) & 0xF;
754 return 4;
755 }
756 case 67:
757 env->lr = ldtul_p(mem_buf);
758 return sizeof(target_ulong);
759 case 68:
760 env->ctr = ldtul_p(mem_buf);
761 return sizeof(target_ulong);
762 case 69:
aurel323d7b4172008-10-21 11:28:46 +0000763 env->xer = ldtul_p(mem_buf);
764 return sizeof(target_ulong);
pbrook56aebc82008-10-11 17:55:29 +0000765 case 70:
766 /* fpscr */
aurel32e571cb42009-01-24 15:07:42 +0000767 if (gdb_has_xml)
768 return 0;
pbrook56aebc82008-10-11 17:55:29 +0000769 return 4;
770 }
bellard9e62fd72004-01-05 22:49:06 +0000771 }
pbrook56aebc82008-10-11 17:55:29 +0000772 return 0;
bellarde95c8d52004-09-30 22:22:08 +0000773}
pbrook56aebc82008-10-11 17:55:29 +0000774
bellarde95c8d52004-09-30 22:22:08 +0000775#elif defined (TARGET_SPARC)
bellarde95c8d52004-09-30 22:22:08 +0000776
pbrook56aebc82008-10-11 17:55:29 +0000777#if defined(TARGET_SPARC64) && !defined(TARGET_ABI32)
778#define NUM_CORE_REGS 86
779#else
blueswir15a377912009-01-13 16:28:01 +0000780#define NUM_CORE_REGS 72
pbrook56aebc82008-10-11 17:55:29 +0000781#endif
782
783#ifdef TARGET_ABI32
784#define GET_REGA(val) GET_REG32(val)
785#else
786#define GET_REGA(val) GET_REGL(val)
787#endif
788
789static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
790{
791 if (n < 8) {
792 /* g0..g7 */
793 GET_REGA(env->gregs[n]);
bellarde95c8d52004-09-30 22:22:08 +0000794 }
pbrook56aebc82008-10-11 17:55:29 +0000795 if (n < 32) {
796 /* register window */
797 GET_REGA(env->regwptr[n - 8]);
bellarde95c8d52004-09-30 22:22:08 +0000798 }
pbrook56aebc82008-10-11 17:55:29 +0000799#if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
800 if (n < 64) {
801 /* fprs */
802 GET_REG32(*((uint32_t *)&env->fpr[n - 32]));
bellarde95c8d52004-09-30 22:22:08 +0000803 }
804 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
pbrook56aebc82008-10-11 17:55:29 +0000805 switch (n) {
806 case 64: GET_REGA(env->y);
807 case 65: GET_REGA(GET_PSR(env));
808 case 66: GET_REGA(env->wim);
809 case 67: GET_REGA(env->tbr);
810 case 68: GET_REGA(env->pc);
811 case 69: GET_REGA(env->npc);
812 case 70: GET_REGA(env->fsr);
813 case 71: GET_REGA(0); /* csr */
blueswir15a377912009-01-13 16:28:01 +0000814 default: GET_REGA(0);
bellard34751872005-07-02 14:31:34 +0000815 }
bellard34751872005-07-02 14:31:34 +0000816#else
pbrook56aebc82008-10-11 17:55:29 +0000817 if (n < 64) {
818 /* f0-f31 */
819 GET_REG32(*((uint32_t *)&env->fpr[n - 32]));
bellard34751872005-07-02 14:31:34 +0000820 }
pbrook56aebc82008-10-11 17:55:29 +0000821 if (n < 80) {
822 /* f32-f62 (double width, even numbers only) */
823 uint64_t val;
824
825 val = (uint64_t)*((uint32_t *)&env->fpr[(n - 64) * 2 + 32]) << 32;
826 val |= *((uint32_t *)&env->fpr[(n - 64) * 2 + 33]);
827 GET_REG64(val);
828 }
829 switch (n) {
830 case 80: GET_REGL(env->pc);
831 case 81: GET_REGL(env->npc);
832 case 82: GET_REGL(((uint64_t)GET_CCR(env) << 32) |
blueswir117d996e2007-07-07 20:53:22 +0000833 ((env->asi & 0xff) << 24) |
834 ((env->pstate & 0xfff) << 8) |
835 GET_CWP64(env));
pbrook56aebc82008-10-11 17:55:29 +0000836 case 83: GET_REGL(env->fsr);
837 case 84: GET_REGL(env->fprs);
838 case 85: GET_REGL(env->y);
839 }
bellard34751872005-07-02 14:31:34 +0000840#endif
pbrook56aebc82008-10-11 17:55:29 +0000841 return 0;
bellarde95c8d52004-09-30 22:22:08 +0000842}
843
pbrook56aebc82008-10-11 17:55:29 +0000844static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellarde95c8d52004-09-30 22:22:08 +0000845{
pbrook56aebc82008-10-11 17:55:29 +0000846#if defined(TARGET_ABI32)
847 abi_ulong tmp;
848
849 tmp = ldl_p(mem_buf);
blueswir196d19122008-06-07 08:03:05 +0000850#else
pbrook56aebc82008-10-11 17:55:29 +0000851 target_ulong tmp;
852
853 tmp = ldtul_p(mem_buf);
blueswir196d19122008-06-07 08:03:05 +0000854#endif
bellarde95c8d52004-09-30 22:22:08 +0000855
pbrook56aebc82008-10-11 17:55:29 +0000856 if (n < 8) {
857 /* g0..g7 */
858 env->gregs[n] = tmp;
859 } else if (n < 32) {
860 /* register window */
861 env->regwptr[n - 8] = tmp;
bellarde95c8d52004-09-30 22:22:08 +0000862 }
pbrook56aebc82008-10-11 17:55:29 +0000863#if defined(TARGET_ABI32) || !defined(TARGET_SPARC64)
864 else if (n < 64) {
865 /* fprs */
866 *((uint32_t *)&env->fpr[n - 32]) = tmp;
867 } else {
868 /* Y, PSR, WIM, TBR, PC, NPC, FPSR, CPSR */
869 switch (n) {
870 case 64: env->y = tmp; break;
871 case 65: PUT_PSR(env, tmp); break;
872 case 66: env->wim = tmp; break;
873 case 67: env->tbr = tmp; break;
874 case 68: env->pc = tmp; break;
875 case 69: env->npc = tmp; break;
876 case 70: env->fsr = tmp; break;
877 default: return 0;
878 }
bellarde95c8d52004-09-30 22:22:08 +0000879 }
pbrook56aebc82008-10-11 17:55:29 +0000880 return 4;
bellard34751872005-07-02 14:31:34 +0000881#else
pbrook56aebc82008-10-11 17:55:29 +0000882 else if (n < 64) {
883 /* f0-f31 */
pbrook56aebc82008-10-11 17:55:29 +0000884 env->fpr[n] = ldfl_p(mem_buf);
885 return 4;
886 } else if (n < 80) {
887 /* f32-f62 (double width, even numbers only) */
888 *((uint32_t *)&env->fpr[(n - 64) * 2 + 32]) = tmp >> 32;
889 *((uint32_t *)&env->fpr[(n - 64) * 2 + 33]) = tmp;
890 } else {
891 switch (n) {
892 case 80: env->pc = tmp; break;
893 case 81: env->npc = tmp; break;
894 case 82:
895 PUT_CCR(env, tmp >> 32);
896 env->asi = (tmp >> 24) & 0xff;
897 env->pstate = (tmp >> 8) & 0xfff;
898 PUT_CWP64(env, tmp & 0xff);
899 break;
900 case 83: env->fsr = tmp; break;
901 case 84: env->fprs = tmp; break;
902 case 85: env->y = tmp; break;
903 default: return 0;
904 }
bellard34751872005-07-02 14:31:34 +0000905 }
pbrook56aebc82008-10-11 17:55:29 +0000906 return 8;
bellard34751872005-07-02 14:31:34 +0000907#endif
bellard9e62fd72004-01-05 22:49:06 +0000908}
bellard1fddef42005-04-17 19:16:13 +0000909#elif defined (TARGET_ARM)
pbrook56aebc82008-10-11 17:55:29 +0000910
911/* Old gdb always expect FPA registers. Newer (xml-aware) gdb only expect
912 whatever the target description contains. Due to a historical mishap
913 the FPA registers appear in between core integer regs and the CPSR.
914 We hack round this by giving the FPA regs zero size when talking to a
915 newer gdb. */
916#define NUM_CORE_REGS 26
917#define GDB_CORE_XML "arm-core.xml"
918
919static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
bellard1fddef42005-04-17 19:16:13 +0000920{
pbrook56aebc82008-10-11 17:55:29 +0000921 if (n < 16) {
922 /* Core integer register. */
923 GET_REG32(env->regs[n]);
924 }
925 if (n < 24) {
926 /* FPA registers. */
927 if (gdb_has_xml)
928 return 0;
929 memset(mem_buf, 0, 12);
930 return 12;
931 }
932 switch (n) {
933 case 24:
934 /* FPA status register. */
935 if (gdb_has_xml)
936 return 0;
937 GET_REG32(0);
938 case 25:
939 /* CPSR */
940 GET_REG32(cpsr_read(env));
941 }
942 /* Unknown register. */
943 return 0;
bellard1fddef42005-04-17 19:16:13 +0000944}
945
pbrook56aebc82008-10-11 17:55:29 +0000946static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellard1fddef42005-04-17 19:16:13 +0000947{
pbrook56aebc82008-10-11 17:55:29 +0000948 uint32_t tmp;
bellard1fddef42005-04-17 19:16:13 +0000949
pbrook56aebc82008-10-11 17:55:29 +0000950 tmp = ldl_p(mem_buf);
951
952 /* Mask out low bit of PC to workaround gdb bugs. This will probably
953 cause problems if we ever implement the Jazelle DBX extensions. */
954 if (n == 15)
955 tmp &= ~1;
956
957 if (n < 16) {
958 /* Core integer register. */
959 env->regs[n] = tmp;
960 return 4;
961 }
962 if (n < 24) { /* 16-23 */
963 /* FPA registers (ignored). */
964 if (gdb_has_xml)
965 return 0;
966 return 12;
967 }
968 switch (n) {
969 case 24:
970 /* FPA status register (ignored). */
971 if (gdb_has_xml)
972 return 0;
973 return 4;
974 case 25:
975 /* CPSR */
976 cpsr_write (env, tmp, 0xffffffff);
977 return 4;
978 }
979 /* Unknown register. */
980 return 0;
bellard1fddef42005-04-17 19:16:13 +0000981}
pbrook56aebc82008-10-11 17:55:29 +0000982
pbrooke6e59062006-10-22 00:18:54 +0000983#elif defined (TARGET_M68K)
pbrook56aebc82008-10-11 17:55:29 +0000984
985#define NUM_CORE_REGS 18
986
987#define GDB_CORE_XML "cf-core.xml"
988
989static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
pbrooke6e59062006-10-22 00:18:54 +0000990{
pbrook56aebc82008-10-11 17:55:29 +0000991 if (n < 8) {
992 /* D0-D7 */
993 GET_REG32(env->dregs[n]);
994 } else if (n < 16) {
995 /* A0-A7 */
996 GET_REG32(env->aregs[n - 8]);
997 } else {
998 switch (n) {
999 case 16: GET_REG32(env->sr);
1000 case 17: GET_REG32(env->pc);
1001 }
pbrooke6e59062006-10-22 00:18:54 +00001002 }
pbrook56aebc82008-10-11 17:55:29 +00001003 /* FP registers not included here because they vary between
1004 ColdFire and m68k. Use XML bits for these. */
1005 return 0;
pbrooke6e59062006-10-22 00:18:54 +00001006}
1007
pbrook56aebc82008-10-11 17:55:29 +00001008static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
pbrooke6e59062006-10-22 00:18:54 +00001009{
pbrook56aebc82008-10-11 17:55:29 +00001010 uint32_t tmp;
pbrooke6e59062006-10-22 00:18:54 +00001011
pbrook56aebc82008-10-11 17:55:29 +00001012 tmp = ldl_p(mem_buf);
1013
1014 if (n < 8) {
1015 /* D0-D7 */
1016 env->dregs[n] = tmp;
1017 } else if (n < 8) {
1018 /* A0-A7 */
1019 env->aregs[n - 8] = tmp;
1020 } else {
1021 switch (n) {
1022 case 16: env->sr = tmp; break;
1023 case 17: env->pc = tmp; break;
1024 default: return 0;
1025 }
pbrooke6e59062006-10-22 00:18:54 +00001026 }
pbrook56aebc82008-10-11 17:55:29 +00001027 return 4;
pbrooke6e59062006-10-22 00:18:54 +00001028}
bellard6f970bd2005-12-05 19:55:19 +00001029#elif defined (TARGET_MIPS)
pbrook56aebc82008-10-11 17:55:29 +00001030
1031#define NUM_CORE_REGS 73
1032
1033static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
bellard6f970bd2005-12-05 19:55:19 +00001034{
pbrook56aebc82008-10-11 17:55:29 +00001035 if (n < 32) {
1036 GET_REGL(env->active_tc.gpr[n]);
1037 }
1038 if (env->CP0_Config1 & (1 << CP0C1_FP)) {
1039 if (n >= 38 && n < 70) {
ths7ac256b2007-10-25 21:30:37 +00001040 if (env->CP0_Status & (1 << CP0St_FR))
pbrook56aebc82008-10-11 17:55:29 +00001041 GET_REGL(env->active_fpu.fpr[n - 38].d);
ths7ac256b2007-10-25 21:30:37 +00001042 else
pbrook56aebc82008-10-11 17:55:29 +00001043 GET_REGL(env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX]);
1044 }
1045 switch (n) {
1046 case 70: GET_REGL((int32_t)env->active_fpu.fcr31);
1047 case 71: GET_REGL((int32_t)env->active_fpu.fcr0);
1048 }
1049 }
1050 switch (n) {
1051 case 32: GET_REGL((int32_t)env->CP0_Status);
1052 case 33: GET_REGL(env->active_tc.LO[0]);
1053 case 34: GET_REGL(env->active_tc.HI[0]);
1054 case 35: GET_REGL(env->CP0_BadVAddr);
1055 case 36: GET_REGL((int32_t)env->CP0_Cause);
1056 case 37: GET_REGL(env->active_tc.PC);
1057 case 72: GET_REGL(0); /* fp */
1058 case 89: GET_REGL((int32_t)env->CP0_PRid);
1059 }
1060 if (n >= 73 && n <= 88) {
1061 /* 16 embedded regs. */
1062 GET_REGL(0);
1063 }
ths36d23952007-02-28 22:37:42 +00001064
pbrook56aebc82008-10-11 17:55:29 +00001065 return 0;
bellard6f970bd2005-12-05 19:55:19 +00001066}
1067
ths8e33c082006-12-11 19:22:27 +00001068/* convert MIPS rounding mode in FCR31 to IEEE library */
1069static unsigned int ieee_rm[] =
1070 {
1071 float_round_nearest_even,
1072 float_round_to_zero,
1073 float_round_up,
1074 float_round_down
1075 };
1076#define RESTORE_ROUNDING_MODE \
thsf01be152008-09-18 11:57:27 +00001077 set_float_rounding_mode(ieee_rm[env->active_fpu.fcr31 & 3], &env->active_fpu.fp_status)
ths8e33c082006-12-11 19:22:27 +00001078
pbrook56aebc82008-10-11 17:55:29 +00001079static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellard6f970bd2005-12-05 19:55:19 +00001080{
pbrook56aebc82008-10-11 17:55:29 +00001081 target_ulong tmp;
bellard6f970bd2005-12-05 19:55:19 +00001082
pbrook56aebc82008-10-11 17:55:29 +00001083 tmp = ldtul_p(mem_buf);
bellard6f970bd2005-12-05 19:55:19 +00001084
pbrook56aebc82008-10-11 17:55:29 +00001085 if (n < 32) {
1086 env->active_tc.gpr[n] = tmp;
1087 return sizeof(target_ulong);
1088 }
1089 if (env->CP0_Config1 & (1 << CP0C1_FP)
1090 && n >= 38 && n < 73) {
1091 if (n < 70) {
ths7ac256b2007-10-25 21:30:37 +00001092 if (env->CP0_Status & (1 << CP0St_FR))
pbrook56aebc82008-10-11 17:55:29 +00001093 env->active_fpu.fpr[n - 38].d = tmp;
ths7ac256b2007-10-25 21:30:37 +00001094 else
pbrook56aebc82008-10-11 17:55:29 +00001095 env->active_fpu.fpr[n - 38].w[FP_ENDIAN_IDX] = tmp;
1096 }
1097 switch (n) {
1098 case 70:
1099 env->active_fpu.fcr31 = tmp & 0xFF83FFFF;
1100 /* set rounding mode */
1101 RESTORE_ROUNDING_MODE;
ths8e33c082006-12-11 19:22:27 +00001102#ifndef CONFIG_SOFTFLOAT
pbrook56aebc82008-10-11 17:55:29 +00001103 /* no floating point exception for native float */
1104 SET_FP_ENABLE(env->active_fpu.fcr31, 0);
ths8e33c082006-12-11 19:22:27 +00001105#endif
pbrook56aebc82008-10-11 17:55:29 +00001106 break;
1107 case 71: env->active_fpu.fcr0 = tmp; break;
1108 }
1109 return sizeof(target_ulong);
1110 }
1111 switch (n) {
1112 case 32: env->CP0_Status = tmp; break;
1113 case 33: env->active_tc.LO[0] = tmp; break;
1114 case 34: env->active_tc.HI[0] = tmp; break;
1115 case 35: env->CP0_BadVAddr = tmp; break;
1116 case 36: env->CP0_Cause = tmp; break;
1117 case 37: env->active_tc.PC = tmp; break;
1118 case 72: /* fp, ignored */ break;
1119 default:
1120 if (n > 89)
1121 return 0;
1122 /* Other registers are readonly. Ignore writes. */
1123 break;
1124 }
1125
1126 return sizeof(target_ulong);
bellard6f970bd2005-12-05 19:55:19 +00001127}
bellardfdf9b3e2006-04-27 21:07:38 +00001128#elif defined (TARGET_SH4)
ths6ef99fc2007-05-13 16:36:24 +00001129
1130/* Hint: Use "set architecture sh4" in GDB to see fpu registers */
pbrook56aebc82008-10-11 17:55:29 +00001131/* FIXME: We should use XML for this. */
ths6ef99fc2007-05-13 16:36:24 +00001132
pbrook56aebc82008-10-11 17:55:29 +00001133#define NUM_CORE_REGS 59
1134
1135static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
bellardfdf9b3e2006-04-27 21:07:38 +00001136{
pbrook56aebc82008-10-11 17:55:29 +00001137 if (n < 8) {
1138 if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
1139 GET_REGL(env->gregs[n + 16]);
1140 } else {
1141 GET_REGL(env->gregs[n]);
1142 }
1143 } else if (n < 16) {
1144 GET_REGL(env->gregs[n - 8]);
1145 } else if (n >= 25 && n < 41) {
1146 GET_REGL(env->fregs[(n - 25) + ((env->fpscr & FPSCR_FR) ? 16 : 0)]);
1147 } else if (n >= 43 && n < 51) {
1148 GET_REGL(env->gregs[n - 43]);
1149 } else if (n >= 51 && n < 59) {
1150 GET_REGL(env->gregs[n - (51 - 16)]);
1151 }
1152 switch (n) {
1153 case 16: GET_REGL(env->pc);
1154 case 17: GET_REGL(env->pr);
1155 case 18: GET_REGL(env->gbr);
1156 case 19: GET_REGL(env->vbr);
1157 case 20: GET_REGL(env->mach);
1158 case 21: GET_REGL(env->macl);
1159 case 22: GET_REGL(env->sr);
1160 case 23: GET_REGL(env->fpul);
1161 case 24: GET_REGL(env->fpscr);
1162 case 41: GET_REGL(env->ssr);
1163 case 42: GET_REGL(env->spc);
1164 }
bellardfdf9b3e2006-04-27 21:07:38 +00001165
pbrook56aebc82008-10-11 17:55:29 +00001166 return 0;
bellardfdf9b3e2006-04-27 21:07:38 +00001167}
1168
pbrook56aebc82008-10-11 17:55:29 +00001169static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellardfdf9b3e2006-04-27 21:07:38 +00001170{
pbrook56aebc82008-10-11 17:55:29 +00001171 uint32_t tmp;
bellardfdf9b3e2006-04-27 21:07:38 +00001172
pbrook56aebc82008-10-11 17:55:29 +00001173 tmp = ldl_p(mem_buf);
1174
1175 if (n < 8) {
1176 if ((env->sr & (SR_MD | SR_RB)) == (SR_MD | SR_RB)) {
1177 env->gregs[n + 16] = tmp;
1178 } else {
1179 env->gregs[n] = tmp;
1180 }
1181 return 4;
1182 } else if (n < 16) {
1183 env->gregs[n - 8] = tmp;
1184 return 4;
1185 } else if (n >= 25 && n < 41) {
1186 env->fregs[(n - 25) + ((env->fpscr & FPSCR_FR) ? 16 : 0)] = tmp;
1187 } else if (n >= 43 && n < 51) {
1188 env->gregs[n - 43] = tmp;
1189 return 4;
1190 } else if (n >= 51 && n < 59) {
1191 env->gregs[n - (51 - 16)] = tmp;
1192 return 4;
1193 }
1194 switch (n) {
1195 case 16: env->pc = tmp;
1196 case 17: env->pr = tmp;
1197 case 18: env->gbr = tmp;
1198 case 19: env->vbr = tmp;
1199 case 20: env->mach = tmp;
1200 case 21: env->macl = tmp;
1201 case 22: env->sr = tmp;
1202 case 23: env->fpul = tmp;
1203 case 24: env->fpscr = tmp;
1204 case 41: env->ssr = tmp;
1205 case 42: env->spc = tmp;
1206 default: return 0;
1207 }
1208
1209 return 4;
bellardfdf9b3e2006-04-27 21:07:38 +00001210}
Edgar E. Iglesiasd74d6a92009-05-20 20:16:31 +02001211#elif defined (TARGET_MICROBLAZE)
1212
1213#define NUM_CORE_REGS (32 + 5)
1214
1215static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
1216{
1217 if (n < 32) {
1218 GET_REG32(env->regs[n]);
1219 } else {
1220 GET_REG32(env->sregs[n - 32]);
1221 }
1222 return 0;
1223}
1224
1225static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
1226{
1227 uint32_t tmp;
1228
1229 if (n > NUM_CORE_REGS)
1230 return 0;
1231
1232 tmp = ldl_p(mem_buf);
1233
1234 if (n < 32) {
1235 env->regs[n] = tmp;
1236 } else {
1237 env->sregs[n - 32] = tmp;
1238 }
1239 return 4;
1240}
thsf1ccf902007-10-08 13:16:14 +00001241#elif defined (TARGET_CRIS)
1242
pbrook56aebc82008-10-11 17:55:29 +00001243#define NUM_CORE_REGS 49
1244
1245static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
thsf1ccf902007-10-08 13:16:14 +00001246{
pbrook56aebc82008-10-11 17:55:29 +00001247 uint8_t srs;
1248
1249 srs = env->pregs[PR_SRS];
1250 if (n < 16) {
1251 GET_REG32(env->regs[n]);
1252 }
1253
1254 if (n >= 21 && n < 32) {
1255 GET_REG32(env->pregs[n - 16]);
1256 }
1257 if (n >= 33 && n < 49) {
1258 GET_REG32(env->sregs[srs][n - 33]);
1259 }
1260 switch (n) {
1261 case 16: GET_REG8(env->pregs[0]);
1262 case 17: GET_REG8(env->pregs[1]);
1263 case 18: GET_REG32(env->pregs[2]);
1264 case 19: GET_REG8(srs);
1265 case 20: GET_REG16(env->pregs[4]);
1266 case 32: GET_REG32(env->pc);
1267 }
1268
1269 return 0;
thsf1ccf902007-10-08 13:16:14 +00001270}
1271
pbrook56aebc82008-10-11 17:55:29 +00001272static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
thsf1ccf902007-10-08 13:16:14 +00001273{
pbrook56aebc82008-10-11 17:55:29 +00001274 uint32_t tmp;
thsf1ccf902007-10-08 13:16:14 +00001275
pbrook56aebc82008-10-11 17:55:29 +00001276 if (n > 49)
1277 return 0;
thsf1ccf902007-10-08 13:16:14 +00001278
pbrook56aebc82008-10-11 17:55:29 +00001279 tmp = ldl_p(mem_buf);
thsf1ccf902007-10-08 13:16:14 +00001280
pbrook56aebc82008-10-11 17:55:29 +00001281 if (n < 16) {
1282 env->regs[n] = tmp;
1283 }
thsf1ccf902007-10-08 13:16:14 +00001284
edgar_igld7b69672008-10-11 19:32:21 +00001285 if (n >= 21 && n < 32) {
1286 env->pregs[n - 16] = tmp;
1287 }
1288
1289 /* FIXME: Should support function regs be writable? */
pbrook56aebc82008-10-11 17:55:29 +00001290 switch (n) {
1291 case 16: return 1;
1292 case 17: return 1;
edgar_igld7b69672008-10-11 19:32:21 +00001293 case 18: env->pregs[PR_PID] = tmp; break;
pbrook56aebc82008-10-11 17:55:29 +00001294 case 19: return 1;
1295 case 20: return 2;
1296 case 32: env->pc = tmp; break;
1297 }
thsf1ccf902007-10-08 13:16:14 +00001298
pbrook56aebc82008-10-11 17:55:29 +00001299 return 4;
thsf1ccf902007-10-08 13:16:14 +00001300}
aurel3219bf5172008-12-07 23:26:32 +00001301#elif defined (TARGET_ALPHA)
1302
1303#define NUM_CORE_REGS 65
1304
1305static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
1306{
1307 if (n < 31) {
1308 GET_REGL(env->ir[n]);
1309 }
1310 else if (n == 31) {
1311 GET_REGL(0);
1312 }
1313 else if (n<63) {
1314 uint64_t val;
1315
Michael S. Tsirkin8f4bee22009-09-30 19:43:47 +02001316 val = *((uint64_t *)&env->fir[n-32]);
aurel3219bf5172008-12-07 23:26:32 +00001317 GET_REGL(val);
1318 }
1319 else if (n==63) {
1320 GET_REGL(env->fpcr);
1321 }
1322 else if (n==64) {
1323 GET_REGL(env->pc);
1324 }
1325 else {
1326 GET_REGL(0);
1327 }
1328
1329 return 0;
1330}
1331
1332static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
1333{
1334 target_ulong tmp;
1335 tmp = ldtul_p(mem_buf);
1336
1337 if (n < 31) {
1338 env->ir[n] = tmp;
1339 }
1340
1341 if (n > 31 && n < 63) {
1342 env->fir[n - 32] = ldfl_p(mem_buf);
1343 }
1344
1345 if (n == 64 ) {
1346 env->pc=tmp;
1347 }
1348
1349 return 8;
1350}
bellard1fddef42005-04-17 19:16:13 +00001351#else
pbrook56aebc82008-10-11 17:55:29 +00001352
1353#define NUM_CORE_REGS 0
1354
1355static int cpu_gdb_read_register(CPUState *env, uint8_t *mem_buf, int n)
bellard6da41ea2004-01-04 15:48:38 +00001356{
1357 return 0;
1358}
1359
pbrook56aebc82008-10-11 17:55:29 +00001360static int cpu_gdb_write_register(CPUState *env, uint8_t *mem_buf, int n)
bellard6da41ea2004-01-04 15:48:38 +00001361{
pbrook56aebc82008-10-11 17:55:29 +00001362 return 0;
bellard6da41ea2004-01-04 15:48:38 +00001363}
1364
1365#endif
bellardb4608c02003-06-27 17:34:32 +00001366
pbrook56aebc82008-10-11 17:55:29 +00001367static int num_g_regs = NUM_CORE_REGS;
1368
1369#ifdef GDB_CORE_XML
1370/* Encode data using the encoding for 'x' packets. */
1371static int memtox(char *buf, const char *mem, int len)
1372{
1373 char *p = buf;
1374 char c;
1375
1376 while (len--) {
1377 c = *(mem++);
1378 switch (c) {
1379 case '#': case '$': case '*': case '}':
1380 *(p++) = '}';
1381 *(p++) = c ^ 0x20;
1382 break;
1383 default:
1384 *(p++) = c;
1385 break;
1386 }
1387 }
1388 return p - buf;
1389}
1390
aurel323faf7782008-12-07 23:26:17 +00001391static const char *get_feature_xml(const char *p, const char **newp)
pbrook56aebc82008-10-11 17:55:29 +00001392{
1393 extern const char *const xml_builtin[][2];
1394 size_t len;
1395 int i;
1396 const char *name;
1397 static char target_xml[1024];
1398
1399 len = 0;
1400 while (p[len] && p[len] != ':')
1401 len++;
1402 *newp = p + len;
1403
1404 name = NULL;
1405 if (strncmp(p, "target.xml", len) == 0) {
1406 /* Generate the XML description for this CPU. */
1407 if (!target_xml[0]) {
1408 GDBRegisterState *r;
1409
blueswir15b3715b2008-10-25 11:18:12 +00001410 snprintf(target_xml, sizeof(target_xml),
1411 "<?xml version=\"1.0\"?>"
1412 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
1413 "<target>"
1414 "<xi:include href=\"%s\"/>",
1415 GDB_CORE_XML);
pbrook56aebc82008-10-11 17:55:29 +00001416
aliguori880a7572008-11-18 20:30:24 +00001417 for (r = first_cpu->gdb_regs; r; r = r->next) {
blueswir12dc766d2009-04-13 16:06:19 +00001418 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
1419 pstrcat(target_xml, sizeof(target_xml), r->xml);
1420 pstrcat(target_xml, sizeof(target_xml), "\"/>");
pbrook56aebc82008-10-11 17:55:29 +00001421 }
blueswir12dc766d2009-04-13 16:06:19 +00001422 pstrcat(target_xml, sizeof(target_xml), "</target>");
pbrook56aebc82008-10-11 17:55:29 +00001423 }
1424 return target_xml;
1425 }
1426 for (i = 0; ; i++) {
1427 name = xml_builtin[i][0];
1428 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
1429 break;
1430 }
1431 return name ? xml_builtin[i][1] : NULL;
1432}
1433#endif
1434
1435static int gdb_read_register(CPUState *env, uint8_t *mem_buf, int reg)
1436{
1437 GDBRegisterState *r;
1438
1439 if (reg < NUM_CORE_REGS)
1440 return cpu_gdb_read_register(env, mem_buf, reg);
1441
1442 for (r = env->gdb_regs; r; r = r->next) {
1443 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
1444 return r->get_reg(env, mem_buf, reg - r->base_reg);
1445 }
1446 }
1447 return 0;
1448}
1449
1450static int gdb_write_register(CPUState *env, uint8_t *mem_buf, int reg)
1451{
1452 GDBRegisterState *r;
1453
1454 if (reg < NUM_CORE_REGS)
1455 return cpu_gdb_write_register(env, mem_buf, reg);
1456
1457 for (r = env->gdb_regs; r; r = r->next) {
1458 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
1459 return r->set_reg(env, mem_buf, reg - r->base_reg);
1460 }
1461 }
1462 return 0;
1463}
1464
1465/* Register a supplemental set of CPU registers. If g_pos is nonzero it
1466 specifies the first register number and these registers are included in
1467 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
1468 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
1469 */
1470
1471void gdb_register_coprocessor(CPUState * env,
1472 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
1473 int num_regs, const char *xml, int g_pos)
1474{
1475 GDBRegisterState *s;
1476 GDBRegisterState **p;
1477 static int last_reg = NUM_CORE_REGS;
1478
1479 s = (GDBRegisterState *)qemu_mallocz(sizeof(GDBRegisterState));
1480 s->base_reg = last_reg;
1481 s->num_regs = num_regs;
1482 s->get_reg = get_reg;
1483 s->set_reg = set_reg;
1484 s->xml = xml;
1485 p = &env->gdb_regs;
1486 while (*p) {
1487 /* Check for duplicates. */
1488 if (strcmp((*p)->xml, xml) == 0)
1489 return;
1490 p = &(*p)->next;
1491 }
1492 /* Add to end of list. */
1493 last_reg += num_regs;
1494 *p = s;
1495 if (g_pos) {
1496 if (g_pos != s->base_reg) {
1497 fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
1498 "Expected %d got %d\n", xml, g_pos, s->base_reg);
1499 } else {
1500 num_g_regs = last_reg;
1501 }
1502 }
1503}
1504
aliguoria1d1bb32008-11-18 20:07:32 +00001505#ifndef CONFIG_USER_ONLY
1506static const int xlat_gdb_type[] = {
1507 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
1508 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
1509 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
1510};
1511#endif
1512
aliguori880a7572008-11-18 20:30:24 +00001513static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +00001514{
aliguori880a7572008-11-18 20:30:24 +00001515 CPUState *env;
1516 int err = 0;
1517
aliguorie22a25c2009-03-12 20:12:48 +00001518 if (kvm_enabled())
1519 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
1520
aliguoria1d1bb32008-11-18 20:07:32 +00001521 switch (type) {
1522 case GDB_BREAKPOINT_SW:
1523 case GDB_BREAKPOINT_HW:
aliguori880a7572008-11-18 20:30:24 +00001524 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1525 err = cpu_breakpoint_insert(env, addr, BP_GDB, NULL);
1526 if (err)
1527 break;
1528 }
1529 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001530#ifndef CONFIG_USER_ONLY
1531 case GDB_WATCHPOINT_WRITE:
1532 case GDB_WATCHPOINT_READ:
1533 case GDB_WATCHPOINT_ACCESS:
aliguori880a7572008-11-18 20:30:24 +00001534 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1535 err = cpu_watchpoint_insert(env, addr, len, xlat_gdb_type[type],
1536 NULL);
1537 if (err)
1538 break;
1539 }
1540 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001541#endif
1542 default:
1543 return -ENOSYS;
1544 }
1545}
1546
aliguori880a7572008-11-18 20:30:24 +00001547static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +00001548{
aliguori880a7572008-11-18 20:30:24 +00001549 CPUState *env;
1550 int err = 0;
1551
aliguorie22a25c2009-03-12 20:12:48 +00001552 if (kvm_enabled())
1553 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
1554
aliguoria1d1bb32008-11-18 20:07:32 +00001555 switch (type) {
1556 case GDB_BREAKPOINT_SW:
1557 case GDB_BREAKPOINT_HW:
aliguori880a7572008-11-18 20:30:24 +00001558 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1559 err = cpu_breakpoint_remove(env, addr, BP_GDB);
1560 if (err)
1561 break;
1562 }
1563 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001564#ifndef CONFIG_USER_ONLY
1565 case GDB_WATCHPOINT_WRITE:
1566 case GDB_WATCHPOINT_READ:
1567 case GDB_WATCHPOINT_ACCESS:
aliguori880a7572008-11-18 20:30:24 +00001568 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1569 err = cpu_watchpoint_remove(env, addr, len, xlat_gdb_type[type]);
1570 if (err)
1571 break;
1572 }
1573 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001574#endif
1575 default:
1576 return -ENOSYS;
1577 }
1578}
1579
aliguori880a7572008-11-18 20:30:24 +00001580static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +00001581{
aliguori880a7572008-11-18 20:30:24 +00001582 CPUState *env;
1583
aliguorie22a25c2009-03-12 20:12:48 +00001584 if (kvm_enabled()) {
1585 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
1586 return;
1587 }
1588
aliguori880a7572008-11-18 20:30:24 +00001589 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1590 cpu_breakpoint_remove_all(env, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +00001591#ifndef CONFIG_USER_ONLY
aliguori880a7572008-11-18 20:30:24 +00001592 cpu_watchpoint_remove_all(env, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +00001593#endif
aliguori880a7572008-11-18 20:30:24 +00001594 }
aliguoria1d1bb32008-11-18 20:07:32 +00001595}
1596
aurel32fab9d282009-04-08 21:29:37 +00001597static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
1598{
1599#if defined(TARGET_I386)
Avi Kivity4c0960c2009-08-17 23:19:53 +03001600 cpu_synchronize_state(s->c_cpu);
aurel32fab9d282009-04-08 21:29:37 +00001601 s->c_cpu->eip = pc;
aurel32fab9d282009-04-08 21:29:37 +00001602#elif defined (TARGET_PPC)
1603 s->c_cpu->nip = pc;
1604#elif defined (TARGET_SPARC)
1605 s->c_cpu->pc = pc;
1606 s->c_cpu->npc = pc + 4;
1607#elif defined (TARGET_ARM)
1608 s->c_cpu->regs[15] = pc;
1609#elif defined (TARGET_SH4)
1610 s->c_cpu->pc = pc;
1611#elif defined (TARGET_MIPS)
1612 s->c_cpu->active_tc.PC = pc;
Edgar E. Iglesiasd74d6a92009-05-20 20:16:31 +02001613#elif defined (TARGET_MICROBLAZE)
1614 s->c_cpu->sregs[SR_PC] = pc;
aurel32fab9d282009-04-08 21:29:37 +00001615#elif defined (TARGET_CRIS)
1616 s->c_cpu->pc = pc;
1617#elif defined (TARGET_ALPHA)
1618 s->c_cpu->pc = pc;
1619#endif
1620}
1621
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001622static inline int gdb_id(CPUState *env)
1623{
Juan Quintela2f7bb872009-07-27 16:13:24 +02001624#if defined(CONFIG_USER_ONLY) && defined(CONFIG_USE_NPTL)
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001625 return env->host_tid;
1626#else
1627 return env->cpu_index + 1;
1628#endif
1629}
1630
1631static CPUState *find_cpu(uint32_t thread_id)
1632{
1633 CPUState *env;
1634
1635 for (env = first_cpu; env != NULL; env = env->next_cpu) {
1636 if (gdb_id(env) == thread_id) {
1637 return env;
1638 }
1639 }
1640
1641 return NULL;
1642}
1643
aliguori880a7572008-11-18 20:30:24 +00001644static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +00001645{
aliguori880a7572008-11-18 20:30:24 +00001646 CPUState *env;
bellardb4608c02003-06-27 17:34:32 +00001647 const char *p;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001648 uint32_t thread;
1649 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +00001650 char buf[MAX_PACKET_LENGTH];
1651 uint8_t mem_buf[MAX_PACKET_LENGTH];
1652 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +00001653 target_ulong addr, len;
ths3b46e622007-09-17 08:09:54 +00001654
bellard858693c2004-03-31 18:52:07 +00001655#ifdef DEBUG_GDB
1656 printf("command='%s'\n", line_buf);
bellard4c3a88a2003-07-26 12:06:08 +00001657#endif
bellard858693c2004-03-31 18:52:07 +00001658 p = line_buf;
1659 ch = *p++;
1660 switch(ch) {
1661 case '?':
bellard1fddef42005-04-17 19:16:13 +00001662 /* TODO: Make this return the correct value for user-mode. */
aurel32ca587a82008-12-18 22:44:13 +00001663 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001664 gdb_id(s->c_cpu));
bellard858693c2004-03-31 18:52:07 +00001665 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +00001666 /* Remove all the breakpoints when this query is issued,
1667 * because gdb is doing and initial connect and the state
1668 * should be cleaned up.
1669 */
aliguori880a7572008-11-18 20:30:24 +00001670 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +00001671 break;
1672 case 'c':
1673 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +00001674 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001675 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001676 }
aurel32ca587a82008-12-18 22:44:13 +00001677 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +00001678 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +00001679 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +00001680 case 'C':
aurel32ca587a82008-12-18 22:44:13 +00001681 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
1682 if (s->signal == -1)
1683 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +00001684 gdb_continue(s);
1685 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001686 case 'v':
1687 if (strncmp(p, "Cont", 4) == 0) {
1688 int res_signal, res_thread;
1689
1690 p += 4;
1691 if (*p == '?') {
1692 put_packet(s, "vCont;c;C;s;S");
1693 break;
1694 }
1695 res = 0;
1696 res_signal = 0;
1697 res_thread = 0;
1698 while (*p) {
1699 int action, signal;
1700
1701 if (*p++ != ';') {
1702 res = 0;
1703 break;
1704 }
1705 action = *p++;
1706 signal = 0;
1707 if (action == 'C' || action == 'S') {
1708 signal = strtoul(p, (char **)&p, 16);
1709 } else if (action != 'c' && action != 's') {
1710 res = 0;
1711 break;
1712 }
1713 thread = 0;
1714 if (*p == ':') {
1715 thread = strtoull(p+1, (char **)&p, 16);
1716 }
1717 action = tolower(action);
1718 if (res == 0 || (res == 'c' && action == 's')) {
1719 res = action;
1720 res_signal = signal;
1721 res_thread = thread;
1722 }
1723 }
1724 if (res) {
1725 if (res_thread != -1 && res_thread != 0) {
1726 env = find_cpu(res_thread);
1727 if (env == NULL) {
1728 put_packet(s, "E22");
1729 break;
1730 }
1731 s->c_cpu = env;
1732 }
1733 if (res == 's') {
1734 cpu_single_step(s->c_cpu, sstep_flags);
1735 }
1736 s->signal = res_signal;
1737 gdb_continue(s);
1738 return RS_IDLE;
1739 }
1740 break;
1741 } else {
1742 goto unknown_command;
1743 }
edgar_igl7d03f822008-05-17 18:58:29 +00001744 case 'k':
1745 /* Kill the target */
1746 fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
1747 exit(0);
1748 case 'D':
1749 /* Detach packet */
aliguori880a7572008-11-18 20:30:24 +00001750 gdb_breakpoint_remove_all();
edgar_igl7d03f822008-05-17 18:58:29 +00001751 gdb_continue(s);
1752 put_packet(s, "OK");
1753 break;
bellard858693c2004-03-31 18:52:07 +00001754 case 's':
1755 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +00001756 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001757 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001758 }
aliguori880a7572008-11-18 20:30:24 +00001759 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +00001760 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +00001761 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +00001762 case 'F':
1763 {
1764 target_ulong ret;
1765 target_ulong err;
1766
1767 ret = strtoull(p, (char **)&p, 16);
1768 if (*p == ',') {
1769 p++;
1770 err = strtoull(p, (char **)&p, 16);
1771 } else {
1772 err = 0;
1773 }
1774 if (*p == ',')
1775 p++;
1776 type = *p;
1777 if (gdb_current_syscall_cb)
aliguori880a7572008-11-18 20:30:24 +00001778 gdb_current_syscall_cb(s->c_cpu, ret, err);
pbrooka2d1eba2007-01-28 03:10:55 +00001779 if (type == 'C') {
1780 put_packet(s, "T02");
1781 } else {
edgar_iglba70a622008-03-14 06:10:42 +00001782 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +00001783 }
1784 }
1785 break;
bellard858693c2004-03-31 18:52:07 +00001786 case 'g':
Avi Kivity4c0960c2009-08-17 23:19:53 +03001787 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001788 len = 0;
1789 for (addr = 0; addr < num_g_regs; addr++) {
aliguori880a7572008-11-18 20:30:24 +00001790 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +00001791 len += reg_size;
1792 }
1793 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +00001794 put_packet(s, buf);
1795 break;
1796 case 'G':
Avi Kivity4c0960c2009-08-17 23:19:53 +03001797 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001798 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +00001799 len = strlen(p) / 2;
1800 hextomem((uint8_t *)registers, p, len);
pbrook56aebc82008-10-11 17:55:29 +00001801 for (addr = 0; addr < num_g_regs && len > 0; addr++) {
aliguori880a7572008-11-18 20:30:24 +00001802 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001803 len -= reg_size;
1804 registers += reg_size;
1805 }
bellard858693c2004-03-31 18:52:07 +00001806 put_packet(s, "OK");
1807 break;
1808 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001809 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001810 if (*p == ',')
1811 p++;
bellard9d9754a2006-06-25 15:32:37 +00001812 len = strtoull(p, NULL, 16);
aliguori880a7572008-11-18 20:30:24 +00001813 if (cpu_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 0) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001814 put_packet (s, "E14");
1815 } else {
1816 memtohex(buf, mem_buf, len);
1817 put_packet(s, buf);
1818 }
bellard858693c2004-03-31 18:52:07 +00001819 break;
1820 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001821 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001822 if (*p == ',')
1823 p++;
bellard9d9754a2006-06-25 15:32:37 +00001824 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001825 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001826 p++;
1827 hextomem(mem_buf, p, len);
aliguori880a7572008-11-18 20:30:24 +00001828 if (cpu_memory_rw_debug(s->g_cpu, addr, mem_buf, len, 1) != 0)
bellard905f20b2005-04-26 21:09:55 +00001829 put_packet(s, "E14");
bellard858693c2004-03-31 18:52:07 +00001830 else
1831 put_packet(s, "OK");
1832 break;
pbrook56aebc82008-10-11 17:55:29 +00001833 case 'p':
1834 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1835 This works, but can be very slow. Anything new enough to
1836 understand XML also knows how to use this properly. */
1837 if (!gdb_has_xml)
1838 goto unknown_command;
1839 addr = strtoull(p, (char **)&p, 16);
aliguori880a7572008-11-18 20:30:24 +00001840 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001841 if (reg_size) {
1842 memtohex(buf, mem_buf, reg_size);
1843 put_packet(s, buf);
1844 } else {
1845 put_packet(s, "E14");
1846 }
1847 break;
1848 case 'P':
1849 if (!gdb_has_xml)
1850 goto unknown_command;
1851 addr = strtoull(p, (char **)&p, 16);
1852 if (*p == '=')
1853 p++;
1854 reg_size = strlen(p) / 2;
1855 hextomem(mem_buf, p, reg_size);
aliguori880a7572008-11-18 20:30:24 +00001856 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001857 put_packet(s, "OK");
1858 break;
bellard858693c2004-03-31 18:52:07 +00001859 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001860 case 'z':
1861 type = strtoul(p, (char **)&p, 16);
1862 if (*p == ',')
1863 p++;
bellard9d9754a2006-06-25 15:32:37 +00001864 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001865 if (*p == ',')
1866 p++;
bellard9d9754a2006-06-25 15:32:37 +00001867 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001868 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001869 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001870 else
aliguori880a7572008-11-18 20:30:24 +00001871 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001872 if (res >= 0)
1873 put_packet(s, "OK");
1874 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001875 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001876 else
1877 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001878 break;
aliguori880a7572008-11-18 20:30:24 +00001879 case 'H':
1880 type = *p++;
1881 thread = strtoull(p, (char **)&p, 16);
1882 if (thread == -1 || thread == 0) {
1883 put_packet(s, "OK");
1884 break;
1885 }
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001886 env = find_cpu(thread);
aliguori880a7572008-11-18 20:30:24 +00001887 if (env == NULL) {
1888 put_packet(s, "E22");
1889 break;
1890 }
1891 switch (type) {
1892 case 'c':
1893 s->c_cpu = env;
1894 put_packet(s, "OK");
1895 break;
1896 case 'g':
1897 s->g_cpu = env;
1898 put_packet(s, "OK");
1899 break;
1900 default:
1901 put_packet(s, "E22");
1902 break;
1903 }
1904 break;
1905 case 'T':
1906 thread = strtoull(p, (char **)&p, 16);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001907 env = find_cpu(thread);
1908
1909 if (env != NULL) {
1910 put_packet(s, "OK");
1911 } else {
aliguori880a7572008-11-18 20:30:24 +00001912 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001913 }
aliguori880a7572008-11-18 20:30:24 +00001914 break;
pbrook978efd62006-06-17 18:30:42 +00001915 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001916 case 'Q':
1917 /* parse any 'q' packets here */
1918 if (!strcmp(p,"qemu.sstepbits")) {
1919 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001920 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1921 SSTEP_ENABLE,
1922 SSTEP_NOIRQ,
1923 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001924 put_packet(s, buf);
1925 break;
1926 } else if (strncmp(p,"qemu.sstep",10) == 0) {
1927 /* Display or change the sstep_flags */
1928 p += 10;
1929 if (*p != '=') {
1930 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001931 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001932 put_packet(s, buf);
1933 break;
1934 }
1935 p++;
1936 type = strtoul(p, (char **)&p, 16);
1937 sstep_flags = type;
1938 put_packet(s, "OK");
1939 break;
aliguori880a7572008-11-18 20:30:24 +00001940 } else if (strcmp(p,"C") == 0) {
1941 /* "Current thread" remains vague in the spec, so always return
1942 * the first CPU (gdb returns the first thread). */
1943 put_packet(s, "QC1");
1944 break;
1945 } else if (strcmp(p,"fThreadInfo") == 0) {
1946 s->query_cpu = first_cpu;
1947 goto report_cpuinfo;
1948 } else if (strcmp(p,"sThreadInfo") == 0) {
1949 report_cpuinfo:
1950 if (s->query_cpu) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001951 snprintf(buf, sizeof(buf), "m%x", gdb_id(s->query_cpu));
aliguori880a7572008-11-18 20:30:24 +00001952 put_packet(s, buf);
1953 s->query_cpu = s->query_cpu->next_cpu;
1954 } else
1955 put_packet(s, "l");
1956 break;
1957 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1958 thread = strtoull(p+16, (char **)&p, 16);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001959 env = find_cpu(thread);
1960 if (env != NULL) {
Avi Kivity4c0960c2009-08-17 23:19:53 +03001961 cpu_synchronize_state(env);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001962 len = snprintf((char *)mem_buf, sizeof(mem_buf),
1963 "CPU#%d [%s]", env->cpu_index,
1964 env->halted ? "halted " : "running");
1965 memtohex(buf, mem_buf, len);
1966 put_packet(s, buf);
1967 }
aliguori880a7572008-11-18 20:30:24 +00001968 break;
edgar_igl60897d32008-05-09 08:25:14 +00001969 }
blueswir10b8a9882009-03-07 10:51:36 +00001970#ifdef CONFIG_USER_ONLY
edgar_igl60897d32008-05-09 08:25:14 +00001971 else if (strncmp(p, "Offsets", 7) == 0) {
aliguori880a7572008-11-18 20:30:24 +00001972 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001973
blueswir1363a37d2008-08-21 17:58:08 +00001974 snprintf(buf, sizeof(buf),
1975 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1976 ";Bss=" TARGET_ABI_FMT_lx,
1977 ts->info->code_offset,
1978 ts->info->data_offset,
1979 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001980 put_packet(s, buf);
1981 break;
1982 }
blueswir10b8a9882009-03-07 10:51:36 +00001983#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001984 else if (strncmp(p, "Rcmd,", 5) == 0) {
1985 int len = strlen(p + 5);
1986
1987 if ((len % 2) != 0) {
1988 put_packet(s, "E01");
1989 break;
1990 }
1991 hextomem(mem_buf, p + 5, len);
1992 len = len / 2;
1993 mem_buf[len++] = 0;
1994 qemu_chr_read(s->mon_chr, mem_buf, len);
1995 put_packet(s, "OK");
1996 break;
1997 }
blueswir10b8a9882009-03-07 10:51:36 +00001998#endif /* !CONFIG_USER_ONLY */
pbrook56aebc82008-10-11 17:55:29 +00001999 if (strncmp(p, "Supported", 9) == 0) {
blueswir15b3715b2008-10-25 11:18:12 +00002000 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
pbrook56aebc82008-10-11 17:55:29 +00002001#ifdef GDB_CORE_XML
blueswir12dc766d2009-04-13 16:06:19 +00002002 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
pbrook56aebc82008-10-11 17:55:29 +00002003#endif
2004 put_packet(s, buf);
2005 break;
2006 }
2007#ifdef GDB_CORE_XML
2008 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
2009 const char *xml;
2010 target_ulong total_len;
2011
2012 gdb_has_xml = 1;
2013 p += 19;
aliguori880a7572008-11-18 20:30:24 +00002014 xml = get_feature_xml(p, &p);
pbrook56aebc82008-10-11 17:55:29 +00002015 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00002016 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00002017 put_packet(s, buf);
2018 break;
2019 }
2020
2021 if (*p == ':')
2022 p++;
2023 addr = strtoul(p, (char **)&p, 16);
2024 if (*p == ',')
2025 p++;
2026 len = strtoul(p, (char **)&p, 16);
2027
2028 total_len = strlen(xml);
2029 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00002030 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00002031 put_packet(s, buf);
2032 break;
2033 }
2034 if (len > (MAX_PACKET_LENGTH - 5) / 2)
2035 len = (MAX_PACKET_LENGTH - 5) / 2;
2036 if (len < total_len - addr) {
2037 buf[0] = 'm';
2038 len = memtox(buf + 1, xml + addr, len);
2039 } else {
2040 buf[0] = 'l';
2041 len = memtox(buf + 1, xml + addr, total_len - addr);
2042 }
2043 put_packet_binary(s, buf, len + 1);
2044 break;
2045 }
2046#endif
2047 /* Unrecognised 'q' command. */
2048 goto unknown_command;
2049
bellard858693c2004-03-31 18:52:07 +00002050 default:
pbrook56aebc82008-10-11 17:55:29 +00002051 unknown_command:
bellard858693c2004-03-31 18:52:07 +00002052 /* put empty packet */
2053 buf[0] = '\0';
2054 put_packet(s, buf);
2055 break;
2056 }
2057 return RS_IDLE;
2058}
2059
aliguori880a7572008-11-18 20:30:24 +00002060void gdb_set_stop_cpu(CPUState *env)
2061{
2062 gdbserver_state->c_cpu = env;
2063 gdbserver_state->g_cpu = env;
2064}
2065
bellard1fddef42005-04-17 19:16:13 +00002066#ifndef CONFIG_USER_ONLY
aliguori9781e042009-01-22 17:15:29 +00002067static void gdb_vm_state_change(void *opaque, int running, int reason)
bellard858693c2004-03-31 18:52:07 +00002068{
aliguori880a7572008-11-18 20:30:24 +00002069 GDBState *s = gdbserver_state;
2070 CPUState *env = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00002071 char buf[256];
aliguorid6fc1b32008-11-18 19:55:44 +00002072 const char *type;
bellard858693c2004-03-31 18:52:07 +00002073 int ret;
2074
aliguori9781e042009-01-22 17:15:29 +00002075 if (running || (reason != EXCP_DEBUG && reason != EXCP_INTERRUPT) ||
aliguori36556b22009-03-28 18:05:53 +00002076 s->state == RS_INACTIVE || s->state == RS_SYSCALL)
pbrooka2d1eba2007-01-28 03:10:55 +00002077 return;
2078
bellard858693c2004-03-31 18:52:07 +00002079 /* disable single step if it was enable */
aliguori880a7572008-11-18 20:30:24 +00002080 cpu_single_step(env, 0);
bellard858693c2004-03-31 18:52:07 +00002081
bellarde80cfcf2004-12-19 23:18:01 +00002082 if (reason == EXCP_DEBUG) {
aliguori880a7572008-11-18 20:30:24 +00002083 if (env->watchpoint_hit) {
2084 switch (env->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00002085 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00002086 type = "r";
2087 break;
aliguoria1d1bb32008-11-18 20:07:32 +00002088 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00002089 type = "a";
2090 break;
2091 default:
2092 type = "";
2093 break;
2094 }
aliguori880a7572008-11-18 20:30:24 +00002095 snprintf(buf, sizeof(buf),
2096 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
Nathan Froyd1e9fa732009-06-03 11:33:08 -07002097 GDB_SIGNAL_TRAP, gdb_id(env), type,
aliguori880a7572008-11-18 20:30:24 +00002098 env->watchpoint_hit->vaddr);
pbrook6658ffb2007-03-16 23:58:11 +00002099 put_packet(s, buf);
aliguori880a7572008-11-18 20:30:24 +00002100 env->watchpoint_hit = NULL;
pbrook6658ffb2007-03-16 23:58:11 +00002101 return;
2102 }
aliguori880a7572008-11-18 20:30:24 +00002103 tb_flush(env);
aurel32ca587a82008-12-18 22:44:13 +00002104 ret = GDB_SIGNAL_TRAP;
bellardbbeb7b52006-04-23 18:42:15 +00002105 } else {
aliguori9781e042009-01-22 17:15:29 +00002106 ret = GDB_SIGNAL_INT;
bellardbbeb7b52006-04-23 18:42:15 +00002107 }
Nathan Froyd1e9fa732009-06-03 11:33:08 -07002108 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, gdb_id(env));
bellard858693c2004-03-31 18:52:07 +00002109 put_packet(s, buf);
2110}
bellard1fddef42005-04-17 19:16:13 +00002111#endif
bellard858693c2004-03-31 18:52:07 +00002112
pbrooka2d1eba2007-01-28 03:10:55 +00002113/* Send a gdb syscall request.
2114 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00002115 %x - target_ulong argument printed in hex.
2116 %lx - 64-bit argument printed in hex.
2117 %s - string pointer (target_ulong) and length (int) pair. */
blueswir17ccfb2e2008-09-14 06:45:34 +00002118void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
pbrooka2d1eba2007-01-28 03:10:55 +00002119{
2120 va_list va;
2121 char buf[256];
2122 char *p;
2123 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00002124 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00002125 GDBState *s;
2126
aliguori880a7572008-11-18 20:30:24 +00002127 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00002128 if (!s)
2129 return;
2130 gdb_current_syscall_cb = cb;
2131 s->state = RS_SYSCALL;
2132#ifndef CONFIG_USER_ONLY
2133 vm_stop(EXCP_DEBUG);
2134#endif
2135 s->state = RS_IDLE;
2136 va_start(va, fmt);
2137 p = buf;
2138 *(p++) = 'F';
2139 while (*fmt) {
2140 if (*fmt == '%') {
2141 fmt++;
2142 switch (*fmt++) {
2143 case 'x':
2144 addr = va_arg(va, target_ulong);
blueswir1363a37d2008-08-21 17:58:08 +00002145 p += snprintf(p, &buf[sizeof(buf)] - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00002146 break;
pbrooka87295e2007-05-26 15:09:38 +00002147 case 'l':
2148 if (*(fmt++) != 'x')
2149 goto bad_format;
2150 i64 = va_arg(va, uint64_t);
blueswir1363a37d2008-08-21 17:58:08 +00002151 p += snprintf(p, &buf[sizeof(buf)] - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00002152 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002153 case 's':
2154 addr = va_arg(va, target_ulong);
blueswir1363a37d2008-08-21 17:58:08 +00002155 p += snprintf(p, &buf[sizeof(buf)] - p, TARGET_FMT_lx "/%x",
2156 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00002157 break;
2158 default:
pbrooka87295e2007-05-26 15:09:38 +00002159 bad_format:
pbrooka2d1eba2007-01-28 03:10:55 +00002160 fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
2161 fmt - 1);
2162 break;
2163 }
2164 } else {
2165 *(p++) = *(fmt++);
2166 }
2167 }
pbrook8a93e022007-08-06 13:19:15 +00002168 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00002169 va_end(va);
2170 put_packet(s, buf);
2171#ifdef CONFIG_USER_ONLY
aliguori880a7572008-11-18 20:30:24 +00002172 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00002173#else
aurel323098dba2009-03-07 21:28:24 +00002174 cpu_exit(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00002175#endif
2176}
2177
bellard6a00d602005-11-21 23:25:50 +00002178static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00002179{
2180 int i, csum;
ths60fe76f2007-12-16 03:02:09 +00002181 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00002182
bellard1fddef42005-04-17 19:16:13 +00002183#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00002184 if (s->last_packet_len) {
2185 /* Waiting for a response to the last packet. If we see the start
2186 of a new command then abandon the previous response. */
2187 if (ch == '-') {
2188#ifdef DEBUG_GDB
2189 printf("Got NACK, retransmitting\n");
2190#endif
thsffe8ab82007-12-16 03:16:05 +00002191 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
pbrook4046d912007-01-28 01:53:16 +00002192 }
2193#ifdef DEBUG_GDB
2194 else if (ch == '+')
2195 printf("Got ACK\n");
2196 else
2197 printf("Got '%c' when expecting ACK/NACK\n", ch);
2198#endif
2199 if (ch == '+' || ch == '$')
2200 s->last_packet_len = 0;
2201 if (ch != '$')
2202 return;
2203 }
bellard858693c2004-03-31 18:52:07 +00002204 if (vm_running) {
2205 /* when the CPU is running, we cannot do anything except stop
2206 it when receiving a char */
2207 vm_stop(EXCP_INTERRUPT);
ths5fafdf22007-09-16 21:08:06 +00002208 } else
bellard1fddef42005-04-17 19:16:13 +00002209#endif
bellard41625032005-04-24 10:07:11 +00002210 {
bellard858693c2004-03-31 18:52:07 +00002211 switch(s->state) {
2212 case RS_IDLE:
2213 if (ch == '$') {
2214 s->line_buf_index = 0;
2215 s->state = RS_GETLINE;
bellard4c3a88a2003-07-26 12:06:08 +00002216 }
2217 break;
bellard858693c2004-03-31 18:52:07 +00002218 case RS_GETLINE:
2219 if (ch == '#') {
2220 s->state = RS_CHKSUM1;
2221 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2222 s->state = RS_IDLE;
2223 } else {
2224 s->line_buf[s->line_buf_index++] = ch;
2225 }
2226 break;
2227 case RS_CHKSUM1:
2228 s->line_buf[s->line_buf_index] = '\0';
2229 s->line_csum = fromhex(ch) << 4;
2230 s->state = RS_CHKSUM2;
2231 break;
2232 case RS_CHKSUM2:
2233 s->line_csum |= fromhex(ch);
2234 csum = 0;
2235 for(i = 0; i < s->line_buf_index; i++) {
2236 csum += s->line_buf[i];
2237 }
2238 if (s->line_csum != (csum & 0xff)) {
ths60fe76f2007-12-16 03:02:09 +00002239 reply = '-';
2240 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00002241 s->state = RS_IDLE;
2242 } else {
ths60fe76f2007-12-16 03:02:09 +00002243 reply = '+';
2244 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00002245 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00002246 }
bellardb4608c02003-06-27 17:34:32 +00002247 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002248 default:
2249 abort();
bellardb4608c02003-06-27 17:34:32 +00002250 }
2251 }
bellard858693c2004-03-31 18:52:07 +00002252}
2253
bellard1fddef42005-04-17 19:16:13 +00002254#ifdef CONFIG_USER_ONLY
2255int
aurel32ca587a82008-12-18 22:44:13 +00002256gdb_queuesig (void)
2257{
2258 GDBState *s;
2259
2260 s = gdbserver_state;
2261
2262 if (gdbserver_fd < 0 || s->fd < 0)
2263 return 0;
2264 else
2265 return 1;
2266}
2267
2268int
bellard1fddef42005-04-17 19:16:13 +00002269gdb_handlesig (CPUState *env, int sig)
2270{
2271 GDBState *s;
2272 char buf[256];
2273 int n;
2274
aliguori880a7572008-11-18 20:30:24 +00002275 s = gdbserver_state;
edgar_igl1f487ee2008-05-17 22:20:53 +00002276 if (gdbserver_fd < 0 || s->fd < 0)
2277 return sig;
bellard1fddef42005-04-17 19:16:13 +00002278
2279 /* disable single step if it was enabled */
2280 cpu_single_step(env, 0);
2281 tb_flush(env);
2282
2283 if (sig != 0)
2284 {
aurel32ca587a82008-12-18 22:44:13 +00002285 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb (sig));
bellard1fddef42005-04-17 19:16:13 +00002286 put_packet(s, buf);
2287 }
edgar_igl1f487ee2008-05-17 22:20:53 +00002288 /* put_packet() might have detected that the peer terminated the
2289 connection. */
2290 if (s->fd < 0)
2291 return sig;
bellard1fddef42005-04-17 19:16:13 +00002292
bellard1fddef42005-04-17 19:16:13 +00002293 sig = 0;
2294 s->state = RS_IDLE;
bellard41625032005-04-24 10:07:11 +00002295 s->running_state = 0;
2296 while (s->running_state == 0) {
bellard1fddef42005-04-17 19:16:13 +00002297 n = read (s->fd, buf, 256);
2298 if (n > 0)
2299 {
2300 int i;
2301
2302 for (i = 0; i < n; i++)
bellard6a00d602005-11-21 23:25:50 +00002303 gdb_read_byte (s, buf[i]);
bellard1fddef42005-04-17 19:16:13 +00002304 }
2305 else if (n == 0 || errno != EAGAIN)
2306 {
2307 /* XXX: Connection closed. Should probably wait for annother
2308 connection before continuing. */
2309 return sig;
2310 }
bellard41625032005-04-24 10:07:11 +00002311 }
edgar_igl1f487ee2008-05-17 22:20:53 +00002312 sig = s->signal;
2313 s->signal = 0;
bellard1fddef42005-04-17 19:16:13 +00002314 return sig;
2315}
bellarde9009672005-04-26 20:42:36 +00002316
2317/* Tell the remote gdb that the process has exited. */
2318void gdb_exit(CPUState *env, int code)
2319{
2320 GDBState *s;
2321 char buf[4];
2322
aliguori880a7572008-11-18 20:30:24 +00002323 s = gdbserver_state;
edgar_igl1f487ee2008-05-17 22:20:53 +00002324 if (gdbserver_fd < 0 || s->fd < 0)
2325 return;
bellarde9009672005-04-26 20:42:36 +00002326
2327 snprintf(buf, sizeof(buf), "W%02x", code);
2328 put_packet(s, buf);
2329}
2330
aurel32ca587a82008-12-18 22:44:13 +00002331/* Tell the remote gdb that the process has exited due to SIG. */
2332void gdb_signalled(CPUState *env, int sig)
2333{
2334 GDBState *s;
2335 char buf[4];
2336
2337 s = gdbserver_state;
2338 if (gdbserver_fd < 0 || s->fd < 0)
2339 return;
2340
2341 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb (sig));
2342 put_packet(s, buf);
2343}
bellard1fddef42005-04-17 19:16:13 +00002344
aliguori880a7572008-11-18 20:30:24 +00002345static void gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00002346{
2347 GDBState *s;
2348 struct sockaddr_in sockaddr;
2349 socklen_t len;
2350 int val, fd;
2351
2352 for(;;) {
2353 len = sizeof(sockaddr);
2354 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
2355 if (fd < 0 && errno != EINTR) {
2356 perror("accept");
2357 return;
2358 } else if (fd >= 0) {
2359 break;
2360 }
2361 }
2362
2363 /* set short latency */
2364 val = 1;
bellard8f447cc2006-06-14 15:21:14 +00002365 setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, sizeof(val));
ths3b46e622007-09-17 08:09:54 +00002366
aliguori880a7572008-11-18 20:30:24 +00002367 s = qemu_mallocz(sizeof(GDBState));
aliguori880a7572008-11-18 20:30:24 +00002368 s->c_cpu = first_cpu;
2369 s->g_cpu = first_cpu;
bellard858693c2004-03-31 18:52:07 +00002370 s->fd = fd;
pbrook56aebc82008-10-11 17:55:29 +00002371 gdb_has_xml = 0;
bellard858693c2004-03-31 18:52:07 +00002372
aliguori880a7572008-11-18 20:30:24 +00002373 gdbserver_state = s;
pbrooka2d1eba2007-01-28 03:10:55 +00002374
bellard858693c2004-03-31 18:52:07 +00002375 fcntl(fd, F_SETFL, O_NONBLOCK);
bellard858693c2004-03-31 18:52:07 +00002376}
2377
2378static int gdbserver_open(int port)
2379{
2380 struct sockaddr_in sockaddr;
2381 int fd, val, ret;
2382
2383 fd = socket(PF_INET, SOCK_STREAM, 0);
2384 if (fd < 0) {
2385 perror("socket");
2386 return -1;
2387 }
2388
2389 /* allow fast reuse */
2390 val = 1;
bellard8f447cc2006-06-14 15:21:14 +00002391 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
bellard858693c2004-03-31 18:52:07 +00002392
2393 sockaddr.sin_family = AF_INET;
2394 sockaddr.sin_port = htons(port);
2395 sockaddr.sin_addr.s_addr = 0;
2396 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
2397 if (ret < 0) {
2398 perror("bind");
2399 return -1;
2400 }
2401 ret = listen(fd, 0);
2402 if (ret < 0) {
2403 perror("listen");
2404 return -1;
2405 }
bellard858693c2004-03-31 18:52:07 +00002406 return fd;
2407}
2408
2409int gdbserver_start(int port)
2410{
2411 gdbserver_fd = gdbserver_open(port);
2412 if (gdbserver_fd < 0)
2413 return -1;
2414 /* accept connections */
aliguori880a7572008-11-18 20:30:24 +00002415 gdb_accept();
bellardb4608c02003-06-27 17:34:32 +00002416 return 0;
2417}
aurel322b1319c2008-12-18 22:44:04 +00002418
2419/* Disable gdb stub for child processes. */
2420void gdbserver_fork(CPUState *env)
2421{
2422 GDBState *s = gdbserver_state;
edgar_igl9f6164d2009-01-07 10:22:28 +00002423 if (gdbserver_fd < 0 || s->fd < 0)
aurel322b1319c2008-12-18 22:44:04 +00002424 return;
2425 close(s->fd);
2426 s->fd = -1;
2427 cpu_breakpoint_remove_all(env, BP_GDB);
2428 cpu_watchpoint_remove_all(env, BP_GDB);
2429}
pbrook4046d912007-01-28 01:53:16 +00002430#else
thsaa1f17c2007-07-11 22:48:58 +00002431static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00002432{
pbrook56aebc82008-10-11 17:55:29 +00002433 /* We can handle an arbitrarily large amount of data.
2434 Pick the maximum packet size, which is as good as anything. */
2435 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00002436}
2437
thsaa1f17c2007-07-11 22:48:58 +00002438static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00002439{
pbrook4046d912007-01-28 01:53:16 +00002440 int i;
2441
2442 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00002443 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00002444 }
2445}
2446
2447static void gdb_chr_event(void *opaque, int event)
2448{
2449 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05302450 case CHR_EVENT_OPENED:
pbrook4046d912007-01-28 01:53:16 +00002451 vm_stop(EXCP_INTERRUPT);
pbrook56aebc82008-10-11 17:55:29 +00002452 gdb_has_xml = 0;
pbrook4046d912007-01-28 01:53:16 +00002453 break;
2454 default:
2455 break;
2456 }
2457}
2458
aliguori8a34a0f2009-03-05 23:01:55 +00002459static void gdb_monitor_output(GDBState *s, const char *msg, int len)
2460{
2461 char buf[MAX_PACKET_LENGTH];
2462
2463 buf[0] = 'O';
2464 if (len > (MAX_PACKET_LENGTH/2) - 1)
2465 len = (MAX_PACKET_LENGTH/2) - 1;
2466 memtohex(buf + 1, (uint8_t *)msg, len);
2467 put_packet(s, buf);
2468}
2469
2470static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
2471{
2472 const char *p = (const char *)buf;
2473 int max_sz;
2474
2475 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
2476 for (;;) {
2477 if (len <= max_sz) {
2478 gdb_monitor_output(gdbserver_state, p, len);
2479 break;
2480 }
2481 gdb_monitor_output(gdbserver_state, p, max_sz);
2482 p += max_sz;
2483 len -= max_sz;
2484 }
2485 return len;
2486}
2487
aliguori59030a82009-04-05 18:43:41 +00002488#ifndef _WIN32
2489static void gdb_sigterm_handler(int signal)
2490{
2491 if (vm_running)
2492 vm_stop(EXCP_INTERRUPT);
2493}
2494#endif
2495
2496int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00002497{
2498 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00002499 char gdbstub_device_name[128];
aliguori36556b22009-03-28 18:05:53 +00002500 CharDriverState *chr = NULL;
2501 CharDriverState *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00002502
aliguori59030a82009-04-05 18:43:41 +00002503 if (!device)
2504 return -1;
2505 if (strcmp(device, "none") != 0) {
2506 if (strstart(device, "tcp:", NULL)) {
2507 /* enforce required TCP attributes */
2508 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
2509 "%s,nowait,nodelay,server", device);
2510 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00002511 }
aliguori59030a82009-04-05 18:43:41 +00002512#ifndef _WIN32
2513 else if (strcmp(device, "stdio") == 0) {
2514 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00002515
aliguori59030a82009-04-05 18:43:41 +00002516 memset(&act, 0, sizeof(act));
2517 act.sa_handler = gdb_sigterm_handler;
2518 sigaction(SIGINT, &act, NULL);
2519 }
2520#endif
2521 chr = qemu_chr_open("gdb", device, NULL);
aliguori36556b22009-03-28 18:05:53 +00002522 if (!chr)
2523 return -1;
2524
2525 qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
2526 gdb_chr_event, NULL);
pbrookcfc34752007-02-22 01:48:01 +00002527 }
2528
aliguori36556b22009-03-28 18:05:53 +00002529 s = gdbserver_state;
2530 if (!s) {
2531 s = qemu_mallocz(sizeof(GDBState));
2532 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00002533
aliguori36556b22009-03-28 18:05:53 +00002534 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
2535
2536 /* Initialize a monitor terminal for gdb */
2537 mon_chr = qemu_mallocz(sizeof(*mon_chr));
2538 mon_chr->chr_write = gdb_monitor_write;
2539 monitor_init(mon_chr, 0);
2540 } else {
2541 if (s->chr)
2542 qemu_chr_close(s->chr);
2543 mon_chr = s->mon_chr;
2544 memset(s, 0, sizeof(GDBState));
2545 }
aliguori880a7572008-11-18 20:30:24 +00002546 s->c_cpu = first_cpu;
2547 s->g_cpu = first_cpu;
pbrook4046d912007-01-28 01:53:16 +00002548 s->chr = chr;
aliguori36556b22009-03-28 18:05:53 +00002549 s->state = chr ? RS_IDLE : RS_INACTIVE;
2550 s->mon_chr = mon_chr;
aliguori8a34a0f2009-03-05 23:01:55 +00002551
pbrook4046d912007-01-28 01:53:16 +00002552 return 0;
2553}
2554#endif