blob: 75271954d548c1b1a0cdf9f2a300e2f4a76b577d [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
Paolo Bonzini83c90892012-12-17 18:19:49 +010032#include "monitor/monitor.h"
Paolo Bonzinidccfcd02013-04-08 16:55:25 +020033#include "sysemu/char.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010034#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010035#include "exec/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
Blue Swirl2b41f102011-06-19 20:38:22 +000040#include "cpu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010041#include "qemu/sockets.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010042#include "sysemu/kvm.h"
Richard Henderson6ee77b12012-08-23 10:44:45 -070043#include "qemu/bitops.h"
aurel32ca587a82008-12-18 22:44:13 +000044
Andreas Färberf3659ee2013-06-27 19:09:09 +020045static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
46 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020047{
Andreas Färberf3659ee2013-06-27 19:09:09 +020048 CPUClass *cc = CPU_GET_CLASS(cpu);
49
50 if (cc->memory_rw_debug) {
51 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
52 }
53 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020054}
aurel32ca587a82008-12-18 22:44:13 +000055
56enum {
57 GDB_SIGNAL_0 = 0,
58 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +010059 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +000060 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +010061 GDB_SIGNAL_ABRT = 6,
62 GDB_SIGNAL_ALRM = 14,
63 GDB_SIGNAL_IO = 23,
64 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +000065 GDB_SIGNAL_UNKNOWN = 143
66};
67
68#ifdef CONFIG_USER_ONLY
69
70/* Map target signal numbers to GDB protocol signal numbers and vice
71 * versa. For user emulation's currently supported systems, we can
72 * assume most signals are defined.
73 */
74
75static int gdb_signal_table[] = {
76 0,
77 TARGET_SIGHUP,
78 TARGET_SIGINT,
79 TARGET_SIGQUIT,
80 TARGET_SIGILL,
81 TARGET_SIGTRAP,
82 TARGET_SIGABRT,
83 -1, /* SIGEMT */
84 TARGET_SIGFPE,
85 TARGET_SIGKILL,
86 TARGET_SIGBUS,
87 TARGET_SIGSEGV,
88 TARGET_SIGSYS,
89 TARGET_SIGPIPE,
90 TARGET_SIGALRM,
91 TARGET_SIGTERM,
92 TARGET_SIGURG,
93 TARGET_SIGSTOP,
94 TARGET_SIGTSTP,
95 TARGET_SIGCONT,
96 TARGET_SIGCHLD,
97 TARGET_SIGTTIN,
98 TARGET_SIGTTOU,
99 TARGET_SIGIO,
100 TARGET_SIGXCPU,
101 TARGET_SIGXFSZ,
102 TARGET_SIGVTALRM,
103 TARGET_SIGPROF,
104 TARGET_SIGWINCH,
105 -1, /* SIGLOST */
106 TARGET_SIGUSR1,
107 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000108#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000109 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000110#else
111 -1,
112#endif
aurel32ca587a82008-12-18 22:44:13 +0000113 -1, /* SIGPOLL */
114 -1,
115 -1,
116 -1,
117 -1,
118 -1,
119 -1,
120 -1,
121 -1,
122 -1,
123 -1,
124 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000125#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000126 __SIGRTMIN + 1,
127 __SIGRTMIN + 2,
128 __SIGRTMIN + 3,
129 __SIGRTMIN + 4,
130 __SIGRTMIN + 5,
131 __SIGRTMIN + 6,
132 __SIGRTMIN + 7,
133 __SIGRTMIN + 8,
134 __SIGRTMIN + 9,
135 __SIGRTMIN + 10,
136 __SIGRTMIN + 11,
137 __SIGRTMIN + 12,
138 __SIGRTMIN + 13,
139 __SIGRTMIN + 14,
140 __SIGRTMIN + 15,
141 __SIGRTMIN + 16,
142 __SIGRTMIN + 17,
143 __SIGRTMIN + 18,
144 __SIGRTMIN + 19,
145 __SIGRTMIN + 20,
146 __SIGRTMIN + 21,
147 __SIGRTMIN + 22,
148 __SIGRTMIN + 23,
149 __SIGRTMIN + 24,
150 __SIGRTMIN + 25,
151 __SIGRTMIN + 26,
152 __SIGRTMIN + 27,
153 __SIGRTMIN + 28,
154 __SIGRTMIN + 29,
155 __SIGRTMIN + 30,
156 __SIGRTMIN + 31,
157 -1, /* SIGCANCEL */
158 __SIGRTMIN,
159 __SIGRTMIN + 32,
160 __SIGRTMIN + 33,
161 __SIGRTMIN + 34,
162 __SIGRTMIN + 35,
163 __SIGRTMIN + 36,
164 __SIGRTMIN + 37,
165 __SIGRTMIN + 38,
166 __SIGRTMIN + 39,
167 __SIGRTMIN + 40,
168 __SIGRTMIN + 41,
169 __SIGRTMIN + 42,
170 __SIGRTMIN + 43,
171 __SIGRTMIN + 44,
172 __SIGRTMIN + 45,
173 __SIGRTMIN + 46,
174 __SIGRTMIN + 47,
175 __SIGRTMIN + 48,
176 __SIGRTMIN + 49,
177 __SIGRTMIN + 50,
178 __SIGRTMIN + 51,
179 __SIGRTMIN + 52,
180 __SIGRTMIN + 53,
181 __SIGRTMIN + 54,
182 __SIGRTMIN + 55,
183 __SIGRTMIN + 56,
184 __SIGRTMIN + 57,
185 __SIGRTMIN + 58,
186 __SIGRTMIN + 59,
187 __SIGRTMIN + 60,
188 __SIGRTMIN + 61,
189 __SIGRTMIN + 62,
190 __SIGRTMIN + 63,
191 __SIGRTMIN + 64,
192 __SIGRTMIN + 65,
193 __SIGRTMIN + 66,
194 __SIGRTMIN + 67,
195 __SIGRTMIN + 68,
196 __SIGRTMIN + 69,
197 __SIGRTMIN + 70,
198 __SIGRTMIN + 71,
199 __SIGRTMIN + 72,
200 __SIGRTMIN + 73,
201 __SIGRTMIN + 74,
202 __SIGRTMIN + 75,
203 __SIGRTMIN + 76,
204 __SIGRTMIN + 77,
205 __SIGRTMIN + 78,
206 __SIGRTMIN + 79,
207 __SIGRTMIN + 80,
208 __SIGRTMIN + 81,
209 __SIGRTMIN + 82,
210 __SIGRTMIN + 83,
211 __SIGRTMIN + 84,
212 __SIGRTMIN + 85,
213 __SIGRTMIN + 86,
214 __SIGRTMIN + 87,
215 __SIGRTMIN + 88,
216 __SIGRTMIN + 89,
217 __SIGRTMIN + 90,
218 __SIGRTMIN + 91,
219 __SIGRTMIN + 92,
220 __SIGRTMIN + 93,
221 __SIGRTMIN + 94,
222 __SIGRTMIN + 95,
223 -1, /* SIGINFO */
224 -1, /* UNKNOWN */
225 -1, /* DEFAULT */
226 -1,
227 -1,
228 -1,
229 -1,
230 -1,
231 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000232#endif
aurel32ca587a82008-12-18 22:44:13 +0000233};
bellard8f447cc2006-06-14 15:21:14 +0000234#else
aurel32ca587a82008-12-18 22:44:13 +0000235/* In system mode we only need SIGINT and SIGTRAP; other signals
236 are not yet supported. */
237
238enum {
239 TARGET_SIGINT = 2,
240 TARGET_SIGTRAP = 5
241};
242
243static int gdb_signal_table[] = {
244 -1,
245 -1,
246 TARGET_SIGINT,
247 -1,
248 -1,
249 TARGET_SIGTRAP
250};
bellard8f447cc2006-06-14 15:21:14 +0000251#endif
bellardb4608c02003-06-27 17:34:32 +0000252
aurel32ca587a82008-12-18 22:44:13 +0000253#ifdef CONFIG_USER_ONLY
254static int target_signal_to_gdb (int sig)
255{
256 int i;
257 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
258 if (gdb_signal_table[i] == sig)
259 return i;
260 return GDB_SIGNAL_UNKNOWN;
261}
262#endif
263
264static int gdb_signal_to_target (int sig)
265{
266 if (sig < ARRAY_SIZE (gdb_signal_table))
267 return gdb_signal_table[sig];
268 else
269 return -1;
270}
271
bellard4abe6152003-07-26 18:01:58 +0000272//#define DEBUG_GDB
bellardb4608c02003-06-27 17:34:32 +0000273
pbrook56aebc82008-10-11 17:55:29 +0000274typedef struct GDBRegisterState {
275 int base_reg;
276 int num_regs;
277 gdb_reg_cb get_reg;
278 gdb_reg_cb set_reg;
279 const char *xml;
280 struct GDBRegisterState *next;
281} GDBRegisterState;
282
bellard858693c2004-03-31 18:52:07 +0000283enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000284 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000285 RS_IDLE,
286 RS_GETLINE,
287 RS_CHKSUM1,
288 RS_CHKSUM2,
289};
bellard858693c2004-03-31 18:52:07 +0000290typedef struct GDBState {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200291 CPUState *c_cpu; /* current CPU for step/continue ops */
292 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200293 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000294 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000295 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000296 int line_buf_index;
297 int line_csum;
pbrook56aebc82008-10-11 17:55:29 +0000298 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000299 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000300 int signal;
bellard41625032005-04-24 10:07:11 +0000301#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000302 int fd;
bellard41625032005-04-24 10:07:11 +0000303 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000304#else
305 CharDriverState *chr;
aliguori8a34a0f2009-03-05 23:01:55 +0000306 CharDriverState *mon_chr;
bellard41625032005-04-24 10:07:11 +0000307#endif
Meador Ingecdb432b2012-03-15 17:49:45 +0000308 char syscall_buf[256];
309 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000310} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000311
edgar_igl60897d32008-05-09 08:25:14 +0000312/* By default use no IRQs and no timers while single stepping so as to
313 * make single stepping like an ICE HW step.
314 */
315static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
316
aliguori880a7572008-11-18 20:30:24 +0000317static GDBState *gdbserver_state;
318
pbrook56aebc82008-10-11 17:55:29 +0000319/* This is an ugly hack to cope with both new and old gdb.
320 If gdb sends qXfer:features:read then assume we're talking to a newish
321 gdb that understands target descriptions. */
322static int gdb_has_xml;
323
bellard1fddef42005-04-17 19:16:13 +0000324#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000325/* XXX: This is not thread safe. Do we care? */
326static int gdbserver_fd = -1;
327
bellard858693c2004-03-31 18:52:07 +0000328static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000329{
330 uint8_t ch;
331 int ret;
332
333 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000334 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000335 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000336 if (errno == ECONNRESET)
337 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000338 if (errno != EINTR && errno != EAGAIN)
339 return -1;
340 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000341 close(s->fd);
342 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000343 return -1;
344 } else {
345 break;
346 }
347 }
348 return ch;
349}
pbrook4046d912007-01-28 01:53:16 +0000350#endif
bellardb4608c02003-06-27 17:34:32 +0000351
blueswir1654efcf2009-04-18 07:29:59 +0000352static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000353 GDB_SYS_UNKNOWN,
354 GDB_SYS_ENABLED,
355 GDB_SYS_DISABLED,
356} gdb_syscall_mode;
357
358/* If gdb is connected when the first semihosting syscall occurs then use
359 remote gdb syscalls. Otherwise use native file IO. */
360int use_gdb_syscalls(void)
361{
362 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000363 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
364 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000365 }
366 return gdb_syscall_mode == GDB_SYS_ENABLED;
367}
368
edgar_iglba70a622008-03-14 06:10:42 +0000369/* Resume execution. */
370static inline void gdb_continue(GDBState *s)
371{
372#ifdef CONFIG_USER_ONLY
373 s->running_state = 1;
374#else
Paolo Bonzinibc7d0e62013-06-03 17:06:55 +0200375 if (runstate_check(RUN_STATE_GUEST_PANICKED)) {
376 runstate_set(RUN_STATE_DEBUG);
377 }
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200378 if (!runstate_needs_reset()) {
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200379 vm_start();
380 }
edgar_iglba70a622008-03-14 06:10:42 +0000381#endif
382}
383
bellard858693c2004-03-31 18:52:07 +0000384static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000385{
pbrook4046d912007-01-28 01:53:16 +0000386#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000387 int ret;
388
389 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000390 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000391 if (ret < 0) {
392 if (errno != EINTR && errno != EAGAIN)
393 return;
394 } else {
395 buf += ret;
396 len -= ret;
397 }
398 }
pbrook4046d912007-01-28 01:53:16 +0000399#else
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500400 qemu_chr_fe_write(s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000401#endif
bellardb4608c02003-06-27 17:34:32 +0000402}
403
404static inline int fromhex(int v)
405{
406 if (v >= '0' && v <= '9')
407 return v - '0';
408 else if (v >= 'A' && v <= 'F')
409 return v - 'A' + 10;
410 else if (v >= 'a' && v <= 'f')
411 return v - 'a' + 10;
412 else
413 return 0;
414}
415
416static inline int tohex(int v)
417{
418 if (v < 10)
419 return v + '0';
420 else
421 return v - 10 + 'a';
422}
423
424static void memtohex(char *buf, const uint8_t *mem, int len)
425{
426 int i, c;
427 char *q;
428 q = buf;
429 for(i = 0; i < len; i++) {
430 c = mem[i];
431 *q++ = tohex(c >> 4);
432 *q++ = tohex(c & 0xf);
433 }
434 *q = '\0';
435}
436
437static void hextomem(uint8_t *mem, const char *buf, int len)
438{
439 int i;
440
441 for(i = 0; i < len; i++) {
442 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
443 buf += 2;
444 }
445}
446
bellardb4608c02003-06-27 17:34:32 +0000447/* return -1 if error, 0 if OK */
pbrook56aebc82008-10-11 17:55:29 +0000448static int put_packet_binary(GDBState *s, const char *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000449{
pbrook56aebc82008-10-11 17:55:29 +0000450 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000451 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000452
bellardb4608c02003-06-27 17:34:32 +0000453 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000454 p = s->last_packet;
455 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000456 memcpy(p, buf, len);
457 p += len;
bellardb4608c02003-06-27 17:34:32 +0000458 csum = 0;
459 for(i = 0; i < len; i++) {
460 csum += buf[i];
461 }
pbrook4046d912007-01-28 01:53:16 +0000462 *(p++) = '#';
463 *(p++) = tohex((csum >> 4) & 0xf);
464 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000465
pbrook4046d912007-01-28 01:53:16 +0000466 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000467 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000468
pbrook4046d912007-01-28 01:53:16 +0000469#ifdef CONFIG_USER_ONLY
470 i = get_char(s);
471 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000472 return -1;
pbrook4046d912007-01-28 01:53:16 +0000473 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000474 break;
pbrook4046d912007-01-28 01:53:16 +0000475#else
476 break;
477#endif
bellardb4608c02003-06-27 17:34:32 +0000478 }
479 return 0;
480}
481
pbrook56aebc82008-10-11 17:55:29 +0000482/* return -1 if error, 0 if OK */
483static int put_packet(GDBState *s, const char *buf)
484{
485#ifdef DEBUG_GDB
486 printf("reply='%s'\n", buf);
487#endif
488
489 return put_packet_binary(s, buf, strlen(buf));
490}
491
492/* The GDB remote protocol transfers values in target byte order. This means
493 we can use the raw memory access routines to access the value buffer.
494 Conveniently, these also handle the case where the buffer is mis-aligned.
495 */
496#define GET_REG8(val) do { \
497 stb_p(mem_buf, val); \
498 return 1; \
499 } while(0)
500#define GET_REG16(val) do { \
501 stw_p(mem_buf, val); \
502 return 2; \
503 } while(0)
504#define GET_REG32(val) do { \
505 stl_p(mem_buf, val); \
506 return 4; \
507 } while(0)
508#define GET_REG64(val) do { \
509 stq_p(mem_buf, val); \
510 return 8; \
511 } while(0)
512
513#if TARGET_LONG_BITS == 64
514#define GET_REGL(val) GET_REG64(val)
515#define ldtul_p(addr) ldq_p(addr)
516#else
517#define GET_REGL(val) GET_REG32(val)
518#define ldtul_p(addr) ldl_p(addr)
519#endif
520
edgar_iglfde3fd62008-05-09 08:50:01 +0000521#if defined(TARGET_I386)
balrog5ad265e2007-10-31 00:21:35 +0000522
Andreas Färberf20f9df2013-07-07 12:07:54 +0200523#include "target-i386/gdbstub.c"
bellard6da41ea2004-01-04 15:48:38 +0000524
bellard9e62fd72004-01-05 22:49:06 +0000525#elif defined (TARGET_PPC)
pbrook56aebc82008-10-11 17:55:29 +0000526
aurel32e571cb42009-01-24 15:07:42 +0000527#if defined (TARGET_PPC64)
528#define GDB_CORE_XML "power64-core.xml"
529#else
530#define GDB_CORE_XML "power-core.xml"
531#endif
pbrook56aebc82008-10-11 17:55:29 +0000532
Andreas Färber0980bfa2013-07-07 12:26:33 +0200533#include "target-ppc/gdbstub.c"
pbrook56aebc82008-10-11 17:55:29 +0000534
bellarde95c8d52004-09-30 22:22:08 +0000535#elif defined (TARGET_SPARC)
bellarde95c8d52004-09-30 22:22:08 +0000536
Andreas Färberd19c87f2013-07-07 12:29:26 +0200537#include "target-sparc/gdbstub.c"
pbrook56aebc82008-10-11 17:55:29 +0000538
bellard1fddef42005-04-17 19:16:13 +0000539#elif defined (TARGET_ARM)
pbrook56aebc82008-10-11 17:55:29 +0000540
pbrook56aebc82008-10-11 17:55:29 +0000541#define GDB_CORE_XML "arm-core.xml"
542
Andreas Färber58850da2013-07-07 12:32:15 +0200543#include "target-arm/gdbstub.c"
pbrook56aebc82008-10-11 17:55:29 +0000544
pbrooke6e59062006-10-22 00:18:54 +0000545#elif defined (TARGET_M68K)
pbrook56aebc82008-10-11 17:55:29 +0000546
pbrook56aebc82008-10-11 17:55:29 +0000547#define GDB_CORE_XML "cf-core.xml"
548
Andreas Färberc88de142013-07-07 12:33:56 +0200549#include "target-m68k/gdbstub.c"
pbrooke6e59062006-10-22 00:18:54 +0000550
bellard6f970bd2005-12-05 19:55:19 +0000551#elif defined (TARGET_MIPS)
pbrook56aebc82008-10-11 17:55:29 +0000552
Andreas Färber814ac262013-07-07 12:38:42 +0200553#include "target-mips/gdbstub.c"
ths36d23952007-02-28 22:37:42 +0000554
Jia Liufc043552012-07-20 15:50:50 +0800555#elif defined(TARGET_OPENRISC)
556
Andreas Färber30028732013-07-07 12:40:38 +0200557#include "target-openrisc/gdbstub.c"
Jia Liufc043552012-07-20 15:50:50 +0800558
bellardfdf9b3e2006-04-27 21:07:38 +0000559#elif defined (TARGET_SH4)
ths6ef99fc2007-05-13 16:36:24 +0000560
Andreas Färber2f937732013-07-07 12:42:52 +0200561#include "target-sh4/gdbstub.c"
ths6ef99fc2007-05-13 16:36:24 +0000562
Edgar E. Iglesiasd74d6a92009-05-20 20:16:31 +0200563#elif defined (TARGET_MICROBLAZE)
564
Andreas Färbereabfc232013-07-07 12:45:47 +0200565#include "target-microblaze/gdbstub.c"
Edgar E. Iglesiasd74d6a92009-05-20 20:16:31 +0200566
thsf1ccf902007-10-08 13:16:14 +0000567#elif defined (TARGET_CRIS)
568
Andreas Färber213c19d2013-07-07 12:50:22 +0200569#include "target-cris/gdbstub.c"
Edgar E. Iglesias4a0b59f2010-02-20 19:51:56 +0100570
aurel3219bf5172008-12-07 23:26:32 +0000571#elif defined (TARGET_ALPHA)
572
Andreas Färberc3ce8eb2013-07-07 12:52:32 +0200573#include "target-alpha/gdbstub.c"
aurel3219bf5172008-12-07 23:26:32 +0000574
Alexander Grafafcb0e42009-12-05 12:44:29 +0100575#elif defined (TARGET_S390X)
576
Andreas Färbercfae5c92013-07-07 12:54:12 +0200577#include "target-s390x/gdbstub.c"
Richard Henderson6ee77b12012-08-23 10:44:45 -0700578
Michael Walle0c45d3d2011-02-17 23:45:06 +0100579#elif defined (TARGET_LM32)
580
Andreas Färberd0ff8d02013-07-07 12:55:44 +0200581#include "target-lm32/gdbstub.c"
Michael Walle0c45d3d2011-02-17 23:45:06 +0100582
Max Filippovccfcaba2011-09-06 03:55:52 +0400583#elif defined(TARGET_XTENSA)
584
Andreas Färber25d8ac02013-07-07 12:57:38 +0200585#include "target-xtensa/gdbstub.c"
Max Filippovccfcaba2011-09-06 03:55:52 +0400586
bellard1fddef42005-04-17 19:16:13 +0000587#else
pbrook56aebc82008-10-11 17:55:29 +0000588
Andreas Färber9349b4f2012-03-14 01:38:32 +0100589static int cpu_gdb_read_register(CPUArchState *env, uint8_t *mem_buf, int n)
bellard6da41ea2004-01-04 15:48:38 +0000590{
591 return 0;
592}
593
Andreas Färber9349b4f2012-03-14 01:38:32 +0100594static int cpu_gdb_write_register(CPUArchState *env, uint8_t *mem_buf, int n)
bellard6da41ea2004-01-04 15:48:38 +0000595{
pbrook56aebc82008-10-11 17:55:29 +0000596 return 0;
bellard6da41ea2004-01-04 15:48:38 +0000597}
598
599#endif
bellardb4608c02003-06-27 17:34:32 +0000600
pbrook56aebc82008-10-11 17:55:29 +0000601#ifdef GDB_CORE_XML
602/* Encode data using the encoding for 'x' packets. */
603static int memtox(char *buf, const char *mem, int len)
604{
605 char *p = buf;
606 char c;
607
608 while (len--) {
609 c = *(mem++);
610 switch (c) {
611 case '#': case '$': case '*': case '}':
612 *(p++) = '}';
613 *(p++) = c ^ 0x20;
614 break;
615 default:
616 *(p++) = c;
617 break;
618 }
619 }
620 return p - buf;
621}
622
aurel323faf7782008-12-07 23:26:17 +0000623static const char *get_feature_xml(const char *p, const char **newp)
pbrook56aebc82008-10-11 17:55:29 +0000624{
pbrook56aebc82008-10-11 17:55:29 +0000625 size_t len;
626 int i;
627 const char *name;
628 static char target_xml[1024];
629
630 len = 0;
631 while (p[len] && p[len] != ':')
632 len++;
633 *newp = p + len;
634
635 name = NULL;
636 if (strncmp(p, "target.xml", len) == 0) {
637 /* Generate the XML description for this CPU. */
638 if (!target_xml[0]) {
639 GDBRegisterState *r;
Andreas Färbereac8b352013-06-28 21:11:37 +0200640 CPUState *cpu = first_cpu;
pbrook56aebc82008-10-11 17:55:29 +0000641
blueswir15b3715b2008-10-25 11:18:12 +0000642 snprintf(target_xml, sizeof(target_xml),
643 "<?xml version=\"1.0\"?>"
644 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
645 "<target>"
646 "<xi:include href=\"%s\"/>",
647 GDB_CORE_XML);
pbrook56aebc82008-10-11 17:55:29 +0000648
Andreas Färbereac8b352013-06-28 21:11:37 +0200649 for (r = cpu->gdb_regs; r; r = r->next) {
blueswir12dc766d2009-04-13 16:06:19 +0000650 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
651 pstrcat(target_xml, sizeof(target_xml), r->xml);
652 pstrcat(target_xml, sizeof(target_xml), "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000653 }
blueswir12dc766d2009-04-13 16:06:19 +0000654 pstrcat(target_xml, sizeof(target_xml), "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000655 }
656 return target_xml;
657 }
658 for (i = 0; ; i++) {
659 name = xml_builtin[i][0];
660 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
661 break;
662 }
663 return name ? xml_builtin[i][1] : NULL;
664}
665#endif
666
Andreas Färber385b9f02013-06-27 18:25:36 +0200667static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000668{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200669 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200670 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000671 GDBRegisterState *r;
672
Andreas Färbera0e372f2013-06-28 23:18:47 +0200673 if (reg < cc->gdb_num_core_regs) {
pbrook56aebc82008-10-11 17:55:29 +0000674 return cpu_gdb_read_register(env, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200675 }
pbrook56aebc82008-10-11 17:55:29 +0000676
Andreas Färbereac8b352013-06-28 21:11:37 +0200677 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000678 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
679 return r->get_reg(env, mem_buf, reg - r->base_reg);
680 }
681 }
682 return 0;
683}
684
Andreas Färber385b9f02013-06-27 18:25:36 +0200685static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000686{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200687 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200688 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000689 GDBRegisterState *r;
690
Andreas Färbera0e372f2013-06-28 23:18:47 +0200691 if (reg < cc->gdb_num_core_regs) {
pbrook56aebc82008-10-11 17:55:29 +0000692 return cpu_gdb_write_register(env, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200693 }
pbrook56aebc82008-10-11 17:55:29 +0000694
Andreas Färbereac8b352013-06-28 21:11:37 +0200695 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000696 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
697 return r->set_reg(env, mem_buf, reg - r->base_reg);
698 }
699 }
700 return 0;
701}
702
703/* Register a supplemental set of CPU registers. If g_pos is nonzero it
704 specifies the first register number and these registers are included in
705 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
706 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
707 */
708
Andreas Färber22169d42013-06-28 21:27:39 +0200709void gdb_register_coprocessor(CPUState *cpu,
710 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
711 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000712{
713 GDBRegisterState *s;
714 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000715
Andreas Färbereac8b352013-06-28 21:11:37 +0200716 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000717 while (*p) {
718 /* Check for duplicates. */
719 if (strcmp((*p)->xml, xml) == 0)
720 return;
721 p = &(*p)->next;
722 }
Stefan Weil9643c252011-10-18 22:25:38 +0200723
724 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200725 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200726 s->num_regs = num_regs;
727 s->get_reg = get_reg;
728 s->set_reg = set_reg;
729 s->xml = xml;
730
pbrook56aebc82008-10-11 17:55:29 +0000731 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200732 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000733 *p = s;
734 if (g_pos) {
735 if (g_pos != s->base_reg) {
736 fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
737 "Expected %d got %d\n", xml, g_pos, s->base_reg);
pbrook56aebc82008-10-11 17:55:29 +0000738 }
739 }
740}
741
aliguoria1d1bb32008-11-18 20:07:32 +0000742#ifndef CONFIG_USER_ONLY
743static const int xlat_gdb_type[] = {
744 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
745 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
746 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
747};
748#endif
749
aliguori880a7572008-11-18 20:30:24 +0000750static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000751{
Andreas Färber182735e2013-05-29 22:29:20 +0200752 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100753 CPUArchState *env;
aliguori880a7572008-11-18 20:30:24 +0000754 int err = 0;
755
Andreas Färber62278812013-06-27 17:12:06 +0200756 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200757 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200758 }
aliguorie22a25c2009-03-12 20:12:48 +0000759
aliguoria1d1bb32008-11-18 20:07:32 +0000760 switch (type) {
761 case GDB_BREAKPOINT_SW:
762 case GDB_BREAKPOINT_HW:
Andreas Färber182735e2013-05-29 22:29:20 +0200763 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
764 env = cpu->env_ptr;
aliguori880a7572008-11-18 20:30:24 +0000765 err = cpu_breakpoint_insert(env, addr, BP_GDB, NULL);
766 if (err)
767 break;
768 }
769 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000770#ifndef CONFIG_USER_ONLY
771 case GDB_WATCHPOINT_WRITE:
772 case GDB_WATCHPOINT_READ:
773 case GDB_WATCHPOINT_ACCESS:
Andreas Färber182735e2013-05-29 22:29:20 +0200774 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
775 env = cpu->env_ptr;
aliguori880a7572008-11-18 20:30:24 +0000776 err = cpu_watchpoint_insert(env, addr, len, xlat_gdb_type[type],
777 NULL);
778 if (err)
779 break;
780 }
781 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000782#endif
783 default:
784 return -ENOSYS;
785 }
786}
787
aliguori880a7572008-11-18 20:30:24 +0000788static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000789{
Andreas Färber182735e2013-05-29 22:29:20 +0200790 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100791 CPUArchState *env;
aliguori880a7572008-11-18 20:30:24 +0000792 int err = 0;
793
Andreas Färber62278812013-06-27 17:12:06 +0200794 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200795 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200796 }
aliguorie22a25c2009-03-12 20:12:48 +0000797
aliguoria1d1bb32008-11-18 20:07:32 +0000798 switch (type) {
799 case GDB_BREAKPOINT_SW:
800 case GDB_BREAKPOINT_HW:
Andreas Färber182735e2013-05-29 22:29:20 +0200801 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
802 env = cpu->env_ptr;
aliguori880a7572008-11-18 20:30:24 +0000803 err = cpu_breakpoint_remove(env, addr, BP_GDB);
804 if (err)
805 break;
806 }
807 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000808#ifndef CONFIG_USER_ONLY
809 case GDB_WATCHPOINT_WRITE:
810 case GDB_WATCHPOINT_READ:
811 case GDB_WATCHPOINT_ACCESS:
Andreas Färber182735e2013-05-29 22:29:20 +0200812 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
813 env = cpu->env_ptr;
aliguori880a7572008-11-18 20:30:24 +0000814 err = cpu_watchpoint_remove(env, addr, len, xlat_gdb_type[type]);
815 if (err)
816 break;
817 }
818 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000819#endif
820 default:
821 return -ENOSYS;
822 }
823}
824
aliguori880a7572008-11-18 20:30:24 +0000825static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +0000826{
Andreas Färber182735e2013-05-29 22:29:20 +0200827 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100828 CPUArchState *env;
aliguori880a7572008-11-18 20:30:24 +0000829
aliguorie22a25c2009-03-12 20:12:48 +0000830 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200831 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +0000832 return;
833 }
834
Andreas Färber182735e2013-05-29 22:29:20 +0200835 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
836 env = cpu->env_ptr;
aliguori880a7572008-11-18 20:30:24 +0000837 cpu_breakpoint_remove_all(env, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000838#ifndef CONFIG_USER_ONLY
aliguori880a7572008-11-18 20:30:24 +0000839 cpu_watchpoint_remove_all(env, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000840#endif
aliguori880a7572008-11-18 20:30:24 +0000841 }
aliguoria1d1bb32008-11-18 20:07:32 +0000842}
843
aurel32fab9d282009-04-08 21:29:37 +0000844static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
845{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200846 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +0200847 CPUClass *cc = CPU_GET_CLASS(cpu);
848
849 cpu_synchronize_state(cpu);
850 if (cc->set_pc) {
851 cc->set_pc(cpu, pc);
Nathan Froydff1d1972009-12-08 08:06:30 -0800852 }
aurel32fab9d282009-04-08 21:29:37 +0000853}
854
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200855static CPUState *find_cpu(uint32_t thread_id)
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700856{
Andreas Färber0d342822012-12-17 07:12:13 +0100857 CPUState *cpu;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700858
Andreas Färber182735e2013-05-29 22:29:20 +0200859 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
Andreas Färberaa48dd92013-07-09 20:50:52 +0200860 if (cpu_index(cpu) == thread_id) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200861 return cpu;
Andreas Färberaa48dd92013-07-09 20:50:52 +0200862 }
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700863 }
Andreas Färberaa48dd92013-07-09 20:50:52 +0200864
865 return NULL;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700866}
867
aliguori880a7572008-11-18 20:30:24 +0000868static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +0000869{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200870 CPUState *cpu;
bellardb4608c02003-06-27 17:34:32 +0000871 const char *p;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700872 uint32_t thread;
873 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +0000874 char buf[MAX_PACKET_LENGTH];
875 uint8_t mem_buf[MAX_PACKET_LENGTH];
876 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +0000877 target_ulong addr, len;
ths3b46e622007-09-17 08:09:54 +0000878
bellard858693c2004-03-31 18:52:07 +0000879#ifdef DEBUG_GDB
880 printf("command='%s'\n", line_buf);
bellard4c3a88a2003-07-26 12:06:08 +0000881#endif
bellard858693c2004-03-31 18:52:07 +0000882 p = line_buf;
883 ch = *p++;
884 switch(ch) {
885 case '?':
bellard1fddef42005-04-17 19:16:13 +0000886 /* TODO: Make this return the correct value for user-mode. */
aurel32ca587a82008-12-18 22:44:13 +0000887 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200888 cpu_index(s->c_cpu));
bellard858693c2004-03-31 18:52:07 +0000889 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +0000890 /* Remove all the breakpoints when this query is issued,
891 * because gdb is doing and initial connect and the state
892 * should be cleaned up.
893 */
aliguori880a7572008-11-18 20:30:24 +0000894 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +0000895 break;
896 case 'c':
897 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +0000898 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +0000899 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +0000900 }
aurel32ca587a82008-12-18 22:44:13 +0000901 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +0000902 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +0000903 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +0000904 case 'C':
aurel32ca587a82008-12-18 22:44:13 +0000905 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
906 if (s->signal == -1)
907 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +0000908 gdb_continue(s);
909 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200910 case 'v':
911 if (strncmp(p, "Cont", 4) == 0) {
912 int res_signal, res_thread;
913
914 p += 4;
915 if (*p == '?') {
916 put_packet(s, "vCont;c;C;s;S");
917 break;
918 }
919 res = 0;
920 res_signal = 0;
921 res_thread = 0;
922 while (*p) {
923 int action, signal;
924
925 if (*p++ != ';') {
926 res = 0;
927 break;
928 }
929 action = *p++;
930 signal = 0;
931 if (action == 'C' || action == 'S') {
932 signal = strtoul(p, (char **)&p, 16);
933 } else if (action != 'c' && action != 's') {
934 res = 0;
935 break;
936 }
937 thread = 0;
938 if (*p == ':') {
939 thread = strtoull(p+1, (char **)&p, 16);
940 }
941 action = tolower(action);
942 if (res == 0 || (res == 'c' && action == 's')) {
943 res = action;
944 res_signal = signal;
945 res_thread = thread;
946 }
947 }
948 if (res) {
949 if (res_thread != -1 && res_thread != 0) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200950 cpu = find_cpu(res_thread);
951 if (cpu == NULL) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200952 put_packet(s, "E22");
953 break;
954 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200955 s->c_cpu = cpu;
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200956 }
957 if (res == 's') {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200958 cpu_single_step(s->c_cpu, sstep_flags);
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200959 }
960 s->signal = res_signal;
961 gdb_continue(s);
962 return RS_IDLE;
963 }
964 break;
965 } else {
966 goto unknown_command;
967 }
edgar_igl7d03f822008-05-17 18:58:29 +0000968 case 'k':
Jan Kiszka00e94db2012-03-06 18:32:35 +0100969#ifdef CONFIG_USER_ONLY
edgar_igl7d03f822008-05-17 18:58:29 +0000970 /* Kill the target */
971 fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
972 exit(0);
Jan Kiszka00e94db2012-03-06 18:32:35 +0100973#endif
edgar_igl7d03f822008-05-17 18:58:29 +0000974 case 'D':
975 /* Detach packet */
aliguori880a7572008-11-18 20:30:24 +0000976 gdb_breakpoint_remove_all();
Daniel Gutson7ea06da2010-02-26 14:13:50 -0300977 gdb_syscall_mode = GDB_SYS_DISABLED;
edgar_igl7d03f822008-05-17 18:58:29 +0000978 gdb_continue(s);
979 put_packet(s, "OK");
980 break;
bellard858693c2004-03-31 18:52:07 +0000981 case 's':
982 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +0000983 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +0000984 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +0000985 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200986 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +0000987 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +0000988 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +0000989 case 'F':
990 {
991 target_ulong ret;
992 target_ulong err;
993
994 ret = strtoull(p, (char **)&p, 16);
995 if (*p == ',') {
996 p++;
997 err = strtoull(p, (char **)&p, 16);
998 } else {
999 err = 0;
1000 }
1001 if (*p == ',')
1002 p++;
1003 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001004 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001005 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +00001006 s->current_syscall_cb = NULL;
1007 }
pbrooka2d1eba2007-01-28 03:10:55 +00001008 if (type == 'C') {
1009 put_packet(s, "T02");
1010 } else {
edgar_iglba70a622008-03-14 06:10:42 +00001011 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +00001012 }
1013 }
1014 break;
bellard858693c2004-03-31 18:52:07 +00001015 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001016 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001017 len = 0;
Andreas Färbera0e372f2013-06-28 23:18:47 +02001018 for (addr = 0; addr < s->g_cpu->gdb_num_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001019 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +00001020 len += reg_size;
1021 }
1022 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +00001023 put_packet(s, buf);
1024 break;
1025 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001026 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001027 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +00001028 len = strlen(p) / 2;
1029 hextomem((uint8_t *)registers, p, len);
Andreas Färbera0e372f2013-06-28 23:18:47 +02001030 for (addr = 0; addr < s->g_cpu->gdb_num_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001031 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001032 len -= reg_size;
1033 registers += reg_size;
1034 }
bellard858693c2004-03-31 18:52:07 +00001035 put_packet(s, "OK");
1036 break;
1037 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001038 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001039 if (*p == ',')
1040 p++;
bellard9d9754a2006-06-25 15:32:37 +00001041 len = strtoull(p, NULL, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001042 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001043 put_packet (s, "E14");
1044 } else {
1045 memtohex(buf, mem_buf, len);
1046 put_packet(s, buf);
1047 }
bellard858693c2004-03-31 18:52:07 +00001048 break;
1049 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001050 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001051 if (*p == ',')
1052 p++;
bellard9d9754a2006-06-25 15:32:37 +00001053 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001054 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001055 p++;
1056 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001057 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +02001058 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +00001059 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001060 } else {
bellard858693c2004-03-31 18:52:07 +00001061 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001062 }
bellard858693c2004-03-31 18:52:07 +00001063 break;
pbrook56aebc82008-10-11 17:55:29 +00001064 case 'p':
1065 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1066 This works, but can be very slow. Anything new enough to
1067 understand XML also knows how to use this properly. */
1068 if (!gdb_has_xml)
1069 goto unknown_command;
1070 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001071 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001072 if (reg_size) {
1073 memtohex(buf, mem_buf, reg_size);
1074 put_packet(s, buf);
1075 } else {
1076 put_packet(s, "E14");
1077 }
1078 break;
1079 case 'P':
1080 if (!gdb_has_xml)
1081 goto unknown_command;
1082 addr = strtoull(p, (char **)&p, 16);
1083 if (*p == '=')
1084 p++;
1085 reg_size = strlen(p) / 2;
1086 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001087 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001088 put_packet(s, "OK");
1089 break;
bellard858693c2004-03-31 18:52:07 +00001090 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001091 case 'z':
1092 type = strtoul(p, (char **)&p, 16);
1093 if (*p == ',')
1094 p++;
bellard9d9754a2006-06-25 15:32:37 +00001095 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001096 if (*p == ',')
1097 p++;
bellard9d9754a2006-06-25 15:32:37 +00001098 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001099 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001100 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001101 else
aliguori880a7572008-11-18 20:30:24 +00001102 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001103 if (res >= 0)
1104 put_packet(s, "OK");
1105 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001106 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001107 else
1108 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001109 break;
aliguori880a7572008-11-18 20:30:24 +00001110 case 'H':
1111 type = *p++;
1112 thread = strtoull(p, (char **)&p, 16);
1113 if (thread == -1 || thread == 0) {
1114 put_packet(s, "OK");
1115 break;
1116 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001117 cpu = find_cpu(thread);
1118 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001119 put_packet(s, "E22");
1120 break;
1121 }
1122 switch (type) {
1123 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001124 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001125 put_packet(s, "OK");
1126 break;
1127 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001128 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001129 put_packet(s, "OK");
1130 break;
1131 default:
1132 put_packet(s, "E22");
1133 break;
1134 }
1135 break;
1136 case 'T':
1137 thread = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001138 cpu = find_cpu(thread);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001139
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001140 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001141 put_packet(s, "OK");
1142 } else {
aliguori880a7572008-11-18 20:30:24 +00001143 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001144 }
aliguori880a7572008-11-18 20:30:24 +00001145 break;
pbrook978efd62006-06-17 18:30:42 +00001146 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001147 case 'Q':
1148 /* parse any 'q' packets here */
1149 if (!strcmp(p,"qemu.sstepbits")) {
1150 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001151 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1152 SSTEP_ENABLE,
1153 SSTEP_NOIRQ,
1154 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001155 put_packet(s, buf);
1156 break;
1157 } else if (strncmp(p,"qemu.sstep",10) == 0) {
1158 /* Display or change the sstep_flags */
1159 p += 10;
1160 if (*p != '=') {
1161 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001162 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001163 put_packet(s, buf);
1164 break;
1165 }
1166 p++;
1167 type = strtoul(p, (char **)&p, 16);
1168 sstep_flags = type;
1169 put_packet(s, "OK");
1170 break;
aliguori880a7572008-11-18 20:30:24 +00001171 } else if (strcmp(p,"C") == 0) {
1172 /* "Current thread" remains vague in the spec, so always return
1173 * the first CPU (gdb returns the first thread). */
1174 put_packet(s, "QC1");
1175 break;
1176 } else if (strcmp(p,"fThreadInfo") == 0) {
Andreas Färber52f34622013-06-27 13:44:40 +02001177 s->query_cpu = first_cpu;
aliguori880a7572008-11-18 20:30:24 +00001178 goto report_cpuinfo;
1179 } else if (strcmp(p,"sThreadInfo") == 0) {
1180 report_cpuinfo:
1181 if (s->query_cpu) {
Andreas Färber52f34622013-06-27 13:44:40 +02001182 snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu));
aliguori880a7572008-11-18 20:30:24 +00001183 put_packet(s, buf);
Andreas Färber52f34622013-06-27 13:44:40 +02001184 s->query_cpu = s->query_cpu->next_cpu;
aliguori880a7572008-11-18 20:30:24 +00001185 } else
1186 put_packet(s, "l");
1187 break;
1188 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1189 thread = strtoull(p+16, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001190 cpu = find_cpu(thread);
1191 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001192 cpu_synchronize_state(cpu);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001193 len = snprintf((char *)mem_buf, sizeof(mem_buf),
Andreas Färber55e5c282012-12-17 06:18:02 +01001194 "CPU#%d [%s]", cpu->cpu_index,
Andreas Färber259186a2013-01-17 18:51:17 +01001195 cpu->halted ? "halted " : "running");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001196 memtohex(buf, mem_buf, len);
1197 put_packet(s, buf);
1198 }
aliguori880a7572008-11-18 20:30:24 +00001199 break;
edgar_igl60897d32008-05-09 08:25:14 +00001200 }
blueswir10b8a9882009-03-07 10:51:36 +00001201#ifdef CONFIG_USER_ONLY
edgar_igl60897d32008-05-09 08:25:14 +00001202 else if (strncmp(p, "Offsets", 7) == 0) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001203 CPUArchState *env = s->c_cpu->env_ptr;
1204 TaskState *ts = env->opaque;
pbrook978efd62006-06-17 18:30:42 +00001205
blueswir1363a37d2008-08-21 17:58:08 +00001206 snprintf(buf, sizeof(buf),
1207 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1208 ";Bss=" TARGET_ABI_FMT_lx,
1209 ts->info->code_offset,
1210 ts->info->data_offset,
1211 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001212 put_packet(s, buf);
1213 break;
1214 }
blueswir10b8a9882009-03-07 10:51:36 +00001215#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001216 else if (strncmp(p, "Rcmd,", 5) == 0) {
1217 int len = strlen(p + 5);
1218
1219 if ((len % 2) != 0) {
1220 put_packet(s, "E01");
1221 break;
1222 }
1223 hextomem(mem_buf, p + 5, len);
1224 len = len / 2;
1225 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001226 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001227 put_packet(s, "OK");
1228 break;
1229 }
blueswir10b8a9882009-03-07 10:51:36 +00001230#endif /* !CONFIG_USER_ONLY */
pbrook56aebc82008-10-11 17:55:29 +00001231 if (strncmp(p, "Supported", 9) == 0) {
blueswir15b3715b2008-10-25 11:18:12 +00001232 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
pbrook56aebc82008-10-11 17:55:29 +00001233#ifdef GDB_CORE_XML
blueswir12dc766d2009-04-13 16:06:19 +00001234 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
pbrook56aebc82008-10-11 17:55:29 +00001235#endif
1236 put_packet(s, buf);
1237 break;
1238 }
1239#ifdef GDB_CORE_XML
1240 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1241 const char *xml;
1242 target_ulong total_len;
1243
1244 gdb_has_xml = 1;
1245 p += 19;
aliguori880a7572008-11-18 20:30:24 +00001246 xml = get_feature_xml(p, &p);
pbrook56aebc82008-10-11 17:55:29 +00001247 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001248 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001249 put_packet(s, buf);
1250 break;
1251 }
1252
1253 if (*p == ':')
1254 p++;
1255 addr = strtoul(p, (char **)&p, 16);
1256 if (*p == ',')
1257 p++;
1258 len = strtoul(p, (char **)&p, 16);
1259
1260 total_len = strlen(xml);
1261 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001262 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001263 put_packet(s, buf);
1264 break;
1265 }
1266 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1267 len = (MAX_PACKET_LENGTH - 5) / 2;
1268 if (len < total_len - addr) {
1269 buf[0] = 'm';
1270 len = memtox(buf + 1, xml + addr, len);
1271 } else {
1272 buf[0] = 'l';
1273 len = memtox(buf + 1, xml + addr, total_len - addr);
1274 }
1275 put_packet_binary(s, buf, len + 1);
1276 break;
1277 }
1278#endif
1279 /* Unrecognised 'q' command. */
1280 goto unknown_command;
1281
bellard858693c2004-03-31 18:52:07 +00001282 default:
pbrook56aebc82008-10-11 17:55:29 +00001283 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001284 /* put empty packet */
1285 buf[0] = '\0';
1286 put_packet(s, buf);
1287 break;
1288 }
1289 return RS_IDLE;
1290}
1291
Andreas Färber64f6b342013-05-27 02:06:09 +02001292void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001293{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001294 gdbserver_state->c_cpu = cpu;
1295 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001296}
1297
bellard1fddef42005-04-17 19:16:13 +00001298#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001299static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001300{
aliguori880a7572008-11-18 20:30:24 +00001301 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001302 CPUArchState *env = s->c_cpu->env_ptr;
1303 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001304 char buf[256];
aliguorid6fc1b32008-11-18 19:55:44 +00001305 const char *type;
bellard858693c2004-03-31 18:52:07 +00001306 int ret;
1307
Meador Ingecdb432b2012-03-15 17:49:45 +00001308 if (running || s->state == RS_INACTIVE) {
1309 return;
1310 }
1311 /* Is there a GDB syscall waiting to be sent? */
1312 if (s->current_syscall_cb) {
1313 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001314 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001315 }
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001316 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001317 case RUN_STATE_DEBUG:
aliguori880a7572008-11-18 20:30:24 +00001318 if (env->watchpoint_hit) {
1319 switch (env->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001320 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001321 type = "r";
1322 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001323 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001324 type = "a";
1325 break;
1326 default:
1327 type = "";
1328 break;
1329 }
aliguori880a7572008-11-18 20:30:24 +00001330 snprintf(buf, sizeof(buf),
1331 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
Andreas Färber0d342822012-12-17 07:12:13 +01001332 GDB_SIGNAL_TRAP, cpu_index(cpu), type,
aliguori880a7572008-11-18 20:30:24 +00001333 env->watchpoint_hit->vaddr);
aliguori880a7572008-11-18 20:30:24 +00001334 env->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001335 goto send_packet;
pbrook6658ffb2007-03-16 23:58:11 +00001336 }
Jan Kiszka425189a2011-03-22 11:02:09 +01001337 tb_flush(env);
aurel32ca587a82008-12-18 22:44:13 +00001338 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001339 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001340 case RUN_STATE_PAUSED:
aliguori9781e042009-01-22 17:15:29 +00001341 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001342 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001343 case RUN_STATE_SHUTDOWN:
Jan Kiszka425189a2011-03-22 11:02:09 +01001344 ret = GDB_SIGNAL_QUIT;
1345 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001346 case RUN_STATE_IO_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001347 ret = GDB_SIGNAL_IO;
1348 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001349 case RUN_STATE_WATCHDOG:
Jan Kiszka425189a2011-03-22 11:02:09 +01001350 ret = GDB_SIGNAL_ALRM;
1351 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001352 case RUN_STATE_INTERNAL_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001353 ret = GDB_SIGNAL_ABRT;
1354 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001355 case RUN_STATE_SAVE_VM:
1356 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001357 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001358 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001359 ret = GDB_SIGNAL_XCPU;
1360 break;
1361 default:
1362 ret = GDB_SIGNAL_UNKNOWN;
1363 break;
bellardbbeb7b52006-04-23 18:42:15 +00001364 }
Andreas Färber0d342822012-12-17 07:12:13 +01001365 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(cpu));
Jan Kiszka425189a2011-03-22 11:02:09 +01001366
1367send_packet:
bellard858693c2004-03-31 18:52:07 +00001368 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001369
1370 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001371 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001372}
bellard1fddef42005-04-17 19:16:13 +00001373#endif
bellard858693c2004-03-31 18:52:07 +00001374
pbrooka2d1eba2007-01-28 03:10:55 +00001375/* Send a gdb syscall request.
1376 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001377 %x - target_ulong argument printed in hex.
1378 %lx - 64-bit argument printed in hex.
1379 %s - string pointer (target_ulong) and length (int) pair. */
blueswir17ccfb2e2008-09-14 06:45:34 +00001380void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
pbrooka2d1eba2007-01-28 03:10:55 +00001381{
1382 va_list va;
pbrooka2d1eba2007-01-28 03:10:55 +00001383 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001384 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001385 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001386 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001387 GDBState *s;
1388
aliguori880a7572008-11-18 20:30:24 +00001389 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001390 if (!s)
1391 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001392 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001393#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001394 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001395#endif
pbrooka2d1eba2007-01-28 03:10:55 +00001396 va_start(va, fmt);
Meador Ingecdb432b2012-03-15 17:49:45 +00001397 p = s->syscall_buf;
1398 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001399 *(p++) = 'F';
1400 while (*fmt) {
1401 if (*fmt == '%') {
1402 fmt++;
1403 switch (*fmt++) {
1404 case 'x':
1405 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001406 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001407 break;
pbrooka87295e2007-05-26 15:09:38 +00001408 case 'l':
1409 if (*(fmt++) != 'x')
1410 goto bad_format;
1411 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001412 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001413 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001414 case 's':
1415 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001416 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001417 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001418 break;
1419 default:
pbrooka87295e2007-05-26 15:09:38 +00001420 bad_format:
pbrooka2d1eba2007-01-28 03:10:55 +00001421 fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
1422 fmt - 1);
1423 break;
1424 }
1425 } else {
1426 *(p++) = *(fmt++);
1427 }
1428 }
pbrook8a93e022007-08-06 13:19:15 +00001429 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001430 va_end(va);
pbrooka2d1eba2007-01-28 03:10:55 +00001431#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001432 put_packet(s, s->syscall_buf);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001433 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001434#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001435 /* In this case wait to send the syscall packet until notification that
1436 the CPU has stopped. This must be done because if the packet is sent
1437 now the reply from the syscall request could be received while the CPU
1438 is still in the running state, which can cause packets to be dropped
1439 and state transition 'T' packets to be sent while the syscall is still
1440 being processed. */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001441 cpu_exit(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00001442#endif
1443}
1444
bellard6a00d602005-11-21 23:25:50 +00001445static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00001446{
1447 int i, csum;
ths60fe76f2007-12-16 03:02:09 +00001448 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00001449
bellard1fddef42005-04-17 19:16:13 +00001450#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001451 if (s->last_packet_len) {
1452 /* Waiting for a response to the last packet. If we see the start
1453 of a new command then abandon the previous response. */
1454 if (ch == '-') {
1455#ifdef DEBUG_GDB
1456 printf("Got NACK, retransmitting\n");
1457#endif
thsffe8ab82007-12-16 03:16:05 +00001458 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
pbrook4046d912007-01-28 01:53:16 +00001459 }
1460#ifdef DEBUG_GDB
1461 else if (ch == '+')
1462 printf("Got ACK\n");
1463 else
1464 printf("Got '%c' when expecting ACK/NACK\n", ch);
1465#endif
1466 if (ch == '+' || ch == '$')
1467 s->last_packet_len = 0;
1468 if (ch != '$')
1469 return;
1470 }
Luiz Capitulino13548692011-07-29 15:36:43 -03001471 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00001472 /* when the CPU is running, we cannot do anything except stop
1473 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001474 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00001475 } else
bellard1fddef42005-04-17 19:16:13 +00001476#endif
bellard41625032005-04-24 10:07:11 +00001477 {
bellard858693c2004-03-31 18:52:07 +00001478 switch(s->state) {
1479 case RS_IDLE:
1480 if (ch == '$') {
1481 s->line_buf_index = 0;
1482 s->state = RS_GETLINE;
bellard4c3a88a2003-07-26 12:06:08 +00001483 }
1484 break;
bellard858693c2004-03-31 18:52:07 +00001485 case RS_GETLINE:
1486 if (ch == '#') {
1487 s->state = RS_CHKSUM1;
1488 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1489 s->state = RS_IDLE;
1490 } else {
1491 s->line_buf[s->line_buf_index++] = ch;
1492 }
1493 break;
1494 case RS_CHKSUM1:
1495 s->line_buf[s->line_buf_index] = '\0';
1496 s->line_csum = fromhex(ch) << 4;
1497 s->state = RS_CHKSUM2;
1498 break;
1499 case RS_CHKSUM2:
1500 s->line_csum |= fromhex(ch);
1501 csum = 0;
1502 for(i = 0; i < s->line_buf_index; i++) {
1503 csum += s->line_buf[i];
1504 }
1505 if (s->line_csum != (csum & 0xff)) {
ths60fe76f2007-12-16 03:02:09 +00001506 reply = '-';
1507 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00001508 s->state = RS_IDLE;
1509 } else {
ths60fe76f2007-12-16 03:02:09 +00001510 reply = '+';
1511 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00001512 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00001513 }
bellardb4608c02003-06-27 17:34:32 +00001514 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001515 default:
1516 abort();
bellardb4608c02003-06-27 17:34:32 +00001517 }
1518 }
bellard858693c2004-03-31 18:52:07 +00001519}
1520
Paul Brook0e1c9c52010-06-16 13:03:51 +01001521/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001522void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01001523{
1524 GDBState *s;
1525 char buf[4];
1526
1527 s = gdbserver_state;
1528 if (!s) {
1529 return;
1530 }
1531#ifdef CONFIG_USER_ONLY
1532 if (gdbserver_fd < 0 || s->fd < 0) {
1533 return;
1534 }
1535#endif
1536
1537 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
1538 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001539
1540#ifndef CONFIG_USER_ONLY
1541 if (s->chr) {
Anthony Liguori70f24fb2011-08-15 11:17:38 -05001542 qemu_chr_delete(s->chr);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001543 }
1544#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01001545}
1546
bellard1fddef42005-04-17 19:16:13 +00001547#ifdef CONFIG_USER_ONLY
1548int
aurel32ca587a82008-12-18 22:44:13 +00001549gdb_queuesig (void)
1550{
1551 GDBState *s;
1552
1553 s = gdbserver_state;
1554
1555 if (gdbserver_fd < 0 || s->fd < 0)
1556 return 0;
1557 else
1558 return 1;
1559}
1560
1561int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001562gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00001563{
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001564 CPUArchState *env = cpu->env_ptr;
Andreas Färber5ca666c2013-06-24 19:20:57 +02001565 GDBState *s;
1566 char buf[256];
1567 int n;
bellard1fddef42005-04-17 19:16:13 +00001568
Andreas Färber5ca666c2013-06-24 19:20:57 +02001569 s = gdbserver_state;
1570 if (gdbserver_fd < 0 || s->fd < 0) {
1571 return sig;
bellard1fddef42005-04-17 19:16:13 +00001572 }
1573
Andreas Färber5ca666c2013-06-24 19:20:57 +02001574 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001575 cpu_single_step(cpu, 0);
Andreas Färber5ca666c2013-06-24 19:20:57 +02001576 tb_flush(env);
bellard1fddef42005-04-17 19:16:13 +00001577
Andreas Färber5ca666c2013-06-24 19:20:57 +02001578 if (sig != 0) {
1579 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
1580 put_packet(s, buf);
1581 }
1582 /* put_packet() might have detected that the peer terminated the
1583 connection. */
1584 if (s->fd < 0) {
1585 return sig;
1586 }
1587
1588 sig = 0;
1589 s->state = RS_IDLE;
1590 s->running_state = 0;
1591 while (s->running_state == 0) {
1592 n = read(s->fd, buf, 256);
1593 if (n > 0) {
1594 int i;
1595
1596 for (i = 0; i < n; i++) {
1597 gdb_read_byte(s, buf[i]);
1598 }
1599 } else if (n == 0 || errno != EAGAIN) {
1600 /* XXX: Connection closed. Should probably wait for another
1601 connection before continuing. */
1602 return sig;
bellard1fddef42005-04-17 19:16:13 +00001603 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02001604 }
1605 sig = s->signal;
1606 s->signal = 0;
1607 return sig;
bellard1fddef42005-04-17 19:16:13 +00001608}
bellarde9009672005-04-26 20:42:36 +00001609
aurel32ca587a82008-12-18 22:44:13 +00001610/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001611void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00001612{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001613 GDBState *s;
1614 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00001615
Andreas Färber5ca666c2013-06-24 19:20:57 +02001616 s = gdbserver_state;
1617 if (gdbserver_fd < 0 || s->fd < 0) {
1618 return;
1619 }
aurel32ca587a82008-12-18 22:44:13 +00001620
Andreas Färber5ca666c2013-06-24 19:20:57 +02001621 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
1622 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00001623}
bellard1fddef42005-04-17 19:16:13 +00001624
aliguori880a7572008-11-18 20:30:24 +00001625static void gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00001626{
1627 GDBState *s;
1628 struct sockaddr_in sockaddr;
1629 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001630 int fd;
bellard858693c2004-03-31 18:52:07 +00001631
1632 for(;;) {
1633 len = sizeof(sockaddr);
1634 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1635 if (fd < 0 && errno != EINTR) {
1636 perror("accept");
1637 return;
1638 } else if (fd >= 0) {
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001639#ifndef _WIN32
1640 fcntl(fd, F_SETFD, FD_CLOEXEC);
1641#endif
bellard858693c2004-03-31 18:52:07 +00001642 break;
1643 }
1644 }
1645
1646 /* set short latency */
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001647 socket_set_nodelay(fd);
ths3b46e622007-09-17 08:09:54 +00001648
Anthony Liguori7267c092011-08-20 22:09:37 -05001649 s = g_malloc0(sizeof(GDBState));
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001650 s->c_cpu = first_cpu;
1651 s->g_cpu = first_cpu;
bellard858693c2004-03-31 18:52:07 +00001652 s->fd = fd;
pbrook56aebc82008-10-11 17:55:29 +00001653 gdb_has_xml = 0;
bellard858693c2004-03-31 18:52:07 +00001654
aliguori880a7572008-11-18 20:30:24 +00001655 gdbserver_state = s;
pbrooka2d1eba2007-01-28 03:10:55 +00001656
bellard858693c2004-03-31 18:52:07 +00001657 fcntl(fd, F_SETFL, O_NONBLOCK);
bellard858693c2004-03-31 18:52:07 +00001658}
1659
1660static int gdbserver_open(int port)
1661{
1662 struct sockaddr_in sockaddr;
1663 int fd, val, ret;
1664
1665 fd = socket(PF_INET, SOCK_STREAM, 0);
1666 if (fd < 0) {
1667 perror("socket");
1668 return -1;
1669 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001670#ifndef _WIN32
1671 fcntl(fd, F_SETFD, FD_CLOEXEC);
1672#endif
bellard858693c2004-03-31 18:52:07 +00001673
1674 /* allow fast reuse */
1675 val = 1;
Stefan Weil9957fc72013-03-08 19:58:32 +01001676 qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
bellard858693c2004-03-31 18:52:07 +00001677
1678 sockaddr.sin_family = AF_INET;
1679 sockaddr.sin_port = htons(port);
1680 sockaddr.sin_addr.s_addr = 0;
1681 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1682 if (ret < 0) {
1683 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00001684 close(fd);
bellard858693c2004-03-31 18:52:07 +00001685 return -1;
1686 }
1687 ret = listen(fd, 0);
1688 if (ret < 0) {
1689 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00001690 close(fd);
bellard858693c2004-03-31 18:52:07 +00001691 return -1;
1692 }
bellard858693c2004-03-31 18:52:07 +00001693 return fd;
1694}
1695
1696int gdbserver_start(int port)
1697{
1698 gdbserver_fd = gdbserver_open(port);
1699 if (gdbserver_fd < 0)
1700 return -1;
1701 /* accept connections */
aliguori880a7572008-11-18 20:30:24 +00001702 gdb_accept();
bellardb4608c02003-06-27 17:34:32 +00001703 return 0;
1704}
aurel322b1319c2008-12-18 22:44:04 +00001705
1706/* Disable gdb stub for child processes. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001707void gdbserver_fork(CPUArchState *env)
aurel322b1319c2008-12-18 22:44:04 +00001708{
1709 GDBState *s = gdbserver_state;
edgar_igl9f6164d2009-01-07 10:22:28 +00001710 if (gdbserver_fd < 0 || s->fd < 0)
aurel322b1319c2008-12-18 22:44:04 +00001711 return;
1712 close(s->fd);
1713 s->fd = -1;
1714 cpu_breakpoint_remove_all(env, BP_GDB);
1715 cpu_watchpoint_remove_all(env, BP_GDB);
1716}
pbrook4046d912007-01-28 01:53:16 +00001717#else
thsaa1f17c2007-07-11 22:48:58 +00001718static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00001719{
pbrook56aebc82008-10-11 17:55:29 +00001720 /* We can handle an arbitrarily large amount of data.
1721 Pick the maximum packet size, which is as good as anything. */
1722 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00001723}
1724
thsaa1f17c2007-07-11 22:48:58 +00001725static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00001726{
pbrook4046d912007-01-28 01:53:16 +00001727 int i;
1728
1729 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00001730 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00001731 }
1732}
1733
1734static void gdb_chr_event(void *opaque, int event)
1735{
1736 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05301737 case CHR_EVENT_OPENED:
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001738 vm_stop(RUN_STATE_PAUSED);
pbrook56aebc82008-10-11 17:55:29 +00001739 gdb_has_xml = 0;
pbrook4046d912007-01-28 01:53:16 +00001740 break;
1741 default:
1742 break;
1743 }
1744}
1745
aliguori8a34a0f2009-03-05 23:01:55 +00001746static void gdb_monitor_output(GDBState *s, const char *msg, int len)
1747{
1748 char buf[MAX_PACKET_LENGTH];
1749
1750 buf[0] = 'O';
1751 if (len > (MAX_PACKET_LENGTH/2) - 1)
1752 len = (MAX_PACKET_LENGTH/2) - 1;
1753 memtohex(buf + 1, (uint8_t *)msg, len);
1754 put_packet(s, buf);
1755}
1756
1757static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
1758{
1759 const char *p = (const char *)buf;
1760 int max_sz;
1761
1762 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
1763 for (;;) {
1764 if (len <= max_sz) {
1765 gdb_monitor_output(gdbserver_state, p, len);
1766 break;
1767 }
1768 gdb_monitor_output(gdbserver_state, p, max_sz);
1769 p += max_sz;
1770 len -= max_sz;
1771 }
1772 return len;
1773}
1774
aliguori59030a82009-04-05 18:43:41 +00001775#ifndef _WIN32
1776static void gdb_sigterm_handler(int signal)
1777{
Luiz Capitulino13548692011-07-29 15:36:43 -03001778 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001779 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001780 }
aliguori59030a82009-04-05 18:43:41 +00001781}
1782#endif
1783
1784int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00001785{
1786 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00001787 char gdbstub_device_name[128];
aliguori36556b22009-03-28 18:05:53 +00001788 CharDriverState *chr = NULL;
1789 CharDriverState *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00001790
aliguori59030a82009-04-05 18:43:41 +00001791 if (!device)
1792 return -1;
1793 if (strcmp(device, "none") != 0) {
1794 if (strstart(device, "tcp:", NULL)) {
1795 /* enforce required TCP attributes */
1796 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
1797 "%s,nowait,nodelay,server", device);
1798 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00001799 }
aliguori59030a82009-04-05 18:43:41 +00001800#ifndef _WIN32
1801 else if (strcmp(device, "stdio") == 0) {
1802 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00001803
aliguori59030a82009-04-05 18:43:41 +00001804 memset(&act, 0, sizeof(act));
1805 act.sa_handler = gdb_sigterm_handler;
1806 sigaction(SIGINT, &act, NULL);
1807 }
1808#endif
Anthony Liguori27143a42011-08-15 11:17:36 -05001809 chr = qemu_chr_new("gdb", device, NULL);
aliguori36556b22009-03-28 18:05:53 +00001810 if (!chr)
1811 return -1;
1812
Hans de Goede456d6062013-03-27 20:29:40 +01001813 qemu_chr_fe_claim_no_fail(chr);
aliguori36556b22009-03-28 18:05:53 +00001814 qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
1815 gdb_chr_event, NULL);
pbrookcfc34752007-02-22 01:48:01 +00001816 }
1817
aliguori36556b22009-03-28 18:05:53 +00001818 s = gdbserver_state;
1819 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001820 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00001821 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00001822
aliguori36556b22009-03-28 18:05:53 +00001823 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
1824
1825 /* Initialize a monitor terminal for gdb */
Anthony Liguori7267c092011-08-20 22:09:37 -05001826 mon_chr = g_malloc0(sizeof(*mon_chr));
aliguori36556b22009-03-28 18:05:53 +00001827 mon_chr->chr_write = gdb_monitor_write;
1828 monitor_init(mon_chr, 0);
1829 } else {
1830 if (s->chr)
Anthony Liguori70f24fb2011-08-15 11:17:38 -05001831 qemu_chr_delete(s->chr);
aliguori36556b22009-03-28 18:05:53 +00001832 mon_chr = s->mon_chr;
1833 memset(s, 0, sizeof(GDBState));
1834 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001835 s->c_cpu = first_cpu;
1836 s->g_cpu = first_cpu;
pbrook4046d912007-01-28 01:53:16 +00001837 s->chr = chr;
aliguori36556b22009-03-28 18:05:53 +00001838 s->state = chr ? RS_IDLE : RS_INACTIVE;
1839 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00001840 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00001841
pbrook4046d912007-01-28 01:53:16 +00001842 return 0;
1843}
1844#endif