blob: 57d6e50ddfcb1b42b04fe7553bedf3bfdeeac980 [file] [log] [blame]
bellardb4608c02003-06-27 17:34:32 +00001/*
2 * gdb server stub
ths5fafdf22007-09-16 21:08:06 +00003 *
Alex Bennée42a09592019-07-05 13:28:19 +01004 * This implements a subset of the remote protocol as described in:
5 *
6 * https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
7 *
bellard34751872005-07-02 14:31:34 +00008 * Copyright (c) 2003-2005 Fabrice Bellard
bellardb4608c02003-06-27 17:34:32 +00009 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
Blue Swirl8167ee82009-07-16 20:47:01 +000021 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Alex Bennée42a09592019-07-05 13:28:19 +010022 *
23 * SPDX-License-Identifier: LGPL-2.0+
bellardb4608c02003-06-27 17:34:32 +000024 */
Markus Armbruster856dfd82019-05-23 16:35:06 +020025
Peter Maydelld38ea872016-01-29 17:50:05 +000026#include "qemu/osdep.h"
Markus Armbrustera8d25322019-05-23 16:35:08 +020027#include "qemu-common.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010028#include "qapi/error.h"
Ziyue Yang508b4ec2017-01-18 16:02:41 +080029#include "qemu/error-report.h"
Markus Armbruster856dfd82019-05-23 16:35:06 +020030#include "qemu/ctype.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020031#include "qemu/cutils.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020032#include "qemu/module.h"
Doug Gale5c9522b2017-12-02 20:30:37 -050033#include "trace-root.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020034#ifdef CONFIG_USER_ONLY
bellard1fddef42005-04-17 19:16:13 +000035#include "qemu.h"
36#else
Paolo Bonzini83c90892012-12-17 18:19:49 +010037#include "monitor/monitor.h"
Marc-André Lureau8228e352017-01-26 17:19:46 +040038#include "chardev/char.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040039#include "chardev/char-fe.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010040#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010041#include "exec/gdbstub.h"
Luc Michel8f468632019-01-07 15:23:45 +000042#include "hw/cpu/cluster.h"
Like Xu5cc87672019-05-19 04:54:21 +080043#include "hw/boards.h"
bellard1fddef42005-04-17 19:16:13 +000044#endif
bellard67b915a2004-03-31 23:37:16 +000045
pbrook56aebc82008-10-11 17:55:29 +000046#define MAX_PACKET_LENGTH 4096
47
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010048#include "qemu/sockets.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010049#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010050#include "sysemu/kvm.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020051#include "sysemu/runstate.h"
Alex Bennéef1672e62019-05-13 14:43:57 +010052#include "hw/semihosting/semihost.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010053#include "exec/exec-all.h"
aurel32ca587a82008-12-18 22:44:13 +000054
Jan Kiszkaa3919382015-02-07 09:38:44 +010055#ifdef CONFIG_USER_ONLY
56#define GDB_ATTACHED "0"
57#else
58#define GDB_ATTACHED "1"
59#endif
60
Jon Doronab4752e2019-05-29 09:41:48 +030061#ifndef CONFIG_USER_ONLY
62static int phy_memory_mode;
63#endif
64
Andreas Färberf3659ee2013-06-27 19:09:09 +020065static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
66 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020067{
Jon Doronab4752e2019-05-29 09:41:48 +030068 CPUClass *cc;
Andreas Färberf3659ee2013-06-27 19:09:09 +020069
Jon Doronab4752e2019-05-29 09:41:48 +030070#ifndef CONFIG_USER_ONLY
71 if (phy_memory_mode) {
72 if (is_write) {
73 cpu_physical_memory_write(addr, buf, len);
74 } else {
75 cpu_physical_memory_read(addr, buf, len);
76 }
77 return 0;
78 }
79#endif
80
81 cc = CPU_GET_CLASS(cpu);
Andreas Färberf3659ee2013-06-27 19:09:09 +020082 if (cc->memory_rw_debug) {
83 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
84 }
85 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020086}
aurel32ca587a82008-12-18 22:44:13 +000087
Alex Bennéed2a6c852017-07-12 11:52:14 +010088/* Return the GDB index for a given vCPU state.
89 *
90 * For user mode this is simply the thread id. In system mode GDB
91 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
92 */
93static inline int cpu_gdb_index(CPUState *cpu)
94{
95#if defined(CONFIG_USER_ONLY)
Alex Bennéebd88c782017-07-12 11:52:15 +010096 TaskState *ts = (TaskState *) cpu->opaque;
97 return ts->ts_tid;
Alex Bennéed2a6c852017-07-12 11:52:14 +010098#else
99 return cpu->cpu_index + 1;
100#endif
101}
102
aurel32ca587a82008-12-18 22:44:13 +0000103enum {
104 GDB_SIGNAL_0 = 0,
105 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +0100106 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +0000107 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +0100108 GDB_SIGNAL_ABRT = 6,
109 GDB_SIGNAL_ALRM = 14,
110 GDB_SIGNAL_IO = 23,
111 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +0000112 GDB_SIGNAL_UNKNOWN = 143
113};
114
115#ifdef CONFIG_USER_ONLY
116
117/* Map target signal numbers to GDB protocol signal numbers and vice
118 * versa. For user emulation's currently supported systems, we can
119 * assume most signals are defined.
120 */
121
122static int gdb_signal_table[] = {
123 0,
124 TARGET_SIGHUP,
125 TARGET_SIGINT,
126 TARGET_SIGQUIT,
127 TARGET_SIGILL,
128 TARGET_SIGTRAP,
129 TARGET_SIGABRT,
130 -1, /* SIGEMT */
131 TARGET_SIGFPE,
132 TARGET_SIGKILL,
133 TARGET_SIGBUS,
134 TARGET_SIGSEGV,
135 TARGET_SIGSYS,
136 TARGET_SIGPIPE,
137 TARGET_SIGALRM,
138 TARGET_SIGTERM,
139 TARGET_SIGURG,
140 TARGET_SIGSTOP,
141 TARGET_SIGTSTP,
142 TARGET_SIGCONT,
143 TARGET_SIGCHLD,
144 TARGET_SIGTTIN,
145 TARGET_SIGTTOU,
146 TARGET_SIGIO,
147 TARGET_SIGXCPU,
148 TARGET_SIGXFSZ,
149 TARGET_SIGVTALRM,
150 TARGET_SIGPROF,
151 TARGET_SIGWINCH,
152 -1, /* SIGLOST */
153 TARGET_SIGUSR1,
154 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000155#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000156 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000157#else
158 -1,
159#endif
aurel32ca587a82008-12-18 22:44:13 +0000160 -1, /* SIGPOLL */
161 -1,
162 -1,
163 -1,
164 -1,
165 -1,
166 -1,
167 -1,
168 -1,
169 -1,
170 -1,
171 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000172#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000173 __SIGRTMIN + 1,
174 __SIGRTMIN + 2,
175 __SIGRTMIN + 3,
176 __SIGRTMIN + 4,
177 __SIGRTMIN + 5,
178 __SIGRTMIN + 6,
179 __SIGRTMIN + 7,
180 __SIGRTMIN + 8,
181 __SIGRTMIN + 9,
182 __SIGRTMIN + 10,
183 __SIGRTMIN + 11,
184 __SIGRTMIN + 12,
185 __SIGRTMIN + 13,
186 __SIGRTMIN + 14,
187 __SIGRTMIN + 15,
188 __SIGRTMIN + 16,
189 __SIGRTMIN + 17,
190 __SIGRTMIN + 18,
191 __SIGRTMIN + 19,
192 __SIGRTMIN + 20,
193 __SIGRTMIN + 21,
194 __SIGRTMIN + 22,
195 __SIGRTMIN + 23,
196 __SIGRTMIN + 24,
197 __SIGRTMIN + 25,
198 __SIGRTMIN + 26,
199 __SIGRTMIN + 27,
200 __SIGRTMIN + 28,
201 __SIGRTMIN + 29,
202 __SIGRTMIN + 30,
203 __SIGRTMIN + 31,
204 -1, /* SIGCANCEL */
205 __SIGRTMIN,
206 __SIGRTMIN + 32,
207 __SIGRTMIN + 33,
208 __SIGRTMIN + 34,
209 __SIGRTMIN + 35,
210 __SIGRTMIN + 36,
211 __SIGRTMIN + 37,
212 __SIGRTMIN + 38,
213 __SIGRTMIN + 39,
214 __SIGRTMIN + 40,
215 __SIGRTMIN + 41,
216 __SIGRTMIN + 42,
217 __SIGRTMIN + 43,
218 __SIGRTMIN + 44,
219 __SIGRTMIN + 45,
220 __SIGRTMIN + 46,
221 __SIGRTMIN + 47,
222 __SIGRTMIN + 48,
223 __SIGRTMIN + 49,
224 __SIGRTMIN + 50,
225 __SIGRTMIN + 51,
226 __SIGRTMIN + 52,
227 __SIGRTMIN + 53,
228 __SIGRTMIN + 54,
229 __SIGRTMIN + 55,
230 __SIGRTMIN + 56,
231 __SIGRTMIN + 57,
232 __SIGRTMIN + 58,
233 __SIGRTMIN + 59,
234 __SIGRTMIN + 60,
235 __SIGRTMIN + 61,
236 __SIGRTMIN + 62,
237 __SIGRTMIN + 63,
238 __SIGRTMIN + 64,
239 __SIGRTMIN + 65,
240 __SIGRTMIN + 66,
241 __SIGRTMIN + 67,
242 __SIGRTMIN + 68,
243 __SIGRTMIN + 69,
244 __SIGRTMIN + 70,
245 __SIGRTMIN + 71,
246 __SIGRTMIN + 72,
247 __SIGRTMIN + 73,
248 __SIGRTMIN + 74,
249 __SIGRTMIN + 75,
250 __SIGRTMIN + 76,
251 __SIGRTMIN + 77,
252 __SIGRTMIN + 78,
253 __SIGRTMIN + 79,
254 __SIGRTMIN + 80,
255 __SIGRTMIN + 81,
256 __SIGRTMIN + 82,
257 __SIGRTMIN + 83,
258 __SIGRTMIN + 84,
259 __SIGRTMIN + 85,
260 __SIGRTMIN + 86,
261 __SIGRTMIN + 87,
262 __SIGRTMIN + 88,
263 __SIGRTMIN + 89,
264 __SIGRTMIN + 90,
265 __SIGRTMIN + 91,
266 __SIGRTMIN + 92,
267 __SIGRTMIN + 93,
268 __SIGRTMIN + 94,
269 __SIGRTMIN + 95,
270 -1, /* SIGINFO */
271 -1, /* UNKNOWN */
272 -1, /* DEFAULT */
273 -1,
274 -1,
275 -1,
276 -1,
277 -1,
278 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000279#endif
aurel32ca587a82008-12-18 22:44:13 +0000280};
bellard8f447cc2006-06-14 15:21:14 +0000281#else
aurel32ca587a82008-12-18 22:44:13 +0000282/* In system mode we only need SIGINT and SIGTRAP; other signals
283 are not yet supported. */
284
285enum {
286 TARGET_SIGINT = 2,
287 TARGET_SIGTRAP = 5
288};
289
290static int gdb_signal_table[] = {
291 -1,
292 -1,
293 TARGET_SIGINT,
294 -1,
295 -1,
296 TARGET_SIGTRAP
297};
bellard8f447cc2006-06-14 15:21:14 +0000298#endif
bellardb4608c02003-06-27 17:34:32 +0000299
aurel32ca587a82008-12-18 22:44:13 +0000300#ifdef CONFIG_USER_ONLY
301static int target_signal_to_gdb (int sig)
302{
303 int i;
304 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
305 if (gdb_signal_table[i] == sig)
306 return i;
307 return GDB_SIGNAL_UNKNOWN;
308}
309#endif
310
311static int gdb_signal_to_target (int sig)
312{
313 if (sig < ARRAY_SIZE (gdb_signal_table))
314 return gdb_signal_table[sig];
315 else
316 return -1;
317}
318
pbrook56aebc82008-10-11 17:55:29 +0000319typedef struct GDBRegisterState {
320 int base_reg;
321 int num_regs;
322 gdb_reg_cb get_reg;
323 gdb_reg_cb set_reg;
324 const char *xml;
325 struct GDBRegisterState *next;
326} GDBRegisterState;
327
Luc Michel8f468632019-01-07 15:23:45 +0000328typedef struct GDBProcess {
329 uint32_t pid;
330 bool attached;
Luc Michelc145eea2019-01-07 15:23:46 +0000331
332 char target_xml[1024];
Luc Michel8f468632019-01-07 15:23:45 +0000333} GDBProcess;
334
bellard858693c2004-03-31 18:52:07 +0000335enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000336 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000337 RS_IDLE,
338 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400339 RS_GETLINE_ESC,
340 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000341 RS_CHKSUM1,
342 RS_CHKSUM2,
343};
bellard858693c2004-03-31 18:52:07 +0000344typedef struct GDBState {
Alex Bennée8d98c442020-03-16 17:21:33 +0000345 bool init; /* have we been initialised? */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200346 CPUState *c_cpu; /* current CPU for step/continue ops */
347 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200348 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000349 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000350 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000351 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400352 int line_sum; /* running checksum */
353 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000354 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000355 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000356 int signal;
bellard41625032005-04-24 10:07:11 +0000357#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000358 int fd;
bellard41625032005-04-24 10:07:11 +0000359 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000360#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300361 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300362 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000363#endif
Luc Michel8f468632019-01-07 15:23:45 +0000364 bool multiprocess;
365 GDBProcess *processes;
366 int process_num;
Meador Ingecdb432b2012-03-15 17:49:45 +0000367 char syscall_buf[256];
368 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000369} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000370
edgar_igl60897d32008-05-09 08:25:14 +0000371/* By default use no IRQs and no timers while single stepping so as to
372 * make single stepping like an ICE HW step.
373 */
374static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
375
Alex Bennée8d98c442020-03-16 17:21:33 +0000376static GDBState gdbserver_state;
377
378static void init_gdbserver_state(void)
379{
380 g_assert(!gdbserver_state.init);
381 memset(&gdbserver_state, 0, sizeof(GDBState));
382 gdbserver_state.init = true;
383}
384
385#ifndef CONFIG_USER_ONLY
386static void reset_gdbserver_state(void)
387{
388 g_free(gdbserver_state.processes);
389 gdbserver_state.processes = NULL;
390 gdbserver_state.process_num = 0;
391}
392#endif
aliguori880a7572008-11-18 20:30:24 +0000393
Andreas Färber5b50e792013-06-29 04:18:45 +0200394bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000395
bellard1fddef42005-04-17 19:16:13 +0000396#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000397/* XXX: This is not thread safe. Do we care? */
398static int gdbserver_fd = -1;
399
bellard858693c2004-03-31 18:52:07 +0000400static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000401{
402 uint8_t ch;
403 int ret;
404
405 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000406 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000407 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000408 if (errno == ECONNRESET)
409 s->fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200410 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000411 return -1;
412 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000413 close(s->fd);
414 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000415 return -1;
416 } else {
417 break;
418 }
419 }
420 return ch;
421}
pbrook4046d912007-01-28 01:53:16 +0000422#endif
bellardb4608c02003-06-27 17:34:32 +0000423
blueswir1654efcf2009-04-18 07:29:59 +0000424static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000425 GDB_SYS_UNKNOWN,
426 GDB_SYS_ENABLED,
427 GDB_SYS_DISABLED,
428} gdb_syscall_mode;
429
Liviu Ionescua38bb072014-12-11 12:07:48 +0000430/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000431int use_gdb_syscalls(void)
432{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100433 SemihostingTarget target = semihosting_get_target();
434 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000435 /* -semihosting-config target=native */
436 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100437 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000438 /* -semihosting-config target=gdb */
439 return true;
440 }
441
442 /* -semihosting-config target=auto */
443 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000444 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
Alex Bennée8d98c442020-03-16 17:21:33 +0000445 gdb_syscall_mode = gdbserver_state.init ?
446 GDB_SYS_ENABLED : GDB_SYS_DISABLED;
pbrooka2d1eba2007-01-28 03:10:55 +0000447 }
448 return gdb_syscall_mode == GDB_SYS_ENABLED;
449}
450
edgar_iglba70a622008-03-14 06:10:42 +0000451/* Resume execution. */
452static inline void gdb_continue(GDBState *s)
453{
Doug Gale5c9522b2017-12-02 20:30:37 -0500454
edgar_iglba70a622008-03-14 06:10:42 +0000455#ifdef CONFIG_USER_ONLY
456 s->running_state = 1;
Doug Gale5c9522b2017-12-02 20:30:37 -0500457 trace_gdbstub_op_continue();
edgar_iglba70a622008-03-14 06:10:42 +0000458#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200459 if (!runstate_needs_reset()) {
Doug Gale5c9522b2017-12-02 20:30:37 -0500460 trace_gdbstub_op_continue();
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200461 vm_start();
462 }
edgar_iglba70a622008-03-14 06:10:42 +0000463#endif
464}
465
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100466/*
467 * Resume execution, per CPU actions. For user-mode emulation it's
468 * equivalent to gdb_continue.
469 */
470static int gdb_continue_partial(GDBState *s, char *newstates)
471{
472 CPUState *cpu;
473 int res = 0;
474#ifdef CONFIG_USER_ONLY
475 /*
476 * This is not exactly accurate, but it's an improvement compared to the
477 * previous situation, where only one CPU would be single-stepped.
478 */
479 CPU_FOREACH(cpu) {
480 if (newstates[cpu->cpu_index] == 's') {
Doug Gale5c9522b2017-12-02 20:30:37 -0500481 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100482 cpu_single_step(cpu, sstep_flags);
483 }
484 }
485 s->running_state = 1;
486#else
487 int flag = 0;
488
489 if (!runstate_needs_reset()) {
490 if (vm_prepare_start()) {
491 return 0;
492 }
493
494 CPU_FOREACH(cpu) {
495 switch (newstates[cpu->cpu_index]) {
496 case 0:
497 case 1:
498 break; /* nothing to do here */
499 case 's':
Doug Gale5c9522b2017-12-02 20:30:37 -0500500 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100501 cpu_single_step(cpu, sstep_flags);
502 cpu_resume(cpu);
503 flag = 1;
504 break;
505 case 'c':
Doug Gale5c9522b2017-12-02 20:30:37 -0500506 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100507 cpu_resume(cpu);
508 flag = 1;
509 break;
510 default:
511 res = -1;
512 break;
513 }
514 }
515 }
516 if (flag) {
517 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
518 }
519#endif
520 return res;
521}
522
bellard858693c2004-03-31 18:52:07 +0000523static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000524{
pbrook4046d912007-01-28 01:53:16 +0000525#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000526 int ret;
527
528 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000529 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000530 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200531 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000532 return;
533 } else {
534 buf += ret;
535 len -= ret;
536 }
537 }
pbrook4046d912007-01-28 01:53:16 +0000538#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100539 /* XXX this blocks entire thread. Rewrite to use
540 * qemu_chr_fe_write and background I/O callbacks */
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300541 qemu_chr_fe_write_all(&s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000542#endif
bellardb4608c02003-06-27 17:34:32 +0000543}
544
545static inline int fromhex(int v)
546{
547 if (v >= '0' && v <= '9')
548 return v - '0';
549 else if (v >= 'A' && v <= 'F')
550 return v - 'A' + 10;
551 else if (v >= 'a' && v <= 'f')
552 return v - 'a' + 10;
553 else
554 return 0;
555}
556
557static inline int tohex(int v)
558{
559 if (v < 10)
560 return v + '0';
561 else
562 return v - 10 + 'a';
563}
564
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -0300565/* writes 2*len+1 bytes in buf */
bellardb4608c02003-06-27 17:34:32 +0000566static void memtohex(char *buf, const uint8_t *mem, int len)
567{
568 int i, c;
569 char *q;
570 q = buf;
571 for(i = 0; i < len; i++) {
572 c = mem[i];
573 *q++ = tohex(c >> 4);
574 *q++ = tohex(c & 0xf);
575 }
576 *q = '\0';
577}
578
579static void hextomem(uint8_t *mem, const char *buf, int len)
580{
581 int i;
582
583 for(i = 0; i < len; i++) {
584 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
585 buf += 2;
586 }
587}
588
Doug Gale5c9522b2017-12-02 20:30:37 -0500589static void hexdump(const char *buf, int len,
590 void (*trace_fn)(size_t ofs, char const *text))
591{
592 char line_buffer[3 * 16 + 4 + 16 + 1];
593
594 size_t i;
595 for (i = 0; i < len || (i & 0xF); ++i) {
596 size_t byte_ofs = i & 15;
597
598 if (byte_ofs == 0) {
599 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
600 line_buffer[3 * 16 + 4 + 16] = 0;
601 }
602
603 size_t col_group = (i >> 2) & 3;
604 size_t hex_col = byte_ofs * 3 + col_group;
605 size_t txt_col = 3 * 16 + 4 + byte_ofs;
606
607 if (i < len) {
608 char value = buf[i];
609
610 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
611 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
612 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
613 ? value
614 : '.';
615 }
616
617 if (byte_ofs == 0xF)
618 trace_fn(i & -16, line_buffer);
619 }
620}
621
bellardb4608c02003-06-27 17:34:32 +0000622/* return -1 if error, 0 if OK */
Doug Gale5c9522b2017-12-02 20:30:37 -0500623static int put_packet_binary(GDBState *s, const char *buf, int len, bool dump)
bellardb4608c02003-06-27 17:34:32 +0000624{
pbrook56aebc82008-10-11 17:55:29 +0000625 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000626 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000627
Doug Gale5c9522b2017-12-02 20:30:37 -0500628 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
629 hexdump(buf, len, trace_gdbstub_io_binaryreply);
630 }
631
bellardb4608c02003-06-27 17:34:32 +0000632 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000633 p = s->last_packet;
634 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000635 memcpy(p, buf, len);
636 p += len;
bellardb4608c02003-06-27 17:34:32 +0000637 csum = 0;
638 for(i = 0; i < len; i++) {
639 csum += buf[i];
640 }
pbrook4046d912007-01-28 01:53:16 +0000641 *(p++) = '#';
642 *(p++) = tohex((csum >> 4) & 0xf);
643 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000644
pbrook4046d912007-01-28 01:53:16 +0000645 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000646 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000647
pbrook4046d912007-01-28 01:53:16 +0000648#ifdef CONFIG_USER_ONLY
649 i = get_char(s);
650 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000651 return -1;
pbrook4046d912007-01-28 01:53:16 +0000652 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000653 break;
pbrook4046d912007-01-28 01:53:16 +0000654#else
655 break;
656#endif
bellardb4608c02003-06-27 17:34:32 +0000657 }
658 return 0;
659}
660
pbrook56aebc82008-10-11 17:55:29 +0000661/* return -1 if error, 0 if OK */
662static int put_packet(GDBState *s, const char *buf)
663{
Doug Gale5c9522b2017-12-02 20:30:37 -0500664 trace_gdbstub_io_reply(buf);
pbrook56aebc82008-10-11 17:55:29 +0000665
Doug Gale5c9522b2017-12-02 20:30:37 -0500666 return put_packet_binary(s, buf, strlen(buf), false);
pbrook56aebc82008-10-11 17:55:29 +0000667}
668
pbrook56aebc82008-10-11 17:55:29 +0000669/* Encode data using the encoding for 'x' packets. */
670static int memtox(char *buf, const char *mem, int len)
671{
672 char *p = buf;
673 char c;
674
675 while (len--) {
676 c = *(mem++);
677 switch (c) {
678 case '#': case '$': case '*': case '}':
679 *(p++) = '}';
680 *(p++) = c ^ 0x20;
681 break;
682 default:
683 *(p++) = c;
684 break;
685 }
686 }
687 return p - buf;
688}
689
Luc Michel1a227332019-01-07 15:23:45 +0000690static uint32_t gdb_get_cpu_pid(const GDBState *s, CPUState *cpu)
691{
Luc Michel1a227332019-01-07 15:23:45 +0000692 /* TODO: In user mode, we should use the task state PID */
Peter Maydell46f5abc2019-01-29 11:46:06 +0000693 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
694 /* Return the default process' PID */
695 return s->processes[s->process_num - 1].pid;
696 }
697 return cpu->cluster_index + 1;
Luc Michel1a227332019-01-07 15:23:45 +0000698}
699
Luc Michel7d8c87d2019-01-07 15:23:45 +0000700static GDBProcess *gdb_get_process(const GDBState *s, uint32_t pid)
701{
702 int i;
703
704 if (!pid) {
705 /* 0 means any process, we take the first one */
706 return &s->processes[0];
707 }
708
709 for (i = 0; i < s->process_num; i++) {
710 if (s->processes[i].pid == pid) {
711 return &s->processes[i];
712 }
713 }
714
715 return NULL;
716}
717
718static GDBProcess *gdb_get_cpu_process(const GDBState *s, CPUState *cpu)
719{
720 return gdb_get_process(s, gdb_get_cpu_pid(s, cpu));
721}
722
723static CPUState *find_cpu(uint32_t thread_id)
724{
725 CPUState *cpu;
726
727 CPU_FOREACH(cpu) {
728 if (cpu_gdb_index(cpu) == thread_id) {
729 return cpu;
730 }
731 }
732
733 return NULL;
734}
735
Luc Michele40e5202019-01-07 15:23:46 +0000736static CPUState *get_first_cpu_in_process(const GDBState *s,
737 GDBProcess *process)
738{
739 CPUState *cpu;
740
741 CPU_FOREACH(cpu) {
742 if (gdb_get_cpu_pid(s, cpu) == process->pid) {
743 return cpu;
744 }
745 }
746
747 return NULL;
748}
749
750static CPUState *gdb_next_cpu_in_process(const GDBState *s, CPUState *cpu)
751{
752 uint32_t pid = gdb_get_cpu_pid(s, cpu);
753 cpu = CPU_NEXT(cpu);
754
755 while (cpu) {
756 if (gdb_get_cpu_pid(s, cpu) == pid) {
757 break;
758 }
759
760 cpu = CPU_NEXT(cpu);
761 }
762
763 return cpu;
764}
765
Luc Michele40e5202019-01-07 15:23:46 +0000766/* Return the cpu following @cpu, while ignoring unattached processes. */
767static CPUState *gdb_next_attached_cpu(const GDBState *s, CPUState *cpu)
768{
769 cpu = CPU_NEXT(cpu);
770
771 while (cpu) {
772 if (gdb_get_cpu_process(s, cpu)->attached) {
773 break;
774 }
775
776 cpu = CPU_NEXT(cpu);
777 }
778
779 return cpu;
780}
781
782/* Return the first attached cpu */
783static CPUState *gdb_first_attached_cpu(const GDBState *s)
784{
785 CPUState *cpu = first_cpu;
786 GDBProcess *process = gdb_get_cpu_process(s, cpu);
787
788 if (!process->attached) {
789 return gdb_next_attached_cpu(s, cpu);
790 }
791
792 return cpu;
793}
794
Luc Michelab65eed2019-01-29 11:46:03 +0000795static CPUState *gdb_get_cpu(const GDBState *s, uint32_t pid, uint32_t tid)
796{
797 GDBProcess *process;
798 CPUState *cpu;
799
800 if (!pid && !tid) {
801 /* 0 means any process/thread, we take the first attached one */
802 return gdb_first_attached_cpu(s);
803 } else if (pid && !tid) {
804 /* any thread in a specific process */
805 process = gdb_get_process(s, pid);
806
807 if (process == NULL) {
808 return NULL;
809 }
810
811 if (!process->attached) {
812 return NULL;
813 }
814
815 return get_first_cpu_in_process(s, process);
816 } else {
817 /* a specific thread */
818 cpu = find_cpu(tid);
819
820 if (cpu == NULL) {
821 return NULL;
822 }
823
824 process = gdb_get_cpu_process(s, cpu);
825
826 if (pid && process->pid != pid) {
827 return NULL;
828 }
829
830 if (!process->attached) {
831 return NULL;
832 }
833
834 return cpu;
835 }
836}
837
Luc Michelc145eea2019-01-07 15:23:46 +0000838static const char *get_feature_xml(const GDBState *s, const char *p,
839 const char **newp, GDBProcess *process)
pbrook56aebc82008-10-11 17:55:29 +0000840{
pbrook56aebc82008-10-11 17:55:29 +0000841 size_t len;
842 int i;
843 const char *name;
Luc Michelc145eea2019-01-07 15:23:46 +0000844 CPUState *cpu = get_first_cpu_in_process(s, process);
845 CPUClass *cc = CPU_GET_CLASS(cpu);
pbrook56aebc82008-10-11 17:55:29 +0000846
847 len = 0;
848 while (p[len] && p[len] != ':')
849 len++;
850 *newp = p + len;
851
852 name = NULL;
853 if (strncmp(p, "target.xml", len) == 0) {
Luc Michelc145eea2019-01-07 15:23:46 +0000854 char *buf = process->target_xml;
855 const size_t buf_sz = sizeof(process->target_xml);
pbrook56aebc82008-10-11 17:55:29 +0000856
Luc Michelc145eea2019-01-07 15:23:46 +0000857 /* Generate the XML description for this CPU. */
858 if (!buf[0]) {
859 GDBRegisterState *r;
860
861 pstrcat(buf, buf_sz,
David Hildenbrandb3820e62015-12-03 13:14:41 +0100862 "<?xml version=\"1.0\"?>"
863 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
864 "<target>");
865 if (cc->gdb_arch_name) {
866 gchar *arch = cc->gdb_arch_name(cpu);
Luc Michelc145eea2019-01-07 15:23:46 +0000867 pstrcat(buf, buf_sz, "<architecture>");
868 pstrcat(buf, buf_sz, arch);
869 pstrcat(buf, buf_sz, "</architecture>");
David Hildenbrandb3820e62015-12-03 13:14:41 +0100870 g_free(arch);
871 }
Luc Michelc145eea2019-01-07 15:23:46 +0000872 pstrcat(buf, buf_sz, "<xi:include href=\"");
873 pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
874 pstrcat(buf, buf_sz, "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200875 for (r = cpu->gdb_regs; r; r = r->next) {
Luc Michelc145eea2019-01-07 15:23:46 +0000876 pstrcat(buf, buf_sz, "<xi:include href=\"");
877 pstrcat(buf, buf_sz, r->xml);
878 pstrcat(buf, buf_sz, "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000879 }
Luc Michelc145eea2019-01-07 15:23:46 +0000880 pstrcat(buf, buf_sz, "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000881 }
Luc Michelc145eea2019-01-07 15:23:46 +0000882 return buf;
pbrook56aebc82008-10-11 17:55:29 +0000883 }
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100884 if (cc->gdb_get_dynamic_xml) {
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100885 char *xmlname = g_strndup(p, len);
886 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
887
888 g_free(xmlname);
889 if (xml) {
890 return xml;
891 }
892 }
pbrook56aebc82008-10-11 17:55:29 +0000893 for (i = 0; ; i++) {
894 name = xml_builtin[i][0];
895 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
896 break;
897 }
898 return name ? xml_builtin[i][1] : NULL;
899}
pbrook56aebc82008-10-11 17:55:29 +0000900
Andreas Färber385b9f02013-06-27 18:25:36 +0200901static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000902{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200903 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200904 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000905 GDBRegisterState *r;
906
Andreas Färbera0e372f2013-06-28 23:18:47 +0200907 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200908 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200909 }
pbrook56aebc82008-10-11 17:55:29 +0000910
Andreas Färbereac8b352013-06-28 21:11:37 +0200911 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000912 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
913 return r->get_reg(env, mem_buf, reg - r->base_reg);
914 }
915 }
916 return 0;
917}
918
Andreas Färber385b9f02013-06-27 18:25:36 +0200919static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000920{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200921 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200922 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000923 GDBRegisterState *r;
924
Andreas Färbera0e372f2013-06-28 23:18:47 +0200925 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200926 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200927 }
pbrook56aebc82008-10-11 17:55:29 +0000928
Andreas Färbereac8b352013-06-28 21:11:37 +0200929 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000930 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
931 return r->set_reg(env, mem_buf, reg - r->base_reg);
932 }
933 }
934 return 0;
935}
936
937/* Register a supplemental set of CPU registers. If g_pos is nonzero it
938 specifies the first register number and these registers are included in
939 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
940 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
941 */
942
Andreas Färber22169d42013-06-28 21:27:39 +0200943void gdb_register_coprocessor(CPUState *cpu,
944 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
945 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000946{
947 GDBRegisterState *s;
948 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000949
Andreas Färbereac8b352013-06-28 21:11:37 +0200950 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000951 while (*p) {
952 /* Check for duplicates. */
953 if (strcmp((*p)->xml, xml) == 0)
954 return;
955 p = &(*p)->next;
956 }
Stefan Weil9643c252011-10-18 22:25:38 +0200957
958 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200959 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200960 s->num_regs = num_regs;
961 s->get_reg = get_reg;
962 s->set_reg = set_reg;
963 s->xml = xml;
964
pbrook56aebc82008-10-11 17:55:29 +0000965 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200966 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000967 *p = s;
968 if (g_pos) {
969 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800970 error_report("Error: Bad gdb register numbering for '%s', "
971 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200972 } else {
973 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000974 }
975 }
976}
977
aliguoria1d1bb32008-11-18 20:07:32 +0000978#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100979/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
980static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
981{
982 static const int xlat[] = {
983 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
984 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
985 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
986 };
987
988 CPUClass *cc = CPU_GET_CLASS(cpu);
989 int cputype = xlat[gdbtype];
990
991 if (cc->gdb_stop_before_watchpoint) {
992 cputype |= BP_STOP_BEFORE_ACCESS;
993 }
994 return cputype;
995}
aliguoria1d1bb32008-11-18 20:07:32 +0000996#endif
997
Jon Doron77f6ce52019-05-29 09:41:35 +0300998static int gdb_breakpoint_insert(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +0000999{
Andreas Färber182735e2013-05-29 22:29:20 +02001000 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001001 int err = 0;
1002
Andreas Färber62278812013-06-27 17:12:06 +02001003 if (kvm_enabled()) {
Alex Bennée8d98c442020-03-16 17:21:33 +00001004 return kvm_insert_breakpoint(gdbserver_state.c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001005 }
aliguorie22a25c2009-03-12 20:12:48 +00001006
aliguoria1d1bb32008-11-18 20:07:32 +00001007 switch (type) {
1008 case GDB_BREAKPOINT_SW:
1009 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001010 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001011 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
1012 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001013 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001014 }
aliguori880a7572008-11-18 20:30:24 +00001015 }
1016 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001017#ifndef CONFIG_USER_ONLY
1018 case GDB_WATCHPOINT_WRITE:
1019 case GDB_WATCHPOINT_READ:
1020 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001021 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001022 err = cpu_watchpoint_insert(cpu, addr, len,
1023 xlat_gdb_type(cpu, type), NULL);
1024 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001025 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +01001026 }
aliguori880a7572008-11-18 20:30:24 +00001027 }
1028 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001029#endif
1030 default:
1031 return -ENOSYS;
1032 }
1033}
1034
Jon Doron77f6ce52019-05-29 09:41:35 +03001035static int gdb_breakpoint_remove(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +00001036{
Andreas Färber182735e2013-05-29 22:29:20 +02001037 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001038 int err = 0;
1039
Andreas Färber62278812013-06-27 17:12:06 +02001040 if (kvm_enabled()) {
Alex Bennée8d98c442020-03-16 17:21:33 +00001041 return kvm_remove_breakpoint(gdbserver_state.c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001042 }
aliguorie22a25c2009-03-12 20:12:48 +00001043
aliguoria1d1bb32008-11-18 20:07:32 +00001044 switch (type) {
1045 case GDB_BREAKPOINT_SW:
1046 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001047 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001048 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
1049 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001050 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001051 }
aliguori880a7572008-11-18 20:30:24 +00001052 }
1053 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001054#ifndef CONFIG_USER_ONLY
1055 case GDB_WATCHPOINT_WRITE:
1056 case GDB_WATCHPOINT_READ:
1057 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001058 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001059 err = cpu_watchpoint_remove(cpu, addr, len,
1060 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +00001061 if (err)
1062 break;
1063 }
1064 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001065#endif
1066 default:
1067 return -ENOSYS;
1068 }
1069}
1070
Luc Michel546f3c62019-01-07 15:23:46 +00001071static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu)
1072{
1073 cpu_breakpoint_remove_all(cpu, BP_GDB);
1074#ifndef CONFIG_USER_ONLY
1075 cpu_watchpoint_remove_all(cpu, BP_GDB);
1076#endif
1077}
1078
1079static void gdb_process_breakpoint_remove_all(const GDBState *s, GDBProcess *p)
1080{
1081 CPUState *cpu = get_first_cpu_in_process(s, p);
1082
1083 while (cpu) {
1084 gdb_cpu_breakpoint_remove_all(cpu);
1085 cpu = gdb_next_cpu_in_process(s, cpu);
1086 }
1087}
1088
aliguori880a7572008-11-18 20:30:24 +00001089static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +00001090{
Andreas Färber182735e2013-05-29 22:29:20 +02001091 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001092
aliguorie22a25c2009-03-12 20:12:48 +00001093 if (kvm_enabled()) {
Alex Bennée8d98c442020-03-16 17:21:33 +00001094 kvm_remove_all_breakpoints(gdbserver_state.c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +00001095 return;
1096 }
1097
Andreas Färberbdc44642013-06-24 23:50:24 +02001098 CPU_FOREACH(cpu) {
Luc Michel546f3c62019-01-07 15:23:46 +00001099 gdb_cpu_breakpoint_remove_all(cpu);
aliguori880a7572008-11-18 20:30:24 +00001100 }
aliguoria1d1bb32008-11-18 20:07:32 +00001101}
1102
aurel32fab9d282009-04-08 21:29:37 +00001103static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
1104{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001105 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +02001106
1107 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -07001108 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +00001109}
1110
Luc Michel1a227332019-01-07 15:23:45 +00001111static char *gdb_fmt_thread_id(const GDBState *s, CPUState *cpu,
1112 char *buf, size_t buf_size)
1113{
1114 if (s->multiprocess) {
1115 snprintf(buf, buf_size, "p%02x.%02x",
1116 gdb_get_cpu_pid(s, cpu), cpu_gdb_index(cpu));
1117 } else {
1118 snprintf(buf, buf_size, "%02x", cpu_gdb_index(cpu));
1119 }
1120
1121 return buf;
1122}
1123
Luc Michel7d8c87d2019-01-07 15:23:45 +00001124typedef enum GDBThreadIdKind {
1125 GDB_ONE_THREAD = 0,
1126 GDB_ALL_THREADS, /* One process, all threads */
1127 GDB_ALL_PROCESSES,
1128 GDB_READ_THREAD_ERR
1129} GDBThreadIdKind;
1130
1131static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1132 uint32_t *pid, uint32_t *tid)
1133{
1134 unsigned long p, t;
1135 int ret;
1136
1137 if (*buf == 'p') {
1138 buf++;
1139 ret = qemu_strtoul(buf, &buf, 16, &p);
1140
1141 if (ret) {
1142 return GDB_READ_THREAD_ERR;
1143 }
1144
1145 /* Skip '.' */
1146 buf++;
1147 } else {
1148 p = 1;
1149 }
1150
1151 ret = qemu_strtoul(buf, &buf, 16, &t);
1152
1153 if (ret) {
1154 return GDB_READ_THREAD_ERR;
1155 }
1156
1157 *end_buf = buf;
1158
1159 if (p == -1) {
1160 return GDB_ALL_PROCESSES;
1161 }
1162
1163 if (pid) {
1164 *pid = p;
1165 }
1166
1167 if (t == -1) {
1168 return GDB_ALL_THREADS;
1169 }
1170
1171 if (tid) {
1172 *tid = t;
1173 }
1174
1175 return GDB_ONE_THREAD;
1176}
1177
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001178/**
1179 * gdb_handle_vcont - Parses and handles a vCont packet.
1180 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1181 * a format error, 0 on success.
1182 */
1183static int gdb_handle_vcont(GDBState *s, const char *p)
1184{
Luc Michele40e5202019-01-07 15:23:46 +00001185 int res, signal = 0;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001186 char cur_action;
1187 char *newstates;
1188 unsigned long tmp;
Luc Michele40e5202019-01-07 15:23:46 +00001189 uint32_t pid, tid;
1190 GDBProcess *process;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001191 CPUState *cpu;
Luc Michelc99ef792019-03-26 12:53:26 +00001192 GDBThreadIdKind kind;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001193#ifdef CONFIG_USER_ONLY
1194 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
1195
1196 CPU_FOREACH(cpu) {
1197 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1198 }
Like Xu5cc87672019-05-19 04:54:21 +08001199#else
1200 MachineState *ms = MACHINE(qdev_get_machine());
1201 unsigned int max_cpus = ms->smp.max_cpus;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001202#endif
1203 /* uninitialised CPUs stay 0 */
1204 newstates = g_new0(char, max_cpus);
1205
1206 /* mark valid CPUs with 1 */
1207 CPU_FOREACH(cpu) {
1208 newstates[cpu->cpu_index] = 1;
1209 }
1210
1211 /*
1212 * res keeps track of what error we are returning, with -ENOTSUP meaning
1213 * that the command is unknown or unsupported, thus returning an empty
1214 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1215 * or incorrect parameters passed.
1216 */
1217 res = 0;
1218 while (*p) {
1219 if (*p++ != ';') {
1220 res = -ENOTSUP;
1221 goto out;
1222 }
1223
1224 cur_action = *p++;
1225 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +01001226 cur_action = qemu_tolower(cur_action);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001227 res = qemu_strtoul(p + 1, &p, 16, &tmp);
1228 if (res) {
1229 goto out;
1230 }
1231 signal = gdb_signal_to_target(tmp);
1232 } else if (cur_action != 'c' && cur_action != 's') {
1233 /* unknown/invalid/unsupported command */
1234 res = -ENOTSUP;
1235 goto out;
1236 }
Luc Michele40e5202019-01-07 15:23:46 +00001237
Luc Michelc99ef792019-03-26 12:53:26 +00001238 if (*p == '\0' || *p == ';') {
1239 /*
1240 * No thread specifier, action is on "all threads". The
1241 * specification is unclear regarding the process to act on. We
1242 * choose all processes.
1243 */
1244 kind = GDB_ALL_PROCESSES;
1245 } else if (*p++ == ':') {
1246 kind = read_thread_id(p, &p, &pid, &tid);
1247 } else {
Luc Michele40e5202019-01-07 15:23:46 +00001248 res = -ENOTSUP;
1249 goto out;
1250 }
1251
Luc Michelc99ef792019-03-26 12:53:26 +00001252 switch (kind) {
Luc Michele40e5202019-01-07 15:23:46 +00001253 case GDB_READ_THREAD_ERR:
1254 res = -EINVAL;
1255 goto out;
1256
1257 case GDB_ALL_PROCESSES:
1258 cpu = gdb_first_attached_cpu(s);
1259 while (cpu) {
1260 if (newstates[cpu->cpu_index] == 1) {
1261 newstates[cpu->cpu_index] = cur_action;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001262 }
Luc Michele40e5202019-01-07 15:23:46 +00001263
1264 cpu = gdb_next_attached_cpu(s, cpu);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001265 }
Luc Michele40e5202019-01-07 15:23:46 +00001266 break;
1267
1268 case GDB_ALL_THREADS:
1269 process = gdb_get_process(s, pid);
1270
1271 if (!process->attached) {
1272 res = -EINVAL;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001273 goto out;
1274 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001275
Luc Michele40e5202019-01-07 15:23:46 +00001276 cpu = get_first_cpu_in_process(s, process);
1277 while (cpu) {
1278 if (newstates[cpu->cpu_index] == 1) {
1279 newstates[cpu->cpu_index] = cur_action;
1280 }
1281
1282 cpu = gdb_next_cpu_in_process(s, cpu);
1283 }
1284 break;
1285
1286 case GDB_ONE_THREAD:
1287 cpu = gdb_get_cpu(s, pid, tid);
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001288
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001289 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001290 if (!cpu) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001291 res = -EINVAL;
1292 goto out;
1293 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001294
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001295 /* only use if no previous match occourred */
1296 if (newstates[cpu->cpu_index] == 1) {
1297 newstates[cpu->cpu_index] = cur_action;
1298 }
Luc Michele40e5202019-01-07 15:23:46 +00001299 break;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001300 }
1301 }
1302 s->signal = signal;
1303 gdb_continue_partial(s, newstates);
1304
1305out:
1306 g_free(newstates);
1307
1308 return res;
1309}
1310
Jon Dorond14055d2019-05-29 09:41:29 +03001311typedef union GdbCmdVariant {
1312 const char *data;
1313 uint8_t opcode;
1314 unsigned long val_ul;
1315 unsigned long long val_ull;
1316 struct {
1317 GDBThreadIdKind kind;
1318 uint32_t pid;
1319 uint32_t tid;
1320 } thread_id;
1321} GdbCmdVariant;
1322
1323static const char *cmd_next_param(const char *param, const char delimiter)
1324{
1325 static const char all_delimiters[] = ",;:=";
1326 char curr_delimiters[2] = {0};
1327 const char *delimiters;
1328
1329 if (delimiter == '?') {
1330 delimiters = all_delimiters;
1331 } else if (delimiter == '0') {
1332 return strchr(param, '\0');
1333 } else if (delimiter == '.' && *param) {
1334 return param + 1;
1335 } else {
1336 curr_delimiters[0] = delimiter;
1337 delimiters = curr_delimiters;
1338 }
1339
1340 param += strcspn(param, delimiters);
1341 if (*param) {
1342 param++;
1343 }
1344 return param;
1345}
1346
1347static int cmd_parse_params(const char *data, const char *schema,
1348 GdbCmdVariant *params, int *num_params)
1349{
1350 int curr_param;
1351 const char *curr_schema, *curr_data;
1352
1353 *num_params = 0;
1354
1355 if (!schema) {
1356 return 0;
1357 }
1358
1359 curr_schema = schema;
1360 curr_param = 0;
1361 curr_data = data;
1362 while (curr_schema[0] && curr_schema[1] && *curr_data) {
1363 switch (curr_schema[0]) {
1364 case 'l':
1365 if (qemu_strtoul(curr_data, &curr_data, 16,
1366 &params[curr_param].val_ul)) {
1367 return -EINVAL;
1368 }
1369 curr_param++;
1370 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1371 break;
1372 case 'L':
1373 if (qemu_strtou64(curr_data, &curr_data, 16,
1374 (uint64_t *)&params[curr_param].val_ull)) {
1375 return -EINVAL;
1376 }
1377 curr_param++;
1378 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1379 break;
1380 case 's':
1381 params[curr_param].data = curr_data;
1382 curr_param++;
1383 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1384 break;
1385 case 'o':
1386 params[curr_param].opcode = *(uint8_t *)curr_data;
1387 curr_param++;
1388 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1389 break;
1390 case 't':
1391 params[curr_param].thread_id.kind =
1392 read_thread_id(curr_data, &curr_data,
1393 &params[curr_param].thread_id.pid,
1394 &params[curr_param].thread_id.tid);
1395 curr_param++;
1396 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1397 break;
1398 case '?':
1399 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1400 break;
1401 default:
1402 return -EINVAL;
1403 }
1404 curr_schema += 2;
1405 }
1406
1407 *num_params = curr_param;
1408 return 0;
1409}
1410
1411typedef struct GdbCmdContext {
1412 GDBState *s;
1413 GdbCmdVariant *params;
1414 int num_params;
1415 uint8_t mem_buf[MAX_PACKET_LENGTH];
1416 char str_buf[MAX_PACKET_LENGTH + 1];
1417} GdbCmdContext;
1418
1419typedef void (*GdbCmdHandler)(GdbCmdContext *gdb_ctx, void *user_ctx);
1420
1421/*
1422 * cmd_startswith -> cmd is compared using startswith
1423 *
1424 *
1425 * schema definitions:
1426 * Each schema parameter entry consists of 2 chars,
1427 * the first char represents the parameter type handling
1428 * the second char represents the delimiter for the next parameter
1429 *
1430 * Currently supported schema types:
1431 * 'l' -> unsigned long (stored in .val_ul)
1432 * 'L' -> unsigned long long (stored in .val_ull)
1433 * 's' -> string (stored in .data)
1434 * 'o' -> single char (stored in .opcode)
1435 * 't' -> thread id (stored in .thread_id)
1436 * '?' -> skip according to delimiter
1437 *
1438 * Currently supported delimiters:
1439 * '?' -> Stop at any delimiter (",;:=\0")
1440 * '0' -> Stop at "\0"
1441 * '.' -> Skip 1 char unless reached "\0"
1442 * Any other value is treated as the delimiter value itself
1443 */
1444typedef struct GdbCmdParseEntry {
1445 GdbCmdHandler handler;
1446 const char *cmd;
1447 bool cmd_startswith;
1448 const char *schema;
1449} GdbCmdParseEntry;
1450
1451static inline int startswith(const char *string, const char *pattern)
1452{
1453 return !strncmp(string, pattern, strlen(pattern));
1454}
1455
Jon Dorond14055d2019-05-29 09:41:29 +03001456static int process_string_cmd(GDBState *s, void *user_ctx, const char *data,
1457 const GdbCmdParseEntry *cmds, int num_cmds)
1458{
1459 int i, schema_len, max_num_params = 0;
1460 GdbCmdContext gdb_ctx;
1461
1462 if (!cmds) {
1463 return -1;
1464 }
1465
1466 for (i = 0; i < num_cmds; i++) {
1467 const GdbCmdParseEntry *cmd = &cmds[i];
1468 g_assert(cmd->handler && cmd->cmd);
1469
1470 if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
1471 (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
1472 continue;
1473 }
1474
1475 if (cmd->schema) {
1476 schema_len = strlen(cmd->schema);
1477 if (schema_len % 2) {
1478 return -2;
1479 }
1480
1481 max_num_params = schema_len / 2;
1482 }
1483
1484 gdb_ctx.params =
1485 (GdbCmdVariant *)alloca(sizeof(*gdb_ctx.params) * max_num_params);
1486 memset(gdb_ctx.params, 0, sizeof(*gdb_ctx.params) * max_num_params);
1487
1488 if (cmd_parse_params(&data[strlen(cmd->cmd)], cmd->schema,
1489 gdb_ctx.params, &gdb_ctx.num_params)) {
1490 return -1;
1491 }
1492
1493 gdb_ctx.s = s;
1494 cmd->handler(&gdb_ctx, user_ctx);
1495 return 0;
1496 }
1497
1498 return -1;
1499}
1500
Jon Doron3e2c1262019-05-29 09:41:30 +03001501static void run_cmd_parser(GDBState *s, const char *data,
1502 const GdbCmdParseEntry *cmd)
1503{
1504 if (!data) {
1505 return;
1506 }
1507
1508 /* In case there was an error during the command parsing we must
1509 * send a NULL packet to indicate the command is not supported */
1510 if (process_string_cmd(s, NULL, data, cmd, 1)) {
1511 put_packet(s, "");
1512 }
1513}
1514
1515static void handle_detach(GdbCmdContext *gdb_ctx, void *user_ctx)
1516{
1517 GDBProcess *process;
1518 GDBState *s = gdb_ctx->s;
1519 uint32_t pid = 1;
1520
1521 if (s->multiprocess) {
1522 if (!gdb_ctx->num_params) {
1523 put_packet(s, "E22");
1524 return;
1525 }
1526
1527 pid = gdb_ctx->params[0].val_ul;
1528 }
1529
1530 process = gdb_get_process(s, pid);
1531 gdb_process_breakpoint_remove_all(s, process);
1532 process->attached = false;
1533
1534 if (pid == gdb_get_cpu_pid(s, s->c_cpu)) {
1535 s->c_cpu = gdb_first_attached_cpu(s);
1536 }
1537
1538 if (pid == gdb_get_cpu_pid(s, s->g_cpu)) {
1539 s->g_cpu = gdb_first_attached_cpu(s);
1540 }
1541
1542 if (!s->c_cpu) {
1543 /* No more process attached */
1544 gdb_syscall_mode = GDB_SYS_DISABLED;
1545 gdb_continue(s);
1546 }
1547 put_packet(s, "OK");
1548}
1549
Jon Doron44ffded2019-05-29 09:41:31 +03001550static void handle_thread_alive(GdbCmdContext *gdb_ctx, void *user_ctx)
1551{
1552 CPUState *cpu;
1553
1554 if (!gdb_ctx->num_params) {
1555 put_packet(gdb_ctx->s, "E22");
1556 return;
1557 }
1558
1559 if (gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
1560 put_packet(gdb_ctx->s, "E22");
1561 return;
1562 }
1563
1564 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[0].thread_id.pid,
1565 gdb_ctx->params[0].thread_id.tid);
1566 if (!cpu) {
1567 put_packet(gdb_ctx->s, "E22");
1568 return;
1569 }
1570
1571 put_packet(gdb_ctx->s, "OK");
1572}
1573
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001574static void handle_continue(GdbCmdContext *gdb_ctx, void *user_ctx)
1575{
1576 if (gdb_ctx->num_params) {
1577 gdb_set_cpu_pc(gdb_ctx->s, gdb_ctx->params[0].val_ull);
1578 }
1579
1580 gdb_ctx->s->signal = 0;
1581 gdb_continue(gdb_ctx->s);
1582}
1583
Jon Doronccc47d52019-05-29 09:41:33 +03001584static void handle_cont_with_sig(GdbCmdContext *gdb_ctx, void *user_ctx)
1585{
1586 unsigned long signal = 0;
1587
1588 /*
1589 * Note: C sig;[addr] is currently unsupported and we simply
1590 * omit the addr parameter
1591 */
1592 if (gdb_ctx->num_params) {
1593 signal = gdb_ctx->params[0].val_ul;
1594 }
1595
1596 gdb_ctx->s->signal = gdb_signal_to_target(signal);
1597 if (gdb_ctx->s->signal == -1) {
1598 gdb_ctx->s->signal = 0;
1599 }
1600 gdb_continue(gdb_ctx->s);
1601}
1602
Jon Doron3a9651d2019-05-29 09:41:34 +03001603static void handle_set_thread(GdbCmdContext *gdb_ctx, void *user_ctx)
1604{
1605 CPUState *cpu;
1606
1607 if (gdb_ctx->num_params != 2) {
1608 put_packet(gdb_ctx->s, "E22");
1609 return;
1610 }
1611
1612 if (gdb_ctx->params[1].thread_id.kind == GDB_READ_THREAD_ERR) {
1613 put_packet(gdb_ctx->s, "E22");
1614 return;
1615 }
1616
1617 if (gdb_ctx->params[1].thread_id.kind != GDB_ONE_THREAD) {
1618 put_packet(gdb_ctx->s, "OK");
1619 return;
1620 }
1621
1622 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[1].thread_id.pid,
1623 gdb_ctx->params[1].thread_id.tid);
1624 if (!cpu) {
1625 put_packet(gdb_ctx->s, "E22");
1626 return;
1627 }
1628
1629 /*
1630 * Note: This command is deprecated and modern gdb's will be using the
1631 * vCont command instead.
1632 */
1633 switch (gdb_ctx->params[0].opcode) {
1634 case 'c':
1635 gdb_ctx->s->c_cpu = cpu;
1636 put_packet(gdb_ctx->s, "OK");
1637 break;
1638 case 'g':
1639 gdb_ctx->s->g_cpu = cpu;
1640 put_packet(gdb_ctx->s, "OK");
1641 break;
1642 default:
1643 put_packet(gdb_ctx->s, "E22");
1644 break;
1645 }
1646}
1647
Jon Doron77f6ce52019-05-29 09:41:35 +03001648static void handle_insert_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1649{
1650 int res;
1651
1652 if (gdb_ctx->num_params != 3) {
1653 put_packet(gdb_ctx->s, "E22");
1654 return;
1655 }
1656
1657 res = gdb_breakpoint_insert(gdb_ctx->params[0].val_ul,
1658 gdb_ctx->params[1].val_ull,
1659 gdb_ctx->params[2].val_ull);
1660 if (res >= 0) {
1661 put_packet(gdb_ctx->s, "OK");
1662 return;
1663 } else if (res == -ENOSYS) {
1664 put_packet(gdb_ctx->s, "");
1665 return;
1666 }
1667
1668 put_packet(gdb_ctx->s, "E22");
1669}
1670
1671static void handle_remove_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1672{
1673 int res;
1674
1675 if (gdb_ctx->num_params != 3) {
1676 put_packet(gdb_ctx->s, "E22");
1677 return;
1678 }
1679
1680 res = gdb_breakpoint_remove(gdb_ctx->params[0].val_ul,
1681 gdb_ctx->params[1].val_ull,
1682 gdb_ctx->params[2].val_ull);
1683 if (res >= 0) {
1684 put_packet(gdb_ctx->s, "OK");
1685 return;
1686 } else if (res == -ENOSYS) {
1687 put_packet(gdb_ctx->s, "");
1688 return;
1689 }
1690
1691 put_packet(gdb_ctx->s, "E22");
1692}
1693
Alex Bennée94b2a622019-07-05 14:23:07 +01001694/*
1695 * handle_set/get_reg
1696 *
1697 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1698 * This works, but can be very slow. Anything new enough to understand
1699 * XML also knows how to use this properly. However to use this we
1700 * need to define a local XML file as well as be talking to a
1701 * reasonably modern gdb. Responding with an empty packet will cause
1702 * the remote gdb to fallback to older methods.
1703 */
1704
Jon Doron62b33202019-05-29 09:41:36 +03001705static void handle_set_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1706{
1707 int reg_size;
1708
1709 if (!gdb_has_xml) {
Alex Bennée94b2a622019-07-05 14:23:07 +01001710 put_packet(gdb_ctx->s, "");
Jon Doron62b33202019-05-29 09:41:36 +03001711 return;
1712 }
1713
1714 if (gdb_ctx->num_params != 2) {
1715 put_packet(gdb_ctx->s, "E22");
1716 return;
1717 }
1718
1719 reg_size = strlen(gdb_ctx->params[1].data) / 2;
1720 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[1].data, reg_size);
1721 gdb_write_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf,
1722 gdb_ctx->params[0].val_ull);
1723 put_packet(gdb_ctx->s, "OK");
1724}
1725
Jon Doron5d0e57b2019-05-29 09:41:37 +03001726static void handle_get_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1727{
1728 int reg_size;
1729
Jon Doron5d0e57b2019-05-29 09:41:37 +03001730 if (!gdb_has_xml) {
1731 put_packet(gdb_ctx->s, "");
1732 return;
1733 }
1734
1735 if (!gdb_ctx->num_params) {
1736 put_packet(gdb_ctx->s, "E14");
1737 return;
1738 }
1739
1740 reg_size = gdb_read_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf,
1741 gdb_ctx->params[0].val_ull);
1742 if (!reg_size) {
1743 put_packet(gdb_ctx->s, "E14");
1744 return;
1745 }
1746
1747 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, reg_size);
1748 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1749}
1750
Jon Doroncc0ecc72019-05-29 09:41:38 +03001751static void handle_write_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1752{
1753 if (gdb_ctx->num_params != 3) {
1754 put_packet(gdb_ctx->s, "E22");
1755 return;
1756 }
1757
1758 /* hextomem() reads 2*len bytes */
1759 if (gdb_ctx->params[1].val_ull > strlen(gdb_ctx->params[2].data) / 2) {
1760 put_packet(gdb_ctx->s, "E22");
1761 return;
1762 }
1763
1764 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[2].data,
1765 gdb_ctx->params[1].val_ull);
1766 if (target_memory_rw_debug(gdb_ctx->s->g_cpu, gdb_ctx->params[0].val_ull,
1767 gdb_ctx->mem_buf,
1768 gdb_ctx->params[1].val_ull, true)) {
1769 put_packet(gdb_ctx->s, "E14");
1770 return;
1771 }
1772
1773 put_packet(gdb_ctx->s, "OK");
1774}
1775
Jon Doronda92e232019-05-29 09:41:39 +03001776static void handle_read_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1777{
1778 if (gdb_ctx->num_params != 2) {
1779 put_packet(gdb_ctx->s, "E22");
1780 return;
1781 }
1782
1783 /* memtohex() doubles the required space */
1784 if (gdb_ctx->params[1].val_ull > MAX_PACKET_LENGTH / 2) {
1785 put_packet(gdb_ctx->s, "E22");
1786 return;
1787 }
1788
1789 if (target_memory_rw_debug(gdb_ctx->s->g_cpu, gdb_ctx->params[0].val_ull,
1790 gdb_ctx->mem_buf,
1791 gdb_ctx->params[1].val_ull, false)) {
1792 put_packet(gdb_ctx->s, "E14");
1793 return;
1794 }
1795
1796 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, gdb_ctx->params[1].val_ull);
1797 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1798}
1799
Jon Doron287ca122019-05-29 09:41:40 +03001800static void handle_write_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1801{
1802 target_ulong addr, len;
1803 uint8_t *registers;
1804 int reg_size;
1805
1806 if (!gdb_ctx->num_params) {
1807 return;
1808 }
1809
1810 cpu_synchronize_state(gdb_ctx->s->g_cpu);
1811 registers = gdb_ctx->mem_buf;
1812 len = strlen(gdb_ctx->params[0].data) / 2;
1813 hextomem(registers, gdb_ctx->params[0].data, len);
1814 for (addr = 0; addr < gdb_ctx->s->g_cpu->gdb_num_g_regs && len > 0;
1815 addr++) {
1816 reg_size = gdb_write_register(gdb_ctx->s->g_cpu, registers, addr);
1817 len -= reg_size;
1818 registers += reg_size;
1819 }
1820 put_packet(gdb_ctx->s, "OK");
1821}
1822
Jon Doron397d1372019-05-29 09:41:41 +03001823static void handle_read_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1824{
1825 target_ulong addr, len;
1826
1827 cpu_synchronize_state(gdb_ctx->s->g_cpu);
1828 len = 0;
1829 for (addr = 0; addr < gdb_ctx->s->g_cpu->gdb_num_g_regs; addr++) {
1830 len += gdb_read_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf + len,
1831 addr);
1832 }
1833
1834 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, len);
1835 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1836}
1837
Jon Doron4b20fab2019-05-29 09:41:42 +03001838static void handle_file_io(GdbCmdContext *gdb_ctx, void *user_ctx)
1839{
Sandra Loosemorec6ee9522019-08-27 16:33:17 -06001840 if (gdb_ctx->num_params >= 1 && gdb_ctx->s->current_syscall_cb) {
Jon Doron4b20fab2019-05-29 09:41:42 +03001841 target_ulong ret, err;
1842
1843 ret = (target_ulong)gdb_ctx->params[0].val_ull;
Sandra Loosemorec6ee9522019-08-27 16:33:17 -06001844 if (gdb_ctx->num_params >= 2) {
1845 err = (target_ulong)gdb_ctx->params[1].val_ull;
1846 } else {
1847 err = 0;
1848 }
Jon Doron4b20fab2019-05-29 09:41:42 +03001849 gdb_ctx->s->current_syscall_cb(gdb_ctx->s->c_cpu, ret, err);
1850 gdb_ctx->s->current_syscall_cb = NULL;
1851 }
1852
1853 if (gdb_ctx->num_params >= 3 && gdb_ctx->params[2].opcode == (uint8_t)'C') {
1854 put_packet(gdb_ctx->s, "T02");
1855 return;
1856 }
1857
1858 gdb_continue(gdb_ctx->s);
1859}
1860
Jon Doron933f80d2019-05-29 09:41:43 +03001861static void handle_step(GdbCmdContext *gdb_ctx, void *user_ctx)
1862{
1863 if (gdb_ctx->num_params) {
1864 gdb_set_cpu_pc(gdb_ctx->s, (target_ulong)gdb_ctx->params[0].val_ull);
1865 }
1866
1867 cpu_single_step(gdb_ctx->s->c_cpu, sstep_flags);
1868 gdb_continue(gdb_ctx->s);
1869}
1870
Jon Doron8536ec02019-05-29 09:41:44 +03001871static void handle_v_cont_query(GdbCmdContext *gdb_ctx, void *user_ctx)
1872{
1873 put_packet(gdb_ctx->s, "vCont;c;C;s;S");
1874}
1875
1876static void handle_v_cont(GdbCmdContext *gdb_ctx, void *user_ctx)
1877{
1878 int res;
1879
1880 if (!gdb_ctx->num_params) {
1881 return;
1882 }
1883
1884 res = gdb_handle_vcont(gdb_ctx->s, gdb_ctx->params[0].data);
1885 if ((res == -EINVAL) || (res == -ERANGE)) {
1886 put_packet(gdb_ctx->s, "E22");
1887 } else if (res) {
1888 put_packet(gdb_ctx->s, "");
1889 }
1890}
1891
1892static void handle_v_attach(GdbCmdContext *gdb_ctx, void *user_ctx)
1893{
1894 GDBProcess *process;
1895 CPUState *cpu;
1896 char thread_id[16];
1897
1898 pstrcpy(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "E22");
1899 if (!gdb_ctx->num_params) {
1900 goto cleanup;
1901 }
1902
1903 process = gdb_get_process(gdb_ctx->s, gdb_ctx->params[0].val_ul);
1904 if (!process) {
1905 goto cleanup;
1906 }
1907
1908 cpu = get_first_cpu_in_process(gdb_ctx->s, process);
1909 if (!cpu) {
1910 goto cleanup;
1911 }
1912
1913 process->attached = true;
1914 gdb_ctx->s->g_cpu = cpu;
1915 gdb_ctx->s->c_cpu = cpu;
1916
1917 gdb_fmt_thread_id(gdb_ctx->s, cpu, thread_id, sizeof(thread_id));
1918 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "T%02xthread:%s;",
1919 GDB_SIGNAL_TRAP, thread_id);
1920cleanup:
1921 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1922}
1923
1924static void handle_v_kill(GdbCmdContext *gdb_ctx, void *user_ctx)
1925{
1926 /* Kill the target */
1927 put_packet(gdb_ctx->s, "OK");
1928 error_report("QEMU: Terminated via GDBstub");
1929 exit(0);
1930}
1931
1932static GdbCmdParseEntry gdb_v_commands_table[] = {
1933 /* Order is important if has same prefix */
1934 {
1935 .handler = handle_v_cont_query,
1936 .cmd = "Cont?",
1937 .cmd_startswith = 1
1938 },
1939 {
1940 .handler = handle_v_cont,
1941 .cmd = "Cont",
1942 .cmd_startswith = 1,
1943 .schema = "s0"
1944 },
1945 {
1946 .handler = handle_v_attach,
1947 .cmd = "Attach;",
1948 .cmd_startswith = 1,
1949 .schema = "l0"
1950 },
1951 {
1952 .handler = handle_v_kill,
1953 .cmd = "Kill;",
1954 .cmd_startswith = 1
1955 },
1956};
1957
1958static void handle_v_commands(GdbCmdContext *gdb_ctx, void *user_ctx)
1959{
1960 if (!gdb_ctx->num_params) {
1961 return;
1962 }
1963
1964 if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
1965 gdb_v_commands_table,
1966 ARRAY_SIZE(gdb_v_commands_table))) {
1967 put_packet(gdb_ctx->s, "");
1968 }
1969}
1970
Jon Doron2704efa2019-05-29 09:41:45 +03001971static void handle_query_qemu_sstepbits(GdbCmdContext *gdb_ctx, void *user_ctx)
1972{
1973 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
1974 "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", SSTEP_ENABLE,
1975 SSTEP_NOIRQ, SSTEP_NOTIMER);
1976 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1977}
1978
1979static void handle_set_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
1980{
1981 if (!gdb_ctx->num_params) {
1982 return;
1983 }
1984
1985 sstep_flags = gdb_ctx->params[0].val_ul;
1986 put_packet(gdb_ctx->s, "OK");
1987}
1988
1989static void handle_query_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
1990{
1991 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "0x%x", sstep_flags);
1992 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1993}
1994
1995static void handle_query_curr_tid(GdbCmdContext *gdb_ctx, void *user_ctx)
bellardb4608c02003-06-27 17:34:32 +00001996{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001997 CPUState *cpu;
Luc Michelc145eea2019-01-07 15:23:46 +00001998 GDBProcess *process;
Jon Doron2704efa2019-05-29 09:41:45 +03001999 char thread_id[16];
2000
2001 /*
2002 * "Current thread" remains vague in the spec, so always return
2003 * the first thread of the current process (gdb returns the
2004 * first thread).
2005 */
2006 process = gdb_get_cpu_process(gdb_ctx->s, gdb_ctx->s->g_cpu);
2007 cpu = get_first_cpu_in_process(gdb_ctx->s, process);
2008 gdb_fmt_thread_id(gdb_ctx->s, cpu, thread_id, sizeof(thread_id));
2009 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "QC%s", thread_id);
2010 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2011}
2012
2013static void handle_query_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
2014{
2015 char thread_id[16];
2016
2017 if (!gdb_ctx->s->query_cpu) {
2018 put_packet(gdb_ctx->s, "l");
2019 return;
2020 }
2021
2022 gdb_fmt_thread_id(gdb_ctx->s, gdb_ctx->s->query_cpu, thread_id,
2023 sizeof(thread_id));
2024 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "m%s", thread_id);
2025 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2026 gdb_ctx->s->query_cpu =
2027 gdb_next_attached_cpu(gdb_ctx->s, gdb_ctx->s->query_cpu);
2028}
2029
2030static void handle_query_first_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
2031{
2032 gdb_ctx->s->query_cpu = gdb_first_attached_cpu(gdb_ctx->s);
2033 handle_query_threads(gdb_ctx, user_ctx);
2034}
2035
2036static void handle_query_thread_extra(GdbCmdContext *gdb_ctx, void *user_ctx)
2037{
2038 CPUState *cpu;
2039 int len;
2040
2041 if (!gdb_ctx->num_params ||
2042 gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
2043 put_packet(gdb_ctx->s, "E22");
2044 return;
2045 }
2046
2047 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[0].thread_id.pid,
2048 gdb_ctx->params[0].thread_id.tid);
2049 if (!cpu) {
2050 return;
2051 }
2052
2053 cpu_synchronize_state(cpu);
2054
2055 if (gdb_ctx->s->multiprocess && (gdb_ctx->s->process_num > 1)) {
2056 /* Print the CPU model and name in multiprocess mode */
2057 ObjectClass *oc = object_get_class(OBJECT(cpu));
2058 const char *cpu_model = object_class_get_name(oc);
2059 char *cpu_name = object_get_canonical_path_component(OBJECT(cpu));
2060 len = snprintf((char *)gdb_ctx->mem_buf, sizeof(gdb_ctx->str_buf) / 2,
2061 "%s %s [%s]", cpu_model, cpu_name,
2062 cpu->halted ? "halted " : "running");
2063 g_free(cpu_name);
2064 } else {
2065 /* memtohex() doubles the required space */
2066 len = snprintf((char *)gdb_ctx->mem_buf, sizeof(gdb_ctx->str_buf) / 2,
2067 "CPU#%d [%s]", cpu->cpu_index,
2068 cpu->halted ? "halted " : "running");
2069 }
2070 trace_gdbstub_op_extra_info((char *)gdb_ctx->mem_buf);
2071 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, len);
2072 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2073}
2074
2075#ifdef CONFIG_USER_ONLY
2076static void handle_query_offsets(GdbCmdContext *gdb_ctx, void *user_ctx)
2077{
2078 TaskState *ts;
2079
2080 ts = gdb_ctx->s->c_cpu->opaque;
2081 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
2082 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
2083 ";Bss=" TARGET_ABI_FMT_lx,
2084 ts->info->code_offset,
2085 ts->info->data_offset,
2086 ts->info->data_offset);
2087 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2088}
2089#else
2090static void handle_query_rcmd(GdbCmdContext *gdb_ctx, void *user_ctx)
2091{
2092 int len;
2093
2094 if (!gdb_ctx->num_params) {
2095 put_packet(gdb_ctx->s, "E22");
2096 return;
2097 }
2098
2099 len = strlen(gdb_ctx->params[0].data);
2100 if (len % 2) {
2101 put_packet(gdb_ctx->s, "E01");
2102 return;
2103 }
2104
2105 len = len / 2;
2106 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[0].data, len);
2107 gdb_ctx->mem_buf[len++] = 0;
2108 qemu_chr_be_write(gdb_ctx->s->mon_chr, gdb_ctx->mem_buf, len);
2109 put_packet(gdb_ctx->s, "OK");
2110
2111}
2112#endif
2113
2114static void handle_query_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2115{
Andreas Färber5b24c642013-07-07 15:08:22 +02002116 CPUClass *cc;
Jon Doron2704efa2019-05-29 09:41:45 +03002117
2118 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "PacketSize=%x",
2119 MAX_PACKET_LENGTH);
2120 cc = CPU_GET_CLASS(first_cpu);
2121 if (cc->gdb_core_xml_file) {
2122 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
2123 ";qXfer:features:read+");
2124 }
2125
2126 if (gdb_ctx->num_params &&
2127 strstr(gdb_ctx->params[0].data, "multiprocess+")) {
2128 gdb_ctx->s->multiprocess = true;
2129 }
2130
2131 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), ";multiprocess+");
2132 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2133}
2134
2135static void handle_query_xfer_features(GdbCmdContext *gdb_ctx, void *user_ctx)
2136{
2137 GDBProcess *process;
2138 CPUClass *cc;
2139 unsigned long len, total_len, addr;
2140 const char *xml;
bellardb4608c02003-06-27 17:34:32 +00002141 const char *p;
Jon Doron2704efa2019-05-29 09:41:45 +03002142
2143 if (gdb_ctx->num_params < 3) {
2144 put_packet(gdb_ctx->s, "E22");
2145 return;
2146 }
2147
2148 process = gdb_get_cpu_process(gdb_ctx->s, gdb_ctx->s->g_cpu);
2149 cc = CPU_GET_CLASS(gdb_ctx->s->g_cpu);
2150 if (!cc->gdb_core_xml_file) {
2151 put_packet(gdb_ctx->s, "");
2152 return;
2153 }
2154
2155 gdb_has_xml = true;
2156 p = gdb_ctx->params[0].data;
2157 xml = get_feature_xml(gdb_ctx->s, p, &p, process);
2158 if (!xml) {
2159 put_packet(gdb_ctx->s, "E00");
2160 return;
2161 }
2162
2163 addr = gdb_ctx->params[1].val_ul;
2164 len = gdb_ctx->params[2].val_ul;
2165 total_len = strlen(xml);
2166 if (addr > total_len) {
2167 put_packet(gdb_ctx->s, "E00");
2168 return;
2169 }
2170
2171 if (len > (MAX_PACKET_LENGTH - 5) / 2) {
2172 len = (MAX_PACKET_LENGTH - 5) / 2;
2173 }
2174
2175 if (len < total_len - addr) {
2176 gdb_ctx->str_buf[0] = 'm';
2177 len = memtox(gdb_ctx->str_buf + 1, xml + addr, len);
2178 } else {
2179 gdb_ctx->str_buf[0] = 'l';
2180 len = memtox(gdb_ctx->str_buf + 1, xml + addr, total_len - addr);
2181 }
2182
2183 put_packet_binary(gdb_ctx->s, gdb_ctx->str_buf, len + 1, true);
2184}
2185
2186static void handle_query_attached(GdbCmdContext *gdb_ctx, void *user_ctx)
2187{
2188 put_packet(gdb_ctx->s, GDB_ATTACHED);
2189}
2190
2191static void handle_query_qemu_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2192{
Jon Doronab4752e2019-05-29 09:41:48 +03002193 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "sstepbits;sstep");
2194#ifndef CONFIG_USER_ONLY
2195 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), ";PhyMemMode");
2196#endif
2197 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
Jon Doron2704efa2019-05-29 09:41:45 +03002198}
2199
Jon Doronab4752e2019-05-29 09:41:48 +03002200#ifndef CONFIG_USER_ONLY
2201static void handle_query_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx,
2202 void *user_ctx)
2203{
2204 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "%d", phy_memory_mode);
2205 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2206}
2207
2208static void handle_set_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx, void *user_ctx)
2209{
2210 if (!gdb_ctx->num_params) {
2211 put_packet(gdb_ctx->s, "E22");
2212 return;
2213 }
2214
2215 if (!gdb_ctx->params[0].val_ul) {
2216 phy_memory_mode = 0;
2217 } else {
2218 phy_memory_mode = 1;
2219 }
2220 put_packet(gdb_ctx->s, "OK");
2221}
2222#endif
2223
Jon Doron2704efa2019-05-29 09:41:45 +03002224static GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
2225 /* Order is important if has same prefix */
2226 {
2227 .handler = handle_query_qemu_sstepbits,
2228 .cmd = "qemu.sstepbits",
2229 },
2230 {
2231 .handler = handle_query_qemu_sstep,
2232 .cmd = "qemu.sstep",
2233 },
2234 {
2235 .handler = handle_set_qemu_sstep,
2236 .cmd = "qemu.sstep=",
2237 .cmd_startswith = 1,
2238 .schema = "l0"
2239 },
2240};
2241
2242static GdbCmdParseEntry gdb_gen_query_table[] = {
2243 {
2244 .handler = handle_query_curr_tid,
2245 .cmd = "C",
2246 },
2247 {
2248 .handler = handle_query_threads,
2249 .cmd = "sThreadInfo",
2250 },
2251 {
2252 .handler = handle_query_first_threads,
2253 .cmd = "fThreadInfo",
2254 },
2255 {
2256 .handler = handle_query_thread_extra,
2257 .cmd = "ThreadExtraInfo,",
2258 .cmd_startswith = 1,
2259 .schema = "t0"
2260 },
2261#ifdef CONFIG_USER_ONLY
2262 {
2263 .handler = handle_query_offsets,
2264 .cmd = "Offsets",
2265 },
2266#else
2267 {
2268 .handler = handle_query_rcmd,
2269 .cmd = "Rcmd,",
2270 .cmd_startswith = 1,
2271 .schema = "s0"
2272 },
2273#endif
2274 {
2275 .handler = handle_query_supported,
2276 .cmd = "Supported:",
2277 .cmd_startswith = 1,
2278 .schema = "s0"
2279 },
2280 {
2281 .handler = handle_query_supported,
2282 .cmd = "Supported",
2283 .schema = "s0"
2284 },
2285 {
2286 .handler = handle_query_xfer_features,
2287 .cmd = "Xfer:features:read:",
2288 .cmd_startswith = 1,
2289 .schema = "s:l,l0"
2290 },
2291 {
2292 .handler = handle_query_attached,
2293 .cmd = "Attached:",
2294 .cmd_startswith = 1
2295 },
2296 {
2297 .handler = handle_query_attached,
2298 .cmd = "Attached",
2299 },
2300 {
2301 .handler = handle_query_qemu_supported,
2302 .cmd = "qemu.Supported",
2303 },
Jon Doronab4752e2019-05-29 09:41:48 +03002304#ifndef CONFIG_USER_ONLY
2305 {
2306 .handler = handle_query_qemu_phy_mem_mode,
2307 .cmd = "qemu.PhyMemMode",
2308 },
2309#endif
Jon Doron2704efa2019-05-29 09:41:45 +03002310};
2311
2312static GdbCmdParseEntry gdb_gen_set_table[] = {
2313 /* Order is important if has same prefix */
2314 {
2315 .handler = handle_set_qemu_sstep,
2316 .cmd = "qemu.sstep:",
2317 .cmd_startswith = 1,
2318 .schema = "l0"
2319 },
Jon Doronab4752e2019-05-29 09:41:48 +03002320#ifndef CONFIG_USER_ONLY
2321 {
2322 .handler = handle_set_qemu_phy_mem_mode,
2323 .cmd = "qemu.PhyMemMode:",
2324 .cmd_startswith = 1,
2325 .schema = "l0"
2326 },
2327#endif
Jon Doron2704efa2019-05-29 09:41:45 +03002328};
2329
2330static void handle_gen_query(GdbCmdContext *gdb_ctx, void *user_ctx)
2331{
2332 if (!gdb_ctx->num_params) {
2333 return;
2334 }
2335
2336 if (!process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2337 gdb_gen_query_set_common_table,
2338 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2339 return;
2340 }
2341
2342 if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2343 gdb_gen_query_table,
2344 ARRAY_SIZE(gdb_gen_query_table))) {
2345 put_packet(gdb_ctx->s, "");
2346 }
2347}
2348
2349static void handle_gen_set(GdbCmdContext *gdb_ctx, void *user_ctx)
2350{
2351 if (!gdb_ctx->num_params) {
2352 return;
2353 }
2354
2355 if (!process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2356 gdb_gen_query_set_common_table,
2357 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2358 return;
2359 }
2360
2361 if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2362 gdb_gen_set_table,
2363 ARRAY_SIZE(gdb_gen_set_table))) {
2364 put_packet(gdb_ctx->s, "");
2365 }
2366}
2367
Jon Doron7009d572019-05-29 09:41:46 +03002368static void handle_target_halt(GdbCmdContext *gdb_ctx, void *user_ctx)
2369{
2370 char thread_id[16];
2371
2372 gdb_fmt_thread_id(gdb_ctx->s, gdb_ctx->s->c_cpu, thread_id,
2373 sizeof(thread_id));
2374 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "T%02xthread:%s;",
2375 GDB_SIGNAL_TRAP, thread_id);
2376 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2377 /*
2378 * Remove all the breakpoints when this query is issued,
2379 * because gdb is doing an initial connect and the state
2380 * should be cleaned up.
2381 */
2382 gdb_breakpoint_remove_all();
2383}
2384
Jon Doron2704efa2019-05-29 09:41:45 +03002385static int gdb_handle_packet(GDBState *s, const char *line_buf)
2386{
Jon Doron3e2c1262019-05-29 09:41:30 +03002387 const GdbCmdParseEntry *cmd_parser = NULL;
ths3b46e622007-09-17 08:09:54 +00002388
Doug Gale5c9522b2017-12-02 20:30:37 -05002389 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01002390
Jon Doron3f1cbac2019-05-29 09:41:47 +03002391 switch (line_buf[0]) {
Luc Michel53fd6552019-01-07 15:23:46 +00002392 case '!':
2393 put_packet(s, "OK");
2394 break;
bellard858693c2004-03-31 18:52:07 +00002395 case '?':
Jon Doron7009d572019-05-29 09:41:46 +03002396 {
2397 static const GdbCmdParseEntry target_halted_cmd_desc = {
2398 .handler = handle_target_halt,
2399 .cmd = "?",
2400 .cmd_startswith = 1
2401 };
2402 cmd_parser = &target_halted_cmd_desc;
2403 }
bellard858693c2004-03-31 18:52:07 +00002404 break;
2405 case 'c':
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002406 {
2407 static const GdbCmdParseEntry continue_cmd_desc = {
2408 .handler = handle_continue,
2409 .cmd = "c",
2410 .cmd_startswith = 1,
2411 .schema = "L0"
2412 };
2413 cmd_parser = &continue_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002414 }
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002415 break;
edgar_igl1f487ee2008-05-17 22:20:53 +00002416 case 'C':
Jon Doronccc47d52019-05-29 09:41:33 +03002417 {
2418 static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
2419 .handler = handle_cont_with_sig,
2420 .cmd = "C",
2421 .cmd_startswith = 1,
2422 .schema = "l0"
2423 };
2424 cmd_parser = &cont_with_sig_cmd_desc;
2425 }
2426 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002427 case 'v':
Jon Doron8536ec02019-05-29 09:41:44 +03002428 {
2429 static const GdbCmdParseEntry v_cmd_desc = {
2430 .handler = handle_v_commands,
2431 .cmd = "v",
2432 .cmd_startswith = 1,
2433 .schema = "s0"
2434 };
2435 cmd_parser = &v_cmd_desc;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002436 }
Jon Doron8536ec02019-05-29 09:41:44 +03002437 break;
edgar_igl7d03f822008-05-17 18:58:29 +00002438 case 'k':
2439 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002440 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00002441 exit(0);
2442 case 'D':
Jon Doron3e2c1262019-05-29 09:41:30 +03002443 {
2444 static const GdbCmdParseEntry detach_cmd_desc = {
2445 .handler = handle_detach,
2446 .cmd = "D",
2447 .cmd_startswith = 1,
2448 .schema = "?.l0"
2449 };
2450 cmd_parser = &detach_cmd_desc;
Luc Michel546f3c62019-01-07 15:23:46 +00002451 }
edgar_igl7d03f822008-05-17 18:58:29 +00002452 break;
bellard858693c2004-03-31 18:52:07 +00002453 case 's':
Jon Doron933f80d2019-05-29 09:41:43 +03002454 {
2455 static const GdbCmdParseEntry step_cmd_desc = {
2456 .handler = handle_step,
2457 .cmd = "s",
2458 .cmd_startswith = 1,
2459 .schema = "L0"
2460 };
2461 cmd_parser = &step_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002462 }
Jon Doron933f80d2019-05-29 09:41:43 +03002463 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002464 case 'F':
2465 {
Jon Doron4b20fab2019-05-29 09:41:42 +03002466 static const GdbCmdParseEntry file_io_cmd_desc = {
2467 .handler = handle_file_io,
2468 .cmd = "F",
2469 .cmd_startswith = 1,
2470 .schema = "L,L,o0"
2471 };
2472 cmd_parser = &file_io_cmd_desc;
pbrooka2d1eba2007-01-28 03:10:55 +00002473 }
2474 break;
bellard858693c2004-03-31 18:52:07 +00002475 case 'g':
Jon Doron397d1372019-05-29 09:41:41 +03002476 {
2477 static const GdbCmdParseEntry read_all_regs_cmd_desc = {
2478 .handler = handle_read_all_regs,
2479 .cmd = "g",
2480 .cmd_startswith = 1
2481 };
2482 cmd_parser = &read_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002483 }
bellard858693c2004-03-31 18:52:07 +00002484 break;
2485 case 'G':
Jon Doron287ca122019-05-29 09:41:40 +03002486 {
2487 static const GdbCmdParseEntry write_all_regs_cmd_desc = {
2488 .handler = handle_write_all_regs,
2489 .cmd = "G",
2490 .cmd_startswith = 1,
2491 .schema = "s0"
2492 };
2493 cmd_parser = &write_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002494 }
bellard858693c2004-03-31 18:52:07 +00002495 break;
2496 case 'm':
Jon Doronda92e232019-05-29 09:41:39 +03002497 {
2498 static const GdbCmdParseEntry read_mem_cmd_desc = {
2499 .handler = handle_read_mem,
2500 .cmd = "m",
2501 .cmd_startswith = 1,
2502 .schema = "L,L0"
2503 };
2504 cmd_parser = &read_mem_cmd_desc;
bellard6f970bd2005-12-05 19:55:19 +00002505 }
bellard858693c2004-03-31 18:52:07 +00002506 break;
2507 case 'M':
Jon Doroncc0ecc72019-05-29 09:41:38 +03002508 {
2509 static const GdbCmdParseEntry write_mem_cmd_desc = {
2510 .handler = handle_write_mem,
2511 .cmd = "M",
2512 .cmd_startswith = 1,
2513 .schema = "L,L:s0"
2514 };
2515 cmd_parser = &write_mem_cmd_desc;
Fabien Chouteau44520db2011-09-08 12:48:16 +02002516 }
bellard858693c2004-03-31 18:52:07 +00002517 break;
pbrook56aebc82008-10-11 17:55:29 +00002518 case 'p':
Jon Doron5d0e57b2019-05-29 09:41:37 +03002519 {
2520 static const GdbCmdParseEntry get_reg_cmd_desc = {
2521 .handler = handle_get_reg,
2522 .cmd = "p",
2523 .cmd_startswith = 1,
2524 .schema = "L0"
2525 };
2526 cmd_parser = &get_reg_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002527 }
2528 break;
2529 case 'P':
Jon Doron62b33202019-05-29 09:41:36 +03002530 {
2531 static const GdbCmdParseEntry set_reg_cmd_desc = {
2532 .handler = handle_set_reg,
2533 .cmd = "P",
2534 .cmd_startswith = 1,
2535 .schema = "L?s0"
2536 };
2537 cmd_parser = &set_reg_cmd_desc;
2538 }
pbrook56aebc82008-10-11 17:55:29 +00002539 break;
bellard858693c2004-03-31 18:52:07 +00002540 case 'Z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002541 {
2542 static const GdbCmdParseEntry insert_bp_cmd_desc = {
2543 .handler = handle_insert_bp,
2544 .cmd = "Z",
2545 .cmd_startswith = 1,
2546 .schema = "l?L?L0"
2547 };
2548 cmd_parser = &insert_bp_cmd_desc;
2549 }
2550 break;
bellard858693c2004-03-31 18:52:07 +00002551 case 'z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002552 {
2553 static const GdbCmdParseEntry remove_bp_cmd_desc = {
2554 .handler = handle_remove_bp,
2555 .cmd = "z",
2556 .cmd_startswith = 1,
2557 .schema = "l?L?L0"
2558 };
2559 cmd_parser = &remove_bp_cmd_desc;
2560 }
bellard858693c2004-03-31 18:52:07 +00002561 break;
aliguori880a7572008-11-18 20:30:24 +00002562 case 'H':
Jon Doron3a9651d2019-05-29 09:41:34 +03002563 {
2564 static const GdbCmdParseEntry set_thread_cmd_desc = {
2565 .handler = handle_set_thread,
2566 .cmd = "H",
2567 .cmd_startswith = 1,
2568 .schema = "o.t0"
2569 };
2570 cmd_parser = &set_thread_cmd_desc;
aliguori880a7572008-11-18 20:30:24 +00002571 }
2572 break;
2573 case 'T':
Jon Doron44ffded2019-05-29 09:41:31 +03002574 {
2575 static const GdbCmdParseEntry thread_alive_cmd_desc = {
2576 .handler = handle_thread_alive,
2577 .cmd = "T",
2578 .cmd_startswith = 1,
2579 .schema = "t0"
2580 };
2581 cmd_parser = &thread_alive_cmd_desc;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07002582 }
aliguori880a7572008-11-18 20:30:24 +00002583 break;
pbrook978efd62006-06-17 18:30:42 +00002584 case 'q':
Jon Doron2704efa2019-05-29 09:41:45 +03002585 {
2586 static const GdbCmdParseEntry gen_query_cmd_desc = {
2587 .handler = handle_gen_query,
2588 .cmd = "q",
2589 .cmd_startswith = 1,
2590 .schema = "s0"
2591 };
2592 cmd_parser = &gen_query_cmd_desc;
2593 }
2594 break;
edgar_igl60897d32008-05-09 08:25:14 +00002595 case 'Q':
Jon Doron2704efa2019-05-29 09:41:45 +03002596 {
2597 static const GdbCmdParseEntry gen_set_cmd_desc = {
2598 .handler = handle_gen_set,
2599 .cmd = "Q",
2600 .cmd_startswith = 1,
2601 .schema = "s0"
2602 };
2603 cmd_parser = &gen_set_cmd_desc;
edgar_igl60897d32008-05-09 08:25:14 +00002604 }
Jon Doron2704efa2019-05-29 09:41:45 +03002605 break;
bellard858693c2004-03-31 18:52:07 +00002606 default:
bellard858693c2004-03-31 18:52:07 +00002607 /* put empty packet */
Jon Doron3f1cbac2019-05-29 09:41:47 +03002608 put_packet(s, "");
bellard858693c2004-03-31 18:52:07 +00002609 break;
2610 }
Jon Doron3e2c1262019-05-29 09:41:30 +03002611
Ramiro Polla2bdec392019-08-05 21:09:01 +02002612 if (cmd_parser) {
2613 run_cmd_parser(s, line_buf, cmd_parser);
2614 }
Jon Doron3e2c1262019-05-29 09:41:30 +03002615
bellard858693c2004-03-31 18:52:07 +00002616 return RS_IDLE;
2617}
2618
Andreas Färber64f6b342013-05-27 02:06:09 +02002619void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00002620{
Alex Bennée8d98c442020-03-16 17:21:33 +00002621 GDBProcess *p = gdb_get_cpu_process(&gdbserver_state, cpu);
Luc Michel160d8582019-01-07 15:23:46 +00002622
2623 if (!p->attached) {
2624 /*
2625 * Having a stop CPU corresponding to a process that is not attached
2626 * confuses GDB. So we ignore the request.
2627 */
2628 return;
2629 }
2630
Alex Bennée8d98c442020-03-16 17:21:33 +00002631 gdbserver_state.c_cpu = cpu;
2632 gdbserver_state.g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00002633}
2634
bellard1fddef42005-04-17 19:16:13 +00002635#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002636static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00002637{
Alex Bennée8d98c442020-03-16 17:21:33 +00002638 GDBState *s = &gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002639 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00002640 char buf[256];
Luc Michel95567c22019-01-07 15:23:46 +00002641 char thread_id[16];
aliguorid6fc1b32008-11-18 19:55:44 +00002642 const char *type;
bellard858693c2004-03-31 18:52:07 +00002643 int ret;
2644
Meador Ingecdb432b2012-03-15 17:49:45 +00002645 if (running || s->state == RS_INACTIVE) {
2646 return;
2647 }
2648 /* Is there a GDB syscall waiting to be sent? */
2649 if (s->current_syscall_cb) {
2650 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00002651 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01002652 }
Luc Michel95567c22019-01-07 15:23:46 +00002653
2654 if (cpu == NULL) {
2655 /* No process attached */
2656 return;
2657 }
2658
2659 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id));
2660
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002661 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002662 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02002663 if (cpu->watchpoint_hit) {
2664 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00002665 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00002666 type = "r";
2667 break;
aliguoria1d1bb32008-11-18 20:07:32 +00002668 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00002669 type = "a";
2670 break;
2671 default:
2672 type = "";
2673 break;
2674 }
Doug Gale5c9522b2017-12-02 20:30:37 -05002675 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
2676 (target_ulong)cpu->watchpoint_hit->vaddr);
aliguori880a7572008-11-18 20:30:24 +00002677 snprintf(buf, sizeof(buf),
Luc Michel95567c22019-01-07 15:23:46 +00002678 "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
2679 GDB_SIGNAL_TRAP, thread_id, type,
Andreas Färberff4700b2013-08-26 18:23:18 +02002680 (target_ulong)cpu->watchpoint_hit->vaddr);
2681 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01002682 goto send_packet;
Doug Gale5c9522b2017-12-02 20:30:37 -05002683 } else {
2684 trace_gdbstub_hit_break();
pbrook6658ffb2007-03-16 23:58:11 +00002685 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07002686 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00002687 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01002688 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002689 case RUN_STATE_PAUSED:
Doug Gale5c9522b2017-12-02 20:30:37 -05002690 trace_gdbstub_hit_paused();
aliguori9781e042009-01-22 17:15:29 +00002691 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01002692 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002693 case RUN_STATE_SHUTDOWN:
Doug Gale5c9522b2017-12-02 20:30:37 -05002694 trace_gdbstub_hit_shutdown();
Jan Kiszka425189a2011-03-22 11:02:09 +01002695 ret = GDB_SIGNAL_QUIT;
2696 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002697 case RUN_STATE_IO_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002698 trace_gdbstub_hit_io_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002699 ret = GDB_SIGNAL_IO;
2700 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002701 case RUN_STATE_WATCHDOG:
Doug Gale5c9522b2017-12-02 20:30:37 -05002702 trace_gdbstub_hit_watchdog();
Jan Kiszka425189a2011-03-22 11:02:09 +01002703 ret = GDB_SIGNAL_ALRM;
2704 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002705 case RUN_STATE_INTERNAL_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002706 trace_gdbstub_hit_internal_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002707 ret = GDB_SIGNAL_ABRT;
2708 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002709 case RUN_STATE_SAVE_VM:
2710 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01002711 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002712 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01002713 ret = GDB_SIGNAL_XCPU;
2714 break;
2715 default:
Doug Gale5c9522b2017-12-02 20:30:37 -05002716 trace_gdbstub_hit_unknown(state);
Jan Kiszka425189a2011-03-22 11:02:09 +01002717 ret = GDB_SIGNAL_UNKNOWN;
2718 break;
bellardbbeb7b52006-04-23 18:42:15 +00002719 }
Jan Kiszka226d0072015-07-24 18:52:31 +02002720 gdb_set_stop_cpu(cpu);
Luc Michel95567c22019-01-07 15:23:46 +00002721 snprintf(buf, sizeof(buf), "T%02xthread:%s;", ret, thread_id);
Jan Kiszka425189a2011-03-22 11:02:09 +01002722
2723send_packet:
bellard858693c2004-03-31 18:52:07 +00002724 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01002725
2726 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02002727 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00002728}
bellard1fddef42005-04-17 19:16:13 +00002729#endif
bellard858693c2004-03-31 18:52:07 +00002730
pbrooka2d1eba2007-01-28 03:10:55 +00002731/* Send a gdb syscall request.
2732 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00002733 %x - target_ulong argument printed in hex.
2734 %lx - 64-bit argument printed in hex.
2735 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01002736void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00002737{
pbrooka2d1eba2007-01-28 03:10:55 +00002738 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00002739 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00002740 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00002741 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00002742
Alex Bennée8d98c442020-03-16 17:21:33 +00002743 if (!gdbserver_state.init)
pbrooka2d1eba2007-01-28 03:10:55 +00002744 return;
Alex Bennée8d98c442020-03-16 17:21:33 +00002745
2746 gdbserver_state.current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00002747#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002748 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00002749#endif
Alex Bennée8d98c442020-03-16 17:21:33 +00002750 p = &gdbserver_state.syscall_buf[0];
2751 p_end = &gdbserver_state.syscall_buf[sizeof(gdbserver_state.syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00002752 *(p++) = 'F';
2753 while (*fmt) {
2754 if (*fmt == '%') {
2755 fmt++;
2756 switch (*fmt++) {
2757 case 'x':
2758 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002759 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00002760 break;
pbrooka87295e2007-05-26 15:09:38 +00002761 case 'l':
2762 if (*(fmt++) != 'x')
2763 goto bad_format;
2764 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00002765 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00002766 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002767 case 's':
2768 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002769 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00002770 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00002771 break;
2772 default:
pbrooka87295e2007-05-26 15:09:38 +00002773 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002774 error_report("gdbstub: Bad syscall format string '%s'",
2775 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00002776 break;
2777 }
2778 } else {
2779 *(p++) = *(fmt++);
2780 }
2781 }
pbrook8a93e022007-08-06 13:19:15 +00002782 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00002783#ifdef CONFIG_USER_ONLY
Alex Bennée8d98c442020-03-16 17:21:33 +00002784 put_packet(&gdbserver_state, gdbserver_state.syscall_buf);
Peter Maydell4f710862018-05-15 19:19:58 +01002785 /* Return control to gdb for it to process the syscall request.
2786 * Since the protocol requires that gdb hands control back to us
2787 * using a "here are the results" F packet, we don't need to check
2788 * gdb_handlesig's return value (which is the signal to deliver if
2789 * execution was resumed via a continue packet).
2790 */
Alex Bennée8d98c442020-03-16 17:21:33 +00002791 gdb_handlesig(gdbserver_state.c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00002792#else
Meador Ingecdb432b2012-03-15 17:49:45 +00002793 /* In this case wait to send the syscall packet until notification that
2794 the CPU has stopped. This must be done because if the packet is sent
2795 now the reply from the syscall request could be received while the CPU
2796 is still in the running state, which can cause packets to be dropped
2797 and state transition 'T' packets to be sent while the syscall is still
2798 being processed. */
Alex Bennée8d98c442020-03-16 17:21:33 +00002799 qemu_cpu_kick(gdbserver_state.c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00002800#endif
2801}
2802
Peter Maydell19239b32015-09-07 10:39:27 +01002803void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
2804{
2805 va_list va;
2806
2807 va_start(va, fmt);
2808 gdb_do_syscallv(cb, fmt, va);
2809 va_end(va);
2810}
2811
Markus Armbruster33c846e2019-05-14 20:03:09 +02002812static void gdb_read_byte(GDBState *s, uint8_t ch)
bellard858693c2004-03-31 18:52:07 +00002813{
ths60fe76f2007-12-16 03:02:09 +00002814 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00002815
bellard1fddef42005-04-17 19:16:13 +00002816#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00002817 if (s->last_packet_len) {
2818 /* Waiting for a response to the last packet. If we see the start
2819 of a new command then abandon the previous response. */
2820 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002821 trace_gdbstub_err_got_nack();
thsffe8ab82007-12-16 03:16:05 +00002822 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
Alex Bennée118e2262017-07-12 11:52:13 +01002823 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002824 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01002825 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002826 trace_gdbstub_io_got_unexpected(ch);
pbrook4046d912007-01-28 01:53:16 +00002827 }
Alex Bennée118e2262017-07-12 11:52:13 +01002828
pbrook4046d912007-01-28 01:53:16 +00002829 if (ch == '+' || ch == '$')
2830 s->last_packet_len = 0;
2831 if (ch != '$')
2832 return;
2833 }
Luiz Capitulino13548692011-07-29 15:36:43 -03002834 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00002835 /* when the CPU is running, we cannot do anything except stop
2836 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002837 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00002838 } else
bellard1fddef42005-04-17 19:16:13 +00002839#endif
bellard41625032005-04-24 10:07:11 +00002840 {
bellard858693c2004-03-31 18:52:07 +00002841 switch(s->state) {
2842 case RS_IDLE:
2843 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04002844 /* start of command packet */
bellard858693c2004-03-31 18:52:07 +00002845 s->line_buf_index = 0;
Doug Gale4bf43122017-05-01 12:22:10 -04002846 s->line_sum = 0;
bellard858693c2004-03-31 18:52:07 +00002847 s->state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002848 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002849 trace_gdbstub_err_garbage(ch);
bellard4c3a88a2003-07-26 12:06:08 +00002850 }
2851 break;
bellard858693c2004-03-31 18:52:07 +00002852 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04002853 if (ch == '}') {
2854 /* start escape sequence */
2855 s->state = RS_GETLINE_ESC;
2856 s->line_sum += ch;
2857 } else if (ch == '*') {
2858 /* start run length encoding sequence */
2859 s->state = RS_GETLINE_RLE;
2860 s->line_sum += ch;
2861 } else if (ch == '#') {
2862 /* end of command, start of checksum*/
2863 s->state = RS_CHKSUM1;
bellard858693c2004-03-31 18:52:07 +00002864 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002865 trace_gdbstub_err_overrun();
bellard858693c2004-03-31 18:52:07 +00002866 s->state = RS_IDLE;
2867 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002868 /* unescaped command character */
2869 s->line_buf[s->line_buf_index++] = ch;
2870 s->line_sum += ch;
2871 }
2872 break;
2873 case RS_GETLINE_ESC:
2874 if (ch == '#') {
2875 /* unexpected end of command in escape sequence */
2876 s->state = RS_CHKSUM1;
2877 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2878 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05002879 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002880 s->state = RS_IDLE;
2881 } else {
2882 /* parse escaped character and leave escape state */
2883 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
2884 s->line_sum += ch;
2885 s->state = RS_GETLINE;
2886 }
2887 break;
2888 case RS_GETLINE_RLE:
Markus Armbruster046aba12019-05-14 20:03:08 +02002889 /*
2890 * Run-length encoding is explained in "Debugging with GDB /
2891 * Appendix E GDB Remote Serial Protocol / Overview".
2892 */
2893 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
Doug Gale4bf43122017-05-01 12:22:10 -04002894 /* invalid RLE count encoding */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002895 trace_gdbstub_err_invalid_repeat(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002896 s->state = RS_GETLINE;
2897 } else {
2898 /* decode repeat length */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002899 int repeat = ch - ' ' + 3;
Doug Gale4bf43122017-05-01 12:22:10 -04002900 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
2901 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05002902 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002903 s->state = RS_IDLE;
2904 } else if (s->line_buf_index < 1) {
2905 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05002906 trace_gdbstub_err_invalid_rle();
Doug Gale4bf43122017-05-01 12:22:10 -04002907 s->state = RS_GETLINE;
2908 } else {
2909 /* repeat the last character */
2910 memset(s->line_buf + s->line_buf_index,
2911 s->line_buf[s->line_buf_index - 1], repeat);
2912 s->line_buf_index += repeat;
2913 s->line_sum += ch;
2914 s->state = RS_GETLINE;
2915 }
bellard858693c2004-03-31 18:52:07 +00002916 }
2917 break;
2918 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04002919 /* get high hex digit of checksum */
2920 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002921 trace_gdbstub_err_checksum_invalid(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002922 s->state = RS_GETLINE;
2923 break;
2924 }
bellard858693c2004-03-31 18:52:07 +00002925 s->line_buf[s->line_buf_index] = '\0';
2926 s->line_csum = fromhex(ch) << 4;
2927 s->state = RS_CHKSUM2;
2928 break;
2929 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04002930 /* get low hex digit of checksum */
2931 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002932 trace_gdbstub_err_checksum_invalid(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002933 s->state = RS_GETLINE;
2934 break;
bellard858693c2004-03-31 18:52:07 +00002935 }
Doug Gale4bf43122017-05-01 12:22:10 -04002936 s->line_csum |= fromhex(ch);
2937
2938 if (s->line_csum != (s->line_sum & 0xff)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002939 trace_gdbstub_err_checksum_incorrect(s->line_sum, s->line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04002940 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00002941 reply = '-';
2942 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00002943 s->state = RS_IDLE;
2944 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002945 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00002946 reply = '+';
2947 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00002948 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00002949 }
bellardb4608c02003-06-27 17:34:32 +00002950 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002951 default:
2952 abort();
bellardb4608c02003-06-27 17:34:32 +00002953 }
2954 }
bellard858693c2004-03-31 18:52:07 +00002955}
2956
Paul Brook0e1c9c52010-06-16 13:03:51 +01002957/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002958void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01002959{
Paul Brook0e1c9c52010-06-16 13:03:51 +01002960 char buf[4];
2961
Alex Bennée8d98c442020-03-16 17:21:33 +00002962 if (!gdbserver_state.init) {
Paul Brook0e1c9c52010-06-16 13:03:51 +01002963 return;
2964 }
2965#ifdef CONFIG_USER_ONLY
Alex Bennée8d98c442020-03-16 17:21:33 +00002966 if (gdbserver_fd < 0 || gdbserver_state.fd < 0) {
Paul Brook0e1c9c52010-06-16 13:03:51 +01002967 return;
2968 }
2969#endif
2970
Doug Gale5c9522b2017-12-02 20:30:37 -05002971 trace_gdbstub_op_exiting((uint8_t)code);
2972
Paul Brook0e1c9c52010-06-16 13:03:51 +01002973 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
Alex Bennée8d98c442020-03-16 17:21:33 +00002974 put_packet(&gdbserver_state, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002975
2976#ifndef CONFIG_USER_ONLY
Alex Bennée8d98c442020-03-16 17:21:33 +00002977 qemu_chr_fe_deinit(&gdbserver_state.chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002978#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01002979}
2980
Luc Michel8f468632019-01-07 15:23:45 +00002981/*
2982 * Create the process that will contain all the "orphan" CPUs (that are not
2983 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2984 * be attachable and thus will be invisible to the user.
2985 */
2986static void create_default_process(GDBState *s)
2987{
2988 GDBProcess *process;
2989 int max_pid = 0;
2990
2991 if (s->process_num) {
2992 max_pid = s->processes[s->process_num - 1].pid;
2993 }
2994
2995 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2996 process = &s->processes[s->process_num - 1];
2997
2998 /* We need an available PID slot for this process */
2999 assert(max_pid < UINT32_MAX);
3000
3001 process->pid = max_pid + 1;
3002 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00003003 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00003004}
3005
bellard1fddef42005-04-17 19:16:13 +00003006#ifdef CONFIG_USER_ONLY
3007int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02003008gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00003009{
Andreas Färber5ca666c2013-06-24 19:20:57 +02003010 char buf[256];
3011 int n;
bellard1fddef42005-04-17 19:16:13 +00003012
Alex Bennée8d98c442020-03-16 17:21:33 +00003013 if (gdbserver_fd < 0 || gdbserver_state.fd < 0) {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003014 return sig;
bellard1fddef42005-04-17 19:16:13 +00003015 }
3016
Andreas Färber5ca666c2013-06-24 19:20:57 +02003017 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02003018 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07003019 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00003020
Andreas Färber5ca666c2013-06-24 19:20:57 +02003021 if (sig != 0) {
3022 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
Alex Bennée8d98c442020-03-16 17:21:33 +00003023 put_packet(&gdbserver_state, buf);
Andreas Färber5ca666c2013-06-24 19:20:57 +02003024 }
3025 /* put_packet() might have detected that the peer terminated the
3026 connection. */
Alex Bennée8d98c442020-03-16 17:21:33 +00003027 if (gdbserver_state.fd < 0) {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003028 return sig;
3029 }
3030
3031 sig = 0;
Alex Bennée8d98c442020-03-16 17:21:33 +00003032 gdbserver_state.state = RS_IDLE;
3033 gdbserver_state.running_state = 0;
3034 while (gdbserver_state.running_state == 0) {
3035 n = read(gdbserver_state.fd, buf, 256);
Andreas Färber5ca666c2013-06-24 19:20:57 +02003036 if (n > 0) {
3037 int i;
3038
3039 for (i = 0; i < n; i++) {
Alex Bennée8d98c442020-03-16 17:21:33 +00003040 gdb_read_byte(&gdbserver_state, buf[i]);
Andreas Färber5ca666c2013-06-24 19:20:57 +02003041 }
Peter Wu5819e3e2016-06-05 16:35:48 +02003042 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003043 /* XXX: Connection closed. Should probably wait for another
3044 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02003045 if (n == 0) {
Alex Bennée8d98c442020-03-16 17:21:33 +00003046 close(gdbserver_state.fd);
Peter Wu5819e3e2016-06-05 16:35:48 +02003047 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003048 gdbserver_state.fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02003049 return sig;
bellard1fddef42005-04-17 19:16:13 +00003050 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02003051 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003052 sig = gdbserver_state.signal;
3053 gdbserver_state.signal = 0;
Andreas Färber5ca666c2013-06-24 19:20:57 +02003054 return sig;
bellard1fddef42005-04-17 19:16:13 +00003055}
bellarde9009672005-04-26 20:42:36 +00003056
aurel32ca587a82008-12-18 22:44:13 +00003057/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01003058void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00003059{
Andreas Färber5ca666c2013-06-24 19:20:57 +02003060 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00003061
Alex Bennée8d98c442020-03-16 17:21:33 +00003062 if (gdbserver_fd < 0 || gdbserver_state.fd < 0) {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003063 return;
3064 }
aurel32ca587a82008-12-18 22:44:13 +00003065
Andreas Färber5ca666c2013-06-24 19:20:57 +02003066 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
Alex Bennée8d98c442020-03-16 17:21:33 +00003067 put_packet(&gdbserver_state, buf);
aurel32ca587a82008-12-18 22:44:13 +00003068}
bellard1fddef42005-04-17 19:16:13 +00003069
Peter Maydell2f652222018-05-14 18:30:44 +01003070static bool gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00003071{
bellard858693c2004-03-31 18:52:07 +00003072 struct sockaddr_in sockaddr;
3073 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09003074 int fd;
bellard858693c2004-03-31 18:52:07 +00003075
3076 for(;;) {
3077 len = sizeof(sockaddr);
3078 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
3079 if (fd < 0 && errno != EINTR) {
3080 perror("accept");
Peter Maydell2f652222018-05-14 18:30:44 +01003081 return false;
bellard858693c2004-03-31 18:52:07 +00003082 } else if (fd >= 0) {
Peter Maydellf5bdd782018-05-14 18:30:43 +01003083 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00003084 break;
3085 }
3086 }
3087
3088 /* set short latency */
Peter Maydell2f652222018-05-14 18:30:44 +01003089 if (socket_set_nodelay(fd)) {
3090 perror("setsockopt");
Philippe Mathieu-Daudéead75d82018-05-24 19:34:58 -03003091 close(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01003092 return false;
3093 }
ths3b46e622007-09-17 08:09:54 +00003094
Alex Bennée8d98c442020-03-16 17:21:33 +00003095 init_gdbserver_state();
3096 create_default_process(&gdbserver_state);
3097 gdbserver_state.processes[0].attached = true;
3098 gdbserver_state.c_cpu = gdb_first_attached_cpu(&gdbserver_state);
3099 gdbserver_state.g_cpu = gdbserver_state.c_cpu;
3100 gdbserver_state.fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02003101 gdb_has_xml = false;
Peter Maydell2f652222018-05-14 18:30:44 +01003102 return true;
bellard858693c2004-03-31 18:52:07 +00003103}
3104
3105static int gdbserver_open(int port)
3106{
3107 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02003108 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00003109
3110 fd = socket(PF_INET, SOCK_STREAM, 0);
3111 if (fd < 0) {
3112 perror("socket");
3113 return -1;
3114 }
Peter Maydellf5bdd782018-05-14 18:30:43 +01003115 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00003116
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02003117 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00003118
3119 sockaddr.sin_family = AF_INET;
3120 sockaddr.sin_port = htons(port);
3121 sockaddr.sin_addr.s_addr = 0;
3122 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
3123 if (ret < 0) {
3124 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00003125 close(fd);
bellard858693c2004-03-31 18:52:07 +00003126 return -1;
3127 }
Peter Wu96165b92016-05-04 11:32:17 +02003128 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00003129 if (ret < 0) {
3130 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00003131 close(fd);
bellard858693c2004-03-31 18:52:07 +00003132 return -1;
3133 }
bellard858693c2004-03-31 18:52:07 +00003134 return fd;
3135}
3136
3137int gdbserver_start(int port)
3138{
3139 gdbserver_fd = gdbserver_open(port);
3140 if (gdbserver_fd < 0)
3141 return -1;
3142 /* accept connections */
Peter Maydell2f652222018-05-14 18:30:44 +01003143 if (!gdb_accept()) {
3144 close(gdbserver_fd);
3145 gdbserver_fd = -1;
3146 return -1;
3147 }
bellardb4608c02003-06-27 17:34:32 +00003148 return 0;
3149}
aurel322b1319c2008-12-18 22:44:04 +00003150
3151/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07003152void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00003153{
Alex Bennée8d98c442020-03-16 17:21:33 +00003154 if (gdbserver_fd < 0 || gdbserver_state.fd < 0) {
Andreas Färber75a34032013-09-02 16:57:02 +02003155 return;
3156 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003157 close(gdbserver_state.fd);
3158 gdbserver_state.fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02003159 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02003160 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00003161}
pbrook4046d912007-01-28 01:53:16 +00003162#else
thsaa1f17c2007-07-11 22:48:58 +00003163static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00003164{
pbrook56aebc82008-10-11 17:55:29 +00003165 /* We can handle an arbitrarily large amount of data.
3166 Pick the maximum packet size, which is as good as anything. */
3167 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00003168}
3169
thsaa1f17c2007-07-11 22:48:58 +00003170static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00003171{
pbrook4046d912007-01-28 01:53:16 +00003172 int i;
3173
3174 for (i = 0; i < size; i++) {
Alex Bennée8d98c442020-03-16 17:21:33 +00003175 gdb_read_byte(&gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00003176 }
3177}
3178
Philippe Mathieu-Daudé083b2662019-12-18 18:20:09 +01003179static void gdb_chr_event(void *opaque, QEMUChrEvent event)
pbrook4046d912007-01-28 01:53:16 +00003180{
Luc Michel970ed902019-01-07 15:23:46 +00003181 int i;
3182 GDBState *s = (GDBState *) opaque;
3183
pbrook4046d912007-01-28 01:53:16 +00003184 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05303185 case CHR_EVENT_OPENED:
Luc Michel970ed902019-01-07 15:23:46 +00003186 /* Start with first process attached, others detached */
3187 for (i = 0; i < s->process_num; i++) {
3188 s->processes[i].attached = !i;
3189 }
3190
3191 s->c_cpu = gdb_first_attached_cpu(s);
3192 s->g_cpu = s->c_cpu;
3193
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003194 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02003195 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00003196 break;
3197 default:
3198 break;
3199 }
3200}
3201
aliguori8a34a0f2009-03-05 23:01:55 +00003202static void gdb_monitor_output(GDBState *s, const char *msg, int len)
3203{
3204 char buf[MAX_PACKET_LENGTH];
3205
3206 buf[0] = 'O';
3207 if (len > (MAX_PACKET_LENGTH/2) - 1)
3208 len = (MAX_PACKET_LENGTH/2) - 1;
3209 memtohex(buf + 1, (uint8_t *)msg, len);
3210 put_packet(s, buf);
3211}
3212
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003213static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00003214{
3215 const char *p = (const char *)buf;
3216 int max_sz;
3217
Alex Bennée8d98c442020-03-16 17:21:33 +00003218 max_sz = (sizeof(gdbserver_state.last_packet) - 2) / 2;
aliguori8a34a0f2009-03-05 23:01:55 +00003219 for (;;) {
3220 if (len <= max_sz) {
Alex Bennée8d98c442020-03-16 17:21:33 +00003221 gdb_monitor_output(&gdbserver_state, p, len);
aliguori8a34a0f2009-03-05 23:01:55 +00003222 break;
3223 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003224 gdb_monitor_output(&gdbserver_state, p, max_sz);
aliguori8a34a0f2009-03-05 23:01:55 +00003225 p += max_sz;
3226 len -= max_sz;
3227 }
3228 return len;
3229}
3230
aliguori59030a82009-04-05 18:43:41 +00003231#ifndef _WIN32
3232static void gdb_sigterm_handler(int signal)
3233{
Luiz Capitulino13548692011-07-29 15:36:43 -03003234 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003235 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01003236 }
aliguori59030a82009-04-05 18:43:41 +00003237}
3238#endif
3239
Marc-André Lureau777357d2016-12-07 18:39:10 +03003240static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
3241 bool *be_opened, Error **errp)
3242{
3243 *be_opened = false;
3244}
3245
3246static void char_gdb_class_init(ObjectClass *oc, void *data)
3247{
3248 ChardevClass *cc = CHARDEV_CLASS(oc);
3249
3250 cc->internal = true;
3251 cc->open = gdb_monitor_open;
3252 cc->chr_write = gdb_monitor_write;
3253}
3254
3255#define TYPE_CHARDEV_GDB "chardev-gdb"
3256
3257static const TypeInfo char_gdb_type_info = {
3258 .name = TYPE_CHARDEV_GDB,
3259 .parent = TYPE_CHARDEV,
3260 .class_init = char_gdb_class_init,
3261};
3262
Luc Michel8f468632019-01-07 15:23:45 +00003263static int find_cpu_clusters(Object *child, void *opaque)
3264{
3265 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
3266 GDBState *s = (GDBState *) opaque;
3267 CPUClusterState *cluster = CPU_CLUSTER(child);
3268 GDBProcess *process;
3269
3270 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
3271
3272 process = &s->processes[s->process_num - 1];
3273
3274 /*
3275 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
3276 * runtime, we enforce here that the machine does not use a cluster ID
3277 * that would lead to PID 0.
3278 */
3279 assert(cluster->cluster_id != UINT32_MAX);
3280 process->pid = cluster->cluster_id + 1;
3281 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00003282 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00003283
3284 return 0;
3285 }
3286
3287 return object_child_foreach(child, find_cpu_clusters, opaque);
3288}
3289
3290static int pid_order(const void *a, const void *b)
3291{
3292 GDBProcess *pa = (GDBProcess *) a;
3293 GDBProcess *pb = (GDBProcess *) b;
3294
3295 if (pa->pid < pb->pid) {
3296 return -1;
3297 } else if (pa->pid > pb->pid) {
3298 return 1;
3299 } else {
3300 return 0;
3301 }
3302}
3303
3304static void create_processes(GDBState *s)
3305{
3306 object_child_foreach(object_get_root(), find_cpu_clusters, s);
3307
3308 if (s->processes) {
3309 /* Sort by PID */
3310 qsort(s->processes, s->process_num, sizeof(s->processes[0]), pid_order);
3311 }
3312
3313 create_default_process(s);
3314}
3315
aliguori59030a82009-04-05 18:43:41 +00003316int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00003317{
Doug Gale5c9522b2017-12-02 20:30:37 -05003318 trace_gdbstub_op_start(device);
3319
aliguori59030a82009-04-05 18:43:41 +00003320 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003321 Chardev *chr = NULL;
3322 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00003323
Ziyue Yang508b4ec2017-01-18 16:02:41 +08003324 if (!first_cpu) {
3325 error_report("gdbstub: meaningless to attach gdb to a "
3326 "machine without any CPU.");
3327 return -1;
3328 }
3329
aliguori59030a82009-04-05 18:43:41 +00003330 if (!device)
3331 return -1;
3332 if (strcmp(device, "none") != 0) {
3333 if (strstart(device, "tcp:", NULL)) {
3334 /* enforce required TCP attributes */
3335 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
3336 "%s,nowait,nodelay,server", device);
3337 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00003338 }
aliguori59030a82009-04-05 18:43:41 +00003339#ifndef _WIN32
3340 else if (strcmp(device, "stdio") == 0) {
3341 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00003342
aliguori59030a82009-04-05 18:43:41 +00003343 memset(&act, 0, sizeof(act));
3344 act.sa_handler = gdb_sigterm_handler;
3345 sigaction(SIGINT, &act, NULL);
3346 }
3347#endif
Marc-André Lureau95e30b22018-08-22 19:19:42 +02003348 /*
3349 * FIXME: it's a bit weird to allow using a mux chardev here
3350 * and implicitly setup a monitor. We may want to break this.
3351 */
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01003352 chr = qemu_chr_new_noreplay("gdb", device, true, NULL);
aliguori36556b22009-03-28 18:05:53 +00003353 if (!chr)
3354 return -1;
pbrookcfc34752007-02-22 01:48:01 +00003355 }
3356
Alex Bennée8d98c442020-03-16 17:21:33 +00003357 if (!gdbserver_state.init) {
3358 init_gdbserver_state();
pbrook4046d912007-01-28 01:53:16 +00003359
aliguori36556b22009-03-28 18:05:53 +00003360 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
3361
3362 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03003363 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01003364 NULL, NULL, &error_abort);
Kevin Wolf8e9119a2020-02-24 15:30:06 +01003365 monitor_init_hmp(mon_chr, false, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00003366 } else {
Alex Bennée8d98c442020-03-16 17:21:33 +00003367 qemu_chr_fe_deinit(&gdbserver_state.chr, true);
3368 mon_chr = gdbserver_state.mon_chr;
3369 reset_gdbserver_state();
aliguori36556b22009-03-28 18:05:53 +00003370 }
Luc Michel8f468632019-01-07 15:23:45 +00003371
Alex Bennée8d98c442020-03-16 17:21:33 +00003372 create_processes(&gdbserver_state);
Luc Michel8f468632019-01-07 15:23:45 +00003373
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03003374 if (chr) {
Alex Bennée8d98c442020-03-16 17:21:33 +00003375 qemu_chr_fe_init(&gdbserver_state.chr, chr, &error_abort);
3376 qemu_chr_fe_set_handlers(&gdbserver_state.chr, gdb_chr_can_receive,
3377 gdb_chr_receive, gdb_chr_event,
3378 NULL, &gdbserver_state, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03003379 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003380 gdbserver_state.state = chr ? RS_IDLE : RS_INACTIVE;
3381 gdbserver_state.mon_chr = mon_chr;
3382 gdbserver_state.current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00003383
pbrook4046d912007-01-28 01:53:16 +00003384 return 0;
3385}
Marc-André Lureau777357d2016-12-07 18:39:10 +03003386
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01003387void gdbserver_cleanup(void)
3388{
Alex Bennée8d98c442020-03-16 17:21:33 +00003389 if (gdbserver_state.init) {
3390 put_packet(&gdbserver_state, "W00");
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01003391 }
3392}
3393
Marc-André Lureau777357d2016-12-07 18:39:10 +03003394static void register_types(void)
3395{
3396 type_register_static(&char_gdb_type_info);
3397}
3398
3399type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00003400#endif