blob: 378fdd874d1ce5176dc065aa64e8f5aeea70d6a4 [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)
Alex Bennéebd88c782017-07-12 11:52:15 +010067 TaskState *ts = (TaskState *) cpu->opaque;
68 return ts->ts_tid;
Alex Bennéed2a6c852017-07-12 11:52:14 +010069#else
70 return cpu->cpu_index + 1;
71#endif
72}
73
aurel32ca587a82008-12-18 22:44:13 +000074enum {
75 GDB_SIGNAL_0 = 0,
76 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +010077 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +000078 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +010079 GDB_SIGNAL_ABRT = 6,
80 GDB_SIGNAL_ALRM = 14,
81 GDB_SIGNAL_IO = 23,
82 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +000083 GDB_SIGNAL_UNKNOWN = 143
84};
85
86#ifdef CONFIG_USER_ONLY
87
88/* Map target signal numbers to GDB protocol signal numbers and vice
89 * versa. For user emulation's currently supported systems, we can
90 * assume most signals are defined.
91 */
92
93static int gdb_signal_table[] = {
94 0,
95 TARGET_SIGHUP,
96 TARGET_SIGINT,
97 TARGET_SIGQUIT,
98 TARGET_SIGILL,
99 TARGET_SIGTRAP,
100 TARGET_SIGABRT,
101 -1, /* SIGEMT */
102 TARGET_SIGFPE,
103 TARGET_SIGKILL,
104 TARGET_SIGBUS,
105 TARGET_SIGSEGV,
106 TARGET_SIGSYS,
107 TARGET_SIGPIPE,
108 TARGET_SIGALRM,
109 TARGET_SIGTERM,
110 TARGET_SIGURG,
111 TARGET_SIGSTOP,
112 TARGET_SIGTSTP,
113 TARGET_SIGCONT,
114 TARGET_SIGCHLD,
115 TARGET_SIGTTIN,
116 TARGET_SIGTTOU,
117 TARGET_SIGIO,
118 TARGET_SIGXCPU,
119 TARGET_SIGXFSZ,
120 TARGET_SIGVTALRM,
121 TARGET_SIGPROF,
122 TARGET_SIGWINCH,
123 -1, /* SIGLOST */
124 TARGET_SIGUSR1,
125 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000126#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000127 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000128#else
129 -1,
130#endif
aurel32ca587a82008-12-18 22:44:13 +0000131 -1, /* SIGPOLL */
132 -1,
133 -1,
134 -1,
135 -1,
136 -1,
137 -1,
138 -1,
139 -1,
140 -1,
141 -1,
142 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000143#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000144 __SIGRTMIN + 1,
145 __SIGRTMIN + 2,
146 __SIGRTMIN + 3,
147 __SIGRTMIN + 4,
148 __SIGRTMIN + 5,
149 __SIGRTMIN + 6,
150 __SIGRTMIN + 7,
151 __SIGRTMIN + 8,
152 __SIGRTMIN + 9,
153 __SIGRTMIN + 10,
154 __SIGRTMIN + 11,
155 __SIGRTMIN + 12,
156 __SIGRTMIN + 13,
157 __SIGRTMIN + 14,
158 __SIGRTMIN + 15,
159 __SIGRTMIN + 16,
160 __SIGRTMIN + 17,
161 __SIGRTMIN + 18,
162 __SIGRTMIN + 19,
163 __SIGRTMIN + 20,
164 __SIGRTMIN + 21,
165 __SIGRTMIN + 22,
166 __SIGRTMIN + 23,
167 __SIGRTMIN + 24,
168 __SIGRTMIN + 25,
169 __SIGRTMIN + 26,
170 __SIGRTMIN + 27,
171 __SIGRTMIN + 28,
172 __SIGRTMIN + 29,
173 __SIGRTMIN + 30,
174 __SIGRTMIN + 31,
175 -1, /* SIGCANCEL */
176 __SIGRTMIN,
177 __SIGRTMIN + 32,
178 __SIGRTMIN + 33,
179 __SIGRTMIN + 34,
180 __SIGRTMIN + 35,
181 __SIGRTMIN + 36,
182 __SIGRTMIN + 37,
183 __SIGRTMIN + 38,
184 __SIGRTMIN + 39,
185 __SIGRTMIN + 40,
186 __SIGRTMIN + 41,
187 __SIGRTMIN + 42,
188 __SIGRTMIN + 43,
189 __SIGRTMIN + 44,
190 __SIGRTMIN + 45,
191 __SIGRTMIN + 46,
192 __SIGRTMIN + 47,
193 __SIGRTMIN + 48,
194 __SIGRTMIN + 49,
195 __SIGRTMIN + 50,
196 __SIGRTMIN + 51,
197 __SIGRTMIN + 52,
198 __SIGRTMIN + 53,
199 __SIGRTMIN + 54,
200 __SIGRTMIN + 55,
201 __SIGRTMIN + 56,
202 __SIGRTMIN + 57,
203 __SIGRTMIN + 58,
204 __SIGRTMIN + 59,
205 __SIGRTMIN + 60,
206 __SIGRTMIN + 61,
207 __SIGRTMIN + 62,
208 __SIGRTMIN + 63,
209 __SIGRTMIN + 64,
210 __SIGRTMIN + 65,
211 __SIGRTMIN + 66,
212 __SIGRTMIN + 67,
213 __SIGRTMIN + 68,
214 __SIGRTMIN + 69,
215 __SIGRTMIN + 70,
216 __SIGRTMIN + 71,
217 __SIGRTMIN + 72,
218 __SIGRTMIN + 73,
219 __SIGRTMIN + 74,
220 __SIGRTMIN + 75,
221 __SIGRTMIN + 76,
222 __SIGRTMIN + 77,
223 __SIGRTMIN + 78,
224 __SIGRTMIN + 79,
225 __SIGRTMIN + 80,
226 __SIGRTMIN + 81,
227 __SIGRTMIN + 82,
228 __SIGRTMIN + 83,
229 __SIGRTMIN + 84,
230 __SIGRTMIN + 85,
231 __SIGRTMIN + 86,
232 __SIGRTMIN + 87,
233 __SIGRTMIN + 88,
234 __SIGRTMIN + 89,
235 __SIGRTMIN + 90,
236 __SIGRTMIN + 91,
237 __SIGRTMIN + 92,
238 __SIGRTMIN + 93,
239 __SIGRTMIN + 94,
240 __SIGRTMIN + 95,
241 -1, /* SIGINFO */
242 -1, /* UNKNOWN */
243 -1, /* DEFAULT */
244 -1,
245 -1,
246 -1,
247 -1,
248 -1,
249 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000250#endif
aurel32ca587a82008-12-18 22:44:13 +0000251};
bellard8f447cc2006-06-14 15:21:14 +0000252#else
aurel32ca587a82008-12-18 22:44:13 +0000253/* In system mode we only need SIGINT and SIGTRAP; other signals
254 are not yet supported. */
255
256enum {
257 TARGET_SIGINT = 2,
258 TARGET_SIGTRAP = 5
259};
260
261static int gdb_signal_table[] = {
262 -1,
263 -1,
264 TARGET_SIGINT,
265 -1,
266 -1,
267 TARGET_SIGTRAP
268};
bellard8f447cc2006-06-14 15:21:14 +0000269#endif
bellardb4608c02003-06-27 17:34:32 +0000270
aurel32ca587a82008-12-18 22:44:13 +0000271#ifdef CONFIG_USER_ONLY
272static int target_signal_to_gdb (int sig)
273{
274 int i;
275 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
276 if (gdb_signal_table[i] == sig)
277 return i;
278 return GDB_SIGNAL_UNKNOWN;
279}
280#endif
281
282static int gdb_signal_to_target (int sig)
283{
284 if (sig < ARRAY_SIZE (gdb_signal_table))
285 return gdb_signal_table[sig];
286 else
287 return -1;
288}
289
Alex Bennée118e2262017-07-12 11:52:13 +0100290/* #define DEBUG_GDB */
291
292#ifdef DEBUG_GDB
293# define DEBUG_GDB_GATE 1
294#else
295# define DEBUG_GDB_GATE 0
296#endif
297
298#define gdb_debug(fmt, ...) do { \
299 if (DEBUG_GDB_GATE) { \
300 fprintf(stderr, "%s: " fmt, __func__, ## __VA_ARGS__); \
301 } \
302} while (0)
303
bellardb4608c02003-06-27 17:34:32 +0000304
pbrook56aebc82008-10-11 17:55:29 +0000305typedef struct GDBRegisterState {
306 int base_reg;
307 int num_regs;
308 gdb_reg_cb get_reg;
309 gdb_reg_cb set_reg;
310 const char *xml;
311 struct GDBRegisterState *next;
312} GDBRegisterState;
313
bellard858693c2004-03-31 18:52:07 +0000314enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000315 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000316 RS_IDLE,
317 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400318 RS_GETLINE_ESC,
319 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000320 RS_CHKSUM1,
321 RS_CHKSUM2,
322};
bellard858693c2004-03-31 18:52:07 +0000323typedef struct GDBState {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200324 CPUState *c_cpu; /* current CPU for step/continue ops */
325 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200326 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000327 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000328 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000329 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400330 int line_sum; /* running checksum */
331 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000332 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000333 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000334 int signal;
bellard41625032005-04-24 10:07:11 +0000335#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000336 int fd;
bellard41625032005-04-24 10:07:11 +0000337 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000338#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300339 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300340 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000341#endif
Meador Ingecdb432b2012-03-15 17:49:45 +0000342 char syscall_buf[256];
343 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000344} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000345
edgar_igl60897d32008-05-09 08:25:14 +0000346/* By default use no IRQs and no timers while single stepping so as to
347 * make single stepping like an ICE HW step.
348 */
349static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
350
aliguori880a7572008-11-18 20:30:24 +0000351static GDBState *gdbserver_state;
352
Andreas Färber5b50e792013-06-29 04:18:45 +0200353bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000354
bellard1fddef42005-04-17 19:16:13 +0000355#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000356/* XXX: This is not thread safe. Do we care? */
357static int gdbserver_fd = -1;
358
bellard858693c2004-03-31 18:52:07 +0000359static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000360{
361 uint8_t ch;
362 int ret;
363
364 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000365 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000366 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000367 if (errno == ECONNRESET)
368 s->fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200369 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000370 return -1;
371 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000372 close(s->fd);
373 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000374 return -1;
375 } else {
376 break;
377 }
378 }
379 return ch;
380}
pbrook4046d912007-01-28 01:53:16 +0000381#endif
bellardb4608c02003-06-27 17:34:32 +0000382
blueswir1654efcf2009-04-18 07:29:59 +0000383static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000384 GDB_SYS_UNKNOWN,
385 GDB_SYS_ENABLED,
386 GDB_SYS_DISABLED,
387} gdb_syscall_mode;
388
Liviu Ionescua38bb072014-12-11 12:07:48 +0000389/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000390int use_gdb_syscalls(void)
391{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100392 SemihostingTarget target = semihosting_get_target();
393 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000394 /* -semihosting-config target=native */
395 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100396 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000397 /* -semihosting-config target=gdb */
398 return true;
399 }
400
401 /* -semihosting-config target=auto */
402 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000403 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000404 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
405 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000406 }
407 return gdb_syscall_mode == GDB_SYS_ENABLED;
408}
409
edgar_iglba70a622008-03-14 06:10:42 +0000410/* Resume execution. */
411static inline void gdb_continue(GDBState *s)
412{
413#ifdef CONFIG_USER_ONLY
414 s->running_state = 1;
415#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200416 if (!runstate_needs_reset()) {
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200417 vm_start();
418 }
edgar_iglba70a622008-03-14 06:10:42 +0000419#endif
420}
421
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100422/*
423 * Resume execution, per CPU actions. For user-mode emulation it's
424 * equivalent to gdb_continue.
425 */
426static int gdb_continue_partial(GDBState *s, char *newstates)
427{
428 CPUState *cpu;
429 int res = 0;
430#ifdef CONFIG_USER_ONLY
431 /*
432 * This is not exactly accurate, but it's an improvement compared to the
433 * previous situation, where only one CPU would be single-stepped.
434 */
435 CPU_FOREACH(cpu) {
436 if (newstates[cpu->cpu_index] == 's') {
437 cpu_single_step(cpu, sstep_flags);
438 }
439 }
440 s->running_state = 1;
441#else
442 int flag = 0;
443
444 if (!runstate_needs_reset()) {
445 if (vm_prepare_start()) {
446 return 0;
447 }
448
449 CPU_FOREACH(cpu) {
450 switch (newstates[cpu->cpu_index]) {
451 case 0:
452 case 1:
453 break; /* nothing to do here */
454 case 's':
455 cpu_single_step(cpu, sstep_flags);
456 cpu_resume(cpu);
457 flag = 1;
458 break;
459 case 'c':
460 cpu_resume(cpu);
461 flag = 1;
462 break;
463 default:
464 res = -1;
465 break;
466 }
467 }
468 }
469 if (flag) {
470 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
471 }
472#endif
473 return res;
474}
475
bellard858693c2004-03-31 18:52:07 +0000476static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000477{
pbrook4046d912007-01-28 01:53:16 +0000478#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000479 int ret;
480
481 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000482 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000483 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200484 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000485 return;
486 } else {
487 buf += ret;
488 len -= ret;
489 }
490 }
pbrook4046d912007-01-28 01:53:16 +0000491#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100492 /* XXX this blocks entire thread. Rewrite to use
493 * qemu_chr_fe_write and background I/O callbacks */
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300494 qemu_chr_fe_write_all(&s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000495#endif
bellardb4608c02003-06-27 17:34:32 +0000496}
497
498static inline int fromhex(int v)
499{
500 if (v >= '0' && v <= '9')
501 return v - '0';
502 else if (v >= 'A' && v <= 'F')
503 return v - 'A' + 10;
504 else if (v >= 'a' && v <= 'f')
505 return v - 'a' + 10;
506 else
507 return 0;
508}
509
510static inline int tohex(int v)
511{
512 if (v < 10)
513 return v + '0';
514 else
515 return v - 10 + 'a';
516}
517
518static void memtohex(char *buf, const uint8_t *mem, int len)
519{
520 int i, c;
521 char *q;
522 q = buf;
523 for(i = 0; i < len; i++) {
524 c = mem[i];
525 *q++ = tohex(c >> 4);
526 *q++ = tohex(c & 0xf);
527 }
528 *q = '\0';
529}
530
531static void hextomem(uint8_t *mem, const char *buf, int len)
532{
533 int i;
534
535 for(i = 0; i < len; i++) {
536 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
537 buf += 2;
538 }
539}
540
bellardb4608c02003-06-27 17:34:32 +0000541/* return -1 if error, 0 if OK */
pbrook56aebc82008-10-11 17:55:29 +0000542static int put_packet_binary(GDBState *s, const char *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000543{
pbrook56aebc82008-10-11 17:55:29 +0000544 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000545 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000546
bellardb4608c02003-06-27 17:34:32 +0000547 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000548 p = s->last_packet;
549 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000550 memcpy(p, buf, len);
551 p += len;
bellardb4608c02003-06-27 17:34:32 +0000552 csum = 0;
553 for(i = 0; i < len; i++) {
554 csum += buf[i];
555 }
pbrook4046d912007-01-28 01:53:16 +0000556 *(p++) = '#';
557 *(p++) = tohex((csum >> 4) & 0xf);
558 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000559
pbrook4046d912007-01-28 01:53:16 +0000560 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000561 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000562
pbrook4046d912007-01-28 01:53:16 +0000563#ifdef CONFIG_USER_ONLY
564 i = get_char(s);
565 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000566 return -1;
pbrook4046d912007-01-28 01:53:16 +0000567 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000568 break;
pbrook4046d912007-01-28 01:53:16 +0000569#else
570 break;
571#endif
bellardb4608c02003-06-27 17:34:32 +0000572 }
573 return 0;
574}
575
pbrook56aebc82008-10-11 17:55:29 +0000576/* return -1 if error, 0 if OK */
577static int put_packet(GDBState *s, const char *buf)
578{
Alex Bennée118e2262017-07-12 11:52:13 +0100579 gdb_debug("reply='%s'\n", buf);
pbrook56aebc82008-10-11 17:55:29 +0000580
581 return put_packet_binary(s, buf, strlen(buf));
582}
583
pbrook56aebc82008-10-11 17:55:29 +0000584/* Encode data using the encoding for 'x' packets. */
585static int memtox(char *buf, const char *mem, int len)
586{
587 char *p = buf;
588 char c;
589
590 while (len--) {
591 c = *(mem++);
592 switch (c) {
593 case '#': case '$': case '*': case '}':
594 *(p++) = '}';
595 *(p++) = c ^ 0x20;
596 break;
597 default:
598 *(p++) = c;
599 break;
600 }
601 }
602 return p - buf;
603}
604
Andreas Färber5b24c642013-07-07 15:08:22 +0200605static const char *get_feature_xml(const char *p, const char **newp,
606 CPUClass *cc)
pbrook56aebc82008-10-11 17:55:29 +0000607{
pbrook56aebc82008-10-11 17:55:29 +0000608 size_t len;
609 int i;
610 const char *name;
611 static char target_xml[1024];
612
613 len = 0;
614 while (p[len] && p[len] != ':')
615 len++;
616 *newp = p + len;
617
618 name = NULL;
619 if (strncmp(p, "target.xml", len) == 0) {
620 /* Generate the XML description for this CPU. */
621 if (!target_xml[0]) {
622 GDBRegisterState *r;
Andreas Färbereac8b352013-06-28 21:11:37 +0200623 CPUState *cpu = first_cpu;
pbrook56aebc82008-10-11 17:55:29 +0000624
David Hildenbrandb3820e62015-12-03 13:14:41 +0100625 pstrcat(target_xml, sizeof(target_xml),
626 "<?xml version=\"1.0\"?>"
627 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
628 "<target>");
629 if (cc->gdb_arch_name) {
630 gchar *arch = cc->gdb_arch_name(cpu);
631 pstrcat(target_xml, sizeof(target_xml), "<architecture>");
632 pstrcat(target_xml, sizeof(target_xml), arch);
633 pstrcat(target_xml, sizeof(target_xml), "</architecture>");
634 g_free(arch);
635 }
636 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
637 pstrcat(target_xml, sizeof(target_xml), cc->gdb_core_xml_file);
638 pstrcat(target_xml, sizeof(target_xml), "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200639 for (r = cpu->gdb_regs; r; r = r->next) {
blueswir12dc766d2009-04-13 16:06:19 +0000640 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
641 pstrcat(target_xml, sizeof(target_xml), r->xml);
642 pstrcat(target_xml, sizeof(target_xml), "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000643 }
blueswir12dc766d2009-04-13 16:06:19 +0000644 pstrcat(target_xml, sizeof(target_xml), "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000645 }
646 return target_xml;
647 }
648 for (i = 0; ; i++) {
649 name = xml_builtin[i][0];
650 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
651 break;
652 }
653 return name ? xml_builtin[i][1] : NULL;
654}
pbrook56aebc82008-10-11 17:55:29 +0000655
Andreas Färber385b9f02013-06-27 18:25:36 +0200656static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000657{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200658 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200659 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000660 GDBRegisterState *r;
661
Andreas Färbera0e372f2013-06-28 23:18:47 +0200662 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200663 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200664 }
pbrook56aebc82008-10-11 17:55:29 +0000665
Andreas Färbereac8b352013-06-28 21:11:37 +0200666 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000667 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
668 return r->get_reg(env, mem_buf, reg - r->base_reg);
669 }
670 }
671 return 0;
672}
673
Andreas Färber385b9f02013-06-27 18:25:36 +0200674static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000675{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200676 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200677 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000678 GDBRegisterState *r;
679
Andreas Färbera0e372f2013-06-28 23:18:47 +0200680 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200681 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200682 }
pbrook56aebc82008-10-11 17:55:29 +0000683
Andreas Färbereac8b352013-06-28 21:11:37 +0200684 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000685 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
686 return r->set_reg(env, mem_buf, reg - r->base_reg);
687 }
688 }
689 return 0;
690}
691
692/* Register a supplemental set of CPU registers. If g_pos is nonzero it
693 specifies the first register number and these registers are included in
694 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
695 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
696 */
697
Andreas Färber22169d42013-06-28 21:27:39 +0200698void gdb_register_coprocessor(CPUState *cpu,
699 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
700 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000701{
702 GDBRegisterState *s;
703 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000704
Andreas Färbereac8b352013-06-28 21:11:37 +0200705 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000706 while (*p) {
707 /* Check for duplicates. */
708 if (strcmp((*p)->xml, xml) == 0)
709 return;
710 p = &(*p)->next;
711 }
Stefan Weil9643c252011-10-18 22:25:38 +0200712
713 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200714 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200715 s->num_regs = num_regs;
716 s->get_reg = get_reg;
717 s->set_reg = set_reg;
718 s->xml = xml;
719
pbrook56aebc82008-10-11 17:55:29 +0000720 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200721 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000722 *p = s;
723 if (g_pos) {
724 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800725 error_report("Error: Bad gdb register numbering for '%s', "
726 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200727 } else {
728 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000729 }
730 }
731}
732
aliguoria1d1bb32008-11-18 20:07:32 +0000733#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100734/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
735static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
736{
737 static const int xlat[] = {
738 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
739 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
740 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
741 };
742
743 CPUClass *cc = CPU_GET_CLASS(cpu);
744 int cputype = xlat[gdbtype];
745
746 if (cc->gdb_stop_before_watchpoint) {
747 cputype |= BP_STOP_BEFORE_ACCESS;
748 }
749 return cputype;
750}
aliguoria1d1bb32008-11-18 20:07:32 +0000751#endif
752
aliguori880a7572008-11-18 20:30:24 +0000753static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000754{
Andreas Färber182735e2013-05-29 22:29:20 +0200755 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000756 int err = 0;
757
Andreas Färber62278812013-06-27 17:12:06 +0200758 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200759 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200760 }
aliguorie22a25c2009-03-12 20:12:48 +0000761
aliguoria1d1bb32008-11-18 20:07:32 +0000762 switch (type) {
763 case GDB_BREAKPOINT_SW:
764 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200765 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200766 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
767 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000768 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200769 }
aliguori880a7572008-11-18 20:30:24 +0000770 }
771 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000772#ifndef CONFIG_USER_ONLY
773 case GDB_WATCHPOINT_WRITE:
774 case GDB_WATCHPOINT_READ:
775 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200776 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100777 err = cpu_watchpoint_insert(cpu, addr, len,
778 xlat_gdb_type(cpu, type), NULL);
779 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000780 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +0100781 }
aliguori880a7572008-11-18 20:30:24 +0000782 }
783 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000784#endif
785 default:
786 return -ENOSYS;
787 }
788}
789
aliguori880a7572008-11-18 20:30:24 +0000790static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000791{
Andreas Färber182735e2013-05-29 22:29:20 +0200792 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000793 int err = 0;
794
Andreas Färber62278812013-06-27 17:12:06 +0200795 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200796 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200797 }
aliguorie22a25c2009-03-12 20:12:48 +0000798
aliguoria1d1bb32008-11-18 20:07:32 +0000799 switch (type) {
800 case GDB_BREAKPOINT_SW:
801 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200802 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200803 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
804 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000805 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200806 }
aliguori880a7572008-11-18 20:30:24 +0000807 }
808 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000809#ifndef CONFIG_USER_ONLY
810 case GDB_WATCHPOINT_WRITE:
811 case GDB_WATCHPOINT_READ:
812 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200813 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100814 err = cpu_watchpoint_remove(cpu, addr, len,
815 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +0000816 if (err)
817 break;
818 }
819 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000820#endif
821 default:
822 return -ENOSYS;
823 }
824}
825
aliguori880a7572008-11-18 20:30:24 +0000826static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +0000827{
Andreas Färber182735e2013-05-29 22:29:20 +0200828 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000829
aliguorie22a25c2009-03-12 20:12:48 +0000830 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200831 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +0000832 return;
833 }
834
Andreas Färberbdc44642013-06-24 23:50:24 +0200835 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200836 cpu_breakpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000837#ifndef CONFIG_USER_ONLY
Andreas Färber75a34032013-09-02 16:57:02 +0200838 cpu_watchpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000839#endif
aliguori880a7572008-11-18 20:30:24 +0000840 }
aliguoria1d1bb32008-11-18 20:07:32 +0000841}
842
aurel32fab9d282009-04-08 21:29:37 +0000843static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
844{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200845 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +0200846
847 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -0700848 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +0000849}
850
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200851static CPUState *find_cpu(uint32_t thread_id)
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700852{
Andreas Färber0d342822012-12-17 07:12:13 +0100853 CPUState *cpu;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700854
Andreas Färberbdc44642013-06-24 23:50:24 +0200855 CPU_FOREACH(cpu) {
Alex Bennéed2a6c852017-07-12 11:52:14 +0100856 if (cpu_gdb_index(cpu) == thread_id) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200857 return cpu;
Andreas Färberaa48dd92013-07-09 20:50:52 +0200858 }
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700859 }
Andreas Färberaa48dd92013-07-09 20:50:52 +0200860
861 return NULL;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700862}
863
Jan Kiszka4dabe742015-02-07 09:38:43 +0100864static int is_query_packet(const char *p, const char *query, char separator)
865{
866 unsigned int query_len = strlen(query);
867
868 return strncmp(p, query, query_len) == 0 &&
869 (p[query_len] == '\0' || p[query_len] == separator);
870}
871
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100872/**
873 * gdb_handle_vcont - Parses and handles a vCont packet.
874 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
875 * a format error, 0 on success.
876 */
877static int gdb_handle_vcont(GDBState *s, const char *p)
878{
879 int res, idx, signal = 0;
880 char cur_action;
881 char *newstates;
882 unsigned long tmp;
883 CPUState *cpu;
884#ifdef CONFIG_USER_ONLY
885 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
886
887 CPU_FOREACH(cpu) {
888 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
889 }
890#endif
891 /* uninitialised CPUs stay 0 */
892 newstates = g_new0(char, max_cpus);
893
894 /* mark valid CPUs with 1 */
895 CPU_FOREACH(cpu) {
896 newstates[cpu->cpu_index] = 1;
897 }
898
899 /*
900 * res keeps track of what error we are returning, with -ENOTSUP meaning
901 * that the command is unknown or unsupported, thus returning an empty
902 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
903 * or incorrect parameters passed.
904 */
905 res = 0;
906 while (*p) {
907 if (*p++ != ';') {
908 res = -ENOTSUP;
909 goto out;
910 }
911
912 cur_action = *p++;
913 if (cur_action == 'C' || cur_action == 'S') {
914 cur_action = tolower(cur_action);
915 res = qemu_strtoul(p + 1, &p, 16, &tmp);
916 if (res) {
917 goto out;
918 }
919 signal = gdb_signal_to_target(tmp);
920 } else if (cur_action != 'c' && cur_action != 's') {
921 /* unknown/invalid/unsupported command */
922 res = -ENOTSUP;
923 goto out;
924 }
925 /* thread specification. special values: (none), -1 = all; 0 = any */
926 if ((p[0] == ':' && p[1] == '-' && p[2] == '1') || (p[0] != ':')) {
927 if (*p == ':') {
928 p += 3;
929 }
930 for (idx = 0; idx < max_cpus; idx++) {
931 if (newstates[idx] == 1) {
932 newstates[idx] = cur_action;
933 }
934 }
935 } else if (*p == ':') {
936 p++;
937 res = qemu_strtoul(p, &p, 16, &tmp);
938 if (res) {
939 goto out;
940 }
941 idx = tmp;
942 /* 0 means any thread, so we pick the first valid CPU */
943 if (!idx) {
Alex Bennéed2a6c852017-07-12 11:52:14 +0100944 idx = cpu_gdb_index(first_cpu);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100945 }
946
947 /*
948 * If we are in user mode, the thread specified is actually a
949 * thread id, and not an index. We need to find the actual
950 * CPU first, and only then we can use its index.
951 */
952 cpu = find_cpu(idx);
953 /* invalid CPU/thread specified */
954 if (!idx || !cpu) {
955 res = -EINVAL;
956 goto out;
957 }
958 /* only use if no previous match occourred */
959 if (newstates[cpu->cpu_index] == 1) {
960 newstates[cpu->cpu_index] = cur_action;
961 }
962 }
963 }
964 s->signal = signal;
965 gdb_continue_partial(s, newstates);
966
967out:
968 g_free(newstates);
969
970 return res;
971}
972
aliguori880a7572008-11-18 20:30:24 +0000973static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +0000974{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200975 CPUState *cpu;
Andreas Färber5b24c642013-07-07 15:08:22 +0200976 CPUClass *cc;
bellardb4608c02003-06-27 17:34:32 +0000977 const char *p;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700978 uint32_t thread;
979 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +0000980 char buf[MAX_PACKET_LENGTH];
981 uint8_t mem_buf[MAX_PACKET_LENGTH];
982 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +0000983 target_ulong addr, len;
ths3b46e622007-09-17 08:09:54 +0000984
Alex Bennée118e2262017-07-12 11:52:13 +0100985
986 gdb_debug("command='%s'\n", line_buf);
987
bellard858693c2004-03-31 18:52:07 +0000988 p = line_buf;
989 ch = *p++;
990 switch(ch) {
991 case '?':
bellard1fddef42005-04-17 19:16:13 +0000992 /* TODO: Make this return the correct value for user-mode. */
aurel32ca587a82008-12-18 22:44:13 +0000993 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
Alex Bennéed2a6c852017-07-12 11:52:14 +0100994 cpu_gdb_index(s->c_cpu));
bellard858693c2004-03-31 18:52:07 +0000995 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +0000996 /* Remove all the breakpoints when this query is issued,
997 * because gdb is doing and initial connect and the state
998 * should be cleaned up.
999 */
aliguori880a7572008-11-18 20:30:24 +00001000 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +00001001 break;
1002 case 'c':
1003 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +00001004 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001005 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001006 }
aurel32ca587a82008-12-18 22:44:13 +00001007 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +00001008 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +00001009 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +00001010 case 'C':
aurel32ca587a82008-12-18 22:44:13 +00001011 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
1012 if (s->signal == -1)
1013 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +00001014 gdb_continue(s);
1015 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001016 case 'v':
1017 if (strncmp(p, "Cont", 4) == 0) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001018 p += 4;
1019 if (*p == '?') {
1020 put_packet(s, "vCont;c;C;s;S");
1021 break;
1022 }
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001023
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001024 res = gdb_handle_vcont(s, p);
1025
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001026 if (res) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001027 if ((res == -EINVAL) || (res == -ERANGE)) {
1028 put_packet(s, "E22");
1029 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001030 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001031 goto unknown_command;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001032 }
1033 break;
1034 } else {
1035 goto unknown_command;
1036 }
edgar_igl7d03f822008-05-17 18:58:29 +00001037 case 'k':
1038 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001039 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00001040 exit(0);
1041 case 'D':
1042 /* Detach packet */
aliguori880a7572008-11-18 20:30:24 +00001043 gdb_breakpoint_remove_all();
Daniel Gutson7ea06da2010-02-26 14:13:50 -03001044 gdb_syscall_mode = GDB_SYS_DISABLED;
edgar_igl7d03f822008-05-17 18:58:29 +00001045 gdb_continue(s);
1046 put_packet(s, "OK");
1047 break;
bellard858693c2004-03-31 18:52:07 +00001048 case 's':
1049 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +00001050 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001051 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001052 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001053 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +00001054 gdb_continue(s);
bellard41625032005-04-24 10:07:11 +00001055 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +00001056 case 'F':
1057 {
1058 target_ulong ret;
1059 target_ulong err;
1060
1061 ret = strtoull(p, (char **)&p, 16);
1062 if (*p == ',') {
1063 p++;
1064 err = strtoull(p, (char **)&p, 16);
1065 } else {
1066 err = 0;
1067 }
1068 if (*p == ',')
1069 p++;
1070 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001071 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001072 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +00001073 s->current_syscall_cb = NULL;
1074 }
pbrooka2d1eba2007-01-28 03:10:55 +00001075 if (type == 'C') {
1076 put_packet(s, "T02");
1077 } else {
edgar_iglba70a622008-03-14 06:10:42 +00001078 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +00001079 }
1080 }
1081 break;
bellard858693c2004-03-31 18:52:07 +00001082 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001083 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001084 len = 0;
Andreas Färber35143f02013-08-12 18:09:47 +02001085 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001086 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +00001087 len += reg_size;
1088 }
1089 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +00001090 put_packet(s, buf);
1091 break;
1092 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001093 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001094 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +00001095 len = strlen(p) / 2;
1096 hextomem((uint8_t *)registers, p, len);
Andreas Färber35143f02013-08-12 18:09:47 +02001097 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001098 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001099 len -= reg_size;
1100 registers += reg_size;
1101 }
bellard858693c2004-03-31 18:52:07 +00001102 put_packet(s, "OK");
1103 break;
1104 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001105 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001106 if (*p == ',')
1107 p++;
bellard9d9754a2006-06-25 15:32:37 +00001108 len = strtoull(p, NULL, 16);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001109
1110 /* memtohex() doubles the required space */
1111 if (len > MAX_PACKET_LENGTH / 2) {
1112 put_packet (s, "E22");
1113 break;
1114 }
1115
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001116 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001117 put_packet (s, "E14");
1118 } else {
1119 memtohex(buf, mem_buf, len);
1120 put_packet(s, buf);
1121 }
bellard858693c2004-03-31 18:52:07 +00001122 break;
1123 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001124 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001125 if (*p == ',')
1126 p++;
bellard9d9754a2006-06-25 15:32:37 +00001127 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001128 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001129 p++;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001130
1131 /* hextomem() reads 2*len bytes */
1132 if (len > strlen(p) / 2) {
1133 put_packet (s, "E22");
1134 break;
1135 }
bellard858693c2004-03-31 18:52:07 +00001136 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001137 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +02001138 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +00001139 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001140 } else {
bellard858693c2004-03-31 18:52:07 +00001141 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001142 }
bellard858693c2004-03-31 18:52:07 +00001143 break;
pbrook56aebc82008-10-11 17:55:29 +00001144 case 'p':
1145 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1146 This works, but can be very slow. Anything new enough to
1147 understand XML also knows how to use this properly. */
1148 if (!gdb_has_xml)
1149 goto unknown_command;
1150 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001151 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001152 if (reg_size) {
1153 memtohex(buf, mem_buf, reg_size);
1154 put_packet(s, buf);
1155 } else {
1156 put_packet(s, "E14");
1157 }
1158 break;
1159 case 'P':
1160 if (!gdb_has_xml)
1161 goto unknown_command;
1162 addr = strtoull(p, (char **)&p, 16);
1163 if (*p == '=')
1164 p++;
1165 reg_size = strlen(p) / 2;
1166 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001167 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001168 put_packet(s, "OK");
1169 break;
bellard858693c2004-03-31 18:52:07 +00001170 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001171 case 'z':
1172 type = strtoul(p, (char **)&p, 16);
1173 if (*p == ',')
1174 p++;
bellard9d9754a2006-06-25 15:32:37 +00001175 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001176 if (*p == ',')
1177 p++;
bellard9d9754a2006-06-25 15:32:37 +00001178 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001179 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001180 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001181 else
aliguori880a7572008-11-18 20:30:24 +00001182 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001183 if (res >= 0)
1184 put_packet(s, "OK");
1185 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001186 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001187 else
1188 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001189 break;
aliguori880a7572008-11-18 20:30:24 +00001190 case 'H':
1191 type = *p++;
1192 thread = strtoull(p, (char **)&p, 16);
1193 if (thread == -1 || thread == 0) {
1194 put_packet(s, "OK");
1195 break;
1196 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001197 cpu = find_cpu(thread);
1198 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001199 put_packet(s, "E22");
1200 break;
1201 }
1202 switch (type) {
1203 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001204 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001205 put_packet(s, "OK");
1206 break;
1207 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001208 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001209 put_packet(s, "OK");
1210 break;
1211 default:
1212 put_packet(s, "E22");
1213 break;
1214 }
1215 break;
1216 case 'T':
1217 thread = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001218 cpu = find_cpu(thread);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001219
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001220 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001221 put_packet(s, "OK");
1222 } else {
aliguori880a7572008-11-18 20:30:24 +00001223 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001224 }
aliguori880a7572008-11-18 20:30:24 +00001225 break;
pbrook978efd62006-06-17 18:30:42 +00001226 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001227 case 'Q':
1228 /* parse any 'q' packets here */
1229 if (!strcmp(p,"qemu.sstepbits")) {
1230 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001231 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1232 SSTEP_ENABLE,
1233 SSTEP_NOIRQ,
1234 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001235 put_packet(s, buf);
1236 break;
Jan Kiszka4dabe742015-02-07 09:38:43 +01001237 } else if (is_query_packet(p, "qemu.sstep", '=')) {
edgar_igl60897d32008-05-09 08:25:14 +00001238 /* Display or change the sstep_flags */
1239 p += 10;
1240 if (*p != '=') {
1241 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001242 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001243 put_packet(s, buf);
1244 break;
1245 }
1246 p++;
1247 type = strtoul(p, (char **)&p, 16);
1248 sstep_flags = type;
1249 put_packet(s, "OK");
1250 break;
aliguori880a7572008-11-18 20:30:24 +00001251 } else if (strcmp(p,"C") == 0) {
1252 /* "Current thread" remains vague in the spec, so always return
1253 * the first CPU (gdb returns the first thread). */
1254 put_packet(s, "QC1");
1255 break;
1256 } else if (strcmp(p,"fThreadInfo") == 0) {
Andreas Färber52f34622013-06-27 13:44:40 +02001257 s->query_cpu = first_cpu;
aliguori880a7572008-11-18 20:30:24 +00001258 goto report_cpuinfo;
1259 } else if (strcmp(p,"sThreadInfo") == 0) {
1260 report_cpuinfo:
1261 if (s->query_cpu) {
Alex Bennéed2a6c852017-07-12 11:52:14 +01001262 snprintf(buf, sizeof(buf), "m%x", cpu_gdb_index(s->query_cpu));
aliguori880a7572008-11-18 20:30:24 +00001263 put_packet(s, buf);
Andreas Färberbdc44642013-06-24 23:50:24 +02001264 s->query_cpu = CPU_NEXT(s->query_cpu);
aliguori880a7572008-11-18 20:30:24 +00001265 } else
1266 put_packet(s, "l");
1267 break;
1268 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1269 thread = strtoull(p+16, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001270 cpu = find_cpu(thread);
1271 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001272 cpu_synchronize_state(cpu);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001273 /* memtohex() doubles the required space */
1274 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
Andreas Färber55e5c282012-12-17 06:18:02 +01001275 "CPU#%d [%s]", cpu->cpu_index,
Andreas Färber259186a2013-01-17 18:51:17 +01001276 cpu->halted ? "halted " : "running");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001277 memtohex(buf, mem_buf, len);
1278 put_packet(s, buf);
1279 }
aliguori880a7572008-11-18 20:30:24 +00001280 break;
edgar_igl60897d32008-05-09 08:25:14 +00001281 }
blueswir10b8a9882009-03-07 10:51:36 +00001282#ifdef CONFIG_USER_ONLY
Jan Kiszka070949f2015-02-07 09:38:42 +01001283 else if (strcmp(p, "Offsets") == 0) {
Andreas Färber0429a972013-08-26 18:14:44 +02001284 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001285
blueswir1363a37d2008-08-21 17:58:08 +00001286 snprintf(buf, sizeof(buf),
1287 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1288 ";Bss=" TARGET_ABI_FMT_lx,
1289 ts->info->code_offset,
1290 ts->info->data_offset,
1291 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001292 put_packet(s, buf);
1293 break;
1294 }
blueswir10b8a9882009-03-07 10:51:36 +00001295#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001296 else if (strncmp(p, "Rcmd,", 5) == 0) {
1297 int len = strlen(p + 5);
1298
1299 if ((len % 2) != 0) {
1300 put_packet(s, "E01");
1301 break;
1302 }
aliguori8a34a0f2009-03-05 23:01:55 +00001303 len = len / 2;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001304 hextomem(mem_buf, p + 5, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001305 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001306 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001307 put_packet(s, "OK");
1308 break;
1309 }
blueswir10b8a9882009-03-07 10:51:36 +00001310#endif /* !CONFIG_USER_ONLY */
Jan Kiszka4dabe742015-02-07 09:38:43 +01001311 if (is_query_packet(p, "Supported", ':')) {
blueswir15b3715b2008-10-25 11:18:12 +00001312 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
Andreas Färber5b24c642013-07-07 15:08:22 +02001313 cc = CPU_GET_CLASS(first_cpu);
1314 if (cc->gdb_core_xml_file != NULL) {
1315 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1316 }
pbrook56aebc82008-10-11 17:55:29 +00001317 put_packet(s, buf);
1318 break;
1319 }
pbrook56aebc82008-10-11 17:55:29 +00001320 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1321 const char *xml;
1322 target_ulong total_len;
1323
Andreas Färber5b24c642013-07-07 15:08:22 +02001324 cc = CPU_GET_CLASS(first_cpu);
1325 if (cc->gdb_core_xml_file == NULL) {
1326 goto unknown_command;
1327 }
1328
Andreas Färber5b50e792013-06-29 04:18:45 +02001329 gdb_has_xml = true;
pbrook56aebc82008-10-11 17:55:29 +00001330 p += 19;
Andreas Färber5b24c642013-07-07 15:08:22 +02001331 xml = get_feature_xml(p, &p, cc);
pbrook56aebc82008-10-11 17:55:29 +00001332 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001333 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001334 put_packet(s, buf);
1335 break;
1336 }
1337
1338 if (*p == ':')
1339 p++;
1340 addr = strtoul(p, (char **)&p, 16);
1341 if (*p == ',')
1342 p++;
1343 len = strtoul(p, (char **)&p, 16);
1344
1345 total_len = strlen(xml);
1346 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001347 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001348 put_packet(s, buf);
1349 break;
1350 }
1351 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1352 len = (MAX_PACKET_LENGTH - 5) / 2;
1353 if (len < total_len - addr) {
1354 buf[0] = 'm';
1355 len = memtox(buf + 1, xml + addr, len);
1356 } else {
1357 buf[0] = 'l';
1358 len = memtox(buf + 1, xml + addr, total_len - addr);
1359 }
1360 put_packet_binary(s, buf, len + 1);
1361 break;
1362 }
Jan Kiszkaa3919382015-02-07 09:38:44 +01001363 if (is_query_packet(p, "Attached", ':')) {
1364 put_packet(s, GDB_ATTACHED);
1365 break;
1366 }
pbrook56aebc82008-10-11 17:55:29 +00001367 /* Unrecognised 'q' command. */
1368 goto unknown_command;
1369
bellard858693c2004-03-31 18:52:07 +00001370 default:
pbrook56aebc82008-10-11 17:55:29 +00001371 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001372 /* put empty packet */
1373 buf[0] = '\0';
1374 put_packet(s, buf);
1375 break;
1376 }
1377 return RS_IDLE;
1378}
1379
Andreas Färber64f6b342013-05-27 02:06:09 +02001380void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001381{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001382 gdbserver_state->c_cpu = cpu;
1383 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001384}
1385
bellard1fddef42005-04-17 19:16:13 +00001386#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001387static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001388{
aliguori880a7572008-11-18 20:30:24 +00001389 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001390 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001391 char buf[256];
aliguorid6fc1b32008-11-18 19:55:44 +00001392 const char *type;
bellard858693c2004-03-31 18:52:07 +00001393 int ret;
1394
Meador Ingecdb432b2012-03-15 17:49:45 +00001395 if (running || s->state == RS_INACTIVE) {
1396 return;
1397 }
1398 /* Is there a GDB syscall waiting to be sent? */
1399 if (s->current_syscall_cb) {
1400 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001401 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001402 }
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001403 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001404 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02001405 if (cpu->watchpoint_hit) {
1406 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001407 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001408 type = "r";
1409 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001410 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001411 type = "a";
1412 break;
1413 default:
1414 type = "";
1415 break;
1416 }
aliguori880a7572008-11-18 20:30:24 +00001417 snprintf(buf, sizeof(buf),
1418 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
Alex Bennéed2a6c852017-07-12 11:52:14 +01001419 GDB_SIGNAL_TRAP, cpu_gdb_index(cpu), type,
Andreas Färberff4700b2013-08-26 18:23:18 +02001420 (target_ulong)cpu->watchpoint_hit->vaddr);
1421 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001422 goto send_packet;
pbrook6658ffb2007-03-16 23:58:11 +00001423 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001424 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00001425 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001426 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001427 case RUN_STATE_PAUSED:
aliguori9781e042009-01-22 17:15:29 +00001428 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001429 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001430 case RUN_STATE_SHUTDOWN:
Jan Kiszka425189a2011-03-22 11:02:09 +01001431 ret = GDB_SIGNAL_QUIT;
1432 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001433 case RUN_STATE_IO_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001434 ret = GDB_SIGNAL_IO;
1435 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001436 case RUN_STATE_WATCHDOG:
Jan Kiszka425189a2011-03-22 11:02:09 +01001437 ret = GDB_SIGNAL_ALRM;
1438 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001439 case RUN_STATE_INTERNAL_ERROR:
Jan Kiszka425189a2011-03-22 11:02:09 +01001440 ret = GDB_SIGNAL_ABRT;
1441 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001442 case RUN_STATE_SAVE_VM:
1443 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001444 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001445 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001446 ret = GDB_SIGNAL_XCPU;
1447 break;
1448 default:
1449 ret = GDB_SIGNAL_UNKNOWN;
1450 break;
bellardbbeb7b52006-04-23 18:42:15 +00001451 }
Jan Kiszka226d0072015-07-24 18:52:31 +02001452 gdb_set_stop_cpu(cpu);
Alex Bennéed2a6c852017-07-12 11:52:14 +01001453 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_gdb_index(cpu));
Jan Kiszka425189a2011-03-22 11:02:09 +01001454
1455send_packet:
bellard858693c2004-03-31 18:52:07 +00001456 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001457
1458 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001459 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001460}
bellard1fddef42005-04-17 19:16:13 +00001461#endif
bellard858693c2004-03-31 18:52:07 +00001462
pbrooka2d1eba2007-01-28 03:10:55 +00001463/* Send a gdb syscall request.
1464 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001465 %x - target_ulong argument printed in hex.
1466 %lx - 64-bit argument printed in hex.
1467 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01001468void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00001469{
pbrooka2d1eba2007-01-28 03:10:55 +00001470 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001471 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001472 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001473 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001474 GDBState *s;
1475
aliguori880a7572008-11-18 20:30:24 +00001476 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001477 if (!s)
1478 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001479 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001480#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001481 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001482#endif
Meador Ingecdb432b2012-03-15 17:49:45 +00001483 p = s->syscall_buf;
1484 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001485 *(p++) = 'F';
1486 while (*fmt) {
1487 if (*fmt == '%') {
1488 fmt++;
1489 switch (*fmt++) {
1490 case 'x':
1491 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001492 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001493 break;
pbrooka87295e2007-05-26 15:09:38 +00001494 case 'l':
1495 if (*(fmt++) != 'x')
1496 goto bad_format;
1497 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001498 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001499 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001500 case 's':
1501 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001502 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001503 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001504 break;
1505 default:
pbrooka87295e2007-05-26 15:09:38 +00001506 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001507 error_report("gdbstub: Bad syscall format string '%s'",
1508 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00001509 break;
1510 }
1511 } else {
1512 *(p++) = *(fmt++);
1513 }
1514 }
pbrook8a93e022007-08-06 13:19:15 +00001515 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001516#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001517 put_packet(s, s->syscall_buf);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001518 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001519#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001520 /* In this case wait to send the syscall packet until notification that
1521 the CPU has stopped. This must be done because if the packet is sent
1522 now the reply from the syscall request could be received while the CPU
1523 is still in the running state, which can cause packets to be dropped
1524 and state transition 'T' packets to be sent while the syscall is still
1525 being processed. */
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001526 qemu_cpu_kick(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00001527#endif
1528}
1529
Peter Maydell19239b32015-09-07 10:39:27 +01001530void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
1531{
1532 va_list va;
1533
1534 va_start(va, fmt);
1535 gdb_do_syscallv(cb, fmt, va);
1536 va_end(va);
1537}
1538
bellard6a00d602005-11-21 23:25:50 +00001539static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00001540{
ths60fe76f2007-12-16 03:02:09 +00001541 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00001542
bellard1fddef42005-04-17 19:16:13 +00001543#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001544 if (s->last_packet_len) {
1545 /* Waiting for a response to the last packet. If we see the start
1546 of a new command then abandon the previous response. */
1547 if (ch == '-') {
Alex Bennée118e2262017-07-12 11:52:13 +01001548 gdb_debug("Got NACK, retransmitting\n");
thsffe8ab82007-12-16 03:16:05 +00001549 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
Alex Bennée118e2262017-07-12 11:52:13 +01001550 } else if (ch == '+') {
1551 gdb_debug("Got ACK\n");
1552 } else {
1553 gdb_debug("Got '%c' when expecting ACK/NACK\n", ch);
pbrook4046d912007-01-28 01:53:16 +00001554 }
Alex Bennée118e2262017-07-12 11:52:13 +01001555
pbrook4046d912007-01-28 01:53:16 +00001556 if (ch == '+' || ch == '$')
1557 s->last_packet_len = 0;
1558 if (ch != '$')
1559 return;
1560 }
Luiz Capitulino13548692011-07-29 15:36:43 -03001561 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00001562 /* when the CPU is running, we cannot do anything except stop
1563 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001564 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00001565 } else
bellard1fddef42005-04-17 19:16:13 +00001566#endif
bellard41625032005-04-24 10:07:11 +00001567 {
bellard858693c2004-03-31 18:52:07 +00001568 switch(s->state) {
1569 case RS_IDLE:
1570 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04001571 /* start of command packet */
bellard858693c2004-03-31 18:52:07 +00001572 s->line_buf_index = 0;
Doug Gale4bf43122017-05-01 12:22:10 -04001573 s->line_sum = 0;
bellard858693c2004-03-31 18:52:07 +00001574 s->state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04001575 } else {
Alex Bennée118e2262017-07-12 11:52:13 +01001576 gdb_debug("received garbage between packets: 0x%x\n", ch);
bellard4c3a88a2003-07-26 12:06:08 +00001577 }
1578 break;
bellard858693c2004-03-31 18:52:07 +00001579 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04001580 if (ch == '}') {
1581 /* start escape sequence */
1582 s->state = RS_GETLINE_ESC;
1583 s->line_sum += ch;
1584 } else if (ch == '*') {
1585 /* start run length encoding sequence */
1586 s->state = RS_GETLINE_RLE;
1587 s->line_sum += ch;
1588 } else if (ch == '#') {
1589 /* end of command, start of checksum*/
1590 s->state = RS_CHKSUM1;
bellard858693c2004-03-31 18:52:07 +00001591 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
Alex Bennée118e2262017-07-12 11:52:13 +01001592 gdb_debug("command buffer overrun, dropping command\n");
bellard858693c2004-03-31 18:52:07 +00001593 s->state = RS_IDLE;
1594 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04001595 /* unescaped command character */
1596 s->line_buf[s->line_buf_index++] = ch;
1597 s->line_sum += ch;
1598 }
1599 break;
1600 case RS_GETLINE_ESC:
1601 if (ch == '#') {
1602 /* unexpected end of command in escape sequence */
1603 s->state = RS_CHKSUM1;
1604 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1605 /* command buffer overrun */
Alex Bennée118e2262017-07-12 11:52:13 +01001606 gdb_debug("command buffer overrun, dropping command\n");
Doug Gale4bf43122017-05-01 12:22:10 -04001607 s->state = RS_IDLE;
1608 } else {
1609 /* parse escaped character and leave escape state */
1610 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
1611 s->line_sum += ch;
1612 s->state = RS_GETLINE;
1613 }
1614 break;
1615 case RS_GETLINE_RLE:
1616 if (ch < ' ') {
1617 /* invalid RLE count encoding */
Alex Bennée118e2262017-07-12 11:52:13 +01001618 gdb_debug("got invalid RLE count: 0x%x\n", ch);
Doug Gale4bf43122017-05-01 12:22:10 -04001619 s->state = RS_GETLINE;
1620 } else {
1621 /* decode repeat length */
1622 int repeat = (unsigned char)ch - ' ' + 3;
1623 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
1624 /* that many repeats would overrun the command buffer */
Alex Bennée118e2262017-07-12 11:52:13 +01001625 gdb_debug("command buffer overrun, dropping command\n");
Doug Gale4bf43122017-05-01 12:22:10 -04001626 s->state = RS_IDLE;
1627 } else if (s->line_buf_index < 1) {
1628 /* got a repeat but we have nothing to repeat */
Alex Bennée118e2262017-07-12 11:52:13 +01001629 gdb_debug("got invalid RLE sequence\n");
Doug Gale4bf43122017-05-01 12:22:10 -04001630 s->state = RS_GETLINE;
1631 } else {
1632 /* repeat the last character */
1633 memset(s->line_buf + s->line_buf_index,
1634 s->line_buf[s->line_buf_index - 1], repeat);
1635 s->line_buf_index += repeat;
1636 s->line_sum += ch;
1637 s->state = RS_GETLINE;
1638 }
bellard858693c2004-03-31 18:52:07 +00001639 }
1640 break;
1641 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04001642 /* get high hex digit of checksum */
1643 if (!isxdigit(ch)) {
Alex Bennée118e2262017-07-12 11:52:13 +01001644 gdb_debug("got invalid command checksum digit\n");
Doug Gale4bf43122017-05-01 12:22:10 -04001645 s->state = RS_GETLINE;
1646 break;
1647 }
bellard858693c2004-03-31 18:52:07 +00001648 s->line_buf[s->line_buf_index] = '\0';
1649 s->line_csum = fromhex(ch) << 4;
1650 s->state = RS_CHKSUM2;
1651 break;
1652 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04001653 /* get low hex digit of checksum */
1654 if (!isxdigit(ch)) {
Alex Bennée118e2262017-07-12 11:52:13 +01001655 gdb_debug("got invalid command checksum digit\n");
Doug Gale4bf43122017-05-01 12:22:10 -04001656 s->state = RS_GETLINE;
1657 break;
bellard858693c2004-03-31 18:52:07 +00001658 }
Doug Gale4bf43122017-05-01 12:22:10 -04001659 s->line_csum |= fromhex(ch);
1660
1661 if (s->line_csum != (s->line_sum & 0xff)) {
Alex Bennée118e2262017-07-12 11:52:13 +01001662 gdb_debug("got command packet with incorrect checksum\n");
Doug Gale4bf43122017-05-01 12:22:10 -04001663 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00001664 reply = '-';
1665 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00001666 s->state = RS_IDLE;
1667 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04001668 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00001669 reply = '+';
1670 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00001671 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00001672 }
bellardb4608c02003-06-27 17:34:32 +00001673 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001674 default:
1675 abort();
bellardb4608c02003-06-27 17:34:32 +00001676 }
1677 }
bellard858693c2004-03-31 18:52:07 +00001678}
1679
Paul Brook0e1c9c52010-06-16 13:03:51 +01001680/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001681void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01001682{
1683 GDBState *s;
1684 char buf[4];
1685
1686 s = gdbserver_state;
1687 if (!s) {
1688 return;
1689 }
1690#ifdef CONFIG_USER_ONLY
1691 if (gdbserver_fd < 0 || s->fd < 0) {
1692 return;
1693 }
1694#endif
1695
1696 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
1697 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001698
1699#ifndef CONFIG_USER_ONLY
Marc-André Lureau1ce26102017-01-27 00:49:13 +04001700 qemu_chr_fe_deinit(&s->chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001701#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01001702}
1703
bellard1fddef42005-04-17 19:16:13 +00001704#ifdef CONFIG_USER_ONLY
1705int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001706gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00001707{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001708 GDBState *s;
1709 char buf[256];
1710 int n;
bellard1fddef42005-04-17 19:16:13 +00001711
Andreas Färber5ca666c2013-06-24 19:20:57 +02001712 s = gdbserver_state;
1713 if (gdbserver_fd < 0 || s->fd < 0) {
1714 return sig;
bellard1fddef42005-04-17 19:16:13 +00001715 }
1716
Andreas Färber5ca666c2013-06-24 19:20:57 +02001717 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001718 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001719 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00001720
Andreas Färber5ca666c2013-06-24 19:20:57 +02001721 if (sig != 0) {
1722 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
1723 put_packet(s, buf);
1724 }
1725 /* put_packet() might have detected that the peer terminated the
1726 connection. */
1727 if (s->fd < 0) {
1728 return sig;
1729 }
1730
1731 sig = 0;
1732 s->state = RS_IDLE;
1733 s->running_state = 0;
1734 while (s->running_state == 0) {
1735 n = read(s->fd, buf, 256);
1736 if (n > 0) {
1737 int i;
1738
1739 for (i = 0; i < n; i++) {
1740 gdb_read_byte(s, buf[i]);
1741 }
Peter Wu5819e3e2016-06-05 16:35:48 +02001742 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02001743 /* XXX: Connection closed. Should probably wait for another
1744 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02001745 if (n == 0) {
1746 close(s->fd);
1747 }
1748 s->fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02001749 return sig;
bellard1fddef42005-04-17 19:16:13 +00001750 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02001751 }
1752 sig = s->signal;
1753 s->signal = 0;
1754 return sig;
bellard1fddef42005-04-17 19:16:13 +00001755}
bellarde9009672005-04-26 20:42:36 +00001756
aurel32ca587a82008-12-18 22:44:13 +00001757/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001758void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00001759{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001760 GDBState *s;
1761 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00001762
Andreas Färber5ca666c2013-06-24 19:20:57 +02001763 s = gdbserver_state;
1764 if (gdbserver_fd < 0 || s->fd < 0) {
1765 return;
1766 }
aurel32ca587a82008-12-18 22:44:13 +00001767
Andreas Färber5ca666c2013-06-24 19:20:57 +02001768 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
1769 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00001770}
bellard1fddef42005-04-17 19:16:13 +00001771
aliguori880a7572008-11-18 20:30:24 +00001772static void gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00001773{
1774 GDBState *s;
1775 struct sockaddr_in sockaddr;
1776 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001777 int fd;
bellard858693c2004-03-31 18:52:07 +00001778
1779 for(;;) {
1780 len = sizeof(sockaddr);
1781 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1782 if (fd < 0 && errno != EINTR) {
1783 perror("accept");
1784 return;
1785 } else if (fd >= 0) {
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001786#ifndef _WIN32
1787 fcntl(fd, F_SETFD, FD_CLOEXEC);
1788#endif
bellard858693c2004-03-31 18:52:07 +00001789 break;
1790 }
1791 }
1792
1793 /* set short latency */
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001794 socket_set_nodelay(fd);
ths3b46e622007-09-17 08:09:54 +00001795
Anthony Liguori7267c092011-08-20 22:09:37 -05001796 s = g_malloc0(sizeof(GDBState));
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001797 s->c_cpu = first_cpu;
1798 s->g_cpu = first_cpu;
bellard858693c2004-03-31 18:52:07 +00001799 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02001800 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00001801
aliguori880a7572008-11-18 20:30:24 +00001802 gdbserver_state = s;
bellard858693c2004-03-31 18:52:07 +00001803}
1804
1805static int gdbserver_open(int port)
1806{
1807 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001808 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00001809
1810 fd = socket(PF_INET, SOCK_STREAM, 0);
1811 if (fd < 0) {
1812 perror("socket");
1813 return -1;
1814 }
Kevin Wolf40ff6d72009-12-02 12:24:42 +01001815#ifndef _WIN32
1816 fcntl(fd, F_SETFD, FD_CLOEXEC);
1817#endif
bellard858693c2004-03-31 18:52:07 +00001818
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001819 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00001820
1821 sockaddr.sin_family = AF_INET;
1822 sockaddr.sin_port = htons(port);
1823 sockaddr.sin_addr.s_addr = 0;
1824 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1825 if (ret < 0) {
1826 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00001827 close(fd);
bellard858693c2004-03-31 18:52:07 +00001828 return -1;
1829 }
Peter Wu96165b92016-05-04 11:32:17 +02001830 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00001831 if (ret < 0) {
1832 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00001833 close(fd);
bellard858693c2004-03-31 18:52:07 +00001834 return -1;
1835 }
bellard858693c2004-03-31 18:52:07 +00001836 return fd;
1837}
1838
1839int gdbserver_start(int port)
1840{
1841 gdbserver_fd = gdbserver_open(port);
1842 if (gdbserver_fd < 0)
1843 return -1;
1844 /* accept connections */
aliguori880a7572008-11-18 20:30:24 +00001845 gdb_accept();
bellardb4608c02003-06-27 17:34:32 +00001846 return 0;
1847}
aurel322b1319c2008-12-18 22:44:04 +00001848
1849/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07001850void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00001851{
1852 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02001853
1854 if (gdbserver_fd < 0 || s->fd < 0) {
1855 return;
1856 }
aurel322b1319c2008-12-18 22:44:04 +00001857 close(s->fd);
1858 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001859 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02001860 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00001861}
pbrook4046d912007-01-28 01:53:16 +00001862#else
thsaa1f17c2007-07-11 22:48:58 +00001863static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00001864{
pbrook56aebc82008-10-11 17:55:29 +00001865 /* We can handle an arbitrarily large amount of data.
1866 Pick the maximum packet size, which is as good as anything. */
1867 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00001868}
1869
thsaa1f17c2007-07-11 22:48:58 +00001870static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00001871{
pbrook4046d912007-01-28 01:53:16 +00001872 int i;
1873
1874 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00001875 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00001876 }
1877}
1878
1879static void gdb_chr_event(void *opaque, int event)
1880{
1881 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05301882 case CHR_EVENT_OPENED:
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001883 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02001884 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00001885 break;
1886 default:
1887 break;
1888 }
1889}
1890
aliguori8a34a0f2009-03-05 23:01:55 +00001891static void gdb_monitor_output(GDBState *s, const char *msg, int len)
1892{
1893 char buf[MAX_PACKET_LENGTH];
1894
1895 buf[0] = 'O';
1896 if (len > (MAX_PACKET_LENGTH/2) - 1)
1897 len = (MAX_PACKET_LENGTH/2) - 1;
1898 memtohex(buf + 1, (uint8_t *)msg, len);
1899 put_packet(s, buf);
1900}
1901
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001902static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00001903{
1904 const char *p = (const char *)buf;
1905 int max_sz;
1906
1907 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
1908 for (;;) {
1909 if (len <= max_sz) {
1910 gdb_monitor_output(gdbserver_state, p, len);
1911 break;
1912 }
1913 gdb_monitor_output(gdbserver_state, p, max_sz);
1914 p += max_sz;
1915 len -= max_sz;
1916 }
1917 return len;
1918}
1919
aliguori59030a82009-04-05 18:43:41 +00001920#ifndef _WIN32
1921static void gdb_sigterm_handler(int signal)
1922{
Luiz Capitulino13548692011-07-29 15:36:43 -03001923 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001924 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001925 }
aliguori59030a82009-04-05 18:43:41 +00001926}
1927#endif
1928
Marc-André Lureau777357d2016-12-07 18:39:10 +03001929static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
1930 bool *be_opened, Error **errp)
1931{
1932 *be_opened = false;
1933}
1934
1935static void char_gdb_class_init(ObjectClass *oc, void *data)
1936{
1937 ChardevClass *cc = CHARDEV_CLASS(oc);
1938
1939 cc->internal = true;
1940 cc->open = gdb_monitor_open;
1941 cc->chr_write = gdb_monitor_write;
1942}
1943
1944#define TYPE_CHARDEV_GDB "chardev-gdb"
1945
1946static const TypeInfo char_gdb_type_info = {
1947 .name = TYPE_CHARDEV_GDB,
1948 .parent = TYPE_CHARDEV,
1949 .class_init = char_gdb_class_init,
1950};
1951
aliguori59030a82009-04-05 18:43:41 +00001952int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00001953{
1954 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00001955 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001956 Chardev *chr = NULL;
1957 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00001958
Ziyue Yang508b4ec2017-01-18 16:02:41 +08001959 if (!first_cpu) {
1960 error_report("gdbstub: meaningless to attach gdb to a "
1961 "machine without any CPU.");
1962 return -1;
1963 }
1964
aliguori59030a82009-04-05 18:43:41 +00001965 if (!device)
1966 return -1;
1967 if (strcmp(device, "none") != 0) {
1968 if (strstart(device, "tcp:", NULL)) {
1969 /* enforce required TCP attributes */
1970 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
1971 "%s,nowait,nodelay,server", device);
1972 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00001973 }
aliguori59030a82009-04-05 18:43:41 +00001974#ifndef _WIN32
1975 else if (strcmp(device, "stdio") == 0) {
1976 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00001977
aliguori59030a82009-04-05 18:43:41 +00001978 memset(&act, 0, sizeof(act));
1979 act.sa_handler = gdb_sigterm_handler;
1980 sigaction(SIGINT, &act, NULL);
1981 }
1982#endif
Marc-André Lureaub4948be2016-10-22 12:52:46 +03001983 chr = qemu_chr_new_noreplay("gdb", device);
aliguori36556b22009-03-28 18:05:53 +00001984 if (!chr)
1985 return -1;
pbrookcfc34752007-02-22 01:48:01 +00001986 }
1987
aliguori36556b22009-03-28 18:05:53 +00001988 s = gdbserver_state;
1989 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001990 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00001991 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00001992
aliguori36556b22009-03-28 18:05:53 +00001993 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
1994
1995 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03001996 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
1997 NULL, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00001998 monitor_init(mon_chr, 0);
1999 } else {
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002000 qemu_chr_fe_deinit(&s->chr, true);
aliguori36556b22009-03-28 18:05:53 +00002001 mon_chr = s->mon_chr;
2002 memset(s, 0, sizeof(GDBState));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002003 s->mon_chr = mon_chr;
aliguori36556b22009-03-28 18:05:53 +00002004 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002005 s->c_cpu = first_cpu;
2006 s->g_cpu = first_cpu;
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002007 if (chr) {
2008 qemu_chr_fe_init(&s->chr, chr, &error_abort);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002009 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
Anton Nefedov81517ba2017-07-06 15:08:49 +03002010 gdb_chr_event, NULL, NULL, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002011 }
aliguori36556b22009-03-28 18:05:53 +00002012 s->state = chr ? RS_IDLE : RS_INACTIVE;
2013 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00002014 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00002015
pbrook4046d912007-01-28 01:53:16 +00002016 return 0;
2017}
Marc-André Lureau777357d2016-12-07 18:39:10 +03002018
2019static void register_types(void)
2020{
2021 type_register_static(&char_gdb_type_info);
2022}
2023
2024type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00002025#endif