blob: 7243a2f7af989c7568401e4ea3cb47295b31e733 [file] [log] [blame]
bellardb4608c02003-06-27 17:34:32 +00001/*
2 * gdb server stub
ths5fafdf22007-09-16 21:08:06 +00003 *
Alex Bennée42a09592019-07-05 13:28:19 +01004 * This implements a subset of the remote protocol as described in:
5 *
6 * https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
7 *
bellard34751872005-07-02 14:31:34 +00008 * Copyright (c) 2003-2005 Fabrice Bellard
bellardb4608c02003-06-27 17:34:32 +00009 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
Blue Swirl8167ee82009-07-16 20:47:01 +000021 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Alex Bennée42a09592019-07-05 13:28:19 +010022 *
23 * SPDX-License-Identifier: LGPL-2.0+
bellardb4608c02003-06-27 17:34:32 +000024 */
Markus Armbruster856dfd82019-05-23 16:35:06 +020025
Peter Maydelld38ea872016-01-29 17:50:05 +000026#include "qemu/osdep.h"
Markus Armbrustera8d25322019-05-23 16:35:08 +020027#include "qemu-common.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010028#include "qapi/error.h"
Ziyue Yang508b4ec2017-01-18 16:02:41 +080029#include "qemu/error-report.h"
Markus Armbruster856dfd82019-05-23 16:35:06 +020030#include "qemu/ctype.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020031#include "qemu/cutils.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020032#include "qemu/module.h"
Doug Gale5c9522b2017-12-02 20:30:37 -050033#include "trace-root.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020034#ifdef CONFIG_USER_ONLY
bellard1fddef42005-04-17 19:16:13 +000035#include "qemu.h"
36#else
Paolo Bonzini83c90892012-12-17 18:19:49 +010037#include "monitor/monitor.h"
Marc-André Lureau8228e352017-01-26 17:19:46 +040038#include "chardev/char.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040039#include "chardev/char-fe.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010040#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010041#include "exec/gdbstub.h"
Luc Michel8f468632019-01-07 15:23:45 +000042#include "hw/cpu/cluster.h"
Like Xu5cc87672019-05-19 04:54:21 +080043#include "hw/boards.h"
bellard1fddef42005-04-17 19:16:13 +000044#endif
bellard67b915a2004-03-31 23:37:16 +000045
pbrook56aebc82008-10-11 17:55:29 +000046#define MAX_PACKET_LENGTH 4096
47
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010048#include "qemu/sockets.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010049#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010050#include "sysemu/kvm.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020051#include "sysemu/runstate.h"
Alex Bennéef1672e62019-05-13 14:43:57 +010052#include "hw/semihosting/semihost.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010053#include "exec/exec-all.h"
aurel32ca587a82008-12-18 22:44:13 +000054
Jan Kiszkaa3919382015-02-07 09:38:44 +010055#ifdef CONFIG_USER_ONLY
56#define GDB_ATTACHED "0"
57#else
58#define GDB_ATTACHED "1"
59#endif
60
Jon Doronab4752e2019-05-29 09:41:48 +030061#ifndef CONFIG_USER_ONLY
62static int phy_memory_mode;
63#endif
64
Andreas Färberf3659ee2013-06-27 19:09:09 +020065static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
66 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020067{
Jon Doronab4752e2019-05-29 09:41:48 +030068 CPUClass *cc;
Andreas Färberf3659ee2013-06-27 19:09:09 +020069
Jon Doronab4752e2019-05-29 09:41:48 +030070#ifndef CONFIG_USER_ONLY
71 if (phy_memory_mode) {
72 if (is_write) {
73 cpu_physical_memory_write(addr, buf, len);
74 } else {
75 cpu_physical_memory_read(addr, buf, len);
76 }
77 return 0;
78 }
79#endif
80
81 cc = CPU_GET_CLASS(cpu);
Andreas Färberf3659ee2013-06-27 19:09:09 +020082 if (cc->memory_rw_debug) {
83 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
84 }
85 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020086}
aurel32ca587a82008-12-18 22:44:13 +000087
Alex Bennéed2a6c852017-07-12 11:52:14 +010088/* Return the GDB index for a given vCPU state.
89 *
90 * For user mode this is simply the thread id. In system mode GDB
91 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
92 */
93static inline int cpu_gdb_index(CPUState *cpu)
94{
95#if defined(CONFIG_USER_ONLY)
Alex Bennéebd88c782017-07-12 11:52:15 +010096 TaskState *ts = (TaskState *) cpu->opaque;
97 return ts->ts_tid;
Alex Bennéed2a6c852017-07-12 11:52:14 +010098#else
99 return cpu->cpu_index + 1;
100#endif
101}
102
aurel32ca587a82008-12-18 22:44:13 +0000103enum {
104 GDB_SIGNAL_0 = 0,
105 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +0100106 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +0000107 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +0100108 GDB_SIGNAL_ABRT = 6,
109 GDB_SIGNAL_ALRM = 14,
110 GDB_SIGNAL_IO = 23,
111 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +0000112 GDB_SIGNAL_UNKNOWN = 143
113};
114
115#ifdef CONFIG_USER_ONLY
116
117/* Map target signal numbers to GDB protocol signal numbers and vice
118 * versa. For user emulation's currently supported systems, we can
119 * assume most signals are defined.
120 */
121
122static int gdb_signal_table[] = {
123 0,
124 TARGET_SIGHUP,
125 TARGET_SIGINT,
126 TARGET_SIGQUIT,
127 TARGET_SIGILL,
128 TARGET_SIGTRAP,
129 TARGET_SIGABRT,
130 -1, /* SIGEMT */
131 TARGET_SIGFPE,
132 TARGET_SIGKILL,
133 TARGET_SIGBUS,
134 TARGET_SIGSEGV,
135 TARGET_SIGSYS,
136 TARGET_SIGPIPE,
137 TARGET_SIGALRM,
138 TARGET_SIGTERM,
139 TARGET_SIGURG,
140 TARGET_SIGSTOP,
141 TARGET_SIGTSTP,
142 TARGET_SIGCONT,
143 TARGET_SIGCHLD,
144 TARGET_SIGTTIN,
145 TARGET_SIGTTOU,
146 TARGET_SIGIO,
147 TARGET_SIGXCPU,
148 TARGET_SIGXFSZ,
149 TARGET_SIGVTALRM,
150 TARGET_SIGPROF,
151 TARGET_SIGWINCH,
152 -1, /* SIGLOST */
153 TARGET_SIGUSR1,
154 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000155#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000156 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000157#else
158 -1,
159#endif
aurel32ca587a82008-12-18 22:44:13 +0000160 -1, /* SIGPOLL */
161 -1,
162 -1,
163 -1,
164 -1,
165 -1,
166 -1,
167 -1,
168 -1,
169 -1,
170 -1,
171 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000172#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000173 __SIGRTMIN + 1,
174 __SIGRTMIN + 2,
175 __SIGRTMIN + 3,
176 __SIGRTMIN + 4,
177 __SIGRTMIN + 5,
178 __SIGRTMIN + 6,
179 __SIGRTMIN + 7,
180 __SIGRTMIN + 8,
181 __SIGRTMIN + 9,
182 __SIGRTMIN + 10,
183 __SIGRTMIN + 11,
184 __SIGRTMIN + 12,
185 __SIGRTMIN + 13,
186 __SIGRTMIN + 14,
187 __SIGRTMIN + 15,
188 __SIGRTMIN + 16,
189 __SIGRTMIN + 17,
190 __SIGRTMIN + 18,
191 __SIGRTMIN + 19,
192 __SIGRTMIN + 20,
193 __SIGRTMIN + 21,
194 __SIGRTMIN + 22,
195 __SIGRTMIN + 23,
196 __SIGRTMIN + 24,
197 __SIGRTMIN + 25,
198 __SIGRTMIN + 26,
199 __SIGRTMIN + 27,
200 __SIGRTMIN + 28,
201 __SIGRTMIN + 29,
202 __SIGRTMIN + 30,
203 __SIGRTMIN + 31,
204 -1, /* SIGCANCEL */
205 __SIGRTMIN,
206 __SIGRTMIN + 32,
207 __SIGRTMIN + 33,
208 __SIGRTMIN + 34,
209 __SIGRTMIN + 35,
210 __SIGRTMIN + 36,
211 __SIGRTMIN + 37,
212 __SIGRTMIN + 38,
213 __SIGRTMIN + 39,
214 __SIGRTMIN + 40,
215 __SIGRTMIN + 41,
216 __SIGRTMIN + 42,
217 __SIGRTMIN + 43,
218 __SIGRTMIN + 44,
219 __SIGRTMIN + 45,
220 __SIGRTMIN + 46,
221 __SIGRTMIN + 47,
222 __SIGRTMIN + 48,
223 __SIGRTMIN + 49,
224 __SIGRTMIN + 50,
225 __SIGRTMIN + 51,
226 __SIGRTMIN + 52,
227 __SIGRTMIN + 53,
228 __SIGRTMIN + 54,
229 __SIGRTMIN + 55,
230 __SIGRTMIN + 56,
231 __SIGRTMIN + 57,
232 __SIGRTMIN + 58,
233 __SIGRTMIN + 59,
234 __SIGRTMIN + 60,
235 __SIGRTMIN + 61,
236 __SIGRTMIN + 62,
237 __SIGRTMIN + 63,
238 __SIGRTMIN + 64,
239 __SIGRTMIN + 65,
240 __SIGRTMIN + 66,
241 __SIGRTMIN + 67,
242 __SIGRTMIN + 68,
243 __SIGRTMIN + 69,
244 __SIGRTMIN + 70,
245 __SIGRTMIN + 71,
246 __SIGRTMIN + 72,
247 __SIGRTMIN + 73,
248 __SIGRTMIN + 74,
249 __SIGRTMIN + 75,
250 __SIGRTMIN + 76,
251 __SIGRTMIN + 77,
252 __SIGRTMIN + 78,
253 __SIGRTMIN + 79,
254 __SIGRTMIN + 80,
255 __SIGRTMIN + 81,
256 __SIGRTMIN + 82,
257 __SIGRTMIN + 83,
258 __SIGRTMIN + 84,
259 __SIGRTMIN + 85,
260 __SIGRTMIN + 86,
261 __SIGRTMIN + 87,
262 __SIGRTMIN + 88,
263 __SIGRTMIN + 89,
264 __SIGRTMIN + 90,
265 __SIGRTMIN + 91,
266 __SIGRTMIN + 92,
267 __SIGRTMIN + 93,
268 __SIGRTMIN + 94,
269 __SIGRTMIN + 95,
270 -1, /* SIGINFO */
271 -1, /* UNKNOWN */
272 -1, /* DEFAULT */
273 -1,
274 -1,
275 -1,
276 -1,
277 -1,
278 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000279#endif
aurel32ca587a82008-12-18 22:44:13 +0000280};
bellard8f447cc2006-06-14 15:21:14 +0000281#else
aurel32ca587a82008-12-18 22:44:13 +0000282/* In system mode we only need SIGINT and SIGTRAP; other signals
283 are not yet supported. */
284
285enum {
286 TARGET_SIGINT = 2,
287 TARGET_SIGTRAP = 5
288};
289
290static int gdb_signal_table[] = {
291 -1,
292 -1,
293 TARGET_SIGINT,
294 -1,
295 -1,
296 TARGET_SIGTRAP
297};
bellard8f447cc2006-06-14 15:21:14 +0000298#endif
bellardb4608c02003-06-27 17:34:32 +0000299
aurel32ca587a82008-12-18 22:44:13 +0000300#ifdef CONFIG_USER_ONLY
301static int target_signal_to_gdb (int sig)
302{
303 int i;
304 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
305 if (gdb_signal_table[i] == sig)
306 return i;
307 return GDB_SIGNAL_UNKNOWN;
308}
309#endif
310
311static int gdb_signal_to_target (int sig)
312{
313 if (sig < ARRAY_SIZE (gdb_signal_table))
314 return gdb_signal_table[sig];
315 else
316 return -1;
317}
318
pbrook56aebc82008-10-11 17:55:29 +0000319typedef struct GDBRegisterState {
320 int base_reg;
321 int num_regs;
322 gdb_reg_cb get_reg;
323 gdb_reg_cb set_reg;
324 const char *xml;
325 struct GDBRegisterState *next;
326} GDBRegisterState;
327
Luc Michel8f468632019-01-07 15:23:45 +0000328typedef struct GDBProcess {
329 uint32_t pid;
330 bool attached;
Luc Michelc145eea2019-01-07 15:23:46 +0000331
332 char target_xml[1024];
Luc Michel8f468632019-01-07 15:23:45 +0000333} GDBProcess;
334
bellard858693c2004-03-31 18:52:07 +0000335enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000336 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000337 RS_IDLE,
338 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400339 RS_GETLINE_ESC,
340 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000341 RS_CHKSUM1,
342 RS_CHKSUM2,
343};
bellard858693c2004-03-31 18:52:07 +0000344typedef struct GDBState {
Alex Bennée8d98c442020-03-16 17:21:33 +0000345 bool init; /* have we been initialised? */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200346 CPUState *c_cpu; /* current CPU for step/continue ops */
347 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200348 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000349 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000350 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000351 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400352 int line_sum; /* running checksum */
353 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000354 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000355 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000356 int signal;
bellard41625032005-04-24 10:07:11 +0000357#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000358 int fd;
bellard41625032005-04-24 10:07:11 +0000359 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000360#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300361 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300362 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000363#endif
Luc Michel8f468632019-01-07 15:23:45 +0000364 bool multiprocess;
365 GDBProcess *processes;
366 int process_num;
Meador Ingecdb432b2012-03-15 17:49:45 +0000367 char syscall_buf[256];
368 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000369} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000370
edgar_igl60897d32008-05-09 08:25:14 +0000371/* By default use no IRQs and no timers while single stepping so as to
372 * make single stepping like an ICE HW step.
373 */
374static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
375
Alex Bennée8d98c442020-03-16 17:21:33 +0000376static GDBState gdbserver_state;
377
378static void init_gdbserver_state(void)
379{
380 g_assert(!gdbserver_state.init);
381 memset(&gdbserver_state, 0, sizeof(GDBState));
382 gdbserver_state.init = true;
383}
384
385#ifndef CONFIG_USER_ONLY
386static void reset_gdbserver_state(void)
387{
388 g_free(gdbserver_state.processes);
389 gdbserver_state.processes = NULL;
390 gdbserver_state.process_num = 0;
391}
392#endif
aliguori880a7572008-11-18 20:30:24 +0000393
Andreas Färber5b50e792013-06-29 04:18:45 +0200394bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000395
bellard1fddef42005-04-17 19:16:13 +0000396#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000397/* XXX: This is not thread safe. Do we care? */
398static int gdbserver_fd = -1;
399
Alex Bennéea346af32020-03-16 17:21:34 +0000400static int get_char(void)
bellardb4608c02003-06-27 17:34:32 +0000401{
402 uint8_t ch;
403 int ret;
404
405 for(;;) {
Alex Bennéea346af32020-03-16 17:21:34 +0000406 ret = qemu_recv(gdbserver_state.fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000407 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000408 if (errno == ECONNRESET)
Alex Bennéea346af32020-03-16 17:21:34 +0000409 gdbserver_state.fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200410 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000411 return -1;
412 } else if (ret == 0) {
Alex Bennéea346af32020-03-16 17:21:34 +0000413 close(gdbserver_state.fd);
414 gdbserver_state.fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000415 return -1;
416 } else {
417 break;
418 }
419 }
420 return ch;
421}
pbrook4046d912007-01-28 01:53:16 +0000422#endif
bellardb4608c02003-06-27 17:34:32 +0000423
blueswir1654efcf2009-04-18 07:29:59 +0000424static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000425 GDB_SYS_UNKNOWN,
426 GDB_SYS_ENABLED,
427 GDB_SYS_DISABLED,
428} gdb_syscall_mode;
429
Liviu Ionescua38bb072014-12-11 12:07:48 +0000430/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000431int use_gdb_syscalls(void)
432{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100433 SemihostingTarget target = semihosting_get_target();
434 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000435 /* -semihosting-config target=native */
436 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100437 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000438 /* -semihosting-config target=gdb */
439 return true;
440 }
441
442 /* -semihosting-config target=auto */
443 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000444 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
Alex Bennée8d98c442020-03-16 17:21:33 +0000445 gdb_syscall_mode = gdbserver_state.init ?
446 GDB_SYS_ENABLED : GDB_SYS_DISABLED;
pbrooka2d1eba2007-01-28 03:10:55 +0000447 }
448 return gdb_syscall_mode == GDB_SYS_ENABLED;
449}
450
edgar_iglba70a622008-03-14 06:10:42 +0000451/* Resume execution. */
Alex Bennéea346af32020-03-16 17:21:34 +0000452static inline void gdb_continue(void)
edgar_iglba70a622008-03-14 06:10:42 +0000453{
Doug Gale5c9522b2017-12-02 20:30:37 -0500454
edgar_iglba70a622008-03-14 06:10:42 +0000455#ifdef CONFIG_USER_ONLY
Alex Bennéea346af32020-03-16 17:21:34 +0000456 gdbserver_state.running_state = 1;
Doug Gale5c9522b2017-12-02 20:30:37 -0500457 trace_gdbstub_op_continue();
edgar_iglba70a622008-03-14 06:10:42 +0000458#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200459 if (!runstate_needs_reset()) {
Doug Gale5c9522b2017-12-02 20:30:37 -0500460 trace_gdbstub_op_continue();
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200461 vm_start();
462 }
edgar_iglba70a622008-03-14 06:10:42 +0000463#endif
464}
465
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100466/*
467 * Resume execution, per CPU actions. For user-mode emulation it's
468 * equivalent to gdb_continue.
469 */
Alex Bennéea346af32020-03-16 17:21:34 +0000470static int gdb_continue_partial(char *newstates)
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100471{
472 CPUState *cpu;
473 int res = 0;
474#ifdef CONFIG_USER_ONLY
475 /*
476 * This is not exactly accurate, but it's an improvement compared to the
477 * previous situation, where only one CPU would be single-stepped.
478 */
479 CPU_FOREACH(cpu) {
480 if (newstates[cpu->cpu_index] == 's') {
Doug Gale5c9522b2017-12-02 20:30:37 -0500481 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100482 cpu_single_step(cpu, sstep_flags);
483 }
484 }
Alex Bennéea346af32020-03-16 17:21:34 +0000485 gdbserver_state.running_state = 1;
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100486#else
487 int flag = 0;
488
489 if (!runstate_needs_reset()) {
490 if (vm_prepare_start()) {
491 return 0;
492 }
493
494 CPU_FOREACH(cpu) {
495 switch (newstates[cpu->cpu_index]) {
496 case 0:
497 case 1:
498 break; /* nothing to do here */
499 case 's':
Doug Gale5c9522b2017-12-02 20:30:37 -0500500 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100501 cpu_single_step(cpu, sstep_flags);
502 cpu_resume(cpu);
503 flag = 1;
504 break;
505 case 'c':
Doug Gale5c9522b2017-12-02 20:30:37 -0500506 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100507 cpu_resume(cpu);
508 flag = 1;
509 break;
510 default:
511 res = -1;
512 break;
513 }
514 }
515 }
516 if (flag) {
517 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
518 }
519#endif
520 return res;
521}
522
Alex Bennéea346af32020-03-16 17:21:34 +0000523static void put_buffer(const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000524{
pbrook4046d912007-01-28 01:53:16 +0000525#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000526 int ret;
527
528 while (len > 0) {
Alex Bennéea346af32020-03-16 17:21:34 +0000529 ret = send(gdbserver_state.fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000530 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200531 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000532 return;
533 } else {
534 buf += ret;
535 len -= ret;
536 }
537 }
pbrook4046d912007-01-28 01:53:16 +0000538#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100539 /* XXX this blocks entire thread. Rewrite to use
540 * qemu_chr_fe_write and background I/O callbacks */
Alex Bennéea346af32020-03-16 17:21:34 +0000541 qemu_chr_fe_write_all(&gdbserver_state.chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000542#endif
bellardb4608c02003-06-27 17:34:32 +0000543}
544
545static inline int fromhex(int v)
546{
547 if (v >= '0' && v <= '9')
548 return v - '0';
549 else if (v >= 'A' && v <= 'F')
550 return v - 'A' + 10;
551 else if (v >= 'a' && v <= 'f')
552 return v - 'a' + 10;
553 else
554 return 0;
555}
556
557static inline int tohex(int v)
558{
559 if (v < 10)
560 return v + '0';
561 else
562 return v - 10 + 'a';
563}
564
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -0300565/* writes 2*len+1 bytes in buf */
bellardb4608c02003-06-27 17:34:32 +0000566static void memtohex(char *buf, const uint8_t *mem, int len)
567{
568 int i, c;
569 char *q;
570 q = buf;
571 for(i = 0; i < len; i++) {
572 c = mem[i];
573 *q++ = tohex(c >> 4);
574 *q++ = tohex(c & 0xf);
575 }
576 *q = '\0';
577}
578
579static void hextomem(uint8_t *mem, const char *buf, int len)
580{
581 int i;
582
583 for(i = 0; i < len; i++) {
584 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
585 buf += 2;
586 }
587}
588
Doug Gale5c9522b2017-12-02 20:30:37 -0500589static void hexdump(const char *buf, int len,
590 void (*trace_fn)(size_t ofs, char const *text))
591{
592 char line_buffer[3 * 16 + 4 + 16 + 1];
593
594 size_t i;
595 for (i = 0; i < len || (i & 0xF); ++i) {
596 size_t byte_ofs = i & 15;
597
598 if (byte_ofs == 0) {
599 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
600 line_buffer[3 * 16 + 4 + 16] = 0;
601 }
602
603 size_t col_group = (i >> 2) & 3;
604 size_t hex_col = byte_ofs * 3 + col_group;
605 size_t txt_col = 3 * 16 + 4 + byte_ofs;
606
607 if (i < len) {
608 char value = buf[i];
609
610 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
611 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
612 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
613 ? value
614 : '.';
615 }
616
617 if (byte_ofs == 0xF)
618 trace_fn(i & -16, line_buffer);
619 }
620}
621
bellardb4608c02003-06-27 17:34:32 +0000622/* return -1 if error, 0 if OK */
Alex Bennéea346af32020-03-16 17:21:34 +0000623static int put_packet_binary(const char *buf, int len, bool dump)
bellardb4608c02003-06-27 17:34:32 +0000624{
pbrook56aebc82008-10-11 17:55:29 +0000625 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000626 uint8_t *p;
Alex Bennéea346af32020-03-16 17:21:34 +0000627 uint8_t *ps = &gdbserver_state.last_packet[0];
bellardb4608c02003-06-27 17:34:32 +0000628
Doug Gale5c9522b2017-12-02 20:30:37 -0500629 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
630 hexdump(buf, len, trace_gdbstub_io_binaryreply);
631 }
632
bellardb4608c02003-06-27 17:34:32 +0000633 for(;;) {
Alex Bennéea346af32020-03-16 17:21:34 +0000634 p = ps;
pbrook4046d912007-01-28 01:53:16 +0000635 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000636 memcpy(p, buf, len);
637 p += len;
bellardb4608c02003-06-27 17:34:32 +0000638 csum = 0;
639 for(i = 0; i < len; i++) {
640 csum += buf[i];
641 }
pbrook4046d912007-01-28 01:53:16 +0000642 *(p++) = '#';
643 *(p++) = tohex((csum >> 4) & 0xf);
644 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000645
Alex Bennéea346af32020-03-16 17:21:34 +0000646 gdbserver_state.last_packet_len = p - ps;
647 put_buffer(ps, gdbserver_state.last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000648
pbrook4046d912007-01-28 01:53:16 +0000649#ifdef CONFIG_USER_ONLY
Alex Bennéea346af32020-03-16 17:21:34 +0000650 i = get_char();
pbrook4046d912007-01-28 01:53:16 +0000651 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000652 return -1;
pbrook4046d912007-01-28 01:53:16 +0000653 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000654 break;
pbrook4046d912007-01-28 01:53:16 +0000655#else
656 break;
657#endif
bellardb4608c02003-06-27 17:34:32 +0000658 }
659 return 0;
660}
661
pbrook56aebc82008-10-11 17:55:29 +0000662/* return -1 if error, 0 if OK */
Alex Bennéea346af32020-03-16 17:21:34 +0000663static int put_packet(const char *buf)
pbrook56aebc82008-10-11 17:55:29 +0000664{
Doug Gale5c9522b2017-12-02 20:30:37 -0500665 trace_gdbstub_io_reply(buf);
pbrook56aebc82008-10-11 17:55:29 +0000666
Alex Bennéea346af32020-03-16 17:21:34 +0000667 return put_packet_binary(buf, strlen(buf), false);
pbrook56aebc82008-10-11 17:55:29 +0000668}
669
pbrook56aebc82008-10-11 17:55:29 +0000670/* Encode data using the encoding for 'x' packets. */
671static int memtox(char *buf, const char *mem, int len)
672{
673 char *p = buf;
674 char c;
675
676 while (len--) {
677 c = *(mem++);
678 switch (c) {
679 case '#': case '$': case '*': case '}':
680 *(p++) = '}';
681 *(p++) = c ^ 0x20;
682 break;
683 default:
684 *(p++) = c;
685 break;
686 }
687 }
688 return p - buf;
689}
690
Alex Bennéea346af32020-03-16 17:21:34 +0000691static uint32_t gdb_get_cpu_pid(CPUState *cpu)
Luc Michel1a227332019-01-07 15:23:45 +0000692{
Luc Michel1a227332019-01-07 15:23:45 +0000693 /* TODO: In user mode, we should use the task state PID */
Peter Maydell46f5abc2019-01-29 11:46:06 +0000694 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
695 /* Return the default process' PID */
Alex Bennéea346af32020-03-16 17:21:34 +0000696 int index = gdbserver_state.process_num - 1;
697 return gdbserver_state.processes[index].pid;
Peter Maydell46f5abc2019-01-29 11:46:06 +0000698 }
699 return cpu->cluster_index + 1;
Luc Michel1a227332019-01-07 15:23:45 +0000700}
701
Alex Bennéea346af32020-03-16 17:21:34 +0000702static GDBProcess *gdb_get_process(uint32_t pid)
Luc Michel7d8c87d2019-01-07 15:23:45 +0000703{
704 int i;
705
706 if (!pid) {
707 /* 0 means any process, we take the first one */
Alex Bennéea346af32020-03-16 17:21:34 +0000708 return &gdbserver_state.processes[0];
Luc Michel7d8c87d2019-01-07 15:23:45 +0000709 }
710
Alex Bennéea346af32020-03-16 17:21:34 +0000711 for (i = 0; i < gdbserver_state.process_num; i++) {
712 if (gdbserver_state.processes[i].pid == pid) {
713 return &gdbserver_state.processes[i];
Luc Michel7d8c87d2019-01-07 15:23:45 +0000714 }
715 }
716
717 return NULL;
718}
719
Alex Bennéea346af32020-03-16 17:21:34 +0000720static GDBProcess *gdb_get_cpu_process(CPUState *cpu)
Luc Michel7d8c87d2019-01-07 15:23:45 +0000721{
Alex Bennéea346af32020-03-16 17:21:34 +0000722 return gdb_get_process(gdb_get_cpu_pid(cpu));
Luc Michel7d8c87d2019-01-07 15:23:45 +0000723}
724
725static CPUState *find_cpu(uint32_t thread_id)
726{
727 CPUState *cpu;
728
729 CPU_FOREACH(cpu) {
730 if (cpu_gdb_index(cpu) == thread_id) {
731 return cpu;
732 }
733 }
734
735 return NULL;
736}
737
Alex Bennéea346af32020-03-16 17:21:34 +0000738static CPUState *get_first_cpu_in_process(GDBProcess *process)
Luc Michele40e5202019-01-07 15:23:46 +0000739{
740 CPUState *cpu;
741
742 CPU_FOREACH(cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +0000743 if (gdb_get_cpu_pid(cpu) == process->pid) {
Luc Michele40e5202019-01-07 15:23:46 +0000744 return cpu;
745 }
746 }
747
748 return NULL;
749}
750
Alex Bennéea346af32020-03-16 17:21:34 +0000751static CPUState *gdb_next_cpu_in_process(CPUState *cpu)
Luc Michele40e5202019-01-07 15:23:46 +0000752{
Alex Bennéea346af32020-03-16 17:21:34 +0000753 uint32_t pid = gdb_get_cpu_pid(cpu);
Luc Michele40e5202019-01-07 15:23:46 +0000754 cpu = CPU_NEXT(cpu);
755
756 while (cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +0000757 if (gdb_get_cpu_pid(cpu) == pid) {
Luc Michele40e5202019-01-07 15:23:46 +0000758 break;
759 }
760
761 cpu = CPU_NEXT(cpu);
762 }
763
764 return cpu;
765}
766
Luc Michele40e5202019-01-07 15:23:46 +0000767/* Return the cpu following @cpu, while ignoring unattached processes. */
Alex Bennéea346af32020-03-16 17:21:34 +0000768static CPUState *gdb_next_attached_cpu(CPUState *cpu)
Luc Michele40e5202019-01-07 15:23:46 +0000769{
770 cpu = CPU_NEXT(cpu);
771
772 while (cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +0000773 if (gdb_get_cpu_process(cpu)->attached) {
Luc Michele40e5202019-01-07 15:23:46 +0000774 break;
775 }
776
777 cpu = CPU_NEXT(cpu);
778 }
779
780 return cpu;
781}
782
783/* Return the first attached cpu */
Alex Bennéea346af32020-03-16 17:21:34 +0000784static CPUState *gdb_first_attached_cpu(void)
Luc Michele40e5202019-01-07 15:23:46 +0000785{
786 CPUState *cpu = first_cpu;
Alex Bennéea346af32020-03-16 17:21:34 +0000787 GDBProcess *process = gdb_get_cpu_process(cpu);
Luc Michele40e5202019-01-07 15:23:46 +0000788
789 if (!process->attached) {
Alex Bennéea346af32020-03-16 17:21:34 +0000790 return gdb_next_attached_cpu(cpu);
Luc Michele40e5202019-01-07 15:23:46 +0000791 }
792
793 return cpu;
794}
795
Alex Bennéea346af32020-03-16 17:21:34 +0000796static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid)
Luc Michelab65eed2019-01-29 11:46:03 +0000797{
798 GDBProcess *process;
799 CPUState *cpu;
800
801 if (!pid && !tid) {
802 /* 0 means any process/thread, we take the first attached one */
Alex Bennéea346af32020-03-16 17:21:34 +0000803 return gdb_first_attached_cpu();
Luc Michelab65eed2019-01-29 11:46:03 +0000804 } else if (pid && !tid) {
805 /* any thread in a specific process */
Alex Bennéea346af32020-03-16 17:21:34 +0000806 process = gdb_get_process(pid);
Luc Michelab65eed2019-01-29 11:46:03 +0000807
808 if (process == NULL) {
809 return NULL;
810 }
811
812 if (!process->attached) {
813 return NULL;
814 }
815
Alex Bennéea346af32020-03-16 17:21:34 +0000816 return get_first_cpu_in_process(process);
Luc Michelab65eed2019-01-29 11:46:03 +0000817 } else {
818 /* a specific thread */
819 cpu = find_cpu(tid);
820
821 if (cpu == NULL) {
822 return NULL;
823 }
824
Alex Bennéea346af32020-03-16 17:21:34 +0000825 process = gdb_get_cpu_process(cpu);
Luc Michelab65eed2019-01-29 11:46:03 +0000826
827 if (pid && process->pid != pid) {
828 return NULL;
829 }
830
831 if (!process->attached) {
832 return NULL;
833 }
834
835 return cpu;
836 }
837}
838
Alex Bennéea346af32020-03-16 17:21:34 +0000839static const char *get_feature_xml(const char *p, const char **newp,
840 GDBProcess *process)
pbrook56aebc82008-10-11 17:55:29 +0000841{
pbrook56aebc82008-10-11 17:55:29 +0000842 size_t len;
843 int i;
844 const char *name;
Alex Bennéea346af32020-03-16 17:21:34 +0000845 CPUState *cpu = get_first_cpu_in_process(process);
Luc Michelc145eea2019-01-07 15:23:46 +0000846 CPUClass *cc = CPU_GET_CLASS(cpu);
pbrook56aebc82008-10-11 17:55:29 +0000847
848 len = 0;
849 while (p[len] && p[len] != ':')
850 len++;
851 *newp = p + len;
852
853 name = NULL;
854 if (strncmp(p, "target.xml", len) == 0) {
Luc Michelc145eea2019-01-07 15:23:46 +0000855 char *buf = process->target_xml;
856 const size_t buf_sz = sizeof(process->target_xml);
pbrook56aebc82008-10-11 17:55:29 +0000857
Luc Michelc145eea2019-01-07 15:23:46 +0000858 /* Generate the XML description for this CPU. */
859 if (!buf[0]) {
860 GDBRegisterState *r;
861
862 pstrcat(buf, buf_sz,
David Hildenbrandb3820e62015-12-03 13:14:41 +0100863 "<?xml version=\"1.0\"?>"
864 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
865 "<target>");
866 if (cc->gdb_arch_name) {
867 gchar *arch = cc->gdb_arch_name(cpu);
Luc Michelc145eea2019-01-07 15:23:46 +0000868 pstrcat(buf, buf_sz, "<architecture>");
869 pstrcat(buf, buf_sz, arch);
870 pstrcat(buf, buf_sz, "</architecture>");
David Hildenbrandb3820e62015-12-03 13:14:41 +0100871 g_free(arch);
872 }
Luc Michelc145eea2019-01-07 15:23:46 +0000873 pstrcat(buf, buf_sz, "<xi:include href=\"");
874 pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
875 pstrcat(buf, buf_sz, "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200876 for (r = cpu->gdb_regs; r; r = r->next) {
Luc Michelc145eea2019-01-07 15:23:46 +0000877 pstrcat(buf, buf_sz, "<xi:include href=\"");
878 pstrcat(buf, buf_sz, r->xml);
879 pstrcat(buf, buf_sz, "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000880 }
Luc Michelc145eea2019-01-07 15:23:46 +0000881 pstrcat(buf, buf_sz, "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000882 }
Luc Michelc145eea2019-01-07 15:23:46 +0000883 return buf;
pbrook56aebc82008-10-11 17:55:29 +0000884 }
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100885 if (cc->gdb_get_dynamic_xml) {
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100886 char *xmlname = g_strndup(p, len);
887 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
888
889 g_free(xmlname);
890 if (xml) {
891 return xml;
892 }
893 }
pbrook56aebc82008-10-11 17:55:29 +0000894 for (i = 0; ; i++) {
895 name = xml_builtin[i][0];
896 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
897 break;
898 }
899 return name ? xml_builtin[i][1] : NULL;
900}
pbrook56aebc82008-10-11 17:55:29 +0000901
Andreas Färber385b9f02013-06-27 18:25:36 +0200902static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000903{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200904 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200905 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000906 GDBRegisterState *r;
907
Andreas Färbera0e372f2013-06-28 23:18:47 +0200908 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200909 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200910 }
pbrook56aebc82008-10-11 17:55:29 +0000911
Andreas Färbereac8b352013-06-28 21:11:37 +0200912 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000913 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
914 return r->get_reg(env, mem_buf, reg - r->base_reg);
915 }
916 }
917 return 0;
918}
919
Andreas Färber385b9f02013-06-27 18:25:36 +0200920static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000921{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200922 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200923 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000924 GDBRegisterState *r;
925
Andreas Färbera0e372f2013-06-28 23:18:47 +0200926 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200927 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200928 }
pbrook56aebc82008-10-11 17:55:29 +0000929
Andreas Färbereac8b352013-06-28 21:11:37 +0200930 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000931 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
932 return r->set_reg(env, mem_buf, reg - r->base_reg);
933 }
934 }
935 return 0;
936}
937
938/* Register a supplemental set of CPU registers. If g_pos is nonzero it
939 specifies the first register number and these registers are included in
940 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
941 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
942 */
943
Andreas Färber22169d42013-06-28 21:27:39 +0200944void gdb_register_coprocessor(CPUState *cpu,
945 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
946 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000947{
948 GDBRegisterState *s;
949 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000950
Andreas Färbereac8b352013-06-28 21:11:37 +0200951 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000952 while (*p) {
953 /* Check for duplicates. */
954 if (strcmp((*p)->xml, xml) == 0)
955 return;
956 p = &(*p)->next;
957 }
Stefan Weil9643c252011-10-18 22:25:38 +0200958
959 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200960 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200961 s->num_regs = num_regs;
962 s->get_reg = get_reg;
963 s->set_reg = set_reg;
964 s->xml = xml;
965
pbrook56aebc82008-10-11 17:55:29 +0000966 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200967 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000968 *p = s;
969 if (g_pos) {
970 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800971 error_report("Error: Bad gdb register numbering for '%s', "
972 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200973 } else {
974 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000975 }
976 }
977}
978
aliguoria1d1bb32008-11-18 20:07:32 +0000979#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100980/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
981static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
982{
983 static const int xlat[] = {
984 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
985 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
986 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
987 };
988
989 CPUClass *cc = CPU_GET_CLASS(cpu);
990 int cputype = xlat[gdbtype];
991
992 if (cc->gdb_stop_before_watchpoint) {
993 cputype |= BP_STOP_BEFORE_ACCESS;
994 }
995 return cputype;
996}
aliguoria1d1bb32008-11-18 20:07:32 +0000997#endif
998
Jon Doron77f6ce52019-05-29 09:41:35 +0300999static int gdb_breakpoint_insert(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +00001000{
Andreas Färber182735e2013-05-29 22:29:20 +02001001 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001002 int err = 0;
1003
Andreas Färber62278812013-06-27 17:12:06 +02001004 if (kvm_enabled()) {
Alex Bennée8d98c442020-03-16 17:21:33 +00001005 return kvm_insert_breakpoint(gdbserver_state.c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001006 }
aliguorie22a25c2009-03-12 20:12:48 +00001007
aliguoria1d1bb32008-11-18 20:07:32 +00001008 switch (type) {
1009 case GDB_BREAKPOINT_SW:
1010 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001011 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001012 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
1013 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001014 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001015 }
aliguori880a7572008-11-18 20:30:24 +00001016 }
1017 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001018#ifndef CONFIG_USER_ONLY
1019 case GDB_WATCHPOINT_WRITE:
1020 case GDB_WATCHPOINT_READ:
1021 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001022 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001023 err = cpu_watchpoint_insert(cpu, addr, len,
1024 xlat_gdb_type(cpu, type), NULL);
1025 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001026 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +01001027 }
aliguori880a7572008-11-18 20:30:24 +00001028 }
1029 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001030#endif
1031 default:
1032 return -ENOSYS;
1033 }
1034}
1035
Jon Doron77f6ce52019-05-29 09:41:35 +03001036static int gdb_breakpoint_remove(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +00001037{
Andreas Färber182735e2013-05-29 22:29:20 +02001038 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001039 int err = 0;
1040
Andreas Färber62278812013-06-27 17:12:06 +02001041 if (kvm_enabled()) {
Alex Bennée8d98c442020-03-16 17:21:33 +00001042 return kvm_remove_breakpoint(gdbserver_state.c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001043 }
aliguorie22a25c2009-03-12 20:12:48 +00001044
aliguoria1d1bb32008-11-18 20:07:32 +00001045 switch (type) {
1046 case GDB_BREAKPOINT_SW:
1047 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001048 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001049 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
1050 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001051 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001052 }
aliguori880a7572008-11-18 20:30:24 +00001053 }
1054 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001055#ifndef CONFIG_USER_ONLY
1056 case GDB_WATCHPOINT_WRITE:
1057 case GDB_WATCHPOINT_READ:
1058 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001059 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001060 err = cpu_watchpoint_remove(cpu, addr, len,
1061 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +00001062 if (err)
1063 break;
1064 }
1065 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001066#endif
1067 default:
1068 return -ENOSYS;
1069 }
1070}
1071
Luc Michel546f3c62019-01-07 15:23:46 +00001072static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu)
1073{
1074 cpu_breakpoint_remove_all(cpu, BP_GDB);
1075#ifndef CONFIG_USER_ONLY
1076 cpu_watchpoint_remove_all(cpu, BP_GDB);
1077#endif
1078}
1079
Alex Bennéea346af32020-03-16 17:21:34 +00001080static void gdb_process_breakpoint_remove_all(GDBProcess *p)
Luc Michel546f3c62019-01-07 15:23:46 +00001081{
Alex Bennéea346af32020-03-16 17:21:34 +00001082 CPUState *cpu = get_first_cpu_in_process(p);
Luc Michel546f3c62019-01-07 15:23:46 +00001083
1084 while (cpu) {
1085 gdb_cpu_breakpoint_remove_all(cpu);
Alex Bennéea346af32020-03-16 17:21:34 +00001086 cpu = gdb_next_cpu_in_process(cpu);
Luc Michel546f3c62019-01-07 15:23:46 +00001087 }
1088}
1089
aliguori880a7572008-11-18 20:30:24 +00001090static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +00001091{
Andreas Färber182735e2013-05-29 22:29:20 +02001092 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001093
aliguorie22a25c2009-03-12 20:12:48 +00001094 if (kvm_enabled()) {
Alex Bennée8d98c442020-03-16 17:21:33 +00001095 kvm_remove_all_breakpoints(gdbserver_state.c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +00001096 return;
1097 }
1098
Andreas Färberbdc44642013-06-24 23:50:24 +02001099 CPU_FOREACH(cpu) {
Luc Michel546f3c62019-01-07 15:23:46 +00001100 gdb_cpu_breakpoint_remove_all(cpu);
aliguori880a7572008-11-18 20:30:24 +00001101 }
aliguoria1d1bb32008-11-18 20:07:32 +00001102}
1103
Alex Bennéea346af32020-03-16 17:21:34 +00001104static void gdb_set_cpu_pc(target_ulong pc)
aurel32fab9d282009-04-08 21:29:37 +00001105{
Alex Bennéea346af32020-03-16 17:21:34 +00001106 CPUState *cpu = gdbserver_state.c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +02001107
1108 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -07001109 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +00001110}
1111
Alex Bennéea346af32020-03-16 17:21:34 +00001112static char *gdb_fmt_thread_id(CPUState *cpu, char *buf, size_t buf_size)
Luc Michel1a227332019-01-07 15:23:45 +00001113{
Alex Bennéea346af32020-03-16 17:21:34 +00001114 if (gdbserver_state.multiprocess) {
Luc Michel1a227332019-01-07 15:23:45 +00001115 snprintf(buf, buf_size, "p%02x.%02x",
Alex Bennéea346af32020-03-16 17:21:34 +00001116 gdb_get_cpu_pid(cpu), cpu_gdb_index(cpu));
Luc Michel1a227332019-01-07 15:23:45 +00001117 } else {
1118 snprintf(buf, buf_size, "%02x", cpu_gdb_index(cpu));
1119 }
1120
1121 return buf;
1122}
1123
Luc Michel7d8c87d2019-01-07 15:23:45 +00001124typedef enum GDBThreadIdKind {
1125 GDB_ONE_THREAD = 0,
1126 GDB_ALL_THREADS, /* One process, all threads */
1127 GDB_ALL_PROCESSES,
1128 GDB_READ_THREAD_ERR
1129} GDBThreadIdKind;
1130
1131static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1132 uint32_t *pid, uint32_t *tid)
1133{
1134 unsigned long p, t;
1135 int ret;
1136
1137 if (*buf == 'p') {
1138 buf++;
1139 ret = qemu_strtoul(buf, &buf, 16, &p);
1140
1141 if (ret) {
1142 return GDB_READ_THREAD_ERR;
1143 }
1144
1145 /* Skip '.' */
1146 buf++;
1147 } else {
1148 p = 1;
1149 }
1150
1151 ret = qemu_strtoul(buf, &buf, 16, &t);
1152
1153 if (ret) {
1154 return GDB_READ_THREAD_ERR;
1155 }
1156
1157 *end_buf = buf;
1158
1159 if (p == -1) {
1160 return GDB_ALL_PROCESSES;
1161 }
1162
1163 if (pid) {
1164 *pid = p;
1165 }
1166
1167 if (t == -1) {
1168 return GDB_ALL_THREADS;
1169 }
1170
1171 if (tid) {
1172 *tid = t;
1173 }
1174
1175 return GDB_ONE_THREAD;
1176}
1177
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001178/**
1179 * gdb_handle_vcont - Parses and handles a vCont packet.
1180 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1181 * a format error, 0 on success.
1182 */
Alex Bennéea346af32020-03-16 17:21:34 +00001183static int gdb_handle_vcont(const char *p)
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001184{
Luc Michele40e5202019-01-07 15:23:46 +00001185 int res, signal = 0;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001186 char cur_action;
1187 char *newstates;
1188 unsigned long tmp;
Luc Michele40e5202019-01-07 15:23:46 +00001189 uint32_t pid, tid;
1190 GDBProcess *process;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001191 CPUState *cpu;
Luc Michelc99ef792019-03-26 12:53:26 +00001192 GDBThreadIdKind kind;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001193#ifdef CONFIG_USER_ONLY
1194 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
1195
1196 CPU_FOREACH(cpu) {
1197 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1198 }
Like Xu5cc87672019-05-19 04:54:21 +08001199#else
1200 MachineState *ms = MACHINE(qdev_get_machine());
1201 unsigned int max_cpus = ms->smp.max_cpus;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001202#endif
1203 /* uninitialised CPUs stay 0 */
1204 newstates = g_new0(char, max_cpus);
1205
1206 /* mark valid CPUs with 1 */
1207 CPU_FOREACH(cpu) {
1208 newstates[cpu->cpu_index] = 1;
1209 }
1210
1211 /*
1212 * res keeps track of what error we are returning, with -ENOTSUP meaning
1213 * that the command is unknown or unsupported, thus returning an empty
1214 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1215 * or incorrect parameters passed.
1216 */
1217 res = 0;
1218 while (*p) {
1219 if (*p++ != ';') {
1220 res = -ENOTSUP;
1221 goto out;
1222 }
1223
1224 cur_action = *p++;
1225 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +01001226 cur_action = qemu_tolower(cur_action);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001227 res = qemu_strtoul(p + 1, &p, 16, &tmp);
1228 if (res) {
1229 goto out;
1230 }
1231 signal = gdb_signal_to_target(tmp);
1232 } else if (cur_action != 'c' && cur_action != 's') {
1233 /* unknown/invalid/unsupported command */
1234 res = -ENOTSUP;
1235 goto out;
1236 }
Luc Michele40e5202019-01-07 15:23:46 +00001237
Luc Michelc99ef792019-03-26 12:53:26 +00001238 if (*p == '\0' || *p == ';') {
1239 /*
1240 * No thread specifier, action is on "all threads". The
1241 * specification is unclear regarding the process to act on. We
1242 * choose all processes.
1243 */
1244 kind = GDB_ALL_PROCESSES;
1245 } else if (*p++ == ':') {
1246 kind = read_thread_id(p, &p, &pid, &tid);
1247 } else {
Luc Michele40e5202019-01-07 15:23:46 +00001248 res = -ENOTSUP;
1249 goto out;
1250 }
1251
Luc Michelc99ef792019-03-26 12:53:26 +00001252 switch (kind) {
Luc Michele40e5202019-01-07 15:23:46 +00001253 case GDB_READ_THREAD_ERR:
1254 res = -EINVAL;
1255 goto out;
1256
1257 case GDB_ALL_PROCESSES:
Alex Bennéea346af32020-03-16 17:21:34 +00001258 cpu = gdb_first_attached_cpu();
Luc Michele40e5202019-01-07 15:23:46 +00001259 while (cpu) {
1260 if (newstates[cpu->cpu_index] == 1) {
1261 newstates[cpu->cpu_index] = cur_action;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001262 }
Luc Michele40e5202019-01-07 15:23:46 +00001263
Alex Bennéea346af32020-03-16 17:21:34 +00001264 cpu = gdb_next_attached_cpu(cpu);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001265 }
Luc Michele40e5202019-01-07 15:23:46 +00001266 break;
1267
1268 case GDB_ALL_THREADS:
Alex Bennéea346af32020-03-16 17:21:34 +00001269 process = gdb_get_process(pid);
Luc Michele40e5202019-01-07 15:23:46 +00001270
1271 if (!process->attached) {
1272 res = -EINVAL;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001273 goto out;
1274 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001275
Alex Bennéea346af32020-03-16 17:21:34 +00001276 cpu = get_first_cpu_in_process(process);
Luc Michele40e5202019-01-07 15:23:46 +00001277 while (cpu) {
1278 if (newstates[cpu->cpu_index] == 1) {
1279 newstates[cpu->cpu_index] = cur_action;
1280 }
1281
Alex Bennéea346af32020-03-16 17:21:34 +00001282 cpu = gdb_next_cpu_in_process(cpu);
Luc Michele40e5202019-01-07 15:23:46 +00001283 }
1284 break;
1285
1286 case GDB_ONE_THREAD:
Alex Bennéea346af32020-03-16 17:21:34 +00001287 cpu = gdb_get_cpu(pid, tid);
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001288
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001289 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001290 if (!cpu) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001291 res = -EINVAL;
1292 goto out;
1293 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001294
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001295 /* only use if no previous match occourred */
1296 if (newstates[cpu->cpu_index] == 1) {
1297 newstates[cpu->cpu_index] = cur_action;
1298 }
Luc Michele40e5202019-01-07 15:23:46 +00001299 break;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001300 }
1301 }
Alex Bennéea346af32020-03-16 17:21:34 +00001302 gdbserver_state.signal = signal;
1303 gdb_continue_partial(newstates);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001304
1305out:
1306 g_free(newstates);
1307
1308 return res;
1309}
1310
Jon Dorond14055d2019-05-29 09:41:29 +03001311typedef union GdbCmdVariant {
1312 const char *data;
1313 uint8_t opcode;
1314 unsigned long val_ul;
1315 unsigned long long val_ull;
1316 struct {
1317 GDBThreadIdKind kind;
1318 uint32_t pid;
1319 uint32_t tid;
1320 } thread_id;
1321} GdbCmdVariant;
1322
1323static const char *cmd_next_param(const char *param, const char delimiter)
1324{
1325 static const char all_delimiters[] = ",;:=";
1326 char curr_delimiters[2] = {0};
1327 const char *delimiters;
1328
1329 if (delimiter == '?') {
1330 delimiters = all_delimiters;
1331 } else if (delimiter == '0') {
1332 return strchr(param, '\0');
1333 } else if (delimiter == '.' && *param) {
1334 return param + 1;
1335 } else {
1336 curr_delimiters[0] = delimiter;
1337 delimiters = curr_delimiters;
1338 }
1339
1340 param += strcspn(param, delimiters);
1341 if (*param) {
1342 param++;
1343 }
1344 return param;
1345}
1346
1347static int cmd_parse_params(const char *data, const char *schema,
1348 GdbCmdVariant *params, int *num_params)
1349{
1350 int curr_param;
1351 const char *curr_schema, *curr_data;
1352
1353 *num_params = 0;
1354
1355 if (!schema) {
1356 return 0;
1357 }
1358
1359 curr_schema = schema;
1360 curr_param = 0;
1361 curr_data = data;
1362 while (curr_schema[0] && curr_schema[1] && *curr_data) {
1363 switch (curr_schema[0]) {
1364 case 'l':
1365 if (qemu_strtoul(curr_data, &curr_data, 16,
1366 &params[curr_param].val_ul)) {
1367 return -EINVAL;
1368 }
1369 curr_param++;
1370 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1371 break;
1372 case 'L':
1373 if (qemu_strtou64(curr_data, &curr_data, 16,
1374 (uint64_t *)&params[curr_param].val_ull)) {
1375 return -EINVAL;
1376 }
1377 curr_param++;
1378 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1379 break;
1380 case 's':
1381 params[curr_param].data = curr_data;
1382 curr_param++;
1383 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1384 break;
1385 case 'o':
1386 params[curr_param].opcode = *(uint8_t *)curr_data;
1387 curr_param++;
1388 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1389 break;
1390 case 't':
1391 params[curr_param].thread_id.kind =
1392 read_thread_id(curr_data, &curr_data,
1393 &params[curr_param].thread_id.pid,
1394 &params[curr_param].thread_id.tid);
1395 curr_param++;
1396 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1397 break;
1398 case '?':
1399 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1400 break;
1401 default:
1402 return -EINVAL;
1403 }
1404 curr_schema += 2;
1405 }
1406
1407 *num_params = curr_param;
1408 return 0;
1409}
1410
1411typedef struct GdbCmdContext {
Jon Dorond14055d2019-05-29 09:41:29 +03001412 GdbCmdVariant *params;
1413 int num_params;
1414 uint8_t mem_buf[MAX_PACKET_LENGTH];
1415 char str_buf[MAX_PACKET_LENGTH + 1];
1416} GdbCmdContext;
1417
1418typedef void (*GdbCmdHandler)(GdbCmdContext *gdb_ctx, void *user_ctx);
1419
1420/*
1421 * cmd_startswith -> cmd is compared using startswith
1422 *
1423 *
1424 * schema definitions:
1425 * Each schema parameter entry consists of 2 chars,
1426 * the first char represents the parameter type handling
1427 * the second char represents the delimiter for the next parameter
1428 *
1429 * Currently supported schema types:
1430 * 'l' -> unsigned long (stored in .val_ul)
1431 * 'L' -> unsigned long long (stored in .val_ull)
1432 * 's' -> string (stored in .data)
1433 * 'o' -> single char (stored in .opcode)
1434 * 't' -> thread id (stored in .thread_id)
1435 * '?' -> skip according to delimiter
1436 *
1437 * Currently supported delimiters:
1438 * '?' -> Stop at any delimiter (",;:=\0")
1439 * '0' -> Stop at "\0"
1440 * '.' -> Skip 1 char unless reached "\0"
1441 * Any other value is treated as the delimiter value itself
1442 */
1443typedef struct GdbCmdParseEntry {
1444 GdbCmdHandler handler;
1445 const char *cmd;
1446 bool cmd_startswith;
1447 const char *schema;
1448} GdbCmdParseEntry;
1449
1450static inline int startswith(const char *string, const char *pattern)
1451{
1452 return !strncmp(string, pattern, strlen(pattern));
1453}
1454
Alex Bennéea346af32020-03-16 17:21:34 +00001455static int process_string_cmd(void *user_ctx, const char *data,
Jon Dorond14055d2019-05-29 09:41:29 +03001456 const GdbCmdParseEntry *cmds, int num_cmds)
1457{
1458 int i, schema_len, max_num_params = 0;
1459 GdbCmdContext gdb_ctx;
1460
1461 if (!cmds) {
1462 return -1;
1463 }
1464
1465 for (i = 0; i < num_cmds; i++) {
1466 const GdbCmdParseEntry *cmd = &cmds[i];
1467 g_assert(cmd->handler && cmd->cmd);
1468
1469 if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
1470 (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
1471 continue;
1472 }
1473
1474 if (cmd->schema) {
1475 schema_len = strlen(cmd->schema);
1476 if (schema_len % 2) {
1477 return -2;
1478 }
1479
1480 max_num_params = schema_len / 2;
1481 }
1482
1483 gdb_ctx.params =
1484 (GdbCmdVariant *)alloca(sizeof(*gdb_ctx.params) * max_num_params);
1485 memset(gdb_ctx.params, 0, sizeof(*gdb_ctx.params) * max_num_params);
1486
1487 if (cmd_parse_params(&data[strlen(cmd->cmd)], cmd->schema,
1488 gdb_ctx.params, &gdb_ctx.num_params)) {
1489 return -1;
1490 }
1491
Jon Dorond14055d2019-05-29 09:41:29 +03001492 cmd->handler(&gdb_ctx, user_ctx);
1493 return 0;
1494 }
1495
1496 return -1;
1497}
1498
Alex Bennéea346af32020-03-16 17:21:34 +00001499static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd)
Jon Doron3e2c1262019-05-29 09:41:30 +03001500{
1501 if (!data) {
1502 return;
1503 }
1504
1505 /* In case there was an error during the command parsing we must
1506 * send a NULL packet to indicate the command is not supported */
Alex Bennéea346af32020-03-16 17:21:34 +00001507 if (process_string_cmd(NULL, data, cmd, 1)) {
1508 put_packet("");
Jon Doron3e2c1262019-05-29 09:41:30 +03001509 }
1510}
1511
1512static void handle_detach(GdbCmdContext *gdb_ctx, void *user_ctx)
1513{
1514 GDBProcess *process;
Jon Doron3e2c1262019-05-29 09:41:30 +03001515 uint32_t pid = 1;
1516
Alex Bennéea346af32020-03-16 17:21:34 +00001517 if (gdbserver_state.multiprocess) {
Jon Doron3e2c1262019-05-29 09:41:30 +03001518 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001519 put_packet("E22");
Jon Doron3e2c1262019-05-29 09:41:30 +03001520 return;
1521 }
1522
1523 pid = gdb_ctx->params[0].val_ul;
1524 }
1525
Alex Bennéea346af32020-03-16 17:21:34 +00001526 process = gdb_get_process(pid);
1527 gdb_process_breakpoint_remove_all(process);
Jon Doron3e2c1262019-05-29 09:41:30 +03001528 process->attached = false;
1529
Alex Bennéea346af32020-03-16 17:21:34 +00001530 if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) {
1531 gdbserver_state.c_cpu = gdb_first_attached_cpu();
Jon Doron3e2c1262019-05-29 09:41:30 +03001532 }
1533
Alex Bennéea346af32020-03-16 17:21:34 +00001534 if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) {
1535 gdbserver_state.g_cpu = gdb_first_attached_cpu();
Jon Doron3e2c1262019-05-29 09:41:30 +03001536 }
1537
Alex Bennéea346af32020-03-16 17:21:34 +00001538 if (!gdbserver_state.c_cpu) {
Jon Doron3e2c1262019-05-29 09:41:30 +03001539 /* No more process attached */
1540 gdb_syscall_mode = GDB_SYS_DISABLED;
Alex Bennéea346af32020-03-16 17:21:34 +00001541 gdb_continue();
Jon Doron3e2c1262019-05-29 09:41:30 +03001542 }
Alex Bennéea346af32020-03-16 17:21:34 +00001543 put_packet("OK");
Jon Doron3e2c1262019-05-29 09:41:30 +03001544}
1545
Jon Doron44ffded2019-05-29 09:41:31 +03001546static void handle_thread_alive(GdbCmdContext *gdb_ctx, void *user_ctx)
1547{
1548 CPUState *cpu;
1549
1550 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001551 put_packet("E22");
Jon Doron44ffded2019-05-29 09:41:31 +03001552 return;
1553 }
1554
1555 if (gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
Alex Bennéea346af32020-03-16 17:21:34 +00001556 put_packet("E22");
Jon Doron44ffded2019-05-29 09:41:31 +03001557 return;
1558 }
1559
Alex Bennéea346af32020-03-16 17:21:34 +00001560 cpu = gdb_get_cpu(gdb_ctx->params[0].thread_id.pid,
Jon Doron44ffded2019-05-29 09:41:31 +03001561 gdb_ctx->params[0].thread_id.tid);
1562 if (!cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +00001563 put_packet("E22");
Jon Doron44ffded2019-05-29 09:41:31 +03001564 return;
1565 }
1566
Alex Bennéea346af32020-03-16 17:21:34 +00001567 put_packet("OK");
Jon Doron44ffded2019-05-29 09:41:31 +03001568}
1569
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001570static void handle_continue(GdbCmdContext *gdb_ctx, void *user_ctx)
1571{
1572 if (gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001573 gdb_set_cpu_pc(gdb_ctx->params[0].val_ull);
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001574 }
1575
Alex Bennéea346af32020-03-16 17:21:34 +00001576 gdbserver_state.signal = 0;
1577 gdb_continue();
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001578}
1579
Jon Doronccc47d52019-05-29 09:41:33 +03001580static void handle_cont_with_sig(GdbCmdContext *gdb_ctx, void *user_ctx)
1581{
1582 unsigned long signal = 0;
1583
1584 /*
1585 * Note: C sig;[addr] is currently unsupported and we simply
1586 * omit the addr parameter
1587 */
1588 if (gdb_ctx->num_params) {
1589 signal = gdb_ctx->params[0].val_ul;
1590 }
1591
Alex Bennéea346af32020-03-16 17:21:34 +00001592 gdbserver_state.signal = gdb_signal_to_target(signal);
1593 if (gdbserver_state.signal == -1) {
1594 gdbserver_state.signal = 0;
Jon Doronccc47d52019-05-29 09:41:33 +03001595 }
Alex Bennéea346af32020-03-16 17:21:34 +00001596 gdb_continue();
Jon Doronccc47d52019-05-29 09:41:33 +03001597}
1598
Jon Doron3a9651d2019-05-29 09:41:34 +03001599static void handle_set_thread(GdbCmdContext *gdb_ctx, void *user_ctx)
1600{
1601 CPUState *cpu;
1602
1603 if (gdb_ctx->num_params != 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001604 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001605 return;
1606 }
1607
1608 if (gdb_ctx->params[1].thread_id.kind == GDB_READ_THREAD_ERR) {
Alex Bennéea346af32020-03-16 17:21:34 +00001609 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001610 return;
1611 }
1612
1613 if (gdb_ctx->params[1].thread_id.kind != GDB_ONE_THREAD) {
Alex Bennéea346af32020-03-16 17:21:34 +00001614 put_packet("OK");
Jon Doron3a9651d2019-05-29 09:41:34 +03001615 return;
1616 }
1617
Alex Bennéea346af32020-03-16 17:21:34 +00001618 cpu = gdb_get_cpu(gdb_ctx->params[1].thread_id.pid,
Jon Doron3a9651d2019-05-29 09:41:34 +03001619 gdb_ctx->params[1].thread_id.tid);
1620 if (!cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +00001621 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001622 return;
1623 }
1624
1625 /*
1626 * Note: This command is deprecated and modern gdb's will be using the
1627 * vCont command instead.
1628 */
1629 switch (gdb_ctx->params[0].opcode) {
1630 case 'c':
Alex Bennéea346af32020-03-16 17:21:34 +00001631 gdbserver_state.c_cpu = cpu;
1632 put_packet("OK");
Jon Doron3a9651d2019-05-29 09:41:34 +03001633 break;
1634 case 'g':
Alex Bennéea346af32020-03-16 17:21:34 +00001635 gdbserver_state.g_cpu = cpu;
1636 put_packet("OK");
Jon Doron3a9651d2019-05-29 09:41:34 +03001637 break;
1638 default:
Alex Bennéea346af32020-03-16 17:21:34 +00001639 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001640 break;
1641 }
1642}
1643
Jon Doron77f6ce52019-05-29 09:41:35 +03001644static void handle_insert_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1645{
1646 int res;
1647
1648 if (gdb_ctx->num_params != 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00001649 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001650 return;
1651 }
1652
1653 res = gdb_breakpoint_insert(gdb_ctx->params[0].val_ul,
1654 gdb_ctx->params[1].val_ull,
1655 gdb_ctx->params[2].val_ull);
1656 if (res >= 0) {
Alex Bennéea346af32020-03-16 17:21:34 +00001657 put_packet("OK");
Jon Doron77f6ce52019-05-29 09:41:35 +03001658 return;
1659 } else if (res == -ENOSYS) {
Alex Bennéea346af32020-03-16 17:21:34 +00001660 put_packet("");
Jon Doron77f6ce52019-05-29 09:41:35 +03001661 return;
1662 }
1663
Alex Bennéea346af32020-03-16 17:21:34 +00001664 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001665}
1666
1667static void handle_remove_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1668{
1669 int res;
1670
1671 if (gdb_ctx->num_params != 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00001672 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001673 return;
1674 }
1675
1676 res = gdb_breakpoint_remove(gdb_ctx->params[0].val_ul,
1677 gdb_ctx->params[1].val_ull,
1678 gdb_ctx->params[2].val_ull);
1679 if (res >= 0) {
Alex Bennéea346af32020-03-16 17:21:34 +00001680 put_packet("OK");
Jon Doron77f6ce52019-05-29 09:41:35 +03001681 return;
1682 } else if (res == -ENOSYS) {
Alex Bennéea346af32020-03-16 17:21:34 +00001683 put_packet("");
Jon Doron77f6ce52019-05-29 09:41:35 +03001684 return;
1685 }
1686
Alex Bennéea346af32020-03-16 17:21:34 +00001687 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001688}
1689
Alex Bennée94b2a622019-07-05 14:23:07 +01001690/*
1691 * handle_set/get_reg
1692 *
1693 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1694 * This works, but can be very slow. Anything new enough to understand
1695 * XML also knows how to use this properly. However to use this we
1696 * need to define a local XML file as well as be talking to a
1697 * reasonably modern gdb. Responding with an empty packet will cause
1698 * the remote gdb to fallback to older methods.
1699 */
1700
Jon Doron62b33202019-05-29 09:41:36 +03001701static void handle_set_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1702{
1703 int reg_size;
1704
1705 if (!gdb_has_xml) {
Alex Bennéea346af32020-03-16 17:21:34 +00001706 put_packet("");
Jon Doron62b33202019-05-29 09:41:36 +03001707 return;
1708 }
1709
1710 if (gdb_ctx->num_params != 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001711 put_packet("E22");
Jon Doron62b33202019-05-29 09:41:36 +03001712 return;
1713 }
1714
1715 reg_size = strlen(gdb_ctx->params[1].data) / 2;
1716 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[1].data, reg_size);
Alex Bennéea346af32020-03-16 17:21:34 +00001717 gdb_write_register(gdbserver_state.g_cpu, gdb_ctx->mem_buf,
Jon Doron62b33202019-05-29 09:41:36 +03001718 gdb_ctx->params[0].val_ull);
Alex Bennéea346af32020-03-16 17:21:34 +00001719 put_packet("OK");
Jon Doron62b33202019-05-29 09:41:36 +03001720}
1721
Jon Doron5d0e57b2019-05-29 09:41:37 +03001722static void handle_get_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1723{
1724 int reg_size;
1725
Jon Doron5d0e57b2019-05-29 09:41:37 +03001726 if (!gdb_has_xml) {
Alex Bennéea346af32020-03-16 17:21:34 +00001727 put_packet("");
Jon Doron5d0e57b2019-05-29 09:41:37 +03001728 return;
1729 }
1730
1731 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001732 put_packet("E14");
Jon Doron5d0e57b2019-05-29 09:41:37 +03001733 return;
1734 }
1735
Alex Bennéea346af32020-03-16 17:21:34 +00001736 reg_size = gdb_read_register(gdbserver_state.g_cpu, gdb_ctx->mem_buf,
Jon Doron5d0e57b2019-05-29 09:41:37 +03001737 gdb_ctx->params[0].val_ull);
1738 if (!reg_size) {
Alex Bennéea346af32020-03-16 17:21:34 +00001739 put_packet("E14");
Jon Doron5d0e57b2019-05-29 09:41:37 +03001740 return;
1741 }
1742
1743 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, reg_size);
Alex Bennéea346af32020-03-16 17:21:34 +00001744 put_packet(gdb_ctx->str_buf);
Jon Doron5d0e57b2019-05-29 09:41:37 +03001745}
1746
Jon Doroncc0ecc72019-05-29 09:41:38 +03001747static void handle_write_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1748{
1749 if (gdb_ctx->num_params != 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00001750 put_packet("E22");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001751 return;
1752 }
1753
1754 /* hextomem() reads 2*len bytes */
1755 if (gdb_ctx->params[1].val_ull > strlen(gdb_ctx->params[2].data) / 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001756 put_packet("E22");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001757 return;
1758 }
1759
1760 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[2].data,
1761 gdb_ctx->params[1].val_ull);
Alex Bennéea346af32020-03-16 17:21:34 +00001762 if (target_memory_rw_debug(gdbserver_state.g_cpu, gdb_ctx->params[0].val_ull,
Jon Doroncc0ecc72019-05-29 09:41:38 +03001763 gdb_ctx->mem_buf,
1764 gdb_ctx->params[1].val_ull, true)) {
Alex Bennéea346af32020-03-16 17:21:34 +00001765 put_packet("E14");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001766 return;
1767 }
1768
Alex Bennéea346af32020-03-16 17:21:34 +00001769 put_packet("OK");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001770}
1771
Jon Doronda92e232019-05-29 09:41:39 +03001772static void handle_read_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1773{
1774 if (gdb_ctx->num_params != 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001775 put_packet("E22");
Jon Doronda92e232019-05-29 09:41:39 +03001776 return;
1777 }
1778
1779 /* memtohex() doubles the required space */
1780 if (gdb_ctx->params[1].val_ull > MAX_PACKET_LENGTH / 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001781 put_packet("E22");
Jon Doronda92e232019-05-29 09:41:39 +03001782 return;
1783 }
1784
Alex Bennéea346af32020-03-16 17:21:34 +00001785 if (target_memory_rw_debug(gdbserver_state.g_cpu, gdb_ctx->params[0].val_ull,
Jon Doronda92e232019-05-29 09:41:39 +03001786 gdb_ctx->mem_buf,
1787 gdb_ctx->params[1].val_ull, false)) {
Alex Bennéea346af32020-03-16 17:21:34 +00001788 put_packet("E14");
Jon Doronda92e232019-05-29 09:41:39 +03001789 return;
1790 }
1791
1792 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, gdb_ctx->params[1].val_ull);
Alex Bennéea346af32020-03-16 17:21:34 +00001793 put_packet(gdb_ctx->str_buf);
Jon Doronda92e232019-05-29 09:41:39 +03001794}
1795
Jon Doron287ca122019-05-29 09:41:40 +03001796static void handle_write_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1797{
1798 target_ulong addr, len;
1799 uint8_t *registers;
1800 int reg_size;
1801
1802 if (!gdb_ctx->num_params) {
1803 return;
1804 }
1805
Alex Bennéea346af32020-03-16 17:21:34 +00001806 cpu_synchronize_state(gdbserver_state.g_cpu);
Jon Doron287ca122019-05-29 09:41:40 +03001807 registers = gdb_ctx->mem_buf;
1808 len = strlen(gdb_ctx->params[0].data) / 2;
1809 hextomem(registers, gdb_ctx->params[0].data, len);
Alex Bennéea346af32020-03-16 17:21:34 +00001810 for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0;
Jon Doron287ca122019-05-29 09:41:40 +03001811 addr++) {
Alex Bennéea346af32020-03-16 17:21:34 +00001812 reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, addr);
Jon Doron287ca122019-05-29 09:41:40 +03001813 len -= reg_size;
1814 registers += reg_size;
1815 }
Alex Bennéea346af32020-03-16 17:21:34 +00001816 put_packet("OK");
Jon Doron287ca122019-05-29 09:41:40 +03001817}
1818
Jon Doron397d1372019-05-29 09:41:41 +03001819static void handle_read_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1820{
1821 target_ulong addr, len;
1822
Alex Bennéea346af32020-03-16 17:21:34 +00001823 cpu_synchronize_state(gdbserver_state.g_cpu);
Jon Doron397d1372019-05-29 09:41:41 +03001824 len = 0;
Alex Bennéea346af32020-03-16 17:21:34 +00001825 for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs; addr++) {
1826 len += gdb_read_register(gdbserver_state.g_cpu, gdb_ctx->mem_buf + len,
Jon Doron397d1372019-05-29 09:41:41 +03001827 addr);
1828 }
1829
1830 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, len);
Alex Bennéea346af32020-03-16 17:21:34 +00001831 put_packet(gdb_ctx->str_buf);
Jon Doron397d1372019-05-29 09:41:41 +03001832}
1833
Jon Doron4b20fab2019-05-29 09:41:42 +03001834static void handle_file_io(GdbCmdContext *gdb_ctx, void *user_ctx)
1835{
Alex Bennéea346af32020-03-16 17:21:34 +00001836 if (gdb_ctx->num_params >= 1 && gdbserver_state.current_syscall_cb) {
Jon Doron4b20fab2019-05-29 09:41:42 +03001837 target_ulong ret, err;
1838
1839 ret = (target_ulong)gdb_ctx->params[0].val_ull;
Sandra Loosemorec6ee9522019-08-27 16:33:17 -06001840 if (gdb_ctx->num_params >= 2) {
1841 err = (target_ulong)gdb_ctx->params[1].val_ull;
1842 } else {
1843 err = 0;
1844 }
Alex Bennéea346af32020-03-16 17:21:34 +00001845 gdbserver_state.current_syscall_cb(gdbserver_state.c_cpu, ret, err);
1846 gdbserver_state.current_syscall_cb = NULL;
Jon Doron4b20fab2019-05-29 09:41:42 +03001847 }
1848
1849 if (gdb_ctx->num_params >= 3 && gdb_ctx->params[2].opcode == (uint8_t)'C') {
Alex Bennéea346af32020-03-16 17:21:34 +00001850 put_packet("T02");
Jon Doron4b20fab2019-05-29 09:41:42 +03001851 return;
1852 }
1853
Alex Bennéea346af32020-03-16 17:21:34 +00001854 gdb_continue();
Jon Doron4b20fab2019-05-29 09:41:42 +03001855}
1856
Jon Doron933f80d2019-05-29 09:41:43 +03001857static void handle_step(GdbCmdContext *gdb_ctx, void *user_ctx)
1858{
1859 if (gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001860 gdb_set_cpu_pc((target_ulong)gdb_ctx->params[0].val_ull);
Jon Doron933f80d2019-05-29 09:41:43 +03001861 }
1862
Alex Bennéea346af32020-03-16 17:21:34 +00001863 cpu_single_step(gdbserver_state.c_cpu, sstep_flags);
1864 gdb_continue();
Jon Doron933f80d2019-05-29 09:41:43 +03001865}
1866
Jon Doron8536ec02019-05-29 09:41:44 +03001867static void handle_v_cont_query(GdbCmdContext *gdb_ctx, void *user_ctx)
1868{
Alex Bennéea346af32020-03-16 17:21:34 +00001869 put_packet("vCont;c;C;s;S");
Jon Doron8536ec02019-05-29 09:41:44 +03001870}
1871
1872static void handle_v_cont(GdbCmdContext *gdb_ctx, void *user_ctx)
1873{
1874 int res;
1875
1876 if (!gdb_ctx->num_params) {
1877 return;
1878 }
1879
Alex Bennéea346af32020-03-16 17:21:34 +00001880 res = gdb_handle_vcont(gdb_ctx->params[0].data);
Jon Doron8536ec02019-05-29 09:41:44 +03001881 if ((res == -EINVAL) || (res == -ERANGE)) {
Alex Bennéea346af32020-03-16 17:21:34 +00001882 put_packet("E22");
Jon Doron8536ec02019-05-29 09:41:44 +03001883 } else if (res) {
Alex Bennéea346af32020-03-16 17:21:34 +00001884 put_packet("");
Jon Doron8536ec02019-05-29 09:41:44 +03001885 }
1886}
1887
1888static void handle_v_attach(GdbCmdContext *gdb_ctx, void *user_ctx)
1889{
1890 GDBProcess *process;
1891 CPUState *cpu;
1892 char thread_id[16];
1893
1894 pstrcpy(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "E22");
1895 if (!gdb_ctx->num_params) {
1896 goto cleanup;
1897 }
1898
Alex Bennéea346af32020-03-16 17:21:34 +00001899 process = gdb_get_process(gdb_ctx->params[0].val_ul);
Jon Doron8536ec02019-05-29 09:41:44 +03001900 if (!process) {
1901 goto cleanup;
1902 }
1903
Alex Bennéea346af32020-03-16 17:21:34 +00001904 cpu = get_first_cpu_in_process(process);
Jon Doron8536ec02019-05-29 09:41:44 +03001905 if (!cpu) {
1906 goto cleanup;
1907 }
1908
1909 process->attached = true;
Alex Bennéea346af32020-03-16 17:21:34 +00001910 gdbserver_state.g_cpu = cpu;
1911 gdbserver_state.c_cpu = cpu;
Jon Doron8536ec02019-05-29 09:41:44 +03001912
Alex Bennéea346af32020-03-16 17:21:34 +00001913 gdb_fmt_thread_id(cpu, thread_id, sizeof(thread_id));
Jon Doron8536ec02019-05-29 09:41:44 +03001914 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "T%02xthread:%s;",
1915 GDB_SIGNAL_TRAP, thread_id);
1916cleanup:
Alex Bennéea346af32020-03-16 17:21:34 +00001917 put_packet(gdb_ctx->str_buf);
Jon Doron8536ec02019-05-29 09:41:44 +03001918}
1919
1920static void handle_v_kill(GdbCmdContext *gdb_ctx, void *user_ctx)
1921{
1922 /* Kill the target */
Alex Bennéea346af32020-03-16 17:21:34 +00001923 put_packet("OK");
Jon Doron8536ec02019-05-29 09:41:44 +03001924 error_report("QEMU: Terminated via GDBstub");
1925 exit(0);
1926}
1927
1928static GdbCmdParseEntry gdb_v_commands_table[] = {
1929 /* Order is important if has same prefix */
1930 {
1931 .handler = handle_v_cont_query,
1932 .cmd = "Cont?",
1933 .cmd_startswith = 1
1934 },
1935 {
1936 .handler = handle_v_cont,
1937 .cmd = "Cont",
1938 .cmd_startswith = 1,
1939 .schema = "s0"
1940 },
1941 {
1942 .handler = handle_v_attach,
1943 .cmd = "Attach;",
1944 .cmd_startswith = 1,
1945 .schema = "l0"
1946 },
1947 {
1948 .handler = handle_v_kill,
1949 .cmd = "Kill;",
1950 .cmd_startswith = 1
1951 },
1952};
1953
1954static void handle_v_commands(GdbCmdContext *gdb_ctx, void *user_ctx)
1955{
1956 if (!gdb_ctx->num_params) {
1957 return;
1958 }
1959
Alex Bennéea346af32020-03-16 17:21:34 +00001960 if (process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron8536ec02019-05-29 09:41:44 +03001961 gdb_v_commands_table,
1962 ARRAY_SIZE(gdb_v_commands_table))) {
Alex Bennéea346af32020-03-16 17:21:34 +00001963 put_packet("");
Jon Doron8536ec02019-05-29 09:41:44 +03001964 }
1965}
1966
Jon Doron2704efa2019-05-29 09:41:45 +03001967static void handle_query_qemu_sstepbits(GdbCmdContext *gdb_ctx, void *user_ctx)
1968{
1969 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
1970 "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", SSTEP_ENABLE,
1971 SSTEP_NOIRQ, SSTEP_NOTIMER);
Alex Bennéea346af32020-03-16 17:21:34 +00001972 put_packet(gdb_ctx->str_buf);
Jon Doron2704efa2019-05-29 09:41:45 +03001973}
1974
1975static void handle_set_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
1976{
1977 if (!gdb_ctx->num_params) {
1978 return;
1979 }
1980
1981 sstep_flags = gdb_ctx->params[0].val_ul;
Alex Bennéea346af32020-03-16 17:21:34 +00001982 put_packet("OK");
Jon Doron2704efa2019-05-29 09:41:45 +03001983}
1984
1985static void handle_query_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
1986{
1987 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "0x%x", sstep_flags);
Alex Bennéea346af32020-03-16 17:21:34 +00001988 put_packet(gdb_ctx->str_buf);
Jon Doron2704efa2019-05-29 09:41:45 +03001989}
1990
1991static void handle_query_curr_tid(GdbCmdContext *gdb_ctx, void *user_ctx)
bellardb4608c02003-06-27 17:34:32 +00001992{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001993 CPUState *cpu;
Luc Michelc145eea2019-01-07 15:23:46 +00001994 GDBProcess *process;
Jon Doron2704efa2019-05-29 09:41:45 +03001995 char thread_id[16];
1996
1997 /*
1998 * "Current thread" remains vague in the spec, so always return
1999 * the first thread of the current process (gdb returns the
2000 * first thread).
2001 */
Alex Bennéea346af32020-03-16 17:21:34 +00002002 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
2003 cpu = get_first_cpu_in_process(process);
2004 gdb_fmt_thread_id(cpu, thread_id, sizeof(thread_id));
Jon Doron2704efa2019-05-29 09:41:45 +03002005 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "QC%s", thread_id);
Alex Bennéea346af32020-03-16 17:21:34 +00002006 put_packet(gdb_ctx->str_buf);
Jon Doron2704efa2019-05-29 09:41:45 +03002007}
2008
2009static void handle_query_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
2010{
2011 char thread_id[16];
2012
Alex Bennéea346af32020-03-16 17:21:34 +00002013 if (!gdbserver_state.query_cpu) {
2014 put_packet("l");
Jon Doron2704efa2019-05-29 09:41:45 +03002015 return;
2016 }
2017
Alex Bennéea346af32020-03-16 17:21:34 +00002018 gdb_fmt_thread_id(gdbserver_state.query_cpu, thread_id,
Jon Doron2704efa2019-05-29 09:41:45 +03002019 sizeof(thread_id));
2020 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "m%s", thread_id);
Alex Bennéea346af32020-03-16 17:21:34 +00002021 put_packet(gdb_ctx->str_buf);
2022 gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu);
Jon Doron2704efa2019-05-29 09:41:45 +03002023}
2024
2025static void handle_query_first_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
2026{
Alex Bennéea346af32020-03-16 17:21:34 +00002027 gdbserver_state.query_cpu = gdb_first_attached_cpu();
Jon Doron2704efa2019-05-29 09:41:45 +03002028 handle_query_threads(gdb_ctx, user_ctx);
2029}
2030
2031static void handle_query_thread_extra(GdbCmdContext *gdb_ctx, void *user_ctx)
2032{
2033 CPUState *cpu;
2034 int len;
2035
2036 if (!gdb_ctx->num_params ||
2037 gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
Alex Bennéea346af32020-03-16 17:21:34 +00002038 put_packet("E22");
Jon Doron2704efa2019-05-29 09:41:45 +03002039 return;
2040 }
2041
Alex Bennéea346af32020-03-16 17:21:34 +00002042 cpu = gdb_get_cpu(gdb_ctx->params[0].thread_id.pid,
Jon Doron2704efa2019-05-29 09:41:45 +03002043 gdb_ctx->params[0].thread_id.tid);
2044 if (!cpu) {
2045 return;
2046 }
2047
2048 cpu_synchronize_state(cpu);
2049
Alex Bennéea346af32020-03-16 17:21:34 +00002050 if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) {
Jon Doron2704efa2019-05-29 09:41:45 +03002051 /* Print the CPU model and name in multiprocess mode */
2052 ObjectClass *oc = object_get_class(OBJECT(cpu));
2053 const char *cpu_model = object_class_get_name(oc);
2054 char *cpu_name = object_get_canonical_path_component(OBJECT(cpu));
2055 len = snprintf((char *)gdb_ctx->mem_buf, sizeof(gdb_ctx->str_buf) / 2,
2056 "%s %s [%s]", cpu_model, cpu_name,
2057 cpu->halted ? "halted " : "running");
2058 g_free(cpu_name);
2059 } else {
2060 /* memtohex() doubles the required space */
2061 len = snprintf((char *)gdb_ctx->mem_buf, sizeof(gdb_ctx->str_buf) / 2,
2062 "CPU#%d [%s]", cpu->cpu_index,
2063 cpu->halted ? "halted " : "running");
2064 }
2065 trace_gdbstub_op_extra_info((char *)gdb_ctx->mem_buf);
2066 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, len);
Alex Bennéea346af32020-03-16 17:21:34 +00002067 put_packet(gdb_ctx->str_buf);
Jon Doron2704efa2019-05-29 09:41:45 +03002068}
2069
2070#ifdef CONFIG_USER_ONLY
2071static void handle_query_offsets(GdbCmdContext *gdb_ctx, void *user_ctx)
2072{
2073 TaskState *ts;
2074
Alex Bennéea346af32020-03-16 17:21:34 +00002075 ts = gdbserver_state.c_cpu->opaque;
Jon Doron2704efa2019-05-29 09:41:45 +03002076 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
2077 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
2078 ";Bss=" TARGET_ABI_FMT_lx,
2079 ts->info->code_offset,
2080 ts->info->data_offset,
2081 ts->info->data_offset);
Alex Bennéea346af32020-03-16 17:21:34 +00002082 put_packet(gdb_ctx->str_buf);
Jon Doron2704efa2019-05-29 09:41:45 +03002083}
2084#else
2085static void handle_query_rcmd(GdbCmdContext *gdb_ctx, void *user_ctx)
2086{
2087 int len;
2088
2089 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00002090 put_packet("E22");
Jon Doron2704efa2019-05-29 09:41:45 +03002091 return;
2092 }
2093
2094 len = strlen(gdb_ctx->params[0].data);
2095 if (len % 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00002096 put_packet("E01");
Jon Doron2704efa2019-05-29 09:41:45 +03002097 return;
2098 }
2099
2100 len = len / 2;
2101 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[0].data, len);
2102 gdb_ctx->mem_buf[len++] = 0;
Alex Bennéea346af32020-03-16 17:21:34 +00002103 qemu_chr_be_write(gdbserver_state.mon_chr, gdb_ctx->mem_buf, len);
2104 put_packet("OK");
Jon Doron2704efa2019-05-29 09:41:45 +03002105
2106}
2107#endif
2108
2109static void handle_query_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2110{
Andreas Färber5b24c642013-07-07 15:08:22 +02002111 CPUClass *cc;
Jon Doron2704efa2019-05-29 09:41:45 +03002112
2113 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "PacketSize=%x",
2114 MAX_PACKET_LENGTH);
2115 cc = CPU_GET_CLASS(first_cpu);
2116 if (cc->gdb_core_xml_file) {
2117 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
2118 ";qXfer:features:read+");
2119 }
2120
2121 if (gdb_ctx->num_params &&
2122 strstr(gdb_ctx->params[0].data, "multiprocess+")) {
Alex Bennéea346af32020-03-16 17:21:34 +00002123 gdbserver_state.multiprocess = true;
Jon Doron2704efa2019-05-29 09:41:45 +03002124 }
2125
2126 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), ";multiprocess+");
Alex Bennéea346af32020-03-16 17:21:34 +00002127 put_packet(gdb_ctx->str_buf);
Jon Doron2704efa2019-05-29 09:41:45 +03002128}
2129
2130static void handle_query_xfer_features(GdbCmdContext *gdb_ctx, void *user_ctx)
2131{
2132 GDBProcess *process;
2133 CPUClass *cc;
2134 unsigned long len, total_len, addr;
2135 const char *xml;
bellardb4608c02003-06-27 17:34:32 +00002136 const char *p;
Jon Doron2704efa2019-05-29 09:41:45 +03002137
2138 if (gdb_ctx->num_params < 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00002139 put_packet("E22");
Jon Doron2704efa2019-05-29 09:41:45 +03002140 return;
2141 }
2142
Alex Bennéea346af32020-03-16 17:21:34 +00002143 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
2144 cc = CPU_GET_CLASS(gdbserver_state.g_cpu);
Jon Doron2704efa2019-05-29 09:41:45 +03002145 if (!cc->gdb_core_xml_file) {
Alex Bennéea346af32020-03-16 17:21:34 +00002146 put_packet("");
Jon Doron2704efa2019-05-29 09:41:45 +03002147 return;
2148 }
2149
2150 gdb_has_xml = true;
2151 p = gdb_ctx->params[0].data;
Alex Bennéea346af32020-03-16 17:21:34 +00002152 xml = get_feature_xml(p, &p, process);
Jon Doron2704efa2019-05-29 09:41:45 +03002153 if (!xml) {
Alex Bennéea346af32020-03-16 17:21:34 +00002154 put_packet("E00");
Jon Doron2704efa2019-05-29 09:41:45 +03002155 return;
2156 }
2157
2158 addr = gdb_ctx->params[1].val_ul;
2159 len = gdb_ctx->params[2].val_ul;
2160 total_len = strlen(xml);
2161 if (addr > total_len) {
Alex Bennéea346af32020-03-16 17:21:34 +00002162 put_packet("E00");
Jon Doron2704efa2019-05-29 09:41:45 +03002163 return;
2164 }
2165
2166 if (len > (MAX_PACKET_LENGTH - 5) / 2) {
2167 len = (MAX_PACKET_LENGTH - 5) / 2;
2168 }
2169
2170 if (len < total_len - addr) {
2171 gdb_ctx->str_buf[0] = 'm';
2172 len = memtox(gdb_ctx->str_buf + 1, xml + addr, len);
2173 } else {
2174 gdb_ctx->str_buf[0] = 'l';
2175 len = memtox(gdb_ctx->str_buf + 1, xml + addr, total_len - addr);
2176 }
2177
Alex Bennéea346af32020-03-16 17:21:34 +00002178 put_packet_binary(gdb_ctx->str_buf, len + 1, true);
Jon Doron2704efa2019-05-29 09:41:45 +03002179}
2180
2181static void handle_query_attached(GdbCmdContext *gdb_ctx, void *user_ctx)
2182{
Alex Bennéea346af32020-03-16 17:21:34 +00002183 put_packet(GDB_ATTACHED);
Jon Doron2704efa2019-05-29 09:41:45 +03002184}
2185
2186static void handle_query_qemu_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2187{
Jon Doronab4752e2019-05-29 09:41:48 +03002188 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "sstepbits;sstep");
2189#ifndef CONFIG_USER_ONLY
2190 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), ";PhyMemMode");
2191#endif
Alex Bennéea346af32020-03-16 17:21:34 +00002192 put_packet(gdb_ctx->str_buf);
Jon Doron2704efa2019-05-29 09:41:45 +03002193}
2194
Jon Doronab4752e2019-05-29 09:41:48 +03002195#ifndef CONFIG_USER_ONLY
2196static void handle_query_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx,
2197 void *user_ctx)
2198{
2199 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "%d", phy_memory_mode);
Alex Bennéea346af32020-03-16 17:21:34 +00002200 put_packet(gdb_ctx->str_buf);
Jon Doronab4752e2019-05-29 09:41:48 +03002201}
2202
2203static void handle_set_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx, void *user_ctx)
2204{
2205 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00002206 put_packet("E22");
Jon Doronab4752e2019-05-29 09:41:48 +03002207 return;
2208 }
2209
2210 if (!gdb_ctx->params[0].val_ul) {
2211 phy_memory_mode = 0;
2212 } else {
2213 phy_memory_mode = 1;
2214 }
Alex Bennéea346af32020-03-16 17:21:34 +00002215 put_packet("OK");
Jon Doronab4752e2019-05-29 09:41:48 +03002216}
2217#endif
2218
Jon Doron2704efa2019-05-29 09:41:45 +03002219static GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
2220 /* Order is important if has same prefix */
2221 {
2222 .handler = handle_query_qemu_sstepbits,
2223 .cmd = "qemu.sstepbits",
2224 },
2225 {
2226 .handler = handle_query_qemu_sstep,
2227 .cmd = "qemu.sstep",
2228 },
2229 {
2230 .handler = handle_set_qemu_sstep,
2231 .cmd = "qemu.sstep=",
2232 .cmd_startswith = 1,
2233 .schema = "l0"
2234 },
2235};
2236
2237static GdbCmdParseEntry gdb_gen_query_table[] = {
2238 {
2239 .handler = handle_query_curr_tid,
2240 .cmd = "C",
2241 },
2242 {
2243 .handler = handle_query_threads,
2244 .cmd = "sThreadInfo",
2245 },
2246 {
2247 .handler = handle_query_first_threads,
2248 .cmd = "fThreadInfo",
2249 },
2250 {
2251 .handler = handle_query_thread_extra,
2252 .cmd = "ThreadExtraInfo,",
2253 .cmd_startswith = 1,
2254 .schema = "t0"
2255 },
2256#ifdef CONFIG_USER_ONLY
2257 {
2258 .handler = handle_query_offsets,
2259 .cmd = "Offsets",
2260 },
2261#else
2262 {
2263 .handler = handle_query_rcmd,
2264 .cmd = "Rcmd,",
2265 .cmd_startswith = 1,
2266 .schema = "s0"
2267 },
2268#endif
2269 {
2270 .handler = handle_query_supported,
2271 .cmd = "Supported:",
2272 .cmd_startswith = 1,
2273 .schema = "s0"
2274 },
2275 {
2276 .handler = handle_query_supported,
2277 .cmd = "Supported",
2278 .schema = "s0"
2279 },
2280 {
2281 .handler = handle_query_xfer_features,
2282 .cmd = "Xfer:features:read:",
2283 .cmd_startswith = 1,
2284 .schema = "s:l,l0"
2285 },
2286 {
2287 .handler = handle_query_attached,
2288 .cmd = "Attached:",
2289 .cmd_startswith = 1
2290 },
2291 {
2292 .handler = handle_query_attached,
2293 .cmd = "Attached",
2294 },
2295 {
2296 .handler = handle_query_qemu_supported,
2297 .cmd = "qemu.Supported",
2298 },
Jon Doronab4752e2019-05-29 09:41:48 +03002299#ifndef CONFIG_USER_ONLY
2300 {
2301 .handler = handle_query_qemu_phy_mem_mode,
2302 .cmd = "qemu.PhyMemMode",
2303 },
2304#endif
Jon Doron2704efa2019-05-29 09:41:45 +03002305};
2306
2307static GdbCmdParseEntry gdb_gen_set_table[] = {
2308 /* Order is important if has same prefix */
2309 {
2310 .handler = handle_set_qemu_sstep,
2311 .cmd = "qemu.sstep:",
2312 .cmd_startswith = 1,
2313 .schema = "l0"
2314 },
Jon Doronab4752e2019-05-29 09:41:48 +03002315#ifndef CONFIG_USER_ONLY
2316 {
2317 .handler = handle_set_qemu_phy_mem_mode,
2318 .cmd = "qemu.PhyMemMode:",
2319 .cmd_startswith = 1,
2320 .schema = "l0"
2321 },
2322#endif
Jon Doron2704efa2019-05-29 09:41:45 +03002323};
2324
2325static void handle_gen_query(GdbCmdContext *gdb_ctx, void *user_ctx)
2326{
2327 if (!gdb_ctx->num_params) {
2328 return;
2329 }
2330
Alex Bennéea346af32020-03-16 17:21:34 +00002331 if (!process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron2704efa2019-05-29 09:41:45 +03002332 gdb_gen_query_set_common_table,
2333 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2334 return;
2335 }
2336
Alex Bennéea346af32020-03-16 17:21:34 +00002337 if (process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron2704efa2019-05-29 09:41:45 +03002338 gdb_gen_query_table,
2339 ARRAY_SIZE(gdb_gen_query_table))) {
Alex Bennéea346af32020-03-16 17:21:34 +00002340 put_packet("");
Jon Doron2704efa2019-05-29 09:41:45 +03002341 }
2342}
2343
2344static void handle_gen_set(GdbCmdContext *gdb_ctx, void *user_ctx)
2345{
2346 if (!gdb_ctx->num_params) {
2347 return;
2348 }
2349
Alex Bennéea346af32020-03-16 17:21:34 +00002350 if (!process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron2704efa2019-05-29 09:41:45 +03002351 gdb_gen_query_set_common_table,
2352 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2353 return;
2354 }
2355
Alex Bennéea346af32020-03-16 17:21:34 +00002356 if (process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron2704efa2019-05-29 09:41:45 +03002357 gdb_gen_set_table,
2358 ARRAY_SIZE(gdb_gen_set_table))) {
Alex Bennéea346af32020-03-16 17:21:34 +00002359 put_packet("");
Jon Doron2704efa2019-05-29 09:41:45 +03002360 }
2361}
2362
Jon Doron7009d572019-05-29 09:41:46 +03002363static void handle_target_halt(GdbCmdContext *gdb_ctx, void *user_ctx)
2364{
2365 char thread_id[16];
2366
Alex Bennéea346af32020-03-16 17:21:34 +00002367 gdb_fmt_thread_id(gdbserver_state.c_cpu, thread_id,
Jon Doron7009d572019-05-29 09:41:46 +03002368 sizeof(thread_id));
2369 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "T%02xthread:%s;",
2370 GDB_SIGNAL_TRAP, thread_id);
Alex Bennéea346af32020-03-16 17:21:34 +00002371 put_packet(gdb_ctx->str_buf);
Jon Doron7009d572019-05-29 09:41:46 +03002372 /*
2373 * Remove all the breakpoints when this query is issued,
2374 * because gdb is doing an initial connect and the state
2375 * should be cleaned up.
2376 */
2377 gdb_breakpoint_remove_all();
2378}
2379
Alex Bennéea346af32020-03-16 17:21:34 +00002380static int gdb_handle_packet(const char *line_buf)
Jon Doron2704efa2019-05-29 09:41:45 +03002381{
Jon Doron3e2c1262019-05-29 09:41:30 +03002382 const GdbCmdParseEntry *cmd_parser = NULL;
ths3b46e622007-09-17 08:09:54 +00002383
Doug Gale5c9522b2017-12-02 20:30:37 -05002384 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01002385
Jon Doron3f1cbac2019-05-29 09:41:47 +03002386 switch (line_buf[0]) {
Luc Michel53fd6552019-01-07 15:23:46 +00002387 case '!':
Alex Bennéea346af32020-03-16 17:21:34 +00002388 put_packet("OK");
Luc Michel53fd6552019-01-07 15:23:46 +00002389 break;
bellard858693c2004-03-31 18:52:07 +00002390 case '?':
Jon Doron7009d572019-05-29 09:41:46 +03002391 {
2392 static const GdbCmdParseEntry target_halted_cmd_desc = {
2393 .handler = handle_target_halt,
2394 .cmd = "?",
2395 .cmd_startswith = 1
2396 };
2397 cmd_parser = &target_halted_cmd_desc;
2398 }
bellard858693c2004-03-31 18:52:07 +00002399 break;
2400 case 'c':
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002401 {
2402 static const GdbCmdParseEntry continue_cmd_desc = {
2403 .handler = handle_continue,
2404 .cmd = "c",
2405 .cmd_startswith = 1,
2406 .schema = "L0"
2407 };
2408 cmd_parser = &continue_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002409 }
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002410 break;
edgar_igl1f487ee2008-05-17 22:20:53 +00002411 case 'C':
Jon Doronccc47d52019-05-29 09:41:33 +03002412 {
2413 static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
2414 .handler = handle_cont_with_sig,
2415 .cmd = "C",
2416 .cmd_startswith = 1,
2417 .schema = "l0"
2418 };
2419 cmd_parser = &cont_with_sig_cmd_desc;
2420 }
2421 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002422 case 'v':
Jon Doron8536ec02019-05-29 09:41:44 +03002423 {
2424 static const GdbCmdParseEntry v_cmd_desc = {
2425 .handler = handle_v_commands,
2426 .cmd = "v",
2427 .cmd_startswith = 1,
2428 .schema = "s0"
2429 };
2430 cmd_parser = &v_cmd_desc;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002431 }
Jon Doron8536ec02019-05-29 09:41:44 +03002432 break;
edgar_igl7d03f822008-05-17 18:58:29 +00002433 case 'k':
2434 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002435 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00002436 exit(0);
2437 case 'D':
Jon Doron3e2c1262019-05-29 09:41:30 +03002438 {
2439 static const GdbCmdParseEntry detach_cmd_desc = {
2440 .handler = handle_detach,
2441 .cmd = "D",
2442 .cmd_startswith = 1,
2443 .schema = "?.l0"
2444 };
2445 cmd_parser = &detach_cmd_desc;
Luc Michel546f3c62019-01-07 15:23:46 +00002446 }
edgar_igl7d03f822008-05-17 18:58:29 +00002447 break;
bellard858693c2004-03-31 18:52:07 +00002448 case 's':
Jon Doron933f80d2019-05-29 09:41:43 +03002449 {
2450 static const GdbCmdParseEntry step_cmd_desc = {
2451 .handler = handle_step,
2452 .cmd = "s",
2453 .cmd_startswith = 1,
2454 .schema = "L0"
2455 };
2456 cmd_parser = &step_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002457 }
Jon Doron933f80d2019-05-29 09:41:43 +03002458 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002459 case 'F':
2460 {
Jon Doron4b20fab2019-05-29 09:41:42 +03002461 static const GdbCmdParseEntry file_io_cmd_desc = {
2462 .handler = handle_file_io,
2463 .cmd = "F",
2464 .cmd_startswith = 1,
2465 .schema = "L,L,o0"
2466 };
2467 cmd_parser = &file_io_cmd_desc;
pbrooka2d1eba2007-01-28 03:10:55 +00002468 }
2469 break;
bellard858693c2004-03-31 18:52:07 +00002470 case 'g':
Jon Doron397d1372019-05-29 09:41:41 +03002471 {
2472 static const GdbCmdParseEntry read_all_regs_cmd_desc = {
2473 .handler = handle_read_all_regs,
2474 .cmd = "g",
2475 .cmd_startswith = 1
2476 };
2477 cmd_parser = &read_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002478 }
bellard858693c2004-03-31 18:52:07 +00002479 break;
2480 case 'G':
Jon Doron287ca122019-05-29 09:41:40 +03002481 {
2482 static const GdbCmdParseEntry write_all_regs_cmd_desc = {
2483 .handler = handle_write_all_regs,
2484 .cmd = "G",
2485 .cmd_startswith = 1,
2486 .schema = "s0"
2487 };
2488 cmd_parser = &write_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002489 }
bellard858693c2004-03-31 18:52:07 +00002490 break;
2491 case 'm':
Jon Doronda92e232019-05-29 09:41:39 +03002492 {
2493 static const GdbCmdParseEntry read_mem_cmd_desc = {
2494 .handler = handle_read_mem,
2495 .cmd = "m",
2496 .cmd_startswith = 1,
2497 .schema = "L,L0"
2498 };
2499 cmd_parser = &read_mem_cmd_desc;
bellard6f970bd2005-12-05 19:55:19 +00002500 }
bellard858693c2004-03-31 18:52:07 +00002501 break;
2502 case 'M':
Jon Doroncc0ecc72019-05-29 09:41:38 +03002503 {
2504 static const GdbCmdParseEntry write_mem_cmd_desc = {
2505 .handler = handle_write_mem,
2506 .cmd = "M",
2507 .cmd_startswith = 1,
2508 .schema = "L,L:s0"
2509 };
2510 cmd_parser = &write_mem_cmd_desc;
Fabien Chouteau44520db2011-09-08 12:48:16 +02002511 }
bellard858693c2004-03-31 18:52:07 +00002512 break;
pbrook56aebc82008-10-11 17:55:29 +00002513 case 'p':
Jon Doron5d0e57b2019-05-29 09:41:37 +03002514 {
2515 static const GdbCmdParseEntry get_reg_cmd_desc = {
2516 .handler = handle_get_reg,
2517 .cmd = "p",
2518 .cmd_startswith = 1,
2519 .schema = "L0"
2520 };
2521 cmd_parser = &get_reg_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002522 }
2523 break;
2524 case 'P':
Jon Doron62b33202019-05-29 09:41:36 +03002525 {
2526 static const GdbCmdParseEntry set_reg_cmd_desc = {
2527 .handler = handle_set_reg,
2528 .cmd = "P",
2529 .cmd_startswith = 1,
2530 .schema = "L?s0"
2531 };
2532 cmd_parser = &set_reg_cmd_desc;
2533 }
pbrook56aebc82008-10-11 17:55:29 +00002534 break;
bellard858693c2004-03-31 18:52:07 +00002535 case 'Z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002536 {
2537 static const GdbCmdParseEntry insert_bp_cmd_desc = {
2538 .handler = handle_insert_bp,
2539 .cmd = "Z",
2540 .cmd_startswith = 1,
2541 .schema = "l?L?L0"
2542 };
2543 cmd_parser = &insert_bp_cmd_desc;
2544 }
2545 break;
bellard858693c2004-03-31 18:52:07 +00002546 case 'z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002547 {
2548 static const GdbCmdParseEntry remove_bp_cmd_desc = {
2549 .handler = handle_remove_bp,
2550 .cmd = "z",
2551 .cmd_startswith = 1,
2552 .schema = "l?L?L0"
2553 };
2554 cmd_parser = &remove_bp_cmd_desc;
2555 }
bellard858693c2004-03-31 18:52:07 +00002556 break;
aliguori880a7572008-11-18 20:30:24 +00002557 case 'H':
Jon Doron3a9651d2019-05-29 09:41:34 +03002558 {
2559 static const GdbCmdParseEntry set_thread_cmd_desc = {
2560 .handler = handle_set_thread,
2561 .cmd = "H",
2562 .cmd_startswith = 1,
2563 .schema = "o.t0"
2564 };
2565 cmd_parser = &set_thread_cmd_desc;
aliguori880a7572008-11-18 20:30:24 +00002566 }
2567 break;
2568 case 'T':
Jon Doron44ffded2019-05-29 09:41:31 +03002569 {
2570 static const GdbCmdParseEntry thread_alive_cmd_desc = {
2571 .handler = handle_thread_alive,
2572 .cmd = "T",
2573 .cmd_startswith = 1,
2574 .schema = "t0"
2575 };
2576 cmd_parser = &thread_alive_cmd_desc;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07002577 }
aliguori880a7572008-11-18 20:30:24 +00002578 break;
pbrook978efd62006-06-17 18:30:42 +00002579 case 'q':
Jon Doron2704efa2019-05-29 09:41:45 +03002580 {
2581 static const GdbCmdParseEntry gen_query_cmd_desc = {
2582 .handler = handle_gen_query,
2583 .cmd = "q",
2584 .cmd_startswith = 1,
2585 .schema = "s0"
2586 };
2587 cmd_parser = &gen_query_cmd_desc;
2588 }
2589 break;
edgar_igl60897d32008-05-09 08:25:14 +00002590 case 'Q':
Jon Doron2704efa2019-05-29 09:41:45 +03002591 {
2592 static const GdbCmdParseEntry gen_set_cmd_desc = {
2593 .handler = handle_gen_set,
2594 .cmd = "Q",
2595 .cmd_startswith = 1,
2596 .schema = "s0"
2597 };
2598 cmd_parser = &gen_set_cmd_desc;
edgar_igl60897d32008-05-09 08:25:14 +00002599 }
Jon Doron2704efa2019-05-29 09:41:45 +03002600 break;
bellard858693c2004-03-31 18:52:07 +00002601 default:
bellard858693c2004-03-31 18:52:07 +00002602 /* put empty packet */
Alex Bennéea346af32020-03-16 17:21:34 +00002603 put_packet("");
bellard858693c2004-03-31 18:52:07 +00002604 break;
2605 }
Jon Doron3e2c1262019-05-29 09:41:30 +03002606
Ramiro Polla2bdec392019-08-05 21:09:01 +02002607 if (cmd_parser) {
Alex Bennéea346af32020-03-16 17:21:34 +00002608 run_cmd_parser(line_buf, cmd_parser);
Ramiro Polla2bdec392019-08-05 21:09:01 +02002609 }
Jon Doron3e2c1262019-05-29 09:41:30 +03002610
bellard858693c2004-03-31 18:52:07 +00002611 return RS_IDLE;
2612}
2613
Andreas Färber64f6b342013-05-27 02:06:09 +02002614void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00002615{
Alex Bennéea346af32020-03-16 17:21:34 +00002616 GDBProcess *p = gdb_get_cpu_process(cpu);
Luc Michel160d8582019-01-07 15:23:46 +00002617
2618 if (!p->attached) {
2619 /*
2620 * Having a stop CPU corresponding to a process that is not attached
2621 * confuses GDB. So we ignore the request.
2622 */
2623 return;
2624 }
2625
Alex Bennée8d98c442020-03-16 17:21:33 +00002626 gdbserver_state.c_cpu = cpu;
2627 gdbserver_state.g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00002628}
2629
bellard1fddef42005-04-17 19:16:13 +00002630#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002631static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00002632{
Alex Bennéea346af32020-03-16 17:21:34 +00002633 CPUState *cpu = gdbserver_state.c_cpu;
bellard858693c2004-03-31 18:52:07 +00002634 char buf[256];
Luc Michel95567c22019-01-07 15:23:46 +00002635 char thread_id[16];
aliguorid6fc1b32008-11-18 19:55:44 +00002636 const char *type;
bellard858693c2004-03-31 18:52:07 +00002637 int ret;
2638
Alex Bennéea346af32020-03-16 17:21:34 +00002639 if (running || gdbserver_state.state == RS_INACTIVE) {
Meador Ingecdb432b2012-03-15 17:49:45 +00002640 return;
2641 }
2642 /* Is there a GDB syscall waiting to be sent? */
Alex Bennéea346af32020-03-16 17:21:34 +00002643 if (gdbserver_state.current_syscall_cb) {
2644 put_packet(gdbserver_state.syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00002645 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01002646 }
Luc Michel95567c22019-01-07 15:23:46 +00002647
2648 if (cpu == NULL) {
2649 /* No process attached */
2650 return;
2651 }
2652
Alex Bennéea346af32020-03-16 17:21:34 +00002653 gdb_fmt_thread_id(cpu, thread_id, sizeof(thread_id));
Luc Michel95567c22019-01-07 15:23:46 +00002654
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002655 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002656 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02002657 if (cpu->watchpoint_hit) {
2658 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00002659 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00002660 type = "r";
2661 break;
aliguoria1d1bb32008-11-18 20:07:32 +00002662 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00002663 type = "a";
2664 break;
2665 default:
2666 type = "";
2667 break;
2668 }
Doug Gale5c9522b2017-12-02 20:30:37 -05002669 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
2670 (target_ulong)cpu->watchpoint_hit->vaddr);
aliguori880a7572008-11-18 20:30:24 +00002671 snprintf(buf, sizeof(buf),
Luc Michel95567c22019-01-07 15:23:46 +00002672 "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
2673 GDB_SIGNAL_TRAP, thread_id, type,
Andreas Färberff4700b2013-08-26 18:23:18 +02002674 (target_ulong)cpu->watchpoint_hit->vaddr);
2675 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01002676 goto send_packet;
Doug Gale5c9522b2017-12-02 20:30:37 -05002677 } else {
2678 trace_gdbstub_hit_break();
pbrook6658ffb2007-03-16 23:58:11 +00002679 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07002680 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00002681 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01002682 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002683 case RUN_STATE_PAUSED:
Doug Gale5c9522b2017-12-02 20:30:37 -05002684 trace_gdbstub_hit_paused();
aliguori9781e042009-01-22 17:15:29 +00002685 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01002686 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002687 case RUN_STATE_SHUTDOWN:
Doug Gale5c9522b2017-12-02 20:30:37 -05002688 trace_gdbstub_hit_shutdown();
Jan Kiszka425189a2011-03-22 11:02:09 +01002689 ret = GDB_SIGNAL_QUIT;
2690 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002691 case RUN_STATE_IO_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002692 trace_gdbstub_hit_io_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002693 ret = GDB_SIGNAL_IO;
2694 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002695 case RUN_STATE_WATCHDOG:
Doug Gale5c9522b2017-12-02 20:30:37 -05002696 trace_gdbstub_hit_watchdog();
Jan Kiszka425189a2011-03-22 11:02:09 +01002697 ret = GDB_SIGNAL_ALRM;
2698 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002699 case RUN_STATE_INTERNAL_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002700 trace_gdbstub_hit_internal_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002701 ret = GDB_SIGNAL_ABRT;
2702 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002703 case RUN_STATE_SAVE_VM:
2704 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01002705 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002706 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01002707 ret = GDB_SIGNAL_XCPU;
2708 break;
2709 default:
Doug Gale5c9522b2017-12-02 20:30:37 -05002710 trace_gdbstub_hit_unknown(state);
Jan Kiszka425189a2011-03-22 11:02:09 +01002711 ret = GDB_SIGNAL_UNKNOWN;
2712 break;
bellardbbeb7b52006-04-23 18:42:15 +00002713 }
Jan Kiszka226d0072015-07-24 18:52:31 +02002714 gdb_set_stop_cpu(cpu);
Luc Michel95567c22019-01-07 15:23:46 +00002715 snprintf(buf, sizeof(buf), "T%02xthread:%s;", ret, thread_id);
Jan Kiszka425189a2011-03-22 11:02:09 +01002716
2717send_packet:
Alex Bennéea346af32020-03-16 17:21:34 +00002718 put_packet(buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01002719
2720 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02002721 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00002722}
bellard1fddef42005-04-17 19:16:13 +00002723#endif
bellard858693c2004-03-31 18:52:07 +00002724
pbrooka2d1eba2007-01-28 03:10:55 +00002725/* Send a gdb syscall request.
2726 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00002727 %x - target_ulong argument printed in hex.
2728 %lx - 64-bit argument printed in hex.
2729 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01002730void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00002731{
pbrooka2d1eba2007-01-28 03:10:55 +00002732 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00002733 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00002734 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00002735 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00002736
Alex Bennéea346af32020-03-16 17:21:34 +00002737 if (!gdbserver_state.init) {
pbrooka2d1eba2007-01-28 03:10:55 +00002738 return;
Alex Bennéea346af32020-03-16 17:21:34 +00002739 }
Alex Bennée8d98c442020-03-16 17:21:33 +00002740
2741 gdbserver_state.current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00002742#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002743 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00002744#endif
Alex Bennée8d98c442020-03-16 17:21:33 +00002745 p = &gdbserver_state.syscall_buf[0];
2746 p_end = &gdbserver_state.syscall_buf[sizeof(gdbserver_state.syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00002747 *(p++) = 'F';
2748 while (*fmt) {
2749 if (*fmt == '%') {
2750 fmt++;
2751 switch (*fmt++) {
2752 case 'x':
2753 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002754 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00002755 break;
pbrooka87295e2007-05-26 15:09:38 +00002756 case 'l':
2757 if (*(fmt++) != 'x')
2758 goto bad_format;
2759 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00002760 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00002761 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002762 case 's':
2763 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002764 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00002765 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00002766 break;
2767 default:
pbrooka87295e2007-05-26 15:09:38 +00002768 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002769 error_report("gdbstub: Bad syscall format string '%s'",
2770 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00002771 break;
2772 }
2773 } else {
2774 *(p++) = *(fmt++);
2775 }
2776 }
pbrook8a93e022007-08-06 13:19:15 +00002777 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00002778#ifdef CONFIG_USER_ONLY
Alex Bennéea346af32020-03-16 17:21:34 +00002779 put_packet(gdbserver_state.syscall_buf);
Peter Maydell4f710862018-05-15 19:19:58 +01002780 /* Return control to gdb for it to process the syscall request.
2781 * Since the protocol requires that gdb hands control back to us
2782 * using a "here are the results" F packet, we don't need to check
2783 * gdb_handlesig's return value (which is the signal to deliver if
2784 * execution was resumed via a continue packet).
2785 */
Alex Bennée8d98c442020-03-16 17:21:33 +00002786 gdb_handlesig(gdbserver_state.c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00002787#else
Meador Ingecdb432b2012-03-15 17:49:45 +00002788 /* In this case wait to send the syscall packet until notification that
2789 the CPU has stopped. This must be done because if the packet is sent
2790 now the reply from the syscall request could be received while the CPU
2791 is still in the running state, which can cause packets to be dropped
2792 and state transition 'T' packets to be sent while the syscall is still
2793 being processed. */
Alex Bennée8d98c442020-03-16 17:21:33 +00002794 qemu_cpu_kick(gdbserver_state.c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00002795#endif
2796}
2797
Peter Maydell19239b32015-09-07 10:39:27 +01002798void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
2799{
2800 va_list va;
2801
2802 va_start(va, fmt);
2803 gdb_do_syscallv(cb, fmt, va);
2804 va_end(va);
2805}
2806
Alex Bennéea346af32020-03-16 17:21:34 +00002807static void gdb_read_byte(uint8_t ch)
bellard858693c2004-03-31 18:52:07 +00002808{
ths60fe76f2007-12-16 03:02:09 +00002809 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00002810
bellard1fddef42005-04-17 19:16:13 +00002811#ifndef CONFIG_USER_ONLY
Alex Bennéea346af32020-03-16 17:21:34 +00002812 if (gdbserver_state.last_packet_len) {
pbrook4046d912007-01-28 01:53:16 +00002813 /* Waiting for a response to the last packet. If we see the start
2814 of a new command then abandon the previous response. */
2815 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002816 trace_gdbstub_err_got_nack();
Alex Bennéea346af32020-03-16 17:21:34 +00002817 put_buffer((uint8_t *)gdbserver_state.last_packet, gdbserver_state.last_packet_len);
Alex Bennée118e2262017-07-12 11:52:13 +01002818 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002819 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01002820 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002821 trace_gdbstub_io_got_unexpected(ch);
pbrook4046d912007-01-28 01:53:16 +00002822 }
Alex Bennée118e2262017-07-12 11:52:13 +01002823
pbrook4046d912007-01-28 01:53:16 +00002824 if (ch == '+' || ch == '$')
Alex Bennéea346af32020-03-16 17:21:34 +00002825 gdbserver_state.last_packet_len = 0;
pbrook4046d912007-01-28 01:53:16 +00002826 if (ch != '$')
2827 return;
2828 }
Luiz Capitulino13548692011-07-29 15:36:43 -03002829 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00002830 /* when the CPU is running, we cannot do anything except stop
2831 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002832 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00002833 } else
bellard1fddef42005-04-17 19:16:13 +00002834#endif
bellard41625032005-04-24 10:07:11 +00002835 {
Alex Bennéea346af32020-03-16 17:21:34 +00002836 switch(gdbserver_state.state) {
bellard858693c2004-03-31 18:52:07 +00002837 case RS_IDLE:
2838 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04002839 /* start of command packet */
Alex Bennéea346af32020-03-16 17:21:34 +00002840 gdbserver_state.line_buf_index = 0;
2841 gdbserver_state.line_sum = 0;
2842 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002843 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002844 trace_gdbstub_err_garbage(ch);
bellard4c3a88a2003-07-26 12:06:08 +00002845 }
2846 break;
bellard858693c2004-03-31 18:52:07 +00002847 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04002848 if (ch == '}') {
2849 /* start escape sequence */
Alex Bennéea346af32020-03-16 17:21:34 +00002850 gdbserver_state.state = RS_GETLINE_ESC;
2851 gdbserver_state.line_sum += ch;
Doug Gale4bf43122017-05-01 12:22:10 -04002852 } else if (ch == '*') {
2853 /* start run length encoding sequence */
Alex Bennéea346af32020-03-16 17:21:34 +00002854 gdbserver_state.state = RS_GETLINE_RLE;
2855 gdbserver_state.line_sum += ch;
Doug Gale4bf43122017-05-01 12:22:10 -04002856 } else if (ch == '#') {
2857 /* end of command, start of checksum*/
Alex Bennéea346af32020-03-16 17:21:34 +00002858 gdbserver_state.state = RS_CHKSUM1;
2859 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002860 trace_gdbstub_err_overrun();
Alex Bennéea346af32020-03-16 17:21:34 +00002861 gdbserver_state.state = RS_IDLE;
bellard858693c2004-03-31 18:52:07 +00002862 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002863 /* unescaped command character */
Alex Bennéea346af32020-03-16 17:21:34 +00002864 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch;
2865 gdbserver_state.line_sum += ch;
Doug Gale4bf43122017-05-01 12:22:10 -04002866 }
2867 break;
2868 case RS_GETLINE_ESC:
2869 if (ch == '#') {
2870 /* unexpected end of command in escape sequence */
Alex Bennéea346af32020-03-16 17:21:34 +00002871 gdbserver_state.state = RS_CHKSUM1;
2872 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04002873 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05002874 trace_gdbstub_err_overrun();
Alex Bennéea346af32020-03-16 17:21:34 +00002875 gdbserver_state.state = RS_IDLE;
Doug Gale4bf43122017-05-01 12:22:10 -04002876 } else {
2877 /* parse escaped character and leave escape state */
Alex Bennéea346af32020-03-16 17:21:34 +00002878 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20;
2879 gdbserver_state.line_sum += ch;
2880 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002881 }
2882 break;
2883 case RS_GETLINE_RLE:
Markus Armbruster046aba12019-05-14 20:03:08 +02002884 /*
2885 * Run-length encoding is explained in "Debugging with GDB /
2886 * Appendix E GDB Remote Serial Protocol / Overview".
2887 */
2888 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
Doug Gale4bf43122017-05-01 12:22:10 -04002889 /* invalid RLE count encoding */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002890 trace_gdbstub_err_invalid_repeat(ch);
Alex Bennéea346af32020-03-16 17:21:34 +00002891 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002892 } else {
2893 /* decode repeat length */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002894 int repeat = ch - ' ' + 3;
Alex Bennéea346af32020-03-16 17:21:34 +00002895 if (gdbserver_state.line_buf_index + repeat >= sizeof(gdbserver_state.line_buf) - 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04002896 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05002897 trace_gdbstub_err_overrun();
Alex Bennéea346af32020-03-16 17:21:34 +00002898 gdbserver_state.state = RS_IDLE;
2899 } else if (gdbserver_state.line_buf_index < 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04002900 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05002901 trace_gdbstub_err_invalid_rle();
Alex Bennéea346af32020-03-16 17:21:34 +00002902 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002903 } else {
2904 /* repeat the last character */
Alex Bennéea346af32020-03-16 17:21:34 +00002905 memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index,
2906 gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat);
2907 gdbserver_state.line_buf_index += repeat;
2908 gdbserver_state.line_sum += ch;
2909 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002910 }
bellard858693c2004-03-31 18:52:07 +00002911 }
2912 break;
2913 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04002914 /* get high hex digit of checksum */
2915 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002916 trace_gdbstub_err_checksum_invalid(ch);
Alex Bennéea346af32020-03-16 17:21:34 +00002917 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002918 break;
2919 }
Alex Bennéea346af32020-03-16 17:21:34 +00002920 gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0';
2921 gdbserver_state.line_csum = fromhex(ch) << 4;
2922 gdbserver_state.state = RS_CHKSUM2;
bellard858693c2004-03-31 18:52:07 +00002923 break;
2924 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04002925 /* get low hex digit of checksum */
2926 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002927 trace_gdbstub_err_checksum_invalid(ch);
Alex Bennéea346af32020-03-16 17:21:34 +00002928 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002929 break;
bellard858693c2004-03-31 18:52:07 +00002930 }
Alex Bennéea346af32020-03-16 17:21:34 +00002931 gdbserver_state.line_csum |= fromhex(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002932
Alex Bennéea346af32020-03-16 17:21:34 +00002933 if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) {
2934 trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04002935 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00002936 reply = '-';
Alex Bennéea346af32020-03-16 17:21:34 +00002937 put_buffer(&reply, 1);
2938 gdbserver_state.state = RS_IDLE;
bellard858693c2004-03-31 18:52:07 +00002939 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002940 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00002941 reply = '+';
Alex Bennéea346af32020-03-16 17:21:34 +00002942 put_buffer(&reply, 1);
2943 gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf);
bellard858693c2004-03-31 18:52:07 +00002944 }
bellardb4608c02003-06-27 17:34:32 +00002945 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002946 default:
2947 abort();
bellardb4608c02003-06-27 17:34:32 +00002948 }
2949 }
bellard858693c2004-03-31 18:52:07 +00002950}
2951
Paul Brook0e1c9c52010-06-16 13:03:51 +01002952/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002953void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01002954{
Paul Brook0e1c9c52010-06-16 13:03:51 +01002955 char buf[4];
2956
Alex Bennée8d98c442020-03-16 17:21:33 +00002957 if (!gdbserver_state.init) {
Paul Brook0e1c9c52010-06-16 13:03:51 +01002958 return;
2959 }
2960#ifdef CONFIG_USER_ONLY
Alex Bennée8d98c442020-03-16 17:21:33 +00002961 if (gdbserver_fd < 0 || gdbserver_state.fd < 0) {
Paul Brook0e1c9c52010-06-16 13:03:51 +01002962 return;
2963 }
2964#endif
2965
Doug Gale5c9522b2017-12-02 20:30:37 -05002966 trace_gdbstub_op_exiting((uint8_t)code);
2967
Paul Brook0e1c9c52010-06-16 13:03:51 +01002968 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
Alex Bennéea346af32020-03-16 17:21:34 +00002969 put_packet(buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002970
2971#ifndef CONFIG_USER_ONLY
Alex Bennée8d98c442020-03-16 17:21:33 +00002972 qemu_chr_fe_deinit(&gdbserver_state.chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002973#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01002974}
2975
Luc Michel8f468632019-01-07 15:23:45 +00002976/*
2977 * Create the process that will contain all the "orphan" CPUs (that are not
2978 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2979 * be attachable and thus will be invisible to the user.
2980 */
2981static void create_default_process(GDBState *s)
2982{
2983 GDBProcess *process;
2984 int max_pid = 0;
2985
Alex Bennéea346af32020-03-16 17:21:34 +00002986 if (gdbserver_state.process_num) {
Luc Michel8f468632019-01-07 15:23:45 +00002987 max_pid = s->processes[s->process_num - 1].pid;
2988 }
2989
2990 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2991 process = &s->processes[s->process_num - 1];
2992
2993 /* We need an available PID slot for this process */
2994 assert(max_pid < UINT32_MAX);
2995
2996 process->pid = max_pid + 1;
2997 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00002998 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00002999}
3000
bellard1fddef42005-04-17 19:16:13 +00003001#ifdef CONFIG_USER_ONLY
3002int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02003003gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00003004{
Andreas Färber5ca666c2013-06-24 19:20:57 +02003005 char buf[256];
3006 int n;
bellard1fddef42005-04-17 19:16:13 +00003007
Alex Bennée8d98c442020-03-16 17:21:33 +00003008 if (gdbserver_fd < 0 || gdbserver_state.fd < 0) {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003009 return sig;
bellard1fddef42005-04-17 19:16:13 +00003010 }
3011
Andreas Färber5ca666c2013-06-24 19:20:57 +02003012 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02003013 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07003014 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00003015
Andreas Färber5ca666c2013-06-24 19:20:57 +02003016 if (sig != 0) {
3017 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
Alex Bennéea346af32020-03-16 17:21:34 +00003018 put_packet(buf);
Andreas Färber5ca666c2013-06-24 19:20:57 +02003019 }
3020 /* put_packet() might have detected that the peer terminated the
3021 connection. */
Alex Bennée8d98c442020-03-16 17:21:33 +00003022 if (gdbserver_state.fd < 0) {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003023 return sig;
3024 }
3025
3026 sig = 0;
Alex Bennée8d98c442020-03-16 17:21:33 +00003027 gdbserver_state.state = RS_IDLE;
3028 gdbserver_state.running_state = 0;
3029 while (gdbserver_state.running_state == 0) {
3030 n = read(gdbserver_state.fd, buf, 256);
Andreas Färber5ca666c2013-06-24 19:20:57 +02003031 if (n > 0) {
3032 int i;
3033
3034 for (i = 0; i < n; i++) {
Alex Bennéea346af32020-03-16 17:21:34 +00003035 gdb_read_byte(buf[i]);
Andreas Färber5ca666c2013-06-24 19:20:57 +02003036 }
Peter Wu5819e3e2016-06-05 16:35:48 +02003037 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003038 /* XXX: Connection closed. Should probably wait for another
3039 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02003040 if (n == 0) {
Alex Bennée8d98c442020-03-16 17:21:33 +00003041 close(gdbserver_state.fd);
Peter Wu5819e3e2016-06-05 16:35:48 +02003042 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003043 gdbserver_state.fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02003044 return sig;
bellard1fddef42005-04-17 19:16:13 +00003045 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02003046 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003047 sig = gdbserver_state.signal;
3048 gdbserver_state.signal = 0;
Andreas Färber5ca666c2013-06-24 19:20:57 +02003049 return sig;
bellard1fddef42005-04-17 19:16:13 +00003050}
bellarde9009672005-04-26 20:42:36 +00003051
aurel32ca587a82008-12-18 22:44:13 +00003052/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01003053void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00003054{
Andreas Färber5ca666c2013-06-24 19:20:57 +02003055 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00003056
Alex Bennée8d98c442020-03-16 17:21:33 +00003057 if (gdbserver_fd < 0 || gdbserver_state.fd < 0) {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003058 return;
3059 }
aurel32ca587a82008-12-18 22:44:13 +00003060
Andreas Färber5ca666c2013-06-24 19:20:57 +02003061 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
Alex Bennéea346af32020-03-16 17:21:34 +00003062 put_packet(buf);
aurel32ca587a82008-12-18 22:44:13 +00003063}
bellard1fddef42005-04-17 19:16:13 +00003064
Peter Maydell2f652222018-05-14 18:30:44 +01003065static bool gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00003066{
bellard858693c2004-03-31 18:52:07 +00003067 struct sockaddr_in sockaddr;
3068 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09003069 int fd;
bellard858693c2004-03-31 18:52:07 +00003070
3071 for(;;) {
3072 len = sizeof(sockaddr);
3073 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
3074 if (fd < 0 && errno != EINTR) {
3075 perror("accept");
Peter Maydell2f652222018-05-14 18:30:44 +01003076 return false;
bellard858693c2004-03-31 18:52:07 +00003077 } else if (fd >= 0) {
Peter Maydellf5bdd782018-05-14 18:30:43 +01003078 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00003079 break;
3080 }
3081 }
3082
3083 /* set short latency */
Peter Maydell2f652222018-05-14 18:30:44 +01003084 if (socket_set_nodelay(fd)) {
3085 perror("setsockopt");
Philippe Mathieu-Daudéead75d82018-05-24 19:34:58 -03003086 close(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01003087 return false;
3088 }
ths3b46e622007-09-17 08:09:54 +00003089
Alex Bennée8d98c442020-03-16 17:21:33 +00003090 init_gdbserver_state();
3091 create_default_process(&gdbserver_state);
3092 gdbserver_state.processes[0].attached = true;
Alex Bennéea346af32020-03-16 17:21:34 +00003093 gdbserver_state.c_cpu = gdb_first_attached_cpu();
Alex Bennée8d98c442020-03-16 17:21:33 +00003094 gdbserver_state.g_cpu = gdbserver_state.c_cpu;
3095 gdbserver_state.fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02003096 gdb_has_xml = false;
Peter Maydell2f652222018-05-14 18:30:44 +01003097 return true;
bellard858693c2004-03-31 18:52:07 +00003098}
3099
3100static int gdbserver_open(int port)
3101{
3102 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02003103 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00003104
3105 fd = socket(PF_INET, SOCK_STREAM, 0);
3106 if (fd < 0) {
3107 perror("socket");
3108 return -1;
3109 }
Peter Maydellf5bdd782018-05-14 18:30:43 +01003110 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00003111
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02003112 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00003113
3114 sockaddr.sin_family = AF_INET;
3115 sockaddr.sin_port = htons(port);
3116 sockaddr.sin_addr.s_addr = 0;
3117 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
3118 if (ret < 0) {
3119 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00003120 close(fd);
bellard858693c2004-03-31 18:52:07 +00003121 return -1;
3122 }
Peter Wu96165b92016-05-04 11:32:17 +02003123 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00003124 if (ret < 0) {
3125 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00003126 close(fd);
bellard858693c2004-03-31 18:52:07 +00003127 return -1;
3128 }
bellard858693c2004-03-31 18:52:07 +00003129 return fd;
3130}
3131
3132int gdbserver_start(int port)
3133{
3134 gdbserver_fd = gdbserver_open(port);
3135 if (gdbserver_fd < 0)
3136 return -1;
3137 /* accept connections */
Peter Maydell2f652222018-05-14 18:30:44 +01003138 if (!gdb_accept()) {
3139 close(gdbserver_fd);
3140 gdbserver_fd = -1;
3141 return -1;
3142 }
bellardb4608c02003-06-27 17:34:32 +00003143 return 0;
3144}
aurel322b1319c2008-12-18 22:44:04 +00003145
3146/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07003147void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00003148{
Alex Bennée8d98c442020-03-16 17:21:33 +00003149 if (gdbserver_fd < 0 || gdbserver_state.fd < 0) {
Andreas Färber75a34032013-09-02 16:57:02 +02003150 return;
3151 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003152 close(gdbserver_state.fd);
3153 gdbserver_state.fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02003154 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02003155 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00003156}
pbrook4046d912007-01-28 01:53:16 +00003157#else
thsaa1f17c2007-07-11 22:48:58 +00003158static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00003159{
pbrook56aebc82008-10-11 17:55:29 +00003160 /* We can handle an arbitrarily large amount of data.
3161 Pick the maximum packet size, which is as good as anything. */
3162 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00003163}
3164
thsaa1f17c2007-07-11 22:48:58 +00003165static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00003166{
pbrook4046d912007-01-28 01:53:16 +00003167 int i;
3168
3169 for (i = 0; i < size; i++) {
Alex Bennéea346af32020-03-16 17:21:34 +00003170 gdb_read_byte(buf[i]);
pbrook4046d912007-01-28 01:53:16 +00003171 }
3172}
3173
Philippe Mathieu-Daudé083b2662019-12-18 18:20:09 +01003174static void gdb_chr_event(void *opaque, QEMUChrEvent event)
pbrook4046d912007-01-28 01:53:16 +00003175{
Luc Michel970ed902019-01-07 15:23:46 +00003176 int i;
3177 GDBState *s = (GDBState *) opaque;
3178
pbrook4046d912007-01-28 01:53:16 +00003179 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05303180 case CHR_EVENT_OPENED:
Luc Michel970ed902019-01-07 15:23:46 +00003181 /* Start with first process attached, others detached */
3182 for (i = 0; i < s->process_num; i++) {
3183 s->processes[i].attached = !i;
3184 }
3185
Alex Bennéea346af32020-03-16 17:21:34 +00003186 s->c_cpu = gdb_first_attached_cpu();
Luc Michel970ed902019-01-07 15:23:46 +00003187 s->g_cpu = s->c_cpu;
3188
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003189 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02003190 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00003191 break;
3192 default:
3193 break;
3194 }
3195}
3196
Alex Bennéea346af32020-03-16 17:21:34 +00003197static void gdb_monitor_output(const char *msg, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00003198{
3199 char buf[MAX_PACKET_LENGTH];
3200
3201 buf[0] = 'O';
3202 if (len > (MAX_PACKET_LENGTH/2) - 1)
3203 len = (MAX_PACKET_LENGTH/2) - 1;
3204 memtohex(buf + 1, (uint8_t *)msg, len);
Alex Bennéea346af32020-03-16 17:21:34 +00003205 put_packet(buf);
aliguori8a34a0f2009-03-05 23:01:55 +00003206}
3207
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003208static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00003209{
3210 const char *p = (const char *)buf;
3211 int max_sz;
3212
Alex Bennée8d98c442020-03-16 17:21:33 +00003213 max_sz = (sizeof(gdbserver_state.last_packet) - 2) / 2;
aliguori8a34a0f2009-03-05 23:01:55 +00003214 for (;;) {
3215 if (len <= max_sz) {
Alex Bennéea346af32020-03-16 17:21:34 +00003216 gdb_monitor_output(p, len);
aliguori8a34a0f2009-03-05 23:01:55 +00003217 break;
3218 }
Alex Bennéea346af32020-03-16 17:21:34 +00003219 gdb_monitor_output(p, max_sz);
aliguori8a34a0f2009-03-05 23:01:55 +00003220 p += max_sz;
3221 len -= max_sz;
3222 }
3223 return len;
3224}
3225
aliguori59030a82009-04-05 18:43:41 +00003226#ifndef _WIN32
3227static void gdb_sigterm_handler(int signal)
3228{
Luiz Capitulino13548692011-07-29 15:36:43 -03003229 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003230 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01003231 }
aliguori59030a82009-04-05 18:43:41 +00003232}
3233#endif
3234
Marc-André Lureau777357d2016-12-07 18:39:10 +03003235static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
3236 bool *be_opened, Error **errp)
3237{
3238 *be_opened = false;
3239}
3240
3241static void char_gdb_class_init(ObjectClass *oc, void *data)
3242{
3243 ChardevClass *cc = CHARDEV_CLASS(oc);
3244
3245 cc->internal = true;
3246 cc->open = gdb_monitor_open;
3247 cc->chr_write = gdb_monitor_write;
3248}
3249
3250#define TYPE_CHARDEV_GDB "chardev-gdb"
3251
3252static const TypeInfo char_gdb_type_info = {
3253 .name = TYPE_CHARDEV_GDB,
3254 .parent = TYPE_CHARDEV,
3255 .class_init = char_gdb_class_init,
3256};
3257
Luc Michel8f468632019-01-07 15:23:45 +00003258static int find_cpu_clusters(Object *child, void *opaque)
3259{
3260 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
3261 GDBState *s = (GDBState *) opaque;
3262 CPUClusterState *cluster = CPU_CLUSTER(child);
3263 GDBProcess *process;
3264
3265 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
3266
3267 process = &s->processes[s->process_num - 1];
3268
3269 /*
3270 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
3271 * runtime, we enforce here that the machine does not use a cluster ID
3272 * that would lead to PID 0.
3273 */
3274 assert(cluster->cluster_id != UINT32_MAX);
3275 process->pid = cluster->cluster_id + 1;
3276 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00003277 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00003278
3279 return 0;
3280 }
3281
3282 return object_child_foreach(child, find_cpu_clusters, opaque);
3283}
3284
3285static int pid_order(const void *a, const void *b)
3286{
3287 GDBProcess *pa = (GDBProcess *) a;
3288 GDBProcess *pb = (GDBProcess *) b;
3289
3290 if (pa->pid < pb->pid) {
3291 return -1;
3292 } else if (pa->pid > pb->pid) {
3293 return 1;
3294 } else {
3295 return 0;
3296 }
3297}
3298
3299static void create_processes(GDBState *s)
3300{
3301 object_child_foreach(object_get_root(), find_cpu_clusters, s);
3302
Alex Bennéea346af32020-03-16 17:21:34 +00003303 if (gdbserver_state.processes) {
Luc Michel8f468632019-01-07 15:23:45 +00003304 /* Sort by PID */
Alex Bennéea346af32020-03-16 17:21:34 +00003305 qsort(gdbserver_state.processes, gdbserver_state.process_num, sizeof(gdbserver_state.processes[0]), pid_order);
Luc Michel8f468632019-01-07 15:23:45 +00003306 }
3307
3308 create_default_process(s);
3309}
3310
aliguori59030a82009-04-05 18:43:41 +00003311int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00003312{
Doug Gale5c9522b2017-12-02 20:30:37 -05003313 trace_gdbstub_op_start(device);
3314
aliguori59030a82009-04-05 18:43:41 +00003315 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003316 Chardev *chr = NULL;
3317 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00003318
Ziyue Yang508b4ec2017-01-18 16:02:41 +08003319 if (!first_cpu) {
3320 error_report("gdbstub: meaningless to attach gdb to a "
3321 "machine without any CPU.");
3322 return -1;
3323 }
3324
aliguori59030a82009-04-05 18:43:41 +00003325 if (!device)
3326 return -1;
3327 if (strcmp(device, "none") != 0) {
3328 if (strstart(device, "tcp:", NULL)) {
3329 /* enforce required TCP attributes */
3330 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
3331 "%s,nowait,nodelay,server", device);
3332 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00003333 }
aliguori59030a82009-04-05 18:43:41 +00003334#ifndef _WIN32
3335 else if (strcmp(device, "stdio") == 0) {
3336 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00003337
aliguori59030a82009-04-05 18:43:41 +00003338 memset(&act, 0, sizeof(act));
3339 act.sa_handler = gdb_sigterm_handler;
3340 sigaction(SIGINT, &act, NULL);
3341 }
3342#endif
Marc-André Lureau95e30b22018-08-22 19:19:42 +02003343 /*
3344 * FIXME: it's a bit weird to allow using a mux chardev here
3345 * and implicitly setup a monitor. We may want to break this.
3346 */
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01003347 chr = qemu_chr_new_noreplay("gdb", device, true, NULL);
aliguori36556b22009-03-28 18:05:53 +00003348 if (!chr)
3349 return -1;
pbrookcfc34752007-02-22 01:48:01 +00003350 }
3351
Alex Bennée8d98c442020-03-16 17:21:33 +00003352 if (!gdbserver_state.init) {
3353 init_gdbserver_state();
pbrook4046d912007-01-28 01:53:16 +00003354
aliguori36556b22009-03-28 18:05:53 +00003355 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
3356
3357 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03003358 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01003359 NULL, NULL, &error_abort);
Kevin Wolf8e9119a2020-02-24 15:30:06 +01003360 monitor_init_hmp(mon_chr, false, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00003361 } else {
Alex Bennée8d98c442020-03-16 17:21:33 +00003362 qemu_chr_fe_deinit(&gdbserver_state.chr, true);
3363 mon_chr = gdbserver_state.mon_chr;
3364 reset_gdbserver_state();
aliguori36556b22009-03-28 18:05:53 +00003365 }
Luc Michel8f468632019-01-07 15:23:45 +00003366
Alex Bennée8d98c442020-03-16 17:21:33 +00003367 create_processes(&gdbserver_state);
Luc Michel8f468632019-01-07 15:23:45 +00003368
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03003369 if (chr) {
Alex Bennée8d98c442020-03-16 17:21:33 +00003370 qemu_chr_fe_init(&gdbserver_state.chr, chr, &error_abort);
3371 qemu_chr_fe_set_handlers(&gdbserver_state.chr, gdb_chr_can_receive,
3372 gdb_chr_receive, gdb_chr_event,
3373 NULL, &gdbserver_state, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03003374 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003375 gdbserver_state.state = chr ? RS_IDLE : RS_INACTIVE;
3376 gdbserver_state.mon_chr = mon_chr;
3377 gdbserver_state.current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00003378
pbrook4046d912007-01-28 01:53:16 +00003379 return 0;
3380}
Marc-André Lureau777357d2016-12-07 18:39:10 +03003381
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01003382void gdbserver_cleanup(void)
3383{
Alex Bennée8d98c442020-03-16 17:21:33 +00003384 if (gdbserver_state.init) {
Alex Bennéea346af32020-03-16 17:21:34 +00003385 put_packet("W00");
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01003386 }
3387}
3388
Marc-André Lureau777357d2016-12-07 18:39:10 +03003389static void register_types(void)
3390{
3391 type_register_static(&char_gdb_type_info);
3392}
3393
3394type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00003395#endif