blob: f6b42825445cf4e57c52d3f3e50d343feddb3c9c [file] [log] [blame]
bellardb4608c02003-06-27 17:34:32 +00001/*
2 * gdb server stub
ths5fafdf22007-09-16 21:08:06 +00003 *
Alex Bennée42a09592019-07-05 13:28:19 +01004 * This implements a subset of the remote protocol as described in:
5 *
6 * https://sourceware.org/gdb/onlinedocs/gdb/Remote-Protocol.html
7 *
bellard34751872005-07-02 14:31:34 +00008 * Copyright (c) 2003-2005 Fabrice Bellard
bellardb4608c02003-06-27 17:34:32 +00009 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
Blue Swirl8167ee82009-07-16 20:47:01 +000021 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Alex Bennée42a09592019-07-05 13:28:19 +010022 *
23 * SPDX-License-Identifier: LGPL-2.0+
bellardb4608c02003-06-27 17:34:32 +000024 */
Markus Armbruster856dfd82019-05-23 16:35:06 +020025
Peter Maydelld38ea872016-01-29 17:50:05 +000026#include "qemu/osdep.h"
Markus Armbrustera8d25322019-05-23 16:35:08 +020027#include "qemu-common.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010028#include "qapi/error.h"
Ziyue Yang508b4ec2017-01-18 16:02:41 +080029#include "qemu/error-report.h"
Markus Armbruster856dfd82019-05-23 16:35:06 +020030#include "qemu/ctype.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020031#include "qemu/cutils.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020032#include "qemu/module.h"
Doug Gale5c9522b2017-12-02 20:30:37 -050033#include "trace-root.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020034#ifdef CONFIG_USER_ONLY
bellard1fddef42005-04-17 19:16:13 +000035#include "qemu.h"
36#else
Paolo Bonzini83c90892012-12-17 18:19:49 +010037#include "monitor/monitor.h"
Marc-André Lureau8228e352017-01-26 17:19:46 +040038#include "chardev/char.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040039#include "chardev/char-fe.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010040#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010041#include "exec/gdbstub.h"
Luc Michel8f468632019-01-07 15:23:45 +000042#include "hw/cpu/cluster.h"
Like Xu5cc87672019-05-19 04:54:21 +080043#include "hw/boards.h"
bellard1fddef42005-04-17 19:16:13 +000044#endif
bellard67b915a2004-03-31 23:37:16 +000045
pbrook56aebc82008-10-11 17:55:29 +000046#define MAX_PACKET_LENGTH 4096
47
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010048#include "qemu/sockets.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010049#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010050#include "sysemu/kvm.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020051#include "sysemu/runstate.h"
Alex Bennéef1672e62019-05-13 14:43:57 +010052#include "hw/semihosting/semihost.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010053#include "exec/exec-all.h"
aurel32ca587a82008-12-18 22:44:13 +000054
Jan Kiszkaa3919382015-02-07 09:38:44 +010055#ifdef CONFIG_USER_ONLY
56#define GDB_ATTACHED "0"
57#else
58#define GDB_ATTACHED "1"
59#endif
60
Jon Doronab4752e2019-05-29 09:41:48 +030061#ifndef CONFIG_USER_ONLY
62static int phy_memory_mode;
63#endif
64
Andreas Färberf3659ee2013-06-27 19:09:09 +020065static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
66 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020067{
Jon Doronab4752e2019-05-29 09:41:48 +030068 CPUClass *cc;
Andreas Färberf3659ee2013-06-27 19:09:09 +020069
Jon Doronab4752e2019-05-29 09:41:48 +030070#ifndef CONFIG_USER_ONLY
71 if (phy_memory_mode) {
72 if (is_write) {
73 cpu_physical_memory_write(addr, buf, len);
74 } else {
75 cpu_physical_memory_read(addr, buf, len);
76 }
77 return 0;
78 }
79#endif
80
81 cc = CPU_GET_CLASS(cpu);
Andreas Färberf3659ee2013-06-27 19:09:09 +020082 if (cc->memory_rw_debug) {
83 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
84 }
85 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020086}
aurel32ca587a82008-12-18 22:44:13 +000087
Alex Bennéed2a6c852017-07-12 11:52:14 +010088/* Return the GDB index for a given vCPU state.
89 *
90 * For user mode this is simply the thread id. In system mode GDB
91 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
92 */
93static inline int cpu_gdb_index(CPUState *cpu)
94{
95#if defined(CONFIG_USER_ONLY)
Alex Bennéebd88c782017-07-12 11:52:15 +010096 TaskState *ts = (TaskState *) cpu->opaque;
97 return ts->ts_tid;
Alex Bennéed2a6c852017-07-12 11:52:14 +010098#else
99 return cpu->cpu_index + 1;
100#endif
101}
102
aurel32ca587a82008-12-18 22:44:13 +0000103enum {
104 GDB_SIGNAL_0 = 0,
105 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +0100106 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +0000107 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +0100108 GDB_SIGNAL_ABRT = 6,
109 GDB_SIGNAL_ALRM = 14,
110 GDB_SIGNAL_IO = 23,
111 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +0000112 GDB_SIGNAL_UNKNOWN = 143
113};
114
115#ifdef CONFIG_USER_ONLY
116
117/* Map target signal numbers to GDB protocol signal numbers and vice
118 * versa. For user emulation's currently supported systems, we can
119 * assume most signals are defined.
120 */
121
122static int gdb_signal_table[] = {
123 0,
124 TARGET_SIGHUP,
125 TARGET_SIGINT,
126 TARGET_SIGQUIT,
127 TARGET_SIGILL,
128 TARGET_SIGTRAP,
129 TARGET_SIGABRT,
130 -1, /* SIGEMT */
131 TARGET_SIGFPE,
132 TARGET_SIGKILL,
133 TARGET_SIGBUS,
134 TARGET_SIGSEGV,
135 TARGET_SIGSYS,
136 TARGET_SIGPIPE,
137 TARGET_SIGALRM,
138 TARGET_SIGTERM,
139 TARGET_SIGURG,
140 TARGET_SIGSTOP,
141 TARGET_SIGTSTP,
142 TARGET_SIGCONT,
143 TARGET_SIGCHLD,
144 TARGET_SIGTTIN,
145 TARGET_SIGTTOU,
146 TARGET_SIGIO,
147 TARGET_SIGXCPU,
148 TARGET_SIGXFSZ,
149 TARGET_SIGVTALRM,
150 TARGET_SIGPROF,
151 TARGET_SIGWINCH,
152 -1, /* SIGLOST */
153 TARGET_SIGUSR1,
154 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000155#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000156 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000157#else
158 -1,
159#endif
aurel32ca587a82008-12-18 22:44:13 +0000160 -1, /* SIGPOLL */
161 -1,
162 -1,
163 -1,
164 -1,
165 -1,
166 -1,
167 -1,
168 -1,
169 -1,
170 -1,
171 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000172#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000173 __SIGRTMIN + 1,
174 __SIGRTMIN + 2,
175 __SIGRTMIN + 3,
176 __SIGRTMIN + 4,
177 __SIGRTMIN + 5,
178 __SIGRTMIN + 6,
179 __SIGRTMIN + 7,
180 __SIGRTMIN + 8,
181 __SIGRTMIN + 9,
182 __SIGRTMIN + 10,
183 __SIGRTMIN + 11,
184 __SIGRTMIN + 12,
185 __SIGRTMIN + 13,
186 __SIGRTMIN + 14,
187 __SIGRTMIN + 15,
188 __SIGRTMIN + 16,
189 __SIGRTMIN + 17,
190 __SIGRTMIN + 18,
191 __SIGRTMIN + 19,
192 __SIGRTMIN + 20,
193 __SIGRTMIN + 21,
194 __SIGRTMIN + 22,
195 __SIGRTMIN + 23,
196 __SIGRTMIN + 24,
197 __SIGRTMIN + 25,
198 __SIGRTMIN + 26,
199 __SIGRTMIN + 27,
200 __SIGRTMIN + 28,
201 __SIGRTMIN + 29,
202 __SIGRTMIN + 30,
203 __SIGRTMIN + 31,
204 -1, /* SIGCANCEL */
205 __SIGRTMIN,
206 __SIGRTMIN + 32,
207 __SIGRTMIN + 33,
208 __SIGRTMIN + 34,
209 __SIGRTMIN + 35,
210 __SIGRTMIN + 36,
211 __SIGRTMIN + 37,
212 __SIGRTMIN + 38,
213 __SIGRTMIN + 39,
214 __SIGRTMIN + 40,
215 __SIGRTMIN + 41,
216 __SIGRTMIN + 42,
217 __SIGRTMIN + 43,
218 __SIGRTMIN + 44,
219 __SIGRTMIN + 45,
220 __SIGRTMIN + 46,
221 __SIGRTMIN + 47,
222 __SIGRTMIN + 48,
223 __SIGRTMIN + 49,
224 __SIGRTMIN + 50,
225 __SIGRTMIN + 51,
226 __SIGRTMIN + 52,
227 __SIGRTMIN + 53,
228 __SIGRTMIN + 54,
229 __SIGRTMIN + 55,
230 __SIGRTMIN + 56,
231 __SIGRTMIN + 57,
232 __SIGRTMIN + 58,
233 __SIGRTMIN + 59,
234 __SIGRTMIN + 60,
235 __SIGRTMIN + 61,
236 __SIGRTMIN + 62,
237 __SIGRTMIN + 63,
238 __SIGRTMIN + 64,
239 __SIGRTMIN + 65,
240 __SIGRTMIN + 66,
241 __SIGRTMIN + 67,
242 __SIGRTMIN + 68,
243 __SIGRTMIN + 69,
244 __SIGRTMIN + 70,
245 __SIGRTMIN + 71,
246 __SIGRTMIN + 72,
247 __SIGRTMIN + 73,
248 __SIGRTMIN + 74,
249 __SIGRTMIN + 75,
250 __SIGRTMIN + 76,
251 __SIGRTMIN + 77,
252 __SIGRTMIN + 78,
253 __SIGRTMIN + 79,
254 __SIGRTMIN + 80,
255 __SIGRTMIN + 81,
256 __SIGRTMIN + 82,
257 __SIGRTMIN + 83,
258 __SIGRTMIN + 84,
259 __SIGRTMIN + 85,
260 __SIGRTMIN + 86,
261 __SIGRTMIN + 87,
262 __SIGRTMIN + 88,
263 __SIGRTMIN + 89,
264 __SIGRTMIN + 90,
265 __SIGRTMIN + 91,
266 __SIGRTMIN + 92,
267 __SIGRTMIN + 93,
268 __SIGRTMIN + 94,
269 __SIGRTMIN + 95,
270 -1, /* SIGINFO */
271 -1, /* UNKNOWN */
272 -1, /* DEFAULT */
273 -1,
274 -1,
275 -1,
276 -1,
277 -1,
278 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000279#endif
aurel32ca587a82008-12-18 22:44:13 +0000280};
bellard8f447cc2006-06-14 15:21:14 +0000281#else
aurel32ca587a82008-12-18 22:44:13 +0000282/* In system mode we only need SIGINT and SIGTRAP; other signals
283 are not yet supported. */
284
285enum {
286 TARGET_SIGINT = 2,
287 TARGET_SIGTRAP = 5
288};
289
290static int gdb_signal_table[] = {
291 -1,
292 -1,
293 TARGET_SIGINT,
294 -1,
295 -1,
296 TARGET_SIGTRAP
297};
bellard8f447cc2006-06-14 15:21:14 +0000298#endif
bellardb4608c02003-06-27 17:34:32 +0000299
aurel32ca587a82008-12-18 22:44:13 +0000300#ifdef CONFIG_USER_ONLY
301static int target_signal_to_gdb (int sig)
302{
303 int i;
304 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
305 if (gdb_signal_table[i] == sig)
306 return i;
307 return GDB_SIGNAL_UNKNOWN;
308}
309#endif
310
311static int gdb_signal_to_target (int sig)
312{
313 if (sig < ARRAY_SIZE (gdb_signal_table))
314 return gdb_signal_table[sig];
315 else
316 return -1;
317}
318
pbrook56aebc82008-10-11 17:55:29 +0000319typedef struct GDBRegisterState {
320 int base_reg;
321 int num_regs;
322 gdb_reg_cb get_reg;
323 gdb_reg_cb set_reg;
324 const char *xml;
325 struct GDBRegisterState *next;
326} GDBRegisterState;
327
Luc Michel8f468632019-01-07 15:23:45 +0000328typedef struct GDBProcess {
329 uint32_t pid;
330 bool attached;
Luc Michelc145eea2019-01-07 15:23:46 +0000331
332 char target_xml[1024];
Luc Michel8f468632019-01-07 15:23:45 +0000333} GDBProcess;
334
bellard858693c2004-03-31 18:52:07 +0000335enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000336 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000337 RS_IDLE,
338 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400339 RS_GETLINE_ESC,
340 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000341 RS_CHKSUM1,
342 RS_CHKSUM2,
343};
bellard858693c2004-03-31 18:52:07 +0000344typedef struct GDBState {
Alex Bennée8d98c442020-03-16 17:21:33 +0000345 bool init; /* have we been initialised? */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200346 CPUState *c_cpu; /* current CPU for step/continue ops */
347 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200348 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000349 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000350 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000351 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400352 int line_sum; /* running checksum */
353 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000354 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000355 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000356 int signal;
bellard41625032005-04-24 10:07:11 +0000357#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000358 int fd;
bellard41625032005-04-24 10:07:11 +0000359 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000360#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300361 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300362 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000363#endif
Luc Michel8f468632019-01-07 15:23:45 +0000364 bool multiprocess;
365 GDBProcess *processes;
366 int process_num;
Meador Ingecdb432b2012-03-15 17:49:45 +0000367 char syscall_buf[256];
368 gdb_syscall_complete_cb current_syscall_cb;
Alex Bennée308f9e82020-03-16 17:21:35 +0000369 GString *str_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ée8d98c442020-03-16 17:21:33 +0000385}
386
387#ifndef CONFIG_USER_ONLY
388static void reset_gdbserver_state(void)
389{
390 g_free(gdbserver_state.processes);
391 gdbserver_state.processes = NULL;
392 gdbserver_state.process_num = 0;
393}
394#endif
aliguori880a7572008-11-18 20:30:24 +0000395
Andreas Färber5b50e792013-06-29 04:18:45 +0200396bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000397
bellard1fddef42005-04-17 19:16:13 +0000398#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000399/* XXX: This is not thread safe. Do we care? */
400static int gdbserver_fd = -1;
401
Alex Bennéea346af32020-03-16 17:21:34 +0000402static int get_char(void)
bellardb4608c02003-06-27 17:34:32 +0000403{
404 uint8_t ch;
405 int ret;
406
407 for(;;) {
Alex Bennéea346af32020-03-16 17:21:34 +0000408 ret = qemu_recv(gdbserver_state.fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000409 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000410 if (errno == ECONNRESET)
Alex Bennéea346af32020-03-16 17:21:34 +0000411 gdbserver_state.fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200412 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000413 return -1;
414 } else if (ret == 0) {
Alex Bennéea346af32020-03-16 17:21:34 +0000415 close(gdbserver_state.fd);
416 gdbserver_state.fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000417 return -1;
418 } else {
419 break;
420 }
421 }
422 return ch;
423}
pbrook4046d912007-01-28 01:53:16 +0000424#endif
bellardb4608c02003-06-27 17:34:32 +0000425
blueswir1654efcf2009-04-18 07:29:59 +0000426static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000427 GDB_SYS_UNKNOWN,
428 GDB_SYS_ENABLED,
429 GDB_SYS_DISABLED,
430} gdb_syscall_mode;
431
Liviu Ionescua38bb072014-12-11 12:07:48 +0000432/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000433int use_gdb_syscalls(void)
434{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100435 SemihostingTarget target = semihosting_get_target();
436 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000437 /* -semihosting-config target=native */
438 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100439 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000440 /* -semihosting-config target=gdb */
441 return true;
442 }
443
444 /* -semihosting-config target=auto */
445 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000446 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
Alex Bennée8d98c442020-03-16 17:21:33 +0000447 gdb_syscall_mode = gdbserver_state.init ?
448 GDB_SYS_ENABLED : GDB_SYS_DISABLED;
pbrooka2d1eba2007-01-28 03:10:55 +0000449 }
450 return gdb_syscall_mode == GDB_SYS_ENABLED;
451}
452
edgar_iglba70a622008-03-14 06:10:42 +0000453/* Resume execution. */
Alex Bennéea346af32020-03-16 17:21:34 +0000454static inline void gdb_continue(void)
edgar_iglba70a622008-03-14 06:10:42 +0000455{
Doug Gale5c9522b2017-12-02 20:30:37 -0500456
edgar_iglba70a622008-03-14 06:10:42 +0000457#ifdef CONFIG_USER_ONLY
Alex Bennéea346af32020-03-16 17:21:34 +0000458 gdbserver_state.running_state = 1;
Doug Gale5c9522b2017-12-02 20:30:37 -0500459 trace_gdbstub_op_continue();
edgar_iglba70a622008-03-14 06:10:42 +0000460#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200461 if (!runstate_needs_reset()) {
Doug Gale5c9522b2017-12-02 20:30:37 -0500462 trace_gdbstub_op_continue();
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200463 vm_start();
464 }
edgar_iglba70a622008-03-14 06:10:42 +0000465#endif
466}
467
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100468/*
469 * Resume execution, per CPU actions. For user-mode emulation it's
470 * equivalent to gdb_continue.
471 */
Alex Bennéea346af32020-03-16 17:21:34 +0000472static int gdb_continue_partial(char *newstates)
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100473{
474 CPUState *cpu;
475 int res = 0;
476#ifdef CONFIG_USER_ONLY
477 /*
478 * This is not exactly accurate, but it's an improvement compared to the
479 * previous situation, where only one CPU would be single-stepped.
480 */
481 CPU_FOREACH(cpu) {
482 if (newstates[cpu->cpu_index] == 's') {
Doug Gale5c9522b2017-12-02 20:30:37 -0500483 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100484 cpu_single_step(cpu, sstep_flags);
485 }
486 }
Alex Bennéea346af32020-03-16 17:21:34 +0000487 gdbserver_state.running_state = 1;
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100488#else
489 int flag = 0;
490
491 if (!runstate_needs_reset()) {
492 if (vm_prepare_start()) {
493 return 0;
494 }
495
496 CPU_FOREACH(cpu) {
497 switch (newstates[cpu->cpu_index]) {
498 case 0:
499 case 1:
500 break; /* nothing to do here */
501 case 's':
Doug Gale5c9522b2017-12-02 20:30:37 -0500502 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100503 cpu_single_step(cpu, sstep_flags);
504 cpu_resume(cpu);
505 flag = 1;
506 break;
507 case 'c':
Doug Gale5c9522b2017-12-02 20:30:37 -0500508 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100509 cpu_resume(cpu);
510 flag = 1;
511 break;
512 default:
513 res = -1;
514 break;
515 }
516 }
517 }
518 if (flag) {
519 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
520 }
521#endif
522 return res;
523}
524
Alex Bennéea346af32020-03-16 17:21:34 +0000525static void put_buffer(const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000526{
pbrook4046d912007-01-28 01:53:16 +0000527#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000528 int ret;
529
530 while (len > 0) {
Alex Bennéea346af32020-03-16 17:21:34 +0000531 ret = send(gdbserver_state.fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000532 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200533 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000534 return;
535 } else {
536 buf += ret;
537 len -= ret;
538 }
539 }
pbrook4046d912007-01-28 01:53:16 +0000540#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100541 /* XXX this blocks entire thread. Rewrite to use
542 * qemu_chr_fe_write and background I/O callbacks */
Alex Bennéea346af32020-03-16 17:21:34 +0000543 qemu_chr_fe_write_all(&gdbserver_state.chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000544#endif
bellardb4608c02003-06-27 17:34:32 +0000545}
546
547static inline int fromhex(int v)
548{
549 if (v >= '0' && v <= '9')
550 return v - '0';
551 else if (v >= 'A' && v <= 'F')
552 return v - 'A' + 10;
553 else if (v >= 'a' && v <= 'f')
554 return v - 'a' + 10;
555 else
556 return 0;
557}
558
559static inline int tohex(int v)
560{
561 if (v < 10)
562 return v + '0';
563 else
564 return v - 10 + 'a';
565}
566
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -0300567/* writes 2*len+1 bytes in buf */
Alex Bennée308f9e82020-03-16 17:21:35 +0000568static void memtohex(GString *buf, const uint8_t *mem, int len)
bellardb4608c02003-06-27 17:34:32 +0000569{
570 int i, c;
bellardb4608c02003-06-27 17:34:32 +0000571 for(i = 0; i < len; i++) {
572 c = mem[i];
Alex Bennée308f9e82020-03-16 17:21:35 +0000573 g_string_append_c(buf, tohex(c >> 4));
574 g_string_append_c(buf, tohex(c & 0xf));
bellardb4608c02003-06-27 17:34:32 +0000575 }
Alex Bennée308f9e82020-03-16 17:21:35 +0000576 g_string_append_c(buf, '\0');
bellardb4608c02003-06-27 17:34:32 +0000577}
578
579static void hextomem(uint8_t *mem, const char *buf, int len)
580{
581 int i;
582
583 for(i = 0; i < len; i++) {
584 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
585 buf += 2;
586 }
587}
588
Doug Gale5c9522b2017-12-02 20:30:37 -0500589static void hexdump(const char *buf, int len,
590 void (*trace_fn)(size_t ofs, char const *text))
591{
592 char line_buffer[3 * 16 + 4 + 16 + 1];
593
594 size_t i;
595 for (i = 0; i < len || (i & 0xF); ++i) {
596 size_t byte_ofs = i & 15;
597
598 if (byte_ofs == 0) {
599 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
600 line_buffer[3 * 16 + 4 + 16] = 0;
601 }
602
603 size_t col_group = (i >> 2) & 3;
604 size_t hex_col = byte_ofs * 3 + col_group;
605 size_t txt_col = 3 * 16 + 4 + byte_ofs;
606
607 if (i < len) {
608 char value = buf[i];
609
610 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
611 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
612 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
613 ? value
614 : '.';
615 }
616
617 if (byte_ofs == 0xF)
618 trace_fn(i & -16, line_buffer);
619 }
620}
621
bellardb4608c02003-06-27 17:34:32 +0000622/* return -1 if error, 0 if OK */
Alex Bennéea346af32020-03-16 17:21:34 +0000623static int put_packet_binary(const char *buf, int len, bool dump)
bellardb4608c02003-06-27 17:34:32 +0000624{
pbrook56aebc82008-10-11 17:55:29 +0000625 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000626 uint8_t *p;
Alex Bennéea346af32020-03-16 17:21:34 +0000627 uint8_t *ps = &gdbserver_state.last_packet[0];
bellardb4608c02003-06-27 17:34:32 +0000628
Doug Gale5c9522b2017-12-02 20:30:37 -0500629 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
630 hexdump(buf, len, trace_gdbstub_io_binaryreply);
631 }
632
bellardb4608c02003-06-27 17:34:32 +0000633 for(;;) {
Alex Bennéea346af32020-03-16 17:21:34 +0000634 p = ps;
pbrook4046d912007-01-28 01:53:16 +0000635 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000636 memcpy(p, buf, len);
637 p += len;
bellardb4608c02003-06-27 17:34:32 +0000638 csum = 0;
639 for(i = 0; i < len; i++) {
640 csum += buf[i];
641 }
pbrook4046d912007-01-28 01:53:16 +0000642 *(p++) = '#';
643 *(p++) = tohex((csum >> 4) & 0xf);
644 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000645
Alex Bennéea346af32020-03-16 17:21:34 +0000646 gdbserver_state.last_packet_len = p - ps;
647 put_buffer(ps, gdbserver_state.last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000648
pbrook4046d912007-01-28 01:53:16 +0000649#ifdef CONFIG_USER_ONLY
Alex Bennéea346af32020-03-16 17:21:34 +0000650 i = get_char();
pbrook4046d912007-01-28 01:53:16 +0000651 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000652 return -1;
pbrook4046d912007-01-28 01:53:16 +0000653 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000654 break;
pbrook4046d912007-01-28 01:53:16 +0000655#else
656 break;
657#endif
bellardb4608c02003-06-27 17:34:32 +0000658 }
659 return 0;
660}
661
pbrook56aebc82008-10-11 17:55:29 +0000662/* return -1 if error, 0 if OK */
Alex Bennéea346af32020-03-16 17:21:34 +0000663static int put_packet(const char *buf)
pbrook56aebc82008-10-11 17:55:29 +0000664{
Doug Gale5c9522b2017-12-02 20:30:37 -0500665 trace_gdbstub_io_reply(buf);
pbrook56aebc82008-10-11 17:55:29 +0000666
Alex Bennéea346af32020-03-16 17:21:34 +0000667 return put_packet_binary(buf, strlen(buf), false);
pbrook56aebc82008-10-11 17:55:29 +0000668}
669
Alex Bennée308f9e82020-03-16 17:21:35 +0000670static void put_strbuf(void)
pbrook56aebc82008-10-11 17:55:29 +0000671{
Alex Bennée308f9e82020-03-16 17:21:35 +0000672 put_packet(gdbserver_state.str_buf->str);
673}
674
675/* Encode data using the encoding for 'x' packets. */
676static void memtox(GString *buf, const char *mem, int len)
677{
pbrook56aebc82008-10-11 17:55:29 +0000678 char c;
679
680 while (len--) {
681 c = *(mem++);
682 switch (c) {
683 case '#': case '$': case '*': case '}':
Alex Bennée308f9e82020-03-16 17:21:35 +0000684 g_string_append_c(buf, '}');
685 g_string_append_c(buf, c ^ 0x20);
pbrook56aebc82008-10-11 17:55:29 +0000686 break;
687 default:
Alex Bennée308f9e82020-03-16 17:21:35 +0000688 g_string_append_c(buf, c);
pbrook56aebc82008-10-11 17:55:29 +0000689 break;
690 }
691 }
pbrook56aebc82008-10-11 17:55:29 +0000692}
693
Alex Bennéea346af32020-03-16 17:21:34 +0000694static uint32_t gdb_get_cpu_pid(CPUState *cpu)
Luc Michel1a227332019-01-07 15:23:45 +0000695{
Luc Michel1a227332019-01-07 15:23:45 +0000696 /* TODO: In user mode, we should use the task state PID */
Peter Maydell46f5abc2019-01-29 11:46:06 +0000697 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
698 /* Return the default process' PID */
Alex Bennéea346af32020-03-16 17:21:34 +0000699 int index = gdbserver_state.process_num - 1;
700 return gdbserver_state.processes[index].pid;
Peter Maydell46f5abc2019-01-29 11:46:06 +0000701 }
702 return cpu->cluster_index + 1;
Luc Michel1a227332019-01-07 15:23:45 +0000703}
704
Alex Bennéea346af32020-03-16 17:21:34 +0000705static GDBProcess *gdb_get_process(uint32_t pid)
Luc Michel7d8c87d2019-01-07 15:23:45 +0000706{
707 int i;
708
709 if (!pid) {
710 /* 0 means any process, we take the first one */
Alex Bennéea346af32020-03-16 17:21:34 +0000711 return &gdbserver_state.processes[0];
Luc Michel7d8c87d2019-01-07 15:23:45 +0000712 }
713
Alex Bennéea346af32020-03-16 17:21:34 +0000714 for (i = 0; i < gdbserver_state.process_num; i++) {
715 if (gdbserver_state.processes[i].pid == pid) {
716 return &gdbserver_state.processes[i];
Luc Michel7d8c87d2019-01-07 15:23:45 +0000717 }
718 }
719
720 return NULL;
721}
722
Alex Bennéea346af32020-03-16 17:21:34 +0000723static GDBProcess *gdb_get_cpu_process(CPUState *cpu)
Luc Michel7d8c87d2019-01-07 15:23:45 +0000724{
Alex Bennéea346af32020-03-16 17:21:34 +0000725 return gdb_get_process(gdb_get_cpu_pid(cpu));
Luc Michel7d8c87d2019-01-07 15:23:45 +0000726}
727
728static CPUState *find_cpu(uint32_t thread_id)
729{
730 CPUState *cpu;
731
732 CPU_FOREACH(cpu) {
733 if (cpu_gdb_index(cpu) == thread_id) {
734 return cpu;
735 }
736 }
737
738 return NULL;
739}
740
Alex Bennéea346af32020-03-16 17:21:34 +0000741static CPUState *get_first_cpu_in_process(GDBProcess *process)
Luc Michele40e5202019-01-07 15:23:46 +0000742{
743 CPUState *cpu;
744
745 CPU_FOREACH(cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +0000746 if (gdb_get_cpu_pid(cpu) == process->pid) {
Luc Michele40e5202019-01-07 15:23:46 +0000747 return cpu;
748 }
749 }
750
751 return NULL;
752}
753
Alex Bennéea346af32020-03-16 17:21:34 +0000754static CPUState *gdb_next_cpu_in_process(CPUState *cpu)
Luc Michele40e5202019-01-07 15:23:46 +0000755{
Alex Bennéea346af32020-03-16 17:21:34 +0000756 uint32_t pid = gdb_get_cpu_pid(cpu);
Luc Michele40e5202019-01-07 15:23:46 +0000757 cpu = CPU_NEXT(cpu);
758
759 while (cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +0000760 if (gdb_get_cpu_pid(cpu) == pid) {
Luc Michele40e5202019-01-07 15:23:46 +0000761 break;
762 }
763
764 cpu = CPU_NEXT(cpu);
765 }
766
767 return cpu;
768}
769
Luc Michele40e5202019-01-07 15:23:46 +0000770/* Return the cpu following @cpu, while ignoring unattached processes. */
Alex Bennéea346af32020-03-16 17:21:34 +0000771static CPUState *gdb_next_attached_cpu(CPUState *cpu)
Luc Michele40e5202019-01-07 15:23:46 +0000772{
773 cpu = CPU_NEXT(cpu);
774
775 while (cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +0000776 if (gdb_get_cpu_process(cpu)->attached) {
Luc Michele40e5202019-01-07 15:23:46 +0000777 break;
778 }
779
780 cpu = CPU_NEXT(cpu);
781 }
782
783 return cpu;
784}
785
786/* Return the first attached cpu */
Alex Bennéea346af32020-03-16 17:21:34 +0000787static CPUState *gdb_first_attached_cpu(void)
Luc Michele40e5202019-01-07 15:23:46 +0000788{
789 CPUState *cpu = first_cpu;
Alex Bennéea346af32020-03-16 17:21:34 +0000790 GDBProcess *process = gdb_get_cpu_process(cpu);
Luc Michele40e5202019-01-07 15:23:46 +0000791
792 if (!process->attached) {
Alex Bennéea346af32020-03-16 17:21:34 +0000793 return gdb_next_attached_cpu(cpu);
Luc Michele40e5202019-01-07 15:23:46 +0000794 }
795
796 return cpu;
797}
798
Alex Bennéea346af32020-03-16 17:21:34 +0000799static CPUState *gdb_get_cpu(uint32_t pid, uint32_t tid)
Luc Michelab65eed2019-01-29 11:46:03 +0000800{
801 GDBProcess *process;
802 CPUState *cpu;
803
804 if (!pid && !tid) {
805 /* 0 means any process/thread, we take the first attached one */
Alex Bennéea346af32020-03-16 17:21:34 +0000806 return gdb_first_attached_cpu();
Luc Michelab65eed2019-01-29 11:46:03 +0000807 } else if (pid && !tid) {
808 /* any thread in a specific process */
Alex Bennéea346af32020-03-16 17:21:34 +0000809 process = gdb_get_process(pid);
Luc Michelab65eed2019-01-29 11:46:03 +0000810
811 if (process == NULL) {
812 return NULL;
813 }
814
815 if (!process->attached) {
816 return NULL;
817 }
818
Alex Bennéea346af32020-03-16 17:21:34 +0000819 return get_first_cpu_in_process(process);
Luc Michelab65eed2019-01-29 11:46:03 +0000820 } else {
821 /* a specific thread */
822 cpu = find_cpu(tid);
823
824 if (cpu == NULL) {
825 return NULL;
826 }
827
Alex Bennéea346af32020-03-16 17:21:34 +0000828 process = gdb_get_cpu_process(cpu);
Luc Michelab65eed2019-01-29 11:46:03 +0000829
830 if (pid && process->pid != pid) {
831 return NULL;
832 }
833
834 if (!process->attached) {
835 return NULL;
836 }
837
838 return cpu;
839 }
840}
841
Alex Bennéea346af32020-03-16 17:21:34 +0000842static const char *get_feature_xml(const char *p, const char **newp,
843 GDBProcess *process)
pbrook56aebc82008-10-11 17:55:29 +0000844{
pbrook56aebc82008-10-11 17:55:29 +0000845 size_t len;
846 int i;
847 const char *name;
Alex Bennéea346af32020-03-16 17:21:34 +0000848 CPUState *cpu = get_first_cpu_in_process(process);
Luc Michelc145eea2019-01-07 15:23:46 +0000849 CPUClass *cc = CPU_GET_CLASS(cpu);
pbrook56aebc82008-10-11 17:55:29 +0000850
851 len = 0;
852 while (p[len] && p[len] != ':')
853 len++;
854 *newp = p + len;
855
856 name = NULL;
857 if (strncmp(p, "target.xml", len) == 0) {
Luc Michelc145eea2019-01-07 15:23:46 +0000858 char *buf = process->target_xml;
859 const size_t buf_sz = sizeof(process->target_xml);
pbrook56aebc82008-10-11 17:55:29 +0000860
Luc Michelc145eea2019-01-07 15:23:46 +0000861 /* Generate the XML description for this CPU. */
862 if (!buf[0]) {
863 GDBRegisterState *r;
864
865 pstrcat(buf, buf_sz,
David Hildenbrandb3820e62015-12-03 13:14:41 +0100866 "<?xml version=\"1.0\"?>"
867 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
868 "<target>");
869 if (cc->gdb_arch_name) {
870 gchar *arch = cc->gdb_arch_name(cpu);
Luc Michelc145eea2019-01-07 15:23:46 +0000871 pstrcat(buf, buf_sz, "<architecture>");
872 pstrcat(buf, buf_sz, arch);
873 pstrcat(buf, buf_sz, "</architecture>");
David Hildenbrandb3820e62015-12-03 13:14:41 +0100874 g_free(arch);
875 }
Luc Michelc145eea2019-01-07 15:23:46 +0000876 pstrcat(buf, buf_sz, "<xi:include href=\"");
877 pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
878 pstrcat(buf, buf_sz, "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200879 for (r = cpu->gdb_regs; r; r = r->next) {
Luc Michelc145eea2019-01-07 15:23:46 +0000880 pstrcat(buf, buf_sz, "<xi:include href=\"");
881 pstrcat(buf, buf_sz, r->xml);
882 pstrcat(buf, buf_sz, "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000883 }
Luc Michelc145eea2019-01-07 15:23:46 +0000884 pstrcat(buf, buf_sz, "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000885 }
Luc Michelc145eea2019-01-07 15:23:46 +0000886 return buf;
pbrook56aebc82008-10-11 17:55:29 +0000887 }
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100888 if (cc->gdb_get_dynamic_xml) {
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100889 char *xmlname = g_strndup(p, len);
890 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
891
892 g_free(xmlname);
893 if (xml) {
894 return xml;
895 }
896 }
pbrook56aebc82008-10-11 17:55:29 +0000897 for (i = 0; ; i++) {
898 name = xml_builtin[i][0];
899 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
900 break;
901 }
902 return name ? xml_builtin[i][1] : NULL;
903}
pbrook56aebc82008-10-11 17:55:29 +0000904
Andreas Färber385b9f02013-06-27 18:25:36 +0200905static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000906{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200907 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200908 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000909 GDBRegisterState *r;
910
Andreas Färbera0e372f2013-06-28 23:18:47 +0200911 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200912 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200913 }
pbrook56aebc82008-10-11 17:55:29 +0000914
Andreas Färbereac8b352013-06-28 21:11:37 +0200915 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000916 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
917 return r->get_reg(env, mem_buf, reg - r->base_reg);
918 }
919 }
920 return 0;
921}
922
Andreas Färber385b9f02013-06-27 18:25:36 +0200923static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000924{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200925 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200926 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000927 GDBRegisterState *r;
928
Andreas Färbera0e372f2013-06-28 23:18:47 +0200929 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200930 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200931 }
pbrook56aebc82008-10-11 17:55:29 +0000932
Andreas Färbereac8b352013-06-28 21:11:37 +0200933 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000934 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
935 return r->set_reg(env, mem_buf, reg - r->base_reg);
936 }
937 }
938 return 0;
939}
940
941/* Register a supplemental set of CPU registers. If g_pos is nonzero it
942 specifies the first register number and these registers are included in
943 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
944 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
945 */
946
Andreas Färber22169d42013-06-28 21:27:39 +0200947void gdb_register_coprocessor(CPUState *cpu,
948 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
949 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000950{
951 GDBRegisterState *s;
952 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000953
Andreas Färbereac8b352013-06-28 21:11:37 +0200954 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000955 while (*p) {
956 /* Check for duplicates. */
957 if (strcmp((*p)->xml, xml) == 0)
958 return;
959 p = &(*p)->next;
960 }
Stefan Weil9643c252011-10-18 22:25:38 +0200961
962 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200963 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200964 s->num_regs = num_regs;
965 s->get_reg = get_reg;
966 s->set_reg = set_reg;
967 s->xml = xml;
968
pbrook56aebc82008-10-11 17:55:29 +0000969 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200970 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000971 *p = s;
972 if (g_pos) {
973 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800974 error_report("Error: Bad gdb register numbering for '%s', "
975 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200976 } else {
977 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000978 }
979 }
980}
981
aliguoria1d1bb32008-11-18 20:07:32 +0000982#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100983/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
984static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
985{
986 static const int xlat[] = {
987 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
988 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
989 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
990 };
991
992 CPUClass *cc = CPU_GET_CLASS(cpu);
993 int cputype = xlat[gdbtype];
994
995 if (cc->gdb_stop_before_watchpoint) {
996 cputype |= BP_STOP_BEFORE_ACCESS;
997 }
998 return cputype;
999}
aliguoria1d1bb32008-11-18 20:07:32 +00001000#endif
1001
Jon Doron77f6ce52019-05-29 09:41:35 +03001002static int gdb_breakpoint_insert(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +00001003{
Andreas Färber182735e2013-05-29 22:29:20 +02001004 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001005 int err = 0;
1006
Andreas Färber62278812013-06-27 17:12:06 +02001007 if (kvm_enabled()) {
Alex Bennée8d98c442020-03-16 17:21:33 +00001008 return kvm_insert_breakpoint(gdbserver_state.c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001009 }
aliguorie22a25c2009-03-12 20:12:48 +00001010
aliguoria1d1bb32008-11-18 20:07:32 +00001011 switch (type) {
1012 case GDB_BREAKPOINT_SW:
1013 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001014 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001015 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
1016 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001017 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001018 }
aliguori880a7572008-11-18 20:30:24 +00001019 }
1020 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001021#ifndef CONFIG_USER_ONLY
1022 case GDB_WATCHPOINT_WRITE:
1023 case GDB_WATCHPOINT_READ:
1024 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001025 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001026 err = cpu_watchpoint_insert(cpu, addr, len,
1027 xlat_gdb_type(cpu, type), NULL);
1028 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001029 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +01001030 }
aliguori880a7572008-11-18 20:30:24 +00001031 }
1032 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001033#endif
1034 default:
1035 return -ENOSYS;
1036 }
1037}
1038
Jon Doron77f6ce52019-05-29 09:41:35 +03001039static int gdb_breakpoint_remove(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +00001040{
Andreas Färber182735e2013-05-29 22:29:20 +02001041 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001042 int err = 0;
1043
Andreas Färber62278812013-06-27 17:12:06 +02001044 if (kvm_enabled()) {
Alex Bennée8d98c442020-03-16 17:21:33 +00001045 return kvm_remove_breakpoint(gdbserver_state.c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001046 }
aliguorie22a25c2009-03-12 20:12:48 +00001047
aliguoria1d1bb32008-11-18 20:07:32 +00001048 switch (type) {
1049 case GDB_BREAKPOINT_SW:
1050 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001051 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001052 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
1053 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001054 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001055 }
aliguori880a7572008-11-18 20:30:24 +00001056 }
1057 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001058#ifndef CONFIG_USER_ONLY
1059 case GDB_WATCHPOINT_WRITE:
1060 case GDB_WATCHPOINT_READ:
1061 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001062 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001063 err = cpu_watchpoint_remove(cpu, addr, len,
1064 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +00001065 if (err)
1066 break;
1067 }
1068 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001069#endif
1070 default:
1071 return -ENOSYS;
1072 }
1073}
1074
Luc Michel546f3c62019-01-07 15:23:46 +00001075static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu)
1076{
1077 cpu_breakpoint_remove_all(cpu, BP_GDB);
1078#ifndef CONFIG_USER_ONLY
1079 cpu_watchpoint_remove_all(cpu, BP_GDB);
1080#endif
1081}
1082
Alex Bennéea346af32020-03-16 17:21:34 +00001083static void gdb_process_breakpoint_remove_all(GDBProcess *p)
Luc Michel546f3c62019-01-07 15:23:46 +00001084{
Alex Bennéea346af32020-03-16 17:21:34 +00001085 CPUState *cpu = get_first_cpu_in_process(p);
Luc Michel546f3c62019-01-07 15:23:46 +00001086
1087 while (cpu) {
1088 gdb_cpu_breakpoint_remove_all(cpu);
Alex Bennéea346af32020-03-16 17:21:34 +00001089 cpu = gdb_next_cpu_in_process(cpu);
Luc Michel546f3c62019-01-07 15:23:46 +00001090 }
1091}
1092
aliguori880a7572008-11-18 20:30:24 +00001093static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +00001094{
Andreas Färber182735e2013-05-29 22:29:20 +02001095 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001096
aliguorie22a25c2009-03-12 20:12:48 +00001097 if (kvm_enabled()) {
Alex Bennée8d98c442020-03-16 17:21:33 +00001098 kvm_remove_all_breakpoints(gdbserver_state.c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +00001099 return;
1100 }
1101
Andreas Färberbdc44642013-06-24 23:50:24 +02001102 CPU_FOREACH(cpu) {
Luc Michel546f3c62019-01-07 15:23:46 +00001103 gdb_cpu_breakpoint_remove_all(cpu);
aliguori880a7572008-11-18 20:30:24 +00001104 }
aliguoria1d1bb32008-11-18 20:07:32 +00001105}
1106
Alex Bennéea346af32020-03-16 17:21:34 +00001107static void gdb_set_cpu_pc(target_ulong pc)
aurel32fab9d282009-04-08 21:29:37 +00001108{
Alex Bennéea346af32020-03-16 17:21:34 +00001109 CPUState *cpu = gdbserver_state.c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +02001110
1111 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -07001112 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +00001113}
1114
Alex Bennée308f9e82020-03-16 17:21:35 +00001115static void gdb_append_thread_id(CPUState *cpu, GString *buf)
Luc Michel1a227332019-01-07 15:23:45 +00001116{
Alex Bennéea346af32020-03-16 17:21:34 +00001117 if (gdbserver_state.multiprocess) {
Alex Bennée308f9e82020-03-16 17:21:35 +00001118 g_string_append_printf(buf, "p%02x.%02x",
1119 gdb_get_cpu_pid(cpu), cpu_gdb_index(cpu));
Luc Michel1a227332019-01-07 15:23:45 +00001120 } else {
Alex Bennée308f9e82020-03-16 17:21:35 +00001121 g_string_append_printf(buf, "%02x", cpu_gdb_index(cpu));
Luc Michel1a227332019-01-07 15:23:45 +00001122 }
Luc Michel1a227332019-01-07 15:23:45 +00001123}
1124
Luc Michel7d8c87d2019-01-07 15:23:45 +00001125typedef enum GDBThreadIdKind {
1126 GDB_ONE_THREAD = 0,
1127 GDB_ALL_THREADS, /* One process, all threads */
1128 GDB_ALL_PROCESSES,
1129 GDB_READ_THREAD_ERR
1130} GDBThreadIdKind;
1131
1132static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1133 uint32_t *pid, uint32_t *tid)
1134{
1135 unsigned long p, t;
1136 int ret;
1137
1138 if (*buf == 'p') {
1139 buf++;
1140 ret = qemu_strtoul(buf, &buf, 16, &p);
1141
1142 if (ret) {
1143 return GDB_READ_THREAD_ERR;
1144 }
1145
1146 /* Skip '.' */
1147 buf++;
1148 } else {
1149 p = 1;
1150 }
1151
1152 ret = qemu_strtoul(buf, &buf, 16, &t);
1153
1154 if (ret) {
1155 return GDB_READ_THREAD_ERR;
1156 }
1157
1158 *end_buf = buf;
1159
1160 if (p == -1) {
1161 return GDB_ALL_PROCESSES;
1162 }
1163
1164 if (pid) {
1165 *pid = p;
1166 }
1167
1168 if (t == -1) {
1169 return GDB_ALL_THREADS;
1170 }
1171
1172 if (tid) {
1173 *tid = t;
1174 }
1175
1176 return GDB_ONE_THREAD;
1177}
1178
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001179/**
1180 * gdb_handle_vcont - Parses and handles a vCont packet.
1181 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1182 * a format error, 0 on success.
1183 */
Alex Bennéea346af32020-03-16 17:21:34 +00001184static int gdb_handle_vcont(const char *p)
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001185{
Luc Michele40e5202019-01-07 15:23:46 +00001186 int res, signal = 0;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001187 char cur_action;
1188 char *newstates;
1189 unsigned long tmp;
Luc Michele40e5202019-01-07 15:23:46 +00001190 uint32_t pid, tid;
1191 GDBProcess *process;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001192 CPUState *cpu;
Luc Michelc99ef792019-03-26 12:53:26 +00001193 GDBThreadIdKind kind;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001194#ifdef CONFIG_USER_ONLY
1195 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
1196
1197 CPU_FOREACH(cpu) {
1198 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1199 }
Like Xu5cc87672019-05-19 04:54:21 +08001200#else
1201 MachineState *ms = MACHINE(qdev_get_machine());
1202 unsigned int max_cpus = ms->smp.max_cpus;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001203#endif
1204 /* uninitialised CPUs stay 0 */
1205 newstates = g_new0(char, max_cpus);
1206
1207 /* mark valid CPUs with 1 */
1208 CPU_FOREACH(cpu) {
1209 newstates[cpu->cpu_index] = 1;
1210 }
1211
1212 /*
1213 * res keeps track of what error we are returning, with -ENOTSUP meaning
1214 * that the command is unknown or unsupported, thus returning an empty
1215 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1216 * or incorrect parameters passed.
1217 */
1218 res = 0;
1219 while (*p) {
1220 if (*p++ != ';') {
1221 res = -ENOTSUP;
1222 goto out;
1223 }
1224
1225 cur_action = *p++;
1226 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +01001227 cur_action = qemu_tolower(cur_action);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001228 res = qemu_strtoul(p + 1, &p, 16, &tmp);
1229 if (res) {
1230 goto out;
1231 }
1232 signal = gdb_signal_to_target(tmp);
1233 } else if (cur_action != 'c' && cur_action != 's') {
1234 /* unknown/invalid/unsupported command */
1235 res = -ENOTSUP;
1236 goto out;
1237 }
Luc Michele40e5202019-01-07 15:23:46 +00001238
Luc Michelc99ef792019-03-26 12:53:26 +00001239 if (*p == '\0' || *p == ';') {
1240 /*
1241 * No thread specifier, action is on "all threads". The
1242 * specification is unclear regarding the process to act on. We
1243 * choose all processes.
1244 */
1245 kind = GDB_ALL_PROCESSES;
1246 } else if (*p++ == ':') {
1247 kind = read_thread_id(p, &p, &pid, &tid);
1248 } else {
Luc Michele40e5202019-01-07 15:23:46 +00001249 res = -ENOTSUP;
1250 goto out;
1251 }
1252
Luc Michelc99ef792019-03-26 12:53:26 +00001253 switch (kind) {
Luc Michele40e5202019-01-07 15:23:46 +00001254 case GDB_READ_THREAD_ERR:
1255 res = -EINVAL;
1256 goto out;
1257
1258 case GDB_ALL_PROCESSES:
Alex Bennéea346af32020-03-16 17:21:34 +00001259 cpu = gdb_first_attached_cpu();
Luc Michele40e5202019-01-07 15:23:46 +00001260 while (cpu) {
1261 if (newstates[cpu->cpu_index] == 1) {
1262 newstates[cpu->cpu_index] = cur_action;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001263 }
Luc Michele40e5202019-01-07 15:23:46 +00001264
Alex Bennéea346af32020-03-16 17:21:34 +00001265 cpu = gdb_next_attached_cpu(cpu);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001266 }
Luc Michele40e5202019-01-07 15:23:46 +00001267 break;
1268
1269 case GDB_ALL_THREADS:
Alex Bennéea346af32020-03-16 17:21:34 +00001270 process = gdb_get_process(pid);
Luc Michele40e5202019-01-07 15:23:46 +00001271
1272 if (!process->attached) {
1273 res = -EINVAL;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001274 goto out;
1275 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001276
Alex Bennéea346af32020-03-16 17:21:34 +00001277 cpu = get_first_cpu_in_process(process);
Luc Michele40e5202019-01-07 15:23:46 +00001278 while (cpu) {
1279 if (newstates[cpu->cpu_index] == 1) {
1280 newstates[cpu->cpu_index] = cur_action;
1281 }
1282
Alex Bennéea346af32020-03-16 17:21:34 +00001283 cpu = gdb_next_cpu_in_process(cpu);
Luc Michele40e5202019-01-07 15:23:46 +00001284 }
1285 break;
1286
1287 case GDB_ONE_THREAD:
Alex Bennéea346af32020-03-16 17:21:34 +00001288 cpu = gdb_get_cpu(pid, tid);
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001289
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001290 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001291 if (!cpu) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001292 res = -EINVAL;
1293 goto out;
1294 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001295
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001296 /* only use if no previous match occourred */
1297 if (newstates[cpu->cpu_index] == 1) {
1298 newstates[cpu->cpu_index] = cur_action;
1299 }
Luc Michele40e5202019-01-07 15:23:46 +00001300 break;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001301 }
1302 }
Alex Bennéea346af32020-03-16 17:21:34 +00001303 gdbserver_state.signal = signal;
1304 gdb_continue_partial(newstates);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001305
1306out:
1307 g_free(newstates);
1308
1309 return res;
1310}
1311
Jon Dorond14055d2019-05-29 09:41:29 +03001312typedef union GdbCmdVariant {
1313 const char *data;
1314 uint8_t opcode;
1315 unsigned long val_ul;
1316 unsigned long long val_ull;
1317 struct {
1318 GDBThreadIdKind kind;
1319 uint32_t pid;
1320 uint32_t tid;
1321 } thread_id;
1322} GdbCmdVariant;
1323
1324static const char *cmd_next_param(const char *param, const char delimiter)
1325{
1326 static const char all_delimiters[] = ",;:=";
1327 char curr_delimiters[2] = {0};
1328 const char *delimiters;
1329
1330 if (delimiter == '?') {
1331 delimiters = all_delimiters;
1332 } else if (delimiter == '0') {
1333 return strchr(param, '\0');
1334 } else if (delimiter == '.' && *param) {
1335 return param + 1;
1336 } else {
1337 curr_delimiters[0] = delimiter;
1338 delimiters = curr_delimiters;
1339 }
1340
1341 param += strcspn(param, delimiters);
1342 if (*param) {
1343 param++;
1344 }
1345 return param;
1346}
1347
1348static int cmd_parse_params(const char *data, const char *schema,
1349 GdbCmdVariant *params, int *num_params)
1350{
1351 int curr_param;
1352 const char *curr_schema, *curr_data;
1353
1354 *num_params = 0;
1355
1356 if (!schema) {
1357 return 0;
1358 }
1359
1360 curr_schema = schema;
1361 curr_param = 0;
1362 curr_data = data;
1363 while (curr_schema[0] && curr_schema[1] && *curr_data) {
1364 switch (curr_schema[0]) {
1365 case 'l':
1366 if (qemu_strtoul(curr_data, &curr_data, 16,
1367 &params[curr_param].val_ul)) {
1368 return -EINVAL;
1369 }
1370 curr_param++;
1371 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1372 break;
1373 case 'L':
1374 if (qemu_strtou64(curr_data, &curr_data, 16,
1375 (uint64_t *)&params[curr_param].val_ull)) {
1376 return -EINVAL;
1377 }
1378 curr_param++;
1379 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1380 break;
1381 case 's':
1382 params[curr_param].data = curr_data;
1383 curr_param++;
1384 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1385 break;
1386 case 'o':
1387 params[curr_param].opcode = *(uint8_t *)curr_data;
1388 curr_param++;
1389 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1390 break;
1391 case 't':
1392 params[curr_param].thread_id.kind =
1393 read_thread_id(curr_data, &curr_data,
1394 &params[curr_param].thread_id.pid,
1395 &params[curr_param].thread_id.tid);
1396 curr_param++;
1397 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1398 break;
1399 case '?':
1400 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1401 break;
1402 default:
1403 return -EINVAL;
1404 }
1405 curr_schema += 2;
1406 }
1407
1408 *num_params = curr_param;
1409 return 0;
1410}
1411
1412typedef struct GdbCmdContext {
Jon Dorond14055d2019-05-29 09:41:29 +03001413 GdbCmdVariant *params;
1414 int num_params;
1415 uint8_t mem_buf[MAX_PACKET_LENGTH];
Jon Dorond14055d2019-05-29 09:41:29 +03001416} GdbCmdContext;
1417
1418typedef void (*GdbCmdHandler)(GdbCmdContext *gdb_ctx, void *user_ctx);
1419
1420/*
1421 * cmd_startswith -> cmd is compared using startswith
1422 *
1423 *
1424 * schema definitions:
1425 * Each schema parameter entry consists of 2 chars,
1426 * the first char represents the parameter type handling
1427 * the second char represents the delimiter for the next parameter
1428 *
1429 * Currently supported schema types:
1430 * 'l' -> unsigned long (stored in .val_ul)
1431 * 'L' -> unsigned long long (stored in .val_ull)
1432 * 's' -> string (stored in .data)
1433 * 'o' -> single char (stored in .opcode)
1434 * 't' -> thread id (stored in .thread_id)
1435 * '?' -> skip according to delimiter
1436 *
1437 * Currently supported delimiters:
1438 * '?' -> Stop at any delimiter (",;:=\0")
1439 * '0' -> Stop at "\0"
1440 * '.' -> Skip 1 char unless reached "\0"
1441 * Any other value is treated as the delimiter value itself
1442 */
1443typedef struct GdbCmdParseEntry {
1444 GdbCmdHandler handler;
1445 const char *cmd;
1446 bool cmd_startswith;
1447 const char *schema;
1448} GdbCmdParseEntry;
1449
1450static inline int startswith(const char *string, const char *pattern)
1451{
1452 return !strncmp(string, pattern, strlen(pattern));
1453}
1454
Alex Bennéea346af32020-03-16 17:21:34 +00001455static int process_string_cmd(void *user_ctx, const char *data,
Jon Dorond14055d2019-05-29 09:41:29 +03001456 const GdbCmdParseEntry *cmds, int num_cmds)
1457{
1458 int i, schema_len, max_num_params = 0;
1459 GdbCmdContext gdb_ctx;
1460
1461 if (!cmds) {
1462 return -1;
1463 }
1464
1465 for (i = 0; i < num_cmds; i++) {
1466 const GdbCmdParseEntry *cmd = &cmds[i];
1467 g_assert(cmd->handler && cmd->cmd);
1468
1469 if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
1470 (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
1471 continue;
1472 }
1473
1474 if (cmd->schema) {
1475 schema_len = strlen(cmd->schema);
1476 if (schema_len % 2) {
1477 return -2;
1478 }
1479
1480 max_num_params = schema_len / 2;
1481 }
1482
1483 gdb_ctx.params =
1484 (GdbCmdVariant *)alloca(sizeof(*gdb_ctx.params) * max_num_params);
1485 memset(gdb_ctx.params, 0, sizeof(*gdb_ctx.params) * max_num_params);
1486
1487 if (cmd_parse_params(&data[strlen(cmd->cmd)], cmd->schema,
1488 gdb_ctx.params, &gdb_ctx.num_params)) {
1489 return -1;
1490 }
1491
Jon Dorond14055d2019-05-29 09:41:29 +03001492 cmd->handler(&gdb_ctx, user_ctx);
1493 return 0;
1494 }
1495
1496 return -1;
1497}
1498
Alex Bennéea346af32020-03-16 17:21:34 +00001499static void run_cmd_parser(const char *data, const GdbCmdParseEntry *cmd)
Jon Doron3e2c1262019-05-29 09:41:30 +03001500{
1501 if (!data) {
1502 return;
1503 }
1504
Alex Bennée308f9e82020-03-16 17:21:35 +00001505 g_string_set_size(gdbserver_state.str_buf, 0);
1506
Jon Doron3e2c1262019-05-29 09:41:30 +03001507 /* In case there was an error during the command parsing we must
1508 * send a NULL packet to indicate the command is not supported */
Alex Bennéea346af32020-03-16 17:21:34 +00001509 if (process_string_cmd(NULL, data, cmd, 1)) {
1510 put_packet("");
Jon Doron3e2c1262019-05-29 09:41:30 +03001511 }
1512}
1513
1514static void handle_detach(GdbCmdContext *gdb_ctx, void *user_ctx)
1515{
1516 GDBProcess *process;
Jon Doron3e2c1262019-05-29 09:41:30 +03001517 uint32_t pid = 1;
1518
Alex Bennéea346af32020-03-16 17:21:34 +00001519 if (gdbserver_state.multiprocess) {
Jon Doron3e2c1262019-05-29 09:41:30 +03001520 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001521 put_packet("E22");
Jon Doron3e2c1262019-05-29 09:41:30 +03001522 return;
1523 }
1524
1525 pid = gdb_ctx->params[0].val_ul;
1526 }
1527
Alex Bennéea346af32020-03-16 17:21:34 +00001528 process = gdb_get_process(pid);
1529 gdb_process_breakpoint_remove_all(process);
Jon Doron3e2c1262019-05-29 09:41:30 +03001530 process->attached = false;
1531
Alex Bennéea346af32020-03-16 17:21:34 +00001532 if (pid == gdb_get_cpu_pid(gdbserver_state.c_cpu)) {
1533 gdbserver_state.c_cpu = gdb_first_attached_cpu();
Jon Doron3e2c1262019-05-29 09:41:30 +03001534 }
1535
Alex Bennéea346af32020-03-16 17:21:34 +00001536 if (pid == gdb_get_cpu_pid(gdbserver_state.g_cpu)) {
1537 gdbserver_state.g_cpu = gdb_first_attached_cpu();
Jon Doron3e2c1262019-05-29 09:41:30 +03001538 }
1539
Alex Bennéea346af32020-03-16 17:21:34 +00001540 if (!gdbserver_state.c_cpu) {
Jon Doron3e2c1262019-05-29 09:41:30 +03001541 /* No more process attached */
1542 gdb_syscall_mode = GDB_SYS_DISABLED;
Alex Bennéea346af32020-03-16 17:21:34 +00001543 gdb_continue();
Jon Doron3e2c1262019-05-29 09:41:30 +03001544 }
Alex Bennéea346af32020-03-16 17:21:34 +00001545 put_packet("OK");
Jon Doron3e2c1262019-05-29 09:41:30 +03001546}
1547
Jon Doron44ffded2019-05-29 09:41:31 +03001548static void handle_thread_alive(GdbCmdContext *gdb_ctx, void *user_ctx)
1549{
1550 CPUState *cpu;
1551
1552 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001553 put_packet("E22");
Jon Doron44ffded2019-05-29 09:41:31 +03001554 return;
1555 }
1556
1557 if (gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
Alex Bennéea346af32020-03-16 17:21:34 +00001558 put_packet("E22");
Jon Doron44ffded2019-05-29 09:41:31 +03001559 return;
1560 }
1561
Alex Bennéea346af32020-03-16 17:21:34 +00001562 cpu = gdb_get_cpu(gdb_ctx->params[0].thread_id.pid,
Jon Doron44ffded2019-05-29 09:41:31 +03001563 gdb_ctx->params[0].thread_id.tid);
1564 if (!cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +00001565 put_packet("E22");
Jon Doron44ffded2019-05-29 09:41:31 +03001566 return;
1567 }
1568
Alex Bennéea346af32020-03-16 17:21:34 +00001569 put_packet("OK");
Jon Doron44ffded2019-05-29 09:41:31 +03001570}
1571
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001572static void handle_continue(GdbCmdContext *gdb_ctx, void *user_ctx)
1573{
1574 if (gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001575 gdb_set_cpu_pc(gdb_ctx->params[0].val_ull);
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001576 }
1577
Alex Bennéea346af32020-03-16 17:21:34 +00001578 gdbserver_state.signal = 0;
1579 gdb_continue();
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001580}
1581
Jon Doronccc47d52019-05-29 09:41:33 +03001582static void handle_cont_with_sig(GdbCmdContext *gdb_ctx, void *user_ctx)
1583{
1584 unsigned long signal = 0;
1585
1586 /*
1587 * Note: C sig;[addr] is currently unsupported and we simply
1588 * omit the addr parameter
1589 */
1590 if (gdb_ctx->num_params) {
1591 signal = gdb_ctx->params[0].val_ul;
1592 }
1593
Alex Bennéea346af32020-03-16 17:21:34 +00001594 gdbserver_state.signal = gdb_signal_to_target(signal);
1595 if (gdbserver_state.signal == -1) {
1596 gdbserver_state.signal = 0;
Jon Doronccc47d52019-05-29 09:41:33 +03001597 }
Alex Bennéea346af32020-03-16 17:21:34 +00001598 gdb_continue();
Jon Doronccc47d52019-05-29 09:41:33 +03001599}
1600
Jon Doron3a9651d2019-05-29 09:41:34 +03001601static void handle_set_thread(GdbCmdContext *gdb_ctx, void *user_ctx)
1602{
1603 CPUState *cpu;
1604
1605 if (gdb_ctx->num_params != 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001606 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001607 return;
1608 }
1609
1610 if (gdb_ctx->params[1].thread_id.kind == GDB_READ_THREAD_ERR) {
Alex Bennéea346af32020-03-16 17:21:34 +00001611 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001612 return;
1613 }
1614
1615 if (gdb_ctx->params[1].thread_id.kind != GDB_ONE_THREAD) {
Alex Bennéea346af32020-03-16 17:21:34 +00001616 put_packet("OK");
Jon Doron3a9651d2019-05-29 09:41:34 +03001617 return;
1618 }
1619
Alex Bennéea346af32020-03-16 17:21:34 +00001620 cpu = gdb_get_cpu(gdb_ctx->params[1].thread_id.pid,
Jon Doron3a9651d2019-05-29 09:41:34 +03001621 gdb_ctx->params[1].thread_id.tid);
1622 if (!cpu) {
Alex Bennéea346af32020-03-16 17:21:34 +00001623 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001624 return;
1625 }
1626
1627 /*
1628 * Note: This command is deprecated and modern gdb's will be using the
1629 * vCont command instead.
1630 */
1631 switch (gdb_ctx->params[0].opcode) {
1632 case 'c':
Alex Bennéea346af32020-03-16 17:21:34 +00001633 gdbserver_state.c_cpu = cpu;
1634 put_packet("OK");
Jon Doron3a9651d2019-05-29 09:41:34 +03001635 break;
1636 case 'g':
Alex Bennéea346af32020-03-16 17:21:34 +00001637 gdbserver_state.g_cpu = cpu;
1638 put_packet("OK");
Jon Doron3a9651d2019-05-29 09:41:34 +03001639 break;
1640 default:
Alex Bennéea346af32020-03-16 17:21:34 +00001641 put_packet("E22");
Jon Doron3a9651d2019-05-29 09:41:34 +03001642 break;
1643 }
1644}
1645
Jon Doron77f6ce52019-05-29 09:41:35 +03001646static void handle_insert_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1647{
1648 int res;
1649
1650 if (gdb_ctx->num_params != 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00001651 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001652 return;
1653 }
1654
1655 res = gdb_breakpoint_insert(gdb_ctx->params[0].val_ul,
1656 gdb_ctx->params[1].val_ull,
1657 gdb_ctx->params[2].val_ull);
1658 if (res >= 0) {
Alex Bennéea346af32020-03-16 17:21:34 +00001659 put_packet("OK");
Jon Doron77f6ce52019-05-29 09:41:35 +03001660 return;
1661 } else if (res == -ENOSYS) {
Alex Bennéea346af32020-03-16 17:21:34 +00001662 put_packet("");
Jon Doron77f6ce52019-05-29 09:41:35 +03001663 return;
1664 }
1665
Alex Bennéea346af32020-03-16 17:21:34 +00001666 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001667}
1668
1669static void handle_remove_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1670{
1671 int res;
1672
1673 if (gdb_ctx->num_params != 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00001674 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001675 return;
1676 }
1677
1678 res = gdb_breakpoint_remove(gdb_ctx->params[0].val_ul,
1679 gdb_ctx->params[1].val_ull,
1680 gdb_ctx->params[2].val_ull);
1681 if (res >= 0) {
Alex Bennéea346af32020-03-16 17:21:34 +00001682 put_packet("OK");
Jon Doron77f6ce52019-05-29 09:41:35 +03001683 return;
1684 } else if (res == -ENOSYS) {
Alex Bennéea346af32020-03-16 17:21:34 +00001685 put_packet("");
Jon Doron77f6ce52019-05-29 09:41:35 +03001686 return;
1687 }
1688
Alex Bennéea346af32020-03-16 17:21:34 +00001689 put_packet("E22");
Jon Doron77f6ce52019-05-29 09:41:35 +03001690}
1691
Alex Bennée94b2a622019-07-05 14:23:07 +01001692/*
1693 * handle_set/get_reg
1694 *
1695 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1696 * This works, but can be very slow. Anything new enough to understand
1697 * XML also knows how to use this properly. However to use this we
1698 * need to define a local XML file as well as be talking to a
1699 * reasonably modern gdb. Responding with an empty packet will cause
1700 * the remote gdb to fallback to older methods.
1701 */
1702
Jon Doron62b33202019-05-29 09:41:36 +03001703static void handle_set_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1704{
1705 int reg_size;
1706
1707 if (!gdb_has_xml) {
Alex Bennéea346af32020-03-16 17:21:34 +00001708 put_packet("");
Jon Doron62b33202019-05-29 09:41:36 +03001709 return;
1710 }
1711
1712 if (gdb_ctx->num_params != 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001713 put_packet("E22");
Jon Doron62b33202019-05-29 09:41:36 +03001714 return;
1715 }
1716
1717 reg_size = strlen(gdb_ctx->params[1].data) / 2;
1718 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[1].data, reg_size);
Alex Bennéea346af32020-03-16 17:21:34 +00001719 gdb_write_register(gdbserver_state.g_cpu, gdb_ctx->mem_buf,
Jon Doron62b33202019-05-29 09:41:36 +03001720 gdb_ctx->params[0].val_ull);
Alex Bennéea346af32020-03-16 17:21:34 +00001721 put_packet("OK");
Jon Doron62b33202019-05-29 09:41:36 +03001722}
1723
Jon Doron5d0e57b2019-05-29 09:41:37 +03001724static void handle_get_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1725{
1726 int reg_size;
1727
Jon Doron5d0e57b2019-05-29 09:41:37 +03001728 if (!gdb_has_xml) {
Alex Bennéea346af32020-03-16 17:21:34 +00001729 put_packet("");
Jon Doron5d0e57b2019-05-29 09:41:37 +03001730 return;
1731 }
1732
1733 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001734 put_packet("E14");
Jon Doron5d0e57b2019-05-29 09:41:37 +03001735 return;
1736 }
1737
Alex Bennéea346af32020-03-16 17:21:34 +00001738 reg_size = gdb_read_register(gdbserver_state.g_cpu, gdb_ctx->mem_buf,
Jon Doron5d0e57b2019-05-29 09:41:37 +03001739 gdb_ctx->params[0].val_ull);
1740 if (!reg_size) {
Alex Bennéea346af32020-03-16 17:21:34 +00001741 put_packet("E14");
Jon Doron5d0e57b2019-05-29 09:41:37 +03001742 return;
1743 }
1744
Alex Bennée308f9e82020-03-16 17:21:35 +00001745 memtohex(gdbserver_state.str_buf, gdb_ctx->mem_buf, reg_size);
1746 put_strbuf();
Jon Doron5d0e57b2019-05-29 09:41:37 +03001747}
1748
Jon Doroncc0ecc72019-05-29 09:41:38 +03001749static void handle_write_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1750{
1751 if (gdb_ctx->num_params != 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00001752 put_packet("E22");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001753 return;
1754 }
1755
1756 /* hextomem() reads 2*len bytes */
1757 if (gdb_ctx->params[1].val_ull > strlen(gdb_ctx->params[2].data) / 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001758 put_packet("E22");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001759 return;
1760 }
1761
1762 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[2].data,
1763 gdb_ctx->params[1].val_ull);
Alex Bennéea346af32020-03-16 17:21:34 +00001764 if (target_memory_rw_debug(gdbserver_state.g_cpu, gdb_ctx->params[0].val_ull,
Jon Doroncc0ecc72019-05-29 09:41:38 +03001765 gdb_ctx->mem_buf,
1766 gdb_ctx->params[1].val_ull, true)) {
Alex Bennéea346af32020-03-16 17:21:34 +00001767 put_packet("E14");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001768 return;
1769 }
1770
Alex Bennéea346af32020-03-16 17:21:34 +00001771 put_packet("OK");
Jon Doroncc0ecc72019-05-29 09:41:38 +03001772}
1773
Jon Doronda92e232019-05-29 09:41:39 +03001774static void handle_read_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1775{
1776 if (gdb_ctx->num_params != 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001777 put_packet("E22");
Jon Doronda92e232019-05-29 09:41:39 +03001778 return;
1779 }
1780
1781 /* memtohex() doubles the required space */
1782 if (gdb_ctx->params[1].val_ull > MAX_PACKET_LENGTH / 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00001783 put_packet("E22");
Jon Doronda92e232019-05-29 09:41:39 +03001784 return;
1785 }
1786
Alex Bennéea346af32020-03-16 17:21:34 +00001787 if (target_memory_rw_debug(gdbserver_state.g_cpu, gdb_ctx->params[0].val_ull,
Jon Doronda92e232019-05-29 09:41:39 +03001788 gdb_ctx->mem_buf,
1789 gdb_ctx->params[1].val_ull, false)) {
Alex Bennéea346af32020-03-16 17:21:34 +00001790 put_packet("E14");
Jon Doronda92e232019-05-29 09:41:39 +03001791 return;
1792 }
1793
Alex Bennée308f9e82020-03-16 17:21:35 +00001794 memtohex(gdbserver_state.str_buf, gdb_ctx->mem_buf, gdb_ctx->params[1].val_ull);
1795 put_strbuf();
Jon Doronda92e232019-05-29 09:41:39 +03001796}
1797
Jon Doron287ca122019-05-29 09:41:40 +03001798static void handle_write_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1799{
1800 target_ulong addr, len;
1801 uint8_t *registers;
1802 int reg_size;
1803
1804 if (!gdb_ctx->num_params) {
1805 return;
1806 }
1807
Alex Bennéea346af32020-03-16 17:21:34 +00001808 cpu_synchronize_state(gdbserver_state.g_cpu);
Jon Doron287ca122019-05-29 09:41:40 +03001809 registers = gdb_ctx->mem_buf;
1810 len = strlen(gdb_ctx->params[0].data) / 2;
1811 hextomem(registers, gdb_ctx->params[0].data, len);
Alex Bennéea346af32020-03-16 17:21:34 +00001812 for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs && len > 0;
Jon Doron287ca122019-05-29 09:41:40 +03001813 addr++) {
Alex Bennéea346af32020-03-16 17:21:34 +00001814 reg_size = gdb_write_register(gdbserver_state.g_cpu, registers, addr);
Jon Doron287ca122019-05-29 09:41:40 +03001815 len -= reg_size;
1816 registers += reg_size;
1817 }
Alex Bennéea346af32020-03-16 17:21:34 +00001818 put_packet("OK");
Jon Doron287ca122019-05-29 09:41:40 +03001819}
1820
Jon Doron397d1372019-05-29 09:41:41 +03001821static void handle_read_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1822{
1823 target_ulong addr, len;
1824
Alex Bennéea346af32020-03-16 17:21:34 +00001825 cpu_synchronize_state(gdbserver_state.g_cpu);
Jon Doron397d1372019-05-29 09:41:41 +03001826 len = 0;
Alex Bennéea346af32020-03-16 17:21:34 +00001827 for (addr = 0; addr < gdbserver_state.g_cpu->gdb_num_g_regs; addr++) {
1828 len += gdb_read_register(gdbserver_state.g_cpu, gdb_ctx->mem_buf + len,
Jon Doron397d1372019-05-29 09:41:41 +03001829 addr);
1830 }
1831
Alex Bennée308f9e82020-03-16 17:21:35 +00001832 memtohex(gdbserver_state.str_buf, gdb_ctx->mem_buf, len);
1833 put_strbuf();
Jon Doron397d1372019-05-29 09:41:41 +03001834}
1835
Jon Doron4b20fab2019-05-29 09:41:42 +03001836static void handle_file_io(GdbCmdContext *gdb_ctx, void *user_ctx)
1837{
Alex Bennéea346af32020-03-16 17:21:34 +00001838 if (gdb_ctx->num_params >= 1 && gdbserver_state.current_syscall_cb) {
Jon Doron4b20fab2019-05-29 09:41:42 +03001839 target_ulong ret, err;
1840
1841 ret = (target_ulong)gdb_ctx->params[0].val_ull;
Sandra Loosemorec6ee9522019-08-27 16:33:17 -06001842 if (gdb_ctx->num_params >= 2) {
1843 err = (target_ulong)gdb_ctx->params[1].val_ull;
1844 } else {
1845 err = 0;
1846 }
Alex Bennéea346af32020-03-16 17:21:34 +00001847 gdbserver_state.current_syscall_cb(gdbserver_state.c_cpu, ret, err);
1848 gdbserver_state.current_syscall_cb = NULL;
Jon Doron4b20fab2019-05-29 09:41:42 +03001849 }
1850
1851 if (gdb_ctx->num_params >= 3 && gdb_ctx->params[2].opcode == (uint8_t)'C') {
Alex Bennéea346af32020-03-16 17:21:34 +00001852 put_packet("T02");
Jon Doron4b20fab2019-05-29 09:41:42 +03001853 return;
1854 }
1855
Alex Bennéea346af32020-03-16 17:21:34 +00001856 gdb_continue();
Jon Doron4b20fab2019-05-29 09:41:42 +03001857}
1858
Jon Doron933f80d2019-05-29 09:41:43 +03001859static void handle_step(GdbCmdContext *gdb_ctx, void *user_ctx)
1860{
1861 if (gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00001862 gdb_set_cpu_pc((target_ulong)gdb_ctx->params[0].val_ull);
Jon Doron933f80d2019-05-29 09:41:43 +03001863 }
1864
Alex Bennéea346af32020-03-16 17:21:34 +00001865 cpu_single_step(gdbserver_state.c_cpu, sstep_flags);
1866 gdb_continue();
Jon Doron933f80d2019-05-29 09:41:43 +03001867}
1868
Jon Doron8536ec02019-05-29 09:41:44 +03001869static void handle_v_cont_query(GdbCmdContext *gdb_ctx, void *user_ctx)
1870{
Alex Bennéea346af32020-03-16 17:21:34 +00001871 put_packet("vCont;c;C;s;S");
Jon Doron8536ec02019-05-29 09:41:44 +03001872}
1873
1874static void handle_v_cont(GdbCmdContext *gdb_ctx, void *user_ctx)
1875{
1876 int res;
1877
1878 if (!gdb_ctx->num_params) {
1879 return;
1880 }
1881
Alex Bennéea346af32020-03-16 17:21:34 +00001882 res = gdb_handle_vcont(gdb_ctx->params[0].data);
Jon Doron8536ec02019-05-29 09:41:44 +03001883 if ((res == -EINVAL) || (res == -ERANGE)) {
Alex Bennéea346af32020-03-16 17:21:34 +00001884 put_packet("E22");
Jon Doron8536ec02019-05-29 09:41:44 +03001885 } else if (res) {
Alex Bennéea346af32020-03-16 17:21:34 +00001886 put_packet("");
Jon Doron8536ec02019-05-29 09:41:44 +03001887 }
1888}
1889
1890static void handle_v_attach(GdbCmdContext *gdb_ctx, void *user_ctx)
1891{
1892 GDBProcess *process;
1893 CPUState *cpu;
Jon Doron8536ec02019-05-29 09:41:44 +03001894
Alex Bennée308f9e82020-03-16 17:21:35 +00001895 g_string_assign(gdbserver_state.str_buf, "E22");
Jon Doron8536ec02019-05-29 09:41:44 +03001896 if (!gdb_ctx->num_params) {
1897 goto cleanup;
1898 }
1899
Alex Bennéea346af32020-03-16 17:21:34 +00001900 process = gdb_get_process(gdb_ctx->params[0].val_ul);
Jon Doron8536ec02019-05-29 09:41:44 +03001901 if (!process) {
1902 goto cleanup;
1903 }
1904
Alex Bennéea346af32020-03-16 17:21:34 +00001905 cpu = get_first_cpu_in_process(process);
Jon Doron8536ec02019-05-29 09:41:44 +03001906 if (!cpu) {
1907 goto cleanup;
1908 }
1909
1910 process->attached = true;
Alex Bennéea346af32020-03-16 17:21:34 +00001911 gdbserver_state.g_cpu = cpu;
1912 gdbserver_state.c_cpu = cpu;
Jon Doron8536ec02019-05-29 09:41:44 +03001913
Alex Bennée308f9e82020-03-16 17:21:35 +00001914 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
1915 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
1916 g_string_append_c(gdbserver_state.str_buf, ';');
Jon Doron8536ec02019-05-29 09:41:44 +03001917cleanup:
Alex Bennée308f9e82020-03-16 17:21:35 +00001918 put_strbuf();
Jon Doron8536ec02019-05-29 09:41:44 +03001919}
1920
1921static void handle_v_kill(GdbCmdContext *gdb_ctx, void *user_ctx)
1922{
1923 /* Kill the target */
Alex Bennéea346af32020-03-16 17:21:34 +00001924 put_packet("OK");
Jon Doron8536ec02019-05-29 09:41:44 +03001925 error_report("QEMU: Terminated via GDBstub");
1926 exit(0);
1927}
1928
1929static GdbCmdParseEntry gdb_v_commands_table[] = {
1930 /* Order is important if has same prefix */
1931 {
1932 .handler = handle_v_cont_query,
1933 .cmd = "Cont?",
1934 .cmd_startswith = 1
1935 },
1936 {
1937 .handler = handle_v_cont,
1938 .cmd = "Cont",
1939 .cmd_startswith = 1,
1940 .schema = "s0"
1941 },
1942 {
1943 .handler = handle_v_attach,
1944 .cmd = "Attach;",
1945 .cmd_startswith = 1,
1946 .schema = "l0"
1947 },
1948 {
1949 .handler = handle_v_kill,
1950 .cmd = "Kill;",
1951 .cmd_startswith = 1
1952 },
1953};
1954
1955static void handle_v_commands(GdbCmdContext *gdb_ctx, void *user_ctx)
1956{
1957 if (!gdb_ctx->num_params) {
1958 return;
1959 }
1960
Alex Bennéea346af32020-03-16 17:21:34 +00001961 if (process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron8536ec02019-05-29 09:41:44 +03001962 gdb_v_commands_table,
1963 ARRAY_SIZE(gdb_v_commands_table))) {
Alex Bennéea346af32020-03-16 17:21:34 +00001964 put_packet("");
Jon Doron8536ec02019-05-29 09:41:44 +03001965 }
1966}
1967
Jon Doron2704efa2019-05-29 09:41:45 +03001968static void handle_query_qemu_sstepbits(GdbCmdContext *gdb_ctx, void *user_ctx)
1969{
Alex Bennée308f9e82020-03-16 17:21:35 +00001970 g_string_printf(gdbserver_state.str_buf, "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1971 SSTEP_ENABLE, SSTEP_NOIRQ, SSTEP_NOTIMER);
1972 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03001973}
1974
1975static void handle_set_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
1976{
1977 if (!gdb_ctx->num_params) {
1978 return;
1979 }
1980
1981 sstep_flags = gdb_ctx->params[0].val_ul;
Alex Bennéea346af32020-03-16 17:21:34 +00001982 put_packet("OK");
Jon Doron2704efa2019-05-29 09:41:45 +03001983}
1984
1985static void handle_query_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
1986{
Alex Bennée308f9e82020-03-16 17:21:35 +00001987 g_string_printf(gdbserver_state.str_buf, "0x%x", sstep_flags);
1988 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03001989}
1990
1991static void handle_query_curr_tid(GdbCmdContext *gdb_ctx, void *user_ctx)
bellardb4608c02003-06-27 17:34:32 +00001992{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001993 CPUState *cpu;
Luc Michelc145eea2019-01-07 15:23:46 +00001994 GDBProcess *process;
Jon Doron2704efa2019-05-29 09:41:45 +03001995
1996 /*
1997 * "Current thread" remains vague in the spec, so always return
1998 * the first thread of the current process (gdb returns the
1999 * first thread).
2000 */
Alex Bennéea346af32020-03-16 17:21:34 +00002001 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
2002 cpu = get_first_cpu_in_process(process);
Alex Bennée308f9e82020-03-16 17:21:35 +00002003 g_string_assign(gdbserver_state.str_buf, "QC");
2004 gdb_append_thread_id(cpu, gdbserver_state.str_buf);
2005 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002006}
2007
2008static void handle_query_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
2009{
Alex Bennéea346af32020-03-16 17:21:34 +00002010 if (!gdbserver_state.query_cpu) {
2011 put_packet("l");
Jon Doron2704efa2019-05-29 09:41:45 +03002012 return;
2013 }
2014
Alex Bennée308f9e82020-03-16 17:21:35 +00002015 g_string_assign(gdbserver_state.str_buf, "m");
2016 gdb_append_thread_id(gdbserver_state.query_cpu, gdbserver_state.str_buf);
2017 put_strbuf();
Alex Bennéea346af32020-03-16 17:21:34 +00002018 gdbserver_state.query_cpu = gdb_next_attached_cpu(gdbserver_state.query_cpu);
Jon Doron2704efa2019-05-29 09:41:45 +03002019}
2020
2021static void handle_query_first_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
2022{
Alex Bennéea346af32020-03-16 17:21:34 +00002023 gdbserver_state.query_cpu = gdb_first_attached_cpu();
Jon Doron2704efa2019-05-29 09:41:45 +03002024 handle_query_threads(gdb_ctx, user_ctx);
2025}
2026
2027static void handle_query_thread_extra(GdbCmdContext *gdb_ctx, void *user_ctx)
2028{
Alex Bennée308f9e82020-03-16 17:21:35 +00002029 g_autoptr(GString) rs = g_string_new(NULL);
Jon Doron2704efa2019-05-29 09:41:45 +03002030 CPUState *cpu;
Jon Doron2704efa2019-05-29 09:41:45 +03002031
2032 if (!gdb_ctx->num_params ||
2033 gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
Alex Bennéea346af32020-03-16 17:21:34 +00002034 put_packet("E22");
Jon Doron2704efa2019-05-29 09:41:45 +03002035 return;
2036 }
2037
Alex Bennéea346af32020-03-16 17:21:34 +00002038 cpu = gdb_get_cpu(gdb_ctx->params[0].thread_id.pid,
Jon Doron2704efa2019-05-29 09:41:45 +03002039 gdb_ctx->params[0].thread_id.tid);
2040 if (!cpu) {
2041 return;
2042 }
2043
2044 cpu_synchronize_state(cpu);
2045
Alex Bennéea346af32020-03-16 17:21:34 +00002046 if (gdbserver_state.multiprocess && (gdbserver_state.process_num > 1)) {
Jon Doron2704efa2019-05-29 09:41:45 +03002047 /* Print the CPU model and name in multiprocess mode */
2048 ObjectClass *oc = object_get_class(OBJECT(cpu));
2049 const char *cpu_model = object_class_get_name(oc);
Alex Bennée308f9e82020-03-16 17:21:35 +00002050 g_autofree char *cpu_name;
2051 cpu_name = object_get_canonical_path_component(OBJECT(cpu));
2052 g_string_printf(rs, "%s %s [%s]", cpu_model, cpu_name,
2053 cpu->halted ? "halted " : "running");
Jon Doron2704efa2019-05-29 09:41:45 +03002054 } else {
Alex Bennée308f9e82020-03-16 17:21:35 +00002055 g_string_printf(rs, "CPU#%d [%s]", cpu->cpu_index,
Jon Doron2704efa2019-05-29 09:41:45 +03002056 cpu->halted ? "halted " : "running");
2057 }
Alex Bennée308f9e82020-03-16 17:21:35 +00002058 trace_gdbstub_op_extra_info(rs->str);
2059 memtohex(gdbserver_state.str_buf, (uint8_t *)rs->str, rs->len);
2060 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002061}
2062
2063#ifdef CONFIG_USER_ONLY
2064static void handle_query_offsets(GdbCmdContext *gdb_ctx, void *user_ctx)
2065{
2066 TaskState *ts;
2067
Alex Bennéea346af32020-03-16 17:21:34 +00002068 ts = gdbserver_state.c_cpu->opaque;
Alex Bennée308f9e82020-03-16 17:21:35 +00002069 g_string_printf(gdbserver_state.str_buf,
2070 "Text=" TARGET_ABI_FMT_lx
2071 ";Data=" TARGET_ABI_FMT_lx
2072 ";Bss=" TARGET_ABI_FMT_lx,
2073 ts->info->code_offset,
2074 ts->info->data_offset,
2075 ts->info->data_offset);
2076 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002077}
2078#else
2079static void handle_query_rcmd(GdbCmdContext *gdb_ctx, void *user_ctx)
2080{
2081 int len;
2082
2083 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00002084 put_packet("E22");
Jon Doron2704efa2019-05-29 09:41:45 +03002085 return;
2086 }
2087
2088 len = strlen(gdb_ctx->params[0].data);
2089 if (len % 2) {
Alex Bennéea346af32020-03-16 17:21:34 +00002090 put_packet("E01");
Jon Doron2704efa2019-05-29 09:41:45 +03002091 return;
2092 }
2093
2094 len = len / 2;
2095 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[0].data, len);
2096 gdb_ctx->mem_buf[len++] = 0;
Alex Bennéea346af32020-03-16 17:21:34 +00002097 qemu_chr_be_write(gdbserver_state.mon_chr, gdb_ctx->mem_buf, len);
2098 put_packet("OK");
Jon Doron2704efa2019-05-29 09:41:45 +03002099
2100}
2101#endif
2102
2103static void handle_query_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2104{
Andreas Färber5b24c642013-07-07 15:08:22 +02002105 CPUClass *cc;
Jon Doron2704efa2019-05-29 09:41:45 +03002106
Alex Bennée308f9e82020-03-16 17:21:35 +00002107 g_string_printf(gdbserver_state.str_buf, "PacketSize=%x", MAX_PACKET_LENGTH);
Jon Doron2704efa2019-05-29 09:41:45 +03002108 cc = CPU_GET_CLASS(first_cpu);
2109 if (cc->gdb_core_xml_file) {
Alex Bennée308f9e82020-03-16 17:21:35 +00002110 g_string_append(gdbserver_state.str_buf, ";qXfer:features:read+");
Jon Doron2704efa2019-05-29 09:41:45 +03002111 }
2112
2113 if (gdb_ctx->num_params &&
2114 strstr(gdb_ctx->params[0].data, "multiprocess+")) {
Alex Bennéea346af32020-03-16 17:21:34 +00002115 gdbserver_state.multiprocess = true;
Jon Doron2704efa2019-05-29 09:41:45 +03002116 }
2117
Alex Bennée308f9e82020-03-16 17:21:35 +00002118 g_string_append(gdbserver_state.str_buf, ";multiprocess+");
2119 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002120}
2121
2122static void handle_query_xfer_features(GdbCmdContext *gdb_ctx, void *user_ctx)
2123{
2124 GDBProcess *process;
2125 CPUClass *cc;
2126 unsigned long len, total_len, addr;
2127 const char *xml;
bellardb4608c02003-06-27 17:34:32 +00002128 const char *p;
Jon Doron2704efa2019-05-29 09:41:45 +03002129
2130 if (gdb_ctx->num_params < 3) {
Alex Bennéea346af32020-03-16 17:21:34 +00002131 put_packet("E22");
Jon Doron2704efa2019-05-29 09:41:45 +03002132 return;
2133 }
2134
Alex Bennéea346af32020-03-16 17:21:34 +00002135 process = gdb_get_cpu_process(gdbserver_state.g_cpu);
2136 cc = CPU_GET_CLASS(gdbserver_state.g_cpu);
Jon Doron2704efa2019-05-29 09:41:45 +03002137 if (!cc->gdb_core_xml_file) {
Alex Bennéea346af32020-03-16 17:21:34 +00002138 put_packet("");
Jon Doron2704efa2019-05-29 09:41:45 +03002139 return;
2140 }
2141
2142 gdb_has_xml = true;
2143 p = gdb_ctx->params[0].data;
Alex Bennéea346af32020-03-16 17:21:34 +00002144 xml = get_feature_xml(p, &p, process);
Jon Doron2704efa2019-05-29 09:41:45 +03002145 if (!xml) {
Alex Bennéea346af32020-03-16 17:21:34 +00002146 put_packet("E00");
Jon Doron2704efa2019-05-29 09:41:45 +03002147 return;
2148 }
2149
2150 addr = gdb_ctx->params[1].val_ul;
2151 len = gdb_ctx->params[2].val_ul;
2152 total_len = strlen(xml);
2153 if (addr > total_len) {
Alex Bennéea346af32020-03-16 17:21:34 +00002154 put_packet("E00");
Jon Doron2704efa2019-05-29 09:41:45 +03002155 return;
2156 }
2157
2158 if (len > (MAX_PACKET_LENGTH - 5) / 2) {
2159 len = (MAX_PACKET_LENGTH - 5) / 2;
2160 }
2161
2162 if (len < total_len - addr) {
Alex Bennée308f9e82020-03-16 17:21:35 +00002163 g_string_assign(gdbserver_state.str_buf, "m");
2164 memtox(gdbserver_state.str_buf, xml + addr, len);
Jon Doron2704efa2019-05-29 09:41:45 +03002165 } else {
Alex Bennée308f9e82020-03-16 17:21:35 +00002166 g_string_assign(gdbserver_state.str_buf, "l");
2167 memtox(gdbserver_state.str_buf, xml + addr, total_len - addr);
Jon Doron2704efa2019-05-29 09:41:45 +03002168 }
2169
Alex Bennée308f9e82020-03-16 17:21:35 +00002170 put_packet_binary(gdbserver_state.str_buf->str,
2171 gdbserver_state.str_buf->len, true);
Jon Doron2704efa2019-05-29 09:41:45 +03002172}
2173
2174static void handle_query_attached(GdbCmdContext *gdb_ctx, void *user_ctx)
2175{
Alex Bennéea346af32020-03-16 17:21:34 +00002176 put_packet(GDB_ATTACHED);
Jon Doron2704efa2019-05-29 09:41:45 +03002177}
2178
2179static void handle_query_qemu_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2180{
Alex Bennée308f9e82020-03-16 17:21:35 +00002181 g_string_printf(gdbserver_state.str_buf, "sstepbits;sstep");
Jon Doronab4752e2019-05-29 09:41:48 +03002182#ifndef CONFIG_USER_ONLY
Alex Bennée308f9e82020-03-16 17:21:35 +00002183 g_string_append(gdbserver_state.str_buf, ";PhyMemMode");
Jon Doronab4752e2019-05-29 09:41:48 +03002184#endif
Alex Bennée308f9e82020-03-16 17:21:35 +00002185 put_strbuf();
Jon Doron2704efa2019-05-29 09:41:45 +03002186}
2187
Jon Doronab4752e2019-05-29 09:41:48 +03002188#ifndef CONFIG_USER_ONLY
2189static void handle_query_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx,
2190 void *user_ctx)
2191{
Alex Bennée308f9e82020-03-16 17:21:35 +00002192 g_string_printf(gdbserver_state.str_buf, "%d", phy_memory_mode);
2193 put_strbuf();
Jon Doronab4752e2019-05-29 09:41:48 +03002194}
2195
2196static void handle_set_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx, void *user_ctx)
2197{
2198 if (!gdb_ctx->num_params) {
Alex Bennéea346af32020-03-16 17:21:34 +00002199 put_packet("E22");
Jon Doronab4752e2019-05-29 09:41:48 +03002200 return;
2201 }
2202
2203 if (!gdb_ctx->params[0].val_ul) {
2204 phy_memory_mode = 0;
2205 } else {
2206 phy_memory_mode = 1;
2207 }
Alex Bennéea346af32020-03-16 17:21:34 +00002208 put_packet("OK");
Jon Doronab4752e2019-05-29 09:41:48 +03002209}
2210#endif
2211
Jon Doron2704efa2019-05-29 09:41:45 +03002212static GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
2213 /* Order is important if has same prefix */
2214 {
2215 .handler = handle_query_qemu_sstepbits,
2216 .cmd = "qemu.sstepbits",
2217 },
2218 {
2219 .handler = handle_query_qemu_sstep,
2220 .cmd = "qemu.sstep",
2221 },
2222 {
2223 .handler = handle_set_qemu_sstep,
2224 .cmd = "qemu.sstep=",
2225 .cmd_startswith = 1,
2226 .schema = "l0"
2227 },
2228};
2229
2230static GdbCmdParseEntry gdb_gen_query_table[] = {
2231 {
2232 .handler = handle_query_curr_tid,
2233 .cmd = "C",
2234 },
2235 {
2236 .handler = handle_query_threads,
2237 .cmd = "sThreadInfo",
2238 },
2239 {
2240 .handler = handle_query_first_threads,
2241 .cmd = "fThreadInfo",
2242 },
2243 {
2244 .handler = handle_query_thread_extra,
2245 .cmd = "ThreadExtraInfo,",
2246 .cmd_startswith = 1,
2247 .schema = "t0"
2248 },
2249#ifdef CONFIG_USER_ONLY
2250 {
2251 .handler = handle_query_offsets,
2252 .cmd = "Offsets",
2253 },
2254#else
2255 {
2256 .handler = handle_query_rcmd,
2257 .cmd = "Rcmd,",
2258 .cmd_startswith = 1,
2259 .schema = "s0"
2260 },
2261#endif
2262 {
2263 .handler = handle_query_supported,
2264 .cmd = "Supported:",
2265 .cmd_startswith = 1,
2266 .schema = "s0"
2267 },
2268 {
2269 .handler = handle_query_supported,
2270 .cmd = "Supported",
2271 .schema = "s0"
2272 },
2273 {
2274 .handler = handle_query_xfer_features,
2275 .cmd = "Xfer:features:read:",
2276 .cmd_startswith = 1,
2277 .schema = "s:l,l0"
2278 },
2279 {
2280 .handler = handle_query_attached,
2281 .cmd = "Attached:",
2282 .cmd_startswith = 1
2283 },
2284 {
2285 .handler = handle_query_attached,
2286 .cmd = "Attached",
2287 },
2288 {
2289 .handler = handle_query_qemu_supported,
2290 .cmd = "qemu.Supported",
2291 },
Jon Doronab4752e2019-05-29 09:41:48 +03002292#ifndef CONFIG_USER_ONLY
2293 {
2294 .handler = handle_query_qemu_phy_mem_mode,
2295 .cmd = "qemu.PhyMemMode",
2296 },
2297#endif
Jon Doron2704efa2019-05-29 09:41:45 +03002298};
2299
2300static GdbCmdParseEntry gdb_gen_set_table[] = {
2301 /* Order is important if has same prefix */
2302 {
2303 .handler = handle_set_qemu_sstep,
2304 .cmd = "qemu.sstep:",
2305 .cmd_startswith = 1,
2306 .schema = "l0"
2307 },
Jon Doronab4752e2019-05-29 09:41:48 +03002308#ifndef CONFIG_USER_ONLY
2309 {
2310 .handler = handle_set_qemu_phy_mem_mode,
2311 .cmd = "qemu.PhyMemMode:",
2312 .cmd_startswith = 1,
2313 .schema = "l0"
2314 },
2315#endif
Jon Doron2704efa2019-05-29 09:41:45 +03002316};
2317
2318static void handle_gen_query(GdbCmdContext *gdb_ctx, void *user_ctx)
2319{
2320 if (!gdb_ctx->num_params) {
2321 return;
2322 }
2323
Alex Bennéea346af32020-03-16 17:21:34 +00002324 if (!process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron2704efa2019-05-29 09:41:45 +03002325 gdb_gen_query_set_common_table,
2326 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2327 return;
2328 }
2329
Alex Bennéea346af32020-03-16 17:21:34 +00002330 if (process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron2704efa2019-05-29 09:41:45 +03002331 gdb_gen_query_table,
2332 ARRAY_SIZE(gdb_gen_query_table))) {
Alex Bennéea346af32020-03-16 17:21:34 +00002333 put_packet("");
Jon Doron2704efa2019-05-29 09:41:45 +03002334 }
2335}
2336
2337static void handle_gen_set(GdbCmdContext *gdb_ctx, void *user_ctx)
2338{
2339 if (!gdb_ctx->num_params) {
2340 return;
2341 }
2342
Alex Bennéea346af32020-03-16 17:21:34 +00002343 if (!process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron2704efa2019-05-29 09:41:45 +03002344 gdb_gen_query_set_common_table,
2345 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2346 return;
2347 }
2348
Alex Bennéea346af32020-03-16 17:21:34 +00002349 if (process_string_cmd(NULL, gdb_ctx->params[0].data,
Jon Doron2704efa2019-05-29 09:41:45 +03002350 gdb_gen_set_table,
2351 ARRAY_SIZE(gdb_gen_set_table))) {
Alex Bennéea346af32020-03-16 17:21:34 +00002352 put_packet("");
Jon Doron2704efa2019-05-29 09:41:45 +03002353 }
2354}
2355
Jon Doron7009d572019-05-29 09:41:46 +03002356static void handle_target_halt(GdbCmdContext *gdb_ctx, void *user_ctx)
2357{
Alex Bennée308f9e82020-03-16 17:21:35 +00002358 g_string_printf(gdbserver_state.str_buf, "T%02xthread:", GDB_SIGNAL_TRAP);
2359 gdb_append_thread_id(gdbserver_state.c_cpu, gdbserver_state.str_buf);
2360 g_string_append_c(gdbserver_state.str_buf, ';');
2361 put_strbuf();
Jon Doron7009d572019-05-29 09:41:46 +03002362 /*
2363 * Remove all the breakpoints when this query is issued,
2364 * because gdb is doing an initial connect and the state
2365 * should be cleaned up.
2366 */
2367 gdb_breakpoint_remove_all();
2368}
2369
Alex Bennéea346af32020-03-16 17:21:34 +00002370static int gdb_handle_packet(const char *line_buf)
Jon Doron2704efa2019-05-29 09:41:45 +03002371{
Jon Doron3e2c1262019-05-29 09:41:30 +03002372 const GdbCmdParseEntry *cmd_parser = NULL;
ths3b46e622007-09-17 08:09:54 +00002373
Doug Gale5c9522b2017-12-02 20:30:37 -05002374 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01002375
Jon Doron3f1cbac2019-05-29 09:41:47 +03002376 switch (line_buf[0]) {
Luc Michel53fd6552019-01-07 15:23:46 +00002377 case '!':
Alex Bennéea346af32020-03-16 17:21:34 +00002378 put_packet("OK");
Luc Michel53fd6552019-01-07 15:23:46 +00002379 break;
bellard858693c2004-03-31 18:52:07 +00002380 case '?':
Jon Doron7009d572019-05-29 09:41:46 +03002381 {
2382 static const GdbCmdParseEntry target_halted_cmd_desc = {
2383 .handler = handle_target_halt,
2384 .cmd = "?",
2385 .cmd_startswith = 1
2386 };
2387 cmd_parser = &target_halted_cmd_desc;
2388 }
bellard858693c2004-03-31 18:52:07 +00002389 break;
2390 case 'c':
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002391 {
2392 static const GdbCmdParseEntry continue_cmd_desc = {
2393 .handler = handle_continue,
2394 .cmd = "c",
2395 .cmd_startswith = 1,
2396 .schema = "L0"
2397 };
2398 cmd_parser = &continue_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002399 }
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002400 break;
edgar_igl1f487ee2008-05-17 22:20:53 +00002401 case 'C':
Jon Doronccc47d52019-05-29 09:41:33 +03002402 {
2403 static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
2404 .handler = handle_cont_with_sig,
2405 .cmd = "C",
2406 .cmd_startswith = 1,
2407 .schema = "l0"
2408 };
2409 cmd_parser = &cont_with_sig_cmd_desc;
2410 }
2411 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002412 case 'v':
Jon Doron8536ec02019-05-29 09:41:44 +03002413 {
2414 static const GdbCmdParseEntry v_cmd_desc = {
2415 .handler = handle_v_commands,
2416 .cmd = "v",
2417 .cmd_startswith = 1,
2418 .schema = "s0"
2419 };
2420 cmd_parser = &v_cmd_desc;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002421 }
Jon Doron8536ec02019-05-29 09:41:44 +03002422 break;
edgar_igl7d03f822008-05-17 18:58:29 +00002423 case 'k':
2424 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002425 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00002426 exit(0);
2427 case 'D':
Jon Doron3e2c1262019-05-29 09:41:30 +03002428 {
2429 static const GdbCmdParseEntry detach_cmd_desc = {
2430 .handler = handle_detach,
2431 .cmd = "D",
2432 .cmd_startswith = 1,
2433 .schema = "?.l0"
2434 };
2435 cmd_parser = &detach_cmd_desc;
Luc Michel546f3c62019-01-07 15:23:46 +00002436 }
edgar_igl7d03f822008-05-17 18:58:29 +00002437 break;
bellard858693c2004-03-31 18:52:07 +00002438 case 's':
Jon Doron933f80d2019-05-29 09:41:43 +03002439 {
2440 static const GdbCmdParseEntry step_cmd_desc = {
2441 .handler = handle_step,
2442 .cmd = "s",
2443 .cmd_startswith = 1,
2444 .schema = "L0"
2445 };
2446 cmd_parser = &step_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002447 }
Jon Doron933f80d2019-05-29 09:41:43 +03002448 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002449 case 'F':
2450 {
Jon Doron4b20fab2019-05-29 09:41:42 +03002451 static const GdbCmdParseEntry file_io_cmd_desc = {
2452 .handler = handle_file_io,
2453 .cmd = "F",
2454 .cmd_startswith = 1,
2455 .schema = "L,L,o0"
2456 };
2457 cmd_parser = &file_io_cmd_desc;
pbrooka2d1eba2007-01-28 03:10:55 +00002458 }
2459 break;
bellard858693c2004-03-31 18:52:07 +00002460 case 'g':
Jon Doron397d1372019-05-29 09:41:41 +03002461 {
2462 static const GdbCmdParseEntry read_all_regs_cmd_desc = {
2463 .handler = handle_read_all_regs,
2464 .cmd = "g",
2465 .cmd_startswith = 1
2466 };
2467 cmd_parser = &read_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002468 }
bellard858693c2004-03-31 18:52:07 +00002469 break;
2470 case 'G':
Jon Doron287ca122019-05-29 09:41:40 +03002471 {
2472 static const GdbCmdParseEntry write_all_regs_cmd_desc = {
2473 .handler = handle_write_all_regs,
2474 .cmd = "G",
2475 .cmd_startswith = 1,
2476 .schema = "s0"
2477 };
2478 cmd_parser = &write_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002479 }
bellard858693c2004-03-31 18:52:07 +00002480 break;
2481 case 'm':
Jon Doronda92e232019-05-29 09:41:39 +03002482 {
2483 static const GdbCmdParseEntry read_mem_cmd_desc = {
2484 .handler = handle_read_mem,
2485 .cmd = "m",
2486 .cmd_startswith = 1,
2487 .schema = "L,L0"
2488 };
2489 cmd_parser = &read_mem_cmd_desc;
bellard6f970bd2005-12-05 19:55:19 +00002490 }
bellard858693c2004-03-31 18:52:07 +00002491 break;
2492 case 'M':
Jon Doroncc0ecc72019-05-29 09:41:38 +03002493 {
2494 static const GdbCmdParseEntry write_mem_cmd_desc = {
2495 .handler = handle_write_mem,
2496 .cmd = "M",
2497 .cmd_startswith = 1,
2498 .schema = "L,L:s0"
2499 };
2500 cmd_parser = &write_mem_cmd_desc;
Fabien Chouteau44520db2011-09-08 12:48:16 +02002501 }
bellard858693c2004-03-31 18:52:07 +00002502 break;
pbrook56aebc82008-10-11 17:55:29 +00002503 case 'p':
Jon Doron5d0e57b2019-05-29 09:41:37 +03002504 {
2505 static const GdbCmdParseEntry get_reg_cmd_desc = {
2506 .handler = handle_get_reg,
2507 .cmd = "p",
2508 .cmd_startswith = 1,
2509 .schema = "L0"
2510 };
2511 cmd_parser = &get_reg_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002512 }
2513 break;
2514 case 'P':
Jon Doron62b33202019-05-29 09:41:36 +03002515 {
2516 static const GdbCmdParseEntry set_reg_cmd_desc = {
2517 .handler = handle_set_reg,
2518 .cmd = "P",
2519 .cmd_startswith = 1,
2520 .schema = "L?s0"
2521 };
2522 cmd_parser = &set_reg_cmd_desc;
2523 }
pbrook56aebc82008-10-11 17:55:29 +00002524 break;
bellard858693c2004-03-31 18:52:07 +00002525 case 'Z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002526 {
2527 static const GdbCmdParseEntry insert_bp_cmd_desc = {
2528 .handler = handle_insert_bp,
2529 .cmd = "Z",
2530 .cmd_startswith = 1,
2531 .schema = "l?L?L0"
2532 };
2533 cmd_parser = &insert_bp_cmd_desc;
2534 }
2535 break;
bellard858693c2004-03-31 18:52:07 +00002536 case 'z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002537 {
2538 static const GdbCmdParseEntry remove_bp_cmd_desc = {
2539 .handler = handle_remove_bp,
2540 .cmd = "z",
2541 .cmd_startswith = 1,
2542 .schema = "l?L?L0"
2543 };
2544 cmd_parser = &remove_bp_cmd_desc;
2545 }
bellard858693c2004-03-31 18:52:07 +00002546 break;
aliguori880a7572008-11-18 20:30:24 +00002547 case 'H':
Jon Doron3a9651d2019-05-29 09:41:34 +03002548 {
2549 static const GdbCmdParseEntry set_thread_cmd_desc = {
2550 .handler = handle_set_thread,
2551 .cmd = "H",
2552 .cmd_startswith = 1,
2553 .schema = "o.t0"
2554 };
2555 cmd_parser = &set_thread_cmd_desc;
aliguori880a7572008-11-18 20:30:24 +00002556 }
2557 break;
2558 case 'T':
Jon Doron44ffded2019-05-29 09:41:31 +03002559 {
2560 static const GdbCmdParseEntry thread_alive_cmd_desc = {
2561 .handler = handle_thread_alive,
2562 .cmd = "T",
2563 .cmd_startswith = 1,
2564 .schema = "t0"
2565 };
2566 cmd_parser = &thread_alive_cmd_desc;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07002567 }
aliguori880a7572008-11-18 20:30:24 +00002568 break;
pbrook978efd62006-06-17 18:30:42 +00002569 case 'q':
Jon Doron2704efa2019-05-29 09:41:45 +03002570 {
2571 static const GdbCmdParseEntry gen_query_cmd_desc = {
2572 .handler = handle_gen_query,
2573 .cmd = "q",
2574 .cmd_startswith = 1,
2575 .schema = "s0"
2576 };
2577 cmd_parser = &gen_query_cmd_desc;
2578 }
2579 break;
edgar_igl60897d32008-05-09 08:25:14 +00002580 case 'Q':
Jon Doron2704efa2019-05-29 09:41:45 +03002581 {
2582 static const GdbCmdParseEntry gen_set_cmd_desc = {
2583 .handler = handle_gen_set,
2584 .cmd = "Q",
2585 .cmd_startswith = 1,
2586 .schema = "s0"
2587 };
2588 cmd_parser = &gen_set_cmd_desc;
edgar_igl60897d32008-05-09 08:25:14 +00002589 }
Jon Doron2704efa2019-05-29 09:41:45 +03002590 break;
bellard858693c2004-03-31 18:52:07 +00002591 default:
bellard858693c2004-03-31 18:52:07 +00002592 /* put empty packet */
Alex Bennéea346af32020-03-16 17:21:34 +00002593 put_packet("");
bellard858693c2004-03-31 18:52:07 +00002594 break;
2595 }
Jon Doron3e2c1262019-05-29 09:41:30 +03002596
Ramiro Polla2bdec392019-08-05 21:09:01 +02002597 if (cmd_parser) {
Alex Bennéea346af32020-03-16 17:21:34 +00002598 run_cmd_parser(line_buf, cmd_parser);
Ramiro Polla2bdec392019-08-05 21:09:01 +02002599 }
Jon Doron3e2c1262019-05-29 09:41:30 +03002600
bellard858693c2004-03-31 18:52:07 +00002601 return RS_IDLE;
2602}
2603
Andreas Färber64f6b342013-05-27 02:06:09 +02002604void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00002605{
Alex Bennéea346af32020-03-16 17:21:34 +00002606 GDBProcess *p = gdb_get_cpu_process(cpu);
Luc Michel160d8582019-01-07 15:23:46 +00002607
2608 if (!p->attached) {
2609 /*
2610 * Having a stop CPU corresponding to a process that is not attached
2611 * confuses GDB. So we ignore the request.
2612 */
2613 return;
2614 }
2615
Alex Bennée8d98c442020-03-16 17:21:33 +00002616 gdbserver_state.c_cpu = cpu;
2617 gdbserver_state.g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00002618}
2619
bellard1fddef42005-04-17 19:16:13 +00002620#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002621static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00002622{
Alex Bennéea346af32020-03-16 17:21:34 +00002623 CPUState *cpu = gdbserver_state.c_cpu;
Alex Bennée308f9e82020-03-16 17:21:35 +00002624 g_autoptr(GString) buf = g_string_new(NULL);
2625 g_autoptr(GString) tid = g_string_new(NULL);
aliguorid6fc1b32008-11-18 19:55:44 +00002626 const char *type;
bellard858693c2004-03-31 18:52:07 +00002627 int ret;
2628
Alex Bennéea346af32020-03-16 17:21:34 +00002629 if (running || gdbserver_state.state == RS_INACTIVE) {
Meador Ingecdb432b2012-03-15 17:49:45 +00002630 return;
2631 }
2632 /* Is there a GDB syscall waiting to be sent? */
Alex Bennéea346af32020-03-16 17:21:34 +00002633 if (gdbserver_state.current_syscall_cb) {
2634 put_packet(gdbserver_state.syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00002635 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01002636 }
Luc Michel95567c22019-01-07 15:23:46 +00002637
2638 if (cpu == NULL) {
2639 /* No process attached */
2640 return;
2641 }
2642
Alex Bennée308f9e82020-03-16 17:21:35 +00002643 gdb_append_thread_id(cpu, tid);
Luc Michel95567c22019-01-07 15:23:46 +00002644
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002645 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002646 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02002647 if (cpu->watchpoint_hit) {
2648 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00002649 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00002650 type = "r";
2651 break;
aliguoria1d1bb32008-11-18 20:07:32 +00002652 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00002653 type = "a";
2654 break;
2655 default:
2656 type = "";
2657 break;
2658 }
Doug Gale5c9522b2017-12-02 20:30:37 -05002659 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
2660 (target_ulong)cpu->watchpoint_hit->vaddr);
Alex Bennée308f9e82020-03-16 17:21:35 +00002661 g_string_printf(buf, "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
2662 GDB_SIGNAL_TRAP, tid->str, type,
2663 (target_ulong)cpu->watchpoint_hit->vaddr);
Andreas Färberff4700b2013-08-26 18:23:18 +02002664 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01002665 goto send_packet;
Doug Gale5c9522b2017-12-02 20:30:37 -05002666 } else {
2667 trace_gdbstub_hit_break();
pbrook6658ffb2007-03-16 23:58:11 +00002668 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07002669 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00002670 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01002671 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002672 case RUN_STATE_PAUSED:
Doug Gale5c9522b2017-12-02 20:30:37 -05002673 trace_gdbstub_hit_paused();
aliguori9781e042009-01-22 17:15:29 +00002674 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01002675 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002676 case RUN_STATE_SHUTDOWN:
Doug Gale5c9522b2017-12-02 20:30:37 -05002677 trace_gdbstub_hit_shutdown();
Jan Kiszka425189a2011-03-22 11:02:09 +01002678 ret = GDB_SIGNAL_QUIT;
2679 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002680 case RUN_STATE_IO_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002681 trace_gdbstub_hit_io_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002682 ret = GDB_SIGNAL_IO;
2683 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002684 case RUN_STATE_WATCHDOG:
Doug Gale5c9522b2017-12-02 20:30:37 -05002685 trace_gdbstub_hit_watchdog();
Jan Kiszka425189a2011-03-22 11:02:09 +01002686 ret = GDB_SIGNAL_ALRM;
2687 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002688 case RUN_STATE_INTERNAL_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002689 trace_gdbstub_hit_internal_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002690 ret = GDB_SIGNAL_ABRT;
2691 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002692 case RUN_STATE_SAVE_VM:
2693 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01002694 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002695 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01002696 ret = GDB_SIGNAL_XCPU;
2697 break;
2698 default:
Doug Gale5c9522b2017-12-02 20:30:37 -05002699 trace_gdbstub_hit_unknown(state);
Jan Kiszka425189a2011-03-22 11:02:09 +01002700 ret = GDB_SIGNAL_UNKNOWN;
2701 break;
bellardbbeb7b52006-04-23 18:42:15 +00002702 }
Jan Kiszka226d0072015-07-24 18:52:31 +02002703 gdb_set_stop_cpu(cpu);
Alex Bennée308f9e82020-03-16 17:21:35 +00002704 g_string_printf(buf, "T%02xthread:%s;", ret, tid->str);
Jan Kiszka425189a2011-03-22 11:02:09 +01002705
2706send_packet:
Alex Bennée308f9e82020-03-16 17:21:35 +00002707 put_packet(buf->str);
Jan Kiszka425189a2011-03-22 11:02:09 +01002708
2709 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02002710 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00002711}
bellard1fddef42005-04-17 19:16:13 +00002712#endif
bellard858693c2004-03-31 18:52:07 +00002713
pbrooka2d1eba2007-01-28 03:10:55 +00002714/* Send a gdb syscall request.
2715 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00002716 %x - target_ulong argument printed in hex.
2717 %lx - 64-bit argument printed in hex.
2718 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01002719void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00002720{
pbrooka2d1eba2007-01-28 03:10:55 +00002721 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00002722 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00002723 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00002724 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00002725
Alex Bennéea346af32020-03-16 17:21:34 +00002726 if (!gdbserver_state.init) {
pbrooka2d1eba2007-01-28 03:10:55 +00002727 return;
Alex Bennéea346af32020-03-16 17:21:34 +00002728 }
Alex Bennée8d98c442020-03-16 17:21:33 +00002729
2730 gdbserver_state.current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00002731#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002732 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00002733#endif
Alex Bennée8d98c442020-03-16 17:21:33 +00002734 p = &gdbserver_state.syscall_buf[0];
2735 p_end = &gdbserver_state.syscall_buf[sizeof(gdbserver_state.syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00002736 *(p++) = 'F';
2737 while (*fmt) {
2738 if (*fmt == '%') {
2739 fmt++;
2740 switch (*fmt++) {
2741 case 'x':
2742 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002743 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00002744 break;
pbrooka87295e2007-05-26 15:09:38 +00002745 case 'l':
2746 if (*(fmt++) != 'x')
2747 goto bad_format;
2748 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00002749 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00002750 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002751 case 's':
2752 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002753 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00002754 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00002755 break;
2756 default:
pbrooka87295e2007-05-26 15:09:38 +00002757 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002758 error_report("gdbstub: Bad syscall format string '%s'",
2759 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00002760 break;
2761 }
2762 } else {
2763 *(p++) = *(fmt++);
2764 }
2765 }
pbrook8a93e022007-08-06 13:19:15 +00002766 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00002767#ifdef CONFIG_USER_ONLY
Alex Bennéea346af32020-03-16 17:21:34 +00002768 put_packet(gdbserver_state.syscall_buf);
Peter Maydell4f710862018-05-15 19:19:58 +01002769 /* Return control to gdb for it to process the syscall request.
2770 * Since the protocol requires that gdb hands control back to us
2771 * using a "here are the results" F packet, we don't need to check
2772 * gdb_handlesig's return value (which is the signal to deliver if
2773 * execution was resumed via a continue packet).
2774 */
Alex Bennée8d98c442020-03-16 17:21:33 +00002775 gdb_handlesig(gdbserver_state.c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00002776#else
Meador Ingecdb432b2012-03-15 17:49:45 +00002777 /* In this case wait to send the syscall packet until notification that
2778 the CPU has stopped. This must be done because if the packet is sent
2779 now the reply from the syscall request could be received while the CPU
2780 is still in the running state, which can cause packets to be dropped
2781 and state transition 'T' packets to be sent while the syscall is still
2782 being processed. */
Alex Bennée8d98c442020-03-16 17:21:33 +00002783 qemu_cpu_kick(gdbserver_state.c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00002784#endif
2785}
2786
Peter Maydell19239b32015-09-07 10:39:27 +01002787void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
2788{
2789 va_list va;
2790
2791 va_start(va, fmt);
2792 gdb_do_syscallv(cb, fmt, va);
2793 va_end(va);
2794}
2795
Alex Bennéea346af32020-03-16 17:21:34 +00002796static void gdb_read_byte(uint8_t ch)
bellard858693c2004-03-31 18:52:07 +00002797{
ths60fe76f2007-12-16 03:02:09 +00002798 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00002799
bellard1fddef42005-04-17 19:16:13 +00002800#ifndef CONFIG_USER_ONLY
Alex Bennéea346af32020-03-16 17:21:34 +00002801 if (gdbserver_state.last_packet_len) {
pbrook4046d912007-01-28 01:53:16 +00002802 /* Waiting for a response to the last packet. If we see the start
2803 of a new command then abandon the previous response. */
2804 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002805 trace_gdbstub_err_got_nack();
Alex Bennéea346af32020-03-16 17:21:34 +00002806 put_buffer((uint8_t *)gdbserver_state.last_packet, gdbserver_state.last_packet_len);
Alex Bennée118e2262017-07-12 11:52:13 +01002807 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002808 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01002809 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002810 trace_gdbstub_io_got_unexpected(ch);
pbrook4046d912007-01-28 01:53:16 +00002811 }
Alex Bennée118e2262017-07-12 11:52:13 +01002812
pbrook4046d912007-01-28 01:53:16 +00002813 if (ch == '+' || ch == '$')
Alex Bennéea346af32020-03-16 17:21:34 +00002814 gdbserver_state.last_packet_len = 0;
pbrook4046d912007-01-28 01:53:16 +00002815 if (ch != '$')
2816 return;
2817 }
Luiz Capitulino13548692011-07-29 15:36:43 -03002818 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00002819 /* when the CPU is running, we cannot do anything except stop
2820 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002821 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00002822 } else
bellard1fddef42005-04-17 19:16:13 +00002823#endif
bellard41625032005-04-24 10:07:11 +00002824 {
Alex Bennéea346af32020-03-16 17:21:34 +00002825 switch(gdbserver_state.state) {
bellard858693c2004-03-31 18:52:07 +00002826 case RS_IDLE:
2827 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04002828 /* start of command packet */
Alex Bennéea346af32020-03-16 17:21:34 +00002829 gdbserver_state.line_buf_index = 0;
2830 gdbserver_state.line_sum = 0;
2831 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002832 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002833 trace_gdbstub_err_garbage(ch);
bellard4c3a88a2003-07-26 12:06:08 +00002834 }
2835 break;
bellard858693c2004-03-31 18:52:07 +00002836 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04002837 if (ch == '}') {
2838 /* start escape sequence */
Alex Bennéea346af32020-03-16 17:21:34 +00002839 gdbserver_state.state = RS_GETLINE_ESC;
2840 gdbserver_state.line_sum += ch;
Doug Gale4bf43122017-05-01 12:22:10 -04002841 } else if (ch == '*') {
2842 /* start run length encoding sequence */
Alex Bennéea346af32020-03-16 17:21:34 +00002843 gdbserver_state.state = RS_GETLINE_RLE;
2844 gdbserver_state.line_sum += ch;
Doug Gale4bf43122017-05-01 12:22:10 -04002845 } else if (ch == '#') {
2846 /* end of command, start of checksum*/
Alex Bennéea346af32020-03-16 17:21:34 +00002847 gdbserver_state.state = RS_CHKSUM1;
2848 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002849 trace_gdbstub_err_overrun();
Alex Bennéea346af32020-03-16 17:21:34 +00002850 gdbserver_state.state = RS_IDLE;
bellard858693c2004-03-31 18:52:07 +00002851 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002852 /* unescaped command character */
Alex Bennéea346af32020-03-16 17:21:34 +00002853 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch;
2854 gdbserver_state.line_sum += ch;
Doug Gale4bf43122017-05-01 12:22:10 -04002855 }
2856 break;
2857 case RS_GETLINE_ESC:
2858 if (ch == '#') {
2859 /* unexpected end of command in escape sequence */
Alex Bennéea346af32020-03-16 17:21:34 +00002860 gdbserver_state.state = RS_CHKSUM1;
2861 } else if (gdbserver_state.line_buf_index >= sizeof(gdbserver_state.line_buf) - 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04002862 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05002863 trace_gdbstub_err_overrun();
Alex Bennéea346af32020-03-16 17:21:34 +00002864 gdbserver_state.state = RS_IDLE;
Doug Gale4bf43122017-05-01 12:22:10 -04002865 } else {
2866 /* parse escaped character and leave escape state */
Alex Bennéea346af32020-03-16 17:21:34 +00002867 gdbserver_state.line_buf[gdbserver_state.line_buf_index++] = ch ^ 0x20;
2868 gdbserver_state.line_sum += ch;
2869 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002870 }
2871 break;
2872 case RS_GETLINE_RLE:
Markus Armbruster046aba12019-05-14 20:03:08 +02002873 /*
2874 * Run-length encoding is explained in "Debugging with GDB /
2875 * Appendix E GDB Remote Serial Protocol / Overview".
2876 */
2877 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
Doug Gale4bf43122017-05-01 12:22:10 -04002878 /* invalid RLE count encoding */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002879 trace_gdbstub_err_invalid_repeat(ch);
Alex Bennéea346af32020-03-16 17:21:34 +00002880 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002881 } else {
2882 /* decode repeat length */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002883 int repeat = ch - ' ' + 3;
Alex Bennéea346af32020-03-16 17:21:34 +00002884 if (gdbserver_state.line_buf_index + repeat >= sizeof(gdbserver_state.line_buf) - 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04002885 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05002886 trace_gdbstub_err_overrun();
Alex Bennéea346af32020-03-16 17:21:34 +00002887 gdbserver_state.state = RS_IDLE;
2888 } else if (gdbserver_state.line_buf_index < 1) {
Doug Gale4bf43122017-05-01 12:22:10 -04002889 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05002890 trace_gdbstub_err_invalid_rle();
Alex Bennéea346af32020-03-16 17:21:34 +00002891 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002892 } else {
2893 /* repeat the last character */
Alex Bennéea346af32020-03-16 17:21:34 +00002894 memset(gdbserver_state.line_buf + gdbserver_state.line_buf_index,
2895 gdbserver_state.line_buf[gdbserver_state.line_buf_index - 1], repeat);
2896 gdbserver_state.line_buf_index += repeat;
2897 gdbserver_state.line_sum += ch;
2898 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002899 }
bellard858693c2004-03-31 18:52:07 +00002900 }
2901 break;
2902 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04002903 /* get high hex digit of checksum */
2904 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002905 trace_gdbstub_err_checksum_invalid(ch);
Alex Bennéea346af32020-03-16 17:21:34 +00002906 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002907 break;
2908 }
Alex Bennéea346af32020-03-16 17:21:34 +00002909 gdbserver_state.line_buf[gdbserver_state.line_buf_index] = '\0';
2910 gdbserver_state.line_csum = fromhex(ch) << 4;
2911 gdbserver_state.state = RS_CHKSUM2;
bellard858693c2004-03-31 18:52:07 +00002912 break;
2913 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04002914 /* get low hex digit of checksum */
2915 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002916 trace_gdbstub_err_checksum_invalid(ch);
Alex Bennéea346af32020-03-16 17:21:34 +00002917 gdbserver_state.state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002918 break;
bellard858693c2004-03-31 18:52:07 +00002919 }
Alex Bennéea346af32020-03-16 17:21:34 +00002920 gdbserver_state.line_csum |= fromhex(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002921
Alex Bennéea346af32020-03-16 17:21:34 +00002922 if (gdbserver_state.line_csum != (gdbserver_state.line_sum & 0xff)) {
2923 trace_gdbstub_err_checksum_incorrect(gdbserver_state.line_sum, gdbserver_state.line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04002924 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00002925 reply = '-';
Alex Bennéea346af32020-03-16 17:21:34 +00002926 put_buffer(&reply, 1);
2927 gdbserver_state.state = RS_IDLE;
bellard858693c2004-03-31 18:52:07 +00002928 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002929 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00002930 reply = '+';
Alex Bennéea346af32020-03-16 17:21:34 +00002931 put_buffer(&reply, 1);
2932 gdbserver_state.state = gdb_handle_packet(gdbserver_state.line_buf);
bellard858693c2004-03-31 18:52:07 +00002933 }
bellardb4608c02003-06-27 17:34:32 +00002934 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002935 default:
2936 abort();
bellardb4608c02003-06-27 17:34:32 +00002937 }
2938 }
bellard858693c2004-03-31 18:52:07 +00002939}
2940
Paul Brook0e1c9c52010-06-16 13:03:51 +01002941/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002942void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01002943{
Paul Brook0e1c9c52010-06-16 13:03:51 +01002944 char buf[4];
2945
Alex Bennée8d98c442020-03-16 17:21:33 +00002946 if (!gdbserver_state.init) {
Paul Brook0e1c9c52010-06-16 13:03:51 +01002947 return;
2948 }
2949#ifdef CONFIG_USER_ONLY
Alex Bennée8d98c442020-03-16 17:21:33 +00002950 if (gdbserver_fd < 0 || gdbserver_state.fd < 0) {
Paul Brook0e1c9c52010-06-16 13:03:51 +01002951 return;
2952 }
2953#endif
2954
Doug Gale5c9522b2017-12-02 20:30:37 -05002955 trace_gdbstub_op_exiting((uint8_t)code);
2956
Paul Brook0e1c9c52010-06-16 13:03:51 +01002957 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
Alex Bennéea346af32020-03-16 17:21:34 +00002958 put_packet(buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002959
2960#ifndef CONFIG_USER_ONLY
Alex Bennée8d98c442020-03-16 17:21:33 +00002961 qemu_chr_fe_deinit(&gdbserver_state.chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002962#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01002963}
2964
Luc Michel8f468632019-01-07 15:23:45 +00002965/*
2966 * Create the process that will contain all the "orphan" CPUs (that are not
2967 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2968 * be attachable and thus will be invisible to the user.
2969 */
2970static void create_default_process(GDBState *s)
2971{
2972 GDBProcess *process;
2973 int max_pid = 0;
2974
Alex Bennéea346af32020-03-16 17:21:34 +00002975 if (gdbserver_state.process_num) {
Luc Michel8f468632019-01-07 15:23:45 +00002976 max_pid = s->processes[s->process_num - 1].pid;
2977 }
2978
2979 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2980 process = &s->processes[s->process_num - 1];
2981
2982 /* We need an available PID slot for this process */
2983 assert(max_pid < UINT32_MAX);
2984
2985 process->pid = max_pid + 1;
2986 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00002987 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00002988}
2989
bellard1fddef42005-04-17 19:16:13 +00002990#ifdef CONFIG_USER_ONLY
2991int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02002992gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00002993{
Andreas Färber5ca666c2013-06-24 19:20:57 +02002994 char buf[256];
2995 int n;
bellard1fddef42005-04-17 19:16:13 +00002996
Alex Bennée8d98c442020-03-16 17:21:33 +00002997 if (gdbserver_fd < 0 || gdbserver_state.fd < 0) {
Andreas Färber5ca666c2013-06-24 19:20:57 +02002998 return sig;
bellard1fddef42005-04-17 19:16:13 +00002999 }
3000
Andreas Färber5ca666c2013-06-24 19:20:57 +02003001 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02003002 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07003003 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00003004
Andreas Färber5ca666c2013-06-24 19:20:57 +02003005 if (sig != 0) {
3006 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
Alex Bennéea346af32020-03-16 17:21:34 +00003007 put_packet(buf);
Andreas Färber5ca666c2013-06-24 19:20:57 +02003008 }
3009 /* put_packet() might have detected that the peer terminated the
3010 connection. */
Alex Bennée8d98c442020-03-16 17:21:33 +00003011 if (gdbserver_state.fd < 0) {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003012 return sig;
3013 }
3014
3015 sig = 0;
Alex Bennée8d98c442020-03-16 17:21:33 +00003016 gdbserver_state.state = RS_IDLE;
3017 gdbserver_state.running_state = 0;
3018 while (gdbserver_state.running_state == 0) {
3019 n = read(gdbserver_state.fd, buf, 256);
Andreas Färber5ca666c2013-06-24 19:20:57 +02003020 if (n > 0) {
3021 int i;
3022
3023 for (i = 0; i < n; i++) {
Alex Bennéea346af32020-03-16 17:21:34 +00003024 gdb_read_byte(buf[i]);
Andreas Färber5ca666c2013-06-24 19:20:57 +02003025 }
Peter Wu5819e3e2016-06-05 16:35:48 +02003026 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003027 /* XXX: Connection closed. Should probably wait for another
3028 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02003029 if (n == 0) {
Alex Bennée8d98c442020-03-16 17:21:33 +00003030 close(gdbserver_state.fd);
Peter Wu5819e3e2016-06-05 16:35:48 +02003031 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003032 gdbserver_state.fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02003033 return sig;
bellard1fddef42005-04-17 19:16:13 +00003034 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02003035 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003036 sig = gdbserver_state.signal;
3037 gdbserver_state.signal = 0;
Andreas Färber5ca666c2013-06-24 19:20:57 +02003038 return sig;
bellard1fddef42005-04-17 19:16:13 +00003039}
bellarde9009672005-04-26 20:42:36 +00003040
aurel32ca587a82008-12-18 22:44:13 +00003041/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01003042void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00003043{
Andreas Färber5ca666c2013-06-24 19:20:57 +02003044 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00003045
Alex Bennée8d98c442020-03-16 17:21:33 +00003046 if (gdbserver_fd < 0 || gdbserver_state.fd < 0) {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003047 return;
3048 }
aurel32ca587a82008-12-18 22:44:13 +00003049
Andreas Färber5ca666c2013-06-24 19:20:57 +02003050 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
Alex Bennéea346af32020-03-16 17:21:34 +00003051 put_packet(buf);
aurel32ca587a82008-12-18 22:44:13 +00003052}
bellard1fddef42005-04-17 19:16:13 +00003053
Peter Maydell2f652222018-05-14 18:30:44 +01003054static bool gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00003055{
bellard858693c2004-03-31 18:52:07 +00003056 struct sockaddr_in sockaddr;
3057 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09003058 int fd;
bellard858693c2004-03-31 18:52:07 +00003059
3060 for(;;) {
3061 len = sizeof(sockaddr);
3062 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
3063 if (fd < 0 && errno != EINTR) {
3064 perror("accept");
Peter Maydell2f652222018-05-14 18:30:44 +01003065 return false;
bellard858693c2004-03-31 18:52:07 +00003066 } else if (fd >= 0) {
Peter Maydellf5bdd782018-05-14 18:30:43 +01003067 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00003068 break;
3069 }
3070 }
3071
3072 /* set short latency */
Peter Maydell2f652222018-05-14 18:30:44 +01003073 if (socket_set_nodelay(fd)) {
3074 perror("setsockopt");
Philippe Mathieu-Daudéead75d82018-05-24 19:34:58 -03003075 close(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01003076 return false;
3077 }
ths3b46e622007-09-17 08:09:54 +00003078
Alex Bennée8d98c442020-03-16 17:21:33 +00003079 init_gdbserver_state();
3080 create_default_process(&gdbserver_state);
3081 gdbserver_state.processes[0].attached = true;
Alex Bennéea346af32020-03-16 17:21:34 +00003082 gdbserver_state.c_cpu = gdb_first_attached_cpu();
Alex Bennée8d98c442020-03-16 17:21:33 +00003083 gdbserver_state.g_cpu = gdbserver_state.c_cpu;
3084 gdbserver_state.fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02003085 gdb_has_xml = false;
Peter Maydell2f652222018-05-14 18:30:44 +01003086 return true;
bellard858693c2004-03-31 18:52:07 +00003087}
3088
3089static int gdbserver_open(int port)
3090{
3091 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02003092 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00003093
3094 fd = socket(PF_INET, SOCK_STREAM, 0);
3095 if (fd < 0) {
3096 perror("socket");
3097 return -1;
3098 }
Peter Maydellf5bdd782018-05-14 18:30:43 +01003099 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00003100
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02003101 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00003102
3103 sockaddr.sin_family = AF_INET;
3104 sockaddr.sin_port = htons(port);
3105 sockaddr.sin_addr.s_addr = 0;
3106 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
3107 if (ret < 0) {
3108 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00003109 close(fd);
bellard858693c2004-03-31 18:52:07 +00003110 return -1;
3111 }
Peter Wu96165b92016-05-04 11:32:17 +02003112 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00003113 if (ret < 0) {
3114 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00003115 close(fd);
bellard858693c2004-03-31 18:52:07 +00003116 return -1;
3117 }
bellard858693c2004-03-31 18:52:07 +00003118 return fd;
3119}
3120
3121int gdbserver_start(int port)
3122{
3123 gdbserver_fd = gdbserver_open(port);
3124 if (gdbserver_fd < 0)
3125 return -1;
3126 /* accept connections */
Peter Maydell2f652222018-05-14 18:30:44 +01003127 if (!gdb_accept()) {
3128 close(gdbserver_fd);
3129 gdbserver_fd = -1;
3130 return -1;
3131 }
bellardb4608c02003-06-27 17:34:32 +00003132 return 0;
3133}
aurel322b1319c2008-12-18 22:44:04 +00003134
3135/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07003136void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00003137{
Alex Bennée8d98c442020-03-16 17:21:33 +00003138 if (gdbserver_fd < 0 || gdbserver_state.fd < 0) {
Andreas Färber75a34032013-09-02 16:57:02 +02003139 return;
3140 }
Alex Bennée8d98c442020-03-16 17:21:33 +00003141 close(gdbserver_state.fd);
3142 gdbserver_state.fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02003143 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02003144 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00003145}
pbrook4046d912007-01-28 01:53:16 +00003146#else
thsaa1f17c2007-07-11 22:48:58 +00003147static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00003148{
pbrook56aebc82008-10-11 17:55:29 +00003149 /* We can handle an arbitrarily large amount of data.
3150 Pick the maximum packet size, which is as good as anything. */
3151 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00003152}
3153
thsaa1f17c2007-07-11 22:48:58 +00003154static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00003155{
pbrook4046d912007-01-28 01:53:16 +00003156 int i;
3157
3158 for (i = 0; i < size; i++) {
Alex Bennéea346af32020-03-16 17:21:34 +00003159 gdb_read_byte(buf[i]);
pbrook4046d912007-01-28 01:53:16 +00003160 }
3161}
3162
Philippe Mathieu-Daudé083b2662019-12-18 18:20:09 +01003163static void gdb_chr_event(void *opaque, QEMUChrEvent event)
pbrook4046d912007-01-28 01:53:16 +00003164{
Luc Michel970ed902019-01-07 15:23:46 +00003165 int i;
3166 GDBState *s = (GDBState *) opaque;
3167
pbrook4046d912007-01-28 01:53:16 +00003168 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05303169 case CHR_EVENT_OPENED:
Luc Michel970ed902019-01-07 15:23:46 +00003170 /* Start with first process attached, others detached */
3171 for (i = 0; i < s->process_num; i++) {
3172 s->processes[i].attached = !i;
3173 }
3174
Alex Bennéea346af32020-03-16 17:21:34 +00003175 s->c_cpu = gdb_first_attached_cpu();
Luc Michel970ed902019-01-07 15:23:46 +00003176 s->g_cpu = s->c_cpu;
3177
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003178 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02003179 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00003180 break;
3181 default:
3182 break;
3183 }
3184}
3185
Alex Bennéea346af32020-03-16 17:21:34 +00003186static void gdb_monitor_output(const char *msg, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00003187{
Alex Bennée308f9e82020-03-16 17:21:35 +00003188 g_autoptr(GString) buf = g_string_new("O");
3189 memtohex(buf, (uint8_t *)msg, len);
3190 put_packet(buf->str);
aliguori8a34a0f2009-03-05 23:01:55 +00003191}
3192
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003193static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00003194{
3195 const char *p = (const char *)buf;
3196 int max_sz;
3197
Alex Bennée8d98c442020-03-16 17:21:33 +00003198 max_sz = (sizeof(gdbserver_state.last_packet) - 2) / 2;
aliguori8a34a0f2009-03-05 23:01:55 +00003199 for (;;) {
3200 if (len <= max_sz) {
Alex Bennéea346af32020-03-16 17:21:34 +00003201 gdb_monitor_output(p, len);
aliguori8a34a0f2009-03-05 23:01:55 +00003202 break;
3203 }
Alex Bennéea346af32020-03-16 17:21:34 +00003204 gdb_monitor_output(p, max_sz);
aliguori8a34a0f2009-03-05 23:01:55 +00003205 p += max_sz;
3206 len -= max_sz;
3207 }
3208 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