blob: 92b2f815847ac20953f607bc666fbea90822a7d0 [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"
Leon Alraecfe67ce2015-06-19 14:17:45 +010043#include "exec/semihost.h"
aurel32ca587a82008-12-18 22:44:13 +000044
Jan Kiszkaa3919382015-02-07 09:38:44 +010045#ifdef CONFIG_USER_ONLY
46#define GDB_ATTACHED "0"
47#else
48#define GDB_ATTACHED "1"
49#endif
50
Andreas Färberf3659ee2013-06-27 19:09:09 +020051static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
52 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020053{
Andreas Färberf3659ee2013-06-27 19:09:09 +020054 CPUClass *cc = CPU_GET_CLASS(cpu);
55
56 if (cc->memory_rw_debug) {
57 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
58 }
59 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020060}
aurel32ca587a82008-12-18 22:44:13 +000061
62enum {
63 GDB_SIGNAL_0 = 0,
64 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +010065 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +000066 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +010067 GDB_SIGNAL_ABRT = 6,
68 GDB_SIGNAL_ALRM = 14,
69 GDB_SIGNAL_IO = 23,
70 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +000071 GDB_SIGNAL_UNKNOWN = 143
72};
73
74#ifdef CONFIG_USER_ONLY
75
76/* Map target signal numbers to GDB protocol signal numbers and vice
77 * versa. For user emulation's currently supported systems, we can
78 * assume most signals are defined.
79 */
80
81static int gdb_signal_table[] = {
82 0,
83 TARGET_SIGHUP,
84 TARGET_SIGINT,
85 TARGET_SIGQUIT,
86 TARGET_SIGILL,
87 TARGET_SIGTRAP,
88 TARGET_SIGABRT,
89 -1, /* SIGEMT */
90 TARGET_SIGFPE,
91 TARGET_SIGKILL,
92 TARGET_SIGBUS,
93 TARGET_SIGSEGV,
94 TARGET_SIGSYS,
95 TARGET_SIGPIPE,
96 TARGET_SIGALRM,
97 TARGET_SIGTERM,
98 TARGET_SIGURG,
99 TARGET_SIGSTOP,
100 TARGET_SIGTSTP,
101 TARGET_SIGCONT,
102 TARGET_SIGCHLD,
103 TARGET_SIGTTIN,
104 TARGET_SIGTTOU,
105 TARGET_SIGIO,
106 TARGET_SIGXCPU,
107 TARGET_SIGXFSZ,
108 TARGET_SIGVTALRM,
109 TARGET_SIGPROF,
110 TARGET_SIGWINCH,
111 -1, /* SIGLOST */
112 TARGET_SIGUSR1,
113 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000114#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000115 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000116#else
117 -1,
118#endif
aurel32ca587a82008-12-18 22:44:13 +0000119 -1, /* SIGPOLL */
120 -1,
121 -1,
122 -1,
123 -1,
124 -1,
125 -1,
126 -1,
127 -1,
128 -1,
129 -1,
130 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000131#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000132 __SIGRTMIN + 1,
133 __SIGRTMIN + 2,
134 __SIGRTMIN + 3,
135 __SIGRTMIN + 4,
136 __SIGRTMIN + 5,
137 __SIGRTMIN + 6,
138 __SIGRTMIN + 7,
139 __SIGRTMIN + 8,
140 __SIGRTMIN + 9,
141 __SIGRTMIN + 10,
142 __SIGRTMIN + 11,
143 __SIGRTMIN + 12,
144 __SIGRTMIN + 13,
145 __SIGRTMIN + 14,
146 __SIGRTMIN + 15,
147 __SIGRTMIN + 16,
148 __SIGRTMIN + 17,
149 __SIGRTMIN + 18,
150 __SIGRTMIN + 19,
151 __SIGRTMIN + 20,
152 __SIGRTMIN + 21,
153 __SIGRTMIN + 22,
154 __SIGRTMIN + 23,
155 __SIGRTMIN + 24,
156 __SIGRTMIN + 25,
157 __SIGRTMIN + 26,
158 __SIGRTMIN + 27,
159 __SIGRTMIN + 28,
160 __SIGRTMIN + 29,
161 __SIGRTMIN + 30,
162 __SIGRTMIN + 31,
163 -1, /* SIGCANCEL */
164 __SIGRTMIN,
165 __SIGRTMIN + 32,
166 __SIGRTMIN + 33,
167 __SIGRTMIN + 34,
168 __SIGRTMIN + 35,
169 __SIGRTMIN + 36,
170 __SIGRTMIN + 37,
171 __SIGRTMIN + 38,
172 __SIGRTMIN + 39,
173 __SIGRTMIN + 40,
174 __SIGRTMIN + 41,
175 __SIGRTMIN + 42,
176 __SIGRTMIN + 43,
177 __SIGRTMIN + 44,
178 __SIGRTMIN + 45,
179 __SIGRTMIN + 46,
180 __SIGRTMIN + 47,
181 __SIGRTMIN + 48,
182 __SIGRTMIN + 49,
183 __SIGRTMIN + 50,
184 __SIGRTMIN + 51,
185 __SIGRTMIN + 52,
186 __SIGRTMIN + 53,
187 __SIGRTMIN + 54,
188 __SIGRTMIN + 55,
189 __SIGRTMIN + 56,
190 __SIGRTMIN + 57,
191 __SIGRTMIN + 58,
192 __SIGRTMIN + 59,
193 __SIGRTMIN + 60,
194 __SIGRTMIN + 61,
195 __SIGRTMIN + 62,
196 __SIGRTMIN + 63,
197 __SIGRTMIN + 64,
198 __SIGRTMIN + 65,
199 __SIGRTMIN + 66,
200 __SIGRTMIN + 67,
201 __SIGRTMIN + 68,
202 __SIGRTMIN + 69,
203 __SIGRTMIN + 70,
204 __SIGRTMIN + 71,
205 __SIGRTMIN + 72,
206 __SIGRTMIN + 73,
207 __SIGRTMIN + 74,
208 __SIGRTMIN + 75,
209 __SIGRTMIN + 76,
210 __SIGRTMIN + 77,
211 __SIGRTMIN + 78,
212 __SIGRTMIN + 79,
213 __SIGRTMIN + 80,
214 __SIGRTMIN + 81,
215 __SIGRTMIN + 82,
216 __SIGRTMIN + 83,
217 __SIGRTMIN + 84,
218 __SIGRTMIN + 85,
219 __SIGRTMIN + 86,
220 __SIGRTMIN + 87,
221 __SIGRTMIN + 88,
222 __SIGRTMIN + 89,
223 __SIGRTMIN + 90,
224 __SIGRTMIN + 91,
225 __SIGRTMIN + 92,
226 __SIGRTMIN + 93,
227 __SIGRTMIN + 94,
228 __SIGRTMIN + 95,
229 -1, /* SIGINFO */
230 -1, /* UNKNOWN */
231 -1, /* DEFAULT */
232 -1,
233 -1,
234 -1,
235 -1,
236 -1,
237 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000238#endif
aurel32ca587a82008-12-18 22:44:13 +0000239};
bellard8f447cc2006-06-14 15:21:14 +0000240#else
aurel32ca587a82008-12-18 22:44:13 +0000241/* In system mode we only need SIGINT and SIGTRAP; other signals
242 are not yet supported. */
243
244enum {
245 TARGET_SIGINT = 2,
246 TARGET_SIGTRAP = 5
247};
248
249static int gdb_signal_table[] = {
250 -1,
251 -1,
252 TARGET_SIGINT,
253 -1,
254 -1,
255 TARGET_SIGTRAP
256};
bellard8f447cc2006-06-14 15:21:14 +0000257#endif
bellardb4608c02003-06-27 17:34:32 +0000258
aurel32ca587a82008-12-18 22:44:13 +0000259#ifdef CONFIG_USER_ONLY
260static int target_signal_to_gdb (int sig)
261{
262 int i;
263 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
264 if (gdb_signal_table[i] == sig)
265 return i;
266 return GDB_SIGNAL_UNKNOWN;
267}
268#endif
269
270static int gdb_signal_to_target (int sig)
271{
272 if (sig < ARRAY_SIZE (gdb_signal_table))
273 return gdb_signal_table[sig];
274 else
275 return -1;
276}
277
bellard4abe6152003-07-26 18:01:58 +0000278//#define DEBUG_GDB
bellardb4608c02003-06-27 17:34:32 +0000279
pbrook56aebc82008-10-11 17:55:29 +0000280typedef struct GDBRegisterState {
281 int base_reg;
282 int num_regs;
283 gdb_reg_cb get_reg;
284 gdb_reg_cb set_reg;
285 const char *xml;
286 struct GDBRegisterState *next;
287} GDBRegisterState;
288
bellard858693c2004-03-31 18:52:07 +0000289enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000290 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000291 RS_IDLE,
292 RS_GETLINE,
293 RS_CHKSUM1,
294 RS_CHKSUM2,
295};
bellard858693c2004-03-31 18:52:07 +0000296typedef struct GDBState {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200297 CPUState *c_cpu; /* current CPU for step/continue ops */
298 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200299 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000300 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000301 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000302 int line_buf_index;
303 int line_csum;
pbrook56aebc82008-10-11 17:55:29 +0000304 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000305 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000306 int signal;
bellard41625032005-04-24 10:07:11 +0000307#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000308 int fd;
bellard41625032005-04-24 10:07:11 +0000309 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000310#else
311 CharDriverState *chr;
aliguori8a34a0f2009-03-05 23:01:55 +0000312 CharDriverState *mon_chr;
bellard41625032005-04-24 10:07:11 +0000313#endif
Meador Ingecdb432b2012-03-15 17:49:45 +0000314 char syscall_buf[256];
315 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000316} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000317
edgar_igl60897d32008-05-09 08:25:14 +0000318/* By default use no IRQs and no timers while single stepping so as to
319 * make single stepping like an ICE HW step.
320 */
321static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
322
aliguori880a7572008-11-18 20:30:24 +0000323static GDBState *gdbserver_state;
324
Andreas Färber5b50e792013-06-29 04:18:45 +0200325bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000326
bellard1fddef42005-04-17 19:16:13 +0000327#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000328/* XXX: This is not thread safe. Do we care? */
329static int gdbserver_fd = -1;
330
bellard858693c2004-03-31 18:52:07 +0000331static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000332{
333 uint8_t ch;
334 int ret;
335
336 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000337 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000338 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000339 if (errno == ECONNRESET)
340 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000341 if (errno != EINTR && errno != EAGAIN)
342 return -1;
343 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000344 close(s->fd);
345 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000346 return -1;
347 } else {
348 break;
349 }
350 }
351 return ch;
352}
pbrook4046d912007-01-28 01:53:16 +0000353#endif
bellardb4608c02003-06-27 17:34:32 +0000354
blueswir1654efcf2009-04-18 07:29:59 +0000355static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000356 GDB_SYS_UNKNOWN,
357 GDB_SYS_ENABLED,
358 GDB_SYS_DISABLED,
359} gdb_syscall_mode;
360
Liviu Ionescua38bb072014-12-11 12:07:48 +0000361/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000362int use_gdb_syscalls(void)
363{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100364 SemihostingTarget target = semihosting_get_target();
365 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000366 /* -semihosting-config target=native */
367 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100368 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000369 /* -semihosting-config target=gdb */
370 return true;
371 }
372
373 /* -semihosting-config target=auto */
374 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000375 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000376 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
377 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000378 }
379 return gdb_syscall_mode == GDB_SYS_ENABLED;
380}
381
edgar_iglba70a622008-03-14 06:10:42 +0000382/* Resume execution. */
383static inline void gdb_continue(GDBState *s)
384{
385#ifdef CONFIG_USER_ONLY
386 s->running_state = 1;
387#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200388 if (!runstate_needs_reset()) {
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200389 vm_start();
390 }
edgar_iglba70a622008-03-14 06:10:42 +0000391#endif
392}
393
bellard858693c2004-03-31 18:52:07 +0000394static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000395{
pbrook4046d912007-01-28 01:53:16 +0000396#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000397 int ret;
398
399 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000400 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000401 if (ret < 0) {
402 if (errno != EINTR && errno != EAGAIN)
403 return;
404 } else {
405 buf += ret;
406 len -= ret;
407 }
408 }
pbrook4046d912007-01-28 01:53:16 +0000409#else
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500410 qemu_chr_fe_write(s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000411#endif
bellardb4608c02003-06-27 17:34:32 +0000412}
413
414static inline int fromhex(int v)
415{
416 if (v >= '0' && v <= '9')
417 return v - '0';
418 else if (v >= 'A' && v <= 'F')
419 return v - 'A' + 10;
420 else if (v >= 'a' && v <= 'f')
421 return v - 'a' + 10;
422 else
423 return 0;
424}
425
426static inline int tohex(int v)
427{
428 if (v < 10)
429 return v + '0';
430 else
431 return v - 10 + 'a';
432}
433
434static void memtohex(char *buf, const uint8_t *mem, int len)
435{
436 int i, c;
437 char *q;
438 q = buf;
439 for(i = 0; i < len; i++) {
440 c = mem[i];
441 *q++ = tohex(c >> 4);
442 *q++ = tohex(c & 0xf);
443 }
444 *q = '\0';
445}
446
447static void hextomem(uint8_t *mem, const char *buf, int len)
448{
449 int i;
450
451 for(i = 0; i < len; i++) {
452 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
453 buf += 2;
454 }
455}
456
bellardb4608c02003-06-27 17:34:32 +0000457/* return -1 if error, 0 if OK */
pbrook56aebc82008-10-11 17:55:29 +0000458static int put_packet_binary(GDBState *s, const char *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000459{
pbrook56aebc82008-10-11 17:55:29 +0000460 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000461 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000462
bellardb4608c02003-06-27 17:34:32 +0000463 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000464 p = s->last_packet;
465 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000466 memcpy(p, buf, len);
467 p += len;
bellardb4608c02003-06-27 17:34:32 +0000468 csum = 0;
469 for(i = 0; i < len; i++) {
470 csum += buf[i];
471 }
pbrook4046d912007-01-28 01:53:16 +0000472 *(p++) = '#';
473 *(p++) = tohex((csum >> 4) & 0xf);
474 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000475
pbrook4046d912007-01-28 01:53:16 +0000476 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000477 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000478
pbrook4046d912007-01-28 01:53:16 +0000479#ifdef CONFIG_USER_ONLY
480 i = get_char(s);
481 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000482 return -1;
pbrook4046d912007-01-28 01:53:16 +0000483 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000484 break;
pbrook4046d912007-01-28 01:53:16 +0000485#else
486 break;
487#endif
bellardb4608c02003-06-27 17:34:32 +0000488 }
489 return 0;
490}
491
pbrook56aebc82008-10-11 17:55:29 +0000492/* return -1 if error, 0 if OK */
493static int put_packet(GDBState *s, const char *buf)
494{
495#ifdef DEBUG_GDB
496 printf("reply='%s'\n", buf);
497#endif
498
499 return put_packet_binary(s, buf, strlen(buf));
500}
501
pbrook56aebc82008-10-11 17:55:29 +0000502/* Encode data using the encoding for 'x' packets. */
503static int memtox(char *buf, const char *mem, int len)
504{
505 char *p = buf;
506 char c;
507
508 while (len--) {
509 c = *(mem++);
510 switch (c) {
511 case '#': case '$': case '*': case '}':
512 *(p++) = '}';
513 *(p++) = c ^ 0x20;
514 break;
515 default:
516 *(p++) = c;
517 break;
518 }
519 }
520 return p - buf;
521}
522
Andreas Färber5b24c642013-07-07 15:08:22 +0200523static const char *get_feature_xml(const char *p, const char **newp,
524 CPUClass *cc)
pbrook56aebc82008-10-11 17:55:29 +0000525{
pbrook56aebc82008-10-11 17:55:29 +0000526 size_t len;
527 int i;
528 const char *name;
529 static char target_xml[1024];
530
531 len = 0;
532 while (p[len] && p[len] != ':')
533 len++;
534 *newp = p + len;
535
536 name = NULL;
537 if (strncmp(p, "target.xml", len) == 0) {
538 /* Generate the XML description for this CPU. */
539 if (!target_xml[0]) {
540 GDBRegisterState *r;
Andreas Färbereac8b352013-06-28 21:11:37 +0200541 CPUState *cpu = first_cpu;
pbrook56aebc82008-10-11 17:55:29 +0000542
blueswir15b3715b2008-10-25 11:18:12 +0000543 snprintf(target_xml, sizeof(target_xml),
544 "<?xml version=\"1.0\"?>"
545 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
546 "<target>"
547 "<xi:include href=\"%s\"/>",
Andreas Färber5b24c642013-07-07 15:08:22 +0200548 cc->gdb_core_xml_file);
pbrook56aebc82008-10-11 17:55:29 +0000549
Andreas Färbereac8b352013-06-28 21:11:37 +0200550 for (r = cpu->gdb_regs; r; r = r->next) {
blueswir12dc766d2009-04-13 16:06:19 +0000551 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
552 pstrcat(target_xml, sizeof(target_xml), r->xml);
553 pstrcat(target_xml, sizeof(target_xml), "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000554 }
blueswir12dc766d2009-04-13 16:06:19 +0000555 pstrcat(target_xml, sizeof(target_xml), "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000556 }
557 return target_xml;
558 }
559 for (i = 0; ; i++) {
560 name = xml_builtin[i][0];
561 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
562 break;
563 }
564 return name ? xml_builtin[i][1] : NULL;
565}
pbrook56aebc82008-10-11 17:55:29 +0000566
Andreas Färber385b9f02013-06-27 18:25:36 +0200567static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000568{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200569 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200570 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000571 GDBRegisterState *r;
572
Andreas Färbera0e372f2013-06-28 23:18:47 +0200573 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200574 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200575 }
pbrook56aebc82008-10-11 17:55:29 +0000576
Andreas Färbereac8b352013-06-28 21:11:37 +0200577 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000578 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
579 return r->get_reg(env, mem_buf, reg - r->base_reg);
580 }
581 }
582 return 0;
583}
584
Andreas Färber385b9f02013-06-27 18:25:36 +0200585static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000586{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200587 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200588 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000589 GDBRegisterState *r;
590
Andreas Färbera0e372f2013-06-28 23:18:47 +0200591 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200592 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200593 }
pbrook56aebc82008-10-11 17:55:29 +0000594
Andreas Färbereac8b352013-06-28 21:11:37 +0200595 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000596 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
597 return r->set_reg(env, mem_buf, reg - r->base_reg);
598 }
599 }
600 return 0;
601}
602
603/* Register a supplemental set of CPU registers. If g_pos is nonzero it
604 specifies the first register number and these registers are included in
605 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
606 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
607 */
608
Andreas Färber22169d42013-06-28 21:27:39 +0200609void gdb_register_coprocessor(CPUState *cpu,
610 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
611 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000612{
613 GDBRegisterState *s;
614 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000615
Andreas Färbereac8b352013-06-28 21:11:37 +0200616 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000617 while (*p) {
618 /* Check for duplicates. */
619 if (strcmp((*p)->xml, xml) == 0)
620 return;
621 p = &(*p)->next;
622 }
Stefan Weil9643c252011-10-18 22:25:38 +0200623
624 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200625 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200626 s->num_regs = num_regs;
627 s->get_reg = get_reg;
628 s->set_reg = set_reg;
629 s->xml = xml;
630
pbrook56aebc82008-10-11 17:55:29 +0000631 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200632 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000633 *p = s;
634 if (g_pos) {
635 if (g_pos != s->base_reg) {
636 fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
637 "Expected %d got %d\n", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200638 } else {
639 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000640 }
641 }
642}
643
aliguoria1d1bb32008-11-18 20:07:32 +0000644#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100645/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
646static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
647{
648 static const int xlat[] = {
649 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
650 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
651 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
652 };
653
654 CPUClass *cc = CPU_GET_CLASS(cpu);
655 int cputype = xlat[gdbtype];
656
657 if (cc->gdb_stop_before_watchpoint) {
658 cputype |= BP_STOP_BEFORE_ACCESS;
659 }
660 return cputype;
661}
aliguoria1d1bb32008-11-18 20:07:32 +0000662#endif
663
aliguori880a7572008-11-18 20:30:24 +0000664static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000665{
Andreas Färber182735e2013-05-29 22:29:20 +0200666 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000667 int err = 0;
668
Andreas Färber62278812013-06-27 17:12:06 +0200669 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200670 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200671 }
aliguorie22a25c2009-03-12 20:12:48 +0000672
aliguoria1d1bb32008-11-18 20:07:32 +0000673 switch (type) {
674 case GDB_BREAKPOINT_SW:
675 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200676 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200677 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
678 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000679 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200680 }
aliguori880a7572008-11-18 20:30:24 +0000681 }
682 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000683#ifndef CONFIG_USER_ONLY
684 case GDB_WATCHPOINT_WRITE:
685 case GDB_WATCHPOINT_READ:
686 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200687 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100688 err = cpu_watchpoint_insert(cpu, addr, len,
689 xlat_gdb_type(cpu, type), NULL);
690 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000691 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +0100692 }
aliguori880a7572008-11-18 20:30:24 +0000693 }
694 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000695#endif
696 default:
697 return -ENOSYS;
698 }
699}
700
aliguori880a7572008-11-18 20:30:24 +0000701static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000702{
Andreas Färber182735e2013-05-29 22:29:20 +0200703 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000704 int err = 0;
705
Andreas Färber62278812013-06-27 17:12:06 +0200706 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200707 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200708 }
aliguorie22a25c2009-03-12 20:12:48 +0000709
aliguoria1d1bb32008-11-18 20:07:32 +0000710 switch (type) {
711 case GDB_BREAKPOINT_SW:
712 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200713 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200714 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
715 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000716 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200717 }
aliguori880a7572008-11-18 20:30:24 +0000718 }
719 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000720#ifndef CONFIG_USER_ONLY
721 case GDB_WATCHPOINT_WRITE:
722 case GDB_WATCHPOINT_READ:
723 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200724 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100725 err = cpu_watchpoint_remove(cpu, addr, len,
726 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +0000727 if (err)
728 break;
729 }
730 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000731#endif
732 default:
733 return -ENOSYS;
734 }
735}
736
aliguori880a7572008-11-18 20:30:24 +0000737static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +0000738{
Andreas Färber182735e2013-05-29 22:29:20 +0200739 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000740
aliguorie22a25c2009-03-12 20:12:48 +0000741 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200742 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +0000743 return;
744 }
745
Andreas Färberbdc44642013-06-24 23:50:24 +0200746 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200747 cpu_breakpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000748#ifndef CONFIG_USER_ONLY
Andreas Färber75a34032013-09-02 16:57:02 +0200749 cpu_watchpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000750#endif
aliguori880a7572008-11-18 20:30:24 +0000751 }
aliguoria1d1bb32008-11-18 20:07:32 +0000752}
753
aurel32fab9d282009-04-08 21:29:37 +0000754static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
755{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200756 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +0200757
758 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -0700759 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +0000760}
761
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200762static CPUState *find_cpu(uint32_t thread_id)
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700763{
Andreas Färber0d342822012-12-17 07:12:13 +0100764 CPUState *cpu;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700765
Andreas Färberbdc44642013-06-24 23:50:24 +0200766 CPU_FOREACH(cpu) {
Andreas Färberaa48dd92013-07-09 20:50:52 +0200767 if (cpu_index(cpu) == thread_id) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200768 return cpu;
Andreas Färberaa48dd92013-07-09 20:50:52 +0200769 }
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700770 }
Andreas Färberaa48dd92013-07-09 20:50:52 +0200771
772 return NULL;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700773}
774
Jan Kiszka4dabe742015-02-07 09:38:43 +0100775static int is_query_packet(const char *p, const char *query, char separator)
776{
777 unsigned int query_len = strlen(query);
778
779 return strncmp(p, query, query_len) == 0 &&
780 (p[query_len] == '\0' || p[query_len] == separator);
781}
782
aliguori880a7572008-11-18 20:30:24 +0000783static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +0000784{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200785 CPUState *cpu;
Andreas Färber5b24c642013-07-07 15:08:22 +0200786 CPUClass *cc;
bellardb4608c02003-06-27 17:34:32 +0000787 const char *p;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700788 uint32_t thread;
789 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +0000790 char buf[MAX_PACKET_LENGTH];
791 uint8_t mem_buf[MAX_PACKET_LENGTH];
792 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +0000793 target_ulong addr, len;
ths3b46e622007-09-17 08:09:54 +0000794
bellard858693c2004-03-31 18:52:07 +0000795#ifdef DEBUG_GDB
796 printf("command='%s'\n", line_buf);
bellard4c3a88a2003-07-26 12:06:08 +0000797#endif
bellard858693c2004-03-31 18:52:07 +0000798 p = line_buf;
799 ch = *p++;
800 switch(ch) {
801 case '?':
bellard1fddef42005-04-17 19:16:13 +0000802 /* TODO: Make this return the correct value for user-mode. */
aurel32ca587a82008-12-18 22:44:13 +0000803 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200804 cpu_index(s->c_cpu));
bellard858693c2004-03-31 18:52:07 +0000805 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +0000806 /* Remove all the breakpoints when this query is issued,
807 * because gdb is doing and initial connect and the state
808 * should be cleaned up.
809 */
aliguori880a7572008-11-18 20:30:24 +0000810 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +0000811 break;
812 case 'c':
813 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +0000814 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +0000815 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +0000816 }
aurel32ca587a82008-12-18 22:44:13 +0000817 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +0000818 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +0000819 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +0000820 case 'C':
aurel32ca587a82008-12-18 22:44:13 +0000821 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
822 if (s->signal == -1)
823 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +0000824 gdb_continue(s);
825 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200826 case 'v':
827 if (strncmp(p, "Cont", 4) == 0) {
828 int res_signal, res_thread;
829
830 p += 4;
831 if (*p == '?') {
832 put_packet(s, "vCont;c;C;s;S");
833 break;
834 }
835 res = 0;
836 res_signal = 0;
837 res_thread = 0;
838 while (*p) {
839 int action, signal;
840
841 if (*p++ != ';') {
842 res = 0;
843 break;
844 }
845 action = *p++;
846 signal = 0;
847 if (action == 'C' || action == 'S') {
Martin Simmonsf17b0692014-11-05 14:47:39 +0000848 signal = gdb_signal_to_target(strtoul(p, (char **)&p, 16));
849 if (signal == -1) {
850 signal = 0;
851 }
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200852 } else if (action != 'c' && action != 's') {
853 res = 0;
854 break;
855 }
856 thread = 0;
857 if (*p == ':') {
858 thread = strtoull(p+1, (char **)&p, 16);
859 }
860 action = tolower(action);
861 if (res == 0 || (res == 'c' && action == 's')) {
862 res = action;
863 res_signal = signal;
864 res_thread = thread;
865 }
866 }
867 if (res) {
868 if (res_thread != -1 && res_thread != 0) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200869 cpu = find_cpu(res_thread);
870 if (cpu == NULL) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200871 put_packet(s, "E22");
872 break;
873 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200874 s->c_cpu = cpu;
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200875 }
876 if (res == 's') {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200877 cpu_single_step(s->c_cpu, sstep_flags);
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200878 }
879 s->signal = res_signal;
880 gdb_continue(s);
881 return RS_IDLE;
882 }
883 break;
884 } else {
885 goto unknown_command;
886 }
edgar_igl7d03f822008-05-17 18:58:29 +0000887 case 'k':
888 /* Kill the target */
889 fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
890 exit(0);
891 case 'D':
892 /* Detach packet */
aliguori880a7572008-11-18 20:30:24 +0000893 gdb_breakpoint_remove_all();
Daniel Gutson7ea06da2010-02-26 14:13:50 -0300894 gdb_syscall_mode = GDB_SYS_DISABLED;
edgar_igl7d03f822008-05-17 18:58:29 +0000895 gdb_continue(s);
896 put_packet(s, "OK");
897 break;
bellard858693c2004-03-31 18:52:07 +0000898 case 's':
899 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +0000900 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +0000901 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +0000902 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200903 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +0000904 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +0000905 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +0000906 case 'F':
907 {
908 target_ulong ret;
909 target_ulong err;
910
911 ret = strtoull(p, (char **)&p, 16);
912 if (*p == ',') {
913 p++;
914 err = strtoull(p, (char **)&p, 16);
915 } else {
916 err = 0;
917 }
918 if (*p == ',')
919 p++;
920 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +0000921 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200922 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +0000923 s->current_syscall_cb = NULL;
924 }
pbrooka2d1eba2007-01-28 03:10:55 +0000925 if (type == 'C') {
926 put_packet(s, "T02");
927 } else {
edgar_iglba70a622008-03-14 06:10:42 +0000928 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +0000929 }
930 }
931 break;
bellard858693c2004-03-31 18:52:07 +0000932 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200933 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +0000934 len = 0;
Andreas Färber35143f02013-08-12 18:09:47 +0200935 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200936 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +0000937 len += reg_size;
938 }
939 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +0000940 put_packet(s, buf);
941 break;
942 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200943 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +0000944 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +0000945 len = strlen(p) / 2;
946 hextomem((uint8_t *)registers, p, len);
Andreas Färber35143f02013-08-12 18:09:47 +0200947 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200948 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +0000949 len -= reg_size;
950 registers += reg_size;
951 }
bellard858693c2004-03-31 18:52:07 +0000952 put_packet(s, "OK");
953 break;
954 case 'm':
bellard9d9754a2006-06-25 15:32:37 +0000955 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +0000956 if (*p == ',')
957 p++;
bellard9d9754a2006-06-25 15:32:37 +0000958 len = strtoull(p, NULL, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200959 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +0000960 put_packet (s, "E14");
961 } else {
962 memtohex(buf, mem_buf, len);
963 put_packet(s, buf);
964 }
bellard858693c2004-03-31 18:52:07 +0000965 break;
966 case 'M':
bellard9d9754a2006-06-25 15:32:37 +0000967 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +0000968 if (*p == ',')
969 p++;
bellard9d9754a2006-06-25 15:32:37 +0000970 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +0000971 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +0000972 p++;
973 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200974 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +0200975 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +0000976 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +0200977 } else {
bellard858693c2004-03-31 18:52:07 +0000978 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +0200979 }
bellard858693c2004-03-31 18:52:07 +0000980 break;
pbrook56aebc82008-10-11 17:55:29 +0000981 case 'p':
982 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
983 This works, but can be very slow. Anything new enough to
984 understand XML also knows how to use this properly. */
985 if (!gdb_has_xml)
986 goto unknown_command;
987 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200988 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +0000989 if (reg_size) {
990 memtohex(buf, mem_buf, reg_size);
991 put_packet(s, buf);
992 } else {
993 put_packet(s, "E14");
994 }
995 break;
996 case 'P':
997 if (!gdb_has_xml)
998 goto unknown_command;
999 addr = strtoull(p, (char **)&p, 16);
1000 if (*p == '=')
1001 p++;
1002 reg_size = strlen(p) / 2;
1003 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001004 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001005 put_packet(s, "OK");
1006 break;
bellard858693c2004-03-31 18:52:07 +00001007 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001008 case 'z':
1009 type = strtoul(p, (char **)&p, 16);
1010 if (*p == ',')
1011 p++;
bellard9d9754a2006-06-25 15:32:37 +00001012 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001013 if (*p == ',')
1014 p++;
bellard9d9754a2006-06-25 15:32:37 +00001015 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001016 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001017 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001018 else
aliguori880a7572008-11-18 20:30:24 +00001019 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001020 if (res >= 0)
1021 put_packet(s, "OK");
1022 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001023 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001024 else
1025 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001026 break;
aliguori880a7572008-11-18 20:30:24 +00001027 case 'H':
1028 type = *p++;
1029 thread = strtoull(p, (char **)&p, 16);
1030 if (thread == -1 || thread == 0) {
1031 put_packet(s, "OK");
1032 break;
1033 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001034 cpu = find_cpu(thread);
1035 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001036 put_packet(s, "E22");
1037 break;
1038 }
1039 switch (type) {
1040 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001041 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001042 put_packet(s, "OK");
1043 break;
1044 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001045 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001046 put_packet(s, "OK");
1047 break;
1048 default:
1049 put_packet(s, "E22");
1050 break;
1051 }
1052 break;
1053 case 'T':
1054 thread = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001055 cpu = find_cpu(thread);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001056
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001057 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001058 put_packet(s, "OK");
1059 } else {
aliguori880a7572008-11-18 20:30:24 +00001060 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001061 }
aliguori880a7572008-11-18 20:30:24 +00001062 break;
pbrook978efd62006-06-17 18:30:42 +00001063 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001064 case 'Q':
1065 /* parse any 'q' packets here */
1066 if (!strcmp(p,"qemu.sstepbits")) {
1067 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001068 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1069 SSTEP_ENABLE,
1070 SSTEP_NOIRQ,
1071 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001072 put_packet(s, buf);
1073 break;
Jan Kiszka4dabe742015-02-07 09:38:43 +01001074 } else if (is_query_packet(p, "qemu.sstep", '=')) {
edgar_igl60897d32008-05-09 08:25:14 +00001075 /* Display or change the sstep_flags */
1076 p += 10;
1077 if (*p != '=') {
1078 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001079 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001080 put_packet(s, buf);
1081 break;
1082 }
1083 p++;
1084 type = strtoul(p, (char **)&p, 16);
1085 sstep_flags = type;
1086 put_packet(s, "OK");
1087 break;
aliguori880a7572008-11-18 20:30:24 +00001088 } else if (strcmp(p,"C") == 0) {
1089 /* "Current thread" remains vague in the spec, so always return
1090 * the first CPU (gdb returns the first thread). */
1091 put_packet(s, "QC1");
1092 break;
1093 } else if (strcmp(p,"fThreadInfo") == 0) {
Andreas Färber52f34622013-06-27 13:44:40 +02001094 s->query_cpu = first_cpu;
aliguori880a7572008-11-18 20:30:24 +00001095 goto report_cpuinfo;
1096 } else if (strcmp(p,"sThreadInfo") == 0) {
1097 report_cpuinfo:
1098 if (s->query_cpu) {
Andreas Färber52f34622013-06-27 13:44:40 +02001099 snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu));
aliguori880a7572008-11-18 20:30:24 +00001100 put_packet(s, buf);
Andreas Färberbdc44642013-06-24 23:50:24 +02001101 s->query_cpu = CPU_NEXT(s->query_cpu);
aliguori880a7572008-11-18 20:30:24 +00001102 } else
1103 put_packet(s, "l");
1104 break;
1105 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1106 thread = strtoull(p+16, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001107 cpu = find_cpu(thread);
1108 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001109 cpu_synchronize_state(cpu);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001110 len = snprintf((char *)mem_buf, sizeof(mem_buf),
Andreas Färber55e5c282012-12-17 06:18:02 +01001111 "CPU#%d [%s]", cpu->cpu_index,
Andreas Färber259186a2013-01-17 18:51:17 +01001112 cpu->halted ? "halted " : "running");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001113 memtohex(buf, mem_buf, len);
1114 put_packet(s, buf);
1115 }
aliguori880a7572008-11-18 20:30:24 +00001116 break;
edgar_igl60897d32008-05-09 08:25:14 +00001117 }
blueswir10b8a9882009-03-07 10:51:36 +00001118#ifdef CONFIG_USER_ONLY
Jan Kiszka070949f2015-02-07 09:38:42 +01001119 else if (strcmp(p, "Offsets") == 0) {
Andreas Färber0429a972013-08-26 18:14:44 +02001120 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001121
blueswir1363a37d2008-08-21 17:58:08 +00001122 snprintf(buf, sizeof(buf),
1123 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1124 ";Bss=" TARGET_ABI_FMT_lx,
1125 ts->info->code_offset,
1126 ts->info->data_offset,
1127 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001128 put_packet(s, buf);
1129 break;
1130 }
blueswir10b8a9882009-03-07 10:51:36 +00001131#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001132 else if (strncmp(p, "Rcmd,", 5) == 0) {
1133 int len = strlen(p + 5);
1134
1135 if ((len % 2) != 0) {
1136 put_packet(s, "E01");
1137 break;
1138 }
1139 hextomem(mem_buf, p + 5, len);
1140 len = len / 2;
1141 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001142 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001143 put_packet(s, "OK");
1144 break;
1145 }
blueswir10b8a9882009-03-07 10:51:36 +00001146#endif /* !CONFIG_USER_ONLY */
Jan Kiszka4dabe742015-02-07 09:38:43 +01001147 if (is_query_packet(p, "Supported", ':')) {
blueswir15b3715b2008-10-25 11:18:12 +00001148 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
Andreas Färber5b24c642013-07-07 15:08:22 +02001149 cc = CPU_GET_CLASS(first_cpu);
1150 if (cc->gdb_core_xml_file != NULL) {
1151 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1152 }
pbrook56aebc82008-10-11 17:55:29 +00001153 put_packet(s, buf);
1154 break;
1155 }
pbrook56aebc82008-10-11 17:55:29 +00001156 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1157 const char *xml;
1158 target_ulong total_len;
1159
Andreas Färber5b24c642013-07-07 15:08:22 +02001160 cc = CPU_GET_CLASS(first_cpu);
1161 if (cc->gdb_core_xml_file == NULL) {
1162 goto unknown_command;
1163 }
1164
Andreas Färber5b50e792013-06-29 04:18:45 +02001165 gdb_has_xml = true;
pbrook56aebc82008-10-11 17:55:29 +00001166 p += 19;
Andreas Färber5b24c642013-07-07 15:08:22 +02001167 xml = get_feature_xml(p, &p, cc);
pbrook56aebc82008-10-11 17:55:29 +00001168 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001169 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001170 put_packet(s, buf);
1171 break;
1172 }
1173
1174 if (*p == ':')
1175 p++;
1176 addr = strtoul(p, (char **)&p, 16);
1177 if (*p == ',')
1178 p++;
1179 len = strtoul(p, (char **)&p, 16);
1180
1181 total_len = strlen(xml);
1182 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001183 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001184 put_packet(s, buf);
1185 break;
1186 }
1187 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1188 len = (MAX_PACKET_LENGTH - 5) / 2;
1189 if (len < total_len - addr) {
1190 buf[0] = 'm';
1191 len = memtox(buf + 1, xml + addr, len);
1192 } else {
1193 buf[0] = 'l';
1194 len = memtox(buf + 1, xml + addr, total_len - addr);
1195 }
1196 put_packet_binary(s, buf, len + 1);
1197 break;
1198 }
Jan Kiszkaa3919382015-02-07 09:38:44 +01001199 if (is_query_packet(p, "Attached", ':')) {
1200 put_packet(s, GDB_ATTACHED);
1201 break;
1202 }
pbrook56aebc82008-10-11 17:55:29 +00001203 /* Unrecognised 'q' command. */
1204 goto unknown_command;
1205
bellard858693c2004-03-31 18:52:07 +00001206 default:
pbrook56aebc82008-10-11 17:55:29 +00001207 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001208 /* put empty packet */
1209 buf[0] = '\0';
1210 put_packet(s, buf);
1211 break;
1212 }
1213 return RS_IDLE;
1214}
1215
Andreas Färber64f6b342013-05-27 02:06:09 +02001216void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001217{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001218 gdbserver_state->c_cpu = cpu;
1219 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001220}
1221
bellard1fddef42005-04-17 19:16:13 +00001222#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001223static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001224{
aliguori880a7572008-11-18 20:30:24 +00001225 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001226 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001227 char buf[256];
aliguorid6fc1b32008-11-18 19:55:44 +00001228 const char *type;
bellard858693c2004-03-31 18:52:07 +00001229 int ret;
1230
Meador Ingecdb432b2012-03-15 17:49:45 +00001231 if (running || s->state == RS_INACTIVE) {
1232 return;
1233 }
1234 /* Is there a GDB syscall waiting to be sent? */
1235 if (s->current_syscall_cb) {
1236 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001237 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001238 }
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001239 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001240 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02001241 if (cpu->watchpoint_hit) {
1242 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001243 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001244 type = "r";
1245 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001246 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001247 type = "a";
1248 break;
1249 default:
1250 type = "";
1251 break;
1252 }
aliguori880a7572008-11-18 20:30:24 +00001253 snprintf(buf, sizeof(buf),
1254 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
Andreas Färber0d342822012-12-17 07:12:13 +01001255 GDB_SIGNAL_TRAP, cpu_index(cpu), type,
Andreas Färberff4700b2013-08-26 18:23:18 +02001256 (target_ulong)cpu->watchpoint_hit->vaddr);
1257 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001258 goto send_packet;
pbrook6658ffb2007-03-16 23:58:11 +00001259 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001260 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00001261 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001262 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001263 case RUN_STATE_PAUSED:
aliguori9781e042009-01-22 17:15:29 +00001264 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001265 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001266 case RUN_STATE_SHUTDOWN:
Jan Kiszka425189a2011-03-22 11:02:09 +01001267 ret = GDB_SIGNAL_QUIT;
1268 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001269 case RUN_STATE_IO_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001270 ret = GDB_SIGNAL_IO;
1271 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001272 case RUN_STATE_WATCHDOG:
Jan Kiszka425189a2011-03-22 11:02:09 +01001273 ret = GDB_SIGNAL_ALRM;
1274 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001275 case RUN_STATE_INTERNAL_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001276 ret = GDB_SIGNAL_ABRT;
1277 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001278 case RUN_STATE_SAVE_VM:
1279 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001280 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001281 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001282 ret = GDB_SIGNAL_XCPU;
1283 break;
1284 default:
1285 ret = GDB_SIGNAL_UNKNOWN;
1286 break;
bellardbbeb7b52006-04-23 18:42:15 +00001287 }
Andreas Färber0d342822012-12-17 07:12:13 +01001288 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(cpu));
Jan Kiszka425189a2011-03-22 11:02:09 +01001289
1290send_packet:
bellard858693c2004-03-31 18:52:07 +00001291 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001292
1293 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001294 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001295}
bellard1fddef42005-04-17 19:16:13 +00001296#endif
bellard858693c2004-03-31 18:52:07 +00001297
pbrooka2d1eba2007-01-28 03:10:55 +00001298/* Send a gdb syscall request.
1299 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001300 %x - target_ulong argument printed in hex.
1301 %lx - 64-bit argument printed in hex.
1302 %s - string pointer (target_ulong) and length (int) pair. */
blueswir17ccfb2e2008-09-14 06:45:34 +00001303void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
pbrooka2d1eba2007-01-28 03:10:55 +00001304{
1305 va_list va;
pbrooka2d1eba2007-01-28 03:10:55 +00001306 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001307 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001308 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001309 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001310 GDBState *s;
1311
aliguori880a7572008-11-18 20:30:24 +00001312 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001313 if (!s)
1314 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001315 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001316#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001317 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001318#endif
pbrooka2d1eba2007-01-28 03:10:55 +00001319 va_start(va, fmt);
Meador Ingecdb432b2012-03-15 17:49:45 +00001320 p = s->syscall_buf;
1321 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001322 *(p++) = 'F';
1323 while (*fmt) {
1324 if (*fmt == '%') {
1325 fmt++;
1326 switch (*fmt++) {
1327 case 'x':
1328 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001329 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001330 break;
pbrooka87295e2007-05-26 15:09:38 +00001331 case 'l':
1332 if (*(fmt++) != 'x')
1333 goto bad_format;
1334 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001335 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001336 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001337 case 's':
1338 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001339 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001340 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001341 break;
1342 default:
pbrooka87295e2007-05-26 15:09:38 +00001343 bad_format:
pbrooka2d1eba2007-01-28 03:10:55 +00001344 fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
1345 fmt - 1);
1346 break;
1347 }
1348 } else {
1349 *(p++) = *(fmt++);
1350 }
1351 }
pbrook8a93e022007-08-06 13:19:15 +00001352 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001353 va_end(va);
pbrooka2d1eba2007-01-28 03:10:55 +00001354#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001355 put_packet(s, s->syscall_buf);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001356 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001357#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001358 /* In this case wait to send the syscall packet until notification that
1359 the CPU has stopped. This must be done because if the packet is sent
1360 now the reply from the syscall request could be received while the CPU
1361 is still in the running state, which can cause packets to be dropped
1362 and state transition 'T' packets to be sent while the syscall is still
1363 being processed. */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001364 cpu_exit(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00001365#endif
1366}
1367
bellard6a00d602005-11-21 23:25:50 +00001368static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00001369{
1370 int i, csum;
ths60fe76f2007-12-16 03:02:09 +00001371 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00001372
bellard1fddef42005-04-17 19:16:13 +00001373#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001374 if (s->last_packet_len) {
1375 /* Waiting for a response to the last packet. If we see the start
1376 of a new command then abandon the previous response. */
1377 if (ch == '-') {
1378#ifdef DEBUG_GDB
1379 printf("Got NACK, retransmitting\n");
1380#endif
thsffe8ab82007-12-16 03:16:05 +00001381 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
pbrook4046d912007-01-28 01:53:16 +00001382 }
1383#ifdef DEBUG_GDB
1384 else if (ch == '+')
1385 printf("Got ACK\n");
1386 else
1387 printf("Got '%c' when expecting ACK/NACK\n", ch);
1388#endif
1389 if (ch == '+' || ch == '$')
1390 s->last_packet_len = 0;
1391 if (ch != '$')
1392 return;
1393 }
Luiz Capitulino13548692011-07-29 15:36:43 -03001394 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00001395 /* when the CPU is running, we cannot do anything except stop
1396 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001397 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00001398 } else
bellard1fddef42005-04-17 19:16:13 +00001399#endif
bellard41625032005-04-24 10:07:11 +00001400 {
bellard858693c2004-03-31 18:52:07 +00001401 switch(s->state) {
1402 case RS_IDLE:
1403 if (ch == '$') {
1404 s->line_buf_index = 0;
1405 s->state = RS_GETLINE;
bellard4c3a88a2003-07-26 12:06:08 +00001406 }
1407 break;
bellard858693c2004-03-31 18:52:07 +00001408 case RS_GETLINE:
1409 if (ch == '#') {
1410 s->state = RS_CHKSUM1;
1411 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1412 s->state = RS_IDLE;
1413 } else {
1414 s->line_buf[s->line_buf_index++] = ch;
1415 }
1416 break;
1417 case RS_CHKSUM1:
1418 s->line_buf[s->line_buf_index] = '\0';
1419 s->line_csum = fromhex(ch) << 4;
1420 s->state = RS_CHKSUM2;
1421 break;
1422 case RS_CHKSUM2:
1423 s->line_csum |= fromhex(ch);
1424 csum = 0;
1425 for(i = 0; i < s->line_buf_index; i++) {
1426 csum += s->line_buf[i];
1427 }
1428 if (s->line_csum != (csum & 0xff)) {
ths60fe76f2007-12-16 03:02:09 +00001429 reply = '-';
1430 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00001431 s->state = RS_IDLE;
1432 } else {
ths60fe76f2007-12-16 03:02:09 +00001433 reply = '+';
1434 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00001435 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00001436 }
bellardb4608c02003-06-27 17:34:32 +00001437 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001438 default:
1439 abort();
bellardb4608c02003-06-27 17:34:32 +00001440 }
1441 }
bellard858693c2004-03-31 18:52:07 +00001442}
1443
Paul Brook0e1c9c52010-06-16 13:03:51 +01001444/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001445void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01001446{
1447 GDBState *s;
1448 char buf[4];
1449
1450 s = gdbserver_state;
1451 if (!s) {
1452 return;
1453 }
1454#ifdef CONFIG_USER_ONLY
1455 if (gdbserver_fd < 0 || s->fd < 0) {
1456 return;
1457 }
Paolo Bonzini3d0f4412015-03-02 13:26:58 +01001458#else
1459 if (!s->chr) {
1460 return;
1461 }
Paul Brook0e1c9c52010-06-16 13:03:51 +01001462#endif
1463
1464 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
1465 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001466
1467#ifndef CONFIG_USER_ONLY
Paolo Bonzini3d0f4412015-03-02 13:26:58 +01001468 qemu_chr_delete(s->chr);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001469#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01001470}
1471
bellard1fddef42005-04-17 19:16:13 +00001472#ifdef CONFIG_USER_ONLY
1473int
aurel32ca587a82008-12-18 22:44:13 +00001474gdb_queuesig (void)
1475{
1476 GDBState *s;
1477
1478 s = gdbserver_state;
1479
1480 if (gdbserver_fd < 0 || s->fd < 0)
1481 return 0;
1482 else
1483 return 1;
1484}
1485
1486int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001487gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00001488{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001489 GDBState *s;
1490 char buf[256];
1491 int n;
bellard1fddef42005-04-17 19:16:13 +00001492
Andreas Färber5ca666c2013-06-24 19:20:57 +02001493 s = gdbserver_state;
1494 if (gdbserver_fd < 0 || s->fd < 0) {
1495 return sig;
bellard1fddef42005-04-17 19:16:13 +00001496 }
1497
Andreas Färber5ca666c2013-06-24 19:20:57 +02001498 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001499 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001500 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00001501
Andreas Färber5ca666c2013-06-24 19:20:57 +02001502 if (sig != 0) {
1503 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
1504 put_packet(s, buf);
1505 }
1506 /* put_packet() might have detected that the peer terminated the
1507 connection. */
1508 if (s->fd < 0) {
1509 return sig;
1510 }
1511
1512 sig = 0;
1513 s->state = RS_IDLE;
1514 s->running_state = 0;
1515 while (s->running_state == 0) {
1516 n = read(s->fd, buf, 256);
1517 if (n > 0) {
1518 int i;
1519
1520 for (i = 0; i < n; i++) {
1521 gdb_read_byte(s, buf[i]);
1522 }
1523 } else if (n == 0 || errno != EAGAIN) {
1524 /* XXX: Connection closed. Should probably wait for another
1525 connection before continuing. */
1526 return sig;
bellard1fddef42005-04-17 19:16:13 +00001527 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02001528 }
1529 sig = s->signal;
1530 s->signal = 0;
1531 return sig;
bellard1fddef42005-04-17 19:16:13 +00001532}
bellarde9009672005-04-26 20:42:36 +00001533
aurel32ca587a82008-12-18 22:44:13 +00001534/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001535void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00001536{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001537 GDBState *s;
1538 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00001539
Andreas Färber5ca666c2013-06-24 19:20:57 +02001540 s = gdbserver_state;
1541 if (gdbserver_fd < 0 || s->fd < 0) {
1542 return;
1543 }
aurel32ca587a82008-12-18 22:44:13 +00001544
Andreas Färber5ca666c2013-06-24 19:20:57 +02001545 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
1546 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00001547}
bellard1fddef42005-04-17 19:16:13 +00001548
aliguori880a7572008-11-18 20:30:24 +00001549static void gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00001550{
1551 GDBState *s;
1552 struct sockaddr_in sockaddr;
1553 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001554 int fd;
bellard858693c2004-03-31 18:52:07 +00001555
1556 for(;;) {
1557 len = sizeof(sockaddr);
1558 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1559 if (fd < 0 && errno != EINTR) {
1560 perror("accept");
1561 return;
1562 } else if (fd >= 0) {
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001563#ifndef _WIN32
1564 fcntl(fd, F_SETFD, FD_CLOEXEC);
1565#endif
bellard858693c2004-03-31 18:52:07 +00001566 break;
1567 }
1568 }
1569
1570 /* set short latency */
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001571 socket_set_nodelay(fd);
ths3b46e622007-09-17 08:09:54 +00001572
Anthony Liguori7267c092011-08-20 22:09:37 -05001573 s = g_malloc0(sizeof(GDBState));
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001574 s->c_cpu = first_cpu;
1575 s->g_cpu = first_cpu;
bellard858693c2004-03-31 18:52:07 +00001576 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02001577 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00001578
aliguori880a7572008-11-18 20:30:24 +00001579 gdbserver_state = s;
pbrooka2d1eba2007-01-28 03:10:55 +00001580
bellard858693c2004-03-31 18:52:07 +00001581 fcntl(fd, F_SETFL, O_NONBLOCK);
bellard858693c2004-03-31 18:52:07 +00001582}
1583
1584static int gdbserver_open(int port)
1585{
1586 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001587 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00001588
1589 fd = socket(PF_INET, SOCK_STREAM, 0);
1590 if (fd < 0) {
1591 perror("socket");
1592 return -1;
1593 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001594#ifndef _WIN32
1595 fcntl(fd, F_SETFD, FD_CLOEXEC);
1596#endif
bellard858693c2004-03-31 18:52:07 +00001597
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001598 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00001599
1600 sockaddr.sin_family = AF_INET;
1601 sockaddr.sin_port = htons(port);
1602 sockaddr.sin_addr.s_addr = 0;
1603 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1604 if (ret < 0) {
1605 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00001606 close(fd);
bellard858693c2004-03-31 18:52:07 +00001607 return -1;
1608 }
1609 ret = listen(fd, 0);
1610 if (ret < 0) {
1611 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00001612 close(fd);
bellard858693c2004-03-31 18:52:07 +00001613 return -1;
1614 }
bellard858693c2004-03-31 18:52:07 +00001615 return fd;
1616}
1617
1618int gdbserver_start(int port)
1619{
1620 gdbserver_fd = gdbserver_open(port);
1621 if (gdbserver_fd < 0)
1622 return -1;
1623 /* accept connections */
aliguori880a7572008-11-18 20:30:24 +00001624 gdb_accept();
bellardb4608c02003-06-27 17:34:32 +00001625 return 0;
1626}
aurel322b1319c2008-12-18 22:44:04 +00001627
1628/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07001629void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00001630{
1631 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02001632
1633 if (gdbserver_fd < 0 || s->fd < 0) {
1634 return;
1635 }
aurel322b1319c2008-12-18 22:44:04 +00001636 close(s->fd);
1637 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001638 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02001639 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00001640}
pbrook4046d912007-01-28 01:53:16 +00001641#else
thsaa1f17c2007-07-11 22:48:58 +00001642static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00001643{
pbrook56aebc82008-10-11 17:55:29 +00001644 /* We can handle an arbitrarily large amount of data.
1645 Pick the maximum packet size, which is as good as anything. */
1646 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00001647}
1648
thsaa1f17c2007-07-11 22:48:58 +00001649static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00001650{
pbrook4046d912007-01-28 01:53:16 +00001651 int i;
1652
1653 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00001654 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00001655 }
1656}
1657
1658static void gdb_chr_event(void *opaque, int event)
1659{
1660 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05301661 case CHR_EVENT_OPENED:
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001662 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02001663 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00001664 break;
1665 default:
1666 break;
1667 }
1668}
1669
aliguori8a34a0f2009-03-05 23:01:55 +00001670static void gdb_monitor_output(GDBState *s, const char *msg, int len)
1671{
1672 char buf[MAX_PACKET_LENGTH];
1673
1674 buf[0] = 'O';
1675 if (len > (MAX_PACKET_LENGTH/2) - 1)
1676 len = (MAX_PACKET_LENGTH/2) - 1;
1677 memtohex(buf + 1, (uint8_t *)msg, len);
1678 put_packet(s, buf);
1679}
1680
1681static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
1682{
1683 const char *p = (const char *)buf;
1684 int max_sz;
1685
1686 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
1687 for (;;) {
1688 if (len <= max_sz) {
1689 gdb_monitor_output(gdbserver_state, p, len);
1690 break;
1691 }
1692 gdb_monitor_output(gdbserver_state, p, max_sz);
1693 p += max_sz;
1694 len -= max_sz;
1695 }
1696 return len;
1697}
1698
aliguori59030a82009-04-05 18:43:41 +00001699#ifndef _WIN32
1700static void gdb_sigterm_handler(int signal)
1701{
Luiz Capitulino13548692011-07-29 15:36:43 -03001702 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001703 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001704 }
aliguori59030a82009-04-05 18:43:41 +00001705}
1706#endif
1707
1708int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00001709{
1710 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00001711 char gdbstub_device_name[128];
aliguori36556b22009-03-28 18:05:53 +00001712 CharDriverState *chr = NULL;
1713 CharDriverState *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00001714
aliguori59030a82009-04-05 18:43:41 +00001715 if (!device)
1716 return -1;
1717 if (strcmp(device, "none") != 0) {
1718 if (strstart(device, "tcp:", NULL)) {
1719 /* enforce required TCP attributes */
1720 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
1721 "%s,nowait,nodelay,server", device);
1722 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00001723 }
aliguori59030a82009-04-05 18:43:41 +00001724#ifndef _WIN32
1725 else if (strcmp(device, "stdio") == 0) {
1726 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00001727
aliguori59030a82009-04-05 18:43:41 +00001728 memset(&act, 0, sizeof(act));
1729 act.sa_handler = gdb_sigterm_handler;
1730 sigaction(SIGINT, &act, NULL);
1731 }
1732#endif
Anthony Liguori27143a42011-08-15 11:17:36 -05001733 chr = qemu_chr_new("gdb", device, NULL);
aliguori36556b22009-03-28 18:05:53 +00001734 if (!chr)
1735 return -1;
1736
Hans de Goede456d6062013-03-27 20:29:40 +01001737 qemu_chr_fe_claim_no_fail(chr);
aliguori36556b22009-03-28 18:05:53 +00001738 qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
1739 gdb_chr_event, NULL);
pbrookcfc34752007-02-22 01:48:01 +00001740 }
1741
aliguori36556b22009-03-28 18:05:53 +00001742 s = gdbserver_state;
1743 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001744 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00001745 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00001746
aliguori36556b22009-03-28 18:05:53 +00001747 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
1748
1749 /* Initialize a monitor terminal for gdb */
Pavel Dovgalyuk462efe92014-09-10 18:34:14 +04001750 mon_chr = qemu_chr_alloc();
aliguori36556b22009-03-28 18:05:53 +00001751 mon_chr->chr_write = gdb_monitor_write;
1752 monitor_init(mon_chr, 0);
1753 } else {
1754 if (s->chr)
Anthony Liguori70f24fb2011-08-15 11:17:38 -05001755 qemu_chr_delete(s->chr);
aliguori36556b22009-03-28 18:05:53 +00001756 mon_chr = s->mon_chr;
1757 memset(s, 0, sizeof(GDBState));
1758 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001759 s->c_cpu = first_cpu;
1760 s->g_cpu = first_cpu;
pbrook4046d912007-01-28 01:53:16 +00001761 s->chr = chr;
aliguori36556b22009-03-28 18:05:53 +00001762 s->state = chr ? RS_IDLE : RS_INACTIVE;
1763 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00001764 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00001765
pbrook4046d912007-01-28 01:53:16 +00001766 return 0;
1767}
1768#endif