blob: 88a34c8f5223725952d0e7f0ccd3a55b58b5e330 [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 Armbrusterda34e652016-03-14 09:01:28 +010027#include "qapi/error.h"
Ziyue Yang508b4ec2017-01-18 16:02:41 +080028#include "qemu/error-report.h"
Markus Armbruster856dfd82019-05-23 16:35:06 +020029#include "qemu/ctype.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020030#include "qemu/cutils.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020031#include "qemu/module.h"
Paolo Bonzini243af022020-02-04 12:20:10 +010032#include "trace/trace-root.h"
Peter Maydell85b4fa02021-09-08 16:44:04 +010033#include "exec/gdbstub.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"
Luc Michel8f468632019-01-07 15:23:45 +000040#include "hw/cpu/cluster.h"
Like Xu5cc87672019-05-19 04:54:21 +080041#include "hw/boards.h"
bellard1fddef42005-04-17 19:16:13 +000042#endif
bellard67b915a2004-03-31 23:37:16 +000043
pbrook56aebc82008-10-11 17:55:29 +000044#define MAX_PACKET_LENGTH 4096
45
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010046#include "qemu/sockets.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010047#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010048#include "sysemu/kvm.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020049#include "sysemu/runstate.h"
Philippe Mathieu-Daudé6b5fe132021-03-05 13:54:49 +000050#include "semihosting/semihost.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010051#include "exec/exec-all.h"
Pavel Dovgalyukfda84582020-10-03 20:13:43 +030052#include "sysemu/replay.h"
aurel32ca587a82008-12-18 22:44:13 +000053
Jan Kiszkaa3919382015-02-07 09:38:44 +010054#ifdef CONFIG_USER_ONLY
55#define GDB_ATTACHED "0"
56#else
57#define GDB_ATTACHED "1"
58#endif
59
Jon Doronab4752e2019-05-29 09:41:48 +030060#ifndef CONFIG_USER_ONLY
61static int phy_memory_mode;
62#endif
63
Andreas Färberf3659ee2013-06-27 19:09:09 +020064static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
65 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020066{
Jon Doronab4752e2019-05-29 09:41:48 +030067 CPUClass *cc;
Andreas Färberf3659ee2013-06-27 19:09:09 +020068
Jon Doronab4752e2019-05-29 09:41:48 +030069#ifndef CONFIG_USER_ONLY
70 if (phy_memory_mode) {
71 if (is_write) {
72 cpu_physical_memory_write(addr, buf, len);
73 } else {
74 cpu_physical_memory_read(addr, buf, len);
75 }
76 return 0;
77 }
78#endif
79
80 cc = CPU_GET_CLASS(cpu);
Andreas Färberf3659ee2013-06-27 19:09:09 +020081 if (cc->memory_rw_debug) {
82 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
83 }
84 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020085}
aurel32ca587a82008-12-18 22:44:13 +000086
Alex Bennéed2a6c852017-07-12 11:52:14 +010087/* Return the GDB index for a given vCPU state.
88 *
89 * For user mode this is simply the thread id. In system mode GDB
90 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
91 */
92static inline int cpu_gdb_index(CPUState *cpu)
93{
94#if defined(CONFIG_USER_ONLY)
Alex Bennéebd88c782017-07-12 11:52:15 +010095 TaskState *ts = (TaskState *) cpu->opaque;
Alex Bennéea8e537f2021-11-29 14:09:29 +000096 return ts ? ts->ts_tid : -1;
Alex Bennéed2a6c852017-07-12 11:52:14 +010097#else
98 return cpu->cpu_index + 1;
99#endif
100}
101
aurel32ca587a82008-12-18 22:44:13 +0000102enum {
103 GDB_SIGNAL_0 = 0,
104 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +0100105 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +0000106 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +0100107 GDB_SIGNAL_ABRT = 6,
108 GDB_SIGNAL_ALRM = 14,
109 GDB_SIGNAL_IO = 23,
110 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +0000111 GDB_SIGNAL_UNKNOWN = 143
112};
113
114#ifdef CONFIG_USER_ONLY
115
116/* Map target signal numbers to GDB protocol signal numbers and vice
117 * versa. For user emulation's currently supported systems, we can
118 * assume most signals are defined.
119 */
120
121static int gdb_signal_table[] = {
122 0,
123 TARGET_SIGHUP,
124 TARGET_SIGINT,
125 TARGET_SIGQUIT,
126 TARGET_SIGILL,
127 TARGET_SIGTRAP,
128 TARGET_SIGABRT,
129 -1, /* SIGEMT */
130 TARGET_SIGFPE,
131 TARGET_SIGKILL,
132 TARGET_SIGBUS,
133 TARGET_SIGSEGV,
134 TARGET_SIGSYS,
135 TARGET_SIGPIPE,
136 TARGET_SIGALRM,
137 TARGET_SIGTERM,
138 TARGET_SIGURG,
139 TARGET_SIGSTOP,
140 TARGET_SIGTSTP,
141 TARGET_SIGCONT,
142 TARGET_SIGCHLD,
143 TARGET_SIGTTIN,
144 TARGET_SIGTTOU,
145 TARGET_SIGIO,
146 TARGET_SIGXCPU,
147 TARGET_SIGXFSZ,
148 TARGET_SIGVTALRM,
149 TARGET_SIGPROF,
150 TARGET_SIGWINCH,
151 -1, /* SIGLOST */
152 TARGET_SIGUSR1,
153 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000154#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000155 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000156#else
157 -1,
158#endif
aurel32ca587a82008-12-18 22:44:13 +0000159 -1, /* SIGPOLL */
160 -1,
161 -1,
162 -1,
163 -1,
164 -1,
165 -1,
166 -1,
167 -1,
168 -1,
169 -1,
170 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000171#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000172 __SIGRTMIN + 1,
173 __SIGRTMIN + 2,
174 __SIGRTMIN + 3,
175 __SIGRTMIN + 4,
176 __SIGRTMIN + 5,
177 __SIGRTMIN + 6,
178 __SIGRTMIN + 7,
179 __SIGRTMIN + 8,
180 __SIGRTMIN + 9,
181 __SIGRTMIN + 10,
182 __SIGRTMIN + 11,
183 __SIGRTMIN + 12,
184 __SIGRTMIN + 13,
185 __SIGRTMIN + 14,
186 __SIGRTMIN + 15,
187 __SIGRTMIN + 16,
188 __SIGRTMIN + 17,
189 __SIGRTMIN + 18,
190 __SIGRTMIN + 19,
191 __SIGRTMIN + 20,
192 __SIGRTMIN + 21,
193 __SIGRTMIN + 22,
194 __SIGRTMIN + 23,
195 __SIGRTMIN + 24,
196 __SIGRTMIN + 25,
197 __SIGRTMIN + 26,
198 __SIGRTMIN + 27,
199 __SIGRTMIN + 28,
200 __SIGRTMIN + 29,
201 __SIGRTMIN + 30,
202 __SIGRTMIN + 31,
203 -1, /* SIGCANCEL */
204 __SIGRTMIN,
205 __SIGRTMIN + 32,
206 __SIGRTMIN + 33,
207 __SIGRTMIN + 34,
208 __SIGRTMIN + 35,
209 __SIGRTMIN + 36,
210 __SIGRTMIN + 37,
211 __SIGRTMIN + 38,
212 __SIGRTMIN + 39,
213 __SIGRTMIN + 40,
214 __SIGRTMIN + 41,
215 __SIGRTMIN + 42,
216 __SIGRTMIN + 43,
217 __SIGRTMIN + 44,
218 __SIGRTMIN + 45,
219 __SIGRTMIN + 46,
220 __SIGRTMIN + 47,
221 __SIGRTMIN + 48,
222 __SIGRTMIN + 49,
223 __SIGRTMIN + 50,
224 __SIGRTMIN + 51,
225 __SIGRTMIN + 52,
226 __SIGRTMIN + 53,
227 __SIGRTMIN + 54,
228 __SIGRTMIN + 55,
229 __SIGRTMIN + 56,
230 __SIGRTMIN + 57,
231 __SIGRTMIN + 58,
232 __SIGRTMIN + 59,
233 __SIGRTMIN + 60,
234 __SIGRTMIN + 61,
235 __SIGRTMIN + 62,
236 __SIGRTMIN + 63,
237 __SIGRTMIN + 64,
238 __SIGRTMIN + 65,
239 __SIGRTMIN + 66,
240 __SIGRTMIN + 67,
241 __SIGRTMIN + 68,
242 __SIGRTMIN + 69,
243 __SIGRTMIN + 70,
244 __SIGRTMIN + 71,
245 __SIGRTMIN + 72,
246 __SIGRTMIN + 73,
247 __SIGRTMIN + 74,
248 __SIGRTMIN + 75,
249 __SIGRTMIN + 76,
250 __SIGRTMIN + 77,
251 __SIGRTMIN + 78,
252 __SIGRTMIN + 79,
253 __SIGRTMIN + 80,
254 __SIGRTMIN + 81,
255 __SIGRTMIN + 82,
256 __SIGRTMIN + 83,
257 __SIGRTMIN + 84,
258 __SIGRTMIN + 85,
259 __SIGRTMIN + 86,
260 __SIGRTMIN + 87,
261 __SIGRTMIN + 88,
262 __SIGRTMIN + 89,
263 __SIGRTMIN + 90,
264 __SIGRTMIN + 91,
265 __SIGRTMIN + 92,
266 __SIGRTMIN + 93,
267 __SIGRTMIN + 94,
268 __SIGRTMIN + 95,
269 -1, /* SIGINFO */
270 -1, /* UNKNOWN */
271 -1, /* DEFAULT */
272 -1,
273 -1,
274 -1,
275 -1,
276 -1,
277 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000278#endif
aurel32ca587a82008-12-18 22:44:13 +0000279};
bellard8f447cc2006-06-14 15:21:14 +0000280#else
aurel32ca587a82008-12-18 22:44:13 +0000281/* In system mode we only need SIGINT and SIGTRAP; other signals
282 are not yet supported. */
283
284enum {
285 TARGET_SIGINT = 2,
286 TARGET_SIGTRAP = 5
287};
288
289static int gdb_signal_table[] = {
290 -1,
291 -1,
292 TARGET_SIGINT,
293 -1,
294 -1,
295 TARGET_SIGTRAP
296};
bellard8f447cc2006-06-14 15:21:14 +0000297#endif
bellardb4608c02003-06-27 17:34:32 +0000298
aurel32ca587a82008-12-18 22:44:13 +0000299#ifdef CONFIG_USER_ONLY
300static int target_signal_to_gdb (int sig)
301{
302 int i;
303 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
304 if (gdb_signal_table[i] == sig)
305 return i;
306 return GDB_SIGNAL_UNKNOWN;
307}
308#endif
309
310static int gdb_signal_to_target (int sig)
311{
312 if (sig < ARRAY_SIZE (gdb_signal_table))
313 return gdb_signal_table[sig];
314 else
315 return -1;
316}
317
pbrook56aebc82008-10-11 17:55:29 +0000318typedef struct GDBRegisterState {
319 int base_reg;
320 int num_regs;
Alex Bennéea010bdb2020-03-16 17:21:41 +0000321 gdb_get_reg_cb get_reg;
322 gdb_set_reg_cb set_reg;
pbrook56aebc82008-10-11 17:55:29 +0000323 const char *xml;
324 struct GDBRegisterState *next;
325} GDBRegisterState;
326
Luc Michel8f468632019-01-07 15:23:45 +0000327typedef struct GDBProcess {
328 uint32_t pid;
329 bool attached;
Luc Michelc145eea2019-01-07 15:23:46 +0000330
331 char target_xml[1024];
Luc Michel8f468632019-01-07 15:23:45 +0000332} GDBProcess;
333
bellard858693c2004-03-31 18:52:07 +0000334enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000335 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000336 RS_IDLE,
337 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400338 RS_GETLINE_ESC,
339 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000340 RS_CHKSUM1,
341 RS_CHKSUM2,
342};
bellard858693c2004-03-31 18:52:07 +0000343typedef struct GDBState {
Alex Bennée8d98c442020-03-16 17:21:33 +0000344 bool init; /* have we been initialised? */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200345 CPUState *c_cpu; /* current CPU for step/continue ops */
346 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200347 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000348 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000349 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000350 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400351 int line_sum; /* running checksum */
352 int line_csum; /* checksum at the end of the packet */
Damien Hedded116e812020-03-16 17:21:53 +0000353 GByteArray *last_packet;
edgar_igl1f487ee2008-05-17 22:20:53 +0000354 int signal;
bellard41625032005-04-24 10:07:11 +0000355#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000356 int fd;
Alex Bennéefcedd922020-04-30 20:01:19 +0100357 char *socket_path;
bellard41625032005-04-24 10:07:11 +0000358 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000359#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300360 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300361 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000362#endif
Luc Michel8f468632019-01-07 15:23:45 +0000363 bool multiprocess;
364 GDBProcess *processes;
365 int process_num;
Meador Ingecdb432b2012-03-15 17:49:45 +0000366 char syscall_buf[256];
367 gdb_syscall_complete_cb current_syscall_cb;
Alex Bennée308f9e82020-03-16 17:21:35 +0000368 GString *str_buf;
Alex Bennée4a25f1b2020-03-16 17:21:36 +0000369 GByteArray *mem_buf;
Maxim Levitskyecd39d62021-11-11 12:06:02 +0100370 int sstep_flags;
371 int supported_sstep_flags;
bellard858693c2004-03-31 18:52:07 +0000372} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000373
Alex Bennée8d98c442020-03-16 17:21:33 +0000374static GDBState gdbserver_state;
375
376static void init_gdbserver_state(void)
377{
378 g_assert(!gdbserver_state.init);
379 memset(&gdbserver_state, 0, sizeof(GDBState));
380 gdbserver_state.init = true;
Alex Bennée308f9e82020-03-16 17:21:35 +0000381 gdbserver_state.str_buf = g_string_new(NULL);
Alex Bennée4a25f1b2020-03-16 17:21:36 +0000382 gdbserver_state.mem_buf = g_byte_array_sized_new(MAX_PACKET_LENGTH);
Damien Hedded116e812020-03-16 17:21:53 +0000383 gdbserver_state.last_packet = g_byte_array_sized_new(MAX_PACKET_LENGTH + 4);
Maxim Levitskyecd39d62021-11-11 12:06:02 +0100384
385 /*
386 * In replay mode all events will come from the log and can't be
387 * suppressed otherwise we would break determinism. However as those
388 * events are tied to the number of executed instructions we won't see
389 * them occurring every time we single step.
390 */
391 if (replay_mode != REPLAY_MODE_NONE) {
392 gdbserver_state.supported_sstep_flags = SSTEP_ENABLE;
Maxim Levitsky12bc5b42021-11-11 12:06:03 +0100393 } else if (kvm_enabled()) {
394 gdbserver_state.supported_sstep_flags = kvm_get_supported_sstep_flags();
Maxim Levitskyecd39d62021-11-11 12:06:02 +0100395 } else {
396 gdbserver_state.supported_sstep_flags =
397 SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER;
398 }
399
400 /*
401 * By default use no IRQs and no timers while single stepping so as to
402 * make single stepping like an ICE HW step.
403 */
Maxim Levitsky12bc5b42021-11-11 12:06:03 +0100404 gdbserver_state.sstep_flags = SSTEP_ENABLE | SSTEP_NOIRQ | SSTEP_NOTIMER;
405 gdbserver_state.sstep_flags &= gdbserver_state.supported_sstep_flags;
Maxim Levitskyecd39d62021-11-11 12:06:02 +0100406
Alex Bennée8d98c442020-03-16 17:21:33 +0000407}
408
409#ifndef CONFIG_USER_ONLY
410static void reset_gdbserver_state(void)
411{
412 g_free(gdbserver_state.processes);
413 gdbserver_state.processes = NULL;
414 gdbserver_state.process_num = 0;
415}
416#endif
aliguori880a7572008-11-18 20:30:24 +0000417
Andreas Färber5b50e792013-06-29 04:18:45 +0200418bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000419
bellard1fddef42005-04-17 19:16:13 +0000420#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000421
Alex Bennéea346af32020-03-16 17:21:34 +0000422static int get_char(void)
bellardb4608c02003-06-27 17:34:32 +0000423{
424 uint8_t ch;
425 int ret;
426
427 for(;;) {
Marc-André Lureaue7b79422022-02-19 01:34:50 +0400428 ret = recv(gdbserver_state.fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000429 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000430 if (errno == ECONNRESET)
Alex Bennéea346af32020-03-16 17:21:34 +0000431 gdbserver_state.fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200432 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000433 return -1;
434 } else if (ret == 0) {
Alex Bennéea346af32020-03-16 17:21:34 +0000435 close(gdbserver_state.fd);
436 gdbserver_state.fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000437 return -1;
438 } else {
439 break;
440 }
441 }
442 return ch;
443}
pbrook4046d912007-01-28 01:53:16 +0000444#endif
bellardb4608c02003-06-27 17:34:32 +0000445
Peter Maydellebf1b4c2022-06-10 14:32:36 +0100446/*
447 * Return true if there is a GDB currently connected to the stub
448 * and attached to a CPU
449 */
450static bool gdb_attached(void)
451{
452 return gdbserver_state.init && gdbserver_state.c_cpu;
453}
454
blueswir1654efcf2009-04-18 07:29:59 +0000455static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000456 GDB_SYS_UNKNOWN,
457 GDB_SYS_ENABLED,
458 GDB_SYS_DISABLED,
459} gdb_syscall_mode;
460
Liviu Ionescua38bb072014-12-11 12:07:48 +0000461/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000462int use_gdb_syscalls(void)
463{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100464 SemihostingTarget target = semihosting_get_target();
465 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000466 /* -semihosting-config target=native */
467 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100468 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000469 /* -semihosting-config target=gdb */
470 return true;
471 }
472
473 /* -semihosting-config target=auto */
474 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000475 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
Peter Maydellebf1b4c2022-06-10 14:32:36 +0100476 gdb_syscall_mode = gdb_attached() ? GDB_SYS_ENABLED : GDB_SYS_DISABLED;
pbrooka2d1eba2007-01-28 03:10:55 +0000477 }
478 return gdb_syscall_mode == GDB_SYS_ENABLED;
479}
480
Alex Bennéeed12f5b2021-05-20 18:43:02 +0100481static bool stub_can_reverse(void)
482{
483#ifdef CONFIG_USER_ONLY
484 return false;
485#else
486 return replay_mode == REPLAY_MODE_PLAY;
487#endif
488}
489
edgar_iglba70a622008-03-14 06:10:42 +0000490/* Resume execution. */
Alex Bennéea346af32020-03-16 17:21:34 +0000491static inline void gdb_continue(void)
edgar_iglba70a622008-03-14 06:10:42 +0000492{
Doug Gale5c9522b2017-12-02 20:30:37 -0500493
edgar_iglba70a622008-03-14 06:10:42 +0000494#ifdef CONFIG_USER_ONLY
Alex Bennéea346af32020-03-16 17:21:34 +0000495 gdbserver_state.running_state = 1;
Doug Gale5c9522b2017-12-02 20:30:37 -0500496 trace_gdbstub_op_continue();
edgar_iglba70a622008-03-14 06:10:42 +0000497#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200498 if (!runstate_needs_reset()) {
Doug Gale5c9522b2017-12-02 20:30:37 -0500499 trace_gdbstub_op_continue();
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200500 vm_start();
501 }
edgar_iglba70a622008-03-14 06:10:42 +0000502#endif
503}
504
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100505/*
506 * Resume execution, per CPU actions. For user-mode emulation it's
507 * equivalent to gdb_continue.
508 */
Alex Bennéea346af32020-03-16 17:21:34 +0000509static int gdb_continue_partial(char *newstates)
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100510{
511 CPUState *cpu;
512 int res = 0;
513#ifdef CONFIG_USER_ONLY
514 /*
515 * This is not exactly accurate, but it's an improvement compared to the
516 * previous situation, where only one CPU would be single-stepped.
517 */
518 CPU_FOREACH(cpu) {
519 if (newstates[cpu->cpu_index] == 's') {
Doug Gale5c9522b2017-12-02 20:30:37 -0500520 trace_gdbstub_op_stepping(cpu->cpu_index);
Maxim Levitskyecd39d62021-11-11 12:06:02 +0100521 cpu_single_step(cpu, gdbserver_state.sstep_flags);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100522 }
523 }
Alex Bennéea346af32020-03-16 17:21:34 +0000524 gdbserver_state.running_state = 1;
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100525#else
526 int flag = 0;
527
528 if (!runstate_needs_reset()) {
Ivan Shcherbakovd7482ff2022-03-02 17:28:33 -0800529 bool step_requested = false;
530 CPU_FOREACH(cpu) {
531 if (newstates[cpu->cpu_index] == 's') {
532 step_requested = true;
533 break;
534 }
535 }
536
537 if (vm_prepare_start(step_requested)) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100538 return 0;
539 }
540
541 CPU_FOREACH(cpu) {
542 switch (newstates[cpu->cpu_index]) {
543 case 0:
544 case 1:
545 break; /* nothing to do here */
546 case 's':
Doug Gale5c9522b2017-12-02 20:30:37 -0500547 trace_gdbstub_op_stepping(cpu->cpu_index);
Maxim Levitskyecd39d62021-11-11 12:06:02 +0100548 cpu_single_step(cpu, gdbserver_state.sstep_flags);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100549 cpu_resume(cpu);
550 flag = 1;
551 break;
552 case 'c':
Doug Gale5c9522b2017-12-02 20:30:37 -0500553 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100554 cpu_resume(cpu);
555 flag = 1;
556 break;
557 default:
558 res = -1;
559 break;
560 }
561 }
562 }
563 if (flag) {
564 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
565 }
566#endif
567 return res;
568}
569
Alex Bennéea346af32020-03-16 17:21:34 +0000570static void put_buffer(const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000571{
pbrook4046d912007-01-28 01:53:16 +0000572#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000573 int ret;
574
575 while (len > 0) {
Alex Bennéea346af32020-03-16 17:21:34 +0000576 ret = send(gdbserver_state.fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000577 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200578 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000579 return;
580 } else {
581 buf += ret;
582 len -= ret;
583 }
584 }
pbrook4046d912007-01-28 01:53:16 +0000585#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100586 /* XXX this blocks entire thread. Rewrite to use
587 * qemu_chr_fe_write and background I/O callbacks */
Alex Bennéea346af32020-03-16 17:21:34 +0000588 qemu_chr_fe_write_all(&gdbserver_state.chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000589#endif
bellardb4608c02003-06-27 17:34:32 +0000590}
591
592static inline int fromhex(int v)
593{
594 if (v >= '0' && v <= '9')
595 return v - '0';
596 else if (v >= 'A' && v <= 'F')
597 return v - 'A' + 10;
598 else if (v >= 'a' && v <= 'f')
599 return v - 'a' + 10;
600 else
601 return 0;
602}
603
604static inline int tohex(int v)
605{
606 if (v < 10)
607 return v + '0';
608 else
609 return v - 10 + 'a';
610}
611
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -0300612/* writes 2*len+1 bytes in buf */
Alex Bennée308f9e82020-03-16 17:21:35 +0000613static void memtohex(GString *buf, const uint8_t *mem, int len)
bellardb4608c02003-06-27 17:34:32 +0000614{
615 int i, c;
bellardb4608c02003-06-27 17:34:32 +0000616 for(i = 0; i < len; i++) {
617 c = mem[i];
Alex Bennée308f9e82020-03-16 17:21:35 +0000618 g_string_append_c(buf, tohex(c >> 4));
619 g_string_append_c(buf, tohex(c & 0xf));
bellardb4608c02003-06-27 17:34:32 +0000620 }
Alex Bennée308f9e82020-03-16 17:21:35 +0000621 g_string_append_c(buf, '\0');
bellardb4608c02003-06-27 17:34:32 +0000622}
623
Alex Bennée4a25f1b2020-03-16 17:21:36 +0000624static void hextomem(GByteArray *mem, const char *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000625{
626 int i;
627
628 for(i = 0; i < len; i++) {
Alex Bennée4a25f1b2020-03-16 17:21:36 +0000629 guint8 byte = fromhex(buf[0]) << 4 | fromhex(buf[1]);
630 g_byte_array_append(mem, &byte, 1);
bellardb4608c02003-06-27 17:34:32 +0000631 buf += 2;
632 }
633}
634
Doug Gale5c9522b2017-12-02 20:30:37 -0500635static void hexdump(const char *buf, int len,
636 void (*trace_fn)(size_t ofs, char const *text))
637{
638 char line_buffer[3 * 16 + 4 + 16 + 1];
639
640 size_t i;
641 for (i = 0; i < len || (i & 0xF); ++i) {
642 size_t byte_ofs = i & 15;
643
644 if (byte_ofs == 0) {
645 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
646 line_buffer[3 * 16 + 4 + 16] = 0;
647 }
648
649 size_t col_group = (i >> 2) & 3;
650 size_t hex_col = byte_ofs * 3 + col_group;
651 size_t txt_col = 3 * 16 + 4 + byte_ofs;
652
653 if (i < len) {
654 char value = buf[i];
655
656 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
657 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
658 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
659 ? value
660 : '.';
661 }
662
663 if (byte_ofs == 0xF)
664 trace_fn(i & -16, line_buffer);
665 }
666}
667
bellardb4608c02003-06-27 17:34:32 +0000668/* return -1 if error, 0 if OK */
Alex Bennéea346af32020-03-16 17:21:34 +0000669static int put_packet_binary(const char *buf, int len, bool dump)
bellardb4608c02003-06-27 17:34:32 +0000670{
pbrook56aebc82008-10-11 17:55:29 +0000671 int csum, i;
Damien Hedded116e812020-03-16 17:21:53 +0000672 uint8_t footer[3];
bellardb4608c02003-06-27 17:34:32 +0000673
Doug Gale5c9522b2017-12-02 20:30:37 -0500674 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
675 hexdump(buf, len, trace_gdbstub_io_binaryreply);
676 }
677
bellardb4608c02003-06-27 17:34:32 +0000678 for(;;) {
Damien Hedded116e812020-03-16 17:21:53 +0000679 g_byte_array_set_size(gdbserver_state.last_packet, 0);
680 g_byte_array_append(gdbserver_state.last_packet,
681 (const uint8_t *) "$", 1);
682 g_byte_array_append(gdbserver_state.last_packet,
683 (const uint8_t *) buf, len);
bellardb4608c02003-06-27 17:34:32 +0000684 csum = 0;
685 for(i = 0; i < len; i++) {
686 csum += buf[i];
687 }
Damien Hedded116e812020-03-16 17:21:53 +0000688 footer[0] = '#';
689 footer[1] = tohex((csum >> 4) & 0xf);
690 footer[2] = tohex((csum) & 0xf);
691 g_byte_array_append(gdbserver_state.last_packet, footer, 3);
bellardb4608c02003-06-27 17:34:32 +0000692
Damien Hedded116e812020-03-16 17:21:53 +0000693 put_buffer(gdbserver_state.last_packet->data,
694 gdbserver_state.last_packet->len);
bellardb4608c02003-06-27 17:34:32 +0000695
pbrook4046d912007-01-28 01:53:16 +0000696#ifdef CONFIG_USER_ONLY
Alex Bennéea346af32020-03-16 17:21:34 +0000697 i = get_char();
pbrook4046d912007-01-28 01:53:16 +0000698 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000699 return -1;
pbrook4046d912007-01-28 01:53:16 +0000700 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000701 break;
pbrook4046d912007-01-28 01:53:16 +0000702#else
703 break;
704#endif
bellardb4608c02003-06-27 17:34:32 +0000705 }
706 return 0;
707}
708
pbrook56aebc82008-10-11 17:55:29 +0000709/* return -1 if error, 0 if OK */
Alex Bennéea346af32020-03-16 17:21:34 +0000710static int put_packet(const char *buf)
pbrook56aebc82008-10-11 17:55:29 +0000711{
Doug Gale5c9522b2017-12-02 20:30:37 -0500712 trace_gdbstub_io_reply(buf);
pbrook56aebc82008-10-11 17:55:29 +0000713
Alex Bennéea346af32020-03-16 17:21:34 +0000714 return put_packet_binary(buf, strlen(buf), false);
pbrook56aebc82008-10-11 17:55:29 +0000715}
716
Alex Bennée308f9e82020-03-16 17:21:35 +0000717static void put_strbuf(void)
pbrook56aebc82008-10-11 17:55:29 +0000718{
Alex Bennée308f9e82020-03-16 17:21:35 +0000719 put_packet(gdbserver_state.str_buf->str);
720}
721
722/* Encode data using the encoding for 'x' packets. */
723static void memtox(GString *buf, const char *mem, int len)
724{
pbrook56aebc82008-10-11 17:55:29 +0000725 char c;
726
727 while (len--) {
728 c = *(mem++);
729 switch (c) {
730 case '#': case '$': case '*': case '}':
Alex Bennée308f9e82020-03-16 17:21:35 +0000731 g_string_append_c(buf, '}');
732 g_string_append_c(buf, c ^ 0x20);
pbrook56aebc82008-10-11 17:55:29 +0000733 break;
734 default:
Alex Bennée308f9e82020-03-16 17:21:35 +0000735 g_string_append_c(buf, c);
pbrook56aebc82008-10-11 17:55:29 +0000736 break;
737 }
738 }
pbrook56aebc82008-10-11 17:55:29 +0000739}
740
Alex Bennéea346af32020-03-16 17:21:34 +0000741static uint32_t gdb_get_cpu_pid(CPUState *cpu)
Luc Michel1a227332019-01-07 15:23:45 +0000742{
Luc Michel1a227332019-01-07 15:23:45 +0000743 /* TODO: In user mode, we should use the task state PID */
Peter Maydell46f5abc2019-01-29 11:46:06 +0000744 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
745 /* Return the default process' PID */
Alex Bennéea346af32020-03-16 17:21:34 +0000746 int index = gdbserver_state.process_num - 1;
747 return gdbserver_state.processes[index].pid;
Peter Maydell46f5abc2019-01-29 11:46:06 +0000748 }
749 return cpu->cluster_index + 1;
Luc Michel1a227332019-01-07 15:23:45 +0000750}
751
Alex Bennéea346af32020-03-16 17:21:34 +0000752static GDBProcess *gdb_get_process(uint32_t pid)
Luc Michel7d8c87d2019-01-07 15:23:45 +0000753{
754 int i;
755
756 if (!pid) {
757 /* 0 means any process, we take the first one */
Alex Bennéea346af32020-03-16 17:21:34 +0000758 return &gdbserver_state.processes[0];
Luc Michel7d8c87d2019-01-07 15:23:45 +0000759 }
760
Alex Bennéea346af32020-03-16 17:21:34 +0000761 for (i = 0; i < gdbserver_state.process_num; i++) {
762 if (gdbserver_state.processes[i].pid == pid) {
763 return &gdbserver_state.processes[i];
Luc Michel7d8c87d2019-01-07 15:23:45 +0000764 }
765 }
766
767 return NULL;
768}
769
Alex Bennéea346af32020-03-16 17:21:34 +0000770static GDBProcess *gdb_get_cpu_process(CPUState *cpu)
Luc Michel7d8c87d2019-01-07 15:23:45 +0000771{
Alex Bennéea346af32020-03-16 17:21:34 +0000772 return gdb_get_process(gdb_get_cpu_pid(cpu));
Luc Michel7d8c87d2019-01-07 15:23:45 +0000773}
774
775static CPUState *find_cpu(uint32_t thread_id)
776{
777 CPUState *cpu;
778
779 CPU_FOREACH(cpu) {
780 if (cpu_gdb_index(cpu) == thread_id) {
781 return cpu;
782 }
783 }
784
785 return NULL;
786}
787
Alex Bennéea346af32020-03-16 17:21:34 +0000788static CPUState *get_first_cpu_in_process(GDBProcess *process)
Luc Michele40e5202019-01-07 15:23:46 +0000789{
790 CPUState *cpu;
791
792 CPU_FOREACH(cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +0000793 if (gdb_get_cpu_pid(cpu) == process->pid) {
Luc Michele40e5202019-01-07 15:23:46 +0000794 return cpu;
795 }
796 }
797
798 return NULL;
799}
800
Alex Bennéea346af32020-03-16 17:21:34 +0000801static CPUState *gdb_next_cpu_in_process(CPUState *cpu)
Luc Michele40e5202019-01-07 15:23:46 +0000802{
Alex Bennéea346af32020-03-16 17:21:34 +0000803 uint32_t pid = gdb_get_cpu_pid(cpu);
Luc Michele40e5202019-01-07 15:23:46 +0000804 cpu = CPU_NEXT(cpu);
805
806 while (cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +0000807 if (gdb_get_cpu_pid(cpu) == pid) {
Luc Michele40e5202019-01-07 15:23:46 +0000808 break;
809 }
810
811 cpu = CPU_NEXT(cpu);
812 }
813
814 return cpu;
815}
816
Luc Michele40e5202019-01-07 15:23:46 +0000817/* Return the cpu following @cpu, while ignoring unattached processes. */
Alex Bennéea346af32020-03-16 17:21:34 +0000818static CPUState *gdb_next_attached_cpu(CPUState *cpu)
Luc Michele40e5202019-01-07 15:23:46 +0000819{
820 cpu = CPU_NEXT(cpu);
821
822 while (cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +0000823 if (gdb_get_cpu_process(cpu)->attached) {
Luc Michele40e5202019-01-07 15:23:46 +0000824 break;
825 }
826
827 cpu = CPU_NEXT(cpu);
828 }
829
830 return cpu;
831}
832
833/* Return the first attached cpu */
Alex Bennéea346af32020-03-16 17:21:34 +0000834static CPUState *gdb_first_attached_cpu(void)
Luc Michele40e5202019-01-07 15:23:46 +0000835{
836 CPUState *cpu = first_cpu;
Alex Bennéea346af32020-03-16 17:21:34 +0000837 GDBProcess *process = gdb_get_cpu_process(cpu);
Luc Michele40e5202019-01-07 15:23:46 +0000838
839 if (!process->attached) {
Alex Bennéea346af32020-03-16 17:21:34 +0000840 return gdb_next_attached_cpu(cpu);
Luc Michele40e5202019-01-07 15:23:46 +0000841 }
842
843 return cpu;
844}
845
Alex Bennéea346af32020-03-16 17:21:34 +0000846static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid)
Luc Michelab65eed2019-01-29 11:46:03 +0000847{
848 GDBProcess *process;
849 CPUState *cpu;
850
851 if (!pid && !tid) {
852 /* 0 means any process/thread, we take the first attached one */
Alex Bennéea346af32020-03-16 17:21:34 +0000853 return gdb_first_attached_cpu();
Luc Michelab65eed2019-01-29 11:46:03 +0000854 } else if (pid && !tid) {
855 /* any thread in a specific process */
Alex Bennéea346af32020-03-16 17:21:34 +0000856 process = gdb_get_process(pid);
Luc Michelab65eed2019-01-29 11:46:03 +0000857
858 if (process == NULL) {
859 return NULL;
860 }
861
862 if (!process->attached) {
863 return NULL;
864 }
865
Alex Bennéea346af32020-03-16 17:21:34 +0000866 return get_first_cpu_in_process(process);
Luc Michelab65eed2019-01-29 11:46:03 +0000867 } else {
868 /* a specific thread */
869 cpu = find_cpu(tid);
870
871 if (cpu == NULL) {
872 return NULL;
873 }
874
Alex Bennéea346af32020-03-16 17:21:34 +0000875 process = gdb_get_cpu_process(cpu);
Luc Michelab65eed2019-01-29 11:46:03 +0000876
877 if (pid && process->pid != pid) {
878 return NULL;
879 }
880
881 if (!process->attached) {
882 return NULL;
883 }
884
885 return cpu;
886 }
887}
888
Alex Bennéea346af32020-03-16 17:21:34 +0000889static const char *get_feature_xml(const char *p, const char **newp,
890 GDBProcess *process)
pbrook56aebc82008-10-11 17:55:29 +0000891{
pbrook56aebc82008-10-11 17:55:29 +0000892 size_t len;
893 int i;
894 const char *name;
Alex Bennéea346af32020-03-16 17:21:34 +0000895 CPUState *cpu = get_first_cpu_in_process(process);
Luc Michelc145eea2019-01-07 15:23:46 +0000896 CPUClass *cc = CPU_GET_CLASS(cpu);
pbrook56aebc82008-10-11 17:55:29 +0000897
898 len = 0;
899 while (p[len] && p[len] != ':')
900 len++;
901 *newp = p + len;
902
903 name = NULL;
904 if (strncmp(p, "target.xml", len) == 0) {
Luc Michelc145eea2019-01-07 15:23:46 +0000905 char *buf = process->target_xml;
906 const size_t buf_sz = sizeof(process->target_xml);
pbrook56aebc82008-10-11 17:55:29 +0000907
Luc Michelc145eea2019-01-07 15:23:46 +0000908 /* Generate the XML description for this CPU. */
909 if (!buf[0]) {
910 GDBRegisterState *r;
911
912 pstrcat(buf, buf_sz,
David Hildenbrandb3820e62015-12-03 13:14:41 +0100913 "<?xml version=\"1.0\"?>"
914 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
915 "<target>");
916 if (cc->gdb_arch_name) {
917 gchar *arch = cc->gdb_arch_name(cpu);
Luc Michelc145eea2019-01-07 15:23:46 +0000918 pstrcat(buf, buf_sz, "<architecture>");
919 pstrcat(buf, buf_sz, arch);
920 pstrcat(buf, buf_sz, "</architecture>");
David Hildenbrandb3820e62015-12-03 13:14:41 +0100921 g_free(arch);
922 }
Luc Michelc145eea2019-01-07 15:23:46 +0000923 pstrcat(buf, buf_sz, "<xi:include href=\"");
924 pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
925 pstrcat(buf, buf_sz, "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200926 for (r = cpu->gdb_regs; r; r = r->next) {
Luc Michelc145eea2019-01-07 15:23:46 +0000927 pstrcat(buf, buf_sz, "<xi:include href=\"");
928 pstrcat(buf, buf_sz, r->xml);
929 pstrcat(buf, buf_sz, "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000930 }
Luc Michelc145eea2019-01-07 15:23:46 +0000931 pstrcat(buf, buf_sz, "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000932 }
Luc Michelc145eea2019-01-07 15:23:46 +0000933 return buf;
pbrook56aebc82008-10-11 17:55:29 +0000934 }
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100935 if (cc->gdb_get_dynamic_xml) {
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100936 char *xmlname = g_strndup(p, len);
937 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
938
939 g_free(xmlname);
940 if (xml) {
941 return xml;
942 }
943 }
pbrook56aebc82008-10-11 17:55:29 +0000944 for (i = 0; ; i++) {
945 name = xml_builtin[i][0];
946 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
947 break;
948 }
949 return name ? xml_builtin[i][1] : NULL;
950}
pbrook56aebc82008-10-11 17:55:29 +0000951
Alex Bennéea010bdb2020-03-16 17:21:41 +0000952static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000953{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200954 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200955 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000956 GDBRegisterState *r;
957
Andreas Färbera0e372f2013-06-28 23:18:47 +0200958 if (reg < cc->gdb_num_core_regs) {
Alex Bennéea010bdb2020-03-16 17:21:41 +0000959 return cc->gdb_read_register(cpu, buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200960 }
pbrook56aebc82008-10-11 17:55:29 +0000961
Andreas Färbereac8b352013-06-28 21:11:37 +0200962 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000963 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
Alex Bennéea010bdb2020-03-16 17:21:41 +0000964 return r->get_reg(env, buf, reg - r->base_reg);
pbrook56aebc82008-10-11 17:55:29 +0000965 }
966 }
967 return 0;
968}
969
Andreas Färber385b9f02013-06-27 18:25:36 +0200970static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000971{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200972 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200973 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000974 GDBRegisterState *r;
975
Andreas Färbera0e372f2013-06-28 23:18:47 +0200976 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200977 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200978 }
pbrook56aebc82008-10-11 17:55:29 +0000979
Andreas Färbereac8b352013-06-28 21:11:37 +0200980 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000981 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
982 return r->set_reg(env, mem_buf, reg - r->base_reg);
983 }
984 }
985 return 0;
986}
987
988/* Register a supplemental set of CPU registers. If g_pos is nonzero it
989 specifies the first register number and these registers are included in
990 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
991 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
992 */
993
Andreas Färber22169d42013-06-28 21:27:39 +0200994void gdb_register_coprocessor(CPUState *cpu,
Alex Bennéea010bdb2020-03-16 17:21:41 +0000995 gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
Andreas Färber22169d42013-06-28 21:27:39 +0200996 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000997{
998 GDBRegisterState *s;
999 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +00001000
Andreas Färbereac8b352013-06-28 21:11:37 +02001001 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +00001002 while (*p) {
1003 /* Check for duplicates. */
1004 if (strcmp((*p)->xml, xml) == 0)
1005 return;
1006 p = &(*p)->next;
1007 }
Stefan Weil9643c252011-10-18 22:25:38 +02001008
1009 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +02001010 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +02001011 s->num_regs = num_regs;
1012 s->get_reg = get_reg;
1013 s->set_reg = set_reg;
1014 s->xml = xml;
1015
pbrook56aebc82008-10-11 17:55:29 +00001016 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +02001017 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +00001018 *p = s;
1019 if (g_pos) {
1020 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001021 error_report("Error: Bad gdb register numbering for '%s', "
1022 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +02001023 } else {
1024 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +00001025 }
1026 }
1027}
1028
aliguoria1d1bb32008-11-18 20:07:32 +00001029#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +01001030/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
1031static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
1032{
1033 static const int xlat[] = {
1034 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
1035 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
1036 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
1037 };
1038
1039 CPUClass *cc = CPU_GET_CLASS(cpu);
1040 int cputype = xlat[gdbtype];
1041
1042 if (cc->gdb_stop_before_watchpoint) {
1043 cputype |= BP_STOP_BEFORE_ACCESS;
1044 }
1045 return cputype;
1046}
aliguoria1d1bb32008-11-18 20:07:32 +00001047#endif
1048
Jon Doron77f6ce52019-05-29 09:41:35 +03001049static int gdb_breakpoint_insert(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +00001050{
Andreas Färber182735e2013-05-29 22:29:20 +02001051 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001052 int err = 0;
1053
Andreas Färber62278812013-06-27 17:12:06 +02001054 if (kvm_enabled()) {
Alex Bennée8d98c442020-03-16 17:21:33 +00001055 return kvm_insert_breakpoint(gdbserver_state.c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001056 }
aliguorie22a25c2009-03-12 20:12:48 +00001057
aliguoria1d1bb32008-11-18 20:07:32 +00001058 switch (type) {
1059 case GDB_BREAKPOINT_SW:
1060 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001061 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001062 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
1063 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001064 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001065 }
aliguori880a7572008-11-18 20:30:24 +00001066 }
1067 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001068#ifndef CONFIG_USER_ONLY
1069 case GDB_WATCHPOINT_WRITE:
1070 case GDB_WATCHPOINT_READ:
1071 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001072 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001073 err = cpu_watchpoint_insert(cpu, addr, len,
1074 xlat_gdb_type(cpu, type), NULL);
1075 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001076 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +01001077 }
aliguori880a7572008-11-18 20:30:24 +00001078 }
1079 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001080#endif
1081 default:
1082 return -ENOSYS;
1083 }
1084}
1085
Jon Doron77f6ce52019-05-29 09:41:35 +03001086static int gdb_breakpoint_remove(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +00001087{
Andreas Färber182735e2013-05-29 22:29:20 +02001088 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001089 int err = 0;
1090
Andreas Färber62278812013-06-27 17:12:06 +02001091 if (kvm_enabled()) {
Alex Bennée8d98c442020-03-16 17:21:33 +00001092 return kvm_remove_breakpoint(gdbserver_state.c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001093 }
aliguorie22a25c2009-03-12 20:12:48 +00001094
aliguoria1d1bb32008-11-18 20:07:32 +00001095 switch (type) {
1096 case GDB_BREAKPOINT_SW:
1097 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001098 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001099 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
1100 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001101 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001102 }
aliguori880a7572008-11-18 20:30:24 +00001103 }
1104 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001105#ifndef CONFIG_USER_ONLY
1106 case GDB_WATCHPOINT_WRITE:
1107 case GDB_WATCHPOINT_READ:
1108 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001109 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001110 err = cpu_watchpoint_remove(cpu, addr, len,
1111 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +00001112 if (err)
1113 break;
1114 }
1115 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001116#endif
1117 default:
1118 return -ENOSYS;
1119 }
1120}
1121
Luc Michel546f3c62019-01-07 15:23:46 +00001122static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu)
1123{
1124 cpu_breakpoint_remove_all(cpu, BP_GDB);
1125#ifndef CONFIG_USER_ONLY
1126 cpu_watchpoint_remove_all(cpu, BP_GDB);
1127#endif
1128}
1129
Alex Bennéea346af32020-03-16 17:21:34 +00001130static void gdb_process_breakpoint_remove_all(GDBProcess *p)
Luc Michel546f3c62019-01-07 15:23:46 +00001131{
Alex Bennéea346af32020-03-16 17:21:34 +00001132 CPUState *cpu = get_first_cpu_in_process(p);
Luc Michel546f3c62019-01-07 15:23:46 +00001133
1134 while (cpu) {
1135 gdb_cpu_breakpoint_remove_all(cpu);
Alex Bennéea346af32020-03-16 17:21:34 +00001136 cpu = gdb_next_cpu_in_process(cpu);
Luc Michel546f3c62019-01-07 15:23:46 +00001137 }
1138}
1139
aliguori880a7572008-11-18 20:30:24 +00001140static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +00001141{
Andreas Färber182735e2013-05-29 22:29:20 +02001142 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001143
aliguorie22a25c2009-03-12 20:12:48 +00001144 if (kvm_enabled()) {
Alex Bennée8d98c442020-03-16 17:21:33 +00001145 kvm_remove_all_breakpoints(gdbserver_state.c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +00001146 return;
1147 }
1148
Andreas Färberbdc44642013-06-24 23:50:24 +02001149 CPU_FOREACH(cpu) {
Luc Michel546f3c62019-01-07 15:23:46 +00001150 gdb_cpu_breakpoint_remove_all(cpu);
aliguori880a7572008-11-18 20:30:24 +00001151 }
aliguoria1d1bb32008-11-18 20:07:32 +00001152}
1153
Alex Bennéea346af32020-03-16 17:21:34 +00001154static void gdb_set_cpu_pc(target_ulong pc)
aurel32fab9d282009-04-08 21:29:37 +00001155{
Alex Bennéea346af32020-03-16 17:21:34 +00001156 CPUState *cpu = gdbserver_state.c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +02001157
1158 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -07001159 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +00001160}
1161
Alex Bennée308f9e82020-03-16 17:21:35 +00001162static void gdb_append_thread_id(CPUState *cpu, GString *buf)
Luc Michel1a227332019-01-07 15:23:45 +00001163{
Alex Bennéea346af32020-03-16 17:21:34 +00001164 if (gdbserver_state.multiprocess) {
Alex Bennée308f9e82020-03-16 17:21:35 +00001165 g_string_append_printf(buf, "p%02x.%02x",
1166 gdb_get_cpu_pid(cpu), cpu_gdb_index(cpu));
Luc Michel1a227332019-01-07 15:23:45 +00001167 } else {
Alex Bennée308f9e82020-03-16 17:21:35 +00001168 g_string_append_printf(buf, "%02x", cpu_gdb_index(cpu));
Luc Michel1a227332019-01-07 15:23:45 +00001169 }
Luc Michel1a227332019-01-07 15:23:45 +00001170}
1171
Luc Michel7d8c87d2019-01-07 15:23:45 +00001172typedef enum GDBThreadIdKind {
1173 GDB_ONE_THREAD = 0,
1174 GDB_ALL_THREADS, /* One process, all threads */
1175 GDB_ALL_PROCESSES,
1176 GDB_READ_THREAD_ERR
1177} GDBThreadIdKind;
1178
1179static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1180 uint32_t *pid, uint32_t *tid)
1181{
1182 unsigned long p, t;
1183 int ret;
1184
1185 if (*buf == 'p') {
1186 buf++;
1187 ret = qemu_strtoul(buf, &buf, 16, &p);
1188
1189 if (ret) {
1190 return GDB_READ_THREAD_ERR;
1191 }
1192
1193 /* Skip '.' */
1194 buf++;
1195 } else {
1196 p = 1;
1197 }
1198
1199 ret = qemu_strtoul(buf, &buf, 16, &t);
1200
1201 if (ret) {
1202 return GDB_READ_THREAD_ERR;
1203 }
1204
1205 *end_buf = buf;
1206
1207 if (p == -1) {
1208 return GDB_ALL_PROCESSES;
1209 }
1210
1211 if (pid) {
1212 *pid = p;
1213 }
1214
1215 if (t == -1) {
1216 return GDB_ALL_THREADS;
1217 }
1218
1219 if (tid) {
1220 *tid = t;
1221 }
1222
1223 return GDB_ONE_THREAD;
1224}
1225
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001226/**
1227 * gdb_handle_vcont - Parses and handles a vCont packet.
1228 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1229 * a format error, 0 on success.
1230 */
Alex Bennéea346af32020-03-16 17:21:34 +00001231static int gdb_handle_vcont(const char *p)
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001232{
Luc Michele40e5202019-01-07 15:23:46 +00001233 int res, signal = 0;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001234 char cur_action;
1235 char *newstates;
1236 unsigned long tmp;
Luc Michele40e5202019-01-07 15:23:46 +00001237 uint32_t pid, tid;
1238 GDBProcess *process;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001239 CPUState *cpu;
Luc Michelc99ef792019-03-26 12:53:26 +00001240 GDBThreadIdKind kind;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001241#ifdef CONFIG_USER_ONLY
1242 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
1243
1244 CPU_FOREACH(cpu) {
1245 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1246 }
Like Xu5cc87672019-05-19 04:54:21 +08001247#else
1248 MachineState *ms = MACHINE(qdev_get_machine());
1249 unsigned int max_cpus = ms->smp.max_cpus;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001250#endif
1251 /* uninitialised CPUs stay 0 */
1252 newstates = g_new0(char, max_cpus);
1253
1254 /* mark valid CPUs with 1 */
1255 CPU_FOREACH(cpu) {
1256 newstates[cpu->cpu_index] = 1;
1257 }
1258
1259 /*
1260 * res keeps track of what error we are returning, with -ENOTSUP meaning
1261 * that the command is unknown or unsupported, thus returning an empty
1262 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1263 * or incorrect parameters passed.
1264 */
1265 res = 0;
1266 while (*p) {
1267 if (*p++ != ';') {
1268 res = -ENOTSUP;
1269 goto out;
1270 }
1271
1272 cur_action = *p++;
1273 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +01001274 cur_action = qemu_tolower(cur_action);
Peter Maydell3ddd9032020-11-21 21:03:42 +00001275 res = qemu_strtoul(p, &p, 16, &tmp);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001276 if (res) {
1277 goto out;
1278 }
1279 signal = gdb_signal_to_target(tmp);
1280 } else if (cur_action != 'c' && cur_action != 's') {
1281 /* unknown/invalid/unsupported command */
1282 res = -ENOTSUP;
1283 goto out;
1284 }
Luc Michele40e5202019-01-07 15:23:46 +00001285
Luc Michelc99ef792019-03-26 12:53:26 +00001286 if (*p == '\0' || *p == ';') {
1287 /*
1288 * No thread specifier, action is on "all threads". The
1289 * specification is unclear regarding the process to act on. We
1290 * choose all processes.
1291 */
1292 kind = GDB_ALL_PROCESSES;
1293 } else if (*p++ == ':') {
1294 kind = read_thread_id(p, &p, &pid, &tid);
1295 } else {
Luc Michele40e5202019-01-07 15:23:46 +00001296 res = -ENOTSUP;
1297 goto out;
1298 }
1299
Luc Michelc99ef792019-03-26 12:53:26 +00001300 switch (kind) {
Luc Michele40e5202019-01-07 15:23:46 +00001301 case GDB_READ_THREAD_ERR:
1302 res = -EINVAL;
1303 goto out;
1304
1305 case GDB_ALL_PROCESSES:
Alex Bennéea346af32020-03-16 17:21:34 +00001306 cpu = gdb_first_attached_cpu();
Luc Michele40e5202019-01-07 15:23:46 +00001307 while (cpu) {
1308 if (newstates[cpu->cpu_index] == 1) {
1309 newstates[cpu->cpu_index] = cur_action;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001310 }
Luc Michele40e5202019-01-07 15:23:46 +00001311
Alex Bennéea346af32020-03-16 17:21:34 +00001312 cpu = gdb_next_attached_cpu(cpu);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001313 }
Luc Michele40e5202019-01-07 15:23:46 +00001314 break;
1315
1316 case GDB_ALL_THREADS:
Alex Bennéea346af32020-03-16 17:21:34 +00001317 process = gdb_get_process(pid);
Luc Michele40e5202019-01-07 15:23:46 +00001318
1319 if (!process->attached) {
1320 res = -EINVAL;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001321 goto out;
1322 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001323
Alex Bennéea346af32020-03-16 17:21:34 +00001324 cpu = get_first_cpu_in_process(process);
Luc Michele40e5202019-01-07 15:23:46 +00001325 while (cpu) {
1326 if (newstates[cpu->cpu_index] == 1) {
1327 newstates[cpu->cpu_index] = cur_action;
1328 }
1329
Alex Bennéea346af32020-03-16 17:21:34 +00001330 cpu = gdb_next_cpu_in_process(cpu);
Luc Michele40e5202019-01-07 15:23:46 +00001331 }
1332 break;
1333
1334 case GDB_ONE_THREAD:
Alex Bennéea346af32020-03-16 17:21:34 +00001335 cpu = gdb_get_cpu(pid, tid);
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001336
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001337 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001338 if (!cpu) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001339 res = -EINVAL;
1340 goto out;
1341 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001342
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001343 /* only use if no previous match occourred */
1344 if (newstates[cpu->cpu_index] == 1) {
1345 newstates[cpu->cpu_index] = cur_action;
1346 }
Luc Michele40e5202019-01-07 15:23:46 +00001347 break;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001348 }
1349 }
Alex Bennéea346af32020-03-16 17:21:34 +00001350 gdbserver_state.signal = signal;
1351 gdb_continue_partial(newstates);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001352
1353out:
1354 g_free(newstates);
1355
1356 return res;
1357}
1358
Jon Dorond14055d2019-05-29 09:41:29 +03001359typedef union GdbCmdVariant {
1360 const char *data;
1361 uint8_t opcode;
1362 unsigned long val_ul;
1363 unsigned long long val_ull;
1364 struct {
1365 GDBThreadIdKind kind;
1366 uint32_t pid;
1367 uint32_t tid;
1368 } thread_id;
1369} GdbCmdVariant;
1370
Alex Bennée26a16182021-05-25 09:24:14 +01001371#define get_param(p, i) (&g_array_index(p, GdbCmdVariant, i))
1372
Jon Dorond14055d2019-05-29 09:41:29 +03001373static const char *cmd_next_param(const char *param, const char delimiter)
1374{
1375 static const char all_delimiters[] = ",;:=";
1376 char curr_delimiters[2] = {0};
1377 const char *delimiters;
1378
1379 if (delimiter == '?') {
1380 delimiters = all_delimiters;
1381 } else if (delimiter == '0') {
1382 return strchr(param, '\0');
1383 } else if (delimiter == '.' && *param) {
1384 return param + 1;
1385 } else {
1386 curr_delimiters[0] = delimiter;
1387 delimiters = curr_delimiters;
1388 }
1389
1390 param += strcspn(param, delimiters);
1391 if (*param) {
1392 param++;
1393 }
1394 return param;
1395}
1396
1397static int cmd_parse_params(const char *data, const char *schema,
Alex Bennée26a16182021-05-25 09:24:14 +01001398 GArray *params)
Jon Dorond14055d2019-05-29 09:41:29 +03001399{
Jon Dorond14055d2019-05-29 09:41:29 +03001400 const char *curr_schema, *curr_data;
1401
Alex Bennée26a16182021-05-25 09:24:14 +01001402 g_assert(schema);
1403 g_assert(params->len == 0);
Jon Dorond14055d2019-05-29 09:41:29 +03001404
1405 curr_schema = schema;
Jon Dorond14055d2019-05-29 09:41:29 +03001406 curr_data = data;
1407 while (curr_schema[0] && curr_schema[1] && *curr_data) {
Alex Bennée26a16182021-05-25 09:24:14 +01001408 GdbCmdVariant this_param;
1409
Jon Dorond14055d2019-05-29 09:41:29 +03001410 switch (curr_schema[0]) {
1411 case 'l':
1412 if (qemu_strtoul(curr_data, &curr_data, 16,
Alex Bennée26a16182021-05-25 09:24:14 +01001413 &this_param.val_ul)) {
Jon Dorond14055d2019-05-29 09:41:29 +03001414 return -EINVAL;
1415 }
Jon Dorond14055d2019-05-29 09:41:29 +03001416 curr_data = cmd_next_param(curr_data, curr_schema[1]);
Alex Bennée26a16182021-05-25 09:24:14 +01001417 g_array_append_val(params, this_param);
Jon Dorond14055d2019-05-29 09:41:29 +03001418 break;
1419 case 'L':
1420 if (qemu_strtou64(curr_data, &curr_data, 16,
Alex Bennée26a16182021-05-25 09:24:14 +01001421 (uint64_t *)&this_param.val_ull)) {
Jon Dorond14055d2019-05-29 09:41:29 +03001422 return -EINVAL;
1423 }
Jon Dorond14055d2019-05-29 09:41:29 +03001424 curr_data = cmd_next_param(curr_data, curr_schema[1]);
Alex Bennée26a16182021-05-25 09:24:14 +01001425 g_array_append_val(params, this_param);
Jon Dorond14055d2019-05-29 09:41:29 +03001426 break;
1427 case 's':
Alex Bennée26a16182021-05-25 09:24:14 +01001428 this_param.data = curr_data;
Jon Dorond14055d2019-05-29 09:41:29 +03001429 curr_data = cmd_next_param(curr_data, curr_schema[1]);
Alex Bennée26a16182021-05-25 09:24:14 +01001430 g_array_append_val(params, this_param);
Jon Dorond14055d2019-05-29 09:41:29 +03001431 break;
1432 case 'o':
Alex Bennée26a16182021-05-25 09:24:14 +01001433 this_param.opcode = *(uint8_t *)curr_data;
Jon Dorond14055d2019-05-29 09:41:29 +03001434 curr_data = cmd_next_param(curr_data, curr_schema[1]);
Alex Bennée26a16182021-05-25 09:24:14 +01001435 g_array_append_val(params, this_param);
Jon Dorond14055d2019-05-29 09:41:29 +03001436 break;
1437 case 't':
Alex Bennée26a16182021-05-25 09:24:14 +01001438 this_param.thread_id.kind =
Jon Dorond14055d2019-05-29 09:41:29 +03001439 read_thread_id(curr_data, &curr_data,
Alex Bennée26a16182021-05-25 09:24:14 +01001440 &this_param.thread_id.pid,
1441 &this_param.thread_id.tid);
Jon Dorond14055d2019-05-29 09:41:29 +03001442 curr_data = cmd_next_param(curr_data, curr_schema[1]);
Alex Bennée26a16182021-05-25 09:24:14 +01001443 g_array_append_val(params, this_param);
Jon Dorond14055d2019-05-29 09:41:29 +03001444 break;
1445 case '?':
1446 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1447 break;
1448 default:
1449 return -EINVAL;
1450 }
1451 curr_schema += 2;
1452 }
1453
Jon Dorond14055d2019-05-29 09:41:29 +03001454 return 0;
1455}
1456
Alex Bennée26a16182021-05-25 09:24:14 +01001457typedef void (*GdbCmdHandler)(GArray *params, void *user_ctx);
Jon Dorond14055d2019-05-29 09:41:29 +03001458
1459/*
1460 * cmd_startswith -> cmd is compared using startswith
1461 *
1462 *
1463 * schema definitions:
1464 * Each schema parameter entry consists of 2 chars,
1465 * the first char represents the parameter type handling
1466 * the second char represents the delimiter for the next parameter
1467 *
1468 * Currently supported schema types:
1469 * 'l' -> unsigned long (stored in .val_ul)
1470 * 'L' -> unsigned long long (stored in .val_ull)
1471 * 's' -> string (stored in .data)
1472 * 'o' -> single char (stored in .opcode)
1473 * 't' -> thread id (stored in .thread_id)
1474 * '?' -> skip according to delimiter
1475 *
1476 * Currently supported delimiters:
1477 * '?' -> Stop at any delimiter (",;:=\0")
1478 * '0' -> Stop at "\0"
1479 * '.' -> Skip 1 char unless reached "\0"
1480 * Any other value is treated as the delimiter value itself
1481 */
1482typedef struct GdbCmdParseEntry {
1483 GdbCmdHandler handler;
1484 const char *cmd;
1485 bool cmd_startswith;
1486 const char *schema;
1487} GdbCmdParseEntry;
1488
1489static inline int startswith(const char *string, const char *pattern)
1490{
1491 return !strncmp(string, pattern, strlen(pattern));
1492}
1493
Alex Bennéea346af32020-03-16 17:21:34 +00001494static int process_string_cmd(void *user_ctx, const char *data,
Jon Dorond14055d2019-05-29 09:41:29 +03001495 const GdbCmdParseEntry *cmds, int num_cmds)
1496{
Alex Bennée26a16182021-05-25 09:24:14 +01001497 int i;
1498 g_autoptr(GArray) params = g_array_new(false, true, sizeof(GdbCmdVariant));
Jon Dorond14055d2019-05-29 09:41:29 +03001499
1500 if (!cmds) {
1501 return -1;
1502 }
1503
1504 for (i = 0; i < num_cmds; i++) {
1505 const GdbCmdParseEntry *cmd = &cmds[i];
1506 g_assert(cmd->handler && cmd->cmd);
1507
1508 if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
1509 (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
1510 continue;
1511 }
1512
1513 if (cmd->schema) {
Alex Bennée26a16182021-05-25 09:24:14 +01001514 if (cmd_parse_params(&data[strlen(cmd->cmd)],
1515 cmd->schema, params)) {
1516 return -1;
Jon Dorond14055d2019-05-29 09:41:29 +03001517 }
Jon Dorond14055d2019-05-29 09:41:29 +03001518 }
1519
Alex Bennée26a16182021-05-25 09:24:14 +01001520 cmd->handler(params, user_ctx);
Jon Dorond14055d2019-05-29 09:41:29 +03001521 return 0;
1522 }
1523
1524 return -1;
1525}
1526
Alex Bennéea346af32020-03-16 17:21:34 +00001527static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd)
Jon Doron3e2c1262019-05-29 09:41:30 +03001528{
1529 if (!data) {
1530 return;
1531 }
1532
Alex Bennée308f9e82020-03-16 17:21:35 +00001533 g_string_set_size(gdbserver_state.str_buf, 0);
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001534 g_byte_array_set_size(gdbserver_state.mem_buf, 0);
Alex Bennée308f9e82020-03-16 17:21:35 +00001535
Jon Doron3e2c1262019-05-29 09:41:30 +03001536 /* In case there was an error during the command parsing we must
1537 * send a NULL packet to indicate the command is not supported */
Alex Bennéea346af32020-03-16 17:21:34 +00001538 if (process_string_cmd(NULL, data, cmd, 1)) {
1539 put_packet("");
Jon Doron3e2c1262019-05-29 09:41:30 +03001540 }
1541}
1542
Alex Bennée26a16182021-05-25 09:24:14 +01001543static void handle_detach(GArray *params, void *user_ctx)
Jon Doron3e2c1262019-05-29 09:41:30 +03001544{
1545 GDBProcess *process;
Jon Doron3e2c1262019-05-29 09:41:30 +03001546 uint32_t pid = 1;
1547
Alex Bennéea346af32020-03-16 17:21:34 +00001548 if (gdbserver_state.multiprocess) {
Alex Bennée26a16182021-05-25 09:24:14 +01001549 if (!params->len) {
Alex Bennéea346af32020-03-16 17:21:34 +00001550 put_packet("E22");
Jon Doron3e2c1262019-05-29 09:41:30 +03001551 return;
1552 }
1553
Alex Bennée26a16182021-05-25 09:24:14 +01001554 pid = get_param(params, 0)->val_ul;
Jon Doron3e2c1262019-05-29 09:41:30 +03001555 }
1556
Alex Bennéea346af32020-03-16 17:21:34 +00001557 process = gdb_get_process(pid);
1558 gdb_process_breakpoint_remove_all(process);
Jon Doron3e2c1262019-05-29 09:41:30 +03001559 process->attached = false;
1560
Alex Bennéea346af32020-03-16 17:21:34 +00001561 if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) {
1562 gdbserver_state.c_cpu = gdb_first_attached_cpu();
Jon Doron3e2c1262019-05-29 09:41:30 +03001563 }
1564
Alex Bennéea346af32020-03-16 17:21:34 +00001565 if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) {
1566 gdbserver_state.g_cpu = gdb_first_attached_cpu();
Jon Doron3e2c1262019-05-29 09:41:30 +03001567 }
1568
Alex Bennéea346af32020-03-16 17:21:34 +00001569 if (!gdbserver_state.c_cpu) {
Jon Doron3e2c1262019-05-29 09:41:30 +03001570 /* No more process attached */
1571 gdb_syscall_mode = GDB_SYS_DISABLED;
Alex Bennéea346af32020-03-16 17:21:34 +00001572 gdb_continue();
Jon Doron3e2c1262019-05-29 09:41:30 +03001573 }
Alex Bennéea346af32020-03-16 17:21:34 +00001574 put_packet("OK");
Jon Doron3e2c1262019-05-29 09:41:30 +03001575}
1576
Alex Bennée26a16182021-05-25 09:24:14 +01001577static void handle_thread_alive(GArray *params, void *user_ctx)
Jon Doron44ffded2019-05-29 09:41:31 +03001578{
1579 CPUState *cpu;
1580
Alex Bennée26a16182021-05-25 09:24:14 +01001581 if (!params->len) {
Alex Bennéea346af32020-03-16 17:21:34 +00001582 put_packet("E22");
Jon Doron44ffded2019-05-29 09:41:31 +03001583 return;
1584 }
1585
Alex Bennée26a16182021-05-25 09:24:14 +01001586 if (get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
Alex Bennéea346af32020-03-16 17:21:34 +00001587 put_packet("E22");
Jon Doron44ffded2019-05-29 09:41:31 +03001588 return;
1589 }
1590
Alex Bennée26a16182021-05-25 09:24:14 +01001591 cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
1592 get_param(params, 0)->thread_id.tid);
Jon Doron44ffded2019-05-29 09:41:31 +03001593 if (!cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +00001594 put_packet("E22");
Jon Doron44ffded2019-05-29 09:41:31 +03001595 return;
1596 }
1597
Alex Bennéea346af32020-03-16 17:21:34 +00001598 put_packet("OK");
Jon Doron44ffded2019-05-29 09:41:31 +03001599}
1600
Alex Bennée26a16182021-05-25 09:24:14 +01001601static void handle_continue(GArray *params, void *user_ctx)
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001602{
Alex Bennée26a16182021-05-25 09:24:14 +01001603 if (params->len) {
1604 gdb_set_cpu_pc(get_param(params, 0)->val_ull);
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001605 }
1606
Alex Bennéea346af32020-03-16 17:21:34 +00001607 gdbserver_state.signal = 0;
1608 gdb_continue();
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001609}
1610
Alex Bennée26a16182021-05-25 09:24:14 +01001611static void handle_cont_with_sig(GArray *params, void *user_ctx)
Jon Doronccc47d52019-05-29 09:41:33 +03001612{
1613 unsigned long signal = 0;
1614
1615 /*
1616 * Note: C sig;[addr] is currently unsupported and we simply
1617 * omit the addr parameter
1618 */
Alex Bennée26a16182021-05-25 09:24:14 +01001619 if (params->len) {
1620 signal = get_param(params, 0)->val_ul;
Jon Doronccc47d52019-05-29 09:41:33 +03001621 }
1622
Alex Bennéea346af32020-03-16 17:21:34 +00001623 gdbserver_state.signal = gdb_signal_to_target(signal);
1624 if (gdbserver_state.signal == -1) {
1625 gdbserver_state.signal = 0;
Jon Doronccc47d52019-05-29 09:41:33 +03001626 }
Alex Bennéea346af32020-03-16 17:21:34 +00001627 gdb_continue();
Jon Doronccc47d52019-05-29 09:41:33 +03001628}
1629
Alex Bennée26a16182021-05-25 09:24:14 +01001630static void handle_set_thread(GArray *params, void *user_ctx)
Jon Doron3a9651d2019-05-29 09:41:34 +03001631{
1632 CPUState *cpu;
1633
Alex Bennée26a16182021-05-25 09:24:14 +01001634 if (params->len != 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001635 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001636 return;
1637 }
1638
Alex Bennée26a16182021-05-25 09:24:14 +01001639 if (get_param(params, 1)->thread_id.kind == GDB_READ_THREAD_ERR) {
Alex Bennéea346af32020-03-16 17:21:34 +00001640 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001641 return;
1642 }
1643
Alex Bennée26a16182021-05-25 09:24:14 +01001644 if (get_param(params, 1)->thread_id.kind != GDB_ONE_THREAD) {
Alex Bennéea346af32020-03-16 17:21:34 +00001645 put_packet("OK");
Jon Doron3a9651d2019-05-29 09:41:34 +03001646 return;
1647 }
1648
Alex Bennée26a16182021-05-25 09:24:14 +01001649 cpu = gdb_get_cpu(get_param(params, 1)->thread_id.pid,
1650 get_param(params, 1)->thread_id.tid);
Jon Doron3a9651d2019-05-29 09:41:34 +03001651 if (!cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +00001652 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001653 return;
1654 }
1655
1656 /*
1657 * Note: This command is deprecated and modern gdb's will be using the
1658 * vCont command instead.
1659 */
Alex Bennée26a16182021-05-25 09:24:14 +01001660 switch (get_param(params, 0)->opcode) {
Jon Doron3a9651d2019-05-29 09:41:34 +03001661 case 'c':
Alex Bennéea346af32020-03-16 17:21:34 +00001662 gdbserver_state.c_cpu = cpu;
1663 put_packet("OK");
Jon Doron3a9651d2019-05-29 09:41:34 +03001664 break;
1665 case 'g':
Alex Bennéea346af32020-03-16 17:21:34 +00001666 gdbserver_state.g_cpu = cpu;
1667 put_packet("OK");
Jon Doron3a9651d2019-05-29 09:41:34 +03001668 break;
1669 default:
Alex Bennéea346af32020-03-16 17:21:34 +00001670 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001671 break;
1672 }
1673}
1674
Alex Bennée26a16182021-05-25 09:24:14 +01001675static void handle_insert_bp(GArray *params, void *user_ctx)
Jon Doron77f6ce52019-05-29 09:41:35 +03001676{
1677 int res;
1678
Alex Bennée26a16182021-05-25 09:24:14 +01001679 if (params->len != 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00001680 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001681 return;
1682 }
1683
Alex Bennée26a16182021-05-25 09:24:14 +01001684 res = gdb_breakpoint_insert(get_param(params, 0)->val_ul,
1685 get_param(params, 1)->val_ull,
1686 get_param(params, 2)->val_ull);
Jon Doron77f6ce52019-05-29 09:41:35 +03001687 if (res >= 0) {
Alex Bennéea346af32020-03-16 17:21:34 +00001688 put_packet("OK");
Jon Doron77f6ce52019-05-29 09:41:35 +03001689 return;
1690 } else if (res == -ENOSYS) {
Alex Bennéea346af32020-03-16 17:21:34 +00001691 put_packet("");
Jon Doron77f6ce52019-05-29 09:41:35 +03001692 return;
1693 }
1694
Alex Bennéea346af32020-03-16 17:21:34 +00001695 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001696}
1697
Alex Bennée26a16182021-05-25 09:24:14 +01001698static void handle_remove_bp(GArray *params, void *user_ctx)
Jon Doron77f6ce52019-05-29 09:41:35 +03001699{
1700 int res;
1701
Alex Bennée26a16182021-05-25 09:24:14 +01001702 if (params->len != 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00001703 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001704 return;
1705 }
1706
Alex Bennée26a16182021-05-25 09:24:14 +01001707 res = gdb_breakpoint_remove(get_param(params, 0)->val_ul,
1708 get_param(params, 1)->val_ull,
1709 get_param(params, 2)->val_ull);
Jon Doron77f6ce52019-05-29 09:41:35 +03001710 if (res >= 0) {
Alex Bennéea346af32020-03-16 17:21:34 +00001711 put_packet("OK");
Jon Doron77f6ce52019-05-29 09:41:35 +03001712 return;
1713 } else if (res == -ENOSYS) {
Alex Bennéea346af32020-03-16 17:21:34 +00001714 put_packet("");
Jon Doron77f6ce52019-05-29 09:41:35 +03001715 return;
1716 }
1717
Alex Bennéea346af32020-03-16 17:21:34 +00001718 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001719}
1720
Alex Bennée94b2a622019-07-05 14:23:07 +01001721/*
1722 * handle_set/get_reg
1723 *
1724 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1725 * This works, but can be very slow. Anything new enough to understand
1726 * XML also knows how to use this properly. However to use this we
1727 * need to define a local XML file as well as be talking to a
1728 * reasonably modern gdb. Responding with an empty packet will cause
1729 * the remote gdb to fallback to older methods.
1730 */
1731
Alex Bennée26a16182021-05-25 09:24:14 +01001732static void handle_set_reg(GArray *params, void *user_ctx)
Jon Doron62b33202019-05-29 09:41:36 +03001733{
1734 int reg_size;
1735
1736 if (!gdb_has_xml) {
Alex Bennéea346af32020-03-16 17:21:34 +00001737 put_packet("");
Jon Doron62b33202019-05-29 09:41:36 +03001738 return;
1739 }
1740
Alex Bennée26a16182021-05-25 09:24:14 +01001741 if (params->len != 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001742 put_packet("E22");
Jon Doron62b33202019-05-29 09:41:36 +03001743 return;
1744 }
1745
Alex Bennée26a16182021-05-25 09:24:14 +01001746 reg_size = strlen(get_param(params, 1)->data) / 2;
1747 hextomem(gdbserver_state.mem_buf, get_param(params, 1)->data, reg_size);
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001748 gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data,
Alex Bennée26a16182021-05-25 09:24:14 +01001749 get_param(params, 0)->val_ull);
Alex Bennéea346af32020-03-16 17:21:34 +00001750 put_packet("OK");
Jon Doron62b33202019-05-29 09:41:36 +03001751}
1752
Alex Bennée26a16182021-05-25 09:24:14 +01001753static void handle_get_reg(GArray *params, void *user_ctx)
Jon Doron5d0e57b2019-05-29 09:41:37 +03001754{
1755 int reg_size;
1756
Jon Doron5d0e57b2019-05-29 09:41:37 +03001757 if (!gdb_has_xml) {
Alex Bennéea346af32020-03-16 17:21:34 +00001758 put_packet("");
Jon Doron5d0e57b2019-05-29 09:41:37 +03001759 return;
1760 }
1761
Alex Bennée26a16182021-05-25 09:24:14 +01001762 if (!params->len) {
Alex Bennéea346af32020-03-16 17:21:34 +00001763 put_packet("E14");
Jon Doron5d0e57b2019-05-29 09:41:37 +03001764 return;
1765 }
1766
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001767 reg_size = gdb_read_register(gdbserver_state.g_cpu,
Alex Bennéea010bdb2020-03-16 17:21:41 +00001768 gdbserver_state.mem_buf,
Alex Bennée26a16182021-05-25 09:24:14 +01001769 get_param(params, 0)->val_ull);
Jon Doron5d0e57b2019-05-29 09:41:37 +03001770 if (!reg_size) {
Alex Bennéea346af32020-03-16 17:21:34 +00001771 put_packet("E14");
Jon Doron5d0e57b2019-05-29 09:41:37 +03001772 return;
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001773 } else {
1774 g_byte_array_set_size(gdbserver_state.mem_buf, reg_size);
Jon Doron5d0e57b2019-05-29 09:41:37 +03001775 }
1776
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001777 memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, reg_size);
Alex Bennée308f9e82020-03-16 17:21:35 +00001778 put_strbuf();
Jon Doron5d0e57b2019-05-29 09:41:37 +03001779}
1780
Alex Bennée26a16182021-05-25 09:24:14 +01001781static void handle_write_mem(GArray *params, void *user_ctx)
Jon Doroncc0ecc72019-05-29 09:41:38 +03001782{
Alex Bennée26a16182021-05-25 09:24:14 +01001783 if (params->len != 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00001784 put_packet("E22");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001785 return;
1786 }
1787
1788 /* hextomem() reads 2*len bytes */
Alex Bennée26a16182021-05-25 09:24:14 +01001789 if (get_param(params, 1)->val_ull >
1790 strlen(get_param(params, 2)->data) / 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001791 put_packet("E22");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001792 return;
1793 }
1794
Alex Bennée26a16182021-05-25 09:24:14 +01001795 hextomem(gdbserver_state.mem_buf, get_param(params, 2)->data,
1796 get_param(params, 1)->val_ull);
1797 if (target_memory_rw_debug(gdbserver_state.g_cpu,
1798 get_param(params, 0)->val_ull,
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001799 gdbserver_state.mem_buf->data,
1800 gdbserver_state.mem_buf->len, true)) {
Alex Bennéea346af32020-03-16 17:21:34 +00001801 put_packet("E14");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001802 return;
1803 }
1804
Alex Bennéea346af32020-03-16 17:21:34 +00001805 put_packet("OK");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001806}
1807
Alex Bennée26a16182021-05-25 09:24:14 +01001808static void handle_read_mem(GArray *params, void *user_ctx)
Jon Doronda92e232019-05-29 09:41:39 +03001809{
Alex Bennée26a16182021-05-25 09:24:14 +01001810 if (params->len != 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001811 put_packet("E22");
Jon Doronda92e232019-05-29 09:41:39 +03001812 return;
1813 }
1814
1815 /* memtohex() doubles the required space */
Alex Bennée26a16182021-05-25 09:24:14 +01001816 if (get_param(params, 1)->val_ull > MAX_PACKET_LENGTH / 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001817 put_packet("E22");
Jon Doronda92e232019-05-29 09:41:39 +03001818 return;
1819 }
1820
Alex Bennée26a16182021-05-25 09:24:14 +01001821 g_byte_array_set_size(gdbserver_state.mem_buf,
1822 get_param(params, 1)->val_ull);
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001823
Alex Bennée26a16182021-05-25 09:24:14 +01001824 if (target_memory_rw_debug(gdbserver_state.g_cpu,
1825 get_param(params, 0)->val_ull,
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001826 gdbserver_state.mem_buf->data,
1827 gdbserver_state.mem_buf->len, false)) {
Alex Bennéea346af32020-03-16 17:21:34 +00001828 put_packet("E14");
Jon Doronda92e232019-05-29 09:41:39 +03001829 return;
1830 }
1831
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001832 memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data,
1833 gdbserver_state.mem_buf->len);
Alex Bennée308f9e82020-03-16 17:21:35 +00001834 put_strbuf();
Jon Doronda92e232019-05-29 09:41:39 +03001835}
1836
Alex Bennée26a16182021-05-25 09:24:14 +01001837static void handle_write_all_regs(GArray *params, void *user_ctx)
Jon Doron287ca122019-05-29 09:41:40 +03001838{
1839 target_ulong addr, len;
1840 uint8_t *registers;
1841 int reg_size;
1842
Alex Bennée26a16182021-05-25 09:24:14 +01001843 if (!params->len) {
Jon Doron287ca122019-05-29 09:41:40 +03001844 return;
1845 }
1846
Alex Bennéea346af32020-03-16 17:21:34 +00001847 cpu_synchronize_state(gdbserver_state.g_cpu);
Alex Bennée26a16182021-05-25 09:24:14 +01001848 len = strlen(get_param(params, 0)->data) / 2;
1849 hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len);
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001850 registers = gdbserver_state.mem_buf->data;
Alex Bennéea346af32020-03-16 17:21:34 +00001851 for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0;
Jon Doron287ca122019-05-29 09:41:40 +03001852 addr++) {
Alex Bennéea346af32020-03-16 17:21:34 +00001853 reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, addr);
Jon Doron287ca122019-05-29 09:41:40 +03001854 len -= reg_size;
1855 registers += reg_size;
1856 }
Alex Bennéea346af32020-03-16 17:21:34 +00001857 put_packet("OK");
Jon Doron287ca122019-05-29 09:41:40 +03001858}
1859
Alex Bennée26a16182021-05-25 09:24:14 +01001860static void handle_read_all_regs(GArray *params, void *user_ctx)
Jon Doron397d1372019-05-29 09:41:41 +03001861{
1862 target_ulong addr, len;
1863
Alex Bennéea346af32020-03-16 17:21:34 +00001864 cpu_synchronize_state(gdbserver_state.g_cpu);
Alex Bennéea010bdb2020-03-16 17:21:41 +00001865 g_byte_array_set_size(gdbserver_state.mem_buf, 0);
Jon Doron397d1372019-05-29 09:41:41 +03001866 len = 0;
Alex Bennéea346af32020-03-16 17:21:34 +00001867 for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs; addr++) {
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001868 len += gdb_read_register(gdbserver_state.g_cpu,
Alex Bennéea010bdb2020-03-16 17:21:41 +00001869 gdbserver_state.mem_buf,
Jon Doron397d1372019-05-29 09:41:41 +03001870 addr);
1871 }
Alex Bennéea010bdb2020-03-16 17:21:41 +00001872 g_assert(len == gdbserver_state.mem_buf->len);
Jon Doron397d1372019-05-29 09:41:41 +03001873
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001874 memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len);
Alex Bennée308f9e82020-03-16 17:21:35 +00001875 put_strbuf();
Jon Doron397d1372019-05-29 09:41:41 +03001876}
1877
Alex Bennée26a16182021-05-25 09:24:14 +01001878static void handle_file_io(GArray *params, void *user_ctx)
Jon Doron4b20fab2019-05-29 09:41:42 +03001879{
Alex Bennée26a16182021-05-25 09:24:14 +01001880 if (params->len >= 1 && gdbserver_state.current_syscall_cb) {
Jon Doron4b20fab2019-05-29 09:41:42 +03001881 target_ulong ret, err;
1882
Alex Bennée26a16182021-05-25 09:24:14 +01001883 ret = (target_ulong)get_param(params, 0)->val_ull;
1884 if (params->len >= 2) {
1885 err = (target_ulong)get_param(params, 1)->val_ull;
Sandra Loosemorec6ee9522019-08-27 16:33:17 -06001886 } else {
1887 err = 0;
1888 }
Alex Bennéea346af32020-03-16 17:21:34 +00001889 gdbserver_state.current_syscall_cb(gdbserver_state.c_cpu, ret, err);
1890 gdbserver_state.current_syscall_cb = NULL;
Jon Doron4b20fab2019-05-29 09:41:42 +03001891 }
1892
Alex Bennée26a16182021-05-25 09:24:14 +01001893 if (params->len >= 3 && get_param(params, 2)->opcode == (uint8_t)'C') {
Alex Bennéea346af32020-03-16 17:21:34 +00001894 put_packet("T02");
Jon Doron4b20fab2019-05-29 09:41:42 +03001895 return;
1896 }
1897
Alex Bennéea346af32020-03-16 17:21:34 +00001898 gdb_continue();
Jon Doron4b20fab2019-05-29 09:41:42 +03001899}
1900
Alex Bennée26a16182021-05-25 09:24:14 +01001901static void handle_step(GArray *params, void *user_ctx)
Jon Doron933f80d2019-05-29 09:41:43 +03001902{
Alex Bennée26a16182021-05-25 09:24:14 +01001903 if (params->len) {
1904 gdb_set_cpu_pc((target_ulong)get_param(params, 0)->val_ull);
Jon Doron933f80d2019-05-29 09:41:43 +03001905 }
1906
Maxim Levitskyecd39d62021-11-11 12:06:02 +01001907 cpu_single_step(gdbserver_state.c_cpu, gdbserver_state.sstep_flags);
Alex Bennéea346af32020-03-16 17:21:34 +00001908 gdb_continue();
Jon Doron933f80d2019-05-29 09:41:43 +03001909}
1910
Alex Bennée26a16182021-05-25 09:24:14 +01001911static void handle_backward(GArray *params, void *user_ctx)
Pavel Dovgalyukfda84582020-10-03 20:13:43 +03001912{
Alex Bennéeed12f5b2021-05-20 18:43:02 +01001913 if (!stub_can_reverse()) {
Pavel Dovgalyukfda84582020-10-03 20:13:43 +03001914 put_packet("E22");
1915 }
Alex Bennée26a16182021-05-25 09:24:14 +01001916 if (params->len == 1) {
1917 switch (get_param(params, 0)->opcode) {
Pavel Dovgalyukfda84582020-10-03 20:13:43 +03001918 case 's':
1919 if (replay_reverse_step()) {
1920 gdb_continue();
1921 } else {
1922 put_packet("E14");
1923 }
1924 return;
Pavel Dovgalyukcda38252020-10-03 20:13:49 +03001925 case 'c':
1926 if (replay_reverse_continue()) {
1927 gdb_continue();
1928 } else {
1929 put_packet("E14");
1930 }
1931 return;
Pavel Dovgalyukfda84582020-10-03 20:13:43 +03001932 }
1933 }
1934
1935 /* Default invalid command */
1936 put_packet("");
1937}
1938
Alex Bennée26a16182021-05-25 09:24:14 +01001939static void handle_v_cont_query(GArray *params, void *user_ctx)
Jon Doron8536ec02019-05-29 09:41:44 +03001940{
Alex Bennéea346af32020-03-16 17:21:34 +00001941 put_packet("vCont;c;C;s;S");
Jon Doron8536ec02019-05-29 09:41:44 +03001942}
1943
Alex Bennée26a16182021-05-25 09:24:14 +01001944static void handle_v_cont(GArray *params, void *user_ctx)
Jon Doron8536ec02019-05-29 09:41:44 +03001945{
1946 int res;
1947
Alex Bennée26a16182021-05-25 09:24:14 +01001948 if (!params->len) {
Jon Doron8536ec02019-05-29 09:41:44 +03001949 return;
1950 }
1951
Alex Bennée26a16182021-05-25 09:24:14 +01001952 res = gdb_handle_vcont(get_param(params, 0)->data);
Jon Doron8536ec02019-05-29 09:41:44 +03001953 if ((res == -EINVAL) || (res == -ERANGE)) {
Alex Bennéea346af32020-03-16 17:21:34 +00001954 put_packet("E22");
Jon Doron8536ec02019-05-29 09:41:44 +03001955 } else if (res) {
Alex Bennéea346af32020-03-16 17:21:34 +00001956 put_packet("");
Jon Doron8536ec02019-05-29 09:41:44 +03001957 }
1958}
1959
Alex Bennée26a16182021-05-25 09:24:14 +01001960static void handle_v_attach(GArray *params, void *user_ctx)
Jon Doron8536ec02019-05-29 09:41:44 +03001961{
1962 GDBProcess *process;
1963 CPUState *cpu;
Jon Doron8536ec02019-05-29 09:41:44 +03001964
Alex Bennée308f9e82020-03-16 17:21:35 +00001965 g_string_assign(gdbserver_state.str_buf, "E22");
Alex Bennée26a16182021-05-25 09:24:14 +01001966 if (!params->len) {
Jon Doron8536ec02019-05-29 09:41:44 +03001967 goto cleanup;
1968 }
1969
Alex Bennée26a16182021-05-25 09:24:14 +01001970 process = gdb_get_process(get_param(params, 0)->val_ul);
Jon Doron8536ec02019-05-29 09:41:44 +03001971 if (!process) {
1972 goto cleanup;
1973 }
1974
Alex Bennéea346af32020-03-16 17:21:34 +00001975 cpu = get_first_cpu_in_process(process);
Jon Doron8536ec02019-05-29 09:41:44 +03001976 if (!cpu) {
1977 goto cleanup;
1978 }
1979
1980 process->attached = true;
Alex Bennéea346af32020-03-16 17:21:34 +00001981 gdbserver_state.g_cpu = cpu;
1982 gdbserver_state.c_cpu = cpu;
Jon Doron8536ec02019-05-29 09:41:44 +03001983
Alex Bennée308f9e82020-03-16 17:21:35 +00001984 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
1985 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
1986 g_string_append_c(gdbserver_state.str_buf, ';');
Jon Doron8536ec02019-05-29 09:41:44 +03001987cleanup:
Alex Bennée308f9e82020-03-16 17:21:35 +00001988 put_strbuf();
Jon Doron8536ec02019-05-29 09:41:44 +03001989}
1990
Alex Bennée26a16182021-05-25 09:24:14 +01001991static void handle_v_kill(GArray *params, void *user_ctx)
Jon Doron8536ec02019-05-29 09:41:44 +03001992{
1993 /* Kill the target */
Alex Bennéea346af32020-03-16 17:21:34 +00001994 put_packet("OK");
Jon Doron8536ec02019-05-29 09:41:44 +03001995 error_report("QEMU: Terminated via GDBstub");
Alex Bennéeb9e10c62021-01-08 22:42:45 +00001996 gdb_exit(0);
Jon Doron8536ec02019-05-29 09:41:44 +03001997 exit(0);
1998}
1999
Philippe Mathieu-Daudé305bea02021-05-20 18:42:59 +01002000static const GdbCmdParseEntry gdb_v_commands_table[] = {
Jon Doron8536ec02019-05-29 09:41:44 +03002001 /* Order is important if has same prefix */
2002 {
2003 .handler = handle_v_cont_query,
2004 .cmd = "Cont?",
2005 .cmd_startswith = 1
2006 },
2007 {
2008 .handler = handle_v_cont,
2009 .cmd = "Cont",
2010 .cmd_startswith = 1,
2011 .schema = "s0"
2012 },
2013 {
2014 .handler = handle_v_attach,
2015 .cmd = "Attach;",
2016 .cmd_startswith = 1,
2017 .schema = "l0"
2018 },
2019 {
2020 .handler = handle_v_kill,
2021 .cmd = "Kill;",
2022 .cmd_startswith = 1
2023 },
2024};
2025
Alex Bennée26a16182021-05-25 09:24:14 +01002026static void handle_v_commands(GArray *params, void *user_ctx)
Jon Doron8536ec02019-05-29 09:41:44 +03002027{
Alex Bennée26a16182021-05-25 09:24:14 +01002028 if (!params->len) {
Jon Doron8536ec02019-05-29 09:41:44 +03002029 return;
2030 }
2031
Alex Bennée26a16182021-05-25 09:24:14 +01002032 if (process_string_cmd(NULL, get_param(params, 0)->data,
Jon Doron8536ec02019-05-29 09:41:44 +03002033 gdb_v_commands_table,
2034 ARRAY_SIZE(gdb_v_commands_table))) {
Alex Bennéea346af32020-03-16 17:21:34 +00002035 put_packet("");
Jon Doron8536ec02019-05-29 09:41:44 +03002036 }
2037}
2038
Alex Bennée26a16182021-05-25 09:24:14 +01002039static void handle_query_qemu_sstepbits(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03002040{
Maxim Levitskyecd39d62021-11-11 12:06:02 +01002041 g_string_printf(gdbserver_state.str_buf, "ENABLE=%x", SSTEP_ENABLE);
2042
2043 if (gdbserver_state.supported_sstep_flags & SSTEP_NOIRQ) {
2044 g_string_append_printf(gdbserver_state.str_buf, ",NOIRQ=%x",
2045 SSTEP_NOIRQ);
2046 }
2047
2048 if (gdbserver_state.supported_sstep_flags & SSTEP_NOTIMER) {
2049 g_string_append_printf(gdbserver_state.str_buf, ",NOTIMER=%x",
2050 SSTEP_NOTIMER);
2051 }
2052
Alex Bennée308f9e82020-03-16 17:21:35 +00002053 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002054}
2055
Alex Bennée26a16182021-05-25 09:24:14 +01002056static void handle_set_qemu_sstep(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03002057{
Maxim Levitskyecd39d62021-11-11 12:06:02 +01002058 int new_sstep_flags;
2059
Alex Bennée26a16182021-05-25 09:24:14 +01002060 if (!params->len) {
Jon Doron2704efa2019-05-29 09:41:45 +03002061 return;
2062 }
2063
Maxim Levitskyecd39d62021-11-11 12:06:02 +01002064 new_sstep_flags = get_param(params, 0)->val_ul;
2065
2066 if (new_sstep_flags & ~gdbserver_state.supported_sstep_flags) {
2067 put_packet("E22");
2068 return;
2069 }
2070
2071 gdbserver_state.sstep_flags = new_sstep_flags;
Alex Bennéea346af32020-03-16 17:21:34 +00002072 put_packet("OK");
Jon Doron2704efa2019-05-29 09:41:45 +03002073}
2074
Alex Bennée26a16182021-05-25 09:24:14 +01002075static void handle_query_qemu_sstep(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03002076{
Maxim Levitskyecd39d62021-11-11 12:06:02 +01002077 g_string_printf(gdbserver_state.str_buf, "0x%x",
2078 gdbserver_state.sstep_flags);
Alex Bennée308f9e82020-03-16 17:21:35 +00002079 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002080}
2081
Alex Bennée26a16182021-05-25 09:24:14 +01002082static void handle_query_curr_tid(GArray *params, void *user_ctx)
bellardb4608c02003-06-27 17:34:32 +00002083{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002084 CPUState *cpu;
Luc Michelc145eea2019-01-07 15:23:46 +00002085 GDBProcess *process;
Jon Doron2704efa2019-05-29 09:41:45 +03002086
2087 /*
2088 * "Current thread" remains vague in the spec, so always return
2089 * the first thread of the current process (gdb returns the
2090 * first thread).
2091 */
Alex Bennéea346af32020-03-16 17:21:34 +00002092 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
2093 cpu = get_first_cpu_in_process(process);
Alex Bennée308f9e82020-03-16 17:21:35 +00002094 g_string_assign(gdbserver_state.str_buf, "QC");
2095 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
2096 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002097}
2098
Alex Bennée26a16182021-05-25 09:24:14 +01002099static void handle_query_threads(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03002100{
Alex Bennéea346af32020-03-16 17:21:34 +00002101 if (!gdbserver_state.query_cpu) {
2102 put_packet("l");
Jon Doron2704efa2019-05-29 09:41:45 +03002103 return;
2104 }
2105
Alex Bennée308f9e82020-03-16 17:21:35 +00002106 g_string_assign(gdbserver_state.str_buf, "m");
2107 gdb_append_thread_id(gdbserver_state.query_cpu, gdbserver_state.str_buf);
2108 put_strbuf();
Alex Bennéea346af32020-03-16 17:21:34 +00002109 gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu);
Jon Doron2704efa2019-05-29 09:41:45 +03002110}
2111
Alex Bennée26a16182021-05-25 09:24:14 +01002112static void handle_query_first_threads(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03002113{
Alex Bennéea346af32020-03-16 17:21:34 +00002114 gdbserver_state.query_cpu = gdb_first_attached_cpu();
Alex Bennée26a16182021-05-25 09:24:14 +01002115 handle_query_threads(params, user_ctx);
Jon Doron2704efa2019-05-29 09:41:45 +03002116}
2117
Alex Bennée26a16182021-05-25 09:24:14 +01002118static void handle_query_thread_extra(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03002119{
Alex Bennée308f9e82020-03-16 17:21:35 +00002120 g_autoptr(GString) rs = g_string_new(NULL);
Jon Doron2704efa2019-05-29 09:41:45 +03002121 CPUState *cpu;
Jon Doron2704efa2019-05-29 09:41:45 +03002122
Alex Bennée26a16182021-05-25 09:24:14 +01002123 if (!params->len ||
2124 get_param(params, 0)->thread_id.kind == GDB_READ_THREAD_ERR) {
Alex Bennéea346af32020-03-16 17:21:34 +00002125 put_packet("E22");
Jon Doron2704efa2019-05-29 09:41:45 +03002126 return;
2127 }
2128
Alex Bennée26a16182021-05-25 09:24:14 +01002129 cpu = gdb_get_cpu(get_param(params, 0)->thread_id.pid,
2130 get_param(params, 0)->thread_id.tid);
Jon Doron2704efa2019-05-29 09:41:45 +03002131 if (!cpu) {
2132 return;
2133 }
2134
2135 cpu_synchronize_state(cpu);
2136
Alex Bennéea346af32020-03-16 17:21:34 +00002137 if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) {
Jon Doron2704efa2019-05-29 09:41:45 +03002138 /* Print the CPU model and name in multiprocess mode */
2139 ObjectClass *oc = object_get_class(OBJECT(cpu));
2140 const char *cpu_model = object_class_get_name(oc);
Markus Armbruster7a309cc2020-07-14 18:02:00 +02002141 const char *cpu_name =
Denis Plotnikov076b2fa2020-04-03 20:11:44 +01002142 object_get_canonical_path_component(OBJECT(cpu));
Alex Bennée308f9e82020-03-16 17:21:35 +00002143 g_string_printf(rs, "%s %s [%s]", cpu_model, cpu_name,
2144 cpu->halted ? "halted " : "running");
Jon Doron2704efa2019-05-29 09:41:45 +03002145 } else {
Alex Bennée308f9e82020-03-16 17:21:35 +00002146 g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index,
Jon Doron2704efa2019-05-29 09:41:45 +03002147 cpu->halted ? "halted " : "running");
2148 }
Alex Bennée308f9e82020-03-16 17:21:35 +00002149 trace_gdbstub_op_extra_info(rs->str);
2150 memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len);
2151 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002152}
2153
2154#ifdef CONFIG_USER_ONLY
Alex Bennée26a16182021-05-25 09:24:14 +01002155static void handle_query_offsets(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03002156{
2157 TaskState *ts;
2158
Alex Bennéea346af32020-03-16 17:21:34 +00002159 ts = gdbserver_state.c_cpu->opaque;
Alex Bennée308f9e82020-03-16 17:21:35 +00002160 g_string_printf(gdbserver_state.str_buf,
2161 "Text=" TARGET_ABI_FMT_lx
2162 ";Data=" TARGET_ABI_FMT_lx
2163 ";Bss=" TARGET_ABI_FMT_lx,
2164 ts->info->code_offset,
2165 ts->info->data_offset,
2166 ts->info->data_offset);
2167 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002168}
2169#else
Alex Bennée26a16182021-05-25 09:24:14 +01002170static void handle_query_rcmd(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03002171{
Alex Bennée4a25f1b2020-03-16 17:21:36 +00002172 const guint8 zero = 0;
Jon Doron2704efa2019-05-29 09:41:45 +03002173 int len;
2174
Alex Bennée26a16182021-05-25 09:24:14 +01002175 if (!params->len) {
Alex Bennéea346af32020-03-16 17:21:34 +00002176 put_packet("E22");
Jon Doron2704efa2019-05-29 09:41:45 +03002177 return;
2178 }
2179
Alex Bennée26a16182021-05-25 09:24:14 +01002180 len = strlen(get_param(params, 0)->data);
Jon Doron2704efa2019-05-29 09:41:45 +03002181 if (len % 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00002182 put_packet("E01");
Jon Doron2704efa2019-05-29 09:41:45 +03002183 return;
2184 }
2185
Alex Bennée4a25f1b2020-03-16 17:21:36 +00002186 g_assert(gdbserver_state.mem_buf->len == 0);
Jon Doron2704efa2019-05-29 09:41:45 +03002187 len = len / 2;
Alex Bennée26a16182021-05-25 09:24:14 +01002188 hextomem(gdbserver_state.mem_buf, get_param(params, 0)->data, len);
Alex Bennée4a25f1b2020-03-16 17:21:36 +00002189 g_byte_array_append(gdbserver_state.mem_buf, &zero, 1);
2190 qemu_chr_be_write(gdbserver_state.mon_chr, gdbserver_state.mem_buf->data,
2191 gdbserver_state.mem_buf->len);
Alex Bennéea346af32020-03-16 17:21:34 +00002192 put_packet("OK");
Jon Doron2704efa2019-05-29 09:41:45 +03002193}
2194#endif
2195
Alex Bennée26a16182021-05-25 09:24:14 +01002196static void handle_query_supported(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03002197{
Andreas Färber5b24c642013-07-07 15:08:22 +02002198 CPUClass *cc;
Jon Doron2704efa2019-05-29 09:41:45 +03002199
Alex Bennée308f9e82020-03-16 17:21:35 +00002200 g_string_printf(gdbserver_state.str_buf, "PacketSize=%x", MAX_PACKET_LENGTH);
Jon Doron2704efa2019-05-29 09:41:45 +03002201 cc = CPU_GET_CLASS(first_cpu);
2202 if (cc->gdb_core_xml_file) {
Alex Bennée308f9e82020-03-16 17:21:35 +00002203 g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+");
Jon Doron2704efa2019-05-29 09:41:45 +03002204 }
2205
Alex Bennéeed12f5b2021-05-20 18:43:02 +01002206 if (stub_can_reverse()) {
Pavel Dovgalyukcda38252020-10-03 20:13:49 +03002207 g_string_append(gdbserver_state.str_buf,
2208 ";ReverseStep+;ReverseContinue+");
Pavel Dovgalyukfda84582020-10-03 20:13:43 +03002209 }
2210
Lirong Yuan51c623b2021-01-08 22:42:42 +00002211#ifdef CONFIG_USER_ONLY
2212 if (gdbserver_state.c_cpu->opaque) {
2213 g_string_append(gdbserver_state.str_buf, ";qXfer:auxv:read+");
2214 }
2215#endif
2216
Alex Bennée26a16182021-05-25 09:24:14 +01002217 if (params->len &&
2218 strstr(get_param(params, 0)->data, "multiprocess+")) {
Alex Bennéea346af32020-03-16 17:21:34 +00002219 gdbserver_state.multiprocess = true;
Jon Doron2704efa2019-05-29 09:41:45 +03002220 }
2221
Changbin Du3bc26092020-03-16 17:21:55 +00002222 g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+");
Alex Bennée308f9e82020-03-16 17:21:35 +00002223 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002224}
2225
Alex Bennée26a16182021-05-25 09:24:14 +01002226static void handle_query_xfer_features(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03002227{
2228 GDBProcess *process;
2229 CPUClass *cc;
2230 unsigned long len, total_len, addr;
2231 const char *xml;
bellardb4608c02003-06-27 17:34:32 +00002232 const char *p;
Jon Doron2704efa2019-05-29 09:41:45 +03002233
Alex Bennée26a16182021-05-25 09:24:14 +01002234 if (params->len < 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00002235 put_packet("E22");
Jon Doron2704efa2019-05-29 09:41:45 +03002236 return;
2237 }
2238
Alex Bennéea346af32020-03-16 17:21:34 +00002239 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
2240 cc = CPU_GET_CLASS(gdbserver_state.g_cpu);
Jon Doron2704efa2019-05-29 09:41:45 +03002241 if (!cc->gdb_core_xml_file) {
Alex Bennéea346af32020-03-16 17:21:34 +00002242 put_packet("");
Jon Doron2704efa2019-05-29 09:41:45 +03002243 return;
2244 }
2245
2246 gdb_has_xml = true;
Alex Bennée26a16182021-05-25 09:24:14 +01002247 p = get_param(params, 0)->data;
Alex Bennéea346af32020-03-16 17:21:34 +00002248 xml = get_feature_xml(p, &p, process);
Jon Doron2704efa2019-05-29 09:41:45 +03002249 if (!xml) {
Alex Bennéea346af32020-03-16 17:21:34 +00002250 put_packet("E00");
Jon Doron2704efa2019-05-29 09:41:45 +03002251 return;
2252 }
2253
Alex Bennée26a16182021-05-25 09:24:14 +01002254 addr = get_param(params, 1)->val_ul;
2255 len = get_param(params, 2)->val_ul;
Jon Doron2704efa2019-05-29 09:41:45 +03002256 total_len = strlen(xml);
2257 if (addr > total_len) {
Alex Bennéea346af32020-03-16 17:21:34 +00002258 put_packet("E00");
Jon Doron2704efa2019-05-29 09:41:45 +03002259 return;
2260 }
2261
2262 if (len > (MAX_PACKET_LENGTH - 5) / 2) {
2263 len = (MAX_PACKET_LENGTH - 5) / 2;
2264 }
2265
2266 if (len < total_len - addr) {
Alex Bennée308f9e82020-03-16 17:21:35 +00002267 g_string_assign(gdbserver_state.str_buf, "m");
2268 memtox(gdbserver_state.str_buf, xml + addr, len);
Jon Doron2704efa2019-05-29 09:41:45 +03002269 } else {
Alex Bennée308f9e82020-03-16 17:21:35 +00002270 g_string_assign(gdbserver_state.str_buf, "l");
2271 memtox(gdbserver_state.str_buf, xml + addr, total_len - addr);
Jon Doron2704efa2019-05-29 09:41:45 +03002272 }
2273
Alex Bennée308f9e82020-03-16 17:21:35 +00002274 put_packet_binary(gdbserver_state.str_buf->str,
2275 gdbserver_state.str_buf->len, true);
Jon Doron2704efa2019-05-29 09:41:45 +03002276}
2277
Lirong Yuan51c623b2021-01-08 22:42:42 +00002278#if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX_USER)
Alex Bennée26a16182021-05-25 09:24:14 +01002279static void handle_query_xfer_auxv(GArray *params, void *user_ctx)
Lirong Yuan51c623b2021-01-08 22:42:42 +00002280{
2281 TaskState *ts;
2282 unsigned long offset, len, saved_auxv, auxv_len;
Lirong Yuan51c623b2021-01-08 22:42:42 +00002283
Alex Bennée26a16182021-05-25 09:24:14 +01002284 if (params->len < 2) {
Lirong Yuan51c623b2021-01-08 22:42:42 +00002285 put_packet("E22");
2286 return;
2287 }
2288
Alex Bennée26a16182021-05-25 09:24:14 +01002289 offset = get_param(params, 0)->val_ul;
2290 len = get_param(params, 1)->val_ul;
Lirong Yuan51c623b2021-01-08 22:42:42 +00002291 ts = gdbserver_state.c_cpu->opaque;
2292 saved_auxv = ts->info->saved_auxv;
2293 auxv_len = ts->info->auxv_len;
Richard Henderson6e3dd752021-02-02 13:39:55 +00002294
2295 if (offset >= auxv_len) {
Lirong Yuan51c623b2021-01-08 22:42:42 +00002296 put_packet("E00");
2297 return;
2298 }
2299
2300 if (len > (MAX_PACKET_LENGTH - 5) / 2) {
2301 len = (MAX_PACKET_LENGTH - 5) / 2;
2302 }
2303
2304 if (len < auxv_len - offset) {
2305 g_string_assign(gdbserver_state.str_buf, "m");
Lirong Yuan51c623b2021-01-08 22:42:42 +00002306 } else {
2307 g_string_assign(gdbserver_state.str_buf, "l");
Richard Henderson6e3dd752021-02-02 13:39:55 +00002308 len = auxv_len - offset;
Lirong Yuan51c623b2021-01-08 22:42:42 +00002309 }
2310
Richard Henderson6e3dd752021-02-02 13:39:55 +00002311 g_byte_array_set_size(gdbserver_state.mem_buf, len);
2312 if (target_memory_rw_debug(gdbserver_state.g_cpu, saved_auxv + offset,
2313 gdbserver_state.mem_buf->data, len, false)) {
2314 put_packet("E14");
2315 return;
2316 }
2317
2318 memtox(gdbserver_state.str_buf,
2319 (const char *)gdbserver_state.mem_buf->data, len);
Lirong Yuan51c623b2021-01-08 22:42:42 +00002320 put_packet_binary(gdbserver_state.str_buf->str,
2321 gdbserver_state.str_buf->len, true);
2322}
2323#endif
2324
Alex Bennée26a16182021-05-25 09:24:14 +01002325static void handle_query_attached(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03002326{
Alex Bennéea346af32020-03-16 17:21:34 +00002327 put_packet(GDB_ATTACHED);
Jon Doron2704efa2019-05-29 09:41:45 +03002328}
2329
Alex Bennée26a16182021-05-25 09:24:14 +01002330static void handle_query_qemu_supported(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03002331{
Alex Bennée308f9e82020-03-16 17:21:35 +00002332 g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep");
Jon Doronab4752e2019-05-29 09:41:48 +03002333#ifndef CONFIG_USER_ONLY
Alex Bennée308f9e82020-03-16 17:21:35 +00002334 g_string_append(gdbserver_state.str_buf, ";PhyMemMode");
Jon Doronab4752e2019-05-29 09:41:48 +03002335#endif
Alex Bennée308f9e82020-03-16 17:21:35 +00002336 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002337}
2338
Jon Doronab4752e2019-05-29 09:41:48 +03002339#ifndef CONFIG_USER_ONLY
Alex Bennée26a16182021-05-25 09:24:14 +01002340static void handle_query_qemu_phy_mem_mode(GArray *params,
Jon Doronab4752e2019-05-29 09:41:48 +03002341 void *user_ctx)
2342{
Alex Bennée308f9e82020-03-16 17:21:35 +00002343 g_string_printf(gdbserver_state.str_buf, "%d", phy_memory_mode);
2344 put_strbuf();
Jon Doronab4752e2019-05-29 09:41:48 +03002345}
2346
Alex Bennée26a16182021-05-25 09:24:14 +01002347static void handle_set_qemu_phy_mem_mode(GArray *params, void *user_ctx)
Jon Doronab4752e2019-05-29 09:41:48 +03002348{
Alex Bennée26a16182021-05-25 09:24:14 +01002349 if (!params->len) {
Alex Bennéea346af32020-03-16 17:21:34 +00002350 put_packet("E22");
Jon Doronab4752e2019-05-29 09:41:48 +03002351 return;
2352 }
2353
Alex Bennée26a16182021-05-25 09:24:14 +01002354 if (!get_param(params, 0)->val_ul) {
Jon Doronab4752e2019-05-29 09:41:48 +03002355 phy_memory_mode = 0;
2356 } else {
2357 phy_memory_mode = 1;
2358 }
Alex Bennéea346af32020-03-16 17:21:34 +00002359 put_packet("OK");
Jon Doronab4752e2019-05-29 09:41:48 +03002360}
2361#endif
2362
Philippe Mathieu-Daudé305bea02021-05-20 18:42:59 +01002363static const GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
Jon Doron2704efa2019-05-29 09:41:45 +03002364 /* Order is important if has same prefix */
2365 {
2366 .handler = handle_query_qemu_sstepbits,
2367 .cmd = "qemu.sstepbits",
2368 },
2369 {
2370 .handler = handle_query_qemu_sstep,
2371 .cmd = "qemu.sstep",
2372 },
2373 {
2374 .handler = handle_set_qemu_sstep,
2375 .cmd = "qemu.sstep=",
2376 .cmd_startswith = 1,
2377 .schema = "l0"
2378 },
2379};
2380
Philippe Mathieu-Daudé305bea02021-05-20 18:42:59 +01002381static const GdbCmdParseEntry gdb_gen_query_table[] = {
Jon Doron2704efa2019-05-29 09:41:45 +03002382 {
2383 .handler = handle_query_curr_tid,
2384 .cmd = "C",
2385 },
2386 {
2387 .handler = handle_query_threads,
2388 .cmd = "sThreadInfo",
2389 },
2390 {
2391 .handler = handle_query_first_threads,
2392 .cmd = "fThreadInfo",
2393 },
2394 {
2395 .handler = handle_query_thread_extra,
2396 .cmd = "ThreadExtraInfo,",
2397 .cmd_startswith = 1,
2398 .schema = "t0"
2399 },
2400#ifdef CONFIG_USER_ONLY
2401 {
2402 .handler = handle_query_offsets,
2403 .cmd = "Offsets",
2404 },
2405#else
2406 {
2407 .handler = handle_query_rcmd,
2408 .cmd = "Rcmd,",
2409 .cmd_startswith = 1,
2410 .schema = "s0"
2411 },
2412#endif
2413 {
2414 .handler = handle_query_supported,
2415 .cmd = "Supported:",
2416 .cmd_startswith = 1,
2417 .schema = "s0"
2418 },
2419 {
2420 .handler = handle_query_supported,
2421 .cmd = "Supported",
2422 .schema = "s0"
2423 },
2424 {
2425 .handler = handle_query_xfer_features,
2426 .cmd = "Xfer:features:read:",
2427 .cmd_startswith = 1,
2428 .schema = "s:l,l0"
2429 },
Lirong Yuan51c623b2021-01-08 22:42:42 +00002430#if defined(CONFIG_USER_ONLY) && defined(CONFIG_LINUX_USER)
2431 {
2432 .handler = handle_query_xfer_auxv,
2433 .cmd = "Xfer:auxv:read::",
2434 .cmd_startswith = 1,
2435 .schema = "l,l0"
2436 },
2437#endif
Jon Doron2704efa2019-05-29 09:41:45 +03002438 {
2439 .handler = handle_query_attached,
2440 .cmd = "Attached:",
2441 .cmd_startswith = 1
2442 },
2443 {
2444 .handler = handle_query_attached,
2445 .cmd = "Attached",
2446 },
2447 {
2448 .handler = handle_query_qemu_supported,
2449 .cmd = "qemu.Supported",
2450 },
Jon Doronab4752e2019-05-29 09:41:48 +03002451#ifndef CONFIG_USER_ONLY
2452 {
2453 .handler = handle_query_qemu_phy_mem_mode,
2454 .cmd = "qemu.PhyMemMode",
2455 },
2456#endif
Jon Doron2704efa2019-05-29 09:41:45 +03002457};
2458
Philippe Mathieu-Daudé305bea02021-05-20 18:42:59 +01002459static const GdbCmdParseEntry gdb_gen_set_table[] = {
Jon Doron2704efa2019-05-29 09:41:45 +03002460 /* Order is important if has same prefix */
2461 {
2462 .handler = handle_set_qemu_sstep,
2463 .cmd = "qemu.sstep:",
2464 .cmd_startswith = 1,
2465 .schema = "l0"
2466 },
Jon Doronab4752e2019-05-29 09:41:48 +03002467#ifndef CONFIG_USER_ONLY
2468 {
2469 .handler = handle_set_qemu_phy_mem_mode,
2470 .cmd = "qemu.PhyMemMode:",
2471 .cmd_startswith = 1,
2472 .schema = "l0"
2473 },
2474#endif
Jon Doron2704efa2019-05-29 09:41:45 +03002475};
2476
Alex Bennée26a16182021-05-25 09:24:14 +01002477static void handle_gen_query(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03002478{
Alex Bennée26a16182021-05-25 09:24:14 +01002479 if (!params->len) {
Jon Doron2704efa2019-05-29 09:41:45 +03002480 return;
2481 }
2482
Alex Bennée26a16182021-05-25 09:24:14 +01002483 if (!process_string_cmd(NULL, get_param(params, 0)->data,
Jon Doron2704efa2019-05-29 09:41:45 +03002484 gdb_gen_query_set_common_table,
2485 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2486 return;
2487 }
2488
Alex Bennée26a16182021-05-25 09:24:14 +01002489 if (process_string_cmd(NULL, get_param(params, 0)->data,
Jon Doron2704efa2019-05-29 09:41:45 +03002490 gdb_gen_query_table,
2491 ARRAY_SIZE(gdb_gen_query_table))) {
Alex Bennéea346af32020-03-16 17:21:34 +00002492 put_packet("");
Jon Doron2704efa2019-05-29 09:41:45 +03002493 }
2494}
2495
Alex Bennée26a16182021-05-25 09:24:14 +01002496static void handle_gen_set(GArray *params, void *user_ctx)
Jon Doron2704efa2019-05-29 09:41:45 +03002497{
Alex Bennée26a16182021-05-25 09:24:14 +01002498 if (!params->len) {
Jon Doron2704efa2019-05-29 09:41:45 +03002499 return;
2500 }
2501
Alex Bennée26a16182021-05-25 09:24:14 +01002502 if (!process_string_cmd(NULL, get_param(params, 0)->data,
Jon Doron2704efa2019-05-29 09:41:45 +03002503 gdb_gen_query_set_common_table,
2504 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2505 return;
2506 }
2507
Alex Bennée26a16182021-05-25 09:24:14 +01002508 if (process_string_cmd(NULL, get_param(params, 0)->data,
Jon Doron2704efa2019-05-29 09:41:45 +03002509 gdb_gen_set_table,
2510 ARRAY_SIZE(gdb_gen_set_table))) {
Alex Bennéea346af32020-03-16 17:21:34 +00002511 put_packet("");
Jon Doron2704efa2019-05-29 09:41:45 +03002512 }
2513}
2514
Alex Bennée26a16182021-05-25 09:24:14 +01002515static void handle_target_halt(GArray *params, void *user_ctx)
Jon Doron7009d572019-05-29 09:41:46 +03002516{
Alex Bennée308f9e82020-03-16 17:21:35 +00002517 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
2518 gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf);
2519 g_string_append_c(gdbserver_state.str_buf, ';');
2520 put_strbuf();
Jon Doron7009d572019-05-29 09:41:46 +03002521 /*
2522 * Remove all the breakpoints when this query is issued,
2523 * because gdb is doing an initial connect and the state
2524 * should be cleaned up.
2525 */
2526 gdb_breakpoint_remove_all();
2527}
2528
Alex Bennéea346af32020-03-16 17:21:34 +00002529static int gdb_handle_packet(const char *line_buf)
Jon Doron2704efa2019-05-29 09:41:45 +03002530{
Jon Doron3e2c1262019-05-29 09:41:30 +03002531 const GdbCmdParseEntry *cmd_parser = NULL;
ths3b46e622007-09-17 08:09:54 +00002532
Doug Gale5c9522b2017-12-02 20:30:37 -05002533 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01002534
Jon Doron3f1cbac2019-05-29 09:41:47 +03002535 switch (line_buf[0]) {
Luc Michel53fd6552019-01-07 15:23:46 +00002536 case '!':
Alex Bennéea346af32020-03-16 17:21:34 +00002537 put_packet("OK");
Luc Michel53fd6552019-01-07 15:23:46 +00002538 break;
bellard858693c2004-03-31 18:52:07 +00002539 case '?':
Jon Doron7009d572019-05-29 09:41:46 +03002540 {
2541 static const GdbCmdParseEntry target_halted_cmd_desc = {
2542 .handler = handle_target_halt,
2543 .cmd = "?",
2544 .cmd_startswith = 1
2545 };
2546 cmd_parser = &target_halted_cmd_desc;
2547 }
bellard858693c2004-03-31 18:52:07 +00002548 break;
2549 case 'c':
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002550 {
2551 static const GdbCmdParseEntry continue_cmd_desc = {
2552 .handler = handle_continue,
2553 .cmd = "c",
2554 .cmd_startswith = 1,
2555 .schema = "L0"
2556 };
2557 cmd_parser = &continue_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002558 }
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002559 break;
edgar_igl1f487ee2008-05-17 22:20:53 +00002560 case 'C':
Jon Doronccc47d52019-05-29 09:41:33 +03002561 {
2562 static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
2563 .handler = handle_cont_with_sig,
2564 .cmd = "C",
2565 .cmd_startswith = 1,
2566 .schema = "l0"
2567 };
2568 cmd_parser = &cont_with_sig_cmd_desc;
2569 }
2570 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002571 case 'v':
Jon Doron8536ec02019-05-29 09:41:44 +03002572 {
2573 static const GdbCmdParseEntry v_cmd_desc = {
2574 .handler = handle_v_commands,
2575 .cmd = "v",
2576 .cmd_startswith = 1,
2577 .schema = "s0"
2578 };
2579 cmd_parser = &v_cmd_desc;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002580 }
Jon Doron8536ec02019-05-29 09:41:44 +03002581 break;
edgar_igl7d03f822008-05-17 18:58:29 +00002582 case 'k':
2583 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002584 error_report("QEMU: Terminated via GDBstub");
Alex Bennéeb9e10c62021-01-08 22:42:45 +00002585 gdb_exit(0);
edgar_igl7d03f822008-05-17 18:58:29 +00002586 exit(0);
2587 case 'D':
Jon Doron3e2c1262019-05-29 09:41:30 +03002588 {
2589 static const GdbCmdParseEntry detach_cmd_desc = {
2590 .handler = handle_detach,
2591 .cmd = "D",
2592 .cmd_startswith = 1,
2593 .schema = "?.l0"
2594 };
2595 cmd_parser = &detach_cmd_desc;
Luc Michel546f3c62019-01-07 15:23:46 +00002596 }
edgar_igl7d03f822008-05-17 18:58:29 +00002597 break;
bellard858693c2004-03-31 18:52:07 +00002598 case 's':
Jon Doron933f80d2019-05-29 09:41:43 +03002599 {
2600 static const GdbCmdParseEntry step_cmd_desc = {
2601 .handler = handle_step,
2602 .cmd = "s",
2603 .cmd_startswith = 1,
2604 .schema = "L0"
2605 };
2606 cmd_parser = &step_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002607 }
Jon Doron933f80d2019-05-29 09:41:43 +03002608 break;
Pavel Dovgalyukfda84582020-10-03 20:13:43 +03002609 case 'b':
2610 {
2611 static const GdbCmdParseEntry backward_cmd_desc = {
2612 .handler = handle_backward,
2613 .cmd = "b",
2614 .cmd_startswith = 1,
2615 .schema = "o0"
2616 };
2617 cmd_parser = &backward_cmd_desc;
2618 }
2619 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002620 case 'F':
2621 {
Jon Doron4b20fab2019-05-29 09:41:42 +03002622 static const GdbCmdParseEntry file_io_cmd_desc = {
2623 .handler = handle_file_io,
2624 .cmd = "F",
2625 .cmd_startswith = 1,
2626 .schema = "L,L,o0"
2627 };
2628 cmd_parser = &file_io_cmd_desc;
pbrooka2d1eba2007-01-28 03:10:55 +00002629 }
2630 break;
bellard858693c2004-03-31 18:52:07 +00002631 case 'g':
Jon Doron397d1372019-05-29 09:41:41 +03002632 {
2633 static const GdbCmdParseEntry read_all_regs_cmd_desc = {
2634 .handler = handle_read_all_regs,
2635 .cmd = "g",
2636 .cmd_startswith = 1
2637 };
2638 cmd_parser = &read_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002639 }
bellard858693c2004-03-31 18:52:07 +00002640 break;
2641 case 'G':
Jon Doron287ca122019-05-29 09:41:40 +03002642 {
2643 static const GdbCmdParseEntry write_all_regs_cmd_desc = {
2644 .handler = handle_write_all_regs,
2645 .cmd = "G",
2646 .cmd_startswith = 1,
2647 .schema = "s0"
2648 };
2649 cmd_parser = &write_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002650 }
bellard858693c2004-03-31 18:52:07 +00002651 break;
2652 case 'm':
Jon Doronda92e232019-05-29 09:41:39 +03002653 {
2654 static const GdbCmdParseEntry read_mem_cmd_desc = {
2655 .handler = handle_read_mem,
2656 .cmd = "m",
2657 .cmd_startswith = 1,
2658 .schema = "L,L0"
2659 };
2660 cmd_parser = &read_mem_cmd_desc;
bellard6f970bd2005-12-05 19:55:19 +00002661 }
bellard858693c2004-03-31 18:52:07 +00002662 break;
2663 case 'M':
Jon Doroncc0ecc72019-05-29 09:41:38 +03002664 {
2665 static const GdbCmdParseEntry write_mem_cmd_desc = {
2666 .handler = handle_write_mem,
2667 .cmd = "M",
2668 .cmd_startswith = 1,
2669 .schema = "L,L:s0"
2670 };
2671 cmd_parser = &write_mem_cmd_desc;
Fabien Chouteau44520db2011-09-08 12:48:16 +02002672 }
bellard858693c2004-03-31 18:52:07 +00002673 break;
pbrook56aebc82008-10-11 17:55:29 +00002674 case 'p':
Jon Doron5d0e57b2019-05-29 09:41:37 +03002675 {
2676 static const GdbCmdParseEntry get_reg_cmd_desc = {
2677 .handler = handle_get_reg,
2678 .cmd = "p",
2679 .cmd_startswith = 1,
2680 .schema = "L0"
2681 };
2682 cmd_parser = &get_reg_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002683 }
2684 break;
2685 case 'P':
Jon Doron62b33202019-05-29 09:41:36 +03002686 {
2687 static const GdbCmdParseEntry set_reg_cmd_desc = {
2688 .handler = handle_set_reg,
2689 .cmd = "P",
2690 .cmd_startswith = 1,
2691 .schema = "L?s0"
2692 };
2693 cmd_parser = &set_reg_cmd_desc;
2694 }
pbrook56aebc82008-10-11 17:55:29 +00002695 break;
bellard858693c2004-03-31 18:52:07 +00002696 case 'Z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002697 {
2698 static const GdbCmdParseEntry insert_bp_cmd_desc = {
2699 .handler = handle_insert_bp,
2700 .cmd = "Z",
2701 .cmd_startswith = 1,
2702 .schema = "l?L?L0"
2703 };
2704 cmd_parser = &insert_bp_cmd_desc;
2705 }
2706 break;
bellard858693c2004-03-31 18:52:07 +00002707 case 'z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002708 {
2709 static const GdbCmdParseEntry remove_bp_cmd_desc = {
2710 .handler = handle_remove_bp,
2711 .cmd = "z",
2712 .cmd_startswith = 1,
2713 .schema = "l?L?L0"
2714 };
2715 cmd_parser = &remove_bp_cmd_desc;
2716 }
bellard858693c2004-03-31 18:52:07 +00002717 break;
aliguori880a7572008-11-18 20:30:24 +00002718 case 'H':
Jon Doron3a9651d2019-05-29 09:41:34 +03002719 {
2720 static const GdbCmdParseEntry set_thread_cmd_desc = {
2721 .handler = handle_set_thread,
2722 .cmd = "H",
2723 .cmd_startswith = 1,
2724 .schema = "o.t0"
2725 };
2726 cmd_parser = &set_thread_cmd_desc;
aliguori880a7572008-11-18 20:30:24 +00002727 }
2728 break;
2729 case 'T':
Jon Doron44ffded2019-05-29 09:41:31 +03002730 {
2731 static const GdbCmdParseEntry thread_alive_cmd_desc = {
2732 .handler = handle_thread_alive,
2733 .cmd = "T",
2734 .cmd_startswith = 1,
2735 .schema = "t0"
2736 };
2737 cmd_parser = &thread_alive_cmd_desc;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07002738 }
aliguori880a7572008-11-18 20:30:24 +00002739 break;
pbrook978efd62006-06-17 18:30:42 +00002740 case 'q':
Jon Doron2704efa2019-05-29 09:41:45 +03002741 {
2742 static const GdbCmdParseEntry gen_query_cmd_desc = {
2743 .handler = handle_gen_query,
2744 .cmd = "q",
2745 .cmd_startswith = 1,
2746 .schema = "s0"
2747 };
2748 cmd_parser = &gen_query_cmd_desc;
2749 }
2750 break;
edgar_igl60897d32008-05-09 08:25:14 +00002751 case 'Q':
Jon Doron2704efa2019-05-29 09:41:45 +03002752 {
2753 static const GdbCmdParseEntry gen_set_cmd_desc = {
2754 .handler = handle_gen_set,
2755 .cmd = "Q",
2756 .cmd_startswith = 1,
2757 .schema = "s0"
2758 };
2759 cmd_parser = &gen_set_cmd_desc;
edgar_igl60897d32008-05-09 08:25:14 +00002760 }
Jon Doron2704efa2019-05-29 09:41:45 +03002761 break;
bellard858693c2004-03-31 18:52:07 +00002762 default:
bellard858693c2004-03-31 18:52:07 +00002763 /* put empty packet */
Alex Bennéea346af32020-03-16 17:21:34 +00002764 put_packet("");
bellard858693c2004-03-31 18:52:07 +00002765 break;
2766 }
Jon Doron3e2c1262019-05-29 09:41:30 +03002767
Ramiro Polla2bdec392019-08-05 21:09:01 +02002768 if (cmd_parser) {
Alex Bennéea346af32020-03-16 17:21:34 +00002769 run_cmd_parser(line_buf, cmd_parser);
Ramiro Polla2bdec392019-08-05 21:09:01 +02002770 }
Jon Doron3e2c1262019-05-29 09:41:30 +03002771
bellard858693c2004-03-31 18:52:07 +00002772 return RS_IDLE;
2773}
2774
Andreas Färber64f6b342013-05-27 02:06:09 +02002775void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00002776{
Alex Bennéea346af32020-03-16 17:21:34 +00002777 GDBProcess *p = gdb_get_cpu_process(cpu);
Luc Michel160d8582019-01-07 15:23:46 +00002778
2779 if (!p->attached) {
2780 /*
2781 * Having a stop CPU corresponding to a process that is not attached
2782 * confuses GDB. So we ignore the request.
2783 */
2784 return;
2785 }
2786
Alex Bennée8d98c442020-03-16 17:21:33 +00002787 gdbserver_state.c_cpu = cpu;
2788 gdbserver_state.g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00002789}
2790
bellard1fddef42005-04-17 19:16:13 +00002791#ifndef CONFIG_USER_ONLY
Philippe Mathieu-Daudé538f0492021-01-11 16:20:20 +01002792static void gdb_vm_state_change(void *opaque, bool running, RunState state)
bellard858693c2004-03-31 18:52:07 +00002793{
Alex Bennéea346af32020-03-16 17:21:34 +00002794 CPUState *cpu = gdbserver_state.c_cpu;
Alex Bennée308f9e82020-03-16 17:21:35 +00002795 g_autoptr(GString) buf = g_string_new(NULL);
2796 g_autoptr(GString) tid = g_string_new(NULL);
aliguorid6fc1b32008-11-18 19:55:44 +00002797 const char *type;
bellard858693c2004-03-31 18:52:07 +00002798 int ret;
2799
Alex Bennéea346af32020-03-16 17:21:34 +00002800 if (running || gdbserver_state.state == RS_INACTIVE) {
Meador Ingecdb432b2012-03-15 17:49:45 +00002801 return;
2802 }
2803 /* Is there a GDB syscall waiting to be sent? */
Alex Bennéea346af32020-03-16 17:21:34 +00002804 if (gdbserver_state.current_syscall_cb) {
2805 put_packet(gdbserver_state.syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00002806 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01002807 }
Luc Michel95567c22019-01-07 15:23:46 +00002808
2809 if (cpu == NULL) {
2810 /* No process attached */
2811 return;
2812 }
2813
Alex Bennée308f9e82020-03-16 17:21:35 +00002814 gdb_append_thread_id(cpu, tid);
Luc Michel95567c22019-01-07 15:23:46 +00002815
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002816 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002817 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02002818 if (cpu->watchpoint_hit) {
2819 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00002820 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00002821 type = "r";
2822 break;
aliguoria1d1bb32008-11-18 20:07:32 +00002823 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00002824 type = "a";
2825 break;
2826 default:
2827 type = "";
2828 break;
2829 }
Doug Gale5c9522b2017-12-02 20:30:37 -05002830 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
2831 (target_ulong)cpu->watchpoint_hit->vaddr);
Alex Bennée308f9e82020-03-16 17:21:35 +00002832 g_string_printf(buf, "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
2833 GDB_SIGNAL_TRAP, tid->str, type,
2834 (target_ulong)cpu->watchpoint_hit->vaddr);
Andreas Färberff4700b2013-08-26 18:23:18 +02002835 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01002836 goto send_packet;
Doug Gale5c9522b2017-12-02 20:30:37 -05002837 } else {
2838 trace_gdbstub_hit_break();
pbrook6658ffb2007-03-16 23:58:11 +00002839 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07002840 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00002841 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01002842 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002843 case RUN_STATE_PAUSED:
Doug Gale5c9522b2017-12-02 20:30:37 -05002844 trace_gdbstub_hit_paused();
aliguori9781e042009-01-22 17:15:29 +00002845 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01002846 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002847 case RUN_STATE_SHUTDOWN:
Doug Gale5c9522b2017-12-02 20:30:37 -05002848 trace_gdbstub_hit_shutdown();
Jan Kiszka425189a2011-03-22 11:02:09 +01002849 ret = GDB_SIGNAL_QUIT;
2850 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002851 case RUN_STATE_IO_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002852 trace_gdbstub_hit_io_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002853 ret = GDB_SIGNAL_IO;
2854 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002855 case RUN_STATE_WATCHDOG:
Doug Gale5c9522b2017-12-02 20:30:37 -05002856 trace_gdbstub_hit_watchdog();
Jan Kiszka425189a2011-03-22 11:02:09 +01002857 ret = GDB_SIGNAL_ALRM;
2858 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002859 case RUN_STATE_INTERNAL_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002860 trace_gdbstub_hit_internal_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002861 ret = GDB_SIGNAL_ABRT;
2862 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002863 case RUN_STATE_SAVE_VM:
2864 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01002865 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002866 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01002867 ret = GDB_SIGNAL_XCPU;
2868 break;
2869 default:
Doug Gale5c9522b2017-12-02 20:30:37 -05002870 trace_gdbstub_hit_unknown(state);
Jan Kiszka425189a2011-03-22 11:02:09 +01002871 ret = GDB_SIGNAL_UNKNOWN;
2872 break;
bellardbbeb7b52006-04-23 18:42:15 +00002873 }
Jan Kiszka226d0072015-07-24 18:52:31 +02002874 gdb_set_stop_cpu(cpu);
Alex Bennée308f9e82020-03-16 17:21:35 +00002875 g_string_printf(buf, "T%02xthread:%s;", ret, tid->str);
Jan Kiszka425189a2011-03-22 11:02:09 +01002876
2877send_packet:
Alex Bennée308f9e82020-03-16 17:21:35 +00002878 put_packet(buf->str);
Jan Kiszka425189a2011-03-22 11:02:09 +01002879
2880 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02002881 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00002882}
bellard1fddef42005-04-17 19:16:13 +00002883#endif
bellard858693c2004-03-31 18:52:07 +00002884
pbrooka2d1eba2007-01-28 03:10:55 +00002885/* Send a gdb syscall request.
2886 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00002887 %x - target_ulong argument printed in hex.
2888 %lx - 64-bit argument printed in hex.
2889 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01002890void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00002891{
pbrooka2d1eba2007-01-28 03:10:55 +00002892 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00002893 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00002894 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00002895 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00002896
Peter Maydellebf1b4c2022-06-10 14:32:36 +01002897 if (!gdb_attached()) {
pbrooka2d1eba2007-01-28 03:10:55 +00002898 return;
Alex Bennéea346af32020-03-16 17:21:34 +00002899 }
Alex Bennée8d98c442020-03-16 17:21:33 +00002900
2901 gdbserver_state.current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00002902#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002903 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00002904#endif
Alex Bennée8d98c442020-03-16 17:21:33 +00002905 p = &gdbserver_state.syscall_buf[0];
2906 p_end = &gdbserver_state.syscall_buf[sizeof(gdbserver_state.syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00002907 *(p++) = 'F';
2908 while (*fmt) {
2909 if (*fmt == '%') {
2910 fmt++;
2911 switch (*fmt++) {
2912 case 'x':
2913 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002914 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00002915 break;
pbrooka87295e2007-05-26 15:09:38 +00002916 case 'l':
2917 if (*(fmt++) != 'x')
2918 goto bad_format;
2919 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00002920 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00002921 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002922 case 's':
2923 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002924 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00002925 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00002926 break;
2927 default:
pbrooka87295e2007-05-26 15:09:38 +00002928 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002929 error_report("gdbstub: Bad syscall format string '%s'",
2930 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00002931 break;
2932 }
2933 } else {
2934 *(p++) = *(fmt++);
2935 }
2936 }
pbrook8a93e022007-08-06 13:19:15 +00002937 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00002938#ifdef CONFIG_USER_ONLY
Alex Bennéea346af32020-03-16 17:21:34 +00002939 put_packet(gdbserver_state.syscall_buf);
Peter Maydell4f710862018-05-15 19:19:58 +01002940 /* Return control to gdb for it to process the syscall request.
2941 * Since the protocol requires that gdb hands control back to us
2942 * using a "here are the results" F packet, we don't need to check
2943 * gdb_handlesig's return value (which is the signal to deliver if
2944 * execution was resumed via a continue packet).
2945 */
Alex Bennée8d98c442020-03-16 17:21:33 +00002946 gdb_handlesig(gdbserver_state.c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00002947#else
Meador Ingecdb432b2012-03-15 17:49:45 +00002948 /* In this case wait to send the syscall packet until notification that
2949 the CPU has stopped. This must be done because if the packet is sent
2950 now the reply from the syscall request could be received while the CPU
2951 is still in the running state, which can cause packets to be dropped
2952 and state transition 'T' packets to be sent while the syscall is still
2953 being processed. */
Alex Bennée8d98c442020-03-16 17:21:33 +00002954 qemu_cpu_kick(gdbserver_state.c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00002955#endif
2956}
2957
Peter Maydell19239b32015-09-07 10:39:27 +01002958void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
2959{
2960 va_list va;
2961
2962 va_start(va, fmt);
2963 gdb_do_syscallv(cb, fmt, va);
2964 va_end(va);
2965}
2966
Alex Bennéea346af32020-03-16 17:21:34 +00002967static void gdb_read_byte(uint8_t ch)
bellard858693c2004-03-31 18:52:07 +00002968{
ths60fe76f2007-12-16 03:02:09 +00002969 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00002970
bellard1fddef42005-04-17 19:16:13 +00002971#ifndef CONFIG_USER_ONLY
Damien Hedded116e812020-03-16 17:21:53 +00002972 if (gdbserver_state.last_packet->len) {
pbrook4046d912007-01-28 01:53:16 +00002973 /* Waiting for a response to the last packet. If we see the start
2974 of a new command then abandon the previous response. */
2975 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002976 trace_gdbstub_err_got_nack();
Damien Hedded116e812020-03-16 17:21:53 +00002977 put_buffer(gdbserver_state.last_packet->data,
2978 gdbserver_state.last_packet->len);
Alex Bennée118e2262017-07-12 11:52:13 +01002979 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002980 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01002981 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002982 trace_gdbstub_io_got_unexpected(ch);
pbrook4046d912007-01-28 01:53:16 +00002983 }
Alex Bennée118e2262017-07-12 11:52:13 +01002984
Damien Hedded116e812020-03-16 17:21:53 +00002985 if (ch == '+' || ch == '$') {
2986 g_byte_array_set_size(gdbserver_state.last_packet, 0);
2987 }
pbrook4046d912007-01-28 01:53:16 +00002988 if (ch != '$')
2989 return;
2990 }
Luiz Capitulino13548692011-07-29 15:36:43 -03002991 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00002992 /* when the CPU is running, we cannot do anything except stop
2993 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002994 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00002995 } else
bellard1fddef42005-04-17 19:16:13 +00002996#endif
bellard41625032005-04-24 10:07:11 +00002997 {
Alex Bennéea346af32020-03-16 17:21:34 +00002998 switch(gdbserver_state.state) {
bellard858693c2004-03-31 18:52:07 +00002999 case RS_IDLE:
3000 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04003001 /* start of command packet */
Alex Bennéea346af32020-03-16 17:21:34 +00003002 gdbserver_state.line_buf_index = 0;
3003 gdbserver_state.line_sum = 0;
3004 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04003005 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02003006 trace_gdbstub_err_garbage(ch);
bellard4c3a88a2003-07-26 12:06:08 +00003007 }
3008 break;
bellard858693c2004-03-31 18:52:07 +00003009 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04003010 if (ch == '}') {
3011 /* start escape sequence */
Alex Bennéea346af32020-03-16 17:21:34 +00003012 gdbserver_state.state = RS_GETLINE_ESC;
3013 gdbserver_state.line_sum += ch;
Doug Gale4bf43122017-05-01 12:22:10 -04003014 } else if (ch == '*') {
3015 /* start run length encoding sequence */
Alex Bennéea346af32020-03-16 17:21:34 +00003016 gdbserver_state.state = RS_GETLINE_RLE;
3017 gdbserver_state.line_sum += ch;
Doug Gale4bf43122017-05-01 12:22:10 -04003018 } else if (ch == '#') {
3019 /* end of command, start of checksum*/
Alex Bennéea346af32020-03-16 17:21:34 +00003020 gdbserver_state.state = RS_CHKSUM1;
3021 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05003022 trace_gdbstub_err_overrun();
Alex Bennéea346af32020-03-16 17:21:34 +00003023 gdbserver_state.state = RS_IDLE;
bellard858693c2004-03-31 18:52:07 +00003024 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04003025 /* unescaped command character */
Alex Bennéea346af32020-03-16 17:21:34 +00003026 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch;
3027 gdbserver_state.line_sum += ch;
Doug Gale4bf43122017-05-01 12:22:10 -04003028 }
3029 break;
3030 case RS_GETLINE_ESC:
3031 if (ch == '#') {
3032 /* unexpected end of command in escape sequence */
Alex Bennéea346af32020-03-16 17:21:34 +00003033 gdbserver_state.state = RS_CHKSUM1;
3034 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04003035 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05003036 trace_gdbstub_err_overrun();
Alex Bennéea346af32020-03-16 17:21:34 +00003037 gdbserver_state.state = RS_IDLE;
Doug Gale4bf43122017-05-01 12:22:10 -04003038 } else {
3039 /* parse escaped character and leave escape state */
Alex Bennéea346af32020-03-16 17:21:34 +00003040 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20;
3041 gdbserver_state.line_sum += ch;
3042 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04003043 }
3044 break;
3045 case RS_GETLINE_RLE:
Markus Armbruster046aba12019-05-14 20:03:08 +02003046 /*
3047 * Run-length encoding is explained in "Debugging with GDB /
3048 * Appendix E GDB Remote Serial Protocol / Overview".
3049 */
3050 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
Doug Gale4bf43122017-05-01 12:22:10 -04003051 /* invalid RLE count encoding */
Markus Armbruster33c846e2019-05-14 20:03:09 +02003052 trace_gdbstub_err_invalid_repeat(ch);
Alex Bennéea346af32020-03-16 17:21:34 +00003053 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04003054 } else {
3055 /* decode repeat length */
Markus Armbruster33c846e2019-05-14 20:03:09 +02003056 int repeat = ch - ' ' + 3;
Alex Bennéea346af32020-03-16 17:21:34 +00003057 if (gdbserver_state.line_buf_index + repeat >= sizeof(gdbserver_state.line_buf) - 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04003058 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05003059 trace_gdbstub_err_overrun();
Alex Bennéea346af32020-03-16 17:21:34 +00003060 gdbserver_state.state = RS_IDLE;
3061 } else if (gdbserver_state.line_buf_index < 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04003062 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05003063 trace_gdbstub_err_invalid_rle();
Alex Bennéea346af32020-03-16 17:21:34 +00003064 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04003065 } else {
3066 /* repeat the last character */
Alex Bennéea346af32020-03-16 17:21:34 +00003067 memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index,
3068 gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat);
3069 gdbserver_state.line_buf_index += repeat;
3070 gdbserver_state.line_sum += ch;
3071 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04003072 }
bellard858693c2004-03-31 18:52:07 +00003073 }
3074 break;
3075 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04003076 /* get high hex digit of checksum */
3077 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02003078 trace_gdbstub_err_checksum_invalid(ch);
Alex Bennéea346af32020-03-16 17:21:34 +00003079 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04003080 break;
3081 }
Alex Bennéea346af32020-03-16 17:21:34 +00003082 gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0';
3083 gdbserver_state.line_csum = fromhex(ch) << 4;
3084 gdbserver_state.state = RS_CHKSUM2;
bellard858693c2004-03-31 18:52:07 +00003085 break;
3086 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04003087 /* get low hex digit of checksum */
3088 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02003089 trace_gdbstub_err_checksum_invalid(ch);
Alex Bennéea346af32020-03-16 17:21:34 +00003090 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04003091 break;
bellard858693c2004-03-31 18:52:07 +00003092 }
Alex Bennéea346af32020-03-16 17:21:34 +00003093 gdbserver_state.line_csum |= fromhex(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04003094
Alex Bennéea346af32020-03-16 17:21:34 +00003095 if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) {
3096 trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04003097 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00003098 reply = '-';
Alex Bennéea346af32020-03-16 17:21:34 +00003099 put_buffer(&reply, 1);
3100 gdbserver_state.state = RS_IDLE;
bellard858693c2004-03-31 18:52:07 +00003101 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04003102 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00003103 reply = '+';
Alex Bennéea346af32020-03-16 17:21:34 +00003104 put_buffer(&reply, 1);
3105 gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf);
bellard858693c2004-03-31 18:52:07 +00003106 }
bellardb4608c02003-06-27 17:34:32 +00003107 break;
pbrooka2d1eba2007-01-28 03:10:55 +00003108 default:
3109 abort();
bellardb4608c02003-06-27 17:34:32 +00003110 }
3111 }
bellard858693c2004-03-31 18:52:07 +00003112}
3113
Paul Brook0e1c9c52010-06-16 13:03:51 +01003114/* Tell the remote gdb that the process has exited. */
Alex Bennéead9dcb22021-01-08 22:42:43 +00003115void gdb_exit(int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01003116{
Paul Brook0e1c9c52010-06-16 13:03:51 +01003117 char buf[4];
3118
Alex Bennée8d98c442020-03-16 17:21:33 +00003119 if (!gdbserver_state.init) {
Paul Brook0e1c9c52010-06-16 13:03:51 +01003120 return;
3121 }
3122#ifdef CONFIG_USER_ONLY
Alex Bennéefcedd922020-04-30 20:01:19 +01003123 if (gdbserver_state.socket_path) {
3124 unlink(gdbserver_state.socket_path);
3125 }
Alex Bennéee0a1e202020-04-30 20:01:18 +01003126 if (gdbserver_state.fd < 0) {
Paul Brook0e1c9c52010-06-16 13:03:51 +01003127 return;
3128 }
3129#endif
3130
Doug Gale5c9522b2017-12-02 20:30:37 -05003131 trace_gdbstub_op_exiting((uint8_t)code);
3132
Paul Brook0e1c9c52010-06-16 13:03:51 +01003133 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
Alex Bennéea346af32020-03-16 17:21:34 +00003134 put_packet(buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01003135
3136#ifndef CONFIG_USER_ONLY
Alex Bennée8d98c442020-03-16 17:21:33 +00003137 qemu_chr_fe_deinit(&gdbserver_state.chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01003138#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01003139}
3140
Luc Michel8f468632019-01-07 15:23:45 +00003141/*
3142 * Create the process that will contain all the "orphan" CPUs (that are not
3143 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
3144 * be attachable and thus will be invisible to the user.
3145 */
3146static void create_default_process(GDBState *s)
3147{
3148 GDBProcess *process;
3149 int max_pid = 0;
3150
Alex Bennéea346af32020-03-16 17:21:34 +00003151 if (gdbserver_state.process_num) {
Luc Michel8f468632019-01-07 15:23:45 +00003152 max_pid = s->processes[s->process_num - 1].pid;
3153 }
3154
3155 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
3156 process = &s->processes[s->process_num - 1];
3157
3158 /* We need an available PID slot for this process */
3159 assert(max_pid < UINT32_MAX);
3160
3161 process->pid = max_pid + 1;
3162 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00003163 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00003164}
3165
bellard1fddef42005-04-17 19:16:13 +00003166#ifdef CONFIG_USER_ONLY
3167int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02003168gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00003169{
Andreas Färber5ca666c2013-06-24 19:20:57 +02003170 char buf[256];
3171 int n;
bellard1fddef42005-04-17 19:16:13 +00003172
Alex Bennéee0a1e202020-04-30 20:01:18 +01003173 if (!gdbserver_state.init || gdbserver_state.fd < 0) {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003174 return sig;
bellard1fddef42005-04-17 19:16:13 +00003175 }
3176
Andreas Färber5ca666c2013-06-24 19:20:57 +02003177 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02003178 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07003179 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00003180
Andreas Färber5ca666c2013-06-24 19:20:57 +02003181 if (sig != 0) {
Pavel Labath4a82be72021-10-26 11:22:34 +01003182 gdb_set_stop_cpu(cpu);
3183 g_string_printf(gdbserver_state.str_buf,
3184 "T%02xthread:", target_signal_to_gdb(sig));
3185 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
3186 g_string_append_c(gdbserver_state.str_buf, ';');
3187 put_strbuf();
Andreas Färber5ca666c2013-06-24 19:20:57 +02003188 }
3189 /* put_packet() might have detected that the peer terminated the
3190 connection. */
Alex Bennée8d98c442020-03-16 17:21:33 +00003191 if (gdbserver_state.fd < 0) {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003192 return sig;
3193 }
3194
3195 sig = 0;
Alex Bennée8d98c442020-03-16 17:21:33 +00003196 gdbserver_state.state = RS_IDLE;
3197 gdbserver_state.running_state = 0;
3198 while (gdbserver_state.running_state == 0) {
3199 n = read(gdbserver_state.fd, buf, 256);
Andreas Färber5ca666c2013-06-24 19:20:57 +02003200 if (n > 0) {
3201 int i;
3202
3203 for (i = 0; i < n; i++) {
Alex Bennéea346af32020-03-16 17:21:34 +00003204 gdb_read_byte(buf[i]);
Andreas Färber5ca666c2013-06-24 19:20:57 +02003205 }
Peter Wu5819e3e2016-06-05 16:35:48 +02003206 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003207 /* XXX: Connection closed. Should probably wait for another
3208 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02003209 if (n == 0) {
Alex Bennée8d98c442020-03-16 17:21:33 +00003210 close(gdbserver_state.fd);
Peter Wu5819e3e2016-06-05 16:35:48 +02003211 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003212 gdbserver_state.fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02003213 return sig;
bellard1fddef42005-04-17 19:16:13 +00003214 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02003215 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003216 sig = gdbserver_state.signal;
3217 gdbserver_state.signal = 0;
Andreas Färber5ca666c2013-06-24 19:20:57 +02003218 return sig;
bellard1fddef42005-04-17 19:16:13 +00003219}
bellarde9009672005-04-26 20:42:36 +00003220
aurel32ca587a82008-12-18 22:44:13 +00003221/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01003222void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00003223{
Andreas Färber5ca666c2013-06-24 19:20:57 +02003224 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00003225
Alex Bennéee0a1e202020-04-30 20:01:18 +01003226 if (!gdbserver_state.init || gdbserver_state.fd < 0) {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003227 return;
3228 }
aurel32ca587a82008-12-18 22:44:13 +00003229
Andreas Färber5ca666c2013-06-24 19:20:57 +02003230 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
Alex Bennéea346af32020-03-16 17:21:34 +00003231 put_packet(buf);
aurel32ca587a82008-12-18 22:44:13 +00003232}
bellard1fddef42005-04-17 19:16:13 +00003233
Alex Bennéefcedd922020-04-30 20:01:19 +01003234static void gdb_accept_init(int fd)
3235{
3236 init_gdbserver_state();
3237 create_default_process(&gdbserver_state);
3238 gdbserver_state.processes[0].attached = true;
3239 gdbserver_state.c_cpu = gdb_first_attached_cpu();
3240 gdbserver_state.g_cpu = gdbserver_state.c_cpu;
3241 gdbserver_state.fd = fd;
3242 gdb_has_xml = false;
3243}
3244
3245static bool gdb_accept_socket(int gdb_fd)
3246{
3247 int fd;
3248
3249 for(;;) {
3250 fd = accept(gdb_fd, NULL, NULL);
3251 if (fd < 0 && errno != EINTR) {
3252 perror("accept socket");
3253 return false;
3254 } else if (fd >= 0) {
3255 qemu_set_cloexec(fd);
3256 break;
3257 }
3258 }
3259
3260 gdb_accept_init(fd);
3261 return true;
3262}
3263
3264static int gdbserver_open_socket(const char *path)
3265{
Peter Maydellfdcdf542021-08-13 16:05:04 +01003266 struct sockaddr_un sockaddr = {};
Alex Bennéefcedd922020-04-30 20:01:19 +01003267 int fd, ret;
3268
3269 fd = socket(AF_UNIX, SOCK_STREAM, 0);
3270 if (fd < 0) {
3271 perror("create socket");
3272 return -1;
3273 }
3274
3275 sockaddr.sun_family = AF_UNIX;
3276 pstrcpy(sockaddr.sun_path, sizeof(sockaddr.sun_path) - 1, path);
3277 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
3278 if (ret < 0) {
3279 perror("bind socket");
3280 close(fd);
3281 return -1;
3282 }
3283 ret = listen(fd, 1);
3284 if (ret < 0) {
3285 perror("listen socket");
3286 close(fd);
3287 return -1;
3288 }
3289
3290 return fd;
3291}
3292
3293static bool gdb_accept_tcp(int gdb_fd)
bellard858693c2004-03-31 18:52:07 +00003294{
Peter Maydellfdcdf542021-08-13 16:05:04 +01003295 struct sockaddr_in sockaddr = {};
bellard858693c2004-03-31 18:52:07 +00003296 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09003297 int fd;
bellard858693c2004-03-31 18:52:07 +00003298
3299 for(;;) {
3300 len = sizeof(sockaddr);
Alex Bennéee0a1e202020-04-30 20:01:18 +01003301 fd = accept(gdb_fd, (struct sockaddr *)&sockaddr, &len);
bellard858693c2004-03-31 18:52:07 +00003302 if (fd < 0 && errno != EINTR) {
3303 perror("accept");
Peter Maydell2f652222018-05-14 18:30:44 +01003304 return false;
bellard858693c2004-03-31 18:52:07 +00003305 } else if (fd >= 0) {
Peter Maydellf5bdd782018-05-14 18:30:43 +01003306 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00003307 break;
3308 }
3309 }
3310
3311 /* set short latency */
Peter Maydell2f652222018-05-14 18:30:44 +01003312 if (socket_set_nodelay(fd)) {
3313 perror("setsockopt");
Philippe Mathieu-Daudéead75d82018-05-24 19:34:58 -03003314 close(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01003315 return false;
3316 }
ths3b46e622007-09-17 08:09:54 +00003317
Alex Bennéefcedd922020-04-30 20:01:19 +01003318 gdb_accept_init(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01003319 return true;
bellard858693c2004-03-31 18:52:07 +00003320}
3321
Alex Bennéefcedd922020-04-30 20:01:19 +01003322static int gdbserver_open_port(int port)
bellard858693c2004-03-31 18:52:07 +00003323{
3324 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02003325 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00003326
3327 fd = socket(PF_INET, SOCK_STREAM, 0);
3328 if (fd < 0) {
3329 perror("socket");
3330 return -1;
3331 }
Peter Maydellf5bdd782018-05-14 18:30:43 +01003332 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00003333
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02003334 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00003335
3336 sockaddr.sin_family = AF_INET;
3337 sockaddr.sin_port = htons(port);
3338 sockaddr.sin_addr.s_addr = 0;
3339 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
3340 if (ret < 0) {
3341 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00003342 close(fd);
bellard858693c2004-03-31 18:52:07 +00003343 return -1;
3344 }
Peter Wu96165b92016-05-04 11:32:17 +02003345 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00003346 if (ret < 0) {
3347 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00003348 close(fd);
bellard858693c2004-03-31 18:52:07 +00003349 return -1;
3350 }
Alex Bennéefcedd922020-04-30 20:01:19 +01003351
bellard858693c2004-03-31 18:52:07 +00003352 return fd;
3353}
3354
Alex Bennéefcedd922020-04-30 20:01:19 +01003355int gdbserver_start(const char *port_or_path)
bellard858693c2004-03-31 18:52:07 +00003356{
Alex Bennéefcedd922020-04-30 20:01:19 +01003357 int port = g_ascii_strtoull(port_or_path, NULL, 10);
3358 int gdb_fd;
3359
3360 if (port > 0) {
3361 gdb_fd = gdbserver_open_port(port);
3362 } else {
3363 gdb_fd = gdbserver_open_socket(port_or_path);
3364 }
3365
Alex Bennéee0a1e202020-04-30 20:01:18 +01003366 if (gdb_fd < 0) {
bellard858693c2004-03-31 18:52:07 +00003367 return -1;
Alex Bennéee0a1e202020-04-30 20:01:18 +01003368 }
Alex Bennéefcedd922020-04-30 20:01:19 +01003369
3370 if (port > 0 && gdb_accept_tcp(gdb_fd)) {
3371 return 0;
3372 } else if (gdb_accept_socket(gdb_fd)) {
3373 gdbserver_state.socket_path = g_strdup(port_or_path);
3374 return 0;
Peter Maydell2f652222018-05-14 18:30:44 +01003375 }
Alex Bennéefcedd922020-04-30 20:01:19 +01003376
3377 /* gone wrong */
3378 close(gdb_fd);
3379 return -1;
bellardb4608c02003-06-27 17:34:32 +00003380}
aurel322b1319c2008-12-18 22:44:04 +00003381
3382/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07003383void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00003384{
Alex Bennéee0a1e202020-04-30 20:01:18 +01003385 if (!gdbserver_state.init || gdbserver_state.fd < 0) {
Andreas Färber75a34032013-09-02 16:57:02 +02003386 return;
3387 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003388 close(gdbserver_state.fd);
3389 gdbserver_state.fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02003390 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02003391 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00003392}
pbrook4046d912007-01-28 01:53:16 +00003393#else
thsaa1f17c2007-07-11 22:48:58 +00003394static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00003395{
pbrook56aebc82008-10-11 17:55:29 +00003396 /* We can handle an arbitrarily large amount of data.
3397 Pick the maximum packet size, which is as good as anything. */
3398 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00003399}
3400
thsaa1f17c2007-07-11 22:48:58 +00003401static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00003402{
pbrook4046d912007-01-28 01:53:16 +00003403 int i;
3404
3405 for (i = 0; i < size; i++) {
Alex Bennéea346af32020-03-16 17:21:34 +00003406 gdb_read_byte(buf[i]);
pbrook4046d912007-01-28 01:53:16 +00003407 }
3408}
3409
Philippe Mathieu-Daudé083b2662019-12-18 18:20:09 +01003410static void gdb_chr_event(void *opaque, QEMUChrEvent event)
pbrook4046d912007-01-28 01:53:16 +00003411{
Luc Michel970ed902019-01-07 15:23:46 +00003412 int i;
3413 GDBState *s = (GDBState *) opaque;
3414
pbrook4046d912007-01-28 01:53:16 +00003415 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05303416 case CHR_EVENT_OPENED:
Luc Michel970ed902019-01-07 15:23:46 +00003417 /* Start with first process attached, others detached */
3418 for (i = 0; i < s->process_num; i++) {
3419 s->processes[i].attached = !i;
3420 }
3421
Alex Bennéea346af32020-03-16 17:21:34 +00003422 s->c_cpu = gdb_first_attached_cpu();
Luc Michel970ed902019-01-07 15:23:46 +00003423 s->g_cpu = s->c_cpu;
3424
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003425 vm_stop(RUN_STATE_PAUSED);
Pavel Dovgalyuk56357d82020-10-03 20:14:01 +03003426 replay_gdb_attached();
Andreas Färber5b50e792013-06-29 04:18:45 +02003427 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00003428 break;
3429 default:
3430 break;
3431 }
3432}
3433
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003434static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00003435{
Damien Hedded86b4672020-03-16 17:21:54 +00003436 g_autoptr(GString) hex_buf = g_string_new("O");
3437 memtohex(hex_buf, buf, len);
3438 put_packet(hex_buf->str);
aliguori8a34a0f2009-03-05 23:01:55 +00003439 return len;
3440}
3441
aliguori59030a82009-04-05 18:43:41 +00003442#ifndef _WIN32
3443static void gdb_sigterm_handler(int signal)
3444{
Luiz Capitulino13548692011-07-29 15:36:43 -03003445 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003446 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01003447 }
aliguori59030a82009-04-05 18:43:41 +00003448}
3449#endif
3450
Marc-André Lureau777357d2016-12-07 18:39:10 +03003451static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
3452 bool *be_opened, Error **errp)
3453{
3454 *be_opened = false;
3455}
3456
3457static void char_gdb_class_init(ObjectClass *oc, void *data)
3458{
3459 ChardevClass *cc = CHARDEV_CLASS(oc);
3460
3461 cc->internal = true;
3462 cc->open = gdb_monitor_open;
3463 cc->chr_write = gdb_monitor_write;
3464}
3465
3466#define TYPE_CHARDEV_GDB "chardev-gdb"
3467
3468static const TypeInfo char_gdb_type_info = {
3469 .name = TYPE_CHARDEV_GDB,
3470 .parent = TYPE_CHARDEV,
3471 .class_init = char_gdb_class_init,
3472};
3473
Luc Michel8f468632019-01-07 15:23:45 +00003474static int find_cpu_clusters(Object *child, void *opaque)
3475{
3476 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
3477 GDBState *s = (GDBState *) opaque;
3478 CPUClusterState *cluster = CPU_CLUSTER(child);
3479 GDBProcess *process;
3480
3481 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
3482
3483 process = &s->processes[s->process_num - 1];
3484
3485 /*
3486 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
3487 * runtime, we enforce here that the machine does not use a cluster ID
3488 * that would lead to PID 0.
3489 */
3490 assert(cluster->cluster_id != UINT32_MAX);
3491 process->pid = cluster->cluster_id + 1;
3492 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00003493 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00003494
3495 return 0;
3496 }
3497
3498 return object_child_foreach(child, find_cpu_clusters, opaque);
3499}
3500
3501static int pid_order(const void *a, const void *b)
3502{
3503 GDBProcess *pa = (GDBProcess *) a;
3504 GDBProcess *pb = (GDBProcess *) b;
3505
3506 if (pa->pid < pb->pid) {
3507 return -1;
3508 } else if (pa->pid > pb->pid) {
3509 return 1;
3510 } else {
3511 return 0;
3512 }
3513}
3514
3515static void create_processes(GDBState *s)
3516{
3517 object_child_foreach(object_get_root(), find_cpu_clusters, s);
3518
Alex Bennéea346af32020-03-16 17:21:34 +00003519 if (gdbserver_state.processes) {
Luc Michel8f468632019-01-07 15:23:45 +00003520 /* Sort by PID */
Alex Bennéea346af32020-03-16 17:21:34 +00003521 qsort(gdbserver_state.processes, gdbserver_state.process_num, sizeof(gdbserver_state.processes[0]), pid_order);
Luc Michel8f468632019-01-07 15:23:45 +00003522 }
3523
3524 create_default_process(s);
3525}
3526
aliguori59030a82009-04-05 18:43:41 +00003527int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00003528{
Doug Gale5c9522b2017-12-02 20:30:37 -05003529 trace_gdbstub_op_start(device);
3530
aliguori59030a82009-04-05 18:43:41 +00003531 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003532 Chardev *chr = NULL;
3533 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00003534
Ziyue Yang508b4ec2017-01-18 16:02:41 +08003535 if (!first_cpu) {
3536 error_report("gdbstub: meaningless to attach gdb to a "
3537 "machine without any CPU.");
3538 return -1;
3539 }
3540
Maxim Levitsky12bc5b42021-11-11 12:06:03 +01003541 if (kvm_enabled() && !kvm_supports_guest_debug()) {
3542 error_report("gdbstub: KVM doesn't support guest debugging");
3543 return -1;
3544 }
3545
aliguori59030a82009-04-05 18:43:41 +00003546 if (!device)
3547 return -1;
3548 if (strcmp(device, "none") != 0) {
3549 if (strstart(device, "tcp:", NULL)) {
3550 /* enforce required TCP attributes */
3551 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
Paolo Bonzinia9b13152021-02-25 11:47:52 +01003552 "%s,wait=off,nodelay=on,server=on", device);
aliguori59030a82009-04-05 18:43:41 +00003553 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00003554 }
aliguori59030a82009-04-05 18:43:41 +00003555#ifndef _WIN32
3556 else if (strcmp(device, "stdio") == 0) {
3557 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00003558
aliguori59030a82009-04-05 18:43:41 +00003559 memset(&act, 0, sizeof(act));
3560 act.sa_handler = gdb_sigterm_handler;
3561 sigaction(SIGINT, &act, NULL);
3562 }
3563#endif
Marc-André Lureau95e30b22018-08-22 19:19:42 +02003564 /*
3565 * FIXME: it's a bit weird to allow using a mux chardev here
3566 * and implicitly setup a monitor. We may want to break this.
3567 */
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01003568 chr = qemu_chr_new_noreplay("gdb", device, true, NULL);
aliguori36556b22009-03-28 18:05:53 +00003569 if (!chr)
3570 return -1;
pbrookcfc34752007-02-22 01:48:01 +00003571 }
3572
Alex Bennée8d98c442020-03-16 17:21:33 +00003573 if (!gdbserver_state.init) {
3574 init_gdbserver_state();
pbrook4046d912007-01-28 01:53:16 +00003575
aliguori36556b22009-03-28 18:05:53 +00003576 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
3577
3578 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03003579 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01003580 NULL, NULL, &error_abort);
Kevin Wolf8e9119a2020-02-24 15:30:06 +01003581 monitor_init_hmp(mon_chr, false, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00003582 } else {
Alex Bennée8d98c442020-03-16 17:21:33 +00003583 qemu_chr_fe_deinit(&gdbserver_state.chr, true);
3584 mon_chr = gdbserver_state.mon_chr;
3585 reset_gdbserver_state();
aliguori36556b22009-03-28 18:05:53 +00003586 }
Luc Michel8f468632019-01-07 15:23:45 +00003587
Alex Bennée8d98c442020-03-16 17:21:33 +00003588 create_processes(&gdbserver_state);
Luc Michel8f468632019-01-07 15:23:45 +00003589
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03003590 if (chr) {
Alex Bennée8d98c442020-03-16 17:21:33 +00003591 qemu_chr_fe_init(&gdbserver_state.chr, chr, &error_abort);
3592 qemu_chr_fe_set_handlers(&gdbserver_state.chr, gdb_chr_can_receive,
3593 gdb_chr_receive, gdb_chr_event,
3594 NULL, &gdbserver_state, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03003595 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003596 gdbserver_state.state = chr ? RS_IDLE : RS_INACTIVE;
3597 gdbserver_state.mon_chr = mon_chr;
3598 gdbserver_state.current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00003599
pbrook4046d912007-01-28 01:53:16 +00003600 return 0;
3601}
Marc-André Lureau777357d2016-12-07 18:39:10 +03003602
3603static void register_types(void)
3604{
3605 type_register_static(&char_gdb_type_info);
3606}
3607
3608type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00003609#endif