blob: 5c067594baef02b49b20fefdbd523eaa8ecaa8ac [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 {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200345 CPUState *c_cpu; /* current CPU for step/continue ops */
346 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200347 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000348 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000349 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000350 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400351 int line_sum; /* running checksum */
352 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000353 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000354 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000355 int signal;
bellard41625032005-04-24 10:07:11 +0000356#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000357 int fd;
bellard41625032005-04-24 10:07:11 +0000358 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000359#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300360 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300361 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000362#endif
Luc Michel8f468632019-01-07 15:23:45 +0000363 bool multiprocess;
364 GDBProcess *processes;
365 int process_num;
Meador Ingecdb432b2012-03-15 17:49:45 +0000366 char syscall_buf[256];
367 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000368} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000369
edgar_igl60897d32008-05-09 08:25:14 +0000370/* By default use no IRQs and no timers while single stepping so as to
371 * make single stepping like an ICE HW step.
372 */
373static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
374
aliguori880a7572008-11-18 20:30:24 +0000375static GDBState *gdbserver_state;
376
Andreas Färber5b50e792013-06-29 04:18:45 +0200377bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000378
bellard1fddef42005-04-17 19:16:13 +0000379#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000380/* XXX: This is not thread safe. Do we care? */
381static int gdbserver_fd = -1;
382
bellard858693c2004-03-31 18:52:07 +0000383static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000384{
385 uint8_t ch;
386 int ret;
387
388 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000389 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000390 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000391 if (errno == ECONNRESET)
392 s->fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200393 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000394 return -1;
395 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000396 close(s->fd);
397 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000398 return -1;
399 } else {
400 break;
401 }
402 }
403 return ch;
404}
pbrook4046d912007-01-28 01:53:16 +0000405#endif
bellardb4608c02003-06-27 17:34:32 +0000406
blueswir1654efcf2009-04-18 07:29:59 +0000407static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000408 GDB_SYS_UNKNOWN,
409 GDB_SYS_ENABLED,
410 GDB_SYS_DISABLED,
411} gdb_syscall_mode;
412
Liviu Ionescua38bb072014-12-11 12:07:48 +0000413/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000414int use_gdb_syscalls(void)
415{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100416 SemihostingTarget target = semihosting_get_target();
417 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000418 /* -semihosting-config target=native */
419 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100420 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000421 /* -semihosting-config target=gdb */
422 return true;
423 }
424
425 /* -semihosting-config target=auto */
426 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000427 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000428 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
429 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000430 }
431 return gdb_syscall_mode == GDB_SYS_ENABLED;
432}
433
edgar_iglba70a622008-03-14 06:10:42 +0000434/* Resume execution. */
435static inline void gdb_continue(GDBState *s)
436{
Doug Gale5c9522b2017-12-02 20:30:37 -0500437
edgar_iglba70a622008-03-14 06:10:42 +0000438#ifdef CONFIG_USER_ONLY
439 s->running_state = 1;
Doug Gale5c9522b2017-12-02 20:30:37 -0500440 trace_gdbstub_op_continue();
edgar_iglba70a622008-03-14 06:10:42 +0000441#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200442 if (!runstate_needs_reset()) {
Doug Gale5c9522b2017-12-02 20:30:37 -0500443 trace_gdbstub_op_continue();
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200444 vm_start();
445 }
edgar_iglba70a622008-03-14 06:10:42 +0000446#endif
447}
448
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100449/*
450 * Resume execution, per CPU actions. For user-mode emulation it's
451 * equivalent to gdb_continue.
452 */
453static int gdb_continue_partial(GDBState *s, char *newstates)
454{
455 CPUState *cpu;
456 int res = 0;
457#ifdef CONFIG_USER_ONLY
458 /*
459 * This is not exactly accurate, but it's an improvement compared to the
460 * previous situation, where only one CPU would be single-stepped.
461 */
462 CPU_FOREACH(cpu) {
463 if (newstates[cpu->cpu_index] == 's') {
Doug Gale5c9522b2017-12-02 20:30:37 -0500464 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100465 cpu_single_step(cpu, sstep_flags);
466 }
467 }
468 s->running_state = 1;
469#else
470 int flag = 0;
471
472 if (!runstate_needs_reset()) {
473 if (vm_prepare_start()) {
474 return 0;
475 }
476
477 CPU_FOREACH(cpu) {
478 switch (newstates[cpu->cpu_index]) {
479 case 0:
480 case 1:
481 break; /* nothing to do here */
482 case '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 cpu_resume(cpu);
486 flag = 1;
487 break;
488 case 'c':
Doug Gale5c9522b2017-12-02 20:30:37 -0500489 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100490 cpu_resume(cpu);
491 flag = 1;
492 break;
493 default:
494 res = -1;
495 break;
496 }
497 }
498 }
499 if (flag) {
500 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
501 }
502#endif
503 return res;
504}
505
bellard858693c2004-03-31 18:52:07 +0000506static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000507{
pbrook4046d912007-01-28 01:53:16 +0000508#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000509 int ret;
510
511 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000512 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000513 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200514 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000515 return;
516 } else {
517 buf += ret;
518 len -= ret;
519 }
520 }
pbrook4046d912007-01-28 01:53:16 +0000521#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100522 /* XXX this blocks entire thread. Rewrite to use
523 * qemu_chr_fe_write and background I/O callbacks */
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300524 qemu_chr_fe_write_all(&s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000525#endif
bellardb4608c02003-06-27 17:34:32 +0000526}
527
528static inline int fromhex(int v)
529{
530 if (v >= '0' && v <= '9')
531 return v - '0';
532 else if (v >= 'A' && v <= 'F')
533 return v - 'A' + 10;
534 else if (v >= 'a' && v <= 'f')
535 return v - 'a' + 10;
536 else
537 return 0;
538}
539
540static inline int tohex(int v)
541{
542 if (v < 10)
543 return v + '0';
544 else
545 return v - 10 + 'a';
546}
547
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -0300548/* writes 2*len+1 bytes in buf */
bellardb4608c02003-06-27 17:34:32 +0000549static void memtohex(char *buf, const uint8_t *mem, int len)
550{
551 int i, c;
552 char *q;
553 q = buf;
554 for(i = 0; i < len; i++) {
555 c = mem[i];
556 *q++ = tohex(c >> 4);
557 *q++ = tohex(c & 0xf);
558 }
559 *q = '\0';
560}
561
562static void hextomem(uint8_t *mem, const char *buf, int len)
563{
564 int i;
565
566 for(i = 0; i < len; i++) {
567 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
568 buf += 2;
569 }
570}
571
Doug Gale5c9522b2017-12-02 20:30:37 -0500572static void hexdump(const char *buf, int len,
573 void (*trace_fn)(size_t ofs, char const *text))
574{
575 char line_buffer[3 * 16 + 4 + 16 + 1];
576
577 size_t i;
578 for (i = 0; i < len || (i & 0xF); ++i) {
579 size_t byte_ofs = i & 15;
580
581 if (byte_ofs == 0) {
582 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
583 line_buffer[3 * 16 + 4 + 16] = 0;
584 }
585
586 size_t col_group = (i >> 2) & 3;
587 size_t hex_col = byte_ofs * 3 + col_group;
588 size_t txt_col = 3 * 16 + 4 + byte_ofs;
589
590 if (i < len) {
591 char value = buf[i];
592
593 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
594 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
595 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
596 ? value
597 : '.';
598 }
599
600 if (byte_ofs == 0xF)
601 trace_fn(i & -16, line_buffer);
602 }
603}
604
bellardb4608c02003-06-27 17:34:32 +0000605/* return -1 if error, 0 if OK */
Doug Gale5c9522b2017-12-02 20:30:37 -0500606static int put_packet_binary(GDBState *s, const char *buf, int len, bool dump)
bellardb4608c02003-06-27 17:34:32 +0000607{
pbrook56aebc82008-10-11 17:55:29 +0000608 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000609 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000610
Doug Gale5c9522b2017-12-02 20:30:37 -0500611 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
612 hexdump(buf, len, trace_gdbstub_io_binaryreply);
613 }
614
bellardb4608c02003-06-27 17:34:32 +0000615 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000616 p = s->last_packet;
617 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000618 memcpy(p, buf, len);
619 p += len;
bellardb4608c02003-06-27 17:34:32 +0000620 csum = 0;
621 for(i = 0; i < len; i++) {
622 csum += buf[i];
623 }
pbrook4046d912007-01-28 01:53:16 +0000624 *(p++) = '#';
625 *(p++) = tohex((csum >> 4) & 0xf);
626 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000627
pbrook4046d912007-01-28 01:53:16 +0000628 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000629 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000630
pbrook4046d912007-01-28 01:53:16 +0000631#ifdef CONFIG_USER_ONLY
632 i = get_char(s);
633 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000634 return -1;
pbrook4046d912007-01-28 01:53:16 +0000635 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000636 break;
pbrook4046d912007-01-28 01:53:16 +0000637#else
638 break;
639#endif
bellardb4608c02003-06-27 17:34:32 +0000640 }
641 return 0;
642}
643
pbrook56aebc82008-10-11 17:55:29 +0000644/* return -1 if error, 0 if OK */
645static int put_packet(GDBState *s, const char *buf)
646{
Doug Gale5c9522b2017-12-02 20:30:37 -0500647 trace_gdbstub_io_reply(buf);
pbrook56aebc82008-10-11 17:55:29 +0000648
Doug Gale5c9522b2017-12-02 20:30:37 -0500649 return put_packet_binary(s, buf, strlen(buf), false);
pbrook56aebc82008-10-11 17:55:29 +0000650}
651
pbrook56aebc82008-10-11 17:55:29 +0000652/* Encode data using the encoding for 'x' packets. */
653static int memtox(char *buf, const char *mem, int len)
654{
655 char *p = buf;
656 char c;
657
658 while (len--) {
659 c = *(mem++);
660 switch (c) {
661 case '#': case '$': case '*': case '}':
662 *(p++) = '}';
663 *(p++) = c ^ 0x20;
664 break;
665 default:
666 *(p++) = c;
667 break;
668 }
669 }
670 return p - buf;
671}
672
Luc Michel1a227332019-01-07 15:23:45 +0000673static uint32_t gdb_get_cpu_pid(const GDBState *s, CPUState *cpu)
674{
Luc Michel1a227332019-01-07 15:23:45 +0000675 /* TODO: In user mode, we should use the task state PID */
Peter Maydell46f5abc2019-01-29 11:46:06 +0000676 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
677 /* Return the default process' PID */
678 return s->processes[s->process_num - 1].pid;
679 }
680 return cpu->cluster_index + 1;
Luc Michel1a227332019-01-07 15:23:45 +0000681}
682
Luc Michel7d8c87d2019-01-07 15:23:45 +0000683static GDBProcess *gdb_get_process(const GDBState *s, uint32_t pid)
684{
685 int i;
686
687 if (!pid) {
688 /* 0 means any process, we take the first one */
689 return &s->processes[0];
690 }
691
692 for (i = 0; i < s->process_num; i++) {
693 if (s->processes[i].pid == pid) {
694 return &s->processes[i];
695 }
696 }
697
698 return NULL;
699}
700
701static GDBProcess *gdb_get_cpu_process(const GDBState *s, CPUState *cpu)
702{
703 return gdb_get_process(s, gdb_get_cpu_pid(s, cpu));
704}
705
706static CPUState *find_cpu(uint32_t thread_id)
707{
708 CPUState *cpu;
709
710 CPU_FOREACH(cpu) {
711 if (cpu_gdb_index(cpu) == thread_id) {
712 return cpu;
713 }
714 }
715
716 return NULL;
717}
718
Luc Michele40e5202019-01-07 15:23:46 +0000719static CPUState *get_first_cpu_in_process(const GDBState *s,
720 GDBProcess *process)
721{
722 CPUState *cpu;
723
724 CPU_FOREACH(cpu) {
725 if (gdb_get_cpu_pid(s, cpu) == process->pid) {
726 return cpu;
727 }
728 }
729
730 return NULL;
731}
732
733static CPUState *gdb_next_cpu_in_process(const GDBState *s, CPUState *cpu)
734{
735 uint32_t pid = gdb_get_cpu_pid(s, cpu);
736 cpu = CPU_NEXT(cpu);
737
738 while (cpu) {
739 if (gdb_get_cpu_pid(s, cpu) == pid) {
740 break;
741 }
742
743 cpu = CPU_NEXT(cpu);
744 }
745
746 return cpu;
747}
748
Luc Michele40e5202019-01-07 15:23:46 +0000749/* Return the cpu following @cpu, while ignoring unattached processes. */
750static CPUState *gdb_next_attached_cpu(const GDBState *s, CPUState *cpu)
751{
752 cpu = CPU_NEXT(cpu);
753
754 while (cpu) {
755 if (gdb_get_cpu_process(s, cpu)->attached) {
756 break;
757 }
758
759 cpu = CPU_NEXT(cpu);
760 }
761
762 return cpu;
763}
764
765/* Return the first attached cpu */
766static CPUState *gdb_first_attached_cpu(const GDBState *s)
767{
768 CPUState *cpu = first_cpu;
769 GDBProcess *process = gdb_get_cpu_process(s, cpu);
770
771 if (!process->attached) {
772 return gdb_next_attached_cpu(s, cpu);
773 }
774
775 return cpu;
776}
777
Luc Michelab65eed2019-01-29 11:46:03 +0000778static CPUState *gdb_get_cpu(const GDBState *s, uint32_t pid, uint32_t tid)
779{
780 GDBProcess *process;
781 CPUState *cpu;
782
783 if (!pid && !tid) {
784 /* 0 means any process/thread, we take the first attached one */
785 return gdb_first_attached_cpu(s);
786 } else if (pid && !tid) {
787 /* any thread in a specific process */
788 process = gdb_get_process(s, pid);
789
790 if (process == NULL) {
791 return NULL;
792 }
793
794 if (!process->attached) {
795 return NULL;
796 }
797
798 return get_first_cpu_in_process(s, process);
799 } else {
800 /* a specific thread */
801 cpu = find_cpu(tid);
802
803 if (cpu == NULL) {
804 return NULL;
805 }
806
807 process = gdb_get_cpu_process(s, cpu);
808
809 if (pid && process->pid != pid) {
810 return NULL;
811 }
812
813 if (!process->attached) {
814 return NULL;
815 }
816
817 return cpu;
818 }
819}
820
Luc Michelc145eea2019-01-07 15:23:46 +0000821static const char *get_feature_xml(const GDBState *s, const char *p,
822 const char **newp, GDBProcess *process)
pbrook56aebc82008-10-11 17:55:29 +0000823{
pbrook56aebc82008-10-11 17:55:29 +0000824 size_t len;
825 int i;
826 const char *name;
Luc Michelc145eea2019-01-07 15:23:46 +0000827 CPUState *cpu = get_first_cpu_in_process(s, process);
828 CPUClass *cc = CPU_GET_CLASS(cpu);
pbrook56aebc82008-10-11 17:55:29 +0000829
830 len = 0;
831 while (p[len] && p[len] != ':')
832 len++;
833 *newp = p + len;
834
835 name = NULL;
836 if (strncmp(p, "target.xml", len) == 0) {
Luc Michelc145eea2019-01-07 15:23:46 +0000837 char *buf = process->target_xml;
838 const size_t buf_sz = sizeof(process->target_xml);
pbrook56aebc82008-10-11 17:55:29 +0000839
Luc Michelc145eea2019-01-07 15:23:46 +0000840 /* Generate the XML description for this CPU. */
841 if (!buf[0]) {
842 GDBRegisterState *r;
843
844 pstrcat(buf, buf_sz,
David Hildenbrandb3820e62015-12-03 13:14:41 +0100845 "<?xml version=\"1.0\"?>"
846 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
847 "<target>");
848 if (cc->gdb_arch_name) {
849 gchar *arch = cc->gdb_arch_name(cpu);
Luc Michelc145eea2019-01-07 15:23:46 +0000850 pstrcat(buf, buf_sz, "<architecture>");
851 pstrcat(buf, buf_sz, arch);
852 pstrcat(buf, buf_sz, "</architecture>");
David Hildenbrandb3820e62015-12-03 13:14:41 +0100853 g_free(arch);
854 }
Luc Michelc145eea2019-01-07 15:23:46 +0000855 pstrcat(buf, buf_sz, "<xi:include href=\"");
856 pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
857 pstrcat(buf, buf_sz, "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200858 for (r = cpu->gdb_regs; r; r = r->next) {
Luc Michelc145eea2019-01-07 15:23:46 +0000859 pstrcat(buf, buf_sz, "<xi:include href=\"");
860 pstrcat(buf, buf_sz, r->xml);
861 pstrcat(buf, buf_sz, "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000862 }
Luc Michelc145eea2019-01-07 15:23:46 +0000863 pstrcat(buf, buf_sz, "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000864 }
Luc Michelc145eea2019-01-07 15:23:46 +0000865 return buf;
pbrook56aebc82008-10-11 17:55:29 +0000866 }
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100867 if (cc->gdb_get_dynamic_xml) {
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100868 char *xmlname = g_strndup(p, len);
869 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
870
871 g_free(xmlname);
872 if (xml) {
873 return xml;
874 }
875 }
pbrook56aebc82008-10-11 17:55:29 +0000876 for (i = 0; ; i++) {
877 name = xml_builtin[i][0];
878 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
879 break;
880 }
881 return name ? xml_builtin[i][1] : NULL;
882}
pbrook56aebc82008-10-11 17:55:29 +0000883
Andreas Färber385b9f02013-06-27 18:25:36 +0200884static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000885{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200886 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200887 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000888 GDBRegisterState *r;
889
Andreas Färbera0e372f2013-06-28 23:18:47 +0200890 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200891 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200892 }
pbrook56aebc82008-10-11 17:55:29 +0000893
Andreas Färbereac8b352013-06-28 21:11:37 +0200894 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000895 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
896 return r->get_reg(env, mem_buf, reg - r->base_reg);
897 }
898 }
899 return 0;
900}
901
Andreas Färber385b9f02013-06-27 18:25:36 +0200902static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000903{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200904 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200905 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000906 GDBRegisterState *r;
907
Andreas Färbera0e372f2013-06-28 23:18:47 +0200908 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200909 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200910 }
pbrook56aebc82008-10-11 17:55:29 +0000911
Andreas Färbereac8b352013-06-28 21:11:37 +0200912 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000913 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
914 return r->set_reg(env, mem_buf, reg - r->base_reg);
915 }
916 }
917 return 0;
918}
919
920/* Register a supplemental set of CPU registers. If g_pos is nonzero it
921 specifies the first register number and these registers are included in
922 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
923 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
924 */
925
Andreas Färber22169d42013-06-28 21:27:39 +0200926void gdb_register_coprocessor(CPUState *cpu,
927 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
928 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000929{
930 GDBRegisterState *s;
931 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000932
Andreas Färbereac8b352013-06-28 21:11:37 +0200933 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000934 while (*p) {
935 /* Check for duplicates. */
936 if (strcmp((*p)->xml, xml) == 0)
937 return;
938 p = &(*p)->next;
939 }
Stefan Weil9643c252011-10-18 22:25:38 +0200940
941 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200942 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200943 s->num_regs = num_regs;
944 s->get_reg = get_reg;
945 s->set_reg = set_reg;
946 s->xml = xml;
947
pbrook56aebc82008-10-11 17:55:29 +0000948 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200949 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000950 *p = s;
951 if (g_pos) {
952 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800953 error_report("Error: Bad gdb register numbering for '%s', "
954 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200955 } else {
956 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000957 }
958 }
959}
960
aliguoria1d1bb32008-11-18 20:07:32 +0000961#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100962/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
963static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
964{
965 static const int xlat[] = {
966 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
967 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
968 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
969 };
970
971 CPUClass *cc = CPU_GET_CLASS(cpu);
972 int cputype = xlat[gdbtype];
973
974 if (cc->gdb_stop_before_watchpoint) {
975 cputype |= BP_STOP_BEFORE_ACCESS;
976 }
977 return cputype;
978}
aliguoria1d1bb32008-11-18 20:07:32 +0000979#endif
980
Jon Doron77f6ce52019-05-29 09:41:35 +0300981static int gdb_breakpoint_insert(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +0000982{
Andreas Färber182735e2013-05-29 22:29:20 +0200983 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000984 int err = 0;
985
Andreas Färber62278812013-06-27 17:12:06 +0200986 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200987 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200988 }
aliguorie22a25c2009-03-12 20:12:48 +0000989
aliguoria1d1bb32008-11-18 20:07:32 +0000990 switch (type) {
991 case GDB_BREAKPOINT_SW:
992 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200993 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200994 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
995 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000996 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200997 }
aliguori880a7572008-11-18 20:30:24 +0000998 }
999 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001000#ifndef CONFIG_USER_ONLY
1001 case GDB_WATCHPOINT_WRITE:
1002 case GDB_WATCHPOINT_READ:
1003 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001004 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001005 err = cpu_watchpoint_insert(cpu, addr, len,
1006 xlat_gdb_type(cpu, type), NULL);
1007 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001008 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +01001009 }
aliguori880a7572008-11-18 20:30:24 +00001010 }
1011 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001012#endif
1013 default:
1014 return -ENOSYS;
1015 }
1016}
1017
Jon Doron77f6ce52019-05-29 09:41:35 +03001018static int gdb_breakpoint_remove(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +00001019{
Andreas Färber182735e2013-05-29 22:29:20 +02001020 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001021 int err = 0;
1022
Andreas Färber62278812013-06-27 17:12:06 +02001023 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001024 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001025 }
aliguorie22a25c2009-03-12 20:12:48 +00001026
aliguoria1d1bb32008-11-18 20:07:32 +00001027 switch (type) {
1028 case GDB_BREAKPOINT_SW:
1029 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001030 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001031 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
1032 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001033 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001034 }
aliguori880a7572008-11-18 20:30:24 +00001035 }
1036 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001037#ifndef CONFIG_USER_ONLY
1038 case GDB_WATCHPOINT_WRITE:
1039 case GDB_WATCHPOINT_READ:
1040 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001041 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001042 err = cpu_watchpoint_remove(cpu, addr, len,
1043 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +00001044 if (err)
1045 break;
1046 }
1047 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001048#endif
1049 default:
1050 return -ENOSYS;
1051 }
1052}
1053
Luc Michel546f3c62019-01-07 15:23:46 +00001054static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu)
1055{
1056 cpu_breakpoint_remove_all(cpu, BP_GDB);
1057#ifndef CONFIG_USER_ONLY
1058 cpu_watchpoint_remove_all(cpu, BP_GDB);
1059#endif
1060}
1061
1062static void gdb_process_breakpoint_remove_all(const GDBState *s, GDBProcess *p)
1063{
1064 CPUState *cpu = get_first_cpu_in_process(s, p);
1065
1066 while (cpu) {
1067 gdb_cpu_breakpoint_remove_all(cpu);
1068 cpu = gdb_next_cpu_in_process(s, cpu);
1069 }
1070}
1071
aliguori880a7572008-11-18 20:30:24 +00001072static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +00001073{
Andreas Färber182735e2013-05-29 22:29:20 +02001074 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001075
aliguorie22a25c2009-03-12 20:12:48 +00001076 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001077 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +00001078 return;
1079 }
1080
Andreas Färberbdc44642013-06-24 23:50:24 +02001081 CPU_FOREACH(cpu) {
Luc Michel546f3c62019-01-07 15:23:46 +00001082 gdb_cpu_breakpoint_remove_all(cpu);
aliguori880a7572008-11-18 20:30:24 +00001083 }
aliguoria1d1bb32008-11-18 20:07:32 +00001084}
1085
aurel32fab9d282009-04-08 21:29:37 +00001086static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
1087{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001088 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +02001089
1090 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -07001091 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +00001092}
1093
Luc Michel1a227332019-01-07 15:23:45 +00001094static char *gdb_fmt_thread_id(const GDBState *s, CPUState *cpu,
1095 char *buf, size_t buf_size)
1096{
1097 if (s->multiprocess) {
1098 snprintf(buf, buf_size, "p%02x.%02x",
1099 gdb_get_cpu_pid(s, cpu), cpu_gdb_index(cpu));
1100 } else {
1101 snprintf(buf, buf_size, "%02x", cpu_gdb_index(cpu));
1102 }
1103
1104 return buf;
1105}
1106
Luc Michel7d8c87d2019-01-07 15:23:45 +00001107typedef enum GDBThreadIdKind {
1108 GDB_ONE_THREAD = 0,
1109 GDB_ALL_THREADS, /* One process, all threads */
1110 GDB_ALL_PROCESSES,
1111 GDB_READ_THREAD_ERR
1112} GDBThreadIdKind;
1113
1114static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1115 uint32_t *pid, uint32_t *tid)
1116{
1117 unsigned long p, t;
1118 int ret;
1119
1120 if (*buf == 'p') {
1121 buf++;
1122 ret = qemu_strtoul(buf, &buf, 16, &p);
1123
1124 if (ret) {
1125 return GDB_READ_THREAD_ERR;
1126 }
1127
1128 /* Skip '.' */
1129 buf++;
1130 } else {
1131 p = 1;
1132 }
1133
1134 ret = qemu_strtoul(buf, &buf, 16, &t);
1135
1136 if (ret) {
1137 return GDB_READ_THREAD_ERR;
1138 }
1139
1140 *end_buf = buf;
1141
1142 if (p == -1) {
1143 return GDB_ALL_PROCESSES;
1144 }
1145
1146 if (pid) {
1147 *pid = p;
1148 }
1149
1150 if (t == -1) {
1151 return GDB_ALL_THREADS;
1152 }
1153
1154 if (tid) {
1155 *tid = t;
1156 }
1157
1158 return GDB_ONE_THREAD;
1159}
1160
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001161/**
1162 * gdb_handle_vcont - Parses and handles a vCont packet.
1163 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1164 * a format error, 0 on success.
1165 */
1166static int gdb_handle_vcont(GDBState *s, const char *p)
1167{
Luc Michele40e5202019-01-07 15:23:46 +00001168 int res, signal = 0;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001169 char cur_action;
1170 char *newstates;
1171 unsigned long tmp;
Luc Michele40e5202019-01-07 15:23:46 +00001172 uint32_t pid, tid;
1173 GDBProcess *process;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001174 CPUState *cpu;
Luc Michelc99ef792019-03-26 12:53:26 +00001175 GDBThreadIdKind kind;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001176#ifdef CONFIG_USER_ONLY
1177 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
1178
1179 CPU_FOREACH(cpu) {
1180 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1181 }
Like Xu5cc87672019-05-19 04:54:21 +08001182#else
1183 MachineState *ms = MACHINE(qdev_get_machine());
1184 unsigned int max_cpus = ms->smp.max_cpus;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001185#endif
1186 /* uninitialised CPUs stay 0 */
1187 newstates = g_new0(char, max_cpus);
1188
1189 /* mark valid CPUs with 1 */
1190 CPU_FOREACH(cpu) {
1191 newstates[cpu->cpu_index] = 1;
1192 }
1193
1194 /*
1195 * res keeps track of what error we are returning, with -ENOTSUP meaning
1196 * that the command is unknown or unsupported, thus returning an empty
1197 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1198 * or incorrect parameters passed.
1199 */
1200 res = 0;
1201 while (*p) {
1202 if (*p++ != ';') {
1203 res = -ENOTSUP;
1204 goto out;
1205 }
1206
1207 cur_action = *p++;
1208 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +01001209 cur_action = qemu_tolower(cur_action);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001210 res = qemu_strtoul(p + 1, &p, 16, &tmp);
1211 if (res) {
1212 goto out;
1213 }
1214 signal = gdb_signal_to_target(tmp);
1215 } else if (cur_action != 'c' && cur_action != 's') {
1216 /* unknown/invalid/unsupported command */
1217 res = -ENOTSUP;
1218 goto out;
1219 }
Luc Michele40e5202019-01-07 15:23:46 +00001220
Luc Michelc99ef792019-03-26 12:53:26 +00001221 if (*p == '\0' || *p == ';') {
1222 /*
1223 * No thread specifier, action is on "all threads". The
1224 * specification is unclear regarding the process to act on. We
1225 * choose all processes.
1226 */
1227 kind = GDB_ALL_PROCESSES;
1228 } else if (*p++ == ':') {
1229 kind = read_thread_id(p, &p, &pid, &tid);
1230 } else {
Luc Michele40e5202019-01-07 15:23:46 +00001231 res = -ENOTSUP;
1232 goto out;
1233 }
1234
Luc Michelc99ef792019-03-26 12:53:26 +00001235 switch (kind) {
Luc Michele40e5202019-01-07 15:23:46 +00001236 case GDB_READ_THREAD_ERR:
1237 res = -EINVAL;
1238 goto out;
1239
1240 case GDB_ALL_PROCESSES:
1241 cpu = gdb_first_attached_cpu(s);
1242 while (cpu) {
1243 if (newstates[cpu->cpu_index] == 1) {
1244 newstates[cpu->cpu_index] = cur_action;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001245 }
Luc Michele40e5202019-01-07 15:23:46 +00001246
1247 cpu = gdb_next_attached_cpu(s, cpu);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001248 }
Luc Michele40e5202019-01-07 15:23:46 +00001249 break;
1250
1251 case GDB_ALL_THREADS:
1252 process = gdb_get_process(s, pid);
1253
1254 if (!process->attached) {
1255 res = -EINVAL;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001256 goto out;
1257 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001258
Luc Michele40e5202019-01-07 15:23:46 +00001259 cpu = get_first_cpu_in_process(s, process);
1260 while (cpu) {
1261 if (newstates[cpu->cpu_index] == 1) {
1262 newstates[cpu->cpu_index] = cur_action;
1263 }
1264
1265 cpu = gdb_next_cpu_in_process(s, cpu);
1266 }
1267 break;
1268
1269 case GDB_ONE_THREAD:
1270 cpu = gdb_get_cpu(s, pid, tid);
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001271
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001272 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001273 if (!cpu) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001274 res = -EINVAL;
1275 goto out;
1276 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001277
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001278 /* only use if no previous match occourred */
1279 if (newstates[cpu->cpu_index] == 1) {
1280 newstates[cpu->cpu_index] = cur_action;
1281 }
Luc Michele40e5202019-01-07 15:23:46 +00001282 break;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001283 }
1284 }
1285 s->signal = signal;
1286 gdb_continue_partial(s, newstates);
1287
1288out:
1289 g_free(newstates);
1290
1291 return res;
1292}
1293
Jon Dorond14055d2019-05-29 09:41:29 +03001294typedef union GdbCmdVariant {
1295 const char *data;
1296 uint8_t opcode;
1297 unsigned long val_ul;
1298 unsigned long long val_ull;
1299 struct {
1300 GDBThreadIdKind kind;
1301 uint32_t pid;
1302 uint32_t tid;
1303 } thread_id;
1304} GdbCmdVariant;
1305
1306static const char *cmd_next_param(const char *param, const char delimiter)
1307{
1308 static const char all_delimiters[] = ",;:=";
1309 char curr_delimiters[2] = {0};
1310 const char *delimiters;
1311
1312 if (delimiter == '?') {
1313 delimiters = all_delimiters;
1314 } else if (delimiter == '0') {
1315 return strchr(param, '\0');
1316 } else if (delimiter == '.' && *param) {
1317 return param + 1;
1318 } else {
1319 curr_delimiters[0] = delimiter;
1320 delimiters = curr_delimiters;
1321 }
1322
1323 param += strcspn(param, delimiters);
1324 if (*param) {
1325 param++;
1326 }
1327 return param;
1328}
1329
1330static int cmd_parse_params(const char *data, const char *schema,
1331 GdbCmdVariant *params, int *num_params)
1332{
1333 int curr_param;
1334 const char *curr_schema, *curr_data;
1335
1336 *num_params = 0;
1337
1338 if (!schema) {
1339 return 0;
1340 }
1341
1342 curr_schema = schema;
1343 curr_param = 0;
1344 curr_data = data;
1345 while (curr_schema[0] && curr_schema[1] && *curr_data) {
1346 switch (curr_schema[0]) {
1347 case 'l':
1348 if (qemu_strtoul(curr_data, &curr_data, 16,
1349 &params[curr_param].val_ul)) {
1350 return -EINVAL;
1351 }
1352 curr_param++;
1353 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1354 break;
1355 case 'L':
1356 if (qemu_strtou64(curr_data, &curr_data, 16,
1357 (uint64_t *)&params[curr_param].val_ull)) {
1358 return -EINVAL;
1359 }
1360 curr_param++;
1361 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1362 break;
1363 case 's':
1364 params[curr_param].data = curr_data;
1365 curr_param++;
1366 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1367 break;
1368 case 'o':
1369 params[curr_param].opcode = *(uint8_t *)curr_data;
1370 curr_param++;
1371 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1372 break;
1373 case 't':
1374 params[curr_param].thread_id.kind =
1375 read_thread_id(curr_data, &curr_data,
1376 &params[curr_param].thread_id.pid,
1377 &params[curr_param].thread_id.tid);
1378 curr_param++;
1379 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1380 break;
1381 case '?':
1382 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1383 break;
1384 default:
1385 return -EINVAL;
1386 }
1387 curr_schema += 2;
1388 }
1389
1390 *num_params = curr_param;
1391 return 0;
1392}
1393
1394typedef struct GdbCmdContext {
1395 GDBState *s;
1396 GdbCmdVariant *params;
1397 int num_params;
1398 uint8_t mem_buf[MAX_PACKET_LENGTH];
1399 char str_buf[MAX_PACKET_LENGTH + 1];
1400} GdbCmdContext;
1401
1402typedef void (*GdbCmdHandler)(GdbCmdContext *gdb_ctx, void *user_ctx);
1403
1404/*
1405 * cmd_startswith -> cmd is compared using startswith
1406 *
1407 *
1408 * schema definitions:
1409 * Each schema parameter entry consists of 2 chars,
1410 * the first char represents the parameter type handling
1411 * the second char represents the delimiter for the next parameter
1412 *
1413 * Currently supported schema types:
1414 * 'l' -> unsigned long (stored in .val_ul)
1415 * 'L' -> unsigned long long (stored in .val_ull)
1416 * 's' -> string (stored in .data)
1417 * 'o' -> single char (stored in .opcode)
1418 * 't' -> thread id (stored in .thread_id)
1419 * '?' -> skip according to delimiter
1420 *
1421 * Currently supported delimiters:
1422 * '?' -> Stop at any delimiter (",;:=\0")
1423 * '0' -> Stop at "\0"
1424 * '.' -> Skip 1 char unless reached "\0"
1425 * Any other value is treated as the delimiter value itself
1426 */
1427typedef struct GdbCmdParseEntry {
1428 GdbCmdHandler handler;
1429 const char *cmd;
1430 bool cmd_startswith;
1431 const char *schema;
1432} GdbCmdParseEntry;
1433
1434static inline int startswith(const char *string, const char *pattern)
1435{
1436 return !strncmp(string, pattern, strlen(pattern));
1437}
1438
Jon Dorond14055d2019-05-29 09:41:29 +03001439static int process_string_cmd(GDBState *s, void *user_ctx, const char *data,
1440 const GdbCmdParseEntry *cmds, int num_cmds)
1441{
1442 int i, schema_len, max_num_params = 0;
1443 GdbCmdContext gdb_ctx;
1444
1445 if (!cmds) {
1446 return -1;
1447 }
1448
1449 for (i = 0; i < num_cmds; i++) {
1450 const GdbCmdParseEntry *cmd = &cmds[i];
1451 g_assert(cmd->handler && cmd->cmd);
1452
1453 if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
1454 (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
1455 continue;
1456 }
1457
1458 if (cmd->schema) {
1459 schema_len = strlen(cmd->schema);
1460 if (schema_len % 2) {
1461 return -2;
1462 }
1463
1464 max_num_params = schema_len / 2;
1465 }
1466
1467 gdb_ctx.params =
1468 (GdbCmdVariant *)alloca(sizeof(*gdb_ctx.params) * max_num_params);
1469 memset(gdb_ctx.params, 0, sizeof(*gdb_ctx.params) * max_num_params);
1470
1471 if (cmd_parse_params(&data[strlen(cmd->cmd)], cmd->schema,
1472 gdb_ctx.params, &gdb_ctx.num_params)) {
1473 return -1;
1474 }
1475
1476 gdb_ctx.s = s;
1477 cmd->handler(&gdb_ctx, user_ctx);
1478 return 0;
1479 }
1480
1481 return -1;
1482}
1483
Jon Doron3e2c1262019-05-29 09:41:30 +03001484static void run_cmd_parser(GDBState *s, const char *data,
1485 const GdbCmdParseEntry *cmd)
1486{
1487 if (!data) {
1488 return;
1489 }
1490
1491 /* In case there was an error during the command parsing we must
1492 * send a NULL packet to indicate the command is not supported */
1493 if (process_string_cmd(s, NULL, data, cmd, 1)) {
1494 put_packet(s, "");
1495 }
1496}
1497
1498static void handle_detach(GdbCmdContext *gdb_ctx, void *user_ctx)
1499{
1500 GDBProcess *process;
1501 GDBState *s = gdb_ctx->s;
1502 uint32_t pid = 1;
1503
1504 if (s->multiprocess) {
1505 if (!gdb_ctx->num_params) {
1506 put_packet(s, "E22");
1507 return;
1508 }
1509
1510 pid = gdb_ctx->params[0].val_ul;
1511 }
1512
1513 process = gdb_get_process(s, pid);
1514 gdb_process_breakpoint_remove_all(s, process);
1515 process->attached = false;
1516
1517 if (pid == gdb_get_cpu_pid(s, s->c_cpu)) {
1518 s->c_cpu = gdb_first_attached_cpu(s);
1519 }
1520
1521 if (pid == gdb_get_cpu_pid(s, s->g_cpu)) {
1522 s->g_cpu = gdb_first_attached_cpu(s);
1523 }
1524
1525 if (!s->c_cpu) {
1526 /* No more process attached */
1527 gdb_syscall_mode = GDB_SYS_DISABLED;
1528 gdb_continue(s);
1529 }
1530 put_packet(s, "OK");
1531}
1532
Jon Doron44ffded2019-05-29 09:41:31 +03001533static void handle_thread_alive(GdbCmdContext *gdb_ctx, void *user_ctx)
1534{
1535 CPUState *cpu;
1536
1537 if (!gdb_ctx->num_params) {
1538 put_packet(gdb_ctx->s, "E22");
1539 return;
1540 }
1541
1542 if (gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
1543 put_packet(gdb_ctx->s, "E22");
1544 return;
1545 }
1546
1547 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[0].thread_id.pid,
1548 gdb_ctx->params[0].thread_id.tid);
1549 if (!cpu) {
1550 put_packet(gdb_ctx->s, "E22");
1551 return;
1552 }
1553
1554 put_packet(gdb_ctx->s, "OK");
1555}
1556
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001557static void handle_continue(GdbCmdContext *gdb_ctx, void *user_ctx)
1558{
1559 if (gdb_ctx->num_params) {
1560 gdb_set_cpu_pc(gdb_ctx->s, gdb_ctx->params[0].val_ull);
1561 }
1562
1563 gdb_ctx->s->signal = 0;
1564 gdb_continue(gdb_ctx->s);
1565}
1566
Jon Doronccc47d52019-05-29 09:41:33 +03001567static void handle_cont_with_sig(GdbCmdContext *gdb_ctx, void *user_ctx)
1568{
1569 unsigned long signal = 0;
1570
1571 /*
1572 * Note: C sig;[addr] is currently unsupported and we simply
1573 * omit the addr parameter
1574 */
1575 if (gdb_ctx->num_params) {
1576 signal = gdb_ctx->params[0].val_ul;
1577 }
1578
1579 gdb_ctx->s->signal = gdb_signal_to_target(signal);
1580 if (gdb_ctx->s->signal == -1) {
1581 gdb_ctx->s->signal = 0;
1582 }
1583 gdb_continue(gdb_ctx->s);
1584}
1585
Jon Doron3a9651d2019-05-29 09:41:34 +03001586static void handle_set_thread(GdbCmdContext *gdb_ctx, void *user_ctx)
1587{
1588 CPUState *cpu;
1589
1590 if (gdb_ctx->num_params != 2) {
1591 put_packet(gdb_ctx->s, "E22");
1592 return;
1593 }
1594
1595 if (gdb_ctx->params[1].thread_id.kind == GDB_READ_THREAD_ERR) {
1596 put_packet(gdb_ctx->s, "E22");
1597 return;
1598 }
1599
1600 if (gdb_ctx->params[1].thread_id.kind != GDB_ONE_THREAD) {
1601 put_packet(gdb_ctx->s, "OK");
1602 return;
1603 }
1604
1605 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[1].thread_id.pid,
1606 gdb_ctx->params[1].thread_id.tid);
1607 if (!cpu) {
1608 put_packet(gdb_ctx->s, "E22");
1609 return;
1610 }
1611
1612 /*
1613 * Note: This command is deprecated and modern gdb's will be using the
1614 * vCont command instead.
1615 */
1616 switch (gdb_ctx->params[0].opcode) {
1617 case 'c':
1618 gdb_ctx->s->c_cpu = cpu;
1619 put_packet(gdb_ctx->s, "OK");
1620 break;
1621 case 'g':
1622 gdb_ctx->s->g_cpu = cpu;
1623 put_packet(gdb_ctx->s, "OK");
1624 break;
1625 default:
1626 put_packet(gdb_ctx->s, "E22");
1627 break;
1628 }
1629}
1630
Jon Doron77f6ce52019-05-29 09:41:35 +03001631static void handle_insert_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1632{
1633 int res;
1634
1635 if (gdb_ctx->num_params != 3) {
1636 put_packet(gdb_ctx->s, "E22");
1637 return;
1638 }
1639
1640 res = gdb_breakpoint_insert(gdb_ctx->params[0].val_ul,
1641 gdb_ctx->params[1].val_ull,
1642 gdb_ctx->params[2].val_ull);
1643 if (res >= 0) {
1644 put_packet(gdb_ctx->s, "OK");
1645 return;
1646 } else if (res == -ENOSYS) {
1647 put_packet(gdb_ctx->s, "");
1648 return;
1649 }
1650
1651 put_packet(gdb_ctx->s, "E22");
1652}
1653
1654static void handle_remove_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1655{
1656 int res;
1657
1658 if (gdb_ctx->num_params != 3) {
1659 put_packet(gdb_ctx->s, "E22");
1660 return;
1661 }
1662
1663 res = gdb_breakpoint_remove(gdb_ctx->params[0].val_ul,
1664 gdb_ctx->params[1].val_ull,
1665 gdb_ctx->params[2].val_ull);
1666 if (res >= 0) {
1667 put_packet(gdb_ctx->s, "OK");
1668 return;
1669 } else if (res == -ENOSYS) {
1670 put_packet(gdb_ctx->s, "");
1671 return;
1672 }
1673
1674 put_packet(gdb_ctx->s, "E22");
1675}
1676
Alex Bennée94b2a622019-07-05 14:23:07 +01001677/*
1678 * handle_set/get_reg
1679 *
1680 * Older gdb are really dumb, and don't use 'G/g' if 'P/p' is available.
1681 * This works, but can be very slow. Anything new enough to understand
1682 * XML also knows how to use this properly. However to use this we
1683 * need to define a local XML file as well as be talking to a
1684 * reasonably modern gdb. Responding with an empty packet will cause
1685 * the remote gdb to fallback to older methods.
1686 */
1687
Jon Doron62b33202019-05-29 09:41:36 +03001688static void handle_set_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1689{
1690 int reg_size;
1691
1692 if (!gdb_has_xml) {
Alex Bennée94b2a622019-07-05 14:23:07 +01001693 put_packet(gdb_ctx->s, "");
Jon Doron62b33202019-05-29 09:41:36 +03001694 return;
1695 }
1696
1697 if (gdb_ctx->num_params != 2) {
1698 put_packet(gdb_ctx->s, "E22");
1699 return;
1700 }
1701
1702 reg_size = strlen(gdb_ctx->params[1].data) / 2;
1703 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[1].data, reg_size);
1704 gdb_write_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf,
1705 gdb_ctx->params[0].val_ull);
1706 put_packet(gdb_ctx->s, "OK");
1707}
1708
Jon Doron5d0e57b2019-05-29 09:41:37 +03001709static void handle_get_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1710{
1711 int reg_size;
1712
Jon Doron5d0e57b2019-05-29 09:41:37 +03001713 if (!gdb_has_xml) {
1714 put_packet(gdb_ctx->s, "");
1715 return;
1716 }
1717
1718 if (!gdb_ctx->num_params) {
1719 put_packet(gdb_ctx->s, "E14");
1720 return;
1721 }
1722
1723 reg_size = gdb_read_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf,
1724 gdb_ctx->params[0].val_ull);
1725 if (!reg_size) {
1726 put_packet(gdb_ctx->s, "E14");
1727 return;
1728 }
1729
1730 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, reg_size);
1731 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1732}
1733
Jon Doroncc0ecc72019-05-29 09:41:38 +03001734static void handle_write_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1735{
1736 if (gdb_ctx->num_params != 3) {
1737 put_packet(gdb_ctx->s, "E22");
1738 return;
1739 }
1740
1741 /* hextomem() reads 2*len bytes */
1742 if (gdb_ctx->params[1].val_ull > strlen(gdb_ctx->params[2].data) / 2) {
1743 put_packet(gdb_ctx->s, "E22");
1744 return;
1745 }
1746
1747 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[2].data,
1748 gdb_ctx->params[1].val_ull);
1749 if (target_memory_rw_debug(gdb_ctx->s->g_cpu, gdb_ctx->params[0].val_ull,
1750 gdb_ctx->mem_buf,
1751 gdb_ctx->params[1].val_ull, true)) {
1752 put_packet(gdb_ctx->s, "E14");
1753 return;
1754 }
1755
1756 put_packet(gdb_ctx->s, "OK");
1757}
1758
Jon Doronda92e232019-05-29 09:41:39 +03001759static void handle_read_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1760{
1761 if (gdb_ctx->num_params != 2) {
1762 put_packet(gdb_ctx->s, "E22");
1763 return;
1764 }
1765
1766 /* memtohex() doubles the required space */
1767 if (gdb_ctx->params[1].val_ull > MAX_PACKET_LENGTH / 2) {
1768 put_packet(gdb_ctx->s, "E22");
1769 return;
1770 }
1771
1772 if (target_memory_rw_debug(gdb_ctx->s->g_cpu, gdb_ctx->params[0].val_ull,
1773 gdb_ctx->mem_buf,
1774 gdb_ctx->params[1].val_ull, false)) {
1775 put_packet(gdb_ctx->s, "E14");
1776 return;
1777 }
1778
1779 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, gdb_ctx->params[1].val_ull);
1780 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1781}
1782
Jon Doron287ca122019-05-29 09:41:40 +03001783static void handle_write_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1784{
1785 target_ulong addr, len;
1786 uint8_t *registers;
1787 int reg_size;
1788
1789 if (!gdb_ctx->num_params) {
1790 return;
1791 }
1792
1793 cpu_synchronize_state(gdb_ctx->s->g_cpu);
1794 registers = gdb_ctx->mem_buf;
1795 len = strlen(gdb_ctx->params[0].data) / 2;
1796 hextomem(registers, gdb_ctx->params[0].data, len);
1797 for (addr = 0; addr < gdb_ctx->s->g_cpu->gdb_num_g_regs && len > 0;
1798 addr++) {
1799 reg_size = gdb_write_register(gdb_ctx->s->g_cpu, registers, addr);
1800 len -= reg_size;
1801 registers += reg_size;
1802 }
1803 put_packet(gdb_ctx->s, "OK");
1804}
1805
Jon Doron397d1372019-05-29 09:41:41 +03001806static void handle_read_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1807{
1808 target_ulong addr, len;
1809
1810 cpu_synchronize_state(gdb_ctx->s->g_cpu);
1811 len = 0;
1812 for (addr = 0; addr < gdb_ctx->s->g_cpu->gdb_num_g_regs; addr++) {
1813 len += gdb_read_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf + len,
1814 addr);
1815 }
1816
1817 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, len);
1818 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1819}
1820
Jon Doron4b20fab2019-05-29 09:41:42 +03001821static void handle_file_io(GdbCmdContext *gdb_ctx, void *user_ctx)
1822{
1823 if (gdb_ctx->num_params >= 2 && gdb_ctx->s->current_syscall_cb) {
1824 target_ulong ret, err;
1825
1826 ret = (target_ulong)gdb_ctx->params[0].val_ull;
1827 err = (target_ulong)gdb_ctx->params[1].val_ull;
1828 gdb_ctx->s->current_syscall_cb(gdb_ctx->s->c_cpu, ret, err);
1829 gdb_ctx->s->current_syscall_cb = NULL;
1830 }
1831
1832 if (gdb_ctx->num_params >= 3 && gdb_ctx->params[2].opcode == (uint8_t)'C') {
1833 put_packet(gdb_ctx->s, "T02");
1834 return;
1835 }
1836
1837 gdb_continue(gdb_ctx->s);
1838}
1839
Jon Doron933f80d2019-05-29 09:41:43 +03001840static void handle_step(GdbCmdContext *gdb_ctx, void *user_ctx)
1841{
1842 if (gdb_ctx->num_params) {
1843 gdb_set_cpu_pc(gdb_ctx->s, (target_ulong)gdb_ctx->params[0].val_ull);
1844 }
1845
1846 cpu_single_step(gdb_ctx->s->c_cpu, sstep_flags);
1847 gdb_continue(gdb_ctx->s);
1848}
1849
Jon Doron8536ec02019-05-29 09:41:44 +03001850static void handle_v_cont_query(GdbCmdContext *gdb_ctx, void *user_ctx)
1851{
1852 put_packet(gdb_ctx->s, "vCont;c;C;s;S");
1853}
1854
1855static void handle_v_cont(GdbCmdContext *gdb_ctx, void *user_ctx)
1856{
1857 int res;
1858
1859 if (!gdb_ctx->num_params) {
1860 return;
1861 }
1862
1863 res = gdb_handle_vcont(gdb_ctx->s, gdb_ctx->params[0].data);
1864 if ((res == -EINVAL) || (res == -ERANGE)) {
1865 put_packet(gdb_ctx->s, "E22");
1866 } else if (res) {
1867 put_packet(gdb_ctx->s, "");
1868 }
1869}
1870
1871static void handle_v_attach(GdbCmdContext *gdb_ctx, void *user_ctx)
1872{
1873 GDBProcess *process;
1874 CPUState *cpu;
1875 char thread_id[16];
1876
1877 pstrcpy(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "E22");
1878 if (!gdb_ctx->num_params) {
1879 goto cleanup;
1880 }
1881
1882 process = gdb_get_process(gdb_ctx->s, gdb_ctx->params[0].val_ul);
1883 if (!process) {
1884 goto cleanup;
1885 }
1886
1887 cpu = get_first_cpu_in_process(gdb_ctx->s, process);
1888 if (!cpu) {
1889 goto cleanup;
1890 }
1891
1892 process->attached = true;
1893 gdb_ctx->s->g_cpu = cpu;
1894 gdb_ctx->s->c_cpu = cpu;
1895
1896 gdb_fmt_thread_id(gdb_ctx->s, cpu, thread_id, sizeof(thread_id));
1897 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "T%02xthread:%s;",
1898 GDB_SIGNAL_TRAP, thread_id);
1899cleanup:
1900 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1901}
1902
1903static void handle_v_kill(GdbCmdContext *gdb_ctx, void *user_ctx)
1904{
1905 /* Kill the target */
1906 put_packet(gdb_ctx->s, "OK");
1907 error_report("QEMU: Terminated via GDBstub");
1908 exit(0);
1909}
1910
1911static GdbCmdParseEntry gdb_v_commands_table[] = {
1912 /* Order is important if has same prefix */
1913 {
1914 .handler = handle_v_cont_query,
1915 .cmd = "Cont?",
1916 .cmd_startswith = 1
1917 },
1918 {
1919 .handler = handle_v_cont,
1920 .cmd = "Cont",
1921 .cmd_startswith = 1,
1922 .schema = "s0"
1923 },
1924 {
1925 .handler = handle_v_attach,
1926 .cmd = "Attach;",
1927 .cmd_startswith = 1,
1928 .schema = "l0"
1929 },
1930 {
1931 .handler = handle_v_kill,
1932 .cmd = "Kill;",
1933 .cmd_startswith = 1
1934 },
1935};
1936
1937static void handle_v_commands(GdbCmdContext *gdb_ctx, void *user_ctx)
1938{
1939 if (!gdb_ctx->num_params) {
1940 return;
1941 }
1942
1943 if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
1944 gdb_v_commands_table,
1945 ARRAY_SIZE(gdb_v_commands_table))) {
1946 put_packet(gdb_ctx->s, "");
1947 }
1948}
1949
Jon Doron2704efa2019-05-29 09:41:45 +03001950static void handle_query_qemu_sstepbits(GdbCmdContext *gdb_ctx, void *user_ctx)
1951{
1952 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
1953 "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", SSTEP_ENABLE,
1954 SSTEP_NOIRQ, SSTEP_NOTIMER);
1955 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1956}
1957
1958static void handle_set_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
1959{
1960 if (!gdb_ctx->num_params) {
1961 return;
1962 }
1963
1964 sstep_flags = gdb_ctx->params[0].val_ul;
1965 put_packet(gdb_ctx->s, "OK");
1966}
1967
1968static void handle_query_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
1969{
1970 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "0x%x", sstep_flags);
1971 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1972}
1973
1974static void handle_query_curr_tid(GdbCmdContext *gdb_ctx, void *user_ctx)
bellardb4608c02003-06-27 17:34:32 +00001975{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001976 CPUState *cpu;
Luc Michelc145eea2019-01-07 15:23:46 +00001977 GDBProcess *process;
Jon Doron2704efa2019-05-29 09:41:45 +03001978 char thread_id[16];
1979
1980 /*
1981 * "Current thread" remains vague in the spec, so always return
1982 * the first thread of the current process (gdb returns the
1983 * first thread).
1984 */
1985 process = gdb_get_cpu_process(gdb_ctx->s, gdb_ctx->s->g_cpu);
1986 cpu = get_first_cpu_in_process(gdb_ctx->s, process);
1987 gdb_fmt_thread_id(gdb_ctx->s, cpu, thread_id, sizeof(thread_id));
1988 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "QC%s", thread_id);
1989 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1990}
1991
1992static void handle_query_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
1993{
1994 char thread_id[16];
1995
1996 if (!gdb_ctx->s->query_cpu) {
1997 put_packet(gdb_ctx->s, "l");
1998 return;
1999 }
2000
2001 gdb_fmt_thread_id(gdb_ctx->s, gdb_ctx->s->query_cpu, thread_id,
2002 sizeof(thread_id));
2003 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "m%s", thread_id);
2004 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2005 gdb_ctx->s->query_cpu =
2006 gdb_next_attached_cpu(gdb_ctx->s, gdb_ctx->s->query_cpu);
2007}
2008
2009static void handle_query_first_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
2010{
2011 gdb_ctx->s->query_cpu = gdb_first_attached_cpu(gdb_ctx->s);
2012 handle_query_threads(gdb_ctx, user_ctx);
2013}
2014
2015static void handle_query_thread_extra(GdbCmdContext *gdb_ctx, void *user_ctx)
2016{
2017 CPUState *cpu;
2018 int len;
2019
2020 if (!gdb_ctx->num_params ||
2021 gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
2022 put_packet(gdb_ctx->s, "E22");
2023 return;
2024 }
2025
2026 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[0].thread_id.pid,
2027 gdb_ctx->params[0].thread_id.tid);
2028 if (!cpu) {
2029 return;
2030 }
2031
2032 cpu_synchronize_state(cpu);
2033
2034 if (gdb_ctx->s->multiprocess && (gdb_ctx->s->process_num > 1)) {
2035 /* Print the CPU model and name in multiprocess mode */
2036 ObjectClass *oc = object_get_class(OBJECT(cpu));
2037 const char *cpu_model = object_class_get_name(oc);
2038 char *cpu_name = object_get_canonical_path_component(OBJECT(cpu));
2039 len = snprintf((char *)gdb_ctx->mem_buf, sizeof(gdb_ctx->str_buf) / 2,
2040 "%s %s [%s]", cpu_model, cpu_name,
2041 cpu->halted ? "halted " : "running");
2042 g_free(cpu_name);
2043 } else {
2044 /* memtohex() doubles the required space */
2045 len = snprintf((char *)gdb_ctx->mem_buf, sizeof(gdb_ctx->str_buf) / 2,
2046 "CPU#%d [%s]", cpu->cpu_index,
2047 cpu->halted ? "halted " : "running");
2048 }
2049 trace_gdbstub_op_extra_info((char *)gdb_ctx->mem_buf);
2050 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, len);
2051 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2052}
2053
2054#ifdef CONFIG_USER_ONLY
2055static void handle_query_offsets(GdbCmdContext *gdb_ctx, void *user_ctx)
2056{
2057 TaskState *ts;
2058
2059 ts = gdb_ctx->s->c_cpu->opaque;
2060 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
2061 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
2062 ";Bss=" TARGET_ABI_FMT_lx,
2063 ts->info->code_offset,
2064 ts->info->data_offset,
2065 ts->info->data_offset);
2066 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2067}
2068#else
2069static void handle_query_rcmd(GdbCmdContext *gdb_ctx, void *user_ctx)
2070{
2071 int len;
2072
2073 if (!gdb_ctx->num_params) {
2074 put_packet(gdb_ctx->s, "E22");
2075 return;
2076 }
2077
2078 len = strlen(gdb_ctx->params[0].data);
2079 if (len % 2) {
2080 put_packet(gdb_ctx->s, "E01");
2081 return;
2082 }
2083
2084 len = len / 2;
2085 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[0].data, len);
2086 gdb_ctx->mem_buf[len++] = 0;
2087 qemu_chr_be_write(gdb_ctx->s->mon_chr, gdb_ctx->mem_buf, len);
2088 put_packet(gdb_ctx->s, "OK");
2089
2090}
2091#endif
2092
2093static void handle_query_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2094{
Andreas Färber5b24c642013-07-07 15:08:22 +02002095 CPUClass *cc;
Jon Doron2704efa2019-05-29 09:41:45 +03002096
2097 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "PacketSize=%x",
2098 MAX_PACKET_LENGTH);
2099 cc = CPU_GET_CLASS(first_cpu);
2100 if (cc->gdb_core_xml_file) {
2101 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
2102 ";qXfer:features:read+");
2103 }
2104
2105 if (gdb_ctx->num_params &&
2106 strstr(gdb_ctx->params[0].data, "multiprocess+")) {
2107 gdb_ctx->s->multiprocess = true;
2108 }
2109
2110 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), ";multiprocess+");
2111 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2112}
2113
2114static void handle_query_xfer_features(GdbCmdContext *gdb_ctx, void *user_ctx)
2115{
2116 GDBProcess *process;
2117 CPUClass *cc;
2118 unsigned long len, total_len, addr;
2119 const char *xml;
bellardb4608c02003-06-27 17:34:32 +00002120 const char *p;
Jon Doron2704efa2019-05-29 09:41:45 +03002121
2122 if (gdb_ctx->num_params < 3) {
2123 put_packet(gdb_ctx->s, "E22");
2124 return;
2125 }
2126
2127 process = gdb_get_cpu_process(gdb_ctx->s, gdb_ctx->s->g_cpu);
2128 cc = CPU_GET_CLASS(gdb_ctx->s->g_cpu);
2129 if (!cc->gdb_core_xml_file) {
2130 put_packet(gdb_ctx->s, "");
2131 return;
2132 }
2133
2134 gdb_has_xml = true;
2135 p = gdb_ctx->params[0].data;
2136 xml = get_feature_xml(gdb_ctx->s, p, &p, process);
2137 if (!xml) {
2138 put_packet(gdb_ctx->s, "E00");
2139 return;
2140 }
2141
2142 addr = gdb_ctx->params[1].val_ul;
2143 len = gdb_ctx->params[2].val_ul;
2144 total_len = strlen(xml);
2145 if (addr > total_len) {
2146 put_packet(gdb_ctx->s, "E00");
2147 return;
2148 }
2149
2150 if (len > (MAX_PACKET_LENGTH - 5) / 2) {
2151 len = (MAX_PACKET_LENGTH - 5) / 2;
2152 }
2153
2154 if (len < total_len - addr) {
2155 gdb_ctx->str_buf[0] = 'm';
2156 len = memtox(gdb_ctx->str_buf + 1, xml + addr, len);
2157 } else {
2158 gdb_ctx->str_buf[0] = 'l';
2159 len = memtox(gdb_ctx->str_buf + 1, xml + addr, total_len - addr);
2160 }
2161
2162 put_packet_binary(gdb_ctx->s, gdb_ctx->str_buf, len + 1, true);
2163}
2164
2165static void handle_query_attached(GdbCmdContext *gdb_ctx, void *user_ctx)
2166{
2167 put_packet(gdb_ctx->s, GDB_ATTACHED);
2168}
2169
2170static void handle_query_qemu_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2171{
Jon Doronab4752e2019-05-29 09:41:48 +03002172 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "sstepbits;sstep");
2173#ifndef CONFIG_USER_ONLY
2174 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), ";PhyMemMode");
2175#endif
2176 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
Jon Doron2704efa2019-05-29 09:41:45 +03002177}
2178
Jon Doronab4752e2019-05-29 09:41:48 +03002179#ifndef CONFIG_USER_ONLY
2180static void handle_query_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx,
2181 void *user_ctx)
2182{
2183 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "%d", phy_memory_mode);
2184 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2185}
2186
2187static void handle_set_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx, void *user_ctx)
2188{
2189 if (!gdb_ctx->num_params) {
2190 put_packet(gdb_ctx->s, "E22");
2191 return;
2192 }
2193
2194 if (!gdb_ctx->params[0].val_ul) {
2195 phy_memory_mode = 0;
2196 } else {
2197 phy_memory_mode = 1;
2198 }
2199 put_packet(gdb_ctx->s, "OK");
2200}
2201#endif
2202
Jon Doron2704efa2019-05-29 09:41:45 +03002203static GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
2204 /* Order is important if has same prefix */
2205 {
2206 .handler = handle_query_qemu_sstepbits,
2207 .cmd = "qemu.sstepbits",
2208 },
2209 {
2210 .handler = handle_query_qemu_sstep,
2211 .cmd = "qemu.sstep",
2212 },
2213 {
2214 .handler = handle_set_qemu_sstep,
2215 .cmd = "qemu.sstep=",
2216 .cmd_startswith = 1,
2217 .schema = "l0"
2218 },
2219};
2220
2221static GdbCmdParseEntry gdb_gen_query_table[] = {
2222 {
2223 .handler = handle_query_curr_tid,
2224 .cmd = "C",
2225 },
2226 {
2227 .handler = handle_query_threads,
2228 .cmd = "sThreadInfo",
2229 },
2230 {
2231 .handler = handle_query_first_threads,
2232 .cmd = "fThreadInfo",
2233 },
2234 {
2235 .handler = handle_query_thread_extra,
2236 .cmd = "ThreadExtraInfo,",
2237 .cmd_startswith = 1,
2238 .schema = "t0"
2239 },
2240#ifdef CONFIG_USER_ONLY
2241 {
2242 .handler = handle_query_offsets,
2243 .cmd = "Offsets",
2244 },
2245#else
2246 {
2247 .handler = handle_query_rcmd,
2248 .cmd = "Rcmd,",
2249 .cmd_startswith = 1,
2250 .schema = "s0"
2251 },
2252#endif
2253 {
2254 .handler = handle_query_supported,
2255 .cmd = "Supported:",
2256 .cmd_startswith = 1,
2257 .schema = "s0"
2258 },
2259 {
2260 .handler = handle_query_supported,
2261 .cmd = "Supported",
2262 .schema = "s0"
2263 },
2264 {
2265 .handler = handle_query_xfer_features,
2266 .cmd = "Xfer:features:read:",
2267 .cmd_startswith = 1,
2268 .schema = "s:l,l0"
2269 },
2270 {
2271 .handler = handle_query_attached,
2272 .cmd = "Attached:",
2273 .cmd_startswith = 1
2274 },
2275 {
2276 .handler = handle_query_attached,
2277 .cmd = "Attached",
2278 },
2279 {
2280 .handler = handle_query_qemu_supported,
2281 .cmd = "qemu.Supported",
2282 },
Jon Doronab4752e2019-05-29 09:41:48 +03002283#ifndef CONFIG_USER_ONLY
2284 {
2285 .handler = handle_query_qemu_phy_mem_mode,
2286 .cmd = "qemu.PhyMemMode",
2287 },
2288#endif
Jon Doron2704efa2019-05-29 09:41:45 +03002289};
2290
2291static GdbCmdParseEntry gdb_gen_set_table[] = {
2292 /* Order is important if has same prefix */
2293 {
2294 .handler = handle_set_qemu_sstep,
2295 .cmd = "qemu.sstep:",
2296 .cmd_startswith = 1,
2297 .schema = "l0"
2298 },
Jon Doronab4752e2019-05-29 09:41:48 +03002299#ifndef CONFIG_USER_ONLY
2300 {
2301 .handler = handle_set_qemu_phy_mem_mode,
2302 .cmd = "qemu.PhyMemMode:",
2303 .cmd_startswith = 1,
2304 .schema = "l0"
2305 },
2306#endif
Jon Doron2704efa2019-05-29 09:41:45 +03002307};
2308
2309static void handle_gen_query(GdbCmdContext *gdb_ctx, void *user_ctx)
2310{
2311 if (!gdb_ctx->num_params) {
2312 return;
2313 }
2314
2315 if (!process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2316 gdb_gen_query_set_common_table,
2317 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2318 return;
2319 }
2320
2321 if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2322 gdb_gen_query_table,
2323 ARRAY_SIZE(gdb_gen_query_table))) {
2324 put_packet(gdb_ctx->s, "");
2325 }
2326}
2327
2328static void handle_gen_set(GdbCmdContext *gdb_ctx, void *user_ctx)
2329{
2330 if (!gdb_ctx->num_params) {
2331 return;
2332 }
2333
2334 if (!process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2335 gdb_gen_query_set_common_table,
2336 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2337 return;
2338 }
2339
2340 if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2341 gdb_gen_set_table,
2342 ARRAY_SIZE(gdb_gen_set_table))) {
2343 put_packet(gdb_ctx->s, "");
2344 }
2345}
2346
Jon Doron7009d572019-05-29 09:41:46 +03002347static void handle_target_halt(GdbCmdContext *gdb_ctx, void *user_ctx)
2348{
2349 char thread_id[16];
2350
2351 gdb_fmt_thread_id(gdb_ctx->s, gdb_ctx->s->c_cpu, thread_id,
2352 sizeof(thread_id));
2353 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "T%02xthread:%s;",
2354 GDB_SIGNAL_TRAP, thread_id);
2355 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2356 /*
2357 * Remove all the breakpoints when this query is issued,
2358 * because gdb is doing an initial connect and the state
2359 * should be cleaned up.
2360 */
2361 gdb_breakpoint_remove_all();
2362}
2363
Jon Doron2704efa2019-05-29 09:41:45 +03002364static int gdb_handle_packet(GDBState *s, const char *line_buf)
2365{
Jon Doron3e2c1262019-05-29 09:41:30 +03002366 const GdbCmdParseEntry *cmd_parser = NULL;
ths3b46e622007-09-17 08:09:54 +00002367
Doug Gale5c9522b2017-12-02 20:30:37 -05002368 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01002369
Jon Doron3f1cbac2019-05-29 09:41:47 +03002370 switch (line_buf[0]) {
Luc Michel53fd6552019-01-07 15:23:46 +00002371 case '!':
2372 put_packet(s, "OK");
2373 break;
bellard858693c2004-03-31 18:52:07 +00002374 case '?':
Jon Doron7009d572019-05-29 09:41:46 +03002375 {
2376 static const GdbCmdParseEntry target_halted_cmd_desc = {
2377 .handler = handle_target_halt,
2378 .cmd = "?",
2379 .cmd_startswith = 1
2380 };
2381 cmd_parser = &target_halted_cmd_desc;
2382 }
bellard858693c2004-03-31 18:52:07 +00002383 break;
2384 case 'c':
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002385 {
2386 static const GdbCmdParseEntry continue_cmd_desc = {
2387 .handler = handle_continue,
2388 .cmd = "c",
2389 .cmd_startswith = 1,
2390 .schema = "L0"
2391 };
2392 cmd_parser = &continue_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002393 }
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002394 break;
edgar_igl1f487ee2008-05-17 22:20:53 +00002395 case 'C':
Jon Doronccc47d52019-05-29 09:41:33 +03002396 {
2397 static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
2398 .handler = handle_cont_with_sig,
2399 .cmd = "C",
2400 .cmd_startswith = 1,
2401 .schema = "l0"
2402 };
2403 cmd_parser = &cont_with_sig_cmd_desc;
2404 }
2405 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002406 case 'v':
Jon Doron8536ec02019-05-29 09:41:44 +03002407 {
2408 static const GdbCmdParseEntry v_cmd_desc = {
2409 .handler = handle_v_commands,
2410 .cmd = "v",
2411 .cmd_startswith = 1,
2412 .schema = "s0"
2413 };
2414 cmd_parser = &v_cmd_desc;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002415 }
Jon Doron8536ec02019-05-29 09:41:44 +03002416 break;
edgar_igl7d03f822008-05-17 18:58:29 +00002417 case 'k':
2418 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002419 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00002420 exit(0);
2421 case 'D':
Jon Doron3e2c1262019-05-29 09:41:30 +03002422 {
2423 static const GdbCmdParseEntry detach_cmd_desc = {
2424 .handler = handle_detach,
2425 .cmd = "D",
2426 .cmd_startswith = 1,
2427 .schema = "?.l0"
2428 };
2429 cmd_parser = &detach_cmd_desc;
Luc Michel546f3c62019-01-07 15:23:46 +00002430 }
edgar_igl7d03f822008-05-17 18:58:29 +00002431 break;
bellard858693c2004-03-31 18:52:07 +00002432 case 's':
Jon Doron933f80d2019-05-29 09:41:43 +03002433 {
2434 static const GdbCmdParseEntry step_cmd_desc = {
2435 .handler = handle_step,
2436 .cmd = "s",
2437 .cmd_startswith = 1,
2438 .schema = "L0"
2439 };
2440 cmd_parser = &step_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002441 }
Jon Doron933f80d2019-05-29 09:41:43 +03002442 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002443 case 'F':
2444 {
Jon Doron4b20fab2019-05-29 09:41:42 +03002445 static const GdbCmdParseEntry file_io_cmd_desc = {
2446 .handler = handle_file_io,
2447 .cmd = "F",
2448 .cmd_startswith = 1,
2449 .schema = "L,L,o0"
2450 };
2451 cmd_parser = &file_io_cmd_desc;
pbrooka2d1eba2007-01-28 03:10:55 +00002452 }
2453 break;
bellard858693c2004-03-31 18:52:07 +00002454 case 'g':
Jon Doron397d1372019-05-29 09:41:41 +03002455 {
2456 static const GdbCmdParseEntry read_all_regs_cmd_desc = {
2457 .handler = handle_read_all_regs,
2458 .cmd = "g",
2459 .cmd_startswith = 1
2460 };
2461 cmd_parser = &read_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002462 }
bellard858693c2004-03-31 18:52:07 +00002463 break;
2464 case 'G':
Jon Doron287ca122019-05-29 09:41:40 +03002465 {
2466 static const GdbCmdParseEntry write_all_regs_cmd_desc = {
2467 .handler = handle_write_all_regs,
2468 .cmd = "G",
2469 .cmd_startswith = 1,
2470 .schema = "s0"
2471 };
2472 cmd_parser = &write_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002473 }
bellard858693c2004-03-31 18:52:07 +00002474 break;
2475 case 'm':
Jon Doronda92e232019-05-29 09:41:39 +03002476 {
2477 static const GdbCmdParseEntry read_mem_cmd_desc = {
2478 .handler = handle_read_mem,
2479 .cmd = "m",
2480 .cmd_startswith = 1,
2481 .schema = "L,L0"
2482 };
2483 cmd_parser = &read_mem_cmd_desc;
bellard6f970bd2005-12-05 19:55:19 +00002484 }
bellard858693c2004-03-31 18:52:07 +00002485 break;
2486 case 'M':
Jon Doroncc0ecc72019-05-29 09:41:38 +03002487 {
2488 static const GdbCmdParseEntry write_mem_cmd_desc = {
2489 .handler = handle_write_mem,
2490 .cmd = "M",
2491 .cmd_startswith = 1,
2492 .schema = "L,L:s0"
2493 };
2494 cmd_parser = &write_mem_cmd_desc;
Fabien Chouteau44520db2011-09-08 12:48:16 +02002495 }
bellard858693c2004-03-31 18:52:07 +00002496 break;
pbrook56aebc82008-10-11 17:55:29 +00002497 case 'p':
Jon Doron5d0e57b2019-05-29 09:41:37 +03002498 {
2499 static const GdbCmdParseEntry get_reg_cmd_desc = {
2500 .handler = handle_get_reg,
2501 .cmd = "p",
2502 .cmd_startswith = 1,
2503 .schema = "L0"
2504 };
2505 cmd_parser = &get_reg_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002506 }
2507 break;
2508 case 'P':
Jon Doron62b33202019-05-29 09:41:36 +03002509 {
2510 static const GdbCmdParseEntry set_reg_cmd_desc = {
2511 .handler = handle_set_reg,
2512 .cmd = "P",
2513 .cmd_startswith = 1,
2514 .schema = "L?s0"
2515 };
2516 cmd_parser = &set_reg_cmd_desc;
2517 }
pbrook56aebc82008-10-11 17:55:29 +00002518 break;
bellard858693c2004-03-31 18:52:07 +00002519 case 'Z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002520 {
2521 static const GdbCmdParseEntry insert_bp_cmd_desc = {
2522 .handler = handle_insert_bp,
2523 .cmd = "Z",
2524 .cmd_startswith = 1,
2525 .schema = "l?L?L0"
2526 };
2527 cmd_parser = &insert_bp_cmd_desc;
2528 }
2529 break;
bellard858693c2004-03-31 18:52:07 +00002530 case 'z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002531 {
2532 static const GdbCmdParseEntry remove_bp_cmd_desc = {
2533 .handler = handle_remove_bp,
2534 .cmd = "z",
2535 .cmd_startswith = 1,
2536 .schema = "l?L?L0"
2537 };
2538 cmd_parser = &remove_bp_cmd_desc;
2539 }
bellard858693c2004-03-31 18:52:07 +00002540 break;
aliguori880a7572008-11-18 20:30:24 +00002541 case 'H':
Jon Doron3a9651d2019-05-29 09:41:34 +03002542 {
2543 static const GdbCmdParseEntry set_thread_cmd_desc = {
2544 .handler = handle_set_thread,
2545 .cmd = "H",
2546 .cmd_startswith = 1,
2547 .schema = "o.t0"
2548 };
2549 cmd_parser = &set_thread_cmd_desc;
aliguori880a7572008-11-18 20:30:24 +00002550 }
2551 break;
2552 case 'T':
Jon Doron44ffded2019-05-29 09:41:31 +03002553 {
2554 static const GdbCmdParseEntry thread_alive_cmd_desc = {
2555 .handler = handle_thread_alive,
2556 .cmd = "T",
2557 .cmd_startswith = 1,
2558 .schema = "t0"
2559 };
2560 cmd_parser = &thread_alive_cmd_desc;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07002561 }
aliguori880a7572008-11-18 20:30:24 +00002562 break;
pbrook978efd62006-06-17 18:30:42 +00002563 case 'q':
Jon Doron2704efa2019-05-29 09:41:45 +03002564 {
2565 static const GdbCmdParseEntry gen_query_cmd_desc = {
2566 .handler = handle_gen_query,
2567 .cmd = "q",
2568 .cmd_startswith = 1,
2569 .schema = "s0"
2570 };
2571 cmd_parser = &gen_query_cmd_desc;
2572 }
2573 break;
edgar_igl60897d32008-05-09 08:25:14 +00002574 case 'Q':
Jon Doron2704efa2019-05-29 09:41:45 +03002575 {
2576 static const GdbCmdParseEntry gen_set_cmd_desc = {
2577 .handler = handle_gen_set,
2578 .cmd = "Q",
2579 .cmd_startswith = 1,
2580 .schema = "s0"
2581 };
2582 cmd_parser = &gen_set_cmd_desc;
edgar_igl60897d32008-05-09 08:25:14 +00002583 }
Jon Doron2704efa2019-05-29 09:41:45 +03002584 break;
bellard858693c2004-03-31 18:52:07 +00002585 default:
bellard858693c2004-03-31 18:52:07 +00002586 /* put empty packet */
Jon Doron3f1cbac2019-05-29 09:41:47 +03002587 put_packet(s, "");
bellard858693c2004-03-31 18:52:07 +00002588 break;
2589 }
Jon Doron3e2c1262019-05-29 09:41:30 +03002590
Ramiro Polla2bdec392019-08-05 21:09:01 +02002591 if (cmd_parser) {
2592 run_cmd_parser(s, line_buf, cmd_parser);
2593 }
Jon Doron3e2c1262019-05-29 09:41:30 +03002594
bellard858693c2004-03-31 18:52:07 +00002595 return RS_IDLE;
2596}
2597
Andreas Färber64f6b342013-05-27 02:06:09 +02002598void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00002599{
Luc Michel160d8582019-01-07 15:23:46 +00002600 GDBProcess *p = gdb_get_cpu_process(gdbserver_state, cpu);
2601
2602 if (!p->attached) {
2603 /*
2604 * Having a stop CPU corresponding to a process that is not attached
2605 * confuses GDB. So we ignore the request.
2606 */
2607 return;
2608 }
2609
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002610 gdbserver_state->c_cpu = cpu;
2611 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00002612}
2613
bellard1fddef42005-04-17 19:16:13 +00002614#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002615static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00002616{
aliguori880a7572008-11-18 20:30:24 +00002617 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002618 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00002619 char buf[256];
Luc Michel95567c22019-01-07 15:23:46 +00002620 char thread_id[16];
aliguorid6fc1b32008-11-18 19:55:44 +00002621 const char *type;
bellard858693c2004-03-31 18:52:07 +00002622 int ret;
2623
Meador Ingecdb432b2012-03-15 17:49:45 +00002624 if (running || s->state == RS_INACTIVE) {
2625 return;
2626 }
2627 /* Is there a GDB syscall waiting to be sent? */
2628 if (s->current_syscall_cb) {
2629 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00002630 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01002631 }
Luc Michel95567c22019-01-07 15:23:46 +00002632
2633 if (cpu == NULL) {
2634 /* No process attached */
2635 return;
2636 }
2637
2638 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id));
2639
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002640 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002641 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02002642 if (cpu->watchpoint_hit) {
2643 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00002644 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00002645 type = "r";
2646 break;
aliguoria1d1bb32008-11-18 20:07:32 +00002647 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00002648 type = "a";
2649 break;
2650 default:
2651 type = "";
2652 break;
2653 }
Doug Gale5c9522b2017-12-02 20:30:37 -05002654 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
2655 (target_ulong)cpu->watchpoint_hit->vaddr);
aliguori880a7572008-11-18 20:30:24 +00002656 snprintf(buf, sizeof(buf),
Luc Michel95567c22019-01-07 15:23:46 +00002657 "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
2658 GDB_SIGNAL_TRAP, thread_id, type,
Andreas Färberff4700b2013-08-26 18:23:18 +02002659 (target_ulong)cpu->watchpoint_hit->vaddr);
2660 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01002661 goto send_packet;
Doug Gale5c9522b2017-12-02 20:30:37 -05002662 } else {
2663 trace_gdbstub_hit_break();
pbrook6658ffb2007-03-16 23:58:11 +00002664 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07002665 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00002666 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01002667 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002668 case RUN_STATE_PAUSED:
Doug Gale5c9522b2017-12-02 20:30:37 -05002669 trace_gdbstub_hit_paused();
aliguori9781e042009-01-22 17:15:29 +00002670 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01002671 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002672 case RUN_STATE_SHUTDOWN:
Doug Gale5c9522b2017-12-02 20:30:37 -05002673 trace_gdbstub_hit_shutdown();
Jan Kiszka425189a2011-03-22 11:02:09 +01002674 ret = GDB_SIGNAL_QUIT;
2675 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002676 case RUN_STATE_IO_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002677 trace_gdbstub_hit_io_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002678 ret = GDB_SIGNAL_IO;
2679 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002680 case RUN_STATE_WATCHDOG:
Doug Gale5c9522b2017-12-02 20:30:37 -05002681 trace_gdbstub_hit_watchdog();
Jan Kiszka425189a2011-03-22 11:02:09 +01002682 ret = GDB_SIGNAL_ALRM;
2683 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002684 case RUN_STATE_INTERNAL_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002685 trace_gdbstub_hit_internal_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002686 ret = GDB_SIGNAL_ABRT;
2687 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002688 case RUN_STATE_SAVE_VM:
2689 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01002690 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002691 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01002692 ret = GDB_SIGNAL_XCPU;
2693 break;
2694 default:
Doug Gale5c9522b2017-12-02 20:30:37 -05002695 trace_gdbstub_hit_unknown(state);
Jan Kiszka425189a2011-03-22 11:02:09 +01002696 ret = GDB_SIGNAL_UNKNOWN;
2697 break;
bellardbbeb7b52006-04-23 18:42:15 +00002698 }
Jan Kiszka226d0072015-07-24 18:52:31 +02002699 gdb_set_stop_cpu(cpu);
Luc Michel95567c22019-01-07 15:23:46 +00002700 snprintf(buf, sizeof(buf), "T%02xthread:%s;", ret, thread_id);
Jan Kiszka425189a2011-03-22 11:02:09 +01002701
2702send_packet:
bellard858693c2004-03-31 18:52:07 +00002703 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01002704
2705 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02002706 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00002707}
bellard1fddef42005-04-17 19:16:13 +00002708#endif
bellard858693c2004-03-31 18:52:07 +00002709
pbrooka2d1eba2007-01-28 03:10:55 +00002710/* Send a gdb syscall request.
2711 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00002712 %x - target_ulong argument printed in hex.
2713 %lx - 64-bit argument printed in hex.
2714 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01002715void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00002716{
pbrooka2d1eba2007-01-28 03:10:55 +00002717 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00002718 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00002719 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00002720 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00002721 GDBState *s;
2722
aliguori880a7572008-11-18 20:30:24 +00002723 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00002724 if (!s)
2725 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00002726 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00002727#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002728 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00002729#endif
Meador Ingecdb432b2012-03-15 17:49:45 +00002730 p = s->syscall_buf;
2731 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00002732 *(p++) = 'F';
2733 while (*fmt) {
2734 if (*fmt == '%') {
2735 fmt++;
2736 switch (*fmt++) {
2737 case 'x':
2738 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002739 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00002740 break;
pbrooka87295e2007-05-26 15:09:38 +00002741 case 'l':
2742 if (*(fmt++) != 'x')
2743 goto bad_format;
2744 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00002745 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00002746 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002747 case 's':
2748 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002749 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00002750 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00002751 break;
2752 default:
pbrooka87295e2007-05-26 15:09:38 +00002753 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002754 error_report("gdbstub: Bad syscall format string '%s'",
2755 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00002756 break;
2757 }
2758 } else {
2759 *(p++) = *(fmt++);
2760 }
2761 }
pbrook8a93e022007-08-06 13:19:15 +00002762 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00002763#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00002764 put_packet(s, s->syscall_buf);
Peter Maydell4f710862018-05-15 19:19:58 +01002765 /* Return control to gdb for it to process the syscall request.
2766 * Since the protocol requires that gdb hands control back to us
2767 * using a "here are the results" F packet, we don't need to check
2768 * gdb_handlesig's return value (which is the signal to deliver if
2769 * execution was resumed via a continue packet).
2770 */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002771 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00002772#else
Meador Ingecdb432b2012-03-15 17:49:45 +00002773 /* In this case wait to send the syscall packet until notification that
2774 the CPU has stopped. This must be done because if the packet is sent
2775 now the reply from the syscall request could be received while the CPU
2776 is still in the running state, which can cause packets to be dropped
2777 and state transition 'T' packets to be sent while the syscall is still
2778 being processed. */
Paolo Bonzini9102ded2015-08-18 06:52:09 -07002779 qemu_cpu_kick(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00002780#endif
2781}
2782
Peter Maydell19239b32015-09-07 10:39:27 +01002783void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
2784{
2785 va_list va;
2786
2787 va_start(va, fmt);
2788 gdb_do_syscallv(cb, fmt, va);
2789 va_end(va);
2790}
2791
Markus Armbruster33c846e2019-05-14 20:03:09 +02002792static void gdb_read_byte(GDBState *s, uint8_t ch)
bellard858693c2004-03-31 18:52:07 +00002793{
ths60fe76f2007-12-16 03:02:09 +00002794 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00002795
bellard1fddef42005-04-17 19:16:13 +00002796#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00002797 if (s->last_packet_len) {
2798 /* Waiting for a response to the last packet. If we see the start
2799 of a new command then abandon the previous response. */
2800 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002801 trace_gdbstub_err_got_nack();
thsffe8ab82007-12-16 03:16:05 +00002802 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
Alex Bennée118e2262017-07-12 11:52:13 +01002803 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002804 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01002805 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002806 trace_gdbstub_io_got_unexpected(ch);
pbrook4046d912007-01-28 01:53:16 +00002807 }
Alex Bennée118e2262017-07-12 11:52:13 +01002808
pbrook4046d912007-01-28 01:53:16 +00002809 if (ch == '+' || ch == '$')
2810 s->last_packet_len = 0;
2811 if (ch != '$')
2812 return;
2813 }
Luiz Capitulino13548692011-07-29 15:36:43 -03002814 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00002815 /* when the CPU is running, we cannot do anything except stop
2816 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002817 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00002818 } else
bellard1fddef42005-04-17 19:16:13 +00002819#endif
bellard41625032005-04-24 10:07:11 +00002820 {
bellard858693c2004-03-31 18:52:07 +00002821 switch(s->state) {
2822 case RS_IDLE:
2823 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04002824 /* start of command packet */
bellard858693c2004-03-31 18:52:07 +00002825 s->line_buf_index = 0;
Doug Gale4bf43122017-05-01 12:22:10 -04002826 s->line_sum = 0;
bellard858693c2004-03-31 18:52:07 +00002827 s->state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002828 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002829 trace_gdbstub_err_garbage(ch);
bellard4c3a88a2003-07-26 12:06:08 +00002830 }
2831 break;
bellard858693c2004-03-31 18:52:07 +00002832 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04002833 if (ch == '}') {
2834 /* start escape sequence */
2835 s->state = RS_GETLINE_ESC;
2836 s->line_sum += ch;
2837 } else if (ch == '*') {
2838 /* start run length encoding sequence */
2839 s->state = RS_GETLINE_RLE;
2840 s->line_sum += ch;
2841 } else if (ch == '#') {
2842 /* end of command, start of checksum*/
2843 s->state = RS_CHKSUM1;
bellard858693c2004-03-31 18:52:07 +00002844 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002845 trace_gdbstub_err_overrun();
bellard858693c2004-03-31 18:52:07 +00002846 s->state = RS_IDLE;
2847 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002848 /* unescaped command character */
2849 s->line_buf[s->line_buf_index++] = ch;
2850 s->line_sum += ch;
2851 }
2852 break;
2853 case RS_GETLINE_ESC:
2854 if (ch == '#') {
2855 /* unexpected end of command in escape sequence */
2856 s->state = RS_CHKSUM1;
2857 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2858 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05002859 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002860 s->state = RS_IDLE;
2861 } else {
2862 /* parse escaped character and leave escape state */
2863 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
2864 s->line_sum += ch;
2865 s->state = RS_GETLINE;
2866 }
2867 break;
2868 case RS_GETLINE_RLE:
Markus Armbruster046aba12019-05-14 20:03:08 +02002869 /*
2870 * Run-length encoding is explained in "Debugging with GDB /
2871 * Appendix E GDB Remote Serial Protocol / Overview".
2872 */
2873 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
Doug Gale4bf43122017-05-01 12:22:10 -04002874 /* invalid RLE count encoding */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002875 trace_gdbstub_err_invalid_repeat(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002876 s->state = RS_GETLINE;
2877 } else {
2878 /* decode repeat length */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002879 int repeat = ch - ' ' + 3;
Doug Gale4bf43122017-05-01 12:22:10 -04002880 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
2881 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05002882 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002883 s->state = RS_IDLE;
2884 } else if (s->line_buf_index < 1) {
2885 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05002886 trace_gdbstub_err_invalid_rle();
Doug Gale4bf43122017-05-01 12:22:10 -04002887 s->state = RS_GETLINE;
2888 } else {
2889 /* repeat the last character */
2890 memset(s->line_buf + s->line_buf_index,
2891 s->line_buf[s->line_buf_index - 1], repeat);
2892 s->line_buf_index += repeat;
2893 s->line_sum += ch;
2894 s->state = RS_GETLINE;
2895 }
bellard858693c2004-03-31 18:52:07 +00002896 }
2897 break;
2898 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04002899 /* get high hex digit of checksum */
2900 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002901 trace_gdbstub_err_checksum_invalid(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002902 s->state = RS_GETLINE;
2903 break;
2904 }
bellard858693c2004-03-31 18:52:07 +00002905 s->line_buf[s->line_buf_index] = '\0';
2906 s->line_csum = fromhex(ch) << 4;
2907 s->state = RS_CHKSUM2;
2908 break;
2909 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04002910 /* get low hex digit of checksum */
2911 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002912 trace_gdbstub_err_checksum_invalid(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002913 s->state = RS_GETLINE;
2914 break;
bellard858693c2004-03-31 18:52:07 +00002915 }
Doug Gale4bf43122017-05-01 12:22:10 -04002916 s->line_csum |= fromhex(ch);
2917
2918 if (s->line_csum != (s->line_sum & 0xff)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002919 trace_gdbstub_err_checksum_incorrect(s->line_sum, s->line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04002920 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00002921 reply = '-';
2922 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00002923 s->state = RS_IDLE;
2924 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002925 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00002926 reply = '+';
2927 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00002928 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00002929 }
bellardb4608c02003-06-27 17:34:32 +00002930 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002931 default:
2932 abort();
bellardb4608c02003-06-27 17:34:32 +00002933 }
2934 }
bellard858693c2004-03-31 18:52:07 +00002935}
2936
Paul Brook0e1c9c52010-06-16 13:03:51 +01002937/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002938void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01002939{
2940 GDBState *s;
2941 char buf[4];
2942
2943 s = gdbserver_state;
2944 if (!s) {
2945 return;
2946 }
2947#ifdef CONFIG_USER_ONLY
2948 if (gdbserver_fd < 0 || s->fd < 0) {
2949 return;
2950 }
2951#endif
2952
Doug Gale5c9522b2017-12-02 20:30:37 -05002953 trace_gdbstub_op_exiting((uint8_t)code);
2954
Paul Brook0e1c9c52010-06-16 13:03:51 +01002955 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
2956 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002957
2958#ifndef CONFIG_USER_ONLY
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002959 qemu_chr_fe_deinit(&s->chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002960#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01002961}
2962
Luc Michel8f468632019-01-07 15:23:45 +00002963/*
2964 * Create the process that will contain all the "orphan" CPUs (that are not
2965 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2966 * be attachable and thus will be invisible to the user.
2967 */
2968static void create_default_process(GDBState *s)
2969{
2970 GDBProcess *process;
2971 int max_pid = 0;
2972
2973 if (s->process_num) {
2974 max_pid = s->processes[s->process_num - 1].pid;
2975 }
2976
2977 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2978 process = &s->processes[s->process_num - 1];
2979
2980 /* We need an available PID slot for this process */
2981 assert(max_pid < UINT32_MAX);
2982
2983 process->pid = max_pid + 1;
2984 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00002985 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00002986}
2987
bellard1fddef42005-04-17 19:16:13 +00002988#ifdef CONFIG_USER_ONLY
2989int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02002990gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00002991{
Andreas Färber5ca666c2013-06-24 19:20:57 +02002992 GDBState *s;
2993 char buf[256];
2994 int n;
bellard1fddef42005-04-17 19:16:13 +00002995
Andreas Färber5ca666c2013-06-24 19:20:57 +02002996 s = gdbserver_state;
2997 if (gdbserver_fd < 0 || s->fd < 0) {
2998 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));
3007 put_packet(s, buf);
3008 }
3009 /* put_packet() might have detected that the peer terminated the
3010 connection. */
3011 if (s->fd < 0) {
3012 return sig;
3013 }
3014
3015 sig = 0;
3016 s->state = RS_IDLE;
3017 s->running_state = 0;
3018 while (s->running_state == 0) {
3019 n = read(s->fd, buf, 256);
3020 if (n > 0) {
3021 int i;
3022
3023 for (i = 0; i < n; i++) {
3024 gdb_read_byte(s, buf[i]);
3025 }
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) {
3030 close(s->fd);
3031 }
3032 s->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 }
3036 sig = s->signal;
3037 s->signal = 0;
3038 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 GDBState *s;
3045 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00003046
Andreas Färber5ca666c2013-06-24 19:20:57 +02003047 s = gdbserver_state;
3048 if (gdbserver_fd < 0 || s->fd < 0) {
3049 return;
3050 }
aurel32ca587a82008-12-18 22:44:13 +00003051
Andreas Färber5ca666c2013-06-24 19:20:57 +02003052 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
3053 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00003054}
bellard1fddef42005-04-17 19:16:13 +00003055
Peter Maydell2f652222018-05-14 18:30:44 +01003056static bool gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00003057{
3058 GDBState *s;
3059 struct sockaddr_in sockaddr;
3060 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09003061 int fd;
bellard858693c2004-03-31 18:52:07 +00003062
3063 for(;;) {
3064 len = sizeof(sockaddr);
3065 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
3066 if (fd < 0 && errno != EINTR) {
3067 perror("accept");
Peter Maydell2f652222018-05-14 18:30:44 +01003068 return false;
bellard858693c2004-03-31 18:52:07 +00003069 } else if (fd >= 0) {
Peter Maydellf5bdd782018-05-14 18:30:43 +01003070 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00003071 break;
3072 }
3073 }
3074
3075 /* set short latency */
Peter Maydell2f652222018-05-14 18:30:44 +01003076 if (socket_set_nodelay(fd)) {
3077 perror("setsockopt");
Philippe Mathieu-Daudéead75d82018-05-24 19:34:58 -03003078 close(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01003079 return false;
3080 }
ths3b46e622007-09-17 08:09:54 +00003081
Anthony Liguori7267c092011-08-20 22:09:37 -05003082 s = g_malloc0(sizeof(GDBState));
Luc Michel8f468632019-01-07 15:23:45 +00003083 create_default_process(s);
Luc Michel970ed902019-01-07 15:23:46 +00003084 s->processes[0].attached = true;
3085 s->c_cpu = gdb_first_attached_cpu(s);
3086 s->g_cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00003087 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02003088 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00003089
aliguori880a7572008-11-18 20:30:24 +00003090 gdbserver_state = s;
Peter Maydell2f652222018-05-14 18:30:44 +01003091 return true;
bellard858693c2004-03-31 18:52:07 +00003092}
3093
3094static int gdbserver_open(int port)
3095{
3096 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02003097 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00003098
3099 fd = socket(PF_INET, SOCK_STREAM, 0);
3100 if (fd < 0) {
3101 perror("socket");
3102 return -1;
3103 }
Peter Maydellf5bdd782018-05-14 18:30:43 +01003104 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00003105
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02003106 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00003107
3108 sockaddr.sin_family = AF_INET;
3109 sockaddr.sin_port = htons(port);
3110 sockaddr.sin_addr.s_addr = 0;
3111 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
3112 if (ret < 0) {
3113 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00003114 close(fd);
bellard858693c2004-03-31 18:52:07 +00003115 return -1;
3116 }
Peter Wu96165b92016-05-04 11:32:17 +02003117 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00003118 if (ret < 0) {
3119 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00003120 close(fd);
bellard858693c2004-03-31 18:52:07 +00003121 return -1;
3122 }
bellard858693c2004-03-31 18:52:07 +00003123 return fd;
3124}
3125
3126int gdbserver_start(int port)
3127{
3128 gdbserver_fd = gdbserver_open(port);
3129 if (gdbserver_fd < 0)
3130 return -1;
3131 /* accept connections */
Peter Maydell2f652222018-05-14 18:30:44 +01003132 if (!gdb_accept()) {
3133 close(gdbserver_fd);
3134 gdbserver_fd = -1;
3135 return -1;
3136 }
bellardb4608c02003-06-27 17:34:32 +00003137 return 0;
3138}
aurel322b1319c2008-12-18 22:44:04 +00003139
3140/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07003141void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00003142{
3143 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02003144
3145 if (gdbserver_fd < 0 || s->fd < 0) {
3146 return;
3147 }
aurel322b1319c2008-12-18 22:44:04 +00003148 close(s->fd);
3149 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02003150 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02003151 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00003152}
pbrook4046d912007-01-28 01:53:16 +00003153#else
thsaa1f17c2007-07-11 22:48:58 +00003154static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00003155{
pbrook56aebc82008-10-11 17:55:29 +00003156 /* We can handle an arbitrarily large amount of data.
3157 Pick the maximum packet size, which is as good as anything. */
3158 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00003159}
3160
thsaa1f17c2007-07-11 22:48:58 +00003161static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00003162{
pbrook4046d912007-01-28 01:53:16 +00003163 int i;
3164
3165 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00003166 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00003167 }
3168}
3169
3170static void gdb_chr_event(void *opaque, int event)
3171{
Luc Michel970ed902019-01-07 15:23:46 +00003172 int i;
3173 GDBState *s = (GDBState *) opaque;
3174
pbrook4046d912007-01-28 01:53:16 +00003175 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05303176 case CHR_EVENT_OPENED:
Luc Michel970ed902019-01-07 15:23:46 +00003177 /* Start with first process attached, others detached */
3178 for (i = 0; i < s->process_num; i++) {
3179 s->processes[i].attached = !i;
3180 }
3181
3182 s->c_cpu = gdb_first_attached_cpu(s);
3183 s->g_cpu = s->c_cpu;
3184
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003185 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02003186 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00003187 break;
3188 default:
3189 break;
3190 }
3191}
3192
aliguori8a34a0f2009-03-05 23:01:55 +00003193static void gdb_monitor_output(GDBState *s, const char *msg, int len)
3194{
3195 char buf[MAX_PACKET_LENGTH];
3196
3197 buf[0] = 'O';
3198 if (len > (MAX_PACKET_LENGTH/2) - 1)
3199 len = (MAX_PACKET_LENGTH/2) - 1;
3200 memtohex(buf + 1, (uint8_t *)msg, len);
3201 put_packet(s, buf);
3202}
3203
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003204static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00003205{
3206 const char *p = (const char *)buf;
3207 int max_sz;
3208
3209 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
3210 for (;;) {
3211 if (len <= max_sz) {
3212 gdb_monitor_output(gdbserver_state, p, len);
3213 break;
3214 }
3215 gdb_monitor_output(gdbserver_state, p, max_sz);
3216 p += max_sz;
3217 len -= max_sz;
3218 }
3219 return len;
3220}
3221
aliguori59030a82009-04-05 18:43:41 +00003222#ifndef _WIN32
3223static void gdb_sigterm_handler(int signal)
3224{
Luiz Capitulino13548692011-07-29 15:36:43 -03003225 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003226 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01003227 }
aliguori59030a82009-04-05 18:43:41 +00003228}
3229#endif
3230
Marc-André Lureau777357d2016-12-07 18:39:10 +03003231static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
3232 bool *be_opened, Error **errp)
3233{
3234 *be_opened = false;
3235}
3236
3237static void char_gdb_class_init(ObjectClass *oc, void *data)
3238{
3239 ChardevClass *cc = CHARDEV_CLASS(oc);
3240
3241 cc->internal = true;
3242 cc->open = gdb_monitor_open;
3243 cc->chr_write = gdb_monitor_write;
3244}
3245
3246#define TYPE_CHARDEV_GDB "chardev-gdb"
3247
3248static const TypeInfo char_gdb_type_info = {
3249 .name = TYPE_CHARDEV_GDB,
3250 .parent = TYPE_CHARDEV,
3251 .class_init = char_gdb_class_init,
3252};
3253
Luc Michel8f468632019-01-07 15:23:45 +00003254static int find_cpu_clusters(Object *child, void *opaque)
3255{
3256 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
3257 GDBState *s = (GDBState *) opaque;
3258 CPUClusterState *cluster = CPU_CLUSTER(child);
3259 GDBProcess *process;
3260
3261 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
3262
3263 process = &s->processes[s->process_num - 1];
3264
3265 /*
3266 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
3267 * runtime, we enforce here that the machine does not use a cluster ID
3268 * that would lead to PID 0.
3269 */
3270 assert(cluster->cluster_id != UINT32_MAX);
3271 process->pid = cluster->cluster_id + 1;
3272 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00003273 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00003274
3275 return 0;
3276 }
3277
3278 return object_child_foreach(child, find_cpu_clusters, opaque);
3279}
3280
3281static int pid_order(const void *a, const void *b)
3282{
3283 GDBProcess *pa = (GDBProcess *) a;
3284 GDBProcess *pb = (GDBProcess *) b;
3285
3286 if (pa->pid < pb->pid) {
3287 return -1;
3288 } else if (pa->pid > pb->pid) {
3289 return 1;
3290 } else {
3291 return 0;
3292 }
3293}
3294
3295static void create_processes(GDBState *s)
3296{
3297 object_child_foreach(object_get_root(), find_cpu_clusters, s);
3298
3299 if (s->processes) {
3300 /* Sort by PID */
3301 qsort(s->processes, s->process_num, sizeof(s->processes[0]), pid_order);
3302 }
3303
3304 create_default_process(s);
3305}
3306
3307static void cleanup_processes(GDBState *s)
3308{
3309 g_free(s->processes);
3310 s->process_num = 0;
3311 s->processes = NULL;
3312}
3313
aliguori59030a82009-04-05 18:43:41 +00003314int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00003315{
Doug Gale5c9522b2017-12-02 20:30:37 -05003316 trace_gdbstub_op_start(device);
3317
pbrook4046d912007-01-28 01:53:16 +00003318 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00003319 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003320 Chardev *chr = NULL;
3321 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00003322
Ziyue Yang508b4ec2017-01-18 16:02:41 +08003323 if (!first_cpu) {
3324 error_report("gdbstub: meaningless to attach gdb to a "
3325 "machine without any CPU.");
3326 return -1;
3327 }
3328
aliguori59030a82009-04-05 18:43:41 +00003329 if (!device)
3330 return -1;
3331 if (strcmp(device, "none") != 0) {
3332 if (strstart(device, "tcp:", NULL)) {
3333 /* enforce required TCP attributes */
3334 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
3335 "%s,nowait,nodelay,server", device);
3336 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00003337 }
aliguori59030a82009-04-05 18:43:41 +00003338#ifndef _WIN32
3339 else if (strcmp(device, "stdio") == 0) {
3340 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00003341
aliguori59030a82009-04-05 18:43:41 +00003342 memset(&act, 0, sizeof(act));
3343 act.sa_handler = gdb_sigterm_handler;
3344 sigaction(SIGINT, &act, NULL);
3345 }
3346#endif
Marc-André Lureau95e30b22018-08-22 19:19:42 +02003347 /*
3348 * FIXME: it's a bit weird to allow using a mux chardev here
3349 * and implicitly setup a monitor. We may want to break this.
3350 */
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01003351 chr = qemu_chr_new_noreplay("gdb", device, true, NULL);
aliguori36556b22009-03-28 18:05:53 +00003352 if (!chr)
3353 return -1;
pbrookcfc34752007-02-22 01:48:01 +00003354 }
3355
aliguori36556b22009-03-28 18:05:53 +00003356 s = gdbserver_state;
3357 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05003358 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00003359 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00003360
aliguori36556b22009-03-28 18:05:53 +00003361 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
3362
3363 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03003364 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01003365 NULL, NULL, &error_abort);
Kevin Wolffbfc29e2019-06-13 17:34:04 +02003366 monitor_init_hmp(mon_chr, false);
aliguori36556b22009-03-28 18:05:53 +00003367 } else {
Marc-André Lureau1ce26102017-01-27 00:49:13 +04003368 qemu_chr_fe_deinit(&s->chr, true);
aliguori36556b22009-03-28 18:05:53 +00003369 mon_chr = s->mon_chr;
Luc Michel8f468632019-01-07 15:23:45 +00003370 cleanup_processes(s);
aliguori36556b22009-03-28 18:05:53 +00003371 memset(s, 0, sizeof(GDBState));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03003372 s->mon_chr = mon_chr;
aliguori36556b22009-03-28 18:05:53 +00003373 }
Luc Michel8f468632019-01-07 15:23:45 +00003374
3375 create_processes(s);
3376
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03003377 if (chr) {
3378 qemu_chr_fe_init(&s->chr, chr, &error_abort);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03003379 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
Luc Michel970ed902019-01-07 15:23:46 +00003380 gdb_chr_event, NULL, s, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03003381 }
aliguori36556b22009-03-28 18:05:53 +00003382 s->state = chr ? RS_IDLE : RS_INACTIVE;
3383 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00003384 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00003385
pbrook4046d912007-01-28 01:53:16 +00003386 return 0;
3387}
Marc-André Lureau777357d2016-12-07 18:39:10 +03003388
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01003389void gdbserver_cleanup(void)
3390{
3391 if (gdbserver_state) {
3392 put_packet(gdbserver_state, "W00");
3393 }
3394}
3395
Marc-André Lureau777357d2016-12-07 18:39:10 +03003396static void register_types(void)
3397{
3398 type_register_static(&char_gdb_type_info);
3399}
3400
3401type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00003402#endif