blob: 687c02e598a81ee3040ceb4e74e8a0dc4fbc9c6f [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"
Like Xu5cc87672019-05-19 04:54:21 +080037#include "hw/boards.h"
bellard1fddef42005-04-17 19:16:13 +000038#endif
bellard67b915a2004-03-31 23:37:16 +000039
pbrook56aebc82008-10-11 17:55:29 +000040#define MAX_PACKET_LENGTH 4096
41
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010042#include "qemu/sockets.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010043#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010044#include "sysemu/kvm.h"
Alex Bennéef1672e62019-05-13 14:43:57 +010045#include "hw/semihosting/semihost.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010046#include "exec/exec-all.h"
aurel32ca587a82008-12-18 22:44:13 +000047
Jan Kiszkaa3919382015-02-07 09:38:44 +010048#ifdef CONFIG_USER_ONLY
49#define GDB_ATTACHED "0"
50#else
51#define GDB_ATTACHED "1"
52#endif
53
Jon Doronab4752e2019-05-29 09:41:48 +030054#ifndef CONFIG_USER_ONLY
55static int phy_memory_mode;
56#endif
57
Andreas Färberf3659ee2013-06-27 19:09:09 +020058static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
59 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020060{
Jon Doronab4752e2019-05-29 09:41:48 +030061 CPUClass *cc;
Andreas Färberf3659ee2013-06-27 19:09:09 +020062
Jon Doronab4752e2019-05-29 09:41:48 +030063#ifndef CONFIG_USER_ONLY
64 if (phy_memory_mode) {
65 if (is_write) {
66 cpu_physical_memory_write(addr, buf, len);
67 } else {
68 cpu_physical_memory_read(addr, buf, len);
69 }
70 return 0;
71 }
72#endif
73
74 cc = CPU_GET_CLASS(cpu);
Andreas Färberf3659ee2013-06-27 19:09:09 +020075 if (cc->memory_rw_debug) {
76 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
77 }
78 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020079}
aurel32ca587a82008-12-18 22:44:13 +000080
Alex Bennéed2a6c852017-07-12 11:52:14 +010081/* Return the GDB index for a given vCPU state.
82 *
83 * For user mode this is simply the thread id. In system mode GDB
84 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
85 */
86static inline int cpu_gdb_index(CPUState *cpu)
87{
88#if defined(CONFIG_USER_ONLY)
Alex Bennéebd88c782017-07-12 11:52:15 +010089 TaskState *ts = (TaskState *) cpu->opaque;
90 return ts->ts_tid;
Alex Bennéed2a6c852017-07-12 11:52:14 +010091#else
92 return cpu->cpu_index + 1;
93#endif
94}
95
aurel32ca587a82008-12-18 22:44:13 +000096enum {
97 GDB_SIGNAL_0 = 0,
98 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +010099 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +0000100 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +0100101 GDB_SIGNAL_ABRT = 6,
102 GDB_SIGNAL_ALRM = 14,
103 GDB_SIGNAL_IO = 23,
104 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +0000105 GDB_SIGNAL_UNKNOWN = 143
106};
107
108#ifdef CONFIG_USER_ONLY
109
110/* Map target signal numbers to GDB protocol signal numbers and vice
111 * versa. For user emulation's currently supported systems, we can
112 * assume most signals are defined.
113 */
114
115static int gdb_signal_table[] = {
116 0,
117 TARGET_SIGHUP,
118 TARGET_SIGINT,
119 TARGET_SIGQUIT,
120 TARGET_SIGILL,
121 TARGET_SIGTRAP,
122 TARGET_SIGABRT,
123 -1, /* SIGEMT */
124 TARGET_SIGFPE,
125 TARGET_SIGKILL,
126 TARGET_SIGBUS,
127 TARGET_SIGSEGV,
128 TARGET_SIGSYS,
129 TARGET_SIGPIPE,
130 TARGET_SIGALRM,
131 TARGET_SIGTERM,
132 TARGET_SIGURG,
133 TARGET_SIGSTOP,
134 TARGET_SIGTSTP,
135 TARGET_SIGCONT,
136 TARGET_SIGCHLD,
137 TARGET_SIGTTIN,
138 TARGET_SIGTTOU,
139 TARGET_SIGIO,
140 TARGET_SIGXCPU,
141 TARGET_SIGXFSZ,
142 TARGET_SIGVTALRM,
143 TARGET_SIGPROF,
144 TARGET_SIGWINCH,
145 -1, /* SIGLOST */
146 TARGET_SIGUSR1,
147 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000148#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000149 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000150#else
151 -1,
152#endif
aurel32ca587a82008-12-18 22:44:13 +0000153 -1, /* SIGPOLL */
154 -1,
155 -1,
156 -1,
157 -1,
158 -1,
159 -1,
160 -1,
161 -1,
162 -1,
163 -1,
164 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000165#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000166 __SIGRTMIN + 1,
167 __SIGRTMIN + 2,
168 __SIGRTMIN + 3,
169 __SIGRTMIN + 4,
170 __SIGRTMIN + 5,
171 __SIGRTMIN + 6,
172 __SIGRTMIN + 7,
173 __SIGRTMIN + 8,
174 __SIGRTMIN + 9,
175 __SIGRTMIN + 10,
176 __SIGRTMIN + 11,
177 __SIGRTMIN + 12,
178 __SIGRTMIN + 13,
179 __SIGRTMIN + 14,
180 __SIGRTMIN + 15,
181 __SIGRTMIN + 16,
182 __SIGRTMIN + 17,
183 __SIGRTMIN + 18,
184 __SIGRTMIN + 19,
185 __SIGRTMIN + 20,
186 __SIGRTMIN + 21,
187 __SIGRTMIN + 22,
188 __SIGRTMIN + 23,
189 __SIGRTMIN + 24,
190 __SIGRTMIN + 25,
191 __SIGRTMIN + 26,
192 __SIGRTMIN + 27,
193 __SIGRTMIN + 28,
194 __SIGRTMIN + 29,
195 __SIGRTMIN + 30,
196 __SIGRTMIN + 31,
197 -1, /* SIGCANCEL */
198 __SIGRTMIN,
199 __SIGRTMIN + 32,
200 __SIGRTMIN + 33,
201 __SIGRTMIN + 34,
202 __SIGRTMIN + 35,
203 __SIGRTMIN + 36,
204 __SIGRTMIN + 37,
205 __SIGRTMIN + 38,
206 __SIGRTMIN + 39,
207 __SIGRTMIN + 40,
208 __SIGRTMIN + 41,
209 __SIGRTMIN + 42,
210 __SIGRTMIN + 43,
211 __SIGRTMIN + 44,
212 __SIGRTMIN + 45,
213 __SIGRTMIN + 46,
214 __SIGRTMIN + 47,
215 __SIGRTMIN + 48,
216 __SIGRTMIN + 49,
217 __SIGRTMIN + 50,
218 __SIGRTMIN + 51,
219 __SIGRTMIN + 52,
220 __SIGRTMIN + 53,
221 __SIGRTMIN + 54,
222 __SIGRTMIN + 55,
223 __SIGRTMIN + 56,
224 __SIGRTMIN + 57,
225 __SIGRTMIN + 58,
226 __SIGRTMIN + 59,
227 __SIGRTMIN + 60,
228 __SIGRTMIN + 61,
229 __SIGRTMIN + 62,
230 __SIGRTMIN + 63,
231 __SIGRTMIN + 64,
232 __SIGRTMIN + 65,
233 __SIGRTMIN + 66,
234 __SIGRTMIN + 67,
235 __SIGRTMIN + 68,
236 __SIGRTMIN + 69,
237 __SIGRTMIN + 70,
238 __SIGRTMIN + 71,
239 __SIGRTMIN + 72,
240 __SIGRTMIN + 73,
241 __SIGRTMIN + 74,
242 __SIGRTMIN + 75,
243 __SIGRTMIN + 76,
244 __SIGRTMIN + 77,
245 __SIGRTMIN + 78,
246 __SIGRTMIN + 79,
247 __SIGRTMIN + 80,
248 __SIGRTMIN + 81,
249 __SIGRTMIN + 82,
250 __SIGRTMIN + 83,
251 __SIGRTMIN + 84,
252 __SIGRTMIN + 85,
253 __SIGRTMIN + 86,
254 __SIGRTMIN + 87,
255 __SIGRTMIN + 88,
256 __SIGRTMIN + 89,
257 __SIGRTMIN + 90,
258 __SIGRTMIN + 91,
259 __SIGRTMIN + 92,
260 __SIGRTMIN + 93,
261 __SIGRTMIN + 94,
262 __SIGRTMIN + 95,
263 -1, /* SIGINFO */
264 -1, /* UNKNOWN */
265 -1, /* DEFAULT */
266 -1,
267 -1,
268 -1,
269 -1,
270 -1,
271 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000272#endif
aurel32ca587a82008-12-18 22:44:13 +0000273};
bellard8f447cc2006-06-14 15:21:14 +0000274#else
aurel32ca587a82008-12-18 22:44:13 +0000275/* In system mode we only need SIGINT and SIGTRAP; other signals
276 are not yet supported. */
277
278enum {
279 TARGET_SIGINT = 2,
280 TARGET_SIGTRAP = 5
281};
282
283static int gdb_signal_table[] = {
284 -1,
285 -1,
286 TARGET_SIGINT,
287 -1,
288 -1,
289 TARGET_SIGTRAP
290};
bellard8f447cc2006-06-14 15:21:14 +0000291#endif
bellardb4608c02003-06-27 17:34:32 +0000292
aurel32ca587a82008-12-18 22:44:13 +0000293#ifdef CONFIG_USER_ONLY
294static int target_signal_to_gdb (int sig)
295{
296 int i;
297 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
298 if (gdb_signal_table[i] == sig)
299 return i;
300 return GDB_SIGNAL_UNKNOWN;
301}
302#endif
303
304static int gdb_signal_to_target (int sig)
305{
306 if (sig < ARRAY_SIZE (gdb_signal_table))
307 return gdb_signal_table[sig];
308 else
309 return -1;
310}
311
pbrook56aebc82008-10-11 17:55:29 +0000312typedef struct GDBRegisterState {
313 int base_reg;
314 int num_regs;
315 gdb_reg_cb get_reg;
316 gdb_reg_cb set_reg;
317 const char *xml;
318 struct GDBRegisterState *next;
319} GDBRegisterState;
320
Luc Michel8f468632019-01-07 15:23:45 +0000321typedef struct GDBProcess {
322 uint32_t pid;
323 bool attached;
Luc Michelc145eea2019-01-07 15:23:46 +0000324
325 char target_xml[1024];
Luc Michel8f468632019-01-07 15:23:45 +0000326} GDBProcess;
327
bellard858693c2004-03-31 18:52:07 +0000328enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000329 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000330 RS_IDLE,
331 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400332 RS_GETLINE_ESC,
333 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000334 RS_CHKSUM1,
335 RS_CHKSUM2,
336};
bellard858693c2004-03-31 18:52:07 +0000337typedef struct GDBState {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200338 CPUState *c_cpu; /* current CPU for step/continue ops */
339 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200340 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000341 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000342 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000343 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400344 int line_sum; /* running checksum */
345 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000346 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000347 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000348 int signal;
bellard41625032005-04-24 10:07:11 +0000349#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000350 int fd;
bellard41625032005-04-24 10:07:11 +0000351 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000352#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300353 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300354 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000355#endif
Luc Michel8f468632019-01-07 15:23:45 +0000356 bool multiprocess;
357 GDBProcess *processes;
358 int process_num;
Meador Ingecdb432b2012-03-15 17:49:45 +0000359 char syscall_buf[256];
360 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000361} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000362
edgar_igl60897d32008-05-09 08:25:14 +0000363/* By default use no IRQs and no timers while single stepping so as to
364 * make single stepping like an ICE HW step.
365 */
366static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
367
aliguori880a7572008-11-18 20:30:24 +0000368static GDBState *gdbserver_state;
369
Andreas Färber5b50e792013-06-29 04:18:45 +0200370bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000371
bellard1fddef42005-04-17 19:16:13 +0000372#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000373/* XXX: This is not thread safe. Do we care? */
374static int gdbserver_fd = -1;
375
bellard858693c2004-03-31 18:52:07 +0000376static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000377{
378 uint8_t ch;
379 int ret;
380
381 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000382 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000383 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000384 if (errno == ECONNRESET)
385 s->fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200386 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000387 return -1;
388 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000389 close(s->fd);
390 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000391 return -1;
392 } else {
393 break;
394 }
395 }
396 return ch;
397}
pbrook4046d912007-01-28 01:53:16 +0000398#endif
bellardb4608c02003-06-27 17:34:32 +0000399
blueswir1654efcf2009-04-18 07:29:59 +0000400static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000401 GDB_SYS_UNKNOWN,
402 GDB_SYS_ENABLED,
403 GDB_SYS_DISABLED,
404} gdb_syscall_mode;
405
Liviu Ionescua38bb072014-12-11 12:07:48 +0000406/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000407int use_gdb_syscalls(void)
408{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100409 SemihostingTarget target = semihosting_get_target();
410 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000411 /* -semihosting-config target=native */
412 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100413 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000414 /* -semihosting-config target=gdb */
415 return true;
416 }
417
418 /* -semihosting-config target=auto */
419 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000420 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000421 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
422 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000423 }
424 return gdb_syscall_mode == GDB_SYS_ENABLED;
425}
426
edgar_iglba70a622008-03-14 06:10:42 +0000427/* Resume execution. */
428static inline void gdb_continue(GDBState *s)
429{
Doug Gale5c9522b2017-12-02 20:30:37 -0500430
edgar_iglba70a622008-03-14 06:10:42 +0000431#ifdef CONFIG_USER_ONLY
432 s->running_state = 1;
Doug Gale5c9522b2017-12-02 20:30:37 -0500433 trace_gdbstub_op_continue();
edgar_iglba70a622008-03-14 06:10:42 +0000434#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200435 if (!runstate_needs_reset()) {
Doug Gale5c9522b2017-12-02 20:30:37 -0500436 trace_gdbstub_op_continue();
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200437 vm_start();
438 }
edgar_iglba70a622008-03-14 06:10:42 +0000439#endif
440}
441
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100442/*
443 * Resume execution, per CPU actions. For user-mode emulation it's
444 * equivalent to gdb_continue.
445 */
446static int gdb_continue_partial(GDBState *s, char *newstates)
447{
448 CPUState *cpu;
449 int res = 0;
450#ifdef CONFIG_USER_ONLY
451 /*
452 * This is not exactly accurate, but it's an improvement compared to the
453 * previous situation, where only one CPU would be single-stepped.
454 */
455 CPU_FOREACH(cpu) {
456 if (newstates[cpu->cpu_index] == 's') {
Doug Gale5c9522b2017-12-02 20:30:37 -0500457 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100458 cpu_single_step(cpu, sstep_flags);
459 }
460 }
461 s->running_state = 1;
462#else
463 int flag = 0;
464
465 if (!runstate_needs_reset()) {
466 if (vm_prepare_start()) {
467 return 0;
468 }
469
470 CPU_FOREACH(cpu) {
471 switch (newstates[cpu->cpu_index]) {
472 case 0:
473 case 1:
474 break; /* nothing to do here */
475 case 's':
Doug Gale5c9522b2017-12-02 20:30:37 -0500476 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100477 cpu_single_step(cpu, sstep_flags);
478 cpu_resume(cpu);
479 flag = 1;
480 break;
481 case 'c':
Doug Gale5c9522b2017-12-02 20:30:37 -0500482 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100483 cpu_resume(cpu);
484 flag = 1;
485 break;
486 default:
487 res = -1;
488 break;
489 }
490 }
491 }
492 if (flag) {
493 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
494 }
495#endif
496 return res;
497}
498
bellard858693c2004-03-31 18:52:07 +0000499static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000500{
pbrook4046d912007-01-28 01:53:16 +0000501#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000502 int ret;
503
504 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000505 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000506 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200507 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000508 return;
509 } else {
510 buf += ret;
511 len -= ret;
512 }
513 }
pbrook4046d912007-01-28 01:53:16 +0000514#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100515 /* XXX this blocks entire thread. Rewrite to use
516 * qemu_chr_fe_write and background I/O callbacks */
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300517 qemu_chr_fe_write_all(&s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000518#endif
bellardb4608c02003-06-27 17:34:32 +0000519}
520
521static inline int fromhex(int v)
522{
523 if (v >= '0' && v <= '9')
524 return v - '0';
525 else if (v >= 'A' && v <= 'F')
526 return v - 'A' + 10;
527 else if (v >= 'a' && v <= 'f')
528 return v - 'a' + 10;
529 else
530 return 0;
531}
532
533static inline int tohex(int v)
534{
535 if (v < 10)
536 return v + '0';
537 else
538 return v - 10 + 'a';
539}
540
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -0300541/* writes 2*len+1 bytes in buf */
bellardb4608c02003-06-27 17:34:32 +0000542static void memtohex(char *buf, const uint8_t *mem, int len)
543{
544 int i, c;
545 char *q;
546 q = buf;
547 for(i = 0; i < len; i++) {
548 c = mem[i];
549 *q++ = tohex(c >> 4);
550 *q++ = tohex(c & 0xf);
551 }
552 *q = '\0';
553}
554
555static void hextomem(uint8_t *mem, const char *buf, int len)
556{
557 int i;
558
559 for(i = 0; i < len; i++) {
560 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
561 buf += 2;
562 }
563}
564
Doug Gale5c9522b2017-12-02 20:30:37 -0500565static void hexdump(const char *buf, int len,
566 void (*trace_fn)(size_t ofs, char const *text))
567{
568 char line_buffer[3 * 16 + 4 + 16 + 1];
569
570 size_t i;
571 for (i = 0; i < len || (i & 0xF); ++i) {
572 size_t byte_ofs = i & 15;
573
574 if (byte_ofs == 0) {
575 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
576 line_buffer[3 * 16 + 4 + 16] = 0;
577 }
578
579 size_t col_group = (i >> 2) & 3;
580 size_t hex_col = byte_ofs * 3 + col_group;
581 size_t txt_col = 3 * 16 + 4 + byte_ofs;
582
583 if (i < len) {
584 char value = buf[i];
585
586 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
587 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
588 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
589 ? value
590 : '.';
591 }
592
593 if (byte_ofs == 0xF)
594 trace_fn(i & -16, line_buffer);
595 }
596}
597
bellardb4608c02003-06-27 17:34:32 +0000598/* return -1 if error, 0 if OK */
Doug Gale5c9522b2017-12-02 20:30:37 -0500599static int put_packet_binary(GDBState *s, const char *buf, int len, bool dump)
bellardb4608c02003-06-27 17:34:32 +0000600{
pbrook56aebc82008-10-11 17:55:29 +0000601 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000602 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000603
Doug Gale5c9522b2017-12-02 20:30:37 -0500604 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
605 hexdump(buf, len, trace_gdbstub_io_binaryreply);
606 }
607
bellardb4608c02003-06-27 17:34:32 +0000608 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000609 p = s->last_packet;
610 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000611 memcpy(p, buf, len);
612 p += len;
bellardb4608c02003-06-27 17:34:32 +0000613 csum = 0;
614 for(i = 0; i < len; i++) {
615 csum += buf[i];
616 }
pbrook4046d912007-01-28 01:53:16 +0000617 *(p++) = '#';
618 *(p++) = tohex((csum >> 4) & 0xf);
619 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000620
pbrook4046d912007-01-28 01:53:16 +0000621 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000622 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000623
pbrook4046d912007-01-28 01:53:16 +0000624#ifdef CONFIG_USER_ONLY
625 i = get_char(s);
626 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000627 return -1;
pbrook4046d912007-01-28 01:53:16 +0000628 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000629 break;
pbrook4046d912007-01-28 01:53:16 +0000630#else
631 break;
632#endif
bellardb4608c02003-06-27 17:34:32 +0000633 }
634 return 0;
635}
636
pbrook56aebc82008-10-11 17:55:29 +0000637/* return -1 if error, 0 if OK */
638static int put_packet(GDBState *s, const char *buf)
639{
Doug Gale5c9522b2017-12-02 20:30:37 -0500640 trace_gdbstub_io_reply(buf);
pbrook56aebc82008-10-11 17:55:29 +0000641
Doug Gale5c9522b2017-12-02 20:30:37 -0500642 return put_packet_binary(s, buf, strlen(buf), false);
pbrook56aebc82008-10-11 17:55:29 +0000643}
644
pbrook56aebc82008-10-11 17:55:29 +0000645/* Encode data using the encoding for 'x' packets. */
646static int memtox(char *buf, const char *mem, int len)
647{
648 char *p = buf;
649 char c;
650
651 while (len--) {
652 c = *(mem++);
653 switch (c) {
654 case '#': case '$': case '*': case '}':
655 *(p++) = '}';
656 *(p++) = c ^ 0x20;
657 break;
658 default:
659 *(p++) = c;
660 break;
661 }
662 }
663 return p - buf;
664}
665
Luc Michel1a227332019-01-07 15:23:45 +0000666static uint32_t gdb_get_cpu_pid(const GDBState *s, CPUState *cpu)
667{
Luc Michel1a227332019-01-07 15:23:45 +0000668 /* TODO: In user mode, we should use the task state PID */
Peter Maydell46f5abc2019-01-29 11:46:06 +0000669 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
670 /* Return the default process' PID */
671 return s->processes[s->process_num - 1].pid;
672 }
673 return cpu->cluster_index + 1;
Luc Michel1a227332019-01-07 15:23:45 +0000674}
675
Luc Michel7d8c87d2019-01-07 15:23:45 +0000676static GDBProcess *gdb_get_process(const GDBState *s, uint32_t pid)
677{
678 int i;
679
680 if (!pid) {
681 /* 0 means any process, we take the first one */
682 return &s->processes[0];
683 }
684
685 for (i = 0; i < s->process_num; i++) {
686 if (s->processes[i].pid == pid) {
687 return &s->processes[i];
688 }
689 }
690
691 return NULL;
692}
693
694static GDBProcess *gdb_get_cpu_process(const GDBState *s, CPUState *cpu)
695{
696 return gdb_get_process(s, gdb_get_cpu_pid(s, cpu));
697}
698
699static CPUState *find_cpu(uint32_t thread_id)
700{
701 CPUState *cpu;
702
703 CPU_FOREACH(cpu) {
704 if (cpu_gdb_index(cpu) == thread_id) {
705 return cpu;
706 }
707 }
708
709 return NULL;
710}
711
Luc Michele40e5202019-01-07 15:23:46 +0000712static CPUState *get_first_cpu_in_process(const GDBState *s,
713 GDBProcess *process)
714{
715 CPUState *cpu;
716
717 CPU_FOREACH(cpu) {
718 if (gdb_get_cpu_pid(s, cpu) == process->pid) {
719 return cpu;
720 }
721 }
722
723 return NULL;
724}
725
726static CPUState *gdb_next_cpu_in_process(const GDBState *s, CPUState *cpu)
727{
728 uint32_t pid = gdb_get_cpu_pid(s, cpu);
729 cpu = CPU_NEXT(cpu);
730
731 while (cpu) {
732 if (gdb_get_cpu_pid(s, cpu) == pid) {
733 break;
734 }
735
736 cpu = CPU_NEXT(cpu);
737 }
738
739 return cpu;
740}
741
Luc Michele40e5202019-01-07 15:23:46 +0000742/* Return the cpu following @cpu, while ignoring unattached processes. */
743static CPUState *gdb_next_attached_cpu(const GDBState *s, CPUState *cpu)
744{
745 cpu = CPU_NEXT(cpu);
746
747 while (cpu) {
748 if (gdb_get_cpu_process(s, cpu)->attached) {
749 break;
750 }
751
752 cpu = CPU_NEXT(cpu);
753 }
754
755 return cpu;
756}
757
758/* Return the first attached cpu */
759static CPUState *gdb_first_attached_cpu(const GDBState *s)
760{
761 CPUState *cpu = first_cpu;
762 GDBProcess *process = gdb_get_cpu_process(s, cpu);
763
764 if (!process->attached) {
765 return gdb_next_attached_cpu(s, cpu);
766 }
767
768 return cpu;
769}
770
Luc Michelab65eed2019-01-29 11:46:03 +0000771static CPUState *gdb_get_cpu(const GDBState *s, uint32_t pid, uint32_t tid)
772{
773 GDBProcess *process;
774 CPUState *cpu;
775
776 if (!pid && !tid) {
777 /* 0 means any process/thread, we take the first attached one */
778 return gdb_first_attached_cpu(s);
779 } else if (pid && !tid) {
780 /* any thread in a specific process */
781 process = gdb_get_process(s, pid);
782
783 if (process == NULL) {
784 return NULL;
785 }
786
787 if (!process->attached) {
788 return NULL;
789 }
790
791 return get_first_cpu_in_process(s, process);
792 } else {
793 /* a specific thread */
794 cpu = find_cpu(tid);
795
796 if (cpu == NULL) {
797 return NULL;
798 }
799
800 process = gdb_get_cpu_process(s, cpu);
801
802 if (pid && process->pid != pid) {
803 return NULL;
804 }
805
806 if (!process->attached) {
807 return NULL;
808 }
809
810 return cpu;
811 }
812}
813
Luc Michelc145eea2019-01-07 15:23:46 +0000814static const char *get_feature_xml(const GDBState *s, const char *p,
815 const char **newp, GDBProcess *process)
pbrook56aebc82008-10-11 17:55:29 +0000816{
pbrook56aebc82008-10-11 17:55:29 +0000817 size_t len;
818 int i;
819 const char *name;
Luc Michelc145eea2019-01-07 15:23:46 +0000820 CPUState *cpu = get_first_cpu_in_process(s, process);
821 CPUClass *cc = CPU_GET_CLASS(cpu);
pbrook56aebc82008-10-11 17:55:29 +0000822
823 len = 0;
824 while (p[len] && p[len] != ':')
825 len++;
826 *newp = p + len;
827
828 name = NULL;
829 if (strncmp(p, "target.xml", len) == 0) {
Luc Michelc145eea2019-01-07 15:23:46 +0000830 char *buf = process->target_xml;
831 const size_t buf_sz = sizeof(process->target_xml);
pbrook56aebc82008-10-11 17:55:29 +0000832
Luc Michelc145eea2019-01-07 15:23:46 +0000833 /* Generate the XML description for this CPU. */
834 if (!buf[0]) {
835 GDBRegisterState *r;
836
837 pstrcat(buf, buf_sz,
David Hildenbrandb3820e62015-12-03 13:14:41 +0100838 "<?xml version=\"1.0\"?>"
839 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
840 "<target>");
841 if (cc->gdb_arch_name) {
842 gchar *arch = cc->gdb_arch_name(cpu);
Luc Michelc145eea2019-01-07 15:23:46 +0000843 pstrcat(buf, buf_sz, "<architecture>");
844 pstrcat(buf, buf_sz, arch);
845 pstrcat(buf, buf_sz, "</architecture>");
David Hildenbrandb3820e62015-12-03 13:14:41 +0100846 g_free(arch);
847 }
Luc Michelc145eea2019-01-07 15:23:46 +0000848 pstrcat(buf, buf_sz, "<xi:include href=\"");
849 pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
850 pstrcat(buf, buf_sz, "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200851 for (r = cpu->gdb_regs; r; r = r->next) {
Luc Michelc145eea2019-01-07 15:23:46 +0000852 pstrcat(buf, buf_sz, "<xi:include href=\"");
853 pstrcat(buf, buf_sz, r->xml);
854 pstrcat(buf, buf_sz, "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000855 }
Luc Michelc145eea2019-01-07 15:23:46 +0000856 pstrcat(buf, buf_sz, "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000857 }
Luc Michelc145eea2019-01-07 15:23:46 +0000858 return buf;
pbrook56aebc82008-10-11 17:55:29 +0000859 }
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100860 if (cc->gdb_get_dynamic_xml) {
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100861 char *xmlname = g_strndup(p, len);
862 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
863
864 g_free(xmlname);
865 if (xml) {
866 return xml;
867 }
868 }
pbrook56aebc82008-10-11 17:55:29 +0000869 for (i = 0; ; i++) {
870 name = xml_builtin[i][0];
871 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
872 break;
873 }
874 return name ? xml_builtin[i][1] : NULL;
875}
pbrook56aebc82008-10-11 17:55:29 +0000876
Andreas Färber385b9f02013-06-27 18:25:36 +0200877static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000878{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200879 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200880 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000881 GDBRegisterState *r;
882
Andreas Färbera0e372f2013-06-28 23:18:47 +0200883 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200884 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200885 }
pbrook56aebc82008-10-11 17:55:29 +0000886
Andreas Färbereac8b352013-06-28 21:11:37 +0200887 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000888 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
889 return r->get_reg(env, mem_buf, reg - r->base_reg);
890 }
891 }
892 return 0;
893}
894
Andreas Färber385b9f02013-06-27 18:25:36 +0200895static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000896{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200897 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200898 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000899 GDBRegisterState *r;
900
Andreas Färbera0e372f2013-06-28 23:18:47 +0200901 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200902 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200903 }
pbrook56aebc82008-10-11 17:55:29 +0000904
Andreas Färbereac8b352013-06-28 21:11:37 +0200905 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000906 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
907 return r->set_reg(env, mem_buf, reg - r->base_reg);
908 }
909 }
910 return 0;
911}
912
913/* Register a supplemental set of CPU registers. If g_pos is nonzero it
914 specifies the first register number and these registers are included in
915 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
916 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
917 */
918
Andreas Färber22169d42013-06-28 21:27:39 +0200919void gdb_register_coprocessor(CPUState *cpu,
920 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
921 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000922{
923 GDBRegisterState *s;
924 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000925
Andreas Färbereac8b352013-06-28 21:11:37 +0200926 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000927 while (*p) {
928 /* Check for duplicates. */
929 if (strcmp((*p)->xml, xml) == 0)
930 return;
931 p = &(*p)->next;
932 }
Stefan Weil9643c252011-10-18 22:25:38 +0200933
934 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200935 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200936 s->num_regs = num_regs;
937 s->get_reg = get_reg;
938 s->set_reg = set_reg;
939 s->xml = xml;
940
pbrook56aebc82008-10-11 17:55:29 +0000941 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200942 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000943 *p = s;
944 if (g_pos) {
945 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800946 error_report("Error: Bad gdb register numbering for '%s', "
947 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200948 } else {
949 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000950 }
951 }
952}
953
aliguoria1d1bb32008-11-18 20:07:32 +0000954#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100955/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
956static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
957{
958 static const int xlat[] = {
959 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
960 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
961 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
962 };
963
964 CPUClass *cc = CPU_GET_CLASS(cpu);
965 int cputype = xlat[gdbtype];
966
967 if (cc->gdb_stop_before_watchpoint) {
968 cputype |= BP_STOP_BEFORE_ACCESS;
969 }
970 return cputype;
971}
aliguoria1d1bb32008-11-18 20:07:32 +0000972#endif
973
Jon Doron77f6ce52019-05-29 09:41:35 +0300974static int gdb_breakpoint_insert(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +0000975{
Andreas Färber182735e2013-05-29 22:29:20 +0200976 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000977 int err = 0;
978
Andreas Färber62278812013-06-27 17:12:06 +0200979 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200980 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200981 }
aliguorie22a25c2009-03-12 20:12:48 +0000982
aliguoria1d1bb32008-11-18 20:07:32 +0000983 switch (type) {
984 case GDB_BREAKPOINT_SW:
985 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200986 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200987 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
988 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000989 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200990 }
aliguori880a7572008-11-18 20:30:24 +0000991 }
992 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000993#ifndef CONFIG_USER_ONLY
994 case GDB_WATCHPOINT_WRITE:
995 case GDB_WATCHPOINT_READ:
996 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200997 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100998 err = cpu_watchpoint_insert(cpu, addr, len,
999 xlat_gdb_type(cpu, type), NULL);
1000 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001001 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +01001002 }
aliguori880a7572008-11-18 20:30:24 +00001003 }
1004 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001005#endif
1006 default:
1007 return -ENOSYS;
1008 }
1009}
1010
Jon Doron77f6ce52019-05-29 09:41:35 +03001011static int gdb_breakpoint_remove(int type, target_ulong addr, target_ulong len)
aliguoria1d1bb32008-11-18 20:07:32 +00001012{
Andreas Färber182735e2013-05-29 22:29:20 +02001013 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001014 int err = 0;
1015
Andreas Färber62278812013-06-27 17:12:06 +02001016 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001017 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +02001018 }
aliguorie22a25c2009-03-12 20:12:48 +00001019
aliguoria1d1bb32008-11-18 20:07:32 +00001020 switch (type) {
1021 case GDB_BREAKPOINT_SW:
1022 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001023 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001024 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
1025 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001026 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001027 }
aliguori880a7572008-11-18 20:30:24 +00001028 }
1029 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001030#ifndef CONFIG_USER_ONLY
1031 case GDB_WATCHPOINT_WRITE:
1032 case GDB_WATCHPOINT_READ:
1033 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001034 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001035 err = cpu_watchpoint_remove(cpu, addr, len,
1036 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +00001037 if (err)
1038 break;
1039 }
1040 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001041#endif
1042 default:
1043 return -ENOSYS;
1044 }
1045}
1046
Luc Michel546f3c62019-01-07 15:23:46 +00001047static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu)
1048{
1049 cpu_breakpoint_remove_all(cpu, BP_GDB);
1050#ifndef CONFIG_USER_ONLY
1051 cpu_watchpoint_remove_all(cpu, BP_GDB);
1052#endif
1053}
1054
1055static void gdb_process_breakpoint_remove_all(const GDBState *s, GDBProcess *p)
1056{
1057 CPUState *cpu = get_first_cpu_in_process(s, p);
1058
1059 while (cpu) {
1060 gdb_cpu_breakpoint_remove_all(cpu);
1061 cpu = gdb_next_cpu_in_process(s, cpu);
1062 }
1063}
1064
aliguori880a7572008-11-18 20:30:24 +00001065static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +00001066{
Andreas Färber182735e2013-05-29 22:29:20 +02001067 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001068
aliguorie22a25c2009-03-12 20:12:48 +00001069 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001070 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +00001071 return;
1072 }
1073
Andreas Färberbdc44642013-06-24 23:50:24 +02001074 CPU_FOREACH(cpu) {
Luc Michel546f3c62019-01-07 15:23:46 +00001075 gdb_cpu_breakpoint_remove_all(cpu);
aliguori880a7572008-11-18 20:30:24 +00001076 }
aliguoria1d1bb32008-11-18 20:07:32 +00001077}
1078
aurel32fab9d282009-04-08 21:29:37 +00001079static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
1080{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001081 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +02001082
1083 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -07001084 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +00001085}
1086
Luc Michel1a227332019-01-07 15:23:45 +00001087static char *gdb_fmt_thread_id(const GDBState *s, CPUState *cpu,
1088 char *buf, size_t buf_size)
1089{
1090 if (s->multiprocess) {
1091 snprintf(buf, buf_size, "p%02x.%02x",
1092 gdb_get_cpu_pid(s, cpu), cpu_gdb_index(cpu));
1093 } else {
1094 snprintf(buf, buf_size, "%02x", cpu_gdb_index(cpu));
1095 }
1096
1097 return buf;
1098}
1099
Luc Michel7d8c87d2019-01-07 15:23:45 +00001100typedef enum GDBThreadIdKind {
1101 GDB_ONE_THREAD = 0,
1102 GDB_ALL_THREADS, /* One process, all threads */
1103 GDB_ALL_PROCESSES,
1104 GDB_READ_THREAD_ERR
1105} GDBThreadIdKind;
1106
1107static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1108 uint32_t *pid, uint32_t *tid)
1109{
1110 unsigned long p, t;
1111 int ret;
1112
1113 if (*buf == 'p') {
1114 buf++;
1115 ret = qemu_strtoul(buf, &buf, 16, &p);
1116
1117 if (ret) {
1118 return GDB_READ_THREAD_ERR;
1119 }
1120
1121 /* Skip '.' */
1122 buf++;
1123 } else {
1124 p = 1;
1125 }
1126
1127 ret = qemu_strtoul(buf, &buf, 16, &t);
1128
1129 if (ret) {
1130 return GDB_READ_THREAD_ERR;
1131 }
1132
1133 *end_buf = buf;
1134
1135 if (p == -1) {
1136 return GDB_ALL_PROCESSES;
1137 }
1138
1139 if (pid) {
1140 *pid = p;
1141 }
1142
1143 if (t == -1) {
1144 return GDB_ALL_THREADS;
1145 }
1146
1147 if (tid) {
1148 *tid = t;
1149 }
1150
1151 return GDB_ONE_THREAD;
1152}
1153
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001154/**
1155 * gdb_handle_vcont - Parses and handles a vCont packet.
1156 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1157 * a format error, 0 on success.
1158 */
1159static int gdb_handle_vcont(GDBState *s, const char *p)
1160{
Luc Michele40e5202019-01-07 15:23:46 +00001161 int res, signal = 0;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001162 char cur_action;
1163 char *newstates;
1164 unsigned long tmp;
Luc Michele40e5202019-01-07 15:23:46 +00001165 uint32_t pid, tid;
1166 GDBProcess *process;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001167 CPUState *cpu;
Luc Michelc99ef792019-03-26 12:53:26 +00001168 GDBThreadIdKind kind;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001169#ifdef CONFIG_USER_ONLY
1170 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
1171
1172 CPU_FOREACH(cpu) {
1173 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1174 }
Like Xu5cc87672019-05-19 04:54:21 +08001175#else
1176 MachineState *ms = MACHINE(qdev_get_machine());
1177 unsigned int max_cpus = ms->smp.max_cpus;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001178#endif
1179 /* uninitialised CPUs stay 0 */
1180 newstates = g_new0(char, max_cpus);
1181
1182 /* mark valid CPUs with 1 */
1183 CPU_FOREACH(cpu) {
1184 newstates[cpu->cpu_index] = 1;
1185 }
1186
1187 /*
1188 * res keeps track of what error we are returning, with -ENOTSUP meaning
1189 * that the command is unknown or unsupported, thus returning an empty
1190 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1191 * or incorrect parameters passed.
1192 */
1193 res = 0;
1194 while (*p) {
1195 if (*p++ != ';') {
1196 res = -ENOTSUP;
1197 goto out;
1198 }
1199
1200 cur_action = *p++;
1201 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +01001202 cur_action = qemu_tolower(cur_action);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001203 res = qemu_strtoul(p + 1, &p, 16, &tmp);
1204 if (res) {
1205 goto out;
1206 }
1207 signal = gdb_signal_to_target(tmp);
1208 } else if (cur_action != 'c' && cur_action != 's') {
1209 /* unknown/invalid/unsupported command */
1210 res = -ENOTSUP;
1211 goto out;
1212 }
Luc Michele40e5202019-01-07 15:23:46 +00001213
Luc Michelc99ef792019-03-26 12:53:26 +00001214 if (*p == '\0' || *p == ';') {
1215 /*
1216 * No thread specifier, action is on "all threads". The
1217 * specification is unclear regarding the process to act on. We
1218 * choose all processes.
1219 */
1220 kind = GDB_ALL_PROCESSES;
1221 } else if (*p++ == ':') {
1222 kind = read_thread_id(p, &p, &pid, &tid);
1223 } else {
Luc Michele40e5202019-01-07 15:23:46 +00001224 res = -ENOTSUP;
1225 goto out;
1226 }
1227
Luc Michelc99ef792019-03-26 12:53:26 +00001228 switch (kind) {
Luc Michele40e5202019-01-07 15:23:46 +00001229 case GDB_READ_THREAD_ERR:
1230 res = -EINVAL;
1231 goto out;
1232
1233 case GDB_ALL_PROCESSES:
1234 cpu = gdb_first_attached_cpu(s);
1235 while (cpu) {
1236 if (newstates[cpu->cpu_index] == 1) {
1237 newstates[cpu->cpu_index] = cur_action;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001238 }
Luc Michele40e5202019-01-07 15:23:46 +00001239
1240 cpu = gdb_next_attached_cpu(s, cpu);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001241 }
Luc Michele40e5202019-01-07 15:23:46 +00001242 break;
1243
1244 case GDB_ALL_THREADS:
1245 process = gdb_get_process(s, pid);
1246
1247 if (!process->attached) {
1248 res = -EINVAL;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001249 goto out;
1250 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001251
Luc Michele40e5202019-01-07 15:23:46 +00001252 cpu = get_first_cpu_in_process(s, process);
1253 while (cpu) {
1254 if (newstates[cpu->cpu_index] == 1) {
1255 newstates[cpu->cpu_index] = cur_action;
1256 }
1257
1258 cpu = gdb_next_cpu_in_process(s, cpu);
1259 }
1260 break;
1261
1262 case GDB_ONE_THREAD:
1263 cpu = gdb_get_cpu(s, pid, tid);
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001264
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001265 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001266 if (!cpu) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001267 res = -EINVAL;
1268 goto out;
1269 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001270
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001271 /* only use if no previous match occourred */
1272 if (newstates[cpu->cpu_index] == 1) {
1273 newstates[cpu->cpu_index] = cur_action;
1274 }
Luc Michele40e5202019-01-07 15:23:46 +00001275 break;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001276 }
1277 }
1278 s->signal = signal;
1279 gdb_continue_partial(s, newstates);
1280
1281out:
1282 g_free(newstates);
1283
1284 return res;
1285}
1286
Jon Dorond14055d2019-05-29 09:41:29 +03001287typedef union GdbCmdVariant {
1288 const char *data;
1289 uint8_t opcode;
1290 unsigned long val_ul;
1291 unsigned long long val_ull;
1292 struct {
1293 GDBThreadIdKind kind;
1294 uint32_t pid;
1295 uint32_t tid;
1296 } thread_id;
1297} GdbCmdVariant;
1298
1299static const char *cmd_next_param(const char *param, const char delimiter)
1300{
1301 static const char all_delimiters[] = ",;:=";
1302 char curr_delimiters[2] = {0};
1303 const char *delimiters;
1304
1305 if (delimiter == '?') {
1306 delimiters = all_delimiters;
1307 } else if (delimiter == '0') {
1308 return strchr(param, '\0');
1309 } else if (delimiter == '.' && *param) {
1310 return param + 1;
1311 } else {
1312 curr_delimiters[0] = delimiter;
1313 delimiters = curr_delimiters;
1314 }
1315
1316 param += strcspn(param, delimiters);
1317 if (*param) {
1318 param++;
1319 }
1320 return param;
1321}
1322
1323static int cmd_parse_params(const char *data, const char *schema,
1324 GdbCmdVariant *params, int *num_params)
1325{
1326 int curr_param;
1327 const char *curr_schema, *curr_data;
1328
1329 *num_params = 0;
1330
1331 if (!schema) {
1332 return 0;
1333 }
1334
1335 curr_schema = schema;
1336 curr_param = 0;
1337 curr_data = data;
1338 while (curr_schema[0] && curr_schema[1] && *curr_data) {
1339 switch (curr_schema[0]) {
1340 case 'l':
1341 if (qemu_strtoul(curr_data, &curr_data, 16,
1342 &params[curr_param].val_ul)) {
1343 return -EINVAL;
1344 }
1345 curr_param++;
1346 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1347 break;
1348 case 'L':
1349 if (qemu_strtou64(curr_data, &curr_data, 16,
1350 (uint64_t *)&params[curr_param].val_ull)) {
1351 return -EINVAL;
1352 }
1353 curr_param++;
1354 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1355 break;
1356 case 's':
1357 params[curr_param].data = curr_data;
1358 curr_param++;
1359 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1360 break;
1361 case 'o':
1362 params[curr_param].opcode = *(uint8_t *)curr_data;
1363 curr_param++;
1364 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1365 break;
1366 case 't':
1367 params[curr_param].thread_id.kind =
1368 read_thread_id(curr_data, &curr_data,
1369 &params[curr_param].thread_id.pid,
1370 &params[curr_param].thread_id.tid);
1371 curr_param++;
1372 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1373 break;
1374 case '?':
1375 curr_data = cmd_next_param(curr_data, curr_schema[1]);
1376 break;
1377 default:
1378 return -EINVAL;
1379 }
1380 curr_schema += 2;
1381 }
1382
1383 *num_params = curr_param;
1384 return 0;
1385}
1386
1387typedef struct GdbCmdContext {
1388 GDBState *s;
1389 GdbCmdVariant *params;
1390 int num_params;
1391 uint8_t mem_buf[MAX_PACKET_LENGTH];
1392 char str_buf[MAX_PACKET_LENGTH + 1];
1393} GdbCmdContext;
1394
1395typedef void (*GdbCmdHandler)(GdbCmdContext *gdb_ctx, void *user_ctx);
1396
1397/*
1398 * cmd_startswith -> cmd is compared using startswith
1399 *
1400 *
1401 * schema definitions:
1402 * Each schema parameter entry consists of 2 chars,
1403 * the first char represents the parameter type handling
1404 * the second char represents the delimiter for the next parameter
1405 *
1406 * Currently supported schema types:
1407 * 'l' -> unsigned long (stored in .val_ul)
1408 * 'L' -> unsigned long long (stored in .val_ull)
1409 * 's' -> string (stored in .data)
1410 * 'o' -> single char (stored in .opcode)
1411 * 't' -> thread id (stored in .thread_id)
1412 * '?' -> skip according to delimiter
1413 *
1414 * Currently supported delimiters:
1415 * '?' -> Stop at any delimiter (",;:=\0")
1416 * '0' -> Stop at "\0"
1417 * '.' -> Skip 1 char unless reached "\0"
1418 * Any other value is treated as the delimiter value itself
1419 */
1420typedef struct GdbCmdParseEntry {
1421 GdbCmdHandler handler;
1422 const char *cmd;
1423 bool cmd_startswith;
1424 const char *schema;
1425} GdbCmdParseEntry;
1426
1427static inline int startswith(const char *string, const char *pattern)
1428{
1429 return !strncmp(string, pattern, strlen(pattern));
1430}
1431
Jon Dorond14055d2019-05-29 09:41:29 +03001432static int process_string_cmd(GDBState *s, void *user_ctx, const char *data,
1433 const GdbCmdParseEntry *cmds, int num_cmds)
1434{
1435 int i, schema_len, max_num_params = 0;
1436 GdbCmdContext gdb_ctx;
1437
1438 if (!cmds) {
1439 return -1;
1440 }
1441
1442 for (i = 0; i < num_cmds; i++) {
1443 const GdbCmdParseEntry *cmd = &cmds[i];
1444 g_assert(cmd->handler && cmd->cmd);
1445
1446 if ((cmd->cmd_startswith && !startswith(data, cmd->cmd)) ||
1447 (!cmd->cmd_startswith && strcmp(cmd->cmd, data))) {
1448 continue;
1449 }
1450
1451 if (cmd->schema) {
1452 schema_len = strlen(cmd->schema);
1453 if (schema_len % 2) {
1454 return -2;
1455 }
1456
1457 max_num_params = schema_len / 2;
1458 }
1459
1460 gdb_ctx.params =
1461 (GdbCmdVariant *)alloca(sizeof(*gdb_ctx.params) * max_num_params);
1462 memset(gdb_ctx.params, 0, sizeof(*gdb_ctx.params) * max_num_params);
1463
1464 if (cmd_parse_params(&data[strlen(cmd->cmd)], cmd->schema,
1465 gdb_ctx.params, &gdb_ctx.num_params)) {
1466 return -1;
1467 }
1468
1469 gdb_ctx.s = s;
1470 cmd->handler(&gdb_ctx, user_ctx);
1471 return 0;
1472 }
1473
1474 return -1;
1475}
1476
Jon Doron3e2c1262019-05-29 09:41:30 +03001477static void run_cmd_parser(GDBState *s, const char *data,
1478 const GdbCmdParseEntry *cmd)
1479{
1480 if (!data) {
1481 return;
1482 }
1483
1484 /* In case there was an error during the command parsing we must
1485 * send a NULL packet to indicate the command is not supported */
1486 if (process_string_cmd(s, NULL, data, cmd, 1)) {
1487 put_packet(s, "");
1488 }
1489}
1490
1491static void handle_detach(GdbCmdContext *gdb_ctx, void *user_ctx)
1492{
1493 GDBProcess *process;
1494 GDBState *s = gdb_ctx->s;
1495 uint32_t pid = 1;
1496
1497 if (s->multiprocess) {
1498 if (!gdb_ctx->num_params) {
1499 put_packet(s, "E22");
1500 return;
1501 }
1502
1503 pid = gdb_ctx->params[0].val_ul;
1504 }
1505
1506 process = gdb_get_process(s, pid);
1507 gdb_process_breakpoint_remove_all(s, process);
1508 process->attached = false;
1509
1510 if (pid == gdb_get_cpu_pid(s, s->c_cpu)) {
1511 s->c_cpu = gdb_first_attached_cpu(s);
1512 }
1513
1514 if (pid == gdb_get_cpu_pid(s, s->g_cpu)) {
1515 s->g_cpu = gdb_first_attached_cpu(s);
1516 }
1517
1518 if (!s->c_cpu) {
1519 /* No more process attached */
1520 gdb_syscall_mode = GDB_SYS_DISABLED;
1521 gdb_continue(s);
1522 }
1523 put_packet(s, "OK");
1524}
1525
Jon Doron44ffded2019-05-29 09:41:31 +03001526static void handle_thread_alive(GdbCmdContext *gdb_ctx, void *user_ctx)
1527{
1528 CPUState *cpu;
1529
1530 if (!gdb_ctx->num_params) {
1531 put_packet(gdb_ctx->s, "E22");
1532 return;
1533 }
1534
1535 if (gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
1536 put_packet(gdb_ctx->s, "E22");
1537 return;
1538 }
1539
1540 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[0].thread_id.pid,
1541 gdb_ctx->params[0].thread_id.tid);
1542 if (!cpu) {
1543 put_packet(gdb_ctx->s, "E22");
1544 return;
1545 }
1546
1547 put_packet(gdb_ctx->s, "OK");
1548}
1549
Jon Doron4d6e3fe2019-05-29 09:41:32 +03001550static void handle_continue(GdbCmdContext *gdb_ctx, void *user_ctx)
1551{
1552 if (gdb_ctx->num_params) {
1553 gdb_set_cpu_pc(gdb_ctx->s, gdb_ctx->params[0].val_ull);
1554 }
1555
1556 gdb_ctx->s->signal = 0;
1557 gdb_continue(gdb_ctx->s);
1558}
1559
Jon Doronccc47d52019-05-29 09:41:33 +03001560static void handle_cont_with_sig(GdbCmdContext *gdb_ctx, void *user_ctx)
1561{
1562 unsigned long signal = 0;
1563
1564 /*
1565 * Note: C sig;[addr] is currently unsupported and we simply
1566 * omit the addr parameter
1567 */
1568 if (gdb_ctx->num_params) {
1569 signal = gdb_ctx->params[0].val_ul;
1570 }
1571
1572 gdb_ctx->s->signal = gdb_signal_to_target(signal);
1573 if (gdb_ctx->s->signal == -1) {
1574 gdb_ctx->s->signal = 0;
1575 }
1576 gdb_continue(gdb_ctx->s);
1577}
1578
Jon Doron3a9651d2019-05-29 09:41:34 +03001579static void handle_set_thread(GdbCmdContext *gdb_ctx, void *user_ctx)
1580{
1581 CPUState *cpu;
1582
1583 if (gdb_ctx->num_params != 2) {
1584 put_packet(gdb_ctx->s, "E22");
1585 return;
1586 }
1587
1588 if (gdb_ctx->params[1].thread_id.kind == GDB_READ_THREAD_ERR) {
1589 put_packet(gdb_ctx->s, "E22");
1590 return;
1591 }
1592
1593 if (gdb_ctx->params[1].thread_id.kind != GDB_ONE_THREAD) {
1594 put_packet(gdb_ctx->s, "OK");
1595 return;
1596 }
1597
1598 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[1].thread_id.pid,
1599 gdb_ctx->params[1].thread_id.tid);
1600 if (!cpu) {
1601 put_packet(gdb_ctx->s, "E22");
1602 return;
1603 }
1604
1605 /*
1606 * Note: This command is deprecated and modern gdb's will be using the
1607 * vCont command instead.
1608 */
1609 switch (gdb_ctx->params[0].opcode) {
1610 case 'c':
1611 gdb_ctx->s->c_cpu = cpu;
1612 put_packet(gdb_ctx->s, "OK");
1613 break;
1614 case 'g':
1615 gdb_ctx->s->g_cpu = cpu;
1616 put_packet(gdb_ctx->s, "OK");
1617 break;
1618 default:
1619 put_packet(gdb_ctx->s, "E22");
1620 break;
1621 }
1622}
1623
Jon Doron77f6ce52019-05-29 09:41:35 +03001624static void handle_insert_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1625{
1626 int res;
1627
1628 if (gdb_ctx->num_params != 3) {
1629 put_packet(gdb_ctx->s, "E22");
1630 return;
1631 }
1632
1633 res = gdb_breakpoint_insert(gdb_ctx->params[0].val_ul,
1634 gdb_ctx->params[1].val_ull,
1635 gdb_ctx->params[2].val_ull);
1636 if (res >= 0) {
1637 put_packet(gdb_ctx->s, "OK");
1638 return;
1639 } else if (res == -ENOSYS) {
1640 put_packet(gdb_ctx->s, "");
1641 return;
1642 }
1643
1644 put_packet(gdb_ctx->s, "E22");
1645}
1646
1647static void handle_remove_bp(GdbCmdContext *gdb_ctx, void *user_ctx)
1648{
1649 int res;
1650
1651 if (gdb_ctx->num_params != 3) {
1652 put_packet(gdb_ctx->s, "E22");
1653 return;
1654 }
1655
1656 res = gdb_breakpoint_remove(gdb_ctx->params[0].val_ul,
1657 gdb_ctx->params[1].val_ull,
1658 gdb_ctx->params[2].val_ull);
1659 if (res >= 0) {
1660 put_packet(gdb_ctx->s, "OK");
1661 return;
1662 } else if (res == -ENOSYS) {
1663 put_packet(gdb_ctx->s, "");
1664 return;
1665 }
1666
1667 put_packet(gdb_ctx->s, "E22");
1668}
1669
Jon Doron62b33202019-05-29 09:41:36 +03001670static void handle_set_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1671{
1672 int reg_size;
1673
1674 if (!gdb_has_xml) {
1675 put_packet(gdb_ctx->s, "E00");
1676 return;
1677 }
1678
1679 if (gdb_ctx->num_params != 2) {
1680 put_packet(gdb_ctx->s, "E22");
1681 return;
1682 }
1683
1684 reg_size = strlen(gdb_ctx->params[1].data) / 2;
1685 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[1].data, reg_size);
1686 gdb_write_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf,
1687 gdb_ctx->params[0].val_ull);
1688 put_packet(gdb_ctx->s, "OK");
1689}
1690
Jon Doron5d0e57b2019-05-29 09:41:37 +03001691static void handle_get_reg(GdbCmdContext *gdb_ctx, void *user_ctx)
1692{
1693 int reg_size;
1694
1695 /*
1696 * Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1697 * This works, but can be very slow. Anything new enough to
1698 * understand XML also knows how to use this properly.
1699 */
1700 if (!gdb_has_xml) {
1701 put_packet(gdb_ctx->s, "");
1702 return;
1703 }
1704
1705 if (!gdb_ctx->num_params) {
1706 put_packet(gdb_ctx->s, "E14");
1707 return;
1708 }
1709
1710 reg_size = gdb_read_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf,
1711 gdb_ctx->params[0].val_ull);
1712 if (!reg_size) {
1713 put_packet(gdb_ctx->s, "E14");
1714 return;
1715 }
1716
1717 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, reg_size);
1718 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1719}
1720
Jon Doroncc0ecc72019-05-29 09:41:38 +03001721static void handle_write_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1722{
1723 if (gdb_ctx->num_params != 3) {
1724 put_packet(gdb_ctx->s, "E22");
1725 return;
1726 }
1727
1728 /* hextomem() reads 2*len bytes */
1729 if (gdb_ctx->params[1].val_ull > strlen(gdb_ctx->params[2].data) / 2) {
1730 put_packet(gdb_ctx->s, "E22");
1731 return;
1732 }
1733
1734 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[2].data,
1735 gdb_ctx->params[1].val_ull);
1736 if (target_memory_rw_debug(gdb_ctx->s->g_cpu, gdb_ctx->params[0].val_ull,
1737 gdb_ctx->mem_buf,
1738 gdb_ctx->params[1].val_ull, true)) {
1739 put_packet(gdb_ctx->s, "E14");
1740 return;
1741 }
1742
1743 put_packet(gdb_ctx->s, "OK");
1744}
1745
Jon Doronda92e232019-05-29 09:41:39 +03001746static void handle_read_mem(GdbCmdContext *gdb_ctx, void *user_ctx)
1747{
1748 if (gdb_ctx->num_params != 2) {
1749 put_packet(gdb_ctx->s, "E22");
1750 return;
1751 }
1752
1753 /* memtohex() doubles the required space */
1754 if (gdb_ctx->params[1].val_ull > MAX_PACKET_LENGTH / 2) {
1755 put_packet(gdb_ctx->s, "E22");
1756 return;
1757 }
1758
1759 if (target_memory_rw_debug(gdb_ctx->s->g_cpu, gdb_ctx->params[0].val_ull,
1760 gdb_ctx->mem_buf,
1761 gdb_ctx->params[1].val_ull, false)) {
1762 put_packet(gdb_ctx->s, "E14");
1763 return;
1764 }
1765
1766 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, gdb_ctx->params[1].val_ull);
1767 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1768}
1769
Jon Doron287ca122019-05-29 09:41:40 +03001770static void handle_write_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1771{
1772 target_ulong addr, len;
1773 uint8_t *registers;
1774 int reg_size;
1775
1776 if (!gdb_ctx->num_params) {
1777 return;
1778 }
1779
1780 cpu_synchronize_state(gdb_ctx->s->g_cpu);
1781 registers = gdb_ctx->mem_buf;
1782 len = strlen(gdb_ctx->params[0].data) / 2;
1783 hextomem(registers, gdb_ctx->params[0].data, len);
1784 for (addr = 0; addr < gdb_ctx->s->g_cpu->gdb_num_g_regs && len > 0;
1785 addr++) {
1786 reg_size = gdb_write_register(gdb_ctx->s->g_cpu, registers, addr);
1787 len -= reg_size;
1788 registers += reg_size;
1789 }
1790 put_packet(gdb_ctx->s, "OK");
1791}
1792
Jon Doron397d1372019-05-29 09:41:41 +03001793static void handle_read_all_regs(GdbCmdContext *gdb_ctx, void *user_ctx)
1794{
1795 target_ulong addr, len;
1796
1797 cpu_synchronize_state(gdb_ctx->s->g_cpu);
1798 len = 0;
1799 for (addr = 0; addr < gdb_ctx->s->g_cpu->gdb_num_g_regs; addr++) {
1800 len += gdb_read_register(gdb_ctx->s->g_cpu, gdb_ctx->mem_buf + len,
1801 addr);
1802 }
1803
1804 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, len);
1805 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1806}
1807
Jon Doron4b20fab2019-05-29 09:41:42 +03001808static void handle_file_io(GdbCmdContext *gdb_ctx, void *user_ctx)
1809{
1810 if (gdb_ctx->num_params >= 2 && gdb_ctx->s->current_syscall_cb) {
1811 target_ulong ret, err;
1812
1813 ret = (target_ulong)gdb_ctx->params[0].val_ull;
1814 err = (target_ulong)gdb_ctx->params[1].val_ull;
1815 gdb_ctx->s->current_syscall_cb(gdb_ctx->s->c_cpu, ret, err);
1816 gdb_ctx->s->current_syscall_cb = NULL;
1817 }
1818
1819 if (gdb_ctx->num_params >= 3 && gdb_ctx->params[2].opcode == (uint8_t)'C') {
1820 put_packet(gdb_ctx->s, "T02");
1821 return;
1822 }
1823
1824 gdb_continue(gdb_ctx->s);
1825}
1826
Jon Doron933f80d2019-05-29 09:41:43 +03001827static void handle_step(GdbCmdContext *gdb_ctx, void *user_ctx)
1828{
1829 if (gdb_ctx->num_params) {
1830 gdb_set_cpu_pc(gdb_ctx->s, (target_ulong)gdb_ctx->params[0].val_ull);
1831 }
1832
1833 cpu_single_step(gdb_ctx->s->c_cpu, sstep_flags);
1834 gdb_continue(gdb_ctx->s);
1835}
1836
Jon Doron8536ec02019-05-29 09:41:44 +03001837static void handle_v_cont_query(GdbCmdContext *gdb_ctx, void *user_ctx)
1838{
1839 put_packet(gdb_ctx->s, "vCont;c;C;s;S");
1840}
1841
1842static void handle_v_cont(GdbCmdContext *gdb_ctx, void *user_ctx)
1843{
1844 int res;
1845
1846 if (!gdb_ctx->num_params) {
1847 return;
1848 }
1849
1850 res = gdb_handle_vcont(gdb_ctx->s, gdb_ctx->params[0].data);
1851 if ((res == -EINVAL) || (res == -ERANGE)) {
1852 put_packet(gdb_ctx->s, "E22");
1853 } else if (res) {
1854 put_packet(gdb_ctx->s, "");
1855 }
1856}
1857
1858static void handle_v_attach(GdbCmdContext *gdb_ctx, void *user_ctx)
1859{
1860 GDBProcess *process;
1861 CPUState *cpu;
1862 char thread_id[16];
1863
1864 pstrcpy(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "E22");
1865 if (!gdb_ctx->num_params) {
1866 goto cleanup;
1867 }
1868
1869 process = gdb_get_process(gdb_ctx->s, gdb_ctx->params[0].val_ul);
1870 if (!process) {
1871 goto cleanup;
1872 }
1873
1874 cpu = get_first_cpu_in_process(gdb_ctx->s, process);
1875 if (!cpu) {
1876 goto cleanup;
1877 }
1878
1879 process->attached = true;
1880 gdb_ctx->s->g_cpu = cpu;
1881 gdb_ctx->s->c_cpu = cpu;
1882
1883 gdb_fmt_thread_id(gdb_ctx->s, cpu, thread_id, sizeof(thread_id));
1884 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "T%02xthread:%s;",
1885 GDB_SIGNAL_TRAP, thread_id);
1886cleanup:
1887 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1888}
1889
1890static void handle_v_kill(GdbCmdContext *gdb_ctx, void *user_ctx)
1891{
1892 /* Kill the target */
1893 put_packet(gdb_ctx->s, "OK");
1894 error_report("QEMU: Terminated via GDBstub");
1895 exit(0);
1896}
1897
1898static GdbCmdParseEntry gdb_v_commands_table[] = {
1899 /* Order is important if has same prefix */
1900 {
1901 .handler = handle_v_cont_query,
1902 .cmd = "Cont?",
1903 .cmd_startswith = 1
1904 },
1905 {
1906 .handler = handle_v_cont,
1907 .cmd = "Cont",
1908 .cmd_startswith = 1,
1909 .schema = "s0"
1910 },
1911 {
1912 .handler = handle_v_attach,
1913 .cmd = "Attach;",
1914 .cmd_startswith = 1,
1915 .schema = "l0"
1916 },
1917 {
1918 .handler = handle_v_kill,
1919 .cmd = "Kill;",
1920 .cmd_startswith = 1
1921 },
1922};
1923
1924static void handle_v_commands(GdbCmdContext *gdb_ctx, void *user_ctx)
1925{
1926 if (!gdb_ctx->num_params) {
1927 return;
1928 }
1929
1930 if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
1931 gdb_v_commands_table,
1932 ARRAY_SIZE(gdb_v_commands_table))) {
1933 put_packet(gdb_ctx->s, "");
1934 }
1935}
1936
Jon Doron2704efa2019-05-29 09:41:45 +03001937static void handle_query_qemu_sstepbits(GdbCmdContext *gdb_ctx, void *user_ctx)
1938{
1939 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
1940 "ENABLE=%x,NOIRQ=%x,NOTIMER=%x", SSTEP_ENABLE,
1941 SSTEP_NOIRQ, SSTEP_NOTIMER);
1942 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1943}
1944
1945static void handle_set_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
1946{
1947 if (!gdb_ctx->num_params) {
1948 return;
1949 }
1950
1951 sstep_flags = gdb_ctx->params[0].val_ul;
1952 put_packet(gdb_ctx->s, "OK");
1953}
1954
1955static void handle_query_qemu_sstep(GdbCmdContext *gdb_ctx, void *user_ctx)
1956{
1957 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "0x%x", sstep_flags);
1958 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1959}
1960
1961static void handle_query_curr_tid(GdbCmdContext *gdb_ctx, void *user_ctx)
bellardb4608c02003-06-27 17:34:32 +00001962{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001963 CPUState *cpu;
Luc Michelc145eea2019-01-07 15:23:46 +00001964 GDBProcess *process;
Jon Doron2704efa2019-05-29 09:41:45 +03001965 char thread_id[16];
1966
1967 /*
1968 * "Current thread" remains vague in the spec, so always return
1969 * the first thread of the current process (gdb returns the
1970 * first thread).
1971 */
1972 process = gdb_get_cpu_process(gdb_ctx->s, gdb_ctx->s->g_cpu);
1973 cpu = get_first_cpu_in_process(gdb_ctx->s, process);
1974 gdb_fmt_thread_id(gdb_ctx->s, cpu, thread_id, sizeof(thread_id));
1975 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "QC%s", thread_id);
1976 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1977}
1978
1979static void handle_query_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
1980{
1981 char thread_id[16];
1982
1983 if (!gdb_ctx->s->query_cpu) {
1984 put_packet(gdb_ctx->s, "l");
1985 return;
1986 }
1987
1988 gdb_fmt_thread_id(gdb_ctx->s, gdb_ctx->s->query_cpu, thread_id,
1989 sizeof(thread_id));
1990 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "m%s", thread_id);
1991 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
1992 gdb_ctx->s->query_cpu =
1993 gdb_next_attached_cpu(gdb_ctx->s, gdb_ctx->s->query_cpu);
1994}
1995
1996static void handle_query_first_threads(GdbCmdContext *gdb_ctx, void *user_ctx)
1997{
1998 gdb_ctx->s->query_cpu = gdb_first_attached_cpu(gdb_ctx->s);
1999 handle_query_threads(gdb_ctx, user_ctx);
2000}
2001
2002static void handle_query_thread_extra(GdbCmdContext *gdb_ctx, void *user_ctx)
2003{
2004 CPUState *cpu;
2005 int len;
2006
2007 if (!gdb_ctx->num_params ||
2008 gdb_ctx->params[0].thread_id.kind == GDB_READ_THREAD_ERR) {
2009 put_packet(gdb_ctx->s, "E22");
2010 return;
2011 }
2012
2013 cpu = gdb_get_cpu(gdb_ctx->s, gdb_ctx->params[0].thread_id.pid,
2014 gdb_ctx->params[0].thread_id.tid);
2015 if (!cpu) {
2016 return;
2017 }
2018
2019 cpu_synchronize_state(cpu);
2020
2021 if (gdb_ctx->s->multiprocess && (gdb_ctx->s->process_num > 1)) {
2022 /* Print the CPU model and name in multiprocess mode */
2023 ObjectClass *oc = object_get_class(OBJECT(cpu));
2024 const char *cpu_model = object_class_get_name(oc);
2025 char *cpu_name = object_get_canonical_path_component(OBJECT(cpu));
2026 len = snprintf((char *)gdb_ctx->mem_buf, sizeof(gdb_ctx->str_buf) / 2,
2027 "%s %s [%s]", cpu_model, cpu_name,
2028 cpu->halted ? "halted " : "running");
2029 g_free(cpu_name);
2030 } else {
2031 /* memtohex() doubles the required space */
2032 len = snprintf((char *)gdb_ctx->mem_buf, sizeof(gdb_ctx->str_buf) / 2,
2033 "CPU#%d [%s]", cpu->cpu_index,
2034 cpu->halted ? "halted " : "running");
2035 }
2036 trace_gdbstub_op_extra_info((char *)gdb_ctx->mem_buf);
2037 memtohex(gdb_ctx->str_buf, gdb_ctx->mem_buf, len);
2038 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2039}
2040
2041#ifdef CONFIG_USER_ONLY
2042static void handle_query_offsets(GdbCmdContext *gdb_ctx, void *user_ctx)
2043{
2044 TaskState *ts;
2045
2046 ts = gdb_ctx->s->c_cpu->opaque;
2047 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
2048 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
2049 ";Bss=" TARGET_ABI_FMT_lx,
2050 ts->info->code_offset,
2051 ts->info->data_offset,
2052 ts->info->data_offset);
2053 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2054}
2055#else
2056static void handle_query_rcmd(GdbCmdContext *gdb_ctx, void *user_ctx)
2057{
2058 int len;
2059
2060 if (!gdb_ctx->num_params) {
2061 put_packet(gdb_ctx->s, "E22");
2062 return;
2063 }
2064
2065 len = strlen(gdb_ctx->params[0].data);
2066 if (len % 2) {
2067 put_packet(gdb_ctx->s, "E01");
2068 return;
2069 }
2070
2071 len = len / 2;
2072 hextomem(gdb_ctx->mem_buf, gdb_ctx->params[0].data, len);
2073 gdb_ctx->mem_buf[len++] = 0;
2074 qemu_chr_be_write(gdb_ctx->s->mon_chr, gdb_ctx->mem_buf, len);
2075 put_packet(gdb_ctx->s, "OK");
2076
2077}
2078#endif
2079
2080static void handle_query_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2081{
Andreas Färber5b24c642013-07-07 15:08:22 +02002082 CPUClass *cc;
Jon Doron2704efa2019-05-29 09:41:45 +03002083
2084 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "PacketSize=%x",
2085 MAX_PACKET_LENGTH);
2086 cc = CPU_GET_CLASS(first_cpu);
2087 if (cc->gdb_core_xml_file) {
2088 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf),
2089 ";qXfer:features:read+");
2090 }
2091
2092 if (gdb_ctx->num_params &&
2093 strstr(gdb_ctx->params[0].data, "multiprocess+")) {
2094 gdb_ctx->s->multiprocess = true;
2095 }
2096
2097 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), ";multiprocess+");
2098 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2099}
2100
2101static void handle_query_xfer_features(GdbCmdContext *gdb_ctx, void *user_ctx)
2102{
2103 GDBProcess *process;
2104 CPUClass *cc;
2105 unsigned long len, total_len, addr;
2106 const char *xml;
bellardb4608c02003-06-27 17:34:32 +00002107 const char *p;
Jon Doron2704efa2019-05-29 09:41:45 +03002108
2109 if (gdb_ctx->num_params < 3) {
2110 put_packet(gdb_ctx->s, "E22");
2111 return;
2112 }
2113
2114 process = gdb_get_cpu_process(gdb_ctx->s, gdb_ctx->s->g_cpu);
2115 cc = CPU_GET_CLASS(gdb_ctx->s->g_cpu);
2116 if (!cc->gdb_core_xml_file) {
2117 put_packet(gdb_ctx->s, "");
2118 return;
2119 }
2120
2121 gdb_has_xml = true;
2122 p = gdb_ctx->params[0].data;
2123 xml = get_feature_xml(gdb_ctx->s, p, &p, process);
2124 if (!xml) {
2125 put_packet(gdb_ctx->s, "E00");
2126 return;
2127 }
2128
2129 addr = gdb_ctx->params[1].val_ul;
2130 len = gdb_ctx->params[2].val_ul;
2131 total_len = strlen(xml);
2132 if (addr > total_len) {
2133 put_packet(gdb_ctx->s, "E00");
2134 return;
2135 }
2136
2137 if (len > (MAX_PACKET_LENGTH - 5) / 2) {
2138 len = (MAX_PACKET_LENGTH - 5) / 2;
2139 }
2140
2141 if (len < total_len - addr) {
2142 gdb_ctx->str_buf[0] = 'm';
2143 len = memtox(gdb_ctx->str_buf + 1, xml + addr, len);
2144 } else {
2145 gdb_ctx->str_buf[0] = 'l';
2146 len = memtox(gdb_ctx->str_buf + 1, xml + addr, total_len - addr);
2147 }
2148
2149 put_packet_binary(gdb_ctx->s, gdb_ctx->str_buf, len + 1, true);
2150}
2151
2152static void handle_query_attached(GdbCmdContext *gdb_ctx, void *user_ctx)
2153{
2154 put_packet(gdb_ctx->s, GDB_ATTACHED);
2155}
2156
2157static void handle_query_qemu_supported(GdbCmdContext *gdb_ctx, void *user_ctx)
2158{
Jon Doronab4752e2019-05-29 09:41:48 +03002159 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "sstepbits;sstep");
2160#ifndef CONFIG_USER_ONLY
2161 pstrcat(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), ";PhyMemMode");
2162#endif
2163 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
Jon Doron2704efa2019-05-29 09:41:45 +03002164}
2165
Jon Doronab4752e2019-05-29 09:41:48 +03002166#ifndef CONFIG_USER_ONLY
2167static void handle_query_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx,
2168 void *user_ctx)
2169{
2170 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "%d", phy_memory_mode);
2171 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2172}
2173
2174static void handle_set_qemu_phy_mem_mode(GdbCmdContext *gdb_ctx, void *user_ctx)
2175{
2176 if (!gdb_ctx->num_params) {
2177 put_packet(gdb_ctx->s, "E22");
2178 return;
2179 }
2180
2181 if (!gdb_ctx->params[0].val_ul) {
2182 phy_memory_mode = 0;
2183 } else {
2184 phy_memory_mode = 1;
2185 }
2186 put_packet(gdb_ctx->s, "OK");
2187}
2188#endif
2189
Jon Doron2704efa2019-05-29 09:41:45 +03002190static GdbCmdParseEntry gdb_gen_query_set_common_table[] = {
2191 /* Order is important if has same prefix */
2192 {
2193 .handler = handle_query_qemu_sstepbits,
2194 .cmd = "qemu.sstepbits",
2195 },
2196 {
2197 .handler = handle_query_qemu_sstep,
2198 .cmd = "qemu.sstep",
2199 },
2200 {
2201 .handler = handle_set_qemu_sstep,
2202 .cmd = "qemu.sstep=",
2203 .cmd_startswith = 1,
2204 .schema = "l0"
2205 },
2206};
2207
2208static GdbCmdParseEntry gdb_gen_query_table[] = {
2209 {
2210 .handler = handle_query_curr_tid,
2211 .cmd = "C",
2212 },
2213 {
2214 .handler = handle_query_threads,
2215 .cmd = "sThreadInfo",
2216 },
2217 {
2218 .handler = handle_query_first_threads,
2219 .cmd = "fThreadInfo",
2220 },
2221 {
2222 .handler = handle_query_thread_extra,
2223 .cmd = "ThreadExtraInfo,",
2224 .cmd_startswith = 1,
2225 .schema = "t0"
2226 },
2227#ifdef CONFIG_USER_ONLY
2228 {
2229 .handler = handle_query_offsets,
2230 .cmd = "Offsets",
2231 },
2232#else
2233 {
2234 .handler = handle_query_rcmd,
2235 .cmd = "Rcmd,",
2236 .cmd_startswith = 1,
2237 .schema = "s0"
2238 },
2239#endif
2240 {
2241 .handler = handle_query_supported,
2242 .cmd = "Supported:",
2243 .cmd_startswith = 1,
2244 .schema = "s0"
2245 },
2246 {
2247 .handler = handle_query_supported,
2248 .cmd = "Supported",
2249 .schema = "s0"
2250 },
2251 {
2252 .handler = handle_query_xfer_features,
2253 .cmd = "Xfer:features:read:",
2254 .cmd_startswith = 1,
2255 .schema = "s:l,l0"
2256 },
2257 {
2258 .handler = handle_query_attached,
2259 .cmd = "Attached:",
2260 .cmd_startswith = 1
2261 },
2262 {
2263 .handler = handle_query_attached,
2264 .cmd = "Attached",
2265 },
2266 {
2267 .handler = handle_query_qemu_supported,
2268 .cmd = "qemu.Supported",
2269 },
Jon Doronab4752e2019-05-29 09:41:48 +03002270#ifndef CONFIG_USER_ONLY
2271 {
2272 .handler = handle_query_qemu_phy_mem_mode,
2273 .cmd = "qemu.PhyMemMode",
2274 },
2275#endif
Jon Doron2704efa2019-05-29 09:41:45 +03002276};
2277
2278static GdbCmdParseEntry gdb_gen_set_table[] = {
2279 /* Order is important if has same prefix */
2280 {
2281 .handler = handle_set_qemu_sstep,
2282 .cmd = "qemu.sstep:",
2283 .cmd_startswith = 1,
2284 .schema = "l0"
2285 },
Jon Doronab4752e2019-05-29 09:41:48 +03002286#ifndef CONFIG_USER_ONLY
2287 {
2288 .handler = handle_set_qemu_phy_mem_mode,
2289 .cmd = "qemu.PhyMemMode:",
2290 .cmd_startswith = 1,
2291 .schema = "l0"
2292 },
2293#endif
Jon Doron2704efa2019-05-29 09:41:45 +03002294};
2295
2296static void handle_gen_query(GdbCmdContext *gdb_ctx, void *user_ctx)
2297{
2298 if (!gdb_ctx->num_params) {
2299 return;
2300 }
2301
2302 if (!process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2303 gdb_gen_query_set_common_table,
2304 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2305 return;
2306 }
2307
2308 if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2309 gdb_gen_query_table,
2310 ARRAY_SIZE(gdb_gen_query_table))) {
2311 put_packet(gdb_ctx->s, "");
2312 }
2313}
2314
2315static void handle_gen_set(GdbCmdContext *gdb_ctx, void *user_ctx)
2316{
2317 if (!gdb_ctx->num_params) {
2318 return;
2319 }
2320
2321 if (!process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2322 gdb_gen_query_set_common_table,
2323 ARRAY_SIZE(gdb_gen_query_set_common_table))) {
2324 return;
2325 }
2326
2327 if (process_string_cmd(gdb_ctx->s, NULL, gdb_ctx->params[0].data,
2328 gdb_gen_set_table,
2329 ARRAY_SIZE(gdb_gen_set_table))) {
2330 put_packet(gdb_ctx->s, "");
2331 }
2332}
2333
Jon Doron7009d572019-05-29 09:41:46 +03002334static void handle_target_halt(GdbCmdContext *gdb_ctx, void *user_ctx)
2335{
2336 char thread_id[16];
2337
2338 gdb_fmt_thread_id(gdb_ctx->s, gdb_ctx->s->c_cpu, thread_id,
2339 sizeof(thread_id));
2340 snprintf(gdb_ctx->str_buf, sizeof(gdb_ctx->str_buf), "T%02xthread:%s;",
2341 GDB_SIGNAL_TRAP, thread_id);
2342 put_packet(gdb_ctx->s, gdb_ctx->str_buf);
2343 /*
2344 * Remove all the breakpoints when this query is issued,
2345 * because gdb is doing an initial connect and the state
2346 * should be cleaned up.
2347 */
2348 gdb_breakpoint_remove_all();
2349}
2350
Jon Doron2704efa2019-05-29 09:41:45 +03002351static int gdb_handle_packet(GDBState *s, const char *line_buf)
2352{
Jon Doron3e2c1262019-05-29 09:41:30 +03002353 const GdbCmdParseEntry *cmd_parser = NULL;
ths3b46e622007-09-17 08:09:54 +00002354
Doug Gale5c9522b2017-12-02 20:30:37 -05002355 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01002356
Jon Doron3f1cbac2019-05-29 09:41:47 +03002357 switch (line_buf[0]) {
Luc Michel53fd6552019-01-07 15:23:46 +00002358 case '!':
2359 put_packet(s, "OK");
2360 break;
bellard858693c2004-03-31 18:52:07 +00002361 case '?':
Jon Doron7009d572019-05-29 09:41:46 +03002362 {
2363 static const GdbCmdParseEntry target_halted_cmd_desc = {
2364 .handler = handle_target_halt,
2365 .cmd = "?",
2366 .cmd_startswith = 1
2367 };
2368 cmd_parser = &target_halted_cmd_desc;
2369 }
bellard858693c2004-03-31 18:52:07 +00002370 break;
2371 case 'c':
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002372 {
2373 static const GdbCmdParseEntry continue_cmd_desc = {
2374 .handler = handle_continue,
2375 .cmd = "c",
2376 .cmd_startswith = 1,
2377 .schema = "L0"
2378 };
2379 cmd_parser = &continue_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002380 }
Jon Doron4d6e3fe2019-05-29 09:41:32 +03002381 break;
edgar_igl1f487ee2008-05-17 22:20:53 +00002382 case 'C':
Jon Doronccc47d52019-05-29 09:41:33 +03002383 {
2384 static const GdbCmdParseEntry cont_with_sig_cmd_desc = {
2385 .handler = handle_cont_with_sig,
2386 .cmd = "C",
2387 .cmd_startswith = 1,
2388 .schema = "l0"
2389 };
2390 cmd_parser = &cont_with_sig_cmd_desc;
2391 }
2392 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002393 case 'v':
Jon Doron8536ec02019-05-29 09:41:44 +03002394 {
2395 static const GdbCmdParseEntry v_cmd_desc = {
2396 .handler = handle_v_commands,
2397 .cmd = "v",
2398 .cmd_startswith = 1,
2399 .schema = "s0"
2400 };
2401 cmd_parser = &v_cmd_desc;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02002402 }
Jon Doron8536ec02019-05-29 09:41:44 +03002403 break;
edgar_igl7d03f822008-05-17 18:58:29 +00002404 case 'k':
2405 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002406 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00002407 exit(0);
2408 case 'D':
Jon Doron3e2c1262019-05-29 09:41:30 +03002409 {
2410 static const GdbCmdParseEntry detach_cmd_desc = {
2411 .handler = handle_detach,
2412 .cmd = "D",
2413 .cmd_startswith = 1,
2414 .schema = "?.l0"
2415 };
2416 cmd_parser = &detach_cmd_desc;
Luc Michel546f3c62019-01-07 15:23:46 +00002417 }
edgar_igl7d03f822008-05-17 18:58:29 +00002418 break;
bellard858693c2004-03-31 18:52:07 +00002419 case 's':
Jon Doron933f80d2019-05-29 09:41:43 +03002420 {
2421 static const GdbCmdParseEntry step_cmd_desc = {
2422 .handler = handle_step,
2423 .cmd = "s",
2424 .cmd_startswith = 1,
2425 .schema = "L0"
2426 };
2427 cmd_parser = &step_cmd_desc;
bellard858693c2004-03-31 18:52:07 +00002428 }
Jon Doron933f80d2019-05-29 09:41:43 +03002429 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002430 case 'F':
2431 {
Jon Doron4b20fab2019-05-29 09:41:42 +03002432 static const GdbCmdParseEntry file_io_cmd_desc = {
2433 .handler = handle_file_io,
2434 .cmd = "F",
2435 .cmd_startswith = 1,
2436 .schema = "L,L,o0"
2437 };
2438 cmd_parser = &file_io_cmd_desc;
pbrooka2d1eba2007-01-28 03:10:55 +00002439 }
2440 break;
bellard858693c2004-03-31 18:52:07 +00002441 case 'g':
Jon Doron397d1372019-05-29 09:41:41 +03002442 {
2443 static const GdbCmdParseEntry read_all_regs_cmd_desc = {
2444 .handler = handle_read_all_regs,
2445 .cmd = "g",
2446 .cmd_startswith = 1
2447 };
2448 cmd_parser = &read_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002449 }
bellard858693c2004-03-31 18:52:07 +00002450 break;
2451 case 'G':
Jon Doron287ca122019-05-29 09:41:40 +03002452 {
2453 static const GdbCmdParseEntry write_all_regs_cmd_desc = {
2454 .handler = handle_write_all_regs,
2455 .cmd = "G",
2456 .cmd_startswith = 1,
2457 .schema = "s0"
2458 };
2459 cmd_parser = &write_all_regs_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002460 }
bellard858693c2004-03-31 18:52:07 +00002461 break;
2462 case 'm':
Jon Doronda92e232019-05-29 09:41:39 +03002463 {
2464 static const GdbCmdParseEntry read_mem_cmd_desc = {
2465 .handler = handle_read_mem,
2466 .cmd = "m",
2467 .cmd_startswith = 1,
2468 .schema = "L,L0"
2469 };
2470 cmd_parser = &read_mem_cmd_desc;
bellard6f970bd2005-12-05 19:55:19 +00002471 }
bellard858693c2004-03-31 18:52:07 +00002472 break;
2473 case 'M':
Jon Doroncc0ecc72019-05-29 09:41:38 +03002474 {
2475 static const GdbCmdParseEntry write_mem_cmd_desc = {
2476 .handler = handle_write_mem,
2477 .cmd = "M",
2478 .cmd_startswith = 1,
2479 .schema = "L,L:s0"
2480 };
2481 cmd_parser = &write_mem_cmd_desc;
Fabien Chouteau44520db2011-09-08 12:48:16 +02002482 }
bellard858693c2004-03-31 18:52:07 +00002483 break;
pbrook56aebc82008-10-11 17:55:29 +00002484 case 'p':
Jon Doron5d0e57b2019-05-29 09:41:37 +03002485 {
2486 static const GdbCmdParseEntry get_reg_cmd_desc = {
2487 .handler = handle_get_reg,
2488 .cmd = "p",
2489 .cmd_startswith = 1,
2490 .schema = "L0"
2491 };
2492 cmd_parser = &get_reg_cmd_desc;
pbrook56aebc82008-10-11 17:55:29 +00002493 }
2494 break;
2495 case 'P':
Jon Doron62b33202019-05-29 09:41:36 +03002496 {
2497 static const GdbCmdParseEntry set_reg_cmd_desc = {
2498 .handler = handle_set_reg,
2499 .cmd = "P",
2500 .cmd_startswith = 1,
2501 .schema = "L?s0"
2502 };
2503 cmd_parser = &set_reg_cmd_desc;
2504 }
pbrook56aebc82008-10-11 17:55:29 +00002505 break;
bellard858693c2004-03-31 18:52:07 +00002506 case 'Z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002507 {
2508 static const GdbCmdParseEntry insert_bp_cmd_desc = {
2509 .handler = handle_insert_bp,
2510 .cmd = "Z",
2511 .cmd_startswith = 1,
2512 .schema = "l?L?L0"
2513 };
2514 cmd_parser = &insert_bp_cmd_desc;
2515 }
2516 break;
bellard858693c2004-03-31 18:52:07 +00002517 case 'z':
Jon Doron77f6ce52019-05-29 09:41:35 +03002518 {
2519 static const GdbCmdParseEntry remove_bp_cmd_desc = {
2520 .handler = handle_remove_bp,
2521 .cmd = "z",
2522 .cmd_startswith = 1,
2523 .schema = "l?L?L0"
2524 };
2525 cmd_parser = &remove_bp_cmd_desc;
2526 }
bellard858693c2004-03-31 18:52:07 +00002527 break;
aliguori880a7572008-11-18 20:30:24 +00002528 case 'H':
Jon Doron3a9651d2019-05-29 09:41:34 +03002529 {
2530 static const GdbCmdParseEntry set_thread_cmd_desc = {
2531 .handler = handle_set_thread,
2532 .cmd = "H",
2533 .cmd_startswith = 1,
2534 .schema = "o.t0"
2535 };
2536 cmd_parser = &set_thread_cmd_desc;
aliguori880a7572008-11-18 20:30:24 +00002537 }
2538 break;
2539 case 'T':
Jon Doron44ffded2019-05-29 09:41:31 +03002540 {
2541 static const GdbCmdParseEntry thread_alive_cmd_desc = {
2542 .handler = handle_thread_alive,
2543 .cmd = "T",
2544 .cmd_startswith = 1,
2545 .schema = "t0"
2546 };
2547 cmd_parser = &thread_alive_cmd_desc;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07002548 }
aliguori880a7572008-11-18 20:30:24 +00002549 break;
pbrook978efd62006-06-17 18:30:42 +00002550 case 'q':
Jon Doron2704efa2019-05-29 09:41:45 +03002551 {
2552 static const GdbCmdParseEntry gen_query_cmd_desc = {
2553 .handler = handle_gen_query,
2554 .cmd = "q",
2555 .cmd_startswith = 1,
2556 .schema = "s0"
2557 };
2558 cmd_parser = &gen_query_cmd_desc;
2559 }
2560 break;
edgar_igl60897d32008-05-09 08:25:14 +00002561 case 'Q':
Jon Doron2704efa2019-05-29 09:41:45 +03002562 {
2563 static const GdbCmdParseEntry gen_set_cmd_desc = {
2564 .handler = handle_gen_set,
2565 .cmd = "Q",
2566 .cmd_startswith = 1,
2567 .schema = "s0"
2568 };
2569 cmd_parser = &gen_set_cmd_desc;
edgar_igl60897d32008-05-09 08:25:14 +00002570 }
Jon Doron2704efa2019-05-29 09:41:45 +03002571 break;
bellard858693c2004-03-31 18:52:07 +00002572 default:
bellard858693c2004-03-31 18:52:07 +00002573 /* put empty packet */
Jon Doron3f1cbac2019-05-29 09:41:47 +03002574 put_packet(s, "");
bellard858693c2004-03-31 18:52:07 +00002575 break;
2576 }
Jon Doron3e2c1262019-05-29 09:41:30 +03002577
2578 run_cmd_parser(s, line_buf, cmd_parser);
2579
bellard858693c2004-03-31 18:52:07 +00002580 return RS_IDLE;
2581}
2582
Andreas Färber64f6b342013-05-27 02:06:09 +02002583void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00002584{
Luc Michel160d8582019-01-07 15:23:46 +00002585 GDBProcess *p = gdb_get_cpu_process(gdbserver_state, cpu);
2586
2587 if (!p->attached) {
2588 /*
2589 * Having a stop CPU corresponding to a process that is not attached
2590 * confuses GDB. So we ignore the request.
2591 */
2592 return;
2593 }
2594
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002595 gdbserver_state->c_cpu = cpu;
2596 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00002597}
2598
bellard1fddef42005-04-17 19:16:13 +00002599#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002600static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00002601{
aliguori880a7572008-11-18 20:30:24 +00002602 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002603 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00002604 char buf[256];
Luc Michel95567c22019-01-07 15:23:46 +00002605 char thread_id[16];
aliguorid6fc1b32008-11-18 19:55:44 +00002606 const char *type;
bellard858693c2004-03-31 18:52:07 +00002607 int ret;
2608
Meador Ingecdb432b2012-03-15 17:49:45 +00002609 if (running || s->state == RS_INACTIVE) {
2610 return;
2611 }
2612 /* Is there a GDB syscall waiting to be sent? */
2613 if (s->current_syscall_cb) {
2614 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00002615 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01002616 }
Luc Michel95567c22019-01-07 15:23:46 +00002617
2618 if (cpu == NULL) {
2619 /* No process attached */
2620 return;
2621 }
2622
2623 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id));
2624
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03002625 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002626 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02002627 if (cpu->watchpoint_hit) {
2628 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00002629 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00002630 type = "r";
2631 break;
aliguoria1d1bb32008-11-18 20:07:32 +00002632 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00002633 type = "a";
2634 break;
2635 default:
2636 type = "";
2637 break;
2638 }
Doug Gale5c9522b2017-12-02 20:30:37 -05002639 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
2640 (target_ulong)cpu->watchpoint_hit->vaddr);
aliguori880a7572008-11-18 20:30:24 +00002641 snprintf(buf, sizeof(buf),
Luc Michel95567c22019-01-07 15:23:46 +00002642 "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
2643 GDB_SIGNAL_TRAP, thread_id, type,
Andreas Färberff4700b2013-08-26 18:23:18 +02002644 (target_ulong)cpu->watchpoint_hit->vaddr);
2645 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01002646 goto send_packet;
Doug Gale5c9522b2017-12-02 20:30:37 -05002647 } else {
2648 trace_gdbstub_hit_break();
pbrook6658ffb2007-03-16 23:58:11 +00002649 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07002650 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00002651 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01002652 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002653 case RUN_STATE_PAUSED:
Doug Gale5c9522b2017-12-02 20:30:37 -05002654 trace_gdbstub_hit_paused();
aliguori9781e042009-01-22 17:15:29 +00002655 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01002656 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002657 case RUN_STATE_SHUTDOWN:
Doug Gale5c9522b2017-12-02 20:30:37 -05002658 trace_gdbstub_hit_shutdown();
Jan Kiszka425189a2011-03-22 11:02:09 +01002659 ret = GDB_SIGNAL_QUIT;
2660 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002661 case RUN_STATE_IO_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002662 trace_gdbstub_hit_io_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002663 ret = GDB_SIGNAL_IO;
2664 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002665 case RUN_STATE_WATCHDOG:
Doug Gale5c9522b2017-12-02 20:30:37 -05002666 trace_gdbstub_hit_watchdog();
Jan Kiszka425189a2011-03-22 11:02:09 +01002667 ret = GDB_SIGNAL_ALRM;
2668 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002669 case RUN_STATE_INTERNAL_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05002670 trace_gdbstub_hit_internal_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01002671 ret = GDB_SIGNAL_ABRT;
2672 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002673 case RUN_STATE_SAVE_VM:
2674 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01002675 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002676 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01002677 ret = GDB_SIGNAL_XCPU;
2678 break;
2679 default:
Doug Gale5c9522b2017-12-02 20:30:37 -05002680 trace_gdbstub_hit_unknown(state);
Jan Kiszka425189a2011-03-22 11:02:09 +01002681 ret = GDB_SIGNAL_UNKNOWN;
2682 break;
bellardbbeb7b52006-04-23 18:42:15 +00002683 }
Jan Kiszka226d0072015-07-24 18:52:31 +02002684 gdb_set_stop_cpu(cpu);
Luc Michel95567c22019-01-07 15:23:46 +00002685 snprintf(buf, sizeof(buf), "T%02xthread:%s;", ret, thread_id);
Jan Kiszka425189a2011-03-22 11:02:09 +01002686
2687send_packet:
bellard858693c2004-03-31 18:52:07 +00002688 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01002689
2690 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02002691 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00002692}
bellard1fddef42005-04-17 19:16:13 +00002693#endif
bellard858693c2004-03-31 18:52:07 +00002694
pbrooka2d1eba2007-01-28 03:10:55 +00002695/* Send a gdb syscall request.
2696 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00002697 %x - target_ulong argument printed in hex.
2698 %lx - 64-bit argument printed in hex.
2699 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01002700void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00002701{
pbrooka2d1eba2007-01-28 03:10:55 +00002702 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00002703 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00002704 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00002705 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00002706 GDBState *s;
2707
aliguori880a7572008-11-18 20:30:24 +00002708 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00002709 if (!s)
2710 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00002711 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00002712#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002713 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00002714#endif
Meador Ingecdb432b2012-03-15 17:49:45 +00002715 p = s->syscall_buf;
2716 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00002717 *(p++) = 'F';
2718 while (*fmt) {
2719 if (*fmt == '%') {
2720 fmt++;
2721 switch (*fmt++) {
2722 case 'x':
2723 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002724 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00002725 break;
pbrooka87295e2007-05-26 15:09:38 +00002726 case 'l':
2727 if (*(fmt++) != 'x')
2728 goto bad_format;
2729 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00002730 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00002731 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002732 case 's':
2733 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00002734 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00002735 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00002736 break;
2737 default:
pbrooka87295e2007-05-26 15:09:38 +00002738 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08002739 error_report("gdbstub: Bad syscall format string '%s'",
2740 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00002741 break;
2742 }
2743 } else {
2744 *(p++) = *(fmt++);
2745 }
2746 }
pbrook8a93e022007-08-06 13:19:15 +00002747 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00002748#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00002749 put_packet(s, s->syscall_buf);
Peter Maydell4f710862018-05-15 19:19:58 +01002750 /* Return control to gdb for it to process the syscall request.
2751 * Since the protocol requires that gdb hands control back to us
2752 * using a "here are the results" F packet, we don't need to check
2753 * gdb_handlesig's return value (which is the signal to deliver if
2754 * execution was resumed via a continue packet).
2755 */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02002756 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00002757#else
Meador Ingecdb432b2012-03-15 17:49:45 +00002758 /* In this case wait to send the syscall packet until notification that
2759 the CPU has stopped. This must be done because if the packet is sent
2760 now the reply from the syscall request could be received while the CPU
2761 is still in the running state, which can cause packets to be dropped
2762 and state transition 'T' packets to be sent while the syscall is still
2763 being processed. */
Paolo Bonzini9102ded2015-08-18 06:52:09 -07002764 qemu_cpu_kick(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00002765#endif
2766}
2767
Peter Maydell19239b32015-09-07 10:39:27 +01002768void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
2769{
2770 va_list va;
2771
2772 va_start(va, fmt);
2773 gdb_do_syscallv(cb, fmt, va);
2774 va_end(va);
2775}
2776
Markus Armbruster33c846e2019-05-14 20:03:09 +02002777static void gdb_read_byte(GDBState *s, uint8_t ch)
bellard858693c2004-03-31 18:52:07 +00002778{
ths60fe76f2007-12-16 03:02:09 +00002779 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00002780
bellard1fddef42005-04-17 19:16:13 +00002781#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00002782 if (s->last_packet_len) {
2783 /* Waiting for a response to the last packet. If we see the start
2784 of a new command then abandon the previous response. */
2785 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002786 trace_gdbstub_err_got_nack();
thsffe8ab82007-12-16 03:16:05 +00002787 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
Alex Bennée118e2262017-07-12 11:52:13 +01002788 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002789 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01002790 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002791 trace_gdbstub_io_got_unexpected(ch);
pbrook4046d912007-01-28 01:53:16 +00002792 }
Alex Bennée118e2262017-07-12 11:52:13 +01002793
pbrook4046d912007-01-28 01:53:16 +00002794 if (ch == '+' || ch == '$')
2795 s->last_packet_len = 0;
2796 if (ch != '$')
2797 return;
2798 }
Luiz Capitulino13548692011-07-29 15:36:43 -03002799 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00002800 /* when the CPU is running, we cannot do anything except stop
2801 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002802 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00002803 } else
bellard1fddef42005-04-17 19:16:13 +00002804#endif
bellard41625032005-04-24 10:07:11 +00002805 {
bellard858693c2004-03-31 18:52:07 +00002806 switch(s->state) {
2807 case RS_IDLE:
2808 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04002809 /* start of command packet */
bellard858693c2004-03-31 18:52:07 +00002810 s->line_buf_index = 0;
Doug Gale4bf43122017-05-01 12:22:10 -04002811 s->line_sum = 0;
bellard858693c2004-03-31 18:52:07 +00002812 s->state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002813 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002814 trace_gdbstub_err_garbage(ch);
bellard4c3a88a2003-07-26 12:06:08 +00002815 }
2816 break;
bellard858693c2004-03-31 18:52:07 +00002817 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04002818 if (ch == '}') {
2819 /* start escape sequence */
2820 s->state = RS_GETLINE_ESC;
2821 s->line_sum += ch;
2822 } else if (ch == '*') {
2823 /* start run length encoding sequence */
2824 s->state = RS_GETLINE_RLE;
2825 s->line_sum += ch;
2826 } else if (ch == '#') {
2827 /* end of command, start of checksum*/
2828 s->state = RS_CHKSUM1;
bellard858693c2004-03-31 18:52:07 +00002829 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002830 trace_gdbstub_err_overrun();
bellard858693c2004-03-31 18:52:07 +00002831 s->state = RS_IDLE;
2832 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002833 /* unescaped command character */
2834 s->line_buf[s->line_buf_index++] = ch;
2835 s->line_sum += ch;
2836 }
2837 break;
2838 case RS_GETLINE_ESC:
2839 if (ch == '#') {
2840 /* unexpected end of command in escape sequence */
2841 s->state = RS_CHKSUM1;
2842 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2843 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05002844 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002845 s->state = RS_IDLE;
2846 } else {
2847 /* parse escaped character and leave escape state */
2848 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
2849 s->line_sum += ch;
2850 s->state = RS_GETLINE;
2851 }
2852 break;
2853 case RS_GETLINE_RLE:
Markus Armbruster046aba12019-05-14 20:03:08 +02002854 /*
2855 * Run-length encoding is explained in "Debugging with GDB /
2856 * Appendix E GDB Remote Serial Protocol / Overview".
2857 */
2858 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
Doug Gale4bf43122017-05-01 12:22:10 -04002859 /* invalid RLE count encoding */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002860 trace_gdbstub_err_invalid_repeat(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002861 s->state = RS_GETLINE;
2862 } else {
2863 /* decode repeat length */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002864 int repeat = ch - ' ' + 3;
Doug Gale4bf43122017-05-01 12:22:10 -04002865 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
2866 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05002867 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002868 s->state = RS_IDLE;
2869 } else if (s->line_buf_index < 1) {
2870 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05002871 trace_gdbstub_err_invalid_rle();
Doug Gale4bf43122017-05-01 12:22:10 -04002872 s->state = RS_GETLINE;
2873 } else {
2874 /* repeat the last character */
2875 memset(s->line_buf + s->line_buf_index,
2876 s->line_buf[s->line_buf_index - 1], repeat);
2877 s->line_buf_index += repeat;
2878 s->line_sum += ch;
2879 s->state = RS_GETLINE;
2880 }
bellard858693c2004-03-31 18:52:07 +00002881 }
2882 break;
2883 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04002884 /* get high hex digit of checksum */
2885 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002886 trace_gdbstub_err_checksum_invalid(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002887 s->state = RS_GETLINE;
2888 break;
2889 }
bellard858693c2004-03-31 18:52:07 +00002890 s->line_buf[s->line_buf_index] = '\0';
2891 s->line_csum = fromhex(ch) << 4;
2892 s->state = RS_CHKSUM2;
2893 break;
2894 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04002895 /* get low hex digit of checksum */
2896 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002897 trace_gdbstub_err_checksum_invalid(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002898 s->state = RS_GETLINE;
2899 break;
bellard858693c2004-03-31 18:52:07 +00002900 }
Doug Gale4bf43122017-05-01 12:22:10 -04002901 s->line_csum |= fromhex(ch);
2902
2903 if (s->line_csum != (s->line_sum & 0xff)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002904 trace_gdbstub_err_checksum_incorrect(s->line_sum, s->line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04002905 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00002906 reply = '-';
2907 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00002908 s->state = RS_IDLE;
2909 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002910 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00002911 reply = '+';
2912 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00002913 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00002914 }
bellardb4608c02003-06-27 17:34:32 +00002915 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002916 default:
2917 abort();
bellardb4608c02003-06-27 17:34:32 +00002918 }
2919 }
bellard858693c2004-03-31 18:52:07 +00002920}
2921
Paul Brook0e1c9c52010-06-16 13:03:51 +01002922/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002923void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01002924{
2925 GDBState *s;
2926 char buf[4];
2927
2928 s = gdbserver_state;
2929 if (!s) {
2930 return;
2931 }
2932#ifdef CONFIG_USER_ONLY
2933 if (gdbserver_fd < 0 || s->fd < 0) {
2934 return;
2935 }
2936#endif
2937
Doug Gale5c9522b2017-12-02 20:30:37 -05002938 trace_gdbstub_op_exiting((uint8_t)code);
2939
Paul Brook0e1c9c52010-06-16 13:03:51 +01002940 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
2941 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002942
2943#ifndef CONFIG_USER_ONLY
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002944 qemu_chr_fe_deinit(&s->chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002945#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01002946}
2947
Luc Michel8f468632019-01-07 15:23:45 +00002948/*
2949 * Create the process that will contain all the "orphan" CPUs (that are not
2950 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2951 * be attachable and thus will be invisible to the user.
2952 */
2953static void create_default_process(GDBState *s)
2954{
2955 GDBProcess *process;
2956 int max_pid = 0;
2957
2958 if (s->process_num) {
2959 max_pid = s->processes[s->process_num - 1].pid;
2960 }
2961
2962 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2963 process = &s->processes[s->process_num - 1];
2964
2965 /* We need an available PID slot for this process */
2966 assert(max_pid < UINT32_MAX);
2967
2968 process->pid = max_pid + 1;
2969 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00002970 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00002971}
2972
bellard1fddef42005-04-17 19:16:13 +00002973#ifdef CONFIG_USER_ONLY
2974int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02002975gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00002976{
Andreas Färber5ca666c2013-06-24 19:20:57 +02002977 GDBState *s;
2978 char buf[256];
2979 int n;
bellard1fddef42005-04-17 19:16:13 +00002980
Andreas Färber5ca666c2013-06-24 19:20:57 +02002981 s = gdbserver_state;
2982 if (gdbserver_fd < 0 || s->fd < 0) {
2983 return sig;
bellard1fddef42005-04-17 19:16:13 +00002984 }
2985
Andreas Färber5ca666c2013-06-24 19:20:57 +02002986 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02002987 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07002988 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00002989
Andreas Färber5ca666c2013-06-24 19:20:57 +02002990 if (sig != 0) {
2991 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
2992 put_packet(s, buf);
2993 }
2994 /* put_packet() might have detected that the peer terminated the
2995 connection. */
2996 if (s->fd < 0) {
2997 return sig;
2998 }
2999
3000 sig = 0;
3001 s->state = RS_IDLE;
3002 s->running_state = 0;
3003 while (s->running_state == 0) {
3004 n = read(s->fd, buf, 256);
3005 if (n > 0) {
3006 int i;
3007
3008 for (i = 0; i < n; i++) {
3009 gdb_read_byte(s, buf[i]);
3010 }
Peter Wu5819e3e2016-06-05 16:35:48 +02003011 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02003012 /* XXX: Connection closed. Should probably wait for another
3013 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02003014 if (n == 0) {
3015 close(s->fd);
3016 }
3017 s->fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02003018 return sig;
bellard1fddef42005-04-17 19:16:13 +00003019 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02003020 }
3021 sig = s->signal;
3022 s->signal = 0;
3023 return sig;
bellard1fddef42005-04-17 19:16:13 +00003024}
bellarde9009672005-04-26 20:42:36 +00003025
aurel32ca587a82008-12-18 22:44:13 +00003026/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01003027void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00003028{
Andreas Färber5ca666c2013-06-24 19:20:57 +02003029 GDBState *s;
3030 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00003031
Andreas Färber5ca666c2013-06-24 19:20:57 +02003032 s = gdbserver_state;
3033 if (gdbserver_fd < 0 || s->fd < 0) {
3034 return;
3035 }
aurel32ca587a82008-12-18 22:44:13 +00003036
Andreas Färber5ca666c2013-06-24 19:20:57 +02003037 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
3038 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00003039}
bellard1fddef42005-04-17 19:16:13 +00003040
Peter Maydell2f652222018-05-14 18:30:44 +01003041static bool gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00003042{
3043 GDBState *s;
3044 struct sockaddr_in sockaddr;
3045 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09003046 int fd;
bellard858693c2004-03-31 18:52:07 +00003047
3048 for(;;) {
3049 len = sizeof(sockaddr);
3050 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
3051 if (fd < 0 && errno != EINTR) {
3052 perror("accept");
Peter Maydell2f652222018-05-14 18:30:44 +01003053 return false;
bellard858693c2004-03-31 18:52:07 +00003054 } else if (fd >= 0) {
Peter Maydellf5bdd782018-05-14 18:30:43 +01003055 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00003056 break;
3057 }
3058 }
3059
3060 /* set short latency */
Peter Maydell2f652222018-05-14 18:30:44 +01003061 if (socket_set_nodelay(fd)) {
3062 perror("setsockopt");
Philippe Mathieu-Daudéead75d82018-05-24 19:34:58 -03003063 close(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01003064 return false;
3065 }
ths3b46e622007-09-17 08:09:54 +00003066
Anthony Liguori7267c092011-08-20 22:09:37 -05003067 s = g_malloc0(sizeof(GDBState));
Luc Michel8f468632019-01-07 15:23:45 +00003068 create_default_process(s);
Luc Michel970ed902019-01-07 15:23:46 +00003069 s->processes[0].attached = true;
3070 s->c_cpu = gdb_first_attached_cpu(s);
3071 s->g_cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00003072 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02003073 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00003074
aliguori880a7572008-11-18 20:30:24 +00003075 gdbserver_state = s;
Peter Maydell2f652222018-05-14 18:30:44 +01003076 return true;
bellard858693c2004-03-31 18:52:07 +00003077}
3078
3079static int gdbserver_open(int port)
3080{
3081 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02003082 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00003083
3084 fd = socket(PF_INET, SOCK_STREAM, 0);
3085 if (fd < 0) {
3086 perror("socket");
3087 return -1;
3088 }
Peter Maydellf5bdd782018-05-14 18:30:43 +01003089 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00003090
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02003091 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00003092
3093 sockaddr.sin_family = AF_INET;
3094 sockaddr.sin_port = htons(port);
3095 sockaddr.sin_addr.s_addr = 0;
3096 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
3097 if (ret < 0) {
3098 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00003099 close(fd);
bellard858693c2004-03-31 18:52:07 +00003100 return -1;
3101 }
Peter Wu96165b92016-05-04 11:32:17 +02003102 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00003103 if (ret < 0) {
3104 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00003105 close(fd);
bellard858693c2004-03-31 18:52:07 +00003106 return -1;
3107 }
bellard858693c2004-03-31 18:52:07 +00003108 return fd;
3109}
3110
3111int gdbserver_start(int port)
3112{
3113 gdbserver_fd = gdbserver_open(port);
3114 if (gdbserver_fd < 0)
3115 return -1;
3116 /* accept connections */
Peter Maydell2f652222018-05-14 18:30:44 +01003117 if (!gdb_accept()) {
3118 close(gdbserver_fd);
3119 gdbserver_fd = -1;
3120 return -1;
3121 }
bellardb4608c02003-06-27 17:34:32 +00003122 return 0;
3123}
aurel322b1319c2008-12-18 22:44:04 +00003124
3125/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07003126void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00003127{
3128 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02003129
3130 if (gdbserver_fd < 0 || s->fd < 0) {
3131 return;
3132 }
aurel322b1319c2008-12-18 22:44:04 +00003133 close(s->fd);
3134 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02003135 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02003136 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00003137}
pbrook4046d912007-01-28 01:53:16 +00003138#else
thsaa1f17c2007-07-11 22:48:58 +00003139static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00003140{
pbrook56aebc82008-10-11 17:55:29 +00003141 /* We can handle an arbitrarily large amount of data.
3142 Pick the maximum packet size, which is as good as anything. */
3143 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00003144}
3145
thsaa1f17c2007-07-11 22:48:58 +00003146static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00003147{
pbrook4046d912007-01-28 01:53:16 +00003148 int i;
3149
3150 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00003151 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00003152 }
3153}
3154
3155static void gdb_chr_event(void *opaque, int event)
3156{
Luc Michel970ed902019-01-07 15:23:46 +00003157 int i;
3158 GDBState *s = (GDBState *) opaque;
3159
pbrook4046d912007-01-28 01:53:16 +00003160 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05303161 case CHR_EVENT_OPENED:
Luc Michel970ed902019-01-07 15:23:46 +00003162 /* Start with first process attached, others detached */
3163 for (i = 0; i < s->process_num; i++) {
3164 s->processes[i].attached = !i;
3165 }
3166
3167 s->c_cpu = gdb_first_attached_cpu(s);
3168 s->g_cpu = s->c_cpu;
3169
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003170 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02003171 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00003172 break;
3173 default:
3174 break;
3175 }
3176}
3177
aliguori8a34a0f2009-03-05 23:01:55 +00003178static void gdb_monitor_output(GDBState *s, const char *msg, int len)
3179{
3180 char buf[MAX_PACKET_LENGTH];
3181
3182 buf[0] = 'O';
3183 if (len > (MAX_PACKET_LENGTH/2) - 1)
3184 len = (MAX_PACKET_LENGTH/2) - 1;
3185 memtohex(buf + 1, (uint8_t *)msg, len);
3186 put_packet(s, buf);
3187}
3188
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003189static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00003190{
3191 const char *p = (const char *)buf;
3192 int max_sz;
3193
3194 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
3195 for (;;) {
3196 if (len <= max_sz) {
3197 gdb_monitor_output(gdbserver_state, p, len);
3198 break;
3199 }
3200 gdb_monitor_output(gdbserver_state, p, max_sz);
3201 p += max_sz;
3202 len -= max_sz;
3203 }
3204 return len;
3205}
3206
aliguori59030a82009-04-05 18:43:41 +00003207#ifndef _WIN32
3208static void gdb_sigterm_handler(int signal)
3209{
Luiz Capitulino13548692011-07-29 15:36:43 -03003210 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03003211 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01003212 }
aliguori59030a82009-04-05 18:43:41 +00003213}
3214#endif
3215
Marc-André Lureau777357d2016-12-07 18:39:10 +03003216static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
3217 bool *be_opened, Error **errp)
3218{
3219 *be_opened = false;
3220}
3221
3222static void char_gdb_class_init(ObjectClass *oc, void *data)
3223{
3224 ChardevClass *cc = CHARDEV_CLASS(oc);
3225
3226 cc->internal = true;
3227 cc->open = gdb_monitor_open;
3228 cc->chr_write = gdb_monitor_write;
3229}
3230
3231#define TYPE_CHARDEV_GDB "chardev-gdb"
3232
3233static const TypeInfo char_gdb_type_info = {
3234 .name = TYPE_CHARDEV_GDB,
3235 .parent = TYPE_CHARDEV,
3236 .class_init = char_gdb_class_init,
3237};
3238
Luc Michel8f468632019-01-07 15:23:45 +00003239static int find_cpu_clusters(Object *child, void *opaque)
3240{
3241 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
3242 GDBState *s = (GDBState *) opaque;
3243 CPUClusterState *cluster = CPU_CLUSTER(child);
3244 GDBProcess *process;
3245
3246 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
3247
3248 process = &s->processes[s->process_num - 1];
3249
3250 /*
3251 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
3252 * runtime, we enforce here that the machine does not use a cluster ID
3253 * that would lead to PID 0.
3254 */
3255 assert(cluster->cluster_id != UINT32_MAX);
3256 process->pid = cluster->cluster_id + 1;
3257 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00003258 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00003259
3260 return 0;
3261 }
3262
3263 return object_child_foreach(child, find_cpu_clusters, opaque);
3264}
3265
3266static int pid_order(const void *a, const void *b)
3267{
3268 GDBProcess *pa = (GDBProcess *) a;
3269 GDBProcess *pb = (GDBProcess *) b;
3270
3271 if (pa->pid < pb->pid) {
3272 return -1;
3273 } else if (pa->pid > pb->pid) {
3274 return 1;
3275 } else {
3276 return 0;
3277 }
3278}
3279
3280static void create_processes(GDBState *s)
3281{
3282 object_child_foreach(object_get_root(), find_cpu_clusters, s);
3283
3284 if (s->processes) {
3285 /* Sort by PID */
3286 qsort(s->processes, s->process_num, sizeof(s->processes[0]), pid_order);
3287 }
3288
3289 create_default_process(s);
3290}
3291
3292static void cleanup_processes(GDBState *s)
3293{
3294 g_free(s->processes);
3295 s->process_num = 0;
3296 s->processes = NULL;
3297}
3298
aliguori59030a82009-04-05 18:43:41 +00003299int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00003300{
Doug Gale5c9522b2017-12-02 20:30:37 -05003301 trace_gdbstub_op_start(device);
3302
pbrook4046d912007-01-28 01:53:16 +00003303 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00003304 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03003305 Chardev *chr = NULL;
3306 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00003307
Ziyue Yang508b4ec2017-01-18 16:02:41 +08003308 if (!first_cpu) {
3309 error_report("gdbstub: meaningless to attach gdb to a "
3310 "machine without any CPU.");
3311 return -1;
3312 }
3313
aliguori59030a82009-04-05 18:43:41 +00003314 if (!device)
3315 return -1;
3316 if (strcmp(device, "none") != 0) {
3317 if (strstart(device, "tcp:", NULL)) {
3318 /* enforce required TCP attributes */
3319 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
3320 "%s,nowait,nodelay,server", device);
3321 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00003322 }
aliguori59030a82009-04-05 18:43:41 +00003323#ifndef _WIN32
3324 else if (strcmp(device, "stdio") == 0) {
3325 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00003326
aliguori59030a82009-04-05 18:43:41 +00003327 memset(&act, 0, sizeof(act));
3328 act.sa_handler = gdb_sigterm_handler;
3329 sigaction(SIGINT, &act, NULL);
3330 }
3331#endif
Marc-André Lureau95e30b22018-08-22 19:19:42 +02003332 /*
3333 * FIXME: it's a bit weird to allow using a mux chardev here
3334 * and implicitly setup a monitor. We may want to break this.
3335 */
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01003336 chr = qemu_chr_new_noreplay("gdb", device, true, NULL);
aliguori36556b22009-03-28 18:05:53 +00003337 if (!chr)
3338 return -1;
pbrookcfc34752007-02-22 01:48:01 +00003339 }
3340
aliguori36556b22009-03-28 18:05:53 +00003341 s = gdbserver_state;
3342 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05003343 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00003344 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00003345
aliguori36556b22009-03-28 18:05:53 +00003346 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
3347
3348 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03003349 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01003350 NULL, NULL, &error_abort);
Kevin Wolffbfc29e2019-06-13 17:34:04 +02003351 monitor_init_hmp(mon_chr, false);
aliguori36556b22009-03-28 18:05:53 +00003352 } else {
Marc-André Lureau1ce26102017-01-27 00:49:13 +04003353 qemu_chr_fe_deinit(&s->chr, true);
aliguori36556b22009-03-28 18:05:53 +00003354 mon_chr = s->mon_chr;
Luc Michel8f468632019-01-07 15:23:45 +00003355 cleanup_processes(s);
aliguori36556b22009-03-28 18:05:53 +00003356 memset(s, 0, sizeof(GDBState));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03003357 s->mon_chr = mon_chr;
aliguori36556b22009-03-28 18:05:53 +00003358 }
Luc Michel8f468632019-01-07 15:23:45 +00003359
3360 create_processes(s);
3361
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03003362 if (chr) {
3363 qemu_chr_fe_init(&s->chr, chr, &error_abort);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03003364 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
Luc Michel970ed902019-01-07 15:23:46 +00003365 gdb_chr_event, NULL, s, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03003366 }
aliguori36556b22009-03-28 18:05:53 +00003367 s->state = chr ? RS_IDLE : RS_INACTIVE;
3368 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00003369 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00003370
pbrook4046d912007-01-28 01:53:16 +00003371 return 0;
3372}
Marc-André Lureau777357d2016-12-07 18:39:10 +03003373
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01003374void gdbserver_cleanup(void)
3375{
3376 if (gdbserver_state) {
3377 put_packet(gdbserver_state, "W00");
3378 }
3379}
3380
Marc-André Lureau777357d2016-12-07 18:39:10 +03003381static void register_types(void)
3382{
3383 type_register_static(&char_gdb_type_info);
3384}
3385
3386type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00003387#endif