blob: 6ec87e169a30d8da080a99a486c8255af7925f3d [file] [log] [blame]
bellardb4608c02003-06-27 17:34:32 +00001/*
2 * gdb server stub
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard34751872005-07-02 14:31:34 +00004 * Copyright (c) 2003-2005 Fabrice Bellard
bellardb4608c02003-06-27 17:34:32 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
Blue Swirl8167ee82009-07-16 20:47:01 +000017 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
bellardb4608c02003-06-27 17:34:32 +000018 */
Markus Armbruster856dfd82019-05-23 16:35:06 +020019
Peter Maydelld38ea872016-01-29 17:50:05 +000020#include "qemu/osdep.h"
Markus Armbrustera8d25322019-05-23 16:35:08 +020021#include "qemu-common.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010022#include "qapi/error.h"
Ziyue Yang508b4ec2017-01-18 16:02:41 +080023#include "qemu/error-report.h"
Markus Armbruster856dfd82019-05-23 16:35:06 +020024#include "qemu/ctype.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020025#include "qemu/cutils.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020026#include "qemu/module.h"
Doug Gale5c9522b2017-12-02 20:30:37 -050027#include "trace-root.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020028#ifdef CONFIG_USER_ONLY
bellard1fddef42005-04-17 19:16:13 +000029#include "qemu.h"
30#else
Paolo Bonzini83c90892012-12-17 18:19:49 +010031#include "monitor/monitor.h"
Marc-André Lureau8228e352017-01-26 17:19:46 +040032#include "chardev/char.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040033#include "chardev/char-fe.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010034#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010035#include "exec/gdbstub.h"
Luc Michel8f468632019-01-07 15:23:45 +000036#include "hw/cpu/cluster.h"
bellard1fddef42005-04-17 19:16:13 +000037#endif
bellard67b915a2004-03-31 23:37:16 +000038
pbrook56aebc82008-10-11 17:55:29 +000039#define MAX_PACKET_LENGTH 4096
40
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010041#include "qemu/sockets.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010042#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010043#include "sysemu/kvm.h"
Alex Bennéef1672e62019-05-13 14:43:57 +010044#include "hw/semihosting/semihost.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010045#include "exec/exec-all.h"
aurel32ca587a82008-12-18 22:44:13 +000046
Jan Kiszkaa3919382015-02-07 09:38:44 +010047#ifdef CONFIG_USER_ONLY
48#define GDB_ATTACHED "0"
49#else
50#define GDB_ATTACHED "1"
51#endif
52
Andreas Färberf3659ee2013-06-27 19:09:09 +020053static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
54 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020055{
Andreas Färberf3659ee2013-06-27 19:09:09 +020056 CPUClass *cc = CPU_GET_CLASS(cpu);
57
58 if (cc->memory_rw_debug) {
59 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
60 }
61 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020062}
aurel32ca587a82008-12-18 22:44:13 +000063
Alex Bennéed2a6c852017-07-12 11:52:14 +010064/* Return the GDB index for a given vCPU state.
65 *
66 * For user mode this is simply the thread id. In system mode GDB
67 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
68 */
69static inline int cpu_gdb_index(CPUState *cpu)
70{
71#if defined(CONFIG_USER_ONLY)
Alex Bennéebd88c782017-07-12 11:52:15 +010072 TaskState *ts = (TaskState *) cpu->opaque;
73 return ts->ts_tid;
Alex Bennéed2a6c852017-07-12 11:52:14 +010074#else
75 return cpu->cpu_index + 1;
76#endif
77}
78
aurel32ca587a82008-12-18 22:44:13 +000079enum {
80 GDB_SIGNAL_0 = 0,
81 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +010082 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +000083 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +010084 GDB_SIGNAL_ABRT = 6,
85 GDB_SIGNAL_ALRM = 14,
86 GDB_SIGNAL_IO = 23,
87 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +000088 GDB_SIGNAL_UNKNOWN = 143
89};
90
91#ifdef CONFIG_USER_ONLY
92
93/* Map target signal numbers to GDB protocol signal numbers and vice
94 * versa. For user emulation's currently supported systems, we can
95 * assume most signals are defined.
96 */
97
98static int gdb_signal_table[] = {
99 0,
100 TARGET_SIGHUP,
101 TARGET_SIGINT,
102 TARGET_SIGQUIT,
103 TARGET_SIGILL,
104 TARGET_SIGTRAP,
105 TARGET_SIGABRT,
106 -1, /* SIGEMT */
107 TARGET_SIGFPE,
108 TARGET_SIGKILL,
109 TARGET_SIGBUS,
110 TARGET_SIGSEGV,
111 TARGET_SIGSYS,
112 TARGET_SIGPIPE,
113 TARGET_SIGALRM,
114 TARGET_SIGTERM,
115 TARGET_SIGURG,
116 TARGET_SIGSTOP,
117 TARGET_SIGTSTP,
118 TARGET_SIGCONT,
119 TARGET_SIGCHLD,
120 TARGET_SIGTTIN,
121 TARGET_SIGTTOU,
122 TARGET_SIGIO,
123 TARGET_SIGXCPU,
124 TARGET_SIGXFSZ,
125 TARGET_SIGVTALRM,
126 TARGET_SIGPROF,
127 TARGET_SIGWINCH,
128 -1, /* SIGLOST */
129 TARGET_SIGUSR1,
130 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000131#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000132 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000133#else
134 -1,
135#endif
aurel32ca587a82008-12-18 22:44:13 +0000136 -1, /* SIGPOLL */
137 -1,
138 -1,
139 -1,
140 -1,
141 -1,
142 -1,
143 -1,
144 -1,
145 -1,
146 -1,
147 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000148#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000149 __SIGRTMIN + 1,
150 __SIGRTMIN + 2,
151 __SIGRTMIN + 3,
152 __SIGRTMIN + 4,
153 __SIGRTMIN + 5,
154 __SIGRTMIN + 6,
155 __SIGRTMIN + 7,
156 __SIGRTMIN + 8,
157 __SIGRTMIN + 9,
158 __SIGRTMIN + 10,
159 __SIGRTMIN + 11,
160 __SIGRTMIN + 12,
161 __SIGRTMIN + 13,
162 __SIGRTMIN + 14,
163 __SIGRTMIN + 15,
164 __SIGRTMIN + 16,
165 __SIGRTMIN + 17,
166 __SIGRTMIN + 18,
167 __SIGRTMIN + 19,
168 __SIGRTMIN + 20,
169 __SIGRTMIN + 21,
170 __SIGRTMIN + 22,
171 __SIGRTMIN + 23,
172 __SIGRTMIN + 24,
173 __SIGRTMIN + 25,
174 __SIGRTMIN + 26,
175 __SIGRTMIN + 27,
176 __SIGRTMIN + 28,
177 __SIGRTMIN + 29,
178 __SIGRTMIN + 30,
179 __SIGRTMIN + 31,
180 -1, /* SIGCANCEL */
181 __SIGRTMIN,
182 __SIGRTMIN + 32,
183 __SIGRTMIN + 33,
184 __SIGRTMIN + 34,
185 __SIGRTMIN + 35,
186 __SIGRTMIN + 36,
187 __SIGRTMIN + 37,
188 __SIGRTMIN + 38,
189 __SIGRTMIN + 39,
190 __SIGRTMIN + 40,
191 __SIGRTMIN + 41,
192 __SIGRTMIN + 42,
193 __SIGRTMIN + 43,
194 __SIGRTMIN + 44,
195 __SIGRTMIN + 45,
196 __SIGRTMIN + 46,
197 __SIGRTMIN + 47,
198 __SIGRTMIN + 48,
199 __SIGRTMIN + 49,
200 __SIGRTMIN + 50,
201 __SIGRTMIN + 51,
202 __SIGRTMIN + 52,
203 __SIGRTMIN + 53,
204 __SIGRTMIN + 54,
205 __SIGRTMIN + 55,
206 __SIGRTMIN + 56,
207 __SIGRTMIN + 57,
208 __SIGRTMIN + 58,
209 __SIGRTMIN + 59,
210 __SIGRTMIN + 60,
211 __SIGRTMIN + 61,
212 __SIGRTMIN + 62,
213 __SIGRTMIN + 63,
214 __SIGRTMIN + 64,
215 __SIGRTMIN + 65,
216 __SIGRTMIN + 66,
217 __SIGRTMIN + 67,
218 __SIGRTMIN + 68,
219 __SIGRTMIN + 69,
220 __SIGRTMIN + 70,
221 __SIGRTMIN + 71,
222 __SIGRTMIN + 72,
223 __SIGRTMIN + 73,
224 __SIGRTMIN + 74,
225 __SIGRTMIN + 75,
226 __SIGRTMIN + 76,
227 __SIGRTMIN + 77,
228 __SIGRTMIN + 78,
229 __SIGRTMIN + 79,
230 __SIGRTMIN + 80,
231 __SIGRTMIN + 81,
232 __SIGRTMIN + 82,
233 __SIGRTMIN + 83,
234 __SIGRTMIN + 84,
235 __SIGRTMIN + 85,
236 __SIGRTMIN + 86,
237 __SIGRTMIN + 87,
238 __SIGRTMIN + 88,
239 __SIGRTMIN + 89,
240 __SIGRTMIN + 90,
241 __SIGRTMIN + 91,
242 __SIGRTMIN + 92,
243 __SIGRTMIN + 93,
244 __SIGRTMIN + 94,
245 __SIGRTMIN + 95,
246 -1, /* SIGINFO */
247 -1, /* UNKNOWN */
248 -1, /* DEFAULT */
249 -1,
250 -1,
251 -1,
252 -1,
253 -1,
254 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000255#endif
aurel32ca587a82008-12-18 22:44:13 +0000256};
bellard8f447cc2006-06-14 15:21:14 +0000257#else
aurel32ca587a82008-12-18 22:44:13 +0000258/* In system mode we only need SIGINT and SIGTRAP; other signals
259 are not yet supported. */
260
261enum {
262 TARGET_SIGINT = 2,
263 TARGET_SIGTRAP = 5
264};
265
266static int gdb_signal_table[] = {
267 -1,
268 -1,
269 TARGET_SIGINT,
270 -1,
271 -1,
272 TARGET_SIGTRAP
273};
bellard8f447cc2006-06-14 15:21:14 +0000274#endif
bellardb4608c02003-06-27 17:34:32 +0000275
aurel32ca587a82008-12-18 22:44:13 +0000276#ifdef CONFIG_USER_ONLY
277static int target_signal_to_gdb (int sig)
278{
279 int i;
280 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
281 if (gdb_signal_table[i] == sig)
282 return i;
283 return GDB_SIGNAL_UNKNOWN;
284}
285#endif
286
287static int gdb_signal_to_target (int sig)
288{
289 if (sig < ARRAY_SIZE (gdb_signal_table))
290 return gdb_signal_table[sig];
291 else
292 return -1;
293}
294
pbrook56aebc82008-10-11 17:55:29 +0000295typedef struct GDBRegisterState {
296 int base_reg;
297 int num_regs;
298 gdb_reg_cb get_reg;
299 gdb_reg_cb set_reg;
300 const char *xml;
301 struct GDBRegisterState *next;
302} GDBRegisterState;
303
Luc Michel8f468632019-01-07 15:23:45 +0000304typedef struct GDBProcess {
305 uint32_t pid;
306 bool attached;
Luc Michelc145eea2019-01-07 15:23:46 +0000307
308 char target_xml[1024];
Luc Michel8f468632019-01-07 15:23:45 +0000309} GDBProcess;
310
bellard858693c2004-03-31 18:52:07 +0000311enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000312 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000313 RS_IDLE,
314 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400315 RS_GETLINE_ESC,
316 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000317 RS_CHKSUM1,
318 RS_CHKSUM2,
319};
bellard858693c2004-03-31 18:52:07 +0000320typedef struct GDBState {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200321 CPUState *c_cpu; /* current CPU for step/continue ops */
322 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200323 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000324 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000325 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000326 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400327 int line_sum; /* running checksum */
328 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000329 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000330 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000331 int signal;
bellard41625032005-04-24 10:07:11 +0000332#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000333 int fd;
bellard41625032005-04-24 10:07:11 +0000334 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000335#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300336 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300337 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000338#endif
Luc Michel8f468632019-01-07 15:23:45 +0000339 bool multiprocess;
340 GDBProcess *processes;
341 int process_num;
Meador Ingecdb432b2012-03-15 17:49:45 +0000342 char syscall_buf[256];
343 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000344} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000345
edgar_igl60897d32008-05-09 08:25:14 +0000346/* By default use no IRQs and no timers while single stepping so as to
347 * make single stepping like an ICE HW step.
348 */
349static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
350
aliguori880a7572008-11-18 20:30:24 +0000351static GDBState *gdbserver_state;
352
Andreas Färber5b50e792013-06-29 04:18:45 +0200353bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000354
bellard1fddef42005-04-17 19:16:13 +0000355#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000356/* XXX: This is not thread safe. Do we care? */
357static int gdbserver_fd = -1;
358
bellard858693c2004-03-31 18:52:07 +0000359static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000360{
361 uint8_t ch;
362 int ret;
363
364 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000365 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000366 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000367 if (errno == ECONNRESET)
368 s->fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200369 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000370 return -1;
371 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000372 close(s->fd);
373 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000374 return -1;
375 } else {
376 break;
377 }
378 }
379 return ch;
380}
pbrook4046d912007-01-28 01:53:16 +0000381#endif
bellardb4608c02003-06-27 17:34:32 +0000382
blueswir1654efcf2009-04-18 07:29:59 +0000383static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000384 GDB_SYS_UNKNOWN,
385 GDB_SYS_ENABLED,
386 GDB_SYS_DISABLED,
387} gdb_syscall_mode;
388
Liviu Ionescua38bb072014-12-11 12:07:48 +0000389/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000390int use_gdb_syscalls(void)
391{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100392 SemihostingTarget target = semihosting_get_target();
393 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000394 /* -semihosting-config target=native */
395 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100396 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000397 /* -semihosting-config target=gdb */
398 return true;
399 }
400
401 /* -semihosting-config target=auto */
402 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000403 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000404 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
405 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000406 }
407 return gdb_syscall_mode == GDB_SYS_ENABLED;
408}
409
edgar_iglba70a622008-03-14 06:10:42 +0000410/* Resume execution. */
411static inline void gdb_continue(GDBState *s)
412{
Doug Gale5c9522b2017-12-02 20:30:37 -0500413
edgar_iglba70a622008-03-14 06:10:42 +0000414#ifdef CONFIG_USER_ONLY
415 s->running_state = 1;
Doug Gale5c9522b2017-12-02 20:30:37 -0500416 trace_gdbstub_op_continue();
edgar_iglba70a622008-03-14 06:10:42 +0000417#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200418 if (!runstate_needs_reset()) {
Doug Gale5c9522b2017-12-02 20:30:37 -0500419 trace_gdbstub_op_continue();
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200420 vm_start();
421 }
edgar_iglba70a622008-03-14 06:10:42 +0000422#endif
423}
424
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100425/*
426 * Resume execution, per CPU actions. For user-mode emulation it's
427 * equivalent to gdb_continue.
428 */
429static int gdb_continue_partial(GDBState *s, char *newstates)
430{
431 CPUState *cpu;
432 int res = 0;
433#ifdef CONFIG_USER_ONLY
434 /*
435 * This is not exactly accurate, but it's an improvement compared to the
436 * previous situation, where only one CPU would be single-stepped.
437 */
438 CPU_FOREACH(cpu) {
439 if (newstates[cpu->cpu_index] == 's') {
Doug Gale5c9522b2017-12-02 20:30:37 -0500440 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100441 cpu_single_step(cpu, sstep_flags);
442 }
443 }
444 s->running_state = 1;
445#else
446 int flag = 0;
447
448 if (!runstate_needs_reset()) {
449 if (vm_prepare_start()) {
450 return 0;
451 }
452
453 CPU_FOREACH(cpu) {
454 switch (newstates[cpu->cpu_index]) {
455 case 0:
456 case 1:
457 break; /* nothing to do here */
458 case 's':
Doug Gale5c9522b2017-12-02 20:30:37 -0500459 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100460 cpu_single_step(cpu, sstep_flags);
461 cpu_resume(cpu);
462 flag = 1;
463 break;
464 case 'c':
Doug Gale5c9522b2017-12-02 20:30:37 -0500465 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100466 cpu_resume(cpu);
467 flag = 1;
468 break;
469 default:
470 res = -1;
471 break;
472 }
473 }
474 }
475 if (flag) {
476 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
477 }
478#endif
479 return res;
480}
481
bellard858693c2004-03-31 18:52:07 +0000482static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000483{
pbrook4046d912007-01-28 01:53:16 +0000484#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000485 int ret;
486
487 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000488 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000489 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200490 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000491 return;
492 } else {
493 buf += ret;
494 len -= ret;
495 }
496 }
pbrook4046d912007-01-28 01:53:16 +0000497#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100498 /* XXX this blocks entire thread. Rewrite to use
499 * qemu_chr_fe_write and background I/O callbacks */
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300500 qemu_chr_fe_write_all(&s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000501#endif
bellardb4608c02003-06-27 17:34:32 +0000502}
503
504static inline int fromhex(int v)
505{
506 if (v >= '0' && v <= '9')
507 return v - '0';
508 else if (v >= 'A' && v <= 'F')
509 return v - 'A' + 10;
510 else if (v >= 'a' && v <= 'f')
511 return v - 'a' + 10;
512 else
513 return 0;
514}
515
516static inline int tohex(int v)
517{
518 if (v < 10)
519 return v + '0';
520 else
521 return v - 10 + 'a';
522}
523
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -0300524/* writes 2*len+1 bytes in buf */
bellardb4608c02003-06-27 17:34:32 +0000525static void memtohex(char *buf, const uint8_t *mem, int len)
526{
527 int i, c;
528 char *q;
529 q = buf;
530 for(i = 0; i < len; i++) {
531 c = mem[i];
532 *q++ = tohex(c >> 4);
533 *q++ = tohex(c & 0xf);
534 }
535 *q = '\0';
536}
537
538static void hextomem(uint8_t *mem, const char *buf, int len)
539{
540 int i;
541
542 for(i = 0; i < len; i++) {
543 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
544 buf += 2;
545 }
546}
547
Doug Gale5c9522b2017-12-02 20:30:37 -0500548static void hexdump(const char *buf, int len,
549 void (*trace_fn)(size_t ofs, char const *text))
550{
551 char line_buffer[3 * 16 + 4 + 16 + 1];
552
553 size_t i;
554 for (i = 0; i < len || (i & 0xF); ++i) {
555 size_t byte_ofs = i & 15;
556
557 if (byte_ofs == 0) {
558 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
559 line_buffer[3 * 16 + 4 + 16] = 0;
560 }
561
562 size_t col_group = (i >> 2) & 3;
563 size_t hex_col = byte_ofs * 3 + col_group;
564 size_t txt_col = 3 * 16 + 4 + byte_ofs;
565
566 if (i < len) {
567 char value = buf[i];
568
569 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
570 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
571 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
572 ? value
573 : '.';
574 }
575
576 if (byte_ofs == 0xF)
577 trace_fn(i & -16, line_buffer);
578 }
579}
580
bellardb4608c02003-06-27 17:34:32 +0000581/* return -1 if error, 0 if OK */
Doug Gale5c9522b2017-12-02 20:30:37 -0500582static int put_packet_binary(GDBState *s, const char *buf, int len, bool dump)
bellardb4608c02003-06-27 17:34:32 +0000583{
pbrook56aebc82008-10-11 17:55:29 +0000584 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000585 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000586
Doug Gale5c9522b2017-12-02 20:30:37 -0500587 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
588 hexdump(buf, len, trace_gdbstub_io_binaryreply);
589 }
590
bellardb4608c02003-06-27 17:34:32 +0000591 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000592 p = s->last_packet;
593 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000594 memcpy(p, buf, len);
595 p += len;
bellardb4608c02003-06-27 17:34:32 +0000596 csum = 0;
597 for(i = 0; i < len; i++) {
598 csum += buf[i];
599 }
pbrook4046d912007-01-28 01:53:16 +0000600 *(p++) = '#';
601 *(p++) = tohex((csum >> 4) & 0xf);
602 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000603
pbrook4046d912007-01-28 01:53:16 +0000604 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000605 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000606
pbrook4046d912007-01-28 01:53:16 +0000607#ifdef CONFIG_USER_ONLY
608 i = get_char(s);
609 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000610 return -1;
pbrook4046d912007-01-28 01:53:16 +0000611 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000612 break;
pbrook4046d912007-01-28 01:53:16 +0000613#else
614 break;
615#endif
bellardb4608c02003-06-27 17:34:32 +0000616 }
617 return 0;
618}
619
pbrook56aebc82008-10-11 17:55:29 +0000620/* return -1 if error, 0 if OK */
621static int put_packet(GDBState *s, const char *buf)
622{
Doug Gale5c9522b2017-12-02 20:30:37 -0500623 trace_gdbstub_io_reply(buf);
pbrook56aebc82008-10-11 17:55:29 +0000624
Doug Gale5c9522b2017-12-02 20:30:37 -0500625 return put_packet_binary(s, buf, strlen(buf), false);
pbrook56aebc82008-10-11 17:55:29 +0000626}
627
pbrook56aebc82008-10-11 17:55:29 +0000628/* Encode data using the encoding for 'x' packets. */
629static int memtox(char *buf, const char *mem, int len)
630{
631 char *p = buf;
632 char c;
633
634 while (len--) {
635 c = *(mem++);
636 switch (c) {
637 case '#': case '$': case '*': case '}':
638 *(p++) = '}';
639 *(p++) = c ^ 0x20;
640 break;
641 default:
642 *(p++) = c;
643 break;
644 }
645 }
646 return p - buf;
647}
648
Luc Michel1a227332019-01-07 15:23:45 +0000649static uint32_t gdb_get_cpu_pid(const GDBState *s, CPUState *cpu)
650{
Luc Michel1a227332019-01-07 15:23:45 +0000651 /* TODO: In user mode, we should use the task state PID */
Peter Maydell46f5abc2019-01-29 11:46:06 +0000652 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
653 /* Return the default process' PID */
654 return s->processes[s->process_num - 1].pid;
655 }
656 return cpu->cluster_index + 1;
Luc Michel1a227332019-01-07 15:23:45 +0000657}
658
Luc Michel7d8c87d2019-01-07 15:23:45 +0000659static GDBProcess *gdb_get_process(const GDBState *s, uint32_t pid)
660{
661 int i;
662
663 if (!pid) {
664 /* 0 means any process, we take the first one */
665 return &s->processes[0];
666 }
667
668 for (i = 0; i < s->process_num; i++) {
669 if (s->processes[i].pid == pid) {
670 return &s->processes[i];
671 }
672 }
673
674 return NULL;
675}
676
677static GDBProcess *gdb_get_cpu_process(const GDBState *s, CPUState *cpu)
678{
679 return gdb_get_process(s, gdb_get_cpu_pid(s, cpu));
680}
681
682static CPUState *find_cpu(uint32_t thread_id)
683{
684 CPUState *cpu;
685
686 CPU_FOREACH(cpu) {
687 if (cpu_gdb_index(cpu) == thread_id) {
688 return cpu;
689 }
690 }
691
692 return NULL;
693}
694
Luc Michele40e5202019-01-07 15:23:46 +0000695static CPUState *get_first_cpu_in_process(const GDBState *s,
696 GDBProcess *process)
697{
698 CPUState *cpu;
699
700 CPU_FOREACH(cpu) {
701 if (gdb_get_cpu_pid(s, cpu) == process->pid) {
702 return cpu;
703 }
704 }
705
706 return NULL;
707}
708
709static CPUState *gdb_next_cpu_in_process(const GDBState *s, CPUState *cpu)
710{
711 uint32_t pid = gdb_get_cpu_pid(s, cpu);
712 cpu = CPU_NEXT(cpu);
713
714 while (cpu) {
715 if (gdb_get_cpu_pid(s, cpu) == pid) {
716 break;
717 }
718
719 cpu = CPU_NEXT(cpu);
720 }
721
722 return cpu;
723}
724
Luc Michele40e5202019-01-07 15:23:46 +0000725/* Return the cpu following @cpu, while ignoring unattached processes. */
726static CPUState *gdb_next_attached_cpu(const GDBState *s, CPUState *cpu)
727{
728 cpu = CPU_NEXT(cpu);
729
730 while (cpu) {
731 if (gdb_get_cpu_process(s, cpu)->attached) {
732 break;
733 }
734
735 cpu = CPU_NEXT(cpu);
736 }
737
738 return cpu;
739}
740
741/* Return the first attached cpu */
742static CPUState *gdb_first_attached_cpu(const GDBState *s)
743{
744 CPUState *cpu = first_cpu;
745 GDBProcess *process = gdb_get_cpu_process(s, cpu);
746
747 if (!process->attached) {
748 return gdb_next_attached_cpu(s, cpu);
749 }
750
751 return cpu;
752}
753
Luc Michelab65eed2019-01-29 11:46:03 +0000754static CPUState *gdb_get_cpu(const GDBState *s, uint32_t pid, uint32_t tid)
755{
756 GDBProcess *process;
757 CPUState *cpu;
758
759 if (!pid && !tid) {
760 /* 0 means any process/thread, we take the first attached one */
761 return gdb_first_attached_cpu(s);
762 } else if (pid && !tid) {
763 /* any thread in a specific process */
764 process = gdb_get_process(s, pid);
765
766 if (process == NULL) {
767 return NULL;
768 }
769
770 if (!process->attached) {
771 return NULL;
772 }
773
774 return get_first_cpu_in_process(s, process);
775 } else {
776 /* a specific thread */
777 cpu = find_cpu(tid);
778
779 if (cpu == NULL) {
780 return NULL;
781 }
782
783 process = gdb_get_cpu_process(s, cpu);
784
785 if (pid && process->pid != pid) {
786 return NULL;
787 }
788
789 if (!process->attached) {
790 return NULL;
791 }
792
793 return cpu;
794 }
795}
796
Luc Michelc145eea2019-01-07 15:23:46 +0000797static const char *get_feature_xml(const GDBState *s, const char *p,
798 const char **newp, GDBProcess *process)
pbrook56aebc82008-10-11 17:55:29 +0000799{
pbrook56aebc82008-10-11 17:55:29 +0000800 size_t len;
801 int i;
802 const char *name;
Luc Michelc145eea2019-01-07 15:23:46 +0000803 CPUState *cpu = get_first_cpu_in_process(s, process);
804 CPUClass *cc = CPU_GET_CLASS(cpu);
pbrook56aebc82008-10-11 17:55:29 +0000805
806 len = 0;
807 while (p[len] && p[len] != ':')
808 len++;
809 *newp = p + len;
810
811 name = NULL;
812 if (strncmp(p, "target.xml", len) == 0) {
Luc Michelc145eea2019-01-07 15:23:46 +0000813 char *buf = process->target_xml;
814 const size_t buf_sz = sizeof(process->target_xml);
pbrook56aebc82008-10-11 17:55:29 +0000815
Luc Michelc145eea2019-01-07 15:23:46 +0000816 /* Generate the XML description for this CPU. */
817 if (!buf[0]) {
818 GDBRegisterState *r;
819
820 pstrcat(buf, buf_sz,
David Hildenbrandb3820e62015-12-03 13:14:41 +0100821 "<?xml version=\"1.0\"?>"
822 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
823 "<target>");
824 if (cc->gdb_arch_name) {
825 gchar *arch = cc->gdb_arch_name(cpu);
Luc Michelc145eea2019-01-07 15:23:46 +0000826 pstrcat(buf, buf_sz, "<architecture>");
827 pstrcat(buf, buf_sz, arch);
828 pstrcat(buf, buf_sz, "</architecture>");
David Hildenbrandb3820e62015-12-03 13:14:41 +0100829 g_free(arch);
830 }
Luc Michelc145eea2019-01-07 15:23:46 +0000831 pstrcat(buf, buf_sz, "<xi:include href=\"");
832 pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
833 pstrcat(buf, buf_sz, "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200834 for (r = cpu->gdb_regs; r; r = r->next) {
Luc Michelc145eea2019-01-07 15:23:46 +0000835 pstrcat(buf, buf_sz, "<xi:include href=\"");
836 pstrcat(buf, buf_sz, r->xml);
837 pstrcat(buf, buf_sz, "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000838 }
Luc Michelc145eea2019-01-07 15:23:46 +0000839 pstrcat(buf, buf_sz, "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000840 }
Luc Michelc145eea2019-01-07 15:23:46 +0000841 return buf;
pbrook56aebc82008-10-11 17:55:29 +0000842 }
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100843 if (cc->gdb_get_dynamic_xml) {
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100844 char *xmlname = g_strndup(p, len);
845 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
846
847 g_free(xmlname);
848 if (xml) {
849 return xml;
850 }
851 }
pbrook56aebc82008-10-11 17:55:29 +0000852 for (i = 0; ; i++) {
853 name = xml_builtin[i][0];
854 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
855 break;
856 }
857 return name ? xml_builtin[i][1] : NULL;
858}
pbrook56aebc82008-10-11 17:55:29 +0000859
Andreas Färber385b9f02013-06-27 18:25:36 +0200860static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000861{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200862 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200863 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000864 GDBRegisterState *r;
865
Andreas Färbera0e372f2013-06-28 23:18:47 +0200866 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200867 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200868 }
pbrook56aebc82008-10-11 17:55:29 +0000869
Andreas Färbereac8b352013-06-28 21:11:37 +0200870 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000871 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
872 return r->get_reg(env, mem_buf, reg - r->base_reg);
873 }
874 }
875 return 0;
876}
877
Andreas Färber385b9f02013-06-27 18:25:36 +0200878static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000879{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200880 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200881 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000882 GDBRegisterState *r;
883
Andreas Färbera0e372f2013-06-28 23:18:47 +0200884 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200885 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200886 }
pbrook56aebc82008-10-11 17:55:29 +0000887
Andreas Färbereac8b352013-06-28 21:11:37 +0200888 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000889 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
890 return r->set_reg(env, mem_buf, reg - r->base_reg);
891 }
892 }
893 return 0;
894}
895
896/* Register a supplemental set of CPU registers. If g_pos is nonzero it
897 specifies the first register number and these registers are included in
898 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
899 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
900 */
901
Andreas Färber22169d42013-06-28 21:27:39 +0200902void gdb_register_coprocessor(CPUState *cpu,
903 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
904 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000905{
906 GDBRegisterState *s;
907 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000908
Andreas Färbereac8b352013-06-28 21:11:37 +0200909 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000910 while (*p) {
911 /* Check for duplicates. */
912 if (strcmp((*p)->xml, xml) == 0)
913 return;
914 p = &(*p)->next;
915 }
Stefan Weil9643c252011-10-18 22:25:38 +0200916
917 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200918 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200919 s->num_regs = num_regs;
920 s->get_reg = get_reg;
921 s->set_reg = set_reg;
922 s->xml = xml;
923
pbrook56aebc82008-10-11 17:55:29 +0000924 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200925 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000926 *p = s;
927 if (g_pos) {
928 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800929 error_report("Error: Bad gdb register numbering for '%s', "
930 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200931 } else {
932 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000933 }
934 }
935}
936
aliguoria1d1bb32008-11-18 20:07:32 +0000937#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100938/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
939static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
940{
941 static const int xlat[] = {
942 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
943 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
944 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
945 };
946
947 CPUClass *cc = CPU_GET_CLASS(cpu);
948 int cputype = xlat[gdbtype];
949
950 if (cc->gdb_stop_before_watchpoint) {
951 cputype |= BP_STOP_BEFORE_ACCESS;
952 }
953 return cputype;
954}
aliguoria1d1bb32008-11-18 20:07:32 +0000955#endif
956
Jon Doron77f6ce52019-05-29 09:41:35 +0300957static int gdb_breakpoint_insert(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +0000958{
Andreas Färber182735e2013-05-29 22:29:20 +0200959 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000960 int err = 0;
961
Andreas Färber62278812013-06-27 17:12:06 +0200962 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200963 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200964 }
aliguorie22a25c2009-03-12 20:12:48 +0000965
aliguoria1d1bb32008-11-18 20:07:32 +0000966 switch (type) {
967 case GDB_BREAKPOINT_SW:
968 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200969 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200970 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
971 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000972 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200973 }
aliguori880a7572008-11-18 20:30:24 +0000974 }
975 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000976#ifndef CONFIG_USER_ONLY
977 case GDB_WATCHPOINT_WRITE:
978 case GDB_WATCHPOINT_READ:
979 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200980 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100981 err = cpu_watchpoint_insert(cpu, addr, len,
982 xlat_gdb_type(cpu, type), NULL);
983 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000984 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +0100985 }
aliguori880a7572008-11-18 20:30:24 +0000986 }
987 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000988#endif
989 default:
990 return -ENOSYS;
991 }
992}
993
Jon Doron77f6ce52019-05-29 09:41:35 +0300994static int gdb_breakpoint_remove(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +0000995{
Andreas Färber182735e2013-05-29 22:29:20 +0200996 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000997 int err = 0;
998
Andreas Färber62278812013-06-27 17:12:06 +0200999 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001000 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001001 }
aliguorie22a25c2009-03-12 20:12:48 +00001002
aliguoria1d1bb32008-11-18 20:07:32 +00001003 switch (type) {
1004 case GDB_BREAKPOINT_SW:
1005 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001006 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001007 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
1008 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001009 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001010 }
aliguori880a7572008-11-18 20:30:24 +00001011 }
1012 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001013#ifndef CONFIG_USER_ONLY
1014 case GDB_WATCHPOINT_WRITE:
1015 case GDB_WATCHPOINT_READ:
1016 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001017 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001018 err = cpu_watchpoint_remove(cpu, addr, len,
1019 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +00001020 if (err)
1021 break;
1022 }
1023 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001024#endif
1025 default:
1026 return -ENOSYS;
1027 }
1028}
1029
Luc Michel546f3c62019-01-07 15:23:46 +00001030static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu)
1031{
1032 cpu_breakpoint_remove_all(cpu, BP_GDB);
1033#ifndef CONFIG_USER_ONLY
1034 cpu_watchpoint_remove_all(cpu, BP_GDB);
1035#endif
1036}
1037
1038static void gdb_process_breakpoint_remove_all(const GDBState *s, GDBProcess *p)
1039{
1040 CPUState *cpu = get_first_cpu_in_process(s, p);
1041
1042 while (cpu) {
1043 gdb_cpu_breakpoint_remove_all(cpu);
1044 cpu = gdb_next_cpu_in_process(s, cpu);
1045 }
1046}
1047
aliguori880a7572008-11-18 20:30:24 +00001048static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +00001049{
Andreas Färber182735e2013-05-29 22:29:20 +02001050 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001051
aliguorie22a25c2009-03-12 20:12:48 +00001052 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001053 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +00001054 return;
1055 }
1056
Andreas Färberbdc44642013-06-24 23:50:24 +02001057 CPU_FOREACH(cpu) {
Luc Michel546f3c62019-01-07 15:23:46 +00001058 gdb_cpu_breakpoint_remove_all(cpu);
aliguori880a7572008-11-18 20:30:24 +00001059 }
aliguoria1d1bb32008-11-18 20:07:32 +00001060}
1061
aurel32fab9d282009-04-08 21:29:37 +00001062static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
1063{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001064 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +02001065
1066 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -07001067 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +00001068}
1069
Luc Michel1a227332019-01-07 15:23:45 +00001070static char *gdb_fmt_thread_id(const GDBState *s, CPUState *cpu,
1071 char *buf, size_t buf_size)
1072{
1073 if (s->multiprocess) {
1074 snprintf(buf, buf_size, "p%02x.%02x",
1075 gdb_get_cpu_pid(s, cpu), cpu_gdb_index(cpu));
1076 } else {
1077 snprintf(buf, buf_size, "%02x", cpu_gdb_index(cpu));
1078 }
1079
1080 return buf;
1081}
1082
Luc Michel7d8c87d2019-01-07 15:23:45 +00001083typedef enum GDBThreadIdKind {
1084 GDB_ONE_THREAD = 0,
1085 GDB_ALL_THREADS, /* One process, all threads */
1086 GDB_ALL_PROCESSES,
1087 GDB_READ_THREAD_ERR
1088} GDBThreadIdKind;
1089
1090static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1091 uint32_t *pid, uint32_t *tid)
1092{
1093 unsigned long p, t;
1094 int ret;
1095
1096 if (*buf == 'p') {
1097 buf++;
1098 ret = qemu_strtoul(buf, &buf, 16, &p);
1099
1100 if (ret) {
1101 return GDB_READ_THREAD_ERR;
1102 }
1103
1104 /* Skip '.' */
1105 buf++;
1106 } else {
1107 p = 1;
1108 }
1109
1110 ret = qemu_strtoul(buf, &buf, 16, &t);
1111
1112 if (ret) {
1113 return GDB_READ_THREAD_ERR;
1114 }
1115
1116 *end_buf = buf;
1117
1118 if (p == -1) {
1119 return GDB_ALL_PROCESSES;
1120 }
1121
1122 if (pid) {
1123 *pid = p;
1124 }
1125
1126 if (t == -1) {
1127 return GDB_ALL_THREADS;
1128 }
1129
1130 if (tid) {
1131 *tid = t;
1132 }
1133
1134 return GDB_ONE_THREAD;
1135}
1136
Jan Kiszka4dabe742015-02-07 09:38:43 +01001137static int is_query_packet(const char *p, const char *query, char separator)
1138{
1139 unsigned int query_len = strlen(query);
1140
1141 return strncmp(p, query, query_len) == 0 &&
1142 (p[query_len] == '\0' || p[query_len] == separator);
1143}
1144
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001145/**
1146 * gdb_handle_vcont - Parses and handles a vCont packet.
1147 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1148 * a format error, 0 on success.
1149 */
1150static int gdb_handle_vcont(GDBState *s, const char *p)
1151{
Luc Michele40e5202019-01-07 15:23:46 +00001152 int res, signal = 0;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001153 char cur_action;
1154 char *newstates;
1155 unsigned long tmp;
Luc Michele40e5202019-01-07 15:23:46 +00001156 uint32_t pid, tid;
1157 GDBProcess *process;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001158 CPUState *cpu;
Luc Michelc99ef792019-03-26 12:53:26 +00001159 GDBThreadIdKind kind;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001160#ifdef CONFIG_USER_ONLY
1161 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
1162
1163 CPU_FOREACH(cpu) {
1164 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1165 }
1166#endif
1167 /* uninitialised CPUs stay 0 */
1168 newstates = g_new0(char, max_cpus);
1169
1170 /* mark valid CPUs with 1 */
1171 CPU_FOREACH(cpu) {
1172 newstates[cpu->cpu_index] = 1;
1173 }
1174
1175 /*
1176 * res keeps track of what error we are returning, with -ENOTSUP meaning
1177 * that the command is unknown or unsupported, thus returning an empty
1178 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1179 * or incorrect parameters passed.
1180 */
1181 res = 0;
1182 while (*p) {
1183 if (*p++ != ';') {
1184 res = -ENOTSUP;
1185 goto out;
1186 }
1187
1188 cur_action = *p++;
1189 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +01001190 cur_action = qemu_tolower(cur_action);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001191 res = qemu_strtoul(p + 1, &p, 16, &tmp);
1192 if (res) {
1193 goto out;
1194 }
1195 signal = gdb_signal_to_target(tmp);
1196 } else if (cur_action != 'c' && cur_action != 's') {
1197 /* unknown/invalid/unsupported command */
1198 res = -ENOTSUP;
1199 goto out;
1200 }
Luc Michele40e5202019-01-07 15:23:46 +00001201
Luc Michelc99ef792019-03-26 12:53:26 +00001202 if (*p == '\0' || *p == ';') {
1203 /*
1204 * No thread specifier, action is on "all threads". The
1205 * specification is unclear regarding the process to act on. We
1206 * choose all processes.
1207 */
1208 kind = GDB_ALL_PROCESSES;
1209 } else if (*p++ == ':') {
1210 kind = read_thread_id(p, &p, &pid, &tid);
1211 } else {
Luc Michele40e5202019-01-07 15:23:46 +00001212 res = -ENOTSUP;
1213 goto out;
1214 }
1215
Luc Michelc99ef792019-03-26 12:53:26 +00001216 switch (kind) {
Luc Michele40e5202019-01-07 15:23:46 +00001217 case GDB_READ_THREAD_ERR:
1218 res = -EINVAL;
1219 goto out;
1220
1221 case GDB_ALL_PROCESSES:
1222 cpu = gdb_first_attached_cpu(s);
1223 while (cpu) {
1224 if (newstates[cpu->cpu_index] == 1) {
1225 newstates[cpu->cpu_index] = cur_action;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001226 }
Luc Michele40e5202019-01-07 15:23:46 +00001227
1228 cpu = gdb_next_attached_cpu(s, cpu);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001229 }
Luc Michele40e5202019-01-07 15:23:46 +00001230 break;
1231
1232 case GDB_ALL_THREADS:
1233 process = gdb_get_process(s, pid);
1234
1235 if (!process->attached) {
1236 res = -EINVAL;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001237 goto out;
1238 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001239
Luc Michele40e5202019-01-07 15:23:46 +00001240 cpu = get_first_cpu_in_process(s, process);
1241 while (cpu) {
1242 if (newstates[cpu->cpu_index] == 1) {
1243 newstates[cpu->cpu_index] = cur_action;
1244 }
1245
1246 cpu = gdb_next_cpu_in_process(s, cpu);
1247 }
1248 break;
1249
1250 case GDB_ONE_THREAD:
1251 cpu = gdb_get_cpu(s, pid, tid);
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001252
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001253 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001254 if (!cpu) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001255 res = -EINVAL;
1256 goto out;
1257 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001258
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001259 /* only use if no previous match occourred */
1260 if (newstates[cpu->cpu_index] == 1) {
1261 newstates[cpu->cpu_index] = cur_action;
1262 }
Luc Michele40e5202019-01-07 15:23:46 +00001263 break;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001264 }
1265 }
1266 s->signal = signal;
1267 gdb_continue_partial(s, newstates);
1268
1269out:
1270 g_free(newstates);
1271
1272 return res;
1273}
1274
Jon Dorond14055d2019-05-29 09:41:29 +03001275typedef union GdbCmdVariant {
1276 const char *data;
1277 uint8_t opcode;
1278 unsigned long val_ul;
1279 unsigned long long val_ull;
1280 struct {
1281 GDBThreadIdKind kind;
1282 uint32_t pid;
1283 uint32_t tid;
1284 } thread_id;
1285} GdbCmdVariant;
1286
1287static const char *cmd_next_param(const char *param, const char delimiter)
1288{
1289 static const char all_delimiters[] = ",;:=";
1290 char curr_delimiters[2] = {0};
1291 const char *delimiters;
1292
1293 if (delimiter == '?') {
1294 delimiters = all_delimiters;
1295 } else if (delimiter == '0') {
1296 return strchr(param, '\0');
1297 } else if (delimiter == '.' && *param) {
1298 return param + 1;
1299 } else {
1300 curr_delimiters[0] = delimiter;
1301 delimiters = curr_delimiters;
1302 }
1303
1304 param += strcspn(param, delimiters);
1305 if (*param) {
1306 param++;
1307 }
1308 return param;
1309}
1310
1311static int cmd_parse_params(const char *data, const char *schema,
1312 GdbCmdVariant *params, int *num_params)
1313{
1314 int curr_param;
1315 const char *curr_schema, *curr_data;
1316
1317 *num_params = 0;
1318
1319 if (!schema) {
1320 return 0;
1321 }
1322
1323 curr_schema = schema;
1324 curr_param = 0;
1325 curr_data = data;
1326 while (curr_schema[0] && curr_schema[1] && *curr_data) {
1327 switch (curr_schema[0]) {
1328 case 'l':
1329 if (qemu_strtoul(curr_data, &curr_data, 16,
1330 &params[curr_param].val_ul)) {
1331 return -EINVAL;
1332 }
1333 curr_param++;
1334 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1335 break;
1336 case 'L':
1337 if (qemu_strtou64(curr_data, &curr_data, 16,
1338 (uint64_t *)&params[curr_param].val_ull)) {
1339 return -EINVAL;
1340 }
1341 curr_param++;
1342 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1343 break;
1344 case 's':
1345 params[curr_param].data = curr_data;
1346 curr_param++;
1347 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1348 break;
1349 case 'o':
1350 params[curr_param].opcode = *(uint8_t *)curr_data;
1351 curr_param++;
1352 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1353 break;
1354 case 't':
1355 params[curr_param].thread_id.kind =
1356 read_thread_id(curr_data, &curr_data,
1357 &params[curr_param].thread_id.pid,
1358 &params[curr_param].thread_id.tid);
1359 curr_param++;
1360 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1361 break;
1362 case '?':
1363 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1364 break;
1365 default:
1366 return -EINVAL;
1367 }
1368 curr_schema += 2;
1369 }
1370
1371 *num_params = curr_param;
1372 return 0;
1373}
1374
1375typedef struct GdbCmdContext {
1376 GDBState *s;
1377 GdbCmdVariant *params;
1378 int num_params;
1379 uint8_t mem_buf[MAX_PACKET_LENGTH];
1380 char str_buf[MAX_PACKET_LENGTH + 1];
1381} GdbCmdContext;
1382
1383typedef void (*GdbCmdHandler)(GdbCmdContext *gdb_ctx, void *user_ctx);
1384
1385/*
1386 * cmd_startswith -> cmd is compared using startswith
1387 *
1388 *
1389 * schema definitions:
1390 * Each schema parameter entry consists of 2 chars,
1391 * the first char represents the parameter type handling
1392 * the second char represents the delimiter for the next parameter
1393 *
1394 * Currently supported schema types:
1395 * 'l' -> unsigned long (stored in .val_ul)
1396 * 'L' -> unsigned long long (stored in .val_ull)
1397 * 's' -> string (stored in .data)
1398 * 'o' -> single char (stored in .opcode)
1399 * 't' -> thread id (stored in .thread_id)
1400 * '?' -> skip according to delimiter
1401 *
1402 * Currently supported delimiters:
1403 * '?' -> Stop at any delimiter (",;:=\0")
1404 * '0' -> Stop at "\0"
1405 * '.' -> Skip 1 char unless reached "\0"
1406 * Any other value is treated as the delimiter value itself
1407 */
1408typedef struct GdbCmdParseEntry {
1409 GdbCmdHandler handler;
1410 const char *cmd;
1411 bool cmd_startswith;
1412 const char *schema;
1413} GdbCmdParseEntry;
1414
1415static inline int startswith(const char *string, const char *pattern)
1416{
1417 return !strncmp(string, pattern, strlen(pattern));
1418}
1419
Jon Dorond14055d2019-05-29 09:41:29 +03001420static int process_string_cmd(GDBState *s, void *user_ctx, const char *data,
1421 const GdbCmdParseEntry *cmds, int num_cmds)
1422{
1423 int i, schema_len, max_num_params = 0;
1424 GdbCmdContext gdb_ctx;
1425
1426 if (!cmds) {
1427 return -1;
1428 }
1429
1430 for (i = 0; i < num_cmds; i++) {
1431 const GdbCmdParseEntry *cmd = &cmds[i];
1432 g_assert(cmd->handler && cmd->cmd);
1433
1434 if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
1435 (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
1436 continue;
1437 }
1438
1439 if (cmd->schema) {
1440 schema_len = strlen(cmd->schema);
1441 if (schema_len % 2) {
1442 return -2;
1443 }
1444
1445 max_num_params = schema_len / 2;
1446 }
1447
1448 gdb_ctx.params =
1449 (GdbCmdVariant *)alloca(sizeof(*gdb_ctx.params) * max_num_params);
1450 memset(gdb_ctx.params, 0, sizeof(*gdb_ctx.params) * max_num_params);
1451
1452 if (cmd_parse_params(&data[strlen(cmd->cmd)], cmd->schema,
1453 gdb_ctx.params, &gdb_ctx.num_params)) {
1454 return -1;
1455 }
1456
1457 gdb_ctx.s = s;
1458 cmd->handler(&gdb_ctx, user_ctx);
1459 return 0;
1460 }
1461
1462 return -1;
1463}
1464
Jon Doron3e2c1262019-05-29 09:41:30 +03001465static void run_cmd_parser(GDBState *s, const char *data,
1466 const GdbCmdParseEntry *cmd)
1467{
1468 if (!data) {
1469 return;
1470 }
1471
1472 /* In case there was an error during the command parsing we must
1473 * send a NULL packet to indicate the command is not supported */
1474 if (process_string_cmd(s, NULL, data, cmd, 1)) {
1475 put_packet(s, "");
1476 }
1477}
1478
1479static void handle_detach(GdbCmdContext *gdb_ctx, void *user_ctx)
1480{
1481 GDBProcess *process;
1482 GDBState *s = gdb_ctx->s;
1483 uint32_t pid = 1;
1484
1485 if (s->multiprocess) {
1486 if (!gdb_ctx->num_params) {
1487 put_packet(s, "E22");
1488 return;
1489 }
1490
1491 pid = gdb_ctx->params[0].val_ul;
1492 }
1493
1494 process = gdb_get_process(s, pid);
1495 gdb_process_breakpoint_remove_all(s, process);
1496 process->attached = false;
1497
1498 if (pid == gdb_get_cpu_pid(s, s->c_cpu)) {
1499 s->c_cpu = gdb_first_attached_cpu(s);
1500 }
1501
1502 if (pid == gdb_get_cpu_pid(s, s->g_cpu)) {
1503 s->g_cpu = gdb_first_attached_cpu(s);
1504 }
1505
1506 if (!s->c_cpu) {
1507 /* No more process attached */
1508 gdb_syscall_mode = GDB_SYS_DISABLED;
1509 gdb_continue(s);
1510 }
1511 put_packet(s, "OK");
1512}
1513
Jon Doron44ffded2019-05-29 09:41:31 +03001514static void handle_thread_alive(GdbCmdContext *gdb_ctx, void *user_ctx)
1515{
1516 CPUState *cpu;
1517
1518 if (!gdb_ctx->num_params) {
1519 put_packet(gdb_ctx->s, "E22");
1520 return;
1521 }
1522
1523 if (gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
1524 put_packet(gdb_ctx->s, "E22");
1525 return;
1526 }
1527
1528 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[0].thread_id.pid,
1529 gdb_ctx->params[0].thread_id.tid);
1530 if (!cpu) {
1531 put_packet(gdb_ctx->s, "E22");
1532 return;
1533 }
1534
1535 put_packet(gdb_ctx->s, "OK");
1536}
1537
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001538static void handle_continue(GdbCmdContext *gdb_ctx, void *user_ctx)
1539{
1540 if (gdb_ctx->num_params) {
1541 gdb_set_cpu_pc(gdb_ctx->s, gdb_ctx->params[0].val_ull);
1542 }
1543
1544 gdb_ctx->s->signal = 0;
1545 gdb_continue(gdb_ctx->s);
1546}
1547
Jon Doronccc47d52019-05-29 09:41:33 +03001548static void handle_cont_with_sig(GdbCmdContext *gdb_ctx, void *user_ctx)
1549{
1550 unsigned long signal = 0;
1551
1552 /*
1553 * Note: C sig;[addr] is currently unsupported and we simply
1554 * omit the addr parameter
1555 */
1556 if (gdb_ctx->num_params) {
1557 signal = gdb_ctx->params[0].val_ul;
1558 }
1559
1560 gdb_ctx->s->signal = gdb_signal_to_target(signal);
1561 if (gdb_ctx->s->signal == -1) {
1562 gdb_ctx->s->signal = 0;
1563 }
1564 gdb_continue(gdb_ctx->s);
1565}
1566
Jon Doron3a9651d2019-05-29 09:41:34 +03001567static void handle_set_thread(GdbCmdContext *gdb_ctx, void *user_ctx)
1568{
1569 CPUState *cpu;
1570
1571 if (gdb_ctx->num_params != 2) {
1572 put_packet(gdb_ctx->s, "E22");
1573 return;
1574 }
1575
1576 if (gdb_ctx->params[1].thread_id.kind == GDB_READ_THREAD_ERR) {
1577 put_packet(gdb_ctx->s, "E22");
1578 return;
1579 }
1580
1581 if (gdb_ctx->params[1].thread_id.kind != GDB_ONE_THREAD) {
1582 put_packet(gdb_ctx->s, "OK");
1583 return;
1584 }
1585
1586 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[1].thread_id.pid,
1587 gdb_ctx->params[1].thread_id.tid);
1588 if (!cpu) {
1589 put_packet(gdb_ctx->s, "E22");
1590 return;
1591 }
1592
1593 /*
1594 * Note: This command is deprecated and modern gdb's will be using the
1595 * vCont command instead.
1596 */
1597 switch (gdb_ctx->params[0].opcode) {
1598 case 'c':
1599 gdb_ctx->s->c_cpu = cpu;
1600 put_packet(gdb_ctx->s, "OK");
1601 break;
1602 case 'g':
1603 gdb_ctx->s->g_cpu = cpu;
1604 put_packet(gdb_ctx->s, "OK");
1605 break;
1606 default:
1607 put_packet(gdb_ctx->s, "E22");
1608 break;
1609 }
1610}
1611
Jon Doron77f6ce52019-05-29 09:41:35 +03001612static void handle_insert_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1613{
1614 int res;
1615
1616 if (gdb_ctx->num_params != 3) {
1617 put_packet(gdb_ctx->s, "E22");
1618 return;
1619 }
1620
1621 res = gdb_breakpoint_insert(gdb_ctx->params[0].val_ul,
1622 gdb_ctx->params[1].val_ull,
1623 gdb_ctx->params[2].val_ull);
1624 if (res >= 0) {
1625 put_packet(gdb_ctx->s, "OK");
1626 return;
1627 } else if (res == -ENOSYS) {
1628 put_packet(gdb_ctx->s, "");
1629 return;
1630 }
1631
1632 put_packet(gdb_ctx->s, "E22");
1633}
1634
1635static void handle_remove_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1636{
1637 int res;
1638
1639 if (gdb_ctx->num_params != 3) {
1640 put_packet(gdb_ctx->s, "E22");
1641 return;
1642 }
1643
1644 res = gdb_breakpoint_remove(gdb_ctx->params[0].val_ul,
1645 gdb_ctx->params[1].val_ull,
1646 gdb_ctx->params[2].val_ull);
1647 if (res >= 0) {
1648 put_packet(gdb_ctx->s, "OK");
1649 return;
1650 } else if (res == -ENOSYS) {
1651 put_packet(gdb_ctx->s, "");
1652 return;
1653 }
1654
1655 put_packet(gdb_ctx->s, "E22");
1656}
1657
Jon Doron62b33202019-05-29 09:41:36 +03001658static void handle_set_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1659{
1660 int reg_size;
1661
1662 if (!gdb_has_xml) {
1663 put_packet(gdb_ctx->s, "E00");
1664 return;
1665 }
1666
1667 if (gdb_ctx->num_params != 2) {
1668 put_packet(gdb_ctx->s, "E22");
1669 return;
1670 }
1671
1672 reg_size = strlen(gdb_ctx->params[1].data) / 2;
1673 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[1].data, reg_size);
1674 gdb_write_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf,
1675 gdb_ctx->params[0].val_ull);
1676 put_packet(gdb_ctx->s, "OK");
1677}
1678
aliguori880a7572008-11-18 20:30:24 +00001679static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +00001680{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001681 CPUState *cpu;
Luc Michelc145eea2019-01-07 15:23:46 +00001682 GDBProcess *process;
Andreas Färber5b24c642013-07-07 15:08:22 +02001683 CPUClass *cc;
bellardb4608c02003-06-27 17:34:32 +00001684 const char *p;
Luc Michel7d8c87d2019-01-07 15:23:45 +00001685 uint32_t pid, tid;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001686 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +00001687 uint8_t mem_buf[MAX_PACKET_LENGTH];
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -03001688 char buf[sizeof(mem_buf) + 1 /* trailing NUL */];
Luc Michel1a227332019-01-07 15:23:45 +00001689 char thread_id[16];
pbrook56aebc82008-10-11 17:55:29 +00001690 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +00001691 target_ulong addr, len;
Jon Doron3e2c1262019-05-29 09:41:30 +03001692 const GdbCmdParseEntry *cmd_parser = NULL;
ths3b46e622007-09-17 08:09:54 +00001693
Doug Gale5c9522b2017-12-02 20:30:37 -05001694 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01001695
bellard858693c2004-03-31 18:52:07 +00001696 p = line_buf;
1697 ch = *p++;
1698 switch(ch) {
Luc Michel53fd6552019-01-07 15:23:46 +00001699 case '!':
1700 put_packet(s, "OK");
1701 break;
bellard858693c2004-03-31 18:52:07 +00001702 case '?':
bellard1fddef42005-04-17 19:16:13 +00001703 /* TODO: Make this return the correct value for user-mode. */
Luc Michel1a227332019-01-07 15:23:45 +00001704 snprintf(buf, sizeof(buf), "T%02xthread:%s;", GDB_SIGNAL_TRAP,
1705 gdb_fmt_thread_id(s, s->c_cpu, thread_id, sizeof(thread_id)));
bellard858693c2004-03-31 18:52:07 +00001706 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +00001707 /* Remove all the breakpoints when this query is issued,
1708 * because gdb is doing and initial connect and the state
1709 * should be cleaned up.
1710 */
aliguori880a7572008-11-18 20:30:24 +00001711 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +00001712 break;
1713 case 'c':
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001714 {
1715 static const GdbCmdParseEntry continue_cmd_desc = {
1716 .handler = handle_continue,
1717 .cmd = "c",
1718 .cmd_startswith = 1,
1719 .schema = "L0"
1720 };
1721 cmd_parser = &continue_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00001722 }
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001723 break;
edgar_igl1f487ee2008-05-17 22:20:53 +00001724 case 'C':
Jon Doronccc47d52019-05-29 09:41:33 +03001725 {
1726 static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
1727 .handler = handle_cont_with_sig,
1728 .cmd = "C",
1729 .cmd_startswith = 1,
1730 .schema = "l0"
1731 };
1732 cmd_parser = &cont_with_sig_cmd_desc;
1733 }
1734 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001735 case 'v':
1736 if (strncmp(p, "Cont", 4) == 0) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001737 p += 4;
1738 if (*p == '?') {
1739 put_packet(s, "vCont;c;C;s;S");
1740 break;
1741 }
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001742
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001743 res = gdb_handle_vcont(s, p);
1744
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001745 if (res) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001746 if ((res == -EINVAL) || (res == -ERANGE)) {
1747 put_packet(s, "E22");
1748 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001749 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001750 goto unknown_command;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001751 }
1752 break;
Luc Michel3f940dc2019-01-07 15:23:46 +00001753 } else if (strncmp(p, "Attach;", 7) == 0) {
1754 unsigned long pid;
1755
1756 p += 7;
1757
1758 if (qemu_strtoul(p, &p, 16, &pid)) {
1759 put_packet(s, "E22");
1760 break;
1761 }
1762
1763 process = gdb_get_process(s, pid);
1764
1765 if (process == NULL) {
1766 put_packet(s, "E22");
1767 break;
1768 }
1769
1770 cpu = get_first_cpu_in_process(s, process);
1771
1772 if (cpu == NULL) {
1773 /* Refuse to attach an empty process */
1774 put_packet(s, "E22");
1775 break;
1776 }
1777
1778 process->attached = true;
1779
1780 s->g_cpu = cpu;
1781 s->c_cpu = cpu;
1782
1783 snprintf(buf, sizeof(buf), "T%02xthread:%s;", GDB_SIGNAL_TRAP,
1784 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id)));
1785
1786 put_packet(s, buf);
1787 break;
Max Filippov45a4de22019-02-05 16:52:41 +00001788 } else if (strncmp(p, "Kill;", 5) == 0) {
1789 /* Kill the target */
Sandra Loosemore0f8b09b2019-02-15 09:56:41 +00001790 put_packet(s, "OK");
Max Filippov45a4de22019-02-05 16:52:41 +00001791 error_report("QEMU: Terminated via GDBstub");
1792 exit(0);
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001793 } else {
1794 goto unknown_command;
1795 }
edgar_igl7d03f822008-05-17 18:58:29 +00001796 case 'k':
1797 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001798 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00001799 exit(0);
1800 case 'D':
Jon Doron3e2c1262019-05-29 09:41:30 +03001801 {
1802 static const GdbCmdParseEntry detach_cmd_desc = {
1803 .handler = handle_detach,
1804 .cmd = "D",
1805 .cmd_startswith = 1,
1806 .schema = "?.l0"
1807 };
1808 cmd_parser = &detach_cmd_desc;
Luc Michel546f3c62019-01-07 15:23:46 +00001809 }
edgar_igl7d03f822008-05-17 18:58:29 +00001810 break;
bellard858693c2004-03-31 18:52:07 +00001811 case 's':
1812 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +00001813 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001814 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001815 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001816 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +00001817 gdb_continue(s);
Doug Gale5c9522b2017-12-02 20:30:37 -05001818 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +00001819 case 'F':
1820 {
1821 target_ulong ret;
1822 target_ulong err;
1823
1824 ret = strtoull(p, (char **)&p, 16);
1825 if (*p == ',') {
1826 p++;
1827 err = strtoull(p, (char **)&p, 16);
1828 } else {
1829 err = 0;
1830 }
1831 if (*p == ',')
1832 p++;
1833 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001834 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001835 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +00001836 s->current_syscall_cb = NULL;
1837 }
pbrooka2d1eba2007-01-28 03:10:55 +00001838 if (type == 'C') {
1839 put_packet(s, "T02");
1840 } else {
edgar_iglba70a622008-03-14 06:10:42 +00001841 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +00001842 }
1843 }
1844 break;
bellard858693c2004-03-31 18:52:07 +00001845 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001846 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001847 len = 0;
Andreas Färber35143f02013-08-12 18:09:47 +02001848 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001849 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +00001850 len += reg_size;
1851 }
1852 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +00001853 put_packet(s, buf);
1854 break;
1855 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001856 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001857 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +00001858 len = strlen(p) / 2;
1859 hextomem((uint8_t *)registers, p, len);
Andreas Färber35143f02013-08-12 18:09:47 +02001860 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001861 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001862 len -= reg_size;
1863 registers += reg_size;
1864 }
bellard858693c2004-03-31 18:52:07 +00001865 put_packet(s, "OK");
1866 break;
1867 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001868 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001869 if (*p == ',')
1870 p++;
bellard9d9754a2006-06-25 15:32:37 +00001871 len = strtoull(p, NULL, 16);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001872
1873 /* memtohex() doubles the required space */
1874 if (len > MAX_PACKET_LENGTH / 2) {
1875 put_packet (s, "E22");
1876 break;
1877 }
1878
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001879 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001880 put_packet (s, "E14");
1881 } else {
1882 memtohex(buf, mem_buf, len);
1883 put_packet(s, buf);
1884 }
bellard858693c2004-03-31 18:52:07 +00001885 break;
1886 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001887 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001888 if (*p == ',')
1889 p++;
bellard9d9754a2006-06-25 15:32:37 +00001890 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001891 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001892 p++;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001893
1894 /* hextomem() reads 2*len bytes */
1895 if (len > strlen(p) / 2) {
1896 put_packet (s, "E22");
1897 break;
1898 }
bellard858693c2004-03-31 18:52:07 +00001899 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001900 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +02001901 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +00001902 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001903 } else {
bellard858693c2004-03-31 18:52:07 +00001904 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001905 }
bellard858693c2004-03-31 18:52:07 +00001906 break;
pbrook56aebc82008-10-11 17:55:29 +00001907 case 'p':
1908 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1909 This works, but can be very slow. Anything new enough to
1910 understand XML also knows how to use this properly. */
1911 if (!gdb_has_xml)
1912 goto unknown_command;
1913 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001914 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001915 if (reg_size) {
1916 memtohex(buf, mem_buf, reg_size);
1917 put_packet(s, buf);
1918 } else {
1919 put_packet(s, "E14");
1920 }
1921 break;
1922 case 'P':
Jon Doron62b33202019-05-29 09:41:36 +03001923 {
1924 static const GdbCmdParseEntry set_reg_cmd_desc = {
1925 .handler = handle_set_reg,
1926 .cmd = "P",
1927 .cmd_startswith = 1,
1928 .schema = "L?s0"
1929 };
1930 cmd_parser = &set_reg_cmd_desc;
1931 }
pbrook56aebc82008-10-11 17:55:29 +00001932 break;
bellard858693c2004-03-31 18:52:07 +00001933 case 'Z':
Jon Doron77f6ce52019-05-29 09:41:35 +03001934 {
1935 static const GdbCmdParseEntry insert_bp_cmd_desc = {
1936 .handler = handle_insert_bp,
1937 .cmd = "Z",
1938 .cmd_startswith = 1,
1939 .schema = "l?L?L0"
1940 };
1941 cmd_parser = &insert_bp_cmd_desc;
1942 }
1943 break;
bellard858693c2004-03-31 18:52:07 +00001944 case 'z':
Jon Doron77f6ce52019-05-29 09:41:35 +03001945 {
1946 static const GdbCmdParseEntry remove_bp_cmd_desc = {
1947 .handler = handle_remove_bp,
1948 .cmd = "z",
1949 .cmd_startswith = 1,
1950 .schema = "l?L?L0"
1951 };
1952 cmd_parser = &remove_bp_cmd_desc;
1953 }
bellard858693c2004-03-31 18:52:07 +00001954 break;
aliguori880a7572008-11-18 20:30:24 +00001955 case 'H':
Jon Doron3a9651d2019-05-29 09:41:34 +03001956 {
1957 static const GdbCmdParseEntry set_thread_cmd_desc = {
1958 .handler = handle_set_thread,
1959 .cmd = "H",
1960 .cmd_startswith = 1,
1961 .schema = "o.t0"
1962 };
1963 cmd_parser = &set_thread_cmd_desc;
aliguori880a7572008-11-18 20:30:24 +00001964 }
1965 break;
1966 case 'T':
Jon Doron44ffded2019-05-29 09:41:31 +03001967 {
1968 static const GdbCmdParseEntry thread_alive_cmd_desc = {
1969 .handler = handle_thread_alive,
1970 .cmd = "T",
1971 .cmd_startswith = 1,
1972 .schema = "t0"
1973 };
1974 cmd_parser = &thread_alive_cmd_desc;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001975 }
aliguori880a7572008-11-18 20:30:24 +00001976 break;
pbrook978efd62006-06-17 18:30:42 +00001977 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001978 case 'Q':
1979 /* parse any 'q' packets here */
1980 if (!strcmp(p,"qemu.sstepbits")) {
1981 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001982 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1983 SSTEP_ENABLE,
1984 SSTEP_NOIRQ,
1985 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001986 put_packet(s, buf);
1987 break;
Jan Kiszka4dabe742015-02-07 09:38:43 +01001988 } else if (is_query_packet(p, "qemu.sstep", '=')) {
edgar_igl60897d32008-05-09 08:25:14 +00001989 /* Display or change the sstep_flags */
1990 p += 10;
1991 if (*p != '=') {
1992 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001993 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001994 put_packet(s, buf);
1995 break;
1996 }
1997 p++;
1998 type = strtoul(p, (char **)&p, 16);
1999 sstep_flags = type;
2000 put_packet(s, "OK");
2001 break;
aliguori880a7572008-11-18 20:30:24 +00002002 } else if (strcmp(p,"C") == 0) {
Luc Michel8dbbe9a2019-01-07 15:23:46 +00002003 /*
2004 * "Current thread" remains vague in the spec, so always return
2005 * the first thread of the current process (gdb returns the
2006 * first thread).
2007 */
2008 cpu = get_first_cpu_in_process(s, gdb_get_cpu_process(s, s->g_cpu));
2009 snprintf(buf, sizeof(buf), "QC%s",
2010 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id)));
2011 put_packet(s, buf);
aliguori880a7572008-11-18 20:30:24 +00002012 break;
2013 } else if (strcmp(p,"fThreadInfo") == 0) {
Luc Michel7cf48f62019-01-07 15:23:46 +00002014 s->query_cpu = gdb_first_attached_cpu(s);
aliguori880a7572008-11-18 20:30:24 +00002015 goto report_cpuinfo;
2016 } else if (strcmp(p,"sThreadInfo") == 0) {
2017 report_cpuinfo:
2018 if (s->query_cpu) {
Luc Michel7cf48f62019-01-07 15:23:46 +00002019 snprintf(buf, sizeof(buf), "m%s",
2020 gdb_fmt_thread_id(s, s->query_cpu,
2021 thread_id, sizeof(thread_id)));
aliguori880a7572008-11-18 20:30:24 +00002022 put_packet(s, buf);
Luc Michel7cf48f62019-01-07 15:23:46 +00002023 s->query_cpu = gdb_next_attached_cpu(s, s->query_cpu);
aliguori880a7572008-11-18 20:30:24 +00002024 } else
2025 put_packet(s, "l");
2026 break;
2027 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
Luc Michel7cf48f62019-01-07 15:23:46 +00002028 if (read_thread_id(p + 16, &p, &pid, &tid) == GDB_READ_THREAD_ERR) {
2029 put_packet(s, "E22");
2030 break;
2031 }
2032 cpu = gdb_get_cpu(s, pid, tid);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002033 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02002034 cpu_synchronize_state(cpu);
Luc Michel7cf48f62019-01-07 15:23:46 +00002035
2036 if (s->multiprocess && (s->process_num > 1)) {
2037 /* Print the CPU model and name in multiprocess mode */
2038 ObjectClass *oc = object_get_class(OBJECT(cpu));
2039 const char *cpu_model = object_class_get_name(oc);
2040 char *cpu_name =
2041 object_get_canonical_path_component(OBJECT(cpu));
2042 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
2043 "%s %s [%s]", cpu_model, cpu_name,
2044 cpu->halted ? "halted " : "running");
2045 g_free(cpu_name);
2046 } else {
2047 /* memtohex() doubles the required space */
2048 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
2049 "CPU#%d [%s]", cpu->cpu_index,
2050 cpu->halted ? "halted " : "running");
2051 }
Doug Gale5c9522b2017-12-02 20:30:37 -05002052 trace_gdbstub_op_extra_info((char *)mem_buf);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07002053 memtohex(buf, mem_buf, len);
2054 put_packet(s, buf);
2055 }
aliguori880a7572008-11-18 20:30:24 +00002056 break;
edgar_igl60897d32008-05-09 08:25:14 +00002057 }
blueswir10b8a9882009-03-07 10:51:36 +00002058#ifdef CONFIG_USER_ONLY
Jan Kiszka070949f2015-02-07 09:38:42 +01002059 else if (strcmp(p, "Offsets") == 0) {
Andreas Färber0429a972013-08-26 18:14:44 +02002060 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00002061
blueswir1363a37d2008-08-21 17:58:08 +00002062 snprintf(buf, sizeof(buf),
2063 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
2064 ";Bss=" TARGET_ABI_FMT_lx,
2065 ts->info->code_offset,
2066 ts->info->data_offset,
2067 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00002068 put_packet(s, buf);
2069 break;
2070 }
blueswir10b8a9882009-03-07 10:51:36 +00002071#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00002072 else if (strncmp(p, "Rcmd,", 5) == 0) {
2073 int len = strlen(p + 5);
2074
2075 if ((len % 2) != 0) {
2076 put_packet(s, "E01");
2077 break;
2078 }
aliguori8a34a0f2009-03-05 23:01:55 +00002079 len = len / 2;
Kevin Wolf5accecb2015-10-13 09:38:50 +02002080 hextomem(mem_buf, p + 5, len);
aliguori8a34a0f2009-03-05 23:01:55 +00002081 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05002082 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00002083 put_packet(s, "OK");
2084 break;
2085 }
blueswir10b8a9882009-03-07 10:51:36 +00002086#endif /* !CONFIG_USER_ONLY */
Jan Kiszka4dabe742015-02-07 09:38:43 +01002087 if (is_query_packet(p, "Supported", ':')) {
blueswir15b3715b2008-10-25 11:18:12 +00002088 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
Andreas Färber5b24c642013-07-07 15:08:22 +02002089 cc = CPU_GET_CLASS(first_cpu);
2090 if (cc->gdb_core_xml_file != NULL) {
2091 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
2092 }
Luc Michel364fce62019-01-07 15:23:46 +00002093
2094 if (strstr(p, "multiprocess+")) {
2095 s->multiprocess = true;
2096 }
2097 pstrcat(buf, sizeof(buf), ";multiprocess+");
2098
pbrook56aebc82008-10-11 17:55:29 +00002099 put_packet(s, buf);
2100 break;
2101 }
pbrook56aebc82008-10-11 17:55:29 +00002102 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
2103 const char *xml;
2104 target_ulong total_len;
2105
Luc Michelc145eea2019-01-07 15:23:46 +00002106 process = gdb_get_cpu_process(s, s->g_cpu);
2107 cc = CPU_GET_CLASS(s->g_cpu);
Andreas Färber5b24c642013-07-07 15:08:22 +02002108 if (cc->gdb_core_xml_file == NULL) {
2109 goto unknown_command;
2110 }
2111
Andreas Färber5b50e792013-06-29 04:18:45 +02002112 gdb_has_xml = true;
pbrook56aebc82008-10-11 17:55:29 +00002113 p += 19;
Luc Michelc145eea2019-01-07 15:23:46 +00002114 xml = get_feature_xml(s, p, &p, process);
pbrook56aebc82008-10-11 17:55:29 +00002115 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00002116 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00002117 put_packet(s, buf);
2118 break;
2119 }
2120
2121 if (*p == ':')
2122 p++;
2123 addr = strtoul(p, (char **)&p, 16);
2124 if (*p == ',')
2125 p++;
2126 len = strtoul(p, (char **)&p, 16);
2127
2128 total_len = strlen(xml);
2129 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00002130 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00002131 put_packet(s, buf);
2132 break;
2133 }
2134 if (len > (MAX_PACKET_LENGTH - 5) / 2)
2135 len = (MAX_PACKET_LENGTH - 5) / 2;
2136 if (len < total_len - addr) {
2137 buf[0] = 'm';
2138 len = memtox(buf + 1, xml + addr, len);
2139 } else {
2140 buf[0] = 'l';
2141 len = memtox(buf + 1, xml + addr, total_len - addr);
2142 }
Doug Gale5c9522b2017-12-02 20:30:37 -05002143 put_packet_binary(s, buf, len + 1, true);
pbrook56aebc82008-10-11 17:55:29 +00002144 break;
2145 }
Jan Kiszkaa3919382015-02-07 09:38:44 +01002146 if (is_query_packet(p, "Attached", ':')) {
2147 put_packet(s, GDB_ATTACHED);
2148 break;
2149 }
pbrook56aebc82008-10-11 17:55:29 +00002150 /* Unrecognised 'q' command. */
2151 goto unknown_command;
2152
bellard858693c2004-03-31 18:52:07 +00002153 default:
pbrook56aebc82008-10-11 17:55:29 +00002154 unknown_command:
bellard858693c2004-03-31 18:52:07 +00002155 /* put empty packet */
2156 buf[0] = '\0';
2157 put_packet(s, buf);
2158 break;
2159 }
Jon Doron3e2c1262019-05-29 09:41:30 +03002160
2161 run_cmd_parser(s, line_buf, cmd_parser);
2162
bellard858693c2004-03-31 18:52:07 +00002163 return RS_IDLE;
2164}
2165
Andreas Färber64f6b342013-05-27 02:06:09 +02002166void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00002167{
Luc Michel160d8582019-01-07 15:23:46 +00002168 GDBProcess *p = gdb_get_cpu_process(gdbserver_state, cpu);
2169
2170 if (!p->attached) {
2171 /*
2172 * Having a stop CPU corresponding to a process that is not attached
2173 * confuses GDB. So we ignore the request.
2174 */
2175 return;
2176 }
2177
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002178 gdbserver_state->c_cpu = cpu;
2179 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00002180}
2181
bellard1fddef42005-04-17 19:16:13 +00002182#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002183static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00002184{
aliguori880a7572008-11-18 20:30:24 +00002185 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002186 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00002187 char buf[256];
Luc Michel95567c22019-01-07 15:23:46 +00002188 char thread_id[16];
aliguorid6fc1b32008-11-18 19:55:44 +00002189 const char *type;
bellard858693c2004-03-31 18:52:07 +00002190 int ret;
2191
Meador Ingecdb432b2012-03-15 17:49:45 +00002192 if (running || s->state == RS_INACTIVE) {
2193 return;
2194 }
2195 /* Is there a GDB syscall waiting to be sent? */
2196 if (s->current_syscall_cb) {
2197 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00002198 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01002199 }
Luc Michel95567c22019-01-07 15:23:46 +00002200
2201 if (cpu == NULL) {
2202 /* No process attached */
2203 return;
2204 }
2205
2206 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id));
2207
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002208 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002209 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02002210 if (cpu->watchpoint_hit) {
2211 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00002212 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00002213 type = "r";
2214 break;
aliguoria1d1bb32008-11-18 20:07:32 +00002215 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00002216 type = "a";
2217 break;
2218 default:
2219 type = "";
2220 break;
2221 }
Doug Gale5c9522b2017-12-02 20:30:37 -05002222 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
2223 (target_ulong)cpu->watchpoint_hit->vaddr);
aliguori880a7572008-11-18 20:30:24 +00002224 snprintf(buf, sizeof(buf),
Luc Michel95567c22019-01-07 15:23:46 +00002225 "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
2226 GDB_SIGNAL_TRAP, thread_id, type,
Andreas Färberff4700b2013-08-26 18:23:18 +02002227 (target_ulong)cpu->watchpoint_hit->vaddr);
2228 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01002229 goto send_packet;
Doug Gale5c9522b2017-12-02 20:30:37 -05002230 } else {
2231 trace_gdbstub_hit_break();
pbrook6658ffb2007-03-16 23:58:11 +00002232 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07002233 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00002234 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01002235 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002236 case RUN_STATE_PAUSED:
Doug Gale5c9522b2017-12-02 20:30:37 -05002237 trace_gdbstub_hit_paused();
aliguori9781e042009-01-22 17:15:29 +00002238 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01002239 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002240 case RUN_STATE_SHUTDOWN:
Doug Gale5c9522b2017-12-02 20:30:37 -05002241 trace_gdbstub_hit_shutdown();
Jan Kiszka425189a2011-03-22 11:02:09 +01002242 ret = GDB_SIGNAL_QUIT;
2243 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002244 case RUN_STATE_IO_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002245 trace_gdbstub_hit_io_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002246 ret = GDB_SIGNAL_IO;
2247 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002248 case RUN_STATE_WATCHDOG:
Doug Gale5c9522b2017-12-02 20:30:37 -05002249 trace_gdbstub_hit_watchdog();
Jan Kiszka425189a2011-03-22 11:02:09 +01002250 ret = GDB_SIGNAL_ALRM;
2251 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002252 case RUN_STATE_INTERNAL_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002253 trace_gdbstub_hit_internal_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002254 ret = GDB_SIGNAL_ABRT;
2255 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002256 case RUN_STATE_SAVE_VM:
2257 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01002258 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002259 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01002260 ret = GDB_SIGNAL_XCPU;
2261 break;
2262 default:
Doug Gale5c9522b2017-12-02 20:30:37 -05002263 trace_gdbstub_hit_unknown(state);
Jan Kiszka425189a2011-03-22 11:02:09 +01002264 ret = GDB_SIGNAL_UNKNOWN;
2265 break;
bellardbbeb7b52006-04-23 18:42:15 +00002266 }
Jan Kiszka226d0072015-07-24 18:52:31 +02002267 gdb_set_stop_cpu(cpu);
Luc Michel95567c22019-01-07 15:23:46 +00002268 snprintf(buf, sizeof(buf), "T%02xthread:%s;", ret, thread_id);
Jan Kiszka425189a2011-03-22 11:02:09 +01002269
2270send_packet:
bellard858693c2004-03-31 18:52:07 +00002271 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01002272
2273 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02002274 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00002275}
bellard1fddef42005-04-17 19:16:13 +00002276#endif
bellard858693c2004-03-31 18:52:07 +00002277
pbrooka2d1eba2007-01-28 03:10:55 +00002278/* Send a gdb syscall request.
2279 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00002280 %x - target_ulong argument printed in hex.
2281 %lx - 64-bit argument printed in hex.
2282 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01002283void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00002284{
pbrooka2d1eba2007-01-28 03:10:55 +00002285 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00002286 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00002287 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00002288 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00002289 GDBState *s;
2290
aliguori880a7572008-11-18 20:30:24 +00002291 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00002292 if (!s)
2293 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00002294 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00002295#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002296 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00002297#endif
Meador Ingecdb432b2012-03-15 17:49:45 +00002298 p = s->syscall_buf;
2299 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00002300 *(p++) = 'F';
2301 while (*fmt) {
2302 if (*fmt == '%') {
2303 fmt++;
2304 switch (*fmt++) {
2305 case 'x':
2306 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002307 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00002308 break;
pbrooka87295e2007-05-26 15:09:38 +00002309 case 'l':
2310 if (*(fmt++) != 'x')
2311 goto bad_format;
2312 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00002313 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00002314 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002315 case 's':
2316 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002317 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00002318 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00002319 break;
2320 default:
pbrooka87295e2007-05-26 15:09:38 +00002321 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002322 error_report("gdbstub: Bad syscall format string '%s'",
2323 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00002324 break;
2325 }
2326 } else {
2327 *(p++) = *(fmt++);
2328 }
2329 }
pbrook8a93e022007-08-06 13:19:15 +00002330 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00002331#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00002332 put_packet(s, s->syscall_buf);
Peter Maydell4f710862018-05-15 19:19:58 +01002333 /* Return control to gdb for it to process the syscall request.
2334 * Since the protocol requires that gdb hands control back to us
2335 * using a "here are the results" F packet, we don't need to check
2336 * gdb_handlesig's return value (which is the signal to deliver if
2337 * execution was resumed via a continue packet).
2338 */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002339 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00002340#else
Meador Ingecdb432b2012-03-15 17:49:45 +00002341 /* In this case wait to send the syscall packet until notification that
2342 the CPU has stopped. This must be done because if the packet is sent
2343 now the reply from the syscall request could be received while the CPU
2344 is still in the running state, which can cause packets to be dropped
2345 and state transition 'T' packets to be sent while the syscall is still
2346 being processed. */
Paolo Bonzini9102ded2015-08-18 06:52:09 -07002347 qemu_cpu_kick(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00002348#endif
2349}
2350
Peter Maydell19239b32015-09-07 10:39:27 +01002351void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
2352{
2353 va_list va;
2354
2355 va_start(va, fmt);
2356 gdb_do_syscallv(cb, fmt, va);
2357 va_end(va);
2358}
2359
Markus Armbruster33c846e2019-05-14 20:03:09 +02002360static void gdb_read_byte(GDBState *s, uint8_t ch)
bellard858693c2004-03-31 18:52:07 +00002361{
ths60fe76f2007-12-16 03:02:09 +00002362 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00002363
bellard1fddef42005-04-17 19:16:13 +00002364#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00002365 if (s->last_packet_len) {
2366 /* Waiting for a response to the last packet. If we see the start
2367 of a new command then abandon the previous response. */
2368 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002369 trace_gdbstub_err_got_nack();
thsffe8ab82007-12-16 03:16:05 +00002370 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
Alex Bennée118e2262017-07-12 11:52:13 +01002371 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002372 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01002373 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002374 trace_gdbstub_io_got_unexpected(ch);
pbrook4046d912007-01-28 01:53:16 +00002375 }
Alex Bennée118e2262017-07-12 11:52:13 +01002376
pbrook4046d912007-01-28 01:53:16 +00002377 if (ch == '+' || ch == '$')
2378 s->last_packet_len = 0;
2379 if (ch != '$')
2380 return;
2381 }
Luiz Capitulino13548692011-07-29 15:36:43 -03002382 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00002383 /* when the CPU is running, we cannot do anything except stop
2384 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002385 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00002386 } else
bellard1fddef42005-04-17 19:16:13 +00002387#endif
bellard41625032005-04-24 10:07:11 +00002388 {
bellard858693c2004-03-31 18:52:07 +00002389 switch(s->state) {
2390 case RS_IDLE:
2391 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04002392 /* start of command packet */
bellard858693c2004-03-31 18:52:07 +00002393 s->line_buf_index = 0;
Doug Gale4bf43122017-05-01 12:22:10 -04002394 s->line_sum = 0;
bellard858693c2004-03-31 18:52:07 +00002395 s->state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002396 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002397 trace_gdbstub_err_garbage(ch);
bellard4c3a88a2003-07-26 12:06:08 +00002398 }
2399 break;
bellard858693c2004-03-31 18:52:07 +00002400 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04002401 if (ch == '}') {
2402 /* start escape sequence */
2403 s->state = RS_GETLINE_ESC;
2404 s->line_sum += ch;
2405 } else if (ch == '*') {
2406 /* start run length encoding sequence */
2407 s->state = RS_GETLINE_RLE;
2408 s->line_sum += ch;
2409 } else if (ch == '#') {
2410 /* end of command, start of checksum*/
2411 s->state = RS_CHKSUM1;
bellard858693c2004-03-31 18:52:07 +00002412 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002413 trace_gdbstub_err_overrun();
bellard858693c2004-03-31 18:52:07 +00002414 s->state = RS_IDLE;
2415 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002416 /* unescaped command character */
2417 s->line_buf[s->line_buf_index++] = ch;
2418 s->line_sum += ch;
2419 }
2420 break;
2421 case RS_GETLINE_ESC:
2422 if (ch == '#') {
2423 /* unexpected end of command in escape sequence */
2424 s->state = RS_CHKSUM1;
2425 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2426 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05002427 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002428 s->state = RS_IDLE;
2429 } else {
2430 /* parse escaped character and leave escape state */
2431 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
2432 s->line_sum += ch;
2433 s->state = RS_GETLINE;
2434 }
2435 break;
2436 case RS_GETLINE_RLE:
Markus Armbruster046aba12019-05-14 20:03:08 +02002437 /*
2438 * Run-length encoding is explained in "Debugging with GDB /
2439 * Appendix E GDB Remote Serial Protocol / Overview".
2440 */
2441 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
Doug Gale4bf43122017-05-01 12:22:10 -04002442 /* invalid RLE count encoding */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002443 trace_gdbstub_err_invalid_repeat(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002444 s->state = RS_GETLINE;
2445 } else {
2446 /* decode repeat length */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002447 int repeat = ch - ' ' + 3;
Doug Gale4bf43122017-05-01 12:22:10 -04002448 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
2449 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05002450 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002451 s->state = RS_IDLE;
2452 } else if (s->line_buf_index < 1) {
2453 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05002454 trace_gdbstub_err_invalid_rle();
Doug Gale4bf43122017-05-01 12:22:10 -04002455 s->state = RS_GETLINE;
2456 } else {
2457 /* repeat the last character */
2458 memset(s->line_buf + s->line_buf_index,
2459 s->line_buf[s->line_buf_index - 1], repeat);
2460 s->line_buf_index += repeat;
2461 s->line_sum += ch;
2462 s->state = RS_GETLINE;
2463 }
bellard858693c2004-03-31 18:52:07 +00002464 }
2465 break;
2466 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04002467 /* get high hex digit of checksum */
2468 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002469 trace_gdbstub_err_checksum_invalid(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002470 s->state = RS_GETLINE;
2471 break;
2472 }
bellard858693c2004-03-31 18:52:07 +00002473 s->line_buf[s->line_buf_index] = '\0';
2474 s->line_csum = fromhex(ch) << 4;
2475 s->state = RS_CHKSUM2;
2476 break;
2477 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04002478 /* get low hex digit of checksum */
2479 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002480 trace_gdbstub_err_checksum_invalid(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002481 s->state = RS_GETLINE;
2482 break;
bellard858693c2004-03-31 18:52:07 +00002483 }
Doug Gale4bf43122017-05-01 12:22:10 -04002484 s->line_csum |= fromhex(ch);
2485
2486 if (s->line_csum != (s->line_sum & 0xff)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002487 trace_gdbstub_err_checksum_incorrect(s->line_sum, s->line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04002488 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00002489 reply = '-';
2490 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00002491 s->state = RS_IDLE;
2492 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002493 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00002494 reply = '+';
2495 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00002496 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00002497 }
bellardb4608c02003-06-27 17:34:32 +00002498 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002499 default:
2500 abort();
bellardb4608c02003-06-27 17:34:32 +00002501 }
2502 }
bellard858693c2004-03-31 18:52:07 +00002503}
2504
Paul Brook0e1c9c52010-06-16 13:03:51 +01002505/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002506void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01002507{
2508 GDBState *s;
2509 char buf[4];
2510
2511 s = gdbserver_state;
2512 if (!s) {
2513 return;
2514 }
2515#ifdef CONFIG_USER_ONLY
2516 if (gdbserver_fd < 0 || s->fd < 0) {
2517 return;
2518 }
2519#endif
2520
Doug Gale5c9522b2017-12-02 20:30:37 -05002521 trace_gdbstub_op_exiting((uint8_t)code);
2522
Paul Brook0e1c9c52010-06-16 13:03:51 +01002523 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
2524 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002525
2526#ifndef CONFIG_USER_ONLY
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002527 qemu_chr_fe_deinit(&s->chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002528#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01002529}
2530
Luc Michel8f468632019-01-07 15:23:45 +00002531/*
2532 * Create the process that will contain all the "orphan" CPUs (that are not
2533 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2534 * be attachable and thus will be invisible to the user.
2535 */
2536static void create_default_process(GDBState *s)
2537{
2538 GDBProcess *process;
2539 int max_pid = 0;
2540
2541 if (s->process_num) {
2542 max_pid = s->processes[s->process_num - 1].pid;
2543 }
2544
2545 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2546 process = &s->processes[s->process_num - 1];
2547
2548 /* We need an available PID slot for this process */
2549 assert(max_pid < UINT32_MAX);
2550
2551 process->pid = max_pid + 1;
2552 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00002553 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00002554}
2555
bellard1fddef42005-04-17 19:16:13 +00002556#ifdef CONFIG_USER_ONLY
2557int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02002558gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00002559{
Andreas Färber5ca666c2013-06-24 19:20:57 +02002560 GDBState *s;
2561 char buf[256];
2562 int n;
bellard1fddef42005-04-17 19:16:13 +00002563
Andreas Färber5ca666c2013-06-24 19:20:57 +02002564 s = gdbserver_state;
2565 if (gdbserver_fd < 0 || s->fd < 0) {
2566 return sig;
bellard1fddef42005-04-17 19:16:13 +00002567 }
2568
Andreas Färber5ca666c2013-06-24 19:20:57 +02002569 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02002570 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07002571 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00002572
Andreas Färber5ca666c2013-06-24 19:20:57 +02002573 if (sig != 0) {
2574 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
2575 put_packet(s, buf);
2576 }
2577 /* put_packet() might have detected that the peer terminated the
2578 connection. */
2579 if (s->fd < 0) {
2580 return sig;
2581 }
2582
2583 sig = 0;
2584 s->state = RS_IDLE;
2585 s->running_state = 0;
2586 while (s->running_state == 0) {
2587 n = read(s->fd, buf, 256);
2588 if (n > 0) {
2589 int i;
2590
2591 for (i = 0; i < n; i++) {
2592 gdb_read_byte(s, buf[i]);
2593 }
Peter Wu5819e3e2016-06-05 16:35:48 +02002594 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02002595 /* XXX: Connection closed. Should probably wait for another
2596 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02002597 if (n == 0) {
2598 close(s->fd);
2599 }
2600 s->fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02002601 return sig;
bellard1fddef42005-04-17 19:16:13 +00002602 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02002603 }
2604 sig = s->signal;
2605 s->signal = 0;
2606 return sig;
bellard1fddef42005-04-17 19:16:13 +00002607}
bellarde9009672005-04-26 20:42:36 +00002608
aurel32ca587a82008-12-18 22:44:13 +00002609/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002610void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00002611{
Andreas Färber5ca666c2013-06-24 19:20:57 +02002612 GDBState *s;
2613 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00002614
Andreas Färber5ca666c2013-06-24 19:20:57 +02002615 s = gdbserver_state;
2616 if (gdbserver_fd < 0 || s->fd < 0) {
2617 return;
2618 }
aurel32ca587a82008-12-18 22:44:13 +00002619
Andreas Färber5ca666c2013-06-24 19:20:57 +02002620 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
2621 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00002622}
bellard1fddef42005-04-17 19:16:13 +00002623
Peter Maydell2f652222018-05-14 18:30:44 +01002624static bool gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00002625{
2626 GDBState *s;
2627 struct sockaddr_in sockaddr;
2628 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09002629 int fd;
bellard858693c2004-03-31 18:52:07 +00002630
2631 for(;;) {
2632 len = sizeof(sockaddr);
2633 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
2634 if (fd < 0 && errno != EINTR) {
2635 perror("accept");
Peter Maydell2f652222018-05-14 18:30:44 +01002636 return false;
bellard858693c2004-03-31 18:52:07 +00002637 } else if (fd >= 0) {
Peter Maydellf5bdd782018-05-14 18:30:43 +01002638 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00002639 break;
2640 }
2641 }
2642
2643 /* set short latency */
Peter Maydell2f652222018-05-14 18:30:44 +01002644 if (socket_set_nodelay(fd)) {
2645 perror("setsockopt");
Philippe Mathieu-Daudéead75d82018-05-24 19:34:58 -03002646 close(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01002647 return false;
2648 }
ths3b46e622007-09-17 08:09:54 +00002649
Anthony Liguori7267c092011-08-20 22:09:37 -05002650 s = g_malloc0(sizeof(GDBState));
Luc Michel8f468632019-01-07 15:23:45 +00002651 create_default_process(s);
Luc Michel970ed902019-01-07 15:23:46 +00002652 s->processes[0].attached = true;
2653 s->c_cpu = gdb_first_attached_cpu(s);
2654 s->g_cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00002655 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02002656 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00002657
aliguori880a7572008-11-18 20:30:24 +00002658 gdbserver_state = s;
Peter Maydell2f652222018-05-14 18:30:44 +01002659 return true;
bellard858693c2004-03-31 18:52:07 +00002660}
2661
2662static int gdbserver_open(int port)
2663{
2664 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02002665 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00002666
2667 fd = socket(PF_INET, SOCK_STREAM, 0);
2668 if (fd < 0) {
2669 perror("socket");
2670 return -1;
2671 }
Peter Maydellf5bdd782018-05-14 18:30:43 +01002672 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00002673
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02002674 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00002675
2676 sockaddr.sin_family = AF_INET;
2677 sockaddr.sin_port = htons(port);
2678 sockaddr.sin_addr.s_addr = 0;
2679 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
2680 if (ret < 0) {
2681 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00002682 close(fd);
bellard858693c2004-03-31 18:52:07 +00002683 return -1;
2684 }
Peter Wu96165b92016-05-04 11:32:17 +02002685 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00002686 if (ret < 0) {
2687 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00002688 close(fd);
bellard858693c2004-03-31 18:52:07 +00002689 return -1;
2690 }
bellard858693c2004-03-31 18:52:07 +00002691 return fd;
2692}
2693
2694int gdbserver_start(int port)
2695{
2696 gdbserver_fd = gdbserver_open(port);
2697 if (gdbserver_fd < 0)
2698 return -1;
2699 /* accept connections */
Peter Maydell2f652222018-05-14 18:30:44 +01002700 if (!gdb_accept()) {
2701 close(gdbserver_fd);
2702 gdbserver_fd = -1;
2703 return -1;
2704 }
bellardb4608c02003-06-27 17:34:32 +00002705 return 0;
2706}
aurel322b1319c2008-12-18 22:44:04 +00002707
2708/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07002709void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00002710{
2711 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02002712
2713 if (gdbserver_fd < 0 || s->fd < 0) {
2714 return;
2715 }
aurel322b1319c2008-12-18 22:44:04 +00002716 close(s->fd);
2717 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02002718 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02002719 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00002720}
pbrook4046d912007-01-28 01:53:16 +00002721#else
thsaa1f17c2007-07-11 22:48:58 +00002722static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00002723{
pbrook56aebc82008-10-11 17:55:29 +00002724 /* We can handle an arbitrarily large amount of data.
2725 Pick the maximum packet size, which is as good as anything. */
2726 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00002727}
2728
thsaa1f17c2007-07-11 22:48:58 +00002729static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00002730{
pbrook4046d912007-01-28 01:53:16 +00002731 int i;
2732
2733 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00002734 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00002735 }
2736}
2737
2738static void gdb_chr_event(void *opaque, int event)
2739{
Luc Michel970ed902019-01-07 15:23:46 +00002740 int i;
2741 GDBState *s = (GDBState *) opaque;
2742
pbrook4046d912007-01-28 01:53:16 +00002743 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05302744 case CHR_EVENT_OPENED:
Luc Michel970ed902019-01-07 15:23:46 +00002745 /* Start with first process attached, others detached */
2746 for (i = 0; i < s->process_num; i++) {
2747 s->processes[i].attached = !i;
2748 }
2749
2750 s->c_cpu = gdb_first_attached_cpu(s);
2751 s->g_cpu = s->c_cpu;
2752
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002753 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02002754 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00002755 break;
2756 default:
2757 break;
2758 }
2759}
2760
aliguori8a34a0f2009-03-05 23:01:55 +00002761static void gdb_monitor_output(GDBState *s, const char *msg, int len)
2762{
2763 char buf[MAX_PACKET_LENGTH];
2764
2765 buf[0] = 'O';
2766 if (len > (MAX_PACKET_LENGTH/2) - 1)
2767 len = (MAX_PACKET_LENGTH/2) - 1;
2768 memtohex(buf + 1, (uint8_t *)msg, len);
2769 put_packet(s, buf);
2770}
2771
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03002772static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00002773{
2774 const char *p = (const char *)buf;
2775 int max_sz;
2776
2777 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
2778 for (;;) {
2779 if (len <= max_sz) {
2780 gdb_monitor_output(gdbserver_state, p, len);
2781 break;
2782 }
2783 gdb_monitor_output(gdbserver_state, p, max_sz);
2784 p += max_sz;
2785 len -= max_sz;
2786 }
2787 return len;
2788}
2789
aliguori59030a82009-04-05 18:43:41 +00002790#ifndef _WIN32
2791static void gdb_sigterm_handler(int signal)
2792{
Luiz Capitulino13548692011-07-29 15:36:43 -03002793 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002794 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01002795 }
aliguori59030a82009-04-05 18:43:41 +00002796}
2797#endif
2798
Marc-André Lureau777357d2016-12-07 18:39:10 +03002799static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
2800 bool *be_opened, Error **errp)
2801{
2802 *be_opened = false;
2803}
2804
2805static void char_gdb_class_init(ObjectClass *oc, void *data)
2806{
2807 ChardevClass *cc = CHARDEV_CLASS(oc);
2808
2809 cc->internal = true;
2810 cc->open = gdb_monitor_open;
2811 cc->chr_write = gdb_monitor_write;
2812}
2813
2814#define TYPE_CHARDEV_GDB "chardev-gdb"
2815
2816static const TypeInfo char_gdb_type_info = {
2817 .name = TYPE_CHARDEV_GDB,
2818 .parent = TYPE_CHARDEV,
2819 .class_init = char_gdb_class_init,
2820};
2821
Luc Michel8f468632019-01-07 15:23:45 +00002822static int find_cpu_clusters(Object *child, void *opaque)
2823{
2824 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
2825 GDBState *s = (GDBState *) opaque;
2826 CPUClusterState *cluster = CPU_CLUSTER(child);
2827 GDBProcess *process;
2828
2829 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2830
2831 process = &s->processes[s->process_num - 1];
2832
2833 /*
2834 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
2835 * runtime, we enforce here that the machine does not use a cluster ID
2836 * that would lead to PID 0.
2837 */
2838 assert(cluster->cluster_id != UINT32_MAX);
2839 process->pid = cluster->cluster_id + 1;
2840 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00002841 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00002842
2843 return 0;
2844 }
2845
2846 return object_child_foreach(child, find_cpu_clusters, opaque);
2847}
2848
2849static int pid_order(const void *a, const void *b)
2850{
2851 GDBProcess *pa = (GDBProcess *) a;
2852 GDBProcess *pb = (GDBProcess *) b;
2853
2854 if (pa->pid < pb->pid) {
2855 return -1;
2856 } else if (pa->pid > pb->pid) {
2857 return 1;
2858 } else {
2859 return 0;
2860 }
2861}
2862
2863static void create_processes(GDBState *s)
2864{
2865 object_child_foreach(object_get_root(), find_cpu_clusters, s);
2866
2867 if (s->processes) {
2868 /* Sort by PID */
2869 qsort(s->processes, s->process_num, sizeof(s->processes[0]), pid_order);
2870 }
2871
2872 create_default_process(s);
2873}
2874
2875static void cleanup_processes(GDBState *s)
2876{
2877 g_free(s->processes);
2878 s->process_num = 0;
2879 s->processes = NULL;
2880}
2881
aliguori59030a82009-04-05 18:43:41 +00002882int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00002883{
Doug Gale5c9522b2017-12-02 20:30:37 -05002884 trace_gdbstub_op_start(device);
2885
pbrook4046d912007-01-28 01:53:16 +00002886 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00002887 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03002888 Chardev *chr = NULL;
2889 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00002890
Ziyue Yang508b4ec2017-01-18 16:02:41 +08002891 if (!first_cpu) {
2892 error_report("gdbstub: meaningless to attach gdb to a "
2893 "machine without any CPU.");
2894 return -1;
2895 }
2896
aliguori59030a82009-04-05 18:43:41 +00002897 if (!device)
2898 return -1;
2899 if (strcmp(device, "none") != 0) {
2900 if (strstart(device, "tcp:", NULL)) {
2901 /* enforce required TCP attributes */
2902 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
2903 "%s,nowait,nodelay,server", device);
2904 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00002905 }
aliguori59030a82009-04-05 18:43:41 +00002906#ifndef _WIN32
2907 else if (strcmp(device, "stdio") == 0) {
2908 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00002909
aliguori59030a82009-04-05 18:43:41 +00002910 memset(&act, 0, sizeof(act));
2911 act.sa_handler = gdb_sigterm_handler;
2912 sigaction(SIGINT, &act, NULL);
2913 }
2914#endif
Marc-André Lureau95e30b22018-08-22 19:19:42 +02002915 /*
2916 * FIXME: it's a bit weird to allow using a mux chardev here
2917 * and implicitly setup a monitor. We may want to break this.
2918 */
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01002919 chr = qemu_chr_new_noreplay("gdb", device, true, NULL);
aliguori36556b22009-03-28 18:05:53 +00002920 if (!chr)
2921 return -1;
pbrookcfc34752007-02-22 01:48:01 +00002922 }
2923
aliguori36556b22009-03-28 18:05:53 +00002924 s = gdbserver_state;
2925 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05002926 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00002927 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00002928
aliguori36556b22009-03-28 18:05:53 +00002929 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
2930
2931 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03002932 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01002933 NULL, NULL, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00002934 monitor_init(mon_chr, 0);
2935 } else {
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002936 qemu_chr_fe_deinit(&s->chr, true);
aliguori36556b22009-03-28 18:05:53 +00002937 mon_chr = s->mon_chr;
Luc Michel8f468632019-01-07 15:23:45 +00002938 cleanup_processes(s);
aliguori36556b22009-03-28 18:05:53 +00002939 memset(s, 0, sizeof(GDBState));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002940 s->mon_chr = mon_chr;
aliguori36556b22009-03-28 18:05:53 +00002941 }
Luc Michel8f468632019-01-07 15:23:45 +00002942
2943 create_processes(s);
2944
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002945 if (chr) {
2946 qemu_chr_fe_init(&s->chr, chr, &error_abort);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002947 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
Luc Michel970ed902019-01-07 15:23:46 +00002948 gdb_chr_event, NULL, s, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002949 }
aliguori36556b22009-03-28 18:05:53 +00002950 s->state = chr ? RS_IDLE : RS_INACTIVE;
2951 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00002952 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00002953
pbrook4046d912007-01-28 01:53:16 +00002954 return 0;
2955}
Marc-André Lureau777357d2016-12-07 18:39:10 +03002956
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01002957void gdbserver_cleanup(void)
2958{
2959 if (gdbserver_state) {
2960 put_packet(gdbserver_state, "W00");
2961 }
2962}
2963
Marc-André Lureau777357d2016-12-07 18:39:10 +03002964static void register_types(void)
2965{
2966 type_register_static(&char_gdb_type_info);
2967}
2968
2969type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00002970#endif