blob: cd10781c13acc83b4d710ece7e32deb5a91ee873 [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"
aurel32ca587a82008-12-18 22:44:13 +000043
Andreas Färberf3659ee2013-06-27 19:09:09 +020044static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
45 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020046{
Andreas Färberf3659ee2013-06-27 19:09:09 +020047 CPUClass *cc = CPU_GET_CLASS(cpu);
48
49 if (cc->memory_rw_debug) {
50 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
51 }
52 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020053}
aurel32ca587a82008-12-18 22:44:13 +000054
55enum {
56 GDB_SIGNAL_0 = 0,
57 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +010058 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +000059 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +010060 GDB_SIGNAL_ABRT = 6,
61 GDB_SIGNAL_ALRM = 14,
62 GDB_SIGNAL_IO = 23,
63 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +000064 GDB_SIGNAL_UNKNOWN = 143
65};
66
67#ifdef CONFIG_USER_ONLY
68
69/* Map target signal numbers to GDB protocol signal numbers and vice
70 * versa. For user emulation's currently supported systems, we can
71 * assume most signals are defined.
72 */
73
74static int gdb_signal_table[] = {
75 0,
76 TARGET_SIGHUP,
77 TARGET_SIGINT,
78 TARGET_SIGQUIT,
79 TARGET_SIGILL,
80 TARGET_SIGTRAP,
81 TARGET_SIGABRT,
82 -1, /* SIGEMT */
83 TARGET_SIGFPE,
84 TARGET_SIGKILL,
85 TARGET_SIGBUS,
86 TARGET_SIGSEGV,
87 TARGET_SIGSYS,
88 TARGET_SIGPIPE,
89 TARGET_SIGALRM,
90 TARGET_SIGTERM,
91 TARGET_SIGURG,
92 TARGET_SIGSTOP,
93 TARGET_SIGTSTP,
94 TARGET_SIGCONT,
95 TARGET_SIGCHLD,
96 TARGET_SIGTTIN,
97 TARGET_SIGTTOU,
98 TARGET_SIGIO,
99 TARGET_SIGXCPU,
100 TARGET_SIGXFSZ,
101 TARGET_SIGVTALRM,
102 TARGET_SIGPROF,
103 TARGET_SIGWINCH,
104 -1, /* SIGLOST */
105 TARGET_SIGUSR1,
106 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000107#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000108 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000109#else
110 -1,
111#endif
aurel32ca587a82008-12-18 22:44:13 +0000112 -1, /* SIGPOLL */
113 -1,
114 -1,
115 -1,
116 -1,
117 -1,
118 -1,
119 -1,
120 -1,
121 -1,
122 -1,
123 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000124#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000125 __SIGRTMIN + 1,
126 __SIGRTMIN + 2,
127 __SIGRTMIN + 3,
128 __SIGRTMIN + 4,
129 __SIGRTMIN + 5,
130 __SIGRTMIN + 6,
131 __SIGRTMIN + 7,
132 __SIGRTMIN + 8,
133 __SIGRTMIN + 9,
134 __SIGRTMIN + 10,
135 __SIGRTMIN + 11,
136 __SIGRTMIN + 12,
137 __SIGRTMIN + 13,
138 __SIGRTMIN + 14,
139 __SIGRTMIN + 15,
140 __SIGRTMIN + 16,
141 __SIGRTMIN + 17,
142 __SIGRTMIN + 18,
143 __SIGRTMIN + 19,
144 __SIGRTMIN + 20,
145 __SIGRTMIN + 21,
146 __SIGRTMIN + 22,
147 __SIGRTMIN + 23,
148 __SIGRTMIN + 24,
149 __SIGRTMIN + 25,
150 __SIGRTMIN + 26,
151 __SIGRTMIN + 27,
152 __SIGRTMIN + 28,
153 __SIGRTMIN + 29,
154 __SIGRTMIN + 30,
155 __SIGRTMIN + 31,
156 -1, /* SIGCANCEL */
157 __SIGRTMIN,
158 __SIGRTMIN + 32,
159 __SIGRTMIN + 33,
160 __SIGRTMIN + 34,
161 __SIGRTMIN + 35,
162 __SIGRTMIN + 36,
163 __SIGRTMIN + 37,
164 __SIGRTMIN + 38,
165 __SIGRTMIN + 39,
166 __SIGRTMIN + 40,
167 __SIGRTMIN + 41,
168 __SIGRTMIN + 42,
169 __SIGRTMIN + 43,
170 __SIGRTMIN + 44,
171 __SIGRTMIN + 45,
172 __SIGRTMIN + 46,
173 __SIGRTMIN + 47,
174 __SIGRTMIN + 48,
175 __SIGRTMIN + 49,
176 __SIGRTMIN + 50,
177 __SIGRTMIN + 51,
178 __SIGRTMIN + 52,
179 __SIGRTMIN + 53,
180 __SIGRTMIN + 54,
181 __SIGRTMIN + 55,
182 __SIGRTMIN + 56,
183 __SIGRTMIN + 57,
184 __SIGRTMIN + 58,
185 __SIGRTMIN + 59,
186 __SIGRTMIN + 60,
187 __SIGRTMIN + 61,
188 __SIGRTMIN + 62,
189 __SIGRTMIN + 63,
190 __SIGRTMIN + 64,
191 __SIGRTMIN + 65,
192 __SIGRTMIN + 66,
193 __SIGRTMIN + 67,
194 __SIGRTMIN + 68,
195 __SIGRTMIN + 69,
196 __SIGRTMIN + 70,
197 __SIGRTMIN + 71,
198 __SIGRTMIN + 72,
199 __SIGRTMIN + 73,
200 __SIGRTMIN + 74,
201 __SIGRTMIN + 75,
202 __SIGRTMIN + 76,
203 __SIGRTMIN + 77,
204 __SIGRTMIN + 78,
205 __SIGRTMIN + 79,
206 __SIGRTMIN + 80,
207 __SIGRTMIN + 81,
208 __SIGRTMIN + 82,
209 __SIGRTMIN + 83,
210 __SIGRTMIN + 84,
211 __SIGRTMIN + 85,
212 __SIGRTMIN + 86,
213 __SIGRTMIN + 87,
214 __SIGRTMIN + 88,
215 __SIGRTMIN + 89,
216 __SIGRTMIN + 90,
217 __SIGRTMIN + 91,
218 __SIGRTMIN + 92,
219 __SIGRTMIN + 93,
220 __SIGRTMIN + 94,
221 __SIGRTMIN + 95,
222 -1, /* SIGINFO */
223 -1, /* UNKNOWN */
224 -1, /* DEFAULT */
225 -1,
226 -1,
227 -1,
228 -1,
229 -1,
230 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000231#endif
aurel32ca587a82008-12-18 22:44:13 +0000232};
bellard8f447cc2006-06-14 15:21:14 +0000233#else
aurel32ca587a82008-12-18 22:44:13 +0000234/* In system mode we only need SIGINT and SIGTRAP; other signals
235 are not yet supported. */
236
237enum {
238 TARGET_SIGINT = 2,
239 TARGET_SIGTRAP = 5
240};
241
242static int gdb_signal_table[] = {
243 -1,
244 -1,
245 TARGET_SIGINT,
246 -1,
247 -1,
248 TARGET_SIGTRAP
249};
bellard8f447cc2006-06-14 15:21:14 +0000250#endif
bellardb4608c02003-06-27 17:34:32 +0000251
aurel32ca587a82008-12-18 22:44:13 +0000252#ifdef CONFIG_USER_ONLY
253static int target_signal_to_gdb (int sig)
254{
255 int i;
256 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
257 if (gdb_signal_table[i] == sig)
258 return i;
259 return GDB_SIGNAL_UNKNOWN;
260}
261#endif
262
263static int gdb_signal_to_target (int sig)
264{
265 if (sig < ARRAY_SIZE (gdb_signal_table))
266 return gdb_signal_table[sig];
267 else
268 return -1;
269}
270
bellard4abe6152003-07-26 18:01:58 +0000271//#define DEBUG_GDB
bellardb4608c02003-06-27 17:34:32 +0000272
pbrook56aebc82008-10-11 17:55:29 +0000273typedef struct GDBRegisterState {
274 int base_reg;
275 int num_regs;
276 gdb_reg_cb get_reg;
277 gdb_reg_cb set_reg;
278 const char *xml;
279 struct GDBRegisterState *next;
280} GDBRegisterState;
281
bellard858693c2004-03-31 18:52:07 +0000282enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000283 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000284 RS_IDLE,
285 RS_GETLINE,
286 RS_CHKSUM1,
287 RS_CHKSUM2,
288};
bellard858693c2004-03-31 18:52:07 +0000289typedef struct GDBState {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200290 CPUState *c_cpu; /* current CPU for step/continue ops */
291 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200292 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000293 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000294 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000295 int line_buf_index;
296 int line_csum;
pbrook56aebc82008-10-11 17:55:29 +0000297 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000298 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000299 int signal;
bellard41625032005-04-24 10:07:11 +0000300#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000301 int fd;
bellard41625032005-04-24 10:07:11 +0000302 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000303#else
304 CharDriverState *chr;
aliguori8a34a0f2009-03-05 23:01:55 +0000305 CharDriverState *mon_chr;
bellard41625032005-04-24 10:07:11 +0000306#endif
Meador Ingecdb432b2012-03-15 17:49:45 +0000307 char syscall_buf[256];
308 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000309} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000310
edgar_igl60897d32008-05-09 08:25:14 +0000311/* By default use no IRQs and no timers while single stepping so as to
312 * make single stepping like an ICE HW step.
313 */
314static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
315
aliguori880a7572008-11-18 20:30:24 +0000316static GDBState *gdbserver_state;
317
Andreas Färber5b50e792013-06-29 04:18:45 +0200318bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000319
bellard1fddef42005-04-17 19:16:13 +0000320#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000321/* XXX: This is not thread safe. Do we care? */
322static int gdbserver_fd = -1;
323
bellard858693c2004-03-31 18:52:07 +0000324static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000325{
326 uint8_t ch;
327 int ret;
328
329 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000330 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000331 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000332 if (errno == ECONNRESET)
333 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000334 if (errno != EINTR && errno != EAGAIN)
335 return -1;
336 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000337 close(s->fd);
338 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000339 return -1;
340 } else {
341 break;
342 }
343 }
344 return ch;
345}
pbrook4046d912007-01-28 01:53:16 +0000346#endif
bellardb4608c02003-06-27 17:34:32 +0000347
blueswir1654efcf2009-04-18 07:29:59 +0000348static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000349 GDB_SYS_UNKNOWN,
350 GDB_SYS_ENABLED,
351 GDB_SYS_DISABLED,
352} gdb_syscall_mode;
353
354/* If gdb is connected when the first semihosting syscall occurs then use
355 remote gdb syscalls. Otherwise use native file IO. */
356int use_gdb_syscalls(void)
357{
358 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000359 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
360 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000361 }
362 return gdb_syscall_mode == GDB_SYS_ENABLED;
363}
364
edgar_iglba70a622008-03-14 06:10:42 +0000365/* Resume execution. */
366static inline void gdb_continue(GDBState *s)
367{
368#ifdef CONFIG_USER_ONLY
369 s->running_state = 1;
370#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200371 if (!runstate_needs_reset()) {
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200372 vm_start();
373 }
edgar_iglba70a622008-03-14 06:10:42 +0000374#endif
375}
376
bellard858693c2004-03-31 18:52:07 +0000377static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000378{
pbrook4046d912007-01-28 01:53:16 +0000379#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000380 int ret;
381
382 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000383 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000384 if (ret < 0) {
385 if (errno != EINTR && errno != EAGAIN)
386 return;
387 } else {
388 buf += ret;
389 len -= ret;
390 }
391 }
pbrook4046d912007-01-28 01:53:16 +0000392#else
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500393 qemu_chr_fe_write(s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000394#endif
bellardb4608c02003-06-27 17:34:32 +0000395}
396
397static inline int fromhex(int v)
398{
399 if (v >= '0' && v <= '9')
400 return v - '0';
401 else if (v >= 'A' && v <= 'F')
402 return v - 'A' + 10;
403 else if (v >= 'a' && v <= 'f')
404 return v - 'a' + 10;
405 else
406 return 0;
407}
408
409static inline int tohex(int v)
410{
411 if (v < 10)
412 return v + '0';
413 else
414 return v - 10 + 'a';
415}
416
417static void memtohex(char *buf, const uint8_t *mem, int len)
418{
419 int i, c;
420 char *q;
421 q = buf;
422 for(i = 0; i < len; i++) {
423 c = mem[i];
424 *q++ = tohex(c >> 4);
425 *q++ = tohex(c & 0xf);
426 }
427 *q = '\0';
428}
429
430static void hextomem(uint8_t *mem, const char *buf, int len)
431{
432 int i;
433
434 for(i = 0; i < len; i++) {
435 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
436 buf += 2;
437 }
438}
439
bellardb4608c02003-06-27 17:34:32 +0000440/* return -1 if error, 0 if OK */
pbrook56aebc82008-10-11 17:55:29 +0000441static int put_packet_binary(GDBState *s, const char *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000442{
pbrook56aebc82008-10-11 17:55:29 +0000443 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000444 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000445
bellardb4608c02003-06-27 17:34:32 +0000446 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000447 p = s->last_packet;
448 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000449 memcpy(p, buf, len);
450 p += len;
bellardb4608c02003-06-27 17:34:32 +0000451 csum = 0;
452 for(i = 0; i < len; i++) {
453 csum += buf[i];
454 }
pbrook4046d912007-01-28 01:53:16 +0000455 *(p++) = '#';
456 *(p++) = tohex((csum >> 4) & 0xf);
457 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000458
pbrook4046d912007-01-28 01:53:16 +0000459 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000460 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000461
pbrook4046d912007-01-28 01:53:16 +0000462#ifdef CONFIG_USER_ONLY
463 i = get_char(s);
464 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000465 return -1;
pbrook4046d912007-01-28 01:53:16 +0000466 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000467 break;
pbrook4046d912007-01-28 01:53:16 +0000468#else
469 break;
470#endif
bellardb4608c02003-06-27 17:34:32 +0000471 }
472 return 0;
473}
474
pbrook56aebc82008-10-11 17:55:29 +0000475/* return -1 if error, 0 if OK */
476static int put_packet(GDBState *s, const char *buf)
477{
478#ifdef DEBUG_GDB
479 printf("reply='%s'\n", buf);
480#endif
481
482 return put_packet_binary(s, buf, strlen(buf));
483}
484
pbrook56aebc82008-10-11 17:55:29 +0000485/* Encode data using the encoding for 'x' packets. */
486static int memtox(char *buf, const char *mem, int len)
487{
488 char *p = buf;
489 char c;
490
491 while (len--) {
492 c = *(mem++);
493 switch (c) {
494 case '#': case '$': case '*': case '}':
495 *(p++) = '}';
496 *(p++) = c ^ 0x20;
497 break;
498 default:
499 *(p++) = c;
500 break;
501 }
502 }
503 return p - buf;
504}
505
Andreas Färber5b24c642013-07-07 15:08:22 +0200506static const char *get_feature_xml(const char *p, const char **newp,
507 CPUClass *cc)
pbrook56aebc82008-10-11 17:55:29 +0000508{
pbrook56aebc82008-10-11 17:55:29 +0000509 size_t len;
510 int i;
511 const char *name;
512 static char target_xml[1024];
513
514 len = 0;
515 while (p[len] && p[len] != ':')
516 len++;
517 *newp = p + len;
518
519 name = NULL;
520 if (strncmp(p, "target.xml", len) == 0) {
521 /* Generate the XML description for this CPU. */
522 if (!target_xml[0]) {
523 GDBRegisterState *r;
Andreas Färbereac8b352013-06-28 21:11:37 +0200524 CPUState *cpu = first_cpu;
pbrook56aebc82008-10-11 17:55:29 +0000525
blueswir15b3715b2008-10-25 11:18:12 +0000526 snprintf(target_xml, sizeof(target_xml),
527 "<?xml version=\"1.0\"?>"
528 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
529 "<target>"
530 "<xi:include href=\"%s\"/>",
Andreas Färber5b24c642013-07-07 15:08:22 +0200531 cc->gdb_core_xml_file);
pbrook56aebc82008-10-11 17:55:29 +0000532
Andreas Färbereac8b352013-06-28 21:11:37 +0200533 for (r = cpu->gdb_regs; r; r = r->next) {
blueswir12dc766d2009-04-13 16:06:19 +0000534 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
535 pstrcat(target_xml, sizeof(target_xml), r->xml);
536 pstrcat(target_xml, sizeof(target_xml), "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000537 }
blueswir12dc766d2009-04-13 16:06:19 +0000538 pstrcat(target_xml, sizeof(target_xml), "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000539 }
540 return target_xml;
541 }
542 for (i = 0; ; i++) {
543 name = xml_builtin[i][0];
544 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
545 break;
546 }
547 return name ? xml_builtin[i][1] : NULL;
548}
pbrook56aebc82008-10-11 17:55:29 +0000549
Andreas Färber385b9f02013-06-27 18:25:36 +0200550static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000551{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200552 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200553 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000554 GDBRegisterState *r;
555
Andreas Färbera0e372f2013-06-28 23:18:47 +0200556 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200557 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200558 }
pbrook56aebc82008-10-11 17:55:29 +0000559
Andreas Färbereac8b352013-06-28 21:11:37 +0200560 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000561 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
562 return r->get_reg(env, mem_buf, reg - r->base_reg);
563 }
564 }
565 return 0;
566}
567
Andreas Färber385b9f02013-06-27 18:25:36 +0200568static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000569{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200570 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200571 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000572 GDBRegisterState *r;
573
Andreas Färbera0e372f2013-06-28 23:18:47 +0200574 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200575 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200576 }
pbrook56aebc82008-10-11 17:55:29 +0000577
Andreas Färbereac8b352013-06-28 21:11:37 +0200578 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000579 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
580 return r->set_reg(env, mem_buf, reg - r->base_reg);
581 }
582 }
583 return 0;
584}
585
586/* Register a supplemental set of CPU registers. If g_pos is nonzero it
587 specifies the first register number and these registers are included in
588 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
589 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
590 */
591
Andreas Färber22169d42013-06-28 21:27:39 +0200592void gdb_register_coprocessor(CPUState *cpu,
593 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
594 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000595{
596 GDBRegisterState *s;
597 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000598
Andreas Färbereac8b352013-06-28 21:11:37 +0200599 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000600 while (*p) {
601 /* Check for duplicates. */
602 if (strcmp((*p)->xml, xml) == 0)
603 return;
604 p = &(*p)->next;
605 }
Stefan Weil9643c252011-10-18 22:25:38 +0200606
607 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200608 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200609 s->num_regs = num_regs;
610 s->get_reg = get_reg;
611 s->set_reg = set_reg;
612 s->xml = xml;
613
pbrook56aebc82008-10-11 17:55:29 +0000614 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200615 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000616 *p = s;
617 if (g_pos) {
618 if (g_pos != s->base_reg) {
619 fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
620 "Expected %d got %d\n", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200621 } else {
622 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000623 }
624 }
625}
626
aliguoria1d1bb32008-11-18 20:07:32 +0000627#ifndef CONFIG_USER_ONLY
628static const int xlat_gdb_type[] = {
629 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
630 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
631 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
632};
633#endif
634
aliguori880a7572008-11-18 20:30:24 +0000635static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000636{
Andreas Färber182735e2013-05-29 22:29:20 +0200637 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100638 CPUArchState *env;
aliguori880a7572008-11-18 20:30:24 +0000639 int err = 0;
640
Andreas Färber62278812013-06-27 17:12:06 +0200641 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200642 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200643 }
aliguorie22a25c2009-03-12 20:12:48 +0000644
aliguoria1d1bb32008-11-18 20:07:32 +0000645 switch (type) {
646 case GDB_BREAKPOINT_SW:
647 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200648 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200649 env = cpu->env_ptr;
aliguori880a7572008-11-18 20:30:24 +0000650 err = cpu_breakpoint_insert(env, addr, BP_GDB, NULL);
651 if (err)
652 break;
653 }
654 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000655#ifndef CONFIG_USER_ONLY
656 case GDB_WATCHPOINT_WRITE:
657 case GDB_WATCHPOINT_READ:
658 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200659 CPU_FOREACH(cpu) {
Andreas Färber75a34032013-09-02 16:57:02 +0200660 err = cpu_watchpoint_insert(cpu, addr, len, xlat_gdb_type[type],
aliguori880a7572008-11-18 20:30:24 +0000661 NULL);
662 if (err)
663 break;
664 }
665 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000666#endif
667 default:
668 return -ENOSYS;
669 }
670}
671
aliguori880a7572008-11-18 20:30:24 +0000672static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000673{
Andreas Färber182735e2013-05-29 22:29:20 +0200674 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100675 CPUArchState *env;
aliguori880a7572008-11-18 20:30:24 +0000676 int err = 0;
677
Andreas Färber62278812013-06-27 17:12:06 +0200678 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200679 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200680 }
aliguorie22a25c2009-03-12 20:12:48 +0000681
aliguoria1d1bb32008-11-18 20:07:32 +0000682 switch (type) {
683 case GDB_BREAKPOINT_SW:
684 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200685 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200686 env = cpu->env_ptr;
aliguori880a7572008-11-18 20:30:24 +0000687 err = cpu_breakpoint_remove(env, addr, BP_GDB);
688 if (err)
689 break;
690 }
691 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000692#ifndef CONFIG_USER_ONLY
693 case GDB_WATCHPOINT_WRITE:
694 case GDB_WATCHPOINT_READ:
695 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200696 CPU_FOREACH(cpu) {
Andreas Färber75a34032013-09-02 16:57:02 +0200697 err = cpu_watchpoint_remove(cpu, addr, len, xlat_gdb_type[type]);
aliguori880a7572008-11-18 20:30:24 +0000698 if (err)
699 break;
700 }
701 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000702#endif
703 default:
704 return -ENOSYS;
705 }
706}
707
aliguori880a7572008-11-18 20:30:24 +0000708static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +0000709{
Andreas Färber182735e2013-05-29 22:29:20 +0200710 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100711 CPUArchState *env;
aliguori880a7572008-11-18 20:30:24 +0000712
aliguorie22a25c2009-03-12 20:12:48 +0000713 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200714 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +0000715 return;
716 }
717
Andreas Färberbdc44642013-06-24 23:50:24 +0200718 CPU_FOREACH(cpu) {
Andreas Färber182735e2013-05-29 22:29:20 +0200719 env = cpu->env_ptr;
aliguori880a7572008-11-18 20:30:24 +0000720 cpu_breakpoint_remove_all(env, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000721#ifndef CONFIG_USER_ONLY
Andreas Färber75a34032013-09-02 16:57:02 +0200722 cpu_watchpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000723#endif
aliguori880a7572008-11-18 20:30:24 +0000724 }
aliguoria1d1bb32008-11-18 20:07:32 +0000725}
726
aurel32fab9d282009-04-08 21:29:37 +0000727static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
728{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200729 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +0200730 CPUClass *cc = CPU_GET_CLASS(cpu);
731
732 cpu_synchronize_state(cpu);
733 if (cc->set_pc) {
734 cc->set_pc(cpu, pc);
Nathan Froydff1d1972009-12-08 08:06:30 -0800735 }
aurel32fab9d282009-04-08 21:29:37 +0000736}
737
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200738static CPUState *find_cpu(uint32_t thread_id)
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700739{
Andreas Färber0d342822012-12-17 07:12:13 +0100740 CPUState *cpu;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700741
Andreas Färberbdc44642013-06-24 23:50:24 +0200742 CPU_FOREACH(cpu) {
Andreas Färberaa48dd92013-07-09 20:50:52 +0200743 if (cpu_index(cpu) == thread_id) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200744 return cpu;
Andreas Färberaa48dd92013-07-09 20:50:52 +0200745 }
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700746 }
Andreas Färberaa48dd92013-07-09 20:50:52 +0200747
748 return NULL;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700749}
750
aliguori880a7572008-11-18 20:30:24 +0000751static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +0000752{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200753 CPUState *cpu;
Andreas Färber5b24c642013-07-07 15:08:22 +0200754 CPUClass *cc;
bellardb4608c02003-06-27 17:34:32 +0000755 const char *p;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700756 uint32_t thread;
757 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +0000758 char buf[MAX_PACKET_LENGTH];
759 uint8_t mem_buf[MAX_PACKET_LENGTH];
760 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +0000761 target_ulong addr, len;
ths3b46e622007-09-17 08:09:54 +0000762
bellard858693c2004-03-31 18:52:07 +0000763#ifdef DEBUG_GDB
764 printf("command='%s'\n", line_buf);
bellard4c3a88a2003-07-26 12:06:08 +0000765#endif
bellard858693c2004-03-31 18:52:07 +0000766 p = line_buf;
767 ch = *p++;
768 switch(ch) {
769 case '?':
bellard1fddef42005-04-17 19:16:13 +0000770 /* TODO: Make this return the correct value for user-mode. */
aurel32ca587a82008-12-18 22:44:13 +0000771 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200772 cpu_index(s->c_cpu));
bellard858693c2004-03-31 18:52:07 +0000773 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +0000774 /* Remove all the breakpoints when this query is issued,
775 * because gdb is doing and initial connect and the state
776 * should be cleaned up.
777 */
aliguori880a7572008-11-18 20:30:24 +0000778 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +0000779 break;
780 case 'c':
781 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +0000782 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +0000783 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +0000784 }
aurel32ca587a82008-12-18 22:44:13 +0000785 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +0000786 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +0000787 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +0000788 case 'C':
aurel32ca587a82008-12-18 22:44:13 +0000789 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
790 if (s->signal == -1)
791 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +0000792 gdb_continue(s);
793 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200794 case 'v':
795 if (strncmp(p, "Cont", 4) == 0) {
796 int res_signal, res_thread;
797
798 p += 4;
799 if (*p == '?') {
800 put_packet(s, "vCont;c;C;s;S");
801 break;
802 }
803 res = 0;
804 res_signal = 0;
805 res_thread = 0;
806 while (*p) {
807 int action, signal;
808
809 if (*p++ != ';') {
810 res = 0;
811 break;
812 }
813 action = *p++;
814 signal = 0;
815 if (action == 'C' || action == 'S') {
816 signal = strtoul(p, (char **)&p, 16);
817 } else if (action != 'c' && action != 's') {
818 res = 0;
819 break;
820 }
821 thread = 0;
822 if (*p == ':') {
823 thread = strtoull(p+1, (char **)&p, 16);
824 }
825 action = tolower(action);
826 if (res == 0 || (res == 'c' && action == 's')) {
827 res = action;
828 res_signal = signal;
829 res_thread = thread;
830 }
831 }
832 if (res) {
833 if (res_thread != -1 && res_thread != 0) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200834 cpu = find_cpu(res_thread);
835 if (cpu == NULL) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200836 put_packet(s, "E22");
837 break;
838 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200839 s->c_cpu = cpu;
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200840 }
841 if (res == 's') {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200842 cpu_single_step(s->c_cpu, sstep_flags);
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200843 }
844 s->signal = res_signal;
845 gdb_continue(s);
846 return RS_IDLE;
847 }
848 break;
849 } else {
850 goto unknown_command;
851 }
edgar_igl7d03f822008-05-17 18:58:29 +0000852 case 'k':
Jan Kiszka00e94db2012-03-06 18:32:35 +0100853#ifdef CONFIG_USER_ONLY
edgar_igl7d03f822008-05-17 18:58:29 +0000854 /* Kill the target */
855 fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
856 exit(0);
Jan Kiszka00e94db2012-03-06 18:32:35 +0100857#endif
edgar_igl7d03f822008-05-17 18:58:29 +0000858 case 'D':
859 /* Detach packet */
aliguori880a7572008-11-18 20:30:24 +0000860 gdb_breakpoint_remove_all();
Daniel Gutson7ea06da2010-02-26 14:13:50 -0300861 gdb_syscall_mode = GDB_SYS_DISABLED;
edgar_igl7d03f822008-05-17 18:58:29 +0000862 gdb_continue(s);
863 put_packet(s, "OK");
864 break;
bellard858693c2004-03-31 18:52:07 +0000865 case 's':
866 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +0000867 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +0000868 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +0000869 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200870 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +0000871 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +0000872 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +0000873 case 'F':
874 {
875 target_ulong ret;
876 target_ulong err;
877
878 ret = strtoull(p, (char **)&p, 16);
879 if (*p == ',') {
880 p++;
881 err = strtoull(p, (char **)&p, 16);
882 } else {
883 err = 0;
884 }
885 if (*p == ',')
886 p++;
887 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +0000888 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200889 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +0000890 s->current_syscall_cb = NULL;
891 }
pbrooka2d1eba2007-01-28 03:10:55 +0000892 if (type == 'C') {
893 put_packet(s, "T02");
894 } else {
edgar_iglba70a622008-03-14 06:10:42 +0000895 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +0000896 }
897 }
898 break;
bellard858693c2004-03-31 18:52:07 +0000899 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200900 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +0000901 len = 0;
Andreas Färber35143f02013-08-12 18:09:47 +0200902 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200903 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +0000904 len += reg_size;
905 }
906 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +0000907 put_packet(s, buf);
908 break;
909 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200910 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +0000911 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +0000912 len = strlen(p) / 2;
913 hextomem((uint8_t *)registers, p, len);
Andreas Färber35143f02013-08-12 18:09:47 +0200914 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200915 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +0000916 len -= reg_size;
917 registers += reg_size;
918 }
bellard858693c2004-03-31 18:52:07 +0000919 put_packet(s, "OK");
920 break;
921 case 'm':
bellard9d9754a2006-06-25 15:32:37 +0000922 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +0000923 if (*p == ',')
924 p++;
bellard9d9754a2006-06-25 15:32:37 +0000925 len = strtoull(p, NULL, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200926 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +0000927 put_packet (s, "E14");
928 } else {
929 memtohex(buf, mem_buf, len);
930 put_packet(s, buf);
931 }
bellard858693c2004-03-31 18:52:07 +0000932 break;
933 case 'M':
bellard9d9754a2006-06-25 15:32:37 +0000934 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +0000935 if (*p == ',')
936 p++;
bellard9d9754a2006-06-25 15:32:37 +0000937 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +0000938 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +0000939 p++;
940 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200941 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +0200942 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +0000943 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +0200944 } else {
bellard858693c2004-03-31 18:52:07 +0000945 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +0200946 }
bellard858693c2004-03-31 18:52:07 +0000947 break;
pbrook56aebc82008-10-11 17:55:29 +0000948 case 'p':
949 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
950 This works, but can be very slow. Anything new enough to
951 understand XML also knows how to use this properly. */
952 if (!gdb_has_xml)
953 goto unknown_command;
954 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200955 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +0000956 if (reg_size) {
957 memtohex(buf, mem_buf, reg_size);
958 put_packet(s, buf);
959 } else {
960 put_packet(s, "E14");
961 }
962 break;
963 case 'P':
964 if (!gdb_has_xml)
965 goto unknown_command;
966 addr = strtoull(p, (char **)&p, 16);
967 if (*p == '=')
968 p++;
969 reg_size = strlen(p) / 2;
970 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200971 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +0000972 put_packet(s, "OK");
973 break;
bellard858693c2004-03-31 18:52:07 +0000974 case 'Z':
bellard858693c2004-03-31 18:52:07 +0000975 case 'z':
976 type = strtoul(p, (char **)&p, 16);
977 if (*p == ',')
978 p++;
bellard9d9754a2006-06-25 15:32:37 +0000979 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +0000980 if (*p == ',')
981 p++;
bellard9d9754a2006-06-25 15:32:37 +0000982 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +0000983 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +0000984 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +0000985 else
aliguori880a7572008-11-18 20:30:24 +0000986 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +0000987 if (res >= 0)
988 put_packet(s, "OK");
989 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +0000990 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +0000991 else
992 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +0000993 break;
aliguori880a7572008-11-18 20:30:24 +0000994 case 'H':
995 type = *p++;
996 thread = strtoull(p, (char **)&p, 16);
997 if (thread == -1 || thread == 0) {
998 put_packet(s, "OK");
999 break;
1000 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001001 cpu = find_cpu(thread);
1002 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001003 put_packet(s, "E22");
1004 break;
1005 }
1006 switch (type) {
1007 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001008 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001009 put_packet(s, "OK");
1010 break;
1011 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001012 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001013 put_packet(s, "OK");
1014 break;
1015 default:
1016 put_packet(s, "E22");
1017 break;
1018 }
1019 break;
1020 case 'T':
1021 thread = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001022 cpu = find_cpu(thread);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001023
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001024 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001025 put_packet(s, "OK");
1026 } else {
aliguori880a7572008-11-18 20:30:24 +00001027 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001028 }
aliguori880a7572008-11-18 20:30:24 +00001029 break;
pbrook978efd62006-06-17 18:30:42 +00001030 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001031 case 'Q':
1032 /* parse any 'q' packets here */
1033 if (!strcmp(p,"qemu.sstepbits")) {
1034 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001035 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1036 SSTEP_ENABLE,
1037 SSTEP_NOIRQ,
1038 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001039 put_packet(s, buf);
1040 break;
1041 } else if (strncmp(p,"qemu.sstep",10) == 0) {
1042 /* Display or change the sstep_flags */
1043 p += 10;
1044 if (*p != '=') {
1045 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001046 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001047 put_packet(s, buf);
1048 break;
1049 }
1050 p++;
1051 type = strtoul(p, (char **)&p, 16);
1052 sstep_flags = type;
1053 put_packet(s, "OK");
1054 break;
aliguori880a7572008-11-18 20:30:24 +00001055 } else if (strcmp(p,"C") == 0) {
1056 /* "Current thread" remains vague in the spec, so always return
1057 * the first CPU (gdb returns the first thread). */
1058 put_packet(s, "QC1");
1059 break;
1060 } else if (strcmp(p,"fThreadInfo") == 0) {
Andreas Färber52f34622013-06-27 13:44:40 +02001061 s->query_cpu = first_cpu;
aliguori880a7572008-11-18 20:30:24 +00001062 goto report_cpuinfo;
1063 } else if (strcmp(p,"sThreadInfo") == 0) {
1064 report_cpuinfo:
1065 if (s->query_cpu) {
Andreas Färber52f34622013-06-27 13:44:40 +02001066 snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu));
aliguori880a7572008-11-18 20:30:24 +00001067 put_packet(s, buf);
Andreas Färberbdc44642013-06-24 23:50:24 +02001068 s->query_cpu = CPU_NEXT(s->query_cpu);
aliguori880a7572008-11-18 20:30:24 +00001069 } else
1070 put_packet(s, "l");
1071 break;
1072 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1073 thread = strtoull(p+16, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001074 cpu = find_cpu(thread);
1075 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001076 cpu_synchronize_state(cpu);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001077 len = snprintf((char *)mem_buf, sizeof(mem_buf),
Andreas Färber55e5c282012-12-17 06:18:02 +01001078 "CPU#%d [%s]", cpu->cpu_index,
Andreas Färber259186a2013-01-17 18:51:17 +01001079 cpu->halted ? "halted " : "running");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001080 memtohex(buf, mem_buf, len);
1081 put_packet(s, buf);
1082 }
aliguori880a7572008-11-18 20:30:24 +00001083 break;
edgar_igl60897d32008-05-09 08:25:14 +00001084 }
blueswir10b8a9882009-03-07 10:51:36 +00001085#ifdef CONFIG_USER_ONLY
edgar_igl60897d32008-05-09 08:25:14 +00001086 else if (strncmp(p, "Offsets", 7) == 0) {
Andreas Färber0429a972013-08-26 18:14:44 +02001087 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001088
blueswir1363a37d2008-08-21 17:58:08 +00001089 snprintf(buf, sizeof(buf),
1090 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1091 ";Bss=" TARGET_ABI_FMT_lx,
1092 ts->info->code_offset,
1093 ts->info->data_offset,
1094 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001095 put_packet(s, buf);
1096 break;
1097 }
blueswir10b8a9882009-03-07 10:51:36 +00001098#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001099 else if (strncmp(p, "Rcmd,", 5) == 0) {
1100 int len = strlen(p + 5);
1101
1102 if ((len % 2) != 0) {
1103 put_packet(s, "E01");
1104 break;
1105 }
1106 hextomem(mem_buf, p + 5, len);
1107 len = len / 2;
1108 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001109 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001110 put_packet(s, "OK");
1111 break;
1112 }
blueswir10b8a9882009-03-07 10:51:36 +00001113#endif /* !CONFIG_USER_ONLY */
pbrook56aebc82008-10-11 17:55:29 +00001114 if (strncmp(p, "Supported", 9) == 0) {
blueswir15b3715b2008-10-25 11:18:12 +00001115 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
Andreas Färber5b24c642013-07-07 15:08:22 +02001116 cc = CPU_GET_CLASS(first_cpu);
1117 if (cc->gdb_core_xml_file != NULL) {
1118 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1119 }
pbrook56aebc82008-10-11 17:55:29 +00001120 put_packet(s, buf);
1121 break;
1122 }
pbrook56aebc82008-10-11 17:55:29 +00001123 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1124 const char *xml;
1125 target_ulong total_len;
1126
Andreas Färber5b24c642013-07-07 15:08:22 +02001127 cc = CPU_GET_CLASS(first_cpu);
1128 if (cc->gdb_core_xml_file == NULL) {
1129 goto unknown_command;
1130 }
1131
Andreas Färber5b50e792013-06-29 04:18:45 +02001132 gdb_has_xml = true;
pbrook56aebc82008-10-11 17:55:29 +00001133 p += 19;
Andreas Färber5b24c642013-07-07 15:08:22 +02001134 xml = get_feature_xml(p, &p, cc);
pbrook56aebc82008-10-11 17:55:29 +00001135 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001136 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001137 put_packet(s, buf);
1138 break;
1139 }
1140
1141 if (*p == ':')
1142 p++;
1143 addr = strtoul(p, (char **)&p, 16);
1144 if (*p == ',')
1145 p++;
1146 len = strtoul(p, (char **)&p, 16);
1147
1148 total_len = strlen(xml);
1149 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001150 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001151 put_packet(s, buf);
1152 break;
1153 }
1154 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1155 len = (MAX_PACKET_LENGTH - 5) / 2;
1156 if (len < total_len - addr) {
1157 buf[0] = 'm';
1158 len = memtox(buf + 1, xml + addr, len);
1159 } else {
1160 buf[0] = 'l';
1161 len = memtox(buf + 1, xml + addr, total_len - addr);
1162 }
1163 put_packet_binary(s, buf, len + 1);
1164 break;
1165 }
pbrook56aebc82008-10-11 17:55:29 +00001166 /* Unrecognised 'q' command. */
1167 goto unknown_command;
1168
bellard858693c2004-03-31 18:52:07 +00001169 default:
pbrook56aebc82008-10-11 17:55:29 +00001170 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001171 /* put empty packet */
1172 buf[0] = '\0';
1173 put_packet(s, buf);
1174 break;
1175 }
1176 return RS_IDLE;
1177}
1178
Andreas Färber64f6b342013-05-27 02:06:09 +02001179void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001180{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001181 gdbserver_state->c_cpu = cpu;
1182 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001183}
1184
bellard1fddef42005-04-17 19:16:13 +00001185#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001186static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001187{
aliguori880a7572008-11-18 20:30:24 +00001188 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001189 CPUArchState *env = s->c_cpu->env_ptr;
1190 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001191 char buf[256];
aliguorid6fc1b32008-11-18 19:55:44 +00001192 const char *type;
bellard858693c2004-03-31 18:52:07 +00001193 int ret;
1194
Meador Ingecdb432b2012-03-15 17:49:45 +00001195 if (running || s->state == RS_INACTIVE) {
1196 return;
1197 }
1198 /* Is there a GDB syscall waiting to be sent? */
1199 if (s->current_syscall_cb) {
1200 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001201 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001202 }
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001203 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001204 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02001205 if (cpu->watchpoint_hit) {
1206 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001207 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001208 type = "r";
1209 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001210 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001211 type = "a";
1212 break;
1213 default:
1214 type = "";
1215 break;
1216 }
aliguori880a7572008-11-18 20:30:24 +00001217 snprintf(buf, sizeof(buf),
1218 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
Andreas Färber0d342822012-12-17 07:12:13 +01001219 GDB_SIGNAL_TRAP, cpu_index(cpu), type,
Andreas Färberff4700b2013-08-26 18:23:18 +02001220 (target_ulong)cpu->watchpoint_hit->vaddr);
1221 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001222 goto send_packet;
pbrook6658ffb2007-03-16 23:58:11 +00001223 }
Jan Kiszka425189a2011-03-22 11:02:09 +01001224 tb_flush(env);
aurel32ca587a82008-12-18 22:44:13 +00001225 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001226 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001227 case RUN_STATE_PAUSED:
aliguori9781e042009-01-22 17:15:29 +00001228 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001229 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001230 case RUN_STATE_SHUTDOWN:
Jan Kiszka425189a2011-03-22 11:02:09 +01001231 ret = GDB_SIGNAL_QUIT;
1232 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001233 case RUN_STATE_IO_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001234 ret = GDB_SIGNAL_IO;
1235 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001236 case RUN_STATE_WATCHDOG:
Jan Kiszka425189a2011-03-22 11:02:09 +01001237 ret = GDB_SIGNAL_ALRM;
1238 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001239 case RUN_STATE_INTERNAL_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001240 ret = GDB_SIGNAL_ABRT;
1241 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001242 case RUN_STATE_SAVE_VM:
1243 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001244 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001245 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001246 ret = GDB_SIGNAL_XCPU;
1247 break;
1248 default:
1249 ret = GDB_SIGNAL_UNKNOWN;
1250 break;
bellardbbeb7b52006-04-23 18:42:15 +00001251 }
Andreas Färber0d342822012-12-17 07:12:13 +01001252 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(cpu));
Jan Kiszka425189a2011-03-22 11:02:09 +01001253
1254send_packet:
bellard858693c2004-03-31 18:52:07 +00001255 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001256
1257 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001258 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001259}
bellard1fddef42005-04-17 19:16:13 +00001260#endif
bellard858693c2004-03-31 18:52:07 +00001261
pbrooka2d1eba2007-01-28 03:10:55 +00001262/* Send a gdb syscall request.
1263 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001264 %x - target_ulong argument printed in hex.
1265 %lx - 64-bit argument printed in hex.
1266 %s - string pointer (target_ulong) and length (int) pair. */
blueswir17ccfb2e2008-09-14 06:45:34 +00001267void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
pbrooka2d1eba2007-01-28 03:10:55 +00001268{
1269 va_list va;
pbrooka2d1eba2007-01-28 03:10:55 +00001270 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001271 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001272 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001273 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001274 GDBState *s;
1275
aliguori880a7572008-11-18 20:30:24 +00001276 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001277 if (!s)
1278 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001279 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001280#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001281 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001282#endif
pbrooka2d1eba2007-01-28 03:10:55 +00001283 va_start(va, fmt);
Meador Ingecdb432b2012-03-15 17:49:45 +00001284 p = s->syscall_buf;
1285 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001286 *(p++) = 'F';
1287 while (*fmt) {
1288 if (*fmt == '%') {
1289 fmt++;
1290 switch (*fmt++) {
1291 case 'x':
1292 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001293 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001294 break;
pbrooka87295e2007-05-26 15:09:38 +00001295 case 'l':
1296 if (*(fmt++) != 'x')
1297 goto bad_format;
1298 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001299 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001300 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001301 case 's':
1302 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001303 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001304 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001305 break;
1306 default:
pbrooka87295e2007-05-26 15:09:38 +00001307 bad_format:
pbrooka2d1eba2007-01-28 03:10:55 +00001308 fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
1309 fmt - 1);
1310 break;
1311 }
1312 } else {
1313 *(p++) = *(fmt++);
1314 }
1315 }
pbrook8a93e022007-08-06 13:19:15 +00001316 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001317 va_end(va);
pbrooka2d1eba2007-01-28 03:10:55 +00001318#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001319 put_packet(s, s->syscall_buf);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001320 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001321#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001322 /* In this case wait to send the syscall packet until notification that
1323 the CPU has stopped. This must be done because if the packet is sent
1324 now the reply from the syscall request could be received while the CPU
1325 is still in the running state, which can cause packets to be dropped
1326 and state transition 'T' packets to be sent while the syscall is still
1327 being processed. */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001328 cpu_exit(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00001329#endif
1330}
1331
bellard6a00d602005-11-21 23:25:50 +00001332static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00001333{
1334 int i, csum;
ths60fe76f2007-12-16 03:02:09 +00001335 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00001336
bellard1fddef42005-04-17 19:16:13 +00001337#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001338 if (s->last_packet_len) {
1339 /* Waiting for a response to the last packet. If we see the start
1340 of a new command then abandon the previous response. */
1341 if (ch == '-') {
1342#ifdef DEBUG_GDB
1343 printf("Got NACK, retransmitting\n");
1344#endif
thsffe8ab82007-12-16 03:16:05 +00001345 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
pbrook4046d912007-01-28 01:53:16 +00001346 }
1347#ifdef DEBUG_GDB
1348 else if (ch == '+')
1349 printf("Got ACK\n");
1350 else
1351 printf("Got '%c' when expecting ACK/NACK\n", ch);
1352#endif
1353 if (ch == '+' || ch == '$')
1354 s->last_packet_len = 0;
1355 if (ch != '$')
1356 return;
1357 }
Luiz Capitulino13548692011-07-29 15:36:43 -03001358 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00001359 /* when the CPU is running, we cannot do anything except stop
1360 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001361 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00001362 } else
bellard1fddef42005-04-17 19:16:13 +00001363#endif
bellard41625032005-04-24 10:07:11 +00001364 {
bellard858693c2004-03-31 18:52:07 +00001365 switch(s->state) {
1366 case RS_IDLE:
1367 if (ch == '$') {
1368 s->line_buf_index = 0;
1369 s->state = RS_GETLINE;
bellard4c3a88a2003-07-26 12:06:08 +00001370 }
1371 break;
bellard858693c2004-03-31 18:52:07 +00001372 case RS_GETLINE:
1373 if (ch == '#') {
1374 s->state = RS_CHKSUM1;
1375 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1376 s->state = RS_IDLE;
1377 } else {
1378 s->line_buf[s->line_buf_index++] = ch;
1379 }
1380 break;
1381 case RS_CHKSUM1:
1382 s->line_buf[s->line_buf_index] = '\0';
1383 s->line_csum = fromhex(ch) << 4;
1384 s->state = RS_CHKSUM2;
1385 break;
1386 case RS_CHKSUM2:
1387 s->line_csum |= fromhex(ch);
1388 csum = 0;
1389 for(i = 0; i < s->line_buf_index; i++) {
1390 csum += s->line_buf[i];
1391 }
1392 if (s->line_csum != (csum & 0xff)) {
ths60fe76f2007-12-16 03:02:09 +00001393 reply = '-';
1394 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00001395 s->state = RS_IDLE;
1396 } else {
ths60fe76f2007-12-16 03:02:09 +00001397 reply = '+';
1398 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00001399 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00001400 }
bellardb4608c02003-06-27 17:34:32 +00001401 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001402 default:
1403 abort();
bellardb4608c02003-06-27 17:34:32 +00001404 }
1405 }
bellard858693c2004-03-31 18:52:07 +00001406}
1407
Paul Brook0e1c9c52010-06-16 13:03:51 +01001408/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001409void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01001410{
1411 GDBState *s;
1412 char buf[4];
1413
1414 s = gdbserver_state;
1415 if (!s) {
1416 return;
1417 }
1418#ifdef CONFIG_USER_ONLY
1419 if (gdbserver_fd < 0 || s->fd < 0) {
1420 return;
1421 }
1422#endif
1423
1424 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
1425 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001426
1427#ifndef CONFIG_USER_ONLY
1428 if (s->chr) {
Anthony Liguori70f24fb2011-08-15 11:17:38 -05001429 qemu_chr_delete(s->chr);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001430 }
1431#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01001432}
1433
bellard1fddef42005-04-17 19:16:13 +00001434#ifdef CONFIG_USER_ONLY
1435int
aurel32ca587a82008-12-18 22:44:13 +00001436gdb_queuesig (void)
1437{
1438 GDBState *s;
1439
1440 s = gdbserver_state;
1441
1442 if (gdbserver_fd < 0 || s->fd < 0)
1443 return 0;
1444 else
1445 return 1;
1446}
1447
1448int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001449gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00001450{
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001451 CPUArchState *env = cpu->env_ptr;
Andreas Färber5ca666c2013-06-24 19:20:57 +02001452 GDBState *s;
1453 char buf[256];
1454 int n;
bellard1fddef42005-04-17 19:16:13 +00001455
Andreas Färber5ca666c2013-06-24 19:20:57 +02001456 s = gdbserver_state;
1457 if (gdbserver_fd < 0 || s->fd < 0) {
1458 return sig;
bellard1fddef42005-04-17 19:16:13 +00001459 }
1460
Andreas Färber5ca666c2013-06-24 19:20:57 +02001461 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001462 cpu_single_step(cpu, 0);
Andreas Färber5ca666c2013-06-24 19:20:57 +02001463 tb_flush(env);
bellard1fddef42005-04-17 19:16:13 +00001464
Andreas Färber5ca666c2013-06-24 19:20:57 +02001465 if (sig != 0) {
1466 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
1467 put_packet(s, buf);
1468 }
1469 /* put_packet() might have detected that the peer terminated the
1470 connection. */
1471 if (s->fd < 0) {
1472 return sig;
1473 }
1474
1475 sig = 0;
1476 s->state = RS_IDLE;
1477 s->running_state = 0;
1478 while (s->running_state == 0) {
1479 n = read(s->fd, buf, 256);
1480 if (n > 0) {
1481 int i;
1482
1483 for (i = 0; i < n; i++) {
1484 gdb_read_byte(s, buf[i]);
1485 }
1486 } else if (n == 0 || errno != EAGAIN) {
1487 /* XXX: Connection closed. Should probably wait for another
1488 connection before continuing. */
1489 return sig;
bellard1fddef42005-04-17 19:16:13 +00001490 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02001491 }
1492 sig = s->signal;
1493 s->signal = 0;
1494 return sig;
bellard1fddef42005-04-17 19:16:13 +00001495}
bellarde9009672005-04-26 20:42:36 +00001496
aurel32ca587a82008-12-18 22:44:13 +00001497/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001498void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00001499{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001500 GDBState *s;
1501 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00001502
Andreas Färber5ca666c2013-06-24 19:20:57 +02001503 s = gdbserver_state;
1504 if (gdbserver_fd < 0 || s->fd < 0) {
1505 return;
1506 }
aurel32ca587a82008-12-18 22:44:13 +00001507
Andreas Färber5ca666c2013-06-24 19:20:57 +02001508 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
1509 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00001510}
bellard1fddef42005-04-17 19:16:13 +00001511
aliguori880a7572008-11-18 20:30:24 +00001512static void gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00001513{
1514 GDBState *s;
1515 struct sockaddr_in sockaddr;
1516 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001517 int fd;
bellard858693c2004-03-31 18:52:07 +00001518
1519 for(;;) {
1520 len = sizeof(sockaddr);
1521 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1522 if (fd < 0 && errno != EINTR) {
1523 perror("accept");
1524 return;
1525 } else if (fd >= 0) {
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001526#ifndef _WIN32
1527 fcntl(fd, F_SETFD, FD_CLOEXEC);
1528#endif
bellard858693c2004-03-31 18:52:07 +00001529 break;
1530 }
1531 }
1532
1533 /* set short latency */
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001534 socket_set_nodelay(fd);
ths3b46e622007-09-17 08:09:54 +00001535
Anthony Liguori7267c092011-08-20 22:09:37 -05001536 s = g_malloc0(sizeof(GDBState));
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001537 s->c_cpu = first_cpu;
1538 s->g_cpu = first_cpu;
bellard858693c2004-03-31 18:52:07 +00001539 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02001540 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00001541
aliguori880a7572008-11-18 20:30:24 +00001542 gdbserver_state = s;
pbrooka2d1eba2007-01-28 03:10:55 +00001543
bellard858693c2004-03-31 18:52:07 +00001544 fcntl(fd, F_SETFL, O_NONBLOCK);
bellard858693c2004-03-31 18:52:07 +00001545}
1546
1547static int gdbserver_open(int port)
1548{
1549 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001550 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00001551
1552 fd = socket(PF_INET, SOCK_STREAM, 0);
1553 if (fd < 0) {
1554 perror("socket");
1555 return -1;
1556 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001557#ifndef _WIN32
1558 fcntl(fd, F_SETFD, FD_CLOEXEC);
1559#endif
bellard858693c2004-03-31 18:52:07 +00001560
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001561 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00001562
1563 sockaddr.sin_family = AF_INET;
1564 sockaddr.sin_port = htons(port);
1565 sockaddr.sin_addr.s_addr = 0;
1566 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1567 if (ret < 0) {
1568 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00001569 close(fd);
bellard858693c2004-03-31 18:52:07 +00001570 return -1;
1571 }
1572 ret = listen(fd, 0);
1573 if (ret < 0) {
1574 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00001575 close(fd);
bellard858693c2004-03-31 18:52:07 +00001576 return -1;
1577 }
bellard858693c2004-03-31 18:52:07 +00001578 return fd;
1579}
1580
1581int gdbserver_start(int port)
1582{
1583 gdbserver_fd = gdbserver_open(port);
1584 if (gdbserver_fd < 0)
1585 return -1;
1586 /* accept connections */
aliguori880a7572008-11-18 20:30:24 +00001587 gdb_accept();
bellardb4608c02003-06-27 17:34:32 +00001588 return 0;
1589}
aurel322b1319c2008-12-18 22:44:04 +00001590
1591/* Disable gdb stub for child processes. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001592void gdbserver_fork(CPUArchState *env)
aurel322b1319c2008-12-18 22:44:04 +00001593{
Andreas Färber75a34032013-09-02 16:57:02 +02001594 CPUState *cpu = ENV_GET_CPU(env);
aurel322b1319c2008-12-18 22:44:04 +00001595 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02001596
1597 if (gdbserver_fd < 0 || s->fd < 0) {
1598 return;
1599 }
aurel322b1319c2008-12-18 22:44:04 +00001600 close(s->fd);
1601 s->fd = -1;
1602 cpu_breakpoint_remove_all(env, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02001603 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00001604}
pbrook4046d912007-01-28 01:53:16 +00001605#else
thsaa1f17c2007-07-11 22:48:58 +00001606static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00001607{
pbrook56aebc82008-10-11 17:55:29 +00001608 /* We can handle an arbitrarily large amount of data.
1609 Pick the maximum packet size, which is as good as anything. */
1610 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00001611}
1612
thsaa1f17c2007-07-11 22:48:58 +00001613static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00001614{
pbrook4046d912007-01-28 01:53:16 +00001615 int i;
1616
1617 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00001618 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00001619 }
1620}
1621
1622static void gdb_chr_event(void *opaque, int event)
1623{
1624 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05301625 case CHR_EVENT_OPENED:
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001626 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02001627 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00001628 break;
1629 default:
1630 break;
1631 }
1632}
1633
aliguori8a34a0f2009-03-05 23:01:55 +00001634static void gdb_monitor_output(GDBState *s, const char *msg, int len)
1635{
1636 char buf[MAX_PACKET_LENGTH];
1637
1638 buf[0] = 'O';
1639 if (len > (MAX_PACKET_LENGTH/2) - 1)
1640 len = (MAX_PACKET_LENGTH/2) - 1;
1641 memtohex(buf + 1, (uint8_t *)msg, len);
1642 put_packet(s, buf);
1643}
1644
1645static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
1646{
1647 const char *p = (const char *)buf;
1648 int max_sz;
1649
1650 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
1651 for (;;) {
1652 if (len <= max_sz) {
1653 gdb_monitor_output(gdbserver_state, p, len);
1654 break;
1655 }
1656 gdb_monitor_output(gdbserver_state, p, max_sz);
1657 p += max_sz;
1658 len -= max_sz;
1659 }
1660 return len;
1661}
1662
aliguori59030a82009-04-05 18:43:41 +00001663#ifndef _WIN32
1664static void gdb_sigterm_handler(int signal)
1665{
Luiz Capitulino13548692011-07-29 15:36:43 -03001666 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001667 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001668 }
aliguori59030a82009-04-05 18:43:41 +00001669}
1670#endif
1671
1672int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00001673{
1674 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00001675 char gdbstub_device_name[128];
aliguori36556b22009-03-28 18:05:53 +00001676 CharDriverState *chr = NULL;
1677 CharDriverState *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00001678
aliguori59030a82009-04-05 18:43:41 +00001679 if (!device)
1680 return -1;
1681 if (strcmp(device, "none") != 0) {
1682 if (strstart(device, "tcp:", NULL)) {
1683 /* enforce required TCP attributes */
1684 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
1685 "%s,nowait,nodelay,server", device);
1686 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00001687 }
aliguori59030a82009-04-05 18:43:41 +00001688#ifndef _WIN32
1689 else if (strcmp(device, "stdio") == 0) {
1690 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00001691
aliguori59030a82009-04-05 18:43:41 +00001692 memset(&act, 0, sizeof(act));
1693 act.sa_handler = gdb_sigterm_handler;
1694 sigaction(SIGINT, &act, NULL);
1695 }
1696#endif
Anthony Liguori27143a42011-08-15 11:17:36 -05001697 chr = qemu_chr_new("gdb", device, NULL);
aliguori36556b22009-03-28 18:05:53 +00001698 if (!chr)
1699 return -1;
1700
Hans de Goede456d6062013-03-27 20:29:40 +01001701 qemu_chr_fe_claim_no_fail(chr);
aliguori36556b22009-03-28 18:05:53 +00001702 qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
1703 gdb_chr_event, NULL);
pbrookcfc34752007-02-22 01:48:01 +00001704 }
1705
aliguori36556b22009-03-28 18:05:53 +00001706 s = gdbserver_state;
1707 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001708 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00001709 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00001710
aliguori36556b22009-03-28 18:05:53 +00001711 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
1712
1713 /* Initialize a monitor terminal for gdb */
Anthony Liguori7267c092011-08-20 22:09:37 -05001714 mon_chr = g_malloc0(sizeof(*mon_chr));
aliguori36556b22009-03-28 18:05:53 +00001715 mon_chr->chr_write = gdb_monitor_write;
1716 monitor_init(mon_chr, 0);
1717 } else {
1718 if (s->chr)
Anthony Liguori70f24fb2011-08-15 11:17:38 -05001719 qemu_chr_delete(s->chr);
aliguori36556b22009-03-28 18:05:53 +00001720 mon_chr = s->mon_chr;
1721 memset(s, 0, sizeof(GDBState));
1722 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001723 s->c_cpu = first_cpu;
1724 s->g_cpu = first_cpu;
pbrook4046d912007-01-28 01:53:16 +00001725 s->chr = chr;
aliguori36556b22009-03-28 18:05:53 +00001726 s->state = chr ? RS_IDLE : RS_INACTIVE;
1727 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00001728 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00001729
pbrook4046d912007-01-28 01:53:16 +00001730 return 0;
1731}
1732#endif