blob: 9ac6f19a186f99b4fb3a43a306d91cab4c2420aa [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"
Doug Gale5c9522b2017-12-02 20:30:37 -050023#include "trace-root.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"
Luc Michel8f468632019-01-07 15:23:45 +000032#include "hw/cpu/cluster.h"
bellard1fddef42005-04-17 19:16:13 +000033#endif
bellard67b915a2004-03-31 23:37:16 +000034
pbrook56aebc82008-10-11 17:55:29 +000035#define MAX_PACKET_LENGTH 4096
36
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010037#include "qemu/sockets.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010038#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010039#include "sysemu/kvm.h"
Leon Alraecfe67ce2015-06-19 14:17:45 +010040#include "exec/semihost.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010041#include "exec/exec-all.h"
aurel32ca587a82008-12-18 22:44:13 +000042
Jan Kiszkaa3919382015-02-07 09:38:44 +010043#ifdef CONFIG_USER_ONLY
44#define GDB_ATTACHED "0"
45#else
46#define GDB_ATTACHED "1"
47#endif
48
Andreas Färberf3659ee2013-06-27 19:09:09 +020049static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
50 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020051{
Andreas Färberf3659ee2013-06-27 19:09:09 +020052 CPUClass *cc = CPU_GET_CLASS(cpu);
53
54 if (cc->memory_rw_debug) {
55 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
56 }
57 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020058}
aurel32ca587a82008-12-18 22:44:13 +000059
Alex Bennéed2a6c852017-07-12 11:52:14 +010060/* Return the GDB index for a given vCPU state.
61 *
62 * For user mode this is simply the thread id. In system mode GDB
63 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
64 */
65static inline int cpu_gdb_index(CPUState *cpu)
66{
67#if defined(CONFIG_USER_ONLY)
Alex Bennéebd88c782017-07-12 11:52:15 +010068 TaskState *ts = (TaskState *) cpu->opaque;
69 return ts->ts_tid;
Alex Bennéed2a6c852017-07-12 11:52:14 +010070#else
71 return cpu->cpu_index + 1;
72#endif
73}
74
aurel32ca587a82008-12-18 22:44:13 +000075enum {
76 GDB_SIGNAL_0 = 0,
77 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +010078 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +000079 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +010080 GDB_SIGNAL_ABRT = 6,
81 GDB_SIGNAL_ALRM = 14,
82 GDB_SIGNAL_IO = 23,
83 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +000084 GDB_SIGNAL_UNKNOWN = 143
85};
86
87#ifdef CONFIG_USER_ONLY
88
89/* Map target signal numbers to GDB protocol signal numbers and vice
90 * versa. For user emulation's currently supported systems, we can
91 * assume most signals are defined.
92 */
93
94static int gdb_signal_table[] = {
95 0,
96 TARGET_SIGHUP,
97 TARGET_SIGINT,
98 TARGET_SIGQUIT,
99 TARGET_SIGILL,
100 TARGET_SIGTRAP,
101 TARGET_SIGABRT,
102 -1, /* SIGEMT */
103 TARGET_SIGFPE,
104 TARGET_SIGKILL,
105 TARGET_SIGBUS,
106 TARGET_SIGSEGV,
107 TARGET_SIGSYS,
108 TARGET_SIGPIPE,
109 TARGET_SIGALRM,
110 TARGET_SIGTERM,
111 TARGET_SIGURG,
112 TARGET_SIGSTOP,
113 TARGET_SIGTSTP,
114 TARGET_SIGCONT,
115 TARGET_SIGCHLD,
116 TARGET_SIGTTIN,
117 TARGET_SIGTTOU,
118 TARGET_SIGIO,
119 TARGET_SIGXCPU,
120 TARGET_SIGXFSZ,
121 TARGET_SIGVTALRM,
122 TARGET_SIGPROF,
123 TARGET_SIGWINCH,
124 -1, /* SIGLOST */
125 TARGET_SIGUSR1,
126 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000127#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000128 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000129#else
130 -1,
131#endif
aurel32ca587a82008-12-18 22:44:13 +0000132 -1, /* SIGPOLL */
133 -1,
134 -1,
135 -1,
136 -1,
137 -1,
138 -1,
139 -1,
140 -1,
141 -1,
142 -1,
143 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000144#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000145 __SIGRTMIN + 1,
146 __SIGRTMIN + 2,
147 __SIGRTMIN + 3,
148 __SIGRTMIN + 4,
149 __SIGRTMIN + 5,
150 __SIGRTMIN + 6,
151 __SIGRTMIN + 7,
152 __SIGRTMIN + 8,
153 __SIGRTMIN + 9,
154 __SIGRTMIN + 10,
155 __SIGRTMIN + 11,
156 __SIGRTMIN + 12,
157 __SIGRTMIN + 13,
158 __SIGRTMIN + 14,
159 __SIGRTMIN + 15,
160 __SIGRTMIN + 16,
161 __SIGRTMIN + 17,
162 __SIGRTMIN + 18,
163 __SIGRTMIN + 19,
164 __SIGRTMIN + 20,
165 __SIGRTMIN + 21,
166 __SIGRTMIN + 22,
167 __SIGRTMIN + 23,
168 __SIGRTMIN + 24,
169 __SIGRTMIN + 25,
170 __SIGRTMIN + 26,
171 __SIGRTMIN + 27,
172 __SIGRTMIN + 28,
173 __SIGRTMIN + 29,
174 __SIGRTMIN + 30,
175 __SIGRTMIN + 31,
176 -1, /* SIGCANCEL */
177 __SIGRTMIN,
178 __SIGRTMIN + 32,
179 __SIGRTMIN + 33,
180 __SIGRTMIN + 34,
181 __SIGRTMIN + 35,
182 __SIGRTMIN + 36,
183 __SIGRTMIN + 37,
184 __SIGRTMIN + 38,
185 __SIGRTMIN + 39,
186 __SIGRTMIN + 40,
187 __SIGRTMIN + 41,
188 __SIGRTMIN + 42,
189 __SIGRTMIN + 43,
190 __SIGRTMIN + 44,
191 __SIGRTMIN + 45,
192 __SIGRTMIN + 46,
193 __SIGRTMIN + 47,
194 __SIGRTMIN + 48,
195 __SIGRTMIN + 49,
196 __SIGRTMIN + 50,
197 __SIGRTMIN + 51,
198 __SIGRTMIN + 52,
199 __SIGRTMIN + 53,
200 __SIGRTMIN + 54,
201 __SIGRTMIN + 55,
202 __SIGRTMIN + 56,
203 __SIGRTMIN + 57,
204 __SIGRTMIN + 58,
205 __SIGRTMIN + 59,
206 __SIGRTMIN + 60,
207 __SIGRTMIN + 61,
208 __SIGRTMIN + 62,
209 __SIGRTMIN + 63,
210 __SIGRTMIN + 64,
211 __SIGRTMIN + 65,
212 __SIGRTMIN + 66,
213 __SIGRTMIN + 67,
214 __SIGRTMIN + 68,
215 __SIGRTMIN + 69,
216 __SIGRTMIN + 70,
217 __SIGRTMIN + 71,
218 __SIGRTMIN + 72,
219 __SIGRTMIN + 73,
220 __SIGRTMIN + 74,
221 __SIGRTMIN + 75,
222 __SIGRTMIN + 76,
223 __SIGRTMIN + 77,
224 __SIGRTMIN + 78,
225 __SIGRTMIN + 79,
226 __SIGRTMIN + 80,
227 __SIGRTMIN + 81,
228 __SIGRTMIN + 82,
229 __SIGRTMIN + 83,
230 __SIGRTMIN + 84,
231 __SIGRTMIN + 85,
232 __SIGRTMIN + 86,
233 __SIGRTMIN + 87,
234 __SIGRTMIN + 88,
235 __SIGRTMIN + 89,
236 __SIGRTMIN + 90,
237 __SIGRTMIN + 91,
238 __SIGRTMIN + 92,
239 __SIGRTMIN + 93,
240 __SIGRTMIN + 94,
241 __SIGRTMIN + 95,
242 -1, /* SIGINFO */
243 -1, /* UNKNOWN */
244 -1, /* DEFAULT */
245 -1,
246 -1,
247 -1,
248 -1,
249 -1,
250 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000251#endif
aurel32ca587a82008-12-18 22:44:13 +0000252};
bellard8f447cc2006-06-14 15:21:14 +0000253#else
aurel32ca587a82008-12-18 22:44:13 +0000254/* In system mode we only need SIGINT and SIGTRAP; other signals
255 are not yet supported. */
256
257enum {
258 TARGET_SIGINT = 2,
259 TARGET_SIGTRAP = 5
260};
261
262static int gdb_signal_table[] = {
263 -1,
264 -1,
265 TARGET_SIGINT,
266 -1,
267 -1,
268 TARGET_SIGTRAP
269};
bellard8f447cc2006-06-14 15:21:14 +0000270#endif
bellardb4608c02003-06-27 17:34:32 +0000271
aurel32ca587a82008-12-18 22:44:13 +0000272#ifdef CONFIG_USER_ONLY
273static int target_signal_to_gdb (int sig)
274{
275 int i;
276 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
277 if (gdb_signal_table[i] == sig)
278 return i;
279 return GDB_SIGNAL_UNKNOWN;
280}
281#endif
282
283static int gdb_signal_to_target (int sig)
284{
285 if (sig < ARRAY_SIZE (gdb_signal_table))
286 return gdb_signal_table[sig];
287 else
288 return -1;
289}
290
pbrook56aebc82008-10-11 17:55:29 +0000291typedef struct GDBRegisterState {
292 int base_reg;
293 int num_regs;
294 gdb_reg_cb get_reg;
295 gdb_reg_cb set_reg;
296 const char *xml;
297 struct GDBRegisterState *next;
298} GDBRegisterState;
299
Luc Michel8f468632019-01-07 15:23:45 +0000300typedef struct GDBProcess {
301 uint32_t pid;
302 bool attached;
303} GDBProcess;
304
bellard858693c2004-03-31 18:52:07 +0000305enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000306 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000307 RS_IDLE,
308 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400309 RS_GETLINE_ESC,
310 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000311 RS_CHKSUM1,
312 RS_CHKSUM2,
313};
bellard858693c2004-03-31 18:52:07 +0000314typedef struct GDBState {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200315 CPUState *c_cpu; /* current CPU for step/continue ops */
316 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200317 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000318 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000319 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000320 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400321 int line_sum; /* running checksum */
322 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000323 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000324 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000325 int signal;
bellard41625032005-04-24 10:07:11 +0000326#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000327 int fd;
bellard41625032005-04-24 10:07:11 +0000328 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000329#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300330 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300331 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000332#endif
Luc Michel8f468632019-01-07 15:23:45 +0000333 bool multiprocess;
334 GDBProcess *processes;
335 int process_num;
Meador Ingecdb432b2012-03-15 17:49:45 +0000336 char syscall_buf[256];
337 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000338} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000339
edgar_igl60897d32008-05-09 08:25:14 +0000340/* By default use no IRQs and no timers while single stepping so as to
341 * make single stepping like an ICE HW step.
342 */
343static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
344
aliguori880a7572008-11-18 20:30:24 +0000345static GDBState *gdbserver_state;
346
Andreas Färber5b50e792013-06-29 04:18:45 +0200347bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000348
bellard1fddef42005-04-17 19:16:13 +0000349#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000350/* XXX: This is not thread safe. Do we care? */
351static int gdbserver_fd = -1;
352
bellard858693c2004-03-31 18:52:07 +0000353static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000354{
355 uint8_t ch;
356 int ret;
357
358 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000359 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000360 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000361 if (errno == ECONNRESET)
362 s->fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200363 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000364 return -1;
365 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000366 close(s->fd);
367 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000368 return -1;
369 } else {
370 break;
371 }
372 }
373 return ch;
374}
pbrook4046d912007-01-28 01:53:16 +0000375#endif
bellardb4608c02003-06-27 17:34:32 +0000376
blueswir1654efcf2009-04-18 07:29:59 +0000377static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000378 GDB_SYS_UNKNOWN,
379 GDB_SYS_ENABLED,
380 GDB_SYS_DISABLED,
381} gdb_syscall_mode;
382
Liviu Ionescua38bb072014-12-11 12:07:48 +0000383/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000384int use_gdb_syscalls(void)
385{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100386 SemihostingTarget target = semihosting_get_target();
387 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000388 /* -semihosting-config target=native */
389 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100390 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000391 /* -semihosting-config target=gdb */
392 return true;
393 }
394
395 /* -semihosting-config target=auto */
396 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000397 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000398 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
399 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000400 }
401 return gdb_syscall_mode == GDB_SYS_ENABLED;
402}
403
edgar_iglba70a622008-03-14 06:10:42 +0000404/* Resume execution. */
405static inline void gdb_continue(GDBState *s)
406{
Doug Gale5c9522b2017-12-02 20:30:37 -0500407
edgar_iglba70a622008-03-14 06:10:42 +0000408#ifdef CONFIG_USER_ONLY
409 s->running_state = 1;
Doug Gale5c9522b2017-12-02 20:30:37 -0500410 trace_gdbstub_op_continue();
edgar_iglba70a622008-03-14 06:10:42 +0000411#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200412 if (!runstate_needs_reset()) {
Doug Gale5c9522b2017-12-02 20:30:37 -0500413 trace_gdbstub_op_continue();
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200414 vm_start();
415 }
edgar_iglba70a622008-03-14 06:10:42 +0000416#endif
417}
418
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100419/*
420 * Resume execution, per CPU actions. For user-mode emulation it's
421 * equivalent to gdb_continue.
422 */
423static int gdb_continue_partial(GDBState *s, char *newstates)
424{
425 CPUState *cpu;
426 int res = 0;
427#ifdef CONFIG_USER_ONLY
428 /*
429 * This is not exactly accurate, but it's an improvement compared to the
430 * previous situation, where only one CPU would be single-stepped.
431 */
432 CPU_FOREACH(cpu) {
433 if (newstates[cpu->cpu_index] == 's') {
Doug Gale5c9522b2017-12-02 20:30:37 -0500434 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100435 cpu_single_step(cpu, sstep_flags);
436 }
437 }
438 s->running_state = 1;
439#else
440 int flag = 0;
441
442 if (!runstate_needs_reset()) {
443 if (vm_prepare_start()) {
444 return 0;
445 }
446
447 CPU_FOREACH(cpu) {
448 switch (newstates[cpu->cpu_index]) {
449 case 0:
450 case 1:
451 break; /* nothing to do here */
452 case 's':
Doug Gale5c9522b2017-12-02 20:30:37 -0500453 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100454 cpu_single_step(cpu, sstep_flags);
455 cpu_resume(cpu);
456 flag = 1;
457 break;
458 case 'c':
Doug Gale5c9522b2017-12-02 20:30:37 -0500459 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100460 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
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -0300518/* writes 2*len+1 bytes in buf */
bellardb4608c02003-06-27 17:34:32 +0000519static void memtohex(char *buf, const uint8_t *mem, int len)
520{
521 int i, c;
522 char *q;
523 q = buf;
524 for(i = 0; i < len; i++) {
525 c = mem[i];
526 *q++ = tohex(c >> 4);
527 *q++ = tohex(c & 0xf);
528 }
529 *q = '\0';
530}
531
532static void hextomem(uint8_t *mem, const char *buf, int len)
533{
534 int i;
535
536 for(i = 0; i < len; i++) {
537 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
538 buf += 2;
539 }
540}
541
Doug Gale5c9522b2017-12-02 20:30:37 -0500542static void hexdump(const char *buf, int len,
543 void (*trace_fn)(size_t ofs, char const *text))
544{
545 char line_buffer[3 * 16 + 4 + 16 + 1];
546
547 size_t i;
548 for (i = 0; i < len || (i & 0xF); ++i) {
549 size_t byte_ofs = i & 15;
550
551 if (byte_ofs == 0) {
552 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
553 line_buffer[3 * 16 + 4 + 16] = 0;
554 }
555
556 size_t col_group = (i >> 2) & 3;
557 size_t hex_col = byte_ofs * 3 + col_group;
558 size_t txt_col = 3 * 16 + 4 + byte_ofs;
559
560 if (i < len) {
561 char value = buf[i];
562
563 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
564 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
565 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
566 ? value
567 : '.';
568 }
569
570 if (byte_ofs == 0xF)
571 trace_fn(i & -16, line_buffer);
572 }
573}
574
bellardb4608c02003-06-27 17:34:32 +0000575/* return -1 if error, 0 if OK */
Doug Gale5c9522b2017-12-02 20:30:37 -0500576static int put_packet_binary(GDBState *s, const char *buf, int len, bool dump)
bellardb4608c02003-06-27 17:34:32 +0000577{
pbrook56aebc82008-10-11 17:55:29 +0000578 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000579 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000580
Doug Gale5c9522b2017-12-02 20:30:37 -0500581 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
582 hexdump(buf, len, trace_gdbstub_io_binaryreply);
583 }
584
bellardb4608c02003-06-27 17:34:32 +0000585 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000586 p = s->last_packet;
587 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000588 memcpy(p, buf, len);
589 p += len;
bellardb4608c02003-06-27 17:34:32 +0000590 csum = 0;
591 for(i = 0; i < len; i++) {
592 csum += buf[i];
593 }
pbrook4046d912007-01-28 01:53:16 +0000594 *(p++) = '#';
595 *(p++) = tohex((csum >> 4) & 0xf);
596 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000597
pbrook4046d912007-01-28 01:53:16 +0000598 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000599 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000600
pbrook4046d912007-01-28 01:53:16 +0000601#ifdef CONFIG_USER_ONLY
602 i = get_char(s);
603 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000604 return -1;
pbrook4046d912007-01-28 01:53:16 +0000605 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000606 break;
pbrook4046d912007-01-28 01:53:16 +0000607#else
608 break;
609#endif
bellardb4608c02003-06-27 17:34:32 +0000610 }
611 return 0;
612}
613
pbrook56aebc82008-10-11 17:55:29 +0000614/* return -1 if error, 0 if OK */
615static int put_packet(GDBState *s, const char *buf)
616{
Doug Gale5c9522b2017-12-02 20:30:37 -0500617 trace_gdbstub_io_reply(buf);
pbrook56aebc82008-10-11 17:55:29 +0000618
Doug Gale5c9522b2017-12-02 20:30:37 -0500619 return put_packet_binary(s, buf, strlen(buf), false);
pbrook56aebc82008-10-11 17:55:29 +0000620}
621
pbrook56aebc82008-10-11 17:55:29 +0000622/* Encode data using the encoding for 'x' packets. */
623static int memtox(char *buf, const char *mem, int len)
624{
625 char *p = buf;
626 char c;
627
628 while (len--) {
629 c = *(mem++);
630 switch (c) {
631 case '#': case '$': case '*': case '}':
632 *(p++) = '}';
633 *(p++) = c ^ 0x20;
634 break;
635 default:
636 *(p++) = c;
637 break;
638 }
639 }
640 return p - buf;
641}
642
Andreas Färber5b24c642013-07-07 15:08:22 +0200643static const char *get_feature_xml(const char *p, const char **newp,
644 CPUClass *cc)
pbrook56aebc82008-10-11 17:55:29 +0000645{
pbrook56aebc82008-10-11 17:55:29 +0000646 size_t len;
647 int i;
648 const char *name;
649 static char target_xml[1024];
650
651 len = 0;
652 while (p[len] && p[len] != ':')
653 len++;
654 *newp = p + len;
655
656 name = NULL;
657 if (strncmp(p, "target.xml", len) == 0) {
658 /* Generate the XML description for this CPU. */
659 if (!target_xml[0]) {
660 GDBRegisterState *r;
Andreas Färbereac8b352013-06-28 21:11:37 +0200661 CPUState *cpu = first_cpu;
pbrook56aebc82008-10-11 17:55:29 +0000662
David Hildenbrandb3820e62015-12-03 13:14:41 +0100663 pstrcat(target_xml, sizeof(target_xml),
664 "<?xml version=\"1.0\"?>"
665 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
666 "<target>");
667 if (cc->gdb_arch_name) {
668 gchar *arch = cc->gdb_arch_name(cpu);
669 pstrcat(target_xml, sizeof(target_xml), "<architecture>");
670 pstrcat(target_xml, sizeof(target_xml), arch);
671 pstrcat(target_xml, sizeof(target_xml), "</architecture>");
672 g_free(arch);
673 }
674 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
675 pstrcat(target_xml, sizeof(target_xml), cc->gdb_core_xml_file);
676 pstrcat(target_xml, sizeof(target_xml), "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200677 for (r = cpu->gdb_regs; r; r = r->next) {
blueswir12dc766d2009-04-13 16:06:19 +0000678 pstrcat(target_xml, sizeof(target_xml), "<xi:include href=\"");
679 pstrcat(target_xml, sizeof(target_xml), r->xml);
680 pstrcat(target_xml, sizeof(target_xml), "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000681 }
blueswir12dc766d2009-04-13 16:06:19 +0000682 pstrcat(target_xml, sizeof(target_xml), "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000683 }
684 return target_xml;
685 }
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100686 if (cc->gdb_get_dynamic_xml) {
687 CPUState *cpu = first_cpu;
688 char *xmlname = g_strndup(p, len);
689 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
690
691 g_free(xmlname);
692 if (xml) {
693 return xml;
694 }
695 }
pbrook56aebc82008-10-11 17:55:29 +0000696 for (i = 0; ; i++) {
697 name = xml_builtin[i][0];
698 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
699 break;
700 }
701 return name ? xml_builtin[i][1] : NULL;
702}
pbrook56aebc82008-10-11 17:55:29 +0000703
Andreas Färber385b9f02013-06-27 18:25:36 +0200704static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000705{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200706 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200707 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000708 GDBRegisterState *r;
709
Andreas Färbera0e372f2013-06-28 23:18:47 +0200710 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200711 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200712 }
pbrook56aebc82008-10-11 17:55:29 +0000713
Andreas Färbereac8b352013-06-28 21:11:37 +0200714 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000715 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
716 return r->get_reg(env, mem_buf, reg - r->base_reg);
717 }
718 }
719 return 0;
720}
721
Andreas Färber385b9f02013-06-27 18:25:36 +0200722static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000723{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200724 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200725 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000726 GDBRegisterState *r;
727
Andreas Färbera0e372f2013-06-28 23:18:47 +0200728 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200729 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200730 }
pbrook56aebc82008-10-11 17:55:29 +0000731
Andreas Färbereac8b352013-06-28 21:11:37 +0200732 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000733 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
734 return r->set_reg(env, mem_buf, reg - r->base_reg);
735 }
736 }
737 return 0;
738}
739
740/* Register a supplemental set of CPU registers. If g_pos is nonzero it
741 specifies the first register number and these registers are included in
742 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
743 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
744 */
745
Andreas Färber22169d42013-06-28 21:27:39 +0200746void gdb_register_coprocessor(CPUState *cpu,
747 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
748 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000749{
750 GDBRegisterState *s;
751 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000752
Andreas Färbereac8b352013-06-28 21:11:37 +0200753 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000754 while (*p) {
755 /* Check for duplicates. */
756 if (strcmp((*p)->xml, xml) == 0)
757 return;
758 p = &(*p)->next;
759 }
Stefan Weil9643c252011-10-18 22:25:38 +0200760
761 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200762 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200763 s->num_regs = num_regs;
764 s->get_reg = get_reg;
765 s->set_reg = set_reg;
766 s->xml = xml;
767
pbrook56aebc82008-10-11 17:55:29 +0000768 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200769 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000770 *p = s;
771 if (g_pos) {
772 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800773 error_report("Error: Bad gdb register numbering for '%s', "
774 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200775 } else {
776 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000777 }
778 }
779}
780
aliguoria1d1bb32008-11-18 20:07:32 +0000781#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100782/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
783static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
784{
785 static const int xlat[] = {
786 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
787 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
788 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
789 };
790
791 CPUClass *cc = CPU_GET_CLASS(cpu);
792 int cputype = xlat[gdbtype];
793
794 if (cc->gdb_stop_before_watchpoint) {
795 cputype |= BP_STOP_BEFORE_ACCESS;
796 }
797 return cputype;
798}
aliguoria1d1bb32008-11-18 20:07:32 +0000799#endif
800
aliguori880a7572008-11-18 20:30:24 +0000801static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000802{
Andreas Färber182735e2013-05-29 22:29:20 +0200803 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000804 int err = 0;
805
Andreas Färber62278812013-06-27 17:12:06 +0200806 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200807 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200808 }
aliguorie22a25c2009-03-12 20:12:48 +0000809
aliguoria1d1bb32008-11-18 20:07:32 +0000810 switch (type) {
811 case GDB_BREAKPOINT_SW:
812 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200813 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200814 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
815 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000816 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200817 }
aliguori880a7572008-11-18 20:30:24 +0000818 }
819 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000820#ifndef CONFIG_USER_ONLY
821 case GDB_WATCHPOINT_WRITE:
822 case GDB_WATCHPOINT_READ:
823 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200824 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100825 err = cpu_watchpoint_insert(cpu, addr, len,
826 xlat_gdb_type(cpu, type), NULL);
827 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000828 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +0100829 }
aliguori880a7572008-11-18 20:30:24 +0000830 }
831 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000832#endif
833 default:
834 return -ENOSYS;
835 }
836}
837
aliguori880a7572008-11-18 20:30:24 +0000838static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000839{
Andreas Färber182735e2013-05-29 22:29:20 +0200840 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000841 int err = 0;
842
Andreas Färber62278812013-06-27 17:12:06 +0200843 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200844 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200845 }
aliguorie22a25c2009-03-12 20:12:48 +0000846
aliguoria1d1bb32008-11-18 20:07:32 +0000847 switch (type) {
848 case GDB_BREAKPOINT_SW:
849 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200850 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200851 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
852 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000853 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200854 }
aliguori880a7572008-11-18 20:30:24 +0000855 }
856 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000857#ifndef CONFIG_USER_ONLY
858 case GDB_WATCHPOINT_WRITE:
859 case GDB_WATCHPOINT_READ:
860 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200861 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100862 err = cpu_watchpoint_remove(cpu, addr, len,
863 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +0000864 if (err)
865 break;
866 }
867 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000868#endif
869 default:
870 return -ENOSYS;
871 }
872}
873
aliguori880a7572008-11-18 20:30:24 +0000874static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +0000875{
Andreas Färber182735e2013-05-29 22:29:20 +0200876 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000877
aliguorie22a25c2009-03-12 20:12:48 +0000878 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200879 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +0000880 return;
881 }
882
Andreas Färberbdc44642013-06-24 23:50:24 +0200883 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200884 cpu_breakpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000885#ifndef CONFIG_USER_ONLY
Andreas Färber75a34032013-09-02 16:57:02 +0200886 cpu_watchpoint_remove_all(cpu, BP_GDB);
aliguoria1d1bb32008-11-18 20:07:32 +0000887#endif
aliguori880a7572008-11-18 20:30:24 +0000888 }
aliguoria1d1bb32008-11-18 20:07:32 +0000889}
890
aurel32fab9d282009-04-08 21:29:37 +0000891static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
892{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200893 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +0200894
895 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -0700896 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +0000897}
898
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200899static CPUState *find_cpu(uint32_t thread_id)
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700900{
Andreas Färber0d342822012-12-17 07:12:13 +0100901 CPUState *cpu;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700902
Andreas Färberbdc44642013-06-24 23:50:24 +0200903 CPU_FOREACH(cpu) {
Alex Bennéed2a6c852017-07-12 11:52:14 +0100904 if (cpu_gdb_index(cpu) == thread_id) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200905 return cpu;
Andreas Färberaa48dd92013-07-09 20:50:52 +0200906 }
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700907 }
Andreas Färberaa48dd92013-07-09 20:50:52 +0200908
909 return NULL;
Nathan Froyd1e9fa732009-06-03 11:33:08 -0700910}
911
Jan Kiszka4dabe742015-02-07 09:38:43 +0100912static int is_query_packet(const char *p, const char *query, char separator)
913{
914 unsigned int query_len = strlen(query);
915
916 return strncmp(p, query, query_len) == 0 &&
917 (p[query_len] == '\0' || p[query_len] == separator);
918}
919
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100920/**
921 * gdb_handle_vcont - Parses and handles a vCont packet.
922 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
923 * a format error, 0 on success.
924 */
925static int gdb_handle_vcont(GDBState *s, const char *p)
926{
927 int res, idx, signal = 0;
928 char cur_action;
929 char *newstates;
930 unsigned long tmp;
931 CPUState *cpu;
932#ifdef CONFIG_USER_ONLY
933 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
934
935 CPU_FOREACH(cpu) {
936 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
937 }
938#endif
939 /* uninitialised CPUs stay 0 */
940 newstates = g_new0(char, max_cpus);
941
942 /* mark valid CPUs with 1 */
943 CPU_FOREACH(cpu) {
944 newstates[cpu->cpu_index] = 1;
945 }
946
947 /*
948 * res keeps track of what error we are returning, with -ENOTSUP meaning
949 * that the command is unknown or unsupported, thus returning an empty
950 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
951 * or incorrect parameters passed.
952 */
953 res = 0;
954 while (*p) {
955 if (*p++ != ';') {
956 res = -ENOTSUP;
957 goto out;
958 }
959
960 cur_action = *p++;
961 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +0100962 cur_action = qemu_tolower(cur_action);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100963 res = qemu_strtoul(p + 1, &p, 16, &tmp);
964 if (res) {
965 goto out;
966 }
967 signal = gdb_signal_to_target(tmp);
968 } else if (cur_action != 'c' && cur_action != 's') {
969 /* unknown/invalid/unsupported command */
970 res = -ENOTSUP;
971 goto out;
972 }
973 /* thread specification. special values: (none), -1 = all; 0 = any */
974 if ((p[0] == ':' && p[1] == '-' && p[2] == '1') || (p[0] != ':')) {
975 if (*p == ':') {
976 p += 3;
977 }
978 for (idx = 0; idx < max_cpus; idx++) {
979 if (newstates[idx] == 1) {
980 newstates[idx] = cur_action;
981 }
982 }
983 } else if (*p == ':') {
984 p++;
985 res = qemu_strtoul(p, &p, 16, &tmp);
986 if (res) {
987 goto out;
988 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100989
Alex Bennée5a6a1ad2017-07-12 11:52:16 +0100990 /* 0 means any thread, so we pick the first valid CPU */
991 cpu = tmp ? find_cpu(tmp) : first_cpu;
992
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100993 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +0100994 if (!cpu) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100995 res = -EINVAL;
996 goto out;
997 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +0100998
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100999 /* only use if no previous match occourred */
1000 if (newstates[cpu->cpu_index] == 1) {
1001 newstates[cpu->cpu_index] = cur_action;
1002 }
1003 }
1004 }
1005 s->signal = signal;
1006 gdb_continue_partial(s, newstates);
1007
1008out:
1009 g_free(newstates);
1010
1011 return res;
1012}
1013
aliguori880a7572008-11-18 20:30:24 +00001014static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +00001015{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001016 CPUState *cpu;
Andreas Färber5b24c642013-07-07 15:08:22 +02001017 CPUClass *cc;
bellardb4608c02003-06-27 17:34:32 +00001018 const char *p;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001019 uint32_t thread;
1020 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +00001021 uint8_t mem_buf[MAX_PACKET_LENGTH];
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -03001022 char buf[sizeof(mem_buf) + 1 /* trailing NUL */];
pbrook56aebc82008-10-11 17:55:29 +00001023 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +00001024 target_ulong addr, len;
ths3b46e622007-09-17 08:09:54 +00001025
Doug Gale5c9522b2017-12-02 20:30:37 -05001026 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01001027
bellard858693c2004-03-31 18:52:07 +00001028 p = line_buf;
1029 ch = *p++;
1030 switch(ch) {
1031 case '?':
bellard1fddef42005-04-17 19:16:13 +00001032 /* TODO: Make this return the correct value for user-mode. */
aurel32ca587a82008-12-18 22:44:13 +00001033 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", GDB_SIGNAL_TRAP,
Alex Bennéed2a6c852017-07-12 11:52:14 +01001034 cpu_gdb_index(s->c_cpu));
bellard858693c2004-03-31 18:52:07 +00001035 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +00001036 /* Remove all the breakpoints when this query is issued,
1037 * because gdb is doing and initial connect and the state
1038 * should be cleaned up.
1039 */
aliguori880a7572008-11-18 20:30:24 +00001040 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +00001041 break;
1042 case 'c':
1043 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +00001044 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001045 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001046 }
aurel32ca587a82008-12-18 22:44:13 +00001047 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +00001048 gdb_continue(s);
Doug Gale5c9522b2017-12-02 20:30:37 -05001049 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +00001050 case 'C':
aurel32ca587a82008-12-18 22:44:13 +00001051 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
1052 if (s->signal == -1)
1053 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +00001054 gdb_continue(s);
1055 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001056 case 'v':
1057 if (strncmp(p, "Cont", 4) == 0) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001058 p += 4;
1059 if (*p == '?') {
1060 put_packet(s, "vCont;c;C;s;S");
1061 break;
1062 }
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001063
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001064 res = gdb_handle_vcont(s, p);
1065
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001066 if (res) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001067 if ((res == -EINVAL) || (res == -ERANGE)) {
1068 put_packet(s, "E22");
1069 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001070 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001071 goto unknown_command;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001072 }
1073 break;
1074 } else {
1075 goto unknown_command;
1076 }
edgar_igl7d03f822008-05-17 18:58:29 +00001077 case 'k':
1078 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001079 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00001080 exit(0);
1081 case 'D':
1082 /* Detach packet */
aliguori880a7572008-11-18 20:30:24 +00001083 gdb_breakpoint_remove_all();
Daniel Gutson7ea06da2010-02-26 14:13:50 -03001084 gdb_syscall_mode = GDB_SYS_DISABLED;
edgar_igl7d03f822008-05-17 18:58:29 +00001085 gdb_continue(s);
1086 put_packet(s, "OK");
1087 break;
bellard858693c2004-03-31 18:52:07 +00001088 case 's':
1089 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +00001090 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001091 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001092 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001093 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +00001094 gdb_continue(s);
Doug Gale5c9522b2017-12-02 20:30:37 -05001095 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +00001096 case 'F':
1097 {
1098 target_ulong ret;
1099 target_ulong err;
1100
1101 ret = strtoull(p, (char **)&p, 16);
1102 if (*p == ',') {
1103 p++;
1104 err = strtoull(p, (char **)&p, 16);
1105 } else {
1106 err = 0;
1107 }
1108 if (*p == ',')
1109 p++;
1110 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001111 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001112 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +00001113 s->current_syscall_cb = NULL;
1114 }
pbrooka2d1eba2007-01-28 03:10:55 +00001115 if (type == 'C') {
1116 put_packet(s, "T02");
1117 } else {
edgar_iglba70a622008-03-14 06:10:42 +00001118 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +00001119 }
1120 }
1121 break;
bellard858693c2004-03-31 18:52:07 +00001122 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001123 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001124 len = 0;
Andreas Färber35143f02013-08-12 18:09:47 +02001125 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001126 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +00001127 len += reg_size;
1128 }
1129 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +00001130 put_packet(s, buf);
1131 break;
1132 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001133 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001134 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +00001135 len = strlen(p) / 2;
1136 hextomem((uint8_t *)registers, p, len);
Andreas Färber35143f02013-08-12 18:09:47 +02001137 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001138 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001139 len -= reg_size;
1140 registers += reg_size;
1141 }
bellard858693c2004-03-31 18:52:07 +00001142 put_packet(s, "OK");
1143 break;
1144 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001145 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001146 if (*p == ',')
1147 p++;
bellard9d9754a2006-06-25 15:32:37 +00001148 len = strtoull(p, NULL, 16);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001149
1150 /* memtohex() doubles the required space */
1151 if (len > MAX_PACKET_LENGTH / 2) {
1152 put_packet (s, "E22");
1153 break;
1154 }
1155
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001156 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001157 put_packet (s, "E14");
1158 } else {
1159 memtohex(buf, mem_buf, len);
1160 put_packet(s, buf);
1161 }
bellard858693c2004-03-31 18:52:07 +00001162 break;
1163 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001164 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001165 if (*p == ',')
1166 p++;
bellard9d9754a2006-06-25 15:32:37 +00001167 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001168 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001169 p++;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001170
1171 /* hextomem() reads 2*len bytes */
1172 if (len > strlen(p) / 2) {
1173 put_packet (s, "E22");
1174 break;
1175 }
bellard858693c2004-03-31 18:52:07 +00001176 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001177 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +02001178 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +00001179 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001180 } else {
bellard858693c2004-03-31 18:52:07 +00001181 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001182 }
bellard858693c2004-03-31 18:52:07 +00001183 break;
pbrook56aebc82008-10-11 17:55:29 +00001184 case 'p':
1185 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1186 This works, but can be very slow. Anything new enough to
1187 understand XML also knows how to use this properly. */
1188 if (!gdb_has_xml)
1189 goto unknown_command;
1190 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001191 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001192 if (reg_size) {
1193 memtohex(buf, mem_buf, reg_size);
1194 put_packet(s, buf);
1195 } else {
1196 put_packet(s, "E14");
1197 }
1198 break;
1199 case 'P':
1200 if (!gdb_has_xml)
1201 goto unknown_command;
1202 addr = strtoull(p, (char **)&p, 16);
1203 if (*p == '=')
1204 p++;
1205 reg_size = strlen(p) / 2;
1206 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001207 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001208 put_packet(s, "OK");
1209 break;
bellard858693c2004-03-31 18:52:07 +00001210 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001211 case 'z':
1212 type = strtoul(p, (char **)&p, 16);
1213 if (*p == ',')
1214 p++;
bellard9d9754a2006-06-25 15:32:37 +00001215 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001216 if (*p == ',')
1217 p++;
bellard9d9754a2006-06-25 15:32:37 +00001218 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001219 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001220 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001221 else
aliguori880a7572008-11-18 20:30:24 +00001222 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001223 if (res >= 0)
1224 put_packet(s, "OK");
1225 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001226 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001227 else
1228 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001229 break;
aliguori880a7572008-11-18 20:30:24 +00001230 case 'H':
1231 type = *p++;
1232 thread = strtoull(p, (char **)&p, 16);
1233 if (thread == -1 || thread == 0) {
1234 put_packet(s, "OK");
1235 break;
1236 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001237 cpu = find_cpu(thread);
1238 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001239 put_packet(s, "E22");
1240 break;
1241 }
1242 switch (type) {
1243 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001244 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001245 put_packet(s, "OK");
1246 break;
1247 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001248 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001249 put_packet(s, "OK");
1250 break;
1251 default:
1252 put_packet(s, "E22");
1253 break;
1254 }
1255 break;
1256 case 'T':
1257 thread = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001258 cpu = find_cpu(thread);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001259
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001260 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001261 put_packet(s, "OK");
1262 } else {
aliguori880a7572008-11-18 20:30:24 +00001263 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001264 }
aliguori880a7572008-11-18 20:30:24 +00001265 break;
pbrook978efd62006-06-17 18:30:42 +00001266 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001267 case 'Q':
1268 /* parse any 'q' packets here */
1269 if (!strcmp(p,"qemu.sstepbits")) {
1270 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001271 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1272 SSTEP_ENABLE,
1273 SSTEP_NOIRQ,
1274 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001275 put_packet(s, buf);
1276 break;
Jan Kiszka4dabe742015-02-07 09:38:43 +01001277 } else if (is_query_packet(p, "qemu.sstep", '=')) {
edgar_igl60897d32008-05-09 08:25:14 +00001278 /* Display or change the sstep_flags */
1279 p += 10;
1280 if (*p != '=') {
1281 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001282 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001283 put_packet(s, buf);
1284 break;
1285 }
1286 p++;
1287 type = strtoul(p, (char **)&p, 16);
1288 sstep_flags = type;
1289 put_packet(s, "OK");
1290 break;
aliguori880a7572008-11-18 20:30:24 +00001291 } else if (strcmp(p,"C") == 0) {
1292 /* "Current thread" remains vague in the spec, so always return
1293 * the first CPU (gdb returns the first thread). */
1294 put_packet(s, "QC1");
1295 break;
1296 } else if (strcmp(p,"fThreadInfo") == 0) {
Andreas Färber52f34622013-06-27 13:44:40 +02001297 s->query_cpu = first_cpu;
aliguori880a7572008-11-18 20:30:24 +00001298 goto report_cpuinfo;
1299 } else if (strcmp(p,"sThreadInfo") == 0) {
1300 report_cpuinfo:
1301 if (s->query_cpu) {
Alex Bennéed2a6c852017-07-12 11:52:14 +01001302 snprintf(buf, sizeof(buf), "m%x", cpu_gdb_index(s->query_cpu));
aliguori880a7572008-11-18 20:30:24 +00001303 put_packet(s, buf);
Andreas Färberbdc44642013-06-24 23:50:24 +02001304 s->query_cpu = CPU_NEXT(s->query_cpu);
aliguori880a7572008-11-18 20:30:24 +00001305 } else
1306 put_packet(s, "l");
1307 break;
1308 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
1309 thread = strtoull(p+16, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001310 cpu = find_cpu(thread);
1311 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001312 cpu_synchronize_state(cpu);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001313 /* memtohex() doubles the required space */
1314 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
Andreas Färber55e5c282012-12-17 06:18:02 +01001315 "CPU#%d [%s]", cpu->cpu_index,
Andreas Färber259186a2013-01-17 18:51:17 +01001316 cpu->halted ? "halted " : "running");
Doug Gale5c9522b2017-12-02 20:30:37 -05001317 trace_gdbstub_op_extra_info((char *)mem_buf);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001318 memtohex(buf, mem_buf, len);
1319 put_packet(s, buf);
1320 }
aliguori880a7572008-11-18 20:30:24 +00001321 break;
edgar_igl60897d32008-05-09 08:25:14 +00001322 }
blueswir10b8a9882009-03-07 10:51:36 +00001323#ifdef CONFIG_USER_ONLY
Jan Kiszka070949f2015-02-07 09:38:42 +01001324 else if (strcmp(p, "Offsets") == 0) {
Andreas Färber0429a972013-08-26 18:14:44 +02001325 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001326
blueswir1363a37d2008-08-21 17:58:08 +00001327 snprintf(buf, sizeof(buf),
1328 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1329 ";Bss=" TARGET_ABI_FMT_lx,
1330 ts->info->code_offset,
1331 ts->info->data_offset,
1332 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001333 put_packet(s, buf);
1334 break;
1335 }
blueswir10b8a9882009-03-07 10:51:36 +00001336#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001337 else if (strncmp(p, "Rcmd,", 5) == 0) {
1338 int len = strlen(p + 5);
1339
1340 if ((len % 2) != 0) {
1341 put_packet(s, "E01");
1342 break;
1343 }
aliguori8a34a0f2009-03-05 23:01:55 +00001344 len = len / 2;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001345 hextomem(mem_buf, p + 5, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001346 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001347 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001348 put_packet(s, "OK");
1349 break;
1350 }
blueswir10b8a9882009-03-07 10:51:36 +00001351#endif /* !CONFIG_USER_ONLY */
Jan Kiszka4dabe742015-02-07 09:38:43 +01001352 if (is_query_packet(p, "Supported", ':')) {
blueswir15b3715b2008-10-25 11:18:12 +00001353 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
Andreas Färber5b24c642013-07-07 15:08:22 +02001354 cc = CPU_GET_CLASS(first_cpu);
1355 if (cc->gdb_core_xml_file != NULL) {
1356 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1357 }
pbrook56aebc82008-10-11 17:55:29 +00001358 put_packet(s, buf);
1359 break;
1360 }
pbrook56aebc82008-10-11 17:55:29 +00001361 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1362 const char *xml;
1363 target_ulong total_len;
1364
Andreas Färber5b24c642013-07-07 15:08:22 +02001365 cc = CPU_GET_CLASS(first_cpu);
1366 if (cc->gdb_core_xml_file == NULL) {
1367 goto unknown_command;
1368 }
1369
Andreas Färber5b50e792013-06-29 04:18:45 +02001370 gdb_has_xml = true;
pbrook56aebc82008-10-11 17:55:29 +00001371 p += 19;
Andreas Färber5b24c642013-07-07 15:08:22 +02001372 xml = get_feature_xml(p, &p, cc);
pbrook56aebc82008-10-11 17:55:29 +00001373 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001374 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001375 put_packet(s, buf);
1376 break;
1377 }
1378
1379 if (*p == ':')
1380 p++;
1381 addr = strtoul(p, (char **)&p, 16);
1382 if (*p == ',')
1383 p++;
1384 len = strtoul(p, (char **)&p, 16);
1385
1386 total_len = strlen(xml);
1387 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001388 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001389 put_packet(s, buf);
1390 break;
1391 }
1392 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1393 len = (MAX_PACKET_LENGTH - 5) / 2;
1394 if (len < total_len - addr) {
1395 buf[0] = 'm';
1396 len = memtox(buf + 1, xml + addr, len);
1397 } else {
1398 buf[0] = 'l';
1399 len = memtox(buf + 1, xml + addr, total_len - addr);
1400 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001401 put_packet_binary(s, buf, len + 1, true);
pbrook56aebc82008-10-11 17:55:29 +00001402 break;
1403 }
Jan Kiszkaa3919382015-02-07 09:38:44 +01001404 if (is_query_packet(p, "Attached", ':')) {
1405 put_packet(s, GDB_ATTACHED);
1406 break;
1407 }
pbrook56aebc82008-10-11 17:55:29 +00001408 /* Unrecognised 'q' command. */
1409 goto unknown_command;
1410
bellard858693c2004-03-31 18:52:07 +00001411 default:
pbrook56aebc82008-10-11 17:55:29 +00001412 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001413 /* put empty packet */
1414 buf[0] = '\0';
1415 put_packet(s, buf);
1416 break;
1417 }
1418 return RS_IDLE;
1419}
1420
Andreas Färber64f6b342013-05-27 02:06:09 +02001421void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001422{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001423 gdbserver_state->c_cpu = cpu;
1424 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001425}
1426
bellard1fddef42005-04-17 19:16:13 +00001427#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001428static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001429{
aliguori880a7572008-11-18 20:30:24 +00001430 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001431 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001432 char buf[256];
aliguorid6fc1b32008-11-18 19:55:44 +00001433 const char *type;
bellard858693c2004-03-31 18:52:07 +00001434 int ret;
1435
Meador Ingecdb432b2012-03-15 17:49:45 +00001436 if (running || s->state == RS_INACTIVE) {
1437 return;
1438 }
1439 /* Is there a GDB syscall waiting to be sent? */
1440 if (s->current_syscall_cb) {
1441 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001442 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001443 }
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001444 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001445 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02001446 if (cpu->watchpoint_hit) {
1447 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001448 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001449 type = "r";
1450 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001451 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001452 type = "a";
1453 break;
1454 default:
1455 type = "";
1456 break;
1457 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001458 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
1459 (target_ulong)cpu->watchpoint_hit->vaddr);
aliguori880a7572008-11-18 20:30:24 +00001460 snprintf(buf, sizeof(buf),
1461 "T%02xthread:%02x;%swatch:" TARGET_FMT_lx ";",
Alex Bennéed2a6c852017-07-12 11:52:14 +01001462 GDB_SIGNAL_TRAP, cpu_gdb_index(cpu), type,
Andreas Färberff4700b2013-08-26 18:23:18 +02001463 (target_ulong)cpu->watchpoint_hit->vaddr);
1464 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001465 goto send_packet;
Doug Gale5c9522b2017-12-02 20:30:37 -05001466 } else {
1467 trace_gdbstub_hit_break();
pbrook6658ffb2007-03-16 23:58:11 +00001468 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001469 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00001470 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001471 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001472 case RUN_STATE_PAUSED:
Doug Gale5c9522b2017-12-02 20:30:37 -05001473 trace_gdbstub_hit_paused();
aliguori9781e042009-01-22 17:15:29 +00001474 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001475 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001476 case RUN_STATE_SHUTDOWN:
Doug Gale5c9522b2017-12-02 20:30:37 -05001477 trace_gdbstub_hit_shutdown();
Jan Kiszka425189a2011-03-22 11:02:09 +01001478 ret = GDB_SIGNAL_QUIT;
1479 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001480 case RUN_STATE_IO_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05001481 trace_gdbstub_hit_io_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01001482 ret = GDB_SIGNAL_IO;
1483 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001484 case RUN_STATE_WATCHDOG:
Doug Gale5c9522b2017-12-02 20:30:37 -05001485 trace_gdbstub_hit_watchdog();
Jan Kiszka425189a2011-03-22 11:02:09 +01001486 ret = GDB_SIGNAL_ALRM;
1487 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001488 case RUN_STATE_INTERNAL_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05001489 trace_gdbstub_hit_internal_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01001490 ret = GDB_SIGNAL_ABRT;
1491 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001492 case RUN_STATE_SAVE_VM:
1493 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001494 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001495 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001496 ret = GDB_SIGNAL_XCPU;
1497 break;
1498 default:
Doug Gale5c9522b2017-12-02 20:30:37 -05001499 trace_gdbstub_hit_unknown(state);
Jan Kiszka425189a2011-03-22 11:02:09 +01001500 ret = GDB_SIGNAL_UNKNOWN;
1501 break;
bellardbbeb7b52006-04-23 18:42:15 +00001502 }
Jan Kiszka226d0072015-07-24 18:52:31 +02001503 gdb_set_stop_cpu(cpu);
Alex Bennéed2a6c852017-07-12 11:52:14 +01001504 snprintf(buf, sizeof(buf), "T%02xthread:%02x;", ret, cpu_gdb_index(cpu));
Jan Kiszka425189a2011-03-22 11:02:09 +01001505
1506send_packet:
bellard858693c2004-03-31 18:52:07 +00001507 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001508
1509 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001510 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001511}
bellard1fddef42005-04-17 19:16:13 +00001512#endif
bellard858693c2004-03-31 18:52:07 +00001513
pbrooka2d1eba2007-01-28 03:10:55 +00001514/* Send a gdb syscall request.
1515 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001516 %x - target_ulong argument printed in hex.
1517 %lx - 64-bit argument printed in hex.
1518 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01001519void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00001520{
pbrooka2d1eba2007-01-28 03:10:55 +00001521 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001522 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001523 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001524 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001525 GDBState *s;
1526
aliguori880a7572008-11-18 20:30:24 +00001527 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001528 if (!s)
1529 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001530 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001531#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001532 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001533#endif
Meador Ingecdb432b2012-03-15 17:49:45 +00001534 p = s->syscall_buf;
1535 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001536 *(p++) = 'F';
1537 while (*fmt) {
1538 if (*fmt == '%') {
1539 fmt++;
1540 switch (*fmt++) {
1541 case 'x':
1542 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001543 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001544 break;
pbrooka87295e2007-05-26 15:09:38 +00001545 case 'l':
1546 if (*(fmt++) != 'x')
1547 goto bad_format;
1548 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001549 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001550 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001551 case 's':
1552 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001553 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001554 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001555 break;
1556 default:
pbrooka87295e2007-05-26 15:09:38 +00001557 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001558 error_report("gdbstub: Bad syscall format string '%s'",
1559 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00001560 break;
1561 }
1562 } else {
1563 *(p++) = *(fmt++);
1564 }
1565 }
pbrook8a93e022007-08-06 13:19:15 +00001566 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001567#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001568 put_packet(s, s->syscall_buf);
Peter Maydell4f710862018-05-15 19:19:58 +01001569 /* Return control to gdb for it to process the syscall request.
1570 * Since the protocol requires that gdb hands control back to us
1571 * using a "here are the results" F packet, we don't need to check
1572 * gdb_handlesig's return value (which is the signal to deliver if
1573 * execution was resumed via a continue packet).
1574 */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001575 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001576#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001577 /* In this case wait to send the syscall packet until notification that
1578 the CPU has stopped. This must be done because if the packet is sent
1579 now the reply from the syscall request could be received while the CPU
1580 is still in the running state, which can cause packets to be dropped
1581 and state transition 'T' packets to be sent while the syscall is still
1582 being processed. */
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001583 qemu_cpu_kick(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00001584#endif
1585}
1586
Peter Maydell19239b32015-09-07 10:39:27 +01001587void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
1588{
1589 va_list va;
1590
1591 va_start(va, fmt);
1592 gdb_do_syscallv(cb, fmt, va);
1593 va_end(va);
1594}
1595
bellard6a00d602005-11-21 23:25:50 +00001596static void gdb_read_byte(GDBState *s, int ch)
bellard858693c2004-03-31 18:52:07 +00001597{
ths60fe76f2007-12-16 03:02:09 +00001598 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00001599
bellard1fddef42005-04-17 19:16:13 +00001600#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001601 if (s->last_packet_len) {
1602 /* Waiting for a response to the last packet. If we see the start
1603 of a new command then abandon the previous response. */
1604 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05001605 trace_gdbstub_err_got_nack();
thsffe8ab82007-12-16 03:16:05 +00001606 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
Alex Bennée118e2262017-07-12 11:52:13 +01001607 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05001608 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01001609 } else {
Doug Gale5c9522b2017-12-02 20:30:37 -05001610 trace_gdbstub_io_got_unexpected((uint8_t)ch);
pbrook4046d912007-01-28 01:53:16 +00001611 }
Alex Bennée118e2262017-07-12 11:52:13 +01001612
pbrook4046d912007-01-28 01:53:16 +00001613 if (ch == '+' || ch == '$')
1614 s->last_packet_len = 0;
1615 if (ch != '$')
1616 return;
1617 }
Luiz Capitulino13548692011-07-29 15:36:43 -03001618 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00001619 /* when the CPU is running, we cannot do anything except stop
1620 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001621 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00001622 } else
bellard1fddef42005-04-17 19:16:13 +00001623#endif
bellard41625032005-04-24 10:07:11 +00001624 {
bellard858693c2004-03-31 18:52:07 +00001625 switch(s->state) {
1626 case RS_IDLE:
1627 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04001628 /* start of command packet */
bellard858693c2004-03-31 18:52:07 +00001629 s->line_buf_index = 0;
Doug Gale4bf43122017-05-01 12:22:10 -04001630 s->line_sum = 0;
bellard858693c2004-03-31 18:52:07 +00001631 s->state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04001632 } else {
Doug Gale5c9522b2017-12-02 20:30:37 -05001633 trace_gdbstub_err_garbage((uint8_t)ch);
bellard4c3a88a2003-07-26 12:06:08 +00001634 }
1635 break;
bellard858693c2004-03-31 18:52:07 +00001636 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04001637 if (ch == '}') {
1638 /* start escape sequence */
1639 s->state = RS_GETLINE_ESC;
1640 s->line_sum += ch;
1641 } else if (ch == '*') {
1642 /* start run length encoding sequence */
1643 s->state = RS_GETLINE_RLE;
1644 s->line_sum += ch;
1645 } else if (ch == '#') {
1646 /* end of command, start of checksum*/
1647 s->state = RS_CHKSUM1;
bellard858693c2004-03-31 18:52:07 +00001648 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05001649 trace_gdbstub_err_overrun();
bellard858693c2004-03-31 18:52:07 +00001650 s->state = RS_IDLE;
1651 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04001652 /* unescaped command character */
1653 s->line_buf[s->line_buf_index++] = ch;
1654 s->line_sum += ch;
1655 }
1656 break;
1657 case RS_GETLINE_ESC:
1658 if (ch == '#') {
1659 /* unexpected end of command in escape sequence */
1660 s->state = RS_CHKSUM1;
1661 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
1662 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05001663 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04001664 s->state = RS_IDLE;
1665 } else {
1666 /* parse escaped character and leave escape state */
1667 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
1668 s->line_sum += ch;
1669 s->state = RS_GETLINE;
1670 }
1671 break;
1672 case RS_GETLINE_RLE:
1673 if (ch < ' ') {
1674 /* invalid RLE count encoding */
Doug Gale5c9522b2017-12-02 20:30:37 -05001675 trace_gdbstub_err_invalid_repeat((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04001676 s->state = RS_GETLINE;
1677 } else {
1678 /* decode repeat length */
1679 int repeat = (unsigned char)ch - ' ' + 3;
1680 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
1681 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05001682 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04001683 s->state = RS_IDLE;
1684 } else if (s->line_buf_index < 1) {
1685 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05001686 trace_gdbstub_err_invalid_rle();
Doug Gale4bf43122017-05-01 12:22:10 -04001687 s->state = RS_GETLINE;
1688 } else {
1689 /* repeat the last character */
1690 memset(s->line_buf + s->line_buf_index,
1691 s->line_buf[s->line_buf_index - 1], repeat);
1692 s->line_buf_index += repeat;
1693 s->line_sum += ch;
1694 s->state = RS_GETLINE;
1695 }
bellard858693c2004-03-31 18:52:07 +00001696 }
1697 break;
1698 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04001699 /* get high hex digit of checksum */
1700 if (!isxdigit(ch)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05001701 trace_gdbstub_err_checksum_invalid((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04001702 s->state = RS_GETLINE;
1703 break;
1704 }
bellard858693c2004-03-31 18:52:07 +00001705 s->line_buf[s->line_buf_index] = '\0';
1706 s->line_csum = fromhex(ch) << 4;
1707 s->state = RS_CHKSUM2;
1708 break;
1709 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04001710 /* get low hex digit of checksum */
1711 if (!isxdigit(ch)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05001712 trace_gdbstub_err_checksum_invalid((uint8_t)ch);
Doug Gale4bf43122017-05-01 12:22:10 -04001713 s->state = RS_GETLINE;
1714 break;
bellard858693c2004-03-31 18:52:07 +00001715 }
Doug Gale4bf43122017-05-01 12:22:10 -04001716 s->line_csum |= fromhex(ch);
1717
1718 if (s->line_csum != (s->line_sum & 0xff)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05001719 trace_gdbstub_err_checksum_incorrect(s->line_sum, s->line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04001720 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00001721 reply = '-';
1722 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00001723 s->state = RS_IDLE;
1724 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04001725 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00001726 reply = '+';
1727 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00001728 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00001729 }
bellardb4608c02003-06-27 17:34:32 +00001730 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001731 default:
1732 abort();
bellardb4608c02003-06-27 17:34:32 +00001733 }
1734 }
bellard858693c2004-03-31 18:52:07 +00001735}
1736
Paul Brook0e1c9c52010-06-16 13:03:51 +01001737/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001738void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01001739{
1740 GDBState *s;
1741 char buf[4];
1742
1743 s = gdbserver_state;
1744 if (!s) {
1745 return;
1746 }
1747#ifdef CONFIG_USER_ONLY
1748 if (gdbserver_fd < 0 || s->fd < 0) {
1749 return;
1750 }
1751#endif
1752
Doug Gale5c9522b2017-12-02 20:30:37 -05001753 trace_gdbstub_op_exiting((uint8_t)code);
1754
Paul Brook0e1c9c52010-06-16 13:03:51 +01001755 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
1756 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001757
1758#ifndef CONFIG_USER_ONLY
Marc-André Lureau1ce26102017-01-27 00:49:13 +04001759 qemu_chr_fe_deinit(&s->chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01001760#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01001761}
1762
Luc Michel8f468632019-01-07 15:23:45 +00001763/*
1764 * Create the process that will contain all the "orphan" CPUs (that are not
1765 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
1766 * be attachable and thus will be invisible to the user.
1767 */
1768static void create_default_process(GDBState *s)
1769{
1770 GDBProcess *process;
1771 int max_pid = 0;
1772
1773 if (s->process_num) {
1774 max_pid = s->processes[s->process_num - 1].pid;
1775 }
1776
1777 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
1778 process = &s->processes[s->process_num - 1];
1779
1780 /* We need an available PID slot for this process */
1781 assert(max_pid < UINT32_MAX);
1782
1783 process->pid = max_pid + 1;
1784 process->attached = false;
1785}
1786
bellard1fddef42005-04-17 19:16:13 +00001787#ifdef CONFIG_USER_ONLY
1788int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02001789gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00001790{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001791 GDBState *s;
1792 char buf[256];
1793 int n;
bellard1fddef42005-04-17 19:16:13 +00001794
Andreas Färber5ca666c2013-06-24 19:20:57 +02001795 s = gdbserver_state;
1796 if (gdbserver_fd < 0 || s->fd < 0) {
1797 return sig;
bellard1fddef42005-04-17 19:16:13 +00001798 }
1799
Andreas Färber5ca666c2013-06-24 19:20:57 +02001800 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001801 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001802 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00001803
Andreas Färber5ca666c2013-06-24 19:20:57 +02001804 if (sig != 0) {
1805 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
1806 put_packet(s, buf);
1807 }
1808 /* put_packet() might have detected that the peer terminated the
1809 connection. */
1810 if (s->fd < 0) {
1811 return sig;
1812 }
1813
1814 sig = 0;
1815 s->state = RS_IDLE;
1816 s->running_state = 0;
1817 while (s->running_state == 0) {
1818 n = read(s->fd, buf, 256);
1819 if (n > 0) {
1820 int i;
1821
1822 for (i = 0; i < n; i++) {
1823 gdb_read_byte(s, buf[i]);
1824 }
Peter Wu5819e3e2016-06-05 16:35:48 +02001825 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02001826 /* XXX: Connection closed. Should probably wait for another
1827 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02001828 if (n == 0) {
1829 close(s->fd);
1830 }
1831 s->fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02001832 return sig;
bellard1fddef42005-04-17 19:16:13 +00001833 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02001834 }
1835 sig = s->signal;
1836 s->signal = 0;
1837 return sig;
bellard1fddef42005-04-17 19:16:13 +00001838}
bellarde9009672005-04-26 20:42:36 +00001839
aurel32ca587a82008-12-18 22:44:13 +00001840/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01001841void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00001842{
Andreas Färber5ca666c2013-06-24 19:20:57 +02001843 GDBState *s;
1844 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00001845
Andreas Färber5ca666c2013-06-24 19:20:57 +02001846 s = gdbserver_state;
1847 if (gdbserver_fd < 0 || s->fd < 0) {
1848 return;
1849 }
aurel32ca587a82008-12-18 22:44:13 +00001850
Andreas Färber5ca666c2013-06-24 19:20:57 +02001851 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
1852 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00001853}
bellard1fddef42005-04-17 19:16:13 +00001854
Peter Maydell2f652222018-05-14 18:30:44 +01001855static bool gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00001856{
1857 GDBState *s;
1858 struct sockaddr_in sockaddr;
1859 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09001860 int fd;
bellard858693c2004-03-31 18:52:07 +00001861
1862 for(;;) {
1863 len = sizeof(sockaddr);
1864 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
1865 if (fd < 0 && errno != EINTR) {
1866 perror("accept");
Peter Maydell2f652222018-05-14 18:30:44 +01001867 return false;
bellard858693c2004-03-31 18:52:07 +00001868 } else if (fd >= 0) {
Peter Maydellf5bdd782018-05-14 18:30:43 +01001869 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00001870 break;
1871 }
1872 }
1873
1874 /* set short latency */
Peter Maydell2f652222018-05-14 18:30:44 +01001875 if (socket_set_nodelay(fd)) {
1876 perror("setsockopt");
Philippe Mathieu-Daudéead75d82018-05-24 19:34:58 -03001877 close(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01001878 return false;
1879 }
ths3b46e622007-09-17 08:09:54 +00001880
Anthony Liguori7267c092011-08-20 22:09:37 -05001881 s = g_malloc0(sizeof(GDBState));
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001882 s->c_cpu = first_cpu;
1883 s->g_cpu = first_cpu;
Luc Michel8f468632019-01-07 15:23:45 +00001884 create_default_process(s);
bellard858693c2004-03-31 18:52:07 +00001885 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02001886 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00001887
aliguori880a7572008-11-18 20:30:24 +00001888 gdbserver_state = s;
Peter Maydell2f652222018-05-14 18:30:44 +01001889 return true;
bellard858693c2004-03-31 18:52:07 +00001890}
1891
1892static int gdbserver_open(int port)
1893{
1894 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001895 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00001896
1897 fd = socket(PF_INET, SOCK_STREAM, 0);
1898 if (fd < 0) {
1899 perror("socket");
1900 return -1;
1901 }
Peter Maydellf5bdd782018-05-14 18:30:43 +01001902 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00001903
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02001904 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00001905
1906 sockaddr.sin_family = AF_INET;
1907 sockaddr.sin_port = htons(port);
1908 sockaddr.sin_addr.s_addr = 0;
1909 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
1910 if (ret < 0) {
1911 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00001912 close(fd);
bellard858693c2004-03-31 18:52:07 +00001913 return -1;
1914 }
Peter Wu96165b92016-05-04 11:32:17 +02001915 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00001916 if (ret < 0) {
1917 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00001918 close(fd);
bellard858693c2004-03-31 18:52:07 +00001919 return -1;
1920 }
bellard858693c2004-03-31 18:52:07 +00001921 return fd;
1922}
1923
1924int gdbserver_start(int port)
1925{
1926 gdbserver_fd = gdbserver_open(port);
1927 if (gdbserver_fd < 0)
1928 return -1;
1929 /* accept connections */
Peter Maydell2f652222018-05-14 18:30:44 +01001930 if (!gdb_accept()) {
1931 close(gdbserver_fd);
1932 gdbserver_fd = -1;
1933 return -1;
1934 }
bellardb4608c02003-06-27 17:34:32 +00001935 return 0;
1936}
aurel322b1319c2008-12-18 22:44:04 +00001937
1938/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07001939void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00001940{
1941 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02001942
1943 if (gdbserver_fd < 0 || s->fd < 0) {
1944 return;
1945 }
aurel322b1319c2008-12-18 22:44:04 +00001946 close(s->fd);
1947 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001948 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02001949 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00001950}
pbrook4046d912007-01-28 01:53:16 +00001951#else
thsaa1f17c2007-07-11 22:48:58 +00001952static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00001953{
pbrook56aebc82008-10-11 17:55:29 +00001954 /* We can handle an arbitrarily large amount of data.
1955 Pick the maximum packet size, which is as good as anything. */
1956 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00001957}
1958
thsaa1f17c2007-07-11 22:48:58 +00001959static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00001960{
pbrook4046d912007-01-28 01:53:16 +00001961 int i;
1962
1963 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00001964 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00001965 }
1966}
1967
1968static void gdb_chr_event(void *opaque, int event)
1969{
1970 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05301971 case CHR_EVENT_OPENED:
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001972 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02001973 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00001974 break;
1975 default:
1976 break;
1977 }
1978}
1979
aliguori8a34a0f2009-03-05 23:01:55 +00001980static void gdb_monitor_output(GDBState *s, const char *msg, int len)
1981{
1982 char buf[MAX_PACKET_LENGTH];
1983
1984 buf[0] = 'O';
1985 if (len > (MAX_PACKET_LENGTH/2) - 1)
1986 len = (MAX_PACKET_LENGTH/2) - 1;
1987 memtohex(buf + 1, (uint8_t *)msg, len);
1988 put_packet(s, buf);
1989}
1990
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03001991static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00001992{
1993 const char *p = (const char *)buf;
1994 int max_sz;
1995
1996 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
1997 for (;;) {
1998 if (len <= max_sz) {
1999 gdb_monitor_output(gdbserver_state, p, len);
2000 break;
2001 }
2002 gdb_monitor_output(gdbserver_state, p, max_sz);
2003 p += max_sz;
2004 len -= max_sz;
2005 }
2006 return len;
2007}
2008
aliguori59030a82009-04-05 18:43:41 +00002009#ifndef _WIN32
2010static void gdb_sigterm_handler(int signal)
2011{
Luiz Capitulino13548692011-07-29 15:36:43 -03002012 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002013 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01002014 }
aliguori59030a82009-04-05 18:43:41 +00002015}
2016#endif
2017
Marc-André Lureau777357d2016-12-07 18:39:10 +03002018static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
2019 bool *be_opened, Error **errp)
2020{
2021 *be_opened = false;
2022}
2023
2024static void char_gdb_class_init(ObjectClass *oc, void *data)
2025{
2026 ChardevClass *cc = CHARDEV_CLASS(oc);
2027
2028 cc->internal = true;
2029 cc->open = gdb_monitor_open;
2030 cc->chr_write = gdb_monitor_write;
2031}
2032
2033#define TYPE_CHARDEV_GDB "chardev-gdb"
2034
2035static const TypeInfo char_gdb_type_info = {
2036 .name = TYPE_CHARDEV_GDB,
2037 .parent = TYPE_CHARDEV,
2038 .class_init = char_gdb_class_init,
2039};
2040
Luc Michel8f468632019-01-07 15:23:45 +00002041static int find_cpu_clusters(Object *child, void *opaque)
2042{
2043 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
2044 GDBState *s = (GDBState *) opaque;
2045 CPUClusterState *cluster = CPU_CLUSTER(child);
2046 GDBProcess *process;
2047
2048 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2049
2050 process = &s->processes[s->process_num - 1];
2051
2052 /*
2053 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
2054 * runtime, we enforce here that the machine does not use a cluster ID
2055 * that would lead to PID 0.
2056 */
2057 assert(cluster->cluster_id != UINT32_MAX);
2058 process->pid = cluster->cluster_id + 1;
2059 process->attached = false;
2060
2061 return 0;
2062 }
2063
2064 return object_child_foreach(child, find_cpu_clusters, opaque);
2065}
2066
2067static int pid_order(const void *a, const void *b)
2068{
2069 GDBProcess *pa = (GDBProcess *) a;
2070 GDBProcess *pb = (GDBProcess *) b;
2071
2072 if (pa->pid < pb->pid) {
2073 return -1;
2074 } else if (pa->pid > pb->pid) {
2075 return 1;
2076 } else {
2077 return 0;
2078 }
2079}
2080
2081static void create_processes(GDBState *s)
2082{
2083 object_child_foreach(object_get_root(), find_cpu_clusters, s);
2084
2085 if (s->processes) {
2086 /* Sort by PID */
2087 qsort(s->processes, s->process_num, sizeof(s->processes[0]), pid_order);
2088 }
2089
2090 create_default_process(s);
2091}
2092
2093static void cleanup_processes(GDBState *s)
2094{
2095 g_free(s->processes);
2096 s->process_num = 0;
2097 s->processes = NULL;
2098}
2099
aliguori59030a82009-04-05 18:43:41 +00002100int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00002101{
Doug Gale5c9522b2017-12-02 20:30:37 -05002102 trace_gdbstub_op_start(device);
2103
pbrook4046d912007-01-28 01:53:16 +00002104 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00002105 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03002106 Chardev *chr = NULL;
2107 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00002108
Ziyue Yang508b4ec2017-01-18 16:02:41 +08002109 if (!first_cpu) {
2110 error_report("gdbstub: meaningless to attach gdb to a "
2111 "machine without any CPU.");
2112 return -1;
2113 }
2114
aliguori59030a82009-04-05 18:43:41 +00002115 if (!device)
2116 return -1;
2117 if (strcmp(device, "none") != 0) {
2118 if (strstart(device, "tcp:", NULL)) {
2119 /* enforce required TCP attributes */
2120 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
2121 "%s,nowait,nodelay,server", device);
2122 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00002123 }
aliguori59030a82009-04-05 18:43:41 +00002124#ifndef _WIN32
2125 else if (strcmp(device, "stdio") == 0) {
2126 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00002127
aliguori59030a82009-04-05 18:43:41 +00002128 memset(&act, 0, sizeof(act));
2129 act.sa_handler = gdb_sigterm_handler;
2130 sigaction(SIGINT, &act, NULL);
2131 }
2132#endif
Marc-André Lureau95e30b22018-08-22 19:19:42 +02002133 /*
2134 * FIXME: it's a bit weird to allow using a mux chardev here
2135 * and implicitly setup a monitor. We may want to break this.
2136 */
2137 chr = qemu_chr_new_noreplay("gdb", device, true);
aliguori36556b22009-03-28 18:05:53 +00002138 if (!chr)
2139 return -1;
pbrookcfc34752007-02-22 01:48:01 +00002140 }
2141
aliguori36556b22009-03-28 18:05:53 +00002142 s = gdbserver_state;
2143 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05002144 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00002145 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00002146
aliguori36556b22009-03-28 18:05:53 +00002147 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
2148
2149 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03002150 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
2151 NULL, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00002152 monitor_init(mon_chr, 0);
2153 } else {
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002154 qemu_chr_fe_deinit(&s->chr, true);
aliguori36556b22009-03-28 18:05:53 +00002155 mon_chr = s->mon_chr;
Luc Michel8f468632019-01-07 15:23:45 +00002156 cleanup_processes(s);
aliguori36556b22009-03-28 18:05:53 +00002157 memset(s, 0, sizeof(GDBState));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002158 s->mon_chr = mon_chr;
aliguori36556b22009-03-28 18:05:53 +00002159 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002160 s->c_cpu = first_cpu;
2161 s->g_cpu = first_cpu;
Luc Michel8f468632019-01-07 15:23:45 +00002162
2163 create_processes(s);
2164
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002165 if (chr) {
2166 qemu_chr_fe_init(&s->chr, chr, &error_abort);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002167 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
Anton Nefedov81517ba2017-07-06 15:08:49 +03002168 gdb_chr_event, NULL, NULL, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002169 }
aliguori36556b22009-03-28 18:05:53 +00002170 s->state = chr ? RS_IDLE : RS_INACTIVE;
2171 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00002172 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00002173
pbrook4046d912007-01-28 01:53:16 +00002174 return 0;
2175}
Marc-André Lureau777357d2016-12-07 18:39:10 +03002176
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01002177void gdbserver_cleanup(void)
2178{
2179 if (gdbserver_state) {
2180 put_packet(gdbserver_state, "W00");
2181 }
2182}
2183
Marc-André Lureau777357d2016-12-07 18:39:10 +03002184static void register_types(void)
2185{
2186 type_register_static(&char_gdb_type_info);
2187}
2188
2189type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00002190#endif