blob: e4a1a793843f259b62c50709eace79144ee7c299 [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
Liviu Ionescua38bb072014-12-11 12:07:48 +0000320int semihosting_target = SEMIHOSTING_TARGET_AUTO;
321
bellard1fddef42005-04-17 19:16:13 +0000322#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000323/* XXX: This is not thread safe. Do we care? */
324static int gdbserver_fd = -1;
325
bellard858693c2004-03-31 18:52:07 +0000326static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000327{
328 uint8_t ch;
329 int ret;
330
331 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000332 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000333 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000334 if (errno == ECONNRESET)
335 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000336 if (errno != EINTR && errno != EAGAIN)
337 return -1;
338 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000339 close(s->fd);
340 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000341 return -1;
342 } else {
343 break;
344 }
345 }
346 return ch;
347}
pbrook4046d912007-01-28 01:53:16 +0000348#endif
bellardb4608c02003-06-27 17:34:32 +0000349
blueswir1654efcf2009-04-18 07:29:59 +0000350static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000351 GDB_SYS_UNKNOWN,
352 GDB_SYS_ENABLED,
353 GDB_SYS_DISABLED,
354} gdb_syscall_mode;
355
Liviu Ionescua38bb072014-12-11 12:07:48 +0000356/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000357int use_gdb_syscalls(void)
358{
Liviu Ionescua38bb072014-12-11 12:07:48 +0000359 if (semihosting_target == SEMIHOSTING_TARGET_NATIVE) {
360 /* -semihosting-config target=native */
361 return false;
362 } else if (semihosting_target == SEMIHOSTING_TARGET_GDB) {
363 /* -semihosting-config target=gdb */
364 return true;
365 }
366
367 /* -semihosting-config target=auto */
368 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000369 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000370 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
371 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000372 }
373 return gdb_syscall_mode == GDB_SYS_ENABLED;
374}
375
edgar_iglba70a622008-03-14 06:10:42 +0000376/* Resume execution. */
377static inline void gdb_continue(GDBState *s)
378{
379#ifdef CONFIG_USER_ONLY
380 s->running_state = 1;
381#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200382 if (!runstate_needs_reset()) {
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200383 vm_start();
384 }
edgar_iglba70a622008-03-14 06:10:42 +0000385#endif
386}
387
bellard858693c2004-03-31 18:52:07 +0000388static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000389{
pbrook4046d912007-01-28 01:53:16 +0000390#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000391 int ret;
392
393 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000394 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000395 if (ret < 0) {
396 if (errno != EINTR && errno != EAGAIN)
397 return;
398 } else {
399 buf += ret;
400 len -= ret;
401 }
402 }
pbrook4046d912007-01-28 01:53:16 +0000403#else
Anthony Liguori2cc6e0a2011-08-15 11:17:28 -0500404 qemu_chr_fe_write(s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000405#endif
bellardb4608c02003-06-27 17:34:32 +0000406}
407
408static inline int fromhex(int v)
409{
410 if (v >= '0' && v <= '9')
411 return v - '0';
412 else if (v >= 'A' && v <= 'F')
413 return v - 'A' + 10;
414 else if (v >= 'a' && v <= 'f')
415 return v - 'a' + 10;
416 else
417 return 0;
418}
419
420static inline int tohex(int v)
421{
422 if (v < 10)
423 return v + '0';
424 else
425 return v - 10 + 'a';
426}
427
428static void memtohex(char *buf, const uint8_t *mem, int len)
429{
430 int i, c;
431 char *q;
432 q = buf;
433 for(i = 0; i < len; i++) {
434 c = mem[i];
435 *q++ = tohex(c >> 4);
436 *q++ = tohex(c & 0xf);
437 }
438 *q = '\0';
439}
440
441static void hextomem(uint8_t *mem, const char *buf, int len)
442{
443 int i;
444
445 for(i = 0; i < len; i++) {
446 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
447 buf += 2;
448 }
449}
450
bellardb4608c02003-06-27 17:34:32 +0000451/* return -1 if error, 0 if OK */
pbrook56aebc82008-10-11 17:55:29 +0000452static int put_packet_binary(GDBState *s, const char *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000453{
pbrook56aebc82008-10-11 17:55:29 +0000454 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000455 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000456
bellardb4608c02003-06-27 17:34:32 +0000457 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000458 p = s->last_packet;
459 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000460 memcpy(p, buf, len);
461 p += len;
bellardb4608c02003-06-27 17:34:32 +0000462 csum = 0;
463 for(i = 0; i < len; i++) {
464 csum += buf[i];
465 }
pbrook4046d912007-01-28 01:53:16 +0000466 *(p++) = '#';
467 *(p++) = tohex((csum >> 4) & 0xf);
468 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000469
pbrook4046d912007-01-28 01:53:16 +0000470 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000471 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000472
pbrook4046d912007-01-28 01:53:16 +0000473#ifdef CONFIG_USER_ONLY
474 i = get_char(s);
475 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000476 return -1;
pbrook4046d912007-01-28 01:53:16 +0000477 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000478 break;
pbrook4046d912007-01-28 01:53:16 +0000479#else
480 break;
481#endif
bellardb4608c02003-06-27 17:34:32 +0000482 }
483 return 0;
484}
485
pbrook56aebc82008-10-11 17:55:29 +0000486/* return -1 if error, 0 if OK */
487static int put_packet(GDBState *s, const char *buf)
488{
489#ifdef DEBUG_GDB
490 printf("reply='%s'\n", buf);
491#endif
492
493 return put_packet_binary(s, buf, strlen(buf));
494}
495
pbrook56aebc82008-10-11 17:55:29 +0000496/* Encode data using the encoding for 'x' packets. */
497static int memtox(char *buf, const char *mem, int len)
498{
499 char *p = buf;
500 char c;
501
502 while (len--) {
503 c = *(mem++);
504 switch (c) {
505 case '#': case '$': case '*': case '}':
506 *(p++) = '}';
507 *(p++) = c ^ 0x20;
508 break;
509 default:
510 *(p++) = c;
511 break;
512 }
513 }
514 return p - buf;
515}
516
Andreas Färber5b24c642013-07-07 15:08:22 +0200517static const char *get_feature_xml(const char *p, const char **newp,
518 CPUClass *cc)
pbrook56aebc82008-10-11 17:55:29 +0000519{
pbrook56aebc82008-10-11 17:55:29 +0000520 size_t len;
521 int i;
522 const char *name;
523 static char target_xml[1024];
524
525 len = 0;
526 while (p[len] && p[len] != ':')
527 len++;
528 *newp = p + len;
529
530 name = NULL;
531 if (strncmp(p, "target.xml", len) == 0) {
532 /* Generate the XML description for this CPU. */
533 if (!target_xml[0]) {
534 GDBRegisterState *r;
Andreas Färbereac8b352013-06-28 21:11:37 +0200535 CPUState *cpu = first_cpu;
pbrook56aebc82008-10-11 17:55:29 +0000536
blueswir15b3715b2008-10-25 11:18:12 +0000537 snprintf(target_xml, sizeof(target_xml),
538 "<?xml version=\"1.0\"?>"
539 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
540 "<target>"
541 "<xi:include href=\"%s\"/>",
Andreas Färber5b24c642013-07-07 15:08:22 +0200542 cc->gdb_core_xml_file);
pbrook56aebc82008-10-11 17:55:29 +0000543
Andreas Färbereac8b352013-06-28 21:11:37 +0200544 for (r = cpu->gdb_regs; r; r = r->next) {
blueswir12dc766d2009-04-13 16:06:19 +0000545 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
546 pstrcat(target_xml, sizeof(target_xml), r->xml);
547 pstrcat(target_xml, sizeof(target_xml), "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000548 }
blueswir12dc766d2009-04-13 16:06:19 +0000549 pstrcat(target_xml, sizeof(target_xml), "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000550 }
551 return target_xml;
552 }
553 for (i = 0; ; i++) {
554 name = xml_builtin[i][0];
555 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
556 break;
557 }
558 return name ? xml_builtin[i][1] : NULL;
559}
pbrook56aebc82008-10-11 17:55:29 +0000560
Andreas Färber385b9f02013-06-27 18:25:36 +0200561static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000562{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200563 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200564 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000565 GDBRegisterState *r;
566
Andreas Färbera0e372f2013-06-28 23:18:47 +0200567 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200568 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200569 }
pbrook56aebc82008-10-11 17:55:29 +0000570
Andreas Färbereac8b352013-06-28 21:11:37 +0200571 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000572 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
573 return r->get_reg(env, mem_buf, reg - r->base_reg);
574 }
575 }
576 return 0;
577}
578
Andreas Färber385b9f02013-06-27 18:25:36 +0200579static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000580{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200581 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200582 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000583 GDBRegisterState *r;
584
Andreas Färbera0e372f2013-06-28 23:18:47 +0200585 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200586 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200587 }
pbrook56aebc82008-10-11 17:55:29 +0000588
Andreas Färbereac8b352013-06-28 21:11:37 +0200589 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000590 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
591 return r->set_reg(env, mem_buf, reg - r->base_reg);
592 }
593 }
594 return 0;
595}
596
597/* Register a supplemental set of CPU registers. If g_pos is nonzero it
598 specifies the first register number and these registers are included in
599 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
600 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
601 */
602
Andreas Färber22169d42013-06-28 21:27:39 +0200603void gdb_register_coprocessor(CPUState *cpu,
604 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
605 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000606{
607 GDBRegisterState *s;
608 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000609
Andreas Färbereac8b352013-06-28 21:11:37 +0200610 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000611 while (*p) {
612 /* Check for duplicates. */
613 if (strcmp((*p)->xml, xml) == 0)
614 return;
615 p = &(*p)->next;
616 }
Stefan Weil9643c252011-10-18 22:25:38 +0200617
618 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200619 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200620 s->num_regs = num_regs;
621 s->get_reg = get_reg;
622 s->set_reg = set_reg;
623 s->xml = xml;
624
pbrook56aebc82008-10-11 17:55:29 +0000625 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200626 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000627 *p = s;
628 if (g_pos) {
629 if (g_pos != s->base_reg) {
630 fprintf(stderr, "Error: Bad gdb register numbering for '%s'\n"
631 "Expected %d got %d\n", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200632 } else {
633 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000634 }
635 }
636}
637
aliguoria1d1bb32008-11-18 20:07:32 +0000638#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100639/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
640static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
641{
642 static const int xlat[] = {
643 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
644 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
645 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
646 };
647
648 CPUClass *cc = CPU_GET_CLASS(cpu);
649 int cputype = xlat[gdbtype];
650
651 if (cc->gdb_stop_before_watchpoint) {
652 cputype |= BP_STOP_BEFORE_ACCESS;
653 }
654 return cputype;
655}
aliguoria1d1bb32008-11-18 20:07:32 +0000656#endif
657
aliguori880a7572008-11-18 20:30:24 +0000658static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000659{
Andreas Färber182735e2013-05-29 22:29:20 +0200660 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000661 int err = 0;
662
Andreas Färber62278812013-06-27 17:12:06 +0200663 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200664 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200665 }
aliguorie22a25c2009-03-12 20:12:48 +0000666
aliguoria1d1bb32008-11-18 20:07:32 +0000667 switch (type) {
668 case GDB_BREAKPOINT_SW:
669 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200670 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200671 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
672 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000673 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200674 }
aliguori880a7572008-11-18 20:30:24 +0000675 }
676 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000677#ifndef CONFIG_USER_ONLY
678 case GDB_WATCHPOINT_WRITE:
679 case GDB_WATCHPOINT_READ:
680 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200681 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100682 err = cpu_watchpoint_insert(cpu, addr, len,
683 xlat_gdb_type(cpu, type), NULL);
684 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000685 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +0100686 }
aliguori880a7572008-11-18 20:30:24 +0000687 }
688 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000689#endif
690 default:
691 return -ENOSYS;
692 }
693}
694
aliguori880a7572008-11-18 20:30:24 +0000695static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000696{
Andreas Färber182735e2013-05-29 22:29:20 +0200697 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000698 int err = 0;
699
Andreas Färber62278812013-06-27 17:12:06 +0200700 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200701 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200702 }
aliguorie22a25c2009-03-12 20:12:48 +0000703
aliguoria1d1bb32008-11-18 20:07:32 +0000704 switch (type) {
705 case GDB_BREAKPOINT_SW:
706 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200707 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200708 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
709 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000710 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200711 }
aliguori880a7572008-11-18 20:30:24 +0000712 }
713 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000714#ifndef CONFIG_USER_ONLY
715 case GDB_WATCHPOINT_WRITE:
716 case GDB_WATCHPOINT_READ:
717 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200718 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100719 err = cpu_watchpoint_remove(cpu, addr, len,
720 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +0000721 if (err)
722 break;
723 }
724 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000725#endif
726 default:
727 return -ENOSYS;
728 }
729}
730
aliguori880a7572008-11-18 20:30:24 +0000731static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +0000732{
Andreas Färber182735e2013-05-29 22:29:20 +0200733 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000734
aliguorie22a25c2009-03-12 20:12:48 +0000735 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200736 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +0000737 return;
738 }
739
Andreas Färberbdc44642013-06-24 23:50:24 +0200740 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200741 cpu_breakpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000742#ifndef CONFIG_USER_ONLY
Andreas Färber75a34032013-09-02 16:57:02 +0200743 cpu_watchpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000744#endif
aliguori880a7572008-11-18 20:30:24 +0000745 }
aliguoria1d1bb32008-11-18 20:07:32 +0000746}
747
aurel32fab9d282009-04-08 21:29:37 +0000748static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
749{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200750 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +0200751 CPUClass *cc = CPU_GET_CLASS(cpu);
752
753 cpu_synchronize_state(cpu);
754 if (cc->set_pc) {
755 cc->set_pc(cpu, pc);
Nathan Froydff1d1972009-12-08 08:06:30 -0800756 }
aurel32fab9d282009-04-08 21:29:37 +0000757}
758
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200759static CPUState *find_cpu(uint32_t thread_id)
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700760{
Andreas Färber0d342822012-12-17 07:12:13 +0100761 CPUState *cpu;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700762
Andreas Färberbdc44642013-06-24 23:50:24 +0200763 CPU_FOREACH(cpu) {
Andreas Färberaa48dd92013-07-09 20:50:52 +0200764 if (cpu_index(cpu) == thread_id) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200765 return cpu;
Andreas Färberaa48dd92013-07-09 20:50:52 +0200766 }
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700767 }
Andreas Färberaa48dd92013-07-09 20:50:52 +0200768
769 return NULL;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700770}
771
aliguori880a7572008-11-18 20:30:24 +0000772static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +0000773{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200774 CPUState *cpu;
Andreas Färber5b24c642013-07-07 15:08:22 +0200775 CPUClass *cc;
bellardb4608c02003-06-27 17:34:32 +0000776 const char *p;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700777 uint32_t thread;
778 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +0000779 char buf[MAX_PACKET_LENGTH];
780 uint8_t mem_buf[MAX_PACKET_LENGTH];
781 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +0000782 target_ulong addr, len;
ths3b46e622007-09-17 08:09:54 +0000783
bellard858693c2004-03-31 18:52:07 +0000784#ifdef DEBUG_GDB
785 printf("command='%s'\n", line_buf);
bellard4c3a88a2003-07-26 12:06:08 +0000786#endif
bellard858693c2004-03-31 18:52:07 +0000787 p = line_buf;
788 ch = *p++;
789 switch(ch) {
790 case '?':
bellard1fddef42005-04-17 19:16:13 +0000791 /* TODO: Make this return the correct value for user-mode. */
aurel32ca587a82008-12-18 22:44:13 +0000792 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200793 cpu_index(s->c_cpu));
bellard858693c2004-03-31 18:52:07 +0000794 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +0000795 /* Remove all the breakpoints when this query is issued,
796 * because gdb is doing and initial connect and the state
797 * should be cleaned up.
798 */
aliguori880a7572008-11-18 20:30:24 +0000799 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +0000800 break;
801 case 'c':
802 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +0000803 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +0000804 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +0000805 }
aurel32ca587a82008-12-18 22:44:13 +0000806 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +0000807 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +0000808 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +0000809 case 'C':
aurel32ca587a82008-12-18 22:44:13 +0000810 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
811 if (s->signal == -1)
812 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +0000813 gdb_continue(s);
814 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200815 case 'v':
816 if (strncmp(p, "Cont", 4) == 0) {
817 int res_signal, res_thread;
818
819 p += 4;
820 if (*p == '?') {
821 put_packet(s, "vCont;c;C;s;S");
822 break;
823 }
824 res = 0;
825 res_signal = 0;
826 res_thread = 0;
827 while (*p) {
828 int action, signal;
829
830 if (*p++ != ';') {
831 res = 0;
832 break;
833 }
834 action = *p++;
835 signal = 0;
836 if (action == 'C' || action == 'S') {
Martin Simmonsf17b0692014-11-05 14:47:39 +0000837 signal = gdb_signal_to_target(strtoul(p, (char **)&p, 16));
838 if (signal == -1) {
839 signal = 0;
840 }
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200841 } else if (action != 'c' && action != 's') {
842 res = 0;
843 break;
844 }
845 thread = 0;
846 if (*p == ':') {
847 thread = strtoull(p+1, (char **)&p, 16);
848 }
849 action = tolower(action);
850 if (res == 0 || (res == 'c' && action == 's')) {
851 res = action;
852 res_signal = signal;
853 res_thread = thread;
854 }
855 }
856 if (res) {
857 if (res_thread != -1 && res_thread != 0) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200858 cpu = find_cpu(res_thread);
859 if (cpu == NULL) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200860 put_packet(s, "E22");
861 break;
862 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200863 s->c_cpu = cpu;
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200864 }
865 if (res == 's') {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200866 cpu_single_step(s->c_cpu, sstep_flags);
Jan Kiszkadd32aa12009-06-27 09:53:51 +0200867 }
868 s->signal = res_signal;
869 gdb_continue(s);
870 return RS_IDLE;
871 }
872 break;
873 } else {
874 goto unknown_command;
875 }
edgar_igl7d03f822008-05-17 18:58:29 +0000876 case 'k':
Jan Kiszka00e94db2012-03-06 18:32:35 +0100877#ifdef CONFIG_USER_ONLY
edgar_igl7d03f822008-05-17 18:58:29 +0000878 /* Kill the target */
879 fprintf(stderr, "\nQEMU: Terminated via GDBstub\n");
880 exit(0);
Jan Kiszka00e94db2012-03-06 18:32:35 +0100881#endif
edgar_igl7d03f822008-05-17 18:58:29 +0000882 case 'D':
883 /* Detach packet */
aliguori880a7572008-11-18 20:30:24 +0000884 gdb_breakpoint_remove_all();
Daniel Gutson7ea06da2010-02-26 14:13:50 -0300885 gdb_syscall_mode = GDB_SYS_DISABLED;
edgar_igl7d03f822008-05-17 18:58:29 +0000886 gdb_continue(s);
887 put_packet(s, "OK");
888 break;
bellard858693c2004-03-31 18:52:07 +0000889 case 's':
890 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +0000891 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +0000892 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +0000893 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200894 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +0000895 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +0000896 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +0000897 case 'F':
898 {
899 target_ulong ret;
900 target_ulong err;
901
902 ret = strtoull(p, (char **)&p, 16);
903 if (*p == ',') {
904 p++;
905 err = strtoull(p, (char **)&p, 16);
906 } else {
907 err = 0;
908 }
909 if (*p == ',')
910 p++;
911 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +0000912 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200913 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +0000914 s->current_syscall_cb = NULL;
915 }
pbrooka2d1eba2007-01-28 03:10:55 +0000916 if (type == 'C') {
917 put_packet(s, "T02");
918 } else {
edgar_iglba70a622008-03-14 06:10:42 +0000919 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +0000920 }
921 }
922 break;
bellard858693c2004-03-31 18:52:07 +0000923 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200924 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +0000925 len = 0;
Andreas Färber35143f02013-08-12 18:09:47 +0200926 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200927 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +0000928 len += reg_size;
929 }
930 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +0000931 put_packet(s, buf);
932 break;
933 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200934 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +0000935 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +0000936 len = strlen(p) / 2;
937 hextomem((uint8_t *)registers, p, len);
Andreas Färber35143f02013-08-12 18:09:47 +0200938 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200939 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +0000940 len -= reg_size;
941 registers += reg_size;
942 }
bellard858693c2004-03-31 18:52:07 +0000943 put_packet(s, "OK");
944 break;
945 case 'm':
bellard9d9754a2006-06-25 15:32:37 +0000946 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +0000947 if (*p == ',')
948 p++;
bellard9d9754a2006-06-25 15:32:37 +0000949 len = strtoull(p, NULL, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200950 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +0000951 put_packet (s, "E14");
952 } else {
953 memtohex(buf, mem_buf, len);
954 put_packet(s, buf);
955 }
bellard858693c2004-03-31 18:52:07 +0000956 break;
957 case 'M':
bellard9d9754a2006-06-25 15:32:37 +0000958 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +0000959 if (*p == ',')
960 p++;
bellard9d9754a2006-06-25 15:32:37 +0000961 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +0000962 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +0000963 p++;
964 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200965 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +0200966 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +0000967 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +0200968 } else {
bellard858693c2004-03-31 18:52:07 +0000969 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +0200970 }
bellard858693c2004-03-31 18:52:07 +0000971 break;
pbrook56aebc82008-10-11 17:55:29 +0000972 case 'p':
973 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
974 This works, but can be very slow. Anything new enough to
975 understand XML also knows how to use this properly. */
976 if (!gdb_has_xml)
977 goto unknown_command;
978 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200979 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +0000980 if (reg_size) {
981 memtohex(buf, mem_buf, reg_size);
982 put_packet(s, buf);
983 } else {
984 put_packet(s, "E14");
985 }
986 break;
987 case 'P':
988 if (!gdb_has_xml)
989 goto unknown_command;
990 addr = strtoull(p, (char **)&p, 16);
991 if (*p == '=')
992 p++;
993 reg_size = strlen(p) / 2;
994 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200995 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +0000996 put_packet(s, "OK");
997 break;
bellard858693c2004-03-31 18:52:07 +0000998 case 'Z':
bellard858693c2004-03-31 18:52:07 +0000999 case 'z':
1000 type = strtoul(p, (char **)&p, 16);
1001 if (*p == ',')
1002 p++;
bellard9d9754a2006-06-25 15:32:37 +00001003 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001004 if (*p == ',')
1005 p++;
bellard9d9754a2006-06-25 15:32:37 +00001006 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001007 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001008 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001009 else
aliguori880a7572008-11-18 20:30:24 +00001010 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001011 if (res >= 0)
1012 put_packet(s, "OK");
1013 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001014 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001015 else
1016 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001017 break;
aliguori880a7572008-11-18 20:30:24 +00001018 case 'H':
1019 type = *p++;
1020 thread = strtoull(p, (char **)&p, 16);
1021 if (thread == -1 || thread == 0) {
1022 put_packet(s, "OK");
1023 break;
1024 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001025 cpu = find_cpu(thread);
1026 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001027 put_packet(s, "E22");
1028 break;
1029 }
1030 switch (type) {
1031 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001032 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001033 put_packet(s, "OK");
1034 break;
1035 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001036 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001037 put_packet(s, "OK");
1038 break;
1039 default:
1040 put_packet(s, "E22");
1041 break;
1042 }
1043 break;
1044 case 'T':
1045 thread = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001046 cpu = find_cpu(thread);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001047
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001048 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001049 put_packet(s, "OK");
1050 } else {
aliguori880a7572008-11-18 20:30:24 +00001051 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001052 }
aliguori880a7572008-11-18 20:30:24 +00001053 break;
pbrook978efd62006-06-17 18:30:42 +00001054 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001055 case 'Q':
1056 /* parse any 'q' packets here */
1057 if (!strcmp(p,"qemu.sstepbits")) {
1058 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001059 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1060 SSTEP_ENABLE,
1061 SSTEP_NOIRQ,
1062 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001063 put_packet(s, buf);
1064 break;
1065 } else if (strncmp(p,"qemu.sstep",10) == 0) {
1066 /* Display or change the sstep_flags */
1067 p += 10;
1068 if (*p != '=') {
1069 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001070 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001071 put_packet(s, buf);
1072 break;
1073 }
1074 p++;
1075 type = strtoul(p, (char **)&p, 16);
1076 sstep_flags = type;
1077 put_packet(s, "OK");
1078 break;
aliguori880a7572008-11-18 20:30:24 +00001079 } else if (strcmp(p,"C") == 0) {
1080 /* "Current thread" remains vague in the spec, so always return
1081 * the first CPU (gdb returns the first thread). */
1082 put_packet(s, "QC1");
1083 break;
1084 } else if (strcmp(p,"fThreadInfo") == 0) {
Andreas Färber52f34622013-06-27 13:44:40 +02001085 s->query_cpu = first_cpu;
aliguori880a7572008-11-18 20:30:24 +00001086 goto report_cpuinfo;
1087 } else if (strcmp(p,"sThreadInfo") == 0) {
1088 report_cpuinfo:
1089 if (s->query_cpu) {
Andreas Färber52f34622013-06-27 13:44:40 +02001090 snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu));
aliguori880a7572008-11-18 20:30:24 +00001091 put_packet(s, buf);
Andreas Färberbdc44642013-06-24 23:50:24 +02001092 s->query_cpu = CPU_NEXT(s->query_cpu);
aliguori880a7572008-11-18 20:30:24 +00001093 } else
1094 put_packet(s, "l");
1095 break;
1096 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1097 thread = strtoull(p+16, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001098 cpu = find_cpu(thread);
1099 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001100 cpu_synchronize_state(cpu);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001101 len = snprintf((char *)mem_buf, sizeof(mem_buf),
Andreas Färber55e5c282012-12-17 06:18:02 +01001102 "CPU#%d [%s]", cpu->cpu_index,
Andreas Färber259186a2013-01-17 18:51:17 +01001103 cpu->halted ? "halted " : "running");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001104 memtohex(buf, mem_buf, len);
1105 put_packet(s, buf);
1106 }
aliguori880a7572008-11-18 20:30:24 +00001107 break;
edgar_igl60897d32008-05-09 08:25:14 +00001108 }
blueswir10b8a9882009-03-07 10:51:36 +00001109#ifdef CONFIG_USER_ONLY
edgar_igl60897d32008-05-09 08:25:14 +00001110 else if (strncmp(p, "Offsets", 7) == 0) {
Andreas Färber0429a972013-08-26 18:14:44 +02001111 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001112
blueswir1363a37d2008-08-21 17:58:08 +00001113 snprintf(buf, sizeof(buf),
1114 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1115 ";Bss=" TARGET_ABI_FMT_lx,
1116 ts->info->code_offset,
1117 ts->info->data_offset,
1118 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001119 put_packet(s, buf);
1120 break;
1121 }
blueswir10b8a9882009-03-07 10:51:36 +00001122#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001123 else if (strncmp(p, "Rcmd,", 5) == 0) {
1124 int len = strlen(p + 5);
1125
1126 if ((len % 2) != 0) {
1127 put_packet(s, "E01");
1128 break;
1129 }
1130 hextomem(mem_buf, p + 5, len);
1131 len = len / 2;
1132 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001133 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001134 put_packet(s, "OK");
1135 break;
1136 }
blueswir10b8a9882009-03-07 10:51:36 +00001137#endif /* !CONFIG_USER_ONLY */
pbrook56aebc82008-10-11 17:55:29 +00001138 if (strncmp(p, "Supported", 9) == 0) {
blueswir15b3715b2008-10-25 11:18:12 +00001139 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
Andreas Färber5b24c642013-07-07 15:08:22 +02001140 cc = CPU_GET_CLASS(first_cpu);
1141 if (cc->gdb_core_xml_file != NULL) {
1142 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1143 }
pbrook56aebc82008-10-11 17:55:29 +00001144 put_packet(s, buf);
1145 break;
1146 }
pbrook56aebc82008-10-11 17:55:29 +00001147 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1148 const char *xml;
1149 target_ulong total_len;
1150
Andreas Färber5b24c642013-07-07 15:08:22 +02001151 cc = CPU_GET_CLASS(first_cpu);
1152 if (cc->gdb_core_xml_file == NULL) {
1153 goto unknown_command;
1154 }
1155
Andreas Färber5b50e792013-06-29 04:18:45 +02001156 gdb_has_xml = true;
pbrook56aebc82008-10-11 17:55:29 +00001157 p += 19;
Andreas Färber5b24c642013-07-07 15:08:22 +02001158 xml = get_feature_xml(p, &p, cc);
pbrook56aebc82008-10-11 17:55:29 +00001159 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001160 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001161 put_packet(s, buf);
1162 break;
1163 }
1164
1165 if (*p == ':')
1166 p++;
1167 addr = strtoul(p, (char **)&p, 16);
1168 if (*p == ',')
1169 p++;
1170 len = strtoul(p, (char **)&p, 16);
1171
1172 total_len = strlen(xml);
1173 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001174 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001175 put_packet(s, buf);
1176 break;
1177 }
1178 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1179 len = (MAX_PACKET_LENGTH - 5) / 2;
1180 if (len < total_len - addr) {
1181 buf[0] = 'm';
1182 len = memtox(buf + 1, xml + addr, len);
1183 } else {
1184 buf[0] = 'l';
1185 len = memtox(buf + 1, xml + addr, total_len - addr);
1186 }
1187 put_packet_binary(s, buf, len + 1);
1188 break;
1189 }
pbrook56aebc82008-10-11 17:55:29 +00001190 /* Unrecognised 'q' command. */
1191 goto unknown_command;
1192
bellard858693c2004-03-31 18:52:07 +00001193 default:
pbrook56aebc82008-10-11 17:55:29 +00001194 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001195 /* put empty packet */
1196 buf[0] = '\0';
1197 put_packet(s, buf);
1198 break;
1199 }
1200 return RS_IDLE;
1201}
1202
Andreas Färber64f6b342013-05-27 02:06:09 +02001203void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001204{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001205 gdbserver_state->c_cpu = cpu;
1206 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001207}
1208
bellard1fddef42005-04-17 19:16:13 +00001209#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001210static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001211{
aliguori880a7572008-11-18 20:30:24 +00001212 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001213 CPUArchState *env = s->c_cpu->env_ptr;
1214 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001215 char buf[256];
aliguorid6fc1b32008-11-18 19:55:44 +00001216 const char *type;
bellard858693c2004-03-31 18:52:07 +00001217 int ret;
1218
Meador Ingecdb432b2012-03-15 17:49:45 +00001219 if (running || s->state == RS_INACTIVE) {
1220 return;
1221 }
1222 /* Is there a GDB syscall waiting to be sent? */
1223 if (s->current_syscall_cb) {
1224 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001225 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001226 }
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001227 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001228 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02001229 if (cpu->watchpoint_hit) {
1230 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001231 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001232 type = "r";
1233 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001234 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001235 type = "a";
1236 break;
1237 default:
1238 type = "";
1239 break;
1240 }
aliguori880a7572008-11-18 20:30:24 +00001241 snprintf(buf, sizeof(buf),
1242 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
Andreas Färber0d342822012-12-17 07:12:13 +01001243 GDB_SIGNAL_TRAP, cpu_index(cpu), type,
Andreas Färberff4700b2013-08-26 18:23:18 +02001244 (target_ulong)cpu->watchpoint_hit->vaddr);
1245 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001246 goto send_packet;
pbrook6658ffb2007-03-16 23:58:11 +00001247 }
Jan Kiszka425189a2011-03-22 11:02:09 +01001248 tb_flush(env);
aurel32ca587a82008-12-18 22:44:13 +00001249 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001250 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001251 case RUN_STATE_PAUSED:
aliguori9781e042009-01-22 17:15:29 +00001252 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001253 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001254 case RUN_STATE_SHUTDOWN:
Jan Kiszka425189a2011-03-22 11:02:09 +01001255 ret = GDB_SIGNAL_QUIT;
1256 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001257 case RUN_STATE_IO_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001258 ret = GDB_SIGNAL_IO;
1259 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001260 case RUN_STATE_WATCHDOG:
Jan Kiszka425189a2011-03-22 11:02:09 +01001261 ret = GDB_SIGNAL_ALRM;
1262 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001263 case RUN_STATE_INTERNAL_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001264 ret = GDB_SIGNAL_ABRT;
1265 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001266 case RUN_STATE_SAVE_VM:
1267 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001268 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001269 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001270 ret = GDB_SIGNAL_XCPU;
1271 break;
1272 default:
1273 ret = GDB_SIGNAL_UNKNOWN;
1274 break;
bellardbbeb7b52006-04-23 18:42:15 +00001275 }
Andreas Färber0d342822012-12-17 07:12:13 +01001276 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(cpu));
Jan Kiszka425189a2011-03-22 11:02:09 +01001277
1278send_packet:
bellard858693c2004-03-31 18:52:07 +00001279 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001280
1281 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001282 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001283}
bellard1fddef42005-04-17 19:16:13 +00001284#endif
bellard858693c2004-03-31 18:52:07 +00001285
pbrooka2d1eba2007-01-28 03:10:55 +00001286/* Send a gdb syscall request.
1287 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001288 %x - target_ulong argument printed in hex.
1289 %lx - 64-bit argument printed in hex.
1290 %s - string pointer (target_ulong) and length (int) pair. */
blueswir17ccfb2e2008-09-14 06:45:34 +00001291void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
pbrooka2d1eba2007-01-28 03:10:55 +00001292{
1293 va_list va;
pbrooka2d1eba2007-01-28 03:10:55 +00001294 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001295 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001296 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001297 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001298 GDBState *s;
1299
aliguori880a7572008-11-18 20:30:24 +00001300 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001301 if (!s)
1302 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001303 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001304#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001305 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001306#endif
pbrooka2d1eba2007-01-28 03:10:55 +00001307 va_start(va, fmt);
Meador Ingecdb432b2012-03-15 17:49:45 +00001308 p = s->syscall_buf;
1309 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001310 *(p++) = 'F';
1311 while (*fmt) {
1312 if (*fmt == '%') {
1313 fmt++;
1314 switch (*fmt++) {
1315 case 'x':
1316 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001317 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001318 break;
pbrooka87295e2007-05-26 15:09:38 +00001319 case 'l':
1320 if (*(fmt++) != 'x')
1321 goto bad_format;
1322 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001323 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001324 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001325 case 's':
1326 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001327 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001328 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001329 break;
1330 default:
pbrooka87295e2007-05-26 15:09:38 +00001331 bad_format:
pbrooka2d1eba2007-01-28 03:10:55 +00001332 fprintf(stderr, "gdbstub: Bad syscall format string '%s'\n",
1333 fmt - 1);
1334 break;
1335 }
1336 } else {
1337 *(p++) = *(fmt++);
1338 }
1339 }
pbrook8a93e022007-08-06 13:19:15 +00001340 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001341 va_end(va);
pbrooka2d1eba2007-01-28 03:10:55 +00001342#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001343 put_packet(s, s->syscall_buf);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001344 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001345#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001346 /* In this case wait to send the syscall packet until notification that
1347 the CPU has stopped. This must be done because if the packet is sent
1348 now the reply from the syscall request could be received while the CPU
1349 is still in the running state, which can cause packets to be dropped
1350 and state transition 'T' packets to be sent while the syscall is still
1351 being processed. */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001352 cpu_exit(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00001353#endif
1354}
1355
bellard6a00d602005-11-21 23:25:50 +00001356static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00001357{
1358 int i, csum;
ths60fe76f2007-12-16 03:02:09 +00001359 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00001360
bellard1fddef42005-04-17 19:16:13 +00001361#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001362 if (s->last_packet_len) {
1363 /* Waiting for a response to the last packet. If we see the start
1364 of a new command then abandon the previous response. */
1365 if (ch == '-') {
1366#ifdef DEBUG_GDB
1367 printf("Got NACK, retransmitting\n");
1368#endif
thsffe8ab82007-12-16 03:16:05 +00001369 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
pbrook4046d912007-01-28 01:53:16 +00001370 }
1371#ifdef DEBUG_GDB
1372 else if (ch == '+')
1373 printf("Got ACK\n");
1374 else
1375 printf("Got '%c' when expecting ACK/NACK\n", ch);
1376#endif
1377 if (ch == '+' || ch == '$')
1378 s->last_packet_len = 0;
1379 if (ch != '$')
1380 return;
1381 }
Luiz Capitulino13548692011-07-29 15:36:43 -03001382 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00001383 /* when the CPU is running, we cannot do anything except stop
1384 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001385 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00001386 } else
bellard1fddef42005-04-17 19:16:13 +00001387#endif
bellard41625032005-04-24 10:07:11 +00001388 {
bellard858693c2004-03-31 18:52:07 +00001389 switch(s->state) {
1390 case RS_IDLE:
1391 if (ch == '$') {
1392 s->line_buf_index = 0;
1393 s->state = RS_GETLINE;
bellard4c3a88a2003-07-26 12:06:08 +00001394 }
1395 break;
bellard858693c2004-03-31 18:52:07 +00001396 case RS_GETLINE:
1397 if (ch == '#') {
1398 s->state = RS_CHKSUM1;
1399 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1400 s->state = RS_IDLE;
1401 } else {
1402 s->line_buf[s->line_buf_index++] = ch;
1403 }
1404 break;
1405 case RS_CHKSUM1:
1406 s->line_buf[s->line_buf_index] = '\0';
1407 s->line_csum = fromhex(ch) << 4;
1408 s->state = RS_CHKSUM2;
1409 break;
1410 case RS_CHKSUM2:
1411 s->line_csum |= fromhex(ch);
1412 csum = 0;
1413 for(i = 0; i < s->line_buf_index; i++) {
1414 csum += s->line_buf[i];
1415 }
1416 if (s->line_csum != (csum & 0xff)) {
ths60fe76f2007-12-16 03:02:09 +00001417 reply = '-';
1418 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00001419 s->state = RS_IDLE;
1420 } else {
ths60fe76f2007-12-16 03:02:09 +00001421 reply = '+';
1422 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00001423 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00001424 }
bellardb4608c02003-06-27 17:34:32 +00001425 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001426 default:
1427 abort();
bellardb4608c02003-06-27 17:34:32 +00001428 }
1429 }
bellard858693c2004-03-31 18:52:07 +00001430}
1431
Paul Brook0e1c9c52010-06-16 13:03:51 +01001432/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001433void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01001434{
1435 GDBState *s;
1436 char buf[4];
1437
1438 s = gdbserver_state;
1439 if (!s) {
1440 return;
1441 }
1442#ifdef CONFIG_USER_ONLY
1443 if (gdbserver_fd < 0 || s->fd < 0) {
1444 return;
1445 }
1446#endif
1447
1448 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
1449 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001450
1451#ifndef CONFIG_USER_ONLY
1452 if (s->chr) {
Anthony Liguori70f24fb2011-08-15 11:17:38 -05001453 qemu_chr_delete(s->chr);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001454 }
1455#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01001456}
1457
bellard1fddef42005-04-17 19:16:13 +00001458#ifdef CONFIG_USER_ONLY
1459int
aurel32ca587a82008-12-18 22:44:13 +00001460gdb_queuesig (void)
1461{
1462 GDBState *s;
1463
1464 s = gdbserver_state;
1465
1466 if (gdbserver_fd < 0 || s->fd < 0)
1467 return 0;
1468 else
1469 return 1;
1470}
1471
1472int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001473gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00001474{
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001475 CPUArchState *env = cpu->env_ptr;
Andreas Färber5ca666c2013-06-24 19:20:57 +02001476 GDBState *s;
1477 char buf[256];
1478 int n;
bellard1fddef42005-04-17 19:16:13 +00001479
Andreas Färber5ca666c2013-06-24 19:20:57 +02001480 s = gdbserver_state;
1481 if (gdbserver_fd < 0 || s->fd < 0) {
1482 return sig;
bellard1fddef42005-04-17 19:16:13 +00001483 }
1484
Andreas Färber5ca666c2013-06-24 19:20:57 +02001485 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001486 cpu_single_step(cpu, 0);
Andreas Färber5ca666c2013-06-24 19:20:57 +02001487 tb_flush(env);
bellard1fddef42005-04-17 19:16:13 +00001488
Andreas Färber5ca666c2013-06-24 19:20:57 +02001489 if (sig != 0) {
1490 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
1491 put_packet(s, buf);
1492 }
1493 /* put_packet() might have detected that the peer terminated the
1494 connection. */
1495 if (s->fd < 0) {
1496 return sig;
1497 }
1498
1499 sig = 0;
1500 s->state = RS_IDLE;
1501 s->running_state = 0;
1502 while (s->running_state == 0) {
1503 n = read(s->fd, buf, 256);
1504 if (n > 0) {
1505 int i;
1506
1507 for (i = 0; i < n; i++) {
1508 gdb_read_byte(s, buf[i]);
1509 }
1510 } else if (n == 0 || errno != EAGAIN) {
1511 /* XXX: Connection closed. Should probably wait for another
1512 connection before continuing. */
1513 return sig;
bellard1fddef42005-04-17 19:16:13 +00001514 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02001515 }
1516 sig = s->signal;
1517 s->signal = 0;
1518 return sig;
bellard1fddef42005-04-17 19:16:13 +00001519}
bellarde9009672005-04-26 20:42:36 +00001520
aurel32ca587a82008-12-18 22:44:13 +00001521/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001522void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00001523{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001524 GDBState *s;
1525 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00001526
Andreas Färber5ca666c2013-06-24 19:20:57 +02001527 s = gdbserver_state;
1528 if (gdbserver_fd < 0 || s->fd < 0) {
1529 return;
1530 }
aurel32ca587a82008-12-18 22:44:13 +00001531
Andreas Färber5ca666c2013-06-24 19:20:57 +02001532 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
1533 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00001534}
bellard1fddef42005-04-17 19:16:13 +00001535
aliguori880a7572008-11-18 20:30:24 +00001536static void gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00001537{
1538 GDBState *s;
1539 struct sockaddr_in sockaddr;
1540 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001541 int fd;
bellard858693c2004-03-31 18:52:07 +00001542
1543 for(;;) {
1544 len = sizeof(sockaddr);
1545 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1546 if (fd < 0 && errno != EINTR) {
1547 perror("accept");
1548 return;
1549 } else if (fd >= 0) {
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001550#ifndef _WIN32
1551 fcntl(fd, F_SETFD, FD_CLOEXEC);
1552#endif
bellard858693c2004-03-31 18:52:07 +00001553 break;
1554 }
1555 }
1556
1557 /* set short latency */
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001558 socket_set_nodelay(fd);
ths3b46e622007-09-17 08:09:54 +00001559
Anthony Liguori7267c092011-08-20 22:09:37 -05001560 s = g_malloc0(sizeof(GDBState));
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001561 s->c_cpu = first_cpu;
1562 s->g_cpu = first_cpu;
bellard858693c2004-03-31 18:52:07 +00001563 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02001564 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00001565
aliguori880a7572008-11-18 20:30:24 +00001566 gdbserver_state = s;
pbrooka2d1eba2007-01-28 03:10:55 +00001567
bellard858693c2004-03-31 18:52:07 +00001568 fcntl(fd, F_SETFL, O_NONBLOCK);
bellard858693c2004-03-31 18:52:07 +00001569}
1570
1571static int gdbserver_open(int port)
1572{
1573 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001574 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00001575
1576 fd = socket(PF_INET, SOCK_STREAM, 0);
1577 if (fd < 0) {
1578 perror("socket");
1579 return -1;
1580 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001581#ifndef _WIN32
1582 fcntl(fd, F_SETFD, FD_CLOEXEC);
1583#endif
bellard858693c2004-03-31 18:52:07 +00001584
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001585 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00001586
1587 sockaddr.sin_family = AF_INET;
1588 sockaddr.sin_port = htons(port);
1589 sockaddr.sin_addr.s_addr = 0;
1590 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1591 if (ret < 0) {
1592 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00001593 close(fd);
bellard858693c2004-03-31 18:52:07 +00001594 return -1;
1595 }
1596 ret = listen(fd, 0);
1597 if (ret < 0) {
1598 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00001599 close(fd);
bellard858693c2004-03-31 18:52:07 +00001600 return -1;
1601 }
bellard858693c2004-03-31 18:52:07 +00001602 return fd;
1603}
1604
1605int gdbserver_start(int port)
1606{
1607 gdbserver_fd = gdbserver_open(port);
1608 if (gdbserver_fd < 0)
1609 return -1;
1610 /* accept connections */
aliguori880a7572008-11-18 20:30:24 +00001611 gdb_accept();
bellardb4608c02003-06-27 17:34:32 +00001612 return 0;
1613}
aurel322b1319c2008-12-18 22:44:04 +00001614
1615/* Disable gdb stub for child processes. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001616void gdbserver_fork(CPUArchState *env)
aurel322b1319c2008-12-18 22:44:04 +00001617{
Andreas Färber75a34032013-09-02 16:57:02 +02001618 CPUState *cpu = ENV_GET_CPU(env);
aurel322b1319c2008-12-18 22:44:04 +00001619 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02001620
1621 if (gdbserver_fd < 0 || s->fd < 0) {
1622 return;
1623 }
aurel322b1319c2008-12-18 22:44:04 +00001624 close(s->fd);
1625 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001626 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02001627 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00001628}
pbrook4046d912007-01-28 01:53:16 +00001629#else
thsaa1f17c2007-07-11 22:48:58 +00001630static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00001631{
pbrook56aebc82008-10-11 17:55:29 +00001632 /* We can handle an arbitrarily large amount of data.
1633 Pick the maximum packet size, which is as good as anything. */
1634 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00001635}
1636
thsaa1f17c2007-07-11 22:48:58 +00001637static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00001638{
pbrook4046d912007-01-28 01:53:16 +00001639 int i;
1640
1641 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00001642 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00001643 }
1644}
1645
1646static void gdb_chr_event(void *opaque, int event)
1647{
1648 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05301649 case CHR_EVENT_OPENED:
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001650 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02001651 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00001652 break;
1653 default:
1654 break;
1655 }
1656}
1657
aliguori8a34a0f2009-03-05 23:01:55 +00001658static void gdb_monitor_output(GDBState *s, const char *msg, int len)
1659{
1660 char buf[MAX_PACKET_LENGTH];
1661
1662 buf[0] = 'O';
1663 if (len > (MAX_PACKET_LENGTH/2) - 1)
1664 len = (MAX_PACKET_LENGTH/2) - 1;
1665 memtohex(buf + 1, (uint8_t *)msg, len);
1666 put_packet(s, buf);
1667}
1668
1669static int gdb_monitor_write(CharDriverState *chr, const uint8_t *buf, int len)
1670{
1671 const char *p = (const char *)buf;
1672 int max_sz;
1673
1674 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
1675 for (;;) {
1676 if (len <= max_sz) {
1677 gdb_monitor_output(gdbserver_state, p, len);
1678 break;
1679 }
1680 gdb_monitor_output(gdbserver_state, p, max_sz);
1681 p += max_sz;
1682 len -= max_sz;
1683 }
1684 return len;
1685}
1686
aliguori59030a82009-04-05 18:43:41 +00001687#ifndef _WIN32
1688static void gdb_sigterm_handler(int signal)
1689{
Luiz Capitulino13548692011-07-29 15:36:43 -03001690 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001691 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001692 }
aliguori59030a82009-04-05 18:43:41 +00001693}
1694#endif
1695
1696int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00001697{
1698 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00001699 char gdbstub_device_name[128];
aliguori36556b22009-03-28 18:05:53 +00001700 CharDriverState *chr = NULL;
1701 CharDriverState *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00001702
aliguori59030a82009-04-05 18:43:41 +00001703 if (!device)
1704 return -1;
1705 if (strcmp(device, "none") != 0) {
1706 if (strstart(device, "tcp:", NULL)) {
1707 /* enforce required TCP attributes */
1708 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
1709 "%s,nowait,nodelay,server", device);
1710 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00001711 }
aliguori59030a82009-04-05 18:43:41 +00001712#ifndef _WIN32
1713 else if (strcmp(device, "stdio") == 0) {
1714 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00001715
aliguori59030a82009-04-05 18:43:41 +00001716 memset(&act, 0, sizeof(act));
1717 act.sa_handler = gdb_sigterm_handler;
1718 sigaction(SIGINT, &act, NULL);
1719 }
1720#endif
Anthony Liguori27143a42011-08-15 11:17:36 -05001721 chr = qemu_chr_new("gdb", device, NULL);
aliguori36556b22009-03-28 18:05:53 +00001722 if (!chr)
1723 return -1;
1724
Hans de Goede456d6062013-03-27 20:29:40 +01001725 qemu_chr_fe_claim_no_fail(chr);
aliguori36556b22009-03-28 18:05:53 +00001726 qemu_chr_add_handlers(chr, gdb_chr_can_receive, gdb_chr_receive,
1727 gdb_chr_event, NULL);
pbrookcfc34752007-02-22 01:48:01 +00001728 }
1729
aliguori36556b22009-03-28 18:05:53 +00001730 s = gdbserver_state;
1731 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001732 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00001733 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00001734
aliguori36556b22009-03-28 18:05:53 +00001735 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
1736
1737 /* Initialize a monitor terminal for gdb */
Pavel Dovgalyuk462efe92014-09-10 18:34:14 +04001738 mon_chr = qemu_chr_alloc();
aliguori36556b22009-03-28 18:05:53 +00001739 mon_chr->chr_write = gdb_monitor_write;
1740 monitor_init(mon_chr, 0);
1741 } else {
1742 if (s->chr)
Anthony Liguori70f24fb2011-08-15 11:17:38 -05001743 qemu_chr_delete(s->chr);
aliguori36556b22009-03-28 18:05:53 +00001744 mon_chr = s->mon_chr;
1745 memset(s, 0, sizeof(GDBState));
1746 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001747 s->c_cpu = first_cpu;
1748 s->g_cpu = first_cpu;
pbrook4046d912007-01-28 01:53:16 +00001749 s->chr = chr;
aliguori36556b22009-03-28 18:05:53 +00001750 s->state = chr ? RS_IDLE : RS_INACTIVE;
1751 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00001752 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00001753
pbrook4046d912007-01-28 01:53:16 +00001754 return 0;
1755}
1756#endif