blob: b1efe0bccb7c28a442419ba52c49d60981ce9e52 [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 */
Peter Maydelld38ea872016-01-29 17:50:05 +000019#include "qemu/osdep.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010020#include "qapi/error.h"
Ziyue Yang508b4ec2017-01-18 16:02:41 +080021#include "qemu/error-report.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020022#include "qemu/cutils.h"
Paolo Bonzini33c11872016-03-15 16:58:45 +010023#include "cpu.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020024#ifdef CONFIG_USER_ONLY
bellard1fddef42005-04-17 19:16:13 +000025#include "qemu.h"
26#else
Paolo Bonzini83c90892012-12-17 18:19:49 +010027#include "monitor/monitor.h"
Marc-André Lureau8228e352017-01-26 17:19:46 +040028#include "chardev/char.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040029#include "chardev/char-fe.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010030#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010031#include "exec/gdbstub.h"
bellard1fddef42005-04-17 19:16:13 +000032#endif
bellard67b915a2004-03-31 23:37:16 +000033
pbrook56aebc82008-10-11 17:55:29 +000034#define MAX_PACKET_LENGTH 4096
35
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010036#include "qemu/sockets.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010037#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010038#include "sysemu/kvm.h"
Leon Alraecfe67ce2015-06-19 14:17:45 +010039#include "exec/semihost.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010040#include "exec/exec-all.h"
aurel32ca587a82008-12-18 22:44:13 +000041
Jan Kiszkaa3919382015-02-07 09:38:44 +010042#ifdef CONFIG_USER_ONLY
43#define GDB_ATTACHED "0"
44#else
45#define GDB_ATTACHED "1"
46#endif
47
Andreas Färberf3659ee2013-06-27 19:09:09 +020048static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
49 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020050{
Andreas Färberf3659ee2013-06-27 19:09:09 +020051 CPUClass *cc = CPU_GET_CLASS(cpu);
52
53 if (cc->memory_rw_debug) {
54 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
55 }
56 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020057}
aurel32ca587a82008-12-18 22:44:13 +000058
Alex Bennéed2a6c852017-07-12 11:52:14 +010059/* Return the GDB index for a given vCPU state.
60 *
61 * For user mode this is simply the thread id. In system mode GDB
62 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
63 */
64static inline int cpu_gdb_index(CPUState *cpu)
65{
66#if defined(CONFIG_USER_ONLY)
67 return cpu->host_tid;
68#else
69 return cpu->cpu_index + 1;
70#endif
71}
72
aurel32ca587a82008-12-18 22:44:13 +000073enum {
74 GDB_SIGNAL_0 = 0,
75 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +010076 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +000077 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +010078 GDB_SIGNAL_ABRT = 6,
79 GDB_SIGNAL_ALRM = 14,
80 GDB_SIGNAL_IO = 23,
81 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +000082 GDB_SIGNAL_UNKNOWN = 143
83};
84
85#ifdef CONFIG_USER_ONLY
86
87/* Map target signal numbers to GDB protocol signal numbers and vice
88 * versa. For user emulation's currently supported systems, we can
89 * assume most signals are defined.
90 */
91
92static int gdb_signal_table[] = {
93 0,
94 TARGET_SIGHUP,
95 TARGET_SIGINT,
96 TARGET_SIGQUIT,
97 TARGET_SIGILL,
98 TARGET_SIGTRAP,
99 TARGET_SIGABRT,
100 -1, /* SIGEMT */
101 TARGET_SIGFPE,
102 TARGET_SIGKILL,
103 TARGET_SIGBUS,
104 TARGET_SIGSEGV,
105 TARGET_SIGSYS,
106 TARGET_SIGPIPE,
107 TARGET_SIGALRM,
108 TARGET_SIGTERM,
109 TARGET_SIGURG,
110 TARGET_SIGSTOP,
111 TARGET_SIGTSTP,
112 TARGET_SIGCONT,
113 TARGET_SIGCHLD,
114 TARGET_SIGTTIN,
115 TARGET_SIGTTOU,
116 TARGET_SIGIO,
117 TARGET_SIGXCPU,
118 TARGET_SIGXFSZ,
119 TARGET_SIGVTALRM,
120 TARGET_SIGPROF,
121 TARGET_SIGWINCH,
122 -1, /* SIGLOST */
123 TARGET_SIGUSR1,
124 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000125#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000126 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000127#else
128 -1,
129#endif
aurel32ca587a82008-12-18 22:44:13 +0000130 -1, /* SIGPOLL */
131 -1,
132 -1,
133 -1,
134 -1,
135 -1,
136 -1,
137 -1,
138 -1,
139 -1,
140 -1,
141 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000142#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000143 __SIGRTMIN + 1,
144 __SIGRTMIN + 2,
145 __SIGRTMIN + 3,
146 __SIGRTMIN + 4,
147 __SIGRTMIN + 5,
148 __SIGRTMIN + 6,
149 __SIGRTMIN + 7,
150 __SIGRTMIN + 8,
151 __SIGRTMIN + 9,
152 __SIGRTMIN + 10,
153 __SIGRTMIN + 11,
154 __SIGRTMIN + 12,
155 __SIGRTMIN + 13,
156 __SIGRTMIN + 14,
157 __SIGRTMIN + 15,
158 __SIGRTMIN + 16,
159 __SIGRTMIN + 17,
160 __SIGRTMIN + 18,
161 __SIGRTMIN + 19,
162 __SIGRTMIN + 20,
163 __SIGRTMIN + 21,
164 __SIGRTMIN + 22,
165 __SIGRTMIN + 23,
166 __SIGRTMIN + 24,
167 __SIGRTMIN + 25,
168 __SIGRTMIN + 26,
169 __SIGRTMIN + 27,
170 __SIGRTMIN + 28,
171 __SIGRTMIN + 29,
172 __SIGRTMIN + 30,
173 __SIGRTMIN + 31,
174 -1, /* SIGCANCEL */
175 __SIGRTMIN,
176 __SIGRTMIN + 32,
177 __SIGRTMIN + 33,
178 __SIGRTMIN + 34,
179 __SIGRTMIN + 35,
180 __SIGRTMIN + 36,
181 __SIGRTMIN + 37,
182 __SIGRTMIN + 38,
183 __SIGRTMIN + 39,
184 __SIGRTMIN + 40,
185 __SIGRTMIN + 41,
186 __SIGRTMIN + 42,
187 __SIGRTMIN + 43,
188 __SIGRTMIN + 44,
189 __SIGRTMIN + 45,
190 __SIGRTMIN + 46,
191 __SIGRTMIN + 47,
192 __SIGRTMIN + 48,
193 __SIGRTMIN + 49,
194 __SIGRTMIN + 50,
195 __SIGRTMIN + 51,
196 __SIGRTMIN + 52,
197 __SIGRTMIN + 53,
198 __SIGRTMIN + 54,
199 __SIGRTMIN + 55,
200 __SIGRTMIN + 56,
201 __SIGRTMIN + 57,
202 __SIGRTMIN + 58,
203 __SIGRTMIN + 59,
204 __SIGRTMIN + 60,
205 __SIGRTMIN + 61,
206 __SIGRTMIN + 62,
207 __SIGRTMIN + 63,
208 __SIGRTMIN + 64,
209 __SIGRTMIN + 65,
210 __SIGRTMIN + 66,
211 __SIGRTMIN + 67,
212 __SIGRTMIN + 68,
213 __SIGRTMIN + 69,
214 __SIGRTMIN + 70,
215 __SIGRTMIN + 71,
216 __SIGRTMIN + 72,
217 __SIGRTMIN + 73,
218 __SIGRTMIN + 74,
219 __SIGRTMIN + 75,
220 __SIGRTMIN + 76,
221 __SIGRTMIN + 77,
222 __SIGRTMIN + 78,
223 __SIGRTMIN + 79,
224 __SIGRTMIN + 80,
225 __SIGRTMIN + 81,
226 __SIGRTMIN + 82,
227 __SIGRTMIN + 83,
228 __SIGRTMIN + 84,
229 __SIGRTMIN + 85,
230 __SIGRTMIN + 86,
231 __SIGRTMIN + 87,
232 __SIGRTMIN + 88,
233 __SIGRTMIN + 89,
234 __SIGRTMIN + 90,
235 __SIGRTMIN + 91,
236 __SIGRTMIN + 92,
237 __SIGRTMIN + 93,
238 __SIGRTMIN + 94,
239 __SIGRTMIN + 95,
240 -1, /* SIGINFO */
241 -1, /* UNKNOWN */
242 -1, /* DEFAULT */
243 -1,
244 -1,
245 -1,
246 -1,
247 -1,
248 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000249#endif
aurel32ca587a82008-12-18 22:44:13 +0000250};
bellard8f447cc2006-06-14 15:21:14 +0000251#else
aurel32ca587a82008-12-18 22:44:13 +0000252/* In system mode we only need SIGINT and SIGTRAP; other signals
253 are not yet supported. */
254
255enum {
256 TARGET_SIGINT = 2,
257 TARGET_SIGTRAP = 5
258};
259
260static int gdb_signal_table[] = {
261 -1,
262 -1,
263 TARGET_SIGINT,
264 -1,
265 -1,
266 TARGET_SIGTRAP
267};
bellard8f447cc2006-06-14 15:21:14 +0000268#endif
bellardb4608c02003-06-27 17:34:32 +0000269
aurel32ca587a82008-12-18 22:44:13 +0000270#ifdef CONFIG_USER_ONLY
271static int target_signal_to_gdb (int sig)
272{
273 int i;
274 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
275 if (gdb_signal_table[i] == sig)
276 return i;
277 return GDB_SIGNAL_UNKNOWN;
278}
279#endif
280
281static int gdb_signal_to_target (int sig)
282{
283 if (sig < ARRAY_SIZE (gdb_signal_table))
284 return gdb_signal_table[sig];
285 else
286 return -1;
287}
288
Alex Bennée118e2262017-07-12 11:52:13 +0100289/* #define DEBUG_GDB */
290
291#ifdef DEBUG_GDB
292# define DEBUG_GDB_GATE 1
293#else
294# define DEBUG_GDB_GATE 0
295#endif
296
297#define gdb_debug(fmt, ...) do { \
298 if (DEBUG_GDB_GATE) { \
299 fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \
300 } \
301} while (0)
302
bellardb4608c02003-06-27 17:34:32 +0000303
pbrook56aebc82008-10-11 17:55:29 +0000304typedef struct GDBRegisterState {
305 int base_reg;
306 int num_regs;
307 gdb_reg_cb get_reg;
308 gdb_reg_cb set_reg;
309 const char *xml;
310 struct GDBRegisterState *next;
311} GDBRegisterState;
312
bellard858693c2004-03-31 18:52:07 +0000313enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000314 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000315 RS_IDLE,
316 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400317 RS_GETLINE_ESC,
318 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000319 RS_CHKSUM1,
320 RS_CHKSUM2,
321};
bellard858693c2004-03-31 18:52:07 +0000322typedef struct GDBState {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200323 CPUState *c_cpu; /* current CPU for step/continue ops */
324 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200325 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000326 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000327 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000328 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400329 int line_sum; /* running checksum */
330 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000331 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000332 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000333 int signal;
bellard41625032005-04-24 10:07:11 +0000334#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000335 int fd;
bellard41625032005-04-24 10:07:11 +0000336 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000337#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300338 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300339 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000340#endif
Meador Ingecdb432b2012-03-15 17:49:45 +0000341 char syscall_buf[256];
342 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000343} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000344
edgar_igl60897d32008-05-09 08:25:14 +0000345/* By default use no IRQs and no timers while single stepping so as to
346 * make single stepping like an ICE HW step.
347 */
348static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
349
aliguori880a7572008-11-18 20:30:24 +0000350static GDBState *gdbserver_state;
351
Andreas Färber5b50e792013-06-29 04:18:45 +0200352bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000353
bellard1fddef42005-04-17 19:16:13 +0000354#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000355/* XXX: This is not thread safe. Do we care? */
356static int gdbserver_fd = -1;
357
bellard858693c2004-03-31 18:52:07 +0000358static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000359{
360 uint8_t ch;
361 int ret;
362
363 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000364 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000365 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000366 if (errno == ECONNRESET)
367 s->fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200368 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000369 return -1;
370 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000371 close(s->fd);
372 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000373 return -1;
374 } else {
375 break;
376 }
377 }
378 return ch;
379}
pbrook4046d912007-01-28 01:53:16 +0000380#endif
bellardb4608c02003-06-27 17:34:32 +0000381
blueswir1654efcf2009-04-18 07:29:59 +0000382static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000383 GDB_SYS_UNKNOWN,
384 GDB_SYS_ENABLED,
385 GDB_SYS_DISABLED,
386} gdb_syscall_mode;
387
Liviu Ionescua38bb072014-12-11 12:07:48 +0000388/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000389int use_gdb_syscalls(void)
390{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100391 SemihostingTarget target = semihosting_get_target();
392 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000393 /* -semihosting-config target=native */
394 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100395 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000396 /* -semihosting-config target=gdb */
397 return true;
398 }
399
400 /* -semihosting-config target=auto */
401 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000402 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000403 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
404 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000405 }
406 return gdb_syscall_mode == GDB_SYS_ENABLED;
407}
408
edgar_iglba70a622008-03-14 06:10:42 +0000409/* Resume execution. */
410static inline void gdb_continue(GDBState *s)
411{
412#ifdef CONFIG_USER_ONLY
413 s->running_state = 1;
414#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200415 if (!runstate_needs_reset()) {
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200416 vm_start();
417 }
edgar_iglba70a622008-03-14 06:10:42 +0000418#endif
419}
420
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100421/*
422 * Resume execution, per CPU actions. For user-mode emulation it's
423 * equivalent to gdb_continue.
424 */
425static int gdb_continue_partial(GDBState *s, char *newstates)
426{
427 CPUState *cpu;
428 int res = 0;
429#ifdef CONFIG_USER_ONLY
430 /*
431 * This is not exactly accurate, but it's an improvement compared to the
432 * previous situation, where only one CPU would be single-stepped.
433 */
434 CPU_FOREACH(cpu) {
435 if (newstates[cpu->cpu_index] == 's') {
436 cpu_single_step(cpu, sstep_flags);
437 }
438 }
439 s->running_state = 1;
440#else
441 int flag = 0;
442
443 if (!runstate_needs_reset()) {
444 if (vm_prepare_start()) {
445 return 0;
446 }
447
448 CPU_FOREACH(cpu) {
449 switch (newstates[cpu->cpu_index]) {
450 case 0:
451 case 1:
452 break; /* nothing to do here */
453 case 's':
454 cpu_single_step(cpu, sstep_flags);
455 cpu_resume(cpu);
456 flag = 1;
457 break;
458 case 'c':
459 cpu_resume(cpu);
460 flag = 1;
461 break;
462 default:
463 res = -1;
464 break;
465 }
466 }
467 }
468 if (flag) {
469 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
470 }
471#endif
472 return res;
473}
474
bellard858693c2004-03-31 18:52:07 +0000475static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000476{
pbrook4046d912007-01-28 01:53:16 +0000477#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000478 int ret;
479
480 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000481 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000482 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200483 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000484 return;
485 } else {
486 buf += ret;
487 len -= ret;
488 }
489 }
pbrook4046d912007-01-28 01:53:16 +0000490#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100491 /* XXX this blocks entire thread. Rewrite to use
492 * qemu_chr_fe_write and background I/O callbacks */
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300493 qemu_chr_fe_write_all(&s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000494#endif
bellardb4608c02003-06-27 17:34:32 +0000495}
496
497static inline int fromhex(int v)
498{
499 if (v >= '0' && v <= '9')
500 return v - '0';
501 else if (v >= 'A' && v <= 'F')
502 return v - 'A' + 10;
503 else if (v >= 'a' && v <= 'f')
504 return v - 'a' + 10;
505 else
506 return 0;
507}
508
509static inline int tohex(int v)
510{
511 if (v < 10)
512 return v + '0';
513 else
514 return v - 10 + 'a';
515}
516
517static void memtohex(char *buf, const uint8_t *mem, int len)
518{
519 int i, c;
520 char *q;
521 q = buf;
522 for(i = 0; i < len; i++) {
523 c = mem[i];
524 *q++ = tohex(c >> 4);
525 *q++ = tohex(c & 0xf);
526 }
527 *q = '\0';
528}
529
530static void hextomem(uint8_t *mem, const char *buf, int len)
531{
532 int i;
533
534 for(i = 0; i < len; i++) {
535 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
536 buf += 2;
537 }
538}
539
bellardb4608c02003-06-27 17:34:32 +0000540/* return -1 if error, 0 if OK */
pbrook56aebc82008-10-11 17:55:29 +0000541static int put_packet_binary(GDBState *s, const char *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000542{
pbrook56aebc82008-10-11 17:55:29 +0000543 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000544 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000545
bellardb4608c02003-06-27 17:34:32 +0000546 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000547 p = s->last_packet;
548 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000549 memcpy(p, buf, len);
550 p += len;
bellardb4608c02003-06-27 17:34:32 +0000551 csum = 0;
552 for(i = 0; i < len; i++) {
553 csum += buf[i];
554 }
pbrook4046d912007-01-28 01:53:16 +0000555 *(p++) = '#';
556 *(p++) = tohex((csum >> 4) & 0xf);
557 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000558
pbrook4046d912007-01-28 01:53:16 +0000559 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000560 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000561
pbrook4046d912007-01-28 01:53:16 +0000562#ifdef CONFIG_USER_ONLY
563 i = get_char(s);
564 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000565 return -1;
pbrook4046d912007-01-28 01:53:16 +0000566 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000567 break;
pbrook4046d912007-01-28 01:53:16 +0000568#else
569 break;
570#endif
bellardb4608c02003-06-27 17:34:32 +0000571 }
572 return 0;
573}
574
pbrook56aebc82008-10-11 17:55:29 +0000575/* return -1 if error, 0 if OK */
576static int put_packet(GDBState *s, const char *buf)
577{
Alex Bennée118e2262017-07-12 11:52:13 +0100578 gdb_debug("reply='%s'\n", buf);
pbrook56aebc82008-10-11 17:55:29 +0000579
580 return put_packet_binary(s, buf, strlen(buf));
581}
582
pbrook56aebc82008-10-11 17:55:29 +0000583/* Encode data using the encoding for 'x' packets. */
584static int memtox(char *buf, const char *mem, int len)
585{
586 char *p = buf;
587 char c;
588
589 while (len--) {
590 c = *(mem++);
591 switch (c) {
592 case '#': case '$': case '*': case '}':
593 *(p++) = '}';
594 *(p++) = c ^ 0x20;
595 break;
596 default:
597 *(p++) = c;
598 break;
599 }
600 }
601 return p - buf;
602}
603
Andreas Färber5b24c642013-07-07 15:08:22 +0200604static const char *get_feature_xml(const char *p, const char **newp,
605 CPUClass *cc)
pbrook56aebc82008-10-11 17:55:29 +0000606{
pbrook56aebc82008-10-11 17:55:29 +0000607 size_t len;
608 int i;
609 const char *name;
610 static char target_xml[1024];
611
612 len = 0;
613 while (p[len] && p[len] != ':')
614 len++;
615 *newp = p + len;
616
617 name = NULL;
618 if (strncmp(p, "target.xml", len) == 0) {
619 /* Generate the XML description for this CPU. */
620 if (!target_xml[0]) {
621 GDBRegisterState *r;
Andreas Färbereac8b352013-06-28 21:11:37 +0200622 CPUState *cpu = first_cpu;
pbrook56aebc82008-10-11 17:55:29 +0000623
David Hildenbrandb3820e62015-12-03 13:14:41 +0100624 pstrcat(target_xml, sizeof(target_xml),
625 "<?xml version=\"1.0\"?>"
626 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
627 "<target>");
628 if (cc->gdb_arch_name) {
629 gchar *arch = cc->gdb_arch_name(cpu);
630 pstrcat(target_xml, sizeof(target_xml), "<architecture>");
631 pstrcat(target_xml, sizeof(target_xml), arch);
632 pstrcat(target_xml, sizeof(target_xml), "</architecture>");
633 g_free(arch);
634 }
635 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
636 pstrcat(target_xml, sizeof(target_xml), cc->gdb_core_xml_file);
637 pstrcat(target_xml, sizeof(target_xml), "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200638 for (r = cpu->gdb_regs; r; r = r->next) {
blueswir12dc766d2009-04-13 16:06:19 +0000639 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
640 pstrcat(target_xml, sizeof(target_xml), r->xml);
641 pstrcat(target_xml, sizeof(target_xml), "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000642 }
blueswir12dc766d2009-04-13 16:06:19 +0000643 pstrcat(target_xml, sizeof(target_xml), "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000644 }
645 return target_xml;
646 }
647 for (i = 0; ; i++) {
648 name = xml_builtin[i][0];
649 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
650 break;
651 }
652 return name ? xml_builtin[i][1] : NULL;
653}
pbrook56aebc82008-10-11 17:55:29 +0000654
Andreas Färber385b9f02013-06-27 18:25:36 +0200655static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000656{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200657 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200658 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000659 GDBRegisterState *r;
660
Andreas Färbera0e372f2013-06-28 23:18:47 +0200661 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200662 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200663 }
pbrook56aebc82008-10-11 17:55:29 +0000664
Andreas Färbereac8b352013-06-28 21:11:37 +0200665 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000666 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
667 return r->get_reg(env, mem_buf, reg - r->base_reg);
668 }
669 }
670 return 0;
671}
672
Andreas Färber385b9f02013-06-27 18:25:36 +0200673static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000674{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200675 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200676 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000677 GDBRegisterState *r;
678
Andreas Färbera0e372f2013-06-28 23:18:47 +0200679 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200680 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200681 }
pbrook56aebc82008-10-11 17:55:29 +0000682
Andreas Färbereac8b352013-06-28 21:11:37 +0200683 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000684 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
685 return r->set_reg(env, mem_buf, reg - r->base_reg);
686 }
687 }
688 return 0;
689}
690
691/* Register a supplemental set of CPU registers. If g_pos is nonzero it
692 specifies the first register number and these registers are included in
693 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
694 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
695 */
696
Andreas Färber22169d42013-06-28 21:27:39 +0200697void gdb_register_coprocessor(CPUState *cpu,
698 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
699 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000700{
701 GDBRegisterState *s;
702 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000703
Andreas Färbereac8b352013-06-28 21:11:37 +0200704 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000705 while (*p) {
706 /* Check for duplicates. */
707 if (strcmp((*p)->xml, xml) == 0)
708 return;
709 p = &(*p)->next;
710 }
Stefan Weil9643c252011-10-18 22:25:38 +0200711
712 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200713 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200714 s->num_regs = num_regs;
715 s->get_reg = get_reg;
716 s->set_reg = set_reg;
717 s->xml = xml;
718
pbrook56aebc82008-10-11 17:55:29 +0000719 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200720 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000721 *p = s;
722 if (g_pos) {
723 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800724 error_report("Error: Bad gdb register numbering for '%s', "
725 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200726 } else {
727 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000728 }
729 }
730}
731
aliguoria1d1bb32008-11-18 20:07:32 +0000732#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100733/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
734static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
735{
736 static const int xlat[] = {
737 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
738 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
739 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
740 };
741
742 CPUClass *cc = CPU_GET_CLASS(cpu);
743 int cputype = xlat[gdbtype];
744
745 if (cc->gdb_stop_before_watchpoint) {
746 cputype |= BP_STOP_BEFORE_ACCESS;
747 }
748 return cputype;
749}
aliguoria1d1bb32008-11-18 20:07:32 +0000750#endif
751
aliguori880a7572008-11-18 20:30:24 +0000752static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000753{
Andreas Färber182735e2013-05-29 22:29:20 +0200754 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000755 int err = 0;
756
Andreas Färber62278812013-06-27 17:12:06 +0200757 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200758 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200759 }
aliguorie22a25c2009-03-12 20:12:48 +0000760
aliguoria1d1bb32008-11-18 20:07:32 +0000761 switch (type) {
762 case GDB_BREAKPOINT_SW:
763 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200764 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200765 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
766 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000767 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200768 }
aliguori880a7572008-11-18 20:30:24 +0000769 }
770 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000771#ifndef CONFIG_USER_ONLY
772 case GDB_WATCHPOINT_WRITE:
773 case GDB_WATCHPOINT_READ:
774 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200775 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100776 err = cpu_watchpoint_insert(cpu, addr, len,
777 xlat_gdb_type(cpu, type), NULL);
778 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000779 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +0100780 }
aliguori880a7572008-11-18 20:30:24 +0000781 }
782 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000783#endif
784 default:
785 return -ENOSYS;
786 }
787}
788
aliguori880a7572008-11-18 20:30:24 +0000789static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000790{
Andreas Färber182735e2013-05-29 22:29:20 +0200791 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000792 int err = 0;
793
Andreas Färber62278812013-06-27 17:12:06 +0200794 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200795 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200796 }
aliguorie22a25c2009-03-12 20:12:48 +0000797
aliguoria1d1bb32008-11-18 20:07:32 +0000798 switch (type) {
799 case GDB_BREAKPOINT_SW:
800 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200801 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200802 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
803 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000804 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200805 }
aliguori880a7572008-11-18 20:30:24 +0000806 }
807 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000808#ifndef CONFIG_USER_ONLY
809 case GDB_WATCHPOINT_WRITE:
810 case GDB_WATCHPOINT_READ:
811 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200812 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100813 err = cpu_watchpoint_remove(cpu, addr, len,
814 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +0000815 if (err)
816 break;
817 }
818 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000819#endif
820 default:
821 return -ENOSYS;
822 }
823}
824
aliguori880a7572008-11-18 20:30:24 +0000825static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +0000826{
Andreas Färber182735e2013-05-29 22:29:20 +0200827 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000828
aliguorie22a25c2009-03-12 20:12:48 +0000829 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200830 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +0000831 return;
832 }
833
Andreas Färberbdc44642013-06-24 23:50:24 +0200834 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200835 cpu_breakpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000836#ifndef CONFIG_USER_ONLY
Andreas Färber75a34032013-09-02 16:57:02 +0200837 cpu_watchpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000838#endif
aliguori880a7572008-11-18 20:30:24 +0000839 }
aliguoria1d1bb32008-11-18 20:07:32 +0000840}
841
aurel32fab9d282009-04-08 21:29:37 +0000842static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
843{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200844 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +0200845
846 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -0700847 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +0000848}
849
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200850static CPUState *find_cpu(uint32_t thread_id)
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700851{
Andreas Färber0d342822012-12-17 07:12:13 +0100852 CPUState *cpu;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700853
Andreas Färberbdc44642013-06-24 23:50:24 +0200854 CPU_FOREACH(cpu) {
Alex Bennéed2a6c852017-07-12 11:52:14 +0100855 if (cpu_gdb_index(cpu) == thread_id) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200856 return cpu;
Andreas Färberaa48dd92013-07-09 20:50:52 +0200857 }
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700858 }
Andreas Färberaa48dd92013-07-09 20:50:52 +0200859
860 return NULL;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700861}
862
Jan Kiszka4dabe742015-02-07 09:38:43 +0100863static int is_query_packet(const char *p, const char *query, char separator)
864{
865 unsigned int query_len = strlen(query);
866
867 return strncmp(p, query, query_len) == 0 &&
868 (p[query_len] == '\0' || p[query_len] == separator);
869}
870
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100871/**
872 * gdb_handle_vcont - Parses and handles a vCont packet.
873 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
874 * a format error, 0 on success.
875 */
876static int gdb_handle_vcont(GDBState *s, const char *p)
877{
878 int res, idx, signal = 0;
879 char cur_action;
880 char *newstates;
881 unsigned long tmp;
882 CPUState *cpu;
883#ifdef CONFIG_USER_ONLY
884 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
885
886 CPU_FOREACH(cpu) {
887 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
888 }
889#endif
890 /* uninitialised CPUs stay 0 */
891 newstates = g_new0(char, max_cpus);
892
893 /* mark valid CPUs with 1 */
894 CPU_FOREACH(cpu) {
895 newstates[cpu->cpu_index] = 1;
896 }
897
898 /*
899 * res keeps track of what error we are returning, with -ENOTSUP meaning
900 * that the command is unknown or unsupported, thus returning an empty
901 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
902 * or incorrect parameters passed.
903 */
904 res = 0;
905 while (*p) {
906 if (*p++ != ';') {
907 res = -ENOTSUP;
908 goto out;
909 }
910
911 cur_action = *p++;
912 if (cur_action == 'C' || cur_action == 'S') {
913 cur_action = tolower(cur_action);
914 res = qemu_strtoul(p + 1, &p, 16, &tmp);
915 if (res) {
916 goto out;
917 }
918 signal = gdb_signal_to_target(tmp);
919 } else if (cur_action != 'c' && cur_action != 's') {
920 /* unknown/invalid/unsupported command */
921 res = -ENOTSUP;
922 goto out;
923 }
924 /* thread specification. special values: (none), -1 = all; 0 = any */
925 if ((p[0] == ':' && p[1] == '-' && p[2] == '1') || (p[0] != ':')) {
926 if (*p == ':') {
927 p += 3;
928 }
929 for (idx = 0; idx < max_cpus; idx++) {
930 if (newstates[idx] == 1) {
931 newstates[idx] = cur_action;
932 }
933 }
934 } else if (*p == ':') {
935 p++;
936 res = qemu_strtoul(p, &p, 16, &tmp);
937 if (res) {
938 goto out;
939 }
940 idx = tmp;
941 /* 0 means any thread, so we pick the first valid CPU */
942 if (!idx) {
Alex Bennéed2a6c852017-07-12 11:52:14 +0100943 idx = cpu_gdb_index(first_cpu);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100944 }
945
946 /*
947 * If we are in user mode, the thread specified is actually a
948 * thread id, and not an index. We need to find the actual
949 * CPU first, and only then we can use its index.
950 */
951 cpu = find_cpu(idx);
952 /* invalid CPU/thread specified */
953 if (!idx || !cpu) {
954 res = -EINVAL;
955 goto out;
956 }
957 /* only use if no previous match occourred */
958 if (newstates[cpu->cpu_index] == 1) {
959 newstates[cpu->cpu_index] = cur_action;
960 }
961 }
962 }
963 s->signal = signal;
964 gdb_continue_partial(s, newstates);
965
966out:
967 g_free(newstates);
968
969 return res;
970}
971
aliguori880a7572008-11-18 20:30:24 +0000972static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +0000973{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200974 CPUState *cpu;
Andreas Färber5b24c642013-07-07 15:08:22 +0200975 CPUClass *cc;
bellardb4608c02003-06-27 17:34:32 +0000976 const char *p;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700977 uint32_t thread;
978 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +0000979 char buf[MAX_PACKET_LENGTH];
980 uint8_t mem_buf[MAX_PACKET_LENGTH];
981 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +0000982 target_ulong addr, len;
ths3b46e622007-09-17 08:09:54 +0000983
Alex Bennée118e2262017-07-12 11:52:13 +0100984
985 gdb_debug("command='%s'\n", line_buf);
986
bellard858693c2004-03-31 18:52:07 +0000987 p = line_buf;
988 ch = *p++;
989 switch(ch) {
990 case '?':
bellard1fddef42005-04-17 19:16:13 +0000991 /* TODO: Make this return the correct value for user-mode. */
aurel32ca587a82008-12-18 22:44:13 +0000992 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
Alex Bennéed2a6c852017-07-12 11:52:14 +0100993 cpu_gdb_index(s->c_cpu));
bellard858693c2004-03-31 18:52:07 +0000994 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +0000995 /* Remove all the breakpoints when this query is issued,
996 * because gdb is doing and initial connect and the state
997 * should be cleaned up.
998 */
aliguori880a7572008-11-18 20:30:24 +0000999 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +00001000 break;
1001 case 'c':
1002 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +00001003 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001004 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001005 }
aurel32ca587a82008-12-18 22:44:13 +00001006 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +00001007 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +00001008 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +00001009 case 'C':
aurel32ca587a82008-12-18 22:44:13 +00001010 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
1011 if (s->signal == -1)
1012 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +00001013 gdb_continue(s);
1014 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001015 case 'v':
1016 if (strncmp(p, "Cont", 4) == 0) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001017 p += 4;
1018 if (*p == '?') {
1019 put_packet(s, "vCont;c;C;s;S");
1020 break;
1021 }
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001022
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001023 res = gdb_handle_vcont(s, p);
1024
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001025 if (res) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001026 if ((res == -EINVAL) || (res == -ERANGE)) {
1027 put_packet(s, "E22");
1028 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001029 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001030 goto unknown_command;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001031 }
1032 break;
1033 } else {
1034 goto unknown_command;
1035 }
edgar_igl7d03f822008-05-17 18:58:29 +00001036 case 'k':
1037 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001038 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00001039 exit(0);
1040 case 'D':
1041 /* Detach packet */
aliguori880a7572008-11-18 20:30:24 +00001042 gdb_breakpoint_remove_all();
Daniel Gutson7ea06da2010-02-26 14:13:50 -03001043 gdb_syscall_mode = GDB_SYS_DISABLED;
edgar_igl7d03f822008-05-17 18:58:29 +00001044 gdb_continue(s);
1045 put_packet(s, "OK");
1046 break;
bellard858693c2004-03-31 18:52:07 +00001047 case 's':
1048 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +00001049 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001050 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001051 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001052 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +00001053 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +00001054 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +00001055 case 'F':
1056 {
1057 target_ulong ret;
1058 target_ulong err;
1059
1060 ret = strtoull(p, (char **)&p, 16);
1061 if (*p == ',') {
1062 p++;
1063 err = strtoull(p, (char **)&p, 16);
1064 } else {
1065 err = 0;
1066 }
1067 if (*p == ',')
1068 p++;
1069 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001070 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001071 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +00001072 s->current_syscall_cb = NULL;
1073 }
pbrooka2d1eba2007-01-28 03:10:55 +00001074 if (type == 'C') {
1075 put_packet(s, "T02");
1076 } else {
edgar_iglba70a622008-03-14 06:10:42 +00001077 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +00001078 }
1079 }
1080 break;
bellard858693c2004-03-31 18:52:07 +00001081 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001082 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001083 len = 0;
Andreas Färber35143f02013-08-12 18:09:47 +02001084 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001085 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +00001086 len += reg_size;
1087 }
1088 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +00001089 put_packet(s, buf);
1090 break;
1091 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001092 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001093 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +00001094 len = strlen(p) / 2;
1095 hextomem((uint8_t *)registers, p, len);
Andreas Färber35143f02013-08-12 18:09:47 +02001096 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001097 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001098 len -= reg_size;
1099 registers += reg_size;
1100 }
bellard858693c2004-03-31 18:52:07 +00001101 put_packet(s, "OK");
1102 break;
1103 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001104 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001105 if (*p == ',')
1106 p++;
bellard9d9754a2006-06-25 15:32:37 +00001107 len = strtoull(p, NULL, 16);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001108
1109 /* memtohex() doubles the required space */
1110 if (len > MAX_PACKET_LENGTH / 2) {
1111 put_packet (s, "E22");
1112 break;
1113 }
1114
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001115 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001116 put_packet (s, "E14");
1117 } else {
1118 memtohex(buf, mem_buf, len);
1119 put_packet(s, buf);
1120 }
bellard858693c2004-03-31 18:52:07 +00001121 break;
1122 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001123 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001124 if (*p == ',')
1125 p++;
bellard9d9754a2006-06-25 15:32:37 +00001126 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001127 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001128 p++;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001129
1130 /* hextomem() reads 2*len bytes */
1131 if (len > strlen(p) / 2) {
1132 put_packet (s, "E22");
1133 break;
1134 }
bellard858693c2004-03-31 18:52:07 +00001135 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001136 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +02001137 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +00001138 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001139 } else {
bellard858693c2004-03-31 18:52:07 +00001140 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001141 }
bellard858693c2004-03-31 18:52:07 +00001142 break;
pbrook56aebc82008-10-11 17:55:29 +00001143 case 'p':
1144 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1145 This works, but can be very slow. Anything new enough to
1146 understand XML also knows how to use this properly. */
1147 if (!gdb_has_xml)
1148 goto unknown_command;
1149 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001150 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001151 if (reg_size) {
1152 memtohex(buf, mem_buf, reg_size);
1153 put_packet(s, buf);
1154 } else {
1155 put_packet(s, "E14");
1156 }
1157 break;
1158 case 'P':
1159 if (!gdb_has_xml)
1160 goto unknown_command;
1161 addr = strtoull(p, (char **)&p, 16);
1162 if (*p == '=')
1163 p++;
1164 reg_size = strlen(p) / 2;
1165 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001166 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001167 put_packet(s, "OK");
1168 break;
bellard858693c2004-03-31 18:52:07 +00001169 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001170 case 'z':
1171 type = strtoul(p, (char **)&p, 16);
1172 if (*p == ',')
1173 p++;
bellard9d9754a2006-06-25 15:32:37 +00001174 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001175 if (*p == ',')
1176 p++;
bellard9d9754a2006-06-25 15:32:37 +00001177 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001178 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001179 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001180 else
aliguori880a7572008-11-18 20:30:24 +00001181 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001182 if (res >= 0)
1183 put_packet(s, "OK");
1184 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001185 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001186 else
1187 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001188 break;
aliguori880a7572008-11-18 20:30:24 +00001189 case 'H':
1190 type = *p++;
1191 thread = strtoull(p, (char **)&p, 16);
1192 if (thread == -1 || thread == 0) {
1193 put_packet(s, "OK");
1194 break;
1195 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001196 cpu = find_cpu(thread);
1197 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001198 put_packet(s, "E22");
1199 break;
1200 }
1201 switch (type) {
1202 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001203 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001204 put_packet(s, "OK");
1205 break;
1206 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001207 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001208 put_packet(s, "OK");
1209 break;
1210 default:
1211 put_packet(s, "E22");
1212 break;
1213 }
1214 break;
1215 case 'T':
1216 thread = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001217 cpu = find_cpu(thread);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001218
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001219 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001220 put_packet(s, "OK");
1221 } else {
aliguori880a7572008-11-18 20:30:24 +00001222 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001223 }
aliguori880a7572008-11-18 20:30:24 +00001224 break;
pbrook978efd62006-06-17 18:30:42 +00001225 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001226 case 'Q':
1227 /* parse any 'q' packets here */
1228 if (!strcmp(p,"qemu.sstepbits")) {
1229 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001230 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1231 SSTEP_ENABLE,
1232 SSTEP_NOIRQ,
1233 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001234 put_packet(s, buf);
1235 break;
Jan Kiszka4dabe742015-02-07 09:38:43 +01001236 } else if (is_query_packet(p, "qemu.sstep", '=')) {
edgar_igl60897d32008-05-09 08:25:14 +00001237 /* Display or change the sstep_flags */
1238 p += 10;
1239 if (*p != '=') {
1240 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001241 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001242 put_packet(s, buf);
1243 break;
1244 }
1245 p++;
1246 type = strtoul(p, (char **)&p, 16);
1247 sstep_flags = type;
1248 put_packet(s, "OK");
1249 break;
aliguori880a7572008-11-18 20:30:24 +00001250 } else if (strcmp(p,"C") == 0) {
1251 /* "Current thread" remains vague in the spec, so always return
1252 * the first CPU (gdb returns the first thread). */
1253 put_packet(s, "QC1");
1254 break;
1255 } else if (strcmp(p,"fThreadInfo") == 0) {
Andreas Färber52f34622013-06-27 13:44:40 +02001256 s->query_cpu = first_cpu;
aliguori880a7572008-11-18 20:30:24 +00001257 goto report_cpuinfo;
1258 } else if (strcmp(p,"sThreadInfo") == 0) {
1259 report_cpuinfo:
1260 if (s->query_cpu) {
Alex Bennéed2a6c852017-07-12 11:52:14 +01001261 snprintf(buf, sizeof(buf), "m%x", cpu_gdb_index(s->query_cpu));
aliguori880a7572008-11-18 20:30:24 +00001262 put_packet(s, buf);
Andreas Färberbdc44642013-06-24 23:50:24 +02001263 s->query_cpu = CPU_NEXT(s->query_cpu);
aliguori880a7572008-11-18 20:30:24 +00001264 } else
1265 put_packet(s, "l");
1266 break;
1267 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1268 thread = strtoull(p+16, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001269 cpu = find_cpu(thread);
1270 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001271 cpu_synchronize_state(cpu);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001272 /* memtohex() doubles the required space */
1273 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
Andreas Färber55e5c282012-12-17 06:18:02 +01001274 "CPU#%d [%s]", cpu->cpu_index,
Andreas Färber259186a2013-01-17 18:51:17 +01001275 cpu->halted ? "halted " : "running");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001276 memtohex(buf, mem_buf, len);
1277 put_packet(s, buf);
1278 }
aliguori880a7572008-11-18 20:30:24 +00001279 break;
edgar_igl60897d32008-05-09 08:25:14 +00001280 }
blueswir10b8a9882009-03-07 10:51:36 +00001281#ifdef CONFIG_USER_ONLY
Jan Kiszka070949f2015-02-07 09:38:42 +01001282 else if (strcmp(p, "Offsets") == 0) {
Andreas Färber0429a972013-08-26 18:14:44 +02001283 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001284
blueswir1363a37d2008-08-21 17:58:08 +00001285 snprintf(buf, sizeof(buf),
1286 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1287 ";Bss=" TARGET_ABI_FMT_lx,
1288 ts->info->code_offset,
1289 ts->info->data_offset,
1290 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001291 put_packet(s, buf);
1292 break;
1293 }
blueswir10b8a9882009-03-07 10:51:36 +00001294#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001295 else if (strncmp(p, "Rcmd,", 5) == 0) {
1296 int len = strlen(p + 5);
1297
1298 if ((len % 2) != 0) {
1299 put_packet(s, "E01");
1300 break;
1301 }
aliguori8a34a0f2009-03-05 23:01:55 +00001302 len = len / 2;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001303 hextomem(mem_buf, p + 5, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001304 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001305 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001306 put_packet(s, "OK");
1307 break;
1308 }
blueswir10b8a9882009-03-07 10:51:36 +00001309#endif /* !CONFIG_USER_ONLY */
Jan Kiszka4dabe742015-02-07 09:38:43 +01001310 if (is_query_packet(p, "Supported", ':')) {
blueswir15b3715b2008-10-25 11:18:12 +00001311 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
Andreas Färber5b24c642013-07-07 15:08:22 +02001312 cc = CPU_GET_CLASS(first_cpu);
1313 if (cc->gdb_core_xml_file != NULL) {
1314 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1315 }
pbrook56aebc82008-10-11 17:55:29 +00001316 put_packet(s, buf);
1317 break;
1318 }
pbrook56aebc82008-10-11 17:55:29 +00001319 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1320 const char *xml;
1321 target_ulong total_len;
1322
Andreas Färber5b24c642013-07-07 15:08:22 +02001323 cc = CPU_GET_CLASS(first_cpu);
1324 if (cc->gdb_core_xml_file == NULL) {
1325 goto unknown_command;
1326 }
1327
Andreas Färber5b50e792013-06-29 04:18:45 +02001328 gdb_has_xml = true;
pbrook56aebc82008-10-11 17:55:29 +00001329 p += 19;
Andreas Färber5b24c642013-07-07 15:08:22 +02001330 xml = get_feature_xml(p, &p, cc);
pbrook56aebc82008-10-11 17:55:29 +00001331 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001332 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001333 put_packet(s, buf);
1334 break;
1335 }
1336
1337 if (*p == ':')
1338 p++;
1339 addr = strtoul(p, (char **)&p, 16);
1340 if (*p == ',')
1341 p++;
1342 len = strtoul(p, (char **)&p, 16);
1343
1344 total_len = strlen(xml);
1345 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001346 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001347 put_packet(s, buf);
1348 break;
1349 }
1350 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1351 len = (MAX_PACKET_LENGTH - 5) / 2;
1352 if (len < total_len - addr) {
1353 buf[0] = 'm';
1354 len = memtox(buf + 1, xml + addr, len);
1355 } else {
1356 buf[0] = 'l';
1357 len = memtox(buf + 1, xml + addr, total_len - addr);
1358 }
1359 put_packet_binary(s, buf, len + 1);
1360 break;
1361 }
Jan Kiszkaa3919382015-02-07 09:38:44 +01001362 if (is_query_packet(p, "Attached", ':')) {
1363 put_packet(s, GDB_ATTACHED);
1364 break;
1365 }
pbrook56aebc82008-10-11 17:55:29 +00001366 /* Unrecognised 'q' command. */
1367 goto unknown_command;
1368
bellard858693c2004-03-31 18:52:07 +00001369 default:
pbrook56aebc82008-10-11 17:55:29 +00001370 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001371 /* put empty packet */
1372 buf[0] = '\0';
1373 put_packet(s, buf);
1374 break;
1375 }
1376 return RS_IDLE;
1377}
1378
Andreas Färber64f6b342013-05-27 02:06:09 +02001379void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001380{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001381 gdbserver_state->c_cpu = cpu;
1382 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001383}
1384
bellard1fddef42005-04-17 19:16:13 +00001385#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001386static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001387{
aliguori880a7572008-11-18 20:30:24 +00001388 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001389 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001390 char buf[256];
aliguorid6fc1b32008-11-18 19:55:44 +00001391 const char *type;
bellard858693c2004-03-31 18:52:07 +00001392 int ret;
1393
Meador Ingecdb432b2012-03-15 17:49:45 +00001394 if (running || s->state == RS_INACTIVE) {
1395 return;
1396 }
1397 /* Is there a GDB syscall waiting to be sent? */
1398 if (s->current_syscall_cb) {
1399 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001400 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001401 }
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001402 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001403 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02001404 if (cpu->watchpoint_hit) {
1405 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001406 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001407 type = "r";
1408 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001409 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001410 type = "a";
1411 break;
1412 default:
1413 type = "";
1414 break;
1415 }
aliguori880a7572008-11-18 20:30:24 +00001416 snprintf(buf, sizeof(buf),
1417 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
Alex Bennéed2a6c852017-07-12 11:52:14 +01001418 GDB_SIGNAL_TRAP, cpu_gdb_index(cpu), type,
Andreas Färberff4700b2013-08-26 18:23:18 +02001419 (target_ulong)cpu->watchpoint_hit->vaddr);
1420 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001421 goto send_packet;
pbrook6658ffb2007-03-16 23:58:11 +00001422 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001423 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00001424 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001425 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001426 case RUN_STATE_PAUSED:
aliguori9781e042009-01-22 17:15:29 +00001427 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001428 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001429 case RUN_STATE_SHUTDOWN:
Jan Kiszka425189a2011-03-22 11:02:09 +01001430 ret = GDB_SIGNAL_QUIT;
1431 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001432 case RUN_STATE_IO_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001433 ret = GDB_SIGNAL_IO;
1434 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001435 case RUN_STATE_WATCHDOG:
Jan Kiszka425189a2011-03-22 11:02:09 +01001436 ret = GDB_SIGNAL_ALRM;
1437 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001438 case RUN_STATE_INTERNAL_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001439 ret = GDB_SIGNAL_ABRT;
1440 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001441 case RUN_STATE_SAVE_VM:
1442 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001443 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001444 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001445 ret = GDB_SIGNAL_XCPU;
1446 break;
1447 default:
1448 ret = GDB_SIGNAL_UNKNOWN;
1449 break;
bellardbbeb7b52006-04-23 18:42:15 +00001450 }
Jan Kiszka226d0072015-07-24 18:52:31 +02001451 gdb_set_stop_cpu(cpu);
Alex Bennéed2a6c852017-07-12 11:52:14 +01001452 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_gdb_index(cpu));
Jan Kiszka425189a2011-03-22 11:02:09 +01001453
1454send_packet:
bellard858693c2004-03-31 18:52:07 +00001455 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001456
1457 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001458 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001459}
bellard1fddef42005-04-17 19:16:13 +00001460#endif
bellard858693c2004-03-31 18:52:07 +00001461
pbrooka2d1eba2007-01-28 03:10:55 +00001462/* Send a gdb syscall request.
1463 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001464 %x - target_ulong argument printed in hex.
1465 %lx - 64-bit argument printed in hex.
1466 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01001467void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00001468{
pbrooka2d1eba2007-01-28 03:10:55 +00001469 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001470 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001471 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001472 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001473 GDBState *s;
1474
aliguori880a7572008-11-18 20:30:24 +00001475 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001476 if (!s)
1477 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001478 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001479#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001480 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001481#endif
Meador Ingecdb432b2012-03-15 17:49:45 +00001482 p = s->syscall_buf;
1483 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001484 *(p++) = 'F';
1485 while (*fmt) {
1486 if (*fmt == '%') {
1487 fmt++;
1488 switch (*fmt++) {
1489 case 'x':
1490 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001491 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001492 break;
pbrooka87295e2007-05-26 15:09:38 +00001493 case 'l':
1494 if (*(fmt++) != 'x')
1495 goto bad_format;
1496 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001497 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001498 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001499 case 's':
1500 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001501 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001502 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001503 break;
1504 default:
pbrooka87295e2007-05-26 15:09:38 +00001505 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001506 error_report("gdbstub: Bad syscall format string '%s'",
1507 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00001508 break;
1509 }
1510 } else {
1511 *(p++) = *(fmt++);
1512 }
1513 }
pbrook8a93e022007-08-06 13:19:15 +00001514 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001515#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001516 put_packet(s, s->syscall_buf);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001517 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001518#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001519 /* In this case wait to send the syscall packet until notification that
1520 the CPU has stopped. This must be done because if the packet is sent
1521 now the reply from the syscall request could be received while the CPU
1522 is still in the running state, which can cause packets to be dropped
1523 and state transition 'T' packets to be sent while the syscall is still
1524 being processed. */
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001525 qemu_cpu_kick(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00001526#endif
1527}
1528
Peter Maydell19239b32015-09-07 10:39:27 +01001529void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
1530{
1531 va_list va;
1532
1533 va_start(va, fmt);
1534 gdb_do_syscallv(cb, fmt, va);
1535 va_end(va);
1536}
1537
bellard6a00d602005-11-21 23:25:50 +00001538static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00001539{
ths60fe76f2007-12-16 03:02:09 +00001540 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00001541
bellard1fddef42005-04-17 19:16:13 +00001542#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001543 if (s->last_packet_len) {
1544 /* Waiting for a response to the last packet. If we see the start
1545 of a new command then abandon the previous response. */
1546 if (ch == '-') {
Alex Bennée118e2262017-07-12 11:52:13 +01001547 gdb_debug("Got NACK, retransmitting\n");
thsffe8ab82007-12-16 03:16:05 +00001548 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
Alex Bennée118e2262017-07-12 11:52:13 +01001549 } else if (ch == '+') {
1550 gdb_debug("Got ACK\n");
1551 } else {
1552 gdb_debug("Got '%c' when expecting ACK/NACK\n", ch);
pbrook4046d912007-01-28 01:53:16 +00001553 }
Alex Bennée118e2262017-07-12 11:52:13 +01001554
pbrook4046d912007-01-28 01:53:16 +00001555 if (ch == '+' || ch == '$')
1556 s->last_packet_len = 0;
1557 if (ch != '$')
1558 return;
1559 }
Luiz Capitulino13548692011-07-29 15:36:43 -03001560 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00001561 /* when the CPU is running, we cannot do anything except stop
1562 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001563 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00001564 } else
bellard1fddef42005-04-17 19:16:13 +00001565#endif
bellard41625032005-04-24 10:07:11 +00001566 {
bellard858693c2004-03-31 18:52:07 +00001567 switch(s->state) {
1568 case RS_IDLE:
1569 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04001570 /* start of command packet */
bellard858693c2004-03-31 18:52:07 +00001571 s->line_buf_index = 0;
Doug Gale4bf43122017-05-01 12:22:10 -04001572 s->line_sum = 0;
bellard858693c2004-03-31 18:52:07 +00001573 s->state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04001574 } else {
Alex Bennée118e2262017-07-12 11:52:13 +01001575 gdb_debug("received garbage between packets: 0x%x\n", ch);
bellard4c3a88a2003-07-26 12:06:08 +00001576 }
1577 break;
bellard858693c2004-03-31 18:52:07 +00001578 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04001579 if (ch == '}') {
1580 /* start escape sequence */
1581 s->state = RS_GETLINE_ESC;
1582 s->line_sum += ch;
1583 } else if (ch == '*') {
1584 /* start run length encoding sequence */
1585 s->state = RS_GETLINE_RLE;
1586 s->line_sum += ch;
1587 } else if (ch == '#') {
1588 /* end of command, start of checksum*/
1589 s->state = RS_CHKSUM1;
bellard858693c2004-03-31 18:52:07 +00001590 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
Alex Bennée118e2262017-07-12 11:52:13 +01001591 gdb_debug("command buffer overrun, dropping command\n");
bellard858693c2004-03-31 18:52:07 +00001592 s->state = RS_IDLE;
1593 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04001594 /* unescaped command character */
1595 s->line_buf[s->line_buf_index++] = ch;
1596 s->line_sum += ch;
1597 }
1598 break;
1599 case RS_GETLINE_ESC:
1600 if (ch == '#') {
1601 /* unexpected end of command in escape sequence */
1602 s->state = RS_CHKSUM1;
1603 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1604 /* command buffer overrun */
Alex Bennée118e2262017-07-12 11:52:13 +01001605 gdb_debug("command buffer overrun, dropping command\n");
Doug Gale4bf43122017-05-01 12:22:10 -04001606 s->state = RS_IDLE;
1607 } else {
1608 /* parse escaped character and leave escape state */
1609 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
1610 s->line_sum += ch;
1611 s->state = RS_GETLINE;
1612 }
1613 break;
1614 case RS_GETLINE_RLE:
1615 if (ch < ' ') {
1616 /* invalid RLE count encoding */
Alex Bennée118e2262017-07-12 11:52:13 +01001617 gdb_debug("got invalid RLE count: 0x%x\n", ch);
Doug Gale4bf43122017-05-01 12:22:10 -04001618 s->state = RS_GETLINE;
1619 } else {
1620 /* decode repeat length */
1621 int repeat = (unsigned char)ch - ' ' + 3;
1622 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
1623 /* that many repeats would overrun the command buffer */
Alex Bennée118e2262017-07-12 11:52:13 +01001624 gdb_debug("command buffer overrun, dropping command\n");
Doug Gale4bf43122017-05-01 12:22:10 -04001625 s->state = RS_IDLE;
1626 } else if (s->line_buf_index < 1) {
1627 /* got a repeat but we have nothing to repeat */
Alex Bennée118e2262017-07-12 11:52:13 +01001628 gdb_debug("got invalid RLE sequence\n");
Doug Gale4bf43122017-05-01 12:22:10 -04001629 s->state = RS_GETLINE;
1630 } else {
1631 /* repeat the last character */
1632 memset(s->line_buf + s->line_buf_index,
1633 s->line_buf[s->line_buf_index - 1], repeat);
1634 s->line_buf_index += repeat;
1635 s->line_sum += ch;
1636 s->state = RS_GETLINE;
1637 }
bellard858693c2004-03-31 18:52:07 +00001638 }
1639 break;
1640 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04001641 /* get high hex digit of checksum */
1642 if (!isxdigit(ch)) {
Alex Bennée118e2262017-07-12 11:52:13 +01001643 gdb_debug("got invalid command checksum digit\n");
Doug Gale4bf43122017-05-01 12:22:10 -04001644 s->state = RS_GETLINE;
1645 break;
1646 }
bellard858693c2004-03-31 18:52:07 +00001647 s->line_buf[s->line_buf_index] = '\0';
1648 s->line_csum = fromhex(ch) << 4;
1649 s->state = RS_CHKSUM2;
1650 break;
1651 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04001652 /* get low hex digit of checksum */
1653 if (!isxdigit(ch)) {
Alex Bennée118e2262017-07-12 11:52:13 +01001654 gdb_debug("got invalid command checksum digit\n");
Doug Gale4bf43122017-05-01 12:22:10 -04001655 s->state = RS_GETLINE;
1656 break;
bellard858693c2004-03-31 18:52:07 +00001657 }
Doug Gale4bf43122017-05-01 12:22:10 -04001658 s->line_csum |= fromhex(ch);
1659
1660 if (s->line_csum != (s->line_sum & 0xff)) {
Alex Bennée118e2262017-07-12 11:52:13 +01001661 gdb_debug("got command packet with incorrect checksum\n");
Doug Gale4bf43122017-05-01 12:22:10 -04001662 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00001663 reply = '-';
1664 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00001665 s->state = RS_IDLE;
1666 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04001667 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00001668 reply = '+';
1669 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00001670 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00001671 }
bellardb4608c02003-06-27 17:34:32 +00001672 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001673 default:
1674 abort();
bellardb4608c02003-06-27 17:34:32 +00001675 }
1676 }
bellard858693c2004-03-31 18:52:07 +00001677}
1678
Paul Brook0e1c9c52010-06-16 13:03:51 +01001679/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001680void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01001681{
1682 GDBState *s;
1683 char buf[4];
1684
1685 s = gdbserver_state;
1686 if (!s) {
1687 return;
1688 }
1689#ifdef CONFIG_USER_ONLY
1690 if (gdbserver_fd < 0 || s->fd < 0) {
1691 return;
1692 }
1693#endif
1694
1695 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
1696 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001697
1698#ifndef CONFIG_USER_ONLY
Marc-André Lureau1ce26102017-01-27 00:49:13 +04001699 qemu_chr_fe_deinit(&s->chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001700#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01001701}
1702
bellard1fddef42005-04-17 19:16:13 +00001703#ifdef CONFIG_USER_ONLY
1704int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001705gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00001706{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001707 GDBState *s;
1708 char buf[256];
1709 int n;
bellard1fddef42005-04-17 19:16:13 +00001710
Andreas Färber5ca666c2013-06-24 19:20:57 +02001711 s = gdbserver_state;
1712 if (gdbserver_fd < 0 || s->fd < 0) {
1713 return sig;
bellard1fddef42005-04-17 19:16:13 +00001714 }
1715
Andreas Färber5ca666c2013-06-24 19:20:57 +02001716 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001717 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001718 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00001719
Andreas Färber5ca666c2013-06-24 19:20:57 +02001720 if (sig != 0) {
1721 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
1722 put_packet(s, buf);
1723 }
1724 /* put_packet() might have detected that the peer terminated the
1725 connection. */
1726 if (s->fd < 0) {
1727 return sig;
1728 }
1729
1730 sig = 0;
1731 s->state = RS_IDLE;
1732 s->running_state = 0;
1733 while (s->running_state == 0) {
1734 n = read(s->fd, buf, 256);
1735 if (n > 0) {
1736 int i;
1737
1738 for (i = 0; i < n; i++) {
1739 gdb_read_byte(s, buf[i]);
1740 }
Peter Wu5819e3e2016-06-05 16:35:48 +02001741 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02001742 /* XXX: Connection closed. Should probably wait for another
1743 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02001744 if (n == 0) {
1745 close(s->fd);
1746 }
1747 s->fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02001748 return sig;
bellard1fddef42005-04-17 19:16:13 +00001749 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02001750 }
1751 sig = s->signal;
1752 s->signal = 0;
1753 return sig;
bellard1fddef42005-04-17 19:16:13 +00001754}
bellarde9009672005-04-26 20:42:36 +00001755
aurel32ca587a82008-12-18 22:44:13 +00001756/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001757void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00001758{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001759 GDBState *s;
1760 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00001761
Andreas Färber5ca666c2013-06-24 19:20:57 +02001762 s = gdbserver_state;
1763 if (gdbserver_fd < 0 || s->fd < 0) {
1764 return;
1765 }
aurel32ca587a82008-12-18 22:44:13 +00001766
Andreas Färber5ca666c2013-06-24 19:20:57 +02001767 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
1768 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00001769}
bellard1fddef42005-04-17 19:16:13 +00001770
aliguori880a7572008-11-18 20:30:24 +00001771static void gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00001772{
1773 GDBState *s;
1774 struct sockaddr_in sockaddr;
1775 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001776 int fd;
bellard858693c2004-03-31 18:52:07 +00001777
1778 for(;;) {
1779 len = sizeof(sockaddr);
1780 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1781 if (fd < 0 && errno != EINTR) {
1782 perror("accept");
1783 return;
1784 } else if (fd >= 0) {
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001785#ifndef _WIN32
1786 fcntl(fd, F_SETFD, FD_CLOEXEC);
1787#endif
bellard858693c2004-03-31 18:52:07 +00001788 break;
1789 }
1790 }
1791
1792 /* set short latency */
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001793 socket_set_nodelay(fd);
ths3b46e622007-09-17 08:09:54 +00001794
Anthony Liguori7267c092011-08-20 22:09:37 -05001795 s = g_malloc0(sizeof(GDBState));
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001796 s->c_cpu = first_cpu;
1797 s->g_cpu = first_cpu;
bellard858693c2004-03-31 18:52:07 +00001798 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02001799 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00001800
aliguori880a7572008-11-18 20:30:24 +00001801 gdbserver_state = s;
bellard858693c2004-03-31 18:52:07 +00001802}
1803
1804static int gdbserver_open(int port)
1805{
1806 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001807 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00001808
1809 fd = socket(PF_INET, SOCK_STREAM, 0);
1810 if (fd < 0) {
1811 perror("socket");
1812 return -1;
1813 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001814#ifndef _WIN32
1815 fcntl(fd, F_SETFD, FD_CLOEXEC);
1816#endif
bellard858693c2004-03-31 18:52:07 +00001817
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001818 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00001819
1820 sockaddr.sin_family = AF_INET;
1821 sockaddr.sin_port = htons(port);
1822 sockaddr.sin_addr.s_addr = 0;
1823 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1824 if (ret < 0) {
1825 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00001826 close(fd);
bellard858693c2004-03-31 18:52:07 +00001827 return -1;
1828 }
Peter Wu96165b92016-05-04 11:32:17 +02001829 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00001830 if (ret < 0) {
1831 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00001832 close(fd);
bellard858693c2004-03-31 18:52:07 +00001833 return -1;
1834 }
bellard858693c2004-03-31 18:52:07 +00001835 return fd;
1836}
1837
1838int gdbserver_start(int port)
1839{
1840 gdbserver_fd = gdbserver_open(port);
1841 if (gdbserver_fd < 0)
1842 return -1;
1843 /* accept connections */
aliguori880a7572008-11-18 20:30:24 +00001844 gdb_accept();
bellardb4608c02003-06-27 17:34:32 +00001845 return 0;
1846}
aurel322b1319c2008-12-18 22:44:04 +00001847
1848/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07001849void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00001850{
1851 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02001852
1853 if (gdbserver_fd < 0 || s->fd < 0) {
1854 return;
1855 }
aurel322b1319c2008-12-18 22:44:04 +00001856 close(s->fd);
1857 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001858 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02001859 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00001860}
pbrook4046d912007-01-28 01:53:16 +00001861#else
thsaa1f17c2007-07-11 22:48:58 +00001862static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00001863{
pbrook56aebc82008-10-11 17:55:29 +00001864 /* We can handle an arbitrarily large amount of data.
1865 Pick the maximum packet size, which is as good as anything. */
1866 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00001867}
1868
thsaa1f17c2007-07-11 22:48:58 +00001869static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00001870{
pbrook4046d912007-01-28 01:53:16 +00001871 int i;
1872
1873 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00001874 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00001875 }
1876}
1877
1878static void gdb_chr_event(void *opaque, int event)
1879{
1880 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05301881 case CHR_EVENT_OPENED:
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001882 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02001883 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00001884 break;
1885 default:
1886 break;
1887 }
1888}
1889
aliguori8a34a0f2009-03-05 23:01:55 +00001890static void gdb_monitor_output(GDBState *s, const char *msg, int len)
1891{
1892 char buf[MAX_PACKET_LENGTH];
1893
1894 buf[0] = 'O';
1895 if (len > (MAX_PACKET_LENGTH/2) - 1)
1896 len = (MAX_PACKET_LENGTH/2) - 1;
1897 memtohex(buf + 1, (uint8_t *)msg, len);
1898 put_packet(s, buf);
1899}
1900
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001901static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00001902{
1903 const char *p = (const char *)buf;
1904 int max_sz;
1905
1906 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
1907 for (;;) {
1908 if (len <= max_sz) {
1909 gdb_monitor_output(gdbserver_state, p, len);
1910 break;
1911 }
1912 gdb_monitor_output(gdbserver_state, p, max_sz);
1913 p += max_sz;
1914 len -= max_sz;
1915 }
1916 return len;
1917}
1918
aliguori59030a82009-04-05 18:43:41 +00001919#ifndef _WIN32
1920static void gdb_sigterm_handler(int signal)
1921{
Luiz Capitulino13548692011-07-29 15:36:43 -03001922 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001923 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001924 }
aliguori59030a82009-04-05 18:43:41 +00001925}
1926#endif
1927
Marc-André Lureau777357d2016-12-07 18:39:10 +03001928static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
1929 bool *be_opened, Error **errp)
1930{
1931 *be_opened = false;
1932}
1933
1934static void char_gdb_class_init(ObjectClass *oc, void *data)
1935{
1936 ChardevClass *cc = CHARDEV_CLASS(oc);
1937
1938 cc->internal = true;
1939 cc->open = gdb_monitor_open;
1940 cc->chr_write = gdb_monitor_write;
1941}
1942
1943#define TYPE_CHARDEV_GDB "chardev-gdb"
1944
1945static const TypeInfo char_gdb_type_info = {
1946 .name = TYPE_CHARDEV_GDB,
1947 .parent = TYPE_CHARDEV,
1948 .class_init = char_gdb_class_init,
1949};
1950
aliguori59030a82009-04-05 18:43:41 +00001951int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00001952{
1953 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00001954 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001955 Chardev *chr = NULL;
1956 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00001957
Ziyue Yang508b4ec2017-01-18 16:02:41 +08001958 if (!first_cpu) {
1959 error_report("gdbstub: meaningless to attach gdb to a "
1960 "machine without any CPU.");
1961 return -1;
1962 }
1963
aliguori59030a82009-04-05 18:43:41 +00001964 if (!device)
1965 return -1;
1966 if (strcmp(device, "none") != 0) {
1967 if (strstart(device, "tcp:", NULL)) {
1968 /* enforce required TCP attributes */
1969 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
1970 "%s,nowait,nodelay,server", device);
1971 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00001972 }
aliguori59030a82009-04-05 18:43:41 +00001973#ifndef _WIN32
1974 else if (strcmp(device, "stdio") == 0) {
1975 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00001976
aliguori59030a82009-04-05 18:43:41 +00001977 memset(&act, 0, sizeof(act));
1978 act.sa_handler = gdb_sigterm_handler;
1979 sigaction(SIGINT, &act, NULL);
1980 }
1981#endif
Marc-André Lureaub4948be2016-10-22 12:52:46 +03001982 chr = qemu_chr_new_noreplay("gdb", device);
aliguori36556b22009-03-28 18:05:53 +00001983 if (!chr)
1984 return -1;
pbrookcfc34752007-02-22 01:48:01 +00001985 }
1986
aliguori36556b22009-03-28 18:05:53 +00001987 s = gdbserver_state;
1988 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001989 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00001990 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00001991
aliguori36556b22009-03-28 18:05:53 +00001992 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
1993
1994 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03001995 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
1996 NULL, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00001997 monitor_init(mon_chr, 0);
1998 } else {
Marc-André Lureau1ce26102017-01-27 00:49:13 +04001999 qemu_chr_fe_deinit(&s->chr, true);
aliguori36556b22009-03-28 18:05:53 +00002000 mon_chr = s->mon_chr;
2001 memset(s, 0, sizeof(GDBState));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002002 s->mon_chr = mon_chr;
aliguori36556b22009-03-28 18:05:53 +00002003 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002004 s->c_cpu = first_cpu;
2005 s->g_cpu = first_cpu;
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002006 if (chr) {
2007 qemu_chr_fe_init(&s->chr, chr, &error_abort);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002008 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
Anton Nefedov81517ba2017-07-06 15:08:49 +03002009 gdb_chr_event, NULL, NULL, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002010 }
aliguori36556b22009-03-28 18:05:53 +00002011 s->state = chr ? RS_IDLE : RS_INACTIVE;
2012 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00002013 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00002014
pbrook4046d912007-01-28 01:53:16 +00002015 return 0;
2016}
Marc-André Lureau777357d2016-12-07 18:39:10 +03002017
2018static void register_types(void)
2019{
2020 type_register_static(&char_gdb_type_info);
2021}
2022
2023type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00002024#endif