blob: 171e150950902113658f36b6b933fcc9850747db [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;
Alex Bennéea010bdb2020-03-16 17:21:41 +0000322 gdb_get_reg_cb get_reg;
323 gdb_set_reg_cb set_reg;
pbrook56aebc82008-10-11 17:55:29 +0000324 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 */
Damien Hedded116e812020-03-16 17:21:53 +0000354 GByteArray *last_packet;
edgar_igl1f487ee2008-05-17 22:20:53 +0000355 int signal;
bellard41625032005-04-24 10:07:11 +0000356#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000357 int fd;
bellard41625032005-04-24 10:07:11 +0000358 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000359#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300360 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300361 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000362#endif
Luc Michel8f468632019-01-07 15:23:45 +0000363 bool multiprocess;
364 GDBProcess *processes;
365 int process_num;
Meador Ingecdb432b2012-03-15 17:49:45 +0000366 char syscall_buf[256];
367 gdb_syscall_complete_cb current_syscall_cb;
Alex Bennée308f9e82020-03-16 17:21:35 +0000368 GString *str_buf;
Alex Bennée4a25f1b2020-03-16 17:21:36 +0000369 GByteArray *mem_buf;
bellard858693c2004-03-31 18:52:07 +0000370} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000371
edgar_igl60897d32008-05-09 08:25:14 +0000372/* By default use no IRQs and no timers while single stepping so as to
373 * make single stepping like an ICE HW step.
374 */
375static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
376
Alex Bennée8d98c442020-03-16 17:21:33 +0000377static GDBState gdbserver_state;
378
379static void init_gdbserver_state(void)
380{
381 g_assert(!gdbserver_state.init);
382 memset(&gdbserver_state, 0, sizeof(GDBState));
383 gdbserver_state.init = true;
Alex Bennée308f9e82020-03-16 17:21:35 +0000384 gdbserver_state.str_buf = g_string_new(NULL);
Alex Bennée4a25f1b2020-03-16 17:21:36 +0000385 gdbserver_state.mem_buf = g_byte_array_sized_new(MAX_PACKET_LENGTH);
Damien Hedded116e812020-03-16 17:21:53 +0000386 gdbserver_state.last_packet = g_byte_array_sized_new(MAX_PACKET_LENGTH + 4);
Alex Bennée8d98c442020-03-16 17:21:33 +0000387}
388
389#ifndef CONFIG_USER_ONLY
390static void reset_gdbserver_state(void)
391{
392 g_free(gdbserver_state.processes);
393 gdbserver_state.processes = NULL;
394 gdbserver_state.process_num = 0;
395}
396#endif
aliguori880a7572008-11-18 20:30:24 +0000397
Andreas Färber5b50e792013-06-29 04:18:45 +0200398bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000399
bellard1fddef42005-04-17 19:16:13 +0000400#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000401/* XXX: This is not thread safe. Do we care? */
402static int gdbserver_fd = -1;
403
Alex Bennéea346af32020-03-16 17:21:34 +0000404static int get_char(void)
bellardb4608c02003-06-27 17:34:32 +0000405{
406 uint8_t ch;
407 int ret;
408
409 for(;;) {
Alex Bennéea346af32020-03-16 17:21:34 +0000410 ret = qemu_recv(gdbserver_state.fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000411 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000412 if (errno == ECONNRESET)
Alex Bennéea346af32020-03-16 17:21:34 +0000413 gdbserver_state.fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200414 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000415 return -1;
416 } else if (ret == 0) {
Alex Bennéea346af32020-03-16 17:21:34 +0000417 close(gdbserver_state.fd);
418 gdbserver_state.fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000419 return -1;
420 } else {
421 break;
422 }
423 }
424 return ch;
425}
pbrook4046d912007-01-28 01:53:16 +0000426#endif
bellardb4608c02003-06-27 17:34:32 +0000427
blueswir1654efcf2009-04-18 07:29:59 +0000428static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000429 GDB_SYS_UNKNOWN,
430 GDB_SYS_ENABLED,
431 GDB_SYS_DISABLED,
432} gdb_syscall_mode;
433
Liviu Ionescua38bb072014-12-11 12:07:48 +0000434/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000435int use_gdb_syscalls(void)
436{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100437 SemihostingTarget target = semihosting_get_target();
438 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000439 /* -semihosting-config target=native */
440 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100441 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000442 /* -semihosting-config target=gdb */
443 return true;
444 }
445
446 /* -semihosting-config target=auto */
447 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000448 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
Alex Bennée8d98c442020-03-16 17:21:33 +0000449 gdb_syscall_mode = gdbserver_state.init ?
450 GDB_SYS_ENABLED : GDB_SYS_DISABLED;
pbrooka2d1eba2007-01-28 03:10:55 +0000451 }
452 return gdb_syscall_mode == GDB_SYS_ENABLED;
453}
454
edgar_iglba70a622008-03-14 06:10:42 +0000455/* Resume execution. */
Alex Bennéea346af32020-03-16 17:21:34 +0000456static inline void gdb_continue(void)
edgar_iglba70a622008-03-14 06:10:42 +0000457{
Doug Gale5c9522b2017-12-02 20:30:37 -0500458
edgar_iglba70a622008-03-14 06:10:42 +0000459#ifdef CONFIG_USER_ONLY
Alex Bennéea346af32020-03-16 17:21:34 +0000460 gdbserver_state.running_state = 1;
Doug Gale5c9522b2017-12-02 20:30:37 -0500461 trace_gdbstub_op_continue();
edgar_iglba70a622008-03-14 06:10:42 +0000462#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200463 if (!runstate_needs_reset()) {
Doug Gale5c9522b2017-12-02 20:30:37 -0500464 trace_gdbstub_op_continue();
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200465 vm_start();
466 }
edgar_iglba70a622008-03-14 06:10:42 +0000467#endif
468}
469
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100470/*
471 * Resume execution, per CPU actions. For user-mode emulation it's
472 * equivalent to gdb_continue.
473 */
Alex Bennéea346af32020-03-16 17:21:34 +0000474static int gdb_continue_partial(char *newstates)
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100475{
476 CPUState *cpu;
477 int res = 0;
478#ifdef CONFIG_USER_ONLY
479 /*
480 * This is not exactly accurate, but it's an improvement compared to the
481 * previous situation, where only one CPU would be single-stepped.
482 */
483 CPU_FOREACH(cpu) {
484 if (newstates[cpu->cpu_index] == 's') {
Doug Gale5c9522b2017-12-02 20:30:37 -0500485 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100486 cpu_single_step(cpu, sstep_flags);
487 }
488 }
Alex Bennéea346af32020-03-16 17:21:34 +0000489 gdbserver_state.running_state = 1;
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100490#else
491 int flag = 0;
492
493 if (!runstate_needs_reset()) {
494 if (vm_prepare_start()) {
495 return 0;
496 }
497
498 CPU_FOREACH(cpu) {
499 switch (newstates[cpu->cpu_index]) {
500 case 0:
501 case 1:
502 break; /* nothing to do here */
503 case 's':
Doug Gale5c9522b2017-12-02 20:30:37 -0500504 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100505 cpu_single_step(cpu, sstep_flags);
506 cpu_resume(cpu);
507 flag = 1;
508 break;
509 case 'c':
Doug Gale5c9522b2017-12-02 20:30:37 -0500510 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100511 cpu_resume(cpu);
512 flag = 1;
513 break;
514 default:
515 res = -1;
516 break;
517 }
518 }
519 }
520 if (flag) {
521 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
522 }
523#endif
524 return res;
525}
526
Alex Bennéea346af32020-03-16 17:21:34 +0000527static void put_buffer(const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000528{
pbrook4046d912007-01-28 01:53:16 +0000529#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000530 int ret;
531
532 while (len > 0) {
Alex Bennéea346af32020-03-16 17:21:34 +0000533 ret = send(gdbserver_state.fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000534 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200535 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000536 return;
537 } else {
538 buf += ret;
539 len -= ret;
540 }
541 }
pbrook4046d912007-01-28 01:53:16 +0000542#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100543 /* XXX this blocks entire thread. Rewrite to use
544 * qemu_chr_fe_write and background I/O callbacks */
Alex Bennéea346af32020-03-16 17:21:34 +0000545 qemu_chr_fe_write_all(&gdbserver_state.chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000546#endif
bellardb4608c02003-06-27 17:34:32 +0000547}
548
549static inline int fromhex(int v)
550{
551 if (v >= '0' && v <= '9')
552 return v - '0';
553 else if (v >= 'A' && v <= 'F')
554 return v - 'A' + 10;
555 else if (v >= 'a' && v <= 'f')
556 return v - 'a' + 10;
557 else
558 return 0;
559}
560
561static inline int tohex(int v)
562{
563 if (v < 10)
564 return v + '0';
565 else
566 return v - 10 + 'a';
567}
568
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -0300569/* writes 2*len+1 bytes in buf */
Alex Bennée308f9e82020-03-16 17:21:35 +0000570static void memtohex(GString *buf, const uint8_t *mem, int len)
bellardb4608c02003-06-27 17:34:32 +0000571{
572 int i, c;
bellardb4608c02003-06-27 17:34:32 +0000573 for(i = 0; i < len; i++) {
574 c = mem[i];
Alex Bennée308f9e82020-03-16 17:21:35 +0000575 g_string_append_c(buf, tohex(c >> 4));
576 g_string_append_c(buf, tohex(c & 0xf));
bellardb4608c02003-06-27 17:34:32 +0000577 }
Alex Bennée308f9e82020-03-16 17:21:35 +0000578 g_string_append_c(buf, '\0');
bellardb4608c02003-06-27 17:34:32 +0000579}
580
Alex Bennée4a25f1b2020-03-16 17:21:36 +0000581static void hextomem(GByteArray *mem, const char *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000582{
583 int i;
584
585 for(i = 0; i < len; i++) {
Alex Bennée4a25f1b2020-03-16 17:21:36 +0000586 guint8 byte = fromhex(buf[0]) << 4 | fromhex(buf[1]);
587 g_byte_array_append(mem, &byte, 1);
bellardb4608c02003-06-27 17:34:32 +0000588 buf += 2;
589 }
590}
591
Doug Gale5c9522b2017-12-02 20:30:37 -0500592static void hexdump(const char *buf, int len,
593 void (*trace_fn)(size_t ofs, char const *text))
594{
595 char line_buffer[3 * 16 + 4 + 16 + 1];
596
597 size_t i;
598 for (i = 0; i < len || (i & 0xF); ++i) {
599 size_t byte_ofs = i & 15;
600
601 if (byte_ofs == 0) {
602 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
603 line_buffer[3 * 16 + 4 + 16] = 0;
604 }
605
606 size_t col_group = (i >> 2) & 3;
607 size_t hex_col = byte_ofs * 3 + col_group;
608 size_t txt_col = 3 * 16 + 4 + byte_ofs;
609
610 if (i < len) {
611 char value = buf[i];
612
613 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
614 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
615 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
616 ? value
617 : '.';
618 }
619
620 if (byte_ofs == 0xF)
621 trace_fn(i & -16, line_buffer);
622 }
623}
624
bellardb4608c02003-06-27 17:34:32 +0000625/* return -1 if error, 0 if OK */
Alex Bennéea346af32020-03-16 17:21:34 +0000626static int put_packet_binary(const char *buf, int len, bool dump)
bellardb4608c02003-06-27 17:34:32 +0000627{
pbrook56aebc82008-10-11 17:55:29 +0000628 int csum, i;
Damien Hedded116e812020-03-16 17:21:53 +0000629 uint8_t footer[3];
bellardb4608c02003-06-27 17:34:32 +0000630
Doug Gale5c9522b2017-12-02 20:30:37 -0500631 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
632 hexdump(buf, len, trace_gdbstub_io_binaryreply);
633 }
634
bellardb4608c02003-06-27 17:34:32 +0000635 for(;;) {
Damien Hedded116e812020-03-16 17:21:53 +0000636 g_byte_array_set_size(gdbserver_state.last_packet, 0);
637 g_byte_array_append(gdbserver_state.last_packet,
638 (const uint8_t *) "$", 1);
639 g_byte_array_append(gdbserver_state.last_packet,
640 (const uint8_t *) buf, len);
bellardb4608c02003-06-27 17:34:32 +0000641 csum = 0;
642 for(i = 0; i < len; i++) {
643 csum += buf[i];
644 }
Damien Hedded116e812020-03-16 17:21:53 +0000645 footer[0] = '#';
646 footer[1] = tohex((csum >> 4) & 0xf);
647 footer[2] = tohex((csum) & 0xf);
648 g_byte_array_append(gdbserver_state.last_packet, footer, 3);
bellardb4608c02003-06-27 17:34:32 +0000649
Damien Hedded116e812020-03-16 17:21:53 +0000650 put_buffer(gdbserver_state.last_packet->data,
651 gdbserver_state.last_packet->len);
bellardb4608c02003-06-27 17:34:32 +0000652
pbrook4046d912007-01-28 01:53:16 +0000653#ifdef CONFIG_USER_ONLY
Alex Bennéea346af32020-03-16 17:21:34 +0000654 i = get_char();
pbrook4046d912007-01-28 01:53:16 +0000655 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000656 return -1;
pbrook4046d912007-01-28 01:53:16 +0000657 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000658 break;
pbrook4046d912007-01-28 01:53:16 +0000659#else
660 break;
661#endif
bellardb4608c02003-06-27 17:34:32 +0000662 }
663 return 0;
664}
665
pbrook56aebc82008-10-11 17:55:29 +0000666/* return -1 if error, 0 if OK */
Alex Bennéea346af32020-03-16 17:21:34 +0000667static int put_packet(const char *buf)
pbrook56aebc82008-10-11 17:55:29 +0000668{
Doug Gale5c9522b2017-12-02 20:30:37 -0500669 trace_gdbstub_io_reply(buf);
pbrook56aebc82008-10-11 17:55:29 +0000670
Alex Bennéea346af32020-03-16 17:21:34 +0000671 return put_packet_binary(buf, strlen(buf), false);
pbrook56aebc82008-10-11 17:55:29 +0000672}
673
Alex Bennée308f9e82020-03-16 17:21:35 +0000674static void put_strbuf(void)
pbrook56aebc82008-10-11 17:55:29 +0000675{
Alex Bennée308f9e82020-03-16 17:21:35 +0000676 put_packet(gdbserver_state.str_buf->str);
677}
678
679/* Encode data using the encoding for 'x' packets. */
680static void memtox(GString *buf, const char *mem, int len)
681{
pbrook56aebc82008-10-11 17:55:29 +0000682 char c;
683
684 while (len--) {
685 c = *(mem++);
686 switch (c) {
687 case '#': case '$': case '*': case '}':
Alex Bennée308f9e82020-03-16 17:21:35 +0000688 g_string_append_c(buf, '}');
689 g_string_append_c(buf, c ^ 0x20);
pbrook56aebc82008-10-11 17:55:29 +0000690 break;
691 default:
Alex Bennée308f9e82020-03-16 17:21:35 +0000692 g_string_append_c(buf, c);
pbrook56aebc82008-10-11 17:55:29 +0000693 break;
694 }
695 }
pbrook56aebc82008-10-11 17:55:29 +0000696}
697
Alex Bennéea346af32020-03-16 17:21:34 +0000698static uint32_t gdb_get_cpu_pid(CPUState *cpu)
Luc Michel1a227332019-01-07 15:23:45 +0000699{
Luc Michel1a227332019-01-07 15:23:45 +0000700 /* TODO: In user mode, we should use the task state PID */
Peter Maydell46f5abc2019-01-29 11:46:06 +0000701 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
702 /* Return the default process' PID */
Alex Bennéea346af32020-03-16 17:21:34 +0000703 int index = gdbserver_state.process_num - 1;
704 return gdbserver_state.processes[index].pid;
Peter Maydell46f5abc2019-01-29 11:46:06 +0000705 }
706 return cpu->cluster_index + 1;
Luc Michel1a227332019-01-07 15:23:45 +0000707}
708
Alex Bennéea346af32020-03-16 17:21:34 +0000709static GDBProcess *gdb_get_process(uint32_t pid)
Luc Michel7d8c87d2019-01-07 15:23:45 +0000710{
711 int i;
712
713 if (!pid) {
714 /* 0 means any process, we take the first one */
Alex Bennéea346af32020-03-16 17:21:34 +0000715 return &gdbserver_state.processes[0];
Luc Michel7d8c87d2019-01-07 15:23:45 +0000716 }
717
Alex Bennéea346af32020-03-16 17:21:34 +0000718 for (i = 0; i < gdbserver_state.process_num; i++) {
719 if (gdbserver_state.processes[i].pid == pid) {
720 return &gdbserver_state.processes[i];
Luc Michel7d8c87d2019-01-07 15:23:45 +0000721 }
722 }
723
724 return NULL;
725}
726
Alex Bennéea346af32020-03-16 17:21:34 +0000727static GDBProcess *gdb_get_cpu_process(CPUState *cpu)
Luc Michel7d8c87d2019-01-07 15:23:45 +0000728{
Alex Bennéea346af32020-03-16 17:21:34 +0000729 return gdb_get_process(gdb_get_cpu_pid(cpu));
Luc Michel7d8c87d2019-01-07 15:23:45 +0000730}
731
732static CPUState *find_cpu(uint32_t thread_id)
733{
734 CPUState *cpu;
735
736 CPU_FOREACH(cpu) {
737 if (cpu_gdb_index(cpu) == thread_id) {
738 return cpu;
739 }
740 }
741
742 return NULL;
743}
744
Alex Bennéea346af32020-03-16 17:21:34 +0000745static CPUState *get_first_cpu_in_process(GDBProcess *process)
Luc Michele40e5202019-01-07 15:23:46 +0000746{
747 CPUState *cpu;
748
749 CPU_FOREACH(cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +0000750 if (gdb_get_cpu_pid(cpu) == process->pid) {
Luc Michele40e5202019-01-07 15:23:46 +0000751 return cpu;
752 }
753 }
754
755 return NULL;
756}
757
Alex Bennéea346af32020-03-16 17:21:34 +0000758static CPUState *gdb_next_cpu_in_process(CPUState *cpu)
Luc Michele40e5202019-01-07 15:23:46 +0000759{
Alex Bennéea346af32020-03-16 17:21:34 +0000760 uint32_t pid = gdb_get_cpu_pid(cpu);
Luc Michele40e5202019-01-07 15:23:46 +0000761 cpu = CPU_NEXT(cpu);
762
763 while (cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +0000764 if (gdb_get_cpu_pid(cpu) == pid) {
Luc Michele40e5202019-01-07 15:23:46 +0000765 break;
766 }
767
768 cpu = CPU_NEXT(cpu);
769 }
770
771 return cpu;
772}
773
Luc Michele40e5202019-01-07 15:23:46 +0000774/* Return the cpu following @cpu, while ignoring unattached processes. */
Alex Bennéea346af32020-03-16 17:21:34 +0000775static CPUState *gdb_next_attached_cpu(CPUState *cpu)
Luc Michele40e5202019-01-07 15:23:46 +0000776{
777 cpu = CPU_NEXT(cpu);
778
779 while (cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +0000780 if (gdb_get_cpu_process(cpu)->attached) {
Luc Michele40e5202019-01-07 15:23:46 +0000781 break;
782 }
783
784 cpu = CPU_NEXT(cpu);
785 }
786
787 return cpu;
788}
789
790/* Return the first attached cpu */
Alex Bennéea346af32020-03-16 17:21:34 +0000791static CPUState *gdb_first_attached_cpu(void)
Luc Michele40e5202019-01-07 15:23:46 +0000792{
793 CPUState *cpu = first_cpu;
Alex Bennéea346af32020-03-16 17:21:34 +0000794 GDBProcess *process = gdb_get_cpu_process(cpu);
Luc Michele40e5202019-01-07 15:23:46 +0000795
796 if (!process->attached) {
Alex Bennéea346af32020-03-16 17:21:34 +0000797 return gdb_next_attached_cpu(cpu);
Luc Michele40e5202019-01-07 15:23:46 +0000798 }
799
800 return cpu;
801}
802
Alex Bennéea346af32020-03-16 17:21:34 +0000803static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid)
Luc Michelab65eed2019-01-29 11:46:03 +0000804{
805 GDBProcess *process;
806 CPUState *cpu;
807
808 if (!pid && !tid) {
809 /* 0 means any process/thread, we take the first attached one */
Alex Bennéea346af32020-03-16 17:21:34 +0000810 return gdb_first_attached_cpu();
Luc Michelab65eed2019-01-29 11:46:03 +0000811 } else if (pid && !tid) {
812 /* any thread in a specific process */
Alex Bennéea346af32020-03-16 17:21:34 +0000813 process = gdb_get_process(pid);
Luc Michelab65eed2019-01-29 11:46:03 +0000814
815 if (process == NULL) {
816 return NULL;
817 }
818
819 if (!process->attached) {
820 return NULL;
821 }
822
Alex Bennéea346af32020-03-16 17:21:34 +0000823 return get_first_cpu_in_process(process);
Luc Michelab65eed2019-01-29 11:46:03 +0000824 } else {
825 /* a specific thread */
826 cpu = find_cpu(tid);
827
828 if (cpu == NULL) {
829 return NULL;
830 }
831
Alex Bennéea346af32020-03-16 17:21:34 +0000832 process = gdb_get_cpu_process(cpu);
Luc Michelab65eed2019-01-29 11:46:03 +0000833
834 if (pid && process->pid != pid) {
835 return NULL;
836 }
837
838 if (!process->attached) {
839 return NULL;
840 }
841
842 return cpu;
843 }
844}
845
Alex Bennéea346af32020-03-16 17:21:34 +0000846static const char *get_feature_xml(const char *p, const char **newp,
847 GDBProcess *process)
pbrook56aebc82008-10-11 17:55:29 +0000848{
pbrook56aebc82008-10-11 17:55:29 +0000849 size_t len;
850 int i;
851 const char *name;
Alex Bennéea346af32020-03-16 17:21:34 +0000852 CPUState *cpu = get_first_cpu_in_process(process);
Luc Michelc145eea2019-01-07 15:23:46 +0000853 CPUClass *cc = CPU_GET_CLASS(cpu);
pbrook56aebc82008-10-11 17:55:29 +0000854
855 len = 0;
856 while (p[len] && p[len] != ':')
857 len++;
858 *newp = p + len;
859
860 name = NULL;
861 if (strncmp(p, "target.xml", len) == 0) {
Luc Michelc145eea2019-01-07 15:23:46 +0000862 char *buf = process->target_xml;
863 const size_t buf_sz = sizeof(process->target_xml);
pbrook56aebc82008-10-11 17:55:29 +0000864
Luc Michelc145eea2019-01-07 15:23:46 +0000865 /* Generate the XML description for this CPU. */
866 if (!buf[0]) {
867 GDBRegisterState *r;
868
869 pstrcat(buf, buf_sz,
David Hildenbrandb3820e62015-12-03 13:14:41 +0100870 "<?xml version=\"1.0\"?>"
871 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
872 "<target>");
873 if (cc->gdb_arch_name) {
874 gchar *arch = cc->gdb_arch_name(cpu);
Luc Michelc145eea2019-01-07 15:23:46 +0000875 pstrcat(buf, buf_sz, "<architecture>");
876 pstrcat(buf, buf_sz, arch);
877 pstrcat(buf, buf_sz, "</architecture>");
David Hildenbrandb3820e62015-12-03 13:14:41 +0100878 g_free(arch);
879 }
Luc Michelc145eea2019-01-07 15:23:46 +0000880 pstrcat(buf, buf_sz, "<xi:include href=\"");
881 pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
882 pstrcat(buf, buf_sz, "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200883 for (r = cpu->gdb_regs; r; r = r->next) {
Luc Michelc145eea2019-01-07 15:23:46 +0000884 pstrcat(buf, buf_sz, "<xi:include href=\"");
885 pstrcat(buf, buf_sz, r->xml);
886 pstrcat(buf, buf_sz, "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000887 }
Luc Michelc145eea2019-01-07 15:23:46 +0000888 pstrcat(buf, buf_sz, "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000889 }
Luc Michelc145eea2019-01-07 15:23:46 +0000890 return buf;
pbrook56aebc82008-10-11 17:55:29 +0000891 }
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100892 if (cc->gdb_get_dynamic_xml) {
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100893 char *xmlname = g_strndup(p, len);
894 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
895
896 g_free(xmlname);
897 if (xml) {
898 return xml;
899 }
900 }
pbrook56aebc82008-10-11 17:55:29 +0000901 for (i = 0; ; i++) {
902 name = xml_builtin[i][0];
903 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
904 break;
905 }
906 return name ? xml_builtin[i][1] : NULL;
907}
pbrook56aebc82008-10-11 17:55:29 +0000908
Alex Bennéea010bdb2020-03-16 17:21:41 +0000909static int gdb_read_register(CPUState *cpu, GByteArray *buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000910{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200911 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200912 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000913 GDBRegisterState *r;
914
Andreas Färbera0e372f2013-06-28 23:18:47 +0200915 if (reg < cc->gdb_num_core_regs) {
Alex Bennéea010bdb2020-03-16 17:21:41 +0000916 return cc->gdb_read_register(cpu, buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200917 }
pbrook56aebc82008-10-11 17:55:29 +0000918
Andreas Färbereac8b352013-06-28 21:11:37 +0200919 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000920 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
Alex Bennéea010bdb2020-03-16 17:21:41 +0000921 return r->get_reg(env, buf, reg - r->base_reg);
pbrook56aebc82008-10-11 17:55:29 +0000922 }
923 }
924 return 0;
925}
926
Andreas Färber385b9f02013-06-27 18:25:36 +0200927static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000928{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200929 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200930 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000931 GDBRegisterState *r;
932
Andreas Färbera0e372f2013-06-28 23:18:47 +0200933 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200934 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200935 }
pbrook56aebc82008-10-11 17:55:29 +0000936
Andreas Färbereac8b352013-06-28 21:11:37 +0200937 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000938 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
939 return r->set_reg(env, mem_buf, reg - r->base_reg);
940 }
941 }
942 return 0;
943}
944
945/* Register a supplemental set of CPU registers. If g_pos is nonzero it
946 specifies the first register number and these registers are included in
947 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
948 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
949 */
950
Andreas Färber22169d42013-06-28 21:27:39 +0200951void gdb_register_coprocessor(CPUState *cpu,
Alex Bennéea010bdb2020-03-16 17:21:41 +0000952 gdb_get_reg_cb get_reg, gdb_set_reg_cb set_reg,
Andreas Färber22169d42013-06-28 21:27:39 +0200953 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000954{
955 GDBRegisterState *s;
956 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000957
Andreas Färbereac8b352013-06-28 21:11:37 +0200958 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000959 while (*p) {
960 /* Check for duplicates. */
961 if (strcmp((*p)->xml, xml) == 0)
962 return;
963 p = &(*p)->next;
964 }
Stefan Weil9643c252011-10-18 22:25:38 +0200965
966 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200967 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200968 s->num_regs = num_regs;
969 s->get_reg = get_reg;
970 s->set_reg = set_reg;
971 s->xml = xml;
972
pbrook56aebc82008-10-11 17:55:29 +0000973 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200974 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000975 *p = s;
976 if (g_pos) {
977 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800978 error_report("Error: Bad gdb register numbering for '%s', "
979 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200980 } else {
981 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000982 }
983 }
984}
985
aliguoria1d1bb32008-11-18 20:07:32 +0000986#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100987/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
988static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
989{
990 static const int xlat[] = {
991 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
992 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
993 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
994 };
995
996 CPUClass *cc = CPU_GET_CLASS(cpu);
997 int cputype = xlat[gdbtype];
998
999 if (cc->gdb_stop_before_watchpoint) {
1000 cputype |= BP_STOP_BEFORE_ACCESS;
1001 }
1002 return cputype;
1003}
aliguoria1d1bb32008-11-18 20:07:32 +00001004#endif
1005
Jon Doron77f6ce52019-05-29 09:41:35 +03001006static int gdb_breakpoint_insert(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +00001007{
Andreas Färber182735e2013-05-29 22:29:20 +02001008 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001009 int err = 0;
1010
Andreas Färber62278812013-06-27 17:12:06 +02001011 if (kvm_enabled()) {
Alex Bennée8d98c442020-03-16 17:21:33 +00001012 return kvm_insert_breakpoint(gdbserver_state.c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001013 }
aliguorie22a25c2009-03-12 20:12:48 +00001014
aliguoria1d1bb32008-11-18 20:07:32 +00001015 switch (type) {
1016 case GDB_BREAKPOINT_SW:
1017 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001018 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001019 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
1020 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001021 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001022 }
aliguori880a7572008-11-18 20:30:24 +00001023 }
1024 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001025#ifndef CONFIG_USER_ONLY
1026 case GDB_WATCHPOINT_WRITE:
1027 case GDB_WATCHPOINT_READ:
1028 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001029 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001030 err = cpu_watchpoint_insert(cpu, addr, len,
1031 xlat_gdb_type(cpu, type), NULL);
1032 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001033 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +01001034 }
aliguori880a7572008-11-18 20:30:24 +00001035 }
1036 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001037#endif
1038 default:
1039 return -ENOSYS;
1040 }
1041}
1042
Jon Doron77f6ce52019-05-29 09:41:35 +03001043static int gdb_breakpoint_remove(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +00001044{
Andreas Färber182735e2013-05-29 22:29:20 +02001045 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001046 int err = 0;
1047
Andreas Färber62278812013-06-27 17:12:06 +02001048 if (kvm_enabled()) {
Alex Bennée8d98c442020-03-16 17:21:33 +00001049 return kvm_remove_breakpoint(gdbserver_state.c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001050 }
aliguorie22a25c2009-03-12 20:12:48 +00001051
aliguoria1d1bb32008-11-18 20:07:32 +00001052 switch (type) {
1053 case GDB_BREAKPOINT_SW:
1054 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001055 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001056 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
1057 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001058 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001059 }
aliguori880a7572008-11-18 20:30:24 +00001060 }
1061 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001062#ifndef CONFIG_USER_ONLY
1063 case GDB_WATCHPOINT_WRITE:
1064 case GDB_WATCHPOINT_READ:
1065 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001066 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001067 err = cpu_watchpoint_remove(cpu, addr, len,
1068 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +00001069 if (err)
1070 break;
1071 }
1072 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001073#endif
1074 default:
1075 return -ENOSYS;
1076 }
1077}
1078
Luc Michel546f3c62019-01-07 15:23:46 +00001079static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu)
1080{
1081 cpu_breakpoint_remove_all(cpu, BP_GDB);
1082#ifndef CONFIG_USER_ONLY
1083 cpu_watchpoint_remove_all(cpu, BP_GDB);
1084#endif
1085}
1086
Alex Bennéea346af32020-03-16 17:21:34 +00001087static void gdb_process_breakpoint_remove_all(GDBProcess *p)
Luc Michel546f3c62019-01-07 15:23:46 +00001088{
Alex Bennéea346af32020-03-16 17:21:34 +00001089 CPUState *cpu = get_first_cpu_in_process(p);
Luc Michel546f3c62019-01-07 15:23:46 +00001090
1091 while (cpu) {
1092 gdb_cpu_breakpoint_remove_all(cpu);
Alex Bennéea346af32020-03-16 17:21:34 +00001093 cpu = gdb_next_cpu_in_process(cpu);
Luc Michel546f3c62019-01-07 15:23:46 +00001094 }
1095}
1096
aliguori880a7572008-11-18 20:30:24 +00001097static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +00001098{
Andreas Färber182735e2013-05-29 22:29:20 +02001099 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001100
aliguorie22a25c2009-03-12 20:12:48 +00001101 if (kvm_enabled()) {
Alex Bennée8d98c442020-03-16 17:21:33 +00001102 kvm_remove_all_breakpoints(gdbserver_state.c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +00001103 return;
1104 }
1105
Andreas Färberbdc44642013-06-24 23:50:24 +02001106 CPU_FOREACH(cpu) {
Luc Michel546f3c62019-01-07 15:23:46 +00001107 gdb_cpu_breakpoint_remove_all(cpu);
aliguori880a7572008-11-18 20:30:24 +00001108 }
aliguoria1d1bb32008-11-18 20:07:32 +00001109}
1110
Alex Bennéea346af32020-03-16 17:21:34 +00001111static void gdb_set_cpu_pc(target_ulong pc)
aurel32fab9d282009-04-08 21:29:37 +00001112{
Alex Bennéea346af32020-03-16 17:21:34 +00001113 CPUState *cpu = gdbserver_state.c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +02001114
1115 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -07001116 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +00001117}
1118
Alex Bennée308f9e82020-03-16 17:21:35 +00001119static void gdb_append_thread_id(CPUState *cpu, GString *buf)
Luc Michel1a227332019-01-07 15:23:45 +00001120{
Alex Bennéea346af32020-03-16 17:21:34 +00001121 if (gdbserver_state.multiprocess) {
Alex Bennée308f9e82020-03-16 17:21:35 +00001122 g_string_append_printf(buf, "p%02x.%02x",
1123 gdb_get_cpu_pid(cpu), cpu_gdb_index(cpu));
Luc Michel1a227332019-01-07 15:23:45 +00001124 } else {
Alex Bennée308f9e82020-03-16 17:21:35 +00001125 g_string_append_printf(buf, "%02x", cpu_gdb_index(cpu));
Luc Michel1a227332019-01-07 15:23:45 +00001126 }
Luc Michel1a227332019-01-07 15:23:45 +00001127}
1128
Luc Michel7d8c87d2019-01-07 15:23:45 +00001129typedef enum GDBThreadIdKind {
1130 GDB_ONE_THREAD = 0,
1131 GDB_ALL_THREADS, /* One process, all threads */
1132 GDB_ALL_PROCESSES,
1133 GDB_READ_THREAD_ERR
1134} GDBThreadIdKind;
1135
1136static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1137 uint32_t *pid, uint32_t *tid)
1138{
1139 unsigned long p, t;
1140 int ret;
1141
1142 if (*buf == 'p') {
1143 buf++;
1144 ret = qemu_strtoul(buf, &buf, 16, &p);
1145
1146 if (ret) {
1147 return GDB_READ_THREAD_ERR;
1148 }
1149
1150 /* Skip '.' */
1151 buf++;
1152 } else {
1153 p = 1;
1154 }
1155
1156 ret = qemu_strtoul(buf, &buf, 16, &t);
1157
1158 if (ret) {
1159 return GDB_READ_THREAD_ERR;
1160 }
1161
1162 *end_buf = buf;
1163
1164 if (p == -1) {
1165 return GDB_ALL_PROCESSES;
1166 }
1167
1168 if (pid) {
1169 *pid = p;
1170 }
1171
1172 if (t == -1) {
1173 return GDB_ALL_THREADS;
1174 }
1175
1176 if (tid) {
1177 *tid = t;
1178 }
1179
1180 return GDB_ONE_THREAD;
1181}
1182
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001183/**
1184 * gdb_handle_vcont - Parses and handles a vCont packet.
1185 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1186 * a format error, 0 on success.
1187 */
Alex Bennéea346af32020-03-16 17:21:34 +00001188static int gdb_handle_vcont(const char *p)
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001189{
Luc Michele40e5202019-01-07 15:23:46 +00001190 int res, signal = 0;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001191 char cur_action;
1192 char *newstates;
1193 unsigned long tmp;
Luc Michele40e5202019-01-07 15:23:46 +00001194 uint32_t pid, tid;
1195 GDBProcess *process;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001196 CPUState *cpu;
Luc Michelc99ef792019-03-26 12:53:26 +00001197 GDBThreadIdKind kind;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001198#ifdef CONFIG_USER_ONLY
1199 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
1200
1201 CPU_FOREACH(cpu) {
1202 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1203 }
Like Xu5cc87672019-05-19 04:54:21 +08001204#else
1205 MachineState *ms = MACHINE(qdev_get_machine());
1206 unsigned int max_cpus = ms->smp.max_cpus;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001207#endif
1208 /* uninitialised CPUs stay 0 */
1209 newstates = g_new0(char, max_cpus);
1210
1211 /* mark valid CPUs with 1 */
1212 CPU_FOREACH(cpu) {
1213 newstates[cpu->cpu_index] = 1;
1214 }
1215
1216 /*
1217 * res keeps track of what error we are returning, with -ENOTSUP meaning
1218 * that the command is unknown or unsupported, thus returning an empty
1219 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1220 * or incorrect parameters passed.
1221 */
1222 res = 0;
1223 while (*p) {
1224 if (*p++ != ';') {
1225 res = -ENOTSUP;
1226 goto out;
1227 }
1228
1229 cur_action = *p++;
1230 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +01001231 cur_action = qemu_tolower(cur_action);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001232 res = qemu_strtoul(p + 1, &p, 16, &tmp);
1233 if (res) {
1234 goto out;
1235 }
1236 signal = gdb_signal_to_target(tmp);
1237 } else if (cur_action != 'c' && cur_action != 's') {
1238 /* unknown/invalid/unsupported command */
1239 res = -ENOTSUP;
1240 goto out;
1241 }
Luc Michele40e5202019-01-07 15:23:46 +00001242
Luc Michelc99ef792019-03-26 12:53:26 +00001243 if (*p == '\0' || *p == ';') {
1244 /*
1245 * No thread specifier, action is on "all threads". The
1246 * specification is unclear regarding the process to act on. We
1247 * choose all processes.
1248 */
1249 kind = GDB_ALL_PROCESSES;
1250 } else if (*p++ == ':') {
1251 kind = read_thread_id(p, &p, &pid, &tid);
1252 } else {
Luc Michele40e5202019-01-07 15:23:46 +00001253 res = -ENOTSUP;
1254 goto out;
1255 }
1256
Luc Michelc99ef792019-03-26 12:53:26 +00001257 switch (kind) {
Luc Michele40e5202019-01-07 15:23:46 +00001258 case GDB_READ_THREAD_ERR:
1259 res = -EINVAL;
1260 goto out;
1261
1262 case GDB_ALL_PROCESSES:
Alex Bennéea346af32020-03-16 17:21:34 +00001263 cpu = gdb_first_attached_cpu();
Luc Michele40e5202019-01-07 15:23:46 +00001264 while (cpu) {
1265 if (newstates[cpu->cpu_index] == 1) {
1266 newstates[cpu->cpu_index] = cur_action;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001267 }
Luc Michele40e5202019-01-07 15:23:46 +00001268
Alex Bennéea346af32020-03-16 17:21:34 +00001269 cpu = gdb_next_attached_cpu(cpu);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001270 }
Luc Michele40e5202019-01-07 15:23:46 +00001271 break;
1272
1273 case GDB_ALL_THREADS:
Alex Bennéea346af32020-03-16 17:21:34 +00001274 process = gdb_get_process(pid);
Luc Michele40e5202019-01-07 15:23:46 +00001275
1276 if (!process->attached) {
1277 res = -EINVAL;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001278 goto out;
1279 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001280
Alex Bennéea346af32020-03-16 17:21:34 +00001281 cpu = get_first_cpu_in_process(process);
Luc Michele40e5202019-01-07 15:23:46 +00001282 while (cpu) {
1283 if (newstates[cpu->cpu_index] == 1) {
1284 newstates[cpu->cpu_index] = cur_action;
1285 }
1286
Alex Bennéea346af32020-03-16 17:21:34 +00001287 cpu = gdb_next_cpu_in_process(cpu);
Luc Michele40e5202019-01-07 15:23:46 +00001288 }
1289 break;
1290
1291 case GDB_ONE_THREAD:
Alex Bennéea346af32020-03-16 17:21:34 +00001292 cpu = gdb_get_cpu(pid, tid);
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001293
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001294 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001295 if (!cpu) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001296 res = -EINVAL;
1297 goto out;
1298 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001299
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001300 /* only use if no previous match occourred */
1301 if (newstates[cpu->cpu_index] == 1) {
1302 newstates[cpu->cpu_index] = cur_action;
1303 }
Luc Michele40e5202019-01-07 15:23:46 +00001304 break;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001305 }
1306 }
Alex Bennéea346af32020-03-16 17:21:34 +00001307 gdbserver_state.signal = signal;
1308 gdb_continue_partial(newstates);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001309
1310out:
1311 g_free(newstates);
1312
1313 return res;
1314}
1315
Jon Dorond14055d2019-05-29 09:41:29 +03001316typedef union GdbCmdVariant {
1317 const char *data;
1318 uint8_t opcode;
1319 unsigned long val_ul;
1320 unsigned long long val_ull;
1321 struct {
1322 GDBThreadIdKind kind;
1323 uint32_t pid;
1324 uint32_t tid;
1325 } thread_id;
1326} GdbCmdVariant;
1327
1328static const char *cmd_next_param(const char *param, const char delimiter)
1329{
1330 static const char all_delimiters[] = ",;:=";
1331 char curr_delimiters[2] = {0};
1332 const char *delimiters;
1333
1334 if (delimiter == '?') {
1335 delimiters = all_delimiters;
1336 } else if (delimiter == '0') {
1337 return strchr(param, '\0');
1338 } else if (delimiter == '.' && *param) {
1339 return param + 1;
1340 } else {
1341 curr_delimiters[0] = delimiter;
1342 delimiters = curr_delimiters;
1343 }
1344
1345 param += strcspn(param, delimiters);
1346 if (*param) {
1347 param++;
1348 }
1349 return param;
1350}
1351
1352static int cmd_parse_params(const char *data, const char *schema,
1353 GdbCmdVariant *params, int *num_params)
1354{
1355 int curr_param;
1356 const char *curr_schema, *curr_data;
1357
1358 *num_params = 0;
1359
1360 if (!schema) {
1361 return 0;
1362 }
1363
1364 curr_schema = schema;
1365 curr_param = 0;
1366 curr_data = data;
1367 while (curr_schema[0] && curr_schema[1] && *curr_data) {
1368 switch (curr_schema[0]) {
1369 case 'l':
1370 if (qemu_strtoul(curr_data, &curr_data, 16,
1371 &params[curr_param].val_ul)) {
1372 return -EINVAL;
1373 }
1374 curr_param++;
1375 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1376 break;
1377 case 'L':
1378 if (qemu_strtou64(curr_data, &curr_data, 16,
1379 (uint64_t *)&params[curr_param].val_ull)) {
1380 return -EINVAL;
1381 }
1382 curr_param++;
1383 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1384 break;
1385 case 's':
1386 params[curr_param].data = curr_data;
1387 curr_param++;
1388 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1389 break;
1390 case 'o':
1391 params[curr_param].opcode = *(uint8_t *)curr_data;
1392 curr_param++;
1393 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1394 break;
1395 case 't':
1396 params[curr_param].thread_id.kind =
1397 read_thread_id(curr_data, &curr_data,
1398 &params[curr_param].thread_id.pid,
1399 &params[curr_param].thread_id.tid);
1400 curr_param++;
1401 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1402 break;
1403 case '?':
1404 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1405 break;
1406 default:
1407 return -EINVAL;
1408 }
1409 curr_schema += 2;
1410 }
1411
1412 *num_params = curr_param;
1413 return 0;
1414}
1415
1416typedef struct GdbCmdContext {
Jon Dorond14055d2019-05-29 09:41:29 +03001417 GdbCmdVariant *params;
1418 int num_params;
Jon Dorond14055d2019-05-29 09:41:29 +03001419} GdbCmdContext;
1420
1421typedef void (*GdbCmdHandler)(GdbCmdContext *gdb_ctx, void *user_ctx);
1422
1423/*
1424 * cmd_startswith -> cmd is compared using startswith
1425 *
1426 *
1427 * schema definitions:
1428 * Each schema parameter entry consists of 2 chars,
1429 * the first char represents the parameter type handling
1430 * the second char represents the delimiter for the next parameter
1431 *
1432 * Currently supported schema types:
1433 * 'l' -> unsigned long (stored in .val_ul)
1434 * 'L' -> unsigned long long (stored in .val_ull)
1435 * 's' -> string (stored in .data)
1436 * 'o' -> single char (stored in .opcode)
1437 * 't' -> thread id (stored in .thread_id)
1438 * '?' -> skip according to delimiter
1439 *
1440 * Currently supported delimiters:
1441 * '?' -> Stop at any delimiter (",;:=\0")
1442 * '0' -> Stop at "\0"
1443 * '.' -> Skip 1 char unless reached "\0"
1444 * Any other value is treated as the delimiter value itself
1445 */
1446typedef struct GdbCmdParseEntry {
1447 GdbCmdHandler handler;
1448 const char *cmd;
1449 bool cmd_startswith;
1450 const char *schema;
1451} GdbCmdParseEntry;
1452
1453static inline int startswith(const char *string, const char *pattern)
1454{
1455 return !strncmp(string, pattern, strlen(pattern));
1456}
1457
Alex Bennéea346af32020-03-16 17:21:34 +00001458static int process_string_cmd(void *user_ctx, const char *data,
Jon Dorond14055d2019-05-29 09:41:29 +03001459 const GdbCmdParseEntry *cmds, int num_cmds)
1460{
1461 int i, schema_len, max_num_params = 0;
1462 GdbCmdContext gdb_ctx;
1463
1464 if (!cmds) {
1465 return -1;
1466 }
1467
1468 for (i = 0; i < num_cmds; i++) {
1469 const GdbCmdParseEntry *cmd = &cmds[i];
1470 g_assert(cmd->handler && cmd->cmd);
1471
1472 if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
1473 (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
1474 continue;
1475 }
1476
1477 if (cmd->schema) {
1478 schema_len = strlen(cmd->schema);
1479 if (schema_len % 2) {
1480 return -2;
1481 }
1482
1483 max_num_params = schema_len / 2;
1484 }
1485
1486 gdb_ctx.params =
1487 (GdbCmdVariant *)alloca(sizeof(*gdb_ctx.params) * max_num_params);
1488 memset(gdb_ctx.params, 0, sizeof(*gdb_ctx.params) * max_num_params);
1489
1490 if (cmd_parse_params(&data[strlen(cmd->cmd)], cmd->schema,
1491 gdb_ctx.params, &gdb_ctx.num_params)) {
1492 return -1;
1493 }
1494
Jon Dorond14055d2019-05-29 09:41:29 +03001495 cmd->handler(&gdb_ctx, user_ctx);
1496 return 0;
1497 }
1498
1499 return -1;
1500}
1501
Alex Bennéea346af32020-03-16 17:21:34 +00001502static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd)
Jon Doron3e2c1262019-05-29 09:41:30 +03001503{
1504 if (!data) {
1505 return;
1506 }
1507
Alex Bennée308f9e82020-03-16 17:21:35 +00001508 g_string_set_size(gdbserver_state.str_buf, 0);
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001509 g_byte_array_set_size(gdbserver_state.mem_buf, 0);
Alex Bennée308f9e82020-03-16 17:21:35 +00001510
Jon Doron3e2c1262019-05-29 09:41:30 +03001511 /* In case there was an error during the command parsing we must
1512 * send a NULL packet to indicate the command is not supported */
Alex Bennéea346af32020-03-16 17:21:34 +00001513 if (process_string_cmd(NULL, data, cmd, 1)) {
1514 put_packet("");
Jon Doron3e2c1262019-05-29 09:41:30 +03001515 }
1516}
1517
1518static void handle_detach(GdbCmdContext *gdb_ctx, void *user_ctx)
1519{
1520 GDBProcess *process;
Jon Doron3e2c1262019-05-29 09:41:30 +03001521 uint32_t pid = 1;
1522
Alex Bennéea346af32020-03-16 17:21:34 +00001523 if (gdbserver_state.multiprocess) {
Jon Doron3e2c1262019-05-29 09:41:30 +03001524 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001525 put_packet("E22");
Jon Doron3e2c1262019-05-29 09:41:30 +03001526 return;
1527 }
1528
1529 pid = gdb_ctx->params[0].val_ul;
1530 }
1531
Alex Bennéea346af32020-03-16 17:21:34 +00001532 process = gdb_get_process(pid);
1533 gdb_process_breakpoint_remove_all(process);
Jon Doron3e2c1262019-05-29 09:41:30 +03001534 process->attached = false;
1535
Alex Bennéea346af32020-03-16 17:21:34 +00001536 if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) {
1537 gdbserver_state.c_cpu = gdb_first_attached_cpu();
Jon Doron3e2c1262019-05-29 09:41:30 +03001538 }
1539
Alex Bennéea346af32020-03-16 17:21:34 +00001540 if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) {
1541 gdbserver_state.g_cpu = gdb_first_attached_cpu();
Jon Doron3e2c1262019-05-29 09:41:30 +03001542 }
1543
Alex Bennéea346af32020-03-16 17:21:34 +00001544 if (!gdbserver_state.c_cpu) {
Jon Doron3e2c1262019-05-29 09:41:30 +03001545 /* No more process attached */
1546 gdb_syscall_mode = GDB_SYS_DISABLED;
Alex Bennéea346af32020-03-16 17:21:34 +00001547 gdb_continue();
Jon Doron3e2c1262019-05-29 09:41:30 +03001548 }
Alex Bennéea346af32020-03-16 17:21:34 +00001549 put_packet("OK");
Jon Doron3e2c1262019-05-29 09:41:30 +03001550}
1551
Jon Doron44ffded2019-05-29 09:41:31 +03001552static void handle_thread_alive(GdbCmdContext *gdb_ctx, void *user_ctx)
1553{
1554 CPUState *cpu;
1555
1556 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001557 put_packet("E22");
Jon Doron44ffded2019-05-29 09:41:31 +03001558 return;
1559 }
1560
1561 if (gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
Alex Bennéea346af32020-03-16 17:21:34 +00001562 put_packet("E22");
Jon Doron44ffded2019-05-29 09:41:31 +03001563 return;
1564 }
1565
Alex Bennéea346af32020-03-16 17:21:34 +00001566 cpu = gdb_get_cpu(gdb_ctx->params[0].thread_id.pid,
Jon Doron44ffded2019-05-29 09:41:31 +03001567 gdb_ctx->params[0].thread_id.tid);
1568 if (!cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +00001569 put_packet("E22");
Jon Doron44ffded2019-05-29 09:41:31 +03001570 return;
1571 }
1572
Alex Bennéea346af32020-03-16 17:21:34 +00001573 put_packet("OK");
Jon Doron44ffded2019-05-29 09:41:31 +03001574}
1575
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001576static void handle_continue(GdbCmdContext *gdb_ctx, void *user_ctx)
1577{
1578 if (gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001579 gdb_set_cpu_pc(gdb_ctx->params[0].val_ull);
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001580 }
1581
Alex Bennéea346af32020-03-16 17:21:34 +00001582 gdbserver_state.signal = 0;
1583 gdb_continue();
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001584}
1585
Jon Doronccc47d52019-05-29 09:41:33 +03001586static void handle_cont_with_sig(GdbCmdContext *gdb_ctx, void *user_ctx)
1587{
1588 unsigned long signal = 0;
1589
1590 /*
1591 * Note: C sig;[addr] is currently unsupported and we simply
1592 * omit the addr parameter
1593 */
1594 if (gdb_ctx->num_params) {
1595 signal = gdb_ctx->params[0].val_ul;
1596 }
1597
Alex Bennéea346af32020-03-16 17:21:34 +00001598 gdbserver_state.signal = gdb_signal_to_target(signal);
1599 if (gdbserver_state.signal == -1) {
1600 gdbserver_state.signal = 0;
Jon Doronccc47d52019-05-29 09:41:33 +03001601 }
Alex Bennéea346af32020-03-16 17:21:34 +00001602 gdb_continue();
Jon Doronccc47d52019-05-29 09:41:33 +03001603}
1604
Jon Doron3a9651d2019-05-29 09:41:34 +03001605static void handle_set_thread(GdbCmdContext *gdb_ctx, void *user_ctx)
1606{
1607 CPUState *cpu;
1608
1609 if (gdb_ctx->num_params != 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001610 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001611 return;
1612 }
1613
1614 if (gdb_ctx->params[1].thread_id.kind == GDB_READ_THREAD_ERR) {
Alex Bennéea346af32020-03-16 17:21:34 +00001615 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001616 return;
1617 }
1618
1619 if (gdb_ctx->params[1].thread_id.kind != GDB_ONE_THREAD) {
Alex Bennéea346af32020-03-16 17:21:34 +00001620 put_packet("OK");
Jon Doron3a9651d2019-05-29 09:41:34 +03001621 return;
1622 }
1623
Alex Bennéea346af32020-03-16 17:21:34 +00001624 cpu = gdb_get_cpu(gdb_ctx->params[1].thread_id.pid,
Jon Doron3a9651d2019-05-29 09:41:34 +03001625 gdb_ctx->params[1].thread_id.tid);
1626 if (!cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +00001627 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001628 return;
1629 }
1630
1631 /*
1632 * Note: This command is deprecated and modern gdb's will be using the
1633 * vCont command instead.
1634 */
1635 switch (gdb_ctx->params[0].opcode) {
1636 case 'c':
Alex Bennéea346af32020-03-16 17:21:34 +00001637 gdbserver_state.c_cpu = cpu;
1638 put_packet("OK");
Jon Doron3a9651d2019-05-29 09:41:34 +03001639 break;
1640 case 'g':
Alex Bennéea346af32020-03-16 17:21:34 +00001641 gdbserver_state.g_cpu = cpu;
1642 put_packet("OK");
Jon Doron3a9651d2019-05-29 09:41:34 +03001643 break;
1644 default:
Alex Bennéea346af32020-03-16 17:21:34 +00001645 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001646 break;
1647 }
1648}
1649
Jon Doron77f6ce52019-05-29 09:41:35 +03001650static void handle_insert_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1651{
1652 int res;
1653
1654 if (gdb_ctx->num_params != 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00001655 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001656 return;
1657 }
1658
1659 res = gdb_breakpoint_insert(gdb_ctx->params[0].val_ul,
1660 gdb_ctx->params[1].val_ull,
1661 gdb_ctx->params[2].val_ull);
1662 if (res >= 0) {
Alex Bennéea346af32020-03-16 17:21:34 +00001663 put_packet("OK");
Jon Doron77f6ce52019-05-29 09:41:35 +03001664 return;
1665 } else if (res == -ENOSYS) {
Alex Bennéea346af32020-03-16 17:21:34 +00001666 put_packet("");
Jon Doron77f6ce52019-05-29 09:41:35 +03001667 return;
1668 }
1669
Alex Bennéea346af32020-03-16 17:21:34 +00001670 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001671}
1672
1673static void handle_remove_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1674{
1675 int res;
1676
1677 if (gdb_ctx->num_params != 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00001678 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001679 return;
1680 }
1681
1682 res = gdb_breakpoint_remove(gdb_ctx->params[0].val_ul,
1683 gdb_ctx->params[1].val_ull,
1684 gdb_ctx->params[2].val_ull);
1685 if (res >= 0) {
Alex Bennéea346af32020-03-16 17:21:34 +00001686 put_packet("OK");
Jon Doron77f6ce52019-05-29 09:41:35 +03001687 return;
1688 } else if (res == -ENOSYS) {
Alex Bennéea346af32020-03-16 17:21:34 +00001689 put_packet("");
Jon Doron77f6ce52019-05-29 09:41:35 +03001690 return;
1691 }
1692
Alex Bennéea346af32020-03-16 17:21:34 +00001693 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001694}
1695
Alex Bennée94b2a622019-07-05 14:23:07 +01001696/*
1697 * handle_set/get_reg
1698 *
1699 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1700 * This works, but can be very slow. Anything new enough to understand
1701 * XML also knows how to use this properly. However to use this we
1702 * need to define a local XML file as well as be talking to a
1703 * reasonably modern gdb. Responding with an empty packet will cause
1704 * the remote gdb to fallback to older methods.
1705 */
1706
Jon Doron62b33202019-05-29 09:41:36 +03001707static void handle_set_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1708{
1709 int reg_size;
1710
1711 if (!gdb_has_xml) {
Alex Bennéea346af32020-03-16 17:21:34 +00001712 put_packet("");
Jon Doron62b33202019-05-29 09:41:36 +03001713 return;
1714 }
1715
1716 if (gdb_ctx->num_params != 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001717 put_packet("E22");
Jon Doron62b33202019-05-29 09:41:36 +03001718 return;
1719 }
1720
1721 reg_size = strlen(gdb_ctx->params[1].data) / 2;
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001722 hextomem(gdbserver_state.mem_buf, gdb_ctx->params[1].data, reg_size);
1723 gdb_write_register(gdbserver_state.g_cpu, gdbserver_state.mem_buf->data,
Jon Doron62b33202019-05-29 09:41:36 +03001724 gdb_ctx->params[0].val_ull);
Alex Bennéea346af32020-03-16 17:21:34 +00001725 put_packet("OK");
Jon Doron62b33202019-05-29 09:41:36 +03001726}
1727
Jon Doron5d0e57b2019-05-29 09:41:37 +03001728static void handle_get_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1729{
1730 int reg_size;
1731
Jon Doron5d0e57b2019-05-29 09:41:37 +03001732 if (!gdb_has_xml) {
Alex Bennéea346af32020-03-16 17:21:34 +00001733 put_packet("");
Jon Doron5d0e57b2019-05-29 09:41:37 +03001734 return;
1735 }
1736
1737 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001738 put_packet("E14");
Jon Doron5d0e57b2019-05-29 09:41:37 +03001739 return;
1740 }
1741
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001742 reg_size = gdb_read_register(gdbserver_state.g_cpu,
Alex Bennéea010bdb2020-03-16 17:21:41 +00001743 gdbserver_state.mem_buf,
Jon Doron5d0e57b2019-05-29 09:41:37 +03001744 gdb_ctx->params[0].val_ull);
1745 if (!reg_size) {
Alex Bennéea346af32020-03-16 17:21:34 +00001746 put_packet("E14");
Jon Doron5d0e57b2019-05-29 09:41:37 +03001747 return;
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001748 } else {
1749 g_byte_array_set_size(gdbserver_state.mem_buf, reg_size);
Jon Doron5d0e57b2019-05-29 09:41:37 +03001750 }
1751
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001752 memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, reg_size);
Alex Bennée308f9e82020-03-16 17:21:35 +00001753 put_strbuf();
Jon Doron5d0e57b2019-05-29 09:41:37 +03001754}
1755
Jon Doroncc0ecc72019-05-29 09:41:38 +03001756static void handle_write_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1757{
1758 if (gdb_ctx->num_params != 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00001759 put_packet("E22");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001760 return;
1761 }
1762
1763 /* hextomem() reads 2*len bytes */
1764 if (gdb_ctx->params[1].val_ull > strlen(gdb_ctx->params[2].data) / 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001765 put_packet("E22");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001766 return;
1767 }
1768
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001769 hextomem(gdbserver_state.mem_buf, gdb_ctx->params[2].data,
Jon Doroncc0ecc72019-05-29 09:41:38 +03001770 gdb_ctx->params[1].val_ull);
Alex Bennéea346af32020-03-16 17:21:34 +00001771 if (target_memory_rw_debug(gdbserver_state.g_cpu, gdb_ctx->params[0].val_ull,
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001772 gdbserver_state.mem_buf->data,
1773 gdbserver_state.mem_buf->len, true)) {
Alex Bennéea346af32020-03-16 17:21:34 +00001774 put_packet("E14");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001775 return;
1776 }
1777
Alex Bennéea346af32020-03-16 17:21:34 +00001778 put_packet("OK");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001779}
1780
Jon Doronda92e232019-05-29 09:41:39 +03001781static void handle_read_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1782{
1783 if (gdb_ctx->num_params != 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001784 put_packet("E22");
Jon Doronda92e232019-05-29 09:41:39 +03001785 return;
1786 }
1787
1788 /* memtohex() doubles the required space */
1789 if (gdb_ctx->params[1].val_ull > MAX_PACKET_LENGTH / 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001790 put_packet("E22");
Jon Doronda92e232019-05-29 09:41:39 +03001791 return;
1792 }
1793
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001794 g_byte_array_set_size(gdbserver_state.mem_buf, gdb_ctx->params[1].val_ull);
1795
Alex Bennéea346af32020-03-16 17:21:34 +00001796 if (target_memory_rw_debug(gdbserver_state.g_cpu, gdb_ctx->params[0].val_ull,
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001797 gdbserver_state.mem_buf->data,
1798 gdbserver_state.mem_buf->len, false)) {
Alex Bennéea346af32020-03-16 17:21:34 +00001799 put_packet("E14");
Jon Doronda92e232019-05-29 09:41:39 +03001800 return;
1801 }
1802
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001803 memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data,
1804 gdbserver_state.mem_buf->len);
Alex Bennée308f9e82020-03-16 17:21:35 +00001805 put_strbuf();
Jon Doronda92e232019-05-29 09:41:39 +03001806}
1807
Jon Doron287ca122019-05-29 09:41:40 +03001808static void handle_write_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1809{
1810 target_ulong addr, len;
1811 uint8_t *registers;
1812 int reg_size;
1813
1814 if (!gdb_ctx->num_params) {
1815 return;
1816 }
1817
Alex Bennéea346af32020-03-16 17:21:34 +00001818 cpu_synchronize_state(gdbserver_state.g_cpu);
Jon Doron287ca122019-05-29 09:41:40 +03001819 len = strlen(gdb_ctx->params[0].data) / 2;
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001820 hextomem(gdbserver_state.mem_buf, gdb_ctx->params[0].data, len);
1821 registers = gdbserver_state.mem_buf->data;
Alex Bennéea346af32020-03-16 17:21:34 +00001822 for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0;
Jon Doron287ca122019-05-29 09:41:40 +03001823 addr++) {
Alex Bennéea346af32020-03-16 17:21:34 +00001824 reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, addr);
Jon Doron287ca122019-05-29 09:41:40 +03001825 len -= reg_size;
1826 registers += reg_size;
1827 }
Alex Bennéea346af32020-03-16 17:21:34 +00001828 put_packet("OK");
Jon Doron287ca122019-05-29 09:41:40 +03001829}
1830
Jon Doron397d1372019-05-29 09:41:41 +03001831static void handle_read_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1832{
1833 target_ulong addr, len;
1834
Alex Bennéea346af32020-03-16 17:21:34 +00001835 cpu_synchronize_state(gdbserver_state.g_cpu);
Alex Bennéea010bdb2020-03-16 17:21:41 +00001836 g_byte_array_set_size(gdbserver_state.mem_buf, 0);
Jon Doron397d1372019-05-29 09:41:41 +03001837 len = 0;
Alex Bennéea346af32020-03-16 17:21:34 +00001838 for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs; addr++) {
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001839 len += gdb_read_register(gdbserver_state.g_cpu,
Alex Bennéea010bdb2020-03-16 17:21:41 +00001840 gdbserver_state.mem_buf,
Jon Doron397d1372019-05-29 09:41:41 +03001841 addr);
1842 }
Alex Bennéea010bdb2020-03-16 17:21:41 +00001843 g_assert(len == gdbserver_state.mem_buf->len);
Jon Doron397d1372019-05-29 09:41:41 +03001844
Alex Bennée4a25f1b2020-03-16 17:21:36 +00001845 memtohex(gdbserver_state.str_buf, gdbserver_state.mem_buf->data, len);
Alex Bennée308f9e82020-03-16 17:21:35 +00001846 put_strbuf();
Jon Doron397d1372019-05-29 09:41:41 +03001847}
1848
Jon Doron4b20fab2019-05-29 09:41:42 +03001849static void handle_file_io(GdbCmdContext *gdb_ctx, void *user_ctx)
1850{
Alex Bennéea346af32020-03-16 17:21:34 +00001851 if (gdb_ctx->num_params >= 1 && gdbserver_state.current_syscall_cb) {
Jon Doron4b20fab2019-05-29 09:41:42 +03001852 target_ulong ret, err;
1853
1854 ret = (target_ulong)gdb_ctx->params[0].val_ull;
Sandra Loosemorec6ee9522019-08-27 16:33:17 -06001855 if (gdb_ctx->num_params >= 2) {
1856 err = (target_ulong)gdb_ctx->params[1].val_ull;
1857 } else {
1858 err = 0;
1859 }
Alex Bennéea346af32020-03-16 17:21:34 +00001860 gdbserver_state.current_syscall_cb(gdbserver_state.c_cpu, ret, err);
1861 gdbserver_state.current_syscall_cb = NULL;
Jon Doron4b20fab2019-05-29 09:41:42 +03001862 }
1863
1864 if (gdb_ctx->num_params >= 3 && gdb_ctx->params[2].opcode == (uint8_t)'C') {
Alex Bennéea346af32020-03-16 17:21:34 +00001865 put_packet("T02");
Jon Doron4b20fab2019-05-29 09:41:42 +03001866 return;
1867 }
1868
Alex Bennéea346af32020-03-16 17:21:34 +00001869 gdb_continue();
Jon Doron4b20fab2019-05-29 09:41:42 +03001870}
1871
Jon Doron933f80d2019-05-29 09:41:43 +03001872static void handle_step(GdbCmdContext *gdb_ctx, void *user_ctx)
1873{
1874 if (gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001875 gdb_set_cpu_pc((target_ulong)gdb_ctx->params[0].val_ull);
Jon Doron933f80d2019-05-29 09:41:43 +03001876 }
1877
Alex Bennéea346af32020-03-16 17:21:34 +00001878 cpu_single_step(gdbserver_state.c_cpu, sstep_flags);
1879 gdb_continue();
Jon Doron933f80d2019-05-29 09:41:43 +03001880}
1881
Jon Doron8536ec02019-05-29 09:41:44 +03001882static void handle_v_cont_query(GdbCmdContext *gdb_ctx, void *user_ctx)
1883{
Alex Bennéea346af32020-03-16 17:21:34 +00001884 put_packet("vCont;c;C;s;S");
Jon Doron8536ec02019-05-29 09:41:44 +03001885}
1886
1887static void handle_v_cont(GdbCmdContext *gdb_ctx, void *user_ctx)
1888{
1889 int res;
1890
1891 if (!gdb_ctx->num_params) {
1892 return;
1893 }
1894
Alex Bennéea346af32020-03-16 17:21:34 +00001895 res = gdb_handle_vcont(gdb_ctx->params[0].data);
Jon Doron8536ec02019-05-29 09:41:44 +03001896 if ((res == -EINVAL) || (res == -ERANGE)) {
Alex Bennéea346af32020-03-16 17:21:34 +00001897 put_packet("E22");
Jon Doron8536ec02019-05-29 09:41:44 +03001898 } else if (res) {
Alex Bennéea346af32020-03-16 17:21:34 +00001899 put_packet("");
Jon Doron8536ec02019-05-29 09:41:44 +03001900 }
1901}
1902
1903static void handle_v_attach(GdbCmdContext *gdb_ctx, void *user_ctx)
1904{
1905 GDBProcess *process;
1906 CPUState *cpu;
Jon Doron8536ec02019-05-29 09:41:44 +03001907
Alex Bennée308f9e82020-03-16 17:21:35 +00001908 g_string_assign(gdbserver_state.str_buf, "E22");
Jon Doron8536ec02019-05-29 09:41:44 +03001909 if (!gdb_ctx->num_params) {
1910 goto cleanup;
1911 }
1912
Alex Bennéea346af32020-03-16 17:21:34 +00001913 process = gdb_get_process(gdb_ctx->params[0].val_ul);
Jon Doron8536ec02019-05-29 09:41:44 +03001914 if (!process) {
1915 goto cleanup;
1916 }
1917
Alex Bennéea346af32020-03-16 17:21:34 +00001918 cpu = get_first_cpu_in_process(process);
Jon Doron8536ec02019-05-29 09:41:44 +03001919 if (!cpu) {
1920 goto cleanup;
1921 }
1922
1923 process->attached = true;
Alex Bennéea346af32020-03-16 17:21:34 +00001924 gdbserver_state.g_cpu = cpu;
1925 gdbserver_state.c_cpu = cpu;
Jon Doron8536ec02019-05-29 09:41:44 +03001926
Alex Bennée308f9e82020-03-16 17:21:35 +00001927 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
1928 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
1929 g_string_append_c(gdbserver_state.str_buf, ';');
Jon Doron8536ec02019-05-29 09:41:44 +03001930cleanup:
Alex Bennée308f9e82020-03-16 17:21:35 +00001931 put_strbuf();
Jon Doron8536ec02019-05-29 09:41:44 +03001932}
1933
1934static void handle_v_kill(GdbCmdContext *gdb_ctx, void *user_ctx)
1935{
1936 /* Kill the target */
Alex Bennéea346af32020-03-16 17:21:34 +00001937 put_packet("OK");
Jon Doron8536ec02019-05-29 09:41:44 +03001938 error_report("QEMU: Terminated via GDBstub");
1939 exit(0);
1940}
1941
1942static GdbCmdParseEntry gdb_v_commands_table[] = {
1943 /* Order is important if has same prefix */
1944 {
1945 .handler = handle_v_cont_query,
1946 .cmd = "Cont?",
1947 .cmd_startswith = 1
1948 },
1949 {
1950 .handler = handle_v_cont,
1951 .cmd = "Cont",
1952 .cmd_startswith = 1,
1953 .schema = "s0"
1954 },
1955 {
1956 .handler = handle_v_attach,
1957 .cmd = "Attach;",
1958 .cmd_startswith = 1,
1959 .schema = "l0"
1960 },
1961 {
1962 .handler = handle_v_kill,
1963 .cmd = "Kill;",
1964 .cmd_startswith = 1
1965 },
1966};
1967
1968static void handle_v_commands(GdbCmdContext *gdb_ctx, void *user_ctx)
1969{
1970 if (!gdb_ctx->num_params) {
1971 return;
1972 }
1973
Alex Bennéea346af32020-03-16 17:21:34 +00001974 if (process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron8536ec02019-05-29 09:41:44 +03001975 gdb_v_commands_table,
1976 ARRAY_SIZE(gdb_v_commands_table))) {
Alex Bennéea346af32020-03-16 17:21:34 +00001977 put_packet("");
Jon Doron8536ec02019-05-29 09:41:44 +03001978 }
1979}
1980
Jon Doron2704efa2019-05-29 09:41:45 +03001981static void handle_query_qemu_sstepbits(GdbCmdContext *gdb_ctx, void *user_ctx)
1982{
Alex Bennée308f9e82020-03-16 17:21:35 +00001983 g_string_printf(gdbserver_state.str_buf, "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1984 SSTEP_ENABLE, SSTEP_NOIRQ, SSTEP_NOTIMER);
1985 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03001986}
1987
1988static void handle_set_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
1989{
1990 if (!gdb_ctx->num_params) {
1991 return;
1992 }
1993
1994 sstep_flags = gdb_ctx->params[0].val_ul;
Alex Bennéea346af32020-03-16 17:21:34 +00001995 put_packet("OK");
Jon Doron2704efa2019-05-29 09:41:45 +03001996}
1997
1998static void handle_query_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
1999{
Alex Bennée308f9e82020-03-16 17:21:35 +00002000 g_string_printf(gdbserver_state.str_buf, "0x%x", sstep_flags);
2001 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002002}
2003
2004static void handle_query_curr_tid(GdbCmdContext *gdb_ctx, void *user_ctx)
bellardb4608c02003-06-27 17:34:32 +00002005{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002006 CPUState *cpu;
Luc Michelc145eea2019-01-07 15:23:46 +00002007 GDBProcess *process;
Jon Doron2704efa2019-05-29 09:41:45 +03002008
2009 /*
2010 * "Current thread" remains vague in the spec, so always return
2011 * the first thread of the current process (gdb returns the
2012 * first thread).
2013 */
Alex Bennéea346af32020-03-16 17:21:34 +00002014 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
2015 cpu = get_first_cpu_in_process(process);
Alex Bennée308f9e82020-03-16 17:21:35 +00002016 g_string_assign(gdbserver_state.str_buf, "QC");
2017 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
2018 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002019}
2020
2021static void handle_query_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
2022{
Alex Bennéea346af32020-03-16 17:21:34 +00002023 if (!gdbserver_state.query_cpu) {
2024 put_packet("l");
Jon Doron2704efa2019-05-29 09:41:45 +03002025 return;
2026 }
2027
Alex Bennée308f9e82020-03-16 17:21:35 +00002028 g_string_assign(gdbserver_state.str_buf, "m");
2029 gdb_append_thread_id(gdbserver_state.query_cpu, gdbserver_state.str_buf);
2030 put_strbuf();
Alex Bennéea346af32020-03-16 17:21:34 +00002031 gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu);
Jon Doron2704efa2019-05-29 09:41:45 +03002032}
2033
2034static void handle_query_first_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
2035{
Alex Bennéea346af32020-03-16 17:21:34 +00002036 gdbserver_state.query_cpu = gdb_first_attached_cpu();
Jon Doron2704efa2019-05-29 09:41:45 +03002037 handle_query_threads(gdb_ctx, user_ctx);
2038}
2039
2040static void handle_query_thread_extra(GdbCmdContext *gdb_ctx, void *user_ctx)
2041{
Alex Bennée308f9e82020-03-16 17:21:35 +00002042 g_autoptr(GString) rs = g_string_new(NULL);
Jon Doron2704efa2019-05-29 09:41:45 +03002043 CPUState *cpu;
Jon Doron2704efa2019-05-29 09:41:45 +03002044
2045 if (!gdb_ctx->num_params ||
2046 gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
Alex Bennéea346af32020-03-16 17:21:34 +00002047 put_packet("E22");
Jon Doron2704efa2019-05-29 09:41:45 +03002048 return;
2049 }
2050
Alex Bennéea346af32020-03-16 17:21:34 +00002051 cpu = gdb_get_cpu(gdb_ctx->params[0].thread_id.pid,
Jon Doron2704efa2019-05-29 09:41:45 +03002052 gdb_ctx->params[0].thread_id.tid);
2053 if (!cpu) {
2054 return;
2055 }
2056
2057 cpu_synchronize_state(cpu);
2058
Alex Bennéea346af32020-03-16 17:21:34 +00002059 if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) {
Jon Doron2704efa2019-05-29 09:41:45 +03002060 /* Print the CPU model and name in multiprocess mode */
2061 ObjectClass *oc = object_get_class(OBJECT(cpu));
2062 const char *cpu_model = object_class_get_name(oc);
Denis Plotnikov076b2fa2020-04-03 20:11:44 +01002063 g_autofree char *cpu_name =
2064 object_get_canonical_path_component(OBJECT(cpu));
Alex Bennée308f9e82020-03-16 17:21:35 +00002065 g_string_printf(rs, "%s %s [%s]", cpu_model, cpu_name,
2066 cpu->halted ? "halted " : "running");
Jon Doron2704efa2019-05-29 09:41:45 +03002067 } else {
Alex Bennée308f9e82020-03-16 17:21:35 +00002068 g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index,
Jon Doron2704efa2019-05-29 09:41:45 +03002069 cpu->halted ? "halted " : "running");
2070 }
Alex Bennée308f9e82020-03-16 17:21:35 +00002071 trace_gdbstub_op_extra_info(rs->str);
2072 memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len);
2073 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002074}
2075
2076#ifdef CONFIG_USER_ONLY
2077static void handle_query_offsets(GdbCmdContext *gdb_ctx, void *user_ctx)
2078{
2079 TaskState *ts;
2080
Alex Bennéea346af32020-03-16 17:21:34 +00002081 ts = gdbserver_state.c_cpu->opaque;
Alex Bennée308f9e82020-03-16 17:21:35 +00002082 g_string_printf(gdbserver_state.str_buf,
2083 "Text=" TARGET_ABI_FMT_lx
2084 ";Data=" TARGET_ABI_FMT_lx
2085 ";Bss=" TARGET_ABI_FMT_lx,
2086 ts->info->code_offset,
2087 ts->info->data_offset,
2088 ts->info->data_offset);
2089 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002090}
2091#else
2092static void handle_query_rcmd(GdbCmdContext *gdb_ctx, void *user_ctx)
2093{
Alex Bennée4a25f1b2020-03-16 17:21:36 +00002094 const guint8 zero = 0;
Jon Doron2704efa2019-05-29 09:41:45 +03002095 int len;
2096
2097 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00002098 put_packet("E22");
Jon Doron2704efa2019-05-29 09:41:45 +03002099 return;
2100 }
2101
2102 len = strlen(gdb_ctx->params[0].data);
2103 if (len % 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00002104 put_packet("E01");
Jon Doron2704efa2019-05-29 09:41:45 +03002105 return;
2106 }
2107
Alex Bennée4a25f1b2020-03-16 17:21:36 +00002108 g_assert(gdbserver_state.mem_buf->len == 0);
Jon Doron2704efa2019-05-29 09:41:45 +03002109 len = len / 2;
Alex Bennée4a25f1b2020-03-16 17:21:36 +00002110 hextomem(gdbserver_state.mem_buf, gdb_ctx->params[0].data, len);
2111 g_byte_array_append(gdbserver_state.mem_buf, &zero, 1);
2112 qemu_chr_be_write(gdbserver_state.mon_chr, gdbserver_state.mem_buf->data,
2113 gdbserver_state.mem_buf->len);
Alex Bennéea346af32020-03-16 17:21:34 +00002114 put_packet("OK");
Jon Doron2704efa2019-05-29 09:41:45 +03002115}
2116#endif
2117
2118static void handle_query_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2119{
Andreas Färber5b24c642013-07-07 15:08:22 +02002120 CPUClass *cc;
Jon Doron2704efa2019-05-29 09:41:45 +03002121
Alex Bennée308f9e82020-03-16 17:21:35 +00002122 g_string_printf(gdbserver_state.str_buf, "PacketSize=%x", MAX_PACKET_LENGTH);
Jon Doron2704efa2019-05-29 09:41:45 +03002123 cc = CPU_GET_CLASS(first_cpu);
2124 if (cc->gdb_core_xml_file) {
Alex Bennée308f9e82020-03-16 17:21:35 +00002125 g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+");
Jon Doron2704efa2019-05-29 09:41:45 +03002126 }
2127
2128 if (gdb_ctx->num_params &&
2129 strstr(gdb_ctx->params[0].data, "multiprocess+")) {
Alex Bennéea346af32020-03-16 17:21:34 +00002130 gdbserver_state.multiprocess = true;
Jon Doron2704efa2019-05-29 09:41:45 +03002131 }
2132
Changbin Du3bc26092020-03-16 17:21:55 +00002133 g_string_append(gdbserver_state.str_buf, ";vContSupported+;multiprocess+");
Alex Bennée308f9e82020-03-16 17:21:35 +00002134 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002135}
2136
2137static void handle_query_xfer_features(GdbCmdContext *gdb_ctx, void *user_ctx)
2138{
2139 GDBProcess *process;
2140 CPUClass *cc;
2141 unsigned long len, total_len, addr;
2142 const char *xml;
bellardb4608c02003-06-27 17:34:32 +00002143 const char *p;
Jon Doron2704efa2019-05-29 09:41:45 +03002144
2145 if (gdb_ctx->num_params < 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00002146 put_packet("E22");
Jon Doron2704efa2019-05-29 09:41:45 +03002147 return;
2148 }
2149
Alex Bennéea346af32020-03-16 17:21:34 +00002150 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
2151 cc = CPU_GET_CLASS(gdbserver_state.g_cpu);
Jon Doron2704efa2019-05-29 09:41:45 +03002152 if (!cc->gdb_core_xml_file) {
Alex Bennéea346af32020-03-16 17:21:34 +00002153 put_packet("");
Jon Doron2704efa2019-05-29 09:41:45 +03002154 return;
2155 }
2156
2157 gdb_has_xml = true;
2158 p = gdb_ctx->params[0].data;
Alex Bennéea346af32020-03-16 17:21:34 +00002159 xml = get_feature_xml(p, &p, process);
Jon Doron2704efa2019-05-29 09:41:45 +03002160 if (!xml) {
Alex Bennéea346af32020-03-16 17:21:34 +00002161 put_packet("E00");
Jon Doron2704efa2019-05-29 09:41:45 +03002162 return;
2163 }
2164
2165 addr = gdb_ctx->params[1].val_ul;
2166 len = gdb_ctx->params[2].val_ul;
2167 total_len = strlen(xml);
2168 if (addr > total_len) {
Alex Bennéea346af32020-03-16 17:21:34 +00002169 put_packet("E00");
Jon Doron2704efa2019-05-29 09:41:45 +03002170 return;
2171 }
2172
2173 if (len > (MAX_PACKET_LENGTH - 5) / 2) {
2174 len = (MAX_PACKET_LENGTH - 5) / 2;
2175 }
2176
2177 if (len < total_len - addr) {
Alex Bennée308f9e82020-03-16 17:21:35 +00002178 g_string_assign(gdbserver_state.str_buf, "m");
2179 memtox(gdbserver_state.str_buf, xml + addr, len);
Jon Doron2704efa2019-05-29 09:41:45 +03002180 } else {
Alex Bennée308f9e82020-03-16 17:21:35 +00002181 g_string_assign(gdbserver_state.str_buf, "l");
2182 memtox(gdbserver_state.str_buf, xml + addr, total_len - addr);
Jon Doron2704efa2019-05-29 09:41:45 +03002183 }
2184
Alex Bennée308f9e82020-03-16 17:21:35 +00002185 put_packet_binary(gdbserver_state.str_buf->str,
2186 gdbserver_state.str_buf->len, true);
Jon Doron2704efa2019-05-29 09:41:45 +03002187}
2188
2189static void handle_query_attached(GdbCmdContext *gdb_ctx, void *user_ctx)
2190{
Alex Bennéea346af32020-03-16 17:21:34 +00002191 put_packet(GDB_ATTACHED);
Jon Doron2704efa2019-05-29 09:41:45 +03002192}
2193
2194static void handle_query_qemu_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2195{
Alex Bennée308f9e82020-03-16 17:21:35 +00002196 g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep");
Jon Doronab4752e2019-05-29 09:41:48 +03002197#ifndef CONFIG_USER_ONLY
Alex Bennée308f9e82020-03-16 17:21:35 +00002198 g_string_append(gdbserver_state.str_buf, ";PhyMemMode");
Jon Doronab4752e2019-05-29 09:41:48 +03002199#endif
Alex Bennée308f9e82020-03-16 17:21:35 +00002200 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002201}
2202
Jon Doronab4752e2019-05-29 09:41:48 +03002203#ifndef CONFIG_USER_ONLY
2204static void handle_query_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx,
2205 void *user_ctx)
2206{
Alex Bennée308f9e82020-03-16 17:21:35 +00002207 g_string_printf(gdbserver_state.str_buf, "%d", phy_memory_mode);
2208 put_strbuf();
Jon Doronab4752e2019-05-29 09:41:48 +03002209}
2210
2211static void handle_set_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx, void *user_ctx)
2212{
2213 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00002214 put_packet("E22");
Jon Doronab4752e2019-05-29 09:41:48 +03002215 return;
2216 }
2217
2218 if (!gdb_ctx->params[0].val_ul) {
2219 phy_memory_mode = 0;
2220 } else {
2221 phy_memory_mode = 1;
2222 }
Alex Bennéea346af32020-03-16 17:21:34 +00002223 put_packet("OK");
Jon Doronab4752e2019-05-29 09:41:48 +03002224}
2225#endif
2226
Jon Doron2704efa2019-05-29 09:41:45 +03002227static GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
2228 /* Order is important if has same prefix */
2229 {
2230 .handler = handle_query_qemu_sstepbits,
2231 .cmd = "qemu.sstepbits",
2232 },
2233 {
2234 .handler = handle_query_qemu_sstep,
2235 .cmd = "qemu.sstep",
2236 },
2237 {
2238 .handler = handle_set_qemu_sstep,
2239 .cmd = "qemu.sstep=",
2240 .cmd_startswith = 1,
2241 .schema = "l0"
2242 },
2243};
2244
2245static GdbCmdParseEntry gdb_gen_query_table[] = {
2246 {
2247 .handler = handle_query_curr_tid,
2248 .cmd = "C",
2249 },
2250 {
2251 .handler = handle_query_threads,
2252 .cmd = "sThreadInfo",
2253 },
2254 {
2255 .handler = handle_query_first_threads,
2256 .cmd = "fThreadInfo",
2257 },
2258 {
2259 .handler = handle_query_thread_extra,
2260 .cmd = "ThreadExtraInfo,",
2261 .cmd_startswith = 1,
2262 .schema = "t0"
2263 },
2264#ifdef CONFIG_USER_ONLY
2265 {
2266 .handler = handle_query_offsets,
2267 .cmd = "Offsets",
2268 },
2269#else
2270 {
2271 .handler = handle_query_rcmd,
2272 .cmd = "Rcmd,",
2273 .cmd_startswith = 1,
2274 .schema = "s0"
2275 },
2276#endif
2277 {
2278 .handler = handle_query_supported,
2279 .cmd = "Supported:",
2280 .cmd_startswith = 1,
2281 .schema = "s0"
2282 },
2283 {
2284 .handler = handle_query_supported,
2285 .cmd = "Supported",
2286 .schema = "s0"
2287 },
2288 {
2289 .handler = handle_query_xfer_features,
2290 .cmd = "Xfer:features:read:",
2291 .cmd_startswith = 1,
2292 .schema = "s:l,l0"
2293 },
2294 {
2295 .handler = handle_query_attached,
2296 .cmd = "Attached:",
2297 .cmd_startswith = 1
2298 },
2299 {
2300 .handler = handle_query_attached,
2301 .cmd = "Attached",
2302 },
2303 {
2304 .handler = handle_query_qemu_supported,
2305 .cmd = "qemu.Supported",
2306 },
Jon Doronab4752e2019-05-29 09:41:48 +03002307#ifndef CONFIG_USER_ONLY
2308 {
2309 .handler = handle_query_qemu_phy_mem_mode,
2310 .cmd = "qemu.PhyMemMode",
2311 },
2312#endif
Jon Doron2704efa2019-05-29 09:41:45 +03002313};
2314
2315static GdbCmdParseEntry gdb_gen_set_table[] = {
2316 /* Order is important if has same prefix */
2317 {
2318 .handler = handle_set_qemu_sstep,
2319 .cmd = "qemu.sstep:",
2320 .cmd_startswith = 1,
2321 .schema = "l0"
2322 },
Jon Doronab4752e2019-05-29 09:41:48 +03002323#ifndef CONFIG_USER_ONLY
2324 {
2325 .handler = handle_set_qemu_phy_mem_mode,
2326 .cmd = "qemu.PhyMemMode:",
2327 .cmd_startswith = 1,
2328 .schema = "l0"
2329 },
2330#endif
Jon Doron2704efa2019-05-29 09:41:45 +03002331};
2332
2333static void handle_gen_query(GdbCmdContext *gdb_ctx, void *user_ctx)
2334{
2335 if (!gdb_ctx->num_params) {
2336 return;
2337 }
2338
Alex Bennéea346af32020-03-16 17:21:34 +00002339 if (!process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron2704efa2019-05-29 09:41:45 +03002340 gdb_gen_query_set_common_table,
2341 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2342 return;
2343 }
2344
Alex Bennéea346af32020-03-16 17:21:34 +00002345 if (process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron2704efa2019-05-29 09:41:45 +03002346 gdb_gen_query_table,
2347 ARRAY_SIZE(gdb_gen_query_table))) {
Alex Bennéea346af32020-03-16 17:21:34 +00002348 put_packet("");
Jon Doron2704efa2019-05-29 09:41:45 +03002349 }
2350}
2351
2352static void handle_gen_set(GdbCmdContext *gdb_ctx, void *user_ctx)
2353{
2354 if (!gdb_ctx->num_params) {
2355 return;
2356 }
2357
Alex Bennéea346af32020-03-16 17:21:34 +00002358 if (!process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron2704efa2019-05-29 09:41:45 +03002359 gdb_gen_query_set_common_table,
2360 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2361 return;
2362 }
2363
Alex Bennéea346af32020-03-16 17:21:34 +00002364 if (process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron2704efa2019-05-29 09:41:45 +03002365 gdb_gen_set_table,
2366 ARRAY_SIZE(gdb_gen_set_table))) {
Alex Bennéea346af32020-03-16 17:21:34 +00002367 put_packet("");
Jon Doron2704efa2019-05-29 09:41:45 +03002368 }
2369}
2370
Jon Doron7009d572019-05-29 09:41:46 +03002371static void handle_target_halt(GdbCmdContext *gdb_ctx, void *user_ctx)
2372{
Alex Bennée308f9e82020-03-16 17:21:35 +00002373 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
2374 gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf);
2375 g_string_append_c(gdbserver_state.str_buf, ';');
2376 put_strbuf();
Jon Doron7009d572019-05-29 09:41:46 +03002377 /*
2378 * Remove all the breakpoints when this query is issued,
2379 * because gdb is doing an initial connect and the state
2380 * should be cleaned up.
2381 */
2382 gdb_breakpoint_remove_all();
2383}
2384
Alex Bennéea346af32020-03-16 17:21:34 +00002385static int gdb_handle_packet(const char *line_buf)
Jon Doron2704efa2019-05-29 09:41:45 +03002386{
Jon Doron3e2c1262019-05-29 09:41:30 +03002387 const GdbCmdParseEntry *cmd_parser = NULL;
ths3b46e622007-09-17 08:09:54 +00002388
Doug Gale5c9522b2017-12-02 20:30:37 -05002389 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01002390
Jon Doron3f1cbac2019-05-29 09:41:47 +03002391 switch (line_buf[0]) {
Luc Michel53fd6552019-01-07 15:23:46 +00002392 case '!':
Alex Bennéea346af32020-03-16 17:21:34 +00002393 put_packet("OK");
Luc Michel53fd6552019-01-07 15:23:46 +00002394 break;
bellard858693c2004-03-31 18:52:07 +00002395 case '?':
Jon Doron7009d572019-05-29 09:41:46 +03002396 {
2397 static const GdbCmdParseEntry target_halted_cmd_desc = {
2398 .handler = handle_target_halt,
2399 .cmd = "?",
2400 .cmd_startswith = 1
2401 };
2402 cmd_parser = &target_halted_cmd_desc;
2403 }
bellard858693c2004-03-31 18:52:07 +00002404 break;
2405 case 'c':
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002406 {
2407 static const GdbCmdParseEntry continue_cmd_desc = {
2408 .handler = handle_continue,
2409 .cmd = "c",
2410 .cmd_startswith = 1,
2411 .schema = "L0"
2412 };
2413 cmd_parser = &continue_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002414 }
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002415 break;
edgar_igl1f487ee2008-05-17 22:20:53 +00002416 case 'C':
Jon Doronccc47d52019-05-29 09:41:33 +03002417 {
2418 static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
2419 .handler = handle_cont_with_sig,
2420 .cmd = "C",
2421 .cmd_startswith = 1,
2422 .schema = "l0"
2423 };
2424 cmd_parser = &cont_with_sig_cmd_desc;
2425 }
2426 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002427 case 'v':
Jon Doron8536ec02019-05-29 09:41:44 +03002428 {
2429 static const GdbCmdParseEntry v_cmd_desc = {
2430 .handler = handle_v_commands,
2431 .cmd = "v",
2432 .cmd_startswith = 1,
2433 .schema = "s0"
2434 };
2435 cmd_parser = &v_cmd_desc;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002436 }
Jon Doron8536ec02019-05-29 09:41:44 +03002437 break;
edgar_igl7d03f822008-05-17 18:58:29 +00002438 case 'k':
2439 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002440 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00002441 exit(0);
2442 case 'D':
Jon Doron3e2c1262019-05-29 09:41:30 +03002443 {
2444 static const GdbCmdParseEntry detach_cmd_desc = {
2445 .handler = handle_detach,
2446 .cmd = "D",
2447 .cmd_startswith = 1,
2448 .schema = "?.l0"
2449 };
2450 cmd_parser = &detach_cmd_desc;
Luc Michel546f3c62019-01-07 15:23:46 +00002451 }
edgar_igl7d03f822008-05-17 18:58:29 +00002452 break;
bellard858693c2004-03-31 18:52:07 +00002453 case 's':
Jon Doron933f80d2019-05-29 09:41:43 +03002454 {
2455 static const GdbCmdParseEntry step_cmd_desc = {
2456 .handler = handle_step,
2457 .cmd = "s",
2458 .cmd_startswith = 1,
2459 .schema = "L0"
2460 };
2461 cmd_parser = &step_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002462 }
Jon Doron933f80d2019-05-29 09:41:43 +03002463 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002464 case 'F':
2465 {
Jon Doron4b20fab2019-05-29 09:41:42 +03002466 static const GdbCmdParseEntry file_io_cmd_desc = {
2467 .handler = handle_file_io,
2468 .cmd = "F",
2469 .cmd_startswith = 1,
2470 .schema = "L,L,o0"
2471 };
2472 cmd_parser = &file_io_cmd_desc;
pbrooka2d1eba2007-01-28 03:10:55 +00002473 }
2474 break;
bellard858693c2004-03-31 18:52:07 +00002475 case 'g':
Jon Doron397d1372019-05-29 09:41:41 +03002476 {
2477 static const GdbCmdParseEntry read_all_regs_cmd_desc = {
2478 .handler = handle_read_all_regs,
2479 .cmd = "g",
2480 .cmd_startswith = 1
2481 };
2482 cmd_parser = &read_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002483 }
bellard858693c2004-03-31 18:52:07 +00002484 break;
2485 case 'G':
Jon Doron287ca122019-05-29 09:41:40 +03002486 {
2487 static const GdbCmdParseEntry write_all_regs_cmd_desc = {
2488 .handler = handle_write_all_regs,
2489 .cmd = "G",
2490 .cmd_startswith = 1,
2491 .schema = "s0"
2492 };
2493 cmd_parser = &write_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002494 }
bellard858693c2004-03-31 18:52:07 +00002495 break;
2496 case 'm':
Jon Doronda92e232019-05-29 09:41:39 +03002497 {
2498 static const GdbCmdParseEntry read_mem_cmd_desc = {
2499 .handler = handle_read_mem,
2500 .cmd = "m",
2501 .cmd_startswith = 1,
2502 .schema = "L,L0"
2503 };
2504 cmd_parser = &read_mem_cmd_desc;
bellard6f970bd2005-12-05 19:55:19 +00002505 }
bellard858693c2004-03-31 18:52:07 +00002506 break;
2507 case 'M':
Jon Doroncc0ecc72019-05-29 09:41:38 +03002508 {
2509 static const GdbCmdParseEntry write_mem_cmd_desc = {
2510 .handler = handle_write_mem,
2511 .cmd = "M",
2512 .cmd_startswith = 1,
2513 .schema = "L,L:s0"
2514 };
2515 cmd_parser = &write_mem_cmd_desc;
Fabien Chouteau44520db2011-09-08 12:48:16 +02002516 }
bellard858693c2004-03-31 18:52:07 +00002517 break;
pbrook56aebc82008-10-11 17:55:29 +00002518 case 'p':
Jon Doron5d0e57b2019-05-29 09:41:37 +03002519 {
2520 static const GdbCmdParseEntry get_reg_cmd_desc = {
2521 .handler = handle_get_reg,
2522 .cmd = "p",
2523 .cmd_startswith = 1,
2524 .schema = "L0"
2525 };
2526 cmd_parser = &get_reg_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002527 }
2528 break;
2529 case 'P':
Jon Doron62b33202019-05-29 09:41:36 +03002530 {
2531 static const GdbCmdParseEntry set_reg_cmd_desc = {
2532 .handler = handle_set_reg,
2533 .cmd = "P",
2534 .cmd_startswith = 1,
2535 .schema = "L?s0"
2536 };
2537 cmd_parser = &set_reg_cmd_desc;
2538 }
pbrook56aebc82008-10-11 17:55:29 +00002539 break;
bellard858693c2004-03-31 18:52:07 +00002540 case 'Z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002541 {
2542 static const GdbCmdParseEntry insert_bp_cmd_desc = {
2543 .handler = handle_insert_bp,
2544 .cmd = "Z",
2545 .cmd_startswith = 1,
2546 .schema = "l?L?L0"
2547 };
2548 cmd_parser = &insert_bp_cmd_desc;
2549 }
2550 break;
bellard858693c2004-03-31 18:52:07 +00002551 case 'z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002552 {
2553 static const GdbCmdParseEntry remove_bp_cmd_desc = {
2554 .handler = handle_remove_bp,
2555 .cmd = "z",
2556 .cmd_startswith = 1,
2557 .schema = "l?L?L0"
2558 };
2559 cmd_parser = &remove_bp_cmd_desc;
2560 }
bellard858693c2004-03-31 18:52:07 +00002561 break;
aliguori880a7572008-11-18 20:30:24 +00002562 case 'H':
Jon Doron3a9651d2019-05-29 09:41:34 +03002563 {
2564 static const GdbCmdParseEntry set_thread_cmd_desc = {
2565 .handler = handle_set_thread,
2566 .cmd = "H",
2567 .cmd_startswith = 1,
2568 .schema = "o.t0"
2569 };
2570 cmd_parser = &set_thread_cmd_desc;
aliguori880a7572008-11-18 20:30:24 +00002571 }
2572 break;
2573 case 'T':
Jon Doron44ffded2019-05-29 09:41:31 +03002574 {
2575 static const GdbCmdParseEntry thread_alive_cmd_desc = {
2576 .handler = handle_thread_alive,
2577 .cmd = "T",
2578 .cmd_startswith = 1,
2579 .schema = "t0"
2580 };
2581 cmd_parser = &thread_alive_cmd_desc;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07002582 }
aliguori880a7572008-11-18 20:30:24 +00002583 break;
pbrook978efd62006-06-17 18:30:42 +00002584 case 'q':
Jon Doron2704efa2019-05-29 09:41:45 +03002585 {
2586 static const GdbCmdParseEntry gen_query_cmd_desc = {
2587 .handler = handle_gen_query,
2588 .cmd = "q",
2589 .cmd_startswith = 1,
2590 .schema = "s0"
2591 };
2592 cmd_parser = &gen_query_cmd_desc;
2593 }
2594 break;
edgar_igl60897d32008-05-09 08:25:14 +00002595 case 'Q':
Jon Doron2704efa2019-05-29 09:41:45 +03002596 {
2597 static const GdbCmdParseEntry gen_set_cmd_desc = {
2598 .handler = handle_gen_set,
2599 .cmd = "Q",
2600 .cmd_startswith = 1,
2601 .schema = "s0"
2602 };
2603 cmd_parser = &gen_set_cmd_desc;
edgar_igl60897d32008-05-09 08:25:14 +00002604 }
Jon Doron2704efa2019-05-29 09:41:45 +03002605 break;
bellard858693c2004-03-31 18:52:07 +00002606 default:
bellard858693c2004-03-31 18:52:07 +00002607 /* put empty packet */
Alex Bennéea346af32020-03-16 17:21:34 +00002608 put_packet("");
bellard858693c2004-03-31 18:52:07 +00002609 break;
2610 }
Jon Doron3e2c1262019-05-29 09:41:30 +03002611
Ramiro Polla2bdec392019-08-05 21:09:01 +02002612 if (cmd_parser) {
Alex Bennéea346af32020-03-16 17:21:34 +00002613 run_cmd_parser(line_buf, cmd_parser);
Ramiro Polla2bdec392019-08-05 21:09:01 +02002614 }
Jon Doron3e2c1262019-05-29 09:41:30 +03002615
bellard858693c2004-03-31 18:52:07 +00002616 return RS_IDLE;
2617}
2618
Andreas Färber64f6b342013-05-27 02:06:09 +02002619void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00002620{
Alex Bennéea346af32020-03-16 17:21:34 +00002621 GDBProcess *p = gdb_get_cpu_process(cpu);
Luc Michel160d8582019-01-07 15:23:46 +00002622
2623 if (!p->attached) {
2624 /*
2625 * Having a stop CPU corresponding to a process that is not attached
2626 * confuses GDB. So we ignore the request.
2627 */
2628 return;
2629 }
2630
Alex Bennée8d98c442020-03-16 17:21:33 +00002631 gdbserver_state.c_cpu = cpu;
2632 gdbserver_state.g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00002633}
2634
bellard1fddef42005-04-17 19:16:13 +00002635#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002636static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00002637{
Alex Bennéea346af32020-03-16 17:21:34 +00002638 CPUState *cpu = gdbserver_state.c_cpu;
Alex Bennée308f9e82020-03-16 17:21:35 +00002639 g_autoptr(GString) buf = g_string_new(NULL);
2640 g_autoptr(GString) tid = g_string_new(NULL);
aliguorid6fc1b32008-11-18 19:55:44 +00002641 const char *type;
bellard858693c2004-03-31 18:52:07 +00002642 int ret;
2643
Alex Bennéea346af32020-03-16 17:21:34 +00002644 if (running || gdbserver_state.state == RS_INACTIVE) {
Meador Ingecdb432b2012-03-15 17:49:45 +00002645 return;
2646 }
2647 /* Is there a GDB syscall waiting to be sent? */
Alex Bennéea346af32020-03-16 17:21:34 +00002648 if (gdbserver_state.current_syscall_cb) {
2649 put_packet(gdbserver_state.syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00002650 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01002651 }
Luc Michel95567c22019-01-07 15:23:46 +00002652
2653 if (cpu == NULL) {
2654 /* No process attached */
2655 return;
2656 }
2657
Alex Bennée308f9e82020-03-16 17:21:35 +00002658 gdb_append_thread_id(cpu, tid);
Luc Michel95567c22019-01-07 15:23:46 +00002659
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002660 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002661 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02002662 if (cpu->watchpoint_hit) {
2663 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00002664 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00002665 type = "r";
2666 break;
aliguoria1d1bb32008-11-18 20:07:32 +00002667 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00002668 type = "a";
2669 break;
2670 default:
2671 type = "";
2672 break;
2673 }
Doug Gale5c9522b2017-12-02 20:30:37 -05002674 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
2675 (target_ulong)cpu->watchpoint_hit->vaddr);
Alex Bennée308f9e82020-03-16 17:21:35 +00002676 g_string_printf(buf, "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
2677 GDB_SIGNAL_TRAP, tid->str, type,
2678 (target_ulong)cpu->watchpoint_hit->vaddr);
Andreas Färberff4700b2013-08-26 18:23:18 +02002679 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01002680 goto send_packet;
Doug Gale5c9522b2017-12-02 20:30:37 -05002681 } else {
2682 trace_gdbstub_hit_break();
pbrook6658ffb2007-03-16 23:58:11 +00002683 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07002684 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00002685 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01002686 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002687 case RUN_STATE_PAUSED:
Doug Gale5c9522b2017-12-02 20:30:37 -05002688 trace_gdbstub_hit_paused();
aliguori9781e042009-01-22 17:15:29 +00002689 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01002690 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002691 case RUN_STATE_SHUTDOWN:
Doug Gale5c9522b2017-12-02 20:30:37 -05002692 trace_gdbstub_hit_shutdown();
Jan Kiszka425189a2011-03-22 11:02:09 +01002693 ret = GDB_SIGNAL_QUIT;
2694 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002695 case RUN_STATE_IO_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002696 trace_gdbstub_hit_io_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002697 ret = GDB_SIGNAL_IO;
2698 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002699 case RUN_STATE_WATCHDOG:
Doug Gale5c9522b2017-12-02 20:30:37 -05002700 trace_gdbstub_hit_watchdog();
Jan Kiszka425189a2011-03-22 11:02:09 +01002701 ret = GDB_SIGNAL_ALRM;
2702 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002703 case RUN_STATE_INTERNAL_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002704 trace_gdbstub_hit_internal_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002705 ret = GDB_SIGNAL_ABRT;
2706 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002707 case RUN_STATE_SAVE_VM:
2708 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01002709 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002710 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01002711 ret = GDB_SIGNAL_XCPU;
2712 break;
2713 default:
Doug Gale5c9522b2017-12-02 20:30:37 -05002714 trace_gdbstub_hit_unknown(state);
Jan Kiszka425189a2011-03-22 11:02:09 +01002715 ret = GDB_SIGNAL_UNKNOWN;
2716 break;
bellardbbeb7b52006-04-23 18:42:15 +00002717 }
Jan Kiszka226d0072015-07-24 18:52:31 +02002718 gdb_set_stop_cpu(cpu);
Alex Bennée308f9e82020-03-16 17:21:35 +00002719 g_string_printf(buf, "T%02xthread:%s;", ret, tid->str);
Jan Kiszka425189a2011-03-22 11:02:09 +01002720
2721send_packet:
Alex Bennée308f9e82020-03-16 17:21:35 +00002722 put_packet(buf->str);
Jan Kiszka425189a2011-03-22 11:02:09 +01002723
2724 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02002725 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00002726}
bellard1fddef42005-04-17 19:16:13 +00002727#endif
bellard858693c2004-03-31 18:52:07 +00002728
pbrooka2d1eba2007-01-28 03:10:55 +00002729/* Send a gdb syscall request.
2730 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00002731 %x - target_ulong argument printed in hex.
2732 %lx - 64-bit argument printed in hex.
2733 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01002734void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00002735{
pbrooka2d1eba2007-01-28 03:10:55 +00002736 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00002737 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00002738 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00002739 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00002740
Alex Bennéea346af32020-03-16 17:21:34 +00002741 if (!gdbserver_state.init) {
pbrooka2d1eba2007-01-28 03:10:55 +00002742 return;
Alex Bennéea346af32020-03-16 17:21:34 +00002743 }
Alex Bennée8d98c442020-03-16 17:21:33 +00002744
2745 gdbserver_state.current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00002746#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002747 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00002748#endif
Alex Bennée8d98c442020-03-16 17:21:33 +00002749 p = &gdbserver_state.syscall_buf[0];
2750 p_end = &gdbserver_state.syscall_buf[sizeof(gdbserver_state.syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00002751 *(p++) = 'F';
2752 while (*fmt) {
2753 if (*fmt == '%') {
2754 fmt++;
2755 switch (*fmt++) {
2756 case 'x':
2757 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002758 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00002759 break;
pbrooka87295e2007-05-26 15:09:38 +00002760 case 'l':
2761 if (*(fmt++) != 'x')
2762 goto bad_format;
2763 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00002764 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00002765 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002766 case 's':
2767 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002768 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00002769 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00002770 break;
2771 default:
pbrooka87295e2007-05-26 15:09:38 +00002772 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002773 error_report("gdbstub: Bad syscall format string '%s'",
2774 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00002775 break;
2776 }
2777 } else {
2778 *(p++) = *(fmt++);
2779 }
2780 }
pbrook8a93e022007-08-06 13:19:15 +00002781 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00002782#ifdef CONFIG_USER_ONLY
Alex Bennéea346af32020-03-16 17:21:34 +00002783 put_packet(gdbserver_state.syscall_buf);
Peter Maydell4f710862018-05-15 19:19:58 +01002784 /* Return control to gdb for it to process the syscall request.
2785 * Since the protocol requires that gdb hands control back to us
2786 * using a "here are the results" F packet, we don't need to check
2787 * gdb_handlesig's return value (which is the signal to deliver if
2788 * execution was resumed via a continue packet).
2789 */
Alex Bennée8d98c442020-03-16 17:21:33 +00002790 gdb_handlesig(gdbserver_state.c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00002791#else
Meador Ingecdb432b2012-03-15 17:49:45 +00002792 /* In this case wait to send the syscall packet until notification that
2793 the CPU has stopped. This must be done because if the packet is sent
2794 now the reply from the syscall request could be received while the CPU
2795 is still in the running state, which can cause packets to be dropped
2796 and state transition 'T' packets to be sent while the syscall is still
2797 being processed. */
Alex Bennée8d98c442020-03-16 17:21:33 +00002798 qemu_cpu_kick(gdbserver_state.c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00002799#endif
2800}
2801
Peter Maydell19239b32015-09-07 10:39:27 +01002802void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
2803{
2804 va_list va;
2805
2806 va_start(va, fmt);
2807 gdb_do_syscallv(cb, fmt, va);
2808 va_end(va);
2809}
2810
Alex Bennéea346af32020-03-16 17:21:34 +00002811static void gdb_read_byte(uint8_t ch)
bellard858693c2004-03-31 18:52:07 +00002812{
ths60fe76f2007-12-16 03:02:09 +00002813 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00002814
bellard1fddef42005-04-17 19:16:13 +00002815#ifndef CONFIG_USER_ONLY
Damien Hedded116e812020-03-16 17:21:53 +00002816 if (gdbserver_state.last_packet->len) {
pbrook4046d912007-01-28 01:53:16 +00002817 /* Waiting for a response to the last packet. If we see the start
2818 of a new command then abandon the previous response. */
2819 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002820 trace_gdbstub_err_got_nack();
Damien Hedded116e812020-03-16 17:21:53 +00002821 put_buffer(gdbserver_state.last_packet->data,
2822 gdbserver_state.last_packet->len);
Alex Bennée118e2262017-07-12 11:52:13 +01002823 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002824 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01002825 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002826 trace_gdbstub_io_got_unexpected(ch);
pbrook4046d912007-01-28 01:53:16 +00002827 }
Alex Bennée118e2262017-07-12 11:52:13 +01002828
Damien Hedded116e812020-03-16 17:21:53 +00002829 if (ch == '+' || ch == '$') {
2830 g_byte_array_set_size(gdbserver_state.last_packet, 0);
2831 }
pbrook4046d912007-01-28 01:53:16 +00002832 if (ch != '$')
2833 return;
2834 }
Luiz Capitulino13548692011-07-29 15:36:43 -03002835 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00002836 /* when the CPU is running, we cannot do anything except stop
2837 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002838 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00002839 } else
bellard1fddef42005-04-17 19:16:13 +00002840#endif
bellard41625032005-04-24 10:07:11 +00002841 {
Alex Bennéea346af32020-03-16 17:21:34 +00002842 switch(gdbserver_state.state) {
bellard858693c2004-03-31 18:52:07 +00002843 case RS_IDLE:
2844 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04002845 /* start of command packet */
Alex Bennéea346af32020-03-16 17:21:34 +00002846 gdbserver_state.line_buf_index = 0;
2847 gdbserver_state.line_sum = 0;
2848 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002849 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002850 trace_gdbstub_err_garbage(ch);
bellard4c3a88a2003-07-26 12:06:08 +00002851 }
2852 break;
bellard858693c2004-03-31 18:52:07 +00002853 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04002854 if (ch == '}') {
2855 /* start escape sequence */
Alex Bennéea346af32020-03-16 17:21:34 +00002856 gdbserver_state.state = RS_GETLINE_ESC;
2857 gdbserver_state.line_sum += ch;
Doug Gale4bf43122017-05-01 12:22:10 -04002858 } else if (ch == '*') {
2859 /* start run length encoding sequence */
Alex Bennéea346af32020-03-16 17:21:34 +00002860 gdbserver_state.state = RS_GETLINE_RLE;
2861 gdbserver_state.line_sum += ch;
Doug Gale4bf43122017-05-01 12:22:10 -04002862 } else if (ch == '#') {
2863 /* end of command, start of checksum*/
Alex Bennéea346af32020-03-16 17:21:34 +00002864 gdbserver_state.state = RS_CHKSUM1;
2865 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002866 trace_gdbstub_err_overrun();
Alex Bennéea346af32020-03-16 17:21:34 +00002867 gdbserver_state.state = RS_IDLE;
bellard858693c2004-03-31 18:52:07 +00002868 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002869 /* unescaped command character */
Alex Bennéea346af32020-03-16 17:21:34 +00002870 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch;
2871 gdbserver_state.line_sum += ch;
Doug Gale4bf43122017-05-01 12:22:10 -04002872 }
2873 break;
2874 case RS_GETLINE_ESC:
2875 if (ch == '#') {
2876 /* unexpected end of command in escape sequence */
Alex Bennéea346af32020-03-16 17:21:34 +00002877 gdbserver_state.state = RS_CHKSUM1;
2878 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04002879 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05002880 trace_gdbstub_err_overrun();
Alex Bennéea346af32020-03-16 17:21:34 +00002881 gdbserver_state.state = RS_IDLE;
Doug Gale4bf43122017-05-01 12:22:10 -04002882 } else {
2883 /* parse escaped character and leave escape state */
Alex Bennéea346af32020-03-16 17:21:34 +00002884 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20;
2885 gdbserver_state.line_sum += ch;
2886 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002887 }
2888 break;
2889 case RS_GETLINE_RLE:
Markus Armbruster046aba12019-05-14 20:03:08 +02002890 /*
2891 * Run-length encoding is explained in "Debugging with GDB /
2892 * Appendix E GDB Remote Serial Protocol / Overview".
2893 */
2894 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
Doug Gale4bf43122017-05-01 12:22:10 -04002895 /* invalid RLE count encoding */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002896 trace_gdbstub_err_invalid_repeat(ch);
Alex Bennéea346af32020-03-16 17:21:34 +00002897 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002898 } else {
2899 /* decode repeat length */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002900 int repeat = ch - ' ' + 3;
Alex Bennéea346af32020-03-16 17:21:34 +00002901 if (gdbserver_state.line_buf_index + repeat >= sizeof(gdbserver_state.line_buf) - 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04002902 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05002903 trace_gdbstub_err_overrun();
Alex Bennéea346af32020-03-16 17:21:34 +00002904 gdbserver_state.state = RS_IDLE;
2905 } else if (gdbserver_state.line_buf_index < 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04002906 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05002907 trace_gdbstub_err_invalid_rle();
Alex Bennéea346af32020-03-16 17:21:34 +00002908 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002909 } else {
2910 /* repeat the last character */
Alex Bennéea346af32020-03-16 17:21:34 +00002911 memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index,
2912 gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat);
2913 gdbserver_state.line_buf_index += repeat;
2914 gdbserver_state.line_sum += ch;
2915 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002916 }
bellard858693c2004-03-31 18:52:07 +00002917 }
2918 break;
2919 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04002920 /* get high hex digit of checksum */
2921 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002922 trace_gdbstub_err_checksum_invalid(ch);
Alex Bennéea346af32020-03-16 17:21:34 +00002923 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002924 break;
2925 }
Alex Bennéea346af32020-03-16 17:21:34 +00002926 gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0';
2927 gdbserver_state.line_csum = fromhex(ch) << 4;
2928 gdbserver_state.state = RS_CHKSUM2;
bellard858693c2004-03-31 18:52:07 +00002929 break;
2930 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04002931 /* get low hex digit of checksum */
2932 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002933 trace_gdbstub_err_checksum_invalid(ch);
Alex Bennéea346af32020-03-16 17:21:34 +00002934 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002935 break;
bellard858693c2004-03-31 18:52:07 +00002936 }
Alex Bennéea346af32020-03-16 17:21:34 +00002937 gdbserver_state.line_csum |= fromhex(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002938
Alex Bennéea346af32020-03-16 17:21:34 +00002939 if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) {
2940 trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04002941 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00002942 reply = '-';
Alex Bennéea346af32020-03-16 17:21:34 +00002943 put_buffer(&reply, 1);
2944 gdbserver_state.state = RS_IDLE;
bellard858693c2004-03-31 18:52:07 +00002945 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002946 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00002947 reply = '+';
Alex Bennéea346af32020-03-16 17:21:34 +00002948 put_buffer(&reply, 1);
2949 gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf);
bellard858693c2004-03-31 18:52:07 +00002950 }
bellardb4608c02003-06-27 17:34:32 +00002951 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002952 default:
2953 abort();
bellardb4608c02003-06-27 17:34:32 +00002954 }
2955 }
bellard858693c2004-03-31 18:52:07 +00002956}
2957
Paul Brook0e1c9c52010-06-16 13:03:51 +01002958/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002959void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01002960{
Paul Brook0e1c9c52010-06-16 13:03:51 +01002961 char buf[4];
2962
Alex Bennée8d98c442020-03-16 17:21:33 +00002963 if (!gdbserver_state.init) {
Paul Brook0e1c9c52010-06-16 13:03:51 +01002964 return;
2965 }
2966#ifdef CONFIG_USER_ONLY
Alex Bennée8d98c442020-03-16 17:21:33 +00002967 if (gdbserver_fd < 0 || gdbserver_state.fd < 0) {
Paul Brook0e1c9c52010-06-16 13:03:51 +01002968 return;
2969 }
2970#endif
2971
Doug Gale5c9522b2017-12-02 20:30:37 -05002972 trace_gdbstub_op_exiting((uint8_t)code);
2973
Paul Brook0e1c9c52010-06-16 13:03:51 +01002974 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
Alex Bennéea346af32020-03-16 17:21:34 +00002975 put_packet(buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002976
2977#ifndef CONFIG_USER_ONLY
Alex Bennée8d98c442020-03-16 17:21:33 +00002978 qemu_chr_fe_deinit(&gdbserver_state.chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002979#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01002980}
2981
Luc Michel8f468632019-01-07 15:23:45 +00002982/*
2983 * Create the process that will contain all the "orphan" CPUs (that are not
2984 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2985 * be attachable and thus will be invisible to the user.
2986 */
2987static void create_default_process(GDBState *s)
2988{
2989 GDBProcess *process;
2990 int max_pid = 0;
2991
Alex Bennéea346af32020-03-16 17:21:34 +00002992 if (gdbserver_state.process_num) {
Luc Michel8f468632019-01-07 15:23:45 +00002993 max_pid = s->processes[s->process_num - 1].pid;
2994 }
2995
2996 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2997 process = &s->processes[s->process_num - 1];
2998
2999 /* We need an available PID slot for this process */
3000 assert(max_pid < UINT32_MAX);
3001
3002 process->pid = max_pid + 1;
3003 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00003004 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00003005}
3006
bellard1fddef42005-04-17 19:16:13 +00003007#ifdef CONFIG_USER_ONLY
3008int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02003009gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00003010{
Andreas Färber5ca666c2013-06-24 19:20:57 +02003011 char buf[256];
3012 int n;
bellard1fddef42005-04-17 19:16:13 +00003013
Alex Bennée8d98c442020-03-16 17:21:33 +00003014 if (gdbserver_fd < 0 || gdbserver_state.fd < 0) {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003015 return sig;
bellard1fddef42005-04-17 19:16:13 +00003016 }
3017
Andreas Färber5ca666c2013-06-24 19:20:57 +02003018 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02003019 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07003020 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00003021
Andreas Färber5ca666c2013-06-24 19:20:57 +02003022 if (sig != 0) {
3023 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
Alex Bennéea346af32020-03-16 17:21:34 +00003024 put_packet(buf);
Andreas Färber5ca666c2013-06-24 19:20:57 +02003025 }
3026 /* put_packet() might have detected that the peer terminated the
3027 connection. */
Alex Bennée8d98c442020-03-16 17:21:33 +00003028 if (gdbserver_state.fd < 0) {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003029 return sig;
3030 }
3031
3032 sig = 0;
Alex Bennée8d98c442020-03-16 17:21:33 +00003033 gdbserver_state.state = RS_IDLE;
3034 gdbserver_state.running_state = 0;
3035 while (gdbserver_state.running_state == 0) {
3036 n = read(gdbserver_state.fd, buf, 256);
Andreas Färber5ca666c2013-06-24 19:20:57 +02003037 if (n > 0) {
3038 int i;
3039
3040 for (i = 0; i < n; i++) {
Alex Bennéea346af32020-03-16 17:21:34 +00003041 gdb_read_byte(buf[i]);
Andreas Färber5ca666c2013-06-24 19:20:57 +02003042 }
Peter Wu5819e3e2016-06-05 16:35:48 +02003043 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003044 /* XXX: Connection closed. Should probably wait for another
3045 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02003046 if (n == 0) {
Alex Bennée8d98c442020-03-16 17:21:33 +00003047 close(gdbserver_state.fd);
Peter Wu5819e3e2016-06-05 16:35:48 +02003048 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003049 gdbserver_state.fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02003050 return sig;
bellard1fddef42005-04-17 19:16:13 +00003051 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02003052 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003053 sig = gdbserver_state.signal;
3054 gdbserver_state.signal = 0;
Andreas Färber5ca666c2013-06-24 19:20:57 +02003055 return sig;
bellard1fddef42005-04-17 19:16:13 +00003056}
bellarde9009672005-04-26 20:42:36 +00003057
aurel32ca587a82008-12-18 22:44:13 +00003058/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01003059void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00003060{
Andreas Färber5ca666c2013-06-24 19:20:57 +02003061 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00003062
Alex Bennée8d98c442020-03-16 17:21:33 +00003063 if (gdbserver_fd < 0 || gdbserver_state.fd < 0) {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003064 return;
3065 }
aurel32ca587a82008-12-18 22:44:13 +00003066
Andreas Färber5ca666c2013-06-24 19:20:57 +02003067 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
Alex Bennéea346af32020-03-16 17:21:34 +00003068 put_packet(buf);
aurel32ca587a82008-12-18 22:44:13 +00003069}
bellard1fddef42005-04-17 19:16:13 +00003070
Peter Maydell2f652222018-05-14 18:30:44 +01003071static bool gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00003072{
bellard858693c2004-03-31 18:52:07 +00003073 struct sockaddr_in sockaddr;
3074 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09003075 int fd;
bellard858693c2004-03-31 18:52:07 +00003076
3077 for(;;) {
3078 len = sizeof(sockaddr);
3079 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
3080 if (fd < 0 && errno != EINTR) {
3081 perror("accept");
Peter Maydell2f652222018-05-14 18:30:44 +01003082 return false;
bellard858693c2004-03-31 18:52:07 +00003083 } else if (fd >= 0) {
Peter Maydellf5bdd782018-05-14 18:30:43 +01003084 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00003085 break;
3086 }
3087 }
3088
3089 /* set short latency */
Peter Maydell2f652222018-05-14 18:30:44 +01003090 if (socket_set_nodelay(fd)) {
3091 perror("setsockopt");
Philippe Mathieu-Daudéead75d82018-05-24 19:34:58 -03003092 close(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01003093 return false;
3094 }
ths3b46e622007-09-17 08:09:54 +00003095
Alex Bennée8d98c442020-03-16 17:21:33 +00003096 init_gdbserver_state();
3097 create_default_process(&gdbserver_state);
3098 gdbserver_state.processes[0].attached = true;
Alex Bennéea346af32020-03-16 17:21:34 +00003099 gdbserver_state.c_cpu = gdb_first_attached_cpu();
Alex Bennée8d98c442020-03-16 17:21:33 +00003100 gdbserver_state.g_cpu = gdbserver_state.c_cpu;
3101 gdbserver_state.fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02003102 gdb_has_xml = false;
Peter Maydell2f652222018-05-14 18:30:44 +01003103 return true;
bellard858693c2004-03-31 18:52:07 +00003104}
3105
3106static int gdbserver_open(int port)
3107{
3108 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02003109 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00003110
3111 fd = socket(PF_INET, SOCK_STREAM, 0);
3112 if (fd < 0) {
3113 perror("socket");
3114 return -1;
3115 }
Peter Maydellf5bdd782018-05-14 18:30:43 +01003116 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00003117
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02003118 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00003119
3120 sockaddr.sin_family = AF_INET;
3121 sockaddr.sin_port = htons(port);
3122 sockaddr.sin_addr.s_addr = 0;
3123 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
3124 if (ret < 0) {
3125 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00003126 close(fd);
bellard858693c2004-03-31 18:52:07 +00003127 return -1;
3128 }
Peter Wu96165b92016-05-04 11:32:17 +02003129 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00003130 if (ret < 0) {
3131 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00003132 close(fd);
bellard858693c2004-03-31 18:52:07 +00003133 return -1;
3134 }
bellard858693c2004-03-31 18:52:07 +00003135 return fd;
3136}
3137
3138int gdbserver_start(int port)
3139{
3140 gdbserver_fd = gdbserver_open(port);
3141 if (gdbserver_fd < 0)
3142 return -1;
3143 /* accept connections */
Peter Maydell2f652222018-05-14 18:30:44 +01003144 if (!gdb_accept()) {
3145 close(gdbserver_fd);
3146 gdbserver_fd = -1;
3147 return -1;
3148 }
bellardb4608c02003-06-27 17:34:32 +00003149 return 0;
3150}
aurel322b1319c2008-12-18 22:44:04 +00003151
3152/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07003153void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00003154{
Alex Bennée8d98c442020-03-16 17:21:33 +00003155 if (gdbserver_fd < 0 || gdbserver_state.fd < 0) {
Andreas Färber75a34032013-09-02 16:57:02 +02003156 return;
3157 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003158 close(gdbserver_state.fd);
3159 gdbserver_state.fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02003160 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02003161 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00003162}
pbrook4046d912007-01-28 01:53:16 +00003163#else
thsaa1f17c2007-07-11 22:48:58 +00003164static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00003165{
pbrook56aebc82008-10-11 17:55:29 +00003166 /* We can handle an arbitrarily large amount of data.
3167 Pick the maximum packet size, which is as good as anything. */
3168 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00003169}
3170
thsaa1f17c2007-07-11 22:48:58 +00003171static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00003172{
pbrook4046d912007-01-28 01:53:16 +00003173 int i;
3174
3175 for (i = 0; i < size; i++) {
Alex Bennéea346af32020-03-16 17:21:34 +00003176 gdb_read_byte(buf[i]);
pbrook4046d912007-01-28 01:53:16 +00003177 }
3178}
3179
Philippe Mathieu-Daudé083b2662019-12-18 18:20:09 +01003180static void gdb_chr_event(void *opaque, QEMUChrEvent event)
pbrook4046d912007-01-28 01:53:16 +00003181{
Luc Michel970ed902019-01-07 15:23:46 +00003182 int i;
3183 GDBState *s = (GDBState *) opaque;
3184
pbrook4046d912007-01-28 01:53:16 +00003185 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05303186 case CHR_EVENT_OPENED:
Luc Michel970ed902019-01-07 15:23:46 +00003187 /* Start with first process attached, others detached */
3188 for (i = 0; i < s->process_num; i++) {
3189 s->processes[i].attached = !i;
3190 }
3191
Alex Bennéea346af32020-03-16 17:21:34 +00003192 s->c_cpu = gdb_first_attached_cpu();
Luc Michel970ed902019-01-07 15:23:46 +00003193 s->g_cpu = s->c_cpu;
3194
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003195 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02003196 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00003197 break;
3198 default:
3199 break;
3200 }
3201}
3202
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003203static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00003204{
Damien Hedded86b4672020-03-16 17:21:54 +00003205 g_autoptr(GString) hex_buf = g_string_new("O");
3206 memtohex(hex_buf, buf, len);
3207 put_packet(hex_buf->str);
aliguori8a34a0f2009-03-05 23:01:55 +00003208 return len;
3209}
3210
aliguori59030a82009-04-05 18:43:41 +00003211#ifndef _WIN32
3212static void gdb_sigterm_handler(int signal)
3213{
Luiz Capitulino13548692011-07-29 15:36:43 -03003214 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003215 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01003216 }
aliguori59030a82009-04-05 18:43:41 +00003217}
3218#endif
3219
Marc-André Lureau777357d2016-12-07 18:39:10 +03003220static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
3221 bool *be_opened, Error **errp)
3222{
3223 *be_opened = false;
3224}
3225
3226static void char_gdb_class_init(ObjectClass *oc, void *data)
3227{
3228 ChardevClass *cc = CHARDEV_CLASS(oc);
3229
3230 cc->internal = true;
3231 cc->open = gdb_monitor_open;
3232 cc->chr_write = gdb_monitor_write;
3233}
3234
3235#define TYPE_CHARDEV_GDB "chardev-gdb"
3236
3237static const TypeInfo char_gdb_type_info = {
3238 .name = TYPE_CHARDEV_GDB,
3239 .parent = TYPE_CHARDEV,
3240 .class_init = char_gdb_class_init,
3241};
3242
Luc Michel8f468632019-01-07 15:23:45 +00003243static int find_cpu_clusters(Object *child, void *opaque)
3244{
3245 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
3246 GDBState *s = (GDBState *) opaque;
3247 CPUClusterState *cluster = CPU_CLUSTER(child);
3248 GDBProcess *process;
3249
3250 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
3251
3252 process = &s->processes[s->process_num - 1];
3253
3254 /*
3255 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
3256 * runtime, we enforce here that the machine does not use a cluster ID
3257 * that would lead to PID 0.
3258 */
3259 assert(cluster->cluster_id != UINT32_MAX);
3260 process->pid = cluster->cluster_id + 1;
3261 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00003262 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00003263
3264 return 0;
3265 }
3266
3267 return object_child_foreach(child, find_cpu_clusters, opaque);
3268}
3269
3270static int pid_order(const void *a, const void *b)
3271{
3272 GDBProcess *pa = (GDBProcess *) a;
3273 GDBProcess *pb = (GDBProcess *) b;
3274
3275 if (pa->pid < pb->pid) {
3276 return -1;
3277 } else if (pa->pid > pb->pid) {
3278 return 1;
3279 } else {
3280 return 0;
3281 }
3282}
3283
3284static void create_processes(GDBState *s)
3285{
3286 object_child_foreach(object_get_root(), find_cpu_clusters, s);
3287
Alex Bennéea346af32020-03-16 17:21:34 +00003288 if (gdbserver_state.processes) {
Luc Michel8f468632019-01-07 15:23:45 +00003289 /* Sort by PID */
Alex Bennéea346af32020-03-16 17:21:34 +00003290 qsort(gdbserver_state.processes, gdbserver_state.process_num, sizeof(gdbserver_state.processes[0]), pid_order);
Luc Michel8f468632019-01-07 15:23:45 +00003291 }
3292
3293 create_default_process(s);
3294}
3295
aliguori59030a82009-04-05 18:43:41 +00003296int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00003297{
Doug Gale5c9522b2017-12-02 20:30:37 -05003298 trace_gdbstub_op_start(device);
3299
aliguori59030a82009-04-05 18:43:41 +00003300 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003301 Chardev *chr = NULL;
3302 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00003303
Ziyue Yang508b4ec2017-01-18 16:02:41 +08003304 if (!first_cpu) {
3305 error_report("gdbstub: meaningless to attach gdb to a "
3306 "machine without any CPU.");
3307 return -1;
3308 }
3309
aliguori59030a82009-04-05 18:43:41 +00003310 if (!device)
3311 return -1;
3312 if (strcmp(device, "none") != 0) {
3313 if (strstart(device, "tcp:", NULL)) {
3314 /* enforce required TCP attributes */
3315 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
3316 "%s,nowait,nodelay,server", device);
3317 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00003318 }
aliguori59030a82009-04-05 18:43:41 +00003319#ifndef _WIN32
3320 else if (strcmp(device, "stdio") == 0) {
3321 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00003322
aliguori59030a82009-04-05 18:43:41 +00003323 memset(&act, 0, sizeof(act));
3324 act.sa_handler = gdb_sigterm_handler;
3325 sigaction(SIGINT, &act, NULL);
3326 }
3327#endif
Marc-André Lureau95e30b22018-08-22 19:19:42 +02003328 /*
3329 * FIXME: it's a bit weird to allow using a mux chardev here
3330 * and implicitly setup a monitor. We may want to break this.
3331 */
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01003332 chr = qemu_chr_new_noreplay("gdb", device, true, NULL);
aliguori36556b22009-03-28 18:05:53 +00003333 if (!chr)
3334 return -1;
pbrookcfc34752007-02-22 01:48:01 +00003335 }
3336
Alex Bennée8d98c442020-03-16 17:21:33 +00003337 if (!gdbserver_state.init) {
3338 init_gdbserver_state();
pbrook4046d912007-01-28 01:53:16 +00003339
aliguori36556b22009-03-28 18:05:53 +00003340 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
3341
3342 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03003343 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01003344 NULL, NULL, &error_abort);
Kevin Wolf8e9119a2020-02-24 15:30:06 +01003345 monitor_init_hmp(mon_chr, false, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00003346 } else {
Alex Bennée8d98c442020-03-16 17:21:33 +00003347 qemu_chr_fe_deinit(&gdbserver_state.chr, true);
3348 mon_chr = gdbserver_state.mon_chr;
3349 reset_gdbserver_state();
aliguori36556b22009-03-28 18:05:53 +00003350 }
Luc Michel8f468632019-01-07 15:23:45 +00003351
Alex Bennée8d98c442020-03-16 17:21:33 +00003352 create_processes(&gdbserver_state);
Luc Michel8f468632019-01-07 15:23:45 +00003353
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03003354 if (chr) {
Alex Bennée8d98c442020-03-16 17:21:33 +00003355 qemu_chr_fe_init(&gdbserver_state.chr, chr, &error_abort);
3356 qemu_chr_fe_set_handlers(&gdbserver_state.chr, gdb_chr_can_receive,
3357 gdb_chr_receive, gdb_chr_event,
3358 NULL, &gdbserver_state, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03003359 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003360 gdbserver_state.state = chr ? RS_IDLE : RS_INACTIVE;
3361 gdbserver_state.mon_chr = mon_chr;
3362 gdbserver_state.current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00003363
pbrook4046d912007-01-28 01:53:16 +00003364 return 0;
3365}
Marc-André Lureau777357d2016-12-07 18:39:10 +03003366
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01003367void gdbserver_cleanup(void)
3368{
Alex Bennée8d98c442020-03-16 17:21:33 +00003369 if (gdbserver_state.init) {
Alex Bennéea346af32020-03-16 17:21:34 +00003370 put_packet("W00");
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01003371 }
3372}
3373
Marc-André Lureau777357d2016-12-07 18:39:10 +03003374static void register_types(void)
3375{
3376 type_register_static(&char_gdb_type_info);
3377}
3378
3379type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00003380#endif