blob: 9159d7d15314f58ae9a38b55f648b364e3ef7d18 [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 Armbrusterda34e652016-03-14 09:01:28 +010021#include "qapi/error.h"
Ziyue Yang508b4ec2017-01-18 16:02:41 +080022#include "qemu/error-report.h"
Markus Armbruster856dfd82019-05-23 16:35:06 +020023#include "qemu/ctype.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020024#include "qemu/cutils.h"
Doug Gale5c9522b2017-12-02 20:30:37 -050025#include "trace-root.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020026#ifdef CONFIG_USER_ONLY
bellard1fddef42005-04-17 19:16:13 +000027#include "qemu.h"
28#else
Paolo Bonzini83c90892012-12-17 18:19:49 +010029#include "monitor/monitor.h"
Marc-André Lureau8228e352017-01-26 17:19:46 +040030#include "chardev/char.h"
Marc-André Lureau4d43a602017-01-26 18:26:44 +040031#include "chardev/char-fe.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010032#include "sysemu/sysemu.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010033#include "exec/gdbstub.h"
Luc Michel8f468632019-01-07 15:23:45 +000034#include "hw/cpu/cluster.h"
bellard1fddef42005-04-17 19:16:13 +000035#endif
bellard67b915a2004-03-31 23:37:16 +000036
pbrook56aebc82008-10-11 17:55:29 +000037#define MAX_PACKET_LENGTH 4096
38
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010039#include "qemu/sockets.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010040#include "sysemu/hw_accel.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010041#include "sysemu/kvm.h"
Alex Bennéef1672e62019-05-13 14:43:57 +010042#include "hw/semihosting/semihost.h"
Paolo Bonzini63c91552016-03-15 13:18:37 +010043#include "exec/exec-all.h"
aurel32ca587a82008-12-18 22:44:13 +000044
Jan Kiszkaa3919382015-02-07 09:38:44 +010045#ifdef CONFIG_USER_ONLY
46#define GDB_ATTACHED "0"
47#else
48#define GDB_ATTACHED "1"
49#endif
50
Andreas Färberf3659ee2013-06-27 19:09:09 +020051static inline int target_memory_rw_debug(CPUState *cpu, target_ulong addr,
52 uint8_t *buf, int len, bool is_write)
Fabien Chouteau44520db2011-09-08 12:48:16 +020053{
Andreas Färberf3659ee2013-06-27 19:09:09 +020054 CPUClass *cc = CPU_GET_CLASS(cpu);
55
56 if (cc->memory_rw_debug) {
57 return cc->memory_rw_debug(cpu, addr, buf, len, is_write);
58 }
59 return cpu_memory_rw_debug(cpu, addr, buf, len, is_write);
Fabien Chouteau44520db2011-09-08 12:48:16 +020060}
aurel32ca587a82008-12-18 22:44:13 +000061
Alex Bennéed2a6c852017-07-12 11:52:14 +010062/* Return the GDB index for a given vCPU state.
63 *
64 * For user mode this is simply the thread id. In system mode GDB
65 * numbers CPUs from 1 as 0 is reserved as an "any cpu" index.
66 */
67static inline int cpu_gdb_index(CPUState *cpu)
68{
69#if defined(CONFIG_USER_ONLY)
Alex Bennéebd88c782017-07-12 11:52:15 +010070 TaskState *ts = (TaskState *) cpu->opaque;
71 return ts->ts_tid;
Alex Bennéed2a6c852017-07-12 11:52:14 +010072#else
73 return cpu->cpu_index + 1;
74#endif
75}
76
aurel32ca587a82008-12-18 22:44:13 +000077enum {
78 GDB_SIGNAL_0 = 0,
79 GDB_SIGNAL_INT = 2,
Jan Kiszka425189a2011-03-22 11:02:09 +010080 GDB_SIGNAL_QUIT = 3,
aurel32ca587a82008-12-18 22:44:13 +000081 GDB_SIGNAL_TRAP = 5,
Jan Kiszka425189a2011-03-22 11:02:09 +010082 GDB_SIGNAL_ABRT = 6,
83 GDB_SIGNAL_ALRM = 14,
84 GDB_SIGNAL_IO = 23,
85 GDB_SIGNAL_XCPU = 24,
aurel32ca587a82008-12-18 22:44:13 +000086 GDB_SIGNAL_UNKNOWN = 143
87};
88
89#ifdef CONFIG_USER_ONLY
90
91/* Map target signal numbers to GDB protocol signal numbers and vice
92 * versa. For user emulation's currently supported systems, we can
93 * assume most signals are defined.
94 */
95
96static int gdb_signal_table[] = {
97 0,
98 TARGET_SIGHUP,
99 TARGET_SIGINT,
100 TARGET_SIGQUIT,
101 TARGET_SIGILL,
102 TARGET_SIGTRAP,
103 TARGET_SIGABRT,
104 -1, /* SIGEMT */
105 TARGET_SIGFPE,
106 TARGET_SIGKILL,
107 TARGET_SIGBUS,
108 TARGET_SIGSEGV,
109 TARGET_SIGSYS,
110 TARGET_SIGPIPE,
111 TARGET_SIGALRM,
112 TARGET_SIGTERM,
113 TARGET_SIGURG,
114 TARGET_SIGSTOP,
115 TARGET_SIGTSTP,
116 TARGET_SIGCONT,
117 TARGET_SIGCHLD,
118 TARGET_SIGTTIN,
119 TARGET_SIGTTOU,
120 TARGET_SIGIO,
121 TARGET_SIGXCPU,
122 TARGET_SIGXFSZ,
123 TARGET_SIGVTALRM,
124 TARGET_SIGPROF,
125 TARGET_SIGWINCH,
126 -1, /* SIGLOST */
127 TARGET_SIGUSR1,
128 TARGET_SIGUSR2,
blueswir1c72d5bf2009-01-15 17:27:45 +0000129#ifdef TARGET_SIGPWR
aurel32ca587a82008-12-18 22:44:13 +0000130 TARGET_SIGPWR,
blueswir1c72d5bf2009-01-15 17:27:45 +0000131#else
132 -1,
133#endif
aurel32ca587a82008-12-18 22:44:13 +0000134 -1, /* SIGPOLL */
135 -1,
136 -1,
137 -1,
138 -1,
139 -1,
140 -1,
141 -1,
142 -1,
143 -1,
144 -1,
145 -1,
blueswir1c72d5bf2009-01-15 17:27:45 +0000146#ifdef __SIGRTMIN
aurel32ca587a82008-12-18 22:44:13 +0000147 __SIGRTMIN + 1,
148 __SIGRTMIN + 2,
149 __SIGRTMIN + 3,
150 __SIGRTMIN + 4,
151 __SIGRTMIN + 5,
152 __SIGRTMIN + 6,
153 __SIGRTMIN + 7,
154 __SIGRTMIN + 8,
155 __SIGRTMIN + 9,
156 __SIGRTMIN + 10,
157 __SIGRTMIN + 11,
158 __SIGRTMIN + 12,
159 __SIGRTMIN + 13,
160 __SIGRTMIN + 14,
161 __SIGRTMIN + 15,
162 __SIGRTMIN + 16,
163 __SIGRTMIN + 17,
164 __SIGRTMIN + 18,
165 __SIGRTMIN + 19,
166 __SIGRTMIN + 20,
167 __SIGRTMIN + 21,
168 __SIGRTMIN + 22,
169 __SIGRTMIN + 23,
170 __SIGRTMIN + 24,
171 __SIGRTMIN + 25,
172 __SIGRTMIN + 26,
173 __SIGRTMIN + 27,
174 __SIGRTMIN + 28,
175 __SIGRTMIN + 29,
176 __SIGRTMIN + 30,
177 __SIGRTMIN + 31,
178 -1, /* SIGCANCEL */
179 __SIGRTMIN,
180 __SIGRTMIN + 32,
181 __SIGRTMIN + 33,
182 __SIGRTMIN + 34,
183 __SIGRTMIN + 35,
184 __SIGRTMIN + 36,
185 __SIGRTMIN + 37,
186 __SIGRTMIN + 38,
187 __SIGRTMIN + 39,
188 __SIGRTMIN + 40,
189 __SIGRTMIN + 41,
190 __SIGRTMIN + 42,
191 __SIGRTMIN + 43,
192 __SIGRTMIN + 44,
193 __SIGRTMIN + 45,
194 __SIGRTMIN + 46,
195 __SIGRTMIN + 47,
196 __SIGRTMIN + 48,
197 __SIGRTMIN + 49,
198 __SIGRTMIN + 50,
199 __SIGRTMIN + 51,
200 __SIGRTMIN + 52,
201 __SIGRTMIN + 53,
202 __SIGRTMIN + 54,
203 __SIGRTMIN + 55,
204 __SIGRTMIN + 56,
205 __SIGRTMIN + 57,
206 __SIGRTMIN + 58,
207 __SIGRTMIN + 59,
208 __SIGRTMIN + 60,
209 __SIGRTMIN + 61,
210 __SIGRTMIN + 62,
211 __SIGRTMIN + 63,
212 __SIGRTMIN + 64,
213 __SIGRTMIN + 65,
214 __SIGRTMIN + 66,
215 __SIGRTMIN + 67,
216 __SIGRTMIN + 68,
217 __SIGRTMIN + 69,
218 __SIGRTMIN + 70,
219 __SIGRTMIN + 71,
220 __SIGRTMIN + 72,
221 __SIGRTMIN + 73,
222 __SIGRTMIN + 74,
223 __SIGRTMIN + 75,
224 __SIGRTMIN + 76,
225 __SIGRTMIN + 77,
226 __SIGRTMIN + 78,
227 __SIGRTMIN + 79,
228 __SIGRTMIN + 80,
229 __SIGRTMIN + 81,
230 __SIGRTMIN + 82,
231 __SIGRTMIN + 83,
232 __SIGRTMIN + 84,
233 __SIGRTMIN + 85,
234 __SIGRTMIN + 86,
235 __SIGRTMIN + 87,
236 __SIGRTMIN + 88,
237 __SIGRTMIN + 89,
238 __SIGRTMIN + 90,
239 __SIGRTMIN + 91,
240 __SIGRTMIN + 92,
241 __SIGRTMIN + 93,
242 __SIGRTMIN + 94,
243 __SIGRTMIN + 95,
244 -1, /* SIGINFO */
245 -1, /* UNKNOWN */
246 -1, /* DEFAULT */
247 -1,
248 -1,
249 -1,
250 -1,
251 -1,
252 -1
blueswir1c72d5bf2009-01-15 17:27:45 +0000253#endif
aurel32ca587a82008-12-18 22:44:13 +0000254};
bellard8f447cc2006-06-14 15:21:14 +0000255#else
aurel32ca587a82008-12-18 22:44:13 +0000256/* In system mode we only need SIGINT and SIGTRAP; other signals
257 are not yet supported. */
258
259enum {
260 TARGET_SIGINT = 2,
261 TARGET_SIGTRAP = 5
262};
263
264static int gdb_signal_table[] = {
265 -1,
266 -1,
267 TARGET_SIGINT,
268 -1,
269 -1,
270 TARGET_SIGTRAP
271};
bellard8f447cc2006-06-14 15:21:14 +0000272#endif
bellardb4608c02003-06-27 17:34:32 +0000273
aurel32ca587a82008-12-18 22:44:13 +0000274#ifdef CONFIG_USER_ONLY
275static int target_signal_to_gdb (int sig)
276{
277 int i;
278 for (i = 0; i < ARRAY_SIZE (gdb_signal_table); i++)
279 if (gdb_signal_table[i] == sig)
280 return i;
281 return GDB_SIGNAL_UNKNOWN;
282}
283#endif
284
285static int gdb_signal_to_target (int sig)
286{
287 if (sig < ARRAY_SIZE (gdb_signal_table))
288 return gdb_signal_table[sig];
289 else
290 return -1;
291}
292
pbrook56aebc82008-10-11 17:55:29 +0000293typedef struct GDBRegisterState {
294 int base_reg;
295 int num_regs;
296 gdb_reg_cb get_reg;
297 gdb_reg_cb set_reg;
298 const char *xml;
299 struct GDBRegisterState *next;
300} GDBRegisterState;
301
Luc Michel8f468632019-01-07 15:23:45 +0000302typedef struct GDBProcess {
303 uint32_t pid;
304 bool attached;
Luc Michelc145eea2019-01-07 15:23:46 +0000305
306 char target_xml[1024];
Luc Michel8f468632019-01-07 15:23:45 +0000307} GDBProcess;
308
bellard858693c2004-03-31 18:52:07 +0000309enum RSState {
aliguori36556b22009-03-28 18:05:53 +0000310 RS_INACTIVE,
bellard858693c2004-03-31 18:52:07 +0000311 RS_IDLE,
312 RS_GETLINE,
Doug Gale4bf43122017-05-01 12:22:10 -0400313 RS_GETLINE_ESC,
314 RS_GETLINE_RLE,
bellard858693c2004-03-31 18:52:07 +0000315 RS_CHKSUM1,
316 RS_CHKSUM2,
317};
bellard858693c2004-03-31 18:52:07 +0000318typedef struct GDBState {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200319 CPUState *c_cpu; /* current CPU for step/continue ops */
320 CPUState *g_cpu; /* current CPU for other ops */
Andreas Färber52f34622013-06-27 13:44:40 +0200321 CPUState *query_cpu; /* for q{f|s}ThreadInfo */
bellard41625032005-04-24 10:07:11 +0000322 enum RSState state; /* parsing state */
pbrook56aebc82008-10-11 17:55:29 +0000323 char line_buf[MAX_PACKET_LENGTH];
bellard858693c2004-03-31 18:52:07 +0000324 int line_buf_index;
Doug Gale4bf43122017-05-01 12:22:10 -0400325 int line_sum; /* running checksum */
326 int line_csum; /* checksum at the end of the packet */
pbrook56aebc82008-10-11 17:55:29 +0000327 uint8_t last_packet[MAX_PACKET_LENGTH + 4];
pbrook4046d912007-01-28 01:53:16 +0000328 int last_packet_len;
edgar_igl1f487ee2008-05-17 22:20:53 +0000329 int signal;
bellard41625032005-04-24 10:07:11 +0000330#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000331 int fd;
bellard41625032005-04-24 10:07:11 +0000332 int running_state;
pbrook4046d912007-01-28 01:53:16 +0000333#else
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +0300334 CharBackend chr;
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +0300335 Chardev *mon_chr;
bellard41625032005-04-24 10:07:11 +0000336#endif
Luc Michel8f468632019-01-07 15:23:45 +0000337 bool multiprocess;
338 GDBProcess *processes;
339 int process_num;
Meador Ingecdb432b2012-03-15 17:49:45 +0000340 char syscall_buf[256];
341 gdb_syscall_complete_cb current_syscall_cb;
bellard858693c2004-03-31 18:52:07 +0000342} GDBState;
bellardb4608c02003-06-27 17:34:32 +0000343
edgar_igl60897d32008-05-09 08:25:14 +0000344/* By default use no IRQs and no timers while single stepping so as to
345 * make single stepping like an ICE HW step.
346 */
347static int sstep_flags = SSTEP_ENABLE|SSTEP_NOIRQ|SSTEP_NOTIMER;
348
aliguori880a7572008-11-18 20:30:24 +0000349static GDBState *gdbserver_state;
350
Andreas Färber5b50e792013-06-29 04:18:45 +0200351bool gdb_has_xml;
pbrook56aebc82008-10-11 17:55:29 +0000352
bellard1fddef42005-04-17 19:16:13 +0000353#ifdef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +0000354/* XXX: This is not thread safe. Do we care? */
355static int gdbserver_fd = -1;
356
bellard858693c2004-03-31 18:52:07 +0000357static int get_char(GDBState *s)
bellardb4608c02003-06-27 17:34:32 +0000358{
359 uint8_t ch;
360 int ret;
361
362 for(;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000363 ret = qemu_recv(s->fd, &ch, 1, 0);
bellardb4608c02003-06-27 17:34:32 +0000364 if (ret < 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000365 if (errno == ECONNRESET)
366 s->fd = -1;
Peter Wu5819e3e2016-06-05 16:35:48 +0200367 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000368 return -1;
369 } else if (ret == 0) {
edgar_igl1f487ee2008-05-17 22:20:53 +0000370 close(s->fd);
371 s->fd = -1;
bellardb4608c02003-06-27 17:34:32 +0000372 return -1;
373 } else {
374 break;
375 }
376 }
377 return ch;
378}
pbrook4046d912007-01-28 01:53:16 +0000379#endif
bellardb4608c02003-06-27 17:34:32 +0000380
blueswir1654efcf2009-04-18 07:29:59 +0000381static enum {
pbrooka2d1eba2007-01-28 03:10:55 +0000382 GDB_SYS_UNKNOWN,
383 GDB_SYS_ENABLED,
384 GDB_SYS_DISABLED,
385} gdb_syscall_mode;
386
Liviu Ionescua38bb072014-12-11 12:07:48 +0000387/* Decide if either remote gdb syscalls or native file IO should be used. */
pbrooka2d1eba2007-01-28 03:10:55 +0000388int use_gdb_syscalls(void)
389{
Leon Alraecfe67ce2015-06-19 14:17:45 +0100390 SemihostingTarget target = semihosting_get_target();
391 if (target == SEMIHOSTING_TARGET_NATIVE) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000392 /* -semihosting-config target=native */
393 return false;
Leon Alraecfe67ce2015-06-19 14:17:45 +0100394 } else if (target == SEMIHOSTING_TARGET_GDB) {
Liviu Ionescua38bb072014-12-11 12:07:48 +0000395 /* -semihosting-config target=gdb */
396 return true;
397 }
398
399 /* -semihosting-config target=auto */
400 /* On the first call check if gdb is connected and remember. */
pbrooka2d1eba2007-01-28 03:10:55 +0000401 if (gdb_syscall_mode == GDB_SYS_UNKNOWN) {
aliguori880a7572008-11-18 20:30:24 +0000402 gdb_syscall_mode = (gdbserver_state ? GDB_SYS_ENABLED
403 : GDB_SYS_DISABLED);
pbrooka2d1eba2007-01-28 03:10:55 +0000404 }
405 return gdb_syscall_mode == GDB_SYS_ENABLED;
406}
407
edgar_iglba70a622008-03-14 06:10:42 +0000408/* Resume execution. */
409static inline void gdb_continue(GDBState *s)
410{
Doug Gale5c9522b2017-12-02 20:30:37 -0500411
edgar_iglba70a622008-03-14 06:10:42 +0000412#ifdef CONFIG_USER_ONLY
413 s->running_state = 1;
Doug Gale5c9522b2017-12-02 20:30:37 -0500414 trace_gdbstub_op_continue();
edgar_iglba70a622008-03-14 06:10:42 +0000415#else
Paolo Bonzini26ac7a32013-06-03 17:06:54 +0200416 if (!runstate_needs_reset()) {
Doug Gale5c9522b2017-12-02 20:30:37 -0500417 trace_gdbstub_op_continue();
Paolo Bonzini87f25c12013-05-30 13:20:40 +0200418 vm_start();
419 }
edgar_iglba70a622008-03-14 06:10:42 +0000420#endif
421}
422
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100423/*
424 * Resume execution, per CPU actions. For user-mode emulation it's
425 * equivalent to gdb_continue.
426 */
427static int gdb_continue_partial(GDBState *s, char *newstates)
428{
429 CPUState *cpu;
430 int res = 0;
431#ifdef CONFIG_USER_ONLY
432 /*
433 * This is not exactly accurate, but it's an improvement compared to the
434 * previous situation, where only one CPU would be single-stepped.
435 */
436 CPU_FOREACH(cpu) {
437 if (newstates[cpu->cpu_index] == 's') {
Doug Gale5c9522b2017-12-02 20:30:37 -0500438 trace_gdbstub_op_stepping(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100439 cpu_single_step(cpu, sstep_flags);
440 }
441 }
442 s->running_state = 1;
443#else
444 int flag = 0;
445
446 if (!runstate_needs_reset()) {
447 if (vm_prepare_start()) {
448 return 0;
449 }
450
451 CPU_FOREACH(cpu) {
452 switch (newstates[cpu->cpu_index]) {
453 case 0:
454 case 1:
455 break; /* nothing to do here */
456 case '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 cpu_resume(cpu);
460 flag = 1;
461 break;
462 case 'c':
Doug Gale5c9522b2017-12-02 20:30:37 -0500463 trace_gdbstub_op_continue_cpu(cpu->cpu_index);
Claudio Imbrenda544177a2017-02-14 18:07:48 +0100464 cpu_resume(cpu);
465 flag = 1;
466 break;
467 default:
468 res = -1;
469 break;
470 }
471 }
472 }
473 if (flag) {
474 qemu_clock_enable(QEMU_CLOCK_VIRTUAL, true);
475 }
476#endif
477 return res;
478}
479
bellard858693c2004-03-31 18:52:07 +0000480static void put_buffer(GDBState *s, const uint8_t *buf, int len)
bellardb4608c02003-06-27 17:34:32 +0000481{
pbrook4046d912007-01-28 01:53:16 +0000482#ifdef CONFIG_USER_ONLY
bellardb4608c02003-06-27 17:34:32 +0000483 int ret;
484
485 while (len > 0) {
bellard8f447cc2006-06-14 15:21:14 +0000486 ret = send(s->fd, buf, len, 0);
bellardb4608c02003-06-27 17:34:32 +0000487 if (ret < 0) {
Peter Wu5819e3e2016-06-05 16:35:48 +0200488 if (errno != EINTR)
bellardb4608c02003-06-27 17:34:32 +0000489 return;
490 } else {
491 buf += ret;
492 len -= ret;
493 }
494 }
pbrook4046d912007-01-28 01:53:16 +0000495#else
Daniel P. Berrange6ab3fc32016-09-06 14:56:04 +0100496 /* XXX this blocks entire thread. Rewrite to use
497 * qemu_chr_fe_write and background I/O callbacks */
Marc-André Lureau5345fdb2016-10-22 12:52:55 +0300498 qemu_chr_fe_write_all(&s->chr, buf, len);
pbrook4046d912007-01-28 01:53:16 +0000499#endif
bellardb4608c02003-06-27 17:34:32 +0000500}
501
502static inline int fromhex(int v)
503{
504 if (v >= '0' && v <= '9')
505 return v - '0';
506 else if (v >= 'A' && v <= 'F')
507 return v - 'A' + 10;
508 else if (v >= 'a' && v <= 'f')
509 return v - 'a' + 10;
510 else
511 return 0;
512}
513
514static inline int tohex(int v)
515{
516 if (v < 10)
517 return v + '0';
518 else
519 return v - 10 + 'a';
520}
521
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -0300522/* writes 2*len+1 bytes in buf */
bellardb4608c02003-06-27 17:34:32 +0000523static void memtohex(char *buf, const uint8_t *mem, int len)
524{
525 int i, c;
526 char *q;
527 q = buf;
528 for(i = 0; i < len; i++) {
529 c = mem[i];
530 *q++ = tohex(c >> 4);
531 *q++ = tohex(c & 0xf);
532 }
533 *q = '\0';
534}
535
536static void hextomem(uint8_t *mem, const char *buf, int len)
537{
538 int i;
539
540 for(i = 0; i < len; i++) {
541 mem[i] = (fromhex(buf[0]) << 4) | fromhex(buf[1]);
542 buf += 2;
543 }
544}
545
Doug Gale5c9522b2017-12-02 20:30:37 -0500546static void hexdump(const char *buf, int len,
547 void (*trace_fn)(size_t ofs, char const *text))
548{
549 char line_buffer[3 * 16 + 4 + 16 + 1];
550
551 size_t i;
552 for (i = 0; i < len || (i & 0xF); ++i) {
553 size_t byte_ofs = i & 15;
554
555 if (byte_ofs == 0) {
556 memset(line_buffer, ' ', 3 * 16 + 4 + 16);
557 line_buffer[3 * 16 + 4 + 16] = 0;
558 }
559
560 size_t col_group = (i >> 2) & 3;
561 size_t hex_col = byte_ofs * 3 + col_group;
562 size_t txt_col = 3 * 16 + 4 + byte_ofs;
563
564 if (i < len) {
565 char value = buf[i];
566
567 line_buffer[hex_col + 0] = tohex((value >> 4) & 0xF);
568 line_buffer[hex_col + 1] = tohex((value >> 0) & 0xF);
569 line_buffer[txt_col + 0] = (value >= ' ' && value < 127)
570 ? value
571 : '.';
572 }
573
574 if (byte_ofs == 0xF)
575 trace_fn(i & -16, line_buffer);
576 }
577}
578
bellardb4608c02003-06-27 17:34:32 +0000579/* return -1 if error, 0 if OK */
Doug Gale5c9522b2017-12-02 20:30:37 -0500580static int put_packet_binary(GDBState *s, const char *buf, int len, bool dump)
bellardb4608c02003-06-27 17:34:32 +0000581{
pbrook56aebc82008-10-11 17:55:29 +0000582 int csum, i;
ths60fe76f2007-12-16 03:02:09 +0000583 uint8_t *p;
bellardb4608c02003-06-27 17:34:32 +0000584
Doug Gale5c9522b2017-12-02 20:30:37 -0500585 if (dump && trace_event_get_state_backends(TRACE_GDBSTUB_IO_BINARYREPLY)) {
586 hexdump(buf, len, trace_gdbstub_io_binaryreply);
587 }
588
bellardb4608c02003-06-27 17:34:32 +0000589 for(;;) {
pbrook4046d912007-01-28 01:53:16 +0000590 p = s->last_packet;
591 *(p++) = '$';
pbrook4046d912007-01-28 01:53:16 +0000592 memcpy(p, buf, len);
593 p += len;
bellardb4608c02003-06-27 17:34:32 +0000594 csum = 0;
595 for(i = 0; i < len; i++) {
596 csum += buf[i];
597 }
pbrook4046d912007-01-28 01:53:16 +0000598 *(p++) = '#';
599 *(p++) = tohex((csum >> 4) & 0xf);
600 *(p++) = tohex((csum) & 0xf);
bellardb4608c02003-06-27 17:34:32 +0000601
pbrook4046d912007-01-28 01:53:16 +0000602 s->last_packet_len = p - s->last_packet;
thsffe8ab82007-12-16 03:16:05 +0000603 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
bellardb4608c02003-06-27 17:34:32 +0000604
pbrook4046d912007-01-28 01:53:16 +0000605#ifdef CONFIG_USER_ONLY
606 i = get_char(s);
607 if (i < 0)
bellardb4608c02003-06-27 17:34:32 +0000608 return -1;
pbrook4046d912007-01-28 01:53:16 +0000609 if (i == '+')
bellardb4608c02003-06-27 17:34:32 +0000610 break;
pbrook4046d912007-01-28 01:53:16 +0000611#else
612 break;
613#endif
bellardb4608c02003-06-27 17:34:32 +0000614 }
615 return 0;
616}
617
pbrook56aebc82008-10-11 17:55:29 +0000618/* return -1 if error, 0 if OK */
619static int put_packet(GDBState *s, const char *buf)
620{
Doug Gale5c9522b2017-12-02 20:30:37 -0500621 trace_gdbstub_io_reply(buf);
pbrook56aebc82008-10-11 17:55:29 +0000622
Doug Gale5c9522b2017-12-02 20:30:37 -0500623 return put_packet_binary(s, buf, strlen(buf), false);
pbrook56aebc82008-10-11 17:55:29 +0000624}
625
pbrook56aebc82008-10-11 17:55:29 +0000626/* Encode data using the encoding for 'x' packets. */
627static int memtox(char *buf, const char *mem, int len)
628{
629 char *p = buf;
630 char c;
631
632 while (len--) {
633 c = *(mem++);
634 switch (c) {
635 case '#': case '$': case '*': case '}':
636 *(p++) = '}';
637 *(p++) = c ^ 0x20;
638 break;
639 default:
640 *(p++) = c;
641 break;
642 }
643 }
644 return p - buf;
645}
646
Luc Michel1a227332019-01-07 15:23:45 +0000647static uint32_t gdb_get_cpu_pid(const GDBState *s, CPUState *cpu)
648{
Luc Michel1a227332019-01-07 15:23:45 +0000649 /* TODO: In user mode, we should use the task state PID */
Peter Maydell46f5abc2019-01-29 11:46:06 +0000650 if (cpu->cluster_index == UNASSIGNED_CLUSTER_INDEX) {
651 /* Return the default process' PID */
652 return s->processes[s->process_num - 1].pid;
653 }
654 return cpu->cluster_index + 1;
Luc Michel1a227332019-01-07 15:23:45 +0000655}
656
Luc Michel7d8c87d2019-01-07 15:23:45 +0000657static GDBProcess *gdb_get_process(const GDBState *s, uint32_t pid)
658{
659 int i;
660
661 if (!pid) {
662 /* 0 means any process, we take the first one */
663 return &s->processes[0];
664 }
665
666 for (i = 0; i < s->process_num; i++) {
667 if (s->processes[i].pid == pid) {
668 return &s->processes[i];
669 }
670 }
671
672 return NULL;
673}
674
675static GDBProcess *gdb_get_cpu_process(const GDBState *s, CPUState *cpu)
676{
677 return gdb_get_process(s, gdb_get_cpu_pid(s, cpu));
678}
679
680static CPUState *find_cpu(uint32_t thread_id)
681{
682 CPUState *cpu;
683
684 CPU_FOREACH(cpu) {
685 if (cpu_gdb_index(cpu) == thread_id) {
686 return cpu;
687 }
688 }
689
690 return NULL;
691}
692
Luc Michele40e5202019-01-07 15:23:46 +0000693static CPUState *get_first_cpu_in_process(const GDBState *s,
694 GDBProcess *process)
695{
696 CPUState *cpu;
697
698 CPU_FOREACH(cpu) {
699 if (gdb_get_cpu_pid(s, cpu) == process->pid) {
700 return cpu;
701 }
702 }
703
704 return NULL;
705}
706
707static CPUState *gdb_next_cpu_in_process(const GDBState *s, CPUState *cpu)
708{
709 uint32_t pid = gdb_get_cpu_pid(s, cpu);
710 cpu = CPU_NEXT(cpu);
711
712 while (cpu) {
713 if (gdb_get_cpu_pid(s, cpu) == pid) {
714 break;
715 }
716
717 cpu = CPU_NEXT(cpu);
718 }
719
720 return cpu;
721}
722
Luc Michele40e5202019-01-07 15:23:46 +0000723/* Return the cpu following @cpu, while ignoring unattached processes. */
724static CPUState *gdb_next_attached_cpu(const GDBState *s, CPUState *cpu)
725{
726 cpu = CPU_NEXT(cpu);
727
728 while (cpu) {
729 if (gdb_get_cpu_process(s, cpu)->attached) {
730 break;
731 }
732
733 cpu = CPU_NEXT(cpu);
734 }
735
736 return cpu;
737}
738
739/* Return the first attached cpu */
740static CPUState *gdb_first_attached_cpu(const GDBState *s)
741{
742 CPUState *cpu = first_cpu;
743 GDBProcess *process = gdb_get_cpu_process(s, cpu);
744
745 if (!process->attached) {
746 return gdb_next_attached_cpu(s, cpu);
747 }
748
749 return cpu;
750}
751
Luc Michelab65eed2019-01-29 11:46:03 +0000752static CPUState *gdb_get_cpu(const GDBState *s, uint32_t pid, uint32_t tid)
753{
754 GDBProcess *process;
755 CPUState *cpu;
756
757 if (!pid && !tid) {
758 /* 0 means any process/thread, we take the first attached one */
759 return gdb_first_attached_cpu(s);
760 } else if (pid && !tid) {
761 /* any thread in a specific process */
762 process = gdb_get_process(s, pid);
763
764 if (process == NULL) {
765 return NULL;
766 }
767
768 if (!process->attached) {
769 return NULL;
770 }
771
772 return get_first_cpu_in_process(s, process);
773 } else {
774 /* a specific thread */
775 cpu = find_cpu(tid);
776
777 if (cpu == NULL) {
778 return NULL;
779 }
780
781 process = gdb_get_cpu_process(s, cpu);
782
783 if (pid && process->pid != pid) {
784 return NULL;
785 }
786
787 if (!process->attached) {
788 return NULL;
789 }
790
791 return cpu;
792 }
793}
794
Luc Michelc145eea2019-01-07 15:23:46 +0000795static const char *get_feature_xml(const GDBState *s, const char *p,
796 const char **newp, GDBProcess *process)
pbrook56aebc82008-10-11 17:55:29 +0000797{
pbrook56aebc82008-10-11 17:55:29 +0000798 size_t len;
799 int i;
800 const char *name;
Luc Michelc145eea2019-01-07 15:23:46 +0000801 CPUState *cpu = get_first_cpu_in_process(s, process);
802 CPUClass *cc = CPU_GET_CLASS(cpu);
pbrook56aebc82008-10-11 17:55:29 +0000803
804 len = 0;
805 while (p[len] && p[len] != ':')
806 len++;
807 *newp = p + len;
808
809 name = NULL;
810 if (strncmp(p, "target.xml", len) == 0) {
Luc Michelc145eea2019-01-07 15:23:46 +0000811 char *buf = process->target_xml;
812 const size_t buf_sz = sizeof(process->target_xml);
pbrook56aebc82008-10-11 17:55:29 +0000813
Luc Michelc145eea2019-01-07 15:23:46 +0000814 /* Generate the XML description for this CPU. */
815 if (!buf[0]) {
816 GDBRegisterState *r;
817
818 pstrcat(buf, buf_sz,
David Hildenbrandb3820e62015-12-03 13:14:41 +0100819 "<?xml version=\"1.0\"?>"
820 "<!DOCTYPE target SYSTEM \"gdb-target.dtd\">"
821 "<target>");
822 if (cc->gdb_arch_name) {
823 gchar *arch = cc->gdb_arch_name(cpu);
Luc Michelc145eea2019-01-07 15:23:46 +0000824 pstrcat(buf, buf_sz, "<architecture>");
825 pstrcat(buf, buf_sz, arch);
826 pstrcat(buf, buf_sz, "</architecture>");
David Hildenbrandb3820e62015-12-03 13:14:41 +0100827 g_free(arch);
828 }
Luc Michelc145eea2019-01-07 15:23:46 +0000829 pstrcat(buf, buf_sz, "<xi:include href=\"");
830 pstrcat(buf, buf_sz, cc->gdb_core_xml_file);
831 pstrcat(buf, buf_sz, "\"/>");
Andreas Färbereac8b352013-06-28 21:11:37 +0200832 for (r = cpu->gdb_regs; r; r = r->next) {
Luc Michelc145eea2019-01-07 15:23:46 +0000833 pstrcat(buf, buf_sz, "<xi:include href=\"");
834 pstrcat(buf, buf_sz, r->xml);
835 pstrcat(buf, buf_sz, "\"/>");
pbrook56aebc82008-10-11 17:55:29 +0000836 }
Luc Michelc145eea2019-01-07 15:23:46 +0000837 pstrcat(buf, buf_sz, "</target>");
pbrook56aebc82008-10-11 17:55:29 +0000838 }
Luc Michelc145eea2019-01-07 15:23:46 +0000839 return buf;
pbrook56aebc82008-10-11 17:55:29 +0000840 }
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100841 if (cc->gdb_get_dynamic_xml) {
Abdallah Bouassida200bf5b2018-05-18 17:48:07 +0100842 char *xmlname = g_strndup(p, len);
843 const char *xml = cc->gdb_get_dynamic_xml(cpu, xmlname);
844
845 g_free(xmlname);
846 if (xml) {
847 return xml;
848 }
849 }
pbrook56aebc82008-10-11 17:55:29 +0000850 for (i = 0; ; i++) {
851 name = xml_builtin[i][0];
852 if (!name || (strncmp(name, p, len) == 0 && strlen(name) == len))
853 break;
854 }
855 return name ? xml_builtin[i][1] : NULL;
856}
pbrook56aebc82008-10-11 17:55:29 +0000857
Andreas Färber385b9f02013-06-27 18:25:36 +0200858static int gdb_read_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000859{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200860 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200861 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000862 GDBRegisterState *r;
863
Andreas Färbera0e372f2013-06-28 23:18:47 +0200864 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200865 return cc->gdb_read_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200866 }
pbrook56aebc82008-10-11 17:55:29 +0000867
Andreas Färbereac8b352013-06-28 21:11:37 +0200868 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000869 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
870 return r->get_reg(env, mem_buf, reg - r->base_reg);
871 }
872 }
873 return 0;
874}
875
Andreas Färber385b9f02013-06-27 18:25:36 +0200876static int gdb_write_register(CPUState *cpu, uint8_t *mem_buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +0000877{
Andreas Färbera0e372f2013-06-28 23:18:47 +0200878 CPUClass *cc = CPU_GET_CLASS(cpu);
Andreas Färber385b9f02013-06-27 18:25:36 +0200879 CPUArchState *env = cpu->env_ptr;
pbrook56aebc82008-10-11 17:55:29 +0000880 GDBRegisterState *r;
881
Andreas Färbera0e372f2013-06-28 23:18:47 +0200882 if (reg < cc->gdb_num_core_regs) {
Andreas Färber5b50e792013-06-29 04:18:45 +0200883 return cc->gdb_write_register(cpu, mem_buf, reg);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200884 }
pbrook56aebc82008-10-11 17:55:29 +0000885
Andreas Färbereac8b352013-06-28 21:11:37 +0200886 for (r = cpu->gdb_regs; r; r = r->next) {
pbrook56aebc82008-10-11 17:55:29 +0000887 if (r->base_reg <= reg && reg < r->base_reg + r->num_regs) {
888 return r->set_reg(env, mem_buf, reg - r->base_reg);
889 }
890 }
891 return 0;
892}
893
894/* Register a supplemental set of CPU registers. If g_pos is nonzero it
895 specifies the first register number and these registers are included in
896 a standard "g" packet. Direction is relative to gdb, i.e. get_reg is
897 gdb reading a CPU register, and set_reg is gdb modifying a CPU register.
898 */
899
Andreas Färber22169d42013-06-28 21:27:39 +0200900void gdb_register_coprocessor(CPUState *cpu,
901 gdb_reg_cb get_reg, gdb_reg_cb set_reg,
902 int num_regs, const char *xml, int g_pos)
pbrook56aebc82008-10-11 17:55:29 +0000903{
904 GDBRegisterState *s;
905 GDBRegisterState **p;
pbrook56aebc82008-10-11 17:55:29 +0000906
Andreas Färbereac8b352013-06-28 21:11:37 +0200907 p = &cpu->gdb_regs;
pbrook56aebc82008-10-11 17:55:29 +0000908 while (*p) {
909 /* Check for duplicates. */
910 if (strcmp((*p)->xml, xml) == 0)
911 return;
912 p = &(*p)->next;
913 }
Stefan Weil9643c252011-10-18 22:25:38 +0200914
915 s = g_new0(GDBRegisterState, 1);
Andreas Färbera0e372f2013-06-28 23:18:47 +0200916 s->base_reg = cpu->gdb_num_regs;
Stefan Weil9643c252011-10-18 22:25:38 +0200917 s->num_regs = num_regs;
918 s->get_reg = get_reg;
919 s->set_reg = set_reg;
920 s->xml = xml;
921
pbrook56aebc82008-10-11 17:55:29 +0000922 /* Add to end of list. */
Andreas Färbera0e372f2013-06-28 23:18:47 +0200923 cpu->gdb_num_regs += num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000924 *p = s;
925 if (g_pos) {
926 if (g_pos != s->base_reg) {
Ziyue Yang7ae6c572017-01-18 16:03:29 +0800927 error_report("Error: Bad gdb register numbering for '%s', "
928 "expected %d got %d", xml, g_pos, s->base_reg);
Andreas Färber35143f02013-08-12 18:09:47 +0200929 } else {
930 cpu->gdb_num_g_regs = cpu->gdb_num_regs;
pbrook56aebc82008-10-11 17:55:29 +0000931 }
932 }
933}
934
aliguoria1d1bb32008-11-18 20:07:32 +0000935#ifndef CONFIG_USER_ONLY
Peter Maydell2472b6c2014-09-12 19:04:17 +0100936/* Translate GDB watchpoint type to a flags value for cpu_watchpoint_* */
937static inline int xlat_gdb_type(CPUState *cpu, int gdbtype)
938{
939 static const int xlat[] = {
940 [GDB_WATCHPOINT_WRITE] = BP_GDB | BP_MEM_WRITE,
941 [GDB_WATCHPOINT_READ] = BP_GDB | BP_MEM_READ,
942 [GDB_WATCHPOINT_ACCESS] = BP_GDB | BP_MEM_ACCESS,
943 };
944
945 CPUClass *cc = CPU_GET_CLASS(cpu);
946 int cputype = xlat[gdbtype];
947
948 if (cc->gdb_stop_before_watchpoint) {
949 cputype |= BP_STOP_BEFORE_ACCESS;
950 }
951 return cputype;
952}
aliguoria1d1bb32008-11-18 20:07:32 +0000953#endif
954
aliguori880a7572008-11-18 20:30:24 +0000955static int gdb_breakpoint_insert(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000956{
Andreas Färber182735e2013-05-29 22:29:20 +0200957 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000958 int err = 0;
959
Andreas Färber62278812013-06-27 17:12:06 +0200960 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200961 return kvm_insert_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200962 }
aliguorie22a25c2009-03-12 20:12:48 +0000963
aliguoria1d1bb32008-11-18 20:07:32 +0000964 switch (type) {
965 case GDB_BREAKPOINT_SW:
966 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +0200967 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +0200968 err = cpu_breakpoint_insert(cpu, addr, BP_GDB, NULL);
969 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000970 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +0200971 }
aliguori880a7572008-11-18 20:30:24 +0000972 }
973 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000974#ifndef CONFIG_USER_ONLY
975 case GDB_WATCHPOINT_WRITE:
976 case GDB_WATCHPOINT_READ:
977 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +0200978 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +0100979 err = cpu_watchpoint_insert(cpu, addr, len,
980 xlat_gdb_type(cpu, type), NULL);
981 if (err) {
aliguori880a7572008-11-18 20:30:24 +0000982 break;
Peter Maydell2472b6c2014-09-12 19:04:17 +0100983 }
aliguori880a7572008-11-18 20:30:24 +0000984 }
985 return err;
aliguoria1d1bb32008-11-18 20:07:32 +0000986#endif
987 default:
988 return -ENOSYS;
989 }
990}
991
aliguori880a7572008-11-18 20:30:24 +0000992static int gdb_breakpoint_remove(target_ulong addr, target_ulong len, int type)
aliguoria1d1bb32008-11-18 20:07:32 +0000993{
Andreas Färber182735e2013-05-29 22:29:20 +0200994 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +0000995 int err = 0;
996
Andreas Färber62278812013-06-27 17:12:06 +0200997 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +0200998 return kvm_remove_breakpoint(gdbserver_state->c_cpu, addr, len, type);
Andreas Färber62278812013-06-27 17:12:06 +0200999 }
aliguorie22a25c2009-03-12 20:12:48 +00001000
aliguoria1d1bb32008-11-18 20:07:32 +00001001 switch (type) {
1002 case GDB_BREAKPOINT_SW:
1003 case GDB_BREAKPOINT_HW:
Andreas Färberbdc44642013-06-24 23:50:24 +02001004 CPU_FOREACH(cpu) {
Andreas Färberb3310ab2013-09-02 17:26:20 +02001005 err = cpu_breakpoint_remove(cpu, addr, BP_GDB);
1006 if (err) {
aliguori880a7572008-11-18 20:30:24 +00001007 break;
Andreas Färberb3310ab2013-09-02 17:26:20 +02001008 }
aliguori880a7572008-11-18 20:30:24 +00001009 }
1010 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001011#ifndef CONFIG_USER_ONLY
1012 case GDB_WATCHPOINT_WRITE:
1013 case GDB_WATCHPOINT_READ:
1014 case GDB_WATCHPOINT_ACCESS:
Andreas Färberbdc44642013-06-24 23:50:24 +02001015 CPU_FOREACH(cpu) {
Peter Maydell2472b6c2014-09-12 19:04:17 +01001016 err = cpu_watchpoint_remove(cpu, addr, len,
1017 xlat_gdb_type(cpu, type));
aliguori880a7572008-11-18 20:30:24 +00001018 if (err)
1019 break;
1020 }
1021 return err;
aliguoria1d1bb32008-11-18 20:07:32 +00001022#endif
1023 default:
1024 return -ENOSYS;
1025 }
1026}
1027
Luc Michel546f3c62019-01-07 15:23:46 +00001028static inline void gdb_cpu_breakpoint_remove_all(CPUState *cpu)
1029{
1030 cpu_breakpoint_remove_all(cpu, BP_GDB);
1031#ifndef CONFIG_USER_ONLY
1032 cpu_watchpoint_remove_all(cpu, BP_GDB);
1033#endif
1034}
1035
1036static void gdb_process_breakpoint_remove_all(const GDBState *s, GDBProcess *p)
1037{
1038 CPUState *cpu = get_first_cpu_in_process(s, p);
1039
1040 while (cpu) {
1041 gdb_cpu_breakpoint_remove_all(cpu);
1042 cpu = gdb_next_cpu_in_process(s, cpu);
1043 }
1044}
1045
aliguori880a7572008-11-18 20:30:24 +00001046static void gdb_breakpoint_remove_all(void)
aliguoria1d1bb32008-11-18 20:07:32 +00001047{
Andreas Färber182735e2013-05-29 22:29:20 +02001048 CPUState *cpu;
aliguori880a7572008-11-18 20:30:24 +00001049
aliguorie22a25c2009-03-12 20:12:48 +00001050 if (kvm_enabled()) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001051 kvm_remove_all_breakpoints(gdbserver_state->c_cpu);
aliguorie22a25c2009-03-12 20:12:48 +00001052 return;
1053 }
1054
Andreas Färberbdc44642013-06-24 23:50:24 +02001055 CPU_FOREACH(cpu) {
Luc Michel546f3c62019-01-07 15:23:46 +00001056 gdb_cpu_breakpoint_remove_all(cpu);
aliguori880a7572008-11-18 20:30:24 +00001057 }
aliguoria1d1bb32008-11-18 20:07:32 +00001058}
1059
aurel32fab9d282009-04-08 21:29:37 +00001060static void gdb_set_cpu_pc(GDBState *s, target_ulong pc)
1061{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001062 CPUState *cpu = s->c_cpu;
Andreas Färberf45748f2013-06-21 19:09:18 +02001063
1064 cpu_synchronize_state(cpu);
Peter Crosthwaite4a2b24e2015-06-23 20:19:21 -07001065 cpu_set_pc(cpu, pc);
aurel32fab9d282009-04-08 21:29:37 +00001066}
1067
Luc Michel1a227332019-01-07 15:23:45 +00001068static char *gdb_fmt_thread_id(const GDBState *s, CPUState *cpu,
1069 char *buf, size_t buf_size)
1070{
1071 if (s->multiprocess) {
1072 snprintf(buf, buf_size, "p%02x.%02x",
1073 gdb_get_cpu_pid(s, cpu), cpu_gdb_index(cpu));
1074 } else {
1075 snprintf(buf, buf_size, "%02x", cpu_gdb_index(cpu));
1076 }
1077
1078 return buf;
1079}
1080
Luc Michel7d8c87d2019-01-07 15:23:45 +00001081typedef enum GDBThreadIdKind {
1082 GDB_ONE_THREAD = 0,
1083 GDB_ALL_THREADS, /* One process, all threads */
1084 GDB_ALL_PROCESSES,
1085 GDB_READ_THREAD_ERR
1086} GDBThreadIdKind;
1087
1088static GDBThreadIdKind read_thread_id(const char *buf, const char **end_buf,
1089 uint32_t *pid, uint32_t *tid)
1090{
1091 unsigned long p, t;
1092 int ret;
1093
1094 if (*buf == 'p') {
1095 buf++;
1096 ret = qemu_strtoul(buf, &buf, 16, &p);
1097
1098 if (ret) {
1099 return GDB_READ_THREAD_ERR;
1100 }
1101
1102 /* Skip '.' */
1103 buf++;
1104 } else {
1105 p = 1;
1106 }
1107
1108 ret = qemu_strtoul(buf, &buf, 16, &t);
1109
1110 if (ret) {
1111 return GDB_READ_THREAD_ERR;
1112 }
1113
1114 *end_buf = buf;
1115
1116 if (p == -1) {
1117 return GDB_ALL_PROCESSES;
1118 }
1119
1120 if (pid) {
1121 *pid = p;
1122 }
1123
1124 if (t == -1) {
1125 return GDB_ALL_THREADS;
1126 }
1127
1128 if (tid) {
1129 *tid = t;
1130 }
1131
1132 return GDB_ONE_THREAD;
1133}
1134
Jan Kiszka4dabe742015-02-07 09:38:43 +01001135static int is_query_packet(const char *p, const char *query, char separator)
1136{
1137 unsigned int query_len = strlen(query);
1138
1139 return strncmp(p, query, query_len) == 0 &&
1140 (p[query_len] == '\0' || p[query_len] == separator);
1141}
1142
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001143/**
1144 * gdb_handle_vcont - Parses and handles a vCont packet.
1145 * returns -ENOTSUP if a command is unsupported, -EINVAL or -ERANGE if there is
1146 * a format error, 0 on success.
1147 */
1148static int gdb_handle_vcont(GDBState *s, const char *p)
1149{
Luc Michele40e5202019-01-07 15:23:46 +00001150 int res, signal = 0;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001151 char cur_action;
1152 char *newstates;
1153 unsigned long tmp;
Luc Michele40e5202019-01-07 15:23:46 +00001154 uint32_t pid, tid;
1155 GDBProcess *process;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001156 CPUState *cpu;
Luc Michelc99ef792019-03-26 12:53:26 +00001157 GDBThreadIdKind kind;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001158#ifdef CONFIG_USER_ONLY
1159 int max_cpus = 1; /* global variable max_cpus exists only in system mode */
1160
1161 CPU_FOREACH(cpu) {
1162 max_cpus = max_cpus <= cpu->cpu_index ? cpu->cpu_index + 1 : max_cpus;
1163 }
1164#endif
1165 /* uninitialised CPUs stay 0 */
1166 newstates = g_new0(char, max_cpus);
1167
1168 /* mark valid CPUs with 1 */
1169 CPU_FOREACH(cpu) {
1170 newstates[cpu->cpu_index] = 1;
1171 }
1172
1173 /*
1174 * res keeps track of what error we are returning, with -ENOTSUP meaning
1175 * that the command is unknown or unsupported, thus returning an empty
1176 * packet, while -EINVAL and -ERANGE cause an E22 packet, due to invalid,
1177 * or incorrect parameters passed.
1178 */
1179 res = 0;
1180 while (*p) {
1181 if (*p++ != ';') {
1182 res = -ENOTSUP;
1183 goto out;
1184 }
1185
1186 cur_action = *p++;
1187 if (cur_action == 'C' || cur_action == 'S') {
Peter Maydell95a5bef2017-07-20 17:31:30 +01001188 cur_action = qemu_tolower(cur_action);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001189 res = qemu_strtoul(p + 1, &p, 16, &tmp);
1190 if (res) {
1191 goto out;
1192 }
1193 signal = gdb_signal_to_target(tmp);
1194 } else if (cur_action != 'c' && cur_action != 's') {
1195 /* unknown/invalid/unsupported command */
1196 res = -ENOTSUP;
1197 goto out;
1198 }
Luc Michele40e5202019-01-07 15:23:46 +00001199
Luc Michelc99ef792019-03-26 12:53:26 +00001200 if (*p == '\0' || *p == ';') {
1201 /*
1202 * No thread specifier, action is on "all threads". The
1203 * specification is unclear regarding the process to act on. We
1204 * choose all processes.
1205 */
1206 kind = GDB_ALL_PROCESSES;
1207 } else if (*p++ == ':') {
1208 kind = read_thread_id(p, &p, &pid, &tid);
1209 } else {
Luc Michele40e5202019-01-07 15:23:46 +00001210 res = -ENOTSUP;
1211 goto out;
1212 }
1213
Luc Michelc99ef792019-03-26 12:53:26 +00001214 switch (kind) {
Luc Michele40e5202019-01-07 15:23:46 +00001215 case GDB_READ_THREAD_ERR:
1216 res = -EINVAL;
1217 goto out;
1218
1219 case GDB_ALL_PROCESSES:
1220 cpu = gdb_first_attached_cpu(s);
1221 while (cpu) {
1222 if (newstates[cpu->cpu_index] == 1) {
1223 newstates[cpu->cpu_index] = cur_action;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001224 }
Luc Michele40e5202019-01-07 15:23:46 +00001225
1226 cpu = gdb_next_attached_cpu(s, cpu);
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001227 }
Luc Michele40e5202019-01-07 15:23:46 +00001228 break;
1229
1230 case GDB_ALL_THREADS:
1231 process = gdb_get_process(s, pid);
1232
1233 if (!process->attached) {
1234 res = -EINVAL;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001235 goto out;
1236 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001237
Luc Michele40e5202019-01-07 15:23:46 +00001238 cpu = get_first_cpu_in_process(s, process);
1239 while (cpu) {
1240 if (newstates[cpu->cpu_index] == 1) {
1241 newstates[cpu->cpu_index] = cur_action;
1242 }
1243
1244 cpu = gdb_next_cpu_in_process(s, cpu);
1245 }
1246 break;
1247
1248 case GDB_ONE_THREAD:
1249 cpu = gdb_get_cpu(s, pid, tid);
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001250
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001251 /* invalid CPU/thread specified */
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001252 if (!cpu) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001253 res = -EINVAL;
1254 goto out;
1255 }
Alex Bennée5a6a1ad2017-07-12 11:52:16 +01001256
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001257 /* only use if no previous match occourred */
1258 if (newstates[cpu->cpu_index] == 1) {
1259 newstates[cpu->cpu_index] = cur_action;
1260 }
Luc Michele40e5202019-01-07 15:23:46 +00001261 break;
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001262 }
1263 }
1264 s->signal = signal;
1265 gdb_continue_partial(s, newstates);
1266
1267out:
1268 g_free(newstates);
1269
1270 return res;
1271}
1272
aliguori880a7572008-11-18 20:30:24 +00001273static int gdb_handle_packet(GDBState *s, const char *line_buf)
bellardb4608c02003-06-27 17:34:32 +00001274{
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001275 CPUState *cpu;
Luc Michelc145eea2019-01-07 15:23:46 +00001276 GDBProcess *process;
Andreas Färber5b24c642013-07-07 15:08:22 +02001277 CPUClass *cc;
bellardb4608c02003-06-27 17:34:32 +00001278 const char *p;
Luc Michel7d8c87d2019-01-07 15:23:45 +00001279 uint32_t pid, tid;
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001280 int ch, reg_size, type, res;
pbrook56aebc82008-10-11 17:55:29 +00001281 uint8_t mem_buf[MAX_PACKET_LENGTH];
Philippe Mathieu-Daudé90057742018-04-08 11:59:33 -03001282 char buf[sizeof(mem_buf) + 1 /* trailing NUL */];
Luc Michel1a227332019-01-07 15:23:45 +00001283 char thread_id[16];
pbrook56aebc82008-10-11 17:55:29 +00001284 uint8_t *registers;
bellard9d9754a2006-06-25 15:32:37 +00001285 target_ulong addr, len;
Luc Michel7d8c87d2019-01-07 15:23:45 +00001286 GDBThreadIdKind thread_kind;
ths3b46e622007-09-17 08:09:54 +00001287
Doug Gale5c9522b2017-12-02 20:30:37 -05001288 trace_gdbstub_io_command(line_buf);
Alex Bennée118e2262017-07-12 11:52:13 +01001289
bellard858693c2004-03-31 18:52:07 +00001290 p = line_buf;
1291 ch = *p++;
1292 switch(ch) {
Luc Michel53fd6552019-01-07 15:23:46 +00001293 case '!':
1294 put_packet(s, "OK");
1295 break;
bellard858693c2004-03-31 18:52:07 +00001296 case '?':
bellard1fddef42005-04-17 19:16:13 +00001297 /* TODO: Make this return the correct value for user-mode. */
Luc Michel1a227332019-01-07 15:23:45 +00001298 snprintf(buf, sizeof(buf), "T%02xthread:%s;", GDB_SIGNAL_TRAP,
1299 gdb_fmt_thread_id(s, s->c_cpu, thread_id, sizeof(thread_id)));
bellard858693c2004-03-31 18:52:07 +00001300 put_packet(s, buf);
edgar_igl7d03f822008-05-17 18:58:29 +00001301 /* Remove all the breakpoints when this query is issued,
1302 * because gdb is doing and initial connect and the state
1303 * should be cleaned up.
1304 */
aliguori880a7572008-11-18 20:30:24 +00001305 gdb_breakpoint_remove_all();
bellard858693c2004-03-31 18:52:07 +00001306 break;
1307 case 'c':
1308 if (*p != '\0') {
bellard9d9754a2006-06-25 15:32:37 +00001309 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001310 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001311 }
aurel32ca587a82008-12-18 22:44:13 +00001312 s->signal = 0;
edgar_iglba70a622008-03-14 06:10:42 +00001313 gdb_continue(s);
Doug Gale5c9522b2017-12-02 20:30:37 -05001314 return RS_IDLE;
edgar_igl1f487ee2008-05-17 22:20:53 +00001315 case 'C':
aurel32ca587a82008-12-18 22:44:13 +00001316 s->signal = gdb_signal_to_target (strtoul(p, (char **)&p, 16));
1317 if (s->signal == -1)
1318 s->signal = 0;
edgar_igl1f487ee2008-05-17 22:20:53 +00001319 gdb_continue(s);
1320 return RS_IDLE;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001321 case 'v':
1322 if (strncmp(p, "Cont", 4) == 0) {
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001323 p += 4;
1324 if (*p == '?') {
1325 put_packet(s, "vCont;c;C;s;S");
1326 break;
1327 }
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001328
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001329 res = gdb_handle_vcont(s, p);
1330
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001331 if (res) {
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001332 if ((res == -EINVAL) || (res == -ERANGE)) {
1333 put_packet(s, "E22");
1334 break;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001335 }
Claudio Imbrenda544177a2017-02-14 18:07:48 +01001336 goto unknown_command;
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001337 }
1338 break;
Luc Michel3f940dc2019-01-07 15:23:46 +00001339 } else if (strncmp(p, "Attach;", 7) == 0) {
1340 unsigned long pid;
1341
1342 p += 7;
1343
1344 if (qemu_strtoul(p, &p, 16, &pid)) {
1345 put_packet(s, "E22");
1346 break;
1347 }
1348
1349 process = gdb_get_process(s, pid);
1350
1351 if (process == NULL) {
1352 put_packet(s, "E22");
1353 break;
1354 }
1355
1356 cpu = get_first_cpu_in_process(s, process);
1357
1358 if (cpu == NULL) {
1359 /* Refuse to attach an empty process */
1360 put_packet(s, "E22");
1361 break;
1362 }
1363
1364 process->attached = true;
1365
1366 s->g_cpu = cpu;
1367 s->c_cpu = cpu;
1368
1369 snprintf(buf, sizeof(buf), "T%02xthread:%s;", GDB_SIGNAL_TRAP,
1370 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id)));
1371
1372 put_packet(s, buf);
1373 break;
Max Filippov45a4de22019-02-05 16:52:41 +00001374 } else if (strncmp(p, "Kill;", 5) == 0) {
1375 /* Kill the target */
Sandra Loosemore0f8b09b2019-02-15 09:56:41 +00001376 put_packet(s, "OK");
Max Filippov45a4de22019-02-05 16:52:41 +00001377 error_report("QEMU: Terminated via GDBstub");
1378 exit(0);
Jan Kiszkadd32aa12009-06-27 09:53:51 +02001379 } else {
1380 goto unknown_command;
1381 }
edgar_igl7d03f822008-05-17 18:58:29 +00001382 case 'k':
1383 /* Kill the target */
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001384 error_report("QEMU: Terminated via GDBstub");
edgar_igl7d03f822008-05-17 18:58:29 +00001385 exit(0);
1386 case 'D':
1387 /* Detach packet */
Luc Michel546f3c62019-01-07 15:23:46 +00001388 pid = 1;
1389
1390 if (s->multiprocess) {
1391 unsigned long lpid;
1392 if (*p != ';') {
1393 put_packet(s, "E22");
1394 break;
1395 }
1396
1397 if (qemu_strtoul(p + 1, &p, 16, &lpid)) {
1398 put_packet(s, "E22");
1399 break;
1400 }
1401
1402 pid = lpid;
1403 }
1404
1405 process = gdb_get_process(s, pid);
1406 gdb_process_breakpoint_remove_all(s, process);
1407 process->attached = false;
1408
1409 if (pid == gdb_get_cpu_pid(s, s->c_cpu)) {
1410 s->c_cpu = gdb_first_attached_cpu(s);
1411 }
1412
1413 if (pid == gdb_get_cpu_pid(s, s->g_cpu)) {
1414 s->g_cpu = gdb_first_attached_cpu(s);
1415 }
1416
1417 if (s->c_cpu == NULL) {
1418 /* No more process attached */
1419 gdb_syscall_mode = GDB_SYS_DISABLED;
1420 gdb_continue(s);
1421 }
edgar_igl7d03f822008-05-17 18:58:29 +00001422 put_packet(s, "OK");
1423 break;
bellard858693c2004-03-31 18:52:07 +00001424 case 's':
1425 if (*p != '\0') {
ths8fac5802007-07-12 10:05:07 +00001426 addr = strtoull(p, (char **)&p, 16);
aurel32fab9d282009-04-08 21:29:37 +00001427 gdb_set_cpu_pc(s, addr);
bellard858693c2004-03-31 18:52:07 +00001428 }
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001429 cpu_single_step(s->c_cpu, sstep_flags);
edgar_iglba70a622008-03-14 06:10:42 +00001430 gdb_continue(s);
Doug Gale5c9522b2017-12-02 20:30:37 -05001431 return RS_IDLE;
pbrooka2d1eba2007-01-28 03:10:55 +00001432 case 'F':
1433 {
1434 target_ulong ret;
1435 target_ulong err;
1436
1437 ret = strtoull(p, (char **)&p, 16);
1438 if (*p == ',') {
1439 p++;
1440 err = strtoull(p, (char **)&p, 16);
1441 } else {
1442 err = 0;
1443 }
1444 if (*p == ',')
1445 p++;
1446 type = *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001447 if (s->current_syscall_cb) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001448 s->current_syscall_cb(s->c_cpu, ret, err);
Meador Ingecdb432b2012-03-15 17:49:45 +00001449 s->current_syscall_cb = NULL;
1450 }
pbrooka2d1eba2007-01-28 03:10:55 +00001451 if (type == 'C') {
1452 put_packet(s, "T02");
1453 } else {
edgar_iglba70a622008-03-14 06:10:42 +00001454 gdb_continue(s);
pbrooka2d1eba2007-01-28 03:10:55 +00001455 }
1456 }
1457 break;
bellard858693c2004-03-31 18:52:07 +00001458 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001459 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001460 len = 0;
Andreas Färber35143f02013-08-12 18:09:47 +02001461 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001462 reg_size = gdb_read_register(s->g_cpu, mem_buf + len, addr);
pbrook56aebc82008-10-11 17:55:29 +00001463 len += reg_size;
1464 }
1465 memtohex(buf, mem_buf, len);
bellard858693c2004-03-31 18:52:07 +00001466 put_packet(s, buf);
1467 break;
1468 case 'G':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001469 cpu_synchronize_state(s->g_cpu);
pbrook56aebc82008-10-11 17:55:29 +00001470 registers = mem_buf;
bellard858693c2004-03-31 18:52:07 +00001471 len = strlen(p) / 2;
1472 hextomem((uint8_t *)registers, p, len);
Andreas Färber35143f02013-08-12 18:09:47 +02001473 for (addr = 0; addr < s->g_cpu->gdb_num_g_regs && len > 0; addr++) {
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001474 reg_size = gdb_write_register(s->g_cpu, registers, addr);
pbrook56aebc82008-10-11 17:55:29 +00001475 len -= reg_size;
1476 registers += reg_size;
1477 }
bellard858693c2004-03-31 18:52:07 +00001478 put_packet(s, "OK");
1479 break;
1480 case 'm':
bellard9d9754a2006-06-25 15:32:37 +00001481 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001482 if (*p == ',')
1483 p++;
bellard9d9754a2006-06-25 15:32:37 +00001484 len = strtoull(p, NULL, 16);
Kevin Wolf5accecb2015-10-13 09:38:50 +02001485
1486 /* memtohex() doubles the required space */
1487 if (len > MAX_PACKET_LENGTH / 2) {
1488 put_packet (s, "E22");
1489 break;
1490 }
1491
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001492 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len, false) != 0) {
bellard6f970bd2005-12-05 19:55:19 +00001493 put_packet (s, "E14");
1494 } else {
1495 memtohex(buf, mem_buf, len);
1496 put_packet(s, buf);
1497 }
bellard858693c2004-03-31 18:52:07 +00001498 break;
1499 case 'M':
bellard9d9754a2006-06-25 15:32:37 +00001500 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001501 if (*p == ',')
1502 p++;
bellard9d9754a2006-06-25 15:32:37 +00001503 len = strtoull(p, (char **)&p, 16);
bellardb328f872005-01-17 22:03:16 +00001504 if (*p == ':')
bellard858693c2004-03-31 18:52:07 +00001505 p++;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001506
1507 /* hextomem() reads 2*len bytes */
1508 if (len > strlen(p) / 2) {
1509 put_packet (s, "E22");
1510 break;
1511 }
bellard858693c2004-03-31 18:52:07 +00001512 hextomem(mem_buf, p, len);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001513 if (target_memory_rw_debug(s->g_cpu, addr, mem_buf, len,
Andreas Färberf3659ee2013-06-27 19:09:09 +02001514 true) != 0) {
bellard905f20b2005-04-26 21:09:55 +00001515 put_packet(s, "E14");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001516 } else {
bellard858693c2004-03-31 18:52:07 +00001517 put_packet(s, "OK");
Fabien Chouteau44520db2011-09-08 12:48:16 +02001518 }
bellard858693c2004-03-31 18:52:07 +00001519 break;
pbrook56aebc82008-10-11 17:55:29 +00001520 case 'p':
1521 /* Older gdb are really dumb, and don't use 'g' if 'p' is avaialable.
1522 This works, but can be very slow. Anything new enough to
1523 understand XML also knows how to use this properly. */
1524 if (!gdb_has_xml)
1525 goto unknown_command;
1526 addr = strtoull(p, (char **)&p, 16);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001527 reg_size = gdb_read_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001528 if (reg_size) {
1529 memtohex(buf, mem_buf, reg_size);
1530 put_packet(s, buf);
1531 } else {
1532 put_packet(s, "E14");
1533 }
1534 break;
1535 case 'P':
1536 if (!gdb_has_xml)
1537 goto unknown_command;
1538 addr = strtoull(p, (char **)&p, 16);
1539 if (*p == '=')
1540 p++;
1541 reg_size = strlen(p) / 2;
1542 hextomem(mem_buf, p, reg_size);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001543 gdb_write_register(s->g_cpu, mem_buf, addr);
pbrook56aebc82008-10-11 17:55:29 +00001544 put_packet(s, "OK");
1545 break;
bellard858693c2004-03-31 18:52:07 +00001546 case 'Z':
bellard858693c2004-03-31 18:52:07 +00001547 case 'z':
1548 type = strtoul(p, (char **)&p, 16);
1549 if (*p == ',')
1550 p++;
bellard9d9754a2006-06-25 15:32:37 +00001551 addr = strtoull(p, (char **)&p, 16);
bellard858693c2004-03-31 18:52:07 +00001552 if (*p == ',')
1553 p++;
bellard9d9754a2006-06-25 15:32:37 +00001554 len = strtoull(p, (char **)&p, 16);
aliguoria1d1bb32008-11-18 20:07:32 +00001555 if (ch == 'Z')
aliguori880a7572008-11-18 20:30:24 +00001556 res = gdb_breakpoint_insert(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001557 else
aliguori880a7572008-11-18 20:30:24 +00001558 res = gdb_breakpoint_remove(addr, len, type);
aliguoria1d1bb32008-11-18 20:07:32 +00001559 if (res >= 0)
1560 put_packet(s, "OK");
1561 else if (res == -ENOSYS)
pbrook0f459d12008-06-09 00:20:13 +00001562 put_packet(s, "");
aliguoria1d1bb32008-11-18 20:07:32 +00001563 else
1564 put_packet(s, "E22");
bellard858693c2004-03-31 18:52:07 +00001565 break;
aliguori880a7572008-11-18 20:30:24 +00001566 case 'H':
1567 type = *p++;
Luc Michel7d8c87d2019-01-07 15:23:45 +00001568
1569 thread_kind = read_thread_id(p, &p, &pid, &tid);
1570 if (thread_kind == GDB_READ_THREAD_ERR) {
1571 put_packet(s, "E22");
1572 break;
1573 }
1574
1575 if (thread_kind != GDB_ONE_THREAD) {
aliguori880a7572008-11-18 20:30:24 +00001576 put_packet(s, "OK");
1577 break;
1578 }
Luc Michel7d8c87d2019-01-07 15:23:45 +00001579 cpu = gdb_get_cpu(s, pid, tid);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001580 if (cpu == NULL) {
aliguori880a7572008-11-18 20:30:24 +00001581 put_packet(s, "E22");
1582 break;
1583 }
1584 switch (type) {
1585 case 'c':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001586 s->c_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001587 put_packet(s, "OK");
1588 break;
1589 case 'g':
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001590 s->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001591 put_packet(s, "OK");
1592 break;
1593 default:
1594 put_packet(s, "E22");
1595 break;
1596 }
1597 break;
1598 case 'T':
Luc Michel7d8c87d2019-01-07 15:23:45 +00001599 thread_kind = read_thread_id(p, &p, &pid, &tid);
1600 if (thread_kind == GDB_READ_THREAD_ERR) {
1601 put_packet(s, "E22");
1602 break;
1603 }
1604 cpu = gdb_get_cpu(s, pid, tid);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001605
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001606 if (cpu != NULL) {
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001607 put_packet(s, "OK");
1608 } else {
aliguori880a7572008-11-18 20:30:24 +00001609 put_packet(s, "E22");
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001610 }
aliguori880a7572008-11-18 20:30:24 +00001611 break;
pbrook978efd62006-06-17 18:30:42 +00001612 case 'q':
edgar_igl60897d32008-05-09 08:25:14 +00001613 case 'Q':
1614 /* parse any 'q' packets here */
1615 if (!strcmp(p,"qemu.sstepbits")) {
1616 /* Query Breakpoint bit definitions */
blueswir1363a37d2008-08-21 17:58:08 +00001617 snprintf(buf, sizeof(buf), "ENABLE=%x,NOIRQ=%x,NOTIMER=%x",
1618 SSTEP_ENABLE,
1619 SSTEP_NOIRQ,
1620 SSTEP_NOTIMER);
edgar_igl60897d32008-05-09 08:25:14 +00001621 put_packet(s, buf);
1622 break;
Jan Kiszka4dabe742015-02-07 09:38:43 +01001623 } else if (is_query_packet(p, "qemu.sstep", '=')) {
edgar_igl60897d32008-05-09 08:25:14 +00001624 /* Display or change the sstep_flags */
1625 p += 10;
1626 if (*p != '=') {
1627 /* Display current setting */
blueswir1363a37d2008-08-21 17:58:08 +00001628 snprintf(buf, sizeof(buf), "0x%x", sstep_flags);
edgar_igl60897d32008-05-09 08:25:14 +00001629 put_packet(s, buf);
1630 break;
1631 }
1632 p++;
1633 type = strtoul(p, (char **)&p, 16);
1634 sstep_flags = type;
1635 put_packet(s, "OK");
1636 break;
aliguori880a7572008-11-18 20:30:24 +00001637 } else if (strcmp(p,"C") == 0) {
Luc Michel8dbbe9a2019-01-07 15:23:46 +00001638 /*
1639 * "Current thread" remains vague in the spec, so always return
1640 * the first thread of the current process (gdb returns the
1641 * first thread).
1642 */
1643 cpu = get_first_cpu_in_process(s, gdb_get_cpu_process(s, s->g_cpu));
1644 snprintf(buf, sizeof(buf), "QC%s",
1645 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id)));
1646 put_packet(s, buf);
aliguori880a7572008-11-18 20:30:24 +00001647 break;
1648 } else if (strcmp(p,"fThreadInfo") == 0) {
Luc Michel7cf48f62019-01-07 15:23:46 +00001649 s->query_cpu = gdb_first_attached_cpu(s);
aliguori880a7572008-11-18 20:30:24 +00001650 goto report_cpuinfo;
1651 } else if (strcmp(p,"sThreadInfo") == 0) {
1652 report_cpuinfo:
1653 if (s->query_cpu) {
Luc Michel7cf48f62019-01-07 15:23:46 +00001654 snprintf(buf, sizeof(buf), "m%s",
1655 gdb_fmt_thread_id(s, s->query_cpu,
1656 thread_id, sizeof(thread_id)));
aliguori880a7572008-11-18 20:30:24 +00001657 put_packet(s, buf);
Luc Michel7cf48f62019-01-07 15:23:46 +00001658 s->query_cpu = gdb_next_attached_cpu(s, s->query_cpu);
aliguori880a7572008-11-18 20:30:24 +00001659 } else
1660 put_packet(s, "l");
1661 break;
1662 } else if (strncmp(p,"ThreadExtraInfo,", 16) == 0) {
Luc Michel7cf48f62019-01-07 15:23:46 +00001663 if (read_thread_id(p + 16, &p, &pid, &tid) == GDB_READ_THREAD_ERR) {
1664 put_packet(s, "E22");
1665 break;
1666 }
1667 cpu = gdb_get_cpu(s, pid, tid);
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001668 if (cpu != NULL) {
Andreas Färbercb446ec2013-05-01 14:24:52 +02001669 cpu_synchronize_state(cpu);
Luc Michel7cf48f62019-01-07 15:23:46 +00001670
1671 if (s->multiprocess && (s->process_num > 1)) {
1672 /* Print the CPU model and name in multiprocess mode */
1673 ObjectClass *oc = object_get_class(OBJECT(cpu));
1674 const char *cpu_model = object_class_get_name(oc);
1675 char *cpu_name =
1676 object_get_canonical_path_component(OBJECT(cpu));
1677 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
1678 "%s %s [%s]", cpu_model, cpu_name,
1679 cpu->halted ? "halted " : "running");
1680 g_free(cpu_name);
1681 } else {
1682 /* memtohex() doubles the required space */
1683 len = snprintf((char *)mem_buf, sizeof(buf) / 2,
1684 "CPU#%d [%s]", cpu->cpu_index,
1685 cpu->halted ? "halted " : "running");
1686 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001687 trace_gdbstub_op_extra_info((char *)mem_buf);
Nathan Froyd1e9fa732009-06-03 11:33:08 -07001688 memtohex(buf, mem_buf, len);
1689 put_packet(s, buf);
1690 }
aliguori880a7572008-11-18 20:30:24 +00001691 break;
edgar_igl60897d32008-05-09 08:25:14 +00001692 }
blueswir10b8a9882009-03-07 10:51:36 +00001693#ifdef CONFIG_USER_ONLY
Jan Kiszka070949f2015-02-07 09:38:42 +01001694 else if (strcmp(p, "Offsets") == 0) {
Andreas Färber0429a972013-08-26 18:14:44 +02001695 TaskState *ts = s->c_cpu->opaque;
pbrook978efd62006-06-17 18:30:42 +00001696
blueswir1363a37d2008-08-21 17:58:08 +00001697 snprintf(buf, sizeof(buf),
1698 "Text=" TARGET_ABI_FMT_lx ";Data=" TARGET_ABI_FMT_lx
1699 ";Bss=" TARGET_ABI_FMT_lx,
1700 ts->info->code_offset,
1701 ts->info->data_offset,
1702 ts->info->data_offset);
pbrook978efd62006-06-17 18:30:42 +00001703 put_packet(s, buf);
1704 break;
1705 }
blueswir10b8a9882009-03-07 10:51:36 +00001706#else /* !CONFIG_USER_ONLY */
aliguori8a34a0f2009-03-05 23:01:55 +00001707 else if (strncmp(p, "Rcmd,", 5) == 0) {
1708 int len = strlen(p + 5);
1709
1710 if ((len % 2) != 0) {
1711 put_packet(s, "E01");
1712 break;
1713 }
aliguori8a34a0f2009-03-05 23:01:55 +00001714 len = len / 2;
Kevin Wolf5accecb2015-10-13 09:38:50 +02001715 hextomem(mem_buf, p + 5, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001716 mem_buf[len++] = 0;
Anthony Liguorifa5efcc2011-08-15 11:17:30 -05001717 qemu_chr_be_write(s->mon_chr, mem_buf, len);
aliguori8a34a0f2009-03-05 23:01:55 +00001718 put_packet(s, "OK");
1719 break;
1720 }
blueswir10b8a9882009-03-07 10:51:36 +00001721#endif /* !CONFIG_USER_ONLY */
Jan Kiszka4dabe742015-02-07 09:38:43 +01001722 if (is_query_packet(p, "Supported", ':')) {
blueswir15b3715b2008-10-25 11:18:12 +00001723 snprintf(buf, sizeof(buf), "PacketSize=%x", MAX_PACKET_LENGTH);
Andreas Färber5b24c642013-07-07 15:08:22 +02001724 cc = CPU_GET_CLASS(first_cpu);
1725 if (cc->gdb_core_xml_file != NULL) {
1726 pstrcat(buf, sizeof(buf), ";qXfer:features:read+");
1727 }
Luc Michel364fce62019-01-07 15:23:46 +00001728
1729 if (strstr(p, "multiprocess+")) {
1730 s->multiprocess = true;
1731 }
1732 pstrcat(buf, sizeof(buf), ";multiprocess+");
1733
pbrook56aebc82008-10-11 17:55:29 +00001734 put_packet(s, buf);
1735 break;
1736 }
pbrook56aebc82008-10-11 17:55:29 +00001737 if (strncmp(p, "Xfer:features:read:", 19) == 0) {
1738 const char *xml;
1739 target_ulong total_len;
1740
Luc Michelc145eea2019-01-07 15:23:46 +00001741 process = gdb_get_cpu_process(s, s->g_cpu);
1742 cc = CPU_GET_CLASS(s->g_cpu);
Andreas Färber5b24c642013-07-07 15:08:22 +02001743 if (cc->gdb_core_xml_file == NULL) {
1744 goto unknown_command;
1745 }
1746
Andreas Färber5b50e792013-06-29 04:18:45 +02001747 gdb_has_xml = true;
pbrook56aebc82008-10-11 17:55:29 +00001748 p += 19;
Luc Michelc145eea2019-01-07 15:23:46 +00001749 xml = get_feature_xml(s, p, &p, process);
pbrook56aebc82008-10-11 17:55:29 +00001750 if (!xml) {
blueswir15b3715b2008-10-25 11:18:12 +00001751 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001752 put_packet(s, buf);
1753 break;
1754 }
1755
1756 if (*p == ':')
1757 p++;
1758 addr = strtoul(p, (char **)&p, 16);
1759 if (*p == ',')
1760 p++;
1761 len = strtoul(p, (char **)&p, 16);
1762
1763 total_len = strlen(xml);
1764 if (addr > total_len) {
blueswir15b3715b2008-10-25 11:18:12 +00001765 snprintf(buf, sizeof(buf), "E00");
pbrook56aebc82008-10-11 17:55:29 +00001766 put_packet(s, buf);
1767 break;
1768 }
1769 if (len > (MAX_PACKET_LENGTH - 5) / 2)
1770 len = (MAX_PACKET_LENGTH - 5) / 2;
1771 if (len < total_len - addr) {
1772 buf[0] = 'm';
1773 len = memtox(buf + 1, xml + addr, len);
1774 } else {
1775 buf[0] = 'l';
1776 len = memtox(buf + 1, xml + addr, total_len - addr);
1777 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001778 put_packet_binary(s, buf, len + 1, true);
pbrook56aebc82008-10-11 17:55:29 +00001779 break;
1780 }
Jan Kiszkaa3919382015-02-07 09:38:44 +01001781 if (is_query_packet(p, "Attached", ':')) {
1782 put_packet(s, GDB_ATTACHED);
1783 break;
1784 }
pbrook56aebc82008-10-11 17:55:29 +00001785 /* Unrecognised 'q' command. */
1786 goto unknown_command;
1787
bellard858693c2004-03-31 18:52:07 +00001788 default:
pbrook56aebc82008-10-11 17:55:29 +00001789 unknown_command:
bellard858693c2004-03-31 18:52:07 +00001790 /* put empty packet */
1791 buf[0] = '\0';
1792 put_packet(s, buf);
1793 break;
1794 }
1795 return RS_IDLE;
1796}
1797
Andreas Färber64f6b342013-05-27 02:06:09 +02001798void gdb_set_stop_cpu(CPUState *cpu)
aliguori880a7572008-11-18 20:30:24 +00001799{
Luc Michel160d8582019-01-07 15:23:46 +00001800 GDBProcess *p = gdb_get_cpu_process(gdbserver_state, cpu);
1801
1802 if (!p->attached) {
1803 /*
1804 * Having a stop CPU corresponding to a process that is not attached
1805 * confuses GDB. So we ignore the request.
1806 */
1807 return;
1808 }
1809
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001810 gdbserver_state->c_cpu = cpu;
1811 gdbserver_state->g_cpu = cpu;
aliguori880a7572008-11-18 20:30:24 +00001812}
1813
bellard1fddef42005-04-17 19:16:13 +00001814#ifndef CONFIG_USER_ONLY
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001815static void gdb_vm_state_change(void *opaque, int running, RunState state)
bellard858693c2004-03-31 18:52:07 +00001816{
aliguori880a7572008-11-18 20:30:24 +00001817 GDBState *s = gdbserver_state;
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001818 CPUState *cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00001819 char buf[256];
Luc Michel95567c22019-01-07 15:23:46 +00001820 char thread_id[16];
aliguorid6fc1b32008-11-18 19:55:44 +00001821 const char *type;
bellard858693c2004-03-31 18:52:07 +00001822 int ret;
1823
Meador Ingecdb432b2012-03-15 17:49:45 +00001824 if (running || s->state == RS_INACTIVE) {
1825 return;
1826 }
1827 /* Is there a GDB syscall waiting to be sent? */
1828 if (s->current_syscall_cb) {
1829 put_packet(s, s->syscall_buf);
pbrooka2d1eba2007-01-28 03:10:55 +00001830 return;
Jan Kiszkae07bbac2011-02-09 16:29:40 +01001831 }
Luc Michel95567c22019-01-07 15:23:46 +00001832
1833 if (cpu == NULL) {
1834 /* No process attached */
1835 return;
1836 }
1837
1838 gdb_fmt_thread_id(s, cpu, thread_id, sizeof(thread_id));
1839
Luiz Capitulino1dfb4dd2011-07-29 14:26:33 -03001840 switch (state) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001841 case RUN_STATE_DEBUG:
Andreas Färberff4700b2013-08-26 18:23:18 +02001842 if (cpu->watchpoint_hit) {
1843 switch (cpu->watchpoint_hit->flags & BP_MEM_ACCESS) {
aliguoria1d1bb32008-11-18 20:07:32 +00001844 case BP_MEM_READ:
aliguorid6fc1b32008-11-18 19:55:44 +00001845 type = "r";
1846 break;
aliguoria1d1bb32008-11-18 20:07:32 +00001847 case BP_MEM_ACCESS:
aliguorid6fc1b32008-11-18 19:55:44 +00001848 type = "a";
1849 break;
1850 default:
1851 type = "";
1852 break;
1853 }
Doug Gale5c9522b2017-12-02 20:30:37 -05001854 trace_gdbstub_hit_watchpoint(type, cpu_gdb_index(cpu),
1855 (target_ulong)cpu->watchpoint_hit->vaddr);
aliguori880a7572008-11-18 20:30:24 +00001856 snprintf(buf, sizeof(buf),
Luc Michel95567c22019-01-07 15:23:46 +00001857 "T%02xthread:%s;%swatch:" TARGET_FMT_lx ";",
1858 GDB_SIGNAL_TRAP, thread_id, type,
Andreas Färberff4700b2013-08-26 18:23:18 +02001859 (target_ulong)cpu->watchpoint_hit->vaddr);
1860 cpu->watchpoint_hit = NULL;
Jan Kiszka425189a2011-03-22 11:02:09 +01001861 goto send_packet;
Doug Gale5c9522b2017-12-02 20:30:37 -05001862 } else {
1863 trace_gdbstub_hit_break();
pbrook6658ffb2007-03-16 23:58:11 +00001864 }
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07001865 tb_flush(cpu);
aurel32ca587a82008-12-18 22:44:13 +00001866 ret = GDB_SIGNAL_TRAP;
Jan Kiszka425189a2011-03-22 11:02:09 +01001867 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001868 case RUN_STATE_PAUSED:
Doug Gale5c9522b2017-12-02 20:30:37 -05001869 trace_gdbstub_hit_paused();
aliguori9781e042009-01-22 17:15:29 +00001870 ret = GDB_SIGNAL_INT;
Jan Kiszka425189a2011-03-22 11:02:09 +01001871 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001872 case RUN_STATE_SHUTDOWN:
Doug Gale5c9522b2017-12-02 20:30:37 -05001873 trace_gdbstub_hit_shutdown();
Jan Kiszka425189a2011-03-22 11:02:09 +01001874 ret = GDB_SIGNAL_QUIT;
1875 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001876 case RUN_STATE_IO_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05001877 trace_gdbstub_hit_io_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01001878 ret = GDB_SIGNAL_IO;
1879 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001880 case RUN_STATE_WATCHDOG:
Doug Gale5c9522b2017-12-02 20:30:37 -05001881 trace_gdbstub_hit_watchdog();
Jan Kiszka425189a2011-03-22 11:02:09 +01001882 ret = GDB_SIGNAL_ALRM;
1883 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001884 case RUN_STATE_INTERNAL_ERROR:
Doug Gale5c9522b2017-12-02 20:30:37 -05001885 trace_gdbstub_hit_internal_error();
Jan Kiszka425189a2011-03-22 11:02:09 +01001886 ret = GDB_SIGNAL_ABRT;
1887 break;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001888 case RUN_STATE_SAVE_VM:
1889 case RUN_STATE_RESTORE_VM:
Jan Kiszka425189a2011-03-22 11:02:09 +01001890 return;
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001891 case RUN_STATE_FINISH_MIGRATE:
Jan Kiszka425189a2011-03-22 11:02:09 +01001892 ret = GDB_SIGNAL_XCPU;
1893 break;
1894 default:
Doug Gale5c9522b2017-12-02 20:30:37 -05001895 trace_gdbstub_hit_unknown(state);
Jan Kiszka425189a2011-03-22 11:02:09 +01001896 ret = GDB_SIGNAL_UNKNOWN;
1897 break;
bellardbbeb7b52006-04-23 18:42:15 +00001898 }
Jan Kiszka226d0072015-07-24 18:52:31 +02001899 gdb_set_stop_cpu(cpu);
Luc Michel95567c22019-01-07 15:23:46 +00001900 snprintf(buf, sizeof(buf), "T%02xthread:%s;", ret, thread_id);
Jan Kiszka425189a2011-03-22 11:02:09 +01001901
1902send_packet:
bellard858693c2004-03-31 18:52:07 +00001903 put_packet(s, buf);
Jan Kiszka425189a2011-03-22 11:02:09 +01001904
1905 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02001906 cpu_single_step(cpu, 0);
bellard858693c2004-03-31 18:52:07 +00001907}
bellard1fddef42005-04-17 19:16:13 +00001908#endif
bellard858693c2004-03-31 18:52:07 +00001909
pbrooka2d1eba2007-01-28 03:10:55 +00001910/* Send a gdb syscall request.
1911 This accepts limited printf-style format specifiers, specifically:
pbrooka87295e2007-05-26 15:09:38 +00001912 %x - target_ulong argument printed in hex.
1913 %lx - 64-bit argument printed in hex.
1914 %s - string pointer (target_ulong) and length (int) pair. */
Peter Maydell19239b32015-09-07 10:39:27 +01001915void gdb_do_syscallv(gdb_syscall_complete_cb cb, const char *fmt, va_list va)
pbrooka2d1eba2007-01-28 03:10:55 +00001916{
pbrooka2d1eba2007-01-28 03:10:55 +00001917 char *p;
Meador Ingecdb432b2012-03-15 17:49:45 +00001918 char *p_end;
pbrooka2d1eba2007-01-28 03:10:55 +00001919 target_ulong addr;
pbrooka87295e2007-05-26 15:09:38 +00001920 uint64_t i64;
pbrooka2d1eba2007-01-28 03:10:55 +00001921 GDBState *s;
1922
aliguori880a7572008-11-18 20:30:24 +00001923 s = gdbserver_state;
pbrooka2d1eba2007-01-28 03:10:55 +00001924 if (!s)
1925 return;
Meador Ingecdb432b2012-03-15 17:49:45 +00001926 s->current_syscall_cb = cb;
pbrooka2d1eba2007-01-28 03:10:55 +00001927#ifndef CONFIG_USER_ONLY
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03001928 vm_stop(RUN_STATE_DEBUG);
pbrooka2d1eba2007-01-28 03:10:55 +00001929#endif
Meador Ingecdb432b2012-03-15 17:49:45 +00001930 p = s->syscall_buf;
1931 p_end = &s->syscall_buf[sizeof(s->syscall_buf)];
pbrooka2d1eba2007-01-28 03:10:55 +00001932 *(p++) = 'F';
1933 while (*fmt) {
1934 if (*fmt == '%') {
1935 fmt++;
1936 switch (*fmt++) {
1937 case 'x':
1938 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001939 p += snprintf(p, p_end - p, TARGET_FMT_lx, addr);
pbrooka2d1eba2007-01-28 03:10:55 +00001940 break;
pbrooka87295e2007-05-26 15:09:38 +00001941 case 'l':
1942 if (*(fmt++) != 'x')
1943 goto bad_format;
1944 i64 = va_arg(va, uint64_t);
Meador Ingecdb432b2012-03-15 17:49:45 +00001945 p += snprintf(p, p_end - p, "%" PRIx64, i64);
pbrooka87295e2007-05-26 15:09:38 +00001946 break;
pbrooka2d1eba2007-01-28 03:10:55 +00001947 case 's':
1948 addr = va_arg(va, target_ulong);
Meador Ingecdb432b2012-03-15 17:49:45 +00001949 p += snprintf(p, p_end - p, TARGET_FMT_lx "/%x",
blueswir1363a37d2008-08-21 17:58:08 +00001950 addr, va_arg(va, int));
pbrooka2d1eba2007-01-28 03:10:55 +00001951 break;
1952 default:
pbrooka87295e2007-05-26 15:09:38 +00001953 bad_format:
Ziyue Yang7ae6c572017-01-18 16:03:29 +08001954 error_report("gdbstub: Bad syscall format string '%s'",
1955 fmt - 1);
pbrooka2d1eba2007-01-28 03:10:55 +00001956 break;
1957 }
1958 } else {
1959 *(p++) = *(fmt++);
1960 }
1961 }
pbrook8a93e022007-08-06 13:19:15 +00001962 *p = 0;
pbrooka2d1eba2007-01-28 03:10:55 +00001963#ifdef CONFIG_USER_ONLY
Meador Ingecdb432b2012-03-15 17:49:45 +00001964 put_packet(s, s->syscall_buf);
Peter Maydell4f710862018-05-15 19:19:58 +01001965 /* Return control to gdb for it to process the syscall request.
1966 * Since the protocol requires that gdb hands control back to us
1967 * using a "here are the results" F packet, we don't need to check
1968 * gdb_handlesig's return value (which is the signal to deliver if
1969 * execution was resumed via a continue packet).
1970 */
Andreas Färber2e0f2cf2013-06-27 19:19:39 +02001971 gdb_handlesig(s->c_cpu, 0);
pbrooka2d1eba2007-01-28 03:10:55 +00001972#else
Meador Ingecdb432b2012-03-15 17:49:45 +00001973 /* In this case wait to send the syscall packet until notification that
1974 the CPU has stopped. This must be done because if the packet is sent
1975 now the reply from the syscall request could be received while the CPU
1976 is still in the running state, which can cause packets to be dropped
1977 and state transition 'T' packets to be sent while the syscall is still
1978 being processed. */
Paolo Bonzini9102ded2015-08-18 06:52:09 -07001979 qemu_cpu_kick(s->c_cpu);
pbrooka2d1eba2007-01-28 03:10:55 +00001980#endif
1981}
1982
Peter Maydell19239b32015-09-07 10:39:27 +01001983void gdb_do_syscall(gdb_syscall_complete_cb cb, const char *fmt, ...)
1984{
1985 va_list va;
1986
1987 va_start(va, fmt);
1988 gdb_do_syscallv(cb, fmt, va);
1989 va_end(va);
1990}
1991
Markus Armbruster33c846e2019-05-14 20:03:09 +02001992static void gdb_read_byte(GDBState *s, uint8_t ch)
bellard858693c2004-03-31 18:52:07 +00001993{
ths60fe76f2007-12-16 03:02:09 +00001994 uint8_t reply;
bellard858693c2004-03-31 18:52:07 +00001995
bellard1fddef42005-04-17 19:16:13 +00001996#ifndef CONFIG_USER_ONLY
pbrook4046d912007-01-28 01:53:16 +00001997 if (s->last_packet_len) {
1998 /* Waiting for a response to the last packet. If we see the start
1999 of a new command then abandon the previous response. */
2000 if (ch == '-') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002001 trace_gdbstub_err_got_nack();
thsffe8ab82007-12-16 03:16:05 +00002002 put_buffer(s, (uint8_t *)s->last_packet, s->last_packet_len);
Alex Bennée118e2262017-07-12 11:52:13 +01002003 } else if (ch == '+') {
Doug Gale5c9522b2017-12-02 20:30:37 -05002004 trace_gdbstub_io_got_ack();
Alex Bennée118e2262017-07-12 11:52:13 +01002005 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002006 trace_gdbstub_io_got_unexpected(ch);
pbrook4046d912007-01-28 01:53:16 +00002007 }
Alex Bennée118e2262017-07-12 11:52:13 +01002008
pbrook4046d912007-01-28 01:53:16 +00002009 if (ch == '+' || ch == '$')
2010 s->last_packet_len = 0;
2011 if (ch != '$')
2012 return;
2013 }
Luiz Capitulino13548692011-07-29 15:36:43 -03002014 if (runstate_is_running()) {
bellard858693c2004-03-31 18:52:07 +00002015 /* when the CPU is running, we cannot do anything except stop
2016 it when receiving a char */
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002017 vm_stop(RUN_STATE_PAUSED);
ths5fafdf22007-09-16 21:08:06 +00002018 } else
bellard1fddef42005-04-17 19:16:13 +00002019#endif
bellard41625032005-04-24 10:07:11 +00002020 {
bellard858693c2004-03-31 18:52:07 +00002021 switch(s->state) {
2022 case RS_IDLE:
2023 if (ch == '$') {
Doug Gale4bf43122017-05-01 12:22:10 -04002024 /* start of command packet */
bellard858693c2004-03-31 18:52:07 +00002025 s->line_buf_index = 0;
Doug Gale4bf43122017-05-01 12:22:10 -04002026 s->line_sum = 0;
bellard858693c2004-03-31 18:52:07 +00002027 s->state = RS_GETLINE;
Doug Gale4bf43122017-05-01 12:22:10 -04002028 } else {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002029 trace_gdbstub_err_garbage(ch);
bellard4c3a88a2003-07-26 12:06:08 +00002030 }
2031 break;
bellard858693c2004-03-31 18:52:07 +00002032 case RS_GETLINE:
Doug Gale4bf43122017-05-01 12:22:10 -04002033 if (ch == '}') {
2034 /* start escape sequence */
2035 s->state = RS_GETLINE_ESC;
2036 s->line_sum += ch;
2037 } else if (ch == '*') {
2038 /* start run length encoding sequence */
2039 s->state = RS_GETLINE_RLE;
2040 s->line_sum += ch;
2041 } else if (ch == '#') {
2042 /* end of command, start of checksum*/
2043 s->state = RS_CHKSUM1;
bellard858693c2004-03-31 18:52:07 +00002044 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002045 trace_gdbstub_err_overrun();
bellard858693c2004-03-31 18:52:07 +00002046 s->state = RS_IDLE;
2047 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002048 /* unescaped command character */
2049 s->line_buf[s->line_buf_index++] = ch;
2050 s->line_sum += ch;
2051 }
2052 break;
2053 case RS_GETLINE_ESC:
2054 if (ch == '#') {
2055 /* unexpected end of command in escape sequence */
2056 s->state = RS_CHKSUM1;
2057 } else if (s->line_buf_index >= sizeof(s->line_buf) - 1) {
2058 /* command buffer overrun */
Doug Gale5c9522b2017-12-02 20:30:37 -05002059 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002060 s->state = RS_IDLE;
2061 } else {
2062 /* parse escaped character and leave escape state */
2063 s->line_buf[s->line_buf_index++] = ch ^ 0x20;
2064 s->line_sum += ch;
2065 s->state = RS_GETLINE;
2066 }
2067 break;
2068 case RS_GETLINE_RLE:
Markus Armbruster046aba12019-05-14 20:03:08 +02002069 /*
2070 * Run-length encoding is explained in "Debugging with GDB /
2071 * Appendix E GDB Remote Serial Protocol / Overview".
2072 */
2073 if (ch < ' ' || ch == '#' || ch == '$' || ch > 126) {
Doug Gale4bf43122017-05-01 12:22:10 -04002074 /* invalid RLE count encoding */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002075 trace_gdbstub_err_invalid_repeat(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002076 s->state = RS_GETLINE;
2077 } else {
2078 /* decode repeat length */
Markus Armbruster33c846e2019-05-14 20:03:09 +02002079 int repeat = ch - ' ' + 3;
Doug Gale4bf43122017-05-01 12:22:10 -04002080 if (s->line_buf_index + repeat >= sizeof(s->line_buf) - 1) {
2081 /* that many repeats would overrun the command buffer */
Doug Gale5c9522b2017-12-02 20:30:37 -05002082 trace_gdbstub_err_overrun();
Doug Gale4bf43122017-05-01 12:22:10 -04002083 s->state = RS_IDLE;
2084 } else if (s->line_buf_index < 1) {
2085 /* got a repeat but we have nothing to repeat */
Doug Gale5c9522b2017-12-02 20:30:37 -05002086 trace_gdbstub_err_invalid_rle();
Doug Gale4bf43122017-05-01 12:22:10 -04002087 s->state = RS_GETLINE;
2088 } else {
2089 /* repeat the last character */
2090 memset(s->line_buf + s->line_buf_index,
2091 s->line_buf[s->line_buf_index - 1], repeat);
2092 s->line_buf_index += repeat;
2093 s->line_sum += ch;
2094 s->state = RS_GETLINE;
2095 }
bellard858693c2004-03-31 18:52:07 +00002096 }
2097 break;
2098 case RS_CHKSUM1:
Doug Gale4bf43122017-05-01 12:22:10 -04002099 /* get high hex digit of checksum */
2100 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002101 trace_gdbstub_err_checksum_invalid(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002102 s->state = RS_GETLINE;
2103 break;
2104 }
bellard858693c2004-03-31 18:52:07 +00002105 s->line_buf[s->line_buf_index] = '\0';
2106 s->line_csum = fromhex(ch) << 4;
2107 s->state = RS_CHKSUM2;
2108 break;
2109 case RS_CHKSUM2:
Doug Gale4bf43122017-05-01 12:22:10 -04002110 /* get low hex digit of checksum */
2111 if (!isxdigit(ch)) {
Markus Armbruster33c846e2019-05-14 20:03:09 +02002112 trace_gdbstub_err_checksum_invalid(ch);
Doug Gale4bf43122017-05-01 12:22:10 -04002113 s->state = RS_GETLINE;
2114 break;
bellard858693c2004-03-31 18:52:07 +00002115 }
Doug Gale4bf43122017-05-01 12:22:10 -04002116 s->line_csum |= fromhex(ch);
2117
2118 if (s->line_csum != (s->line_sum & 0xff)) {
Doug Gale5c9522b2017-12-02 20:30:37 -05002119 trace_gdbstub_err_checksum_incorrect(s->line_sum, s->line_csum);
Doug Gale4bf43122017-05-01 12:22:10 -04002120 /* send NAK reply */
ths60fe76f2007-12-16 03:02:09 +00002121 reply = '-';
2122 put_buffer(s, &reply, 1);
bellard858693c2004-03-31 18:52:07 +00002123 s->state = RS_IDLE;
2124 } else {
Doug Gale4bf43122017-05-01 12:22:10 -04002125 /* send ACK reply */
ths60fe76f2007-12-16 03:02:09 +00002126 reply = '+';
2127 put_buffer(s, &reply, 1);
aliguori880a7572008-11-18 20:30:24 +00002128 s->state = gdb_handle_packet(s, s->line_buf);
bellard858693c2004-03-31 18:52:07 +00002129 }
bellardb4608c02003-06-27 17:34:32 +00002130 break;
pbrooka2d1eba2007-01-28 03:10:55 +00002131 default:
2132 abort();
bellardb4608c02003-06-27 17:34:32 +00002133 }
2134 }
bellard858693c2004-03-31 18:52:07 +00002135}
2136
Paul Brook0e1c9c52010-06-16 13:03:51 +01002137/* Tell the remote gdb that the process has exited. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002138void gdb_exit(CPUArchState *env, int code)
Paul Brook0e1c9c52010-06-16 13:03:51 +01002139{
2140 GDBState *s;
2141 char buf[4];
2142
2143 s = gdbserver_state;
2144 if (!s) {
2145 return;
2146 }
2147#ifdef CONFIG_USER_ONLY
2148 if (gdbserver_fd < 0 || s->fd < 0) {
2149 return;
2150 }
2151#endif
2152
Doug Gale5c9522b2017-12-02 20:30:37 -05002153 trace_gdbstub_op_exiting((uint8_t)code);
2154
Paul Brook0e1c9c52010-06-16 13:03:51 +01002155 snprintf(buf, sizeof(buf), "W%02x", (uint8_t)code);
2156 put_packet(s, buf);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002157
2158#ifndef CONFIG_USER_ONLY
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002159 qemu_chr_fe_deinit(&s->chr, true);
Fabien Chouteaue2af15b2011-01-13 12:46:57 +01002160#endif
Paul Brook0e1c9c52010-06-16 13:03:51 +01002161}
2162
Luc Michel8f468632019-01-07 15:23:45 +00002163/*
2164 * Create the process that will contain all the "orphan" CPUs (that are not
2165 * part of a CPU cluster). Note that if this process contains no CPUs, it won't
2166 * be attachable and thus will be invisible to the user.
2167 */
2168static void create_default_process(GDBState *s)
2169{
2170 GDBProcess *process;
2171 int max_pid = 0;
2172
2173 if (s->process_num) {
2174 max_pid = s->processes[s->process_num - 1].pid;
2175 }
2176
2177 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2178 process = &s->processes[s->process_num - 1];
2179
2180 /* We need an available PID slot for this process */
2181 assert(max_pid < UINT32_MAX);
2182
2183 process->pid = max_pid + 1;
2184 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00002185 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00002186}
2187
bellard1fddef42005-04-17 19:16:13 +00002188#ifdef CONFIG_USER_ONLY
2189int
Andreas Färberdb6b81d2013-06-27 19:49:31 +02002190gdb_handlesig(CPUState *cpu, int sig)
bellard1fddef42005-04-17 19:16:13 +00002191{
Andreas Färber5ca666c2013-06-24 19:20:57 +02002192 GDBState *s;
2193 char buf[256];
2194 int n;
bellard1fddef42005-04-17 19:16:13 +00002195
Andreas Färber5ca666c2013-06-24 19:20:57 +02002196 s = gdbserver_state;
2197 if (gdbserver_fd < 0 || s->fd < 0) {
2198 return sig;
bellard1fddef42005-04-17 19:16:13 +00002199 }
2200
Andreas Färber5ca666c2013-06-24 19:20:57 +02002201 /* disable single step if it was enabled */
Andreas Färber3825b282013-06-24 18:41:06 +02002202 cpu_single_step(cpu, 0);
Peter Crosthwaitebbd77c12015-06-23 19:31:15 -07002203 tb_flush(cpu);
bellard1fddef42005-04-17 19:16:13 +00002204
Andreas Färber5ca666c2013-06-24 19:20:57 +02002205 if (sig != 0) {
2206 snprintf(buf, sizeof(buf), "S%02x", target_signal_to_gdb(sig));
2207 put_packet(s, buf);
2208 }
2209 /* put_packet() might have detected that the peer terminated the
2210 connection. */
2211 if (s->fd < 0) {
2212 return sig;
2213 }
2214
2215 sig = 0;
2216 s->state = RS_IDLE;
2217 s->running_state = 0;
2218 while (s->running_state == 0) {
2219 n = read(s->fd, buf, 256);
2220 if (n > 0) {
2221 int i;
2222
2223 for (i = 0; i < n; i++) {
2224 gdb_read_byte(s, buf[i]);
2225 }
Peter Wu5819e3e2016-06-05 16:35:48 +02002226 } else {
Andreas Färber5ca666c2013-06-24 19:20:57 +02002227 /* XXX: Connection closed. Should probably wait for another
2228 connection before continuing. */
Peter Wu5819e3e2016-06-05 16:35:48 +02002229 if (n == 0) {
2230 close(s->fd);
2231 }
2232 s->fd = -1;
Andreas Färber5ca666c2013-06-24 19:20:57 +02002233 return sig;
bellard1fddef42005-04-17 19:16:13 +00002234 }
Andreas Färber5ca666c2013-06-24 19:20:57 +02002235 }
2236 sig = s->signal;
2237 s->signal = 0;
2238 return sig;
bellard1fddef42005-04-17 19:16:13 +00002239}
bellarde9009672005-04-26 20:42:36 +00002240
aurel32ca587a82008-12-18 22:44:13 +00002241/* Tell the remote gdb that the process has exited due to SIG. */
Andreas Färber9349b4f2012-03-14 01:38:32 +01002242void gdb_signalled(CPUArchState *env, int sig)
aurel32ca587a82008-12-18 22:44:13 +00002243{
Andreas Färber5ca666c2013-06-24 19:20:57 +02002244 GDBState *s;
2245 char buf[4];
aurel32ca587a82008-12-18 22:44:13 +00002246
Andreas Färber5ca666c2013-06-24 19:20:57 +02002247 s = gdbserver_state;
2248 if (gdbserver_fd < 0 || s->fd < 0) {
2249 return;
2250 }
aurel32ca587a82008-12-18 22:44:13 +00002251
Andreas Färber5ca666c2013-06-24 19:20:57 +02002252 snprintf(buf, sizeof(buf), "X%02x", target_signal_to_gdb(sig));
2253 put_packet(s, buf);
aurel32ca587a82008-12-18 22:44:13 +00002254}
bellard1fddef42005-04-17 19:16:13 +00002255
Peter Maydell2f652222018-05-14 18:30:44 +01002256static bool gdb_accept(void)
bellard858693c2004-03-31 18:52:07 +00002257{
2258 GDBState *s;
2259 struct sockaddr_in sockaddr;
2260 socklen_t len;
MORITA Kazutakabf1c8522013-02-22 12:39:50 +09002261 int fd;
bellard858693c2004-03-31 18:52:07 +00002262
2263 for(;;) {
2264 len = sizeof(sockaddr);
2265 fd = accept(gdbserver_fd, (struct sockaddr *)&sockaddr, &len);
2266 if (fd < 0 && errno != EINTR) {
2267 perror("accept");
Peter Maydell2f652222018-05-14 18:30:44 +01002268 return false;
bellard858693c2004-03-31 18:52:07 +00002269 } else if (fd >= 0) {
Peter Maydellf5bdd782018-05-14 18:30:43 +01002270 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00002271 break;
2272 }
2273 }
2274
2275 /* set short latency */
Peter Maydell2f652222018-05-14 18:30:44 +01002276 if (socket_set_nodelay(fd)) {
2277 perror("setsockopt");
Philippe Mathieu-Daudéead75d82018-05-24 19:34:58 -03002278 close(fd);
Peter Maydell2f652222018-05-14 18:30:44 +01002279 return false;
2280 }
ths3b46e622007-09-17 08:09:54 +00002281
Anthony Liguori7267c092011-08-20 22:09:37 -05002282 s = g_malloc0(sizeof(GDBState));
Luc Michel8f468632019-01-07 15:23:45 +00002283 create_default_process(s);
Luc Michel970ed902019-01-07 15:23:46 +00002284 s->processes[0].attached = true;
2285 s->c_cpu = gdb_first_attached_cpu(s);
2286 s->g_cpu = s->c_cpu;
bellard858693c2004-03-31 18:52:07 +00002287 s->fd = fd;
Andreas Färber5b50e792013-06-29 04:18:45 +02002288 gdb_has_xml = false;
bellard858693c2004-03-31 18:52:07 +00002289
aliguori880a7572008-11-18 20:30:24 +00002290 gdbserver_state = s;
Peter Maydell2f652222018-05-14 18:30:44 +01002291 return true;
bellard858693c2004-03-31 18:52:07 +00002292}
2293
2294static int gdbserver_open(int port)
2295{
2296 struct sockaddr_in sockaddr;
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02002297 int fd, ret;
bellard858693c2004-03-31 18:52:07 +00002298
2299 fd = socket(PF_INET, SOCK_STREAM, 0);
2300 if (fd < 0) {
2301 perror("socket");
2302 return -1;
2303 }
Peter Maydellf5bdd782018-05-14 18:30:43 +01002304 qemu_set_cloexec(fd);
bellard858693c2004-03-31 18:52:07 +00002305
Sebastian Ottlik6669ca12013-10-02 12:23:13 +02002306 socket_set_fast_reuse(fd);
bellard858693c2004-03-31 18:52:07 +00002307
2308 sockaddr.sin_family = AF_INET;
2309 sockaddr.sin_port = htons(port);
2310 sockaddr.sin_addr.s_addr = 0;
2311 ret = bind(fd, (struct sockaddr *)&sockaddr, sizeof(sockaddr));
2312 if (ret < 0) {
2313 perror("bind");
Peter Maydellbb161722011-12-24 23:37:24 +00002314 close(fd);
bellard858693c2004-03-31 18:52:07 +00002315 return -1;
2316 }
Peter Wu96165b92016-05-04 11:32:17 +02002317 ret = listen(fd, 1);
bellard858693c2004-03-31 18:52:07 +00002318 if (ret < 0) {
2319 perror("listen");
Peter Maydellbb161722011-12-24 23:37:24 +00002320 close(fd);
bellard858693c2004-03-31 18:52:07 +00002321 return -1;
2322 }
bellard858693c2004-03-31 18:52:07 +00002323 return fd;
2324}
2325
2326int gdbserver_start(int port)
2327{
2328 gdbserver_fd = gdbserver_open(port);
2329 if (gdbserver_fd < 0)
2330 return -1;
2331 /* accept connections */
Peter Maydell2f652222018-05-14 18:30:44 +01002332 if (!gdb_accept()) {
2333 close(gdbserver_fd);
2334 gdbserver_fd = -1;
2335 return -1;
2336 }
bellardb4608c02003-06-27 17:34:32 +00002337 return 0;
2338}
aurel322b1319c2008-12-18 22:44:04 +00002339
2340/* Disable gdb stub for child processes. */
Peter Crosthwaitef7ec7f72015-06-23 19:31:16 -07002341void gdbserver_fork(CPUState *cpu)
aurel322b1319c2008-12-18 22:44:04 +00002342{
2343 GDBState *s = gdbserver_state;
Andreas Färber75a34032013-09-02 16:57:02 +02002344
2345 if (gdbserver_fd < 0 || s->fd < 0) {
2346 return;
2347 }
aurel322b1319c2008-12-18 22:44:04 +00002348 close(s->fd);
2349 s->fd = -1;
Andreas Färberb3310ab2013-09-02 17:26:20 +02002350 cpu_breakpoint_remove_all(cpu, BP_GDB);
Andreas Färber75a34032013-09-02 16:57:02 +02002351 cpu_watchpoint_remove_all(cpu, BP_GDB);
aurel322b1319c2008-12-18 22:44:04 +00002352}
pbrook4046d912007-01-28 01:53:16 +00002353#else
thsaa1f17c2007-07-11 22:48:58 +00002354static int gdb_chr_can_receive(void *opaque)
pbrook4046d912007-01-28 01:53:16 +00002355{
pbrook56aebc82008-10-11 17:55:29 +00002356 /* We can handle an arbitrarily large amount of data.
2357 Pick the maximum packet size, which is as good as anything. */
2358 return MAX_PACKET_LENGTH;
pbrook4046d912007-01-28 01:53:16 +00002359}
2360
thsaa1f17c2007-07-11 22:48:58 +00002361static void gdb_chr_receive(void *opaque, const uint8_t *buf, int size)
pbrook4046d912007-01-28 01:53:16 +00002362{
pbrook4046d912007-01-28 01:53:16 +00002363 int i;
2364
2365 for (i = 0; i < size; i++) {
aliguori880a7572008-11-18 20:30:24 +00002366 gdb_read_byte(gdbserver_state, buf[i]);
pbrook4046d912007-01-28 01:53:16 +00002367 }
2368}
2369
2370static void gdb_chr_event(void *opaque, int event)
2371{
Luc Michel970ed902019-01-07 15:23:46 +00002372 int i;
2373 GDBState *s = (GDBState *) opaque;
2374
pbrook4046d912007-01-28 01:53:16 +00002375 switch (event) {
Amit Shahb6b8df52009-10-07 18:31:16 +05302376 case CHR_EVENT_OPENED:
Luc Michel970ed902019-01-07 15:23:46 +00002377 /* Start with first process attached, others detached */
2378 for (i = 0; i < s->process_num; i++) {
2379 s->processes[i].attached = !i;
2380 }
2381
2382 s->c_cpu = gdb_first_attached_cpu(s);
2383 s->g_cpu = s->c_cpu;
2384
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002385 vm_stop(RUN_STATE_PAUSED);
Andreas Färber5b50e792013-06-29 04:18:45 +02002386 gdb_has_xml = false;
pbrook4046d912007-01-28 01:53:16 +00002387 break;
2388 default:
2389 break;
2390 }
2391}
2392
aliguori8a34a0f2009-03-05 23:01:55 +00002393static void gdb_monitor_output(GDBState *s, const char *msg, int len)
2394{
2395 char buf[MAX_PACKET_LENGTH];
2396
2397 buf[0] = 'O';
2398 if (len > (MAX_PACKET_LENGTH/2) - 1)
2399 len = (MAX_PACKET_LENGTH/2) - 1;
2400 memtohex(buf + 1, (uint8_t *)msg, len);
2401 put_packet(s, buf);
2402}
2403
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03002404static int gdb_monitor_write(Chardev *chr, const uint8_t *buf, int len)
aliguori8a34a0f2009-03-05 23:01:55 +00002405{
2406 const char *p = (const char *)buf;
2407 int max_sz;
2408
2409 max_sz = (sizeof(gdbserver_state->last_packet) - 2) / 2;
2410 for (;;) {
2411 if (len <= max_sz) {
2412 gdb_monitor_output(gdbserver_state, p, len);
2413 break;
2414 }
2415 gdb_monitor_output(gdbserver_state, p, max_sz);
2416 p += max_sz;
2417 len -= max_sz;
2418 }
2419 return len;
2420}
2421
aliguori59030a82009-04-05 18:43:41 +00002422#ifndef _WIN32
2423static void gdb_sigterm_handler(int signal)
2424{
Luiz Capitulino13548692011-07-29 15:36:43 -03002425 if (runstate_is_running()) {
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002426 vm_stop(RUN_STATE_PAUSED);
Jan Kiszkae07bbac2011-02-09 16:29:40 +01002427 }
aliguori59030a82009-04-05 18:43:41 +00002428}
2429#endif
2430
Marc-André Lureau777357d2016-12-07 18:39:10 +03002431static void gdb_monitor_open(Chardev *chr, ChardevBackend *backend,
2432 bool *be_opened, Error **errp)
2433{
2434 *be_opened = false;
2435}
2436
2437static void char_gdb_class_init(ObjectClass *oc, void *data)
2438{
2439 ChardevClass *cc = CHARDEV_CLASS(oc);
2440
2441 cc->internal = true;
2442 cc->open = gdb_monitor_open;
2443 cc->chr_write = gdb_monitor_write;
2444}
2445
2446#define TYPE_CHARDEV_GDB "chardev-gdb"
2447
2448static const TypeInfo char_gdb_type_info = {
2449 .name = TYPE_CHARDEV_GDB,
2450 .parent = TYPE_CHARDEV,
2451 .class_init = char_gdb_class_init,
2452};
2453
Luc Michel8f468632019-01-07 15:23:45 +00002454static int find_cpu_clusters(Object *child, void *opaque)
2455{
2456 if (object_dynamic_cast(child, TYPE_CPU_CLUSTER)) {
2457 GDBState *s = (GDBState *) opaque;
2458 CPUClusterState *cluster = CPU_CLUSTER(child);
2459 GDBProcess *process;
2460
2461 s->processes = g_renew(GDBProcess, s->processes, ++s->process_num);
2462
2463 process = &s->processes[s->process_num - 1];
2464
2465 /*
2466 * GDB process IDs -1 and 0 are reserved. To avoid subtle errors at
2467 * runtime, we enforce here that the machine does not use a cluster ID
2468 * that would lead to PID 0.
2469 */
2470 assert(cluster->cluster_id != UINT32_MAX);
2471 process->pid = cluster->cluster_id + 1;
2472 process->attached = false;
Luc Michelc145eea2019-01-07 15:23:46 +00002473 process->target_xml[0] = '\0';
Luc Michel8f468632019-01-07 15:23:45 +00002474
2475 return 0;
2476 }
2477
2478 return object_child_foreach(child, find_cpu_clusters, opaque);
2479}
2480
2481static int pid_order(const void *a, const void *b)
2482{
2483 GDBProcess *pa = (GDBProcess *) a;
2484 GDBProcess *pb = (GDBProcess *) b;
2485
2486 if (pa->pid < pb->pid) {
2487 return -1;
2488 } else if (pa->pid > pb->pid) {
2489 return 1;
2490 } else {
2491 return 0;
2492 }
2493}
2494
2495static void create_processes(GDBState *s)
2496{
2497 object_child_foreach(object_get_root(), find_cpu_clusters, s);
2498
2499 if (s->processes) {
2500 /* Sort by PID */
2501 qsort(s->processes, s->process_num, sizeof(s->processes[0]), pid_order);
2502 }
2503
2504 create_default_process(s);
2505}
2506
2507static void cleanup_processes(GDBState *s)
2508{
2509 g_free(s->processes);
2510 s->process_num = 0;
2511 s->processes = NULL;
2512}
2513
aliguori59030a82009-04-05 18:43:41 +00002514int gdbserver_start(const char *device)
pbrook4046d912007-01-28 01:53:16 +00002515{
Doug Gale5c9522b2017-12-02 20:30:37 -05002516 trace_gdbstub_op_start(device);
2517
pbrook4046d912007-01-28 01:53:16 +00002518 GDBState *s;
aliguori59030a82009-04-05 18:43:41 +00002519 char gdbstub_device_name[128];
Marc-André Lureau0ec7b3e2016-12-07 16:20:22 +03002520 Chardev *chr = NULL;
2521 Chardev *mon_chr;
pbrook4046d912007-01-28 01:53:16 +00002522
Ziyue Yang508b4ec2017-01-18 16:02:41 +08002523 if (!first_cpu) {
2524 error_report("gdbstub: meaningless to attach gdb to a "
2525 "machine without any CPU.");
2526 return -1;
2527 }
2528
aliguori59030a82009-04-05 18:43:41 +00002529 if (!device)
2530 return -1;
2531 if (strcmp(device, "none") != 0) {
2532 if (strstart(device, "tcp:", NULL)) {
2533 /* enforce required TCP attributes */
2534 snprintf(gdbstub_device_name, sizeof(gdbstub_device_name),
2535 "%s,nowait,nodelay,server", device);
2536 device = gdbstub_device_name;
aliguori36556b22009-03-28 18:05:53 +00002537 }
aliguori59030a82009-04-05 18:43:41 +00002538#ifndef _WIN32
2539 else if (strcmp(device, "stdio") == 0) {
2540 struct sigaction act;
pbrookcfc34752007-02-22 01:48:01 +00002541
aliguori59030a82009-04-05 18:43:41 +00002542 memset(&act, 0, sizeof(act));
2543 act.sa_handler = gdb_sigterm_handler;
2544 sigaction(SIGINT, &act, NULL);
2545 }
2546#endif
Marc-André Lureau95e30b22018-08-22 19:19:42 +02002547 /*
2548 * FIXME: it's a bit weird to allow using a mux chardev here
2549 * and implicitly setup a monitor. We may want to break this.
2550 */
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01002551 chr = qemu_chr_new_noreplay("gdb", device, true, NULL);
aliguori36556b22009-03-28 18:05:53 +00002552 if (!chr)
2553 return -1;
pbrookcfc34752007-02-22 01:48:01 +00002554 }
2555
aliguori36556b22009-03-28 18:05:53 +00002556 s = gdbserver_state;
2557 if (!s) {
Anthony Liguori7267c092011-08-20 22:09:37 -05002558 s = g_malloc0(sizeof(GDBState));
aliguori36556b22009-03-28 18:05:53 +00002559 gdbserver_state = s;
pbrook4046d912007-01-28 01:53:16 +00002560
aliguori36556b22009-03-28 18:05:53 +00002561 qemu_add_vm_change_state_handler(gdb_vm_state_change, NULL);
2562
2563 /* Initialize a monitor terminal for gdb */
Marc-André Lureau777357d2016-12-07 18:39:10 +03002564 mon_chr = qemu_chardev_new(NULL, TYPE_CHARDEV_GDB,
Paolo Bonzini4ad6f6c2019-02-13 14:18:13 +01002565 NULL, NULL, &error_abort);
aliguori36556b22009-03-28 18:05:53 +00002566 monitor_init(mon_chr, 0);
2567 } else {
Marc-André Lureau1ce26102017-01-27 00:49:13 +04002568 qemu_chr_fe_deinit(&s->chr, true);
aliguori36556b22009-03-28 18:05:53 +00002569 mon_chr = s->mon_chr;
Luc Michel8f468632019-01-07 15:23:45 +00002570 cleanup_processes(s);
aliguori36556b22009-03-28 18:05:53 +00002571 memset(s, 0, sizeof(GDBState));
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002572 s->mon_chr = mon_chr;
aliguori36556b22009-03-28 18:05:53 +00002573 }
Luc Michel8f468632019-01-07 15:23:45 +00002574
2575 create_processes(s);
2576
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002577 if (chr) {
2578 qemu_chr_fe_init(&s->chr, chr, &error_abort);
Marc-André Lureau5345fdb2016-10-22 12:52:55 +03002579 qemu_chr_fe_set_handlers(&s->chr, gdb_chr_can_receive, gdb_chr_receive,
Luc Michel970ed902019-01-07 15:23:46 +00002580 gdb_chr_event, NULL, s, NULL, true);
Marc-André Lureau32a6ebe2016-10-22 12:52:52 +03002581 }
aliguori36556b22009-03-28 18:05:53 +00002582 s->state = chr ? RS_IDLE : RS_INACTIVE;
2583 s->mon_chr = mon_chr;
Meador Ingecdb432b2012-03-15 17:49:45 +00002584 s->current_syscall_cb = NULL;
aliguori8a34a0f2009-03-05 23:01:55 +00002585
pbrook4046d912007-01-28 01:53:16 +00002586 return 0;
2587}
Marc-André Lureau777357d2016-12-07 18:39:10 +03002588
KONRAD Frederic1bb982b2018-03-20 10:39:33 +01002589void gdbserver_cleanup(void)
2590{
2591 if (gdbserver_state) {
2592 put_packet(gdbserver_state, "W00");
2593 }
2594}
2595
Marc-André Lureau777357d2016-12-07 18:39:10 +03002596static void register_types(void)
2597{
2598 type_register_static(&char_gdb_type_info);
2599}
2600
2601type_init(register_types);
pbrook4046d912007-01-28 01:53:16 +00002602#endif