blob: 1af25a6fe6332f89cdbe57b1a2b24e3f2961ff95 [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 Bonzinibc7d0e62013-06-03 17:06:55 +0200371 if (runstate_check(RUN_STATE_GUEST_PANICKED)) {
372 runstate_set(RUN_STATE_DEBUG);
373 }
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200374 if (!runstate_needs_reset()) {
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200375 vm_start();
376 }
edgar_iglba70a622008-03-14 06:10:42 +0000377#endif
378}
379
bellard858693c2004-03-31 18:52:07 +0000380static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000381{
pbrook4046d912007-01-28 01:53:16 +0000382#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000383 int ret;
384
385 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000386 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000387 if (ret < 0) {
388 if (errno != EINTR && errno != EAGAIN)
389 return;
390 } else {
391 buf += ret;
392 len -= ret;
393 }
394 }
pbrook4046d912007-01-28 01:53:16 +0000395#else
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500396 qemu_chr_fe_write(s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000397#endif
bellardb4608c02003-06-27 17:34:32 +0000398}
399
400static inline int fromhex(int v)
401{
402 if (v >= '0' && v <= '9')
403 return v - '0';
404 else if (v >= 'A' && v <= 'F')
405 return v - 'A' + 10;
406 else if (v >= 'a' && v <= 'f')
407 return v - 'a' + 10;
408 else
409 return 0;
410}
411
412static inline int tohex(int v)
413{
414 if (v < 10)
415 return v + '0';
416 else
417 return v - 10 + 'a';
418}
419
420static void memtohex(char *buf, const uint8_t *mem, int len)
421{
422 int i, c;
423 char *q;
424 q = buf;
425 for(i = 0; i < len; i++) {
426 c = mem[i];
427 *q++ = tohex(c >> 4);
428 *q++ = tohex(c & 0xf);
429 }
430 *q = '\0';
431}
432
433static void hextomem(uint8_t *mem, const char *buf, int len)
434{
435 int i;
436
437 for(i = 0; i < len; i++) {
438 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
439 buf += 2;
440 }
441}
442
bellardb4608c02003-06-27 17:34:32 +0000443/* return -1 if error, 0 if OK */
pbrook56aebc82008-10-11 17:55:29 +0000444static int put_packet_binary(GDBState *s, const char *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000445{
pbrook56aebc82008-10-11 17:55:29 +0000446 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000447 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000448
bellardb4608c02003-06-27 17:34:32 +0000449 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000450 p = s->last_packet;
451 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000452 memcpy(p, buf, len);
453 p += len;
bellardb4608c02003-06-27 17:34:32 +0000454 csum = 0;
455 for(i = 0; i < len; i++) {
456 csum += buf[i];
457 }
pbrook4046d912007-01-28 01:53:16 +0000458 *(p++) = '#';
459 *(p++) = tohex((csum >> 4) & 0xf);
460 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000461
pbrook4046d912007-01-28 01:53:16 +0000462 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000463 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000464
pbrook4046d912007-01-28 01:53:16 +0000465#ifdef CONFIG_USER_ONLY
466 i = get_char(s);
467 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000468 return -1;
pbrook4046d912007-01-28 01:53:16 +0000469 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000470 break;
pbrook4046d912007-01-28 01:53:16 +0000471#else
472 break;
473#endif
bellardb4608c02003-06-27 17:34:32 +0000474 }
475 return 0;
476}
477
pbrook56aebc82008-10-11 17:55:29 +0000478/* return -1 if error, 0 if OK */
479static int put_packet(GDBState *s, const char *buf)
480{
481#ifdef DEBUG_GDB
482 printf("reply='%s'\n", buf);
483#endif
484
485 return put_packet_binary(s, buf, strlen(buf));
486}
487
pbrook56aebc82008-10-11 17:55:29 +0000488/* Encode data using the encoding for 'x' packets. */
489static int memtox(char *buf, const char *mem, int len)
490{
491 char *p = buf;
492 char c;
493
494 while (len--) {
495 c = *(mem++);
496 switch (c) {
497 case '#': case '$': case '*': case '}':
498 *(p++) = '}';
499 *(p++) = c ^ 0x20;
500 break;
501 default:
502 *(p++) = c;
503 break;
504 }
505 }
506 return p - buf;
507}
508
Andreas Färber5b24c642013-07-07 15:08:22 +0200509static const char *get_feature_xml(const char *p, const char **newp,
510 CPUClass *cc)
pbrook56aebc82008-10-11 17:55:29 +0000511{
pbrook56aebc82008-10-11 17:55:29 +0000512 size_t len;
513 int i;
514 const char *name;
515 static char target_xml[1024];
516
517 len = 0;
518 while (p[len] && p[len] != ':')
519 len++;
520 *newp = p + len;
521
522 name = NULL;
523 if (strncmp(p, "target.xml", len) == 0) {
524 /* Generate the XML description for this CPU. */
525 if (!target_xml[0]) {
526 GDBRegisterState *r;
Andreas Färbereac8b352013-06-28 21:11:37 +0200527 CPUState *cpu = first_cpu;
pbrook56aebc82008-10-11 17:55:29 +0000528
blueswir15b3715b2008-10-25 11:18:12 +0000529 snprintf(target_xml, sizeof(target_xml),
530 "<?xml version=\"1.0\"?>"
531 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
532 "<target>"
533 "<xi:include href=\"%s\"/>",
Andreas Färber5b24c642013-07-07 15:08:22 +0200534 cc->gdb_core_xml_file);
pbrook56aebc82008-10-11 17:55:29 +0000535
Andreas Färbereac8b352013-06-28 21:11:37 +0200536 for (r = cpu->gdb_regs; r; r = r->next) {
blueswir12dc766d2009-04-13 16:06:19 +0000537 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
538 pstrcat(target_xml, sizeof(target_xml), r->xml);
539 pstrcat(target_xml, sizeof(target_xml), "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000540 }
blueswir12dc766d2009-04-13 16:06:19 +0000541 pstrcat(target_xml, sizeof(target_xml), "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000542 }
543 return target_xml;
544 }
545 for (i = 0; ; i++) {
546 name = xml_builtin[i][0];
547 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
548 break;
549 }
550 return name ? xml_builtin[i][1] : NULL;
551}
pbrook56aebc82008-10-11 17:55:29 +0000552
Andreas Färber385b9f02013-06-27 18:25:36 +0200553static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000554{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200555 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200556 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000557 GDBRegisterState *r;
558
Andreas Färbera0e372f2013-06-28 23:18:47 +0200559 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200560 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200561 }
pbrook56aebc82008-10-11 17:55:29 +0000562
Andreas Färbereac8b352013-06-28 21:11:37 +0200563 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000564 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
565 return r->get_reg(env, mem_buf, reg - r->base_reg);
566 }
567 }
568 return 0;
569}
570
Andreas Färber385b9f02013-06-27 18:25:36 +0200571static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000572{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200573 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200574 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000575 GDBRegisterState *r;
576
Andreas Färbera0e372f2013-06-28 23:18:47 +0200577 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200578 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200579 }
pbrook56aebc82008-10-11 17:55:29 +0000580
Andreas Färbereac8b352013-06-28 21:11:37 +0200581 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000582 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
583 return r->set_reg(env, mem_buf, reg - r->base_reg);
584 }
585 }
586 return 0;
587}
588
589/* Register a supplemental set of CPU registers. If g_pos is nonzero it
590 specifies the first register number and these registers are included in
591 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
592 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
593 */
594
Andreas Färber22169d42013-06-28 21:27:39 +0200595void gdb_register_coprocessor(CPUState *cpu,
596 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
597 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000598{
599 GDBRegisterState *s;
600 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000601
Andreas Färbereac8b352013-06-28 21:11:37 +0200602 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000603 while (*p) {
604 /* Check for duplicates. */
605 if (strcmp((*p)->xml, xml) == 0)
606 return;
607 p = &(*p)->next;
608 }
Stefan Weil9643c252011-10-18 22:25:38 +0200609
610 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200611 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200612 s->num_regs = num_regs;
613 s->get_reg = get_reg;
614 s->set_reg = set_reg;
615 s->xml = xml;
616
pbrook56aebc82008-10-11 17:55:29 +0000617 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200618 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000619 *p = s;
620 if (g_pos) {
621 if (g_pos != s->base_reg) {
622 fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
623 "Expected %d got %d\n", xml, g_pos, s->base_reg);
pbrook56aebc82008-10-11 17:55:29 +0000624 }
625 }
626}
627
aliguoria1d1bb32008-11-18 20:07:32 +0000628#ifndef CONFIG_USER_ONLY
629static const int xlat_gdb_type[] = {
630 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
631 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
632 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
633};
634#endif
635
aliguori880a7572008-11-18 20:30:24 +0000636static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000637{
Andreas Färber182735e2013-05-29 22:29:20 +0200638 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100639 CPUArchState *env;
aliguori880a7572008-11-18 20:30:24 +0000640 int err = 0;
641
Andreas Färber62278812013-06-27 17:12:06 +0200642 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200643 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200644 }
aliguorie22a25c2009-03-12 20:12:48 +0000645
aliguoria1d1bb32008-11-18 20:07:32 +0000646 switch (type) {
647 case GDB_BREAKPOINT_SW:
648 case GDB_BREAKPOINT_HW:
Andreas Färber182735e2013-05-29 22:29:20 +0200649 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
650 env = cpu->env_ptr;
aliguori880a7572008-11-18 20:30:24 +0000651 err = cpu_breakpoint_insert(env, addr, BP_GDB, NULL);
652 if (err)
653 break;
654 }
655 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000656#ifndef CONFIG_USER_ONLY
657 case GDB_WATCHPOINT_WRITE:
658 case GDB_WATCHPOINT_READ:
659 case GDB_WATCHPOINT_ACCESS:
Andreas Färber182735e2013-05-29 22:29:20 +0200660 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
661 env = cpu->env_ptr;
aliguori880a7572008-11-18 20:30:24 +0000662 err = cpu_watchpoint_insert(env, addr, len, xlat_gdb_type[type],
663 NULL);
664 if (err)
665 break;
666 }
667 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000668#endif
669 default:
670 return -ENOSYS;
671 }
672}
673
aliguori880a7572008-11-18 20:30:24 +0000674static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000675{
Andreas Färber182735e2013-05-29 22:29:20 +0200676 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100677 CPUArchState *env;
aliguori880a7572008-11-18 20:30:24 +0000678 int err = 0;
679
Andreas Färber62278812013-06-27 17:12:06 +0200680 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200681 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200682 }
aliguorie22a25c2009-03-12 20:12:48 +0000683
aliguoria1d1bb32008-11-18 20:07:32 +0000684 switch (type) {
685 case GDB_BREAKPOINT_SW:
686 case GDB_BREAKPOINT_HW:
Andreas Färber182735e2013-05-29 22:29:20 +0200687 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
688 env = cpu->env_ptr;
aliguori880a7572008-11-18 20:30:24 +0000689 err = cpu_breakpoint_remove(env, addr, BP_GDB);
690 if (err)
691 break;
692 }
693 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000694#ifndef CONFIG_USER_ONLY
695 case GDB_WATCHPOINT_WRITE:
696 case GDB_WATCHPOINT_READ:
697 case GDB_WATCHPOINT_ACCESS:
Andreas Färber182735e2013-05-29 22:29:20 +0200698 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
699 env = cpu->env_ptr;
aliguori880a7572008-11-18 20:30:24 +0000700 err = cpu_watchpoint_remove(env, addr, len, xlat_gdb_type[type]);
701 if (err)
702 break;
703 }
704 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000705#endif
706 default:
707 return -ENOSYS;
708 }
709}
710
aliguori880a7572008-11-18 20:30:24 +0000711static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +0000712{
Andreas Färber182735e2013-05-29 22:29:20 +0200713 CPUState *cpu;
Andreas Färber9349b4f2012-03-14 01:38:32 +0100714 CPUArchState *env;
aliguori880a7572008-11-18 20:30:24 +0000715
aliguorie22a25c2009-03-12 20:12:48 +0000716 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200717 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +0000718 return;
719 }
720
Andreas Färber182735e2013-05-29 22:29:20 +0200721 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
722 env = cpu->env_ptr;
aliguori880a7572008-11-18 20:30:24 +0000723 cpu_breakpoint_remove_all(env, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000724#ifndef CONFIG_USER_ONLY
aliguori880a7572008-11-18 20:30:24 +0000725 cpu_watchpoint_remove_all(env, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000726#endif
aliguori880a7572008-11-18 20:30:24 +0000727 }
aliguoria1d1bb32008-11-18 20:07:32 +0000728}
729
aurel32fab9d282009-04-08 21:29:37 +0000730static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
731{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200732 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +0200733 CPUClass *cc = CPU_GET_CLASS(cpu);
734
735 cpu_synchronize_state(cpu);
736 if (cc->set_pc) {
737 cc->set_pc(cpu, pc);
Nathan Froydff1d1972009-12-08 08:06:30 -0800738 }
aurel32fab9d282009-04-08 21:29:37 +0000739}
740
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200741static CPUState *find_cpu(uint32_t thread_id)
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700742{
Andreas Färber0d342822012-12-17 07:12:13 +0100743 CPUState *cpu;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700744
Andreas Färber182735e2013-05-29 22:29:20 +0200745 for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
Andreas Färberaa48dd92013-07-09 20:50:52 +0200746 if (cpu_index(cpu) == thread_id) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200747 return cpu;
Andreas Färberaa48dd92013-07-09 20:50:52 +0200748 }
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700749 }
Andreas Färberaa48dd92013-07-09 20:50:52 +0200750
751 return NULL;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700752}
753
aliguori880a7572008-11-18 20:30:24 +0000754static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +0000755{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200756 CPUState *cpu;
Andreas Färber5b24c642013-07-07 15:08:22 +0200757 CPUClass *cc;
bellardb4608c02003-06-27 17:34:32 +0000758 const char *p;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700759 uint32_t thread;
760 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +0000761 char buf[MAX_PACKET_LENGTH];
762 uint8_t mem_buf[MAX_PACKET_LENGTH];
763 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +0000764 target_ulong addr, len;
ths3b46e622007-09-17 08:09:54 +0000765
bellard858693c2004-03-31 18:52:07 +0000766#ifdef DEBUG_GDB
767 printf("command='%s'\n", line_buf);
bellard4c3a88a2003-07-26 12:06:08 +0000768#endif
bellard858693c2004-03-31 18:52:07 +0000769 p = line_buf;
770 ch = *p++;
771 switch(ch) {
772 case '?':
bellard1fddef42005-04-17 19:16:13 +0000773 /* TODO: Make this return the correct value for user-mode. */
aurel32ca587a82008-12-18 22:44:13 +0000774 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200775 cpu_index(s->c_cpu));
bellard858693c2004-03-31 18:52:07 +0000776 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +0000777 /* Remove all the breakpoints when this query is issued,
778 * because gdb is doing and initial connect and the state
779 * should be cleaned up.
780 */
aliguori880a7572008-11-18 20:30:24 +0000781 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +0000782 break;
783 case 'c':
784 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +0000785 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +0000786 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +0000787 }
aurel32ca587a82008-12-18 22:44:13 +0000788 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +0000789 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +0000790 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +0000791 case 'C':
aurel32ca587a82008-12-18 22:44:13 +0000792 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
793 if (s->signal == -1)
794 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +0000795 gdb_continue(s);
796 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200797 case 'v':
798 if (strncmp(p, "Cont", 4) == 0) {
799 int res_signal, res_thread;
800
801 p += 4;
802 if (*p == '?') {
803 put_packet(s, "vCont;c;C;s;S");
804 break;
805 }
806 res = 0;
807 res_signal = 0;
808 res_thread = 0;
809 while (*p) {
810 int action, signal;
811
812 if (*p++ != ';') {
813 res = 0;
814 break;
815 }
816 action = *p++;
817 signal = 0;
818 if (action == 'C' || action == 'S') {
819 signal = strtoul(p, (char **)&p, 16);
820 } else if (action != 'c' && action != 's') {
821 res = 0;
822 break;
823 }
824 thread = 0;
825 if (*p == ':') {
826 thread = strtoull(p+1, (char **)&p, 16);
827 }
828 action = tolower(action);
829 if (res == 0 || (res == 'c' && action == 's')) {
830 res = action;
831 res_signal = signal;
832 res_thread = thread;
833 }
834 }
835 if (res) {
836 if (res_thread != -1 && res_thread != 0) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200837 cpu = find_cpu(res_thread);
838 if (cpu == NULL) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200839 put_packet(s, "E22");
840 break;
841 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200842 s->c_cpu = cpu;
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200843 }
844 if (res == 's') {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200845 cpu_single_step(s->c_cpu, sstep_flags);
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200846 }
847 s->signal = res_signal;
848 gdb_continue(s);
849 return RS_IDLE;
850 }
851 break;
852 } else {
853 goto unknown_command;
854 }
edgar_igl7d03f822008-05-17 18:58:29 +0000855 case 'k':
Jan Kiszka00e94db2012-03-06 18:32:35 +0100856#ifdef CONFIG_USER_ONLY
edgar_igl7d03f822008-05-17 18:58:29 +0000857 /* Kill the target */
858 fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
859 exit(0);
Jan Kiszka00e94db2012-03-06 18:32:35 +0100860#endif
edgar_igl7d03f822008-05-17 18:58:29 +0000861 case 'D':
862 /* Detach packet */
aliguori880a7572008-11-18 20:30:24 +0000863 gdb_breakpoint_remove_all();
Daniel Gutson7ea06da2010-02-26 14:13:50 -0300864 gdb_syscall_mode = GDB_SYS_DISABLED;
edgar_igl7d03f822008-05-17 18:58:29 +0000865 gdb_continue(s);
866 put_packet(s, "OK");
867 break;
bellard858693c2004-03-31 18:52:07 +0000868 case 's':
869 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +0000870 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +0000871 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +0000872 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200873 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +0000874 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +0000875 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +0000876 case 'F':
877 {
878 target_ulong ret;
879 target_ulong err;
880
881 ret = strtoull(p, (char **)&p, 16);
882 if (*p == ',') {
883 p++;
884 err = strtoull(p, (char **)&p, 16);
885 } else {
886 err = 0;
887 }
888 if (*p == ',')
889 p++;
890 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +0000891 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200892 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +0000893 s->current_syscall_cb = NULL;
894 }
pbrooka2d1eba2007-01-28 03:10:55 +0000895 if (type == 'C') {
896 put_packet(s, "T02");
897 } else {
edgar_iglba70a622008-03-14 06:10:42 +0000898 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +0000899 }
900 }
901 break;
bellard858693c2004-03-31 18:52:07 +0000902 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200903 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +0000904 len = 0;
Andreas Färbera0e372f2013-06-28 23:18:47 +0200905 for (addr = 0; addr < s->g_cpu->gdb_num_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200906 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +0000907 len += reg_size;
908 }
909 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +0000910 put_packet(s, buf);
911 break;
912 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200913 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +0000914 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +0000915 len = strlen(p) / 2;
916 hextomem((uint8_t *)registers, p, len);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200917 for (addr = 0; addr < s->g_cpu->gdb_num_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200918 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +0000919 len -= reg_size;
920 registers += reg_size;
921 }
bellard858693c2004-03-31 18:52:07 +0000922 put_packet(s, "OK");
923 break;
924 case 'm':
bellard9d9754a2006-06-25 15:32:37 +0000925 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +0000926 if (*p == ',')
927 p++;
bellard9d9754a2006-06-25 15:32:37 +0000928 len = strtoull(p, NULL, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200929 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +0000930 put_packet (s, "E14");
931 } else {
932 memtohex(buf, mem_buf, len);
933 put_packet(s, buf);
934 }
bellard858693c2004-03-31 18:52:07 +0000935 break;
936 case 'M':
bellard9d9754a2006-06-25 15:32:37 +0000937 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +0000938 if (*p == ',')
939 p++;
bellard9d9754a2006-06-25 15:32:37 +0000940 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +0000941 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +0000942 p++;
943 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200944 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +0200945 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +0000946 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +0200947 } else {
bellard858693c2004-03-31 18:52:07 +0000948 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +0200949 }
bellard858693c2004-03-31 18:52:07 +0000950 break;
pbrook56aebc82008-10-11 17:55:29 +0000951 case 'p':
952 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
953 This works, but can be very slow. Anything new enough to
954 understand XML also knows how to use this properly. */
955 if (!gdb_has_xml)
956 goto unknown_command;
957 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200958 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +0000959 if (reg_size) {
960 memtohex(buf, mem_buf, reg_size);
961 put_packet(s, buf);
962 } else {
963 put_packet(s, "E14");
964 }
965 break;
966 case 'P':
967 if (!gdb_has_xml)
968 goto unknown_command;
969 addr = strtoull(p, (char **)&p, 16);
970 if (*p == '=')
971 p++;
972 reg_size = strlen(p) / 2;
973 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200974 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +0000975 put_packet(s, "OK");
976 break;
bellard858693c2004-03-31 18:52:07 +0000977 case 'Z':
bellard858693c2004-03-31 18:52:07 +0000978 case 'z':
979 type = strtoul(p, (char **)&p, 16);
980 if (*p == ',')
981 p++;
bellard9d9754a2006-06-25 15:32:37 +0000982 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +0000983 if (*p == ',')
984 p++;
bellard9d9754a2006-06-25 15:32:37 +0000985 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +0000986 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +0000987 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +0000988 else
aliguori880a7572008-11-18 20:30:24 +0000989 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +0000990 if (res >= 0)
991 put_packet(s, "OK");
992 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +0000993 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +0000994 else
995 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +0000996 break;
aliguori880a7572008-11-18 20:30:24 +0000997 case 'H':
998 type = *p++;
999 thread = strtoull(p, (char **)&p, 16);
1000 if (thread == -1 || thread == 0) {
1001 put_packet(s, "OK");
1002 break;
1003 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001004 cpu = find_cpu(thread);
1005 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001006 put_packet(s, "E22");
1007 break;
1008 }
1009 switch (type) {
1010 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001011 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001012 put_packet(s, "OK");
1013 break;
1014 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001015 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001016 put_packet(s, "OK");
1017 break;
1018 default:
1019 put_packet(s, "E22");
1020 break;
1021 }
1022 break;
1023 case 'T':
1024 thread = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001025 cpu = find_cpu(thread);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001026
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001027 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001028 put_packet(s, "OK");
1029 } else {
aliguori880a7572008-11-18 20:30:24 +00001030 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001031 }
aliguori880a7572008-11-18 20:30:24 +00001032 break;
pbrook978efd62006-06-17 18:30:42 +00001033 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001034 case 'Q':
1035 /* parse any 'q' packets here */
1036 if (!strcmp(p,"qemu.sstepbits")) {
1037 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001038 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1039 SSTEP_ENABLE,
1040 SSTEP_NOIRQ,
1041 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001042 put_packet(s, buf);
1043 break;
1044 } else if (strncmp(p,"qemu.sstep",10) == 0) {
1045 /* Display or change the sstep_flags */
1046 p += 10;
1047 if (*p != '=') {
1048 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001049 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001050 put_packet(s, buf);
1051 break;
1052 }
1053 p++;
1054 type = strtoul(p, (char **)&p, 16);
1055 sstep_flags = type;
1056 put_packet(s, "OK");
1057 break;
aliguori880a7572008-11-18 20:30:24 +00001058 } else if (strcmp(p,"C") == 0) {
1059 /* "Current thread" remains vague in the spec, so always return
1060 * the first CPU (gdb returns the first thread). */
1061 put_packet(s, "QC1");
1062 break;
1063 } else if (strcmp(p,"fThreadInfo") == 0) {
Andreas Färber52f34622013-06-27 13:44:40 +02001064 s->query_cpu = first_cpu;
aliguori880a7572008-11-18 20:30:24 +00001065 goto report_cpuinfo;
1066 } else if (strcmp(p,"sThreadInfo") == 0) {
1067 report_cpuinfo:
1068 if (s->query_cpu) {
Andreas Färber52f34622013-06-27 13:44:40 +02001069 snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu));
aliguori880a7572008-11-18 20:30:24 +00001070 put_packet(s, buf);
Andreas Färber52f34622013-06-27 13:44:40 +02001071 s->query_cpu = s->query_cpu->next_cpu;
aliguori880a7572008-11-18 20:30:24 +00001072 } else
1073 put_packet(s, "l");
1074 break;
1075 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1076 thread = strtoull(p+16, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001077 cpu = find_cpu(thread);
1078 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001079 cpu_synchronize_state(cpu);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001080 len = snprintf((char *)mem_buf, sizeof(mem_buf),
Andreas Färber55e5c282012-12-17 06:18:02 +01001081 "CPU#%d [%s]", cpu->cpu_index,
Andreas Färber259186a2013-01-17 18:51:17 +01001082 cpu->halted ? "halted " : "running");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001083 memtohex(buf, mem_buf, len);
1084 put_packet(s, buf);
1085 }
aliguori880a7572008-11-18 20:30:24 +00001086 break;
edgar_igl60897d32008-05-09 08:25:14 +00001087 }
blueswir10b8a9882009-03-07 10:51:36 +00001088#ifdef CONFIG_USER_ONLY
edgar_igl60897d32008-05-09 08:25:14 +00001089 else if (strncmp(p, "Offsets", 7) == 0) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001090 CPUArchState *env = s->c_cpu->env_ptr;
1091 TaskState *ts = env->opaque;
pbrook978efd62006-06-17 18:30:42 +00001092
blueswir1363a37d2008-08-21 17:58:08 +00001093 snprintf(buf, sizeof(buf),
1094 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1095 ";Bss=" TARGET_ABI_FMT_lx,
1096 ts->info->code_offset,
1097 ts->info->data_offset,
1098 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001099 put_packet(s, buf);
1100 break;
1101 }
blueswir10b8a9882009-03-07 10:51:36 +00001102#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001103 else if (strncmp(p, "Rcmd,", 5) == 0) {
1104 int len = strlen(p + 5);
1105
1106 if ((len % 2) != 0) {
1107 put_packet(s, "E01");
1108 break;
1109 }
1110 hextomem(mem_buf, p + 5, len);
1111 len = len / 2;
1112 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001113 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001114 put_packet(s, "OK");
1115 break;
1116 }
blueswir10b8a9882009-03-07 10:51:36 +00001117#endif /* !CONFIG_USER_ONLY */
pbrook56aebc82008-10-11 17:55:29 +00001118 if (strncmp(p, "Supported", 9) == 0) {
blueswir15b3715b2008-10-25 11:18:12 +00001119 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
Andreas Färber5b24c642013-07-07 15:08:22 +02001120 cc = CPU_GET_CLASS(first_cpu);
1121 if (cc->gdb_core_xml_file != NULL) {
1122 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1123 }
pbrook56aebc82008-10-11 17:55:29 +00001124 put_packet(s, buf);
1125 break;
1126 }
pbrook56aebc82008-10-11 17:55:29 +00001127 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1128 const char *xml;
1129 target_ulong total_len;
1130
Andreas Färber5b24c642013-07-07 15:08:22 +02001131 cc = CPU_GET_CLASS(first_cpu);
1132 if (cc->gdb_core_xml_file == NULL) {
1133 goto unknown_command;
1134 }
1135
Andreas Färber5b50e792013-06-29 04:18:45 +02001136 gdb_has_xml = true;
pbrook56aebc82008-10-11 17:55:29 +00001137 p += 19;
Andreas Färber5b24c642013-07-07 15:08:22 +02001138 xml = get_feature_xml(p, &p, cc);
pbrook56aebc82008-10-11 17:55:29 +00001139 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001140 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001141 put_packet(s, buf);
1142 break;
1143 }
1144
1145 if (*p == ':')
1146 p++;
1147 addr = strtoul(p, (char **)&p, 16);
1148 if (*p == ',')
1149 p++;
1150 len = strtoul(p, (char **)&p, 16);
1151
1152 total_len = strlen(xml);
1153 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001154 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001155 put_packet(s, buf);
1156 break;
1157 }
1158 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1159 len = (MAX_PACKET_LENGTH - 5) / 2;
1160 if (len < total_len - addr) {
1161 buf[0] = 'm';
1162 len = memtox(buf + 1, xml + addr, len);
1163 } else {
1164 buf[0] = 'l';
1165 len = memtox(buf + 1, xml + addr, total_len - addr);
1166 }
1167 put_packet_binary(s, buf, len + 1);
1168 break;
1169 }
pbrook56aebc82008-10-11 17:55:29 +00001170 /* Unrecognised 'q' command. */
1171 goto unknown_command;
1172
bellard858693c2004-03-31 18:52:07 +00001173 default:
pbrook56aebc82008-10-11 17:55:29 +00001174 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001175 /* put empty packet */
1176 buf[0] = '\0';
1177 put_packet(s, buf);
1178 break;
1179 }
1180 return RS_IDLE;
1181}
1182
Andreas Färber64f6b342013-05-27 02:06:09 +02001183void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001184{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001185 gdbserver_state->c_cpu = cpu;
1186 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001187}
1188
bellard1fddef42005-04-17 19:16:13 +00001189#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001190static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001191{
aliguori880a7572008-11-18 20:30:24 +00001192 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001193 CPUArchState *env = s->c_cpu->env_ptr;
1194 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001195 char buf[256];
aliguorid6fc1b32008-11-18 19:55:44 +00001196 const char *type;
bellard858693c2004-03-31 18:52:07 +00001197 int ret;
1198
Meador Ingecdb432b2012-03-15 17:49:45 +00001199 if (running || s->state == RS_INACTIVE) {
1200 return;
1201 }
1202 /* Is there a GDB syscall waiting to be sent? */
1203 if (s->current_syscall_cb) {
1204 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001205 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001206 }
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001207 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001208 case RUN_STATE_DEBUG:
aliguori880a7572008-11-18 20:30:24 +00001209 if (env->watchpoint_hit) {
1210 switch (env->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001211 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001212 type = "r";
1213 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001214 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001215 type = "a";
1216 break;
1217 default:
1218 type = "";
1219 break;
1220 }
aliguori880a7572008-11-18 20:30:24 +00001221 snprintf(buf, sizeof(buf),
1222 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
Andreas Färber0d342822012-12-17 07:12:13 +01001223 GDB_SIGNAL_TRAP, cpu_index(cpu), type,
aliguori880a7572008-11-18 20:30:24 +00001224 env->watchpoint_hit->vaddr);
aliguori880a7572008-11-18 20:30:24 +00001225 env->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001226 goto send_packet;
pbrook6658ffb2007-03-16 23:58:11 +00001227 }
Jan Kiszka425189a2011-03-22 11:02:09 +01001228 tb_flush(env);
aurel32ca587a82008-12-18 22:44:13 +00001229 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001230 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001231 case RUN_STATE_PAUSED:
aliguori9781e042009-01-22 17:15:29 +00001232 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001233 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001234 case RUN_STATE_SHUTDOWN:
Jan Kiszka425189a2011-03-22 11:02:09 +01001235 ret = GDB_SIGNAL_QUIT;
1236 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001237 case RUN_STATE_IO_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001238 ret = GDB_SIGNAL_IO;
1239 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001240 case RUN_STATE_WATCHDOG:
Jan Kiszka425189a2011-03-22 11:02:09 +01001241 ret = GDB_SIGNAL_ALRM;
1242 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001243 case RUN_STATE_INTERNAL_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001244 ret = GDB_SIGNAL_ABRT;
1245 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001246 case RUN_STATE_SAVE_VM:
1247 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001248 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001249 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001250 ret = GDB_SIGNAL_XCPU;
1251 break;
1252 default:
1253 ret = GDB_SIGNAL_UNKNOWN;
1254 break;
bellardbbeb7b52006-04-23 18:42:15 +00001255 }
Andreas Färber0d342822012-12-17 07:12:13 +01001256 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(cpu));
Jan Kiszka425189a2011-03-22 11:02:09 +01001257
1258send_packet:
bellard858693c2004-03-31 18:52:07 +00001259 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001260
1261 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001262 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001263}
bellard1fddef42005-04-17 19:16:13 +00001264#endif
bellard858693c2004-03-31 18:52:07 +00001265
pbrooka2d1eba2007-01-28 03:10:55 +00001266/* Send a gdb syscall request.
1267 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001268 %x - target_ulong argument printed in hex.
1269 %lx - 64-bit argument printed in hex.
1270 %s - string pointer (target_ulong) and length (int) pair. */
blueswir17ccfb2e2008-09-14 06:45:34 +00001271void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
pbrooka2d1eba2007-01-28 03:10:55 +00001272{
1273 va_list va;
pbrooka2d1eba2007-01-28 03:10:55 +00001274 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001275 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001276 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001277 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001278 GDBState *s;
1279
aliguori880a7572008-11-18 20:30:24 +00001280 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001281 if (!s)
1282 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001283 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001284#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001285 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001286#endif
pbrooka2d1eba2007-01-28 03:10:55 +00001287 va_start(va, fmt);
Meador Ingecdb432b2012-03-15 17:49:45 +00001288 p = s->syscall_buf;
1289 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001290 *(p++) = 'F';
1291 while (*fmt) {
1292 if (*fmt == '%') {
1293 fmt++;
1294 switch (*fmt++) {
1295 case 'x':
1296 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001297 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001298 break;
pbrooka87295e2007-05-26 15:09:38 +00001299 case 'l':
1300 if (*(fmt++) != 'x')
1301 goto bad_format;
1302 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001303 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001304 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001305 case 's':
1306 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001307 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001308 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001309 break;
1310 default:
pbrooka87295e2007-05-26 15:09:38 +00001311 bad_format:
pbrooka2d1eba2007-01-28 03:10:55 +00001312 fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
1313 fmt - 1);
1314 break;
1315 }
1316 } else {
1317 *(p++) = *(fmt++);
1318 }
1319 }
pbrook8a93e022007-08-06 13:19:15 +00001320 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001321 va_end(va);
pbrooka2d1eba2007-01-28 03:10:55 +00001322#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001323 put_packet(s, s->syscall_buf);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001324 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001325#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001326 /* In this case wait to send the syscall packet until notification that
1327 the CPU has stopped. This must be done because if the packet is sent
1328 now the reply from the syscall request could be received while the CPU
1329 is still in the running state, which can cause packets to be dropped
1330 and state transition 'T' packets to be sent while the syscall is still
1331 being processed. */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001332 cpu_exit(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00001333#endif
1334}
1335
bellard6a00d602005-11-21 23:25:50 +00001336static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00001337{
1338 int i, csum;
ths60fe76f2007-12-16 03:02:09 +00001339 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00001340
bellard1fddef42005-04-17 19:16:13 +00001341#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001342 if (s->last_packet_len) {
1343 /* Waiting for a response to the last packet. If we see the start
1344 of a new command then abandon the previous response. */
1345 if (ch == '-') {
1346#ifdef DEBUG_GDB
1347 printf("Got NACK, retransmitting\n");
1348#endif
thsffe8ab82007-12-16 03:16:05 +00001349 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
pbrook4046d912007-01-28 01:53:16 +00001350 }
1351#ifdef DEBUG_GDB
1352 else if (ch == '+')
1353 printf("Got ACK\n");
1354 else
1355 printf("Got '%c' when expecting ACK/NACK\n", ch);
1356#endif
1357 if (ch == '+' || ch == '$')
1358 s->last_packet_len = 0;
1359 if (ch != '$')
1360 return;
1361 }
Luiz Capitulino13548692011-07-29 15:36:43 -03001362 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00001363 /* when the CPU is running, we cannot do anything except stop
1364 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001365 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00001366 } else
bellard1fddef42005-04-17 19:16:13 +00001367#endif
bellard41625032005-04-24 10:07:11 +00001368 {
bellard858693c2004-03-31 18:52:07 +00001369 switch(s->state) {
1370 case RS_IDLE:
1371 if (ch == '$') {
1372 s->line_buf_index = 0;
1373 s->state = RS_GETLINE;
bellard4c3a88a2003-07-26 12:06:08 +00001374 }
1375 break;
bellard858693c2004-03-31 18:52:07 +00001376 case RS_GETLINE:
1377 if (ch == '#') {
1378 s->state = RS_CHKSUM1;
1379 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1380 s->state = RS_IDLE;
1381 } else {
1382 s->line_buf[s->line_buf_index++] = ch;
1383 }
1384 break;
1385 case RS_CHKSUM1:
1386 s->line_buf[s->line_buf_index] = '\0';
1387 s->line_csum = fromhex(ch) << 4;
1388 s->state = RS_CHKSUM2;
1389 break;
1390 case RS_CHKSUM2:
1391 s->line_csum |= fromhex(ch);
1392 csum = 0;
1393 for(i = 0; i < s->line_buf_index; i++) {
1394 csum += s->line_buf[i];
1395 }
1396 if (s->line_csum != (csum & 0xff)) {
ths60fe76f2007-12-16 03:02:09 +00001397 reply = '-';
1398 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00001399 s->state = RS_IDLE;
1400 } else {
ths60fe76f2007-12-16 03:02:09 +00001401 reply = '+';
1402 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00001403 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00001404 }
bellardb4608c02003-06-27 17:34:32 +00001405 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001406 default:
1407 abort();
bellardb4608c02003-06-27 17:34:32 +00001408 }
1409 }
bellard858693c2004-03-31 18:52:07 +00001410}
1411
Paul Brook0e1c9c52010-06-16 13:03:51 +01001412/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001413void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01001414{
1415 GDBState *s;
1416 char buf[4];
1417
1418 s = gdbserver_state;
1419 if (!s) {
1420 return;
1421 }
1422#ifdef CONFIG_USER_ONLY
1423 if (gdbserver_fd < 0 || s->fd < 0) {
1424 return;
1425 }
1426#endif
1427
1428 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
1429 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001430
1431#ifndef CONFIG_USER_ONLY
1432 if (s->chr) {
Anthony Liguori70f24fb2011-08-15 11:17:38 -05001433 qemu_chr_delete(s->chr);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001434 }
1435#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01001436}
1437
bellard1fddef42005-04-17 19:16:13 +00001438#ifdef CONFIG_USER_ONLY
1439int
aurel32ca587a82008-12-18 22:44:13 +00001440gdb_queuesig (void)
1441{
1442 GDBState *s;
1443
1444 s = gdbserver_state;
1445
1446 if (gdbserver_fd < 0 || s->fd < 0)
1447 return 0;
1448 else
1449 return 1;
1450}
1451
1452int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001453gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00001454{
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001455 CPUArchState *env = cpu->env_ptr;
Andreas Färber5ca666c2013-06-24 19:20:57 +02001456 GDBState *s;
1457 char buf[256];
1458 int n;
bellard1fddef42005-04-17 19:16:13 +00001459
Andreas Färber5ca666c2013-06-24 19:20:57 +02001460 s = gdbserver_state;
1461 if (gdbserver_fd < 0 || s->fd < 0) {
1462 return sig;
bellard1fddef42005-04-17 19:16:13 +00001463 }
1464
Andreas Färber5ca666c2013-06-24 19:20:57 +02001465 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001466 cpu_single_step(cpu, 0);
Andreas Färber5ca666c2013-06-24 19:20:57 +02001467 tb_flush(env);
bellard1fddef42005-04-17 19:16:13 +00001468
Andreas Färber5ca666c2013-06-24 19:20:57 +02001469 if (sig != 0) {
1470 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
1471 put_packet(s, buf);
1472 }
1473 /* put_packet() might have detected that the peer terminated the
1474 connection. */
1475 if (s->fd < 0) {
1476 return sig;
1477 }
1478
1479 sig = 0;
1480 s->state = RS_IDLE;
1481 s->running_state = 0;
1482 while (s->running_state == 0) {
1483 n = read(s->fd, buf, 256);
1484 if (n > 0) {
1485 int i;
1486
1487 for (i = 0; i < n; i++) {
1488 gdb_read_byte(s, buf[i]);
1489 }
1490 } else if (n == 0 || errno != EAGAIN) {
1491 /* XXX: Connection closed. Should probably wait for another
1492 connection before continuing. */
1493 return sig;
bellard1fddef42005-04-17 19:16:13 +00001494 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02001495 }
1496 sig = s->signal;
1497 s->signal = 0;
1498 return sig;
bellard1fddef42005-04-17 19:16:13 +00001499}
bellarde9009672005-04-26 20:42:36 +00001500
aurel32ca587a82008-12-18 22:44:13 +00001501/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001502void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00001503{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001504 GDBState *s;
1505 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00001506
Andreas Färber5ca666c2013-06-24 19:20:57 +02001507 s = gdbserver_state;
1508 if (gdbserver_fd < 0 || s->fd < 0) {
1509 return;
1510 }
aurel32ca587a82008-12-18 22:44:13 +00001511
Andreas Färber5ca666c2013-06-24 19:20:57 +02001512 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
1513 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00001514}
bellard1fddef42005-04-17 19:16:13 +00001515
aliguori880a7572008-11-18 20:30:24 +00001516static void gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00001517{
1518 GDBState *s;
1519 struct sockaddr_in sockaddr;
1520 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001521 int fd;
bellard858693c2004-03-31 18:52:07 +00001522
1523 for(;;) {
1524 len = sizeof(sockaddr);
1525 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1526 if (fd < 0 && errno != EINTR) {
1527 perror("accept");
1528 return;
1529 } else if (fd >= 0) {
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001530#ifndef _WIN32
1531 fcntl(fd, F_SETFD, FD_CLOEXEC);
1532#endif
bellard858693c2004-03-31 18:52:07 +00001533 break;
1534 }
1535 }
1536
1537 /* set short latency */
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001538 socket_set_nodelay(fd);
ths3b46e622007-09-17 08:09:54 +00001539
Anthony Liguori7267c092011-08-20 22:09:37 -05001540 s = g_malloc0(sizeof(GDBState));
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001541 s->c_cpu = first_cpu;
1542 s->g_cpu = first_cpu;
bellard858693c2004-03-31 18:52:07 +00001543 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02001544 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00001545
aliguori880a7572008-11-18 20:30:24 +00001546 gdbserver_state = s;
pbrooka2d1eba2007-01-28 03:10:55 +00001547
bellard858693c2004-03-31 18:52:07 +00001548 fcntl(fd, F_SETFL, O_NONBLOCK);
bellard858693c2004-03-31 18:52:07 +00001549}
1550
1551static int gdbserver_open(int port)
1552{
1553 struct sockaddr_in sockaddr;
1554 int fd, val, ret;
1555
1556 fd = socket(PF_INET, SOCK_STREAM, 0);
1557 if (fd < 0) {
1558 perror("socket");
1559 return -1;
1560 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001561#ifndef _WIN32
1562 fcntl(fd, F_SETFD, FD_CLOEXEC);
1563#endif
bellard858693c2004-03-31 18:52:07 +00001564
1565 /* allow fast reuse */
1566 val = 1;
Stefan Weil9957fc72013-03-08 19:58:32 +01001567 qemu_setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
bellard858693c2004-03-31 18:52:07 +00001568
1569 sockaddr.sin_family = AF_INET;
1570 sockaddr.sin_port = htons(port);
1571 sockaddr.sin_addr.s_addr = 0;
1572 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1573 if (ret < 0) {
1574 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00001575 close(fd);
bellard858693c2004-03-31 18:52:07 +00001576 return -1;
1577 }
1578 ret = listen(fd, 0);
1579 if (ret < 0) {
1580 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00001581 close(fd);
bellard858693c2004-03-31 18:52:07 +00001582 return -1;
1583 }
bellard858693c2004-03-31 18:52:07 +00001584 return fd;
1585}
1586
1587int gdbserver_start(int port)
1588{
1589 gdbserver_fd = gdbserver_open(port);
1590 if (gdbserver_fd < 0)
1591 return -1;
1592 /* accept connections */
aliguori880a7572008-11-18 20:30:24 +00001593 gdb_accept();
bellardb4608c02003-06-27 17:34:32 +00001594 return 0;
1595}
aurel322b1319c2008-12-18 22:44:04 +00001596
1597/* Disable gdb stub for child processes. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001598void gdbserver_fork(CPUArchState *env)
aurel322b1319c2008-12-18 22:44:04 +00001599{
1600 GDBState *s = gdbserver_state;
edgar_igl9f6164d2009-01-07 10:22:28 +00001601 if (gdbserver_fd < 0 || s->fd < 0)
aurel322b1319c2008-12-18 22:44:04 +00001602 return;
1603 close(s->fd);
1604 s->fd = -1;
1605 cpu_breakpoint_remove_all(env, BP_GDB);
1606 cpu_watchpoint_remove_all(env, BP_GDB);
1607}
pbrook4046d912007-01-28 01:53:16 +00001608#else
thsaa1f17c2007-07-11 22:48:58 +00001609static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00001610{
pbrook56aebc82008-10-11 17:55:29 +00001611 /* We can handle an arbitrarily large amount of data.
1612 Pick the maximum packet size, which is as good as anything. */
1613 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00001614}
1615
thsaa1f17c2007-07-11 22:48:58 +00001616static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00001617{
pbrook4046d912007-01-28 01:53:16 +00001618 int i;
1619
1620 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00001621 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00001622 }
1623}
1624
1625static void gdb_chr_event(void *opaque, int event)
1626{
1627 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05301628 case CHR_EVENT_OPENED:
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001629 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02001630 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00001631 break;
1632 default:
1633 break;
1634 }
1635}
1636
aliguori8a34a0f2009-03-05 23:01:55 +00001637static void gdb_monitor_output(GDBState *s, const char *msg, int len)
1638{
1639 char buf[MAX_PACKET_LENGTH];
1640
1641 buf[0] = 'O';
1642 if (len > (MAX_PACKET_LENGTH/2) - 1)
1643 len = (MAX_PACKET_LENGTH/2) - 1;
1644 memtohex(buf + 1, (uint8_t *)msg, len);
1645 put_packet(s, buf);
1646}
1647
1648static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
1649{
1650 const char *p = (const char *)buf;
1651 int max_sz;
1652
1653 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
1654 for (;;) {
1655 if (len <= max_sz) {
1656 gdb_monitor_output(gdbserver_state, p, len);
1657 break;
1658 }
1659 gdb_monitor_output(gdbserver_state, p, max_sz);
1660 p += max_sz;
1661 len -= max_sz;
1662 }
1663 return len;
1664}
1665
aliguori59030a82009-04-05 18:43:41 +00001666#ifndef _WIN32
1667static void gdb_sigterm_handler(int signal)
1668{
Luiz Capitulino13548692011-07-29 15:36:43 -03001669 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001670 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001671 }
aliguori59030a82009-04-05 18:43:41 +00001672}
1673#endif
1674
1675int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00001676{
1677 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00001678 char gdbstub_device_name[128];
aliguori36556b22009-03-28 18:05:53 +00001679 CharDriverState *chr = NULL;
1680 CharDriverState *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00001681
aliguori59030a82009-04-05 18:43:41 +00001682 if (!device)
1683 return -1;
1684 if (strcmp(device, "none") != 0) {
1685 if (strstart(device, "tcp:", NULL)) {
1686 /* enforce required TCP attributes */
1687 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
1688 "%s,nowait,nodelay,server", device);
1689 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00001690 }
aliguori59030a82009-04-05 18:43:41 +00001691#ifndef _WIN32
1692 else if (strcmp(device, "stdio") == 0) {
1693 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00001694
aliguori59030a82009-04-05 18:43:41 +00001695 memset(&act, 0, sizeof(act));
1696 act.sa_handler = gdb_sigterm_handler;
1697 sigaction(SIGINT, &act, NULL);
1698 }
1699#endif
Anthony Liguori27143a42011-08-15 11:17:36 -05001700 chr = qemu_chr_new("gdb", device, NULL);
aliguori36556b22009-03-28 18:05:53 +00001701 if (!chr)
1702 return -1;
1703
Hans de Goede456d6062013-03-27 20:29:40 +01001704 qemu_chr_fe_claim_no_fail(chr);
aliguori36556b22009-03-28 18:05:53 +00001705 qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
1706 gdb_chr_event, NULL);
pbrookcfc34752007-02-22 01:48:01 +00001707 }
1708
aliguori36556b22009-03-28 18:05:53 +00001709 s = gdbserver_state;
1710 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001711 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00001712 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00001713
aliguori36556b22009-03-28 18:05:53 +00001714 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
1715
1716 /* Initialize a monitor terminal for gdb */
Anthony Liguori7267c092011-08-20 22:09:37 -05001717 mon_chr = g_malloc0(sizeof(*mon_chr));
aliguori36556b22009-03-28 18:05:53 +00001718 mon_chr->chr_write = gdb_monitor_write;
1719 monitor_init(mon_chr, 0);
1720 } else {
1721 if (s->chr)
Anthony Liguori70f24fb2011-08-15 11:17:38 -05001722 qemu_chr_delete(s->chr);
aliguori36556b22009-03-28 18:05:53 +00001723 mon_chr = s->mon_chr;
1724 memset(s, 0, sizeof(GDBState));
1725 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001726 s->c_cpu = first_cpu;
1727 s->g_cpu = first_cpu;
pbrook4046d912007-01-28 01:53:16 +00001728 s->chr = chr;
aliguori36556b22009-03-28 18:05:53 +00001729 s->state = chr ? RS_IDLE : RS_INACTIVE;
1730 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00001731 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00001732
pbrook4046d912007-01-28 01:53:16 +00001733 return 0;
1734}
1735#endif