blob: eb506309d43fa067d0af4d6b6e916452d5966624 [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
edgar_iglfde3fd62008-05-09 08:50:01 +0000492#if defined(TARGET_I386)
balrog5ad265e2007-10-31 00:21:35 +0000493
Andreas Färberf20f9df2013-07-07 12:07:54 +0200494#include "target-i386/gdbstub.c"
bellard6da41ea2004-01-04 15:48:38 +0000495
bellard9e62fd72004-01-05 22:49:06 +0000496#elif defined (TARGET_PPC)
pbrook56aebc82008-10-11 17:55:29 +0000497
aurel32e571cb42009-01-24 15:07:42 +0000498#if defined (TARGET_PPC64)
499#define GDB_CORE_XML "power64-core.xml"
500#else
501#define GDB_CORE_XML "power-core.xml"
502#endif
pbrook56aebc82008-10-11 17:55:29 +0000503
Andreas Färber0980bfa2013-07-07 12:26:33 +0200504#include "target-ppc/gdbstub.c"
pbrook56aebc82008-10-11 17:55:29 +0000505
bellarde95c8d52004-09-30 22:22:08 +0000506#elif defined (TARGET_SPARC)
bellarde95c8d52004-09-30 22:22:08 +0000507
Andreas Färberd19c87f2013-07-07 12:29:26 +0200508#include "target-sparc/gdbstub.c"
pbrook56aebc82008-10-11 17:55:29 +0000509
bellard1fddef42005-04-17 19:16:13 +0000510#elif defined (TARGET_ARM)
pbrook56aebc82008-10-11 17:55:29 +0000511
pbrook56aebc82008-10-11 17:55:29 +0000512#define GDB_CORE_XML "arm-core.xml"
513
Andreas Färber58850da2013-07-07 12:32:15 +0200514#include "target-arm/gdbstub.c"
pbrook56aebc82008-10-11 17:55:29 +0000515
pbrooke6e59062006-10-22 00:18:54 +0000516#elif defined (TARGET_M68K)
pbrook56aebc82008-10-11 17:55:29 +0000517
pbrook56aebc82008-10-11 17:55:29 +0000518#define GDB_CORE_XML "cf-core.xml"
519
Andreas Färberc88de142013-07-07 12:33:56 +0200520#include "target-m68k/gdbstub.c"
pbrooke6e59062006-10-22 00:18:54 +0000521
bellard6f970bd2005-12-05 19:55:19 +0000522#elif defined (TARGET_MIPS)
pbrook56aebc82008-10-11 17:55:29 +0000523
Andreas Färber814ac262013-07-07 12:38:42 +0200524#include "target-mips/gdbstub.c"
ths36d23952007-02-28 22:37:42 +0000525
Jia Liufc043552012-07-20 15:50:50 +0800526#elif defined(TARGET_OPENRISC)
527
Andreas Färber30028732013-07-07 12:40:38 +0200528#include "target-openrisc/gdbstub.c"
Jia Liufc043552012-07-20 15:50:50 +0800529
bellardfdf9b3e2006-04-27 21:07:38 +0000530#elif defined (TARGET_SH4)
ths6ef99fc2007-05-13 16:36:24 +0000531
Andreas Färber2f937732013-07-07 12:42:52 +0200532#include "target-sh4/gdbstub.c"
ths6ef99fc2007-05-13 16:36:24 +0000533
Edgar E. Iglesiasd74d6a92009-05-20 20:16:31 +0200534#elif defined (TARGET_MICROBLAZE)
535
Andreas Färbereabfc232013-07-07 12:45:47 +0200536#include "target-microblaze/gdbstub.c"
Edgar E. Iglesiasd74d6a92009-05-20 20:16:31 +0200537
thsf1ccf902007-10-08 13:16:14 +0000538#elif defined (TARGET_CRIS)
539
Andreas Färber213c19d2013-07-07 12:50:22 +0200540#include "target-cris/gdbstub.c"
Edgar E. Iglesias4a0b59f2010-02-20 19:51:56 +0100541
aurel3219bf5172008-12-07 23:26:32 +0000542#elif defined (TARGET_ALPHA)
543
Andreas Färberc3ce8eb2013-07-07 12:52:32 +0200544#include "target-alpha/gdbstub.c"
aurel3219bf5172008-12-07 23:26:32 +0000545
Alexander Grafafcb0e42009-12-05 12:44:29 +0100546#elif defined (TARGET_S390X)
547
Andreas Färbercfae5c92013-07-07 12:54:12 +0200548#include "target-s390x/gdbstub.c"
Richard Henderson6ee77b12012-08-23 10:44:45 -0700549
Michael Walle0c45d3d2011-02-17 23:45:06 +0100550#elif defined (TARGET_LM32)
551
Andreas Färberd0ff8d02013-07-07 12:55:44 +0200552#include "target-lm32/gdbstub.c"
Michael Walle0c45d3d2011-02-17 23:45:06 +0100553
Max Filippovccfcaba2011-09-06 03:55:52 +0400554#elif defined(TARGET_XTENSA)
555
Andreas Färber25d8ac02013-07-07 12:57:38 +0200556#include "target-xtensa/gdbstub.c"
Max Filippovccfcaba2011-09-06 03:55:52 +0400557
bellard1fddef42005-04-17 19:16:13 +0000558#else
pbrook56aebc82008-10-11 17:55:29 +0000559
Andreas Färber9349b4f2012-03-14 01:38:32 +0100560static int cpu_gdb_read_register(CPUArchState *env, uint8_t *mem_buf, int n)
bellard6da41ea2004-01-04 15:48:38 +0000561{
562 return 0;
563}
564
Andreas Färber9349b4f2012-03-14 01:38:32 +0100565static int cpu_gdb_write_register(CPUArchState *env, uint8_t *mem_buf, int n)
bellard6da41ea2004-01-04 15:48:38 +0000566{
pbrook56aebc82008-10-11 17:55:29 +0000567 return 0;
bellard6da41ea2004-01-04 15:48:38 +0000568}
569
570#endif
bellardb4608c02003-06-27 17:34:32 +0000571
pbrook56aebc82008-10-11 17:55:29 +0000572#ifdef GDB_CORE_XML
573/* Encode data using the encoding for 'x' packets. */
574static int memtox(char *buf, const char *mem, int len)
575{
576 char *p = buf;
577 char c;
578
579 while (len--) {
580 c = *(mem++);
581 switch (c) {
582 case '#': case '$': case '*': case '}':
583 *(p++) = '}';
584 *(p++) = c ^ 0x20;
585 break;
586 default:
587 *(p++) = c;
588 break;
589 }
590 }
591 return p - buf;
592}
593
aurel323faf7782008-12-07 23:26:17 +0000594static const char *get_feature_xml(const char *p, const char **newp)
pbrook56aebc82008-10-11 17:55:29 +0000595{
pbrook56aebc82008-10-11 17:55:29 +0000596 size_t len;
597 int i;
598 const char *name;
599 static char target_xml[1024];
600
601 len = 0;
602 while (p[len] && p[len] != ':')
603 len++;
604 *newp = p + len;
605
606 name = NULL;
607 if (strncmp(p, "target.xml", len) == 0) {
608 /* Generate the XML description for this CPU. */
609 if (!target_xml[0]) {
610 GDBRegisterState *r;
Andreas Färbereac8b352013-06-28 21:11:37 +0200611 CPUState *cpu = first_cpu;
pbrook56aebc82008-10-11 17:55:29 +0000612
blueswir15b3715b2008-10-25 11:18:12 +0000613 snprintf(target_xml, sizeof(target_xml),
614 "<?xml version=\"1.0\"?>"
615 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
616 "<target>"
617 "<xi:include href=\"%s\"/>",
618 GDB_CORE_XML);
pbrook56aebc82008-10-11 17:55:29 +0000619
Andreas Färbereac8b352013-06-28 21:11:37 +0200620 for (r = cpu->gdb_regs; r; r = r->next) {
blueswir12dc766d2009-04-13 16:06:19 +0000621 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
622 pstrcat(target_xml, sizeof(target_xml), r->xml);
623 pstrcat(target_xml, sizeof(target_xml), "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000624 }
blueswir12dc766d2009-04-13 16:06:19 +0000625 pstrcat(target_xml, sizeof(target_xml), "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000626 }
627 return target_xml;
628 }
629 for (i = 0; ; i++) {
630 name = xml_builtin[i][0];
631 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
632 break;
633 }
634 return name ? xml_builtin[i][1] : NULL;
635}
636#endif
637
Andreas Färber385b9f02013-06-27 18:25:36 +0200638static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000639{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200640 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200641 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000642 GDBRegisterState *r;
643
Andreas Färbera0e372f2013-06-28 23:18:47 +0200644 if (reg < cc->gdb_num_core_regs) {
pbrook56aebc82008-10-11 17:55:29 +0000645 return cpu_gdb_read_register(env, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200646 }
pbrook56aebc82008-10-11 17:55:29 +0000647
Andreas Färbereac8b352013-06-28 21:11:37 +0200648 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000649 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
650 return r->get_reg(env, mem_buf, reg - r->base_reg);
651 }
652 }
653 return 0;
654}
655
Andreas Färber385b9f02013-06-27 18:25:36 +0200656static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000657{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200658 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200659 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000660 GDBRegisterState *r;
661
Andreas Färbera0e372f2013-06-28 23:18:47 +0200662 if (reg < cc->gdb_num_core_regs) {
pbrook56aebc82008-10-11 17:55:29 +0000663 return cpu_gdb_write_register(env, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200664 }
pbrook56aebc82008-10-11 17:55:29 +0000665
Andreas Färbereac8b352013-06-28 21:11:37 +0200666 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000667 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
668 return r->set_reg(env, mem_buf, reg - r->base_reg);
669 }
670 }
671 return 0;
672}
673
674/* Register a supplemental set of CPU registers. If g_pos is nonzero it
675 specifies the first register number and these registers are included in
676 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
677 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
678 */
679
Andreas Färber22169d42013-06-28 21:27:39 +0200680void gdb_register_coprocessor(CPUState *cpu,
681 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
682 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000683{
684 GDBRegisterState *s;
685 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000686
Andreas Färbereac8b352013-06-28 21:11:37 +0200687 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000688 while (*p) {
689 /* Check for duplicates. */
690 if (strcmp((*p)->xml, xml) == 0)
691 return;
692 p = &(*p)->next;
693 }
Stefan Weil9643c252011-10-18 22:25:38 +0200694
695 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200696 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200697 s->num_regs = num_regs;
698 s->get_reg = get_reg;
699 s->set_reg = set_reg;
700 s->xml = xml;
701
pbrook56aebc82008-10-11 17:55:29 +0000702 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200703 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000704 *p = s;
705 if (g_pos) {
706 if (g_pos != s->base_reg) {
707 fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
708 "Expected %d got %d\n", xml, g_pos, s->base_reg);
pbrook56aebc82008-10-11 17:55:29 +0000709 }
710 }
711}
712
aliguoria1d1bb32008-11-18 20:07:32 +0000713#ifndef CONFIG_USER_ONLY
714static const int xlat_gdb_type[] = {
715 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
716 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
717 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
718};
719#endif
720
aliguori880a7572008-11-18 20:30:24 +0000721static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000722{
Andreas Färber182735e2013-05-29 22:29:20 +0200723 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100724 CPUArchState *env;
aliguori880a7572008-11-18 20:30:24 +0000725 int err = 0;
726
Andreas Färber62278812013-06-27 17:12:06 +0200727 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200728 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200729 }
aliguorie22a25c2009-03-12 20:12:48 +0000730
aliguoria1d1bb32008-11-18 20:07:32 +0000731 switch (type) {
732 case GDB_BREAKPOINT_SW:
733 case GDB_BREAKPOINT_HW:
Andreas Färber182735e2013-05-29 22:29:20 +0200734 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
735 env = cpu->env_ptr;
aliguori880a7572008-11-18 20:30:24 +0000736 err = cpu_breakpoint_insert(env, addr, BP_GDB, NULL);
737 if (err)
738 break;
739 }
740 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000741#ifndef CONFIG_USER_ONLY
742 case GDB_WATCHPOINT_WRITE:
743 case GDB_WATCHPOINT_READ:
744 case GDB_WATCHPOINT_ACCESS:
Andreas Färber182735e2013-05-29 22:29:20 +0200745 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
746 env = cpu->env_ptr;
aliguori880a7572008-11-18 20:30:24 +0000747 err = cpu_watchpoint_insert(env, addr, len, xlat_gdb_type[type],
748 NULL);
749 if (err)
750 break;
751 }
752 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000753#endif
754 default:
755 return -ENOSYS;
756 }
757}
758
aliguori880a7572008-11-18 20:30:24 +0000759static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000760{
Andreas Färber182735e2013-05-29 22:29:20 +0200761 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100762 CPUArchState *env;
aliguori880a7572008-11-18 20:30:24 +0000763 int err = 0;
764
Andreas Färber62278812013-06-27 17:12:06 +0200765 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200766 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200767 }
aliguorie22a25c2009-03-12 20:12:48 +0000768
aliguoria1d1bb32008-11-18 20:07:32 +0000769 switch (type) {
770 case GDB_BREAKPOINT_SW:
771 case GDB_BREAKPOINT_HW:
Andreas Färber182735e2013-05-29 22:29:20 +0200772 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
773 env = cpu->env_ptr;
aliguori880a7572008-11-18 20:30:24 +0000774 err = cpu_breakpoint_remove(env, addr, BP_GDB);
775 if (err)
776 break;
777 }
778 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000779#ifndef CONFIG_USER_ONLY
780 case GDB_WATCHPOINT_WRITE:
781 case GDB_WATCHPOINT_READ:
782 case GDB_WATCHPOINT_ACCESS:
Andreas Färber182735e2013-05-29 22:29:20 +0200783 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
784 env = cpu->env_ptr;
aliguori880a7572008-11-18 20:30:24 +0000785 err = cpu_watchpoint_remove(env, addr, len, xlat_gdb_type[type]);
786 if (err)
787 break;
788 }
789 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000790#endif
791 default:
792 return -ENOSYS;
793 }
794}
795
aliguori880a7572008-11-18 20:30:24 +0000796static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +0000797{
Andreas Färber182735e2013-05-29 22:29:20 +0200798 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100799 CPUArchState *env;
aliguori880a7572008-11-18 20:30:24 +0000800
aliguorie22a25c2009-03-12 20:12:48 +0000801 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200802 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +0000803 return;
804 }
805
Andreas Färber182735e2013-05-29 22:29:20 +0200806 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
807 env = cpu->env_ptr;
aliguori880a7572008-11-18 20:30:24 +0000808 cpu_breakpoint_remove_all(env, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000809#ifndef CONFIG_USER_ONLY
aliguori880a7572008-11-18 20:30:24 +0000810 cpu_watchpoint_remove_all(env, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000811#endif
aliguori880a7572008-11-18 20:30:24 +0000812 }
aliguoria1d1bb32008-11-18 20:07:32 +0000813}
814
aurel32fab9d282009-04-08 21:29:37 +0000815static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
816{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200817 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +0200818 CPUClass *cc = CPU_GET_CLASS(cpu);
819
820 cpu_synchronize_state(cpu);
821 if (cc->set_pc) {
822 cc->set_pc(cpu, pc);
Nathan Froydff1d1972009-12-08 08:06:30 -0800823 }
aurel32fab9d282009-04-08 21:29:37 +0000824}
825
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200826static CPUState *find_cpu(uint32_t thread_id)
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700827{
Andreas Färber0d342822012-12-17 07:12:13 +0100828 CPUState *cpu;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700829
Andreas Färber182735e2013-05-29 22:29:20 +0200830 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
Andreas Färberaa48dd92013-07-09 20:50:52 +0200831 if (cpu_index(cpu) == thread_id) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200832 return cpu;
Andreas Färberaa48dd92013-07-09 20:50:52 +0200833 }
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700834 }
Andreas Färberaa48dd92013-07-09 20:50:52 +0200835
836 return NULL;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700837}
838
aliguori880a7572008-11-18 20:30:24 +0000839static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +0000840{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200841 CPUState *cpu;
bellardb4608c02003-06-27 17:34:32 +0000842 const char *p;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700843 uint32_t thread;
844 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +0000845 char buf[MAX_PACKET_LENGTH];
846 uint8_t mem_buf[MAX_PACKET_LENGTH];
847 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +0000848 target_ulong addr, len;
ths3b46e622007-09-17 08:09:54 +0000849
bellard858693c2004-03-31 18:52:07 +0000850#ifdef DEBUG_GDB
851 printf("command='%s'\n", line_buf);
bellard4c3a88a2003-07-26 12:06:08 +0000852#endif
bellard858693c2004-03-31 18:52:07 +0000853 p = line_buf;
854 ch = *p++;
855 switch(ch) {
856 case '?':
bellard1fddef42005-04-17 19:16:13 +0000857 /* TODO: Make this return the correct value for user-mode. */
aurel32ca587a82008-12-18 22:44:13 +0000858 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200859 cpu_index(s->c_cpu));
bellard858693c2004-03-31 18:52:07 +0000860 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +0000861 /* Remove all the breakpoints when this query is issued,
862 * because gdb is doing and initial connect and the state
863 * should be cleaned up.
864 */
aliguori880a7572008-11-18 20:30:24 +0000865 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +0000866 break;
867 case 'c':
868 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +0000869 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +0000870 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +0000871 }
aurel32ca587a82008-12-18 22:44:13 +0000872 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +0000873 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +0000874 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +0000875 case 'C':
aurel32ca587a82008-12-18 22:44:13 +0000876 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
877 if (s->signal == -1)
878 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +0000879 gdb_continue(s);
880 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200881 case 'v':
882 if (strncmp(p, "Cont", 4) == 0) {
883 int res_signal, res_thread;
884
885 p += 4;
886 if (*p == '?') {
887 put_packet(s, "vCont;c;C;s;S");
888 break;
889 }
890 res = 0;
891 res_signal = 0;
892 res_thread = 0;
893 while (*p) {
894 int action, signal;
895
896 if (*p++ != ';') {
897 res = 0;
898 break;
899 }
900 action = *p++;
901 signal = 0;
902 if (action == 'C' || action == 'S') {
903 signal = strtoul(p, (char **)&p, 16);
904 } else if (action != 'c' && action != 's') {
905 res = 0;
906 break;
907 }
908 thread = 0;
909 if (*p == ':') {
910 thread = strtoull(p+1, (char **)&p, 16);
911 }
912 action = tolower(action);
913 if (res == 0 || (res == 'c' && action == 's')) {
914 res = action;
915 res_signal = signal;
916 res_thread = thread;
917 }
918 }
919 if (res) {
920 if (res_thread != -1 && res_thread != 0) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200921 cpu = find_cpu(res_thread);
922 if (cpu == NULL) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200923 put_packet(s, "E22");
924 break;
925 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200926 s->c_cpu = cpu;
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200927 }
928 if (res == 's') {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200929 cpu_single_step(s->c_cpu, sstep_flags);
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200930 }
931 s->signal = res_signal;
932 gdb_continue(s);
933 return RS_IDLE;
934 }
935 break;
936 } else {
937 goto unknown_command;
938 }
edgar_igl7d03f822008-05-17 18:58:29 +0000939 case 'k':
Jan Kiszka00e94db2012-03-06 18:32:35 +0100940#ifdef CONFIG_USER_ONLY
edgar_igl7d03f822008-05-17 18:58:29 +0000941 /* Kill the target */
942 fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
943 exit(0);
Jan Kiszka00e94db2012-03-06 18:32:35 +0100944#endif
edgar_igl7d03f822008-05-17 18:58:29 +0000945 case 'D':
946 /* Detach packet */
aliguori880a7572008-11-18 20:30:24 +0000947 gdb_breakpoint_remove_all();
Daniel Gutson7ea06da2010-02-26 14:13:50 -0300948 gdb_syscall_mode = GDB_SYS_DISABLED;
edgar_igl7d03f822008-05-17 18:58:29 +0000949 gdb_continue(s);
950 put_packet(s, "OK");
951 break;
bellard858693c2004-03-31 18:52:07 +0000952 case 's':
953 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +0000954 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +0000955 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +0000956 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200957 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +0000958 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +0000959 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +0000960 case 'F':
961 {
962 target_ulong ret;
963 target_ulong err;
964
965 ret = strtoull(p, (char **)&p, 16);
966 if (*p == ',') {
967 p++;
968 err = strtoull(p, (char **)&p, 16);
969 } else {
970 err = 0;
971 }
972 if (*p == ',')
973 p++;
974 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +0000975 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200976 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +0000977 s->current_syscall_cb = NULL;
978 }
pbrooka2d1eba2007-01-28 03:10:55 +0000979 if (type == 'C') {
980 put_packet(s, "T02");
981 } else {
edgar_iglba70a622008-03-14 06:10:42 +0000982 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +0000983 }
984 }
985 break;
bellard858693c2004-03-31 18:52:07 +0000986 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200987 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +0000988 len = 0;
Andreas Färbera0e372f2013-06-28 23:18:47 +0200989 for (addr = 0; addr < s->g_cpu->gdb_num_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200990 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +0000991 len += reg_size;
992 }
993 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +0000994 put_packet(s, buf);
995 break;
996 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200997 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +0000998 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +0000999 len = strlen(p) / 2;
1000 hextomem((uint8_t *)registers, p, len);
Andreas Färbera0e372f2013-06-28 23:18:47 +02001001 for (addr = 0; addr < s->g_cpu->gdb_num_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001002 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001003 len -= reg_size;
1004 registers += reg_size;
1005 }
bellard858693c2004-03-31 18:52:07 +00001006 put_packet(s, "OK");
1007 break;
1008 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001009 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001010 if (*p == ',')
1011 p++;
bellard9d9754a2006-06-25 15:32:37 +00001012 len = strtoull(p, NULL, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001013 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001014 put_packet (s, "E14");
1015 } else {
1016 memtohex(buf, mem_buf, len);
1017 put_packet(s, buf);
1018 }
bellard858693c2004-03-31 18:52:07 +00001019 break;
1020 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001021 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001022 if (*p == ',')
1023 p++;
bellard9d9754a2006-06-25 15:32:37 +00001024 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001025 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001026 p++;
1027 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001028 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +02001029 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +00001030 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001031 } else {
bellard858693c2004-03-31 18:52:07 +00001032 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001033 }
bellard858693c2004-03-31 18:52:07 +00001034 break;
pbrook56aebc82008-10-11 17:55:29 +00001035 case 'p':
1036 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1037 This works, but can be very slow. Anything new enough to
1038 understand XML also knows how to use this properly. */
1039 if (!gdb_has_xml)
1040 goto unknown_command;
1041 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001042 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001043 if (reg_size) {
1044 memtohex(buf, mem_buf, reg_size);
1045 put_packet(s, buf);
1046 } else {
1047 put_packet(s, "E14");
1048 }
1049 break;
1050 case 'P':
1051 if (!gdb_has_xml)
1052 goto unknown_command;
1053 addr = strtoull(p, (char **)&p, 16);
1054 if (*p == '=')
1055 p++;
1056 reg_size = strlen(p) / 2;
1057 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001058 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001059 put_packet(s, "OK");
1060 break;
bellard858693c2004-03-31 18:52:07 +00001061 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001062 case 'z':
1063 type = strtoul(p, (char **)&p, 16);
1064 if (*p == ',')
1065 p++;
bellard9d9754a2006-06-25 15:32:37 +00001066 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001067 if (*p == ',')
1068 p++;
bellard9d9754a2006-06-25 15:32:37 +00001069 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001070 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001071 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001072 else
aliguori880a7572008-11-18 20:30:24 +00001073 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001074 if (res >= 0)
1075 put_packet(s, "OK");
1076 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001077 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001078 else
1079 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001080 break;
aliguori880a7572008-11-18 20:30:24 +00001081 case 'H':
1082 type = *p++;
1083 thread = strtoull(p, (char **)&p, 16);
1084 if (thread == -1 || thread == 0) {
1085 put_packet(s, "OK");
1086 break;
1087 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001088 cpu = find_cpu(thread);
1089 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001090 put_packet(s, "E22");
1091 break;
1092 }
1093 switch (type) {
1094 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001095 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001096 put_packet(s, "OK");
1097 break;
1098 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001099 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001100 put_packet(s, "OK");
1101 break;
1102 default:
1103 put_packet(s, "E22");
1104 break;
1105 }
1106 break;
1107 case 'T':
1108 thread = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001109 cpu = find_cpu(thread);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001110
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001111 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001112 put_packet(s, "OK");
1113 } else {
aliguori880a7572008-11-18 20:30:24 +00001114 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001115 }
aliguori880a7572008-11-18 20:30:24 +00001116 break;
pbrook978efd62006-06-17 18:30:42 +00001117 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001118 case 'Q':
1119 /* parse any 'q' packets here */
1120 if (!strcmp(p,"qemu.sstepbits")) {
1121 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001122 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1123 SSTEP_ENABLE,
1124 SSTEP_NOIRQ,
1125 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001126 put_packet(s, buf);
1127 break;
1128 } else if (strncmp(p,"qemu.sstep",10) == 0) {
1129 /* Display or change the sstep_flags */
1130 p += 10;
1131 if (*p != '=') {
1132 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001133 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001134 put_packet(s, buf);
1135 break;
1136 }
1137 p++;
1138 type = strtoul(p, (char **)&p, 16);
1139 sstep_flags = type;
1140 put_packet(s, "OK");
1141 break;
aliguori880a7572008-11-18 20:30:24 +00001142 } else if (strcmp(p,"C") == 0) {
1143 /* "Current thread" remains vague in the spec, so always return
1144 * the first CPU (gdb returns the first thread). */
1145 put_packet(s, "QC1");
1146 break;
1147 } else if (strcmp(p,"fThreadInfo") == 0) {
Andreas Färber52f34622013-06-27 13:44:40 +02001148 s->query_cpu = first_cpu;
aliguori880a7572008-11-18 20:30:24 +00001149 goto report_cpuinfo;
1150 } else if (strcmp(p,"sThreadInfo") == 0) {
1151 report_cpuinfo:
1152 if (s->query_cpu) {
Andreas Färber52f34622013-06-27 13:44:40 +02001153 snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu));
aliguori880a7572008-11-18 20:30:24 +00001154 put_packet(s, buf);
Andreas Färber52f34622013-06-27 13:44:40 +02001155 s->query_cpu = s->query_cpu->next_cpu;
aliguori880a7572008-11-18 20:30:24 +00001156 } else
1157 put_packet(s, "l");
1158 break;
1159 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1160 thread = strtoull(p+16, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001161 cpu = find_cpu(thread);
1162 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001163 cpu_synchronize_state(cpu);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001164 len = snprintf((char *)mem_buf, sizeof(mem_buf),
Andreas Färber55e5c282012-12-17 06:18:02 +01001165 "CPU#%d [%s]", cpu->cpu_index,
Andreas Färber259186a2013-01-17 18:51:17 +01001166 cpu->halted ? "halted " : "running");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001167 memtohex(buf, mem_buf, len);
1168 put_packet(s, buf);
1169 }
aliguori880a7572008-11-18 20:30:24 +00001170 break;
edgar_igl60897d32008-05-09 08:25:14 +00001171 }
blueswir10b8a9882009-03-07 10:51:36 +00001172#ifdef CONFIG_USER_ONLY
edgar_igl60897d32008-05-09 08:25:14 +00001173 else if (strncmp(p, "Offsets", 7) == 0) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001174 CPUArchState *env = s->c_cpu->env_ptr;
1175 TaskState *ts = env->opaque;
pbrook978efd62006-06-17 18:30:42 +00001176
blueswir1363a37d2008-08-21 17:58:08 +00001177 snprintf(buf, sizeof(buf),
1178 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1179 ";Bss=" TARGET_ABI_FMT_lx,
1180 ts->info->code_offset,
1181 ts->info->data_offset,
1182 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001183 put_packet(s, buf);
1184 break;
1185 }
blueswir10b8a9882009-03-07 10:51:36 +00001186#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001187 else if (strncmp(p, "Rcmd,", 5) == 0) {
1188 int len = strlen(p + 5);
1189
1190 if ((len % 2) != 0) {
1191 put_packet(s, "E01");
1192 break;
1193 }
1194 hextomem(mem_buf, p + 5, len);
1195 len = len / 2;
1196 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001197 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001198 put_packet(s, "OK");
1199 break;
1200 }
blueswir10b8a9882009-03-07 10:51:36 +00001201#endif /* !CONFIG_USER_ONLY */
pbrook56aebc82008-10-11 17:55:29 +00001202 if (strncmp(p, "Supported", 9) == 0) {
blueswir15b3715b2008-10-25 11:18:12 +00001203 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
pbrook56aebc82008-10-11 17:55:29 +00001204#ifdef GDB_CORE_XML
blueswir12dc766d2009-04-13 16:06:19 +00001205 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
pbrook56aebc82008-10-11 17:55:29 +00001206#endif
1207 put_packet(s, buf);
1208 break;
1209 }
1210#ifdef GDB_CORE_XML
1211 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1212 const char *xml;
1213 target_ulong total_len;
1214
1215 gdb_has_xml = 1;
1216 p += 19;
aliguori880a7572008-11-18 20:30:24 +00001217 xml = get_feature_xml(p, &p);
pbrook56aebc82008-10-11 17:55:29 +00001218 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001219 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001220 put_packet(s, buf);
1221 break;
1222 }
1223
1224 if (*p == ':')
1225 p++;
1226 addr = strtoul(p, (char **)&p, 16);
1227 if (*p == ',')
1228 p++;
1229 len = strtoul(p, (char **)&p, 16);
1230
1231 total_len = strlen(xml);
1232 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001233 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001234 put_packet(s, buf);
1235 break;
1236 }
1237 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1238 len = (MAX_PACKET_LENGTH - 5) / 2;
1239 if (len < total_len - addr) {
1240 buf[0] = 'm';
1241 len = memtox(buf + 1, xml + addr, len);
1242 } else {
1243 buf[0] = 'l';
1244 len = memtox(buf + 1, xml + addr, total_len - addr);
1245 }
1246 put_packet_binary(s, buf, len + 1);
1247 break;
1248 }
1249#endif
1250 /* Unrecognised 'q' command. */
1251 goto unknown_command;
1252
bellard858693c2004-03-31 18:52:07 +00001253 default:
pbrook56aebc82008-10-11 17:55:29 +00001254 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001255 /* put empty packet */
1256 buf[0] = '\0';
1257 put_packet(s, buf);
1258 break;
1259 }
1260 return RS_IDLE;
1261}
1262
Andreas Färber64f6b342013-05-27 02:06:09 +02001263void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001264{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001265 gdbserver_state->c_cpu = cpu;
1266 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001267}
1268
bellard1fddef42005-04-17 19:16:13 +00001269#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001270static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001271{
aliguori880a7572008-11-18 20:30:24 +00001272 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001273 CPUArchState *env = s->c_cpu->env_ptr;
1274 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001275 char buf[256];
aliguorid6fc1b32008-11-18 19:55:44 +00001276 const char *type;
bellard858693c2004-03-31 18:52:07 +00001277 int ret;
1278
Meador Ingecdb432b2012-03-15 17:49:45 +00001279 if (running || s->state == RS_INACTIVE) {
1280 return;
1281 }
1282 /* Is there a GDB syscall waiting to be sent? */
1283 if (s->current_syscall_cb) {
1284 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001285 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001286 }
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001287 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001288 case RUN_STATE_DEBUG:
aliguori880a7572008-11-18 20:30:24 +00001289 if (env->watchpoint_hit) {
1290 switch (env->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001291 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001292 type = "r";
1293 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001294 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001295 type = "a";
1296 break;
1297 default:
1298 type = "";
1299 break;
1300 }
aliguori880a7572008-11-18 20:30:24 +00001301 snprintf(buf, sizeof(buf),
1302 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
Andreas Färber0d342822012-12-17 07:12:13 +01001303 GDB_SIGNAL_TRAP, cpu_index(cpu), type,
aliguori880a7572008-11-18 20:30:24 +00001304 env->watchpoint_hit->vaddr);
aliguori880a7572008-11-18 20:30:24 +00001305 env->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001306 goto send_packet;
pbrook6658ffb2007-03-16 23:58:11 +00001307 }
Jan Kiszka425189a2011-03-22 11:02:09 +01001308 tb_flush(env);
aurel32ca587a82008-12-18 22:44:13 +00001309 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001310 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001311 case RUN_STATE_PAUSED:
aliguori9781e042009-01-22 17:15:29 +00001312 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001313 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001314 case RUN_STATE_SHUTDOWN:
Jan Kiszka425189a2011-03-22 11:02:09 +01001315 ret = GDB_SIGNAL_QUIT;
1316 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001317 case RUN_STATE_IO_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001318 ret = GDB_SIGNAL_IO;
1319 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001320 case RUN_STATE_WATCHDOG:
Jan Kiszka425189a2011-03-22 11:02:09 +01001321 ret = GDB_SIGNAL_ALRM;
1322 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001323 case RUN_STATE_INTERNAL_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001324 ret = GDB_SIGNAL_ABRT;
1325 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001326 case RUN_STATE_SAVE_VM:
1327 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001328 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001329 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001330 ret = GDB_SIGNAL_XCPU;
1331 break;
1332 default:
1333 ret = GDB_SIGNAL_UNKNOWN;
1334 break;
bellardbbeb7b52006-04-23 18:42:15 +00001335 }
Andreas Färber0d342822012-12-17 07:12:13 +01001336 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(cpu));
Jan Kiszka425189a2011-03-22 11:02:09 +01001337
1338send_packet:
bellard858693c2004-03-31 18:52:07 +00001339 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001340
1341 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001342 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001343}
bellard1fddef42005-04-17 19:16:13 +00001344#endif
bellard858693c2004-03-31 18:52:07 +00001345
pbrooka2d1eba2007-01-28 03:10:55 +00001346/* Send a gdb syscall request.
1347 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001348 %x - target_ulong argument printed in hex.
1349 %lx - 64-bit argument printed in hex.
1350 %s - string pointer (target_ulong) and length (int) pair. */
blueswir17ccfb2e2008-09-14 06:45:34 +00001351void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
pbrooka2d1eba2007-01-28 03:10:55 +00001352{
1353 va_list va;
pbrooka2d1eba2007-01-28 03:10:55 +00001354 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001355 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001356 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001357 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001358 GDBState *s;
1359
aliguori880a7572008-11-18 20:30:24 +00001360 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001361 if (!s)
1362 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001363 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001364#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001365 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001366#endif
pbrooka2d1eba2007-01-28 03:10:55 +00001367 va_start(va, fmt);
Meador Ingecdb432b2012-03-15 17:49:45 +00001368 p = s->syscall_buf;
1369 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001370 *(p++) = 'F';
1371 while (*fmt) {
1372 if (*fmt == '%') {
1373 fmt++;
1374 switch (*fmt++) {
1375 case 'x':
1376 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001377 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001378 break;
pbrooka87295e2007-05-26 15:09:38 +00001379 case 'l':
1380 if (*(fmt++) != 'x')
1381 goto bad_format;
1382 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001383 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001384 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001385 case 's':
1386 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001387 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001388 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001389 break;
1390 default:
pbrooka87295e2007-05-26 15:09:38 +00001391 bad_format:
pbrooka2d1eba2007-01-28 03:10:55 +00001392 fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
1393 fmt - 1);
1394 break;
1395 }
1396 } else {
1397 *(p++) = *(fmt++);
1398 }
1399 }
pbrook8a93e022007-08-06 13:19:15 +00001400 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001401 va_end(va);
pbrooka2d1eba2007-01-28 03:10:55 +00001402#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001403 put_packet(s, s->syscall_buf);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001404 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001405#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001406 /* In this case wait to send the syscall packet until notification that
1407 the CPU has stopped. This must be done because if the packet is sent
1408 now the reply from the syscall request could be received while the CPU
1409 is still in the running state, which can cause packets to be dropped
1410 and state transition 'T' packets to be sent while the syscall is still
1411 being processed. */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001412 cpu_exit(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00001413#endif
1414}
1415
bellard6a00d602005-11-21 23:25:50 +00001416static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00001417{
1418 int i, csum;
ths60fe76f2007-12-16 03:02:09 +00001419 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00001420
bellard1fddef42005-04-17 19:16:13 +00001421#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001422 if (s->last_packet_len) {
1423 /* Waiting for a response to the last packet. If we see the start
1424 of a new command then abandon the previous response. */
1425 if (ch == '-') {
1426#ifdef DEBUG_GDB
1427 printf("Got NACK, retransmitting\n");
1428#endif
thsffe8ab82007-12-16 03:16:05 +00001429 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
pbrook4046d912007-01-28 01:53:16 +00001430 }
1431#ifdef DEBUG_GDB
1432 else if (ch == '+')
1433 printf("Got ACK\n");
1434 else
1435 printf("Got '%c' when expecting ACK/NACK\n", ch);
1436#endif
1437 if (ch == '+' || ch == '$')
1438 s->last_packet_len = 0;
1439 if (ch != '$')
1440 return;
1441 }
Luiz Capitulino13548692011-07-29 15:36:43 -03001442 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00001443 /* when the CPU is running, we cannot do anything except stop
1444 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001445 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00001446 } else
bellard1fddef42005-04-17 19:16:13 +00001447#endif
bellard41625032005-04-24 10:07:11 +00001448 {
bellard858693c2004-03-31 18:52:07 +00001449 switch(s->state) {
1450 case RS_IDLE:
1451 if (ch == '$') {
1452 s->line_buf_index = 0;
1453 s->state = RS_GETLINE;
bellard4c3a88a2003-07-26 12:06:08 +00001454 }
1455 break;
bellard858693c2004-03-31 18:52:07 +00001456 case RS_GETLINE:
1457 if (ch == '#') {
1458 s->state = RS_CHKSUM1;
1459 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1460 s->state = RS_IDLE;
1461 } else {
1462 s->line_buf[s->line_buf_index++] = ch;
1463 }
1464 break;
1465 case RS_CHKSUM1:
1466 s->line_buf[s->line_buf_index] = '\0';
1467 s->line_csum = fromhex(ch) << 4;
1468 s->state = RS_CHKSUM2;
1469 break;
1470 case RS_CHKSUM2:
1471 s->line_csum |= fromhex(ch);
1472 csum = 0;
1473 for(i = 0; i < s->line_buf_index; i++) {
1474 csum += s->line_buf[i];
1475 }
1476 if (s->line_csum != (csum & 0xff)) {
ths60fe76f2007-12-16 03:02:09 +00001477 reply = '-';
1478 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00001479 s->state = RS_IDLE;
1480 } else {
ths60fe76f2007-12-16 03:02:09 +00001481 reply = '+';
1482 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00001483 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00001484 }
bellardb4608c02003-06-27 17:34:32 +00001485 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001486 default:
1487 abort();
bellardb4608c02003-06-27 17:34:32 +00001488 }
1489 }
bellard858693c2004-03-31 18:52:07 +00001490}
1491
Paul Brook0e1c9c52010-06-16 13:03:51 +01001492/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001493void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01001494{
1495 GDBState *s;
1496 char buf[4];
1497
1498 s = gdbserver_state;
1499 if (!s) {
1500 return;
1501 }
1502#ifdef CONFIG_USER_ONLY
1503 if (gdbserver_fd < 0 || s->fd < 0) {
1504 return;
1505 }
1506#endif
1507
1508 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
1509 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001510
1511#ifndef CONFIG_USER_ONLY
1512 if (s->chr) {
Anthony Liguori70f24fb2011-08-15 11:17:38 -05001513 qemu_chr_delete(s->chr);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001514 }
1515#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01001516}
1517
bellard1fddef42005-04-17 19:16:13 +00001518#ifdef CONFIG_USER_ONLY
1519int
aurel32ca587a82008-12-18 22:44:13 +00001520gdb_queuesig (void)
1521{
1522 GDBState *s;
1523
1524 s = gdbserver_state;
1525
1526 if (gdbserver_fd < 0 || s->fd < 0)
1527 return 0;
1528 else
1529 return 1;
1530}
1531
1532int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001533gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00001534{
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001535 CPUArchState *env = cpu->env_ptr;
Andreas Färber5ca666c2013-06-24 19:20:57 +02001536 GDBState *s;
1537 char buf[256];
1538 int n;
bellard1fddef42005-04-17 19:16:13 +00001539
Andreas Färber5ca666c2013-06-24 19:20:57 +02001540 s = gdbserver_state;
1541 if (gdbserver_fd < 0 || s->fd < 0) {
1542 return sig;
bellard1fddef42005-04-17 19:16:13 +00001543 }
1544
Andreas Färber5ca666c2013-06-24 19:20:57 +02001545 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001546 cpu_single_step(cpu, 0);
Andreas Färber5ca666c2013-06-24 19:20:57 +02001547 tb_flush(env);
bellard1fddef42005-04-17 19:16:13 +00001548
Andreas Färber5ca666c2013-06-24 19:20:57 +02001549 if (sig != 0) {
1550 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
1551 put_packet(s, buf);
1552 }
1553 /* put_packet() might have detected that the peer terminated the
1554 connection. */
1555 if (s->fd < 0) {
1556 return sig;
1557 }
1558
1559 sig = 0;
1560 s->state = RS_IDLE;
1561 s->running_state = 0;
1562 while (s->running_state == 0) {
1563 n = read(s->fd, buf, 256);
1564 if (n > 0) {
1565 int i;
1566
1567 for (i = 0; i < n; i++) {
1568 gdb_read_byte(s, buf[i]);
1569 }
1570 } else if (n == 0 || errno != EAGAIN) {
1571 /* XXX: Connection closed. Should probably wait for another
1572 connection before continuing. */
1573 return sig;
bellard1fddef42005-04-17 19:16:13 +00001574 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02001575 }
1576 sig = s->signal;
1577 s->signal = 0;
1578 return sig;
bellard1fddef42005-04-17 19:16:13 +00001579}
bellarde9009672005-04-26 20:42:36 +00001580
aurel32ca587a82008-12-18 22:44:13 +00001581/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001582void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00001583{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001584 GDBState *s;
1585 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00001586
Andreas Färber5ca666c2013-06-24 19:20:57 +02001587 s = gdbserver_state;
1588 if (gdbserver_fd < 0 || s->fd < 0) {
1589 return;
1590 }
aurel32ca587a82008-12-18 22:44:13 +00001591
Andreas Färber5ca666c2013-06-24 19:20:57 +02001592 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
1593 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00001594}
bellard1fddef42005-04-17 19:16:13 +00001595
aliguori880a7572008-11-18 20:30:24 +00001596static void gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00001597{
1598 GDBState *s;
1599 struct sockaddr_in sockaddr;
1600 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001601 int fd;
bellard858693c2004-03-31 18:52:07 +00001602
1603 for(;;) {
1604 len = sizeof(sockaddr);
1605 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1606 if (fd < 0 && errno != EINTR) {
1607 perror("accept");
1608 return;
1609 } else if (fd >= 0) {
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001610#ifndef _WIN32
1611 fcntl(fd, F_SETFD, FD_CLOEXEC);
1612#endif
bellard858693c2004-03-31 18:52:07 +00001613 break;
1614 }
1615 }
1616
1617 /* set short latency */
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001618 socket_set_nodelay(fd);
ths3b46e622007-09-17 08:09:54 +00001619
Anthony Liguori7267c092011-08-20 22:09:37 -05001620 s = g_malloc0(sizeof(GDBState));
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001621 s->c_cpu = first_cpu;
1622 s->g_cpu = first_cpu;
bellard858693c2004-03-31 18:52:07 +00001623 s->fd = fd;
pbrook56aebc82008-10-11 17:55:29 +00001624 gdb_has_xml = 0;
bellard858693c2004-03-31 18:52:07 +00001625
aliguori880a7572008-11-18 20:30:24 +00001626 gdbserver_state = s;
pbrooka2d1eba2007-01-28 03:10:55 +00001627
bellard858693c2004-03-31 18:52:07 +00001628 fcntl(fd, F_SETFL, O_NONBLOCK);
bellard858693c2004-03-31 18:52:07 +00001629}
1630
1631static int gdbserver_open(int port)
1632{
1633 struct sockaddr_in sockaddr;
1634 int fd, val, ret;
1635
1636 fd = socket(PF_INET, SOCK_STREAM, 0);
1637 if (fd < 0) {
1638 perror("socket");
1639 return -1;
1640 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001641#ifndef _WIN32
1642 fcntl(fd, F_SETFD, FD_CLOEXEC);
1643#endif
bellard858693c2004-03-31 18:52:07 +00001644
1645 /* allow fast reuse */
1646 val = 1;
Stefan Weil9957fc72013-03-08 19:58:32 +01001647 qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
bellard858693c2004-03-31 18:52:07 +00001648
1649 sockaddr.sin_family = AF_INET;
1650 sockaddr.sin_port = htons(port);
1651 sockaddr.sin_addr.s_addr = 0;
1652 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1653 if (ret < 0) {
1654 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00001655 close(fd);
bellard858693c2004-03-31 18:52:07 +00001656 return -1;
1657 }
1658 ret = listen(fd, 0);
1659 if (ret < 0) {
1660 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00001661 close(fd);
bellard858693c2004-03-31 18:52:07 +00001662 return -1;
1663 }
bellard858693c2004-03-31 18:52:07 +00001664 return fd;
1665}
1666
1667int gdbserver_start(int port)
1668{
1669 gdbserver_fd = gdbserver_open(port);
1670 if (gdbserver_fd < 0)
1671 return -1;
1672 /* accept connections */
aliguori880a7572008-11-18 20:30:24 +00001673 gdb_accept();
bellardb4608c02003-06-27 17:34:32 +00001674 return 0;
1675}
aurel322b1319c2008-12-18 22:44:04 +00001676
1677/* Disable gdb stub for child processes. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001678void gdbserver_fork(CPUArchState *env)
aurel322b1319c2008-12-18 22:44:04 +00001679{
1680 GDBState *s = gdbserver_state;
edgar_igl9f6164d2009-01-07 10:22:28 +00001681 if (gdbserver_fd < 0 || s->fd < 0)
aurel322b1319c2008-12-18 22:44:04 +00001682 return;
1683 close(s->fd);
1684 s->fd = -1;
1685 cpu_breakpoint_remove_all(env, BP_GDB);
1686 cpu_watchpoint_remove_all(env, BP_GDB);
1687}
pbrook4046d912007-01-28 01:53:16 +00001688#else
thsaa1f17c2007-07-11 22:48:58 +00001689static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00001690{
pbrook56aebc82008-10-11 17:55:29 +00001691 /* We can handle an arbitrarily large amount of data.
1692 Pick the maximum packet size, which is as good as anything. */
1693 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00001694}
1695
thsaa1f17c2007-07-11 22:48:58 +00001696static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00001697{
pbrook4046d912007-01-28 01:53:16 +00001698 int i;
1699
1700 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00001701 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00001702 }
1703}
1704
1705static void gdb_chr_event(void *opaque, int event)
1706{
1707 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05301708 case CHR_EVENT_OPENED:
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001709 vm_stop(RUN_STATE_PAUSED);
pbrook56aebc82008-10-11 17:55:29 +00001710 gdb_has_xml = 0;
pbrook4046d912007-01-28 01:53:16 +00001711 break;
1712 default:
1713 break;
1714 }
1715}
1716
aliguori8a34a0f2009-03-05 23:01:55 +00001717static void gdb_monitor_output(GDBState *s, const char *msg, int len)
1718{
1719 char buf[MAX_PACKET_LENGTH];
1720
1721 buf[0] = 'O';
1722 if (len > (MAX_PACKET_LENGTH/2) - 1)
1723 len = (MAX_PACKET_LENGTH/2) - 1;
1724 memtohex(buf + 1, (uint8_t *)msg, len);
1725 put_packet(s, buf);
1726}
1727
1728static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
1729{
1730 const char *p = (const char *)buf;
1731 int max_sz;
1732
1733 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
1734 for (;;) {
1735 if (len <= max_sz) {
1736 gdb_monitor_output(gdbserver_state, p, len);
1737 break;
1738 }
1739 gdb_monitor_output(gdbserver_state, p, max_sz);
1740 p += max_sz;
1741 len -= max_sz;
1742 }
1743 return len;
1744}
1745
aliguori59030a82009-04-05 18:43:41 +00001746#ifndef _WIN32
1747static void gdb_sigterm_handler(int signal)
1748{
Luiz Capitulino13548692011-07-29 15:36:43 -03001749 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001750 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001751 }
aliguori59030a82009-04-05 18:43:41 +00001752}
1753#endif
1754
1755int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00001756{
1757 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00001758 char gdbstub_device_name[128];
aliguori36556b22009-03-28 18:05:53 +00001759 CharDriverState *chr = NULL;
1760 CharDriverState *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00001761
aliguori59030a82009-04-05 18:43:41 +00001762 if (!device)
1763 return -1;
1764 if (strcmp(device, "none") != 0) {
1765 if (strstart(device, "tcp:", NULL)) {
1766 /* enforce required TCP attributes */
1767 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
1768 "%s,nowait,nodelay,server", device);
1769 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00001770 }
aliguori59030a82009-04-05 18:43:41 +00001771#ifndef _WIN32
1772 else if (strcmp(device, "stdio") == 0) {
1773 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00001774
aliguori59030a82009-04-05 18:43:41 +00001775 memset(&act, 0, sizeof(act));
1776 act.sa_handler = gdb_sigterm_handler;
1777 sigaction(SIGINT, &act, NULL);
1778 }
1779#endif
Anthony Liguori27143a42011-08-15 11:17:36 -05001780 chr = qemu_chr_new("gdb", device, NULL);
aliguori36556b22009-03-28 18:05:53 +00001781 if (!chr)
1782 return -1;
1783
Hans de Goede456d6062013-03-27 20:29:40 +01001784 qemu_chr_fe_claim_no_fail(chr);
aliguori36556b22009-03-28 18:05:53 +00001785 qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
1786 gdb_chr_event, NULL);
pbrookcfc34752007-02-22 01:48:01 +00001787 }
1788
aliguori36556b22009-03-28 18:05:53 +00001789 s = gdbserver_state;
1790 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001791 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00001792 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00001793
aliguori36556b22009-03-28 18:05:53 +00001794 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
1795
1796 /* Initialize a monitor terminal for gdb */
Anthony Liguori7267c092011-08-20 22:09:37 -05001797 mon_chr = g_malloc0(sizeof(*mon_chr));
aliguori36556b22009-03-28 18:05:53 +00001798 mon_chr->chr_write = gdb_monitor_write;
1799 monitor_init(mon_chr, 0);
1800 } else {
1801 if (s->chr)
Anthony Liguori70f24fb2011-08-15 11:17:38 -05001802 qemu_chr_delete(s->chr);
aliguori36556b22009-03-28 18:05:53 +00001803 mon_chr = s->mon_chr;
1804 memset(s, 0, sizeof(GDBState));
1805 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001806 s->c_cpu = first_cpu;
1807 s->g_cpu = first_cpu;
pbrook4046d912007-01-28 01:53:16 +00001808 s->chr = chr;
aliguori36556b22009-03-28 18:05:53 +00001809 s->state = chr ? RS_IDLE : RS_INACTIVE;
1810 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00001811 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00001812
pbrook4046d912007-01-28 01:53:16 +00001813 return 0;
1814}
1815#endif