blob: 8363683852f0bf786a61fb26ec93996427576e85 [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"
Alex Bennéef1672e62019-05-13 14:43:57 +010051#include "hw/semihosting/semihost.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010052#include "exec/exec-all.h"
aurel32ca587a82008-12-18 22:44:13 +000053
Jan Kiszkaa3919382015-02-07 09:38:44 +010054#ifdef CONFIG_USER_ONLY
55#define GDB_ATTACHED "0"
56#else
57#define GDB_ATTACHED "1"
58#endif
59
Jon Doronab4752e2019-05-29 09:41:48 +030060#ifndef CONFIG_USER_ONLY
61static int phy_memory_mode;
62#endif
63
Andreas Färberf3659ee2013-06-27 19:09:09 +020064static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
65 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020066{
Jon Doronab4752e2019-05-29 09:41:48 +030067 CPUClass *cc;
Andreas Färberf3659ee2013-06-27 19:09:09 +020068
Jon Doronab4752e2019-05-29 09:41:48 +030069#ifndef CONFIG_USER_ONLY
70 if (phy_memory_mode) {
71 if (is_write) {
72 cpu_physical_memory_write(addr, buf, len);
73 } else {
74 cpu_physical_memory_read(addr, buf, len);
75 }
76 return 0;
77 }
78#endif
79
80 cc = CPU_GET_CLASS(cpu);
Andreas Färberf3659ee2013-06-27 19:09:09 +020081 if (cc->memory_rw_debug) {
82 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
83 }
84 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020085}
aurel32ca587a82008-12-18 22:44:13 +000086
Alex Bennéed2a6c852017-07-12 11:52:14 +010087/* Return the GDB index for a given vCPU state.
88 *
89 * For user mode this is simply the thread id. In system mode GDB
90 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
91 */
92static inline int cpu_gdb_index(CPUState *cpu)
93{
94#if defined(CONFIG_USER_ONLY)
Alex Bennéebd88c782017-07-12 11:52:15 +010095 TaskState *ts = (TaskState *) cpu->opaque;
96 return ts->ts_tid;
Alex Bennéed2a6c852017-07-12 11:52:14 +010097#else
98 return cpu->cpu_index + 1;
99#endif
100}
101
aurel32ca587a82008-12-18 22:44:13 +0000102enum {
103 GDB_SIGNAL_0 = 0,
104 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +0100105 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +0000106 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +0100107 GDB_SIGNAL_ABRT = 6,
108 GDB_SIGNAL_ALRM = 14,
109 GDB_SIGNAL_IO = 23,
110 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +0000111 GDB_SIGNAL_UNKNOWN = 143
112};
113
114#ifdef CONFIG_USER_ONLY
115
116/* Map target signal numbers to GDB protocol signal numbers and vice
117 * versa. For user emulation's currently supported systems, we can
118 * assume most signals are defined.
119 */
120
121static int gdb_signal_table[] = {
122 0,
123 TARGET_SIGHUP,
124 TARGET_SIGINT,
125 TARGET_SIGQUIT,
126 TARGET_SIGILL,
127 TARGET_SIGTRAP,
128 TARGET_SIGABRT,
129 -1, /* SIGEMT */
130 TARGET_SIGFPE,
131 TARGET_SIGKILL,
132 TARGET_SIGBUS,
133 TARGET_SIGSEGV,
134 TARGET_SIGSYS,
135 TARGET_SIGPIPE,
136 TARGET_SIGALRM,
137 TARGET_SIGTERM,
138 TARGET_SIGURG,
139 TARGET_SIGSTOP,
140 TARGET_SIGTSTP,
141 TARGET_SIGCONT,
142 TARGET_SIGCHLD,
143 TARGET_SIGTTIN,
144 TARGET_SIGTTOU,
145 TARGET_SIGIO,
146 TARGET_SIGXCPU,
147 TARGET_SIGXFSZ,
148 TARGET_SIGVTALRM,
149 TARGET_SIGPROF,
150 TARGET_SIGWINCH,
151 -1, /* SIGLOST */
152 TARGET_SIGUSR1,
153 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000154#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000155 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000156#else
157 -1,
158#endif
aurel32ca587a82008-12-18 22:44:13 +0000159 -1, /* SIGPOLL */
160 -1,
161 -1,
162 -1,
163 -1,
164 -1,
165 -1,
166 -1,
167 -1,
168 -1,
169 -1,
170 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000171#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000172 __SIGRTMIN + 1,
173 __SIGRTMIN + 2,
174 __SIGRTMIN + 3,
175 __SIGRTMIN + 4,
176 __SIGRTMIN + 5,
177 __SIGRTMIN + 6,
178 __SIGRTMIN + 7,
179 __SIGRTMIN + 8,
180 __SIGRTMIN + 9,
181 __SIGRTMIN + 10,
182 __SIGRTMIN + 11,
183 __SIGRTMIN + 12,
184 __SIGRTMIN + 13,
185 __SIGRTMIN + 14,
186 __SIGRTMIN + 15,
187 __SIGRTMIN + 16,
188 __SIGRTMIN + 17,
189 __SIGRTMIN + 18,
190 __SIGRTMIN + 19,
191 __SIGRTMIN + 20,
192 __SIGRTMIN + 21,
193 __SIGRTMIN + 22,
194 __SIGRTMIN + 23,
195 __SIGRTMIN + 24,
196 __SIGRTMIN + 25,
197 __SIGRTMIN + 26,
198 __SIGRTMIN + 27,
199 __SIGRTMIN + 28,
200 __SIGRTMIN + 29,
201 __SIGRTMIN + 30,
202 __SIGRTMIN + 31,
203 -1, /* SIGCANCEL */
204 __SIGRTMIN,
205 __SIGRTMIN + 32,
206 __SIGRTMIN + 33,
207 __SIGRTMIN + 34,
208 __SIGRTMIN + 35,
209 __SIGRTMIN + 36,
210 __SIGRTMIN + 37,
211 __SIGRTMIN + 38,
212 __SIGRTMIN + 39,
213 __SIGRTMIN + 40,
214 __SIGRTMIN + 41,
215 __SIGRTMIN + 42,
216 __SIGRTMIN + 43,
217 __SIGRTMIN + 44,
218 __SIGRTMIN + 45,
219 __SIGRTMIN + 46,
220 __SIGRTMIN + 47,
221 __SIGRTMIN + 48,
222 __SIGRTMIN + 49,
223 __SIGRTMIN + 50,
224 __SIGRTMIN + 51,
225 __SIGRTMIN + 52,
226 __SIGRTMIN + 53,
227 __SIGRTMIN + 54,
228 __SIGRTMIN + 55,
229 __SIGRTMIN + 56,
230 __SIGRTMIN + 57,
231 __SIGRTMIN + 58,
232 __SIGRTMIN + 59,
233 __SIGRTMIN + 60,
234 __SIGRTMIN + 61,
235 __SIGRTMIN + 62,
236 __SIGRTMIN + 63,
237 __SIGRTMIN + 64,
238 __SIGRTMIN + 65,
239 __SIGRTMIN + 66,
240 __SIGRTMIN + 67,
241 __SIGRTMIN + 68,
242 __SIGRTMIN + 69,
243 __SIGRTMIN + 70,
244 __SIGRTMIN + 71,
245 __SIGRTMIN + 72,
246 __SIGRTMIN + 73,
247 __SIGRTMIN + 74,
248 __SIGRTMIN + 75,
249 __SIGRTMIN + 76,
250 __SIGRTMIN + 77,
251 __SIGRTMIN + 78,
252 __SIGRTMIN + 79,
253 __SIGRTMIN + 80,
254 __SIGRTMIN + 81,
255 __SIGRTMIN + 82,
256 __SIGRTMIN + 83,
257 __SIGRTMIN + 84,
258 __SIGRTMIN + 85,
259 __SIGRTMIN + 86,
260 __SIGRTMIN + 87,
261 __SIGRTMIN + 88,
262 __SIGRTMIN + 89,
263 __SIGRTMIN + 90,
264 __SIGRTMIN + 91,
265 __SIGRTMIN + 92,
266 __SIGRTMIN + 93,
267 __SIGRTMIN + 94,
268 __SIGRTMIN + 95,
269 -1, /* SIGINFO */
270 -1, /* UNKNOWN */
271 -1, /* DEFAULT */
272 -1,
273 -1,
274 -1,
275 -1,
276 -1,
277 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000278#endif
aurel32ca587a82008-12-18 22:44:13 +0000279};
bellard8f447cc2006-06-14 15:21:14 +0000280#else
aurel32ca587a82008-12-18 22:44:13 +0000281/* In system mode we only need SIGINT and SIGTRAP; other signals
282 are not yet supported. */
283
284enum {
285 TARGET_SIGINT = 2,
286 TARGET_SIGTRAP = 5
287};
288
289static int gdb_signal_table[] = {
290 -1,
291 -1,
292 TARGET_SIGINT,
293 -1,
294 -1,
295 TARGET_SIGTRAP
296};
bellard8f447cc2006-06-14 15:21:14 +0000297#endif
bellardb4608c02003-06-27 17:34:32 +0000298
aurel32ca587a82008-12-18 22:44:13 +0000299#ifdef CONFIG_USER_ONLY
300static int target_signal_to_gdb (int sig)
301{
302 int i;
303 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
304 if (gdb_signal_table[i] == sig)
305 return i;
306 return GDB_SIGNAL_UNKNOWN;
307}
308#endif
309
310static int gdb_signal_to_target (int sig)
311{
312 if (sig < ARRAY_SIZE (gdb_signal_table))
313 return gdb_signal_table[sig];
314 else
315 return -1;
316}
317
pbrook56aebc82008-10-11 17:55:29 +0000318typedef struct GDBRegisterState {
319 int base_reg;
320 int num_regs;
321 gdb_reg_cb get_reg;
322 gdb_reg_cb set_reg;
323 const char *xml;
324 struct GDBRegisterState *next;
325} GDBRegisterState;
326
Luc Michel8f468632019-01-07 15:23:45 +0000327typedef struct GDBProcess {
328 uint32_t pid;
329 bool attached;
Luc Michelc145eea2019-01-07 15:23:46 +0000330
331 char target_xml[1024];
Luc Michel8f468632019-01-07 15:23:45 +0000332} GDBProcess;
333
bellard858693c2004-03-31 18:52:07 +0000334enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000335 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000336 RS_IDLE,
337 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400338 RS_GETLINE_ESC,
339 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000340 RS_CHKSUM1,
341 RS_CHKSUM2,
342};
bellard858693c2004-03-31 18:52:07 +0000343typedef struct GDBState {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200344 CPUState *c_cpu; /* current CPU for step/continue ops */
345 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200346 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000347 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000348 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000349 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400350 int line_sum; /* running checksum */
351 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000352 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000353 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000354 int signal;
bellard41625032005-04-24 10:07:11 +0000355#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000356 int fd;
bellard41625032005-04-24 10:07:11 +0000357 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000358#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300359 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300360 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000361#endif
Luc Michel8f468632019-01-07 15:23:45 +0000362 bool multiprocess;
363 GDBProcess *processes;
364 int process_num;
Meador Ingecdb432b2012-03-15 17:49:45 +0000365 char syscall_buf[256];
366 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000367} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000368
edgar_igl60897d32008-05-09 08:25:14 +0000369/* By default use no IRQs and no timers while single stepping so as to
370 * make single stepping like an ICE HW step.
371 */
372static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
373
aliguori880a7572008-11-18 20:30:24 +0000374static GDBState *gdbserver_state;
375
Andreas Färber5b50e792013-06-29 04:18:45 +0200376bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000377
bellard1fddef42005-04-17 19:16:13 +0000378#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000379/* XXX: This is not thread safe. Do we care? */
380static int gdbserver_fd = -1;
381
bellard858693c2004-03-31 18:52:07 +0000382static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000383{
384 uint8_t ch;
385 int ret;
386
387 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000388 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000389 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000390 if (errno == ECONNRESET)
391 s->fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200392 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000393 return -1;
394 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000395 close(s->fd);
396 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000397 return -1;
398 } else {
399 break;
400 }
401 }
402 return ch;
403}
pbrook4046d912007-01-28 01:53:16 +0000404#endif
bellardb4608c02003-06-27 17:34:32 +0000405
blueswir1654efcf2009-04-18 07:29:59 +0000406static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000407 GDB_SYS_UNKNOWN,
408 GDB_SYS_ENABLED,
409 GDB_SYS_DISABLED,
410} gdb_syscall_mode;
411
Liviu Ionescua38bb072014-12-11 12:07:48 +0000412/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000413int use_gdb_syscalls(void)
414{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100415 SemihostingTarget target = semihosting_get_target();
416 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000417 /* -semihosting-config target=native */
418 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100419 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000420 /* -semihosting-config target=gdb */
421 return true;
422 }
423
424 /* -semihosting-config target=auto */
425 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000426 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000427 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
428 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000429 }
430 return gdb_syscall_mode == GDB_SYS_ENABLED;
431}
432
edgar_iglba70a622008-03-14 06:10:42 +0000433/* Resume execution. */
434static inline void gdb_continue(GDBState *s)
435{
Doug Gale5c9522b2017-12-02 20:30:37 -0500436
edgar_iglba70a622008-03-14 06:10:42 +0000437#ifdef CONFIG_USER_ONLY
438 s->running_state = 1;
Doug Gale5c9522b2017-12-02 20:30:37 -0500439 trace_gdbstub_op_continue();
edgar_iglba70a622008-03-14 06:10:42 +0000440#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200441 if (!runstate_needs_reset()) {
Doug Gale5c9522b2017-12-02 20:30:37 -0500442 trace_gdbstub_op_continue();
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200443 vm_start();
444 }
edgar_iglba70a622008-03-14 06:10:42 +0000445#endif
446}
447
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100448/*
449 * Resume execution, per CPU actions. For user-mode emulation it's
450 * equivalent to gdb_continue.
451 */
452static int gdb_continue_partial(GDBState *s, char *newstates)
453{
454 CPUState *cpu;
455 int res = 0;
456#ifdef CONFIG_USER_ONLY
457 /*
458 * This is not exactly accurate, but it's an improvement compared to the
459 * previous situation, where only one CPU would be single-stepped.
460 */
461 CPU_FOREACH(cpu) {
462 if (newstates[cpu->cpu_index] == 's') {
Doug Gale5c9522b2017-12-02 20:30:37 -0500463 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100464 cpu_single_step(cpu, sstep_flags);
465 }
466 }
467 s->running_state = 1;
468#else
469 int flag = 0;
470
471 if (!runstate_needs_reset()) {
472 if (vm_prepare_start()) {
473 return 0;
474 }
475
476 CPU_FOREACH(cpu) {
477 switch (newstates[cpu->cpu_index]) {
478 case 0:
479 case 1:
480 break; /* nothing to do here */
481 case 's':
Doug Gale5c9522b2017-12-02 20:30:37 -0500482 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100483 cpu_single_step(cpu, sstep_flags);
484 cpu_resume(cpu);
485 flag = 1;
486 break;
487 case 'c':
Doug Gale5c9522b2017-12-02 20:30:37 -0500488 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100489 cpu_resume(cpu);
490 flag = 1;
491 break;
492 default:
493 res = -1;
494 break;
495 }
496 }
497 }
498 if (flag) {
499 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
500 }
501#endif
502 return res;
503}
504
bellard858693c2004-03-31 18:52:07 +0000505static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000506{
pbrook4046d912007-01-28 01:53:16 +0000507#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000508 int ret;
509
510 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000511 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000512 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200513 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000514 return;
515 } else {
516 buf += ret;
517 len -= ret;
518 }
519 }
pbrook4046d912007-01-28 01:53:16 +0000520#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100521 /* XXX this blocks entire thread. Rewrite to use
522 * qemu_chr_fe_write and background I/O callbacks */
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300523 qemu_chr_fe_write_all(&s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000524#endif
bellardb4608c02003-06-27 17:34:32 +0000525}
526
527static inline int fromhex(int v)
528{
529 if (v >= '0' && v <= '9')
530 return v - '0';
531 else if (v >= 'A' && v <= 'F')
532 return v - 'A' + 10;
533 else if (v >= 'a' && v <= 'f')
534 return v - 'a' + 10;
535 else
536 return 0;
537}
538
539static inline int tohex(int v)
540{
541 if (v < 10)
542 return v + '0';
543 else
544 return v - 10 + 'a';
545}
546
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -0300547/* writes 2*len+1 bytes in buf */
bellardb4608c02003-06-27 17:34:32 +0000548static void memtohex(char *buf, const uint8_t *mem, int len)
549{
550 int i, c;
551 char *q;
552 q = buf;
553 for(i = 0; i < len; i++) {
554 c = mem[i];
555 *q++ = tohex(c >> 4);
556 *q++ = tohex(c & 0xf);
557 }
558 *q = '\0';
559}
560
561static void hextomem(uint8_t *mem, const char *buf, int len)
562{
563 int i;
564
565 for(i = 0; i < len; i++) {
566 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
567 buf += 2;
568 }
569}
570
Doug Gale5c9522b2017-12-02 20:30:37 -0500571static void hexdump(const char *buf, int len,
572 void (*trace_fn)(size_t ofs, char const *text))
573{
574 char line_buffer[3 * 16 + 4 + 16 + 1];
575
576 size_t i;
577 for (i = 0; i < len || (i & 0xF); ++i) {
578 size_t byte_ofs = i & 15;
579
580 if (byte_ofs == 0) {
581 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
582 line_buffer[3 * 16 + 4 + 16] = 0;
583 }
584
585 size_t col_group = (i >> 2) & 3;
586 size_t hex_col = byte_ofs * 3 + col_group;
587 size_t txt_col = 3 * 16 + 4 + byte_ofs;
588
589 if (i < len) {
590 char value = buf[i];
591
592 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
593 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
594 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
595 ? value
596 : '.';
597 }
598
599 if (byte_ofs == 0xF)
600 trace_fn(i & -16, line_buffer);
601 }
602}
603
bellardb4608c02003-06-27 17:34:32 +0000604/* return -1 if error, 0 if OK */
Doug Gale5c9522b2017-12-02 20:30:37 -0500605static int put_packet_binary(GDBState *s, const char *buf, int len, bool dump)
bellardb4608c02003-06-27 17:34:32 +0000606{
pbrook56aebc82008-10-11 17:55:29 +0000607 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000608 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000609
Doug Gale5c9522b2017-12-02 20:30:37 -0500610 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
611 hexdump(buf, len, trace_gdbstub_io_binaryreply);
612 }
613
bellardb4608c02003-06-27 17:34:32 +0000614 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000615 p = s->last_packet;
616 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000617 memcpy(p, buf, len);
618 p += len;
bellardb4608c02003-06-27 17:34:32 +0000619 csum = 0;
620 for(i = 0; i < len; i++) {
621 csum += buf[i];
622 }
pbrook4046d912007-01-28 01:53:16 +0000623 *(p++) = '#';
624 *(p++) = tohex((csum >> 4) & 0xf);
625 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000626
pbrook4046d912007-01-28 01:53:16 +0000627 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000628 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000629
pbrook4046d912007-01-28 01:53:16 +0000630#ifdef CONFIG_USER_ONLY
631 i = get_char(s);
632 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000633 return -1;
pbrook4046d912007-01-28 01:53:16 +0000634 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000635 break;
pbrook4046d912007-01-28 01:53:16 +0000636#else
637 break;
638#endif
bellardb4608c02003-06-27 17:34:32 +0000639 }
640 return 0;
641}
642
pbrook56aebc82008-10-11 17:55:29 +0000643/* return -1 if error, 0 if OK */
644static int put_packet(GDBState *s, const char *buf)
645{
Doug Gale5c9522b2017-12-02 20:30:37 -0500646 trace_gdbstub_io_reply(buf);
pbrook56aebc82008-10-11 17:55:29 +0000647
Doug Gale5c9522b2017-12-02 20:30:37 -0500648 return put_packet_binary(s, buf, strlen(buf), false);
pbrook56aebc82008-10-11 17:55:29 +0000649}
650
pbrook56aebc82008-10-11 17:55:29 +0000651/* Encode data using the encoding for 'x' packets. */
652static int memtox(char *buf, const char *mem, int len)
653{
654 char *p = buf;
655 char c;
656
657 while (len--) {
658 c = *(mem++);
659 switch (c) {
660 case '#': case '$': case '*': case '}':
661 *(p++) = '}';
662 *(p++) = c ^ 0x20;
663 break;
664 default:
665 *(p++) = c;
666 break;
667 }
668 }
669 return p - buf;
670}
671
Luc Michel1a227332019-01-07 15:23:45 +0000672static uint32_t gdb_get_cpu_pid(const GDBState *s, CPUState *cpu)
673{
Luc Michel1a227332019-01-07 15:23:45 +0000674 /* TODO: In user mode, we should use the task state PID */
Peter Maydell46f5abc2019-01-29 11:46:06 +0000675 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
676 /* Return the default process' PID */
677 return s->processes[s->process_num - 1].pid;
678 }
679 return cpu->cluster_index + 1;
Luc Michel1a227332019-01-07 15:23:45 +0000680}
681
Luc Michel7d8c87d2019-01-07 15:23:45 +0000682static GDBProcess *gdb_get_process(const GDBState *s, uint32_t pid)
683{
684 int i;
685
686 if (!pid) {
687 /* 0 means any process, we take the first one */
688 return &s->processes[0];
689 }
690
691 for (i = 0; i < s->process_num; i++) {
692 if (s->processes[i].pid == pid) {
693 return &s->processes[i];
694 }
695 }
696
697 return NULL;
698}
699
700static GDBProcess *gdb_get_cpu_process(const GDBState *s, CPUState *cpu)
701{
702 return gdb_get_process(s, gdb_get_cpu_pid(s, cpu));
703}
704
705static CPUState *find_cpu(uint32_t thread_id)
706{
707 CPUState *cpu;
708
709 CPU_FOREACH(cpu) {
710 if (cpu_gdb_index(cpu) == thread_id) {
711 return cpu;
712 }
713 }
714
715 return NULL;
716}
717
Luc Michele40e5202019-01-07 15:23:46 +0000718static CPUState *get_first_cpu_in_process(const GDBState *s,
719 GDBProcess *process)
720{
721 CPUState *cpu;
722
723 CPU_FOREACH(cpu) {
724 if (gdb_get_cpu_pid(s, cpu) == process->pid) {
725 return cpu;
726 }
727 }
728
729 return NULL;
730}
731
732static CPUState *gdb_next_cpu_in_process(const GDBState *s, CPUState *cpu)
733{
734 uint32_t pid = gdb_get_cpu_pid(s, cpu);
735 cpu = CPU_NEXT(cpu);
736
737 while (cpu) {
738 if (gdb_get_cpu_pid(s, cpu) == pid) {
739 break;
740 }
741
742 cpu = CPU_NEXT(cpu);
743 }
744
745 return cpu;
746}
747
Luc Michele40e5202019-01-07 15:23:46 +0000748/* Return the cpu following @cpu, while ignoring unattached processes. */
749static CPUState *gdb_next_attached_cpu(const GDBState *s, CPUState *cpu)
750{
751 cpu = CPU_NEXT(cpu);
752
753 while (cpu) {
754 if (gdb_get_cpu_process(s, cpu)->attached) {
755 break;
756 }
757
758 cpu = CPU_NEXT(cpu);
759 }
760
761 return cpu;
762}
763
764/* Return the first attached cpu */
765static CPUState *gdb_first_attached_cpu(const GDBState *s)
766{
767 CPUState *cpu = first_cpu;
768 GDBProcess *process = gdb_get_cpu_process(s, cpu);
769
770 if (!process->attached) {
771 return gdb_next_attached_cpu(s, cpu);
772 }
773
774 return cpu;
775}
776
Luc Michelab65eed2019-01-29 11:46:03 +0000777static CPUState *gdb_get_cpu(const GDBState *s, uint32_t pid, uint32_t tid)
778{
779 GDBProcess *process;
780 CPUState *cpu;
781
782 if (!pid && !tid) {
783 /* 0 means any process/thread, we take the first attached one */
784 return gdb_first_attached_cpu(s);
785 } else if (pid && !tid) {
786 /* any thread in a specific process */
787 process = gdb_get_process(s, pid);
788
789 if (process == NULL) {
790 return NULL;
791 }
792
793 if (!process->attached) {
794 return NULL;
795 }
796
797 return get_first_cpu_in_process(s, process);
798 } else {
799 /* a specific thread */
800 cpu = find_cpu(tid);
801
802 if (cpu == NULL) {
803 return NULL;
804 }
805
806 process = gdb_get_cpu_process(s, cpu);
807
808 if (pid && process->pid != pid) {
809 return NULL;
810 }
811
812 if (!process->attached) {
813 return NULL;
814 }
815
816 return cpu;
817 }
818}
819
Luc Michelc145eea2019-01-07 15:23:46 +0000820static const char *get_feature_xml(const GDBState *s, const char *p,
821 const char **newp, GDBProcess *process)
pbrook56aebc82008-10-11 17:55:29 +0000822{
pbrook56aebc82008-10-11 17:55:29 +0000823 size_t len;
824 int i;
825 const char *name;
Luc Michelc145eea2019-01-07 15:23:46 +0000826 CPUState *cpu = get_first_cpu_in_process(s, process);
827 CPUClass *cc = CPU_GET_CLASS(cpu);
pbrook56aebc82008-10-11 17:55:29 +0000828
829 len = 0;
830 while (p[len] && p[len] != ':')
831 len++;
832 *newp = p + len;
833
834 name = NULL;
835 if (strncmp(p, "target.xml", len) == 0) {
Luc Michelc145eea2019-01-07 15:23:46 +0000836 char *buf = process->target_xml;
837 const size_t buf_sz = sizeof(process->target_xml);
pbrook56aebc82008-10-11 17:55:29 +0000838
Luc Michelc145eea2019-01-07 15:23:46 +0000839 /* Generate the XML description for this CPU. */
840 if (!buf[0]) {
841 GDBRegisterState *r;
842
843 pstrcat(buf, buf_sz,
David Hildenbrandb3820e62015-12-03 13:14:41 +0100844 "<?xml version=\"1.0\"?>"
845 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
846 "<target>");
847 if (cc->gdb_arch_name) {
848 gchar *arch = cc->gdb_arch_name(cpu);
Luc Michelc145eea2019-01-07 15:23:46 +0000849 pstrcat(buf, buf_sz, "<architecture>");
850 pstrcat(buf, buf_sz, arch);
851 pstrcat(buf, buf_sz, "</architecture>");
David Hildenbrandb3820e62015-12-03 13:14:41 +0100852 g_free(arch);
853 }
Luc Michelc145eea2019-01-07 15:23:46 +0000854 pstrcat(buf, buf_sz, "<xi:include href=\"");
855 pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
856 pstrcat(buf, buf_sz, "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200857 for (r = cpu->gdb_regs; r; r = r->next) {
Luc Michelc145eea2019-01-07 15:23:46 +0000858 pstrcat(buf, buf_sz, "<xi:include href=\"");
859 pstrcat(buf, buf_sz, r->xml);
860 pstrcat(buf, buf_sz, "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000861 }
Luc Michelc145eea2019-01-07 15:23:46 +0000862 pstrcat(buf, buf_sz, "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000863 }
Luc Michelc145eea2019-01-07 15:23:46 +0000864 return buf;
pbrook56aebc82008-10-11 17:55:29 +0000865 }
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100866 if (cc->gdb_get_dynamic_xml) {
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100867 char *xmlname = g_strndup(p, len);
868 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
869
870 g_free(xmlname);
871 if (xml) {
872 return xml;
873 }
874 }
pbrook56aebc82008-10-11 17:55:29 +0000875 for (i = 0; ; i++) {
876 name = xml_builtin[i][0];
877 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
878 break;
879 }
880 return name ? xml_builtin[i][1] : NULL;
881}
pbrook56aebc82008-10-11 17:55:29 +0000882
Andreas Färber385b9f02013-06-27 18:25:36 +0200883static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000884{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200885 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200886 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000887 GDBRegisterState *r;
888
Andreas Färbera0e372f2013-06-28 23:18:47 +0200889 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200890 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200891 }
pbrook56aebc82008-10-11 17:55:29 +0000892
Andreas Färbereac8b352013-06-28 21:11:37 +0200893 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000894 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
895 return r->get_reg(env, mem_buf, reg - r->base_reg);
896 }
897 }
898 return 0;
899}
900
Andreas Färber385b9f02013-06-27 18:25:36 +0200901static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000902{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200903 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200904 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000905 GDBRegisterState *r;
906
Andreas Färbera0e372f2013-06-28 23:18:47 +0200907 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200908 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200909 }
pbrook56aebc82008-10-11 17:55:29 +0000910
Andreas Färbereac8b352013-06-28 21:11:37 +0200911 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000912 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
913 return r->set_reg(env, mem_buf, reg - r->base_reg);
914 }
915 }
916 return 0;
917}
918
919/* Register a supplemental set of CPU registers. If g_pos is nonzero it
920 specifies the first register number and these registers are included in
921 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
922 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
923 */
924
Andreas Färber22169d42013-06-28 21:27:39 +0200925void gdb_register_coprocessor(CPUState *cpu,
926 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
927 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000928{
929 GDBRegisterState *s;
930 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000931
Andreas Färbereac8b352013-06-28 21:11:37 +0200932 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000933 while (*p) {
934 /* Check for duplicates. */
935 if (strcmp((*p)->xml, xml) == 0)
936 return;
937 p = &(*p)->next;
938 }
Stefan Weil9643c252011-10-18 22:25:38 +0200939
940 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200941 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200942 s->num_regs = num_regs;
943 s->get_reg = get_reg;
944 s->set_reg = set_reg;
945 s->xml = xml;
946
pbrook56aebc82008-10-11 17:55:29 +0000947 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200948 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000949 *p = s;
950 if (g_pos) {
951 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800952 error_report("Error: Bad gdb register numbering for '%s', "
953 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200954 } else {
955 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000956 }
957 }
958}
959
aliguoria1d1bb32008-11-18 20:07:32 +0000960#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100961/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
962static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
963{
964 static const int xlat[] = {
965 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
966 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
967 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
968 };
969
970 CPUClass *cc = CPU_GET_CLASS(cpu);
971 int cputype = xlat[gdbtype];
972
973 if (cc->gdb_stop_before_watchpoint) {
974 cputype |= BP_STOP_BEFORE_ACCESS;
975 }
976 return cputype;
977}
aliguoria1d1bb32008-11-18 20:07:32 +0000978#endif
979
Jon Doron77f6ce52019-05-29 09:41:35 +0300980static int gdb_breakpoint_insert(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +0000981{
Andreas Färber182735e2013-05-29 22:29:20 +0200982 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000983 int err = 0;
984
Andreas Färber62278812013-06-27 17:12:06 +0200985 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200986 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200987 }
aliguorie22a25c2009-03-12 20:12:48 +0000988
aliguoria1d1bb32008-11-18 20:07:32 +0000989 switch (type) {
990 case GDB_BREAKPOINT_SW:
991 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200992 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200993 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
994 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000995 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200996 }
aliguori880a7572008-11-18 20:30:24 +0000997 }
998 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000999#ifndef CONFIG_USER_ONLY
1000 case GDB_WATCHPOINT_WRITE:
1001 case GDB_WATCHPOINT_READ:
1002 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001003 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001004 err = cpu_watchpoint_insert(cpu, addr, len,
1005 xlat_gdb_type(cpu, type), NULL);
1006 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001007 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +01001008 }
aliguori880a7572008-11-18 20:30:24 +00001009 }
1010 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001011#endif
1012 default:
1013 return -ENOSYS;
1014 }
1015}
1016
Jon Doron77f6ce52019-05-29 09:41:35 +03001017static int gdb_breakpoint_remove(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +00001018{
Andreas Färber182735e2013-05-29 22:29:20 +02001019 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001020 int err = 0;
1021
Andreas Färber62278812013-06-27 17:12:06 +02001022 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001023 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001024 }
aliguorie22a25c2009-03-12 20:12:48 +00001025
aliguoria1d1bb32008-11-18 20:07:32 +00001026 switch (type) {
1027 case GDB_BREAKPOINT_SW:
1028 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001029 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001030 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
1031 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001032 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001033 }
aliguori880a7572008-11-18 20:30:24 +00001034 }
1035 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001036#ifndef CONFIG_USER_ONLY
1037 case GDB_WATCHPOINT_WRITE:
1038 case GDB_WATCHPOINT_READ:
1039 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001040 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001041 err = cpu_watchpoint_remove(cpu, addr, len,
1042 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +00001043 if (err)
1044 break;
1045 }
1046 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001047#endif
1048 default:
1049 return -ENOSYS;
1050 }
1051}
1052
Luc Michel546f3c62019-01-07 15:23:46 +00001053static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu)
1054{
1055 cpu_breakpoint_remove_all(cpu, BP_GDB);
1056#ifndef CONFIG_USER_ONLY
1057 cpu_watchpoint_remove_all(cpu, BP_GDB);
1058#endif
1059}
1060
1061static void gdb_process_breakpoint_remove_all(const GDBState *s, GDBProcess *p)
1062{
1063 CPUState *cpu = get_first_cpu_in_process(s, p);
1064
1065 while (cpu) {
1066 gdb_cpu_breakpoint_remove_all(cpu);
1067 cpu = gdb_next_cpu_in_process(s, cpu);
1068 }
1069}
1070
aliguori880a7572008-11-18 20:30:24 +00001071static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +00001072{
Andreas Färber182735e2013-05-29 22:29:20 +02001073 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001074
aliguorie22a25c2009-03-12 20:12:48 +00001075 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001076 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +00001077 return;
1078 }
1079
Andreas Färberbdc44642013-06-24 23:50:24 +02001080 CPU_FOREACH(cpu) {
Luc Michel546f3c62019-01-07 15:23:46 +00001081 gdb_cpu_breakpoint_remove_all(cpu);
aliguori880a7572008-11-18 20:30:24 +00001082 }
aliguoria1d1bb32008-11-18 20:07:32 +00001083}
1084
aurel32fab9d282009-04-08 21:29:37 +00001085static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
1086{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001087 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +02001088
1089 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -07001090 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +00001091}
1092
Luc Michel1a227332019-01-07 15:23:45 +00001093static char *gdb_fmt_thread_id(const GDBState *s, CPUState *cpu,
1094 char *buf, size_t buf_size)
1095{
1096 if (s->multiprocess) {
1097 snprintf(buf, buf_size, "p%02x.%02x",
1098 gdb_get_cpu_pid(s, cpu), cpu_gdb_index(cpu));
1099 } else {
1100 snprintf(buf, buf_size, "%02x", cpu_gdb_index(cpu));
1101 }
1102
1103 return buf;
1104}
1105
Luc Michel7d8c87d2019-01-07 15:23:45 +00001106typedef enum GDBThreadIdKind {
1107 GDB_ONE_THREAD = 0,
1108 GDB_ALL_THREADS, /* One process, all threads */
1109 GDB_ALL_PROCESSES,
1110 GDB_READ_THREAD_ERR
1111} GDBThreadIdKind;
1112
1113static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1114 uint32_t *pid, uint32_t *tid)
1115{
1116 unsigned long p, t;
1117 int ret;
1118
1119 if (*buf == 'p') {
1120 buf++;
1121 ret = qemu_strtoul(buf, &buf, 16, &p);
1122
1123 if (ret) {
1124 return GDB_READ_THREAD_ERR;
1125 }
1126
1127 /* Skip '.' */
1128 buf++;
1129 } else {
1130 p = 1;
1131 }
1132
1133 ret = qemu_strtoul(buf, &buf, 16, &t);
1134
1135 if (ret) {
1136 return GDB_READ_THREAD_ERR;
1137 }
1138
1139 *end_buf = buf;
1140
1141 if (p == -1) {
1142 return GDB_ALL_PROCESSES;
1143 }
1144
1145 if (pid) {
1146 *pid = p;
1147 }
1148
1149 if (t == -1) {
1150 return GDB_ALL_THREADS;
1151 }
1152
1153 if (tid) {
1154 *tid = t;
1155 }
1156
1157 return GDB_ONE_THREAD;
1158}
1159
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001160/**
1161 * gdb_handle_vcont - Parses and handles a vCont packet.
1162 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1163 * a format error, 0 on success.
1164 */
1165static int gdb_handle_vcont(GDBState *s, const char *p)
1166{
Luc Michele40e5202019-01-07 15:23:46 +00001167 int res, signal = 0;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001168 char cur_action;
1169 char *newstates;
1170 unsigned long tmp;
Luc Michele40e5202019-01-07 15:23:46 +00001171 uint32_t pid, tid;
1172 GDBProcess *process;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001173 CPUState *cpu;
Luc Michelc99ef792019-03-26 12:53:26 +00001174 GDBThreadIdKind kind;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001175#ifdef CONFIG_USER_ONLY
1176 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
1177
1178 CPU_FOREACH(cpu) {
1179 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1180 }
Like Xu5cc87672019-05-19 04:54:21 +08001181#else
1182 MachineState *ms = MACHINE(qdev_get_machine());
1183 unsigned int max_cpus = ms->smp.max_cpus;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001184#endif
1185 /* uninitialised CPUs stay 0 */
1186 newstates = g_new0(char, max_cpus);
1187
1188 /* mark valid CPUs with 1 */
1189 CPU_FOREACH(cpu) {
1190 newstates[cpu->cpu_index] = 1;
1191 }
1192
1193 /*
1194 * res keeps track of what error we are returning, with -ENOTSUP meaning
1195 * that the command is unknown or unsupported, thus returning an empty
1196 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1197 * or incorrect parameters passed.
1198 */
1199 res = 0;
1200 while (*p) {
1201 if (*p++ != ';') {
1202 res = -ENOTSUP;
1203 goto out;
1204 }
1205
1206 cur_action = *p++;
1207 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +01001208 cur_action = qemu_tolower(cur_action);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001209 res = qemu_strtoul(p + 1, &p, 16, &tmp);
1210 if (res) {
1211 goto out;
1212 }
1213 signal = gdb_signal_to_target(tmp);
1214 } else if (cur_action != 'c' && cur_action != 's') {
1215 /* unknown/invalid/unsupported command */
1216 res = -ENOTSUP;
1217 goto out;
1218 }
Luc Michele40e5202019-01-07 15:23:46 +00001219
Luc Michelc99ef792019-03-26 12:53:26 +00001220 if (*p == '\0' || *p == ';') {
1221 /*
1222 * No thread specifier, action is on "all threads". The
1223 * specification is unclear regarding the process to act on. We
1224 * choose all processes.
1225 */
1226 kind = GDB_ALL_PROCESSES;
1227 } else if (*p++ == ':') {
1228 kind = read_thread_id(p, &p, &pid, &tid);
1229 } else {
Luc Michele40e5202019-01-07 15:23:46 +00001230 res = -ENOTSUP;
1231 goto out;
1232 }
1233
Luc Michelc99ef792019-03-26 12:53:26 +00001234 switch (kind) {
Luc Michele40e5202019-01-07 15:23:46 +00001235 case GDB_READ_THREAD_ERR:
1236 res = -EINVAL;
1237 goto out;
1238
1239 case GDB_ALL_PROCESSES:
1240 cpu = gdb_first_attached_cpu(s);
1241 while (cpu) {
1242 if (newstates[cpu->cpu_index] == 1) {
1243 newstates[cpu->cpu_index] = cur_action;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001244 }
Luc Michele40e5202019-01-07 15:23:46 +00001245
1246 cpu = gdb_next_attached_cpu(s, cpu);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001247 }
Luc Michele40e5202019-01-07 15:23:46 +00001248 break;
1249
1250 case GDB_ALL_THREADS:
1251 process = gdb_get_process(s, pid);
1252
1253 if (!process->attached) {
1254 res = -EINVAL;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001255 goto out;
1256 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001257
Luc Michele40e5202019-01-07 15:23:46 +00001258 cpu = get_first_cpu_in_process(s, process);
1259 while (cpu) {
1260 if (newstates[cpu->cpu_index] == 1) {
1261 newstates[cpu->cpu_index] = cur_action;
1262 }
1263
1264 cpu = gdb_next_cpu_in_process(s, cpu);
1265 }
1266 break;
1267
1268 case GDB_ONE_THREAD:
1269 cpu = gdb_get_cpu(s, pid, tid);
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001270
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001271 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001272 if (!cpu) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001273 res = -EINVAL;
1274 goto out;
1275 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001276
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001277 /* only use if no previous match occourred */
1278 if (newstates[cpu->cpu_index] == 1) {
1279 newstates[cpu->cpu_index] = cur_action;
1280 }
Luc Michele40e5202019-01-07 15:23:46 +00001281 break;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001282 }
1283 }
1284 s->signal = signal;
1285 gdb_continue_partial(s, newstates);
1286
1287out:
1288 g_free(newstates);
1289
1290 return res;
1291}
1292
Jon Dorond14055d2019-05-29 09:41:29 +03001293typedef union GdbCmdVariant {
1294 const char *data;
1295 uint8_t opcode;
1296 unsigned long val_ul;
1297 unsigned long long val_ull;
1298 struct {
1299 GDBThreadIdKind kind;
1300 uint32_t pid;
1301 uint32_t tid;
1302 } thread_id;
1303} GdbCmdVariant;
1304
1305static const char *cmd_next_param(const char *param, const char delimiter)
1306{
1307 static const char all_delimiters[] = ",;:=";
1308 char curr_delimiters[2] = {0};
1309 const char *delimiters;
1310
1311 if (delimiter == '?') {
1312 delimiters = all_delimiters;
1313 } else if (delimiter == '0') {
1314 return strchr(param, '\0');
1315 } else if (delimiter == '.' && *param) {
1316 return param + 1;
1317 } else {
1318 curr_delimiters[0] = delimiter;
1319 delimiters = curr_delimiters;
1320 }
1321
1322 param += strcspn(param, delimiters);
1323 if (*param) {
1324 param++;
1325 }
1326 return param;
1327}
1328
1329static int cmd_parse_params(const char *data, const char *schema,
1330 GdbCmdVariant *params, int *num_params)
1331{
1332 int curr_param;
1333 const char *curr_schema, *curr_data;
1334
1335 *num_params = 0;
1336
1337 if (!schema) {
1338 return 0;
1339 }
1340
1341 curr_schema = schema;
1342 curr_param = 0;
1343 curr_data = data;
1344 while (curr_schema[0] && curr_schema[1] && *curr_data) {
1345 switch (curr_schema[0]) {
1346 case 'l':
1347 if (qemu_strtoul(curr_data, &curr_data, 16,
1348 &params[curr_param].val_ul)) {
1349 return -EINVAL;
1350 }
1351 curr_param++;
1352 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1353 break;
1354 case 'L':
1355 if (qemu_strtou64(curr_data, &curr_data, 16,
1356 (uint64_t *)&params[curr_param].val_ull)) {
1357 return -EINVAL;
1358 }
1359 curr_param++;
1360 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1361 break;
1362 case 's':
1363 params[curr_param].data = curr_data;
1364 curr_param++;
1365 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1366 break;
1367 case 'o':
1368 params[curr_param].opcode = *(uint8_t *)curr_data;
1369 curr_param++;
1370 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1371 break;
1372 case 't':
1373 params[curr_param].thread_id.kind =
1374 read_thread_id(curr_data, &curr_data,
1375 &params[curr_param].thread_id.pid,
1376 &params[curr_param].thread_id.tid);
1377 curr_param++;
1378 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1379 break;
1380 case '?':
1381 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1382 break;
1383 default:
1384 return -EINVAL;
1385 }
1386 curr_schema += 2;
1387 }
1388
1389 *num_params = curr_param;
1390 return 0;
1391}
1392
1393typedef struct GdbCmdContext {
1394 GDBState *s;
1395 GdbCmdVariant *params;
1396 int num_params;
1397 uint8_t mem_buf[MAX_PACKET_LENGTH];
1398 char str_buf[MAX_PACKET_LENGTH + 1];
1399} GdbCmdContext;
1400
1401typedef void (*GdbCmdHandler)(GdbCmdContext *gdb_ctx, void *user_ctx);
1402
1403/*
1404 * cmd_startswith -> cmd is compared using startswith
1405 *
1406 *
1407 * schema definitions:
1408 * Each schema parameter entry consists of 2 chars,
1409 * the first char represents the parameter type handling
1410 * the second char represents the delimiter for the next parameter
1411 *
1412 * Currently supported schema types:
1413 * 'l' -> unsigned long (stored in .val_ul)
1414 * 'L' -> unsigned long long (stored in .val_ull)
1415 * 's' -> string (stored in .data)
1416 * 'o' -> single char (stored in .opcode)
1417 * 't' -> thread id (stored in .thread_id)
1418 * '?' -> skip according to delimiter
1419 *
1420 * Currently supported delimiters:
1421 * '?' -> Stop at any delimiter (",;:=\0")
1422 * '0' -> Stop at "\0"
1423 * '.' -> Skip 1 char unless reached "\0"
1424 * Any other value is treated as the delimiter value itself
1425 */
1426typedef struct GdbCmdParseEntry {
1427 GdbCmdHandler handler;
1428 const char *cmd;
1429 bool cmd_startswith;
1430 const char *schema;
1431} GdbCmdParseEntry;
1432
1433static inline int startswith(const char *string, const char *pattern)
1434{
1435 return !strncmp(string, pattern, strlen(pattern));
1436}
1437
Jon Dorond14055d2019-05-29 09:41:29 +03001438static int process_string_cmd(GDBState *s, void *user_ctx, const char *data,
1439 const GdbCmdParseEntry *cmds, int num_cmds)
1440{
1441 int i, schema_len, max_num_params = 0;
1442 GdbCmdContext gdb_ctx;
1443
1444 if (!cmds) {
1445 return -1;
1446 }
1447
1448 for (i = 0; i < num_cmds; i++) {
1449 const GdbCmdParseEntry *cmd = &cmds[i];
1450 g_assert(cmd->handler && cmd->cmd);
1451
1452 if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
1453 (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
1454 continue;
1455 }
1456
1457 if (cmd->schema) {
1458 schema_len = strlen(cmd->schema);
1459 if (schema_len % 2) {
1460 return -2;
1461 }
1462
1463 max_num_params = schema_len / 2;
1464 }
1465
1466 gdb_ctx.params =
1467 (GdbCmdVariant *)alloca(sizeof(*gdb_ctx.params) * max_num_params);
1468 memset(gdb_ctx.params, 0, sizeof(*gdb_ctx.params) * max_num_params);
1469
1470 if (cmd_parse_params(&data[strlen(cmd->cmd)], cmd->schema,
1471 gdb_ctx.params, &gdb_ctx.num_params)) {
1472 return -1;
1473 }
1474
1475 gdb_ctx.s = s;
1476 cmd->handler(&gdb_ctx, user_ctx);
1477 return 0;
1478 }
1479
1480 return -1;
1481}
1482
Jon Doron3e2c1262019-05-29 09:41:30 +03001483static void run_cmd_parser(GDBState *s, const char *data,
1484 const GdbCmdParseEntry *cmd)
1485{
1486 if (!data) {
1487 return;
1488 }
1489
1490 /* In case there was an error during the command parsing we must
1491 * send a NULL packet to indicate the command is not supported */
1492 if (process_string_cmd(s, NULL, data, cmd, 1)) {
1493 put_packet(s, "");
1494 }
1495}
1496
1497static void handle_detach(GdbCmdContext *gdb_ctx, void *user_ctx)
1498{
1499 GDBProcess *process;
1500 GDBState *s = gdb_ctx->s;
1501 uint32_t pid = 1;
1502
1503 if (s->multiprocess) {
1504 if (!gdb_ctx->num_params) {
1505 put_packet(s, "E22");
1506 return;
1507 }
1508
1509 pid = gdb_ctx->params[0].val_ul;
1510 }
1511
1512 process = gdb_get_process(s, pid);
1513 gdb_process_breakpoint_remove_all(s, process);
1514 process->attached = false;
1515
1516 if (pid == gdb_get_cpu_pid(s, s->c_cpu)) {
1517 s->c_cpu = gdb_first_attached_cpu(s);
1518 }
1519
1520 if (pid == gdb_get_cpu_pid(s, s->g_cpu)) {
1521 s->g_cpu = gdb_first_attached_cpu(s);
1522 }
1523
1524 if (!s->c_cpu) {
1525 /* No more process attached */
1526 gdb_syscall_mode = GDB_SYS_DISABLED;
1527 gdb_continue(s);
1528 }
1529 put_packet(s, "OK");
1530}
1531
Jon Doron44ffded2019-05-29 09:41:31 +03001532static void handle_thread_alive(GdbCmdContext *gdb_ctx, void *user_ctx)
1533{
1534 CPUState *cpu;
1535
1536 if (!gdb_ctx->num_params) {
1537 put_packet(gdb_ctx->s, "E22");
1538 return;
1539 }
1540
1541 if (gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
1542 put_packet(gdb_ctx->s, "E22");
1543 return;
1544 }
1545
1546 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[0].thread_id.pid,
1547 gdb_ctx->params[0].thread_id.tid);
1548 if (!cpu) {
1549 put_packet(gdb_ctx->s, "E22");
1550 return;
1551 }
1552
1553 put_packet(gdb_ctx->s, "OK");
1554}
1555
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001556static void handle_continue(GdbCmdContext *gdb_ctx, void *user_ctx)
1557{
1558 if (gdb_ctx->num_params) {
1559 gdb_set_cpu_pc(gdb_ctx->s, gdb_ctx->params[0].val_ull);
1560 }
1561
1562 gdb_ctx->s->signal = 0;
1563 gdb_continue(gdb_ctx->s);
1564}
1565
Jon Doronccc47d52019-05-29 09:41:33 +03001566static void handle_cont_with_sig(GdbCmdContext *gdb_ctx, void *user_ctx)
1567{
1568 unsigned long signal = 0;
1569
1570 /*
1571 * Note: C sig;[addr] is currently unsupported and we simply
1572 * omit the addr parameter
1573 */
1574 if (gdb_ctx->num_params) {
1575 signal = gdb_ctx->params[0].val_ul;
1576 }
1577
1578 gdb_ctx->s->signal = gdb_signal_to_target(signal);
1579 if (gdb_ctx->s->signal == -1) {
1580 gdb_ctx->s->signal = 0;
1581 }
1582 gdb_continue(gdb_ctx->s);
1583}
1584
Jon Doron3a9651d2019-05-29 09:41:34 +03001585static void handle_set_thread(GdbCmdContext *gdb_ctx, void *user_ctx)
1586{
1587 CPUState *cpu;
1588
1589 if (gdb_ctx->num_params != 2) {
1590 put_packet(gdb_ctx->s, "E22");
1591 return;
1592 }
1593
1594 if (gdb_ctx->params[1].thread_id.kind == GDB_READ_THREAD_ERR) {
1595 put_packet(gdb_ctx->s, "E22");
1596 return;
1597 }
1598
1599 if (gdb_ctx->params[1].thread_id.kind != GDB_ONE_THREAD) {
1600 put_packet(gdb_ctx->s, "OK");
1601 return;
1602 }
1603
1604 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[1].thread_id.pid,
1605 gdb_ctx->params[1].thread_id.tid);
1606 if (!cpu) {
1607 put_packet(gdb_ctx->s, "E22");
1608 return;
1609 }
1610
1611 /*
1612 * Note: This command is deprecated and modern gdb's will be using the
1613 * vCont command instead.
1614 */
1615 switch (gdb_ctx->params[0].opcode) {
1616 case 'c':
1617 gdb_ctx->s->c_cpu = cpu;
1618 put_packet(gdb_ctx->s, "OK");
1619 break;
1620 case 'g':
1621 gdb_ctx->s->g_cpu = cpu;
1622 put_packet(gdb_ctx->s, "OK");
1623 break;
1624 default:
1625 put_packet(gdb_ctx->s, "E22");
1626 break;
1627 }
1628}
1629
Jon Doron77f6ce52019-05-29 09:41:35 +03001630static void handle_insert_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1631{
1632 int res;
1633
1634 if (gdb_ctx->num_params != 3) {
1635 put_packet(gdb_ctx->s, "E22");
1636 return;
1637 }
1638
1639 res = gdb_breakpoint_insert(gdb_ctx->params[0].val_ul,
1640 gdb_ctx->params[1].val_ull,
1641 gdb_ctx->params[2].val_ull);
1642 if (res >= 0) {
1643 put_packet(gdb_ctx->s, "OK");
1644 return;
1645 } else if (res == -ENOSYS) {
1646 put_packet(gdb_ctx->s, "");
1647 return;
1648 }
1649
1650 put_packet(gdb_ctx->s, "E22");
1651}
1652
1653static void handle_remove_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1654{
1655 int res;
1656
1657 if (gdb_ctx->num_params != 3) {
1658 put_packet(gdb_ctx->s, "E22");
1659 return;
1660 }
1661
1662 res = gdb_breakpoint_remove(gdb_ctx->params[0].val_ul,
1663 gdb_ctx->params[1].val_ull,
1664 gdb_ctx->params[2].val_ull);
1665 if (res >= 0) {
1666 put_packet(gdb_ctx->s, "OK");
1667 return;
1668 } else if (res == -ENOSYS) {
1669 put_packet(gdb_ctx->s, "");
1670 return;
1671 }
1672
1673 put_packet(gdb_ctx->s, "E22");
1674}
1675
Jon Doron62b33202019-05-29 09:41:36 +03001676static void handle_set_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1677{
1678 int reg_size;
1679
1680 if (!gdb_has_xml) {
1681 put_packet(gdb_ctx->s, "E00");
1682 return;
1683 }
1684
1685 if (gdb_ctx->num_params != 2) {
1686 put_packet(gdb_ctx->s, "E22");
1687 return;
1688 }
1689
1690 reg_size = strlen(gdb_ctx->params[1].data) / 2;
1691 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[1].data, reg_size);
1692 gdb_write_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf,
1693 gdb_ctx->params[0].val_ull);
1694 put_packet(gdb_ctx->s, "OK");
1695}
1696
Jon Doron5d0e57b2019-05-29 09:41:37 +03001697static void handle_get_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1698{
1699 int reg_size;
1700
1701 /*
1702 * Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1703 * This works, but can be very slow. Anything new enough to
1704 * understand XML also knows how to use this properly.
1705 */
1706 if (!gdb_has_xml) {
1707 put_packet(gdb_ctx->s, "");
1708 return;
1709 }
1710
1711 if (!gdb_ctx->num_params) {
1712 put_packet(gdb_ctx->s, "E14");
1713 return;
1714 }
1715
1716 reg_size = gdb_read_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf,
1717 gdb_ctx->params[0].val_ull);
1718 if (!reg_size) {
1719 put_packet(gdb_ctx->s, "E14");
1720 return;
1721 }
1722
1723 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, reg_size);
1724 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1725}
1726
Jon Doroncc0ecc72019-05-29 09:41:38 +03001727static void handle_write_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1728{
1729 if (gdb_ctx->num_params != 3) {
1730 put_packet(gdb_ctx->s, "E22");
1731 return;
1732 }
1733
1734 /* hextomem() reads 2*len bytes */
1735 if (gdb_ctx->params[1].val_ull > strlen(gdb_ctx->params[2].data) / 2) {
1736 put_packet(gdb_ctx->s, "E22");
1737 return;
1738 }
1739
1740 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[2].data,
1741 gdb_ctx->params[1].val_ull);
1742 if (target_memory_rw_debug(gdb_ctx->s->g_cpu, gdb_ctx->params[0].val_ull,
1743 gdb_ctx->mem_buf,
1744 gdb_ctx->params[1].val_ull, true)) {
1745 put_packet(gdb_ctx->s, "E14");
1746 return;
1747 }
1748
1749 put_packet(gdb_ctx->s, "OK");
1750}
1751
Jon Doronda92e232019-05-29 09:41:39 +03001752static void handle_read_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1753{
1754 if (gdb_ctx->num_params != 2) {
1755 put_packet(gdb_ctx->s, "E22");
1756 return;
1757 }
1758
1759 /* memtohex() doubles the required space */
1760 if (gdb_ctx->params[1].val_ull > MAX_PACKET_LENGTH / 2) {
1761 put_packet(gdb_ctx->s, "E22");
1762 return;
1763 }
1764
1765 if (target_memory_rw_debug(gdb_ctx->s->g_cpu, gdb_ctx->params[0].val_ull,
1766 gdb_ctx->mem_buf,
1767 gdb_ctx->params[1].val_ull, false)) {
1768 put_packet(gdb_ctx->s, "E14");
1769 return;
1770 }
1771
1772 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, gdb_ctx->params[1].val_ull);
1773 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1774}
1775
Jon Doron287ca122019-05-29 09:41:40 +03001776static void handle_write_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1777{
1778 target_ulong addr, len;
1779 uint8_t *registers;
1780 int reg_size;
1781
1782 if (!gdb_ctx->num_params) {
1783 return;
1784 }
1785
1786 cpu_synchronize_state(gdb_ctx->s->g_cpu);
1787 registers = gdb_ctx->mem_buf;
1788 len = strlen(gdb_ctx->params[0].data) / 2;
1789 hextomem(registers, gdb_ctx->params[0].data, len);
1790 for (addr = 0; addr < gdb_ctx->s->g_cpu->gdb_num_g_regs && len > 0;
1791 addr++) {
1792 reg_size = gdb_write_register(gdb_ctx->s->g_cpu, registers, addr);
1793 len -= reg_size;
1794 registers += reg_size;
1795 }
1796 put_packet(gdb_ctx->s, "OK");
1797}
1798
Jon Doron397d1372019-05-29 09:41:41 +03001799static void handle_read_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1800{
1801 target_ulong addr, len;
1802
1803 cpu_synchronize_state(gdb_ctx->s->g_cpu);
1804 len = 0;
1805 for (addr = 0; addr < gdb_ctx->s->g_cpu->gdb_num_g_regs; addr++) {
1806 len += gdb_read_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf + len,
1807 addr);
1808 }
1809
1810 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, len);
1811 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1812}
1813
Jon Doron4b20fab2019-05-29 09:41:42 +03001814static void handle_file_io(GdbCmdContext *gdb_ctx, void *user_ctx)
1815{
1816 if (gdb_ctx->num_params >= 2 && gdb_ctx->s->current_syscall_cb) {
1817 target_ulong ret, err;
1818
1819 ret = (target_ulong)gdb_ctx->params[0].val_ull;
1820 err = (target_ulong)gdb_ctx->params[1].val_ull;
1821 gdb_ctx->s->current_syscall_cb(gdb_ctx->s->c_cpu, ret, err);
1822 gdb_ctx->s->current_syscall_cb = NULL;
1823 }
1824
1825 if (gdb_ctx->num_params >= 3 && gdb_ctx->params[2].opcode == (uint8_t)'C') {
1826 put_packet(gdb_ctx->s, "T02");
1827 return;
1828 }
1829
1830 gdb_continue(gdb_ctx->s);
1831}
1832
Jon Doron933f80d2019-05-29 09:41:43 +03001833static void handle_step(GdbCmdContext *gdb_ctx, void *user_ctx)
1834{
1835 if (gdb_ctx->num_params) {
1836 gdb_set_cpu_pc(gdb_ctx->s, (target_ulong)gdb_ctx->params[0].val_ull);
1837 }
1838
1839 cpu_single_step(gdb_ctx->s->c_cpu, sstep_flags);
1840 gdb_continue(gdb_ctx->s);
1841}
1842
Jon Doron8536ec02019-05-29 09:41:44 +03001843static void handle_v_cont_query(GdbCmdContext *gdb_ctx, void *user_ctx)
1844{
1845 put_packet(gdb_ctx->s, "vCont;c;C;s;S");
1846}
1847
1848static void handle_v_cont(GdbCmdContext *gdb_ctx, void *user_ctx)
1849{
1850 int res;
1851
1852 if (!gdb_ctx->num_params) {
1853 return;
1854 }
1855
1856 res = gdb_handle_vcont(gdb_ctx->s, gdb_ctx->params[0].data);
1857 if ((res == -EINVAL) || (res == -ERANGE)) {
1858 put_packet(gdb_ctx->s, "E22");
1859 } else if (res) {
1860 put_packet(gdb_ctx->s, "");
1861 }
1862}
1863
1864static void handle_v_attach(GdbCmdContext *gdb_ctx, void *user_ctx)
1865{
1866 GDBProcess *process;
1867 CPUState *cpu;
1868 char thread_id[16];
1869
1870 pstrcpy(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "E22");
1871 if (!gdb_ctx->num_params) {
1872 goto cleanup;
1873 }
1874
1875 process = gdb_get_process(gdb_ctx->s, gdb_ctx->params[0].val_ul);
1876 if (!process) {
1877 goto cleanup;
1878 }
1879
1880 cpu = get_first_cpu_in_process(gdb_ctx->s, process);
1881 if (!cpu) {
1882 goto cleanup;
1883 }
1884
1885 process->attached = true;
1886 gdb_ctx->s->g_cpu = cpu;
1887 gdb_ctx->s->c_cpu = cpu;
1888
1889 gdb_fmt_thread_id(gdb_ctx->s, cpu, thread_id, sizeof(thread_id));
1890 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "T%02xthread:%s;",
1891 GDB_SIGNAL_TRAP, thread_id);
1892cleanup:
1893 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1894}
1895
1896static void handle_v_kill(GdbCmdContext *gdb_ctx, void *user_ctx)
1897{
1898 /* Kill the target */
1899 put_packet(gdb_ctx->s, "OK");
1900 error_report("QEMU: Terminated via GDBstub");
1901 exit(0);
1902}
1903
1904static GdbCmdParseEntry gdb_v_commands_table[] = {
1905 /* Order is important if has same prefix */
1906 {
1907 .handler = handle_v_cont_query,
1908 .cmd = "Cont?",
1909 .cmd_startswith = 1
1910 },
1911 {
1912 .handler = handle_v_cont,
1913 .cmd = "Cont",
1914 .cmd_startswith = 1,
1915 .schema = "s0"
1916 },
1917 {
1918 .handler = handle_v_attach,
1919 .cmd = "Attach;",
1920 .cmd_startswith = 1,
1921 .schema = "l0"
1922 },
1923 {
1924 .handler = handle_v_kill,
1925 .cmd = "Kill;",
1926 .cmd_startswith = 1
1927 },
1928};
1929
1930static void handle_v_commands(GdbCmdContext *gdb_ctx, void *user_ctx)
1931{
1932 if (!gdb_ctx->num_params) {
1933 return;
1934 }
1935
1936 if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
1937 gdb_v_commands_table,
1938 ARRAY_SIZE(gdb_v_commands_table))) {
1939 put_packet(gdb_ctx->s, "");
1940 }
1941}
1942
Jon Doron2704efa2019-05-29 09:41:45 +03001943static void handle_query_qemu_sstepbits(GdbCmdContext *gdb_ctx, void *user_ctx)
1944{
1945 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
1946 "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", SSTEP_ENABLE,
1947 SSTEP_NOIRQ, SSTEP_NOTIMER);
1948 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1949}
1950
1951static void handle_set_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
1952{
1953 if (!gdb_ctx->num_params) {
1954 return;
1955 }
1956
1957 sstep_flags = gdb_ctx->params[0].val_ul;
1958 put_packet(gdb_ctx->s, "OK");
1959}
1960
1961static void handle_query_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
1962{
1963 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "0x%x", sstep_flags);
1964 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1965}
1966
1967static void handle_query_curr_tid(GdbCmdContext *gdb_ctx, void *user_ctx)
bellardb4608c02003-06-27 17:34:32 +00001968{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001969 CPUState *cpu;
Luc Michelc145eea2019-01-07 15:23:46 +00001970 GDBProcess *process;
Jon Doron2704efa2019-05-29 09:41:45 +03001971 char thread_id[16];
1972
1973 /*
1974 * "Current thread" remains vague in the spec, so always return
1975 * the first thread of the current process (gdb returns the
1976 * first thread).
1977 */
1978 process = gdb_get_cpu_process(gdb_ctx->s, gdb_ctx->s->g_cpu);
1979 cpu = get_first_cpu_in_process(gdb_ctx->s, process);
1980 gdb_fmt_thread_id(gdb_ctx->s, cpu, thread_id, sizeof(thread_id));
1981 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "QC%s", thread_id);
1982 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1983}
1984
1985static void handle_query_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
1986{
1987 char thread_id[16];
1988
1989 if (!gdb_ctx->s->query_cpu) {
1990 put_packet(gdb_ctx->s, "l");
1991 return;
1992 }
1993
1994 gdb_fmt_thread_id(gdb_ctx->s, gdb_ctx->s->query_cpu, thread_id,
1995 sizeof(thread_id));
1996 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "m%s", thread_id);
1997 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1998 gdb_ctx->s->query_cpu =
1999 gdb_next_attached_cpu(gdb_ctx->s, gdb_ctx->s->query_cpu);
2000}
2001
2002static void handle_query_first_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
2003{
2004 gdb_ctx->s->query_cpu = gdb_first_attached_cpu(gdb_ctx->s);
2005 handle_query_threads(gdb_ctx, user_ctx);
2006}
2007
2008static void handle_query_thread_extra(GdbCmdContext *gdb_ctx, void *user_ctx)
2009{
2010 CPUState *cpu;
2011 int len;
2012
2013 if (!gdb_ctx->num_params ||
2014 gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
2015 put_packet(gdb_ctx->s, "E22");
2016 return;
2017 }
2018
2019 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[0].thread_id.pid,
2020 gdb_ctx->params[0].thread_id.tid);
2021 if (!cpu) {
2022 return;
2023 }
2024
2025 cpu_synchronize_state(cpu);
2026
2027 if (gdb_ctx->s->multiprocess && (gdb_ctx->s->process_num > 1)) {
2028 /* Print the CPU model and name in multiprocess mode */
2029 ObjectClass *oc = object_get_class(OBJECT(cpu));
2030 const char *cpu_model = object_class_get_name(oc);
2031 char *cpu_name = object_get_canonical_path_component(OBJECT(cpu));
2032 len = snprintf((char *)gdb_ctx->mem_buf, sizeof(gdb_ctx->str_buf) / 2,
2033 "%s %s [%s]", cpu_model, cpu_name,
2034 cpu->halted ? "halted " : "running");
2035 g_free(cpu_name);
2036 } else {
2037 /* memtohex() doubles the required space */
2038 len = snprintf((char *)gdb_ctx->mem_buf, sizeof(gdb_ctx->str_buf) / 2,
2039 "CPU#%d [%s]", cpu->cpu_index,
2040 cpu->halted ? "halted " : "running");
2041 }
2042 trace_gdbstub_op_extra_info((char *)gdb_ctx->mem_buf);
2043 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, len);
2044 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2045}
2046
2047#ifdef CONFIG_USER_ONLY
2048static void handle_query_offsets(GdbCmdContext *gdb_ctx, void *user_ctx)
2049{
2050 TaskState *ts;
2051
2052 ts = gdb_ctx->s->c_cpu->opaque;
2053 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
2054 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
2055 ";Bss=" TARGET_ABI_FMT_lx,
2056 ts->info->code_offset,
2057 ts->info->data_offset,
2058 ts->info->data_offset);
2059 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2060}
2061#else
2062static void handle_query_rcmd(GdbCmdContext *gdb_ctx, void *user_ctx)
2063{
2064 int len;
2065
2066 if (!gdb_ctx->num_params) {
2067 put_packet(gdb_ctx->s, "E22");
2068 return;
2069 }
2070
2071 len = strlen(gdb_ctx->params[0].data);
2072 if (len % 2) {
2073 put_packet(gdb_ctx->s, "E01");
2074 return;
2075 }
2076
2077 len = len / 2;
2078 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[0].data, len);
2079 gdb_ctx->mem_buf[len++] = 0;
2080 qemu_chr_be_write(gdb_ctx->s->mon_chr, gdb_ctx->mem_buf, len);
2081 put_packet(gdb_ctx->s, "OK");
2082
2083}
2084#endif
2085
2086static void handle_query_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2087{
Andreas Färber5b24c642013-07-07 15:08:22 +02002088 CPUClass *cc;
Jon Doron2704efa2019-05-29 09:41:45 +03002089
2090 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "PacketSize=%x",
2091 MAX_PACKET_LENGTH);
2092 cc = CPU_GET_CLASS(first_cpu);
2093 if (cc->gdb_core_xml_file) {
2094 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
2095 ";qXfer:features:read+");
2096 }
2097
2098 if (gdb_ctx->num_params &&
2099 strstr(gdb_ctx->params[0].data, "multiprocess+")) {
2100 gdb_ctx->s->multiprocess = true;
2101 }
2102
2103 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), ";multiprocess+");
2104 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2105}
2106
2107static void handle_query_xfer_features(GdbCmdContext *gdb_ctx, void *user_ctx)
2108{
2109 GDBProcess *process;
2110 CPUClass *cc;
2111 unsigned long len, total_len, addr;
2112 const char *xml;
bellardb4608c02003-06-27 17:34:32 +00002113 const char *p;
Jon Doron2704efa2019-05-29 09:41:45 +03002114
2115 if (gdb_ctx->num_params < 3) {
2116 put_packet(gdb_ctx->s, "E22");
2117 return;
2118 }
2119
2120 process = gdb_get_cpu_process(gdb_ctx->s, gdb_ctx->s->g_cpu);
2121 cc = CPU_GET_CLASS(gdb_ctx->s->g_cpu);
2122 if (!cc->gdb_core_xml_file) {
2123 put_packet(gdb_ctx->s, "");
2124 return;
2125 }
2126
2127 gdb_has_xml = true;
2128 p = gdb_ctx->params[0].data;
2129 xml = get_feature_xml(gdb_ctx->s, p, &p, process);
2130 if (!xml) {
2131 put_packet(gdb_ctx->s, "E00");
2132 return;
2133 }
2134
2135 addr = gdb_ctx->params[1].val_ul;
2136 len = gdb_ctx->params[2].val_ul;
2137 total_len = strlen(xml);
2138 if (addr > total_len) {
2139 put_packet(gdb_ctx->s, "E00");
2140 return;
2141 }
2142
2143 if (len > (MAX_PACKET_LENGTH - 5) / 2) {
2144 len = (MAX_PACKET_LENGTH - 5) / 2;
2145 }
2146
2147 if (len < total_len - addr) {
2148 gdb_ctx->str_buf[0] = 'm';
2149 len = memtox(gdb_ctx->str_buf + 1, xml + addr, len);
2150 } else {
2151 gdb_ctx->str_buf[0] = 'l';
2152 len = memtox(gdb_ctx->str_buf + 1, xml + addr, total_len - addr);
2153 }
2154
2155 put_packet_binary(gdb_ctx->s, gdb_ctx->str_buf, len + 1, true);
2156}
2157
2158static void handle_query_attached(GdbCmdContext *gdb_ctx, void *user_ctx)
2159{
2160 put_packet(gdb_ctx->s, GDB_ATTACHED);
2161}
2162
2163static void handle_query_qemu_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2164{
Jon Doronab4752e2019-05-29 09:41:48 +03002165 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "sstepbits;sstep");
2166#ifndef CONFIG_USER_ONLY
2167 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), ";PhyMemMode");
2168#endif
2169 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
Jon Doron2704efa2019-05-29 09:41:45 +03002170}
2171
Jon Doronab4752e2019-05-29 09:41:48 +03002172#ifndef CONFIG_USER_ONLY
2173static void handle_query_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx,
2174 void *user_ctx)
2175{
2176 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "%d", phy_memory_mode);
2177 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2178}
2179
2180static void handle_set_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx, void *user_ctx)
2181{
2182 if (!gdb_ctx->num_params) {
2183 put_packet(gdb_ctx->s, "E22");
2184 return;
2185 }
2186
2187 if (!gdb_ctx->params[0].val_ul) {
2188 phy_memory_mode = 0;
2189 } else {
2190 phy_memory_mode = 1;
2191 }
2192 put_packet(gdb_ctx->s, "OK");
2193}
2194#endif
2195
Jon Doron2704efa2019-05-29 09:41:45 +03002196static GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
2197 /* Order is important if has same prefix */
2198 {
2199 .handler = handle_query_qemu_sstepbits,
2200 .cmd = "qemu.sstepbits",
2201 },
2202 {
2203 .handler = handle_query_qemu_sstep,
2204 .cmd = "qemu.sstep",
2205 },
2206 {
2207 .handler = handle_set_qemu_sstep,
2208 .cmd = "qemu.sstep=",
2209 .cmd_startswith = 1,
2210 .schema = "l0"
2211 },
2212};
2213
2214static GdbCmdParseEntry gdb_gen_query_table[] = {
2215 {
2216 .handler = handle_query_curr_tid,
2217 .cmd = "C",
2218 },
2219 {
2220 .handler = handle_query_threads,
2221 .cmd = "sThreadInfo",
2222 },
2223 {
2224 .handler = handle_query_first_threads,
2225 .cmd = "fThreadInfo",
2226 },
2227 {
2228 .handler = handle_query_thread_extra,
2229 .cmd = "ThreadExtraInfo,",
2230 .cmd_startswith = 1,
2231 .schema = "t0"
2232 },
2233#ifdef CONFIG_USER_ONLY
2234 {
2235 .handler = handle_query_offsets,
2236 .cmd = "Offsets",
2237 },
2238#else
2239 {
2240 .handler = handle_query_rcmd,
2241 .cmd = "Rcmd,",
2242 .cmd_startswith = 1,
2243 .schema = "s0"
2244 },
2245#endif
2246 {
2247 .handler = handle_query_supported,
2248 .cmd = "Supported:",
2249 .cmd_startswith = 1,
2250 .schema = "s0"
2251 },
2252 {
2253 .handler = handle_query_supported,
2254 .cmd = "Supported",
2255 .schema = "s0"
2256 },
2257 {
2258 .handler = handle_query_xfer_features,
2259 .cmd = "Xfer:features:read:",
2260 .cmd_startswith = 1,
2261 .schema = "s:l,l0"
2262 },
2263 {
2264 .handler = handle_query_attached,
2265 .cmd = "Attached:",
2266 .cmd_startswith = 1
2267 },
2268 {
2269 .handler = handle_query_attached,
2270 .cmd = "Attached",
2271 },
2272 {
2273 .handler = handle_query_qemu_supported,
2274 .cmd = "qemu.Supported",
2275 },
Jon Doronab4752e2019-05-29 09:41:48 +03002276#ifndef CONFIG_USER_ONLY
2277 {
2278 .handler = handle_query_qemu_phy_mem_mode,
2279 .cmd = "qemu.PhyMemMode",
2280 },
2281#endif
Jon Doron2704efa2019-05-29 09:41:45 +03002282};
2283
2284static GdbCmdParseEntry gdb_gen_set_table[] = {
2285 /* Order is important if has same prefix */
2286 {
2287 .handler = handle_set_qemu_sstep,
2288 .cmd = "qemu.sstep:",
2289 .cmd_startswith = 1,
2290 .schema = "l0"
2291 },
Jon Doronab4752e2019-05-29 09:41:48 +03002292#ifndef CONFIG_USER_ONLY
2293 {
2294 .handler = handle_set_qemu_phy_mem_mode,
2295 .cmd = "qemu.PhyMemMode:",
2296 .cmd_startswith = 1,
2297 .schema = "l0"
2298 },
2299#endif
Jon Doron2704efa2019-05-29 09:41:45 +03002300};
2301
2302static void handle_gen_query(GdbCmdContext *gdb_ctx, void *user_ctx)
2303{
2304 if (!gdb_ctx->num_params) {
2305 return;
2306 }
2307
2308 if (!process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2309 gdb_gen_query_set_common_table,
2310 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2311 return;
2312 }
2313
2314 if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2315 gdb_gen_query_table,
2316 ARRAY_SIZE(gdb_gen_query_table))) {
2317 put_packet(gdb_ctx->s, "");
2318 }
2319}
2320
2321static void handle_gen_set(GdbCmdContext *gdb_ctx, void *user_ctx)
2322{
2323 if (!gdb_ctx->num_params) {
2324 return;
2325 }
2326
2327 if (!process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2328 gdb_gen_query_set_common_table,
2329 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2330 return;
2331 }
2332
2333 if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2334 gdb_gen_set_table,
2335 ARRAY_SIZE(gdb_gen_set_table))) {
2336 put_packet(gdb_ctx->s, "");
2337 }
2338}
2339
Jon Doron7009d572019-05-29 09:41:46 +03002340static void handle_target_halt(GdbCmdContext *gdb_ctx, void *user_ctx)
2341{
2342 char thread_id[16];
2343
2344 gdb_fmt_thread_id(gdb_ctx->s, gdb_ctx->s->c_cpu, thread_id,
2345 sizeof(thread_id));
2346 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "T%02xthread:%s;",
2347 GDB_SIGNAL_TRAP, thread_id);
2348 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2349 /*
2350 * Remove all the breakpoints when this query is issued,
2351 * because gdb is doing an initial connect and the state
2352 * should be cleaned up.
2353 */
2354 gdb_breakpoint_remove_all();
2355}
2356
Jon Doron2704efa2019-05-29 09:41:45 +03002357static int gdb_handle_packet(GDBState *s, const char *line_buf)
2358{
Jon Doron3e2c1262019-05-29 09:41:30 +03002359 const GdbCmdParseEntry *cmd_parser = NULL;
ths3b46e622007-09-17 08:09:54 +00002360
Doug Gale5c9522b2017-12-02 20:30:37 -05002361 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01002362
Jon Doron3f1cbac2019-05-29 09:41:47 +03002363 switch (line_buf[0]) {
Luc Michel53fd6552019-01-07 15:23:46 +00002364 case '!':
2365 put_packet(s, "OK");
2366 break;
bellard858693c2004-03-31 18:52:07 +00002367 case '?':
Jon Doron7009d572019-05-29 09:41:46 +03002368 {
2369 static const GdbCmdParseEntry target_halted_cmd_desc = {
2370 .handler = handle_target_halt,
2371 .cmd = "?",
2372 .cmd_startswith = 1
2373 };
2374 cmd_parser = &target_halted_cmd_desc;
2375 }
bellard858693c2004-03-31 18:52:07 +00002376 break;
2377 case 'c':
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002378 {
2379 static const GdbCmdParseEntry continue_cmd_desc = {
2380 .handler = handle_continue,
2381 .cmd = "c",
2382 .cmd_startswith = 1,
2383 .schema = "L0"
2384 };
2385 cmd_parser = &continue_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002386 }
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002387 break;
edgar_igl1f487ee2008-05-17 22:20:53 +00002388 case 'C':
Jon Doronccc47d52019-05-29 09:41:33 +03002389 {
2390 static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
2391 .handler = handle_cont_with_sig,
2392 .cmd = "C",
2393 .cmd_startswith = 1,
2394 .schema = "l0"
2395 };
2396 cmd_parser = &cont_with_sig_cmd_desc;
2397 }
2398 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002399 case 'v':
Jon Doron8536ec02019-05-29 09:41:44 +03002400 {
2401 static const GdbCmdParseEntry v_cmd_desc = {
2402 .handler = handle_v_commands,
2403 .cmd = "v",
2404 .cmd_startswith = 1,
2405 .schema = "s0"
2406 };
2407 cmd_parser = &v_cmd_desc;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002408 }
Jon Doron8536ec02019-05-29 09:41:44 +03002409 break;
edgar_igl7d03f822008-05-17 18:58:29 +00002410 case 'k':
2411 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002412 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00002413 exit(0);
2414 case 'D':
Jon Doron3e2c1262019-05-29 09:41:30 +03002415 {
2416 static const GdbCmdParseEntry detach_cmd_desc = {
2417 .handler = handle_detach,
2418 .cmd = "D",
2419 .cmd_startswith = 1,
2420 .schema = "?.l0"
2421 };
2422 cmd_parser = &detach_cmd_desc;
Luc Michel546f3c62019-01-07 15:23:46 +00002423 }
edgar_igl7d03f822008-05-17 18:58:29 +00002424 break;
bellard858693c2004-03-31 18:52:07 +00002425 case 's':
Jon Doron933f80d2019-05-29 09:41:43 +03002426 {
2427 static const GdbCmdParseEntry step_cmd_desc = {
2428 .handler = handle_step,
2429 .cmd = "s",
2430 .cmd_startswith = 1,
2431 .schema = "L0"
2432 };
2433 cmd_parser = &step_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002434 }
Jon Doron933f80d2019-05-29 09:41:43 +03002435 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002436 case 'F':
2437 {
Jon Doron4b20fab2019-05-29 09:41:42 +03002438 static const GdbCmdParseEntry file_io_cmd_desc = {
2439 .handler = handle_file_io,
2440 .cmd = "F",
2441 .cmd_startswith = 1,
2442 .schema = "L,L,o0"
2443 };
2444 cmd_parser = &file_io_cmd_desc;
pbrooka2d1eba2007-01-28 03:10:55 +00002445 }
2446 break;
bellard858693c2004-03-31 18:52:07 +00002447 case 'g':
Jon Doron397d1372019-05-29 09:41:41 +03002448 {
2449 static const GdbCmdParseEntry read_all_regs_cmd_desc = {
2450 .handler = handle_read_all_regs,
2451 .cmd = "g",
2452 .cmd_startswith = 1
2453 };
2454 cmd_parser = &read_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002455 }
bellard858693c2004-03-31 18:52:07 +00002456 break;
2457 case 'G':
Jon Doron287ca122019-05-29 09:41:40 +03002458 {
2459 static const GdbCmdParseEntry write_all_regs_cmd_desc = {
2460 .handler = handle_write_all_regs,
2461 .cmd = "G",
2462 .cmd_startswith = 1,
2463 .schema = "s0"
2464 };
2465 cmd_parser = &write_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002466 }
bellard858693c2004-03-31 18:52:07 +00002467 break;
2468 case 'm':
Jon Doronda92e232019-05-29 09:41:39 +03002469 {
2470 static const GdbCmdParseEntry read_mem_cmd_desc = {
2471 .handler = handle_read_mem,
2472 .cmd = "m",
2473 .cmd_startswith = 1,
2474 .schema = "L,L0"
2475 };
2476 cmd_parser = &read_mem_cmd_desc;
bellard6f970bd2005-12-05 19:55:19 +00002477 }
bellard858693c2004-03-31 18:52:07 +00002478 break;
2479 case 'M':
Jon Doroncc0ecc72019-05-29 09:41:38 +03002480 {
2481 static const GdbCmdParseEntry write_mem_cmd_desc = {
2482 .handler = handle_write_mem,
2483 .cmd = "M",
2484 .cmd_startswith = 1,
2485 .schema = "L,L:s0"
2486 };
2487 cmd_parser = &write_mem_cmd_desc;
Fabien Chouteau44520db2011-09-08 12:48:16 +02002488 }
bellard858693c2004-03-31 18:52:07 +00002489 break;
pbrook56aebc82008-10-11 17:55:29 +00002490 case 'p':
Jon Doron5d0e57b2019-05-29 09:41:37 +03002491 {
2492 static const GdbCmdParseEntry get_reg_cmd_desc = {
2493 .handler = handle_get_reg,
2494 .cmd = "p",
2495 .cmd_startswith = 1,
2496 .schema = "L0"
2497 };
2498 cmd_parser = &get_reg_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002499 }
2500 break;
2501 case 'P':
Jon Doron62b33202019-05-29 09:41:36 +03002502 {
2503 static const GdbCmdParseEntry set_reg_cmd_desc = {
2504 .handler = handle_set_reg,
2505 .cmd = "P",
2506 .cmd_startswith = 1,
2507 .schema = "L?s0"
2508 };
2509 cmd_parser = &set_reg_cmd_desc;
2510 }
pbrook56aebc82008-10-11 17:55:29 +00002511 break;
bellard858693c2004-03-31 18:52:07 +00002512 case 'Z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002513 {
2514 static const GdbCmdParseEntry insert_bp_cmd_desc = {
2515 .handler = handle_insert_bp,
2516 .cmd = "Z",
2517 .cmd_startswith = 1,
2518 .schema = "l?L?L0"
2519 };
2520 cmd_parser = &insert_bp_cmd_desc;
2521 }
2522 break;
bellard858693c2004-03-31 18:52:07 +00002523 case 'z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002524 {
2525 static const GdbCmdParseEntry remove_bp_cmd_desc = {
2526 .handler = handle_remove_bp,
2527 .cmd = "z",
2528 .cmd_startswith = 1,
2529 .schema = "l?L?L0"
2530 };
2531 cmd_parser = &remove_bp_cmd_desc;
2532 }
bellard858693c2004-03-31 18:52:07 +00002533 break;
aliguori880a7572008-11-18 20:30:24 +00002534 case 'H':
Jon Doron3a9651d2019-05-29 09:41:34 +03002535 {
2536 static const GdbCmdParseEntry set_thread_cmd_desc = {
2537 .handler = handle_set_thread,
2538 .cmd = "H",
2539 .cmd_startswith = 1,
2540 .schema = "o.t0"
2541 };
2542 cmd_parser = &set_thread_cmd_desc;
aliguori880a7572008-11-18 20:30:24 +00002543 }
2544 break;
2545 case 'T':
Jon Doron44ffded2019-05-29 09:41:31 +03002546 {
2547 static const GdbCmdParseEntry thread_alive_cmd_desc = {
2548 .handler = handle_thread_alive,
2549 .cmd = "T",
2550 .cmd_startswith = 1,
2551 .schema = "t0"
2552 };
2553 cmd_parser = &thread_alive_cmd_desc;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07002554 }
aliguori880a7572008-11-18 20:30:24 +00002555 break;
pbrook978efd62006-06-17 18:30:42 +00002556 case 'q':
Jon Doron2704efa2019-05-29 09:41:45 +03002557 {
2558 static const GdbCmdParseEntry gen_query_cmd_desc = {
2559 .handler = handle_gen_query,
2560 .cmd = "q",
2561 .cmd_startswith = 1,
2562 .schema = "s0"
2563 };
2564 cmd_parser = &gen_query_cmd_desc;
2565 }
2566 break;
edgar_igl60897d32008-05-09 08:25:14 +00002567 case 'Q':
Jon Doron2704efa2019-05-29 09:41:45 +03002568 {
2569 static const GdbCmdParseEntry gen_set_cmd_desc = {
2570 .handler = handle_gen_set,
2571 .cmd = "Q",
2572 .cmd_startswith = 1,
2573 .schema = "s0"
2574 };
2575 cmd_parser = &gen_set_cmd_desc;
edgar_igl60897d32008-05-09 08:25:14 +00002576 }
Jon Doron2704efa2019-05-29 09:41:45 +03002577 break;
bellard858693c2004-03-31 18:52:07 +00002578 default:
bellard858693c2004-03-31 18:52:07 +00002579 /* put empty packet */
Jon Doron3f1cbac2019-05-29 09:41:47 +03002580 put_packet(s, "");
bellard858693c2004-03-31 18:52:07 +00002581 break;
2582 }
Jon Doron3e2c1262019-05-29 09:41:30 +03002583
2584 run_cmd_parser(s, line_buf, cmd_parser);
2585
bellard858693c2004-03-31 18:52:07 +00002586 return RS_IDLE;
2587}
2588
Andreas Färber64f6b342013-05-27 02:06:09 +02002589void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00002590{
Luc Michel160d8582019-01-07 15:23:46 +00002591 GDBProcess *p = gdb_get_cpu_process(gdbserver_state, cpu);
2592
2593 if (!p->attached) {
2594 /*
2595 * Having a stop CPU corresponding to a process that is not attached
2596 * confuses GDB. So we ignore the request.
2597 */
2598 return;
2599 }
2600
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002601 gdbserver_state->c_cpu = cpu;
2602 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00002603}
2604
bellard1fddef42005-04-17 19:16:13 +00002605#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002606static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00002607{
aliguori880a7572008-11-18 20:30:24 +00002608 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002609 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00002610 char buf[256];
Luc Michel95567c22019-01-07 15:23:46 +00002611 char thread_id[16];
aliguorid6fc1b32008-11-18 19:55:44 +00002612 const char *type;
bellard858693c2004-03-31 18:52:07 +00002613 int ret;
2614
Meador Ingecdb432b2012-03-15 17:49:45 +00002615 if (running || s->state == RS_INACTIVE) {
2616 return;
2617 }
2618 /* Is there a GDB syscall waiting to be sent? */
2619 if (s->current_syscall_cb) {
2620 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00002621 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01002622 }
Luc Michel95567c22019-01-07 15:23:46 +00002623
2624 if (cpu == NULL) {
2625 /* No process attached */
2626 return;
2627 }
2628
2629 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id));
2630
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002631 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002632 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02002633 if (cpu->watchpoint_hit) {
2634 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00002635 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00002636 type = "r";
2637 break;
aliguoria1d1bb32008-11-18 20:07:32 +00002638 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00002639 type = "a";
2640 break;
2641 default:
2642 type = "";
2643 break;
2644 }
Doug Gale5c9522b2017-12-02 20:30:37 -05002645 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
2646 (target_ulong)cpu->watchpoint_hit->vaddr);
aliguori880a7572008-11-18 20:30:24 +00002647 snprintf(buf, sizeof(buf),
Luc Michel95567c22019-01-07 15:23:46 +00002648 "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
2649 GDB_SIGNAL_TRAP, thread_id, type,
Andreas Färberff4700b2013-08-26 18:23:18 +02002650 (target_ulong)cpu->watchpoint_hit->vaddr);
2651 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01002652 goto send_packet;
Doug Gale5c9522b2017-12-02 20:30:37 -05002653 } else {
2654 trace_gdbstub_hit_break();
pbrook6658ffb2007-03-16 23:58:11 +00002655 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07002656 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00002657 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01002658 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002659 case RUN_STATE_PAUSED:
Doug Gale5c9522b2017-12-02 20:30:37 -05002660 trace_gdbstub_hit_paused();
aliguori9781e042009-01-22 17:15:29 +00002661 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01002662 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002663 case RUN_STATE_SHUTDOWN:
Doug Gale5c9522b2017-12-02 20:30:37 -05002664 trace_gdbstub_hit_shutdown();
Jan Kiszka425189a2011-03-22 11:02:09 +01002665 ret = GDB_SIGNAL_QUIT;
2666 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002667 case RUN_STATE_IO_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002668 trace_gdbstub_hit_io_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002669 ret = GDB_SIGNAL_IO;
2670 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002671 case RUN_STATE_WATCHDOG:
Doug Gale5c9522b2017-12-02 20:30:37 -05002672 trace_gdbstub_hit_watchdog();
Jan Kiszka425189a2011-03-22 11:02:09 +01002673 ret = GDB_SIGNAL_ALRM;
2674 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002675 case RUN_STATE_INTERNAL_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002676 trace_gdbstub_hit_internal_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002677 ret = GDB_SIGNAL_ABRT;
2678 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002679 case RUN_STATE_SAVE_VM:
2680 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01002681 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002682 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01002683 ret = GDB_SIGNAL_XCPU;
2684 break;
2685 default:
Doug Gale5c9522b2017-12-02 20:30:37 -05002686 trace_gdbstub_hit_unknown(state);
Jan Kiszka425189a2011-03-22 11:02:09 +01002687 ret = GDB_SIGNAL_UNKNOWN;
2688 break;
bellardbbeb7b52006-04-23 18:42:15 +00002689 }
Jan Kiszka226d0072015-07-24 18:52:31 +02002690 gdb_set_stop_cpu(cpu);
Luc Michel95567c22019-01-07 15:23:46 +00002691 snprintf(buf, sizeof(buf), "T%02xthread:%s;", ret, thread_id);
Jan Kiszka425189a2011-03-22 11:02:09 +01002692
2693send_packet:
bellard858693c2004-03-31 18:52:07 +00002694 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01002695
2696 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02002697 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00002698}
bellard1fddef42005-04-17 19:16:13 +00002699#endif
bellard858693c2004-03-31 18:52:07 +00002700
pbrooka2d1eba2007-01-28 03:10:55 +00002701/* Send a gdb syscall request.
2702 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00002703 %x - target_ulong argument printed in hex.
2704 %lx - 64-bit argument printed in hex.
2705 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01002706void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00002707{
pbrooka2d1eba2007-01-28 03:10:55 +00002708 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00002709 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00002710 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00002711 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00002712 GDBState *s;
2713
aliguori880a7572008-11-18 20:30:24 +00002714 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00002715 if (!s)
2716 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00002717 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00002718#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002719 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00002720#endif
Meador Ingecdb432b2012-03-15 17:49:45 +00002721 p = s->syscall_buf;
2722 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00002723 *(p++) = 'F';
2724 while (*fmt) {
2725 if (*fmt == '%') {
2726 fmt++;
2727 switch (*fmt++) {
2728 case 'x':
2729 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002730 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00002731 break;
pbrooka87295e2007-05-26 15:09:38 +00002732 case 'l':
2733 if (*(fmt++) != 'x')
2734 goto bad_format;
2735 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00002736 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00002737 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002738 case 's':
2739 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002740 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00002741 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00002742 break;
2743 default:
pbrooka87295e2007-05-26 15:09:38 +00002744 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002745 error_report("gdbstub: Bad syscall format string '%s'",
2746 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00002747 break;
2748 }
2749 } else {
2750 *(p++) = *(fmt++);
2751 }
2752 }
pbrook8a93e022007-08-06 13:19:15 +00002753 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00002754#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00002755 put_packet(s, s->syscall_buf);
Peter Maydell4f710862018-05-15 19:19:58 +01002756 /* Return control to gdb for it to process the syscall request.
2757 * Since the protocol requires that gdb hands control back to us
2758 * using a "here are the results" F packet, we don't need to check
2759 * gdb_handlesig's return value (which is the signal to deliver if
2760 * execution was resumed via a continue packet).
2761 */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002762 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00002763#else
Meador Ingecdb432b2012-03-15 17:49:45 +00002764 /* In this case wait to send the syscall packet until notification that
2765 the CPU has stopped. This must be done because if the packet is sent
2766 now the reply from the syscall request could be received while the CPU
2767 is still in the running state, which can cause packets to be dropped
2768 and state transition 'T' packets to be sent while the syscall is still
2769 being processed. */
Paolo Bonzini9102ded2015-08-18 06:52:09 -07002770 qemu_cpu_kick(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00002771#endif
2772}
2773
Peter Maydell19239b32015-09-07 10:39:27 +01002774void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
2775{
2776 va_list va;
2777
2778 va_start(va, fmt);
2779 gdb_do_syscallv(cb, fmt, va);
2780 va_end(va);
2781}
2782
Markus Armbruster33c846e2019-05-14 20:03:09 +02002783static void gdb_read_byte(GDBState *s, uint8_t ch)
bellard858693c2004-03-31 18:52:07 +00002784{
ths60fe76f2007-12-16 03:02:09 +00002785 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00002786
bellard1fddef42005-04-17 19:16:13 +00002787#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00002788 if (s->last_packet_len) {
2789 /* Waiting for a response to the last packet. If we see the start
2790 of a new command then abandon the previous response. */
2791 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002792 trace_gdbstub_err_got_nack();
thsffe8ab82007-12-16 03:16:05 +00002793 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
Alex Bennée118e2262017-07-12 11:52:13 +01002794 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002795 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01002796 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002797 trace_gdbstub_io_got_unexpected(ch);
pbrook4046d912007-01-28 01:53:16 +00002798 }
Alex Bennée118e2262017-07-12 11:52:13 +01002799
pbrook4046d912007-01-28 01:53:16 +00002800 if (ch == '+' || ch == '$')
2801 s->last_packet_len = 0;
2802 if (ch != '$')
2803 return;
2804 }
Luiz Capitulino13548692011-07-29 15:36:43 -03002805 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00002806 /* when the CPU is running, we cannot do anything except stop
2807 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002808 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00002809 } else
bellard1fddef42005-04-17 19:16:13 +00002810#endif
bellard41625032005-04-24 10:07:11 +00002811 {
bellard858693c2004-03-31 18:52:07 +00002812 switch(s->state) {
2813 case RS_IDLE:
2814 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04002815 /* start of command packet */
bellard858693c2004-03-31 18:52:07 +00002816 s->line_buf_index = 0;
Doug Gale4bf43122017-05-01 12:22:10 -04002817 s->line_sum = 0;
bellard858693c2004-03-31 18:52:07 +00002818 s->state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002819 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002820 trace_gdbstub_err_garbage(ch);
bellard4c3a88a2003-07-26 12:06:08 +00002821 }
2822 break;
bellard858693c2004-03-31 18:52:07 +00002823 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04002824 if (ch == '}') {
2825 /* start escape sequence */
2826 s->state = RS_GETLINE_ESC;
2827 s->line_sum += ch;
2828 } else if (ch == '*') {
2829 /* start run length encoding sequence */
2830 s->state = RS_GETLINE_RLE;
2831 s->line_sum += ch;
2832 } else if (ch == '#') {
2833 /* end of command, start of checksum*/
2834 s->state = RS_CHKSUM1;
bellard858693c2004-03-31 18:52:07 +00002835 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002836 trace_gdbstub_err_overrun();
bellard858693c2004-03-31 18:52:07 +00002837 s->state = RS_IDLE;
2838 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002839 /* unescaped command character */
2840 s->line_buf[s->line_buf_index++] = ch;
2841 s->line_sum += ch;
2842 }
2843 break;
2844 case RS_GETLINE_ESC:
2845 if (ch == '#') {
2846 /* unexpected end of command in escape sequence */
2847 s->state = RS_CHKSUM1;
2848 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2849 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05002850 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002851 s->state = RS_IDLE;
2852 } else {
2853 /* parse escaped character and leave escape state */
2854 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
2855 s->line_sum += ch;
2856 s->state = RS_GETLINE;
2857 }
2858 break;
2859 case RS_GETLINE_RLE:
Markus Armbruster046aba12019-05-14 20:03:08 +02002860 /*
2861 * Run-length encoding is explained in "Debugging with GDB /
2862 * Appendix E GDB Remote Serial Protocol / Overview".
2863 */
2864 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
Doug Gale4bf43122017-05-01 12:22:10 -04002865 /* invalid RLE count encoding */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002866 trace_gdbstub_err_invalid_repeat(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002867 s->state = RS_GETLINE;
2868 } else {
2869 /* decode repeat length */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002870 int repeat = ch - ' ' + 3;
Doug Gale4bf43122017-05-01 12:22:10 -04002871 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
2872 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05002873 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002874 s->state = RS_IDLE;
2875 } else if (s->line_buf_index < 1) {
2876 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05002877 trace_gdbstub_err_invalid_rle();
Doug Gale4bf43122017-05-01 12:22:10 -04002878 s->state = RS_GETLINE;
2879 } else {
2880 /* repeat the last character */
2881 memset(s->line_buf + s->line_buf_index,
2882 s->line_buf[s->line_buf_index - 1], repeat);
2883 s->line_buf_index += repeat;
2884 s->line_sum += ch;
2885 s->state = RS_GETLINE;
2886 }
bellard858693c2004-03-31 18:52:07 +00002887 }
2888 break;
2889 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04002890 /* get high hex digit of checksum */
2891 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002892 trace_gdbstub_err_checksum_invalid(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002893 s->state = RS_GETLINE;
2894 break;
2895 }
bellard858693c2004-03-31 18:52:07 +00002896 s->line_buf[s->line_buf_index] = '\0';
2897 s->line_csum = fromhex(ch) << 4;
2898 s->state = RS_CHKSUM2;
2899 break;
2900 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04002901 /* get low hex digit of checksum */
2902 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002903 trace_gdbstub_err_checksum_invalid(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002904 s->state = RS_GETLINE;
2905 break;
bellard858693c2004-03-31 18:52:07 +00002906 }
Doug Gale4bf43122017-05-01 12:22:10 -04002907 s->line_csum |= fromhex(ch);
2908
2909 if (s->line_csum != (s->line_sum & 0xff)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002910 trace_gdbstub_err_checksum_incorrect(s->line_sum, s->line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04002911 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00002912 reply = '-';
2913 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00002914 s->state = RS_IDLE;
2915 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002916 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00002917 reply = '+';
2918 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00002919 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00002920 }
bellardb4608c02003-06-27 17:34:32 +00002921 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002922 default:
2923 abort();
bellardb4608c02003-06-27 17:34:32 +00002924 }
2925 }
bellard858693c2004-03-31 18:52:07 +00002926}
2927
Paul Brook0e1c9c52010-06-16 13:03:51 +01002928/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002929void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01002930{
2931 GDBState *s;
2932 char buf[4];
2933
2934 s = gdbserver_state;
2935 if (!s) {
2936 return;
2937 }
2938#ifdef CONFIG_USER_ONLY
2939 if (gdbserver_fd < 0 || s->fd < 0) {
2940 return;
2941 }
2942#endif
2943
Doug Gale5c9522b2017-12-02 20:30:37 -05002944 trace_gdbstub_op_exiting((uint8_t)code);
2945
Paul Brook0e1c9c52010-06-16 13:03:51 +01002946 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
2947 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002948
2949#ifndef CONFIG_USER_ONLY
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002950 qemu_chr_fe_deinit(&s->chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002951#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01002952}
2953
Luc Michel8f468632019-01-07 15:23:45 +00002954/*
2955 * Create the process that will contain all the "orphan" CPUs (that are not
2956 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2957 * be attachable and thus will be invisible to the user.
2958 */
2959static void create_default_process(GDBState *s)
2960{
2961 GDBProcess *process;
2962 int max_pid = 0;
2963
2964 if (s->process_num) {
2965 max_pid = s->processes[s->process_num - 1].pid;
2966 }
2967
2968 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2969 process = &s->processes[s->process_num - 1];
2970
2971 /* We need an available PID slot for this process */
2972 assert(max_pid < UINT32_MAX);
2973
2974 process->pid = max_pid + 1;
2975 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00002976 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00002977}
2978
bellard1fddef42005-04-17 19:16:13 +00002979#ifdef CONFIG_USER_ONLY
2980int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02002981gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00002982{
Andreas Färber5ca666c2013-06-24 19:20:57 +02002983 GDBState *s;
2984 char buf[256];
2985 int n;
bellard1fddef42005-04-17 19:16:13 +00002986
Andreas Färber5ca666c2013-06-24 19:20:57 +02002987 s = gdbserver_state;
2988 if (gdbserver_fd < 0 || s->fd < 0) {
2989 return sig;
bellard1fddef42005-04-17 19:16:13 +00002990 }
2991
Andreas Färber5ca666c2013-06-24 19:20:57 +02002992 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02002993 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07002994 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00002995
Andreas Färber5ca666c2013-06-24 19:20:57 +02002996 if (sig != 0) {
2997 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
2998 put_packet(s, buf);
2999 }
3000 /* put_packet() might have detected that the peer terminated the
3001 connection. */
3002 if (s->fd < 0) {
3003 return sig;
3004 }
3005
3006 sig = 0;
3007 s->state = RS_IDLE;
3008 s->running_state = 0;
3009 while (s->running_state == 0) {
3010 n = read(s->fd, buf, 256);
3011 if (n > 0) {
3012 int i;
3013
3014 for (i = 0; i < n; i++) {
3015 gdb_read_byte(s, buf[i]);
3016 }
Peter Wu5819e3e2016-06-05 16:35:48 +02003017 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003018 /* XXX: Connection closed. Should probably wait for another
3019 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02003020 if (n == 0) {
3021 close(s->fd);
3022 }
3023 s->fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02003024 return sig;
bellard1fddef42005-04-17 19:16:13 +00003025 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02003026 }
3027 sig = s->signal;
3028 s->signal = 0;
3029 return sig;
bellard1fddef42005-04-17 19:16:13 +00003030}
bellarde9009672005-04-26 20:42:36 +00003031
aurel32ca587a82008-12-18 22:44:13 +00003032/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01003033void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00003034{
Andreas Färber5ca666c2013-06-24 19:20:57 +02003035 GDBState *s;
3036 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00003037
Andreas Färber5ca666c2013-06-24 19:20:57 +02003038 s = gdbserver_state;
3039 if (gdbserver_fd < 0 || s->fd < 0) {
3040 return;
3041 }
aurel32ca587a82008-12-18 22:44:13 +00003042
Andreas Färber5ca666c2013-06-24 19:20:57 +02003043 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
3044 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00003045}
bellard1fddef42005-04-17 19:16:13 +00003046
Peter Maydell2f652222018-05-14 18:30:44 +01003047static bool gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00003048{
3049 GDBState *s;
3050 struct sockaddr_in sockaddr;
3051 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09003052 int fd;
bellard858693c2004-03-31 18:52:07 +00003053
3054 for(;;) {
3055 len = sizeof(sockaddr);
3056 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
3057 if (fd < 0 && errno != EINTR) {
3058 perror("accept");
Peter Maydell2f652222018-05-14 18:30:44 +01003059 return false;
bellard858693c2004-03-31 18:52:07 +00003060 } else if (fd >= 0) {
Peter Maydellf5bdd782018-05-14 18:30:43 +01003061 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00003062 break;
3063 }
3064 }
3065
3066 /* set short latency */
Peter Maydell2f652222018-05-14 18:30:44 +01003067 if (socket_set_nodelay(fd)) {
3068 perror("setsockopt");
Philippe Mathieu-Daudéead75d82018-05-24 19:34:58 -03003069 close(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01003070 return false;
3071 }
ths3b46e622007-09-17 08:09:54 +00003072
Anthony Liguori7267c092011-08-20 22:09:37 -05003073 s = g_malloc0(sizeof(GDBState));
Luc Michel8f468632019-01-07 15:23:45 +00003074 create_default_process(s);
Luc Michel970ed902019-01-07 15:23:46 +00003075 s->processes[0].attached = true;
3076 s->c_cpu = gdb_first_attached_cpu(s);
3077 s->g_cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00003078 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02003079 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00003080
aliguori880a7572008-11-18 20:30:24 +00003081 gdbserver_state = s;
Peter Maydell2f652222018-05-14 18:30:44 +01003082 return true;
bellard858693c2004-03-31 18:52:07 +00003083}
3084
3085static int gdbserver_open(int port)
3086{
3087 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02003088 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00003089
3090 fd = socket(PF_INET, SOCK_STREAM, 0);
3091 if (fd < 0) {
3092 perror("socket");
3093 return -1;
3094 }
Peter Maydellf5bdd782018-05-14 18:30:43 +01003095 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00003096
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02003097 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00003098
3099 sockaddr.sin_family = AF_INET;
3100 sockaddr.sin_port = htons(port);
3101 sockaddr.sin_addr.s_addr = 0;
3102 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
3103 if (ret < 0) {
3104 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00003105 close(fd);
bellard858693c2004-03-31 18:52:07 +00003106 return -1;
3107 }
Peter Wu96165b92016-05-04 11:32:17 +02003108 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00003109 if (ret < 0) {
3110 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00003111 close(fd);
bellard858693c2004-03-31 18:52:07 +00003112 return -1;
3113 }
bellard858693c2004-03-31 18:52:07 +00003114 return fd;
3115}
3116
3117int gdbserver_start(int port)
3118{
3119 gdbserver_fd = gdbserver_open(port);
3120 if (gdbserver_fd < 0)
3121 return -1;
3122 /* accept connections */
Peter Maydell2f652222018-05-14 18:30:44 +01003123 if (!gdb_accept()) {
3124 close(gdbserver_fd);
3125 gdbserver_fd = -1;
3126 return -1;
3127 }
bellardb4608c02003-06-27 17:34:32 +00003128 return 0;
3129}
aurel322b1319c2008-12-18 22:44:04 +00003130
3131/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07003132void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00003133{
3134 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02003135
3136 if (gdbserver_fd < 0 || s->fd < 0) {
3137 return;
3138 }
aurel322b1319c2008-12-18 22:44:04 +00003139 close(s->fd);
3140 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02003141 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02003142 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00003143}
pbrook4046d912007-01-28 01:53:16 +00003144#else
thsaa1f17c2007-07-11 22:48:58 +00003145static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00003146{
pbrook56aebc82008-10-11 17:55:29 +00003147 /* We can handle an arbitrarily large amount of data.
3148 Pick the maximum packet size, which is as good as anything. */
3149 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00003150}
3151
thsaa1f17c2007-07-11 22:48:58 +00003152static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00003153{
pbrook4046d912007-01-28 01:53:16 +00003154 int i;
3155
3156 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00003157 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00003158 }
3159}
3160
3161static void gdb_chr_event(void *opaque, int event)
3162{
Luc Michel970ed902019-01-07 15:23:46 +00003163 int i;
3164 GDBState *s = (GDBState *) opaque;
3165
pbrook4046d912007-01-28 01:53:16 +00003166 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05303167 case CHR_EVENT_OPENED:
Luc Michel970ed902019-01-07 15:23:46 +00003168 /* Start with first process attached, others detached */
3169 for (i = 0; i < s->process_num; i++) {
3170 s->processes[i].attached = !i;
3171 }
3172
3173 s->c_cpu = gdb_first_attached_cpu(s);
3174 s->g_cpu = s->c_cpu;
3175
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003176 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02003177 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00003178 break;
3179 default:
3180 break;
3181 }
3182}
3183
aliguori8a34a0f2009-03-05 23:01:55 +00003184static void gdb_monitor_output(GDBState *s, const char *msg, int len)
3185{
3186 char buf[MAX_PACKET_LENGTH];
3187
3188 buf[0] = 'O';
3189 if (len > (MAX_PACKET_LENGTH/2) - 1)
3190 len = (MAX_PACKET_LENGTH/2) - 1;
3191 memtohex(buf + 1, (uint8_t *)msg, len);
3192 put_packet(s, buf);
3193}
3194
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003195static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00003196{
3197 const char *p = (const char *)buf;
3198 int max_sz;
3199
3200 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
3201 for (;;) {
3202 if (len <= max_sz) {
3203 gdb_monitor_output(gdbserver_state, p, len);
3204 break;
3205 }
3206 gdb_monitor_output(gdbserver_state, p, max_sz);
3207 p += max_sz;
3208 len -= max_sz;
3209 }
3210 return len;
3211}
3212
aliguori59030a82009-04-05 18:43:41 +00003213#ifndef _WIN32
3214static void gdb_sigterm_handler(int signal)
3215{
Luiz Capitulino13548692011-07-29 15:36:43 -03003216 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003217 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01003218 }
aliguori59030a82009-04-05 18:43:41 +00003219}
3220#endif
3221
Marc-André Lureau777357d2016-12-07 18:39:10 +03003222static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
3223 bool *be_opened, Error **errp)
3224{
3225 *be_opened = false;
3226}
3227
3228static void char_gdb_class_init(ObjectClass *oc, void *data)
3229{
3230 ChardevClass *cc = CHARDEV_CLASS(oc);
3231
3232 cc->internal = true;
3233 cc->open = gdb_monitor_open;
3234 cc->chr_write = gdb_monitor_write;
3235}
3236
3237#define TYPE_CHARDEV_GDB "chardev-gdb"
3238
3239static const TypeInfo char_gdb_type_info = {
3240 .name = TYPE_CHARDEV_GDB,
3241 .parent = TYPE_CHARDEV,
3242 .class_init = char_gdb_class_init,
3243};
3244
Luc Michel8f468632019-01-07 15:23:45 +00003245static int find_cpu_clusters(Object *child, void *opaque)
3246{
3247 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
3248 GDBState *s = (GDBState *) opaque;
3249 CPUClusterState *cluster = CPU_CLUSTER(child);
3250 GDBProcess *process;
3251
3252 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
3253
3254 process = &s->processes[s->process_num - 1];
3255
3256 /*
3257 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
3258 * runtime, we enforce here that the machine does not use a cluster ID
3259 * that would lead to PID 0.
3260 */
3261 assert(cluster->cluster_id != UINT32_MAX);
3262 process->pid = cluster->cluster_id + 1;
3263 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00003264 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00003265
3266 return 0;
3267 }
3268
3269 return object_child_foreach(child, find_cpu_clusters, opaque);
3270}
3271
3272static int pid_order(const void *a, const void *b)
3273{
3274 GDBProcess *pa = (GDBProcess *) a;
3275 GDBProcess *pb = (GDBProcess *) b;
3276
3277 if (pa->pid < pb->pid) {
3278 return -1;
3279 } else if (pa->pid > pb->pid) {
3280 return 1;
3281 } else {
3282 return 0;
3283 }
3284}
3285
3286static void create_processes(GDBState *s)
3287{
3288 object_child_foreach(object_get_root(), find_cpu_clusters, s);
3289
3290 if (s->processes) {
3291 /* Sort by PID */
3292 qsort(s->processes, s->process_num, sizeof(s->processes[0]), pid_order);
3293 }
3294
3295 create_default_process(s);
3296}
3297
3298static void cleanup_processes(GDBState *s)
3299{
3300 g_free(s->processes);
3301 s->process_num = 0;
3302 s->processes = NULL;
3303}
3304
aliguori59030a82009-04-05 18:43:41 +00003305int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00003306{
Doug Gale5c9522b2017-12-02 20:30:37 -05003307 trace_gdbstub_op_start(device);
3308
pbrook4046d912007-01-28 01:53:16 +00003309 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00003310 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003311 Chardev *chr = NULL;
3312 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00003313
Ziyue Yang508b4ec2017-01-18 16:02:41 +08003314 if (!first_cpu) {
3315 error_report("gdbstub: meaningless to attach gdb to a "
3316 "machine without any CPU.");
3317 return -1;
3318 }
3319
aliguori59030a82009-04-05 18:43:41 +00003320 if (!device)
3321 return -1;
3322 if (strcmp(device, "none") != 0) {
3323 if (strstart(device, "tcp:", NULL)) {
3324 /* enforce required TCP attributes */
3325 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
3326 "%s,nowait,nodelay,server", device);
3327 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00003328 }
aliguori59030a82009-04-05 18:43:41 +00003329#ifndef _WIN32
3330 else if (strcmp(device, "stdio") == 0) {
3331 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00003332
aliguori59030a82009-04-05 18:43:41 +00003333 memset(&act, 0, sizeof(act));
3334 act.sa_handler = gdb_sigterm_handler;
3335 sigaction(SIGINT, &act, NULL);
3336 }
3337#endif
Marc-André Lureau95e30b22018-08-22 19:19:42 +02003338 /*
3339 * FIXME: it's a bit weird to allow using a mux chardev here
3340 * and implicitly setup a monitor. We may want to break this.
3341 */
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01003342 chr = qemu_chr_new_noreplay("gdb", device, true, NULL);
aliguori36556b22009-03-28 18:05:53 +00003343 if (!chr)
3344 return -1;
pbrookcfc34752007-02-22 01:48:01 +00003345 }
3346
aliguori36556b22009-03-28 18:05:53 +00003347 s = gdbserver_state;
3348 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05003349 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00003350 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00003351
aliguori36556b22009-03-28 18:05:53 +00003352 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
3353
3354 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03003355 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01003356 NULL, NULL, &error_abort);
Kevin Wolffbfc29e2019-06-13 17:34:04 +02003357 monitor_init_hmp(mon_chr, false);
aliguori36556b22009-03-28 18:05:53 +00003358 } else {
Marc-André Lureau1ce26102017-01-27 00:49:13 +04003359 qemu_chr_fe_deinit(&s->chr, true);
aliguori36556b22009-03-28 18:05:53 +00003360 mon_chr = s->mon_chr;
Luc Michel8f468632019-01-07 15:23:45 +00003361 cleanup_processes(s);
aliguori36556b22009-03-28 18:05:53 +00003362 memset(s, 0, sizeof(GDBState));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03003363 s->mon_chr = mon_chr;
aliguori36556b22009-03-28 18:05:53 +00003364 }
Luc Michel8f468632019-01-07 15:23:45 +00003365
3366 create_processes(s);
3367
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03003368 if (chr) {
3369 qemu_chr_fe_init(&s->chr, chr, &error_abort);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03003370 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
Luc Michel970ed902019-01-07 15:23:46 +00003371 gdb_chr_event, NULL, s, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03003372 }
aliguori36556b22009-03-28 18:05:53 +00003373 s->state = chr ? RS_IDLE : RS_INACTIVE;
3374 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00003375 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00003376
pbrook4046d912007-01-28 01:53:16 +00003377 return 0;
3378}
Marc-André Lureau777357d2016-12-07 18:39:10 +03003379
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01003380void gdbserver_cleanup(void)
3381{
3382 if (gdbserver_state) {
3383 put_packet(gdbserver_state, "W00");
3384 }
3385}
3386
Marc-André Lureau777357d2016-12-07 18:39:10 +03003387static void register_types(void)
3388{
3389 type_register_static(&char_gdb_type_info);
3390}
3391
3392type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00003393#endif