blob: 39659802ad54a05ac6b48e0c435819f05263268a [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
59enum {
60 GDB_SIGNAL_0 = 0,
61 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +010062 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +000063 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +010064 GDB_SIGNAL_ABRT = 6,
65 GDB_SIGNAL_ALRM = 14,
66 GDB_SIGNAL_IO = 23,
67 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +000068 GDB_SIGNAL_UNKNOWN = 143
69};
70
71#ifdef CONFIG_USER_ONLY
72
73/* Map target signal numbers to GDB protocol signal numbers and vice
74 * versa. For user emulation's currently supported systems, we can
75 * assume most signals are defined.
76 */
77
78static int gdb_signal_table[] = {
79 0,
80 TARGET_SIGHUP,
81 TARGET_SIGINT,
82 TARGET_SIGQUIT,
83 TARGET_SIGILL,
84 TARGET_SIGTRAP,
85 TARGET_SIGABRT,
86 -1, /* SIGEMT */
87 TARGET_SIGFPE,
88 TARGET_SIGKILL,
89 TARGET_SIGBUS,
90 TARGET_SIGSEGV,
91 TARGET_SIGSYS,
92 TARGET_SIGPIPE,
93 TARGET_SIGALRM,
94 TARGET_SIGTERM,
95 TARGET_SIGURG,
96 TARGET_SIGSTOP,
97 TARGET_SIGTSTP,
98 TARGET_SIGCONT,
99 TARGET_SIGCHLD,
100 TARGET_SIGTTIN,
101 TARGET_SIGTTOU,
102 TARGET_SIGIO,
103 TARGET_SIGXCPU,
104 TARGET_SIGXFSZ,
105 TARGET_SIGVTALRM,
106 TARGET_SIGPROF,
107 TARGET_SIGWINCH,
108 -1, /* SIGLOST */
109 TARGET_SIGUSR1,
110 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000111#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000112 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000113#else
114 -1,
115#endif
aurel32ca587a82008-12-18 22:44:13 +0000116 -1, /* SIGPOLL */
117 -1,
118 -1,
119 -1,
120 -1,
121 -1,
122 -1,
123 -1,
124 -1,
125 -1,
126 -1,
127 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000128#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000129 __SIGRTMIN + 1,
130 __SIGRTMIN + 2,
131 __SIGRTMIN + 3,
132 __SIGRTMIN + 4,
133 __SIGRTMIN + 5,
134 __SIGRTMIN + 6,
135 __SIGRTMIN + 7,
136 __SIGRTMIN + 8,
137 __SIGRTMIN + 9,
138 __SIGRTMIN + 10,
139 __SIGRTMIN + 11,
140 __SIGRTMIN + 12,
141 __SIGRTMIN + 13,
142 __SIGRTMIN + 14,
143 __SIGRTMIN + 15,
144 __SIGRTMIN + 16,
145 __SIGRTMIN + 17,
146 __SIGRTMIN + 18,
147 __SIGRTMIN + 19,
148 __SIGRTMIN + 20,
149 __SIGRTMIN + 21,
150 __SIGRTMIN + 22,
151 __SIGRTMIN + 23,
152 __SIGRTMIN + 24,
153 __SIGRTMIN + 25,
154 __SIGRTMIN + 26,
155 __SIGRTMIN + 27,
156 __SIGRTMIN + 28,
157 __SIGRTMIN + 29,
158 __SIGRTMIN + 30,
159 __SIGRTMIN + 31,
160 -1, /* SIGCANCEL */
161 __SIGRTMIN,
162 __SIGRTMIN + 32,
163 __SIGRTMIN + 33,
164 __SIGRTMIN + 34,
165 __SIGRTMIN + 35,
166 __SIGRTMIN + 36,
167 __SIGRTMIN + 37,
168 __SIGRTMIN + 38,
169 __SIGRTMIN + 39,
170 __SIGRTMIN + 40,
171 __SIGRTMIN + 41,
172 __SIGRTMIN + 42,
173 __SIGRTMIN + 43,
174 __SIGRTMIN + 44,
175 __SIGRTMIN + 45,
176 __SIGRTMIN + 46,
177 __SIGRTMIN + 47,
178 __SIGRTMIN + 48,
179 __SIGRTMIN + 49,
180 __SIGRTMIN + 50,
181 __SIGRTMIN + 51,
182 __SIGRTMIN + 52,
183 __SIGRTMIN + 53,
184 __SIGRTMIN + 54,
185 __SIGRTMIN + 55,
186 __SIGRTMIN + 56,
187 __SIGRTMIN + 57,
188 __SIGRTMIN + 58,
189 __SIGRTMIN + 59,
190 __SIGRTMIN + 60,
191 __SIGRTMIN + 61,
192 __SIGRTMIN + 62,
193 __SIGRTMIN + 63,
194 __SIGRTMIN + 64,
195 __SIGRTMIN + 65,
196 __SIGRTMIN + 66,
197 __SIGRTMIN + 67,
198 __SIGRTMIN + 68,
199 __SIGRTMIN + 69,
200 __SIGRTMIN + 70,
201 __SIGRTMIN + 71,
202 __SIGRTMIN + 72,
203 __SIGRTMIN + 73,
204 __SIGRTMIN + 74,
205 __SIGRTMIN + 75,
206 __SIGRTMIN + 76,
207 __SIGRTMIN + 77,
208 __SIGRTMIN + 78,
209 __SIGRTMIN + 79,
210 __SIGRTMIN + 80,
211 __SIGRTMIN + 81,
212 __SIGRTMIN + 82,
213 __SIGRTMIN + 83,
214 __SIGRTMIN + 84,
215 __SIGRTMIN + 85,
216 __SIGRTMIN + 86,
217 __SIGRTMIN + 87,
218 __SIGRTMIN + 88,
219 __SIGRTMIN + 89,
220 __SIGRTMIN + 90,
221 __SIGRTMIN + 91,
222 __SIGRTMIN + 92,
223 __SIGRTMIN + 93,
224 __SIGRTMIN + 94,
225 __SIGRTMIN + 95,
226 -1, /* SIGINFO */
227 -1, /* UNKNOWN */
228 -1, /* DEFAULT */
229 -1,
230 -1,
231 -1,
232 -1,
233 -1,
234 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000235#endif
aurel32ca587a82008-12-18 22:44:13 +0000236};
bellard8f447cc2006-06-14 15:21:14 +0000237#else
aurel32ca587a82008-12-18 22:44:13 +0000238/* In system mode we only need SIGINT and SIGTRAP; other signals
239 are not yet supported. */
240
241enum {
242 TARGET_SIGINT = 2,
243 TARGET_SIGTRAP = 5
244};
245
246static int gdb_signal_table[] = {
247 -1,
248 -1,
249 TARGET_SIGINT,
250 -1,
251 -1,
252 TARGET_SIGTRAP
253};
bellard8f447cc2006-06-14 15:21:14 +0000254#endif
bellardb4608c02003-06-27 17:34:32 +0000255
aurel32ca587a82008-12-18 22:44:13 +0000256#ifdef CONFIG_USER_ONLY
257static int target_signal_to_gdb (int sig)
258{
259 int i;
260 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
261 if (gdb_signal_table[i] == sig)
262 return i;
263 return GDB_SIGNAL_UNKNOWN;
264}
265#endif
266
267static int gdb_signal_to_target (int sig)
268{
269 if (sig < ARRAY_SIZE (gdb_signal_table))
270 return gdb_signal_table[sig];
271 else
272 return -1;
273}
274
Alex Bennée118e2262017-07-12 11:52:13 +0100275/* #define DEBUG_GDB */
276
277#ifdef DEBUG_GDB
278# define DEBUG_GDB_GATE 1
279#else
280# define DEBUG_GDB_GATE 0
281#endif
282
283#define gdb_debug(fmt, ...) do { \
284 if (DEBUG_GDB_GATE) { \
285 fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \
286 } \
287} while (0)
288
bellardb4608c02003-06-27 17:34:32 +0000289
pbrook56aebc82008-10-11 17:55:29 +0000290typedef struct GDBRegisterState {
291 int base_reg;
292 int num_regs;
293 gdb_reg_cb get_reg;
294 gdb_reg_cb set_reg;
295 const char *xml;
296 struct GDBRegisterState *next;
297} GDBRegisterState;
298
bellard858693c2004-03-31 18:52:07 +0000299enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000300 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000301 RS_IDLE,
302 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400303 RS_GETLINE_ESC,
304 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000305 RS_CHKSUM1,
306 RS_CHKSUM2,
307};
bellard858693c2004-03-31 18:52:07 +0000308typedef struct GDBState {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200309 CPUState *c_cpu; /* current CPU for step/continue ops */
310 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200311 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000312 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000313 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000314 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400315 int line_sum; /* running checksum */
316 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000317 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000318 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000319 int signal;
bellard41625032005-04-24 10:07:11 +0000320#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000321 int fd;
bellard41625032005-04-24 10:07:11 +0000322 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000323#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300324 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300325 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000326#endif
Meador Ingecdb432b2012-03-15 17:49:45 +0000327 char syscall_buf[256];
328 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000329} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000330
edgar_igl60897d32008-05-09 08:25:14 +0000331/* By default use no IRQs and no timers while single stepping so as to
332 * make single stepping like an ICE HW step.
333 */
334static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
335
aliguori880a7572008-11-18 20:30:24 +0000336static GDBState *gdbserver_state;
337
Andreas Färber5b50e792013-06-29 04:18:45 +0200338bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000339
bellard1fddef42005-04-17 19:16:13 +0000340#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000341/* XXX: This is not thread safe. Do we care? */
342static int gdbserver_fd = -1;
343
bellard858693c2004-03-31 18:52:07 +0000344static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000345{
346 uint8_t ch;
347 int ret;
348
349 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000350 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000351 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000352 if (errno == ECONNRESET)
353 s->fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200354 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000355 return -1;
356 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000357 close(s->fd);
358 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000359 return -1;
360 } else {
361 break;
362 }
363 }
364 return ch;
365}
pbrook4046d912007-01-28 01:53:16 +0000366#endif
bellardb4608c02003-06-27 17:34:32 +0000367
blueswir1654efcf2009-04-18 07:29:59 +0000368static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000369 GDB_SYS_UNKNOWN,
370 GDB_SYS_ENABLED,
371 GDB_SYS_DISABLED,
372} gdb_syscall_mode;
373
Liviu Ionescua38bb072014-12-11 12:07:48 +0000374/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000375int use_gdb_syscalls(void)
376{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100377 SemihostingTarget target = semihosting_get_target();
378 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000379 /* -semihosting-config target=native */
380 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100381 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000382 /* -semihosting-config target=gdb */
383 return true;
384 }
385
386 /* -semihosting-config target=auto */
387 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000388 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000389 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
390 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000391 }
392 return gdb_syscall_mode == GDB_SYS_ENABLED;
393}
394
edgar_iglba70a622008-03-14 06:10:42 +0000395/* Resume execution. */
396static inline void gdb_continue(GDBState *s)
397{
398#ifdef CONFIG_USER_ONLY
399 s->running_state = 1;
400#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200401 if (!runstate_needs_reset()) {
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200402 vm_start();
403 }
edgar_iglba70a622008-03-14 06:10:42 +0000404#endif
405}
406
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100407/*
408 * Resume execution, per CPU actions. For user-mode emulation it's
409 * equivalent to gdb_continue.
410 */
411static int gdb_continue_partial(GDBState *s, char *newstates)
412{
413 CPUState *cpu;
414 int res = 0;
415#ifdef CONFIG_USER_ONLY
416 /*
417 * This is not exactly accurate, but it's an improvement compared to the
418 * previous situation, where only one CPU would be single-stepped.
419 */
420 CPU_FOREACH(cpu) {
421 if (newstates[cpu->cpu_index] == 's') {
422 cpu_single_step(cpu, sstep_flags);
423 }
424 }
425 s->running_state = 1;
426#else
427 int flag = 0;
428
429 if (!runstate_needs_reset()) {
430 if (vm_prepare_start()) {
431 return 0;
432 }
433
434 CPU_FOREACH(cpu) {
435 switch (newstates[cpu->cpu_index]) {
436 case 0:
437 case 1:
438 break; /* nothing to do here */
439 case 's':
440 cpu_single_step(cpu, sstep_flags);
441 cpu_resume(cpu);
442 flag = 1;
443 break;
444 case 'c':
445 cpu_resume(cpu);
446 flag = 1;
447 break;
448 default:
449 res = -1;
450 break;
451 }
452 }
453 }
454 if (flag) {
455 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
456 }
457#endif
458 return res;
459}
460
bellard858693c2004-03-31 18:52:07 +0000461static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000462{
pbrook4046d912007-01-28 01:53:16 +0000463#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000464 int ret;
465
466 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000467 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000468 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200469 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000470 return;
471 } else {
472 buf += ret;
473 len -= ret;
474 }
475 }
pbrook4046d912007-01-28 01:53:16 +0000476#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100477 /* XXX this blocks entire thread. Rewrite to use
478 * qemu_chr_fe_write and background I/O callbacks */
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300479 qemu_chr_fe_write_all(&s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000480#endif
bellardb4608c02003-06-27 17:34:32 +0000481}
482
483static inline int fromhex(int v)
484{
485 if (v >= '0' && v <= '9')
486 return v - '0';
487 else if (v >= 'A' && v <= 'F')
488 return v - 'A' + 10;
489 else if (v >= 'a' && v <= 'f')
490 return v - 'a' + 10;
491 else
492 return 0;
493}
494
495static inline int tohex(int v)
496{
497 if (v < 10)
498 return v + '0';
499 else
500 return v - 10 + 'a';
501}
502
503static void memtohex(char *buf, const uint8_t *mem, int len)
504{
505 int i, c;
506 char *q;
507 q = buf;
508 for(i = 0; i < len; i++) {
509 c = mem[i];
510 *q++ = tohex(c >> 4);
511 *q++ = tohex(c & 0xf);
512 }
513 *q = '\0';
514}
515
516static void hextomem(uint8_t *mem, const char *buf, int len)
517{
518 int i;
519
520 for(i = 0; i < len; i++) {
521 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
522 buf += 2;
523 }
524}
525
bellardb4608c02003-06-27 17:34:32 +0000526/* return -1 if error, 0 if OK */
pbrook56aebc82008-10-11 17:55:29 +0000527static int put_packet_binary(GDBState *s, const char *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000528{
pbrook56aebc82008-10-11 17:55:29 +0000529 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000530 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000531
bellardb4608c02003-06-27 17:34:32 +0000532 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000533 p = s->last_packet;
534 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000535 memcpy(p, buf, len);
536 p += len;
bellardb4608c02003-06-27 17:34:32 +0000537 csum = 0;
538 for(i = 0; i < len; i++) {
539 csum += buf[i];
540 }
pbrook4046d912007-01-28 01:53:16 +0000541 *(p++) = '#';
542 *(p++) = tohex((csum >> 4) & 0xf);
543 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000544
pbrook4046d912007-01-28 01:53:16 +0000545 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000546 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000547
pbrook4046d912007-01-28 01:53:16 +0000548#ifdef CONFIG_USER_ONLY
549 i = get_char(s);
550 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000551 return -1;
pbrook4046d912007-01-28 01:53:16 +0000552 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000553 break;
pbrook4046d912007-01-28 01:53:16 +0000554#else
555 break;
556#endif
bellardb4608c02003-06-27 17:34:32 +0000557 }
558 return 0;
559}
560
pbrook56aebc82008-10-11 17:55:29 +0000561/* return -1 if error, 0 if OK */
562static int put_packet(GDBState *s, const char *buf)
563{
Alex Bennée118e2262017-07-12 11:52:13 +0100564 gdb_debug("reply='%s'\n", buf);
pbrook56aebc82008-10-11 17:55:29 +0000565
566 return put_packet_binary(s, buf, strlen(buf));
567}
568
pbrook56aebc82008-10-11 17:55:29 +0000569/* Encode data using the encoding for 'x' packets. */
570static int memtox(char *buf, const char *mem, int len)
571{
572 char *p = buf;
573 char c;
574
575 while (len--) {
576 c = *(mem++);
577 switch (c) {
578 case '#': case '$': case '*': case '}':
579 *(p++) = '}';
580 *(p++) = c ^ 0x20;
581 break;
582 default:
583 *(p++) = c;
584 break;
585 }
586 }
587 return p - buf;
588}
589
Andreas Färber5b24c642013-07-07 15:08:22 +0200590static const char *get_feature_xml(const char *p, const char **newp,
591 CPUClass *cc)
pbrook56aebc82008-10-11 17:55:29 +0000592{
pbrook56aebc82008-10-11 17:55:29 +0000593 size_t len;
594 int i;
595 const char *name;
596 static char target_xml[1024];
597
598 len = 0;
599 while (p[len] && p[len] != ':')
600 len++;
601 *newp = p + len;
602
603 name = NULL;
604 if (strncmp(p, "target.xml", len) == 0) {
605 /* Generate the XML description for this CPU. */
606 if (!target_xml[0]) {
607 GDBRegisterState *r;
Andreas Färbereac8b352013-06-28 21:11:37 +0200608 CPUState *cpu = first_cpu;
pbrook56aebc82008-10-11 17:55:29 +0000609
David Hildenbrandb3820e62015-12-03 13:14:41 +0100610 pstrcat(target_xml, sizeof(target_xml),
611 "<?xml version=\"1.0\"?>"
612 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
613 "<target>");
614 if (cc->gdb_arch_name) {
615 gchar *arch = cc->gdb_arch_name(cpu);
616 pstrcat(target_xml, sizeof(target_xml), "<architecture>");
617 pstrcat(target_xml, sizeof(target_xml), arch);
618 pstrcat(target_xml, sizeof(target_xml), "</architecture>");
619 g_free(arch);
620 }
621 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
622 pstrcat(target_xml, sizeof(target_xml), cc->gdb_core_xml_file);
623 pstrcat(target_xml, sizeof(target_xml), "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200624 for (r = cpu->gdb_regs; r; r = r->next) {
blueswir12dc766d2009-04-13 16:06:19 +0000625 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
626 pstrcat(target_xml, sizeof(target_xml), r->xml);
627 pstrcat(target_xml, sizeof(target_xml), "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000628 }
blueswir12dc766d2009-04-13 16:06:19 +0000629 pstrcat(target_xml, sizeof(target_xml), "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000630 }
631 return target_xml;
632 }
633 for (i = 0; ; i++) {
634 name = xml_builtin[i][0];
635 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
636 break;
637 }
638 return name ? xml_builtin[i][1] : NULL;
639}
pbrook56aebc82008-10-11 17:55:29 +0000640
Andreas Färber385b9f02013-06-27 18:25:36 +0200641static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000642{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200643 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200644 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000645 GDBRegisterState *r;
646
Andreas Färbera0e372f2013-06-28 23:18:47 +0200647 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200648 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200649 }
pbrook56aebc82008-10-11 17:55:29 +0000650
Andreas Färbereac8b352013-06-28 21:11:37 +0200651 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000652 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
653 return r->get_reg(env, mem_buf, reg - r->base_reg);
654 }
655 }
656 return 0;
657}
658
Andreas Färber385b9f02013-06-27 18:25:36 +0200659static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000660{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200661 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200662 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000663 GDBRegisterState *r;
664
Andreas Färbera0e372f2013-06-28 23:18:47 +0200665 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200666 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200667 }
pbrook56aebc82008-10-11 17:55:29 +0000668
Andreas Färbereac8b352013-06-28 21:11:37 +0200669 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000670 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
671 return r->set_reg(env, mem_buf, reg - r->base_reg);
672 }
673 }
674 return 0;
675}
676
677/* Register a supplemental set of CPU registers. If g_pos is nonzero it
678 specifies the first register number and these registers are included in
679 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
680 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
681 */
682
Andreas Färber22169d42013-06-28 21:27:39 +0200683void gdb_register_coprocessor(CPUState *cpu,
684 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
685 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000686{
687 GDBRegisterState *s;
688 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000689
Andreas Färbereac8b352013-06-28 21:11:37 +0200690 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000691 while (*p) {
692 /* Check for duplicates. */
693 if (strcmp((*p)->xml, xml) == 0)
694 return;
695 p = &(*p)->next;
696 }
Stefan Weil9643c252011-10-18 22:25:38 +0200697
698 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200699 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200700 s->num_regs = num_regs;
701 s->get_reg = get_reg;
702 s->set_reg = set_reg;
703 s->xml = xml;
704
pbrook56aebc82008-10-11 17:55:29 +0000705 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200706 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000707 *p = s;
708 if (g_pos) {
709 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800710 error_report("Error: Bad gdb register numbering for '%s', "
711 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200712 } else {
713 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000714 }
715 }
716}
717
aliguoria1d1bb32008-11-18 20:07:32 +0000718#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100719/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
720static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
721{
722 static const int xlat[] = {
723 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
724 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
725 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
726 };
727
728 CPUClass *cc = CPU_GET_CLASS(cpu);
729 int cputype = xlat[gdbtype];
730
731 if (cc->gdb_stop_before_watchpoint) {
732 cputype |= BP_STOP_BEFORE_ACCESS;
733 }
734 return cputype;
735}
aliguoria1d1bb32008-11-18 20:07:32 +0000736#endif
737
aliguori880a7572008-11-18 20:30:24 +0000738static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000739{
Andreas Färber182735e2013-05-29 22:29:20 +0200740 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000741 int err = 0;
742
Andreas Färber62278812013-06-27 17:12:06 +0200743 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200744 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200745 }
aliguorie22a25c2009-03-12 20:12:48 +0000746
aliguoria1d1bb32008-11-18 20:07:32 +0000747 switch (type) {
748 case GDB_BREAKPOINT_SW:
749 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200750 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200751 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
752 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000753 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200754 }
aliguori880a7572008-11-18 20:30:24 +0000755 }
756 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000757#ifndef CONFIG_USER_ONLY
758 case GDB_WATCHPOINT_WRITE:
759 case GDB_WATCHPOINT_READ:
760 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200761 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100762 err = cpu_watchpoint_insert(cpu, addr, len,
763 xlat_gdb_type(cpu, type), NULL);
764 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000765 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +0100766 }
aliguori880a7572008-11-18 20:30:24 +0000767 }
768 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000769#endif
770 default:
771 return -ENOSYS;
772 }
773}
774
aliguori880a7572008-11-18 20:30:24 +0000775static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000776{
Andreas Färber182735e2013-05-29 22:29:20 +0200777 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000778 int err = 0;
779
Andreas Färber62278812013-06-27 17:12:06 +0200780 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200781 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200782 }
aliguorie22a25c2009-03-12 20:12:48 +0000783
aliguoria1d1bb32008-11-18 20:07:32 +0000784 switch (type) {
785 case GDB_BREAKPOINT_SW:
786 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200787 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200788 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
789 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000790 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200791 }
aliguori880a7572008-11-18 20:30:24 +0000792 }
793 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000794#ifndef CONFIG_USER_ONLY
795 case GDB_WATCHPOINT_WRITE:
796 case GDB_WATCHPOINT_READ:
797 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200798 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100799 err = cpu_watchpoint_remove(cpu, addr, len,
800 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +0000801 if (err)
802 break;
803 }
804 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000805#endif
806 default:
807 return -ENOSYS;
808 }
809}
810
aliguori880a7572008-11-18 20:30:24 +0000811static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +0000812{
Andreas Färber182735e2013-05-29 22:29:20 +0200813 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000814
aliguorie22a25c2009-03-12 20:12:48 +0000815 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200816 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +0000817 return;
818 }
819
Andreas Färberbdc44642013-06-24 23:50:24 +0200820 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200821 cpu_breakpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000822#ifndef CONFIG_USER_ONLY
Andreas Färber75a34032013-09-02 16:57:02 +0200823 cpu_watchpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000824#endif
aliguori880a7572008-11-18 20:30:24 +0000825 }
aliguoria1d1bb32008-11-18 20:07:32 +0000826}
827
aurel32fab9d282009-04-08 21:29:37 +0000828static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
829{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200830 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +0200831
832 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -0700833 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +0000834}
835
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200836static CPUState *find_cpu(uint32_t thread_id)
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700837{
Andreas Färber0d342822012-12-17 07:12:13 +0100838 CPUState *cpu;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700839
Andreas Färberbdc44642013-06-24 23:50:24 +0200840 CPU_FOREACH(cpu) {
Andreas Färberaa48dd92013-07-09 20:50:52 +0200841 if (cpu_index(cpu) == thread_id) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200842 return cpu;
Andreas Färberaa48dd92013-07-09 20:50:52 +0200843 }
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700844 }
Andreas Färberaa48dd92013-07-09 20:50:52 +0200845
846 return NULL;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700847}
848
Jan Kiszka4dabe742015-02-07 09:38:43 +0100849static int is_query_packet(const char *p, const char *query, char separator)
850{
851 unsigned int query_len = strlen(query);
852
853 return strncmp(p, query, query_len) == 0 &&
854 (p[query_len] == '\0' || p[query_len] == separator);
855}
856
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100857/**
858 * gdb_handle_vcont - Parses and handles a vCont packet.
859 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
860 * a format error, 0 on success.
861 */
862static int gdb_handle_vcont(GDBState *s, const char *p)
863{
864 int res, idx, signal = 0;
865 char cur_action;
866 char *newstates;
867 unsigned long tmp;
868 CPUState *cpu;
869#ifdef CONFIG_USER_ONLY
870 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
871
872 CPU_FOREACH(cpu) {
873 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
874 }
875#endif
876 /* uninitialised CPUs stay 0 */
877 newstates = g_new0(char, max_cpus);
878
879 /* mark valid CPUs with 1 */
880 CPU_FOREACH(cpu) {
881 newstates[cpu->cpu_index] = 1;
882 }
883
884 /*
885 * res keeps track of what error we are returning, with -ENOTSUP meaning
886 * that the command is unknown or unsupported, thus returning an empty
887 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
888 * or incorrect parameters passed.
889 */
890 res = 0;
891 while (*p) {
892 if (*p++ != ';') {
893 res = -ENOTSUP;
894 goto out;
895 }
896
897 cur_action = *p++;
898 if (cur_action == 'C' || cur_action == 'S') {
899 cur_action = tolower(cur_action);
900 res = qemu_strtoul(p + 1, &p, 16, &tmp);
901 if (res) {
902 goto out;
903 }
904 signal = gdb_signal_to_target(tmp);
905 } else if (cur_action != 'c' && cur_action != 's') {
906 /* unknown/invalid/unsupported command */
907 res = -ENOTSUP;
908 goto out;
909 }
910 /* thread specification. special values: (none), -1 = all; 0 = any */
911 if ((p[0] == ':' && p[1] == '-' && p[2] == '1') || (p[0] != ':')) {
912 if (*p == ':') {
913 p += 3;
914 }
915 for (idx = 0; idx < max_cpus; idx++) {
916 if (newstates[idx] == 1) {
917 newstates[idx] = cur_action;
918 }
919 }
920 } else if (*p == ':') {
921 p++;
922 res = qemu_strtoul(p, &p, 16, &tmp);
923 if (res) {
924 goto out;
925 }
926 idx = tmp;
927 /* 0 means any thread, so we pick the first valid CPU */
928 if (!idx) {
929 idx = cpu_index(first_cpu);
930 }
931
932 /*
933 * If we are in user mode, the thread specified is actually a
934 * thread id, and not an index. We need to find the actual
935 * CPU first, and only then we can use its index.
936 */
937 cpu = find_cpu(idx);
938 /* invalid CPU/thread specified */
939 if (!idx || !cpu) {
940 res = -EINVAL;
941 goto out;
942 }
943 /* only use if no previous match occourred */
944 if (newstates[cpu->cpu_index] == 1) {
945 newstates[cpu->cpu_index] = cur_action;
946 }
947 }
948 }
949 s->signal = signal;
950 gdb_continue_partial(s, newstates);
951
952out:
953 g_free(newstates);
954
955 return res;
956}
957
aliguori880a7572008-11-18 20:30:24 +0000958static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +0000959{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200960 CPUState *cpu;
Andreas Färber5b24c642013-07-07 15:08:22 +0200961 CPUClass *cc;
bellardb4608c02003-06-27 17:34:32 +0000962 const char *p;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700963 uint32_t thread;
964 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +0000965 char buf[MAX_PACKET_LENGTH];
966 uint8_t mem_buf[MAX_PACKET_LENGTH];
967 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +0000968 target_ulong addr, len;
ths3b46e622007-09-17 08:09:54 +0000969
Alex Bennée118e2262017-07-12 11:52:13 +0100970
971 gdb_debug("command='%s'\n", line_buf);
972
bellard858693c2004-03-31 18:52:07 +0000973 p = line_buf;
974 ch = *p++;
975 switch(ch) {
976 case '?':
bellard1fddef42005-04-17 19:16:13 +0000977 /* TODO: Make this return the correct value for user-mode. */
aurel32ca587a82008-12-18 22:44:13 +0000978 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200979 cpu_index(s->c_cpu));
bellard858693c2004-03-31 18:52:07 +0000980 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +0000981 /* Remove all the breakpoints when this query is issued,
982 * because gdb is doing and initial connect and the state
983 * should be cleaned up.
984 */
aliguori880a7572008-11-18 20:30:24 +0000985 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +0000986 break;
987 case 'c':
988 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +0000989 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +0000990 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +0000991 }
aurel32ca587a82008-12-18 22:44:13 +0000992 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +0000993 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +0000994 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +0000995 case 'C':
aurel32ca587a82008-12-18 22:44:13 +0000996 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
997 if (s->signal == -1)
998 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +0000999 gdb_continue(s);
1000 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001001 case 'v':
1002 if (strncmp(p, "Cont", 4) == 0) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001003 p += 4;
1004 if (*p == '?') {
1005 put_packet(s, "vCont;c;C;s;S");
1006 break;
1007 }
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001008
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001009 res = gdb_handle_vcont(s, p);
1010
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001011 if (res) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001012 if ((res == -EINVAL) || (res == -ERANGE)) {
1013 put_packet(s, "E22");
1014 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001015 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001016 goto unknown_command;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001017 }
1018 break;
1019 } else {
1020 goto unknown_command;
1021 }
edgar_igl7d03f822008-05-17 18:58:29 +00001022 case 'k':
1023 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001024 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00001025 exit(0);
1026 case 'D':
1027 /* Detach packet */
aliguori880a7572008-11-18 20:30:24 +00001028 gdb_breakpoint_remove_all();
Daniel Gutson7ea06da2010-02-26 14:13:50 -03001029 gdb_syscall_mode = GDB_SYS_DISABLED;
edgar_igl7d03f822008-05-17 18:58:29 +00001030 gdb_continue(s);
1031 put_packet(s, "OK");
1032 break;
bellard858693c2004-03-31 18:52:07 +00001033 case 's':
1034 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +00001035 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001036 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001037 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001038 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +00001039 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +00001040 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +00001041 case 'F':
1042 {
1043 target_ulong ret;
1044 target_ulong err;
1045
1046 ret = strtoull(p, (char **)&p, 16);
1047 if (*p == ',') {
1048 p++;
1049 err = strtoull(p, (char **)&p, 16);
1050 } else {
1051 err = 0;
1052 }
1053 if (*p == ',')
1054 p++;
1055 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001056 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001057 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +00001058 s->current_syscall_cb = NULL;
1059 }
pbrooka2d1eba2007-01-28 03:10:55 +00001060 if (type == 'C') {
1061 put_packet(s, "T02");
1062 } else {
edgar_iglba70a622008-03-14 06:10:42 +00001063 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +00001064 }
1065 }
1066 break;
bellard858693c2004-03-31 18:52:07 +00001067 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001068 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001069 len = 0;
Andreas Färber35143f02013-08-12 18:09:47 +02001070 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001071 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +00001072 len += reg_size;
1073 }
1074 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +00001075 put_packet(s, buf);
1076 break;
1077 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001078 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001079 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +00001080 len = strlen(p) / 2;
1081 hextomem((uint8_t *)registers, p, len);
Andreas Färber35143f02013-08-12 18:09:47 +02001082 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001083 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001084 len -= reg_size;
1085 registers += reg_size;
1086 }
bellard858693c2004-03-31 18:52:07 +00001087 put_packet(s, "OK");
1088 break;
1089 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001090 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001091 if (*p == ',')
1092 p++;
bellard9d9754a2006-06-25 15:32:37 +00001093 len = strtoull(p, NULL, 16);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001094
1095 /* memtohex() doubles the required space */
1096 if (len > MAX_PACKET_LENGTH / 2) {
1097 put_packet (s, "E22");
1098 break;
1099 }
1100
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001101 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001102 put_packet (s, "E14");
1103 } else {
1104 memtohex(buf, mem_buf, len);
1105 put_packet(s, buf);
1106 }
bellard858693c2004-03-31 18:52:07 +00001107 break;
1108 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001109 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001110 if (*p == ',')
1111 p++;
bellard9d9754a2006-06-25 15:32:37 +00001112 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001113 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001114 p++;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001115
1116 /* hextomem() reads 2*len bytes */
1117 if (len > strlen(p) / 2) {
1118 put_packet (s, "E22");
1119 break;
1120 }
bellard858693c2004-03-31 18:52:07 +00001121 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001122 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +02001123 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +00001124 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001125 } else {
bellard858693c2004-03-31 18:52:07 +00001126 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001127 }
bellard858693c2004-03-31 18:52:07 +00001128 break;
pbrook56aebc82008-10-11 17:55:29 +00001129 case 'p':
1130 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1131 This works, but can be very slow. Anything new enough to
1132 understand XML also knows how to use this properly. */
1133 if (!gdb_has_xml)
1134 goto unknown_command;
1135 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001136 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001137 if (reg_size) {
1138 memtohex(buf, mem_buf, reg_size);
1139 put_packet(s, buf);
1140 } else {
1141 put_packet(s, "E14");
1142 }
1143 break;
1144 case 'P':
1145 if (!gdb_has_xml)
1146 goto unknown_command;
1147 addr = strtoull(p, (char **)&p, 16);
1148 if (*p == '=')
1149 p++;
1150 reg_size = strlen(p) / 2;
1151 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001152 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001153 put_packet(s, "OK");
1154 break;
bellard858693c2004-03-31 18:52:07 +00001155 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001156 case 'z':
1157 type = strtoul(p, (char **)&p, 16);
1158 if (*p == ',')
1159 p++;
bellard9d9754a2006-06-25 15:32:37 +00001160 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001161 if (*p == ',')
1162 p++;
bellard9d9754a2006-06-25 15:32:37 +00001163 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001164 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001165 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001166 else
aliguori880a7572008-11-18 20:30:24 +00001167 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001168 if (res >= 0)
1169 put_packet(s, "OK");
1170 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001171 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001172 else
1173 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001174 break;
aliguori880a7572008-11-18 20:30:24 +00001175 case 'H':
1176 type = *p++;
1177 thread = strtoull(p, (char **)&p, 16);
1178 if (thread == -1 || thread == 0) {
1179 put_packet(s, "OK");
1180 break;
1181 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001182 cpu = find_cpu(thread);
1183 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001184 put_packet(s, "E22");
1185 break;
1186 }
1187 switch (type) {
1188 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001189 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001190 put_packet(s, "OK");
1191 break;
1192 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001193 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001194 put_packet(s, "OK");
1195 break;
1196 default:
1197 put_packet(s, "E22");
1198 break;
1199 }
1200 break;
1201 case 'T':
1202 thread = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001203 cpu = find_cpu(thread);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001204
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001205 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001206 put_packet(s, "OK");
1207 } else {
aliguori880a7572008-11-18 20:30:24 +00001208 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001209 }
aliguori880a7572008-11-18 20:30:24 +00001210 break;
pbrook978efd62006-06-17 18:30:42 +00001211 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001212 case 'Q':
1213 /* parse any 'q' packets here */
1214 if (!strcmp(p,"qemu.sstepbits")) {
1215 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001216 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1217 SSTEP_ENABLE,
1218 SSTEP_NOIRQ,
1219 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001220 put_packet(s, buf);
1221 break;
Jan Kiszka4dabe742015-02-07 09:38:43 +01001222 } else if (is_query_packet(p, "qemu.sstep", '=')) {
edgar_igl60897d32008-05-09 08:25:14 +00001223 /* Display or change the sstep_flags */
1224 p += 10;
1225 if (*p != '=') {
1226 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001227 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001228 put_packet(s, buf);
1229 break;
1230 }
1231 p++;
1232 type = strtoul(p, (char **)&p, 16);
1233 sstep_flags = type;
1234 put_packet(s, "OK");
1235 break;
aliguori880a7572008-11-18 20:30:24 +00001236 } else if (strcmp(p,"C") == 0) {
1237 /* "Current thread" remains vague in the spec, so always return
1238 * the first CPU (gdb returns the first thread). */
1239 put_packet(s, "QC1");
1240 break;
1241 } else if (strcmp(p,"fThreadInfo") == 0) {
Andreas Färber52f34622013-06-27 13:44:40 +02001242 s->query_cpu = first_cpu;
aliguori880a7572008-11-18 20:30:24 +00001243 goto report_cpuinfo;
1244 } else if (strcmp(p,"sThreadInfo") == 0) {
1245 report_cpuinfo:
1246 if (s->query_cpu) {
Andreas Färber52f34622013-06-27 13:44:40 +02001247 snprintf(buf, sizeof(buf), "m%x", cpu_index(s->query_cpu));
aliguori880a7572008-11-18 20:30:24 +00001248 put_packet(s, buf);
Andreas Färberbdc44642013-06-24 23:50:24 +02001249 s->query_cpu = CPU_NEXT(s->query_cpu);
aliguori880a7572008-11-18 20:30:24 +00001250 } else
1251 put_packet(s, "l");
1252 break;
1253 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1254 thread = strtoull(p+16, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001255 cpu = find_cpu(thread);
1256 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001257 cpu_synchronize_state(cpu);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001258 /* memtohex() doubles the required space */
1259 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
Andreas Färber55e5c282012-12-17 06:18:02 +01001260 "CPU#%d [%s]", cpu->cpu_index,
Andreas Färber259186a2013-01-17 18:51:17 +01001261 cpu->halted ? "halted " : "running");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001262 memtohex(buf, mem_buf, len);
1263 put_packet(s, buf);
1264 }
aliguori880a7572008-11-18 20:30:24 +00001265 break;
edgar_igl60897d32008-05-09 08:25:14 +00001266 }
blueswir10b8a9882009-03-07 10:51:36 +00001267#ifdef CONFIG_USER_ONLY
Jan Kiszka070949f2015-02-07 09:38:42 +01001268 else if (strcmp(p, "Offsets") == 0) {
Andreas Färber0429a972013-08-26 18:14:44 +02001269 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001270
blueswir1363a37d2008-08-21 17:58:08 +00001271 snprintf(buf, sizeof(buf),
1272 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1273 ";Bss=" TARGET_ABI_FMT_lx,
1274 ts->info->code_offset,
1275 ts->info->data_offset,
1276 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001277 put_packet(s, buf);
1278 break;
1279 }
blueswir10b8a9882009-03-07 10:51:36 +00001280#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001281 else if (strncmp(p, "Rcmd,", 5) == 0) {
1282 int len = strlen(p + 5);
1283
1284 if ((len % 2) != 0) {
1285 put_packet(s, "E01");
1286 break;
1287 }
aliguori8a34a0f2009-03-05 23:01:55 +00001288 len = len / 2;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001289 hextomem(mem_buf, p + 5, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001290 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001291 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001292 put_packet(s, "OK");
1293 break;
1294 }
blueswir10b8a9882009-03-07 10:51:36 +00001295#endif /* !CONFIG_USER_ONLY */
Jan Kiszka4dabe742015-02-07 09:38:43 +01001296 if (is_query_packet(p, "Supported", ':')) {
blueswir15b3715b2008-10-25 11:18:12 +00001297 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
Andreas Färber5b24c642013-07-07 15:08:22 +02001298 cc = CPU_GET_CLASS(first_cpu);
1299 if (cc->gdb_core_xml_file != NULL) {
1300 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1301 }
pbrook56aebc82008-10-11 17:55:29 +00001302 put_packet(s, buf);
1303 break;
1304 }
pbrook56aebc82008-10-11 17:55:29 +00001305 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1306 const char *xml;
1307 target_ulong total_len;
1308
Andreas Färber5b24c642013-07-07 15:08:22 +02001309 cc = CPU_GET_CLASS(first_cpu);
1310 if (cc->gdb_core_xml_file == NULL) {
1311 goto unknown_command;
1312 }
1313
Andreas Färber5b50e792013-06-29 04:18:45 +02001314 gdb_has_xml = true;
pbrook56aebc82008-10-11 17:55:29 +00001315 p += 19;
Andreas Färber5b24c642013-07-07 15:08:22 +02001316 xml = get_feature_xml(p, &p, cc);
pbrook56aebc82008-10-11 17:55:29 +00001317 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001318 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001319 put_packet(s, buf);
1320 break;
1321 }
1322
1323 if (*p == ':')
1324 p++;
1325 addr = strtoul(p, (char **)&p, 16);
1326 if (*p == ',')
1327 p++;
1328 len = strtoul(p, (char **)&p, 16);
1329
1330 total_len = strlen(xml);
1331 if (addr > total_len) {
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 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1337 len = (MAX_PACKET_LENGTH - 5) / 2;
1338 if (len < total_len - addr) {
1339 buf[0] = 'm';
1340 len = memtox(buf + 1, xml + addr, len);
1341 } else {
1342 buf[0] = 'l';
1343 len = memtox(buf + 1, xml + addr, total_len - addr);
1344 }
1345 put_packet_binary(s, buf, len + 1);
1346 break;
1347 }
Jan Kiszkaa3919382015-02-07 09:38:44 +01001348 if (is_query_packet(p, "Attached", ':')) {
1349 put_packet(s, GDB_ATTACHED);
1350 break;
1351 }
pbrook56aebc82008-10-11 17:55:29 +00001352 /* Unrecognised 'q' command. */
1353 goto unknown_command;
1354
bellard858693c2004-03-31 18:52:07 +00001355 default:
pbrook56aebc82008-10-11 17:55:29 +00001356 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001357 /* put empty packet */
1358 buf[0] = '\0';
1359 put_packet(s, buf);
1360 break;
1361 }
1362 return RS_IDLE;
1363}
1364
Andreas Färber64f6b342013-05-27 02:06:09 +02001365void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001366{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001367 gdbserver_state->c_cpu = cpu;
1368 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001369}
1370
bellard1fddef42005-04-17 19:16:13 +00001371#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001372static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001373{
aliguori880a7572008-11-18 20:30:24 +00001374 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001375 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001376 char buf[256];
aliguorid6fc1b32008-11-18 19:55:44 +00001377 const char *type;
bellard858693c2004-03-31 18:52:07 +00001378 int ret;
1379
Meador Ingecdb432b2012-03-15 17:49:45 +00001380 if (running || s->state == RS_INACTIVE) {
1381 return;
1382 }
1383 /* Is there a GDB syscall waiting to be sent? */
1384 if (s->current_syscall_cb) {
1385 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001386 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001387 }
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001388 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001389 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02001390 if (cpu->watchpoint_hit) {
1391 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001392 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001393 type = "r";
1394 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001395 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001396 type = "a";
1397 break;
1398 default:
1399 type = "";
1400 break;
1401 }
aliguori880a7572008-11-18 20:30:24 +00001402 snprintf(buf, sizeof(buf),
1403 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
Andreas Färber0d342822012-12-17 07:12:13 +01001404 GDB_SIGNAL_TRAP, cpu_index(cpu), type,
Andreas Färberff4700b2013-08-26 18:23:18 +02001405 (target_ulong)cpu->watchpoint_hit->vaddr);
1406 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001407 goto send_packet;
pbrook6658ffb2007-03-16 23:58:11 +00001408 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001409 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00001410 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001411 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001412 case RUN_STATE_PAUSED:
aliguori9781e042009-01-22 17:15:29 +00001413 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001414 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001415 case RUN_STATE_SHUTDOWN:
Jan Kiszka425189a2011-03-22 11:02:09 +01001416 ret = GDB_SIGNAL_QUIT;
1417 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001418 case RUN_STATE_IO_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001419 ret = GDB_SIGNAL_IO;
1420 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001421 case RUN_STATE_WATCHDOG:
Jan Kiszka425189a2011-03-22 11:02:09 +01001422 ret = GDB_SIGNAL_ALRM;
1423 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001424 case RUN_STATE_INTERNAL_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001425 ret = GDB_SIGNAL_ABRT;
1426 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001427 case RUN_STATE_SAVE_VM:
1428 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001429 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001430 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001431 ret = GDB_SIGNAL_XCPU;
1432 break;
1433 default:
1434 ret = GDB_SIGNAL_UNKNOWN;
1435 break;
bellardbbeb7b52006-04-23 18:42:15 +00001436 }
Jan Kiszka226d0072015-07-24 18:52:31 +02001437 gdb_set_stop_cpu(cpu);
Andreas Färber0d342822012-12-17 07:12:13 +01001438 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_index(cpu));
Jan Kiszka425189a2011-03-22 11:02:09 +01001439
1440send_packet:
bellard858693c2004-03-31 18:52:07 +00001441 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001442
1443 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001444 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001445}
bellard1fddef42005-04-17 19:16:13 +00001446#endif
bellard858693c2004-03-31 18:52:07 +00001447
pbrooka2d1eba2007-01-28 03:10:55 +00001448/* Send a gdb syscall request.
1449 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001450 %x - target_ulong argument printed in hex.
1451 %lx - 64-bit argument printed in hex.
1452 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01001453void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00001454{
pbrooka2d1eba2007-01-28 03:10:55 +00001455 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001456 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001457 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001458 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001459 GDBState *s;
1460
aliguori880a7572008-11-18 20:30:24 +00001461 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001462 if (!s)
1463 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001464 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001465#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001466 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001467#endif
Meador Ingecdb432b2012-03-15 17:49:45 +00001468 p = s->syscall_buf;
1469 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001470 *(p++) = 'F';
1471 while (*fmt) {
1472 if (*fmt == '%') {
1473 fmt++;
1474 switch (*fmt++) {
1475 case 'x':
1476 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001477 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001478 break;
pbrooka87295e2007-05-26 15:09:38 +00001479 case 'l':
1480 if (*(fmt++) != 'x')
1481 goto bad_format;
1482 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001483 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001484 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001485 case 's':
1486 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001487 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001488 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001489 break;
1490 default:
pbrooka87295e2007-05-26 15:09:38 +00001491 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001492 error_report("gdbstub: Bad syscall format string '%s'",
1493 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00001494 break;
1495 }
1496 } else {
1497 *(p++) = *(fmt++);
1498 }
1499 }
pbrook8a93e022007-08-06 13:19:15 +00001500 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001501#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001502 put_packet(s, s->syscall_buf);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001503 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001504#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001505 /* In this case wait to send the syscall packet until notification that
1506 the CPU has stopped. This must be done because if the packet is sent
1507 now the reply from the syscall request could be received while the CPU
1508 is still in the running state, which can cause packets to be dropped
1509 and state transition 'T' packets to be sent while the syscall is still
1510 being processed. */
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001511 qemu_cpu_kick(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00001512#endif
1513}
1514
Peter Maydell19239b32015-09-07 10:39:27 +01001515void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
1516{
1517 va_list va;
1518
1519 va_start(va, fmt);
1520 gdb_do_syscallv(cb, fmt, va);
1521 va_end(va);
1522}
1523
bellard6a00d602005-11-21 23:25:50 +00001524static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00001525{
ths60fe76f2007-12-16 03:02:09 +00001526 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00001527
bellard1fddef42005-04-17 19:16:13 +00001528#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001529 if (s->last_packet_len) {
1530 /* Waiting for a response to the last packet. If we see the start
1531 of a new command then abandon the previous response. */
1532 if (ch == '-') {
Alex Bennée118e2262017-07-12 11:52:13 +01001533 gdb_debug("Got NACK, retransmitting\n");
thsffe8ab82007-12-16 03:16:05 +00001534 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
Alex Bennée118e2262017-07-12 11:52:13 +01001535 } else if (ch == '+') {
1536 gdb_debug("Got ACK\n");
1537 } else {
1538 gdb_debug("Got '%c' when expecting ACK/NACK\n", ch);
pbrook4046d912007-01-28 01:53:16 +00001539 }
Alex Bennée118e2262017-07-12 11:52:13 +01001540
pbrook4046d912007-01-28 01:53:16 +00001541 if (ch == '+' || ch == '$')
1542 s->last_packet_len = 0;
1543 if (ch != '$')
1544 return;
1545 }
Luiz Capitulino13548692011-07-29 15:36:43 -03001546 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00001547 /* when the CPU is running, we cannot do anything except stop
1548 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001549 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00001550 } else
bellard1fddef42005-04-17 19:16:13 +00001551#endif
bellard41625032005-04-24 10:07:11 +00001552 {
bellard858693c2004-03-31 18:52:07 +00001553 switch(s->state) {
1554 case RS_IDLE:
1555 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04001556 /* start of command packet */
bellard858693c2004-03-31 18:52:07 +00001557 s->line_buf_index = 0;
Doug Gale4bf43122017-05-01 12:22:10 -04001558 s->line_sum = 0;
bellard858693c2004-03-31 18:52:07 +00001559 s->state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04001560 } else {
Alex Bennée118e2262017-07-12 11:52:13 +01001561 gdb_debug("received garbage between packets: 0x%x\n", ch);
bellard4c3a88a2003-07-26 12:06:08 +00001562 }
1563 break;
bellard858693c2004-03-31 18:52:07 +00001564 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04001565 if (ch == '}') {
1566 /* start escape sequence */
1567 s->state = RS_GETLINE_ESC;
1568 s->line_sum += ch;
1569 } else if (ch == '*') {
1570 /* start run length encoding sequence */
1571 s->state = RS_GETLINE_RLE;
1572 s->line_sum += ch;
1573 } else if (ch == '#') {
1574 /* end of command, start of checksum*/
1575 s->state = RS_CHKSUM1;
bellard858693c2004-03-31 18:52:07 +00001576 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
Alex Bennée118e2262017-07-12 11:52:13 +01001577 gdb_debug("command buffer overrun, dropping command\n");
bellard858693c2004-03-31 18:52:07 +00001578 s->state = RS_IDLE;
1579 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04001580 /* unescaped command character */
1581 s->line_buf[s->line_buf_index++] = ch;
1582 s->line_sum += ch;
1583 }
1584 break;
1585 case RS_GETLINE_ESC:
1586 if (ch == '#') {
1587 /* unexpected end of command in escape sequence */
1588 s->state = RS_CHKSUM1;
1589 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1590 /* command buffer overrun */
Alex Bennée118e2262017-07-12 11:52:13 +01001591 gdb_debug("command buffer overrun, dropping command\n");
Doug Gale4bf43122017-05-01 12:22:10 -04001592 s->state = RS_IDLE;
1593 } else {
1594 /* parse escaped character and leave escape state */
1595 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
1596 s->line_sum += ch;
1597 s->state = RS_GETLINE;
1598 }
1599 break;
1600 case RS_GETLINE_RLE:
1601 if (ch < ' ') {
1602 /* invalid RLE count encoding */
Alex Bennée118e2262017-07-12 11:52:13 +01001603 gdb_debug("got invalid RLE count: 0x%x\n", ch);
Doug Gale4bf43122017-05-01 12:22:10 -04001604 s->state = RS_GETLINE;
1605 } else {
1606 /* decode repeat length */
1607 int repeat = (unsigned char)ch - ' ' + 3;
1608 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
1609 /* that many repeats would overrun the command buffer */
Alex Bennée118e2262017-07-12 11:52:13 +01001610 gdb_debug("command buffer overrun, dropping command\n");
Doug Gale4bf43122017-05-01 12:22:10 -04001611 s->state = RS_IDLE;
1612 } else if (s->line_buf_index < 1) {
1613 /* got a repeat but we have nothing to repeat */
Alex Bennée118e2262017-07-12 11:52:13 +01001614 gdb_debug("got invalid RLE sequence\n");
Doug Gale4bf43122017-05-01 12:22:10 -04001615 s->state = RS_GETLINE;
1616 } else {
1617 /* repeat the last character */
1618 memset(s->line_buf + s->line_buf_index,
1619 s->line_buf[s->line_buf_index - 1], repeat);
1620 s->line_buf_index += repeat;
1621 s->line_sum += ch;
1622 s->state = RS_GETLINE;
1623 }
bellard858693c2004-03-31 18:52:07 +00001624 }
1625 break;
1626 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04001627 /* get high hex digit of checksum */
1628 if (!isxdigit(ch)) {
Alex Bennée118e2262017-07-12 11:52:13 +01001629 gdb_debug("got invalid command checksum digit\n");
Doug Gale4bf43122017-05-01 12:22:10 -04001630 s->state = RS_GETLINE;
1631 break;
1632 }
bellard858693c2004-03-31 18:52:07 +00001633 s->line_buf[s->line_buf_index] = '\0';
1634 s->line_csum = fromhex(ch) << 4;
1635 s->state = RS_CHKSUM2;
1636 break;
1637 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04001638 /* get low hex digit of checksum */
1639 if (!isxdigit(ch)) {
Alex Bennée118e2262017-07-12 11:52:13 +01001640 gdb_debug("got invalid command checksum digit\n");
Doug Gale4bf43122017-05-01 12:22:10 -04001641 s->state = RS_GETLINE;
1642 break;
bellard858693c2004-03-31 18:52:07 +00001643 }
Doug Gale4bf43122017-05-01 12:22:10 -04001644 s->line_csum |= fromhex(ch);
1645
1646 if (s->line_csum != (s->line_sum & 0xff)) {
Alex Bennée118e2262017-07-12 11:52:13 +01001647 gdb_debug("got command packet with incorrect checksum\n");
Doug Gale4bf43122017-05-01 12:22:10 -04001648 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00001649 reply = '-';
1650 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00001651 s->state = RS_IDLE;
1652 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04001653 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00001654 reply = '+';
1655 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00001656 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00001657 }
bellardb4608c02003-06-27 17:34:32 +00001658 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001659 default:
1660 abort();
bellardb4608c02003-06-27 17:34:32 +00001661 }
1662 }
bellard858693c2004-03-31 18:52:07 +00001663}
1664
Paul Brook0e1c9c52010-06-16 13:03:51 +01001665/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001666void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01001667{
1668 GDBState *s;
1669 char buf[4];
1670
1671 s = gdbserver_state;
1672 if (!s) {
1673 return;
1674 }
1675#ifdef CONFIG_USER_ONLY
1676 if (gdbserver_fd < 0 || s->fd < 0) {
1677 return;
1678 }
1679#endif
1680
1681 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
1682 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001683
1684#ifndef CONFIG_USER_ONLY
Marc-André Lureau1ce26102017-01-27 00:49:13 +04001685 qemu_chr_fe_deinit(&s->chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001686#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01001687}
1688
bellard1fddef42005-04-17 19:16:13 +00001689#ifdef CONFIG_USER_ONLY
1690int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001691gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00001692{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001693 GDBState *s;
1694 char buf[256];
1695 int n;
bellard1fddef42005-04-17 19:16:13 +00001696
Andreas Färber5ca666c2013-06-24 19:20:57 +02001697 s = gdbserver_state;
1698 if (gdbserver_fd < 0 || s->fd < 0) {
1699 return sig;
bellard1fddef42005-04-17 19:16:13 +00001700 }
1701
Andreas Färber5ca666c2013-06-24 19:20:57 +02001702 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001703 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001704 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00001705
Andreas Färber5ca666c2013-06-24 19:20:57 +02001706 if (sig != 0) {
1707 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
1708 put_packet(s, buf);
1709 }
1710 /* put_packet() might have detected that the peer terminated the
1711 connection. */
1712 if (s->fd < 0) {
1713 return sig;
1714 }
1715
1716 sig = 0;
1717 s->state = RS_IDLE;
1718 s->running_state = 0;
1719 while (s->running_state == 0) {
1720 n = read(s->fd, buf, 256);
1721 if (n > 0) {
1722 int i;
1723
1724 for (i = 0; i < n; i++) {
1725 gdb_read_byte(s, buf[i]);
1726 }
Peter Wu5819e3e2016-06-05 16:35:48 +02001727 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02001728 /* XXX: Connection closed. Should probably wait for another
1729 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02001730 if (n == 0) {
1731 close(s->fd);
1732 }
1733 s->fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02001734 return sig;
bellard1fddef42005-04-17 19:16:13 +00001735 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02001736 }
1737 sig = s->signal;
1738 s->signal = 0;
1739 return sig;
bellard1fddef42005-04-17 19:16:13 +00001740}
bellarde9009672005-04-26 20:42:36 +00001741
aurel32ca587a82008-12-18 22:44:13 +00001742/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001743void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00001744{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001745 GDBState *s;
1746 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00001747
Andreas Färber5ca666c2013-06-24 19:20:57 +02001748 s = gdbserver_state;
1749 if (gdbserver_fd < 0 || s->fd < 0) {
1750 return;
1751 }
aurel32ca587a82008-12-18 22:44:13 +00001752
Andreas Färber5ca666c2013-06-24 19:20:57 +02001753 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
1754 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00001755}
bellard1fddef42005-04-17 19:16:13 +00001756
aliguori880a7572008-11-18 20:30:24 +00001757static void gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00001758{
1759 GDBState *s;
1760 struct sockaddr_in sockaddr;
1761 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001762 int fd;
bellard858693c2004-03-31 18:52:07 +00001763
1764 for(;;) {
1765 len = sizeof(sockaddr);
1766 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1767 if (fd < 0 && errno != EINTR) {
1768 perror("accept");
1769 return;
1770 } else if (fd >= 0) {
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001771#ifndef _WIN32
1772 fcntl(fd, F_SETFD, FD_CLOEXEC);
1773#endif
bellard858693c2004-03-31 18:52:07 +00001774 break;
1775 }
1776 }
1777
1778 /* set short latency */
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001779 socket_set_nodelay(fd);
ths3b46e622007-09-17 08:09:54 +00001780
Anthony Liguori7267c092011-08-20 22:09:37 -05001781 s = g_malloc0(sizeof(GDBState));
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001782 s->c_cpu = first_cpu;
1783 s->g_cpu = first_cpu;
bellard858693c2004-03-31 18:52:07 +00001784 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02001785 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00001786
aliguori880a7572008-11-18 20:30:24 +00001787 gdbserver_state = s;
bellard858693c2004-03-31 18:52:07 +00001788}
1789
1790static int gdbserver_open(int port)
1791{
1792 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001793 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00001794
1795 fd = socket(PF_INET, SOCK_STREAM, 0);
1796 if (fd < 0) {
1797 perror("socket");
1798 return -1;
1799 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001800#ifndef _WIN32
1801 fcntl(fd, F_SETFD, FD_CLOEXEC);
1802#endif
bellard858693c2004-03-31 18:52:07 +00001803
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001804 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00001805
1806 sockaddr.sin_family = AF_INET;
1807 sockaddr.sin_port = htons(port);
1808 sockaddr.sin_addr.s_addr = 0;
1809 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1810 if (ret < 0) {
1811 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00001812 close(fd);
bellard858693c2004-03-31 18:52:07 +00001813 return -1;
1814 }
Peter Wu96165b92016-05-04 11:32:17 +02001815 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00001816 if (ret < 0) {
1817 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00001818 close(fd);
bellard858693c2004-03-31 18:52:07 +00001819 return -1;
1820 }
bellard858693c2004-03-31 18:52:07 +00001821 return fd;
1822}
1823
1824int gdbserver_start(int port)
1825{
1826 gdbserver_fd = gdbserver_open(port);
1827 if (gdbserver_fd < 0)
1828 return -1;
1829 /* accept connections */
aliguori880a7572008-11-18 20:30:24 +00001830 gdb_accept();
bellardb4608c02003-06-27 17:34:32 +00001831 return 0;
1832}
aurel322b1319c2008-12-18 22:44:04 +00001833
1834/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07001835void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00001836{
1837 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02001838
1839 if (gdbserver_fd < 0 || s->fd < 0) {
1840 return;
1841 }
aurel322b1319c2008-12-18 22:44:04 +00001842 close(s->fd);
1843 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001844 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02001845 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00001846}
pbrook4046d912007-01-28 01:53:16 +00001847#else
thsaa1f17c2007-07-11 22:48:58 +00001848static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00001849{
pbrook56aebc82008-10-11 17:55:29 +00001850 /* We can handle an arbitrarily large amount of data.
1851 Pick the maximum packet size, which is as good as anything. */
1852 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00001853}
1854
thsaa1f17c2007-07-11 22:48:58 +00001855static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00001856{
pbrook4046d912007-01-28 01:53:16 +00001857 int i;
1858
1859 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00001860 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00001861 }
1862}
1863
1864static void gdb_chr_event(void *opaque, int event)
1865{
1866 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05301867 case CHR_EVENT_OPENED:
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001868 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02001869 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00001870 break;
1871 default:
1872 break;
1873 }
1874}
1875
aliguori8a34a0f2009-03-05 23:01:55 +00001876static void gdb_monitor_output(GDBState *s, const char *msg, int len)
1877{
1878 char buf[MAX_PACKET_LENGTH];
1879
1880 buf[0] = 'O';
1881 if (len > (MAX_PACKET_LENGTH/2) - 1)
1882 len = (MAX_PACKET_LENGTH/2) - 1;
1883 memtohex(buf + 1, (uint8_t *)msg, len);
1884 put_packet(s, buf);
1885}
1886
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001887static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00001888{
1889 const char *p = (const char *)buf;
1890 int max_sz;
1891
1892 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
1893 for (;;) {
1894 if (len <= max_sz) {
1895 gdb_monitor_output(gdbserver_state, p, len);
1896 break;
1897 }
1898 gdb_monitor_output(gdbserver_state, p, max_sz);
1899 p += max_sz;
1900 len -= max_sz;
1901 }
1902 return len;
1903}
1904
aliguori59030a82009-04-05 18:43:41 +00001905#ifndef _WIN32
1906static void gdb_sigterm_handler(int signal)
1907{
Luiz Capitulino13548692011-07-29 15:36:43 -03001908 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001909 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001910 }
aliguori59030a82009-04-05 18:43:41 +00001911}
1912#endif
1913
Marc-André Lureau777357d2016-12-07 18:39:10 +03001914static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
1915 bool *be_opened, Error **errp)
1916{
1917 *be_opened = false;
1918}
1919
1920static void char_gdb_class_init(ObjectClass *oc, void *data)
1921{
1922 ChardevClass *cc = CHARDEV_CLASS(oc);
1923
1924 cc->internal = true;
1925 cc->open = gdb_monitor_open;
1926 cc->chr_write = gdb_monitor_write;
1927}
1928
1929#define TYPE_CHARDEV_GDB "chardev-gdb"
1930
1931static const TypeInfo char_gdb_type_info = {
1932 .name = TYPE_CHARDEV_GDB,
1933 .parent = TYPE_CHARDEV,
1934 .class_init = char_gdb_class_init,
1935};
1936
aliguori59030a82009-04-05 18:43:41 +00001937int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00001938{
1939 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00001940 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001941 Chardev *chr = NULL;
1942 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00001943
Ziyue Yang508b4ec2017-01-18 16:02:41 +08001944 if (!first_cpu) {
1945 error_report("gdbstub: meaningless to attach gdb to a "
1946 "machine without any CPU.");
1947 return -1;
1948 }
1949
aliguori59030a82009-04-05 18:43:41 +00001950 if (!device)
1951 return -1;
1952 if (strcmp(device, "none") != 0) {
1953 if (strstart(device, "tcp:", NULL)) {
1954 /* enforce required TCP attributes */
1955 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
1956 "%s,nowait,nodelay,server", device);
1957 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00001958 }
aliguori59030a82009-04-05 18:43:41 +00001959#ifndef _WIN32
1960 else if (strcmp(device, "stdio") == 0) {
1961 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00001962
aliguori59030a82009-04-05 18:43:41 +00001963 memset(&act, 0, sizeof(act));
1964 act.sa_handler = gdb_sigterm_handler;
1965 sigaction(SIGINT, &act, NULL);
1966 }
1967#endif
Marc-André Lureaub4948be2016-10-22 12:52:46 +03001968 chr = qemu_chr_new_noreplay("gdb", device);
aliguori36556b22009-03-28 18:05:53 +00001969 if (!chr)
1970 return -1;
pbrookcfc34752007-02-22 01:48:01 +00001971 }
1972
aliguori36556b22009-03-28 18:05:53 +00001973 s = gdbserver_state;
1974 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001975 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00001976 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00001977
aliguori36556b22009-03-28 18:05:53 +00001978 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
1979
1980 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03001981 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
1982 NULL, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00001983 monitor_init(mon_chr, 0);
1984 } else {
Marc-André Lureau1ce26102017-01-27 00:49:13 +04001985 qemu_chr_fe_deinit(&s->chr, true);
aliguori36556b22009-03-28 18:05:53 +00001986 mon_chr = s->mon_chr;
1987 memset(s, 0, sizeof(GDBState));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03001988 s->mon_chr = mon_chr;
aliguori36556b22009-03-28 18:05:53 +00001989 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001990 s->c_cpu = first_cpu;
1991 s->g_cpu = first_cpu;
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03001992 if (chr) {
1993 qemu_chr_fe_init(&s->chr, chr, &error_abort);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03001994 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
Anton Nefedov81517ba2017-07-06 15:08:49 +03001995 gdb_chr_event, NULL, NULL, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03001996 }
aliguori36556b22009-03-28 18:05:53 +00001997 s->state = chr ? RS_IDLE : RS_INACTIVE;
1998 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00001999 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00002000
pbrook4046d912007-01-28 01:53:16 +00002001 return 0;
2002}
Marc-André Lureau777357d2016-12-07 18:39:10 +03002003
2004static void register_types(void)
2005{
2006 type_register_static(&char_gdb_type_info);
2007}
2008
2009type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00002010#endif