blob: 79e8ccc0503cc8d14a3d7a1fef4e71b8e8fd256f [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"
Paolo Bonzini243af022020-02-04 12:20:10 +010033#include "trace/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"
Pavel Dovgalyukfda84582020-10-03 20:13:43 +030054#include "sysemu/replay.h"
aurel32ca587a82008-12-18 22:44:13 +000055
Jan Kiszkaa3919382015-02-07 09:38:44 +010056#ifdef CONFIG_USER_ONLY
57#define GDB_ATTACHED "0"
58#else
59#define GDB_ATTACHED "1"
60#endif
61
Jon Doronab4752e2019-05-29 09:41:48 +030062#ifndef CONFIG_USER_ONLY
63static int phy_memory_mode;
64#endif
65
Andreas Färberf3659ee2013-06-27 19:09:09 +020066static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
67 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020068{
Jon Doronab4752e2019-05-29 09:41:48 +030069 CPUClass *cc;
Andreas Färberf3659ee2013-06-27 19:09:09 +020070
Jon Doronab4752e2019-05-29 09:41:48 +030071#ifndef CONFIG_USER_ONLY
72 if (phy_memory_mode) {
73 if (is_write) {
74 cpu_physical_memory_write(addr, buf, len);
75 } else {
76 cpu_physical_memory_read(addr, buf, len);
77 }
78 return 0;
79 }
80#endif
81
82 cc = CPU_GET_CLASS(cpu);
Andreas Färberf3659ee2013-06-27 19:09:09 +020083 if (cc->memory_rw_debug) {
84 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
85 }
86 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020087}
aurel32ca587a82008-12-18 22:44:13 +000088
Alex Bennéed2a6c852017-07-12 11:52:14 +010089/* Return the GDB index for a given vCPU state.
90 *
91 * For user mode this is simply the thread id. In system mode GDB
92 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
93 */
94static inline int cpu_gdb_index(CPUState *cpu)
95{
96#if defined(CONFIG_USER_ONLY)
Alex Bennéebd88c782017-07-12 11:52:15 +010097 TaskState *ts = (TaskState *) cpu->opaque;
98 return ts->ts_tid;
Alex Bennéed2a6c852017-07-12 11:52:14 +010099#else
100 return cpu->cpu_index + 1;
101#endif
102}
103
aurel32ca587a82008-12-18 22:44:13 +0000104enum {
105 GDB_SIGNAL_0 = 0,
106 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +0100107 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +0000108 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +0100109 GDB_SIGNAL_ABRT = 6,
110 GDB_SIGNAL_ALRM = 14,
111 GDB_SIGNAL_IO = 23,
112 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +0000113 GDB_SIGNAL_UNKNOWN = 143
114};
115
116#ifdef CONFIG_USER_ONLY
117
118/* Map target signal numbers to GDB protocol signal numbers and vice
119 * versa. For user emulation's currently supported systems, we can
120 * assume most signals are defined.
121 */
122
123static int gdb_signal_table[] = {
124 0,
125 TARGET_SIGHUP,
126 TARGET_SIGINT,
127 TARGET_SIGQUIT,
128 TARGET_SIGILL,
129 TARGET_SIGTRAP,
130 TARGET_SIGABRT,
131 -1, /* SIGEMT */
132 TARGET_SIGFPE,
133 TARGET_SIGKILL,
134 TARGET_SIGBUS,
135 TARGET_SIGSEGV,
136 TARGET_SIGSYS,
137 TARGET_SIGPIPE,
138 TARGET_SIGALRM,
139 TARGET_SIGTERM,
140 TARGET_SIGURG,
141 TARGET_SIGSTOP,
142 TARGET_SIGTSTP,
143 TARGET_SIGCONT,
144 TARGET_SIGCHLD,
145 TARGET_SIGTTIN,
146 TARGET_SIGTTOU,
147 TARGET_SIGIO,
148 TARGET_SIGXCPU,
149 TARGET_SIGXFSZ,
150 TARGET_SIGVTALRM,
151 TARGET_SIGPROF,
152 TARGET_SIGWINCH,
153 -1, /* SIGLOST */
154 TARGET_SIGUSR1,
155 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000156#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000157 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000158#else
159 -1,
160#endif
aurel32ca587a82008-12-18 22:44:13 +0000161 -1, /* SIGPOLL */
162 -1,
163 -1,
164 -1,
165 -1,
166 -1,
167 -1,
168 -1,
169 -1,
170 -1,
171 -1,
172 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000173#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000174 __SIGRTMIN + 1,
175 __SIGRTMIN + 2,
176 __SIGRTMIN + 3,
177 __SIGRTMIN + 4,
178 __SIGRTMIN + 5,
179 __SIGRTMIN + 6,
180 __SIGRTMIN + 7,
181 __SIGRTMIN + 8,
182 __SIGRTMIN + 9,
183 __SIGRTMIN + 10,
184 __SIGRTMIN + 11,
185 __SIGRTMIN + 12,
186 __SIGRTMIN + 13,
187 __SIGRTMIN + 14,
188 __SIGRTMIN + 15,
189 __SIGRTMIN + 16,
190 __SIGRTMIN + 17,
191 __SIGRTMIN + 18,
192 __SIGRTMIN + 19,
193 __SIGRTMIN + 20,
194 __SIGRTMIN + 21,
195 __SIGRTMIN + 22,
196 __SIGRTMIN + 23,
197 __SIGRTMIN + 24,
198 __SIGRTMIN + 25,
199 __SIGRTMIN + 26,
200 __SIGRTMIN + 27,
201 __SIGRTMIN + 28,
202 __SIGRTMIN + 29,
203 __SIGRTMIN + 30,
204 __SIGRTMIN + 31,
205 -1, /* SIGCANCEL */
206 __SIGRTMIN,
207 __SIGRTMIN + 32,
208 __SIGRTMIN + 33,
209 __SIGRTMIN + 34,
210 __SIGRTMIN + 35,
211 __SIGRTMIN + 36,
212 __SIGRTMIN + 37,
213 __SIGRTMIN + 38,
214 __SIGRTMIN + 39,
215 __SIGRTMIN + 40,
216 __SIGRTMIN + 41,
217 __SIGRTMIN + 42,
218 __SIGRTMIN + 43,
219 __SIGRTMIN + 44,
220 __SIGRTMIN + 45,
221 __SIGRTMIN + 46,
222 __SIGRTMIN + 47,
223 __SIGRTMIN + 48,
224 __SIGRTMIN + 49,
225 __SIGRTMIN + 50,
226 __SIGRTMIN + 51,
227 __SIGRTMIN + 52,
228 __SIGRTMIN + 53,
229 __SIGRTMIN + 54,
230 __SIGRTMIN + 55,
231 __SIGRTMIN + 56,
232 __SIGRTMIN + 57,
233 __SIGRTMIN + 58,
234 __SIGRTMIN + 59,
235 __SIGRTMIN + 60,
236 __SIGRTMIN + 61,
237 __SIGRTMIN + 62,
238 __SIGRTMIN + 63,
239 __SIGRTMIN + 64,
240 __SIGRTMIN + 65,
241 __SIGRTMIN + 66,
242 __SIGRTMIN + 67,
243 __SIGRTMIN + 68,
244 __SIGRTMIN + 69,
245 __SIGRTMIN + 70,
246 __SIGRTMIN + 71,
247 __SIGRTMIN + 72,
248 __SIGRTMIN + 73,
249 __SIGRTMIN + 74,
250 __SIGRTMIN + 75,
251 __SIGRTMIN + 76,
252 __SIGRTMIN + 77,
253 __SIGRTMIN + 78,
254 __SIGRTMIN + 79,
255 __SIGRTMIN + 80,
256 __SIGRTMIN + 81,
257 __SIGRTMIN + 82,
258 __SIGRTMIN + 83,
259 __SIGRTMIN + 84,
260 __SIGRTMIN + 85,
261 __SIGRTMIN + 86,
262 __SIGRTMIN + 87,
263 __SIGRTMIN + 88,
264 __SIGRTMIN + 89,
265 __SIGRTMIN + 90,
266 __SIGRTMIN + 91,
267 __SIGRTMIN + 92,
268 __SIGRTMIN + 93,
269 __SIGRTMIN + 94,
270 __SIGRTMIN + 95,
271 -1, /* SIGINFO */
272 -1, /* UNKNOWN */
273 -1, /* DEFAULT */
274 -1,
275 -1,
276 -1,
277 -1,
278 -1,
279 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000280#endif
aurel32ca587a82008-12-18 22:44:13 +0000281};
bellard8f447cc2006-06-14 15:21:14 +0000282#else
aurel32ca587a82008-12-18 22:44:13 +0000283/* In system mode we only need SIGINT and SIGTRAP; other signals
284 are not yet supported. */
285
286enum {
287 TARGET_SIGINT = 2,
288 TARGET_SIGTRAP = 5
289};
290
291static int gdb_signal_table[] = {
292 -1,
293 -1,
294 TARGET_SIGINT,
295 -1,
296 -1,
297 TARGET_SIGTRAP
298};
bellard8f447cc2006-06-14 15:21:14 +0000299#endif
bellardb4608c02003-06-27 17:34:32 +0000300
aurel32ca587a82008-12-18 22:44:13 +0000301#ifdef CONFIG_USER_ONLY
302static int target_signal_to_gdb (int sig)
303{
304 int i;
305 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
306 if (gdb_signal_table[i] == sig)
307 return i;
308 return GDB_SIGNAL_UNKNOWN;
309}
310#endif
311
312static int gdb_signal_to_target (int sig)
313{
314 if (sig < ARRAY_SIZE (gdb_signal_table))
315 return gdb_signal_table[sig];
316 else
317 return -1;
318}
319
pbrook56aebc82008-10-11 17:55:29 +0000320typedef struct GDBRegisterState {
321 int base_reg;
322 int num_regs;
Alex Bennéea010bdb2020-03-16 17:21:41 +0000323 gdb_get_reg_cb get_reg;
324 gdb_set_reg_cb set_reg;
pbrook56aebc82008-10-11 17:55:29 +0000325 const char *xml;
326 struct GDBRegisterState *next;
327} GDBRegisterState;
328
Luc Michel8f468632019-01-07 15:23:45 +0000329typedef struct GDBProcess {
330 uint32_t pid;
331 bool attached;
Luc Michelc145eea2019-01-07 15:23:46 +0000332
333 char target_xml[1024];
Luc Michel8f468632019-01-07 15:23:45 +0000334} GDBProcess;
335
bellard858693c2004-03-31 18:52:07 +0000336enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000337 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000338 RS_IDLE,
339 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400340 RS_GETLINE_ESC,
341 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000342 RS_CHKSUM1,
343 RS_CHKSUM2,
344};
bellard858693c2004-03-31 18:52:07 +0000345typedef struct GDBState {
Alex Bennée8d98c442020-03-16 17:21:33 +0000346 bool init; /* have we been initialised? */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200347 CPUState *c_cpu; /* current CPU for step/continue ops */
348 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200349 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000350 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000351 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000352 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400353 int line_sum; /* running checksum */
354 int line_csum; /* checksum at the end of the packet */
Damien Hedded116e812020-03-16 17:21:53 +0000355 GByteArray *last_packet;
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;
Alex Bennéefcedd922020-04-30 20:01:19 +0100359 char *socket_path;
bellard41625032005-04-24 10:07:11 +0000360 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000361#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300362 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300363 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000364#endif
Luc Michel8f468632019-01-07 15:23:45 +0000365 bool multiprocess;
366 GDBProcess *processes;
367 int process_num;
Meador Ingecdb432b2012-03-15 17:49:45 +0000368 char syscall_buf[256];
369 gdb_syscall_complete_cb current_syscall_cb;
Alex Bennée308f9e82020-03-16 17:21:35 +0000370 GString *str_buf;
Alex Bennée4a25f1b2020-03-16 17:21:36 +0000371 GByteArray *mem_buf;
bellard858693c2004-03-31 18:52:07 +0000372} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000373
edgar_igl60897d32008-05-09 08:25:14 +0000374/* By default use no IRQs and no timers while single stepping so as to
375 * make single stepping like an ICE HW step.
376 */
377static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
378
Pavel Dovgalyukfda84582020-10-03 20:13:43 +0300379/* Retrieves flags for single step mode. */
380static int get_sstep_flags(void)
381{
382 /*
383 * In replay mode all events written into the log should be replayed.
384 * That is why NOIRQ flag is removed in this mode.
385 */
386 if (replay_mode != REPLAY_MODE_NONE) {
387 return SSTEP_ENABLE;
388 } else {
389 return sstep_flags;
390 }
391}
392
Alex Bennée8d98c442020-03-16 17:21:33 +0000393static GDBState gdbserver_state;
394
395static void init_gdbserver_state(void)
396{
397 g_assert(!gdbserver_state.init);
398 memset(&gdbserver_state, 0, sizeof(GDBState));
399 gdbserver_state.init = true;
Alex Bennée308f9e82020-03-16 17:21:35 +0000400 gdbserver_state.str_buf = g_string_new(NULL);
Alex Bennée4a25f1b2020-03-16 17:21:36 +0000401 gdbserver_state.mem_buf = g_byte_array_sized_new(MAX_PACKET_LENGTH);
Damien Hedded116e812020-03-16 17:21:53 +0000402 gdbserver_state.last_packet = g_byte_array_sized_new(MAX_PACKET_LENGTH + 4);
Alex Bennée8d98c442020-03-16 17:21:33 +0000403}
404
405#ifndef CONFIG_USER_ONLY
406static void reset_gdbserver_state(void)
407{
408 g_free(gdbserver_state.processes);
409 gdbserver_state.processes = NULL;
410 gdbserver_state.process_num = 0;
411}
412#endif
aliguori880a7572008-11-18 20:30:24 +0000413
Andreas Färber5b50e792013-06-29 04:18:45 +0200414bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000415
bellard1fddef42005-04-17 19:16:13 +0000416#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000417
Alex Bennéea346af32020-03-16 17:21:34 +0000418static int get_char(void)
bellardb4608c02003-06-27 17:34:32 +0000419{
420 uint8_t ch;
421 int ret;
422
423 for(;;) {
Alex Bennéea346af32020-03-16 17:21:34 +0000424 ret = qemu_recv(gdbserver_state.fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000425 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000426 if (errno == ECONNRESET)
Alex Bennéea346af32020-03-16 17:21:34 +0000427 gdbserver_state.fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200428 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000429 return -1;
430 } else if (ret == 0) {
Alex Bennéea346af32020-03-16 17:21:34 +0000431 close(gdbserver_state.fd);
432 gdbserver_state.fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000433 return -1;
434 } else {
435 break;
436 }
437 }
438 return ch;
439}
pbrook4046d912007-01-28 01:53:16 +0000440#endif
bellardb4608c02003-06-27 17:34:32 +0000441
blueswir1654efcf2009-04-18 07:29:59 +0000442static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000443 GDB_SYS_UNKNOWN,
444 GDB_SYS_ENABLED,
445 GDB_SYS_DISABLED,
446} gdb_syscall_mode;
447
Liviu Ionescua38bb072014-12-11 12:07:48 +0000448/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000449int use_gdb_syscalls(void)
450{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100451 SemihostingTarget target = semihosting_get_target();
452 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000453 /* -semihosting-config target=native */
454 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100455 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000456 /* -semihosting-config target=gdb */
457 return true;
458 }
459
460 /* -semihosting-config target=auto */
461 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000462 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
Alex Bennée8d98c442020-03-16 17:21:33 +0000463 gdb_syscall_mode = gdbserver_state.init ?
464 GDB_SYS_ENABLED : GDB_SYS_DISABLED;
pbrooka2d1eba2007-01-28 03:10:55 +0000465 }
466 return gdb_syscall_mode == GDB_SYS_ENABLED;
467}
468
edgar_iglba70a622008-03-14 06:10:42 +0000469/* Resume execution. */
Alex Bennéea346af32020-03-16 17:21:34 +0000470static inline void gdb_continue(void)
edgar_iglba70a622008-03-14 06:10:42 +0000471{
Doug Gale5c9522b2017-12-02 20:30:37 -0500472
edgar_iglba70a622008-03-14 06:10:42 +0000473#ifdef CONFIG_USER_ONLY
Alex Bennéea346af32020-03-16 17:21:34 +0000474 gdbserver_state.running_state = 1;
Doug Gale5c9522b2017-12-02 20:30:37 -0500475 trace_gdbstub_op_continue();
edgar_iglba70a622008-03-14 06:10:42 +0000476#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200477 if (!runstate_needs_reset()) {
Doug Gale5c9522b2017-12-02 20:30:37 -0500478 trace_gdbstub_op_continue();
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200479 vm_start();
480 }
edgar_iglba70a622008-03-14 06:10:42 +0000481#endif
482}
483
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100484/*
485 * Resume execution, per CPU actions. For user-mode emulation it's
486 * equivalent to gdb_continue.
487 */
Alex Bennéea346af32020-03-16 17:21:34 +0000488static int gdb_continue_partial(char *newstates)
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100489{
490 CPUState *cpu;
491 int res = 0;
492#ifdef CONFIG_USER_ONLY
493 /*
494 * This is not exactly accurate, but it's an improvement compared to the
495 * previous situation, where only one CPU would be single-stepped.
496 */
497 CPU_FOREACH(cpu) {
498 if (newstates[cpu->cpu_index] == 's') {
Doug Gale5c9522b2017-12-02 20:30:37 -0500499 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100500 cpu_single_step(cpu, sstep_flags);
501 }
502 }
Alex Bennéea346af32020-03-16 17:21:34 +0000503 gdbserver_state.running_state = 1;
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100504#else
505 int flag = 0;
506
507 if (!runstate_needs_reset()) {
508 if (vm_prepare_start()) {
509 return 0;
510 }
511
512 CPU_FOREACH(cpu) {
513 switch (newstates[cpu->cpu_index]) {
514 case 0:
515 case 1:
516 break; /* nothing to do here */
517 case 's':
Doug Gale5c9522b2017-12-02 20:30:37 -0500518 trace_gdbstub_op_stepping(cpu->cpu_index);
Pavel Dovgalyukfda84582020-10-03 20:13:43 +0300519 cpu_single_step(cpu, get_sstep_flags());
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100520 cpu_resume(cpu);
521 flag = 1;
522 break;
523 case 'c':
Doug Gale5c9522b2017-12-02 20:30:37 -0500524 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100525 cpu_resume(cpu);
526 flag = 1;
527 break;
528 default:
529 res = -1;
530 break;
531 }
532 }
533 }
534 if (flag) {
535 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
536 }
537#endif
538 return res;
539}
540
Alex Bennéea346af32020-03-16 17:21:34 +0000541static void put_buffer(const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000542{
pbrook4046d912007-01-28 01:53:16 +0000543#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000544 int ret;
545
546 while (len > 0) {
Alex Bennéea346af32020-03-16 17:21:34 +0000547 ret = send(gdbserver_state.fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000548 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200549 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000550 return;
551 } else {
552 buf += ret;
553 len -= ret;
554 }
555 }
pbrook4046d912007-01-28 01:53:16 +0000556#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100557 /* XXX this blocks entire thread. Rewrite to use
558 * qemu_chr_fe_write and background I/O callbacks */
Alex Bennéea346af32020-03-16 17:21:34 +0000559 qemu_chr_fe_write_all(&gdbserver_state.chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000560#endif
bellardb4608c02003-06-27 17:34:32 +0000561}
562
563static inline int fromhex(int v)
564{
565 if (v >= '0' && v <= '9')
566 return v - '0';
567 else if (v >= 'A' && v <= 'F')
568 return v - 'A' + 10;
569 else if (v >= 'a' && v <= 'f')
570 return v - 'a' + 10;
571 else
572 return 0;
573}
574
575static inline int tohex(int v)
576{
577 if (v < 10)
578 return v + '0';
579 else
580 return v - 10 + 'a';
581}
582
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -0300583/* writes 2*len+1 bytes in buf */
Alex Bennée308f9e82020-03-16 17:21:35 +0000584static void memtohex(GString *buf, const uint8_t *mem, int len)
bellardb4608c02003-06-27 17:34:32 +0000585{
586 int i, c;
bellardb4608c02003-06-27 17:34:32 +0000587 for(i = 0; i < len; i++) {
588 c = mem[i];
Alex Bennée308f9e82020-03-16 17:21:35 +0000589 g_string_append_c(buf, tohex(c >> 4));
590 g_string_append_c(buf, tohex(c & 0xf));
bellardb4608c02003-06-27 17:34:32 +0000591 }
Alex Bennée308f9e82020-03-16 17:21:35 +0000592 g_string_append_c(buf, '\0');
bellardb4608c02003-06-27 17:34:32 +0000593}
594
Alex Bennée4a25f1b2020-03-16 17:21:36 +0000595static void hextomem(GByteArray *mem, const char *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000596{
597 int i;
598
599 for(i = 0; i < len; i++) {
Alex Bennée4a25f1b2020-03-16 17:21:36 +0000600 guint8 byte = fromhex(buf[0]) << 4 | fromhex(buf[1]);
601 g_byte_array_append(mem, &byte, 1);
bellardb4608c02003-06-27 17:34:32 +0000602 buf += 2;
603 }
604}
605
Doug Gale5c9522b2017-12-02 20:30:37 -0500606static void hexdump(const char *buf, int len,
607 void (*trace_fn)(size_t ofs, char const *text))
608{
609 char line_buffer[3 * 16 + 4 + 16 + 1];
610
611 size_t i;
612 for (i = 0; i < len || (i & 0xF); ++i) {
613 size_t byte_ofs = i & 15;
614
615 if (byte_ofs == 0) {
616 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
617 line_buffer[3 * 16 + 4 + 16] = 0;
618 }
619
620 size_t col_group = (i >> 2) & 3;
621 size_t hex_col = byte_ofs * 3 + col_group;
622 size_t txt_col = 3 * 16 + 4 + byte_ofs;
623
624 if (i < len) {
625 char value = buf[i];
626
627 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
628 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
629 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
630 ? value
631 : '.';
632 }
633
634 if (byte_ofs == 0xF)
635 trace_fn(i & -16, line_buffer);
636 }
637}
638
bellardb4608c02003-06-27 17:34:32 +0000639/* return -1 if error, 0 if OK */
Alex Bennéea346af32020-03-16 17:21:34 +0000640static int put_packet_binary(const char *buf, int len, bool dump)
bellardb4608c02003-06-27 17:34:32 +0000641{
pbrook56aebc82008-10-11 17:55:29 +0000642 int csum, i;
Damien Hedded116e812020-03-16 17:21:53 +0000643 uint8_t footer[3];
bellardb4608c02003-06-27 17:34:32 +0000644
Doug Gale5c9522b2017-12-02 20:30:37 -0500645 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
646 hexdump(buf, len, trace_gdbstub_io_binaryreply);
647 }
648
bellardb4608c02003-06-27 17:34:32 +0000649 for(;;) {
Damien Hedded116e812020-03-16 17:21:53 +0000650 g_byte_array_set_size(gdbserver_state.last_packet, 0);
651 g_byte_array_append(gdbserver_state.last_packet,
652 (const uint8_t *) "$", 1);
653 g_byte_array_append(gdbserver_state.last_packet,
654 (const uint8_t *) buf, len);
bellardb4608c02003-06-27 17:34:32 +0000655 csum = 0;
656 for(i = 0; i < len; i++) {
657 csum += buf[i];
658 }
Damien Hedded116e812020-03-16 17:21:53 +0000659 footer[0] = '#';
660 footer[1] = tohex((csum >> 4) & 0xf);
661 footer[2] = tohex((csum) & 0xf);
662 g_byte_array_append(gdbserver_state.last_packet, footer, 3);
bellardb4608c02003-06-27 17:34:32 +0000663
Damien Hedded116e812020-03-16 17:21:53 +0000664 put_buffer(gdbserver_state.last_packet->data,
665 gdbserver_state.last_packet->len);
bellardb4608c02003-06-27 17:34:32 +0000666
pbrook4046d912007-01-28 01:53:16 +0000667#ifdef CONFIG_USER_ONLY
Alex Bennéea346af32020-03-16 17:21:34 +0000668 i = get_char();
pbrook4046d912007-01-28 01:53:16 +0000669 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000670 return -1;
pbrook4046d912007-01-28 01:53:16 +0000671 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000672 break;
pbrook4046d912007-01-28 01:53:16 +0000673#else
674 break;
675#endif
bellardb4608c02003-06-27 17:34:32 +0000676 }
677 return 0;
678}
679
pbrook56aebc82008-10-11 17:55:29 +0000680/* return -1 if error, 0 if OK */
Alex Bennéea346af32020-03-16 17:21:34 +0000681static int put_packet(const char *buf)
pbrook56aebc82008-10-11 17:55:29 +0000682{
Doug Gale5c9522b2017-12-02 20:30:37 -0500683 trace_gdbstub_io_reply(buf);
pbrook56aebc82008-10-11 17:55:29 +0000684
Alex Bennéea346af32020-03-16 17:21:34 +0000685 return put_packet_binary(buf, strlen(buf), false);
pbrook56aebc82008-10-11 17:55:29 +0000686}
687
Alex Bennée308f9e82020-03-16 17:21:35 +0000688static void put_strbuf(void)
pbrook56aebc82008-10-11 17:55:29 +0000689{
Alex Bennée308f9e82020-03-16 17:21:35 +0000690 put_packet(gdbserver_state.str_buf->str);
691}
692
693/* Encode data using the encoding for 'x' packets. */
694static void memtox(GString *buf, const char *mem, int len)
695{
pbrook56aebc82008-10-11 17:55:29 +0000696 char c;
697
698 while (len--) {
699 c = *(mem++);
700 switch (c) {
701 case '#': case '$': case '*': case '}':
Alex Bennée308f9e82020-03-16 17:21:35 +0000702 g_string_append_c(buf, '}');
703 g_string_append_c(buf, c ^ 0x20);
pbrook56aebc82008-10-11 17:55:29 +0000704 break;
705 default:
Alex Bennée308f9e82020-03-16 17:21:35 +0000706 g_string_append_c(buf, c);
pbrook56aebc82008-10-11 17:55:29 +0000707 break;
708 }
709 }
pbrook56aebc82008-10-11 17:55:29 +0000710}
711
Alex Bennéea346af32020-03-16 17:21:34 +0000712static uint32_t gdb_get_cpu_pid(CPUState *cpu)
Luc Michel1a227332019-01-07 15:23:45 +0000713{
Luc Michel1a227332019-01-07 15:23:45 +0000714 /* TODO: In user mode, we should use the task state PID */
Peter Maydell46f5abc2019-01-29 11:46:06 +0000715 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
716 /* Return the default process' PID */
Alex Bennéea346af32020-03-16 17:21:34 +0000717 int index = gdbserver_state.process_num - 1;
718 return gdbserver_state.processes[index].pid;
Peter Maydell46f5abc2019-01-29 11:46:06 +0000719 }
720 return cpu->cluster_index + 1;
Luc Michel1a227332019-01-07 15:23:45 +0000721}
722
Alex Bennéea346af32020-03-16 17:21:34 +0000723static GDBProcess *gdb_get_process(uint32_t pid)
Luc Michel7d8c87d2019-01-07 15:23:45 +0000724{
725 int i;
726
727 if (!pid) {
728 /* 0 means any process, we take the first one */
Alex Bennéea346af32020-03-16 17:21:34 +0000729 return &gdbserver_state.processes[0];
Luc Michel7d8c87d2019-01-07 15:23:45 +0000730 }
731
Alex Bennéea346af32020-03-16 17:21:34 +0000732 for (i = 0; i < gdbserver_state.process_num; i++) {
733 if (gdbserver_state.processes[i].pid == pid) {
734 return &gdbserver_state.processes[i];
Luc Michel7d8c87d2019-01-07 15:23:45 +0000735 }
736 }
737
738 return NULL;
739}
740
Alex Bennéea346af32020-03-16 17:21:34 +0000741static GDBProcess *gdb_get_cpu_process(CPUState *cpu)
Luc Michel7d8c87d2019-01-07 15:23:45 +0000742{
Alex Bennéea346af32020-03-16 17:21:34 +0000743 return gdb_get_process(gdb_get_cpu_pid(cpu));
Luc Michel7d8c87d2019-01-07 15:23:45 +0000744}
745
746static CPUState *find_cpu(uint32_t thread_id)
747{
748 CPUState *cpu;
749
750 CPU_FOREACH(cpu) {
751 if (cpu_gdb_index(cpu) == thread_id) {
752 return cpu;
753 }
754 }
755
756 return NULL;
757}
758
Alex Bennéea346af32020-03-16 17:21:34 +0000759static CPUState *get_first_cpu_in_process(GDBProcess *process)
Luc Michele40e5202019-01-07 15:23:46 +0000760{
761 CPUState *cpu;
762
763 CPU_FOREACH(cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +0000764 if (gdb_get_cpu_pid(cpu) == process->pid) {
Luc Michele40e5202019-01-07 15:23:46 +0000765 return cpu;
766 }
767 }
768
769 return NULL;
770}
771
Alex Bennéea346af32020-03-16 17:21:34 +0000772static CPUState *gdb_next_cpu_in_process(CPUState *cpu)
Luc Michele40e5202019-01-07 15:23:46 +0000773{
Alex Bennéea346af32020-03-16 17:21:34 +0000774 uint32_t pid = gdb_get_cpu_pid(cpu);
Luc Michele40e5202019-01-07 15:23:46 +0000775 cpu = CPU_NEXT(cpu);
776
777 while (cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +0000778 if (gdb_get_cpu_pid(cpu) == pid) {
Luc Michele40e5202019-01-07 15:23:46 +0000779 break;
780 }
781
782 cpu = CPU_NEXT(cpu);
783 }
784
785 return cpu;
786}
787
Luc Michele40e5202019-01-07 15:23:46 +0000788/* Return the cpu following @cpu, while ignoring unattached processes. */
Alex Bennéea346af32020-03-16 17:21:34 +0000789static CPUState *gdb_next_attached_cpu(CPUState *cpu)
Luc Michele40e5202019-01-07 15:23:46 +0000790{
791 cpu = CPU_NEXT(cpu);
792
793 while (cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +0000794 if (gdb_get_cpu_process(cpu)->attached) {
Luc Michele40e5202019-01-07 15:23:46 +0000795 break;
796 }
797
798 cpu = CPU_NEXT(cpu);
799 }
800
801 return cpu;
802}
803
804/* Return the first attached cpu */
Alex Bennéea346af32020-03-16 17:21:34 +0000805static CPUState *gdb_first_attached_cpu(void)
Luc Michele40e5202019-01-07 15:23:46 +0000806{
807 CPUState *cpu = first_cpu;
Alex Bennéea346af32020-03-16 17:21:34 +0000808 GDBProcess *process = gdb_get_cpu_process(cpu);
Luc Michele40e5202019-01-07 15:23:46 +0000809
810 if (!process->attached) {
Alex Bennéea346af32020-03-16 17:21:34 +0000811 return gdb_next_attached_cpu(cpu);
Luc Michele40e5202019-01-07 15:23:46 +0000812 }
813
814 return cpu;
815}
816
Alex Bennéea346af32020-03-16 17:21:34 +0000817static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid)
Luc Michelab65eed2019-01-29 11:46:03 +0000818{
819 GDBProcess *process;
820 CPUState *cpu;
821
822 if (!pid && !tid) {
823 /* 0 means any process/thread, we take the first attached one */
Alex Bennéea346af32020-03-16 17:21:34 +0000824 return gdb_first_attached_cpu();
Luc Michelab65eed2019-01-29 11:46:03 +0000825 } else if (pid && !tid) {
826 /* any thread in a specific process */
Alex Bennéea346af32020-03-16 17:21:34 +0000827 process = gdb_get_process(pid);
Luc Michelab65eed2019-01-29 11:46:03 +0000828
829 if (process == NULL) {
830 return NULL;
831 }
832
833 if (!process->attached) {
834 return NULL;
835 }
836
Alex Bennéea346af32020-03-16 17:21:34 +0000837 return get_first_cpu_in_process(process);
Luc Michelab65eed2019-01-29 11:46:03 +0000838 } else {
839 /* a specific thread */
840 cpu = find_cpu(tid);
841
842 if (cpu == NULL) {
843 return NULL;
844 }
845
Alex Bennéea346af32020-03-16 17:21:34 +0000846 process = gdb_get_cpu_process(cpu);
Luc Michelab65eed2019-01-29 11:46:03 +0000847
848 if (pid && process->pid != pid) {
849 return NULL;
850 }
851
852 if (!process->attached) {
853 return NULL;
854 }
855
856 return cpu;
857 }
858}
859
Alex Bennéea346af32020-03-16 17:21:34 +0000860static const char *get_feature_xml(const char *p, const char **newp,
861 GDBProcess *process)
pbrook56aebc82008-10-11 17:55:29 +0000862{
pbrook56aebc82008-10-11 17:55:29 +0000863 size_t len;
864 int i;
865 const char *name;
Alex Bennéea346af32020-03-16 17:21:34 +0000866 CPUState *cpu = get_first_cpu_in_process(process);
Luc Michelc145eea2019-01-07 15:23:46 +0000867 CPUClass *cc = CPU_GET_CLASS(cpu);
pbrook56aebc82008-10-11 17:55:29 +0000868
869 len = 0;
870 while (p[len] && p[len] != ':')
871 len++;
872 *newp = p + len;
873
874 name = NULL;
875 if (strncmp(p, "target.xml", len) == 0) {
Luc Michelc145eea2019-01-07 15:23:46 +0000876 char *buf = process->target_xml;
877 const size_t buf_sz = sizeof(process->target_xml);
pbrook56aebc82008-10-11 17:55:29 +0000878
Luc Michelc145eea2019-01-07 15:23:46 +0000879 /* Generate the XML description for this CPU. */
880 if (!buf[0]) {
881 GDBRegisterState *r;
882
883 pstrcat(buf, buf_sz,
David Hildenbrandb3820e62015-12-03 13:14:41 +0100884 "<?xml version=\"1.0\"?>"
885 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
886 "<target>");
887 if (cc->gdb_arch_name) {
888 gchar *arch = cc->gdb_arch_name(cpu);
Luc Michelc145eea2019-01-07 15:23:46 +0000889 pstrcat(buf, buf_sz, "<architecture>");
890 pstrcat(buf, buf_sz, arch);
891 pstrcat(buf, buf_sz, "</architecture>");
David Hildenbrandb3820e62015-12-03 13:14:41 +0100892 g_free(arch);
893 }
Luc Michelc145eea2019-01-07 15:23:46 +0000894 pstrcat(buf, buf_sz, "<xi:include href=\"");
895 pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
896 pstrcat(buf, buf_sz, "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200897 for (r = cpu->gdb_regs; r; r = r->next) {
Luc Michelc145eea2019-01-07 15:23:46 +0000898 pstrcat(buf, buf_sz, "<xi:include href=\"");
899 pstrcat(buf, buf_sz, r->xml);
900 pstrcat(buf, buf_sz, "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000901 }
Luc Michelc145eea2019-01-07 15:23:46 +0000902 pstrcat(buf, buf_sz, "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000903 }
Luc Michelc145eea2019-01-07 15:23:46 +0000904 return buf;
pbrook56aebc82008-10-11 17:55:29 +0000905 }
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100906 if (cc->gdb_get_dynamic_xml) {
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100907 char *xmlname = g_strndup(p, len);
908 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
909
910 g_free(xmlname);
911 if (xml) {
912 return xml;
913 }
914 }
pbrook56aebc82008-10-11 17:55:29 +0000915 for (i = 0; ; i++) {
916 name = xml_builtin[i][0];
917 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
918 break;
919 }
920 return name ? xml_builtin[i][1] : NULL;
921}
pbrook56aebc82008-10-11 17:55:29 +0000922
Alex Bennéea010bdb2020-03-16 17:21:41 +0000923static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000924{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200925 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200926 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000927 GDBRegisterState *r;
928
Andreas Färbera0e372f2013-06-28 23:18:47 +0200929 if (reg < cc->gdb_num_core_regs) {
Alex Bennéea010bdb2020-03-16 17:21:41 +0000930 return cc->gdb_read_register(cpu, buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200931 }
pbrook56aebc82008-10-11 17:55:29 +0000932
Andreas Färbereac8b352013-06-28 21:11:37 +0200933 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000934 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
Alex Bennéea010bdb2020-03-16 17:21:41 +0000935 return r->get_reg(env, buf, reg - r->base_reg);
pbrook56aebc82008-10-11 17:55:29 +0000936 }
937 }
938 return 0;
939}
940
Andreas Färber385b9f02013-06-27 18:25:36 +0200941static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000942{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200943 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200944 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000945 GDBRegisterState *r;
946
Andreas Färbera0e372f2013-06-28 23:18:47 +0200947 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200948 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200949 }
pbrook56aebc82008-10-11 17:55:29 +0000950
Andreas Färbereac8b352013-06-28 21:11:37 +0200951 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000952 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
953 return r->set_reg(env, mem_buf, reg - r->base_reg);
954 }
955 }
956 return 0;
957}
958
959/* Register a supplemental set of CPU registers. If g_pos is nonzero it
960 specifies the first register number and these registers are included in
961 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
962 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
963 */
964
Andreas Färber22169d42013-06-28 21:27:39 +0200965void gdb_register_coprocessor(CPUState *cpu,
Alex Bennéea010bdb2020-03-16 17:21:41 +0000966 gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
Andreas Färber22169d42013-06-28 21:27:39 +0200967 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000968{
969 GDBRegisterState *s;
970 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000971
Andreas Färbereac8b352013-06-28 21:11:37 +0200972 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000973 while (*p) {
974 /* Check for duplicates. */
975 if (strcmp((*p)->xml, xml) == 0)
976 return;
977 p = &(*p)->next;
978 }
Stefan Weil9643c252011-10-18 22:25:38 +0200979
980 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200981 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200982 s->num_regs = num_regs;
983 s->get_reg = get_reg;
984 s->set_reg = set_reg;
985 s->xml = xml;
986
pbrook56aebc82008-10-11 17:55:29 +0000987 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200988 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000989 *p = s;
990 if (g_pos) {
991 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800992 error_report("Error: Bad gdb register numbering for '%s', "
993 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200994 } else {
995 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000996 }
997 }
998}
999
aliguoria1d1bb32008-11-18 20:07:32 +00001000#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +01001001/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
1002static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
1003{
1004 static const int xlat[] = {
1005 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
1006 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
1007 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
1008 };
1009
1010 CPUClass *cc = CPU_GET_CLASS(cpu);
1011 int cputype = xlat[gdbtype];
1012
1013 if (cc->gdb_stop_before_watchpoint) {
1014 cputype |= BP_STOP_BEFORE_ACCESS;
1015 }
1016 return cputype;
1017}
aliguoria1d1bb32008-11-18 20:07:32 +00001018#endif
1019
Jon Doron77f6ce52019-05-29 09:41:35 +03001020static int gdb_breakpoint_insert(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +00001021{
Andreas Färber182735e2013-05-29 22:29:20 +02001022 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001023 int err = 0;
1024
Andreas Färber62278812013-06-27 17:12:06 +02001025 if (kvm_enabled()) {
Alex Bennée8d98c442020-03-16 17:21:33 +00001026 return kvm_insert_breakpoint(gdbserver_state.c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001027 }
aliguorie22a25c2009-03-12 20:12:48 +00001028
aliguoria1d1bb32008-11-18 20:07:32 +00001029 switch (type) {
1030 case GDB_BREAKPOINT_SW:
1031 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001032 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001033 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
1034 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001035 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001036 }
aliguori880a7572008-11-18 20:30:24 +00001037 }
1038 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001039#ifndef CONFIG_USER_ONLY
1040 case GDB_WATCHPOINT_WRITE:
1041 case GDB_WATCHPOINT_READ:
1042 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001043 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001044 err = cpu_watchpoint_insert(cpu, addr, len,
1045 xlat_gdb_type(cpu, type), NULL);
1046 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001047 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +01001048 }
aliguori880a7572008-11-18 20:30:24 +00001049 }
1050 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001051#endif
1052 default:
1053 return -ENOSYS;
1054 }
1055}
1056
Jon Doron77f6ce52019-05-29 09:41:35 +03001057static int gdb_breakpoint_remove(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +00001058{
Andreas Färber182735e2013-05-29 22:29:20 +02001059 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001060 int err = 0;
1061
Andreas Färber62278812013-06-27 17:12:06 +02001062 if (kvm_enabled()) {
Alex Bennée8d98c442020-03-16 17:21:33 +00001063 return kvm_remove_breakpoint(gdbserver_state.c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001064 }
aliguorie22a25c2009-03-12 20:12:48 +00001065
aliguoria1d1bb32008-11-18 20:07:32 +00001066 switch (type) {
1067 case GDB_BREAKPOINT_SW:
1068 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001069 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001070 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
1071 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001072 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001073 }
aliguori880a7572008-11-18 20:30:24 +00001074 }
1075 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001076#ifndef CONFIG_USER_ONLY
1077 case GDB_WATCHPOINT_WRITE:
1078 case GDB_WATCHPOINT_READ:
1079 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001080 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001081 err = cpu_watchpoint_remove(cpu, addr, len,
1082 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +00001083 if (err)
1084 break;
1085 }
1086 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001087#endif
1088 default:
1089 return -ENOSYS;
1090 }
1091}
1092
Luc Michel546f3c62019-01-07 15:23:46 +00001093static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu)
1094{
1095 cpu_breakpoint_remove_all(cpu, BP_GDB);
1096#ifndef CONFIG_USER_ONLY
1097 cpu_watchpoint_remove_all(cpu, BP_GDB);
1098#endif
1099}
1100
Alex Bennéea346af32020-03-16 17:21:34 +00001101static void gdb_process_breakpoint_remove_all(GDBProcess *p)
Luc Michel546f3c62019-01-07 15:23:46 +00001102{
Alex Bennéea346af32020-03-16 17:21:34 +00001103 CPUState *cpu = get_first_cpu_in_process(p);
Luc Michel546f3c62019-01-07 15:23:46 +00001104
1105 while (cpu) {
1106 gdb_cpu_breakpoint_remove_all(cpu);
Alex Bennéea346af32020-03-16 17:21:34 +00001107 cpu = gdb_next_cpu_in_process(cpu);
Luc Michel546f3c62019-01-07 15:23:46 +00001108 }
1109}
1110
aliguori880a7572008-11-18 20:30:24 +00001111static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +00001112{
Andreas Färber182735e2013-05-29 22:29:20 +02001113 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001114
aliguorie22a25c2009-03-12 20:12:48 +00001115 if (kvm_enabled()) {
Alex Bennée8d98c442020-03-16 17:21:33 +00001116 kvm_remove_all_breakpoints(gdbserver_state.c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +00001117 return;
1118 }
1119
Andreas Färberbdc44642013-06-24 23:50:24 +02001120 CPU_FOREACH(cpu) {
Luc Michel546f3c62019-01-07 15:23:46 +00001121 gdb_cpu_breakpoint_remove_all(cpu);
aliguori880a7572008-11-18 20:30:24 +00001122 }
aliguoria1d1bb32008-11-18 20:07:32 +00001123}
1124
Alex Bennéea346af32020-03-16 17:21:34 +00001125static void gdb_set_cpu_pc(target_ulong pc)
aurel32fab9d282009-04-08 21:29:37 +00001126{
Alex Bennéea346af32020-03-16 17:21:34 +00001127 CPUState *cpu = gdbserver_state.c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +02001128
1129 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -07001130 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +00001131}
1132
Alex Bennée308f9e82020-03-16 17:21:35 +00001133static void gdb_append_thread_id(CPUState *cpu, GString *buf)
Luc Michel1a227332019-01-07 15:23:45 +00001134{
Alex Bennéea346af32020-03-16 17:21:34 +00001135 if (gdbserver_state.multiprocess) {
Alex Bennée308f9e82020-03-16 17:21:35 +00001136 g_string_append_printf(buf, "p%02x.%02x",
1137 gdb_get_cpu_pid(cpu), cpu_gdb_index(cpu));
Luc Michel1a227332019-01-07 15:23:45 +00001138 } else {
Alex Bennée308f9e82020-03-16 17:21:35 +00001139 g_string_append_printf(buf, "%02x", cpu_gdb_index(cpu));
Luc Michel1a227332019-01-07 15:23:45 +00001140 }
Luc Michel1a227332019-01-07 15:23:45 +00001141}
1142
Luc Michel7d8c87d2019-01-07 15:23:45 +00001143typedef enum GDBThreadIdKind {
1144 GDB_ONE_THREAD = 0,
1145 GDB_ALL_THREADS, /* One process, all threads */
1146 GDB_ALL_PROCESSES,
1147 GDB_READ_THREAD_ERR
1148} GDBThreadIdKind;
1149
1150static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1151 uint32_t *pid, uint32_t *tid)
1152{
1153 unsigned long p, t;
1154 int ret;
1155
1156 if (*buf == 'p') {
1157 buf++;
1158 ret = qemu_strtoul(buf, &buf, 16, &p);
1159
1160 if (ret) {
1161 return GDB_READ_THREAD_ERR;
1162 }
1163
1164 /* Skip '.' */
1165 buf++;
1166 } else {
1167 p = 1;
1168 }
1169
1170 ret = qemu_strtoul(buf, &buf, 16, &t);
1171
1172 if (ret) {
1173 return GDB_READ_THREAD_ERR;
1174 }
1175
1176 *end_buf = buf;
1177
1178 if (p == -1) {
1179 return GDB_ALL_PROCESSES;
1180 }
1181
1182 if (pid) {
1183 *pid = p;
1184 }
1185
1186 if (t == -1) {
1187 return GDB_ALL_THREADS;
1188 }
1189
1190 if (tid) {
1191 *tid = t;
1192 }
1193
1194 return GDB_ONE_THREAD;
1195}
1196
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001197/**
1198 * gdb_handle_vcont - Parses and handles a vCont packet.
1199 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1200 * a format error, 0 on success.
1201 */
Alex Bennéea346af32020-03-16 17:21:34 +00001202static int gdb_handle_vcont(const char *p)
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001203{
Luc Michele40e5202019-01-07 15:23:46 +00001204 int res, signal = 0;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001205 char cur_action;
1206 char *newstates;
1207 unsigned long tmp;
Luc Michele40e5202019-01-07 15:23:46 +00001208 uint32_t pid, tid;
1209 GDBProcess *process;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001210 CPUState *cpu;
Luc Michelc99ef792019-03-26 12:53:26 +00001211 GDBThreadIdKind kind;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001212#ifdef CONFIG_USER_ONLY
1213 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
1214
1215 CPU_FOREACH(cpu) {
1216 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1217 }
Like Xu5cc87672019-05-19 04:54:21 +08001218#else
1219 MachineState *ms = MACHINE(qdev_get_machine());
1220 unsigned int max_cpus = ms->smp.max_cpus;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001221#endif
1222 /* uninitialised CPUs stay 0 */
1223 newstates = g_new0(char, max_cpus);
1224
1225 /* mark valid CPUs with 1 */
1226 CPU_FOREACH(cpu) {
1227 newstates[cpu->cpu_index] = 1;
1228 }
1229
1230 /*
1231 * res keeps track of what error we are returning, with -ENOTSUP meaning
1232 * that the command is unknown or unsupported, thus returning an empty
1233 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1234 * or incorrect parameters passed.
1235 */
1236 res = 0;
1237 while (*p) {
1238 if (*p++ != ';') {
1239 res = -ENOTSUP;
1240 goto out;
1241 }
1242
1243 cur_action = *p++;
1244 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +01001245 cur_action = qemu_tolower(cur_action);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001246 res = qemu_strtoul(p + 1, &p, 16, &tmp);
1247 if (res) {
1248 goto out;
1249 }
1250 signal = gdb_signal_to_target(tmp);
1251 } else if (cur_action != 'c' && cur_action != 's') {
1252 /* unknown/invalid/unsupported command */
1253 res = -ENOTSUP;
1254 goto out;
1255 }
Luc Michele40e5202019-01-07 15:23:46 +00001256
Luc Michelc99ef792019-03-26 12:53:26 +00001257 if (*p == '\0' || *p == ';') {
1258 /*
1259 * No thread specifier, action is on "all threads". The
1260 * specification is unclear regarding the process to act on. We
1261 * choose all processes.
1262 */
1263 kind = GDB_ALL_PROCESSES;
1264 } else if (*p++ == ':') {
1265 kind = read_thread_id(p, &p, &pid, &tid);
1266 } else {
Luc Michele40e5202019-01-07 15:23:46 +00001267 res = -ENOTSUP;
1268 goto out;
1269 }
1270
Luc Michelc99ef792019-03-26 12:53:26 +00001271 switch (kind) {
Luc Michele40e5202019-01-07 15:23:46 +00001272 case GDB_READ_THREAD_ERR:
1273 res = -EINVAL;
1274 goto out;
1275
1276 case GDB_ALL_PROCESSES:
Alex Bennéea346af32020-03-16 17:21:34 +00001277 cpu = gdb_first_attached_cpu();
Luc Michele40e5202019-01-07 15:23:46 +00001278 while (cpu) {
1279 if (newstates[cpu->cpu_index] == 1) {
1280 newstates[cpu->cpu_index] = cur_action;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001281 }
Luc Michele40e5202019-01-07 15:23:46 +00001282
Alex Bennéea346af32020-03-16 17:21:34 +00001283 cpu = gdb_next_attached_cpu(cpu);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001284 }
Luc Michele40e5202019-01-07 15:23:46 +00001285 break;
1286
1287 case GDB_ALL_THREADS:
Alex Bennéea346af32020-03-16 17:21:34 +00001288 process = gdb_get_process(pid);
Luc Michele40e5202019-01-07 15:23:46 +00001289
1290 if (!process->attached) {
1291 res = -EINVAL;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001292 goto out;
1293 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001294
Alex Bennéea346af32020-03-16 17:21:34 +00001295 cpu = get_first_cpu_in_process(process);
Luc Michele40e5202019-01-07 15:23:46 +00001296 while (cpu) {
1297 if (newstates[cpu->cpu_index] == 1) {
1298 newstates[cpu->cpu_index] = cur_action;
1299 }
1300
Alex Bennéea346af32020-03-16 17:21:34 +00001301 cpu = gdb_next_cpu_in_process(cpu);
Luc Michele40e5202019-01-07 15:23:46 +00001302 }
1303 break;
1304
1305 case GDB_ONE_THREAD:
Alex Bennéea346af32020-03-16 17:21:34 +00001306 cpu = gdb_get_cpu(pid, tid);
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001307
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001308 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001309 if (!cpu) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001310 res = -EINVAL;
1311 goto out;
1312 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001313
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001314 /* only use if no previous match occourred */
1315 if (newstates[cpu->cpu_index] == 1) {
1316 newstates[cpu->cpu_index] = cur_action;
1317 }
Luc Michele40e5202019-01-07 15:23:46 +00001318 break;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001319 }
1320 }
Alex Bennéea346af32020-03-16 17:21:34 +00001321 gdbserver_state.signal = signal;
1322 gdb_continue_partial(newstates);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001323
1324out:
1325 g_free(newstates);
1326
1327 return res;
1328}
1329
Jon Dorond14055d2019-05-29 09:41:29 +03001330typedef union GdbCmdVariant {
1331 const char *data;
1332 uint8_t opcode;
1333 unsigned long val_ul;
1334 unsigned long long val_ull;
1335 struct {
1336 GDBThreadIdKind kind;
1337 uint32_t pid;
1338 uint32_t tid;
1339 } thread_id;
1340} GdbCmdVariant;
1341
1342static const char *cmd_next_param(const char *param, const char delimiter)
1343{
1344 static const char all_delimiters[] = ",;:=";
1345 char curr_delimiters[2] = {0};
1346 const char *delimiters;
1347
1348 if (delimiter == '?') {
1349 delimiters = all_delimiters;
1350 } else if (delimiter == '0') {
1351 return strchr(param, '\0');
1352 } else if (delimiter == '.' && *param) {
1353 return param + 1;
1354 } else {
1355 curr_delimiters[0] = delimiter;
1356 delimiters = curr_delimiters;
1357 }
1358
1359 param += strcspn(param, delimiters);
1360 if (*param) {
1361 param++;
1362 }
1363 return param;
1364}
1365
1366static int cmd_parse_params(const char *data, const char *schema,
1367 GdbCmdVariant *params, int *num_params)
1368{
1369 int curr_param;
1370 const char *curr_schema, *curr_data;
1371
1372 *num_params = 0;
1373
1374 if (!schema) {
1375 return 0;
1376 }
1377
1378 curr_schema = schema;
1379 curr_param = 0;
1380 curr_data = data;
1381 while (curr_schema[0] && curr_schema[1] && *curr_data) {
1382 switch (curr_schema[0]) {
1383 case 'l':
1384 if (qemu_strtoul(curr_data, &curr_data, 16,
1385 &params[curr_param].val_ul)) {
1386 return -EINVAL;
1387 }
1388 curr_param++;
1389 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1390 break;
1391 case 'L':
1392 if (qemu_strtou64(curr_data, &curr_data, 16,
1393 (uint64_t *)&params[curr_param].val_ull)) {
1394 return -EINVAL;
1395 }
1396 curr_param++;
1397 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1398 break;
1399 case 's':
1400 params[curr_param].data = curr_data;
1401 curr_param++;
1402 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1403 break;
1404 case 'o':
1405 params[curr_param].opcode = *(uint8_t *)curr_data;
1406 curr_param++;
1407 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1408 break;
1409 case 't':
1410 params[curr_param].thread_id.kind =
1411 read_thread_id(curr_data, &curr_data,
1412 &params[curr_param].thread_id.pid,
1413 &params[curr_param].thread_id.tid);
1414 curr_param++;
1415 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1416 break;
1417 case '?':
1418 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1419 break;
1420 default:
1421 return -EINVAL;
1422 }
1423 curr_schema += 2;
1424 }
1425
1426 *num_params = curr_param;
1427 return 0;
1428}
1429
1430typedef struct GdbCmdContext {
Jon Dorond14055d2019-05-29 09:41:29 +03001431 GdbCmdVariant *params;
1432 int num_params;
Jon Dorond14055d2019-05-29 09:41:29 +03001433} GdbCmdContext;
1434
1435typedef void (*GdbCmdHandler)(GdbCmdContext *gdb_ctx, void *user_ctx);
1436
1437/*
1438 * cmd_startswith -> cmd is compared using startswith
1439 *
1440 *
1441 * schema definitions:
1442 * Each schema parameter entry consists of 2 chars,
1443 * the first char represents the parameter type handling
1444 * the second char represents the delimiter for the next parameter
1445 *
1446 * Currently supported schema types:
1447 * 'l' -> unsigned long (stored in .val_ul)
1448 * 'L' -> unsigned long long (stored in .val_ull)
1449 * 's' -> string (stored in .data)
1450 * 'o' -> single char (stored in .opcode)
1451 * 't' -> thread id (stored in .thread_id)
1452 * '?' -> skip according to delimiter
1453 *
1454 * Currently supported delimiters:
1455 * '?' -> Stop at any delimiter (",;:=\0")
1456 * '0' -> Stop at "\0"
1457 * '.' -> Skip 1 char unless reached "\0"
1458 * Any other value is treated as the delimiter value itself
1459 */
1460typedef struct GdbCmdParseEntry {
1461 GdbCmdHandler handler;
1462 const char *cmd;
1463 bool cmd_startswith;
1464 const char *schema;
1465} GdbCmdParseEntry;
1466
1467static inline int startswith(const char *string, const char *pattern)
1468{
1469 return !strncmp(string, pattern, strlen(pattern));
1470}
1471
Alex Bennéea346af32020-03-16 17:21:34 +00001472static int process_string_cmd(void *user_ctx, const char *data,
Jon Dorond14055d2019-05-29 09:41:29 +03001473 const GdbCmdParseEntry *cmds, int num_cmds)
1474{
1475 int i, schema_len, max_num_params = 0;
1476 GdbCmdContext gdb_ctx;
1477
1478 if (!cmds) {
1479 return -1;
1480 }
1481
1482 for (i = 0; i < num_cmds; i++) {
1483 const GdbCmdParseEntry *cmd = &cmds[i];
1484 g_assert(cmd->handler && cmd->cmd);
1485
1486 if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
1487 (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
1488 continue;
1489 }
1490
1491 if (cmd->schema) {
1492 schema_len = strlen(cmd->schema);
1493 if (schema_len % 2) {
1494 return -2;
1495 }
1496
1497 max_num_params = schema_len / 2;
1498 }
1499
1500 gdb_ctx.params =
1501 (GdbCmdVariant *)alloca(sizeof(*gdb_ctx.params) * max_num_params);
1502 memset(gdb_ctx.params, 0, sizeof(*gdb_ctx.params) * max_num_params);
1503
1504 if (cmd_parse_params(&data[strlen(cmd->cmd)], cmd->schema,
1505 gdb_ctx.params, &gdb_ctx.num_params)) {
1506 return -1;
1507 }
1508
Jon Dorond14055d2019-05-29 09:41:29 +03001509 cmd->handler(&gdb_ctx, user_ctx);
1510 return 0;
1511 }
1512
1513 return -1;
1514}
1515
Alex Bennéea346af32020-03-16 17:21:34 +00001516static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd)
Jon Doron3e2c1262019-05-29 09:41:30 +03001517{
1518 if (!data) {
1519 return;
1520 }
1521
Alex Bennée308f9e82020-03-16 17:21:35 +00001522 g_string_set_size(gdbserver_state.str_buf, 0);
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001523 g_byte_array_set_size(gdbserver_state.mem_buf, 0);
Alex Bennée308f9e82020-03-16 17:21:35 +00001524
Jon Doron3e2c1262019-05-29 09:41:30 +03001525 /* In case there was an error during the command parsing we must
1526 * send a NULL packet to indicate the command is not supported */
Alex Bennéea346af32020-03-16 17:21:34 +00001527 if (process_string_cmd(NULL, data, cmd, 1)) {
1528 put_packet("");
Jon Doron3e2c1262019-05-29 09:41:30 +03001529 }
1530}
1531
1532static void handle_detach(GdbCmdContext *gdb_ctx, void *user_ctx)
1533{
1534 GDBProcess *process;
Jon Doron3e2c1262019-05-29 09:41:30 +03001535 uint32_t pid = 1;
1536
Alex Bennéea346af32020-03-16 17:21:34 +00001537 if (gdbserver_state.multiprocess) {
Jon Doron3e2c1262019-05-29 09:41:30 +03001538 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001539 put_packet("E22");
Jon Doron3e2c1262019-05-29 09:41:30 +03001540 return;
1541 }
1542
1543 pid = gdb_ctx->params[0].val_ul;
1544 }
1545
Alex Bennéea346af32020-03-16 17:21:34 +00001546 process = gdb_get_process(pid);
1547 gdb_process_breakpoint_remove_all(process);
Jon Doron3e2c1262019-05-29 09:41:30 +03001548 process->attached = false;
1549
Alex Bennéea346af32020-03-16 17:21:34 +00001550 if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) {
1551 gdbserver_state.c_cpu = gdb_first_attached_cpu();
Jon Doron3e2c1262019-05-29 09:41:30 +03001552 }
1553
Alex Bennéea346af32020-03-16 17:21:34 +00001554 if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) {
1555 gdbserver_state.g_cpu = gdb_first_attached_cpu();
Jon Doron3e2c1262019-05-29 09:41:30 +03001556 }
1557
Alex Bennéea346af32020-03-16 17:21:34 +00001558 if (!gdbserver_state.c_cpu) {
Jon Doron3e2c1262019-05-29 09:41:30 +03001559 /* No more process attached */
1560 gdb_syscall_mode = GDB_SYS_DISABLED;
Alex Bennéea346af32020-03-16 17:21:34 +00001561 gdb_continue();
Jon Doron3e2c1262019-05-29 09:41:30 +03001562 }
Alex Bennéea346af32020-03-16 17:21:34 +00001563 put_packet("OK");
Jon Doron3e2c1262019-05-29 09:41:30 +03001564}
1565
Jon Doron44ffded2019-05-29 09:41:31 +03001566static void handle_thread_alive(GdbCmdContext *gdb_ctx, void *user_ctx)
1567{
1568 CPUState *cpu;
1569
1570 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001571 put_packet("E22");
Jon Doron44ffded2019-05-29 09:41:31 +03001572 return;
1573 }
1574
1575 if (gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
Alex Bennéea346af32020-03-16 17:21:34 +00001576 put_packet("E22");
Jon Doron44ffded2019-05-29 09:41:31 +03001577 return;
1578 }
1579
Alex Bennéea346af32020-03-16 17:21:34 +00001580 cpu = gdb_get_cpu(gdb_ctx->params[0].thread_id.pid,
Jon Doron44ffded2019-05-29 09:41:31 +03001581 gdb_ctx->params[0].thread_id.tid);
1582 if (!cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +00001583 put_packet("E22");
Jon Doron44ffded2019-05-29 09:41:31 +03001584 return;
1585 }
1586
Alex Bennéea346af32020-03-16 17:21:34 +00001587 put_packet("OK");
Jon Doron44ffded2019-05-29 09:41:31 +03001588}
1589
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001590static void handle_continue(GdbCmdContext *gdb_ctx, void *user_ctx)
1591{
1592 if (gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001593 gdb_set_cpu_pc(gdb_ctx->params[0].val_ull);
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001594 }
1595
Alex Bennéea346af32020-03-16 17:21:34 +00001596 gdbserver_state.signal = 0;
1597 gdb_continue();
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001598}
1599
Jon Doronccc47d52019-05-29 09:41:33 +03001600static void handle_cont_with_sig(GdbCmdContext *gdb_ctx, void *user_ctx)
1601{
1602 unsigned long signal = 0;
1603
1604 /*
1605 * Note: C sig;[addr] is currently unsupported and we simply
1606 * omit the addr parameter
1607 */
1608 if (gdb_ctx->num_params) {
1609 signal = gdb_ctx->params[0].val_ul;
1610 }
1611
Alex Bennéea346af32020-03-16 17:21:34 +00001612 gdbserver_state.signal = gdb_signal_to_target(signal);
1613 if (gdbserver_state.signal == -1) {
1614 gdbserver_state.signal = 0;
Jon Doronccc47d52019-05-29 09:41:33 +03001615 }
Alex Bennéea346af32020-03-16 17:21:34 +00001616 gdb_continue();
Jon Doronccc47d52019-05-29 09:41:33 +03001617}
1618
Jon Doron3a9651d2019-05-29 09:41:34 +03001619static void handle_set_thread(GdbCmdContext *gdb_ctx, void *user_ctx)
1620{
1621 CPUState *cpu;
1622
1623 if (gdb_ctx->num_params != 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001624 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001625 return;
1626 }
1627
1628 if (gdb_ctx->params[1].thread_id.kind == GDB_READ_THREAD_ERR) {
Alex Bennéea346af32020-03-16 17:21:34 +00001629 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001630 return;
1631 }
1632
1633 if (gdb_ctx->params[1].thread_id.kind != GDB_ONE_THREAD) {
Alex Bennéea346af32020-03-16 17:21:34 +00001634 put_packet("OK");
Jon Doron3a9651d2019-05-29 09:41:34 +03001635 return;
1636 }
1637
Alex Bennéea346af32020-03-16 17:21:34 +00001638 cpu = gdb_get_cpu(gdb_ctx->params[1].thread_id.pid,
Jon Doron3a9651d2019-05-29 09:41:34 +03001639 gdb_ctx->params[1].thread_id.tid);
1640 if (!cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +00001641 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001642 return;
1643 }
1644
1645 /*
1646 * Note: This command is deprecated and modern gdb's will be using the
1647 * vCont command instead.
1648 */
1649 switch (gdb_ctx->params[0].opcode) {
1650 case 'c':
Alex Bennéea346af32020-03-16 17:21:34 +00001651 gdbserver_state.c_cpu = cpu;
1652 put_packet("OK");
Jon Doron3a9651d2019-05-29 09:41:34 +03001653 break;
1654 case 'g':
Alex Bennéea346af32020-03-16 17:21:34 +00001655 gdbserver_state.g_cpu = cpu;
1656 put_packet("OK");
Jon Doron3a9651d2019-05-29 09:41:34 +03001657 break;
1658 default:
Alex Bennéea346af32020-03-16 17:21:34 +00001659 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001660 break;
1661 }
1662}
1663
Jon Doron77f6ce52019-05-29 09:41:35 +03001664static void handle_insert_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1665{
1666 int res;
1667
1668 if (gdb_ctx->num_params != 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00001669 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001670 return;
1671 }
1672
1673 res = gdb_breakpoint_insert(gdb_ctx->params[0].val_ul,
1674 gdb_ctx->params[1].val_ull,
1675 gdb_ctx->params[2].val_ull);
1676 if (res >= 0) {
Alex Bennéea346af32020-03-16 17:21:34 +00001677 put_packet("OK");
Jon Doron77f6ce52019-05-29 09:41:35 +03001678 return;
1679 } else if (res == -ENOSYS) {
Alex Bennéea346af32020-03-16 17:21:34 +00001680 put_packet("");
Jon Doron77f6ce52019-05-29 09:41:35 +03001681 return;
1682 }
1683
Alex Bennéea346af32020-03-16 17:21:34 +00001684 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001685}
1686
1687static void handle_remove_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1688{
1689 int res;
1690
1691 if (gdb_ctx->num_params != 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00001692 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001693 return;
1694 }
1695
1696 res = gdb_breakpoint_remove(gdb_ctx->params[0].val_ul,
1697 gdb_ctx->params[1].val_ull,
1698 gdb_ctx->params[2].val_ull);
1699 if (res >= 0) {
Alex Bennéea346af32020-03-16 17:21:34 +00001700 put_packet("OK");
Jon Doron77f6ce52019-05-29 09:41:35 +03001701 return;
1702 } else if (res == -ENOSYS) {
Alex Bennéea346af32020-03-16 17:21:34 +00001703 put_packet("");
Jon Doron77f6ce52019-05-29 09:41:35 +03001704 return;
1705 }
1706
Alex Bennéea346af32020-03-16 17:21:34 +00001707 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001708}
1709
Alex Bennée94b2a622019-07-05 14:23:07 +01001710/*
1711 * handle_set/get_reg
1712 *
1713 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1714 * This works, but can be very slow. Anything new enough to understand
1715 * XML also knows how to use this properly. However to use this we
1716 * need to define a local XML file as well as be talking to a
1717 * reasonably modern gdb. Responding with an empty packet will cause
1718 * the remote gdb to fallback to older methods.
1719 */
1720
Jon Doron62b33202019-05-29 09:41:36 +03001721static void handle_set_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1722{
1723 int reg_size;
1724
1725 if (!gdb_has_xml) {
Alex Bennéea346af32020-03-16 17:21:34 +00001726 put_packet("");
Jon Doron62b33202019-05-29 09:41:36 +03001727 return;
1728 }
1729
1730 if (gdb_ctx->num_params != 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001731 put_packet("E22");
Jon Doron62b33202019-05-29 09:41:36 +03001732 return;
1733 }
1734
1735 reg_size = strlen(gdb_ctx->params[1].data) / 2;
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001736 hextomem(gdbserver_state.mem_buf, gdb_ctx->params[1].data, reg_size);
1737 gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data,
Jon Doron62b33202019-05-29 09:41:36 +03001738 gdb_ctx->params[0].val_ull);
Alex Bennéea346af32020-03-16 17:21:34 +00001739 put_packet("OK");
Jon Doron62b33202019-05-29 09:41:36 +03001740}
1741
Jon Doron5d0e57b2019-05-29 09:41:37 +03001742static void handle_get_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1743{
1744 int reg_size;
1745
Jon Doron5d0e57b2019-05-29 09:41:37 +03001746 if (!gdb_has_xml) {
Alex Bennéea346af32020-03-16 17:21:34 +00001747 put_packet("");
Jon Doron5d0e57b2019-05-29 09:41:37 +03001748 return;
1749 }
1750
1751 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001752 put_packet("E14");
Jon Doron5d0e57b2019-05-29 09:41:37 +03001753 return;
1754 }
1755
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001756 reg_size = gdb_read_register(gdbserver_state.g_cpu,
Alex Bennéea010bdb2020-03-16 17:21:41 +00001757 gdbserver_state.mem_buf,
Jon Doron5d0e57b2019-05-29 09:41:37 +03001758 gdb_ctx->params[0].val_ull);
1759 if (!reg_size) {
Alex Bennéea346af32020-03-16 17:21:34 +00001760 put_packet("E14");
Jon Doron5d0e57b2019-05-29 09:41:37 +03001761 return;
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001762 } else {
1763 g_byte_array_set_size(gdbserver_state.mem_buf, reg_size);
Jon Doron5d0e57b2019-05-29 09:41:37 +03001764 }
1765
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001766 memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, reg_size);
Alex Bennée308f9e82020-03-16 17:21:35 +00001767 put_strbuf();
Jon Doron5d0e57b2019-05-29 09:41:37 +03001768}
1769
Jon Doroncc0ecc72019-05-29 09:41:38 +03001770static void handle_write_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1771{
1772 if (gdb_ctx->num_params != 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00001773 put_packet("E22");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001774 return;
1775 }
1776
1777 /* hextomem() reads 2*len bytes */
1778 if (gdb_ctx->params[1].val_ull > strlen(gdb_ctx->params[2].data) / 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001779 put_packet("E22");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001780 return;
1781 }
1782
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001783 hextomem(gdbserver_state.mem_buf, gdb_ctx->params[2].data,
Jon Doroncc0ecc72019-05-29 09:41:38 +03001784 gdb_ctx->params[1].val_ull);
Alex Bennéea346af32020-03-16 17:21:34 +00001785 if (target_memory_rw_debug(gdbserver_state.g_cpu, gdb_ctx->params[0].val_ull,
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001786 gdbserver_state.mem_buf->data,
1787 gdbserver_state.mem_buf->len, true)) {
Alex Bennéea346af32020-03-16 17:21:34 +00001788 put_packet("E14");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001789 return;
1790 }
1791
Alex Bennéea346af32020-03-16 17:21:34 +00001792 put_packet("OK");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001793}
1794
Jon Doronda92e232019-05-29 09:41:39 +03001795static void handle_read_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1796{
1797 if (gdb_ctx->num_params != 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001798 put_packet("E22");
Jon Doronda92e232019-05-29 09:41:39 +03001799 return;
1800 }
1801
1802 /* memtohex() doubles the required space */
1803 if (gdb_ctx->params[1].val_ull > MAX_PACKET_LENGTH / 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001804 put_packet("E22");
Jon Doronda92e232019-05-29 09:41:39 +03001805 return;
1806 }
1807
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001808 g_byte_array_set_size(gdbserver_state.mem_buf, gdb_ctx->params[1].val_ull);
1809
Alex Bennéea346af32020-03-16 17:21:34 +00001810 if (target_memory_rw_debug(gdbserver_state.g_cpu, gdb_ctx->params[0].val_ull,
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001811 gdbserver_state.mem_buf->data,
1812 gdbserver_state.mem_buf->len, false)) {
Alex Bennéea346af32020-03-16 17:21:34 +00001813 put_packet("E14");
Jon Doronda92e232019-05-29 09:41:39 +03001814 return;
1815 }
1816
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001817 memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data,
1818 gdbserver_state.mem_buf->len);
Alex Bennée308f9e82020-03-16 17:21:35 +00001819 put_strbuf();
Jon Doronda92e232019-05-29 09:41:39 +03001820}
1821
Jon Doron287ca122019-05-29 09:41:40 +03001822static void handle_write_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1823{
1824 target_ulong addr, len;
1825 uint8_t *registers;
1826 int reg_size;
1827
1828 if (!gdb_ctx->num_params) {
1829 return;
1830 }
1831
Alex Bennéea346af32020-03-16 17:21:34 +00001832 cpu_synchronize_state(gdbserver_state.g_cpu);
Jon Doron287ca122019-05-29 09:41:40 +03001833 len = strlen(gdb_ctx->params[0].data) / 2;
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001834 hextomem(gdbserver_state.mem_buf, gdb_ctx->params[0].data, len);
1835 registers = gdbserver_state.mem_buf->data;
Alex Bennéea346af32020-03-16 17:21:34 +00001836 for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0;
Jon Doron287ca122019-05-29 09:41:40 +03001837 addr++) {
Alex Bennéea346af32020-03-16 17:21:34 +00001838 reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, addr);
Jon Doron287ca122019-05-29 09:41:40 +03001839 len -= reg_size;
1840 registers += reg_size;
1841 }
Alex Bennéea346af32020-03-16 17:21:34 +00001842 put_packet("OK");
Jon Doron287ca122019-05-29 09:41:40 +03001843}
1844
Jon Doron397d1372019-05-29 09:41:41 +03001845static void handle_read_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1846{
1847 target_ulong addr, len;
1848
Alex Bennéea346af32020-03-16 17:21:34 +00001849 cpu_synchronize_state(gdbserver_state.g_cpu);
Alex Bennéea010bdb2020-03-16 17:21:41 +00001850 g_byte_array_set_size(gdbserver_state.mem_buf, 0);
Jon Doron397d1372019-05-29 09:41:41 +03001851 len = 0;
Alex Bennéea346af32020-03-16 17:21:34 +00001852 for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs; addr++) {
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001853 len += gdb_read_register(gdbserver_state.g_cpu,
Alex Bennéea010bdb2020-03-16 17:21:41 +00001854 gdbserver_state.mem_buf,
Jon Doron397d1372019-05-29 09:41:41 +03001855 addr);
1856 }
Alex Bennéea010bdb2020-03-16 17:21:41 +00001857 g_assert(len == gdbserver_state.mem_buf->len);
Jon Doron397d1372019-05-29 09:41:41 +03001858
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001859 memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len);
Alex Bennée308f9e82020-03-16 17:21:35 +00001860 put_strbuf();
Jon Doron397d1372019-05-29 09:41:41 +03001861}
1862
Jon Doron4b20fab2019-05-29 09:41:42 +03001863static void handle_file_io(GdbCmdContext *gdb_ctx, void *user_ctx)
1864{
Alex Bennéea346af32020-03-16 17:21:34 +00001865 if (gdb_ctx->num_params >= 1 && gdbserver_state.current_syscall_cb) {
Jon Doron4b20fab2019-05-29 09:41:42 +03001866 target_ulong ret, err;
1867
1868 ret = (target_ulong)gdb_ctx->params[0].val_ull;
Sandra Loosemorec6ee9522019-08-27 16:33:17 -06001869 if (gdb_ctx->num_params >= 2) {
1870 err = (target_ulong)gdb_ctx->params[1].val_ull;
1871 } else {
1872 err = 0;
1873 }
Alex Bennéea346af32020-03-16 17:21:34 +00001874 gdbserver_state.current_syscall_cb(gdbserver_state.c_cpu, ret, err);
1875 gdbserver_state.current_syscall_cb = NULL;
Jon Doron4b20fab2019-05-29 09:41:42 +03001876 }
1877
1878 if (gdb_ctx->num_params >= 3 && gdb_ctx->params[2].opcode == (uint8_t)'C') {
Alex Bennéea346af32020-03-16 17:21:34 +00001879 put_packet("T02");
Jon Doron4b20fab2019-05-29 09:41:42 +03001880 return;
1881 }
1882
Alex Bennéea346af32020-03-16 17:21:34 +00001883 gdb_continue();
Jon Doron4b20fab2019-05-29 09:41:42 +03001884}
1885
Jon Doron933f80d2019-05-29 09:41:43 +03001886static void handle_step(GdbCmdContext *gdb_ctx, void *user_ctx)
1887{
1888 if (gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001889 gdb_set_cpu_pc((target_ulong)gdb_ctx->params[0].val_ull);
Jon Doron933f80d2019-05-29 09:41:43 +03001890 }
1891
Pavel Dovgalyukfda84582020-10-03 20:13:43 +03001892 cpu_single_step(gdbserver_state.c_cpu, get_sstep_flags());
Alex Bennéea346af32020-03-16 17:21:34 +00001893 gdb_continue();
Jon Doron933f80d2019-05-29 09:41:43 +03001894}
1895
Pavel Dovgalyukfda84582020-10-03 20:13:43 +03001896static void handle_backward(GdbCmdContext *gdb_ctx, void *user_ctx)
1897{
1898 if (replay_mode != REPLAY_MODE_PLAY) {
1899 put_packet("E22");
1900 }
1901 if (gdb_ctx->num_params == 1) {
1902 switch (gdb_ctx->params[0].opcode) {
1903 case 's':
1904 if (replay_reverse_step()) {
1905 gdb_continue();
1906 } else {
1907 put_packet("E14");
1908 }
1909 return;
1910 }
1911 }
1912
1913 /* Default invalid command */
1914 put_packet("");
1915}
1916
Jon Doron8536ec02019-05-29 09:41:44 +03001917static void handle_v_cont_query(GdbCmdContext *gdb_ctx, void *user_ctx)
1918{
Alex Bennéea346af32020-03-16 17:21:34 +00001919 put_packet("vCont;c;C;s;S");
Jon Doron8536ec02019-05-29 09:41:44 +03001920}
1921
1922static void handle_v_cont(GdbCmdContext *gdb_ctx, void *user_ctx)
1923{
1924 int res;
1925
1926 if (!gdb_ctx->num_params) {
1927 return;
1928 }
1929
Alex Bennéea346af32020-03-16 17:21:34 +00001930 res = gdb_handle_vcont(gdb_ctx->params[0].data);
Jon Doron8536ec02019-05-29 09:41:44 +03001931 if ((res == -EINVAL) || (res == -ERANGE)) {
Alex Bennéea346af32020-03-16 17:21:34 +00001932 put_packet("E22");
Jon Doron8536ec02019-05-29 09:41:44 +03001933 } else if (res) {
Alex Bennéea346af32020-03-16 17:21:34 +00001934 put_packet("");
Jon Doron8536ec02019-05-29 09:41:44 +03001935 }
1936}
1937
1938static void handle_v_attach(GdbCmdContext *gdb_ctx, void *user_ctx)
1939{
1940 GDBProcess *process;
1941 CPUState *cpu;
Jon Doron8536ec02019-05-29 09:41:44 +03001942
Alex Bennée308f9e82020-03-16 17:21:35 +00001943 g_string_assign(gdbserver_state.str_buf, "E22");
Jon Doron8536ec02019-05-29 09:41:44 +03001944 if (!gdb_ctx->num_params) {
1945 goto cleanup;
1946 }
1947
Alex Bennéea346af32020-03-16 17:21:34 +00001948 process = gdb_get_process(gdb_ctx->params[0].val_ul);
Jon Doron8536ec02019-05-29 09:41:44 +03001949 if (!process) {
1950 goto cleanup;
1951 }
1952
Alex Bennéea346af32020-03-16 17:21:34 +00001953 cpu = get_first_cpu_in_process(process);
Jon Doron8536ec02019-05-29 09:41:44 +03001954 if (!cpu) {
1955 goto cleanup;
1956 }
1957
1958 process->attached = true;
Alex Bennéea346af32020-03-16 17:21:34 +00001959 gdbserver_state.g_cpu = cpu;
1960 gdbserver_state.c_cpu = cpu;
Jon Doron8536ec02019-05-29 09:41:44 +03001961
Alex Bennée308f9e82020-03-16 17:21:35 +00001962 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
1963 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
1964 g_string_append_c(gdbserver_state.str_buf, ';');
Jon Doron8536ec02019-05-29 09:41:44 +03001965cleanup:
Alex Bennée308f9e82020-03-16 17:21:35 +00001966 put_strbuf();
Jon Doron8536ec02019-05-29 09:41:44 +03001967}
1968
1969static void handle_v_kill(GdbCmdContext *gdb_ctx, void *user_ctx)
1970{
1971 /* Kill the target */
Alex Bennéea346af32020-03-16 17:21:34 +00001972 put_packet("OK");
Jon Doron8536ec02019-05-29 09:41:44 +03001973 error_report("QEMU: Terminated via GDBstub");
1974 exit(0);
1975}
1976
1977static GdbCmdParseEntry gdb_v_commands_table[] = {
1978 /* Order is important if has same prefix */
1979 {
1980 .handler = handle_v_cont_query,
1981 .cmd = "Cont?",
1982 .cmd_startswith = 1
1983 },
1984 {
1985 .handler = handle_v_cont,
1986 .cmd = "Cont",
1987 .cmd_startswith = 1,
1988 .schema = "s0"
1989 },
1990 {
1991 .handler = handle_v_attach,
1992 .cmd = "Attach;",
1993 .cmd_startswith = 1,
1994 .schema = "l0"
1995 },
1996 {
1997 .handler = handle_v_kill,
1998 .cmd = "Kill;",
1999 .cmd_startswith = 1
2000 },
2001};
2002
2003static void handle_v_commands(GdbCmdContext *gdb_ctx, void *user_ctx)
2004{
2005 if (!gdb_ctx->num_params) {
2006 return;
2007 }
2008
Alex Bennéea346af32020-03-16 17:21:34 +00002009 if (process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron8536ec02019-05-29 09:41:44 +03002010 gdb_v_commands_table,
2011 ARRAY_SIZE(gdb_v_commands_table))) {
Alex Bennéea346af32020-03-16 17:21:34 +00002012 put_packet("");
Jon Doron8536ec02019-05-29 09:41:44 +03002013 }
2014}
2015
Jon Doron2704efa2019-05-29 09:41:45 +03002016static void handle_query_qemu_sstepbits(GdbCmdContext *gdb_ctx, void *user_ctx)
2017{
Alex Bennée308f9e82020-03-16 17:21:35 +00002018 g_string_printf(gdbserver_state.str_buf, "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
2019 SSTEP_ENABLE, SSTEP_NOIRQ, SSTEP_NOTIMER);
2020 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002021}
2022
2023static void handle_set_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
2024{
2025 if (!gdb_ctx->num_params) {
2026 return;
2027 }
2028
2029 sstep_flags = gdb_ctx->params[0].val_ul;
Alex Bennéea346af32020-03-16 17:21:34 +00002030 put_packet("OK");
Jon Doron2704efa2019-05-29 09:41:45 +03002031}
2032
2033static void handle_query_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
2034{
Alex Bennée308f9e82020-03-16 17:21:35 +00002035 g_string_printf(gdbserver_state.str_buf, "0x%x", sstep_flags);
2036 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002037}
2038
2039static void handle_query_curr_tid(GdbCmdContext *gdb_ctx, void *user_ctx)
bellardb4608c02003-06-27 17:34:32 +00002040{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002041 CPUState *cpu;
Luc Michelc145eea2019-01-07 15:23:46 +00002042 GDBProcess *process;
Jon Doron2704efa2019-05-29 09:41:45 +03002043
2044 /*
2045 * "Current thread" remains vague in the spec, so always return
2046 * the first thread of the current process (gdb returns the
2047 * first thread).
2048 */
Alex Bennéea346af32020-03-16 17:21:34 +00002049 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
2050 cpu = get_first_cpu_in_process(process);
Alex Bennée308f9e82020-03-16 17:21:35 +00002051 g_string_assign(gdbserver_state.str_buf, "QC");
2052 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
2053 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002054}
2055
2056static void handle_query_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
2057{
Alex Bennéea346af32020-03-16 17:21:34 +00002058 if (!gdbserver_state.query_cpu) {
2059 put_packet("l");
Jon Doron2704efa2019-05-29 09:41:45 +03002060 return;
2061 }
2062
Alex Bennée308f9e82020-03-16 17:21:35 +00002063 g_string_assign(gdbserver_state.str_buf, "m");
2064 gdb_append_thread_id(gdbserver_state.query_cpu, gdbserver_state.str_buf);
2065 put_strbuf();
Alex Bennéea346af32020-03-16 17:21:34 +00002066 gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu);
Jon Doron2704efa2019-05-29 09:41:45 +03002067}
2068
2069static void handle_query_first_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
2070{
Alex Bennéea346af32020-03-16 17:21:34 +00002071 gdbserver_state.query_cpu = gdb_first_attached_cpu();
Jon Doron2704efa2019-05-29 09:41:45 +03002072 handle_query_threads(gdb_ctx, user_ctx);
2073}
2074
2075static void handle_query_thread_extra(GdbCmdContext *gdb_ctx, void *user_ctx)
2076{
Alex Bennée308f9e82020-03-16 17:21:35 +00002077 g_autoptr(GString) rs = g_string_new(NULL);
Jon Doron2704efa2019-05-29 09:41:45 +03002078 CPUState *cpu;
Jon Doron2704efa2019-05-29 09:41:45 +03002079
2080 if (!gdb_ctx->num_params ||
2081 gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
Alex Bennéea346af32020-03-16 17:21:34 +00002082 put_packet("E22");
Jon Doron2704efa2019-05-29 09:41:45 +03002083 return;
2084 }
2085
Alex Bennéea346af32020-03-16 17:21:34 +00002086 cpu = gdb_get_cpu(gdb_ctx->params[0].thread_id.pid,
Jon Doron2704efa2019-05-29 09:41:45 +03002087 gdb_ctx->params[0].thread_id.tid);
2088 if (!cpu) {
2089 return;
2090 }
2091
2092 cpu_synchronize_state(cpu);
2093
Alex Bennéea346af32020-03-16 17:21:34 +00002094 if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) {
Jon Doron2704efa2019-05-29 09:41:45 +03002095 /* Print the CPU model and name in multiprocess mode */
2096 ObjectClass *oc = object_get_class(OBJECT(cpu));
2097 const char *cpu_model = object_class_get_name(oc);
Markus Armbruster7a309cc2020-07-14 18:02:00 +02002098 const char *cpu_name =
Denis Plotnikov076b2fa2020-04-03 20:11:44 +01002099 object_get_canonical_path_component(OBJECT(cpu));
Alex Bennée308f9e82020-03-16 17:21:35 +00002100 g_string_printf(rs, "%s %s [%s]", cpu_model, cpu_name,
2101 cpu->halted ? "halted " : "running");
Jon Doron2704efa2019-05-29 09:41:45 +03002102 } else {
Alex Bennée308f9e82020-03-16 17:21:35 +00002103 g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index,
Jon Doron2704efa2019-05-29 09:41:45 +03002104 cpu->halted ? "halted " : "running");
2105 }
Alex Bennée308f9e82020-03-16 17:21:35 +00002106 trace_gdbstub_op_extra_info(rs->str);
2107 memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len);
2108 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002109}
2110
2111#ifdef CONFIG_USER_ONLY
2112static void handle_query_offsets(GdbCmdContext *gdb_ctx, void *user_ctx)
2113{
2114 TaskState *ts;
2115
Alex Bennéea346af32020-03-16 17:21:34 +00002116 ts = gdbserver_state.c_cpu->opaque;
Alex Bennée308f9e82020-03-16 17:21:35 +00002117 g_string_printf(gdbserver_state.str_buf,
2118 "Text=" TARGET_ABI_FMT_lx
2119 ";Data=" TARGET_ABI_FMT_lx
2120 ";Bss=" TARGET_ABI_FMT_lx,
2121 ts->info->code_offset,
2122 ts->info->data_offset,
2123 ts->info->data_offset);
2124 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002125}
2126#else
2127static void handle_query_rcmd(GdbCmdContext *gdb_ctx, void *user_ctx)
2128{
Alex Bennée4a25f1b2020-03-16 17:21:36 +00002129 const guint8 zero = 0;
Jon Doron2704efa2019-05-29 09:41:45 +03002130 int len;
2131
2132 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00002133 put_packet("E22");
Jon Doron2704efa2019-05-29 09:41:45 +03002134 return;
2135 }
2136
2137 len = strlen(gdb_ctx->params[0].data);
2138 if (len % 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00002139 put_packet("E01");
Jon Doron2704efa2019-05-29 09:41:45 +03002140 return;
2141 }
2142
Alex Bennée4a25f1b2020-03-16 17:21:36 +00002143 g_assert(gdbserver_state.mem_buf->len == 0);
Jon Doron2704efa2019-05-29 09:41:45 +03002144 len = len / 2;
Alex Bennée4a25f1b2020-03-16 17:21:36 +00002145 hextomem(gdbserver_state.mem_buf, gdb_ctx->params[0].data, len);
2146 g_byte_array_append(gdbserver_state.mem_buf, &zero, 1);
2147 qemu_chr_be_write(gdbserver_state.mon_chr, gdbserver_state.mem_buf->data,
2148 gdbserver_state.mem_buf->len);
Alex Bennéea346af32020-03-16 17:21:34 +00002149 put_packet("OK");
Jon Doron2704efa2019-05-29 09:41:45 +03002150}
2151#endif
2152
2153static void handle_query_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2154{
Andreas Färber5b24c642013-07-07 15:08:22 +02002155 CPUClass *cc;
Jon Doron2704efa2019-05-29 09:41:45 +03002156
Alex Bennée308f9e82020-03-16 17:21:35 +00002157 g_string_printf(gdbserver_state.str_buf, "PacketSize=%x", MAX_PACKET_LENGTH);
Jon Doron2704efa2019-05-29 09:41:45 +03002158 cc = CPU_GET_CLASS(first_cpu);
2159 if (cc->gdb_core_xml_file) {
Alex Bennée308f9e82020-03-16 17:21:35 +00002160 g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+");
Jon Doron2704efa2019-05-29 09:41:45 +03002161 }
2162
Pavel Dovgalyukfda84582020-10-03 20:13:43 +03002163 if (replay_mode == REPLAY_MODE_PLAY) {
2164 g_string_append(gdbserver_state.str_buf, ";ReverseStep+");
2165 }
2166
Jon Doron2704efa2019-05-29 09:41:45 +03002167 if (gdb_ctx->num_params &&
2168 strstr(gdb_ctx->params[0].data, "multiprocess+")) {
Alex Bennéea346af32020-03-16 17:21:34 +00002169 gdbserver_state.multiprocess = true;
Jon Doron2704efa2019-05-29 09:41:45 +03002170 }
2171
Changbin Du3bc26092020-03-16 17:21:55 +00002172 g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+");
Alex Bennée308f9e82020-03-16 17:21:35 +00002173 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002174}
2175
2176static void handle_query_xfer_features(GdbCmdContext *gdb_ctx, void *user_ctx)
2177{
2178 GDBProcess *process;
2179 CPUClass *cc;
2180 unsigned long len, total_len, addr;
2181 const char *xml;
bellardb4608c02003-06-27 17:34:32 +00002182 const char *p;
Jon Doron2704efa2019-05-29 09:41:45 +03002183
2184 if (gdb_ctx->num_params < 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00002185 put_packet("E22");
Jon Doron2704efa2019-05-29 09:41:45 +03002186 return;
2187 }
2188
Alex Bennéea346af32020-03-16 17:21:34 +00002189 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
2190 cc = CPU_GET_CLASS(gdbserver_state.g_cpu);
Jon Doron2704efa2019-05-29 09:41:45 +03002191 if (!cc->gdb_core_xml_file) {
Alex Bennéea346af32020-03-16 17:21:34 +00002192 put_packet("");
Jon Doron2704efa2019-05-29 09:41:45 +03002193 return;
2194 }
2195
2196 gdb_has_xml = true;
2197 p = gdb_ctx->params[0].data;
Alex Bennéea346af32020-03-16 17:21:34 +00002198 xml = get_feature_xml(p, &p, process);
Jon Doron2704efa2019-05-29 09:41:45 +03002199 if (!xml) {
Alex Bennéea346af32020-03-16 17:21:34 +00002200 put_packet("E00");
Jon Doron2704efa2019-05-29 09:41:45 +03002201 return;
2202 }
2203
2204 addr = gdb_ctx->params[1].val_ul;
2205 len = gdb_ctx->params[2].val_ul;
2206 total_len = strlen(xml);
2207 if (addr > total_len) {
Alex Bennéea346af32020-03-16 17:21:34 +00002208 put_packet("E00");
Jon Doron2704efa2019-05-29 09:41:45 +03002209 return;
2210 }
2211
2212 if (len > (MAX_PACKET_LENGTH - 5) / 2) {
2213 len = (MAX_PACKET_LENGTH - 5) / 2;
2214 }
2215
2216 if (len < total_len - addr) {
Alex Bennée308f9e82020-03-16 17:21:35 +00002217 g_string_assign(gdbserver_state.str_buf, "m");
2218 memtox(gdbserver_state.str_buf, xml + addr, len);
Jon Doron2704efa2019-05-29 09:41:45 +03002219 } else {
Alex Bennée308f9e82020-03-16 17:21:35 +00002220 g_string_assign(gdbserver_state.str_buf, "l");
2221 memtox(gdbserver_state.str_buf, xml + addr, total_len - addr);
Jon Doron2704efa2019-05-29 09:41:45 +03002222 }
2223
Alex Bennée308f9e82020-03-16 17:21:35 +00002224 put_packet_binary(gdbserver_state.str_buf->str,
2225 gdbserver_state.str_buf->len, true);
Jon Doron2704efa2019-05-29 09:41:45 +03002226}
2227
2228static void handle_query_attached(GdbCmdContext *gdb_ctx, void *user_ctx)
2229{
Alex Bennéea346af32020-03-16 17:21:34 +00002230 put_packet(GDB_ATTACHED);
Jon Doron2704efa2019-05-29 09:41:45 +03002231}
2232
2233static void handle_query_qemu_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2234{
Alex Bennée308f9e82020-03-16 17:21:35 +00002235 g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep");
Jon Doronab4752e2019-05-29 09:41:48 +03002236#ifndef CONFIG_USER_ONLY
Alex Bennée308f9e82020-03-16 17:21:35 +00002237 g_string_append(gdbserver_state.str_buf, ";PhyMemMode");
Jon Doronab4752e2019-05-29 09:41:48 +03002238#endif
Alex Bennée308f9e82020-03-16 17:21:35 +00002239 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002240}
2241
Jon Doronab4752e2019-05-29 09:41:48 +03002242#ifndef CONFIG_USER_ONLY
2243static void handle_query_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx,
2244 void *user_ctx)
2245{
Alex Bennée308f9e82020-03-16 17:21:35 +00002246 g_string_printf(gdbserver_state.str_buf, "%d", phy_memory_mode);
2247 put_strbuf();
Jon Doronab4752e2019-05-29 09:41:48 +03002248}
2249
2250static void handle_set_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx, void *user_ctx)
2251{
2252 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00002253 put_packet("E22");
Jon Doronab4752e2019-05-29 09:41:48 +03002254 return;
2255 }
2256
2257 if (!gdb_ctx->params[0].val_ul) {
2258 phy_memory_mode = 0;
2259 } else {
2260 phy_memory_mode = 1;
2261 }
Alex Bennéea346af32020-03-16 17:21:34 +00002262 put_packet("OK");
Jon Doronab4752e2019-05-29 09:41:48 +03002263}
2264#endif
2265
Jon Doron2704efa2019-05-29 09:41:45 +03002266static GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
2267 /* Order is important if has same prefix */
2268 {
2269 .handler = handle_query_qemu_sstepbits,
2270 .cmd = "qemu.sstepbits",
2271 },
2272 {
2273 .handler = handle_query_qemu_sstep,
2274 .cmd = "qemu.sstep",
2275 },
2276 {
2277 .handler = handle_set_qemu_sstep,
2278 .cmd = "qemu.sstep=",
2279 .cmd_startswith = 1,
2280 .schema = "l0"
2281 },
2282};
2283
2284static GdbCmdParseEntry gdb_gen_query_table[] = {
2285 {
2286 .handler = handle_query_curr_tid,
2287 .cmd = "C",
2288 },
2289 {
2290 .handler = handle_query_threads,
2291 .cmd = "sThreadInfo",
2292 },
2293 {
2294 .handler = handle_query_first_threads,
2295 .cmd = "fThreadInfo",
2296 },
2297 {
2298 .handler = handle_query_thread_extra,
2299 .cmd = "ThreadExtraInfo,",
2300 .cmd_startswith = 1,
2301 .schema = "t0"
2302 },
2303#ifdef CONFIG_USER_ONLY
2304 {
2305 .handler = handle_query_offsets,
2306 .cmd = "Offsets",
2307 },
2308#else
2309 {
2310 .handler = handle_query_rcmd,
2311 .cmd = "Rcmd,",
2312 .cmd_startswith = 1,
2313 .schema = "s0"
2314 },
2315#endif
2316 {
2317 .handler = handle_query_supported,
2318 .cmd = "Supported:",
2319 .cmd_startswith = 1,
2320 .schema = "s0"
2321 },
2322 {
2323 .handler = handle_query_supported,
2324 .cmd = "Supported",
2325 .schema = "s0"
2326 },
2327 {
2328 .handler = handle_query_xfer_features,
2329 .cmd = "Xfer:features:read:",
2330 .cmd_startswith = 1,
2331 .schema = "s:l,l0"
2332 },
2333 {
2334 .handler = handle_query_attached,
2335 .cmd = "Attached:",
2336 .cmd_startswith = 1
2337 },
2338 {
2339 .handler = handle_query_attached,
2340 .cmd = "Attached",
2341 },
2342 {
2343 .handler = handle_query_qemu_supported,
2344 .cmd = "qemu.Supported",
2345 },
Jon Doronab4752e2019-05-29 09:41:48 +03002346#ifndef CONFIG_USER_ONLY
2347 {
2348 .handler = handle_query_qemu_phy_mem_mode,
2349 .cmd = "qemu.PhyMemMode",
2350 },
2351#endif
Jon Doron2704efa2019-05-29 09:41:45 +03002352};
2353
2354static GdbCmdParseEntry gdb_gen_set_table[] = {
2355 /* Order is important if has same prefix */
2356 {
2357 .handler = handle_set_qemu_sstep,
2358 .cmd = "qemu.sstep:",
2359 .cmd_startswith = 1,
2360 .schema = "l0"
2361 },
Jon Doronab4752e2019-05-29 09:41:48 +03002362#ifndef CONFIG_USER_ONLY
2363 {
2364 .handler = handle_set_qemu_phy_mem_mode,
2365 .cmd = "qemu.PhyMemMode:",
2366 .cmd_startswith = 1,
2367 .schema = "l0"
2368 },
2369#endif
Jon Doron2704efa2019-05-29 09:41:45 +03002370};
2371
2372static void handle_gen_query(GdbCmdContext *gdb_ctx, void *user_ctx)
2373{
2374 if (!gdb_ctx->num_params) {
2375 return;
2376 }
2377
Alex Bennéea346af32020-03-16 17:21:34 +00002378 if (!process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron2704efa2019-05-29 09:41:45 +03002379 gdb_gen_query_set_common_table,
2380 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2381 return;
2382 }
2383
Alex Bennéea346af32020-03-16 17:21:34 +00002384 if (process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron2704efa2019-05-29 09:41:45 +03002385 gdb_gen_query_table,
2386 ARRAY_SIZE(gdb_gen_query_table))) {
Alex Bennéea346af32020-03-16 17:21:34 +00002387 put_packet("");
Jon Doron2704efa2019-05-29 09:41:45 +03002388 }
2389}
2390
2391static void handle_gen_set(GdbCmdContext *gdb_ctx, void *user_ctx)
2392{
2393 if (!gdb_ctx->num_params) {
2394 return;
2395 }
2396
Alex Bennéea346af32020-03-16 17:21:34 +00002397 if (!process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron2704efa2019-05-29 09:41:45 +03002398 gdb_gen_query_set_common_table,
2399 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2400 return;
2401 }
2402
Alex Bennéea346af32020-03-16 17:21:34 +00002403 if (process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron2704efa2019-05-29 09:41:45 +03002404 gdb_gen_set_table,
2405 ARRAY_SIZE(gdb_gen_set_table))) {
Alex Bennéea346af32020-03-16 17:21:34 +00002406 put_packet("");
Jon Doron2704efa2019-05-29 09:41:45 +03002407 }
2408}
2409
Jon Doron7009d572019-05-29 09:41:46 +03002410static void handle_target_halt(GdbCmdContext *gdb_ctx, void *user_ctx)
2411{
Alex Bennée308f9e82020-03-16 17:21:35 +00002412 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
2413 gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf);
2414 g_string_append_c(gdbserver_state.str_buf, ';');
2415 put_strbuf();
Jon Doron7009d572019-05-29 09:41:46 +03002416 /*
2417 * Remove all the breakpoints when this query is issued,
2418 * because gdb is doing an initial connect and the state
2419 * should be cleaned up.
2420 */
2421 gdb_breakpoint_remove_all();
2422}
2423
Alex Bennéea346af32020-03-16 17:21:34 +00002424static int gdb_handle_packet(const char *line_buf)
Jon Doron2704efa2019-05-29 09:41:45 +03002425{
Jon Doron3e2c1262019-05-29 09:41:30 +03002426 const GdbCmdParseEntry *cmd_parser = NULL;
ths3b46e622007-09-17 08:09:54 +00002427
Doug Gale5c9522b2017-12-02 20:30:37 -05002428 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01002429
Jon Doron3f1cbac2019-05-29 09:41:47 +03002430 switch (line_buf[0]) {
Luc Michel53fd6552019-01-07 15:23:46 +00002431 case '!':
Alex Bennéea346af32020-03-16 17:21:34 +00002432 put_packet("OK");
Luc Michel53fd6552019-01-07 15:23:46 +00002433 break;
bellard858693c2004-03-31 18:52:07 +00002434 case '?':
Jon Doron7009d572019-05-29 09:41:46 +03002435 {
2436 static const GdbCmdParseEntry target_halted_cmd_desc = {
2437 .handler = handle_target_halt,
2438 .cmd = "?",
2439 .cmd_startswith = 1
2440 };
2441 cmd_parser = &target_halted_cmd_desc;
2442 }
bellard858693c2004-03-31 18:52:07 +00002443 break;
2444 case 'c':
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002445 {
2446 static const GdbCmdParseEntry continue_cmd_desc = {
2447 .handler = handle_continue,
2448 .cmd = "c",
2449 .cmd_startswith = 1,
2450 .schema = "L0"
2451 };
2452 cmd_parser = &continue_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002453 }
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002454 break;
edgar_igl1f487ee2008-05-17 22:20:53 +00002455 case 'C':
Jon Doronccc47d52019-05-29 09:41:33 +03002456 {
2457 static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
2458 .handler = handle_cont_with_sig,
2459 .cmd = "C",
2460 .cmd_startswith = 1,
2461 .schema = "l0"
2462 };
2463 cmd_parser = &cont_with_sig_cmd_desc;
2464 }
2465 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002466 case 'v':
Jon Doron8536ec02019-05-29 09:41:44 +03002467 {
2468 static const GdbCmdParseEntry v_cmd_desc = {
2469 .handler = handle_v_commands,
2470 .cmd = "v",
2471 .cmd_startswith = 1,
2472 .schema = "s0"
2473 };
2474 cmd_parser = &v_cmd_desc;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002475 }
Jon Doron8536ec02019-05-29 09:41:44 +03002476 break;
edgar_igl7d03f822008-05-17 18:58:29 +00002477 case 'k':
2478 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002479 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00002480 exit(0);
2481 case 'D':
Jon Doron3e2c1262019-05-29 09:41:30 +03002482 {
2483 static const GdbCmdParseEntry detach_cmd_desc = {
2484 .handler = handle_detach,
2485 .cmd = "D",
2486 .cmd_startswith = 1,
2487 .schema = "?.l0"
2488 };
2489 cmd_parser = &detach_cmd_desc;
Luc Michel546f3c62019-01-07 15:23:46 +00002490 }
edgar_igl7d03f822008-05-17 18:58:29 +00002491 break;
bellard858693c2004-03-31 18:52:07 +00002492 case 's':
Jon Doron933f80d2019-05-29 09:41:43 +03002493 {
2494 static const GdbCmdParseEntry step_cmd_desc = {
2495 .handler = handle_step,
2496 .cmd = "s",
2497 .cmd_startswith = 1,
2498 .schema = "L0"
2499 };
2500 cmd_parser = &step_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002501 }
Jon Doron933f80d2019-05-29 09:41:43 +03002502 break;
Pavel Dovgalyukfda84582020-10-03 20:13:43 +03002503 case 'b':
2504 {
2505 static const GdbCmdParseEntry backward_cmd_desc = {
2506 .handler = handle_backward,
2507 .cmd = "b",
2508 .cmd_startswith = 1,
2509 .schema = "o0"
2510 };
2511 cmd_parser = &backward_cmd_desc;
2512 }
2513 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002514 case 'F':
2515 {
Jon Doron4b20fab2019-05-29 09:41:42 +03002516 static const GdbCmdParseEntry file_io_cmd_desc = {
2517 .handler = handle_file_io,
2518 .cmd = "F",
2519 .cmd_startswith = 1,
2520 .schema = "L,L,o0"
2521 };
2522 cmd_parser = &file_io_cmd_desc;
pbrooka2d1eba2007-01-28 03:10:55 +00002523 }
2524 break;
bellard858693c2004-03-31 18:52:07 +00002525 case 'g':
Jon Doron397d1372019-05-29 09:41:41 +03002526 {
2527 static const GdbCmdParseEntry read_all_regs_cmd_desc = {
2528 .handler = handle_read_all_regs,
2529 .cmd = "g",
2530 .cmd_startswith = 1
2531 };
2532 cmd_parser = &read_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002533 }
bellard858693c2004-03-31 18:52:07 +00002534 break;
2535 case 'G':
Jon Doron287ca122019-05-29 09:41:40 +03002536 {
2537 static const GdbCmdParseEntry write_all_regs_cmd_desc = {
2538 .handler = handle_write_all_regs,
2539 .cmd = "G",
2540 .cmd_startswith = 1,
2541 .schema = "s0"
2542 };
2543 cmd_parser = &write_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002544 }
bellard858693c2004-03-31 18:52:07 +00002545 break;
2546 case 'm':
Jon Doronda92e232019-05-29 09:41:39 +03002547 {
2548 static const GdbCmdParseEntry read_mem_cmd_desc = {
2549 .handler = handle_read_mem,
2550 .cmd = "m",
2551 .cmd_startswith = 1,
2552 .schema = "L,L0"
2553 };
2554 cmd_parser = &read_mem_cmd_desc;
bellard6f970bd2005-12-05 19:55:19 +00002555 }
bellard858693c2004-03-31 18:52:07 +00002556 break;
2557 case 'M':
Jon Doroncc0ecc72019-05-29 09:41:38 +03002558 {
2559 static const GdbCmdParseEntry write_mem_cmd_desc = {
2560 .handler = handle_write_mem,
2561 .cmd = "M",
2562 .cmd_startswith = 1,
2563 .schema = "L,L:s0"
2564 };
2565 cmd_parser = &write_mem_cmd_desc;
Fabien Chouteau44520db2011-09-08 12:48:16 +02002566 }
bellard858693c2004-03-31 18:52:07 +00002567 break;
pbrook56aebc82008-10-11 17:55:29 +00002568 case 'p':
Jon Doron5d0e57b2019-05-29 09:41:37 +03002569 {
2570 static const GdbCmdParseEntry get_reg_cmd_desc = {
2571 .handler = handle_get_reg,
2572 .cmd = "p",
2573 .cmd_startswith = 1,
2574 .schema = "L0"
2575 };
2576 cmd_parser = &get_reg_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002577 }
2578 break;
2579 case 'P':
Jon Doron62b33202019-05-29 09:41:36 +03002580 {
2581 static const GdbCmdParseEntry set_reg_cmd_desc = {
2582 .handler = handle_set_reg,
2583 .cmd = "P",
2584 .cmd_startswith = 1,
2585 .schema = "L?s0"
2586 };
2587 cmd_parser = &set_reg_cmd_desc;
2588 }
pbrook56aebc82008-10-11 17:55:29 +00002589 break;
bellard858693c2004-03-31 18:52:07 +00002590 case 'Z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002591 {
2592 static const GdbCmdParseEntry insert_bp_cmd_desc = {
2593 .handler = handle_insert_bp,
2594 .cmd = "Z",
2595 .cmd_startswith = 1,
2596 .schema = "l?L?L0"
2597 };
2598 cmd_parser = &insert_bp_cmd_desc;
2599 }
2600 break;
bellard858693c2004-03-31 18:52:07 +00002601 case 'z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002602 {
2603 static const GdbCmdParseEntry remove_bp_cmd_desc = {
2604 .handler = handle_remove_bp,
2605 .cmd = "z",
2606 .cmd_startswith = 1,
2607 .schema = "l?L?L0"
2608 };
2609 cmd_parser = &remove_bp_cmd_desc;
2610 }
bellard858693c2004-03-31 18:52:07 +00002611 break;
aliguori880a7572008-11-18 20:30:24 +00002612 case 'H':
Jon Doron3a9651d2019-05-29 09:41:34 +03002613 {
2614 static const GdbCmdParseEntry set_thread_cmd_desc = {
2615 .handler = handle_set_thread,
2616 .cmd = "H",
2617 .cmd_startswith = 1,
2618 .schema = "o.t0"
2619 };
2620 cmd_parser = &set_thread_cmd_desc;
aliguori880a7572008-11-18 20:30:24 +00002621 }
2622 break;
2623 case 'T':
Jon Doron44ffded2019-05-29 09:41:31 +03002624 {
2625 static const GdbCmdParseEntry thread_alive_cmd_desc = {
2626 .handler = handle_thread_alive,
2627 .cmd = "T",
2628 .cmd_startswith = 1,
2629 .schema = "t0"
2630 };
2631 cmd_parser = &thread_alive_cmd_desc;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07002632 }
aliguori880a7572008-11-18 20:30:24 +00002633 break;
pbrook978efd62006-06-17 18:30:42 +00002634 case 'q':
Jon Doron2704efa2019-05-29 09:41:45 +03002635 {
2636 static const GdbCmdParseEntry gen_query_cmd_desc = {
2637 .handler = handle_gen_query,
2638 .cmd = "q",
2639 .cmd_startswith = 1,
2640 .schema = "s0"
2641 };
2642 cmd_parser = &gen_query_cmd_desc;
2643 }
2644 break;
edgar_igl60897d32008-05-09 08:25:14 +00002645 case 'Q':
Jon Doron2704efa2019-05-29 09:41:45 +03002646 {
2647 static const GdbCmdParseEntry gen_set_cmd_desc = {
2648 .handler = handle_gen_set,
2649 .cmd = "Q",
2650 .cmd_startswith = 1,
2651 .schema = "s0"
2652 };
2653 cmd_parser = &gen_set_cmd_desc;
edgar_igl60897d32008-05-09 08:25:14 +00002654 }
Jon Doron2704efa2019-05-29 09:41:45 +03002655 break;
bellard858693c2004-03-31 18:52:07 +00002656 default:
bellard858693c2004-03-31 18:52:07 +00002657 /* put empty packet */
Alex Bennéea346af32020-03-16 17:21:34 +00002658 put_packet("");
bellard858693c2004-03-31 18:52:07 +00002659 break;
2660 }
Jon Doron3e2c1262019-05-29 09:41:30 +03002661
Ramiro Polla2bdec392019-08-05 21:09:01 +02002662 if (cmd_parser) {
Alex Bennéea346af32020-03-16 17:21:34 +00002663 run_cmd_parser(line_buf, cmd_parser);
Ramiro Polla2bdec392019-08-05 21:09:01 +02002664 }
Jon Doron3e2c1262019-05-29 09:41:30 +03002665
bellard858693c2004-03-31 18:52:07 +00002666 return RS_IDLE;
2667}
2668
Andreas Färber64f6b342013-05-27 02:06:09 +02002669void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00002670{
Alex Bennéea346af32020-03-16 17:21:34 +00002671 GDBProcess *p = gdb_get_cpu_process(cpu);
Luc Michel160d8582019-01-07 15:23:46 +00002672
2673 if (!p->attached) {
2674 /*
2675 * Having a stop CPU corresponding to a process that is not attached
2676 * confuses GDB. So we ignore the request.
2677 */
2678 return;
2679 }
2680
Alex Bennée8d98c442020-03-16 17:21:33 +00002681 gdbserver_state.c_cpu = cpu;
2682 gdbserver_state.g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00002683}
2684
bellard1fddef42005-04-17 19:16:13 +00002685#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002686static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00002687{
Alex Bennéea346af32020-03-16 17:21:34 +00002688 CPUState *cpu = gdbserver_state.c_cpu;
Alex Bennée308f9e82020-03-16 17:21:35 +00002689 g_autoptr(GString) buf = g_string_new(NULL);
2690 g_autoptr(GString) tid = g_string_new(NULL);
aliguorid6fc1b32008-11-18 19:55:44 +00002691 const char *type;
bellard858693c2004-03-31 18:52:07 +00002692 int ret;
2693
Alex Bennéea346af32020-03-16 17:21:34 +00002694 if (running || gdbserver_state.state == RS_INACTIVE) {
Meador Ingecdb432b2012-03-15 17:49:45 +00002695 return;
2696 }
2697 /* Is there a GDB syscall waiting to be sent? */
Alex Bennéea346af32020-03-16 17:21:34 +00002698 if (gdbserver_state.current_syscall_cb) {
2699 put_packet(gdbserver_state.syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00002700 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01002701 }
Luc Michel95567c22019-01-07 15:23:46 +00002702
2703 if (cpu == NULL) {
2704 /* No process attached */
2705 return;
2706 }
2707
Alex Bennée308f9e82020-03-16 17:21:35 +00002708 gdb_append_thread_id(cpu, tid);
Luc Michel95567c22019-01-07 15:23:46 +00002709
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002710 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002711 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02002712 if (cpu->watchpoint_hit) {
2713 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00002714 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00002715 type = "r";
2716 break;
aliguoria1d1bb32008-11-18 20:07:32 +00002717 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00002718 type = "a";
2719 break;
2720 default:
2721 type = "";
2722 break;
2723 }
Doug Gale5c9522b2017-12-02 20:30:37 -05002724 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
2725 (target_ulong)cpu->watchpoint_hit->vaddr);
Alex Bennée308f9e82020-03-16 17:21:35 +00002726 g_string_printf(buf, "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
2727 GDB_SIGNAL_TRAP, tid->str, type,
2728 (target_ulong)cpu->watchpoint_hit->vaddr);
Andreas Färberff4700b2013-08-26 18:23:18 +02002729 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01002730 goto send_packet;
Doug Gale5c9522b2017-12-02 20:30:37 -05002731 } else {
2732 trace_gdbstub_hit_break();
pbrook6658ffb2007-03-16 23:58:11 +00002733 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07002734 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00002735 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01002736 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002737 case RUN_STATE_PAUSED:
Doug Gale5c9522b2017-12-02 20:30:37 -05002738 trace_gdbstub_hit_paused();
aliguori9781e042009-01-22 17:15:29 +00002739 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01002740 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002741 case RUN_STATE_SHUTDOWN:
Doug Gale5c9522b2017-12-02 20:30:37 -05002742 trace_gdbstub_hit_shutdown();
Jan Kiszka425189a2011-03-22 11:02:09 +01002743 ret = GDB_SIGNAL_QUIT;
2744 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002745 case RUN_STATE_IO_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002746 trace_gdbstub_hit_io_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002747 ret = GDB_SIGNAL_IO;
2748 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002749 case RUN_STATE_WATCHDOG:
Doug Gale5c9522b2017-12-02 20:30:37 -05002750 trace_gdbstub_hit_watchdog();
Jan Kiszka425189a2011-03-22 11:02:09 +01002751 ret = GDB_SIGNAL_ALRM;
2752 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002753 case RUN_STATE_INTERNAL_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002754 trace_gdbstub_hit_internal_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002755 ret = GDB_SIGNAL_ABRT;
2756 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002757 case RUN_STATE_SAVE_VM:
2758 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01002759 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002760 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01002761 ret = GDB_SIGNAL_XCPU;
2762 break;
2763 default:
Doug Gale5c9522b2017-12-02 20:30:37 -05002764 trace_gdbstub_hit_unknown(state);
Jan Kiszka425189a2011-03-22 11:02:09 +01002765 ret = GDB_SIGNAL_UNKNOWN;
2766 break;
bellardbbeb7b52006-04-23 18:42:15 +00002767 }
Jan Kiszka226d0072015-07-24 18:52:31 +02002768 gdb_set_stop_cpu(cpu);
Alex Bennée308f9e82020-03-16 17:21:35 +00002769 g_string_printf(buf, "T%02xthread:%s;", ret, tid->str);
Jan Kiszka425189a2011-03-22 11:02:09 +01002770
2771send_packet:
Alex Bennée308f9e82020-03-16 17:21:35 +00002772 put_packet(buf->str);
Jan Kiszka425189a2011-03-22 11:02:09 +01002773
2774 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02002775 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00002776}
bellard1fddef42005-04-17 19:16:13 +00002777#endif
bellard858693c2004-03-31 18:52:07 +00002778
pbrooka2d1eba2007-01-28 03:10:55 +00002779/* Send a gdb syscall request.
2780 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00002781 %x - target_ulong argument printed in hex.
2782 %lx - 64-bit argument printed in hex.
2783 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01002784void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00002785{
pbrooka2d1eba2007-01-28 03:10:55 +00002786 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00002787 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00002788 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00002789 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00002790
Alex Bennéea346af32020-03-16 17:21:34 +00002791 if (!gdbserver_state.init) {
pbrooka2d1eba2007-01-28 03:10:55 +00002792 return;
Alex Bennéea346af32020-03-16 17:21:34 +00002793 }
Alex Bennée8d98c442020-03-16 17:21:33 +00002794
2795 gdbserver_state.current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00002796#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002797 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00002798#endif
Alex Bennée8d98c442020-03-16 17:21:33 +00002799 p = &gdbserver_state.syscall_buf[0];
2800 p_end = &gdbserver_state.syscall_buf[sizeof(gdbserver_state.syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00002801 *(p++) = 'F';
2802 while (*fmt) {
2803 if (*fmt == '%') {
2804 fmt++;
2805 switch (*fmt++) {
2806 case 'x':
2807 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002808 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00002809 break;
pbrooka87295e2007-05-26 15:09:38 +00002810 case 'l':
2811 if (*(fmt++) != 'x')
2812 goto bad_format;
2813 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00002814 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00002815 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002816 case 's':
2817 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002818 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00002819 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00002820 break;
2821 default:
pbrooka87295e2007-05-26 15:09:38 +00002822 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002823 error_report("gdbstub: Bad syscall format string '%s'",
2824 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00002825 break;
2826 }
2827 } else {
2828 *(p++) = *(fmt++);
2829 }
2830 }
pbrook8a93e022007-08-06 13:19:15 +00002831 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00002832#ifdef CONFIG_USER_ONLY
Alex Bennéea346af32020-03-16 17:21:34 +00002833 put_packet(gdbserver_state.syscall_buf);
Peter Maydell4f710862018-05-15 19:19:58 +01002834 /* Return control to gdb for it to process the syscall request.
2835 * Since the protocol requires that gdb hands control back to us
2836 * using a "here are the results" F packet, we don't need to check
2837 * gdb_handlesig's return value (which is the signal to deliver if
2838 * execution was resumed via a continue packet).
2839 */
Alex Bennée8d98c442020-03-16 17:21:33 +00002840 gdb_handlesig(gdbserver_state.c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00002841#else
Meador Ingecdb432b2012-03-15 17:49:45 +00002842 /* In this case wait to send the syscall packet until notification that
2843 the CPU has stopped. This must be done because if the packet is sent
2844 now the reply from the syscall request could be received while the CPU
2845 is still in the running state, which can cause packets to be dropped
2846 and state transition 'T' packets to be sent while the syscall is still
2847 being processed. */
Alex Bennée8d98c442020-03-16 17:21:33 +00002848 qemu_cpu_kick(gdbserver_state.c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00002849#endif
2850}
2851
Peter Maydell19239b32015-09-07 10:39:27 +01002852void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
2853{
2854 va_list va;
2855
2856 va_start(va, fmt);
2857 gdb_do_syscallv(cb, fmt, va);
2858 va_end(va);
2859}
2860
Alex Bennéea346af32020-03-16 17:21:34 +00002861static void gdb_read_byte(uint8_t ch)
bellard858693c2004-03-31 18:52:07 +00002862{
ths60fe76f2007-12-16 03:02:09 +00002863 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00002864
bellard1fddef42005-04-17 19:16:13 +00002865#ifndef CONFIG_USER_ONLY
Damien Hedded116e812020-03-16 17:21:53 +00002866 if (gdbserver_state.last_packet->len) {
pbrook4046d912007-01-28 01:53:16 +00002867 /* Waiting for a response to the last packet. If we see the start
2868 of a new command then abandon the previous response. */
2869 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002870 trace_gdbstub_err_got_nack();
Damien Hedded116e812020-03-16 17:21:53 +00002871 put_buffer(gdbserver_state.last_packet->data,
2872 gdbserver_state.last_packet->len);
Alex Bennée118e2262017-07-12 11:52:13 +01002873 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002874 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01002875 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002876 trace_gdbstub_io_got_unexpected(ch);
pbrook4046d912007-01-28 01:53:16 +00002877 }
Alex Bennée118e2262017-07-12 11:52:13 +01002878
Damien Hedded116e812020-03-16 17:21:53 +00002879 if (ch == '+' || ch == '$') {
2880 g_byte_array_set_size(gdbserver_state.last_packet, 0);
2881 }
pbrook4046d912007-01-28 01:53:16 +00002882 if (ch != '$')
2883 return;
2884 }
Luiz Capitulino13548692011-07-29 15:36:43 -03002885 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00002886 /* when the CPU is running, we cannot do anything except stop
2887 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002888 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00002889 } else
bellard1fddef42005-04-17 19:16:13 +00002890#endif
bellard41625032005-04-24 10:07:11 +00002891 {
Alex Bennéea346af32020-03-16 17:21:34 +00002892 switch(gdbserver_state.state) {
bellard858693c2004-03-31 18:52:07 +00002893 case RS_IDLE:
2894 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04002895 /* start of command packet */
Alex Bennéea346af32020-03-16 17:21:34 +00002896 gdbserver_state.line_buf_index = 0;
2897 gdbserver_state.line_sum = 0;
2898 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002899 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002900 trace_gdbstub_err_garbage(ch);
bellard4c3a88a2003-07-26 12:06:08 +00002901 }
2902 break;
bellard858693c2004-03-31 18:52:07 +00002903 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04002904 if (ch == '}') {
2905 /* start escape sequence */
Alex Bennéea346af32020-03-16 17:21:34 +00002906 gdbserver_state.state = RS_GETLINE_ESC;
2907 gdbserver_state.line_sum += ch;
Doug Gale4bf43122017-05-01 12:22:10 -04002908 } else if (ch == '*') {
2909 /* start run length encoding sequence */
Alex Bennéea346af32020-03-16 17:21:34 +00002910 gdbserver_state.state = RS_GETLINE_RLE;
2911 gdbserver_state.line_sum += ch;
Doug Gale4bf43122017-05-01 12:22:10 -04002912 } else if (ch == '#') {
2913 /* end of command, start of checksum*/
Alex Bennéea346af32020-03-16 17:21:34 +00002914 gdbserver_state.state = RS_CHKSUM1;
2915 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002916 trace_gdbstub_err_overrun();
Alex Bennéea346af32020-03-16 17:21:34 +00002917 gdbserver_state.state = RS_IDLE;
bellard858693c2004-03-31 18:52:07 +00002918 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002919 /* unescaped command character */
Alex Bennéea346af32020-03-16 17:21:34 +00002920 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch;
2921 gdbserver_state.line_sum += ch;
Doug Gale4bf43122017-05-01 12:22:10 -04002922 }
2923 break;
2924 case RS_GETLINE_ESC:
2925 if (ch == '#') {
2926 /* unexpected end of command in escape sequence */
Alex Bennéea346af32020-03-16 17:21:34 +00002927 gdbserver_state.state = RS_CHKSUM1;
2928 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04002929 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05002930 trace_gdbstub_err_overrun();
Alex Bennéea346af32020-03-16 17:21:34 +00002931 gdbserver_state.state = RS_IDLE;
Doug Gale4bf43122017-05-01 12:22:10 -04002932 } else {
2933 /* parse escaped character and leave escape state */
Alex Bennéea346af32020-03-16 17:21:34 +00002934 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20;
2935 gdbserver_state.line_sum += ch;
2936 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002937 }
2938 break;
2939 case RS_GETLINE_RLE:
Markus Armbruster046aba12019-05-14 20:03:08 +02002940 /*
2941 * Run-length encoding is explained in "Debugging with GDB /
2942 * Appendix E GDB Remote Serial Protocol / Overview".
2943 */
2944 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
Doug Gale4bf43122017-05-01 12:22:10 -04002945 /* invalid RLE count encoding */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002946 trace_gdbstub_err_invalid_repeat(ch);
Alex Bennéea346af32020-03-16 17:21:34 +00002947 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002948 } else {
2949 /* decode repeat length */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002950 int repeat = ch - ' ' + 3;
Alex Bennéea346af32020-03-16 17:21:34 +00002951 if (gdbserver_state.line_buf_index + repeat >= sizeof(gdbserver_state.line_buf) - 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04002952 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05002953 trace_gdbstub_err_overrun();
Alex Bennéea346af32020-03-16 17:21:34 +00002954 gdbserver_state.state = RS_IDLE;
2955 } else if (gdbserver_state.line_buf_index < 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04002956 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05002957 trace_gdbstub_err_invalid_rle();
Alex Bennéea346af32020-03-16 17:21:34 +00002958 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002959 } else {
2960 /* repeat the last character */
Alex Bennéea346af32020-03-16 17:21:34 +00002961 memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index,
2962 gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat);
2963 gdbserver_state.line_buf_index += repeat;
2964 gdbserver_state.line_sum += ch;
2965 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002966 }
bellard858693c2004-03-31 18:52:07 +00002967 }
2968 break;
2969 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04002970 /* get high hex digit of checksum */
2971 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002972 trace_gdbstub_err_checksum_invalid(ch);
Alex Bennéea346af32020-03-16 17:21:34 +00002973 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002974 break;
2975 }
Alex Bennéea346af32020-03-16 17:21:34 +00002976 gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0';
2977 gdbserver_state.line_csum = fromhex(ch) << 4;
2978 gdbserver_state.state = RS_CHKSUM2;
bellard858693c2004-03-31 18:52:07 +00002979 break;
2980 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04002981 /* get low hex digit of checksum */
2982 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002983 trace_gdbstub_err_checksum_invalid(ch);
Alex Bennéea346af32020-03-16 17:21:34 +00002984 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002985 break;
bellard858693c2004-03-31 18:52:07 +00002986 }
Alex Bennéea346af32020-03-16 17:21:34 +00002987 gdbserver_state.line_csum |= fromhex(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002988
Alex Bennéea346af32020-03-16 17:21:34 +00002989 if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) {
2990 trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04002991 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00002992 reply = '-';
Alex Bennéea346af32020-03-16 17:21:34 +00002993 put_buffer(&reply, 1);
2994 gdbserver_state.state = RS_IDLE;
bellard858693c2004-03-31 18:52:07 +00002995 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002996 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00002997 reply = '+';
Alex Bennéea346af32020-03-16 17:21:34 +00002998 put_buffer(&reply, 1);
2999 gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf);
bellard858693c2004-03-31 18:52:07 +00003000 }
bellardb4608c02003-06-27 17:34:32 +00003001 break;
pbrooka2d1eba2007-01-28 03:10:55 +00003002 default:
3003 abort();
bellardb4608c02003-06-27 17:34:32 +00003004 }
3005 }
bellard858693c2004-03-31 18:52:07 +00003006}
3007
Paul Brook0e1c9c52010-06-16 13:03:51 +01003008/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01003009void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01003010{
Paul Brook0e1c9c52010-06-16 13:03:51 +01003011 char buf[4];
3012
Alex Bennée8d98c442020-03-16 17:21:33 +00003013 if (!gdbserver_state.init) {
Paul Brook0e1c9c52010-06-16 13:03:51 +01003014 return;
3015 }
3016#ifdef CONFIG_USER_ONLY
Alex Bennéefcedd922020-04-30 20:01:19 +01003017 if (gdbserver_state.socket_path) {
3018 unlink(gdbserver_state.socket_path);
3019 }
Alex Bennéee0a1e202020-04-30 20:01:18 +01003020 if (gdbserver_state.fd < 0) {
Paul Brook0e1c9c52010-06-16 13:03:51 +01003021 return;
3022 }
3023#endif
3024
Doug Gale5c9522b2017-12-02 20:30:37 -05003025 trace_gdbstub_op_exiting((uint8_t)code);
3026
Paul Brook0e1c9c52010-06-16 13:03:51 +01003027 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
Alex Bennéea346af32020-03-16 17:21:34 +00003028 put_packet(buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01003029
3030#ifndef CONFIG_USER_ONLY
Alex Bennée8d98c442020-03-16 17:21:33 +00003031 qemu_chr_fe_deinit(&gdbserver_state.chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01003032#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01003033}
3034
Luc Michel8f468632019-01-07 15:23:45 +00003035/*
3036 * Create the process that will contain all the "orphan" CPUs (that are not
3037 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
3038 * be attachable and thus will be invisible to the user.
3039 */
3040static void create_default_process(GDBState *s)
3041{
3042 GDBProcess *process;
3043 int max_pid = 0;
3044
Alex Bennéea346af32020-03-16 17:21:34 +00003045 if (gdbserver_state.process_num) {
Luc Michel8f468632019-01-07 15:23:45 +00003046 max_pid = s->processes[s->process_num - 1].pid;
3047 }
3048
3049 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
3050 process = &s->processes[s->process_num - 1];
3051
3052 /* We need an available PID slot for this process */
3053 assert(max_pid < UINT32_MAX);
3054
3055 process->pid = max_pid + 1;
3056 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00003057 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00003058}
3059
bellard1fddef42005-04-17 19:16:13 +00003060#ifdef CONFIG_USER_ONLY
3061int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02003062gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00003063{
Andreas Färber5ca666c2013-06-24 19:20:57 +02003064 char buf[256];
3065 int n;
bellard1fddef42005-04-17 19:16:13 +00003066
Alex Bennéee0a1e202020-04-30 20:01:18 +01003067 if (!gdbserver_state.init || gdbserver_state.fd < 0) {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003068 return sig;
bellard1fddef42005-04-17 19:16:13 +00003069 }
3070
Andreas Färber5ca666c2013-06-24 19:20:57 +02003071 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02003072 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07003073 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00003074
Andreas Färber5ca666c2013-06-24 19:20:57 +02003075 if (sig != 0) {
3076 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
Alex Bennéea346af32020-03-16 17:21:34 +00003077 put_packet(buf);
Andreas Färber5ca666c2013-06-24 19:20:57 +02003078 }
3079 /* put_packet() might have detected that the peer terminated the
3080 connection. */
Alex Bennée8d98c442020-03-16 17:21:33 +00003081 if (gdbserver_state.fd < 0) {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003082 return sig;
3083 }
3084
3085 sig = 0;
Alex Bennée8d98c442020-03-16 17:21:33 +00003086 gdbserver_state.state = RS_IDLE;
3087 gdbserver_state.running_state = 0;
3088 while (gdbserver_state.running_state == 0) {
3089 n = read(gdbserver_state.fd, buf, 256);
Andreas Färber5ca666c2013-06-24 19:20:57 +02003090 if (n > 0) {
3091 int i;
3092
3093 for (i = 0; i < n; i++) {
Alex Bennéea346af32020-03-16 17:21:34 +00003094 gdb_read_byte(buf[i]);
Andreas Färber5ca666c2013-06-24 19:20:57 +02003095 }
Peter Wu5819e3e2016-06-05 16:35:48 +02003096 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003097 /* XXX: Connection closed. Should probably wait for another
3098 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02003099 if (n == 0) {
Alex Bennée8d98c442020-03-16 17:21:33 +00003100 close(gdbserver_state.fd);
Peter Wu5819e3e2016-06-05 16:35:48 +02003101 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003102 gdbserver_state.fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02003103 return sig;
bellard1fddef42005-04-17 19:16:13 +00003104 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02003105 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003106 sig = gdbserver_state.signal;
3107 gdbserver_state.signal = 0;
Andreas Färber5ca666c2013-06-24 19:20:57 +02003108 return sig;
bellard1fddef42005-04-17 19:16:13 +00003109}
bellarde9009672005-04-26 20:42:36 +00003110
aurel32ca587a82008-12-18 22:44:13 +00003111/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01003112void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00003113{
Andreas Färber5ca666c2013-06-24 19:20:57 +02003114 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00003115
Alex Bennéee0a1e202020-04-30 20:01:18 +01003116 if (!gdbserver_state.init || gdbserver_state.fd < 0) {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003117 return;
3118 }
aurel32ca587a82008-12-18 22:44:13 +00003119
Andreas Färber5ca666c2013-06-24 19:20:57 +02003120 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
Alex Bennéea346af32020-03-16 17:21:34 +00003121 put_packet(buf);
aurel32ca587a82008-12-18 22:44:13 +00003122}
bellard1fddef42005-04-17 19:16:13 +00003123
Alex Bennéefcedd922020-04-30 20:01:19 +01003124static void gdb_accept_init(int fd)
3125{
3126 init_gdbserver_state();
3127 create_default_process(&gdbserver_state);
3128 gdbserver_state.processes[0].attached = true;
3129 gdbserver_state.c_cpu = gdb_first_attached_cpu();
3130 gdbserver_state.g_cpu = gdbserver_state.c_cpu;
3131 gdbserver_state.fd = fd;
3132 gdb_has_xml = false;
3133}
3134
3135static bool gdb_accept_socket(int gdb_fd)
3136{
3137 int fd;
3138
3139 for(;;) {
3140 fd = accept(gdb_fd, NULL, NULL);
3141 if (fd < 0 && errno != EINTR) {
3142 perror("accept socket");
3143 return false;
3144 } else if (fd >= 0) {
3145 qemu_set_cloexec(fd);
3146 break;
3147 }
3148 }
3149
3150 gdb_accept_init(fd);
3151 return true;
3152}
3153
3154static int gdbserver_open_socket(const char *path)
3155{
3156 struct sockaddr_un sockaddr;
3157 int fd, ret;
3158
3159 fd = socket(AF_UNIX, SOCK_STREAM, 0);
3160 if (fd < 0) {
3161 perror("create socket");
3162 return -1;
3163 }
3164
3165 sockaddr.sun_family = AF_UNIX;
3166 pstrcpy(sockaddr.sun_path, sizeof(sockaddr.sun_path) - 1, path);
3167 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
3168 if (ret < 0) {
3169 perror("bind socket");
3170 close(fd);
3171 return -1;
3172 }
3173 ret = listen(fd, 1);
3174 if (ret < 0) {
3175 perror("listen socket");
3176 close(fd);
3177 return -1;
3178 }
3179
3180 return fd;
3181}
3182
3183static bool gdb_accept_tcp(int gdb_fd)
bellard858693c2004-03-31 18:52:07 +00003184{
bellard858693c2004-03-31 18:52:07 +00003185 struct sockaddr_in sockaddr;
3186 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09003187 int fd;
bellard858693c2004-03-31 18:52:07 +00003188
3189 for(;;) {
3190 len = sizeof(sockaddr);
Alex Bennéee0a1e202020-04-30 20:01:18 +01003191 fd = accept(gdb_fd, (struct sockaddr *)&sockaddr, &len);
bellard858693c2004-03-31 18:52:07 +00003192 if (fd < 0 && errno != EINTR) {
3193 perror("accept");
Peter Maydell2f652222018-05-14 18:30:44 +01003194 return false;
bellard858693c2004-03-31 18:52:07 +00003195 } else if (fd >= 0) {
Peter Maydellf5bdd782018-05-14 18:30:43 +01003196 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00003197 break;
3198 }
3199 }
3200
3201 /* set short latency */
Peter Maydell2f652222018-05-14 18:30:44 +01003202 if (socket_set_nodelay(fd)) {
3203 perror("setsockopt");
Philippe Mathieu-Daudéead75d82018-05-24 19:34:58 -03003204 close(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01003205 return false;
3206 }
ths3b46e622007-09-17 08:09:54 +00003207
Alex Bennéefcedd922020-04-30 20:01:19 +01003208 gdb_accept_init(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01003209 return true;
bellard858693c2004-03-31 18:52:07 +00003210}
3211
Alex Bennéefcedd922020-04-30 20:01:19 +01003212static int gdbserver_open_port(int port)
bellard858693c2004-03-31 18:52:07 +00003213{
3214 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02003215 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00003216
3217 fd = socket(PF_INET, SOCK_STREAM, 0);
3218 if (fd < 0) {
3219 perror("socket");
3220 return -1;
3221 }
Peter Maydellf5bdd782018-05-14 18:30:43 +01003222 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00003223
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02003224 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00003225
3226 sockaddr.sin_family = AF_INET;
3227 sockaddr.sin_port = htons(port);
3228 sockaddr.sin_addr.s_addr = 0;
3229 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
3230 if (ret < 0) {
3231 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00003232 close(fd);
bellard858693c2004-03-31 18:52:07 +00003233 return -1;
3234 }
Peter Wu96165b92016-05-04 11:32:17 +02003235 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00003236 if (ret < 0) {
3237 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00003238 close(fd);
bellard858693c2004-03-31 18:52:07 +00003239 return -1;
3240 }
Alex Bennéefcedd922020-04-30 20:01:19 +01003241
bellard858693c2004-03-31 18:52:07 +00003242 return fd;
3243}
3244
Alex Bennéefcedd922020-04-30 20:01:19 +01003245int gdbserver_start(const char *port_or_path)
bellard858693c2004-03-31 18:52:07 +00003246{
Alex Bennéefcedd922020-04-30 20:01:19 +01003247 int port = g_ascii_strtoull(port_or_path, NULL, 10);
3248 int gdb_fd;
3249
3250 if (port > 0) {
3251 gdb_fd = gdbserver_open_port(port);
3252 } else {
3253 gdb_fd = gdbserver_open_socket(port_or_path);
3254 }
3255
Alex Bennéee0a1e202020-04-30 20:01:18 +01003256 if (gdb_fd < 0) {
bellard858693c2004-03-31 18:52:07 +00003257 return -1;
Alex Bennéee0a1e202020-04-30 20:01:18 +01003258 }
Alex Bennéefcedd922020-04-30 20:01:19 +01003259
3260 if (port > 0 && gdb_accept_tcp(gdb_fd)) {
3261 return 0;
3262 } else if (gdb_accept_socket(gdb_fd)) {
3263 gdbserver_state.socket_path = g_strdup(port_or_path);
3264 return 0;
Peter Maydell2f652222018-05-14 18:30:44 +01003265 }
Alex Bennéefcedd922020-04-30 20:01:19 +01003266
3267 /* gone wrong */
3268 close(gdb_fd);
3269 return -1;
bellardb4608c02003-06-27 17:34:32 +00003270}
aurel322b1319c2008-12-18 22:44:04 +00003271
3272/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07003273void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00003274{
Alex Bennéee0a1e202020-04-30 20:01:18 +01003275 if (!gdbserver_state.init || gdbserver_state.fd < 0) {
Andreas Färber75a34032013-09-02 16:57:02 +02003276 return;
3277 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003278 close(gdbserver_state.fd);
3279 gdbserver_state.fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02003280 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02003281 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00003282}
pbrook4046d912007-01-28 01:53:16 +00003283#else
thsaa1f17c2007-07-11 22:48:58 +00003284static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00003285{
pbrook56aebc82008-10-11 17:55:29 +00003286 /* We can handle an arbitrarily large amount of data.
3287 Pick the maximum packet size, which is as good as anything. */
3288 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00003289}
3290
thsaa1f17c2007-07-11 22:48:58 +00003291static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00003292{
pbrook4046d912007-01-28 01:53:16 +00003293 int i;
3294
3295 for (i = 0; i < size; i++) {
Alex Bennéea346af32020-03-16 17:21:34 +00003296 gdb_read_byte(buf[i]);
pbrook4046d912007-01-28 01:53:16 +00003297 }
3298}
3299
Philippe Mathieu-Daudé083b2662019-12-18 18:20:09 +01003300static void gdb_chr_event(void *opaque, QEMUChrEvent event)
pbrook4046d912007-01-28 01:53:16 +00003301{
Luc Michel970ed902019-01-07 15:23:46 +00003302 int i;
3303 GDBState *s = (GDBState *) opaque;
3304
pbrook4046d912007-01-28 01:53:16 +00003305 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05303306 case CHR_EVENT_OPENED:
Luc Michel970ed902019-01-07 15:23:46 +00003307 /* Start with first process attached, others detached */
3308 for (i = 0; i < s->process_num; i++) {
3309 s->processes[i].attached = !i;
3310 }
3311
Alex Bennéea346af32020-03-16 17:21:34 +00003312 s->c_cpu = gdb_first_attached_cpu();
Luc Michel970ed902019-01-07 15:23:46 +00003313 s->g_cpu = s->c_cpu;
3314
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003315 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02003316 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00003317 break;
3318 default:
3319 break;
3320 }
3321}
3322
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003323static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00003324{
Damien Hedded86b4672020-03-16 17:21:54 +00003325 g_autoptr(GString) hex_buf = g_string_new("O");
3326 memtohex(hex_buf, buf, len);
3327 put_packet(hex_buf->str);
aliguori8a34a0f2009-03-05 23:01:55 +00003328 return len;
3329}
3330
aliguori59030a82009-04-05 18:43:41 +00003331#ifndef _WIN32
3332static void gdb_sigterm_handler(int signal)
3333{
Luiz Capitulino13548692011-07-29 15:36:43 -03003334 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003335 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01003336 }
aliguori59030a82009-04-05 18:43:41 +00003337}
3338#endif
3339
Marc-André Lureau777357d2016-12-07 18:39:10 +03003340static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
3341 bool *be_opened, Error **errp)
3342{
3343 *be_opened = false;
3344}
3345
3346static void char_gdb_class_init(ObjectClass *oc, void *data)
3347{
3348 ChardevClass *cc = CHARDEV_CLASS(oc);
3349
3350 cc->internal = true;
3351 cc->open = gdb_monitor_open;
3352 cc->chr_write = gdb_monitor_write;
3353}
3354
3355#define TYPE_CHARDEV_GDB "chardev-gdb"
3356
3357static const TypeInfo char_gdb_type_info = {
3358 .name = TYPE_CHARDEV_GDB,
3359 .parent = TYPE_CHARDEV,
3360 .class_init = char_gdb_class_init,
3361};
3362
Luc Michel8f468632019-01-07 15:23:45 +00003363static int find_cpu_clusters(Object *child, void *opaque)
3364{
3365 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
3366 GDBState *s = (GDBState *) opaque;
3367 CPUClusterState *cluster = CPU_CLUSTER(child);
3368 GDBProcess *process;
3369
3370 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
3371
3372 process = &s->processes[s->process_num - 1];
3373
3374 /*
3375 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
3376 * runtime, we enforce here that the machine does not use a cluster ID
3377 * that would lead to PID 0.
3378 */
3379 assert(cluster->cluster_id != UINT32_MAX);
3380 process->pid = cluster->cluster_id + 1;
3381 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00003382 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00003383
3384 return 0;
3385 }
3386
3387 return object_child_foreach(child, find_cpu_clusters, opaque);
3388}
3389
3390static int pid_order(const void *a, const void *b)
3391{
3392 GDBProcess *pa = (GDBProcess *) a;
3393 GDBProcess *pb = (GDBProcess *) b;
3394
3395 if (pa->pid < pb->pid) {
3396 return -1;
3397 } else if (pa->pid > pb->pid) {
3398 return 1;
3399 } else {
3400 return 0;
3401 }
3402}
3403
3404static void create_processes(GDBState *s)
3405{
3406 object_child_foreach(object_get_root(), find_cpu_clusters, s);
3407
Alex Bennéea346af32020-03-16 17:21:34 +00003408 if (gdbserver_state.processes) {
Luc Michel8f468632019-01-07 15:23:45 +00003409 /* Sort by PID */
Alex Bennéea346af32020-03-16 17:21:34 +00003410 qsort(gdbserver_state.processes, gdbserver_state.process_num, sizeof(gdbserver_state.processes[0]), pid_order);
Luc Michel8f468632019-01-07 15:23:45 +00003411 }
3412
3413 create_default_process(s);
3414}
3415
aliguori59030a82009-04-05 18:43:41 +00003416int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00003417{
Doug Gale5c9522b2017-12-02 20:30:37 -05003418 trace_gdbstub_op_start(device);
3419
aliguori59030a82009-04-05 18:43:41 +00003420 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003421 Chardev *chr = NULL;
3422 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00003423
Ziyue Yang508b4ec2017-01-18 16:02:41 +08003424 if (!first_cpu) {
3425 error_report("gdbstub: meaningless to attach gdb to a "
3426 "machine without any CPU.");
3427 return -1;
3428 }
3429
aliguori59030a82009-04-05 18:43:41 +00003430 if (!device)
3431 return -1;
3432 if (strcmp(device, "none") != 0) {
3433 if (strstart(device, "tcp:", NULL)) {
3434 /* enforce required TCP attributes */
3435 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
3436 "%s,nowait,nodelay,server", device);
3437 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00003438 }
aliguori59030a82009-04-05 18:43:41 +00003439#ifndef _WIN32
3440 else if (strcmp(device, "stdio") == 0) {
3441 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00003442
aliguori59030a82009-04-05 18:43:41 +00003443 memset(&act, 0, sizeof(act));
3444 act.sa_handler = gdb_sigterm_handler;
3445 sigaction(SIGINT, &act, NULL);
3446 }
3447#endif
Marc-André Lureau95e30b22018-08-22 19:19:42 +02003448 /*
3449 * FIXME: it's a bit weird to allow using a mux chardev here
3450 * and implicitly setup a monitor. We may want to break this.
3451 */
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01003452 chr = qemu_chr_new_noreplay("gdb", device, true, NULL);
aliguori36556b22009-03-28 18:05:53 +00003453 if (!chr)
3454 return -1;
pbrookcfc34752007-02-22 01:48:01 +00003455 }
3456
Alex Bennée8d98c442020-03-16 17:21:33 +00003457 if (!gdbserver_state.init) {
3458 init_gdbserver_state();
pbrook4046d912007-01-28 01:53:16 +00003459
aliguori36556b22009-03-28 18:05:53 +00003460 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
3461
3462 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03003463 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01003464 NULL, NULL, &error_abort);
Kevin Wolf8e9119a2020-02-24 15:30:06 +01003465 monitor_init_hmp(mon_chr, false, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00003466 } else {
Alex Bennée8d98c442020-03-16 17:21:33 +00003467 qemu_chr_fe_deinit(&gdbserver_state.chr, true);
3468 mon_chr = gdbserver_state.mon_chr;
3469 reset_gdbserver_state();
aliguori36556b22009-03-28 18:05:53 +00003470 }
Luc Michel8f468632019-01-07 15:23:45 +00003471
Alex Bennée8d98c442020-03-16 17:21:33 +00003472 create_processes(&gdbserver_state);
Luc Michel8f468632019-01-07 15:23:45 +00003473
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03003474 if (chr) {
Alex Bennée8d98c442020-03-16 17:21:33 +00003475 qemu_chr_fe_init(&gdbserver_state.chr, chr, &error_abort);
3476 qemu_chr_fe_set_handlers(&gdbserver_state.chr, gdb_chr_can_receive,
3477 gdb_chr_receive, gdb_chr_event,
3478 NULL, &gdbserver_state, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03003479 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003480 gdbserver_state.state = chr ? RS_IDLE : RS_INACTIVE;
3481 gdbserver_state.mon_chr = mon_chr;
3482 gdbserver_state.current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00003483
pbrook4046d912007-01-28 01:53:16 +00003484 return 0;
3485}
Marc-André Lureau777357d2016-12-07 18:39:10 +03003486
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01003487void gdbserver_cleanup(void)
3488{
Alex Bennée8d98c442020-03-16 17:21:33 +00003489 if (gdbserver_state.init) {
Alex Bennéea346af32020-03-16 17:21:34 +00003490 put_packet("W00");
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01003491 }
3492}
3493
Marc-André Lureau777357d2016-12-07 18:39:10 +03003494static void register_types(void)
3495{
3496 type_register_static(&char_gdb_type_info);
3497}
3498
3499type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00003500#endif